]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs2.patch
comedi disabled on ppc (doesn't build)
[packages/kernel.git] / kernel-aufs2.patch
1 diff --git a/Documentation/ABI/testing/debugfs-aufs b/Documentation/ABI/testing/debugfs-aufs
2 new file mode 100644
3 index 0000000..4110b94
4 --- /dev/null
5 +++ b/Documentation/ABI/testing/debugfs-aufs
6 @@ -0,0 +1,40 @@
7 +What:          /debug/aufs/si_<id>/
8 +Date:          March 2009
9 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
10 +Description:
11 +               Under /debug/aufs, a directory named si_<id> is created
12 +               per aufs mount, where <id> is a unique id generated
13 +               internally.
14 +
15 +What:          /debug/aufs/si_<id>/xib
16 +Date:          March 2009
17 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
18 +Description:
19 +               It shows the consumed blocks by xib (External Inode Number
20 +               Bitmap), its block size and file size.
21 +               When the aufs mount option 'noxino' is specified, it
22 +               will be empty. About XINO files, see
23 +               Documentation/filesystems/aufs/aufs.5 in detail.
24 +
25 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
26 +Date:          March 2009
27 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
28 +Description:
29 +               It shows the consumed blocks by xino (External Inode Number
30 +               Translation Table), its link count, block size and file
31 +               size.
32 +               When the aufs mount option 'noxino' is specified, it
33 +               will be empty. About XINO files, see
34 +               Documentation/filesystems/aufs/aufs.5 in detail.
35 +
36 +What:          /debug/aufs/si_<id>/xigen
37 +Date:          March 2009
38 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
39 +Description:
40 +               It shows the consumed blocks by xigen (External Inode
41 +               Generation Table), its block size and file size.
42 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
43 +               be created.
44 +               When the aufs mount option 'noxino' is specified, it
45 +               will be empty. About XINO files, see
46 +               Documentation/filesystems/aufs/aufs.5 in detail.
47 diff --git a/Documentation/ABI/testing/sysfs-aufs b/Documentation/ABI/testing/sysfs-aufs
48 new file mode 100644
49 index 0000000..ca49330
50 --- /dev/null
51 +++ b/Documentation/ABI/testing/sysfs-aufs
52 @@ -0,0 +1,25 @@
53 +What:          /sys/fs/aufs/si_<id>/
54 +Date:          March 2009
55 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
56 +Description:
57 +               Under /sys/fs/aufs, a directory named si_<id> is created
58 +               per aufs mount, where <id> is a unique id generated
59 +               internally.
60 +
61 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
62 +Date:          March 2009
63 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
64 +Description:
65 +               It shows the abolute path of a member directory (which
66 +               is called branch) in aufs, and its permission.
67 +
68 +What:          /sys/fs/aufs/si_<id>/xi_path
69 +Date:          March 2009
70 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
71 +Description:
72 +               It shows the abolute path of XINO (External Inode Number
73 +               Bitmap, Translation Table and Generation Table) file
74 +               even if it is the default path.
75 +               When the aufs mount option 'noxino' is specified, it
76 +               will be empty. About XINO files, see
77 +               Documentation/filesystems/aufs/aufs.5 in detail.
78 diff --git a/Documentation/filesystems/aufs/README b/Documentation/filesystems/aufs/README
79 new file mode 100644
80 index 0000000..ed68e31
81 --- /dev/null
82 +++ b/Documentation/filesystems/aufs/README
83 @@ -0,0 +1,256 @@
84 +
85 +Aufs2 -- advanced multi layered unification filesystem version 2
86 +http://aufs.sf.net
87 +Junjiro R. Okajima
88 +
89 +
90 +0. Introduction
91 +----------------------------------------
92 +In the early days, aufs was entirely re-designed and re-implemented
93 +Unionfs Version 1.x series. After many original ideas, approaches,
94 +improvements and implementations, it becomes totally different from
95 +Unionfs while keeping the basic features.
96 +Recently, Unionfs Version 2.x series begin taking some of the same
97 +approaches to aufs1's.
98 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
99 +University and his team.
100 +
101 +This version of AUFS, aufs2 has several purposes.
102 +- to be reviewed easily and widely.
103 +- to make the source files simpler and smaller by dropping several
104 +  original features.
105 +
106 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
107 +According to Christoph Hellwig, linux rejects all union-type filesystems
108 +but UnionMount.
109 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
110 +
111 +Through this work, I found some bad things in aufs1 source code and
112 +fixed them. Some of the dropped features will be reverted in the future,
113 +but not all I'm afraid.
114 +Aufs2 supports linux-2.6.27 and later. If you want older kernel version
115 +support, try aufs1 from CVS on SourceForge.
116 +
117 +
118 +1. Features
119 +----------------------------------------
120 +- unite several directories into a single virtual filesystem. The member
121 +  directory is called as a branch.
122 +- you can specify the permission flags to the branch, which are 'readonly',
123 +  'readwrite' and 'whiteout-able.'
124 +- by upper writable branch, internal copyup and whiteout, files/dirs on
125 +  readonly branch are modifiable logically.
126 +- dynamic branch manipulation, add, del.
127 +- etc...
128 +
129 +Also there are many enhancements in aufs1, such as:
130 +- keep inode number by external inode number table
131 +- keep the timestamps of file/dir in internal copyup operation
132 +- seekable directory, supporting NFS readdir.
133 +- support mmap(2) including /proc/PID/exe symlink, without page-copy
134 +- whiteout is hardlinked in order to reduce the consumption of inodes
135 +  on branch
136 +- do not copyup, nor create a whiteout when it is unnecessary
137 +- revert a single systemcall when an error occurs in aufs
138 +- remount interface instead of ioctl
139 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
140 +- loopback mounted filesystem as a branch
141 +- kernel thread for removing the dir who has a plenty of whiteouts
142 +- support copyup sparse file (a file which has a 'hole' in it)
143 +- default permission flags for branches
144 +- selectable permission flags for ro branch, whether whiteout can
145 +  exist or not
146 +- export via NFS.
147 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
148 +- support multiple writable branches, some policies to select one
149 +  among multiple writable branches.
150 +- a new semantics for link(2) and rename(2) to support multiple
151 +  writable branches.
152 +- no glibc changes are required.
153 +- pseudo hardlink (hardlink over branches)
154 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
155 +  including NFS or remote filesystem branch.
156 +- and more...
157 +
158 +Currently these features are dropped temporary from this version, aufs2.
159 +See design/08plan.txt in detail.
160 +- test only the highest one for the directory permission (dirperm1)
161 +- show whiteout mode (shwh)
162 +- copyup on open (coo=)
163 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
164 +  (robr)
165 +- statistics of aufs thread (/sys/fs/aufs/stat)
166 +- delegation mode (dlgt)
167 +  a delegation of the internal branch access to support task I/O
168 +  accounting, which also supports Linux Security Modules (LSM) mainly
169 +  for Suse AppArmor.
170 +- intent.open/create (file open in a single lookup)
171 +
172 +Features or just an idea in the future (see also design/*.txt),
173 +- reorder the branch index without del/re-add.
174 +- permanent xino files for NFSD
175 +- an option for refreshing the opened files after add/del branches
176 +- 'move' policy for copy-up between two writable branches, after
177 +  checking free space.
178 +- O_DIRECT
179 +- light version, without branch manipulation. (unnecessary?)
180 +- copyup in userspace
181 +- inotify in userspace
182 +- readv/writev
183 +- xattr, acl
184 +
185 +
186 +2. Download
187 +----------------------------------------
188 +Kindly one of aufs user, the Center for Scientific Computing and Free
189 +Software (C3SL), Federal University of Parana offered me a public GIT
190 +tree space.
191 +
192 +There are three GIT trees, aufs2-2.6, aufs2-standalone and aufs2-util.
193 +While the aufs2-util is always necessary, you need either of aufs2-2.6
194 +or aufs2-standalone.
195 +
196 +The aufs2-2.6 tree includes the whole linux-2.6 GIT tree,
197 +git://git.kernel.org/.../torvalds/linux-2.6.git.
198 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
199 +build aufs2 as an externel kernel module.
200 +If you already have linux-2.6 GIT tree, you may want to pull and merge
201 +the "aufs2" branch from this tree.
202 +
203 +On the other hand, the aufs2-standalone tree has only aufs2 source files
204 +and a necessary patch, and you can select CONFIG_AUFS_FS=m. In other
205 +words, the aufs2-standalone tree is generated from aufs2-2.6 tree by,
206 +- extract new files and modifications.
207 +- generate a single patch file from modifications.
208 +- generate a ChangeLog file from git-log.
209 +- commit the files newly and no log messages. this is not git-pull.
210 +
211 +Both of aufs2-2.6 and aufs2-standalone trees have a branch whose name is
212 +in form of "aufs2-xx" where "xx" represents the linux kernel version,
213 +"linux-2.6.xx".
214 +
215 +o aufs2-2.6 tree
216 +$ git clone --reference /your/linux-2.6/git/tree \
217 +       http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-2.6.git \
218 +       aufs2-2.6.git
219 +- if you don't have linux-2.6 GIT tree, then remove "--reference ..."
220 +$ cd aufs2-2.6.git
221 +$ git checkout origin/aufs2-xx # for instance, aufs2-27 for linux-2.6.27
222 +                               # aufs2 (no -xx) for the latest -rc version.
223 +
224 +o aufs2-standalone tree
225 +$ git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git \
226 +       aufs2-standalone.git
227 +$ cd aufs2-standalone.git
228 +$ git checkout origin/aufs2-xx # for instance, aufs2-27 for linux-2.6.27
229 +                               # aufs2 (no -xx) for the latest -rc version.
230 +- apply "aufs2-standalone.patch" to your kernel source files.
231 +
232 +o aufs2-util tree
233 +$ git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-util.git \
234 +       aufs2-util.git
235 +$ cd aufs2-util.git
236 +- no particular tag/branch currently.
237 +
238 +
239 +3. Configuration and Compilation
240 +----------------------------------------
241 +For aufs2-2.6 tree,
242 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS.
243 +- set other aufs configurations if necessary.
244 +
245 +For aufs2-standalone tree,
246 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS, you can select =m.
247 +- edit fs/aufs/config.mk and set other aufs configurations if necessary.
248 +
249 +And then,
250 +- build your kernel (or a module) by "make"
251 +- install it and reboot your system
252 +- read README in aufs2-util, build and install it
253 +
254 +
255 +4. Usage
256 +----------------------------------------
257 +At first, make sure aufs2-util are installed, and please read the aufs
258 +manual, ./Documentation/filesystems/aufs/aufs.5.
259 +$ man -l aufs.5
260 +
261 +And then,
262 +$ mkdir /tmp/rw /tmp/aufs
263 +# mount -t aufs -o br=/tmp/rw:${HOME}=ro none /tmp/aufs
264 +
265 +Here is another example. The result is equivalent.
266 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
267 +  Or
268 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
269 +# mount -o remount,append:${HOME} /tmp/aufs
270 +
271 +Then, you can see whole tree of your home dir through /tmp/aufs. If
272 +you modify a file under /tmp/aufs, the one on your home directory is
273 +not affected, instead the same named file will be newly created under
274 +/tmp/rw. And all of your modification to a file will be applied to
275 +the one under /tmp/rw. This is called the file based Copy on Write
276 +(COW) method.
277 +Aufs mount options are described in aufs.5.
278 +
279 +Additionally, there are some sample usages of aufs which are a
280 +diskless system with network booting, and LiveCD over NFS.
281 +See sample dir in CVS tree on SourceForge.
282 +
283 +
284 +5. Contact
285 +----------------------------------------
286 +When you have any problems or strange behaviour in aufs, please let me
287 +know with:
288 +- /proc/mounts (instead of the output of mount(8))
289 +- /sys/module/aufs/*
290 +- /sys/fs/aufs/* (if you have them)
291 +- /debug/aufs/* (if you have them)
292 +- linux kernel version
293 +  if your kernel is not plain, for example modified by distributor,
294 +  the url where i can download its source is necessary too.
295 +- aufs version which was printed at loading the module or booting the
296 +  system, instead of the date you downloaded.
297 +- configuration (define/undefine CONFIG_AUFS_xxx)
298 +- kernel configuration or /proc/config.gz (if you have it)
299 +- behaviour which you think to be incorrect
300 +- actual operation, reproducible one is better
301 +- mailto: aufs-users at lists.sourceforge.net
302 +
303 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
304 +and Feature Requests) on SourceForge. Please join and write to
305 +aufs-users ML.
306 +
307 +
308 +6. Acknowledgements
309 +----------------------------------------
310 +Thanks to everyone who have tried and are using aufs, whoever
311 +have reported a bug or any feedback.
312 +
313 +Especially donors:
314 +Tomas Matejicek(slax.org) made a donation (much more than once).
315 +Dai Itasaka made a donation (2007/8).
316 +Chuck Smith made a donation (2008/4, 10 and 12).
317 +Henk Schoneveld made a donation (2008/9).
318 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
319 +Francois Dupoux made a donation (2008/11).
320 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public GIT
321 +tree (2009/2).
322 +William Grant made a donation (2009/3).
323 +
324 +Thank you very much.
325 +Donations are always, including future donations, very important and
326 +helpful for me to keep on developing aufs.
327 +
328 +
329 +7.
330 +----------------------------------------
331 +If you are an experienced user, no explanation is needed. Aufs is
332 +just a linux filesystem.
333 +
334 +
335 +Enjoy!
336 +
337 +# Local variables: ;
338 +# mode: text;
339 +# End: ;
340 diff --git a/Documentation/filesystems/aufs/aufs.5 b/Documentation/filesystems/aufs/aufs.5
341 new file mode 100644
342 index 0000000..0b9732f
343 --- /dev/null
344 +++ b/Documentation/filesystems/aufs/aufs.5
345 @@ -0,0 +1,1681 @@
346 +.ds AUFS_VERSION aufs2
347 +.ds AUFS_XINO_FNAME .aufs.xino
348 +.ds AUFS_XINO_DEFPATH /tmp/.aufs.xino
349 +.ds AUFS_DIRWH_DEF 3
350 +.ds AUFS_WH_PFX .wh.
351 +.ds AUFS_WH_PFX_LEN 4
352 +.ds AUFS_WKQ_NAME aufsd
353 +.ds AUFS_NWKQ_DEF 4
354 +.ds AUFS_WH_DIROPQ .wh..wh..opq
355 +.ds AUFS_WH_BASE .wh..wh.aufs
356 +.ds AUFS_WH_PLINKDIR .wh..wh.plnk
357 +.ds AUFS_BRANCH_MAX 127
358 +.ds AUFS_MFS_SECOND_DEF 30
359 +.ds AUFS_RDBLK_DEF 512
360 +.ds AUFS_RDHASH_DEF 32
361 +.\".so aufs.tmac
362 +.
363 +.eo
364 +.de TQ
365 +.br
366 +.ns
367 +.TP \$1
368 +..
369 +.de Bu
370 +.IP \(bu 4
371 +..
372 +.ec
373 +.\" end of macro definitions
374 +.
375 +.\" ----------------------------------------------------------------------
376 +.TH aufs 5 \*[AUFS_VERSION] Linux "Linux Aufs User\[aq]s Manual"
377 +.SH NAME
378 +aufs \- advanced multi layered unification filesystem. version \*[AUFS_VERSION]
379 +
380 +.\" ----------------------------------------------------------------------
381 +.SH DESCRIPTION
382 +Aufs is a stackable unification filesystem such as Unionfs, which unifies
383 +several directories and provides a merged single directory.
384 +In the early days, aufs was entirely re-designed and re-implemented
385 +Unionfs Version 1.x series. After
386 +many original ideas, approaches and improvements, it
387 +becomes totally different from Unionfs while keeping the basic features.
388 +See Unionfs Version 1.x series for the basic features.
389 +Recently, Unionfs Version 2.x series begin taking some of same
390 +approaches to aufs\[aq]s.
391 +
392 +.\" ----------------------------------------------------------------------
393 +.SH MOUNT OPTIONS
394 +At mount-time, the order of interpreting options is,
395 +.RS
396 +.Bu
397 +simple flags, except xino/noxino and udba=inotify
398 +.Bu
399 +branches
400 +.Bu
401 +xino/noxino
402 +.Bu
403 +udba=inotify
404 +.RE
405 +
406 +At remount-time,
407 +the options are interpreted in the given order,
408 +e.g. left to right.
409 +.RS
410 +.Bu
411 +create or remove
412 +whiteout-base(\*[AUFS_WH_BASE]) and
413 +whplink-dir(\*[AUFS_WH_PLINKDIR]) if necessary
414 +.RE
415 +.
416 +.TP
417 +.B br:BRANCH[:BRANCH ...] (dirs=BRANCH[:BRANCH ...])
418 +Adds new branches.
419 +(cf. Branch Syntax).
420 +
421 +Aufs rejects the branch which is an ancestor or a descendant of another
422 +branch. It is called overlapped. When the branch is loopback-mounted
423 +directory, aufs also checks the source fs-image file of loopback
424 +device. If the source file is a descendant of another branch, it will
425 +be rejected too.
426 +
427 +After mounting aufs or adding a branch, if you move a branch under
428 +another branch and make it descendant of another branch, aufs will not
429 +work correctly.
430 +.
431 +.TP
432 +.B [ add | ins ]:index:BRANCH
433 +Adds a new branch.
434 +The index begins with 0.
435 +Aufs creates
436 +whiteout-base(\*[AUFS_WH_BASE]) and
437 +whplink-dir(\*[AUFS_WH_PLINKDIR]) if necessary.
438 +
439 +If there is the same named file on the lower branch (larger index),
440 +aufs will hide the lower file.
441 +You can only see the highest file.
442 +You will be confused if the added branch has whiteouts (including
443 +diropq), they may or may not hide the lower entries.
444 +.\" It is recommended to make sure that the added branch has no whiteout.
445 +
446 +Even if a process have once mapped a file by mmap(2) with MAP_SHARED
447 +and the same named file exists on the lower branch,
448 +the process still refers the file on the lower(hidden)
449 +branch after adding the branch.
450 +If you want to update the contents of a process address space after
451 +adding, you need to restart your process or open/mmap the file again.
452 +.\" Usually, such files are executables or shared libraries.
453 +(cf. Branch Syntax).
454 +.
455 +.TP
456 +.B del:dir
457 +Removes a branch.
458 +Aufs does not remove
459 +whiteout-base(\*[AUFS_WH_BASE]) and
460 +whplink-dir(\*[AUFS_WH_PLINKDIR]) automatically.
461 +For example, when you add a RO branch which was unified as RW, you
462 +will see whiteout-base or whplink-dir on the added RO branch.
463 +
464 +If a process is referencing the file/directory on the deleting branch
465 +(by open, mmap, current working directory, etc.), aufs will return an
466 +error EBUSY.
467 +.
468 +.TP
469 +.B mod:BRANCH
470 +Modifies the permission flags of the branch.
471 +Aufs creates or removes
472 +whiteout-base(\*[AUFS_WH_BASE]) and/or
473 +whplink-dir(\*[AUFS_WH_PLINKDIR]) if necessary.
474 +
475 +If the branch permission is been changing \[oq]rw\[cq] to \[oq]ro\[cq], and a process
476 +is mapping a file by mmap(2)
477 +.\" with MAP_SHARED
478 +on the branch, the process may or may not
479 +be able to modify its mapped memory region after modifying branch
480 +permission flags.
481 +(cf. Branch Syntax).
482 +.
483 +.TP
484 +.B append:BRANCH
485 +equivalent to \[oq]add:(last index + 1):BRANCH\[cq].
486 +(cf. Branch Syntax).
487 +.
488 +.TP
489 +.B prepend:BRANCH
490 +equivalent to \[oq]add:0:BRANCH.\[cq]
491 +(cf. Branch Syntax).
492 +.
493 +.TP
494 +.B xino=filename
495 +Use external inode number bitmap and translation table.
496 +When CONFIG_AUFS_EXPORT is enabled, external inode generation table too.
497 +It is set to
498 +<FirstWritableBranch>/\*[AUFS_XINO_FNAME] by default, or
499 +\*[AUFS_XINO_DEFPATH].
500 +Comma character in filename is not allowed.
501 +
502 +The files are created per an aufs and per a branch filesystem, and
503 +unlinked. So you
504 +cannot find this file, but it exists and is read/written frequently by
505 +aufs.
506 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
507 +
508 +If you enable CONFIG_SYSFS, the path of xino files are not shown in
509 +/proc/mounts (and /etc/mtab), instead it is shown in
510 +<sysfs>/fs/aufs/si_<id>/xi_path.
511 +Otherwise, it is shown in /proc/mounts unless it is not the default
512 +path.
513 +.
514 +.TP
515 +.B noxino
516 +Stop using external inode number bitmap and translation table.
517 +
518 +If you use this option,
519 +Some applications will not work correctly.
520 +.\" And pseudo link feature will not work after the inode cache is
521 +.\" shrunk.
522 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
523 +.
524 +.TP
525 +.B trunc_xib
526 +Truncate the external inode number bitmap file. The truncation is done
527 +automatically when you delete a branch unless you do not specify
528 +\[oq]notrunc_xib\[cq] option.
529 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
530 +.
531 +.TP
532 +.B notrunc_xib
533 +Stop truncating the external inode number bitmap file when you delete
534 +a branch.
535 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
536 +.
537 +.TP
538 +.B create_policy | create=CREATE_POLICY
539 +.TQ
540 +.B copyup_policy | copyup | cpup=COPYUP_POLICY
541 +Policies to select one among multiple writable branches. The default
542 +values are \[oq]create=tdp\[cq] and \[oq]cpup=tdp\[cq].
543 +link(2) and rename(2) systemcalls have an exception. In aufs, they
544 +try keeping their operations in the branch where the source exists.
545 +(cf. Policies to Select One among Multiple Writable Branches).
546 +.
547 +.TP
548 +.B verbose | v
549 +Print some information.
550 +Currently, it is only busy file (or inode) at deleting a branch.
551 +.
552 +.TP
553 +.B noverbose | quiet | q | silent
554 +Disable \[oq]verbose\[cq] option.
555 +This is default value.
556 +.
557 +.TP
558 +.B sum
559 +df(1)/statfs(2) returns the total number of blocks and inodes of
560 +all branches.
561 +Note that there are cases that systemcalls may return ENOSPC, even if
562 +df(1)/statfs(2) shows that aufs has some free space/inode.
563 +.
564 +.TP
565 +.B nosum
566 +Disable \[oq]sum\[cq] option.
567 +This is default value.
568 +.
569 +.TP
570 +.B dirwh=N
571 +Watermark to remove a dir actually at rmdir(2) and rename(2).
572 +
573 +If the target dir which is being removed or renamed (destination dir)
574 +has a huge number of whiteouts, i.e. the dir is empty logically but
575 +physically, the cost to remove/rename the single
576 +dir may be very high.
577 +It is
578 +required to unlink all of whiteouts internally before issuing
579 +rmdir/rename to the branch.
580 +To reduce the cost of single systemcall,
581 +aufs renames the target dir to a whiteout-ed temporary name and
582 +invokes a pre-created
583 +kernel thread to remove whiteout-ed children and the target dir.
584 +The rmdir/rename systemcall returns just after kicking the thread.
585 +
586 +When the number of whiteout-ed children is less than the value of
587 +dirwh, aufs remove them in a single systemcall instead of passing
588 +another thread.
589 +This value is ignored when the branch is NFS.
590 +The default value is \*[AUFS_DIRWH_DEF].
591 +.\" .
592 +.\" .TP
593 +.\" .B rdcache=N
594 +.
595 +.TP
596 +.B rdblk=N
597 +Specifies a size of internal VDIR block which is allocated at a time in
598 +byte.
599 +The VDIR block will be allocated several times when necessary. If your
600 +directory has millions of files, you may want to expand this size.
601 +The default value is defined as \*[AUFS_RDBLK_DEF].
602 +The size has to be lager than NAME_MAX (usually 255) and kmalloc\-able
603 +(the maximum limit depends on your system. at least 128KB is available
604 +for every system).
605 +(cf. Virtual or Vertical Directory Block).
606 +.
607 +.TP
608 +.B rdhash=N
609 +Specifies a size of internal VDIR hash table which is used to compare
610 +the file names under the same named directory on multiple branches.
611 +The VDIR hash table will be allocated in readdir(3)/getdents(2),
612 +rmdir(2) and rename(2) for the existing target directory. If your
613 +directory has millions of files, you may want to expand this size.
614 +The default value is defined as \*[AUFS_RDHASH_DEF].
615 +The size has to be lager than zero, and it will be multiplied by 4 or 8
616 +(for 32\-bit and 64\-bit respectively, currently). The result must be
617 +kmalloc\-able
618 +(the maximum limit depends on your system. at least 128KB is available
619 +for every system).
620 +(cf. Virtual or Vertical Directory Block).
621 +.
622 +.TP
623 +.B plink
624 +.TQ
625 +.B noplink
626 +Specifies to use \[oq]pseudo link\[cq] feature or not.
627 +The default is \[oq]plink\[cq] which means use this feature.
628 +(cf. Pseudo Link)
629 +.
630 +.TP
631 +.B clean_plink
632 +Removes all pseudo-links in memory.
633 +In order to make pseudo-link permanent, use
634 +\[oq]auplink\[cq] utility just before one of these operations,
635 +unmounting aufs,
636 +using \[oq]ro\[cq] or \[oq]noplink\[cq] mount option,
637 +deleting a branch from aufs,
638 +adding a branch into aufs,
639 +or changing your writable branch as readonly.
640 +If you installed both of /sbin/mount.aufs and /sbin/umount.aufs, and your
641 +mount(8) and umount(8) support them,
642 +\[oq]auplink\[cq] utility will be executed automatically and flush pseudo-links.
643 +(cf. Pseudo Link)
644 +.
645 +.TP
646 +.B udba=none | reval | inotify
647 +Specifies the level of UDBA (User\[aq]s Direct Branch Access) test.
648 +(cf. User\[aq]s Direct Branch Access and Inotify Limitation).
649 +.
650 +.TP
651 +.B diropq=whiteouted | w | always | a
652 +Specifies whether mkdir(2) and rename(2) dir case make the created directory
653 +\[oq]opaque\[cq] or not.
654 +In other words, to create \[oq]\*[AUFS_WH_DIROPQ]\[cq] under the created or renamed
655 +directory, or not to create.
656 +When you specify diropq=w or diropq=whiteouted, aufs will not create
657 +it if the
658 +directory was not whiteouted or opaqued. If the directory was whiteouted
659 +or opaqued, the created or renamed directory will be opaque.
660 +When you specify diropq=a or diropq==always, aufs will always create
661 +it regardless
662 +the directory was whiteouted/opaqued or not.
663 +The default value is diropq=w, it means not to create when it is unnecessary.
664 +If you define CONFIG_AUFS_COMPAT at aufs compiling time, the default will be
665 +diropq=a.
666 +You need to consider this option if you are planning to add a branch later
667 +since \[oq]diropq\[cq] affects the same named directory on the added branch.
668 +.
669 +.TP
670 +.B warn_perm
671 +.TQ
672 +.B nowarn_perm
673 +Adding a branch, aufs will issue a warning about uid/gid/permission of
674 +the adding branch directory,
675 +when they differ from the existing branch\[aq]s. This difference may or
676 +may not impose a security risk.
677 +If you are sure that there is no problem and want to stop the warning,
678 +use \[oq]nowarn_perm\[cq] option.
679 +The default is \[oq]warn_perm\[cq] (cf. DIAGNOSTICS).
680 +
681 +.\" ----------------------------------------------------------------------
682 +.SH Module Parameters
683 +.TP
684 +.B nwkq=N
685 +The number of kernel thread named \*[AUFS_WKQ_NAME].
686 +
687 +Those threads stay in the system while the aufs module is loaded,
688 +and handle the special I/O requests from aufs.
689 +The default value is \*[AUFS_NWKQ_DEF].
690 +
691 +The special I/O requests from aufs include a part of copy-up, lookup,
692 +directory handling, pseudo-link, xino file operations and the
693 +delegated access to branches.
694 +For example, Unix filesystems allow you to rmdir(2) which has no write
695 +permission bit, if its parent directory has write permission bit. In aufs, the
696 +removing directory may or may not have whiteout or \[oq]dir opaque\[cq] mark as its
697 +child. And aufs needs to unlink(2) them before rmdir(2).
698 +Therefore aufs delegates the actual unlink(2) and rmdir(2) to another kernel
699 +thread which has been created already and has a superuser privilege.
700 +
701 +If you enable CONFIG_SYSFS, you can check this value through
702 +<sysfs>/module/aufs/parameters/nwkq.
703 +
704 +.
705 +.TP
706 +.B brs=1 | 0
707 +Specifies to use the branch path data file under sysfs or not.
708 +
709 +If the number of your branches is large or their path is long
710 +and you meet the limitation of mount(8) ro /etc/mtab, you need to
711 +enable CONFIG_SYSFS and set aufs module parameter brs=1.
712 +
713 +When this parameter is set as 1, aufs does not show \[oq]br:\[cq] (or dirs=)
714 +mount option through /proc/mounts (and /etc/mtab). So you can
715 +keep yourself from the page limitation of
716 +mount(8) or /etc/mtab.
717 +Aufs shows branch paths through <sysfs>/fs/aufs/si_XXX/brNNN.
718 +Actually the file under sysfs has also a size limitation, but I don\[aq]t
719 +think it is harmful.
720 +
721 +There is one more side effect in setting 1 to this parameter.
722 +If you rename your branch, the branch path written in /etc/mtab will be
723 +obsoleted and the future remount will meet some error due to the
724 +unmatched parameters (Remember that mount(8) may take the options from
725 +/etc/mtab and pass them to the systemcall).
726 +If you set 1, /etc/mtab will not hold the branch path and you will not
727 +meet such trouble. On the other hand, the entires for the
728 +branch path under sysfs are generated dynamically. So it must not be obsoleted.
729 +But I don\[aq]t think users want to rename branches so often.
730 +
731 +If CONFIG_SYSFS is disable, this paramater is always set to 0.
732 +.
733 +.TP
734 +.B sysrq=key
735 +Specifies MagicSysRq key for debugging aufs.
736 +You need to enable both of CONFIG_MAGIC_SYSRQ and CONFIG_AUFS_DEBUG.
737 +Currently this is for developers only.
738 +The default is \[oq]a\[cq].
739 +.
740 +.TP
741 +.B debug= 0 | 1
742 +Specifies disable(0) or enable(1) debug print in aufs.
743 +This parameter can be changed dynamically.
744 +You need to enable CONFIG_AUFS_DEBUG.
745 +Currently this is for developers only.
746 +The default is \[oq]0\[cq] (disable).
747 +
748 +.\" ----------------------------------------------------------------------
749 +.SH Entries under Sysfs and Debugfs
750 +See linux/Documentation/ABI/*/{sys,debug}fs-aufs.
751 +
752 +.\" ----------------------------------------------------------------------
753 +.SH Branch Syntax
754 +.TP
755 +.B dir_path[ =permission [ + attribute ] ]
756 +.TQ
757 +.B permission := rw | ro | rr
758 +.TQ
759 +.B attribute := wh | nolwh
760 +dir_path is a directory path.
761 +The keyword after \[oq]dir_path=\[cq] is a
762 +permission flags for that branch.
763 +Comma, colon and the permission flags string (including \[oq]=\[cq])in the path
764 +are not allowed.
765 +
766 +Any filesystem can be a branch, But some are not accepted such like
767 +sysfs, procfs and unionfs.
768 +If you specify such filesystems as an aufs branch, aufs will return an error
769 +saying it is unsupported.
770 +
771 +Cramfs in linux stable release has strange inodes and it makes aufs
772 +confused. For example,
773 +.nf
774 +$ mkdir -p w/d1 w/d2
775 +$ > w/z1
776 +$ > w/z2
777 +$ mkcramfs w cramfs
778 +$ sudo mount -t cramfs -o ro,loop cramfs /mnt
779 +$ find /mnt -ls
780 +    76    1 drwxr-xr-x   1 jro      232            64 Jan  1  1970 /mnt
781 +     1    1 drwxr-xr-x   1 jro      232             0 Jan  1  1970 /mnt/d1
782 +     1    1 drwxr-xr-x   1 jro      232             0 Jan  1  1970 /mnt/d2
783 +     1    1 -rw-r--r--   1 jro      232             0 Jan  1  1970 /mnt/z1
784 +     1    1 -rw-r--r--   1 jro      232             0 Jan  1  1970 /mnt/z2
785 +.fi
786 +
787 +All these two directories and two files have the same inode with one
788 +as their link count. Aufs cannot handle such inode correctly.
789 +Currently, aufs involves a tiny workaround for such inodes. But some
790 +applications may not work correctly since aufs inode number for such
791 +inode will change silently.
792 +If you do not have any empty files, empty directories or special files,
793 +inodes on cramfs will be all fine.
794 +
795 +A branch should not be shared as the writable branch between multiple
796 +aufs. A readonly branch can be shared.
797 +
798 +The maximum number of branches is configurable at compile time.
799 +The current value is \*[AUFS_BRANCH_MAX] which depends upon
800 +configuration.
801 +
802 +When an unknown permission or attribute is given, aufs sets ro to that
803 +branch silently.
804 +
805 +.SS Permission
806 +.
807 +.TP
808 +.B rw
809 +Readable and writable branch. Set as default for the first branch.
810 +If the branch filesystem is mounted as readonly, you cannot set it \[oq]rw.\[cq]
811 +.\" A filesystem which does not support link(2) and i_op\->setattr(), for
812 +.\" example FAT, will not be used as the writable branch.
813 +.
814 +.TP
815 +.B ro
816 +Readonly branch and it has no whiteouts on it.
817 +Set as default for all branches except the first one. Aufs never issue
818 +both of write operation and lookup operation for whiteout to this branch.
819 +.
820 +.TP
821 +.B rr
822 +Real readonly branch, special case of \[oq]ro\[cq], for natively readonly
823 +branch. Assuming the branch is natively readonly, aufs can optimize
824 +some internal operation. For example, if you specify \[oq]udba=inotify\[cq]
825 +option, aufs does not set inotify for the things on rr branch.
826 +Set by default for a branch whose fs-type is either \[oq]iso9660\[cq],
827 +\[oq]cramfs\[cq] or \[oq]romfs\[cq].
828 +
829 +When your branch exists on slower device and you have some
830 +capacity on your hdd, you may want to try ulobdev tool in ULOOP sample.
831 +It can cache the contents of the real devices on another faster device,
832 +so you will be able to get the better access performance.
833 +The ulobdev tool is for a generic block device, and the ulohttp is for a
834 +filesystem image on http server.
835 +If you want to spin down your hdd to save the
836 +battery life or something, then you may want to use ulobdev to save the
837 +access to the hdd, too.
838 +See $AufsCVS/sample/uloop in detail.
839 +
840 +.SS Attribute
841 +.
842 +.TP
843 +.B wh
844 +Readonly branch and it has/might have whiteouts on it.
845 +Aufs never issue write operation to this branch, but lookup for whiteout.
846 +Use this as \[oq]<branch_dir>=ro+wh\[cq].
847 +.
848 +.TP
849 +.B nolwh
850 +Usually, aufs creates a whiteout as a hardlink on a writable
851 +branch. This attributes prohibits aufs to create the hardlinked
852 +whiteout, including the source file of all hardlinked whiteout
853 +(\*[AUFS_WH_BASE].)
854 +If you do not like a hardlink, or your writable branch does not support
855 +link(2), then use this attribute.
856 +But I am afraid a filesystem which does not support link(2) natively
857 +will fail in other place such as copy-up.
858 +Use this as \[oq]<branch_dir>=rw+nolwh\[cq].
859 +Also you may want to try \[oq]noplink\[cq] mount option, while it is not recommended.
860 +
861 +.\" .SS FUSE as a branch
862 +.\" A FUSE branch needs special attention.
863 +.\" The struct fuse_operations has a statfs operation. It is OK, but the
864 +.\" parameter is struct statvfs* instead of struct statfs*. So almost
865 +.\" all user\-space implementaion will call statvfs(3)/fstatvfs(3) instead of
866 +.\" statfs(2)/fstatfs(2).
867 +.\" In glibc, [f]statvfs(3) issues [f]statfs(2), open(2)/read(2) for
868 +.\" /proc/mounts,
869 +.\" and stat(2) for the mountpoint. With this situation, a FUSE branch will
870 +.\" cause a deadlock in creating something in aufs. Here is a sample
871 +.\" scenario,
872 +.\" .\" .RS
873 +.\" .\" .IN -10
874 +.\" .Bu
875 +.\" create/modify a file just under the aufs root dir.
876 +.\" .Bu
877 +.\" aufs aquires a write\-lock for the parent directory, ie. the root dir.
878 +.\" .Bu
879 +.\" A library function or fuse internal may call statfs for a fuse branch.
880 +.\" The create=mfs mode in aufs will surely call statfs for each writable
881 +.\" branches.
882 +.\" .Bu
883 +.\" FUSE in kernel\-space converts and redirects the statfs request to the
884 +.\" user\-space.
885 +.\" .Bu
886 +.\" the user\-space statfs handler will call [f]statvfs(3).
887 +.\" .Bu
888 +.\" the [f]statvfs(3) in glibc will access /proc/mounts and issue
889 +.\" stat(2) for the mountpoint. But those require a read\-lock for the aufs
890 +.\" root directory.
891 +.\" .Bu
892 +.\" Then a deadlock occurs.
893 +.\" .\" .RE 1
894 +.\" .\" .IN
895 +.\" 
896 +.\" In order to avoid this deadlock, I would suggest not to call
897 +.\" [f]statvfs(3) from fuse. Here is a sample code to do this.
898 +.\" .nf
899 +.\" struct statvfs stvfs;
900 +.\" 
901 +.\" main()
902 +.\" {
903 +.\"    statvfs(..., &stvfs)
904 +.\"    or
905 +.\"    fstatvfs(..., &stvfs)
906 +.\"    stvfs.f_fsid = 0
907 +.\" }
908 +.\" 
909 +.\" statfs_handler(const char *path, struct statvfs *arg)
910 +.\" {
911 +.\"    struct statfs stfs
912 +.\" 
913 +.\"    memcpy(arg, &stvfs, sizeof(stvfs))
914 +.\" 
915 +.\"    statfs(..., &stfs)
916 +.\"    or
917 +.\"    fstatfs(..., &stfs)
918 +.\" 
919 +.\"    arg->f_bfree = stfs.f_bfree
920 +.\"    arg->f_bavail = stfs.f_bavail
921 +.\"    arg->f_ffree = stfs.f_ffree
922 +.\"    arg->f_favail = /* any value */
923 +.\" }
924 +.\" .fi
925 +
926 +.\" ----------------------------------------------------------------------
927 +.SH External Inode Number Bitmap, Translation Table and Generation Table (xino)
928 +Aufs uses one external bitmap file and one external inode number
929 +translation table files per an aufs and per a branch
930 +filesystem by default.
931 +Additionally when CONFIG_AUFS_EXPORT is enabled, one external inode
932 +generation table is added.
933 +The bitmap (and the generation table) is for recycling aufs inode number
934 +and the others
935 +are a table for converting an inode number on a branch to
936 +an aufs inode number. The default path
937 +is \[oq]first writable branch\[cq]/\*[AUFS_XINO_FNAME].
938 +If there is no writable branch, the
939 +default path
940 +will be \*[AUFS_XINO_DEFPATH].
941 +.\" A user who executes mount(8) needs the privilege to create xino
942 +.\" file.
943 +
944 +If you enable CONFIG_SYSFS, the path of xino files are not shown in
945 +/proc/mounts (and /etc/mtab), instead it is shown in
946 +<sysfs>/fs/aufs/si_<id>/xi_path.
947 +Otherwise, it is shown in /proc/mounts unless it is not the default
948 +path.
949 +
950 +Those files are always opened and read/write by aufs frequently.
951 +If your writable branch is on flash memory device, it is recommended
952 +to put xino files on other than flash memory by specifying \[oq]xino=\[cq]
953 +mount option.
954 +
955 +The
956 +maximum file size of the bitmap is, basically, the amount of the
957 +number of all the files on all branches divided by 8 (the number of
958 +bits in a byte).
959 +For example, on a 4KB page size system, if you have 32,768 (or
960 +2,599,968) files in aufs world,
961 +then the maximum file size of the bitmap is 4KB (or 320KB).
962 +
963 +The
964 +maximum file size of the table will
965 +be \[oq]max inode number on the branch x size of an inode number\[cq].
966 +For example in 32bit environment,
967 +
968 +.nf
969 +$ df -i /branch_fs
970 +/dev/hda14           2599968  203127 2396841    8% /branch_fs
971 +.fi
972 +
973 +and /branch_fs is an branch of the aufs. When the inode number is
974 +assigned contiguously (without \[oq]hole\[cq]), the maximum xino file size for
975 +/branch_fs will be 2,599,968 x 4 bytes = about 10 MB. But it might not be
976 +allocated all of disk blocks.
977 +When the inode number is assigned discontinuously, the maximum size of
978 +xino file will be the largest inode number on a branch x 4 bytes.
979 +Additionally, the file size is limited to LLONG_MAX or the s_maxbytes
980 +in filesystem\[aq]s superblock (s_maxbytes may be smaller than
981 +LLONG_MAX). So the
982 +support-able largest inode number on a branch is less than
983 +2305843009213693950 (LLONG_MAX/4\-1).
984 +This is the current limitation of aufs.
985 +On 64bit environment, this limitation becomes more strict and the
986 +supported largest inode number is less than LLONG_MAX/8\-1.
987 +
988 +The xino files are always hidden, i.e. removed. So you cannot
989 +do \[oq]ls \-l xino_file\[cq].
990 +If you enable CONFIG_DEBUG_FS, you can check these information through
991 +<debugfs>/aufs/<si_id>/{xib,xi[0-9]*,xigen}. xib is for the bitmap file,
992 +xi0 ix for the first branch, and xi1 is for the next. xigen is for the
993 +generation table.
994 +xib and xigen are in the format of,
995 +
996 +.nf
997 +<blocks>x<block size> <file size>
998 +.fi
999 +
1000 +Note that a filesystem usually has a
1001 +feature called pre-allocation, which means a number of
1002 +blocks are allocated automatically, and then deallocated
1003 +silently when the filesystem thinks they are unnecessary.
1004 +You do not have to be surprised the sudden changes of the number of
1005 +blocks, when your filesystem which xino files are placed supports the
1006 +pre-allocation feature.
1007 +
1008 +The rests are hidden xino file information in the format of,
1009 +
1010 +.nf
1011 +<file count>, <blocks>x<block size> <file size>
1012 +.fi
1013 +
1014 +If the file count is larger than 1, it means some of your branches are
1015 +on the same filesystem and the xino file is shared by them.
1016 +Note that the file size may not be equal to the actual consuming blocks
1017 +since xino file is a sparse file, i.e. a hole in a file which does not
1018 +consume any disk blocks.
1019 +
1020 +Once you unmount aufs, the xino files for that aufs are totally gone.
1021 +It means that the inode number is not permanent across umount or
1022 +shutdown.
1023 +
1024 +The xino files should be created on the filesystem except NFS.
1025 +If your first writable branch is NFS, you will need to specify xino
1026 +file path other than NFS.
1027 +Also if you are going to remove the branch where xino files exist or
1028 +change the branch permission to readonly, you need to use xino option
1029 +before del/mod the branch.
1030 +
1031 +The bitmap file can be truncated.
1032 +For example, if you delete a branch which has huge number of files,
1033 +many inode numbers will be recycled and the bitmap will be truncated
1034 +to smaller size. Aufs does this automatically when a branch is
1035 +deleted.
1036 +You can truncate it anytime you like if you specify \[oq]trunc_xib\[cq] mount
1037 +option. But when the accessed inode number was not deleted, nothing
1038 +will be truncated.
1039 +If you do not want to truncate it (it may be slow) when you delete a
1040 +branch, specify \[oq]notrunc_xib\[cq] after \[oq]del\[cq] mount option.
1041 +
1042 +If you do not want to use xino, use noxino mount option. Use this
1043 +option with care, since the inode number may be changed silently and
1044 +unexpectedly anytime.
1045 +For example,
1046 +rmdir failure, recursive chmod/chown/etc to a large and deep directory
1047 +or anything else.
1048 +And some applications will not work correctly.
1049 +.\" When the inode number has been changed, your system
1050 +.\" can be crazy.
1051 +If you want to change the xino default path, use xino mount option.
1052 +
1053 +After you add branches, the persistence of inode number may not be
1054 +guaranteed.
1055 +At remount time, cached but unused inodes are discarded.
1056 +And the newly appeared inode may have different inode number at the
1057 +next access time. The inodes in use have the persistent inode number.
1058 +
1059 +When aufs assigned an inode number to a file, and if you create the
1060 +same named file on the upper branch directly, then the next time you
1061 +access the file, aufs may assign another inode number to the file even
1062 +if you use xino option.
1063 +Some applications may treat the file whose inode number has been
1064 +changed as totally different file.
1065 +
1066 +.\" ----------------------------------------------------------------------
1067 +.SH Pseudo Link (hardlink over branches)
1068 +Aufs supports \[oq]pseudo link\[cq] which is a logical hard-link over
1069 +branches (cf. ln(1) and link(2)).
1070 +In other words, a copied-up file by link(2) and a copied-up file which was
1071 +hard-linked on a readonly branch filesystem.
1072 +
1073 +When you have files named fileA and fileB which are
1074 +hardlinked on a readonly branch, if you write something into fileA,
1075 +aufs copies-up fileA to a writable branch, and write(2) the originally
1076 +requested thing to the copied-up fileA. On the writable branch,
1077 +fileA is not hardlinked.
1078 +But aufs remembers it was hardlinked, and handles fileB as if it existed
1079 +on the writable branch, by referencing  fileA\[aq]s inode on the writable
1080 +branch as fileB\[aq]s inode.
1081 +
1082 +Once you unmount aufs, the plink info for that aufs kept in memory are totally
1083 +gone.
1084 +It means that the pseudo-link is not permanent.
1085 +If you want to make plink permanent, try \[oq]auplink\[cq] utility just before
1086 +one of these operations,
1087 +unmounting your aufs,
1088 +using \[oq]ro\[cq] or \[oq]noplink\[cq] mount option,
1089 +deleting a branch from aufs,
1090 +adding a branch into aufs,
1091 +or changing your writable branch to readonly.
1092 +
1093 +This utility will reproduces all real hardlinks on a writable branch by linking
1094 +them, and removes pseudo-link info in memory and temporary link on the
1095 +writable branch.
1096 +Since this utility access your branches directly, you cannot hide them by
1097 +\[oq]mount \-\-bind /tmp /branch\[cq] or something.
1098 +
1099 +If you are willing to rebuild your aufs with the same branches later, you
1100 +should use auplink utility before you umount your aufs.
1101 +If you installed both of /sbin/mount.aufs and /sbin/umount.aufs, and your
1102 +mount(8) and umount(8) support them,
1103 +\[oq]auplink\[cq] utility will be executed automatically and flush pseudo-links.
1104 +
1105 +.nf
1106 +# auplink /your/aufs/root flush
1107 +# umount /your/aufs/root
1108 +or
1109 +# auplink /your/aufs/root flush
1110 +# mount -o remount,mod:/your/writable/branch=ro /your/aufs/root
1111 +or
1112 +# auplink /your/aufs/root flush
1113 +# mount -o remount,noplink /your/aufs/root
1114 +or
1115 +# auplink /your/aufs/root flush
1116 +# mount -o remount,del:/your/aufs/branch /your/aufs/root
1117 +or
1118 +# auplink /your/aufs/root flush
1119 +# mount -o remount,append:/your/aufs/branch /your/aufs/root
1120 +.fi
1121 +
1122 +The plinks are kept both in memory and on disk. When they consumes too much
1123 +resources on your system, you can use the \[oq]auplink\[cq] utility at anytime and
1124 +throw away the unnecessary pseudo-links in safe.
1125 +
1126 +Additionally, the \[oq]auplink\[cq] utility is very useful for some security reasons.
1127 +For example, when you have a directory whose permission flags
1128 +are 0700, and a file who is 0644 under the 0700 directory. Usually,
1129 +all files under the 0700 directory are private and no one else can see
1130 +the file. But when the directory is 0711 and someone else knows the 0644
1131 +filename, he can read the file.
1132 +
1133 +Basically, aufs pseudo-link feature creates a temporary link under the
1134 +directory whose owner is root and the permission flags are 0700.
1135 +But when the writable branch is NFS, aufs sets 0711 to the directory.
1136 +When the 0644 file is pseudo-linked, the temporary link, of course the
1137 +contents of the file is totally equivalent, will be created under the
1138 +0711 directory. The filename will be generated by its inode number.
1139 +While it is hard to know the generated filename, someone else may try peeping
1140 +the temporary pseudo-linked file by his software tool which may try the name
1141 +from one to MAX_INT or something.
1142 +In this case, the 0644 file will be read unexpectedly.
1143 +I am afraid that leaving the temporary pseudo-links can be a security hole.
1144 +It makes sense to execute \[oq]auplink /your/aufs/root flush\[cq]
1145 +periodically, when your writable branch is NFS.
1146 +
1147 +When your writable branch is not NFS, or all users are careful enough to set 0600
1148 +to their private files, you do not have to worry about this issue.
1149 +
1150 +If you do not want this feature, use \[oq]noplink\[cq] mount option.
1151 +
1152 +.SS The behaviours of plink and noplink
1153 +This sample shows that the \[oq]f_src_linked2\[cq] with \[oq]noplink\[cq] option cannot follow
1154 +the link.
1155 +
1156 +.nf
1157 +none on /dev/shm/u type aufs (rw,xino=/dev/shm/rw/.aufs.xino,br:/dev/shm/rw=rw:/dev/shm/ro=ro)
1158 +$ ls -li ../r?/f_src_linked* ./f_src_linked* ./copied
1159 +ls: ./copied: No such file or directory
1160 +15 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked
1161 +15 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked2
1162 +22 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ./f_src_linked
1163 +22 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ./f_src_linked2
1164 +$ echo abc >> f_src_linked
1165 +$ cp f_src_linked copied
1166 +$ ls -li ../r?/f_src_linked* ./f_src_linked* ./copied
1167 +15 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked
1168 +15 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked2
1169 +36 -rw-r--r--  2 jro jro 6 Dec 22 11:03 ../rw/f_src_linked
1170 +53 -rw-r--r--  1 jro jro 6 Dec 22 11:03 ./copied
1171 +22 -rw-r--r--  2 jro jro 6 Dec 22 11:03 ./f_src_linked
1172 +22 -rw-r--r--  2 jro jro 6 Dec 22 11:03 ./f_src_linked2
1173 +$ cmp copied f_src_linked2
1174 +$
1175 +
1176 +none on /dev/shm/u type aufs (rw,xino=/dev/shm/rw/.aufs.xino,noplink,br:/dev/shm/rw=rw:/dev/shm/ro=ro)
1177 +$ ls -li ../r?/f_src_linked* ./f_src_linked* ./copied
1178 +ls: ./copied: No such file or directory
1179 +17 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked
1180 +17 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked2
1181 +23 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ./f_src_linked
1182 +23 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ./f_src_linked2
1183 +$ echo abc >> f_src_linked
1184 +$ cp f_src_linked copied
1185 +$ ls -li ../r?/f_src_linked* ./f_src_linked* ./copied
1186 +17 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked
1187 +17 -rw-r--r--  2 jro jro 2 Dec 22 11:03 ../ro/f_src_linked2
1188 +36 -rw-r--r--  1 jro jro 6 Dec 22 11:03 ../rw/f_src_linked
1189 +53 -rw-r--r--  1 jro jro 6 Dec 22 11:03 ./copied
1190 +23 -rw-r--r--  2 jro jro 6 Dec 22 11:03 ./f_src_linked
1191 +23 -rw-r--r--  2 jro jro 6 Dec 22 11:03 ./f_src_linked2
1192 +$ cmp copied f_src_linked2
1193 +cmp: EOF on f_src_linked2
1194 +$
1195 +.fi
1196 +
1197 +.\"
1198 +.\" If you add/del a branch, or link/unlink the pseudo-linked
1199 +.\" file on a branch
1200 +.\" directly, aufs cannot keep the correct link count, but the status of
1201 +.\" \[oq]pseudo-linked.\[cq]
1202 +.\" Those files may or may not keep the file data after you unlink the
1203 +.\" file on the branch directly, especially the case of your branch is
1204 +.\" NFS.
1205 +
1206 +If you add a branch which has fileA or fileB, aufs does not follow the
1207 +pseudo link. The file on the added branch has no relation to the same
1208 +named file(s) on the lower branch(es).
1209 +If you use noxino mount option, pseudo link will not work after the
1210 +kernel shrinks the inode cache.
1211 +
1212 +This feature will not work for squashfs before version 3.2 since its
1213 +inode is tricky.
1214 +When the inode is hardlinked, squashfs inodes has the same inode
1215 +number and correct link count, but the inode memory object is
1216 +different. Squashfs inodes (before v3.2) are generated for each, even
1217 +they are hardlinked.
1218 +
1219 +.\" ----------------------------------------------------------------------
1220 +.SH User\[aq]s Direct Branch Access (UDBA)
1221 +UDBA means a modification to a branch filesystem manually or directly,
1222 +e.g. bypassing aufs.
1223 +While aufs is designed and implemented to be safe after UDBA,
1224 +it can make yourself and your aufs confused. And some information like
1225 +aufs inode will be incorrect.
1226 +For example, if you rename a file on a branch directly, the file on
1227 +aufs may
1228 +or may not be accessible through both of old and new name.
1229 +Because aufs caches various information about the files on
1230 +branches. And the cache still remains after UDBA.
1231 +
1232 +Aufs has a mount option named \[oq]udba\[cq] which specifies the test level at
1233 +access time whether UDBA was happened or not.
1234 +.
1235 +.TP
1236 +.B udba=none
1237 +Aufs trusts the dentry and the inode cache on the system, and never
1238 +test about UDBA. With this option, aufs runs fastest, but it may show
1239 +you incorrect data.
1240 +Additionally, if you often modify a branch
1241 +directly, aufs will not be able to trace the changes of inodes on the
1242 +branch. It can be a cause of wrong behaviour, deadlock or anything else.
1243 +
1244 +It is recommended to use this option only when you are sure that
1245 +nobody access a file on a branch.
1246 +It might be difficult for you to achieve real \[oq]no UDBA\[cq] world when you
1247 +cannot stop your users doing \[oq]find / \-ls\[cq] or something.
1248 +If you really want to forbid all of your users to UDBA, here is a trick
1249 +for it.
1250 +With this trick, users cannot see the
1251 +branches directly and aufs runs with no problem, except \[oq]auplink\[cq] utility.
1252 +But if you are not familiar with aufs, this trick may make
1253 +yourself confused.
1254 +
1255 +.nf
1256 +# d=/tmp/.aufs.hide
1257 +# mkdir $d
1258 +# for i in $branches_you_want_to_hide
1259 +> do
1260 +>      mount -n --bind $d $i
1261 +> done
1262 +.fi
1263 +
1264 +When you unmount the aufs, delete/modify the branch by remount, or you
1265 +want to show the hidden branches again, unmount the bound
1266 +/tmp/.aufs.hide.
1267 +
1268 +.nf
1269 +# umount -n $branches_you_want_to_unbound
1270 +.fi
1271 +
1272 +If you use FUSE filesystem as an aufs branch which supports hardlink,
1273 +you should not set this option, since FUSE makes inode objects for
1274 +each hardlinks (at least in linux\-2.6.23). When your FUSE filesystem
1275 +maintains them at link/unlinking, it is equivalent
1276 +to \[oq]direct branch access\[cq] for aufs.
1277 +
1278 +.
1279 +.TP
1280 +.B udba=reval
1281 +Aufs tests only the existence of the file which existed. If
1282 +the existed file was removed on the branch directly, aufs
1283 +discard the cache about the file and
1284 +re-lookup it. So the data will be updated.
1285 +This test is at minimum level to keep the performance and ensure the
1286 +existence of a file.
1287 +This is default and aufs runs still fast.
1288 +
1289 +This rule leads to some unexpected situation, but I hope it is
1290 +harmless. Those are totally depends upon cache. Here are just a few
1291 +examples.
1292 +.
1293 +.RS
1294 +.Bu
1295 +If the file is cached as negative or
1296 +not-existed, aufs does not test it. And the file is still handled as
1297 +negative after a user created the file on a branch directly. If the
1298 +file is not cached, aufs will lookup normally and find the file.
1299 +.
1300 +.Bu
1301 +When the file is cached as positive or existed, and a user created the
1302 +same named file directly on the upper branch. Aufs detects the cached
1303 +inode of the file is still existing and will show you the old (cached)
1304 +file which is on the lower branch.
1305 +.
1306 +.Bu
1307 +When the file is cached as positive or existed, and a user renamed the
1308 +file by rename(2) directly. Aufs detects the inode of the file is
1309 +still existing. You may or may not see both of the old and new files.
1310 +Todo: If aufs also tests the name, we can detect this case.
1311 +.RE
1312 +
1313 +If your outer modification (UDBA) is rare and you can ignore the
1314 +temporary and minor differences between virtual aufs world and real
1315 +branch filesystem, then try this mount option.
1316 +.
1317 +.TP
1318 +.B udba=inotify
1319 +Aufs sets \[oq]inotify\[cq] to all the accessed directories on its branches
1320 +and receives the event about the dir and its children. It consumes
1321 +resources, cpu and memory. And I am afraid that the performance will be
1322 +hurt, but it is most strict test level.
1323 +There are some limitations of linux inotify, see also Inotify
1324 +Limitation.
1325 +So it is recommended to leave udba default option usually, and set it
1326 +to inotify by remount when you need it.
1327 +
1328 +When a user accesses the file which was notified UDBA before, the cached data
1329 +about the file will be discarded and aufs re-lookup it. So the data will
1330 +be updated.
1331 +When an error condition occurs between UDBA and aufs operation, aufs
1332 +will return an error, including EIO.
1333 +To use this option, you need to enable CONFIG_INOTIFY and
1334 +CONFIG_AUFS_UDBA_INOTIFY.
1335 +
1336 +To rename/rmdir a directory on a branch directory may reveal the same named
1337 +directory on the lower branch. Aufs tries re-lookuping the renamed
1338 +directory and the revealed directory and assigning different inode
1339 +number to them. But the inode number including their children can be a
1340 +problem. The inode numbers will be changed silently, and
1341 +aufs may produce a warning. If you rename a directory repeatedly and
1342 +reveal/hide the lower directory, then aufs may confuse their inode
1343 +numbers too. It depends upon the system cache.
1344 +
1345 +When you make a directory in aufs and mount other filesystem on it,
1346 +the directory in aufs cannot be removed expectedly because it is a
1347 +mount point. But the same named directory on the writable branch can
1348 +be removed, if someone wants. It is just an empty directory, instead
1349 +of a mount point.
1350 +Aufs cannot stop such direct rmdir, but produces a warning about it.
1351 +
1352 +If the pseudo-linked file is hardlinked or unlinked on the branch
1353 +directly, its inode link count in aufs may be incorrect. It is
1354 +recommended to flush the psuedo-links by auplink script.
1355 +
1356 +.\" ----------------------------------------------------------------------
1357 +.SH Linux Inotify Limitation
1358 +Unfortunately, current inotify (linux\-2.6.18) has some limitations,
1359 +and aufs must derive it.
1360 +
1361 +.SS IN_ATTRIB, updating atime
1362 +When a file/dir on a branch is accessed directly, the inode atime (access
1363 +time, cf. stat(2)) may or may not be updated. In some cases, inotify
1364 +does not fire this event. So the aufs inode atime may remain old.
1365 +
1366 +.SS IN_ATTRIB, updating nlink
1367 +When the link count of a file on a branch is incremented by link(2)
1368 +directly,
1369 +inotify fires IN_CREATE to the parent
1370 +directory, but IN_ATTRIB to the file. So the aufs inode nlink may
1371 +remain old.
1372 +
1373 +.SS IN_DELETE, removing file on NFS
1374 +When a file on a NFS branch is deleted directly, inotify may or may
1375 +not fire
1376 +IN_DELETE event. It depends upon the status of dentry
1377 +(DCACHE_NFSFS_RENAMED flag).
1378 +In this case, the file on aufs seems still exists. Aufs and any user can see
1379 +the file.
1380 +
1381 +.SS IN_IGNORED, deleted rename target
1382 +When a file/dir on a branch is unlinked by rename(2) directly, inotify
1383 +fires IN_IGNORED which means the inode is deleted. Actually, in some
1384 +cases, the inode survives. For example, the rename target is linked or
1385 +opened. In this case, inotify watch set by aufs is removed by VFS and
1386 +inotify.
1387 +And aufs cannot receive the events anymore. So aufs may show you
1388 +incorrect data about the file/dir.
1389 +
1390 +.\" ----------------------------------------------------------------------
1391 +.SH Virtual or Vertical Directory Block (VDIR)
1392 +In order to provide the merged view of file listing, aufs builds
1393 +internal direcotry block on memory. For readdir, aufs performs readdir()
1394 +internally for each dir on branches, merges their entries with
1395 +eliminating the whiteout\-ed ones, and sets it to the opened file (dir)
1396 +object. So the file object has its entry list until it is closed. The
1397 +entry list will be updated when the file position is zero (by
1398 +rewinddir(3)) and becomes obsoleted.
1399 +
1400 +Some people may call it can be a security hole or invite DoS attack
1401 +since the opened and once readdir\-ed dir (file object) holds its entry
1402 +list and becomes a pressure for system memory. But I would say it is similar
1403 +to files under /proc or /sys. The virtual files in them also holds a
1404 +memory page (generally) while they are opened. When an idea to reduce
1405 +memory for them is introduced, it will be applied to aufs too.
1406 +
1407 +The dynamically allocated memory block for the name of entries has a
1408 +unit of \*[AUFS_RDBLK_DEF] bytes by default.
1409 +During building dir blocks, aufs creates hash list (hashed and divied by
1410 +\*[AUFS_RDHASH_DEF] by default) and judging whether
1411 +the entry is whiteouted by its upper branch or already listed.
1412 +
1413 +These values are suitable for normal environments. But you may have
1414 +millions of files or very long filenames under a single direcotry. For
1415 +such cases, you may need to customize these values by spicifying rdblk=
1416 +and rdhash= aufs mount options.
1417 +
1418 +For instance, there are 97 files under my /bin, and the total name
1419 +length is 597 bytes.
1420 +
1421 +.nf
1422 +$ \\ls -1 /bin | wc
1423 +     97      97     597
1424 +.fi
1425 +
1426 +Strictly speaking, 97 end\-of\-line codes are
1427 +included. But it is OK since aufs VDIR also stores the name length in 1
1428 +byte. In this case, you do not need to customize the default values. 597 bytes
1429 +filenames will be stored in 2 VDIR memory blocks (597 <
1430 +\*[AUFS_RDBLK_DEF] x 2).
1431 +And 97 filenames are distributed among \*[AUFS_RDHASH_DEF] lists, so one
1432 +list will point 4 names in average. To judge the names is whiteouted or
1433 +not, the number of comparision will be 4. 2 memory allocations
1434 +and 4 comparison costs low (even if the directory is opened for a long
1435 +time). So you do not need to customize.
1436 +
1437 +If your directory has millions of files, the you will need to specify
1438 +rdblk= and rdhash=.
1439 +
1440 +.nf
1441 +$ ls -U /mnt/rotating-rust | wc -l
1442 +1382438
1443 +.fi
1444 +
1445 +In this case, assuming the average length of filenames is 6, in order to
1446 +get better time performance I would
1447 +recommend to set $((128*1024)) or $((64*1024)) for rdblk, and
1448 +$((8*1024)) or $((4*1024)) for rdhash.
1449 +You can change these values of the active aufs mount by "mount -o
1450 +remount".
1451 +
1452 +This customization is not for
1453 +reducing the memory space, but for reducing time for the number of memory
1454 +allocation and the name comparison. The larger value is faster, in
1455 +general. Of course, you will need system memory. This is a generic
1456 +"time\-vs\-space" problem.
1457 +
1458 +.\" ----------------------------------------------------------------------
1459 +.SH Copy On Write, or aufs internal copyup and copydown
1460 +Every stackable filesystem which implements copy\-on\-write supports the
1461 +copyup feature. The feature is to copy a file/dir from the lower branch
1462 +to the upper internally. When you have one readonly branch and one
1463 +upper writable branch, and you append a string to a file which exists on
1464 +the readonly branch, then aufs will copy the file from the readonly
1465 +branch to the writable branch with its directory hierarchy. It means one
1466 +write(2) involves several logical/internal mkdir(2), creat(2), read(2),
1467 +write(2) and close(2) systemcalls
1468 +before the actual expected write(2) is performed. Sometimes it may take
1469 +a long time, particulary when the file is very large.
1470 +If CONFIG_AUFS_DEBUG is enabled, aufs produces a message saying \[oq]copying
1471 +a large file.\[cq]
1472 +
1473 +You may see the message when you change the xino file path or
1474 +truncate the xino/xib files. Sometimes those files can be large and may
1475 +take a long time to handle them.
1476 +
1477 +.\" ----------------------------------------------------------------------
1478 +.SH Policies to Select One among Multiple Writable Branches
1479 +Aufs has some policies to select one among multiple writable branches
1480 +when you are going to write/modify something. There are two kinds of
1481 +policies, one is for newly create something and the other is for
1482 +internal copy-up.
1483 +You can select them by specifying mount option \[oq]create=CREATE_POLICY\[cq]
1484 +or \[oq]cpup=COPYUP_POLICY.\[cq]
1485 +These policies have no meaning when you have only one writable
1486 +branch. If there is some meaning, it must hurt the performance.
1487 +
1488 +.SS Exceptions for Policies
1489 +In every cases below, even if the policy says that the branch where a
1490 +new file should be created is /rw2, the file will be created on /rw1.
1491 +.
1492 +.Bu
1493 +If there is a readonly branch with \[oq]wh\[cq] attribute above the
1494 +policy-selected branch and the parent dir is marked as opaque,
1495 +or the target (creating) file is whiteouted on the ro+wh branch, then
1496 +the policy will be ignored and the target file will be created on the
1497 +nearest upper writable branch than the ro+wh branch.
1498 +.RS
1499 +.nf
1500 +/aufs = /rw1 + /ro+wh/diropq + /rw2
1501 +/aufs = /rw1 + /ro+wh/wh.tgt + /rw2
1502 +.fi
1503 +.RE
1504 +.
1505 +.Bu
1506 +If there is a writable branch above the policy-selected branch and the
1507 +parent dir is marked as opaque or the target file is whiteouted on the
1508 +branch, then the policy will be ignored and the target file will be
1509 +created on the highest one among the upper writable branches who has
1510 +diropq or whiteout. In case of whiteout, aufs removes it as usual.
1511 +.RS
1512 +.nf
1513 +/aufs = /rw1/diropq + /rw2
1514 +/aufs = /rw1/wh.tgt + /rw2
1515 +.fi
1516 +.RE
1517 +.
1518 +.Bu
1519 +link(2) and rename(2) systemcalls are exceptions in every policy.
1520 +They try selecting the branch where the source exists as possible since
1521 +copyup a large file will take long time. If it can\[aq]t be, ie. the
1522 +branch where the source exists is readonly, then they will follow the
1523 +copyup policy.
1524 +.
1525 +.Bu
1526 +There is an exception for rename(2) when the target exists.
1527 +If the rename target exists, aufs compares the index of the branches
1528 +where the source and the target are existing and selects the higher
1529 +one. If the selected branch is readonly, then aufs follows the copyup
1530 +policy.
1531 +
1532 +.SS Policies for Creating
1533 +.
1534 +.TP
1535 +.B create=tdp | top\-down\-parent
1536 +Selects the highest writable branch where the parent dir exists. If
1537 +the parent dir does not exist on a writable branch, then the internal
1538 +copyup will happen. The policy for this copyup is always \[oq]bottom-up.\[cq]
1539 +This is the default policy.
1540 +.
1541 +.TP
1542 +.B create=rr | round\-robin
1543 +Selects a writable branch in round robin. When you have two writable
1544 +branches and creates 10 new files, 5 files will be created for each
1545 +branch.
1546 +mkdir(2) systemcall is an exception. When you create 10 new directories,
1547 +all are created on the same branch.
1548 +.
1549 +.TP
1550 +.B create=mfs[:second] | most\-free\-space[:second]
1551 +Selects a writable branch which has most free space. In order to keep
1552 +the performance, you can specify the duration (\[oq]second\[cq]) which makes
1553 +aufs hold the index of last selected writable branch until the
1554 +specified seconds expires. The first time you create something in aufs
1555 +after the specified seconds expired, aufs checks the amount of free
1556 +space of all writable branches by internal statfs call
1557 +and the held branch index will be updated.
1558 +The default value is \*[AUFS_MFS_SECOND_DEF] seconds.
1559 +.
1560 +.TP
1561 +.B create=mfsrr:low[:second]
1562 +Selects a writable branch in most-free-space mode first, and then
1563 +round-robin mode. If the selected branch has less free space than the
1564 +specified value \[oq]low\[cq] in bytes, then aufs re-tries in round-robin mode.
1565 +.\" \[oq]G\[cq], \[oq]M\[cq] and \[oq]K\[cq] (case insensitive) can be followed after \[oq]low.\[cq] Or
1566 +Try an arithmetic expansion of shell which is defined by POSIX.
1567 +For example, $((10 * 1024 * 1024)) for 10M.
1568 +You can also specify the duration (\[oq]second\[cq]) which is equivalent to
1569 +the \[oq]mfs\[cq] mode.
1570 +.
1571 +.TP
1572 +.B create=pmfs[:second]
1573 +Selects a writable branch where the parent dir exists, such as tdp
1574 +mode. When the parent dir exists on multiple writable branches, aufs
1575 +selects the one which has most free space, such as mfs mode.
1576 +
1577 +.SS Policies for Copy-Up
1578 +.
1579 +.TP
1580 +.B cpup=tdp | top\-down\-parent
1581 +Equivalent to the same named policy for create.
1582 +This is the default policy.
1583 +.
1584 +.TP
1585 +.B cpup=bup | bottom\-up\-parent
1586 +Selects the writable branch where the parent dir exists and the branch
1587 +is nearest upper one from the copyup-source.
1588 +.
1589 +.TP
1590 +.B cpup=bu | bottom\-up
1591 +Selects the nearest upper writable branch from the copyup-source,
1592 +regardless the existence of the parent dir.
1593 +
1594 +.\" ----------------------------------------------------------------------
1595 +.SH Exporting Aufs via NFS
1596 +Aufs is supporting NFS-exporting.
1597 +Since aufs has no actual block device, you need to add NFS \[oq]fsid\[cq] option at
1598 +exporting. Refer to the manual of NFS about the detail of this option.
1599 +
1600 +There are some limitations or requirements.
1601 +.RS
1602 +.Bu
1603 +The branch filesystem must support NFS-exporting.
1604 +.Bu
1605 +NFSv2 is not supported. When you mount the exported aufs from your NFS
1606 +client, you will need to some NFS options like v3 or nfsvers=3,
1607 +especially if it is nfsroot.
1608 +.Bu
1609 +If the size of the NFS file handle on your branch filesystem is large,
1610 +aufs will
1611 +not be able to handle it. The maximum size of NFSv3 file
1612 +handle for a filesystem is 64 bytes. Aufs uses 24 bytes for 32bit
1613 +system, plus 12 bytes for 64bit system. The rest is a room for a file
1614 +handle of a branch filesystem.
1615 +.Bu
1616 +The External Inode Number Bitmap, Translation Table and Generation Table
1617 +(xino) is
1618 +required since NFS file
1619 +handle is based upon inode number. The mount option \[oq]xino\[cq] is enabled
1620 +by default.
1621 +The external inode generation table and its debugfs entry
1622 +(<debugfs>/aufs/si_*/xigen) is created when CONFIG_AUFS_EXPORT is
1623 +enabled even if you don\[aq]t export aufs actually.
1624 +The size of the external inode generation table grows only, never be
1625 +truncated. You might need to pay attention to the free space of the
1626 +filesystem where xino files are placed. By default, it is the first
1627 +writable branch.
1628 +.Bu
1629 +The branch filesystems must be accessible, which means \[oq]not hidden.\[cq]
1630 +It means you need to \[oq]mount \-\-move\[cq] when you use initramfs and
1631 +switch_root(8), or chroot(8).
1632 +.RE
1633 +
1634 +.\" ----------------------------------------------------------------------
1635 +.SH Dentry and Inode Caches
1636 +If you want to clear caches on your system, there are several tricks
1637 +for that. If your system ram is low,
1638 +try \[oq]find /large/dir \-ls > /dev/null\[cq].
1639 +It will read many inodes and dentries and cache them. Then old caches will be
1640 +discarded.
1641 +But when you have large ram or you do not have such large
1642 +directory, it is not effective.
1643 +
1644 +If you want to discard cache within a certain filesystem,
1645 +try \[oq]mount \-o remount /your/mntpnt\[cq]. Some filesystem may return an error of
1646 +EINVAL or something, but VFS discards the unused dentry/inode caches on the
1647 +specified filesystem.
1648 +
1649 +.\" ----------------------------------------------------------------------
1650 +.SH Compatible/Incompatible with Unionfs Version 1.x Series
1651 +If you compile aufs with \-DCONFIG_AUFS_COMPAT, dirs= option and =nfsro
1652 +branch permission flag are available. They are interpreted as
1653 +br: option and =ro flags respectively.
1654 + \[oq]debug\[cq], \[oq]delete\[cq], \[oq]imap\[cq] options are ignored silently. When you
1655 +compile aufs without \-DCONFIG_AUFS_COMPAT, these three options are
1656 +also ignored, but a warning message is issued.
1657 +
1658 +Ignoring \[oq]delete\[cq] option, and to keep filesystem consistency, aufs tries
1659 +writing something to only one branch in a single systemcall. It means
1660 +aufs may copyup even if the copyup-src branch is specified as writable.
1661 +For example, you have two writable branches and a large regular file
1662 +on the lower writable branch. When you issue rename(2) to the file on aufs,
1663 +aufs may copyup it to the upper writable branch.
1664 +If this behaviour is not what you want, then you should rename(2) it
1665 +on the lower branch directly.
1666 +
1667 +And there is a simple shell
1668 +script \[oq]unionctl\[cq] under sample subdirectory, which is compatible with
1669 +unionctl(8) in
1670 +Unionfs Version 1.x series, except \-\-query action.
1671 +This script executes mount(8) with \[oq]remount\[cq] option and uses
1672 +add/del/mod aufs mount options.
1673 +If you are familiar with Unionfs Version 1.x series and want to use unionctl(8), you can
1674 +try this script instead of using mount \-o remount,... directly.
1675 +Aufs does not support ioctl(2) interface.
1676 +This script is highly depending upon mount(8) in
1677 +util\-linux\-2.12p package, and you need to mount /proc to use this script.
1678 +If your mount(8) version differs, you can try modifying this
1679 +script. It is very easy.
1680 +The unionctl script is just for a sample usage of aufs remount
1681 +interface.
1682 +
1683 +Aufs uses the external inode number bitmap and translation table by
1684 +default.
1685 +
1686 +The default branch permission for the first branch is \[oq]rw\[cq], and the
1687 +rest is \[oq]ro.\[cq]
1688 +
1689 +The whiteout is for hiding files on lower branches. Also it is applied
1690 +to stop readdir going lower branches.
1691 +The latter case is called \[oq]opaque directory.\[cq] Any
1692 +whiteout is an empty file, it means whiteout is just an mark.
1693 +In the case of hiding lower files, the name of whiteout is
1694 +\[oq]\*[AUFS_WH_PFX]<filename>.\[cq]
1695 +And in the case of stopping readdir, the name is
1696 +\[oq]\*[AUFS_WH_PFX]\*[AUFS_WH_PFX].opq\[cq] or
1697 +\[oq]\*[AUFS_WH_PFX]__dir_opaque.\[cq] The name depends upon your compile
1698 +configuration
1699 +CONFIG_AUFS_COMPAT.
1700 +.\" All of newly created or renamed directory will be opaque.
1701 +All whiteouts are hardlinked,
1702 +including \[oq]<writable branch top dir>/\*[AUFS_WH_BASE].\[cq]
1703 +
1704 +The hardlink on an ordinary (disk based) filesystem does not
1705 +consume inode resource newly. But in linux tmpfs, the number of free
1706 +inodes will be decremented by link(2). It is recommended to specify
1707 +nr_inodes option to your tmpfs if you meet ENOSPC. Use this option
1708 +after checking by \[oq]df \-i.\[cq]
1709 +
1710 +When you rmdir or rename-to the dir who has a number of whiteouts,
1711 +aufs rename the dir to the temporary whiteouted-name like
1712 +\[oq]\*[AUFS_WH_PFX]<dir>.<random hex>.\[cq] Then remove it after actual operation.
1713 +cf. mount option \[oq]dirwh.\[cq]
1714 +
1715 +.\" ----------------------------------------------------------------------
1716 +.SH Incompatible with an Ordinary Filesystem
1717 +stat(2) returns the inode info from the first existence inode among
1718 +the branches, except the directory link count.
1719 +Aufs computes the directory link count larger than the exact value usually, in
1720 +order to keep UNIX filesystem semantics, or in order to shut find(1) mouth up.
1721 +The size of a directory may be wrong too, but it has to do no harm.
1722 +The timestamp of a directory will not be updated when a file is
1723 +created or removed under it, and it was done on a lower branch.
1724 +
1725 +The test for permission bits has two cases. One is for a directory,
1726 +and the other is for a non-directory. In the case of a directory, aufs
1727 +checks the permission bits of all existing directories. It means you
1728 +need the correct privilege for the directories including the lower
1729 +branches.
1730 +The test for a non-directory is more simple. It checks only the
1731 +topmost inode.
1732 +
1733 +statfs(2) returns the information of the first branch info except
1734 +namelen when \[oq]nosum\[cq] is specified (the default). The namelen is
1735 +decreased by the whiteout prefix length. And the block size may differ
1736 +from st_blksize which is obtained by stat(2).
1737 +
1738 +Remember, seekdir(3) and telldir(3) are not defined in POSIX. They may
1739 +not work as you expect. Try rewinddir(3) or re-open the dir.
1740 +
1741 +The whiteout prefix (\*[AUFS_WH_PFX]) is reserved on all branches. Users should
1742 +not handle the filename begins with this prefix.
1743 +In order to future whiteout, the maxmum filename length is limited by
1744 +the longest value \- \*[AUFS_WH_PFX_LEN]. It may be a violation of POSIX.
1745 +
1746 +If you dislike the difference between the aufs entries in /etc/mtab
1747 +and /proc/mounts, and if you are using mount(8) in util\-linux package,
1748 +then try ./mount.aufs utility. Copy the script to /sbin/mount.aufs.
1749 +This simple utility tries updating
1750 +/etc/mtab. If you do not care about /etc/mtab, you can ignore this
1751 +utility.
1752 +Remember this utility is highly depending upon mount(8) in
1753 +util\-linux\-2.12p package, and you need to mount /proc.
1754 +
1755 +Since aufs uses its own inode and dentry, your system may cache huge
1756 +number of inodes and dentries. It can be as twice as all of the files
1757 +in your union.
1758 +It means that unmounting or remounting readonly at shutdown time may
1759 +take a long time, since mount(2) in VFS tries freeing all of the cache
1760 +on the target filesystem.
1761 +
1762 +When you open a directory, aufs will open several directories
1763 +internally.
1764 +It means you may reach the limit of the number of file descriptor.
1765 +And when the lower directory cannot be opened, aufs will close all the
1766 +opened upper directories and return an error.
1767 +
1768 +The sub-mount under the branch
1769 +of local filesystem
1770 +is ignored.
1771 +For example, if you have mount another filesystem on
1772 +/branch/another/mntpnt, the files under \[oq]mntpnt\[cq] will be ignored by aufs.
1773 +It is recommended to mount the sub-mount under the mounted aufs.
1774 +For example,
1775 +
1776 +.nf
1777 +# sudo mount /dev/sdaXX /ro_branch
1778 +# d=another/mntpnt
1779 +# sudo mount /dev/sdbXX /ro_branch/$d
1780 +# mkdir -p /rw_branch/$d
1781 +# sudo mount -t aufs -o br:/rw_branch:/ro_branch none /aufs
1782 +# sudo mount -t aufs -o br:/rw_branch/${d}:/ro_branch/${d} none /aufs/another/$d
1783 +.fi
1784 +
1785 +There are several characters which are not allowed to use in a branch
1786 +directory path and xino filename. See detail in Branch Syntax and Mount
1787 +Option.
1788 +
1789 +The file-lock which means fcntl(2) with F_SETLK, F_SETLKW or F_GETLK, flock(2)
1790 +and lockf(3), is applied to virtual aufs file only, not to the file on a
1791 +branch. It means you can break the lock by accessing a branch directly.
1792 +TODO: check \[oq]security\[cq] to hook locks, as inotify does.
1793 +
1794 +The I/O to the named pipe or local socket are not handled by aufs, even
1795 +if it exists in aufs. After the reader and the writer established their
1796 +connection if the pipe/socket are copied-up, they keep using the old one
1797 +instead of the copied-up one.
1798 +
1799 +The fsync(2) and fdatasync(2) systemcalls return 0 which means success, even
1800 +if the given file descriptor is not opened for writing.
1801 +I am afraid this behaviour may violate some standards. Checking the
1802 +behaviour of fsync(2) on ext2, aufs decided to return success.
1803 +
1804 +If you want to use disk-quota, you should set it up to your writable
1805 +branch since aufs does not have its own block device.
1806 +
1807 +When your aufs is the root directory of your system, and your system
1808 +tells you some of the filesystem were not unmounted cleanly, try these
1809 +procedure when you shutdown your system.
1810 +.nf
1811 +# mount -no remount,ro /
1812 +# for i in $writable_branches
1813 +# do mount -no remount,ro $i
1814 +# done
1815 +.fi
1816 +If your xino file is on a hard drive, you also need to specify
1817 +\[oq]noxino\[cq] option or \[oq]xino=/your/tmpfs/xino\[cq] at remounting root
1818 +directory.
1819 +
1820 +To rename(2) directory may return EXDEV even if both of src and tgt
1821 +are on the same aufs. When the rename-src dir exists on multiple
1822 +branches and the lower dir has child(ren), aufs has to copyup all his
1823 +children. It can be recursive copyup. Current aufs does not support
1824 +such huge copyup operation at one time in kernel space, instead
1825 +produces a warning and returns EXDEV.
1826 +Generally, mv(1) detects this error and tries mkdir(2) and
1827 +rename(2) or copy/unlink recursively. So the result is harmless.
1828 +If your application which issues rename(2) for a directory does not
1829 +support EXDEV, it will not work on aufs.
1830 +Also this specification is applied to the case when the src directroy
1831 +exists on the lower readonly branch and it has child(ren).
1832 +
1833 +If a sudden accident such like a power failure happens during aufs is
1834 +performing, and regular fsck for branch filesystems is completed after
1835 +the disaster, you need to extra fsck for aufs writable branches. It is
1836 +necessary to check whether the whiteout remains incorrectly or not,
1837 +eg. the real filename and the whiteout for it under the same parent
1838 +directory. If such whiteout remains, aufs cannot handle the file
1839 +correctly.
1840 +To check the consistency from the aufs\[aq] point of view, you can use a
1841 +simple shell script called /sbin/auchk. Its purpose is a fsck tool for
1842 +aufs, and it checks the illegal whiteout, the remained
1843 +pseudo-links and the remained aufs-temp files. If they are found, the
1844 +utility reports you and asks whether to delete or not.
1845 +It is recommended to execute /sbin/auchk for every writable branch
1846 +filesystem before mouting aufs if the system experienced crash.
1847 +
1848 +
1849 +.\" ----------------------------------------------------------------------
1850 +.SH EXAMPLES
1851 +The mount options are interpreted from left to right at remount-time.
1852 +These examples
1853 +shows how the options are handled. (assuming /sbin/mount.aufs was
1854 +installed)
1855 +
1856 +.nf
1857 +# mount -v -t aufs br:/day0:/base none /u
1858 +none on /u type aufs (rw,xino=/day0/.aufs.xino,br:/day0=rw:/base=ro)
1859 +# mount -v -o remount,\\
1860 +       prepend:/day1,\\
1861 +       xino=/day1/xino,\\
1862 +       mod:/day0=ro,\\
1863 +       del:/day0 \\
1864 +       /u
1865 +none on /u type aufs (rw,xino=/day1/xino,br:/day1=rw:/base=ro)
1866 +.fi
1867 +
1868 +.nf
1869 +# mount -t aufs br:/rw none /u
1870 +# mount -o remount,append:/ro /u
1871 +different uid/gid/permission, /ro
1872 +# mount -o remount,del:/ro /u
1873 +# mount -o remount,nowarn_perm,append:/ro /u
1874 +#
1875 +(there is no warning)
1876 +.fi
1877 +
1878 +.\" If you want to expand your filesystem size, aufs may help you by
1879 +.\" adding an writable branch. Since aufs supports multiple writable
1880 +.\" branches, the old writable branch can be being writable, if you want.
1881 +.\" In this example, any modifications to the files under /ro branch will
1882 +.\" be copied-up to /new, but modifications to the files under /rw branch
1883 +.\" will not.
1884 +.\" And the next example shows the modifications to the files under /rw branch
1885 +.\" will be copied-up to /new/a.
1886 +.\"
1887 +.\" Todo: test multiple writable branches policy. cpup=nearest, cpup=exist_parent.
1888 +.\"
1889 +.\" .nf
1890 +.\" # mount -v -t aufs br:/rw:/ro none /u
1891 +.\" none on /u type aufs (rw,xino=/rw/.aufs.xino,br:/rw=rw:/ro=ro)
1892 +.\" # mkfs /new
1893 +.\" # mount -v -o remount,add:1:/new=rw /u
1894 +.\" none on /u type aufs (rw,xino=/rw/.aufs.xino,br:/rw=rw:/new=rw:/ro=ro)
1895 +.\" .fi
1896 +.\"
1897 +.\" .nf
1898 +.\" # mount -v -t aufs br:/rw:/ro none /u
1899 +.\" none on /u type aufs (rw,xino=/rw/.aufs.xino,br:/rw=rw:/ro=ro)
1900 +.\" # mkfs /new
1901 +.\" # mkdir /new/a new/b
1902 +.\" # mount -v -o remount,add:1:/new/b=rw,prepend:/new/a,mod:/rw=ro /u
1903 +.\" none on /u type aufs (rw,xino=/rw/.aufs.xino,br:/new/a=rw:/rw=ro:/new/b=rw:/ro=ro)
1904 +.\" .fi
1905 +
1906 +When you use aufs as root filesystem, it is recommended to consider to
1907 +exclude some directories. For example, /tmp and /var/log are not need
1908 +to stack in many cases. They do not usually need to copyup or to whiteout.
1909 +Also the swapfile on aufs (a regular file, not a block device) is not
1910 +supported.
1911 +In order to exclude the specific dir from aufs, try bind mounting.
1912 +
1913 +And there is a good sample which is for network booted diskless machines. See
1914 +sample/ in detail.
1915 +
1916 +.\" ----------------------------------------------------------------------
1917 +.SH DIAGNOSTICS
1918 +When you add a branch to your union, aufs may warn you about the
1919 +privilege or security of the branch, which is the permission bits,
1920 +owner and group of the top directory of the branch.
1921 +For example, when your upper writable branch has a world writable top
1922 +directory,
1923 +a malicious user can create any files on the writable branch directly,
1924 +like copyup and modify manually. I am afraid it can be a security
1925 +issue.
1926 +
1927 +When you mount or remount your union without \-o ro common mount option
1928 +and without writable branch, aufs will warn you that the first branch
1929 +should be writable.
1930 +
1931 +.\" It is discouraged to set both of \[oq]udba\[cq] and \[oq]noxino\[cq] mount options. In
1932 +.\" this case the inode number under aufs will always be changed and may
1933 +.\" reach the end of inode number which is a maximum of unsigned long. If
1934 +.\" the inode number reaches the end, aufs will return EIO repeatedly.
1935 +
1936 +When you set udba other than inotify and change something on your
1937 +branch filesystem directly, later aufs may detect some mismatches to
1938 +its cache. If it is a critical mismatch, aufs returns EIO.
1939 +
1940 +When an error occurs in aufs, aufs prints the kernel message with
1941 +\[oq]errno.\[cq] The priority of the message (log level) is ERR or WARNING which
1942 +depends upon the message itself.
1943 +You can convert the \[oq]errno\[cq] into the error message by perror(3),
1944 +strerror(3) or something.
1945 +For example, the \[oq]errno\[cq] in the message \[oq]I/O Error, write failed (\-28)\[cq]
1946 +is 28 which means ENOSPC or \[oq]No space left on device.\[cq]
1947 +
1948 +When CONFIG_AUFS_BR_RAMFS is enabled, you can specify ramfs as an aufs
1949 +branch. Since ramfs is simple, it does not set the maximum link count
1950 +originally. In aufs, it is very dangerous, particulary for
1951 +whiteouts. Finally aufs sets the maxmimum link count for ramfs. Tha
1952 +value is 32000 which is borrowed from ext2.
1953 +
1954 +
1955 +.\" .SH Current Limitation
1956 +.
1957 +.\" ----------------------------------------------------------------------
1958 +.\" SYNOPSIS
1959 +.\" briefly describes the command or function\[aq]s interface.  For  commands,  this
1960 +.\" shows the syntax of the command and its arguments (including options); bold-
1961 +.\" face is used for as-is text and italics are  used  to  indicate  replaceable
1962 +.\" arguments. Brackets ([]) surround optional arguments, vertical bars (|) sep-
1963 +.\" arate choices, and ellipses (...) can be repeated.  For functions, it  shows
1964 +.\" any required data declarations or #include directives, followed by the func-
1965 +.\" tion declaration.
1966 +.
1967 +.\" DESCRIPTION
1968 +.\" gives an explanation of what the command, function, or format does.  Discuss
1969 +.\" how  it  interacts  with  files  and standard input, and what it produces on
1970 +.\" standard output  or  standard  error.   Omit  internals  and  implementation
1971 +.\" details  unless  they\[aq]re critical for understanding the interface.  Describe
1972 +.\" the usual case; for information on options  use  the  OPTIONS  section.   If
1973 +.\" there  is some kind of input grammar or complex set of subcommands, consider
1974 +.\" describing them in a separate USAGE section (and just place an  overview  in
1975 +.\" the DESCRIPTION section).
1976 +.
1977 +.\" RETURN VALUE
1978 +.\" gives a list of the values the library routine will return to the caller and
1979 +.\" the conditions that cause these values to be returned.
1980 +.
1981 +.\" EXIT STATUS
1982 +.\" lists the possible exit status values or a program and the  conditions  that
1983 +.\" cause these values to be returned.
1984 +.
1985 +.\" USAGE
1986 +.\" describes the grammar of any sublanguage this implements.
1987 +.
1988 +.\" FILES
1989 +.\" lists  the  files the program or function uses, such as configuration files,
1990 +.\" startup files, and files the program directly operates on.   Give  the  full
1991 +.\" pathname  of  these  files,  and  use the installation process to modify the
1992 +.\" directory part to match user preferences.  For many  programs,  the  default
1993 +.\" installation  location is in /usr/local, so your base manual page should use
1994 +.\" /usr/local as the base.
1995 +.
1996 +.\" ENVIRONMENT
1997 +.\" lists all environment variables that affect your program or function and how
1998 +.\" they affect it.
1999 +.
2000 +.\" SECURITY
2001 +.\" discusses security issues and implications.  Warn  about  configurations  or
2002 +.\" environments  that should be avoided, commands that may have security impli-
2003 +.\" cations, and so on, especially if they aren\[aq]t obvious.  Discussing  security
2004 +.\" in  a  separate section isn\[aq]t necessary; if it\[aq]s easier to understand, place
2005 +.\" security information in the other sections (such as the DESCRIPTION or USAGE
2006 +.\" section).  However, please include security information somewhere!
2007 +.
2008 +.\" CONFORMING TO
2009 +.\" describes any standards or conventions this implements.
2010 +.
2011 +.\" NOTES
2012 +.\" provides miscellaneous notes.
2013 +.
2014 +.\" BUGS
2015 +.\" lists  limitations,  known defects or inconveniences, and other questionable
2016 +.\" activities.
2017 +
2018 +.SH COPYRIGHT
2019 +Copyright \(co 2005\-2009 Junjiro R. Okajima
2020 +
2021 +.SH AUTHOR
2022 +Junjiro R. Okajima
2023 +
2024 +.\" SEE ALSO
2025 +.\" lists  related  man  pages in alphabetical order, possibly followed by other
2026 +.\" related pages or documents.  Conventionally this is the last section.
2027 diff --git a/Documentation/filesystems/aufs/design/01intro.txt b/Documentation/filesystems/aufs/design/01intro.txt
2028 new file mode 100644
2029 index 0000000..4955fdc
2030 --- /dev/null
2031 +++ b/Documentation/filesystems/aufs/design/01intro.txt
2032 @@ -0,0 +1,128 @@
2033 +
2034 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2035 +# 
2036 +# This program is free software; you can redistribute it and/or modify
2037 +# it under the terms of the GNU General Public License as published by
2038 +# the Free Software Foundation; either version 2 of the License, or
2039 +# (at your option) any later version.
2040 +
2041 +Introduction
2042 +----------------------------------------
2043 +
2044 +aufs [ei ju: ef es] | [a u f s]
2045 +1. abbrev. for "advanced multi-layered unification filesystem".
2046 +2. abbrev. for "another unionfs".
2047 +3. abbrev. for "auf das" in German which means "on the" in English.
2048 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
2049 +   But "Filesystem aufs Filesystem" is hard to understand.
2050 +
2051 +AUFS is a filesystem with features:
2052 +- multi layered stackable unification filesystem, the member directory
2053 +  is called as a branch.
2054 +- branch permission and attribute, 'readonly', 'real-readonly',
2055 +  'readwrite', 'whiteout-able', 'link-able whiteout' and their
2056 +  combination.
2057 +- internal "file copy-on-write".
2058 +- logical deletion, whiteout.
2059 +- dynamic branch manipulation, adding, deleting and changing permission.
2060 +- allow bypassing aufs, user's direct branch access.
2061 +- external inode number translation table and bitmap which maintains the
2062 +  persistent aufs inode number.
2063 +- seekable directory, including NFS readdir.
2064 +- file mapping, mmap and sharing pages.
2065 +- pseudo-link, hardlink over branches.
2066 +- loopback mounted filesystem as a branch.
2067 +- several policies to select one among multiple writable branches.
2068 +- revert a single systemcall when an error occurs in aufs.
2069 +- and more...
2070 +
2071 +
2072 +Multi Layered Stackable Unification Filesystem
2073 +----------------------------------------------------------------------
2074 +Most people already knows what it is.
2075 +It is a filesystem which unifies several directories and provides a
2076 +merged single directory. When users access a file, the access will be
2077 +passed/re-directed/converted (sorry, I am not sure which English word is
2078 +correct) to the real file on the member filesystem. The member
2079 +filesystem is called 'lower filesystem' or 'branch' and has a mode
2080 +'readonly' and 'readwrite.' And the deletion for a file on the lower
2081 +readonly branch is handled by creating 'whiteout' on the upper writable
2082 +branch.
2083 +
2084 +On LKML, there have been discussions about UnionMount (Jan Blunck and
2085 +Bharata B Rao) and Unionfs (Erez Zadok). They took different approaches
2086 +to implement the merged-view.
2087 +The former tries putting it into VFS, and the latter implements as a
2088 +separate filesystem.
2089 +(If I misunderstand about these implementations, please let me know and
2090 +I shall correct it. Because it is a long time ago when I read their
2091 +source files last time).
2092 +UnionMount's approach will be able to small, but may be hard to share
2093 +branches between several UnionMount since the whiteout in it is
2094 +implemented in the inode on branch filesystem and always
2095 +shared. According to Bharata's post, readdir does not seems to be
2096 +finished yet.
2097 +Unionfs has a longer history. When I started implementing a stacking filesystem
2098 +(Aug 2005), it already existed. It has virtual super_block, inode,
2099 +dentry and file objects and they have an array pointing lower same kind
2100 +objects. After contributing many patches for Unionfs, I re-started my
2101 +project AUFS (Jun 2006).
2102 +
2103 +In AUFS, the structure of filesystem resembles to Unionfs, but I
2104 +implemented my own ideas, approaches and enhancements and it became
2105 +totally different one.
2106 +
2107 +
2108 +Several characters/aspects of aufs
2109 +----------------------------------------------------------------------
2110 +
2111 +Aufs has several characters or aspects.
2112 +1. a filesystem, callee of VFS helper
2113 +2. sub-VFS, caller of VFS helper for branches
2114 +3. a virtual filesystem which maintains persistent inode number
2115 +4. reader/writer of files on branches such like an application
2116 +
2117 +1. Caller of VFS Helper
2118 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
2119 +unlink(2) from an application reaches sys_unlink() kernel function and
2120 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
2121 +calls filesystem specific unlink operation. Actually aufs implements the
2122 +unlink operation but it behaves like a redirector.
2123 +
2124 +2. Caller of VFS Helper for Branches
2125 +aufs_unlink() passes the unlink request to the branch filesystem as if
2126 +it were called from VFS. So the called unlink operation of the branch
2127 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
2128 +every necessary pre/post operation for the branch filesystem.
2129 +- acquire the lock for the parent dir on a branch
2130 +- lookup in a branch
2131 +- revalidate dentry on a branch
2132 +- mnt_want_write() for a branch
2133 +- vfs_unlink() for a branch
2134 +- mnt_drop_write() for a branch
2135 +- release the lock on a branch
2136 +
2137 +3. Persistent Inode Number
2138 +One of the most important issue for a filesystem is to maintain inode
2139 +numbers. This is particularly important to support exporting a
2140 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
2141 +backend block device for its own. But some storage is necessary to
2142 +maintain inode number. It may be a large space and may not suit to keep
2143 +in memory. Aufs rents some space from its first writable branch
2144 +filesystem (by default) and creates file(s) on it. These files are
2145 +created by aufs internally and removed soon (currently) keeping opened.
2146 +Note: Because these files are removed, they are totally gone after
2147 +      unmounting aufs. It means the inode numbers are not persistent
2148 +      across unmount or reboot. I have a plan to make them really
2149 +      persistent which will be important for aufs on NFS server.
2150 +
2151 +4. Read/Write Files Internally (copy-on-write)
2152 +Because a branch can be readonly, when you write a file on it, aufs will
2153 +"copy-up" it to the upper writable branch internally. And then write the
2154 +originally requested thing to the file. Generally kernel doesn't
2155 +open/read/write file actively. In aufs, even a single write may cause a
2156 +internal "file copy". This behaviour is very similar to cp(1) command.
2157 +
2158 +Some people may think it is better to pass such work to user space
2159 +helper, instead of doing in kernel space. Actually I am still thinking
2160 +about it. But currently I have implemented it in kernel space.
2161 diff --git a/Documentation/filesystems/aufs/design/02struct.txt b/Documentation/filesystems/aufs/design/02struct.txt
2162 new file mode 100644
2163 index 0000000..6db666b
2164 --- /dev/null
2165 +++ b/Documentation/filesystems/aufs/design/02struct.txt
2166 @@ -0,0 +1,205 @@
2167 +
2168 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2169 +# 
2170 +# This program is free software; you can redistribute it and/or modify
2171 +# it under the terms of the GNU General Public License as published by
2172 +# the Free Software Foundation; either version 2 of the License, or
2173 +# (at your option) any later version.
2174 +
2175 +Basic Aufs Internal Structure
2176 +
2177 +Superblock/Inode/Dentry/File Objects
2178 +----------------------------------------------------------------------
2179 +As like an ordinary filesystem, aufs has its own
2180 +superblock/inode/dentry/file objects. All these objects have a
2181 +dynamically allocated array and store the same kind of pointers to the
2182 +lower filesystem, branch.
2183 +For example, when you build a union with one readwrite branch and one
2184 +readonly, mounted /au, /rw and /ro respectively.
2185 +- /au = /rw + /ro
2186 +- /ro/fileA exists but /rw/fileA
2187 +
2188 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
2189 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
2190 +- [0] = NULL
2191 +- [1] = /ro/fileA
2192 +
2193 +This style of an array is essentially same to the aufs
2194 +superblock/inode/dentry/file objects.
2195 +
2196 +Because aufs supports manipulating branches, ie. add/delete/change
2197 +dynamically, these objects has its own generation. When branches are
2198 +changed, the generation in aufs superblock is incremented. And a
2199 +generation in other object are compared when it is accessed.
2200 +When a generation in other objects are obsoleted, aufs refreshes the
2201 +internal array.
2202 +
2203 +
2204 +Superblock
2205 +----------------------------------------------------------------------
2206 +Additionally aufs superblock has some data for policies to select one
2207 +among multiple writable branches, XIB files, pseudo-links and kobject.
2208 +See below in detail.
2209 +About the policies which supports copy-down a directory, see policy.txt
2210 +too.
2211 +
2212 +
2213 +Branch and XINO(External Inode Number Translation Table)
2214 +----------------------------------------------------------------------
2215 +Every branch has its own xino (external inode number translation table)
2216 +file. The xino file is created and unlinked by aufs internally. When two
2217 +members of a union exist on the same filesystem, they share the single
2218 +xino file.
2219 +The struct of a xino file is simple, just a sequence of aufs inode
2220 +numbers which is indexed by the lower inode number.
2221 +In the above sample, assume the inode number of /ro/fileA is i111 and
2222 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
2223 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
2224 +
2225 +When the inode numbers are not contiguous, the xino file will be sparse
2226 +which has a hole in it and doesn't consume as much disk space as it
2227 +might appear. If your branch filesystem consumes disk space for such
2228 +holes, then you should specify 'xino=' option at mounting aufs.
2229 +
2230 +Also a writable branch has three kinds of "whiteout bases". All these
2231 +are existed when the branch is joined to aufs and the names are
2232 +whiteout-ed doubly, so that users will never see their names in aufs
2233 +hierarchy.
2234 +1. a regular file which will be linked to all whiteouts.
2235 +2. a directory to store a pseudo-link.
2236 +3. a directory to store an "orphan-ed" file temporary.
2237 +
2238 +1. Whiteout Base
2239 +   When you remove a file on a readonly branch, aufs handles it as a
2240 +   logical deletion and creates a whiteout on the upper writable branch
2241 +   as a hardlink of this file in order not to consume inode on the
2242 +   writable branch.
2243 +2. Pseudo-link Dir
2244 +   See below, Pseudo-link.
2245 +3. Step-Parent Dir
2246 +   When "fileC" exists on the lower readonly branch only and it is
2247 +   opened and removed with its parent dir, and then user writes
2248 +   something into it, then aufs copies-up fileC to this
2249 +   directory. Because there is no other dir to store fileC. After
2250 +   creating a file under this dir, the file is unlinked.
2251 +
2252 +Because aufs supports manipulating branches, ie. add/delete/change
2253 +dynamically, a branch has its own id. When the branch order changes, aufs
2254 +finds the new index by searching the branch id.
2255 +
2256 +
2257 +Pseudo-link
2258 +----------------------------------------------------------------------
2259 +Assume "fileA" exists on the lower readonly branch only and it is
2260 +hardlinked to "fileB" on the branch. When you write something to fileA,
2261 +aufs copies-up it to the upper writable branch. Additionally aufs
2262 +creates a hardlink under the Pseudo-link Directory of the writable
2263 +branch. The inode of a pseudo-link is kept in aufs super_block as a
2264 +simple list. If fileB is read after unlinking fileA, aufs returns
2265 +filedata from the pseudo-link instead of the lower readonly
2266 +branch. Because the pseudo-link is based upon the inode, to keep the
2267 +inode number by xino (see above) is important.
2268 +
2269 +All the hardlinks under the Pseudo-link Directory of the writable branch
2270 +should be restored in a proper location later. Aufs provides a utility
2271 +to do this. The userspace helpers executed at remounting and unmounting
2272 +aufs by default.
2273 +
2274 +
2275 +XIB(external inode number bitmap)
2276 +----------------------------------------------------------------------
2277 +Addition to the xino file per a branch, aufs has an external inode number
2278 +bitmap in a superblock object. It is also a file such like a xino file.
2279 +It is a simple bitmap to mark whether the aufs inode number is in-use or
2280 +not.
2281 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
2282 +
2283 +Aufs implements a feature to truncate/refresh both of xino and xib to
2284 +reduce the number of consumed disk blocks for these files.
2285 +
2286 +
2287 +Virtual or Vertical Dir
2288 +----------------------------------------------------------------------
2289 +In order to support multiple layers (branches), aufs readdir operation
2290 +constructs a virtual dir block on memory. For readdir, aufs calls
2291 +vfs_readdir() internally for each dir on branches, merges their entries
2292 +with eliminating the whiteout-ed ones, and sets it to file (dir)
2293 +object. So the file object has its entry list until it is closed. The
2294 +entry list will be updated when the file position is zero and becomes
2295 +old. This decision is made in aufs automatically.
2296 +
2297 +The dynamically allocated memory block for the name of entries has a
2298 +unit of 512 bytes (by default) and stores the names contiguously (no
2299 +padding). Another block for each entry is handled by kmem_cache too.
2300 +During building dir blocks, aufs creates hash list and judging whether
2301 +the entry is whiteouted by its upper branch or already listed.
2302 +
2303 +Some people may call it can be a security hole or invite DoS attack
2304 +since the opened and once readdir-ed dir (file object) holds its entry
2305 +list and becomes a pressure for system memory. But I'd say it is similar
2306 +to files under /proc or /sys. The virtual files in them also holds a
2307 +memory page (generally) while they are opened. When an idea to reduce
2308 +memory for them is introduced, it will be applied to aufs too.
2309 +
2310 +
2311 +Workqueue
2312 +----------------------------------------------------------------------
2313 +Aufs sometimes requires privilege access to a branch. For instance,
2314 +in copy-up/down operation. When a user process is going to make changes
2315 +to a file which exists in the lower readonly branch only, and the mode
2316 +of one of ancestor directories may not be writable by a user
2317 +process. Here aufs copy-up the file with its ancestors and they may
2318 +require privilege to set its owner/group/mode/etc.
2319 +This is a typical case of a application character of aufs (see
2320 +Introduction).
2321 +
2322 +Aufs uses workqueue synchronously for this case. It creates its own
2323 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
2324 +passes the request to call mkdir or write (for example), and wait for
2325 +its completion. This approach solves a problem of a signal handler
2326 +simply.
2327 +If aufs didn't adopt the workqueue and changed the privilege of the
2328 +process, and if the mkdir/write call arises SIGXFSZ or other signal,
2329 +then the user process might gain a privilege or the generated core file
2330 +was owned by a superuser. But I have a plan to switch to a new
2331 +credential approach which will be introduced in linux-2.6.29.
2332 +
2333 +Also aufs uses the system global workqueue ("events" kernel thread) too
2334 +for asynchronous tasks, such like handling inotify, re-creating a
2335 +whiteout base and etc. This is unrelated to a privilege.
2336 +Most of aufs operation tries acquiring a rw_semaphore for aufs
2337 +superblock at the beginning, at the same time waits for the completion
2338 +of all queued asynchronous tasks.
2339 +
2340 +
2341 +Whiteout
2342 +----------------------------------------------------------------------
2343 +The whiteout in aufs is very similar to Unionfs's. That is represented
2344 +by its filename. UnionMount takes an approach of a file mode, but I am
2345 +afraid several utilities (find(1) or something) will have to support it.
2346 +
2347 +Basically the whiteout represents "logical deletion" which stops aufs to
2348 +lookup further, but also it represents "dir is opaque" which also stop
2349 +lookup.
2350 +
2351 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
2352 +In order to make several functions in a single systemcall to be
2353 +revertible, aufs adopts an approach to rename a directory to a temporary
2354 +unique whiteouted name.
2355 +For example, in rename(2) dir where the target dir already existed, aufs
2356 +renames the target dir to a temporary unique whiteouted name before the
2357 +actual rename on a branch and then handles other actions (make it opaque,
2358 +update the attributes, etc). If an error happens in these actions, aufs
2359 +simply renames the whiteouted name back and returns an error. If all are
2360 +succeeded, aufs registers a function to remove the whiteouted unique
2361 +temporary name completely and asynchronously to the system global
2362 +workqueue.
2363 +
2364 +
2365 +Copy-up
2366 +----------------------------------------------------------------------
2367 +It is a well-known feature or concept.
2368 +When user modifies a file on a readonly branch, aufs operate "copy-up"
2369 +internally and makes change to the new file on the upper writable branch.
2370 +When the trigger systemcall does not update the timestamps of the parent
2371 +dir, aufs reverts it after copy-up.
2372 diff --git a/Documentation/filesystems/aufs/design/03lookup.txt b/Documentation/filesystems/aufs/design/03lookup.txt
2373 new file mode 100644
2374 index 0000000..36ddac7
2375 --- /dev/null
2376 +++ b/Documentation/filesystems/aufs/design/03lookup.txt
2377 @@ -0,0 +1,95 @@
2378 +
2379 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2380 +# 
2381 +# This program is free software; you can redistribute it and/or modify
2382 +# it under the terms of the GNU General Public License as published by
2383 +# the Free Software Foundation; either version 2 of the License, or
2384 +# (at your option) any later version.
2385 +
2386 +Lookup in a Branch
2387 +----------------------------------------------------------------------
2388 +Since aufs has a character of sub-VFS (see Introduction), it operates
2389 +lookup for branches as VFS does. It may be a heavy work. Generally
2390 +speaking struct nameidata is a bigger structure and includes many
2391 +information. But almost all lookup operation in aufs is the simplest
2392 +case, ie. lookup only an entry directly connected to its parent. Digging
2393 +down the directory hierarchy is unnecessary.
2394 +
2395 +VFS has a function lookup_one_len() for that use, but it is not usable
2396 +for a branch filesystem which requires struct nameidata. So aufs
2397 +implements a simple lookup wrapper function. When a branch filesystem
2398 +allows NULL as nameidata, it calls lookup_one_len(). Otherwise it builds
2399 +a simplest nameidata and calls lookup_hash().
2400 +Here aufs applies "a principle in NFSD", ie. if the filesystem supports
2401 +NFS-export, then it has to support NULL as a nameidata parameter for
2402 +->create(), ->lookup() and ->d_revalidate(). So the lookup wrapper in
2403 +aufs tests if ->s_export_op in the branch is NULL or not.
2404 +
2405 +When a branch is a remote filesystem, aufs trusts its ->d_revalidate().
2406 +For d_revalidate, aufs implements three levels of revalidate tests. See
2407 +"Revalidate Dentry and UDBA" in detail.
2408 +
2409 +
2410 +Loopback Mount
2411 +----------------------------------------------------------------------
2412 +Basically aufs supports any type of filesystem and block device for a
2413 +branch (actually there are some exceptions). But it is prohibited to add
2414 +a loopback mounted one whose backend file exists in a filesystem which is
2415 +already added to aufs. The reason is to protect aufs from a recursive
2416 +lookup. If it was allowed, the aufs lookup operation might re-enter a
2417 +lookup for the loopback mounted branch in the same context, and will
2418 +cause a deadlock.
2419 +
2420 +
2421 +Revalidate Dentry and UDBA (User's Direct Branch Access)
2422 +----------------------------------------------------------------------
2423 +Generally VFS helpers re-validate a dentry as a part of lookup.
2424 +0. digging down the directory hierarchy.
2425 +1. lock the parent dir by its i_mutex.
2426 +2. lookup the final (child) entry.
2427 +3. revalidate it.
2428 +4. call the actual operation (create, unlink, etc.)
2429 +5. unlock the parent dir
2430 +
2431 +If the filesystem implements its ->d_revalidate() (step 3), then it is
2432 +called. Actually aufs implements it and checks the dentry on a branch is
2433 +still valid.
2434 +But it is not enough. Because aufs has to release the lock for the
2435 +parent dir on a branch at the end of ->lookup() (step 2) and
2436 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
2437 +held by VFS.
2438 +If the file on a branch is changed directly, eg. bypassing aufs, after
2439 +aufs released the lock, then the subsequent operation may cause
2440 +something unpleasant result.
2441 +
2442 +This situation is a result of VFS architecture, ->lookup() and
2443 +->d_revalidate() is separated. But I never say it is wrong. It is a good
2444 +design from VFS's point of view. It is just not suitable for sub-VFS
2445 +character in aufs.
2446 +
2447 +Aufs supports such case by three level of revalidation which is
2448 +selectable by user.
2449 +1. Simple Revalidate
2450 +   Addition to the native flow in VFS's, confirm the child-parent
2451 +   relationship on the branch just after locking the parent dir on the
2452 +   branch in the "actual operation" (step 4). When this validation
2453 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
2454 +   checks the validation of the dentry on branches.
2455 +2. Monitor Changes Internally by Inotify
2456 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
2457 +   the dentry on the branch, and returns EBUSY if it finds different
2458 +   dentry.
2459 +   Additionally, aufs sets the inotify watch for every dir on branches
2460 +   during it is in cache. When the event is notified, aufs registers a
2461 +   function to kernel 'events' thread by schedule_work(). And the
2462 +   function sets some special status to the cached aufs dentry and inode
2463 +   private data. If they are not cached, then aufs has nothing to
2464 +   do. When the same file is accessed through aufs (step 0-3) later,
2465 +   aufs will detect the status and refresh all necessary data.
2466 +   In this mode, aufs has to ignore the event which is fired by aufs
2467 +   itself.
2468 +3. No Extra Validation
2469 +   This is the simplest test and doesn't add any additional revalidation
2470 +   test, and skip therevalidatin in step 4. It is useful and improves
2471 +   aufs performance when system surely hide the aufs branches from user,
2472 +   by over-mounting something (or another method).
2473 diff --git a/Documentation/filesystems/aufs/design/04branch.txt b/Documentation/filesystems/aufs/design/04branch.txt
2474 new file mode 100644
2475 index 0000000..78432c5
2476 --- /dev/null
2477 +++ b/Documentation/filesystems/aufs/design/04branch.txt
2478 @@ -0,0 +1,67 @@
2479 +
2480 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2481 +# 
2482 +# This program is free software; you can redistribute it and/or modify
2483 +# it under the terms of the GNU General Public License as published by
2484 +# the Free Software Foundation; either version 2 of the License, or
2485 +# (at your option) any later version.
2486 +
2487 +Branch Manipulation
2488 +
2489 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
2490 +and changing its permission/attribute, there are a lot of works to do.
2491 +
2492 +
2493 +Add a Branch
2494 +----------------------------------------------------------------------
2495 +o Confirm the adding dir exists outside of aufs, including loopback
2496 +  mount.
2497 +- and other various attributes...
2498 +o Initialize the xino file and whiteout bases if necessary.
2499 +  See struct.txt.
2500 +
2501 +o Check the owner/group/mode of the directory
2502 +  When the owner/group/mode of the adding directory differs from the
2503 +  existing branch, aufs issues a warning because it may impose a
2504 +  security risk.
2505 +  For example, when a upper writable branch has a world writable empty
2506 +  top directory, a malicious user can create any files on the writable
2507 +  branch directly, like copy-up and modify manually. If something like
2508 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
2509 +  writable branch, and the writable branch is world-writable, then a
2510 +  malicious guy may create /etc/passwd on the writable branch directly
2511 +  and the infected file will be valid in aufs.
2512 +  I am afraid it can be a security issue, but nothing to do except
2513 +  producing a warning.
2514 +
2515 +
2516 +Delete a Branch
2517 +----------------------------------------------------------------------
2518 +o Confirm the deleting branch is not busy
2519 +  To be general, there is one merit to adopt "remount" interface to
2520 +  manipulate branches. It is to discard caches. At deleting a branch,
2521 +  aufs checks the still cached (and connected) dentries and inodes. If
2522 +  there are any, then they are all in-use. An inode without its
2523 +  corresponding dentry can be alive alone (for example, inotify case).
2524 +
2525 +  For the cached one, aufs checks whether the same named entry exists on
2526 +  other branches.
2527 +  If the cached one is a directory, because aufs provides a merged view
2528 +  to users, as long as one dir is left on any branch aufs can show the
2529 +  dir to users. In this case, the branch can be removed from aufs.
2530 +  Otherwise aufs rejects deleting the branch.
2531 +
2532 +  If any file on the deleting branch is opened by aufs, then aufs
2533 +  rejects deleting.
2534 +
2535 +
2536 +Modify the Permission of a Branch
2537 +----------------------------------------------------------------------
2538 +o Re-initialize or remove the xino file and whiteout bases if necessary.
2539 +  See struct.txt.
2540 +
2541 +o rw --> ro: Confirm the modifying branch is not busy
2542 +  Aufs rejects the request if any of these conditions are true.
2543 +  - a file on the branch is mmap-ed.
2544 +  - a regular file on the branch is opened for write and there is no
2545 +    same named entry on the upper branch.
2546 diff --git a/Documentation/filesystems/aufs/design/05wbr_policy.txt b/Documentation/filesystems/aufs/design/05wbr_policy.txt
2547 new file mode 100644
2548 index 0000000..d9720ca
2549 --- /dev/null
2550 +++ b/Documentation/filesystems/aufs/design/05wbr_policy.txt
2551 @@ -0,0 +1,57 @@
2552 +
2553 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2554 +# 
2555 +# This program is free software; you can redistribute it and/or modify
2556 +# it under the terms of the GNU General Public License as published by
2557 +# the Free Software Foundation; either version 2 of the License, or
2558 +# (at your option) any later version.
2559 +
2560 +
2561 +Policies to Select One among Multiple Writable Branches
2562 +----------------------------------------------------------------------
2563 +When the number of writable branch is more than one, aufs has to decide
2564 +the target branch for file creation or copy-up. By default, the highest
2565 +writable branch which has the parent (or ancestor) dir of the target
2566 +file is chosen (top-down-parent policy).
2567 +By user's request, aufs implements some other policies to select the
2568 +writable branch, for file creation two policies, round-robin and
2569 +most-free-space policies. For copy-up three policies, top-down-parent,
2570 +bottom-up-parent and bottom-up policies.
2571 +
2572 +As expected, the round-robin policy selects the branch in circular. When
2573 +you have two writable branches and creates 10 new files, 5 files will be
2574 +created for each branch. mkdir(2) systemcall is an exception. When you
2575 +create 10 new directories, all will be created on the same branch.
2576 +And the most-free-space policy selects the one which has most free
2577 +space among the writable branches. The amount of free space will be
2578 +checked by aufs internally, and users can specify its time interval.
2579 +
2580 +The policies for copy-up is more simple,
2581 +top-down-parent is equivalent to the same named on in create policy,
2582 +bottom-up-parent selects the writable branch where the parent dir
2583 +exists and the nearest upper one from the copyup-source,
2584 +bottom-up selects the nearest upper writable branch from the
2585 +copyup-source, regardless the existence of the parent dir.
2586 +
2587 +There are some rules or exceptions to apply these policies.
2588 +- If there is a readonly branch above the policy-selected branch and
2589 +  the parent dir is marked as opaque (a variation of whiteout), or the
2590 +  target (creating) file is whiteout-ed on the upper readonly branch,
2591 +  then the result of the policy is ignored and the target file will be
2592 +  created on the nearest upper writable branch than the readonly branch.
2593 +- If there is a writable branch above the policy-selected branch and
2594 +  the parent dir is marked as opaque or the target file is whiteouted
2595 +  on the branch, then the result of the policy is ignored and the target
2596 +  file will be created on the highest one among the upper writable
2597 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
2598 +  it as usual.
2599 +- link(2) and rename(2) systemcalls are exceptions in every policy.
2600 +  They try selecting the branch where the source exists as possible
2601 +  since copyup a large file will take long time. If it can't be,
2602 +  ie. the branch where the source exists is readonly, then they will
2603 +  follow the copyup policy.
2604 +- There is an exception for rename(2) when the target exists.
2605 +  If the rename target exists, aufs compares the index of the branches
2606 +  where the source and the target exists and selects the higher
2607 +  one. If the selected branch is readonly, then aufs follows the
2608 +  copyup policy.
2609 diff --git a/Documentation/filesystems/aufs/design/06fmode_exec.txt b/Documentation/filesystems/aufs/design/06fmode_exec.txt
2610 new file mode 100644
2611 index 0000000..b172cd1
2612 --- /dev/null
2613 +++ b/Documentation/filesystems/aufs/design/06fmode_exec.txt
2614 @@ -0,0 +1,24 @@
2615 +
2616 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2617 +# 
2618 +# This program is free software; you can redistribute it and/or modify
2619 +# it under the terms of the GNU General Public License as published by
2620 +# the Free Software Foundation; either version 2 of the License, or
2621 +# (at your option) any later version.
2622 +
2623 +FMODE_EXEC and deny_write()
2624 +----------------------------------------------------------------------
2625 +Generally Unix prevents an executing file from writing its filedata.
2626 +In linux it is implemented by deny_write() and allow_write().
2627 +When a file is executed by exec() family, open_exec() (and sys_uselib())
2628 +they opens the file and calls deny_write(). If the file is aufs's virtual
2629 +one, it has no meaning. The file which deny_write() is really necessary
2630 +is the file on a branch. But the FMODE_EXEC flag is not passed to
2631 +->open() operation. So aufs adopt a dirty trick.
2632 +
2633 +- in order to get FMODE_EXEC, aufs ->lookup() and ->d_revalidate() set
2634 +  nd->intent.open.file->private_data to nd->intent.open.flags temporary.
2635 +- in aufs ->open(), when FMODE_EXEC is set in file->private_data, it
2636 +  calls deny_write() for the file on a branch.
2637 +- when the aufs file is released, allow_write() for the file on a branch
2638 +  is called.
2639 diff --git a/Documentation/filesystems/aufs/design/07mmap.txt b/Documentation/filesystems/aufs/design/07mmap.txt
2640 new file mode 100644
2641 index 0000000..d751c42
2642 --- /dev/null
2643 +++ b/Documentation/filesystems/aufs/design/07mmap.txt
2644 @@ -0,0 +1,44 @@
2645 +
2646 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2647 +# 
2648 +# This program is free software; you can redistribute it and/or modify
2649 +# it under the terms of the GNU General Public License as published by
2650 +# the Free Software Foundation; either version 2 of the License, or
2651 +# (at your option) any later version.
2652 +
2653 +mmap(2) -- File Memory Mapping
2654 +----------------------------------------------------------------------
2655 +In aufs, the file-mapped pages are shared between the file on a branch
2656 +and the virtual one in aufs by overriding vm_operation, particularly
2657 +->fault().
2658 +
2659 +In aufs_mmap(),
2660 +- get and store vm_ops of the real file on a branch.
2661 +- map the file of aufs by generic_file_mmap() and set aufs's vm
2662 +  operations.
2663 +
2664 +In aufs_fault(),
2665 +- get the file of aufs from the passed vma, sleep if needed.
2666 +- get the real file on a branch from the aufs file.
2667 +- a race may happen. for instance a multithreaded library. so some lock
2668 +  is implemented.
2669 +- call ->fault() in the previously stored vm_ops with setting the
2670 +  real file on a branch to vm_file.
2671 +- restore vm_file and wake_up if someone else got sleep.
2672 +
2673 +When a branch is added to or deleted from aufs, the same-named file may
2674 +unveil and its contents will be replaced by the new one when a process
2675 +read(2) through previously opened file.
2676 +(Some users may not want to refresh the filedata. For such users, I
2677 +have a plan to implement a mount option 'refrof' which decides to
2678 +refresh the opened files or not. See plan.txt too.)
2679 +In this case, an already mapped file will not be updated since the
2680 +contents are a part of a process already and it should not be changed by
2681 +aufs branch manipulation. (Even if MAP_SHARED is specified, currently).
2682 +Of course, in case of the deleting branch has a busy file, it cannot be
2683 +deleted from the union.
2684 +
2685 +In Unionfs, it took an approach which the memory pages mapped to
2686 +filedata are copied from the lower (real) file into the Unionfs's
2687 +virtual one and handles it by address_space operations. Recently Unionfs
2688 +changed it to this approach which aufs adopted since Jul 2006.
2689 diff --git a/Documentation/filesystems/aufs/design/08export.txt b/Documentation/filesystems/aufs/design/08export.txt
2690 new file mode 100644
2691 index 0000000..8a7b9e5
2692 --- /dev/null
2693 +++ b/Documentation/filesystems/aufs/design/08export.txt
2694 @@ -0,0 +1,51 @@
2695 +
2696 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2697 +# 
2698 +# This program is free software; you can redistribute it and/or modify
2699 +# it under the terms of the GNU General Public License as published by
2700 +# the Free Software Foundation; either version 2 of the License, or
2701 +# (at your option) any later version.
2702 +
2703 +
2704 +Export Aufs via NFS
2705 +----------------------------------------------------------------------
2706 +Here is an approach.
2707 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2708 +  generation.
2709 +- iget_locked(): initialize aufs inode generation for a new inode, and
2710 +  store it in xigen file.
2711 +- destroy_inode(): increment aufs inode generation and store it in xigen
2712 +  file. it is necessary even if it is not unlinked, because any data of
2713 +  inode may be changed by UDBA.
2714 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2715 +  build file handle by
2716 +  + branch id (4 bytes)
2717 +  + superblock generation (4 bytes)
2718 +  + inode number (4 or 8 bytes)
2719 +  + parent dir inode number (4 or 8 bytes)
2720 +  + inode generation (4 bytes))
2721 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2722 +    bytes)
2723 +  + file handle for a branch (by exportfs_encode_fh())
2724 +- fh_to_dentry():
2725 +  + find the index of a branch from its id in handle, and check it is
2726 +    still exist in aufs.
2727 +  + 1st level: get the inode number from handle and search it in cache.
2728 +  + 2nd level: if not found, get the parent inode number from handle and
2729 +    search it in cache. and then open the parent dir, find the matching
2730 +    inode number by vfs_readdir() and get its name, and call
2731 +    lookup_one_len() for the target dentry.
2732 +  + 3rd level: if the parent dir is not cached, call
2733 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2734 +    build a pathname of it, convert it a pathname in aufs, call
2735 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2736 +    the 2nd level.
2737 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2738 +    for every branch, but not itself. to get this, (currently) aufs
2739 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2740 +    idea, but I didn't get other approach.
2741 +  + test the generation of the gotten inode.
2742 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2743 +  convert it into ESTALE for NFSD.
2744 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2745 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2746 diff --git a/Documentation/filesystems/aufs/design/99plan.txt b/Documentation/filesystems/aufs/design/99plan.txt
2747 new file mode 100644
2748 index 0000000..3167d0e
2749 --- /dev/null
2750 +++ b/Documentation/filesystems/aufs/design/99plan.txt
2751 @@ -0,0 +1,125 @@
2752 +
2753 +# Copyright (C) 2005-2009 Junjiro R. Okajima
2754 +# 
2755 +# This program is free software; you can redistribute it and/or modify
2756 +# it under the terms of the GNU General Public License as published by
2757 +# the Free Software Foundation; either version 2 of the License, or
2758 +# (at your option) any later version.
2759 +
2760 +Plan
2761 +
2762 +Restoring some features which was implemented in aufs1.
2763 +They were dropped in aufs2 in order to make source files simpler and
2764 +easier to be reviewed.
2765 +
2766 +
2767 +Test Only the Highest One for the Directory Permission (dirperm1 option)
2768 +----------------------------------------------------------------------
2769 +Let's try case study.
2770 +- aufs has two branches, upper readwrite and lower readonly.
2771 +  /au = /rw + /ro
2772 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
2773 +- user invoked "chmod a+rx /au/dirA"
2774 +- then "dirA" becomes world readable?
2775 +
2776 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
2777 +or it may be a natively readonly filesystem. If aufs respects the lower
2778 +branch, it should not respond readdir request from other users. But user
2779 +allowed it by chmod. Should really aufs rejects showing the entries
2780 +under /ro/dirA?
2781 +
2782 +To be honest, I don't have a best solution for this case. So I
2783 +implemented 'dirperm1' and 'nodirperm1' option in aufs1, and leave it to
2784 +users.
2785 +When dirperm1 is specified, aufs checks only the highest one for the
2786 +directory permission, and shows the entries. Otherwise, as usual, checks
2787 +every dir existing on all branches and rejects the request.
2788 +
2789 +As a side effect, dirperm1 option improves the performance of aufs
2790 +because the number of permission check is reduced.
2791 +
2792 +
2793 +Show Whiteout Mode (shwh)
2794 +----------------------------------------------------------------------
2795 +Generally aufs hides the name of whiteouts. But in some cases, to show
2796 +them is very useful for users. For instance, creating a new middle layer
2797 +(branch) by merging existing layers.
2798 +
2799 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2800 +When you have three branches,
2801 +- Bottom: 'system', squashfs (underlying base system), read-only
2802 +- Middle: 'mods', squashfs, read-only
2803 +- Top: 'overlay', ram (tmpfs), read-write
2804 +
2805 +The top layer is loaded at boot time and saved at shutdown, to preserve
2806 +the changes made to the system during the session.
2807 +When larger changes have been made, or smaller changes have accumulated,
2808 +the size of the saved top layer data grows. At this point, it would be
2809 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2810 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2811 +restoring save and load speed.
2812 +
2813 +This merging is simplified by the use of another aufs mount, of just the
2814 +two overlay branches using the 'shwh' option.
2815 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2816 +       aufs /livesys/merge_union
2817 +
2818 +A merged view of these two branches is then available at
2819 +/livesys/merge_union, and the new feature is that the whiteouts are
2820 +visible!
2821 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2822 +writing to all branches. Also the default mode for all branches is 'ro'.
2823 +It is now possible to save the combined contents of the two overlay
2824 +branches to a new squashfs, e.g.:
2825 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2826 +
2827 +This new squashfs archive can be stored on the boot device and the
2828 +initramfs will use it to replace the old one at the next boot.
2829 +
2830 +
2831 +Being Another Aufs's Readonly Branch (robr)
2832 +----------------------------------------------------------------------
2833 +Aufs1 allows aufs to be another aufs's readonly branch.
2834 +This feature was developed by a user's request. But it may not be used
2835 +currecnly.
2836 +
2837 +
2838 +Copy-up on Open (coo=)
2839 +----------------------------------------------------------------------
2840 +By default the internal copy-up is executed when it is really necessary.
2841 +It is not done when a file is opened for writing, but when write(2) is
2842 +done. Users who have many (over 100) branches want to know and analyse
2843 +when and what file is copied-up. To insert a new upper branch which
2844 +contains such files only may improve the performance of aufs.
2845 +
2846 +Aufs1 implemented "coo=none | leaf | all" option.
2847 +
2848 +
2849 +Refresh the Opened File (refrof)
2850 +----------------------------------------------------------------------
2851 +This option is implemented in aufs1 but incomplete.
2852 +
2853 +When user reads from a file, he expects to get its latest filedata
2854 +generally. If the file is removed and a new same named file is created,
2855 +the content he gets is unchanged, ie. the unlinked filedata.
2856 +
2857 +Let's try case study again.
2858 +- aufs has two branches.
2859 +  /au = /rw + /ro
2860 +- "fileA" exists under /ro, but /rw.
2861 +- user opened "/au/fileA".
2862 +- he or someone else inserts a branch (/new) between /rw and /ro.
2863 +  /au = /rw + /new + /ro
2864 +- the new branch has "fileA".
2865 +- user reads from the opened "fileA"
2866 +- which filedata should aufs return, from /ro or /new?
2867 +
2868 +Some people says it has to be "from /ro" and it is a semantics of Unix.
2869 +The others say it should be "from /new" because the file is not removed
2870 +and it is equivalent to the case of someone else modifies the file.
2871 +
2872 +Here again I don't have a best and final answer. I got an idea to
2873 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
2874 +Opened File) is specified (by default), aufs returns the filedata from
2875 +/new.
2876 +Otherwise from /new.
2877 diff --git a/fs/Kconfig b/fs/Kconfig
2878 index 93945dd..75156dd 100644
2879 --- a/fs/Kconfig
2880 +++ b/fs/Kconfig
2881 @@ -222,6 +222,7 @@ source "fs/qnx4/Kconfig"
2882  source "fs/romfs/Kconfig"
2883  source "fs/sysv/Kconfig"
2884  source "fs/ufs/Kconfig"
2885 +source "fs/aufs/Kconfig"
2886  
2887  endif # MISC_FILESYSTEMS
2888  
2889 diff --git a/fs/Makefile b/fs/Makefile
2890 index dc20db3..a4e9a65 100644
2891 --- a/fs/Makefile
2892 +++ b/fs/Makefile
2893 @@ -124,3 +124,4 @@ obj-$(CONFIG_DEBUG_FS)              += debugfs/
2894  obj-$(CONFIG_OCFS2_FS)         += ocfs2/
2895  obj-$(CONFIG_BTRFS_FS)         += btrfs/
2896  obj-$(CONFIG_GFS2_FS)           += gfs2/
2897 +obj-$(CONFIG_AUFS_FS)           += aufs/
2898 diff --git a/fs/aufs/Kconfig b/fs/aufs/Kconfig
2899 new file mode 100644
2900 index 0000000..04b2060
2901 --- /dev/null
2902 +++ b/fs/aufs/Kconfig
2903 @@ -0,0 +1,109 @@
2904 +config AUFS_FS
2905 +       bool "Aufs (Advanced multi layered unification filesystem) support"
2906 +       depends on EXPERIMENTAL
2907 +       help
2908 +       Aufs is a stackable unification filesystem such as Unionfs,
2909 +       which unifies several directories and provides a merged single
2910 +       directory.
2911 +       In the early days, aufs was entirely re-designed and
2912 +       re-implemented Unionfs Version 1.x series. Introducing many
2913 +       original ideas, approaches and improvements, it becomes totally
2914 +       different from Unionfs while keeping the basic features.
2915 +
2916 +if AUFS_FS
2917 +choice
2918 +       prompt "Maximum number of branches"
2919 +       default AUFS_BRANCH_MAX_127
2920 +       help
2921 +       Specifies the maximum number of branches (or member directories)
2922 +       in a single aufs. The larger value consumes more system
2923 +       resources and has a minor impact to performance.
2924 +config AUFS_BRANCH_MAX_127
2925 +       bool "127"
2926 +       help
2927 +       Specifies the maximum number of branches (or member directories)
2928 +       in a single aufs. The larger value consumes more system
2929 +       resources and has a minor impact to performance.
2930 +config AUFS_BRANCH_MAX_511
2931 +       bool "511"
2932 +       help
2933 +       Specifies the maximum number of branches (or member directories)
2934 +       in a single aufs. The larger value consumes more system
2935 +       resources and has a minor impact to performance.
2936 +config AUFS_BRANCH_MAX_1023
2937 +       bool "1023"
2938 +       help
2939 +       Specifies the maximum number of branches (or member directories)
2940 +       in a single aufs. The larger value consumes more system
2941 +       resources and has a minor impact to performance.
2942 +config AUFS_BRANCH_MAX_32767
2943 +       bool "32767"
2944 +       help
2945 +       Specifies the maximum number of branches (or member directories)
2946 +       in a single aufs. The larger value consumes more system
2947 +       resources and has a minor impact to performance.
2948 +endchoice
2949 +
2950 +config AUFS_HINOTIFY
2951 +       bool "Use inotify to detect actions on a branch"
2952 +       depends on INOTIFY
2953 +       help
2954 +       If you want to modify files on branches directly, eg. bypassing aufs,
2955 +       and want aufs to detect the changes of them fully, then enable this
2956 +       option and use 'udba=inotify' mount option.
2957 +       It will have a negative impact to the performance.
2958 +       See detail in aufs.5.
2959 +
2960 +config AUFS_EXPORT
2961 +       bool "NFS-exportable aufs"
2962 +       depends on EXPORTFS = y
2963 +       help
2964 +       If you want to export your mounted aufs via NFS, then enable this
2965 +       option. There are several requirements for this configuration.
2966 +       See detail in aufs.5.
2967 +
2968 +config AUFS_BR_RAMFS
2969 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
2970 +       help
2971 +       If you want to use ramfs as an aufs branch fs, then enable this
2972 +       option. Generally tmpfs is recommended.
2973 +       Aufs prohibited them to be a branch fs by default, because
2974 +       initramfs becomes unusable after switch_root or something
2975 +       generally. If you sets initramfs as an aufs branch and boot your
2976 +       system by switch_root, you will meet a problem easily since the
2977 +       files in initramfs may be inaccessible.
2978 +       Unless you are going to use ramfs as an aufs branch fs without
2979 +       switch_root or something, leave it N.
2980 +
2981 +config AUFS_DEBUG
2982 +       bool "Debug aufs"
2983 +       help
2984 +       Enable this to compile aufs internal debug code.
2985 +       It will have a negative impact to the performance.
2986 +
2987 +config AUFS_MAGIC_SYSRQ
2988 +       bool
2989 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
2990 +       default y
2991 +       help
2992 +       Automatic configuration for internal use.
2993 +       When aufs supports Magic SysRq, enabled automatically.
2994 +
2995 +config AUFS_BDEV_LOOP
2996 +       bool
2997 +       depends on BLK_DEV_LOOP
2998 +       default y
2999 +       help
3000 +       Automatic configuration for internal use.
3001 +       Convert =[ym] into =y.
3002 +
3003 +config AUFS_INO_T_64
3004 +       bool
3005 +       depends on AUFS_EXPORT
3006 +       depends on 64BIT && !(ALPHA || S390)
3007 +       default y
3008 +       help
3009 +       Automatic configuration for internal use.
3010 +       /* typedef unsigned long/int __kernel_ino_t */
3011 +       /* alpha and s390x are int */
3012 +endif
3013 diff --git a/fs/aufs/Makefile b/fs/aufs/Makefile
3014 new file mode 100644
3015 index 0000000..477d245
3016 --- /dev/null
3017 +++ b/fs/aufs/Makefile
3018 @@ -0,0 +1,21 @@
3019 +
3020 +include ${srctree}/${src}/magic.mk
3021 +
3022 +obj-$(CONFIG_AUFS_FS) += aufs.o
3023 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
3024 +       wkq.o vfsub.o dcsub.o \
3025 +       cpup.o whout.o plink.o wbr_policy.o \
3026 +       dinfo.o dentry.o \
3027 +       finfo.o file.o f_op.o \
3028 +       dir.o vdir.o \
3029 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
3030 +       ioctl.o
3031 +
3032 +# all are boolean
3033 +aufs-$(CONFIG_SYSFS) += sysfs.o
3034 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
3035 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
3036 +aufs-$(CONFIG_AUFS_HINOTIFY) += hinotify.o
3037 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
3038 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
3039 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
3040 diff --git a/fs/aufs/aufs.h b/fs/aufs/aufs.h
3041 new file mode 100644
3042 index 0000000..132e84e
3043 --- /dev/null
3044 +++ b/fs/aufs/aufs.h
3045 @@ -0,0 +1,44 @@
3046 +/*
3047 + * Copyright (C) 2005-2009 Junjiro R. Okajima
3048 + *
3049 + * This program, aufs is free software; you can redistribute it and/or modify
3050 + * it under the terms of the GNU General Public License as published by
3051 + * the Free Software Foundation; either version 2 of the License, or
3052 + * (at your option) any later version.
3053 + */
3054 +
3055 +/*
3056 + * all header files
3057 + */
3058 +
3059 +#ifndef __AUFS_H__
3060 +#define __AUFS_H__
3061 +
3062 +#ifdef __KERNEL__
3063 +
3064 +#include <linux/bug.h>
3065 +
3066 +#include "debug.h"
3067 +
3068 +#include "branch.h"
3069 +#include "cpup.h"
3070 +#include "dcsub.h"
3071 +#include "dbgaufs.h"
3072 +#include "dentry.h"
3073 +#include "dir.h"
3074 +#include "file.h"
3075 +#include "fstype.h"
3076 +#include "inode.h"
3077 +#include "loop.h"
3078 +#include "module.h"
3079 +#include "opts.h"
3080 +#include "rwsem.h"
3081 +#include "spl.h"
3082 +#include "super.h"
3083 +#include "sysaufs.h"
3084 +#include "vfsub.h"
3085 +#include "whout.h"
3086 +#include "wkq.h"
3087 +
3088 +#endif /* __KERNEL__ */
3089 +#endif /* __AUFS_H__ */
3090 diff --git a/fs/aufs/branch.c b/fs/aufs/branch.c
3091 new file mode 100644
3092 index 0000000..7ef8a18
3093 --- /dev/null
3094 +++ b/fs/aufs/branch.c
3095 @@ -0,0 +1,946 @@
3096 +/*
3097 + * Copyright (C) 2005-2009 Junjiro R. Okajima
3098 + *
3099 + * This program, aufs is free software; you can redistribute it and/or modify
3100 + * it under the terms of the GNU General Public License as published by
3101 + * the Free Software Foundation; either version 2 of the License, or
3102 + * (at your option) any later version.
3103 + */
3104 +
3105 +/*
3106 + * branch management
3107 + */
3108 +
3109 +#include "aufs.h"
3110 +
3111 +/*
3112 + * free a single branch
3113 + */
3114 +static void au_br_do_free(struct au_branch *br)
3115 +{
3116 +       int i;
3117 +       struct au_wbr *wbr;
3118 +
3119 +       if (br->br_xino.xi_file)
3120 +               fput(br->br_xino.xi_file);
3121 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
3122 +
3123 +       AuDebugOn(atomic_read(&br->br_count));
3124 +
3125 +       wbr = br->br_wbr;
3126 +       if (wbr) {
3127 +               for (i = 0; i < AuBrWh_Last; i++)
3128 +                       dput(wbr->wbr_wh[i]);
3129 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3130 +               au_rwsem_destroy(&wbr->wbr_wh_rwsem);
3131 +       }
3132 +
3133 +       /* some filesystems acquire extra lock */
3134 +       lockdep_off();
3135 +       mntput(br->br_mnt);
3136 +       lockdep_on();
3137 +
3138 +       kfree(wbr);
3139 +       kfree(br);
3140 +}
3141 +
3142 +/*
3143 + * frees all branches
3144 + */
3145 +void au_br_free(struct au_sbinfo *sbinfo)
3146 +{
3147 +       aufs_bindex_t bmax;
3148 +       struct au_branch **br;
3149 +
3150 +       bmax = sbinfo->si_bend + 1;
3151 +       br = sbinfo->si_branch;
3152 +       while (bmax--)
3153 +               au_br_do_free(*br++);
3154 +}
3155 +
3156 +/*
3157 + * find the index of a branch which is specified by @br_id.
3158 + */
3159 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3160 +{
3161 +       aufs_bindex_t bindex, bend;
3162 +
3163 +       bend = au_sbend(sb);
3164 +       for (bindex = 0; bindex <= bend; bindex++)
3165 +               if (au_sbr_id(sb, bindex) == br_id)
3166 +                       return bindex;
3167 +       return -1;
3168 +}
3169 +
3170 +/* ---------------------------------------------------------------------- */
3171 +
3172 +/*
3173 + * add a branch
3174 + */
3175 +
3176 +static int test_overlap(struct super_block *sb, struct dentry *h_d1,
3177 +                       struct dentry *h_d2)
3178 +{
3179 +       if (unlikely(h_d1 == h_d2))
3180 +               return 1;
3181 +       return !!au_test_subdir(h_d1, h_d2)
3182 +               || !!au_test_subdir(h_d2, h_d1)
3183 +               || au_test_loopback_overlap(sb, h_d1, h_d2)
3184 +               || au_test_loopback_overlap(sb, h_d2, h_d1);
3185 +}
3186 +
3187 +/*
3188 + * returns a newly allocated branch. @new_nbranch is a number of branches
3189 + * after adding a branch.
3190 + */
3191 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3192 +                                    int perm)
3193 +{
3194 +       struct au_branch *add_branch;
3195 +       struct dentry *root;
3196 +
3197 +       root = sb->s_root;
3198 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
3199 +       if (unlikely(!add_branch))
3200 +               goto out;
3201 +
3202 +       add_branch->br_wbr = NULL;
3203 +       if (au_br_writable(perm)) {
3204 +               /* may be freed separately at changing the branch permission */
3205 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
3206 +                                            GFP_NOFS);
3207 +               if (unlikely(!add_branch->br_wbr))
3208 +                       goto out_br;
3209 +       }
3210 +
3211 +       if (unlikely(au_sbr_realloc(au_sbi(sb), new_nbranch)
3212 +                    || au_di_realloc(au_di(root), new_nbranch)
3213 +                    || au_ii_realloc(au_ii(root->d_inode), new_nbranch)))
3214 +               goto out_wbr;
3215 +       return add_branch; /* success */
3216 +
3217 + out_wbr:
3218 +       kfree(add_branch->br_wbr);
3219 + out_br:
3220 +       kfree(add_branch);
3221 + out:
3222 +       return ERR_PTR(-ENOMEM);
3223 +}
3224 +
3225 +/*
3226 + * test if the branch permission is legal or not.
3227 + */
3228 +static int test_br(struct inode *inode, int brperm, char *path)
3229 +{
3230 +       int err;
3231 +
3232 +       err = 0;
3233 +       if (unlikely(au_br_writable(brperm) && IS_RDONLY(inode))) {
3234 +               AuErr("write permission for readonly mount or inode, %s\n",
3235 +                     path);
3236 +               err = -EINVAL;
3237 +       }
3238 +
3239 +       return err;
3240 +}
3241 +
3242 +/*
3243 + * returns:
3244 + * 0: success, the caller will add it
3245 + * plus: success, it is already unified, the caller should ignore it
3246 + * minus: error
3247 + */
3248 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3249 +{
3250 +       int err;
3251 +       aufs_bindex_t bend, bindex;
3252 +       struct dentry *root;
3253 +       struct inode *inode, *h_inode;
3254 +
3255 +       root = sb->s_root;
3256 +       bend = au_sbend(sb);
3257 +       if (unlikely(bend >= 0
3258 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3259 +               err = 1;
3260 +               if (!remount) {
3261 +                       err = -EINVAL;
3262 +                       AuErr("%s duplicated\n", add->pathname);
3263 +               }
3264 +               goto out;
3265 +       }
3266 +
3267 +       err = -ENOSPC; /* -E2BIG; */
3268 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3269 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
3270 +               AuErr("number of branches exceeded %s\n", add->pathname);
3271 +               goto out;
3272 +       }
3273 +
3274 +       err = -EDOM;
3275 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
3276 +               AuErr("bad index %d\n", add->bindex);
3277 +               goto out;
3278 +       }
3279 +
3280 +       inode = add->path.dentry->d_inode;
3281 +       err = -ENOENT;
3282 +       if (unlikely(!inode->i_nlink)) {
3283 +               AuErr("no existence %s\n", add->pathname);
3284 +               goto out;
3285 +       }
3286 +
3287 +       err = -EINVAL;
3288 +       if (unlikely(inode->i_sb == sb)) {
3289 +               AuErr("%s must be outside\n", add->pathname);
3290 +               goto out;
3291 +       }
3292 +
3293 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3294 +               AuErr("unsupported filesystem, %s (%s)\n",
3295 +                     add->pathname, au_sbtype(inode->i_sb));
3296 +               goto out;
3297 +       }
3298 +
3299 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
3300 +       if (unlikely(err))
3301 +               goto out;
3302 +
3303 +       if (bend < 0)
3304 +               return 0; /* success */
3305 +
3306 +       err = -EINVAL;
3307 +       for (bindex = 0; bindex <= bend; bindex++)
3308 +               if (unlikely(test_overlap(sb, add->path.dentry,
3309 +                                         au_h_dptr(root, bindex)))) {
3310 +                       AuErr("%s is overlapped\n", add->pathname);
3311 +                       goto out;
3312 +               }
3313 +
3314 +       err = 0;
3315 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3316 +               h_inode = au_h_dptr(root, 0)->d_inode;
3317 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3318 +                   || h_inode->i_uid != inode->i_uid
3319 +                   || h_inode->i_gid != inode->i_gid)
3320 +                       AuWarn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3321 +                              add->pathname,
3322 +                              inode->i_uid, inode->i_gid,
3323 +                              (inode->i_mode & S_IALLUGO),
3324 +                              h_inode->i_uid, h_inode->i_gid,
3325 +                              (h_inode->i_mode & S_IALLUGO));
3326 +       }
3327 +
3328 + out:
3329 +       return err;
3330 +}
3331 +
3332 +/*
3333 + * initialize or clean the whiteouts for an adding branch
3334 + */
3335 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3336 +                        int new_perm, struct dentry *h_root)
3337 +{
3338 +       int err, old_perm;
3339 +       aufs_bindex_t bindex;
3340 +       struct mutex *h_mtx;
3341 +       struct au_wbr *wbr;
3342 +       struct au_hinode *hdir;
3343 +
3344 +       wbr = br->br_wbr;
3345 +       old_perm = br->br_perm;
3346 +       br->br_perm = new_perm;
3347 +       hdir = NULL;
3348 +       h_mtx = NULL;
3349 +       bindex = au_br_index(sb, br->br_id);
3350 +       if (0 <= bindex) {
3351 +               hdir = au_hi(sb->s_root->d_inode, bindex);
3352 +               au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
3353 +       } else {
3354 +               h_mtx = &h_root->d_inode->i_mutex;
3355 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
3356 +       }
3357 +       if (!wbr)
3358 +               err = au_wh_init(h_root, br, sb);
3359 +       else {
3360 +               wbr_wh_write_lock(wbr);
3361 +               err = au_wh_init(h_root, br, sb);
3362 +               wbr_wh_write_unlock(wbr);
3363 +       }
3364 +       if (hdir)
3365 +               au_hin_imtx_unlock(hdir);
3366 +       else
3367 +               mutex_unlock(h_mtx);
3368 +       br->br_perm = old_perm;
3369 +
3370 +       if (!err && wbr && !au_br_writable(new_perm)) {
3371 +               kfree(wbr);
3372 +               br->br_wbr = NULL;
3373 +       }
3374 +
3375 +       return err;
3376 +}
3377 +
3378 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3379 +                      int perm, struct path *path)
3380 +{
3381 +       int err;
3382 +       struct au_wbr *wbr;
3383 +
3384 +       wbr = br->br_wbr;
3385 +       init_rwsem(&wbr->wbr_wh_rwsem);
3386 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
3387 +       atomic_set(&wbr->wbr_wh_running, 0);
3388 +       wbr->wbr_bytes = 0;
3389 +
3390 +       err = au_br_init_wh(sb, br, perm, path->dentry);
3391 +
3392 +       return err;
3393 +}
3394 +
3395 +/* intialize a new branch */
3396 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3397 +                     struct au_opt_add *add)
3398 +{
3399 +       int err;
3400 +
3401 +       err = 0;
3402 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
3403 +       mutex_init(&br->br_xino.xi_nondir_mtx);
3404 +       br->br_perm = add->perm;
3405 +       br->br_mnt = add->path.mnt; /* set first, mntget() later */
3406 +       atomic_set(&br->br_count, 0);
3407 +       br->br_xino_upper = AUFS_XINO_TRUNC_INIT;
3408 +       atomic_set(&br->br_xino_running, 0);
3409 +       br->br_id = au_new_br_id(sb);
3410 +
3411 +       if (au_br_writable(add->perm)) {
3412 +               err = au_wbr_init(br, sb, add->perm, &add->path);
3413 +               if (unlikely(err))
3414 +                       goto out;
3415 +       }
3416 +
3417 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3418 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
3419 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3420 +               if (unlikely(err)) {
3421 +                       AuDebugOn(br->br_xino.xi_file);
3422 +                       goto out;
3423 +               }
3424 +       }
3425 +
3426 +       sysaufs_br_init(br);
3427 +       mntget(add->path.mnt);
3428 +
3429 + out:
3430 +       return err;
3431 +}
3432 +
3433 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3434 +                            struct au_branch *br, aufs_bindex_t bend,
3435 +                            aufs_bindex_t amount)
3436 +{
3437 +       struct au_branch **brp;
3438 +
3439 +       brp = sbinfo->si_branch + bindex;
3440 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3441 +       *brp = br;
3442 +       sbinfo->si_bend++;
3443 +       if (unlikely(bend < 0))
3444 +               sbinfo->si_bend = 0;
3445 +}
3446 +
3447 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3448 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3449 +{
3450 +       struct au_hdentry *hdp;
3451 +
3452 +       hdp = dinfo->di_hdentry + bindex;
3453 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3454 +       au_h_dentry_init(hdp);
3455 +       dinfo->di_bend++;
3456 +       if (unlikely(bend < 0))
3457 +               dinfo->di_bstart = 0;
3458 +}
3459 +
3460 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3461 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3462 +{
3463 +       struct au_hinode *hip;
3464 +
3465 +       hip = iinfo->ii_hinode + bindex;
3466 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3467 +       hip->hi_inode = NULL;
3468 +       au_hin_init(hip, NULL);
3469 +       iinfo->ii_bend++;
3470 +       if (unlikely(bend < 0))
3471 +               iinfo->ii_bstart = 0;
3472 +}
3473 +
3474 +static void au_br_do_add(struct super_block *sb, struct dentry *h_dentry,
3475 +                        struct au_branch *br, aufs_bindex_t bindex)
3476 +{
3477 +       struct dentry *root;
3478 +       struct inode *root_inode;
3479 +       aufs_bindex_t bend, amount;
3480 +
3481 +       root = sb->s_root;
3482 +       root_inode = root->d_inode;
3483 +       au_plink_block_maintain(sb);
3484 +       bend = au_sbend(sb);
3485 +       amount = bend + 1 - bindex;
3486 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
3487 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
3488 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
3489 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3490 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
3491 +                     /*flags*/0);
3492 +}
3493 +
3494 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3495 +{
3496 +       int err;
3497 +       unsigned long long maxb;
3498 +       aufs_bindex_t bend, add_bindex;
3499 +       struct dentry *root, *h_dentry;
3500 +       struct inode *root_inode;
3501 +       struct au_branch *add_branch;
3502 +
3503 +       root = sb->s_root;
3504 +       root_inode = root->d_inode;
3505 +       IMustLock(root_inode);
3506 +       err = test_add(sb, add, remount);
3507 +       if (unlikely(err < 0))
3508 +               goto out;
3509 +       if (err) {
3510 +               err = 0;
3511 +               goto out; /* success */
3512 +       }
3513 +
3514 +       bend = au_sbend(sb);
3515 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
3516 +       err = PTR_ERR(add_branch);
3517 +       if (IS_ERR(add_branch))
3518 +               goto out;
3519 +
3520 +       err = au_br_init(add_branch, sb, add);
3521 +       if (unlikely(err)) {
3522 +               au_br_do_free(add_branch);
3523 +               goto out;
3524 +       }
3525 +
3526 +       add_bindex = add->bindex;
3527 +       h_dentry = add->path.dentry;
3528 +       if (!remount)
3529 +               au_br_do_add(sb, h_dentry, add_branch, add_bindex);
3530 +       else {
3531 +               sysaufs_brs_del(sb, add_bindex);
3532 +               au_br_do_add(sb, h_dentry, add_branch, add_bindex);
3533 +               sysaufs_brs_add(sb, add_bindex);
3534 +       }
3535 +
3536 +       if (!add_bindex)
3537 +               au_cpup_attr_all(root_inode, /*force*/1);
3538 +       else
3539 +               au_add_nlink(root_inode, h_dentry->d_inode);
3540 +       maxb = h_dentry->d_sb->s_maxbytes;
3541 +       if (sb->s_maxbytes < maxb)
3542 +               sb->s_maxbytes = maxb;
3543 +
3544 +       /*
3545 +        * this test/set prevents aufs from handling unnecesary inotify events
3546 +        * of xino files, in a case of re-adding a writable branch which was
3547 +        * once detached from aufs.
3548 +        */
3549 +       if (au_xino_brid(sb) < 0
3550 +           && au_br_writable(add_branch->br_perm)
3551 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3552 +           && add_branch->br_xino.xi_file
3553 +           && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
3554 +               au_xino_brid_set(sb, add_branch->br_id);
3555 +
3556 + out:
3557 +       return err;
3558 +}
3559 +
3560 +/* ---------------------------------------------------------------------- */
3561 +
3562 +/*
3563 + * delete a branch
3564 + */
3565 +
3566 +/* to show the line number, do not make it inlined function */
3567 +#define AuVerbose(do_info, fmt, args...) do { \
3568 +       if (do_info) \
3569 +               AuInfo(fmt, ##args); \
3570 +} while (0)
3571 +
3572 +/*
3573 + * test if the branch is deletable or not.
3574 + */
3575 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3576 +                           unsigned int sigen)
3577 +{
3578 +       int err, i, j, ndentry;
3579 +       aufs_bindex_t bstart, bend;
3580 +       unsigned char verbose;
3581 +       struct au_dcsub_pages dpages;
3582 +       struct au_dpage *dpage;
3583 +       struct dentry *d;
3584 +       struct inode *inode;
3585 +
3586 +       err = au_dpages_init(&dpages, GFP_NOFS);
3587 +       if (unlikely(err))
3588 +               goto out;
3589 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3590 +       if (unlikely(err))
3591 +               goto out_dpages;
3592 +
3593 +       verbose = !!au_opt_test(au_mntflags(root->d_sb), VERBOSE);
3594 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3595 +               dpage = dpages.dpages + i;
3596 +               ndentry = dpage->ndentry;
3597 +               for (j = 0; !err && j < ndentry; j++) {
3598 +                       d = dpage->dentries[j];
3599 +                       AuDebugOn(!atomic_read(&d->d_count));
3600 +                       inode = d->d_inode;
3601 +                       if (au_digen(d) == sigen && au_iigen(inode) == sigen)
3602 +                               di_read_lock_child(d, AuLock_IR);
3603 +                       else {
3604 +                               di_write_lock_child(d);
3605 +                               err = au_reval_dpath(d, sigen);
3606 +                               if (!err)
3607 +                                       di_downgrade_lock(d, AuLock_IR);
3608 +                               else {
3609 +                                       di_write_unlock(d);
3610 +                                       break;
3611 +                               }
3612 +                       }
3613 +
3614 +                       bstart = au_dbstart(d);
3615 +                       bend = au_dbend(d);
3616 +                       if (bstart <= bindex
3617 +                           && bindex <= bend
3618 +                           && au_h_dptr(d, bindex)
3619 +                           && (!S_ISDIR(inode->i_mode) || bstart == bend)) {
3620 +                               err = -EBUSY;
3621 +                               AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
3622 +                       }
3623 +                       di_read_unlock(d, AuLock_IR);
3624 +               }
3625 +       }
3626 +
3627 + out_dpages:
3628 +       au_dpages_free(&dpages);
3629 + out:
3630 +       return err;
3631 +}
3632 +
3633 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3634 +                          unsigned int sigen)
3635 +{
3636 +       int err;
3637 +       struct inode *i;
3638 +       aufs_bindex_t bstart, bend;
3639 +       unsigned char verbose;
3640 +
3641 +       err = 0;
3642 +       verbose = !!au_opt_test(au_mntflags(sb), VERBOSE);
3643 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
3644 +               AuDebugOn(!atomic_read(&i->i_count));
3645 +               if (!list_empty(&i->i_dentry))
3646 +                       continue;
3647 +
3648 +               if (au_iigen(i) == sigen)
3649 +                       ii_read_lock_child(i);
3650 +               else {
3651 +                       ii_write_lock_child(i);
3652 +                       err = au_refresh_hinode_self(i, /*do_attr*/1);
3653 +                       if (!err)
3654 +                               ii_downgrade_lock(i);
3655 +                       else {
3656 +                               ii_write_unlock(i);
3657 +                               break;
3658 +                       }
3659 +               }
3660 +
3661 +               bstart = au_ibstart(i);
3662 +               bend = au_ibend(i);
3663 +               if (bstart <= bindex
3664 +                   && bindex <= bend
3665 +                   && au_h_iptr(i, bindex)
3666 +                   && (!S_ISDIR(i->i_mode) || bstart == bend)) {
3667 +                       err = -EBUSY;
3668 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3669 +                       ii_read_unlock(i);
3670 +                       break;
3671 +               }
3672 +               ii_read_unlock(i);
3673 +       }
3674 +
3675 +       return err;
3676 +}
3677 +
3678 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex)
3679 +{
3680 +       int err;
3681 +       unsigned int sigen;
3682 +
3683 +       sigen = au_sigen(root->d_sb);
3684 +       DiMustNoWaiters(root);
3685 +       IiMustNoWaiters(root->d_inode);
3686 +       di_write_unlock(root);
3687 +       err = test_dentry_busy(root, bindex, sigen);
3688 +       if (!err)
3689 +               err = test_inode_busy(root->d_sb, bindex, sigen);
3690 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3691 +
3692 +       return err;
3693 +}
3694 +
3695 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3696 +                            const aufs_bindex_t bindex,
3697 +                            const aufs_bindex_t bend)
3698 +{
3699 +       struct au_branch **brp, **p;
3700 +
3701 +       brp = sbinfo->si_branch + bindex;
3702 +       if (bindex < bend)
3703 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3704 +       sbinfo->si_branch[0 + bend] = NULL;
3705 +       sbinfo->si_bend--;
3706 +
3707 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, GFP_NOFS);
3708 +       if (p)
3709 +               sbinfo->si_branch = p;
3710 +}
3711 +
3712 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3713 +                            const aufs_bindex_t bend)
3714 +{
3715 +       struct au_hdentry *hdp, *p;
3716 +
3717 +       hdp = dinfo->di_hdentry + bindex;
3718 +       if (bindex < bend)
3719 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bend - bindex));
3720 +       dinfo->di_hdentry[0 + bend].hd_dentry = NULL;
3721 +       dinfo->di_bend--;
3722 +
3723 +       p = krealloc(dinfo->di_hdentry, sizeof(*p) * bend, GFP_NOFS);
3724 +       if (p)
3725 +               dinfo->di_hdentry = p;
3726 +}
3727 +
3728 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3729 +                            const aufs_bindex_t bend)
3730 +{
3731 +       struct au_hinode *hip, *p;
3732 +
3733 +       hip = iinfo->ii_hinode + bindex;
3734 +       if (bindex < bend)
3735 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3736 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3737 +       au_hin_init(iinfo->ii_hinode + bend, NULL);
3738 +       iinfo->ii_bend--;
3739 +
3740 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, GFP_NOFS);
3741 +       if (p)
3742 +               iinfo->ii_hinode = p;
3743 +}
3744 +
3745 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3746 +                        struct au_branch *br)
3747 +{
3748 +       aufs_bindex_t bend;
3749 +       struct au_sbinfo *sbinfo;
3750 +       struct dentry *root;
3751 +       struct inode *inode;
3752 +
3753 +       root = sb->s_root;
3754 +       inode = root->d_inode;
3755 +       au_plink_block_maintain(sb);
3756 +       sbinfo = au_sbi(sb);
3757 +       bend = sbinfo->si_bend;
3758 +
3759 +       dput(au_h_dptr(root, bindex));
3760 +       au_hiput(au_hi(inode, bindex));
3761 +       au_br_do_free(br);
3762 +
3763 +       au_br_do_del_brp(sbinfo, bindex, bend);
3764 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3765 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3766 +}
3767 +
3768 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3769 +{
3770 +       int err, rerr, i;
3771 +       unsigned int mnt_flags;
3772 +       aufs_bindex_t bindex, bend, br_id;
3773 +       unsigned char do_wh, verbose;
3774 +       struct au_branch *br;
3775 +       struct au_wbr *wbr;
3776 +
3777 +       err = 0;
3778 +       bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
3779 +       if (bindex < 0) {
3780 +               if (remount)
3781 +                       goto out; /* success */
3782 +               err = -ENOENT;
3783 +               AuErr("%s no such branch\n", del->pathname);
3784 +               goto out;
3785 +       }
3786 +       AuDbg("bindex b%d\n", bindex);
3787 +
3788 +       err = -EBUSY;
3789 +       mnt_flags = au_mntflags(sb);
3790 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3791 +       bend = au_sbend(sb);
3792 +       if (unlikely(!bend)) {
3793 +               AuVerbose(verbose, "no more branches left\n");
3794 +               goto out;
3795 +       }
3796 +       br = au_sbr(sb, bindex);
3797 +       i = atomic_read(&br->br_count);
3798 +       if (unlikely(i)) {
3799 +               AuVerbose(verbose, "%d file(s) opened\n", i);
3800 +               goto out;
3801 +       }
3802 +
3803 +       wbr = br->br_wbr;
3804 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3805 +       if (do_wh) {
3806 +               for (i = 0; i < AuBrWh_Last; i++) {
3807 +                       dput(wbr->wbr_wh[i]);
3808 +                       wbr->wbr_wh[i] = NULL;
3809 +               }
3810 +       }
3811 +
3812 +       err = test_children_busy(sb->s_root, bindex);
3813 +       if (unlikely(err)) {
3814 +               if (do_wh)
3815 +                       goto out_wh;
3816 +               goto out;
3817 +       }
3818 +
3819 +       err = 0;
3820 +       br_id = br->br_id;
3821 +       if (!remount)
3822 +               au_br_do_del(sb, bindex, br);
3823 +       else {
3824 +               sysaufs_brs_del(sb, bindex);
3825 +               au_br_do_del(sb, bindex, br);
3826 +               sysaufs_brs_add(sb, bindex);
3827 +       }
3828 +
3829 +       if (!bindex)
3830 +               au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
3831 +       else
3832 +               au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
3833 +       if (au_opt_test(mnt_flags, PLINK))
3834 +               au_plink_half_refresh(sb, br_id);
3835 +
3836 +       if (sb->s_maxbytes == del->h_path.dentry->d_sb->s_maxbytes) {
3837 +               bend--;
3838 +               sb->s_maxbytes = 0;
3839 +               for (bindex = 0; bindex <= bend; bindex++) {
3840 +                       unsigned long long maxb;
3841 +
3842 +                       maxb = au_sbr_sb(sb, bindex)->s_maxbytes;
3843 +                       if (sb->s_maxbytes < maxb)
3844 +                               sb->s_maxbytes = maxb;
3845 +               }
3846 +       }
3847 +
3848 +       if (au_xino_brid(sb) == br->br_id)
3849 +               au_xino_brid_set(sb, -1);
3850 +       goto out; /* success */
3851 +
3852 + out_wh:
3853 +       /* revert */
3854 +       rerr = au_br_init_wh(sb, br, br->br_perm, del->h_path.dentry);
3855 +       if (rerr)
3856 +               AuWarn("failed re-creating base whiteout, %s. (%d)\n",
3857 +                      del->pathname, rerr);
3858 + out:
3859 +       return err;
3860 +}
3861 +
3862 +/* ---------------------------------------------------------------------- */
3863 +
3864 +/*
3865 + * change a branch permission
3866 + */
3867 +
3868 +static int do_need_sigen_inc(int a, int b)
3869 +{
3870 +       return au_br_whable(a) && !au_br_whable(b);
3871 +}
3872 +
3873 +static int need_sigen_inc(int old, int new)
3874 +{
3875 +       return do_need_sigen_inc(old, new)
3876 +               || do_need_sigen_inc(new, old);
3877 +}
3878 +
3879 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3880 +{
3881 +       int err;
3882 +       unsigned long n, ul, bytes, files;
3883 +       aufs_bindex_t bstart;
3884 +       struct file *file, *hf, **a;
3885 +       const int step_bytes = 1024, /* memory allocation unit */
3886 +               step_files = step_bytes / sizeof(*a);
3887 +
3888 +       err = -ENOMEM;
3889 +       n = 0;
3890 +       bytes = step_bytes;
3891 +       files = step_files;
3892 +       a = kmalloc(bytes, GFP_NOFS);
3893 +       if (unlikely(!a))
3894 +               goto out;
3895 +
3896 +       /* no need file_list_lock() since sbinfo is locked? defered? */
3897 +       list_for_each_entry(file, &sb->s_files, f_u.fu_list) {
3898 +               if (special_file(file->f_dentry->d_inode->i_mode))
3899 +                       continue;
3900 +
3901 +               AuDbg("%.*s\n", AuDLNPair(file->f_dentry));
3902 +               fi_read_lock(file);
3903 +               if (unlikely(au_test_mmapped(file))) {
3904 +                       err = -EBUSY;
3905 +                       FiMustNoWaiters(file);
3906 +                       fi_read_unlock(file);
3907 +                       goto out_free;
3908 +               }
3909 +
3910 +               bstart = au_fbstart(file);
3911 +               if (!S_ISREG(file->f_dentry->d_inode->i_mode)
3912 +                   || !(file->f_mode & FMODE_WRITE)
3913 +                   || bstart != bindex) {
3914 +                       FiMustNoWaiters(file);
3915 +                       fi_read_unlock(file);
3916 +                       continue;
3917 +               }
3918 +
3919 +               hf = au_h_fptr(file, bstart);
3920 +               FiMustNoWaiters(file);
3921 +               fi_read_unlock(file);
3922 +
3923 +               if (n < files)
3924 +                       a[n++] = hf;
3925 +               else {
3926 +                       void *p;
3927 +
3928 +                       err = -ENOMEM;
3929 +                       bytes += step_bytes;
3930 +                       files += step_files;
3931 +                       p = krealloc(a, bytes, GFP_NOFS);
3932 +                       if (p) {
3933 +                               a = p;
3934 +                               a[n++] = hf;
3935 +                       } else
3936 +                               goto out_free;
3937 +               }
3938 +       }
3939 +
3940 +       err = 0;
3941 +       for (ul = 0; ul < n; ul++) {
3942 +               /* todo: already flushed? */
3943 +               /* cf. fs/super.c:mark_files_ro() */
3944 +               hf = a[ul];
3945 +               hf->f_mode &= ~FMODE_WRITE;
3946 +               if (!file_check_writeable(hf)) {
3947 +                       file_release_write(hf);
3948 +                       mnt_drop_write(hf->f_vfsmnt);
3949 +               }
3950 +       }
3951 +
3952 + out_free:
3953 +       kfree(a);
3954 + out:
3955 +       return err;
3956 +}
3957 +
3958 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3959 +             int *do_update)
3960 +{
3961 +       int err, rerr;
3962 +       aufs_bindex_t bindex;
3963 +       struct dentry *root;
3964 +       struct au_branch *br;
3965 +
3966 +       root = sb->s_root;
3967 +       au_plink_block_maintain(sb);
3968 +       bindex = au_find_dbindex(root, mod->h_root);
3969 +       if (bindex < 0) {
3970 +               if (remount)
3971 +                       return 0; /* success */
3972 +               err = -ENOENT;
3973 +               AuErr("%s no such branch\n", mod->path);
3974 +               goto out;
3975 +       }
3976 +       AuDbg("bindex b%d\n", bindex);
3977 +
3978 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
3979 +       if (unlikely(err))
3980 +               goto out;
3981 +
3982 +       br = au_sbr(sb, bindex);
3983 +       if (br->br_perm == mod->perm)
3984 +               return 0; /* success */
3985 +
3986 +       if (au_br_writable(br->br_perm)) {
3987 +               /* remove whiteout base */
3988 +               err = au_br_init_wh(sb, br, mod->perm, mod->h_root);
3989 +               if (unlikely(err))
3990 +                       goto out;
3991 +
3992 +               if (!au_br_writable(mod->perm)) {
3993 +                       /* rw --> ro, file might be mmapped */
3994 +                       DiMustNoWaiters(root);
3995 +                       IiMustNoWaiters(root->d_inode);
3996 +                       di_write_unlock(root);
3997 +                       err = au_br_mod_files_ro(sb, bindex);
3998 +                       /* aufs_write_lock() calls ..._child() */
3999 +                       di_write_lock_child(root);
4000 +
4001 +                       if (unlikely(err)) {
4002 +                               rerr = -ENOMEM;
4003 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
4004 +                                                    GFP_NOFS);
4005 +                               if (br->br_wbr)
4006 +                                       rerr = au_br_init_wh
4007 +                                               (sb, br, br->br_perm,
4008 +                                                mod->h_root);
4009 +                               if (unlikely(rerr)) {
4010 +                                       AuIOErr("nested error %d (%d)\n",
4011 +                                               rerr, err);
4012 +                                       br->br_perm = mod->perm;
4013 +                               }
4014 +                       }
4015 +               }
4016 +       } else if (au_br_writable(mod->perm)) {
4017 +               /* ro --> rw */
4018 +               err = -ENOMEM;
4019 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
4020 +               if (br->br_wbr) {
4021 +                       struct path path = {
4022 +                               .mnt    = br->br_mnt,
4023 +                               .dentry = mod->h_root
4024 +                       };
4025 +
4026 +                       err = au_wbr_init(br, sb, mod->perm, &path);
4027 +                       if (unlikely(err)) {
4028 +                               kfree(br->br_wbr);
4029 +                               br->br_wbr = NULL;
4030 +                       }
4031 +               }
4032 +       }
4033 +
4034 +       if (!err) {
4035 +               *do_update |= need_sigen_inc(br->br_perm, mod->perm);
4036 +               br->br_perm = mod->perm;
4037 +       }
4038 +
4039 + out:
4040 +       return err;
4041 +}
4042 diff --git a/fs/aufs/branch.h b/fs/aufs/branch.h
4043 new file mode 100644
4044 index 0000000..e448692
4045 --- /dev/null
4046 +++ b/fs/aufs/branch.h
4047 @@ -0,0 +1,207 @@
4048 +/*
4049 + * Copyright (C) 2005-2009 Junjiro R. Okajima
4050 + *
4051 + * This program, aufs is free software; you can redistribute it and/or modify
4052 + * it under the terms of the GNU General Public License as published by
4053 + * the Free Software Foundation; either version 2 of the License, or
4054 + * (at your option) any later version.
4055 + */
4056 +
4057 +/*
4058 + * branch filesystems and xino for them
4059 + */
4060 +
4061 +#ifndef __AUFS_BRANCH_H__
4062 +#define __AUFS_BRANCH_H__
4063 +
4064 +#ifdef __KERNEL__
4065 +
4066 +#include <linux/fs.h>
4067 +#include <linux/mount.h>
4068 +#include <linux/sysfs.h>
4069 +#include <linux/aufs_type.h>
4070 +#include "rwsem.h"
4071 +#include "super.h"
4072 +
4073 +/* ---------------------------------------------------------------------- */
4074 +
4075 +/* a xino file */
4076 +struct au_xino_file {
4077 +       struct file             *xi_file;
4078 +       struct mutex            xi_nondir_mtx;
4079 +
4080 +       /* todo: make xino files an array to support huge inode number */
4081 +
4082 +#ifdef CONFIG_DEBUG_FS
4083 +       struct dentry            *xi_dbgaufs;
4084 +#endif
4085 +};
4086 +
4087 +/* members for writable branch only */
4088 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4089 +struct au_wbr {
4090 +       struct rw_semaphore     wbr_wh_rwsem;
4091 +       struct dentry           *wbr_wh[AuBrWh_Last];
4092 +       atomic_t                wbr_wh_running;
4093 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4094 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4095 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4096 +
4097 +       /* mfs mode */
4098 +       unsigned long long      wbr_bytes;
4099 +};
4100 +
4101 +/* protected by superblock rwsem */
4102 +struct au_branch {
4103 +       struct au_xino_file     br_xino;
4104 +
4105 +       aufs_bindex_t           br_id;
4106 +
4107 +       int                     br_perm;
4108 +       struct vfsmount         *br_mnt;
4109 +       atomic_t                br_count;
4110 +
4111 +       struct au_wbr           *br_wbr;
4112 +
4113 +       /* xino truncation */
4114 +       blkcnt_t                br_xino_upper;  /* watermark in blocks */
4115 +       atomic_t                br_xino_running;
4116 +
4117 +#ifdef CONFIG_SYSFS
4118 +       /* an entry under sysfs per mount-point */
4119 +       char                    br_name[8];
4120 +       struct attribute        br_attr;
4121 +#endif
4122 +};
4123 +
4124 +/* ---------------------------------------------------------------------- */
4125 +
4126 +/* branch permission and attribute */
4127 +enum {
4128 +       AuBrPerm_RW,            /* writable, linkable wh */
4129 +       AuBrPerm_RO,            /* readonly, no wh */
4130 +       AuBrPerm_RR,            /* natively readonly, no wh */
4131 +
4132 +       AuBrPerm_RWNoLinkWH,    /* un-linkable whiteouts */
4133 +
4134 +       AuBrPerm_ROWH,          /* whiteout-able */
4135 +       AuBrPerm_RRWH,          /* whiteout-able */
4136 +
4137 +       AuBrPerm_Last
4138 +};
4139 +
4140 +static inline int au_br_writable(int brperm)
4141 +{
4142 +       return brperm == AuBrPerm_RW || brperm == AuBrPerm_RWNoLinkWH;
4143 +}
4144 +
4145 +static inline int au_br_whable(int brperm)
4146 +{
4147 +       return brperm == AuBrPerm_RW
4148 +               || brperm == AuBrPerm_ROWH
4149 +               || brperm == AuBrPerm_RRWH;
4150 +}
4151 +
4152 +static inline int au_br_rdonly(struct au_branch *br)
4153 +{
4154 +       return ((br->br_mnt->mnt_sb->s_flags & MS_RDONLY)
4155 +               || !au_br_writable(br->br_perm))
4156 +               ? -EROFS : 0;
4157 +}
4158 +
4159 +static inline int au_br_hinotifyable(int brperm __maybe_unused)
4160 +{
4161 +#ifdef CONFIG_AUFS_HINOTIFY
4162 +       return brperm != AuBrPerm_RR && brperm != AuBrPerm_RRWH;
4163 +#else
4164 +       return 0;
4165 +#endif
4166 +}
4167 +
4168 +/* ---------------------------------------------------------------------- */
4169 +
4170 +/* branch.c */
4171 +struct au_sbinfo;
4172 +void au_br_free(struct au_sbinfo *sinfo);
4173 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4174 +struct au_opt_add;
4175 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4176 +struct au_opt_del;
4177 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4178 +struct au_opt_mod;
4179 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4180 +             int *do_update);
4181 +
4182 +/* xino.c */
4183 +static const loff_t au_loff_max = LLONG_MAX;
4184 +
4185 +int au_xib_trunc(struct super_block *sb);
4186 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
4187 +                  loff_t *pos);
4188 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
4189 +                   loff_t *pos);
4190 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4191 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4192 +ino_t au_xino_new_ino(struct super_block *sb);
4193 +int au_xino_write0(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4194 +                  ino_t ino);
4195 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4196 +                 ino_t ino);
4197 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4198 +                ino_t *ino);
4199 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4200 +              struct file *base_file, int do_test);
4201 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4202 +
4203 +struct au_opt_xino;
4204 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4205 +void au_xino_clr(struct super_block *sb);
4206 +struct file *au_xino_def(struct super_block *sb);
4207 +int au_xino_path(struct seq_file *seq, struct file *file);
4208 +
4209 +/* ---------------------------------------------------------------------- */
4210 +
4211 +/* Superblock to branch */
4212 +static inline
4213 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4214 +{
4215 +       return au_sbr(sb, bindex)->br_id;
4216 +}
4217 +
4218 +static inline
4219 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4220 +{
4221 +       return au_sbr(sb, bindex)->br_mnt;
4222 +}
4223 +
4224 +static inline
4225 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4226 +{
4227 +       return au_sbr_mnt(sb, bindex)->mnt_sb;
4228 +}
4229 +
4230 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4231 +{
4232 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
4233 +}
4234 +
4235 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4236 +{
4237 +       return au_sbr(sb, bindex)->br_perm;
4238 +}
4239 +
4240 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4241 +{
4242 +       return au_br_whable(au_sbr_perm(sb, bindex));
4243 +}
4244 +
4245 +/* ---------------------------------------------------------------------- */
4246 +
4247 +/*
4248 + * wbr_wh_read_lock, wbr_wh_write_lock
4249 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4250 + */
4251 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4252 +
4253 +#endif /* __KERNEL__ */
4254 +#endif /* __AUFS_BRANCH_H__ */
4255 diff --git a/fs/aufs/cpup.c b/fs/aufs/cpup.c
4256 new file mode 100644
4257 index 0000000..91808b1
4258 --- /dev/null
4259 +++ b/fs/aufs/cpup.c
4260 @@ -0,0 +1,1028 @@
4261 +/*
4262 + * Copyright (C) 2005-2009 Junjiro R. Okajima
4263 + *
4264 + * This program, aufs is free software; you can redistribute it and/or modify
4265 + * it under the terms of the GNU General Public License as published by
4266 + * the Free Software Foundation; either version 2 of the License, or
4267 + * (at your option) any later version.
4268 + */
4269 +
4270 +/*
4271 + * copy-up functions, see wbr_policy.c for copy-down
4272 + */
4273 +
4274 +#include <linux/fs_stack.h>
4275 +#include <linux/uaccess.h>
4276 +#include "aufs.h"
4277 +
4278 +void au_cpup_attr_flags(struct inode *dst, struct inode *src)
4279 +{
4280 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4281 +               | S_NOATIME | S_NOCMTIME;
4282 +
4283 +       dst->i_flags |= src->i_flags & ~mask;
4284 +       if (au_test_fs_notime(dst->i_sb))
4285 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4286 +}
4287 +
4288 +void au_cpup_attr_timesizes(struct inode *inode)
4289 +{
4290 +       struct inode *h_inode;
4291 +
4292 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4293 +       fsstack_copy_attr_times(inode, h_inode);
4294 +       vfsub_copy_inode_size(inode, h_inode);
4295 +}
4296 +
4297 +void au_cpup_attr_nlink(struct inode *inode, int force)
4298 +{
4299 +       struct inode *h_inode;
4300 +       struct super_block *sb;
4301 +       aufs_bindex_t bindex, bend;
4302 +
4303 +       sb = inode->i_sb;
4304 +       bindex = au_ibstart(inode);
4305 +       h_inode = au_h_iptr(inode, bindex);
4306 +       if (!force
4307 +           && !S_ISDIR(h_inode->i_mode)
4308 +           && au_opt_test(au_mntflags(sb), PLINK)
4309 +           && au_plink_test(inode))
4310 +               return;
4311 +
4312 +       inode->i_nlink = h_inode->i_nlink;
4313 +
4314 +       /*
4315 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4316 +        * it may includes whplink directory.
4317 +        */
4318 +       if (S_ISDIR(h_inode->i_mode)) {
4319 +               bend = au_ibend(inode);
4320 +               for (bindex++; bindex <= bend; bindex++) {
4321 +                       h_inode = au_h_iptr(inode, bindex);
4322 +                       if (h_inode)
4323 +                               au_add_nlink(inode, h_inode);
4324 +               }
4325 +       }
4326 +}
4327 +
4328 +void au_cpup_attr_changeable(struct inode *inode)
4329 +{
4330 +       struct inode *h_inode;
4331 +
4332 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4333 +       inode->i_mode = h_inode->i_mode;
4334 +       inode->i_uid = h_inode->i_uid;
4335 +       inode->i_gid = h_inode->i_gid;
4336 +       au_cpup_attr_timesizes(inode);
4337 +       au_cpup_attr_flags(inode, h_inode);
4338 +}
4339 +
4340 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4341 +{
4342 +       struct au_iinfo *iinfo = au_ii(inode);
4343 +
4344 +       iinfo->ii_higen = h_inode->i_generation;
4345 +       iinfo->ii_hsb1 = h_inode->i_sb;
4346 +}
4347 +
4348 +void au_cpup_attr_all(struct inode *inode, int force)
4349 +{
4350 +       struct inode *h_inode;
4351 +
4352 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4353 +       au_cpup_attr_changeable(inode);
4354 +       if (inode->i_nlink > 0)
4355 +               au_cpup_attr_nlink(inode, force);
4356 +       inode->i_rdev = h_inode->i_rdev;
4357 +       inode->i_blkbits = h_inode->i_blkbits;
4358 +       au_cpup_igen(inode, h_inode);
4359 +}
4360 +
4361 +/* ---------------------------------------------------------------------- */
4362 +
4363 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4364 +
4365 +/* keep the timestamps of the parent dir when cpup */
4366 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4367 +                   struct path *h_path)
4368 +{
4369 +       struct inode *h_inode;
4370 +
4371 +       dt->dt_dentry = dentry;
4372 +       dt->dt_h_path = *h_path;
4373 +       h_inode = h_path->dentry->d_inode;
4374 +       dt->dt_atime = h_inode->i_atime;
4375 +       dt->dt_mtime = h_inode->i_mtime;
4376 +       /* smp_mb(); */
4377 +}
4378 +
4379 +void au_dtime_revert(struct au_dtime *dt)
4380 +{
4381 +       struct iattr attr;
4382 +       int err;
4383 +
4384 +       attr.ia_atime = dt->dt_atime;
4385 +       attr.ia_mtime = dt->dt_mtime;
4386 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4387 +               | ATTR_ATIME | ATTR_ATIME_SET;
4388 +
4389 +       err = vfsub_notify_change(&dt->dt_h_path, &attr);
4390 +       if (unlikely(err))
4391 +               AuWarn("restoring timestamps failed(%d). ignored\n", err);
4392 +}
4393 +
4394 +/* ---------------------------------------------------------------------- */
4395 +
4396 +static noinline_for_stack
4397 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src)
4398 +{
4399 +       int err, sbits;
4400 +       struct iattr ia;
4401 +       struct path h_path;
4402 +       struct inode *h_isrc;
4403 +
4404 +       h_path.dentry = au_h_dptr(dst, bindex);
4405 +       h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
4406 +       h_isrc = h_src->d_inode;
4407 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID
4408 +               | ATTR_ATIME | ATTR_MTIME
4409 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4410 +       ia.ia_mode = h_isrc->i_mode;
4411 +       ia.ia_uid = h_isrc->i_uid;
4412 +       ia.ia_gid = h_isrc->i_gid;
4413 +       ia.ia_atime = h_isrc->i_atime;
4414 +       ia.ia_mtime = h_isrc->i_mtime;
4415 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
4416 +       au_cpup_attr_flags(h_path.dentry->d_inode, h_isrc);
4417 +       err = vfsub_notify_change(&h_path, &ia);
4418 +
4419 +       /* is this nfs only? */
4420 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4421 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4422 +               ia.ia_mode = h_isrc->i_mode;
4423 +               err = vfsub_notify_change(&h_path, &ia);
4424 +       }
4425 +
4426 +       return err;
4427 +}
4428 +
4429 +/* ---------------------------------------------------------------------- */
4430 +
4431 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4432 +                          char *buf, unsigned long blksize)
4433 +{
4434 +       int err;
4435 +       size_t sz, rbytes, wbytes;
4436 +       unsigned char all_zero;
4437 +       char *p, *zp;
4438 +       struct mutex *h_mtx;
4439 +       /* reduce stack usage */
4440 +       struct iattr *ia;
4441 +
4442 +       zp = page_address(ZERO_PAGE(0));
4443 +       if (unlikely(!zp))
4444 +               return -ENOMEM; /* possible? */
4445 +
4446 +       err = 0;
4447 +       all_zero = 0;
4448 +       while (len) {
4449 +               AuDbg("len %lld\n", len);
4450 +               sz = blksize;
4451 +               if (len < blksize)
4452 +                       sz = len;
4453 +
4454 +               rbytes = 0;
4455 +               /* todo: signal_pending? */
4456 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4457 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4458 +                       err = rbytes;
4459 +               }
4460 +               if (unlikely(err < 0))
4461 +                       break;
4462 +
4463 +               all_zero = 0;
4464 +               if (len >= rbytes && rbytes == blksize)
4465 +                       all_zero = !memcmp(buf, zp, rbytes);
4466 +               if (!all_zero) {
4467 +                       wbytes = rbytes;
4468 +                       p = buf;
4469 +                       while (wbytes) {
4470 +                               size_t b;
4471 +
4472 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4473 +                               err = b;
4474 +                               /* todo: signal_pending? */
4475 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4476 +                                       continue;
4477 +                               if (unlikely(err < 0))
4478 +                                       break;
4479 +                               wbytes -= b;
4480 +                               p += b;
4481 +                       }
4482 +               } else {
4483 +                       loff_t res;
4484 +
4485 +                       AuLabel(hole);
4486 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4487 +                       err = res;
4488 +                       if (unlikely(res < 0))
4489 +                               break;
4490 +               }
4491 +               len -= rbytes;
4492 +               err = 0;
4493 +       }
4494 +
4495 +       /* the last block may be a hole */
4496 +       if (!err && all_zero) {
4497 +               AuLabel(last hole);
4498 +
4499 +               err = 1;
4500 +               if (au_test_nfs(dst->f_dentry->d_sb)) {
4501 +                       /* nfs requires this step to make last hole */
4502 +                       /* is this only nfs? */
4503 +                       do {
4504 +                               /* todo: signal_pending? */
4505 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4506 +                       } while (err == -EAGAIN || err == -EINTR);
4507 +                       if (err == 1)
4508 +                               dst->f_pos--;
4509 +               }
4510 +
4511 +               if (err == 1) {
4512 +                       ia = (void *)buf;
4513 +                       ia->ia_size = dst->f_pos;
4514 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4515 +                       ia->ia_file = dst;
4516 +                       h_mtx = &dst->f_dentry->d_inode->i_mutex;
4517 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
4518 +                       err = vfsub_notify_change(&dst->f_path, ia);
4519 +                       mutex_unlock(h_mtx);
4520 +               }
4521 +       }
4522 +
4523 +       return err;
4524 +}
4525 +
4526 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4527 +{
4528 +       int err;
4529 +       unsigned long blksize;
4530 +       unsigned char do_kfree;
4531 +       char *buf;
4532 +
4533 +       err = -ENOMEM;
4534 +       blksize = dst->f_dentry->d_sb->s_blocksize;
4535 +       if (!blksize || PAGE_SIZE < blksize)
4536 +               blksize = PAGE_SIZE;
4537 +       AuDbg("blksize %lu\n", blksize);
4538 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4539 +       if (do_kfree)
4540 +               buf = kmalloc(blksize, GFP_NOFS);
4541 +       else
4542 +               buf = (void *)__get_free_page(GFP_NOFS);
4543 +       if (unlikely(!buf))
4544 +               goto out;
4545 +
4546 +       if (len > (1 << 22))
4547 +               AuDbg("copying a large file %lld\n", (long long)len);
4548 +
4549 +       src->f_pos = 0;
4550 +       dst->f_pos = 0;
4551 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4552 +       if (do_kfree)
4553 +               kfree(buf);
4554 +       else
4555 +               free_page((unsigned long)buf);
4556 +
4557 + out:
4558 +       return err;
4559 +}
4560 +
4561 +/*
4562 + * to support a sparse file which is opened with O_APPEND,
4563 + * we need to close the file.
4564 + */
4565 +static int au_cp_regular(struct dentry *dentry, aufs_bindex_t bdst,
4566 +                       aufs_bindex_t bsrc, loff_t len)
4567 +{
4568 +       int err, i;
4569 +       enum { SRC, DST };
4570 +       struct {
4571 +               aufs_bindex_t bindex;
4572 +               unsigned int flags;
4573 +               struct dentry *dentry;
4574 +               struct file *file;
4575 +               void *label, *label_file;
4576 +       } *f, file[] = {
4577 +               {
4578 +                       .bindex = bsrc,
4579 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4580 +                       .file = NULL,
4581 +                       .label = &&out,
4582 +                       .label_file = &&out_src
4583 +               },
4584 +               {
4585 +                       .bindex = bdst,
4586 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4587 +                       .file = NULL,
4588 +                       .label = &&out_src,
4589 +                       .label_file = &&out_dst
4590 +               }
4591 +       };
4592 +       struct super_block *sb;
4593 +
4594 +       /* bsrc branch can be ro/rw. */
4595 +       sb = dentry->d_sb;
4596 +       f = file;
4597 +       for (i = 0; i < 2; i++, f++) {
4598 +               f->dentry = au_h_dptr(dentry, f->bindex);
4599 +               f->file = au_h_open(dentry, f->bindex, f->flags, /*file*/NULL);
4600 +               err = PTR_ERR(f->file);
4601 +               if (IS_ERR(f->file))
4602 +                       goto *f->label;
4603 +               err = -EINVAL;
4604 +               if (unlikely(!f->file->f_op))
4605 +                       goto *f->label_file;
4606 +       }
4607 +
4608 +       /* try stopping to update while we copyup */
4609 +       IMustLock(file[SRC].dentry->d_inode);
4610 +       err = au_copy_file(file[DST].file, file[SRC].file, len);
4611 +
4612 + out_dst:
4613 +       fput(file[DST].file);
4614 +       au_sbr_put(sb, file[DST].bindex);
4615 + out_src:
4616 +       fput(file[SRC].file);
4617 +       au_sbr_put(sb, file[SRC].bindex);
4618 + out:
4619 +       return err;
4620 +}
4621 +
4622 +static int au_do_cpup_regular(struct dentry *dentry, aufs_bindex_t bdst,
4623 +                             aufs_bindex_t bsrc, loff_t len,
4624 +                             struct inode *h_dir, struct path *h_path)
4625 +{
4626 +       int err, rerr;
4627 +       loff_t l;
4628 +
4629 +       err = 0;
4630 +       l = i_size_read(au_h_iptr(dentry->d_inode, bsrc));
4631 +       if (len == -1 || l < len)
4632 +               len = l;
4633 +       if (len)
4634 +               err = au_cp_regular(dentry, bdst, bsrc, len);
4635 +       if (!err)
4636 +               goto out; /* success */
4637 +
4638 +       rerr = vfsub_unlink(h_dir, h_path, /*force*/0);
4639 +       if (rerr) {
4640 +               AuIOErr("failed unlinking cpup-ed %.*s(%d, %d)\n",
4641 +                       AuDLNPair(h_path->dentry), err, rerr);
4642 +               err = -EIO;
4643 +       }
4644 +
4645 + out:
4646 +       return err;
4647 +}
4648 +
4649 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4650 +                             struct inode *h_dir)
4651 +{
4652 +       int err, symlen;
4653 +       mm_segment_t old_fs;
4654 +       char *sym;
4655 +
4656 +       err = -ENOSYS;
4657 +       if (unlikely(!h_src->d_inode->i_op->readlink))
4658 +               goto out;
4659 +
4660 +       err = -ENOMEM;
4661 +       sym = __getname();
4662 +       if (unlikely(!sym))
4663 +               goto out;
4664 +
4665 +       old_fs = get_fs();
4666 +       set_fs(KERNEL_DS);
4667 +       symlen = h_src->d_inode->i_op->readlink(h_src, (char __user *)sym,
4668 +                                               PATH_MAX);
4669 +       err = symlen;
4670 +       set_fs(old_fs);
4671 +
4672 +       if (symlen > 0) {
4673 +               sym[symlen] = 0;
4674 +               err = vfsub_symlink(h_dir, h_path, sym);
4675 +       }
4676 +       __putname(sym);
4677 +
4678 + out:
4679 +       return err;
4680 +}
4681 +
4682 +/* return with the lower dst inode is locked */
4683 +static noinline_for_stack
4684 +int cpup_entry(struct dentry *dentry, aufs_bindex_t bdst,
4685 +              aufs_bindex_t bsrc, loff_t len, unsigned int flags,
4686 +              struct dentry *dst_parent)
4687 +{
4688 +       int err;
4689 +       umode_t mode;
4690 +       unsigned int mnt_flags;
4691 +       unsigned char isdir;
4692 +       const unsigned char do_dt = !!au_ftest_cpup(flags, DTIME);
4693 +       struct au_dtime dt;
4694 +       struct path h_path;
4695 +       struct dentry *h_src, *h_dst, *h_parent;
4696 +       struct inode *h_inode, *h_dir;
4697 +       struct super_block *sb;
4698 +
4699 +       /* bsrc branch can be ro/rw. */
4700 +       h_src = au_h_dptr(dentry, bsrc);
4701 +       h_inode = h_src->d_inode;
4702 +       AuDebugOn(h_inode != au_h_iptr(dentry->d_inode, bsrc));
4703 +
4704 +       /* try stopping to be referenced while we are creating */
4705 +       h_dst = au_h_dptr(dentry, bdst);
4706 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4707 +       h_dir = h_parent->d_inode;
4708 +       IMustLock(h_dir);
4709 +       AuDebugOn(h_parent != h_dst->d_parent);
4710 +
4711 +       sb = dentry->d_sb;
4712 +       h_path.mnt = au_sbr_mnt(sb, bdst);
4713 +       if (do_dt) {
4714 +               h_path.dentry = h_parent;
4715 +               au_dtime_store(&dt, dst_parent, &h_path);
4716 +       }
4717 +       h_path.dentry = h_dst;
4718 +
4719 +       isdir = 0;
4720 +       mode = h_inode->i_mode;
4721 +       switch (mode & S_IFMT) {
4722 +       case S_IFREG:
4723 +               /* try stopping to update while we are referencing */
4724 +               IMustLock(h_inode);
4725 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
4726 +               if (!err)
4727 +                       err = au_do_cpup_regular
4728 +                               (dentry, bdst, bsrc, len,
4729 +                                au_h_iptr(dst_parent->d_inode, bdst), &h_path);
4730 +               break;
4731 +       case S_IFDIR:
4732 +               isdir = 1;
4733 +               err = vfsub_mkdir(h_dir, &h_path, mode);
4734 +               if (!err) {
4735 +                       /*
4736 +                        * strange behaviour from the users view,
4737 +                        * particularry setattr case
4738 +                        */
4739 +                       if (au_ibstart(dst_parent->d_inode) == bdst)
4740 +                               au_cpup_attr_nlink(dst_parent->d_inode,
4741 +                                                  /*force*/1);
4742 +                       au_cpup_attr_nlink(dentry->d_inode, /*force*/1);
4743 +               }
4744 +               break;
4745 +       case S_IFLNK:
4746 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
4747 +               break;
4748 +       case S_IFCHR:
4749 +       case S_IFBLK:
4750 +               AuDebugOn(!capable(CAP_MKNOD));
4751 +               /*FALLTHROUGH*/
4752 +       case S_IFIFO:
4753 +       case S_IFSOCK:
4754 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
4755 +               break;
4756 +       default:
4757 +               AuIOErr("Unknown inode type 0%o\n", mode);
4758 +               err = -EIO;
4759 +       }
4760 +
4761 +       mnt_flags = au_mntflags(sb);
4762 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
4763 +           && !isdir
4764 +           && au_opt_test(mnt_flags, XINO)
4765 +           && h_inode->i_nlink == 1
4766 +           /* todo: unnecessary? */
4767 +           /* && dentry->d_inode->i_nlink == 1 */
4768 +           && bdst < bsrc
4769 +           && !au_ftest_cpup(flags, KEEPLINO))
4770 +               au_xino_write0(sb, bsrc, h_inode->i_ino, /*ino*/0);
4771 +               /* ignore this error */
4772 +
4773 +       if (do_dt)
4774 +               au_dtime_revert(&dt);
4775 +       return err;
4776 +}
4777 +
4778 +/*
4779 + * copyup the @dentry from @bsrc to @bdst.
4780 + * the caller must set the both of lower dentries.
4781 + * @len is for truncating when it is -1 copyup the entire file.
4782 + * in link/rename cases, @dst_parent may be different from the real one.
4783 + */
4784 +static int au_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
4785 +                         aufs_bindex_t bsrc, loff_t len, unsigned int flags,
4786 +                         struct dentry *dst_parent)
4787 +{
4788 +       int err, rerr;
4789 +       aufs_bindex_t old_ibstart;
4790 +       unsigned char isdir, plink;
4791 +       struct au_dtime dt;
4792 +       struct path h_path;
4793 +       struct dentry *h_src, *h_dst, *h_parent;
4794 +       struct inode *dst_inode, *h_dir, *inode;
4795 +       struct super_block *sb;
4796 +
4797 +       AuDebugOn(bsrc <= bdst);
4798 +
4799 +       sb = dentry->d_sb;
4800 +       h_path.mnt = au_sbr_mnt(sb, bdst);
4801 +       h_dst = au_h_dptr(dentry, bdst);
4802 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4803 +       h_dir = h_parent->d_inode;
4804 +       IMustLock(h_dir);
4805 +
4806 +       h_src = au_h_dptr(dentry, bsrc);
4807 +       inode = dentry->d_inode;
4808 +
4809 +       if (!dst_parent)
4810 +               dst_parent = dget_parent(dentry);
4811 +       else
4812 +               dget(dst_parent);
4813 +
4814 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
4815 +       dst_inode = au_h_iptr(inode, bdst);
4816 +       if (dst_inode) {
4817 +               if (unlikely(!plink)) {
4818 +                       err = -EIO;
4819 +                       AuIOErr("i%lu exists on a upper branch "
4820 +                               "but plink is disabled\n", inode->i_ino);
4821 +                       goto out;
4822 +               }
4823 +
4824 +               if (dst_inode->i_nlink) {
4825 +                       const int do_dt = au_ftest_cpup(flags, DTIME);
4826 +
4827 +                       h_src = au_plink_lkup(inode, bdst);
4828 +                       err = PTR_ERR(h_src);
4829 +                       if (IS_ERR(h_src))
4830 +                               goto out;
4831 +                       if (unlikely(!h_src->d_inode)) {
4832 +                               err = -EIO;
4833 +                               AuIOErr("i%lu exists on a upper branch "
4834 +                                       "but plink is broken\n", inode->i_ino);
4835 +                               dput(h_src);
4836 +                               goto out;
4837 +                       }
4838 +
4839 +                       if (do_dt) {
4840 +                               h_path.dentry = h_parent;
4841 +                               au_dtime_store(&dt, dst_parent, &h_path);
4842 +                       }
4843 +                       h_path.dentry = h_dst;
4844 +                       err = vfsub_link(h_src, h_dir, &h_path);
4845 +                       if (do_dt)
4846 +                               au_dtime_revert(&dt);
4847 +                       dput(h_src);
4848 +                       goto out;
4849 +               } else
4850 +                       /* todo: cpup_wh_file? */
4851 +                       /* udba work */
4852 +                       au_update_brange(inode, 1);
4853 +       }
4854 +
4855 +       old_ibstart = au_ibstart(inode);
4856 +       err = cpup_entry(dentry, bdst, bsrc, len, flags, dst_parent);
4857 +       if (unlikely(err))
4858 +               goto out;
4859 +       dst_inode = h_dst->d_inode;
4860 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
4861 +
4862 +       err = cpup_iattr(dentry, bdst, h_src);
4863 +       isdir = S_ISDIR(dst_inode->i_mode);
4864 +       if (!err) {
4865 +               if (bdst < old_ibstart)
4866 +                       au_set_ibstart(inode, bdst);
4867 +               au_set_h_iptr(inode, bdst, au_igrab(dst_inode),
4868 +                             au_hi_flags(inode, isdir));
4869 +               mutex_unlock(&dst_inode->i_mutex);
4870 +               if (!isdir
4871 +                   && h_src->d_inode->i_nlink > 1
4872 +                   && plink)
4873 +                       au_plink_append(inode, bdst, h_dst);
4874 +               goto out; /* success */
4875 +       }
4876 +
4877 +       /* revert */
4878 +       h_path.dentry = h_parent;
4879 +       mutex_unlock(&dst_inode->i_mutex);
4880 +       au_dtime_store(&dt, dst_parent, &h_path);
4881 +       h_path.dentry = h_dst;
4882 +       if (!isdir)
4883 +               rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
4884 +       else
4885 +               rerr = vfsub_rmdir(h_dir, &h_path);
4886 +       au_dtime_revert(&dt);
4887 +       if (rerr) {
4888 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
4889 +               err = -EIO;
4890 +       }
4891 +
4892 + out:
4893 +       dput(dst_parent);
4894 +       return err;
4895 +}
4896 +
4897 +struct au_cpup_single_args {
4898 +       int *errp;
4899 +       struct dentry *dentry;
4900 +       aufs_bindex_t bdst, bsrc;
4901 +       loff_t len;
4902 +       unsigned int flags;
4903 +       struct dentry *dst_parent;
4904 +};
4905 +
4906 +static void au_call_cpup_single(void *args)
4907 +{
4908 +       struct au_cpup_single_args *a = args;
4909 +       *a->errp = au_cpup_single(a->dentry, a->bdst, a->bsrc, a->len,
4910 +                                 a->flags, a->dst_parent);
4911 +}
4912 +
4913 +int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
4914 +                      aufs_bindex_t bsrc, loff_t len, unsigned int flags,
4915 +                      struct dentry *dst_parent)
4916 +{
4917 +       int err, wkq_err;
4918 +       umode_t mode;
4919 +       struct dentry *h_dentry;
4920 +
4921 +       h_dentry = au_h_dptr(dentry, bsrc);
4922 +       mode = h_dentry->d_inode->i_mode & S_IFMT;
4923 +       if ((mode != S_IFCHR && mode != S_IFBLK)
4924 +           || capable(CAP_MKNOD))
4925 +               err = au_cpup_single(dentry, bdst, bsrc, len, flags,
4926 +                                    dst_parent);
4927 +       else {
4928 +               struct au_cpup_single_args args = {
4929 +                       .errp           = &err,
4930 +                       .dentry         = dentry,
4931 +                       .bdst           = bdst,
4932 +                       .bsrc           = bsrc,
4933 +                       .len            = len,
4934 +                       .flags          = flags,
4935 +                       .dst_parent     = dst_parent
4936 +               };
4937 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
4938 +               if (unlikely(wkq_err))
4939 +                       err = wkq_err;
4940 +       }
4941 +
4942 +       return err;
4943 +}
4944 +
4945 +/*
4946 + * copyup the @dentry from the first active lower branch to @bdst,
4947 + * using au_cpup_single().
4948 + */
4949 +static int au_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4950 +                         unsigned int flags)
4951 +{
4952 +       int err;
4953 +       aufs_bindex_t bsrc, bend;
4954 +
4955 +       bend = au_dbend(dentry);
4956 +       for (bsrc = bdst + 1; bsrc <= bend; bsrc++)
4957 +               if (au_h_dptr(dentry, bsrc))
4958 +                       break;
4959 +
4960 +       err = au_lkup_neg(dentry, bdst);
4961 +       if (!err) {
4962 +               err = au_cpup_single(dentry, bdst, bsrc, len, flags, NULL);
4963 +               if (!err)
4964 +                       return 0; /* success */
4965 +
4966 +               /* revert */
4967 +               au_set_h_dptr(dentry, bdst, NULL);
4968 +               au_set_dbstart(dentry, bsrc);
4969 +       }
4970 +
4971 +       return err;
4972 +}
4973 +
4974 +struct au_cpup_simple_args {
4975 +       int *errp;
4976 +       struct dentry *dentry;
4977 +       aufs_bindex_t bdst;
4978 +       loff_t len;
4979 +       unsigned int flags;
4980 +};
4981 +
4982 +static void au_call_cpup_simple(void *args)
4983 +{
4984 +       struct au_cpup_simple_args *a = args;
4985 +       *a->errp = au_cpup_simple(a->dentry, a->bdst, a->len, a->flags);
4986 +}
4987 +
4988 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4989 +                      unsigned int flags)
4990 +{
4991 +       int err, wkq_err;
4992 +       unsigned char do_sio;
4993 +       struct dentry *parent;
4994 +       struct inode *h_dir;
4995 +
4996 +       parent = dget_parent(dentry);
4997 +       h_dir = au_h_iptr(parent->d_inode, bdst);
4998 +       do_sio = !!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE);
4999 +       if (!do_sio) {
5000 +               /*
5001 +                * testing CAP_MKNOD is for generic fs,
5002 +                * but CAP_FSETID is for xfs only, currently.
5003 +                */
5004 +               umode_t mode = dentry->d_inode->i_mode;
5005 +               do_sio = (((mode & (S_IFCHR | S_IFBLK))
5006 +                          && !capable(CAP_MKNOD))
5007 +                         || ((mode & (S_ISUID | S_ISGID))
5008 +                             && !capable(CAP_FSETID)));
5009 +       }
5010 +       if (!do_sio)
5011 +               err = au_cpup_simple(dentry, bdst, len, flags);
5012 +       else {
5013 +               struct au_cpup_simple_args args = {
5014 +                       .errp           = &err,
5015 +                       .dentry         = dentry,
5016 +                       .bdst           = bdst,
5017 +                       .len            = len,
5018 +                       .flags          = flags
5019 +               };
5020 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5021 +               if (unlikely(wkq_err))
5022 +                       err = wkq_err;
5023 +       }
5024 +
5025 +       dput(parent);
5026 +       return err;
5027 +}
5028 +
5029 +/* ---------------------------------------------------------------------- */
5030 +
5031 +/*
5032 + * copyup the deleted file for writing.
5033 + */
5034 +static int au_do_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst,
5035 +                        struct dentry *wh_dentry, struct file *file,
5036 +                        loff_t len)
5037 +{
5038 +       int err;
5039 +       aufs_bindex_t bstart;
5040 +       struct au_dinfo *dinfo;
5041 +       struct dentry *h_d_dst, *h_d_start;
5042 +
5043 +       dinfo = au_di(dentry);
5044 +       bstart = dinfo->di_bstart;
5045 +       h_d_dst = dinfo->di_hdentry[0 + bdst].hd_dentry;
5046 +       dinfo->di_bstart = bdst;
5047 +       dinfo->di_hdentry[0 + bdst].hd_dentry = wh_dentry;
5048 +       h_d_start = dinfo->di_hdentry[0 + bstart].hd_dentry;
5049 +       if (file)
5050 +               dinfo->di_hdentry[0 + bstart].hd_dentry
5051 +                       = au_h_fptr(file, au_fbstart(file))->f_dentry;
5052 +       err = au_cpup_single(dentry, bdst, bstart, len, !AuCpup_DTIME,
5053 +                            /*h_parent*/NULL);
5054 +       if (!err && file) {
5055 +               err = au_reopen_nondir(file);
5056 +               dinfo->di_hdentry[0 + bstart].hd_dentry = h_d_start;
5057 +       }
5058 +       dinfo->di_hdentry[0 + bdst].hd_dentry = h_d_dst;
5059 +       dinfo->di_bstart = bstart;
5060 +
5061 +       return err;
5062 +}
5063 +
5064 +static int au_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
5065 +                     struct file *file)
5066 +{
5067 +       int err;
5068 +       struct au_dtime dt;
5069 +       struct dentry *parent, *h_parent, *wh_dentry;
5070 +       struct au_branch *br;
5071 +       struct path h_path;
5072 +
5073 +       br = au_sbr(dentry->d_sb, bdst);
5074 +       parent = dget_parent(dentry);
5075 +       h_parent = au_h_dptr(parent, bdst);
5076 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5077 +       err = PTR_ERR(wh_dentry);
5078 +       if (IS_ERR(wh_dentry))
5079 +               goto out;
5080 +
5081 +       h_path.dentry = h_parent;
5082 +       h_path.mnt = br->br_mnt;
5083 +       au_dtime_store(&dt, parent, &h_path);
5084 +       err = au_do_cpup_wh(dentry, bdst, wh_dentry, file, len);
5085 +       if (unlikely(err))
5086 +               goto out_wh;
5087 +
5088 +       dget(wh_dentry);
5089 +       h_path.dentry = wh_dentry;
5090 +       err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
5091 +       if (unlikely(err)) {
5092 +               AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
5093 +                       AuDLNPair(wh_dentry), err);
5094 +               err = -EIO;
5095 +       }
5096 +       au_dtime_revert(&dt);
5097 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
5098 +
5099 + out_wh:
5100 +       dput(wh_dentry);
5101 + out:
5102 +       dput(parent);
5103 +       return err;
5104 +}
5105 +
5106 +struct au_cpup_wh_args {
5107 +       int *errp;
5108 +       struct dentry *dentry;
5109 +       aufs_bindex_t bdst;
5110 +       loff_t len;
5111 +       struct file *file;
5112 +};
5113 +
5114 +static void au_call_cpup_wh(void *args)
5115 +{
5116 +       struct au_cpup_wh_args *a = args;
5117 +       *a->errp = au_cpup_wh(a->dentry, a->bdst, a->len, a->file);
5118 +}
5119 +
5120 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
5121 +                  struct file *file)
5122 +{
5123 +       int err, wkq_err;
5124 +       struct dentry *parent, *h_orph, *h_parent, *h_dentry;
5125 +       struct inode *dir, *h_dir, *h_tmpdir, *h_inode;
5126 +       struct au_wbr *wbr;
5127 +
5128 +       parent = dget_parent(dentry);
5129 +       dir = parent->d_inode;
5130 +       h_orph = NULL;
5131 +       h_parent = NULL;
5132 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5133 +       h_tmpdir = h_dir;
5134 +       if (!h_dir->i_nlink) {
5135 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5136 +               h_orph = wbr->wbr_orph;
5137 +
5138 +               h_parent = dget(au_h_dptr(parent, bdst));
5139 +               au_set_h_dptr(parent, bdst, NULL);
5140 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5141 +               h_tmpdir = h_orph->d_inode;
5142 +               au_set_h_iptr(dir, bdst, NULL, 0);
5143 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5144 +
5145 +               /* this temporary unlock is safe */
5146 +               if (file)
5147 +                       h_dentry = au_h_fptr(file, au_fbstart(file))->f_dentry;
5148 +               else
5149 +                       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
5150 +               h_inode = h_dentry->d_inode;
5151 +               IMustLock(h_inode);
5152 +               mutex_unlock(&h_inode->i_mutex);
5153 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT2);
5154 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
5155 +       }
5156 +
5157 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE))
5158 +               err = au_cpup_wh(dentry, bdst, len, file);
5159 +       else {
5160 +               struct au_cpup_wh_args args = {
5161 +                       .errp   = &err,
5162 +                       .dentry = dentry,
5163 +                       .bdst   = bdst,
5164 +                       .len    = len,
5165 +                       .file   = file
5166 +               };
5167 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5168 +               if (unlikely(wkq_err))
5169 +                       err = wkq_err;
5170 +       }
5171 +
5172 +       if (h_orph) {
5173 +               mutex_unlock(&h_tmpdir->i_mutex);
5174 +               au_set_h_iptr(dir, bdst, NULL, 0);
5175 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5176 +               au_set_h_dptr(parent, bdst, NULL);
5177 +               au_set_h_dptr(parent, bdst, h_parent);
5178 +       }
5179 +       iput(h_dir);
5180 +       dput(parent);
5181 +
5182 +       return err;
5183 +}
5184 +
5185 +/* ---------------------------------------------------------------------- */
5186 +
5187 +/*
5188 + * generic routine for both of copy-up and copy-down.
5189 + */
5190 +/* cf. revalidate function in file.c */
5191 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5192 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5193 +                        struct dentry *h_parent, void *arg),
5194 +              void *arg)
5195 +{
5196 +       int err;
5197 +       struct au_pin pin;
5198 +       struct dentry *d, *parent, *h_parent, *real_parent;
5199 +
5200 +       err = 0;
5201 +       parent = dget_parent(dentry);
5202 +       if (IS_ROOT(parent))
5203 +               goto out;
5204 +
5205 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5206 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5207 +
5208 +       /* do not use au_dpage */
5209 +       real_parent = parent;
5210 +       while (1) {
5211 +               dput(parent);
5212 +               parent = dget_parent(dentry);
5213 +               h_parent = au_h_dptr(parent, bdst);
5214 +               if (h_parent)
5215 +                       goto out; /* success */
5216 +
5217 +               /* find top dir which is necessary to cpup */
5218 +               do {
5219 +                       d = parent;
5220 +                       dput(parent);
5221 +                       parent = dget_parent(d);
5222 +                       di_read_lock_parent3(parent, !AuLock_IR);
5223 +                       h_parent = au_h_dptr(parent, bdst);
5224 +                       di_read_unlock(parent, !AuLock_IR);
5225 +               } while (!h_parent);
5226 +
5227 +               if (d != real_parent)
5228 +                       di_write_lock_child3(d);
5229 +
5230 +               /* somebody else might create while we were sleeping */
5231 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
5232 +                       if (au_h_dptr(d, bdst))
5233 +                               au_update_dbstart(d);
5234 +
5235 +                       au_pin_set_dentry(&pin, d);
5236 +                       err = au_do_pin(&pin);
5237 +                       if (!err) {
5238 +                               err = cp(d, bdst, h_parent, arg);
5239 +                               au_unpin(&pin);
5240 +                       }
5241 +               }
5242 +
5243 +               if (d != real_parent)
5244 +                       di_write_unlock(d);
5245 +               if (unlikely(err))
5246 +                       break;
5247 +       }
5248 +
5249 + out:
5250 +       dput(parent);
5251 +       return err;
5252 +}
5253 +
5254 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5255 +                      struct dentry *h_parent __maybe_unused ,
5256 +                      void *arg __maybe_unused)
5257 +{
5258 +       return au_sio_cpup_simple(dentry, bdst, -1, AuCpup_DTIME);
5259 +}
5260 +
5261 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5262 +{
5263 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5264 +}
5265 +
5266 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5267 +{
5268 +       int err;
5269 +       struct dentry *parent;
5270 +       struct inode *dir;
5271 +
5272 +       parent = dget_parent(dentry);
5273 +       dir = parent->d_inode;
5274 +       err = 0;
5275 +       if (au_h_iptr(dir, bdst))
5276 +               goto out;
5277 +
5278 +       di_read_unlock(parent, AuLock_IR);
5279 +       di_write_lock_parent(parent);
5280 +       /* someone else might change our inode while we were sleeping */
5281 +       if (!au_h_iptr(dir, bdst))
5282 +               err = au_cpup_dirs(dentry, bdst);
5283 +       di_downgrade_lock(parent, AuLock_IR);
5284 +
5285 + out:
5286 +       dput(parent);
5287 +       return err;
5288 +}
5289 diff --git a/fs/aufs/cpup.h b/fs/aufs/cpup.h
5290 new file mode 100644
5291 index 0000000..240014a
5292 --- /dev/null
5293 +++ b/fs/aufs/cpup.h
5294 @@ -0,0 +1,68 @@
5295 +/*
5296 + * Copyright (C) 2005-2009 Junjiro R. Okajima
5297 + *
5298 + * This program, aufs is free software; you can redistribute it and/or modify
5299 + * it under the terms of the GNU General Public License as published by
5300 + * the Free Software Foundation; either version 2 of the License, or
5301 + * (at your option) any later version.
5302 + */
5303 +
5304 +/*
5305 + * copy-up/down functions
5306 + */
5307 +
5308 +#ifndef __AUFS_CPUP_H__
5309 +#define __AUFS_CPUP_H__
5310 +
5311 +#ifdef __KERNEL__
5312 +
5313 +#include <linux/fs.h>
5314 +#include <linux/aufs_type.h>
5315 +
5316 +void au_cpup_attr_flags(struct inode *dst, struct inode *src);
5317 +void au_cpup_attr_timesizes(struct inode *inode);
5318 +void au_cpup_attr_nlink(struct inode *inode, int force);
5319 +void au_cpup_attr_changeable(struct inode *inode);
5320 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5321 +void au_cpup_attr_all(struct inode *inode, int force);
5322 +
5323 +/* ---------------------------------------------------------------------- */
5324 +
5325 +/* cpup flags */
5326 +#define AuCpup_DTIME   1               /* do dtime_store/revert */
5327 +#define AuCpup_KEEPLINO        (1 << 1)        /* do not clear the lower xino,
5328 +                                          for link(2) */
5329 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5330 +#define au_fset_cpup(flags, name)      { (flags) |= AuCpup_##name; }
5331 +#define au_fclr_cpup(flags, name)      { (flags) &= ~AuCpup_##name; }
5332 +
5333 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5334 +int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
5335 +                      aufs_bindex_t bsrc, loff_t len, unsigned int flags,
5336 +                      struct dentry *dst_parent);
5337 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
5338 +                      unsigned int flags);
5339 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
5340 +                  struct file *file);
5341 +
5342 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5343 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5344 +                        struct dentry *h_parent, void *arg),
5345 +              void *arg);
5346 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5347 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5348 +
5349 +/* ---------------------------------------------------------------------- */
5350 +
5351 +/* keep timestamps when copyup */
5352 +struct au_dtime {
5353 +       struct dentry *dt_dentry;
5354 +       struct path dt_h_path;
5355 +       struct timespec dt_atime, dt_mtime;
5356 +};
5357 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5358 +                   struct path *h_path);
5359 +void au_dtime_revert(struct au_dtime *dt);
5360 +
5361 +#endif /* __KERNEL__ */
5362 +#endif /* __AUFS_CPUP_H__ */
5363 diff --git a/fs/aufs/dbgaufs.c b/fs/aufs/dbgaufs.c
5364 new file mode 100644
5365 index 0000000..cb04b5d
5366 --- /dev/null
5367 +++ b/fs/aufs/dbgaufs.c
5368 @@ -0,0 +1,304 @@
5369 +/*
5370 + * Copyright (C) 2005-2009 Junjiro R. Okajima
5371 + *
5372 + * This program, aufs is free software; you can redistribute it and/or modify
5373 + * it under the terms of the GNU General Public License as published by
5374 + * the Free Software Foundation; either version 2 of the License, or
5375 + * (at your option) any later version.
5376 + */
5377 +
5378 +/*
5379 + * debugfs interface
5380 + */
5381 +
5382 +#include <linux/debugfs.h>
5383 +#include "aufs.h"
5384 +
5385 +#ifndef CONFIG_SYSFS
5386 +#error DEBUG_FS depends upon SYSFS
5387 +#endif
5388 +
5389 +static struct dentry *dbgaufs;
5390 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5391 +
5392 +/* 20 is max digits length of ulong 64 */
5393 +struct dbgaufs_arg {
5394 +       int n;
5395 +       char a[20 * 4];
5396 +};
5397 +
5398 +/*
5399 + * common function for all XINO files
5400 + */
5401 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5402 +                             struct file *file)
5403 +{
5404 +       kfree(file->private_data);
5405 +       return 0;
5406 +}
5407 +
5408 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5409 +{
5410 +       int err;
5411 +       struct kstat st;
5412 +       struct dbgaufs_arg *p;
5413 +
5414 +       err = -ENOMEM;
5415 +       p = kmalloc(sizeof(*p), GFP_NOFS);
5416 +       if (unlikely(!p))
5417 +               goto out;
5418 +
5419 +       err = 0;
5420 +       p->n = 0;
5421 +       file->private_data = p;
5422 +       if (!xf)
5423 +               goto out;
5424 +
5425 +       err = vfs_getattr(xf->f_vfsmnt, xf->f_dentry, &st);
5426 +       if (!err) {
5427 +               if (do_fcnt)
5428 +                       p->n = snprintf
5429 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
5430 +                                (long)file_count(xf), st.blocks, st.blksize,
5431 +                                (long long)st.size);
5432 +               else
5433 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
5434 +                                       st.blocks, st.blksize,
5435 +                                       (long long)st.size);
5436 +               AuDebugOn(p->n >= sizeof(p->a));
5437 +       } else {
5438 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5439 +               err = 0;
5440 +       }
5441 +
5442 + out:
5443 +       return err;
5444 +
5445 +}
5446 +
5447 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
5448 +                              size_t count, loff_t *ppos)
5449 +{
5450 +       struct dbgaufs_arg *p;
5451 +
5452 +       p = file->private_data;
5453 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5454 +}
5455 +
5456 +/* ---------------------------------------------------------------------- */
5457 +
5458 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
5459 +{
5460 +       int err;
5461 +       struct au_sbinfo *sbinfo;
5462 +       struct super_block *sb;
5463 +
5464 +       sbinfo = inode->i_private;
5465 +       sb = sbinfo->si_sb;
5466 +       si_noflush_read_lock(sb);
5467 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
5468 +       si_read_unlock(sb);
5469 +       return err;
5470 +}
5471 +
5472 +static const struct file_operations dbgaufs_xib_fop = {
5473 +       .open           = dbgaufs_xib_open,
5474 +       .release        = dbgaufs_xi_release,
5475 +       .read           = dbgaufs_xi_read
5476 +};
5477 +
5478 +/* ---------------------------------------------------------------------- */
5479 +
5480 +#define DbgaufsXi_PREFIX "xi"
5481 +
5482 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
5483 +{
5484 +       int err;
5485 +       long l;
5486 +       struct au_sbinfo *sbinfo;
5487 +       struct super_block *sb;
5488 +       struct file *xf;
5489 +       struct qstr *name;
5490 +
5491 +       err = -ENOENT;
5492 +       xf = NULL;
5493 +       name = &file->f_dentry->d_name;
5494 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
5495 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
5496 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
5497 +               goto out;
5498 +       err = strict_strtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
5499 +       if (unlikely(err))
5500 +               goto out;
5501 +
5502 +       sbinfo = inode->i_private;
5503 +       sb = sbinfo->si_sb;
5504 +       si_noflush_read_lock(sb);
5505 +       if (l <= au_sbend(sb)) {
5506 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
5507 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
5508 +       } else
5509 +               err = -ENOENT;
5510 +       si_read_unlock(sb);
5511 +
5512 + out:
5513 +       return err;
5514 +}
5515 +
5516 +static const struct file_operations dbgaufs_xino_fop = {
5517 +       .open           = dbgaufs_xino_open,
5518 +       .release        = dbgaufs_xi_release,
5519 +       .read           = dbgaufs_xi_read
5520 +};
5521 +
5522 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5523 +{
5524 +       aufs_bindex_t bend;
5525 +       struct au_branch *br;
5526 +       struct au_xino_file *xi;
5527 +
5528 +       if (!au_sbi(sb)->si_dbgaufs)
5529 +               return;
5530 +
5531 +       bend = au_sbend(sb);
5532 +       for (; bindex <= bend; bindex++) {
5533 +               br = au_sbr(sb, bindex);
5534 +               xi = &br->br_xino;
5535 +               if (xi->xi_dbgaufs) {
5536 +                       debugfs_remove(xi->xi_dbgaufs);
5537 +                       xi->xi_dbgaufs = NULL;
5538 +               }
5539 +       }
5540 +}
5541 +
5542 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
5543 +{
5544 +       struct au_sbinfo *sbinfo;
5545 +       struct dentry *parent;
5546 +       struct au_branch *br;
5547 +       struct au_xino_file *xi;
5548 +       aufs_bindex_t bend;
5549 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
5550 +
5551 +       sbinfo = au_sbi(sb);
5552 +       parent = sbinfo->si_dbgaufs;
5553 +       if (!parent)
5554 +               return;
5555 +
5556 +       bend = au_sbend(sb);
5557 +       for (; bindex <= bend; bindex++) {
5558 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
5559 +               br = au_sbr(sb, bindex);
5560 +               xi = &br->br_xino;
5561 +               AuDebugOn(xi->xi_dbgaufs);
5562 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
5563 +                                                    sbinfo, &dbgaufs_xino_fop);
5564 +               /* ignore an error */
5565 +               if (unlikely(!xi->xi_dbgaufs))
5566 +                       AuWarn1("failed %s under debugfs\n", name);
5567 +       }
5568 +}
5569 +
5570 +/* ---------------------------------------------------------------------- */
5571 +
5572 +#ifdef CONFIG_AUFS_EXPORT
5573 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
5574 +{
5575 +       int err;
5576 +       struct au_sbinfo *sbinfo;
5577 +       struct super_block *sb;
5578 +
5579 +       sbinfo = inode->i_private;
5580 +       sb = sbinfo->si_sb;
5581 +       si_noflush_read_lock(sb);
5582 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
5583 +       si_read_unlock(sb);
5584 +       return err;
5585 +}
5586 +
5587 +static const struct file_operations dbgaufs_xigen_fop = {
5588 +       .open           = dbgaufs_xigen_open,
5589 +       .release        = dbgaufs_xi_release,
5590 +       .read           = dbgaufs_xi_read
5591 +};
5592 +
5593 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5594 +{
5595 +       int err;
5596 +
5597 +       err = -EIO;
5598 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
5599 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5600 +                &dbgaufs_xigen_fop);
5601 +       if (sbinfo->si_dbgaufs_xigen)
5602 +               err = 0;
5603 +
5604 +       return err;
5605 +}
5606 +#else
5607 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5608 +{
5609 +       return 0;
5610 +}
5611 +#endif /* CONFIG_AUFS_EXPORT */
5612 +
5613 +/* ---------------------------------------------------------------------- */
5614 +
5615 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5616 +{
5617 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
5618 +       sbinfo->si_dbgaufs = NULL;
5619 +       kobject_put(&sbinfo->si_kobj);
5620 +}
5621 +
5622 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5623 +{
5624 +       int err;
5625 +       char name[SysaufsSiNameLen];
5626 +
5627 +       err = -ENOENT;
5628 +       if (!dbgaufs) {
5629 +               AuErr1("/debug/aufs is uninitialized\n");
5630 +               goto out;
5631 +       }
5632 +
5633 +       err = -EIO;
5634 +       sysaufs_name(sbinfo, name);
5635 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
5636 +       if (unlikely(!sbinfo->si_dbgaufs))
5637 +               goto out;
5638 +       kobject_get(&sbinfo->si_kobj);
5639 +
5640 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
5641 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5642 +                &dbgaufs_xib_fop);
5643 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
5644 +               goto out_dir;
5645 +
5646 +       err = dbgaufs_xigen_init(sbinfo);
5647 +       if (!err)
5648 +               goto out; /* success */
5649 +
5650 + out_dir:
5651 +       dbgaufs_si_fin(sbinfo);
5652 + out:
5653 +       return err;
5654 +}
5655 +
5656 +/* ---------------------------------------------------------------------- */
5657 +
5658 +void dbgaufs_fin(void)
5659 +{
5660 +       debugfs_remove(dbgaufs);
5661 +}
5662 +
5663 +int __init dbgaufs_init(void)
5664 +{
5665 +       int err;
5666 +
5667 +       err = -EIO;
5668 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
5669 +       if (dbgaufs)
5670 +               err = 0;
5671 +       return err;
5672 +}
5673 diff --git a/fs/aufs/dbgaufs.h b/fs/aufs/dbgaufs.h
5674 new file mode 100644
5675 index 0000000..dfb0f0a
5676 --- /dev/null
5677 +++ b/fs/aufs/dbgaufs.h
5678 @@ -0,0 +1,68 @@
5679 +/*
5680 + * Copyright (C) 2005-2009 Junjiro R. Okajima
5681 + *
5682 + * This program, aufs is free software; you can redistribute it and/or modify
5683 + * it under the terms of the GNU General Public License as published by
5684 + * the Free Software Foundation; either version 2 of the License, or
5685 + * (at your option) any later version.
5686 + */
5687 +
5688 +/*
5689 + * debugfs interface
5690 + */
5691 +
5692 +#ifndef __DBGAUFS_H__
5693 +#define __DBGAUFS_H__
5694 +
5695 +#ifdef __KERNEL__
5696 +
5697 +#include <linux/fs.h>
5698 +#include <linux/aufs_type.h>
5699 +
5700 +struct au_sbinfo;
5701 +#ifdef CONFIG_DEBUG_FS
5702 +/* dbgaufs.c */
5703 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
5704 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
5705 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
5706 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
5707 +void dbgaufs_fin(void);
5708 +int __init dbgaufs_init(void);
5709 +
5710 +#else
5711 +
5712 +static inline
5713 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5714 +{
5715 +       /* empty */
5716 +}
5717 +
5718 +static inline
5719 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
5720 +{
5721 +       /* empty */
5722 +}
5723 +
5724 +static inline
5725 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5726 +{
5727 +       /* empty */
5728 +}
5729 +
5730 +static inline
5731 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5732 +{
5733 +       return 0;
5734 +}
5735 +
5736 +#define dbgaufs_fin()  do {} while (0)
5737 +
5738 +static inline
5739 +int __init dbgaufs_init(void)
5740 +{
5741 +       return 0;
5742 +}
5743 +#endif /* CONFIG_DEBUG_FS */
5744 +
5745 +#endif /* __KERNEL__ */
5746 +#endif /* __DBGAUFS_H__ */
5747 diff --git a/fs/aufs/dcsub.c b/fs/aufs/dcsub.c
5748 new file mode 100644
5749 index 0000000..e4597e4
5750 --- /dev/null
5751 +++ b/fs/aufs/dcsub.c
5752 @@ -0,0 +1,214 @@
5753 +/*
5754 + * Copyright (C) 2005-2009 Junjiro R. Okajima
5755 + *
5756 + * This program, aufs is free software; you can redistribute it and/or modify
5757 + * it under the terms of the GNU General Public License as published by
5758 + * the Free Software Foundation; either version 2 of the License, or
5759 + * (at your option) any later version.
5760 + */
5761 +
5762 +/*
5763 + * sub-routines for dentry cache
5764 + */
5765 +
5766 +#include "aufs.h"
5767 +
5768 +static void au_dpage_free(struct au_dpage *dpage)
5769 +{
5770 +       int i;
5771 +       struct dentry **p;
5772 +
5773 +       p = dpage->dentries;
5774 +       for (i = 0; i < dpage->ndentry; i++)
5775 +               dput(*p++);
5776 +       free_page((unsigned long)dpage->dentries);
5777 +}
5778 +
5779 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
5780 +{
5781 +       int err;
5782 +       void *p;
5783 +
5784 +       err = -ENOMEM;
5785 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
5786 +       if (unlikely(!dpages->dpages))
5787 +               goto out;
5788 +
5789 +       p = (void *)__get_free_page(gfp);
5790 +       if (unlikely(!p))
5791 +               goto out_dpages;
5792 +
5793 +       dpages->dpages[0].ndentry = 0;
5794 +       dpages->dpages[0].dentries = p;
5795 +       dpages->ndpage = 1;
5796 +       return 0; /* success */
5797 +
5798 + out_dpages:
5799 +       kfree(dpages->dpages);
5800 + out:
5801 +       return err;
5802 +}
5803 +
5804 +void au_dpages_free(struct au_dcsub_pages *dpages)
5805 +{
5806 +       int i;
5807 +       struct au_dpage *p;
5808 +
5809 +       p = dpages->dpages;
5810 +       for (i = 0; i < dpages->ndpage; i++)
5811 +               au_dpage_free(p++);
5812 +       kfree(dpages->dpages);
5813 +}
5814 +
5815 +static int au_dpages_append(struct au_dcsub_pages *dpages,
5816 +                           struct dentry *dentry, gfp_t gfp)
5817 +{
5818 +       int err, sz;
5819 +       struct au_dpage *dpage;
5820 +       void *p;
5821 +
5822 +       dpage = dpages->dpages + dpages->ndpage - 1;
5823 +       sz = PAGE_SIZE / sizeof(dentry);
5824 +       if (unlikely(dpage->ndentry >= sz)) {
5825 +               AuLabel(new dpage);
5826 +               err = -ENOMEM;
5827 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
5828 +               p = au_kzrealloc(dpages->dpages, sz,
5829 +                                sz + sizeof(*dpages->dpages), gfp);
5830 +               if (unlikely(!p))
5831 +                       goto out;
5832 +
5833 +               dpages->dpages = p;
5834 +               dpage = dpages->dpages + dpages->ndpage;
5835 +               p = (void *)__get_free_page(gfp);
5836 +               if (unlikely(!p))
5837 +                       goto out;
5838 +
5839 +               dpage->ndentry = 0;
5840 +               dpage->dentries = p;
5841 +               dpages->ndpage++;
5842 +       }
5843 +
5844 +       dpage->dentries[dpage->ndentry++] = dget(dentry);
5845 +       return 0; /* success */
5846 +
5847 + out:
5848 +       return err;
5849 +}
5850 +
5851 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5852 +                  au_dpages_test test, void *arg)
5853 +{
5854 +       int err;
5855 +       struct dentry *this_parent = root;
5856 +       struct list_head *next;
5857 +       struct super_block *sb = root->d_sb;
5858 +
5859 +       err = 0;
5860 +       spin_lock(&dcache_lock);
5861 + repeat:
5862 +       next = this_parent->d_subdirs.next;
5863 + resume:
5864 +       if (this_parent->d_sb == sb
5865 +           && !IS_ROOT(this_parent)
5866 +           && atomic_read(&this_parent->d_count)
5867 +           && this_parent->d_inode
5868 +           && (!test || test(this_parent, arg))) {
5869 +               err = au_dpages_append(dpages, this_parent, GFP_ATOMIC);
5870 +               if (unlikely(err))
5871 +                       goto out;
5872 +       }
5873 +
5874 +       while (next != &this_parent->d_subdirs) {
5875 +               struct list_head *tmp = next;
5876 +               struct dentry *dentry = list_entry(tmp, struct dentry,
5877 +                                                  d_u.d_child);
5878 +               next = tmp->next;
5879 +               if (/*d_unhashed(dentry) || */!dentry->d_inode)
5880 +                       continue;
5881 +               if (!list_empty(&dentry->d_subdirs)) {
5882 +                       this_parent = dentry;
5883 +                       goto repeat;
5884 +               }
5885 +               if (dentry->d_sb == sb
5886 +                   && atomic_read(&dentry->d_count)
5887 +                   && (!test || test(dentry, arg))) {
5888 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5889 +                       if (unlikely(err))
5890 +                               goto out;
5891 +               }
5892 +       }
5893 +
5894 +       if (this_parent != root) {
5895 +               next = this_parent->d_u.d_child.next;
5896 +               this_parent = this_parent->d_parent; /* dcache_lock is locked */
5897 +               goto resume;
5898 +       }
5899 + out:
5900 +       spin_unlock(&dcache_lock);
5901 +       return err;
5902 +}
5903 +
5904 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5905 +                      int do_include, au_dpages_test test, void *arg)
5906 +{
5907 +       int err;
5908 +
5909 +       err = 0;
5910 +       spin_lock(&dcache_lock);
5911 +       if (do_include && (!test || test(dentry, arg))) {
5912 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5913 +               if (unlikely(err))
5914 +                       goto out;
5915 +       }
5916 +       while (!IS_ROOT(dentry)) {
5917 +               dentry = dentry->d_parent; /* dcache_lock is locked */
5918 +               if (!test || test(dentry, arg)) {
5919 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5920 +                       if (unlikely(err))
5921 +                               break;
5922 +               }
5923 +       }
5924 +
5925 + out:
5926 +       spin_unlock(&dcache_lock);
5927 +
5928 +       return err;
5929 +}
5930 +
5931 +struct dentry *au_test_subdir(struct dentry *d1, struct dentry *d2)
5932 +{
5933 +       struct dentry *trap, **dentries;
5934 +       int err, i, j;
5935 +       struct au_dcsub_pages dpages;
5936 +       struct au_dpage *dpage;
5937 +
5938 +       trap = ERR_PTR(-ENOMEM);
5939 +       err = au_dpages_init(&dpages, GFP_NOFS);
5940 +       if (unlikely(err))
5941 +               goto out;
5942 +       err = au_dcsub_pages_rev(&dpages, d1, /*do_include*/1, NULL, NULL);
5943 +       if (unlikely(err))
5944 +               goto out_dpages;
5945 +
5946 +       trap = d1;
5947 +       for (i = 0; !err && i < dpages.ndpage; i++) {
5948 +               dpage = dpages.dpages + i;
5949 +               dentries = dpage->dentries;
5950 +               for (j = 0; !err && j < dpage->ndentry; j++) {
5951 +                       struct dentry *d;
5952 +
5953 +                       d = dentries[j];
5954 +                       err = (d == d2);
5955 +                       if (!err)
5956 +                               trap = d;
5957 +               }
5958 +       }
5959 +       if (!err)
5960 +               trap = NULL;
5961 +
5962 + out_dpages:
5963 +       au_dpages_free(&dpages);
5964 + out:
5965 +       return trap;
5966 +}
5967 diff --git a/fs/aufs/dcsub.h b/fs/aufs/dcsub.h
5968 new file mode 100644
5969 index 0000000..89a7745
5970 --- /dev/null
5971 +++ b/fs/aufs/dcsub.h
5972 @@ -0,0 +1,43 @@
5973 +/*
5974 + * Copyright (C) 2005-2009 Junjiro R. Okajima
5975 + *
5976 + * This program, aufs is free software; you can redistribute it and/or modify
5977 + * it under the terms of the GNU General Public License as published by
5978 + * the Free Software Foundation; either version 2 of the License, or
5979 + * (at your option) any later version.
5980 + */
5981 +
5982 +/*
5983 + * sub-routines for dentry cache
5984 + */
5985 +
5986 +#ifndef __AUFS_DCSUB_H__
5987 +#define __AUFS_DCSUB_H__
5988 +
5989 +#ifdef __KERNEL__
5990 +
5991 +#include <linux/dcache.h>
5992 +
5993 +struct au_dpage {
5994 +       int ndentry;
5995 +       struct dentry **dentries;
5996 +};
5997 +
5998 +struct au_dcsub_pages {
5999 +       int ndpage;
6000 +       struct au_dpage *dpages;
6001 +};
6002 +
6003 +/* ---------------------------------------------------------------------- */
6004 +
6005 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6006 +void au_dpages_free(struct au_dcsub_pages *dpages);
6007 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6008 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6009 +                  au_dpages_test test, void *arg);
6010 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6011 +                      int do_include, au_dpages_test test, void *arg);
6012 +struct dentry *au_test_subdir(struct dentry *d1, struct dentry *d2);
6013 +
6014 +#endif /* __KERNEL__ */
6015 +#endif /* __AUFS_DCSUB_H__ */
6016 diff --git a/fs/aufs/debug.c b/fs/aufs/debug.c
6017 new file mode 100644
6018 index 0000000..de38931
6019 --- /dev/null
6020 +++ b/fs/aufs/debug.c
6021 @@ -0,0 +1,416 @@
6022 +/*
6023 + * Copyright (C) 2005-2009 Junjiro R. Okajima
6024 + *
6025 + * This program, aufs is free software; you can redistribute it and/or modify
6026 + * it under the terms of the GNU General Public License as published by
6027 + * the Free Software Foundation; either version 2 of the License, or
6028 + * (at your option) any later version.
6029 + */
6030 +
6031 +/*
6032 + * debug print functions
6033 + */
6034 +
6035 +#include "aufs.h"
6036 +
6037 +int aufs_debug;
6038 +MODULE_PARM_DESC(debug, "debug print");
6039 +module_param_named(debug, aufs_debug, int, S_IRUGO | S_IWUSR | S_IWGRP);
6040 +
6041 +char *au_plevel = KERN_DEBUG;
6042 +#define dpri(fmt, arg...) do { \
6043 +       if (au_debug_test()) \
6044 +               printk("%s" fmt, au_plevel, ##arg); \
6045 +} while (0)
6046 +
6047 +/* ---------------------------------------------------------------------- */
6048 +
6049 +void au_dpri_whlist(struct au_nhash *whlist)
6050 +{
6051 +       unsigned long ul, n;
6052 +       struct hlist_head *head;
6053 +       struct au_vdir_wh *tpos;
6054 +       struct hlist_node *pos;
6055 +
6056 +       n = whlist->nh_num;
6057 +       head = whlist->nh_head;
6058 +       for (ul = 0; ul < n; ul++) {
6059 +               hlist_for_each_entry(tpos, pos, head, wh_hash)
6060 +                       dpri("b%d, %.*s, %d\n",
6061 +                            tpos->wh_bindex,
6062 +                            tpos->wh_str.len, tpos->wh_str.name,
6063 +                            tpos->wh_str.len);
6064 +               head++;
6065 +       }
6066 +}
6067 +
6068 +void au_dpri_vdir(struct au_vdir *vdir)
6069 +{
6070 +       unsigned long ul;
6071 +       union au_vdir_deblk_p p;
6072 +       unsigned char *o;
6073 +
6074 +       if (!vdir || IS_ERR(vdir)) {
6075 +               dpri("err %ld\n", PTR_ERR(vdir));
6076 +               return;
6077 +       }
6078 +
6079 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6080 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6081 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6082 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6083 +               p.deblk = vdir->vd_deblk[ul];
6084 +               o = p.deblk;
6085 +               dpri("[%lu]: %p\n", ul, o);
6086 +       }
6087 +}
6088 +
6089 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode,
6090 +                       struct dentry *wh)
6091 +{
6092 +       char *n = NULL;
6093 +       int l = 0;
6094 +
6095 +       if (!inode || IS_ERR(inode)) {
6096 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
6097 +               return -1;
6098 +       }
6099 +
6100 +       /* the type of i_blocks depends upon CONFIG_LSF */
6101 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
6102 +                    && sizeof(inode->i_blocks) != sizeof(u64));
6103 +       if (wh) {
6104 +               n = (void *)wh->d_name.name;
6105 +               l = wh->d_name.len;
6106 +       }
6107 +
6108 +       dpri("i%d: i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
6109 +            " ct %lld, np %lu, st 0x%lx, f 0x%x, g %x%s%.*s\n",
6110 +            bindex,
6111 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
6112 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
6113 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
6114 +            (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
6115 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
6116 +            inode->i_state, inode->i_flags, inode->i_generation,
6117 +            l ? ", wh " : "", l, n);
6118 +       return 0;
6119 +}
6120 +
6121 +void au_dpri_inode(struct inode *inode)
6122 +{
6123 +       struct au_iinfo *iinfo;
6124 +       aufs_bindex_t bindex;
6125 +       int err;
6126 +
6127 +       err = do_pri_inode(-1, inode, NULL);
6128 +       if (err || !au_test_aufs(inode->i_sb))
6129 +               return;
6130 +
6131 +       iinfo = au_ii(inode);
6132 +       if (!iinfo)
6133 +               return;
6134 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
6135 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode));
6136 +       if (iinfo->ii_bstart < 0)
6137 +               return;
6138 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++)
6139 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode,
6140 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
6141 +}
6142 +
6143 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6144 +{
6145 +       struct dentry *wh = NULL;
6146 +
6147 +       if (!dentry || IS_ERR(dentry)) {
6148 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6149 +               return -1;
6150 +       }
6151 +       /* do not call dget_parent() here */
6152 +       dpri("d%d: %.*s?/%.*s, %s, cnt %d, flags 0x%x\n",
6153 +            bindex,
6154 +            AuDLNPair(dentry->d_parent), AuDLNPair(dentry),
6155 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
6156 +            atomic_read(&dentry->d_count), dentry->d_flags);
6157 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
6158 +               struct au_iinfo *iinfo = au_ii(dentry->d_inode);
6159 +               if (iinfo)
6160 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6161 +       }
6162 +       do_pri_inode(bindex, dentry->d_inode, wh);
6163 +       return 0;
6164 +}
6165 +
6166 +void au_dpri_dentry(struct dentry *dentry)
6167 +{
6168 +       struct au_dinfo *dinfo;
6169 +       aufs_bindex_t bindex;
6170 +       int err;
6171 +
6172 +       err = do_pri_dentry(-1, dentry);
6173 +       if (err || !au_test_aufs(dentry->d_sb))
6174 +               return;
6175 +
6176 +       dinfo = au_di(dentry);
6177 +       if (!dinfo)
6178 +               return;
6179 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d\n",
6180 +            dinfo->di_bstart, dinfo->di_bend,
6181 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry));
6182 +       if (dinfo->di_bstart < 0)
6183 +               return;
6184 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6185 +               do_pri_dentry(bindex, dinfo->di_hdentry[0 + bindex].hd_dentry);
6186 +}
6187 +
6188 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
6189 +{
6190 +       char a[32];
6191 +
6192 +       if (!file || IS_ERR(file)) {
6193 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
6194 +               return -1;
6195 +       }
6196 +       a[0] = 0;
6197 +       if (bindex < 0
6198 +           && file->f_dentry
6199 +           && au_test_aufs(file->f_dentry->d_sb)
6200 +           && au_fi(file))
6201 +               snprintf(a, sizeof(a), ", mmapped %d", au_test_mmapped(file));
6202 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, pos %llu%s\n",
6203 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
6204 +            file->f_pos, a);
6205 +       if (file->f_dentry)
6206 +               do_pri_dentry(bindex, file->f_dentry);
6207 +       return 0;
6208 +}
6209 +
6210 +void au_dpri_file(struct file *file)
6211 +{
6212 +       struct au_finfo *finfo;
6213 +       aufs_bindex_t bindex;
6214 +       int err;
6215 +
6216 +       err = do_pri_file(-1, file);
6217 +       if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
6218 +               return;
6219 +
6220 +       finfo = au_fi(file);
6221 +       if (!finfo)
6222 +               return;
6223 +       if (finfo->fi_bstart < 0)
6224 +               return;
6225 +       for (bindex = finfo->fi_bstart; bindex <= finfo->fi_bend; bindex++) {
6226 +               struct au_hfile *hf;
6227 +
6228 +               hf = finfo->fi_hfile + bindex;
6229 +               do_pri_file(bindex, hf ? hf->hf_file : NULL);
6230 +       }
6231 +}
6232 +
6233 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
6234 +{
6235 +       struct vfsmount *mnt;
6236 +       struct super_block *sb;
6237 +
6238 +       if (!br || IS_ERR(br))
6239 +               goto out;
6240 +       mnt = br->br_mnt;
6241 +       if (!mnt || IS_ERR(mnt))
6242 +               goto out;
6243 +       sb = mnt->mnt_sb;
6244 +       if (!sb || IS_ERR(sb))
6245 +               goto out;
6246 +
6247 +       dpri("s%d: {perm 0x%x, cnt %d, wbr %p}, "
6248 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt(BIAS) %d, active %d, "
6249 +            "xino %d\n",
6250 +            bindex, br->br_perm, atomic_read(&br->br_count), br->br_wbr,
6251 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
6252 +            sb->s_flags, sb->s_count - S_BIAS,
6253 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
6254 +       return 0;
6255 +
6256 + out:
6257 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
6258 +       return -1;
6259 +}
6260 +
6261 +void au_dpri_sb(struct super_block *sb)
6262 +{
6263 +       struct au_sbinfo *sbinfo;
6264 +       aufs_bindex_t bindex;
6265 +       int err;
6266 +       /* to reuduce stack size */
6267 +       struct {
6268 +               struct vfsmount mnt;
6269 +               struct au_branch fake;
6270 +       } *a;
6271 +
6272 +       /* this function can be called from magic sysrq */
6273 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
6274 +       if (unlikely(!a)) {
6275 +               dpri("no memory\n");
6276 +               return;
6277 +       }
6278 +
6279 +       a->mnt.mnt_sb = sb;
6280 +       a->fake.br_perm = 0;
6281 +       a->fake.br_mnt = &a->mnt;
6282 +       a->fake.br_xino.xi_file = NULL;
6283 +       atomic_set(&a->fake.br_count, 0);
6284 +       smp_mb(); /* atomic_set */
6285 +       err = do_pri_br(-1, &a->fake);
6286 +       kfree(a);
6287 +       dpri("dev 0x%x\n", sb->s_dev);
6288 +       if (err || !au_test_aufs(sb))
6289 +               return;
6290 +
6291 +       sbinfo = au_sbi(sb);
6292 +       if (!sbinfo)
6293 +               return;
6294 +       dpri("nw %d, gen %u, kobj %d\n",
6295 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
6296 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
6297 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
6298 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
6299 +}
6300 +
6301 +/* ---------------------------------------------------------------------- */
6302 +
6303 +void au_dbg_sleep_jiffy(int jiffy)
6304 +{
6305 +       while (jiffy)
6306 +               jiffy = schedule_timeout_uninterruptible(jiffy);
6307 +}
6308 +
6309 +void au_dbg_iattr(struct iattr *ia)
6310 +{
6311 +#define AuBit(name)    if (ia->ia_valid & ATTR_ ## name) \
6312 +                               dpri(#name "\n")
6313 +       AuBit(MODE);
6314 +       AuBit(UID);
6315 +       AuBit(GID);
6316 +       AuBit(SIZE);
6317 +       AuBit(ATIME);
6318 +       AuBit(MTIME);
6319 +       AuBit(CTIME);
6320 +       AuBit(ATIME_SET);
6321 +       AuBit(MTIME_SET);
6322 +       AuBit(FORCE);
6323 +       AuBit(ATTR_FLAG);
6324 +       AuBit(KILL_SUID);
6325 +       AuBit(KILL_SGID);
6326 +       AuBit(FILE);
6327 +       AuBit(KILL_PRIV);
6328 +       AuBit(OPEN);
6329 +       AuBit(TIMES_SET);
6330 +#undef AuBit
6331 +       dpri("ia_file %p\n", ia->ia_file);
6332 +}
6333 +
6334 +/* ---------------------------------------------------------------------- */
6335 +
6336 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
6337 +{
6338 +       struct dentry *parent;
6339 +
6340 +       parent = dget_parent(dentry);
6341 +       AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode)
6342 +                 || IS_ROOT(dentry)
6343 +                 || au_digen(parent) != sigen);
6344 +       dput(parent);
6345 +}
6346 +
6347 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
6348 +{
6349 +       struct dentry *parent;
6350 +
6351 +       parent = dget_parent(dentry);
6352 +       AuDebugOn(S_ISDIR(dentry->d_inode->i_mode)
6353 +                 || au_digen(parent) != sigen);
6354 +       dput(parent);
6355 +}
6356 +
6357 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6358 +{
6359 +       int err, i, j;
6360 +       struct au_dcsub_pages dpages;
6361 +       struct au_dpage *dpage;
6362 +       struct dentry **dentries;
6363 +
6364 +       err = au_dpages_init(&dpages, GFP_NOFS);
6365 +       AuDebugOn(err);
6366 +       err = au_dcsub_pages_rev(&dpages, parent, /*do_include*/1, NULL, NULL);
6367 +       AuDebugOn(err);
6368 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
6369 +               dpage = dpages.dpages + i;
6370 +               dentries = dpage->dentries;
6371 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
6372 +                       AuDebugOn(au_digen(dentries[j]) != sigen);
6373 +       }
6374 +       au_dpages_free(&dpages);
6375 +}
6376 +
6377 +void au_dbg_verify_hf(struct au_finfo *finfo)
6378 +{
6379 +       struct au_hfile *hf;
6380 +       aufs_bindex_t bend, bindex;
6381 +
6382 +       if (finfo->fi_bstart >= 0) {
6383 +               bend = finfo->fi_bend;
6384 +               for (bindex = finfo->fi_bstart; bindex <= bend; bindex++) {
6385 +                       hf = finfo->fi_hfile + bindex;
6386 +                       AuDebugOn(hf->hf_file || hf->hf_br);
6387 +               }
6388 +       }
6389 +}
6390 +
6391 +void au_dbg_verify_kthread(void)
6392 +{
6393 +       if (au_test_wkq(current)) {
6394 +               au_dbg_blocked();
6395 +               BUG();
6396 +       }
6397 +}
6398 +
6399 +/* ---------------------------------------------------------------------- */
6400 +
6401 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
6402 +{
6403 +#ifdef AuForceNoPlink
6404 +       au_opt_clr(sbinfo->si_mntflags, PLINK);
6405 +#endif
6406 +#ifdef AuForceNoXino
6407 +       au_opt_clr(sbinfo->si_mntflags, XINO);
6408 +#endif
6409 +#ifdef AuForceNoRefrof
6410 +       au_opt_clr(sbinfo->si_mntflags, REFROF);
6411 +#endif
6412 +#ifdef AuForceHinotify
6413 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_HINOTIFY);
6414 +#endif
6415 +}
6416 +
6417 +int __init au_debug_init(void)
6418 +{
6419 +       aufs_bindex_t bindex;
6420 +       struct au_vdir_destr destr;
6421 +
6422 +       bindex = -1;
6423 +       AuDebugOn(bindex >= 0);
6424 +
6425 +       destr.len = -1;
6426 +       AuDebugOn(destr.len < NAME_MAX);
6427 +
6428 +#ifdef CONFIG_4KSTACKS
6429 +       AuWarn("CONFIG_4KSTACKS is defined.\n");
6430 +#endif
6431 +
6432 +#ifdef AuForceNoBrs
6433 +       sysaufs_brs = 0;
6434 +#endif
6435 +
6436 +       return 0;
6437 +}
6438 diff --git a/fs/aufs/debug.h b/fs/aufs/debug.h
6439 new file mode 100644
6440 index 0000000..36ff0ea
6441 --- /dev/null
6442 +++ b/fs/aufs/debug.h
6443 @@ -0,0 +1,243 @@
6444 +/*
6445 + * Copyright (C) 2005-2009 Junjiro R. Okajima
6446 + *
6447 + * This program, aufs is free software; you can redistribute it and/or modify
6448 + * it under the terms of the GNU General Public License as published by
6449 + * the Free Software Foundation; either version 2 of the License, or
6450 + * (at your option) any later version.
6451 + */
6452 +
6453 +/*
6454 + * debug print functions
6455 + */
6456 +
6457 +#ifndef __AUFS_DEBUG_H__
6458 +#define __AUFS_DEBUG_H__
6459 +
6460 +#ifdef __KERNEL__
6461 +
6462 +#include <linux/delay.h>
6463 +#include <linux/fs.h>
6464 +#include <linux/kd.h>
6465 +#include <linux/vt_kern.h>
6466 +#include <linux/sysrq.h>
6467 +#include <linux/aufs_type.h>
6468 +
6469 +#ifdef CONFIG_AUFS_DEBUG
6470 +#define AuDebugOn(a)           BUG_ON(a)
6471 +
6472 +/* module parameter */
6473 +extern int aufs_debug;
6474 +static inline void au_debug(int n)
6475 +{
6476 +       aufs_debug = n;
6477 +       smp_mb();
6478 +}
6479 +
6480 +static inline int au_debug_test(void)
6481 +{
6482 +       return aufs_debug;
6483 +}
6484 +#else
6485 +#define AuDebugOn(a)           do {} while (0)
6486 +#define au_debug()             do {} while (0)
6487 +static inline int au_debug_test(void)
6488 +{
6489 +       return 0;
6490 +}
6491 +#endif /* CONFIG_AUFS_DEBUG */
6492 +
6493 +/* ---------------------------------------------------------------------- */
6494 +
6495 +/* debug print */
6496 +
6497 +#define AuDpri(lvl, fmt, arg...) \
6498 +       printk(lvl AUFS_NAME " %s:%d:%s[%d]: " fmt, \
6499 +              __func__, __LINE__, current->comm, current->pid, ##arg)
6500 +#define AuDbg(fmt, arg...) do { \
6501 +       if (au_debug_test()) \
6502 +               AuDpri(KERN_DEBUG, fmt, ##arg); \
6503 +} while (0)
6504 +#define AuLabel(l)             AuDbg(#l "\n")
6505 +#define AuInfo(fmt, arg...)    AuDpri(KERN_INFO, fmt, ##arg)
6506 +#define AuWarn(fmt, arg...)    AuDpri(KERN_WARNING, fmt, ##arg)
6507 +#define AuErr(fmt, arg...)     AuDpri(KERN_ERR, fmt, ##arg)
6508 +#define AuIOErr(fmt, arg...)   AuErr("I/O Error, " fmt, ##arg)
6509 +#define AuWarn1(fmt, arg...) do { \
6510 +       static unsigned char _c; \
6511 +       if (!_c++) \
6512 +               AuWarn(fmt, ##arg); \
6513 +} while (0)
6514 +
6515 +#define AuErr1(fmt, arg...) do { \
6516 +       static unsigned char _c; \
6517 +       if (!_c++) \
6518 +               AuErr(fmt, ##arg); \
6519 +} while (0)
6520 +
6521 +#define AuIOErr1(fmt, arg...) do { \
6522 +       static unsigned char _c; \
6523 +       if (!_c++) \
6524 +               AuIOErr(fmt, ##arg); \
6525 +} while (0)
6526 +
6527 +#define AuUnsupportMsg "This operation is not supported." \
6528 +                       " Please report this application to aufs-users ML."
6529 +#define AuUnsupport(fmt, args...) do { \
6530 +       AuErr(AuUnsupportMsg "\n" fmt, ##args); \
6531 +       dump_stack(); \
6532 +} while (0)
6533 +
6534 +#define AuTraceErr(e) do { \
6535 +       if (unlikely((e) < 0)) \
6536 +               AuDbg("err %d\n", (int)(e)); \
6537 +} while (0)
6538 +
6539 +#define AuTraceErrPtr(p) do { \
6540 +       if (IS_ERR(p)) \
6541 +               AuDbg("err %ld\n", PTR_ERR(p)); \
6542 +} while (0)
6543 +
6544 +/* dirty macros for debug print, use with "%.*s" and caution */
6545 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
6546 +#define AuDLNPair(d)           AuLNPair(&(d)->d_name)
6547 +
6548 +/* ---------------------------------------------------------------------- */
6549 +
6550 +struct au_sbinfo;
6551 +struct au_finfo;
6552 +#ifdef CONFIG_AUFS_DEBUG
6553 +extern char *au_plevel;
6554 +struct au_nhash;
6555 +void au_dpri_whlist(struct au_nhash *whlist);
6556 +struct au_vdir;
6557 +void au_dpri_vdir(struct au_vdir *vdir);
6558 +void au_dpri_inode(struct inode *inode);
6559 +void au_dpri_dentry(struct dentry *dentry);
6560 +void au_dpri_file(struct file *filp);
6561 +void au_dpri_sb(struct super_block *sb);
6562 +
6563 +void au_dbg_sleep_jiffy(int jiffy);
6564 +void au_dbg_iattr(struct iattr *ia);
6565 +
6566 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen);
6567 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen);
6568 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
6569 +void au_dbg_verify_hf(struct au_finfo *finfo);
6570 +void au_dbg_verify_kthread(void);
6571 +
6572 +int __init au_debug_init(void);
6573 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
6574 +#define AuDbgWhlist(w) do { \
6575 +       AuDbg(#w "\n"); \
6576 +       au_dpri_whlist(w); \
6577 +} while (0)
6578 +
6579 +#define AuDbgVdir(v) do { \
6580 +       AuDbg(#v "\n"); \
6581 +       au_dpri_vdir(v); \
6582 +} while (0)
6583 +
6584 +#define AuDbgInode(i) do { \
6585 +       AuDbg(#i "\n"); \
6586 +       au_dpri_inode(i); \
6587 +} while (0)
6588 +
6589 +#define AuDbgDentry(d) do { \
6590 +       AuDbg(#d "\n"); \
6591 +       au_dpri_dentry(d); \
6592 +} while (0)
6593 +
6594 +#define AuDbgFile(f) do { \
6595 +       AuDbg(#f "\n"); \
6596 +       au_dpri_file(f); \
6597 +} while (0)
6598 +
6599 +#define AuDbgSb(sb) do { \
6600 +       AuDbg(#sb "\n"); \
6601 +       au_dpri_sb(sb); \
6602 +} while (0)
6603 +
6604 +#define AuDbgSleep(sec) do { \
6605 +       AuDbg("sleep %d sec\n", sec); \
6606 +       ssleep(sec); \
6607 +} while (0)
6608 +
6609 +#define AuDbgSleepJiffy(jiffy) do { \
6610 +       AuDbg("sleep %d jiffies\n", jiffy); \
6611 +       au_dbg_sleep_jiffy(jiffy); \
6612 +} while (0)
6613 +
6614 +#define AuDbgIAttr(ia) do { \
6615 +       AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
6616 +       au_dbg_iattr(ia); \
6617 +} while (0)
6618 +#else
6619 +static inline void au_dbg_verify_dir_parent(struct dentry *dentry,
6620 +                                           unsigned int sigen)
6621 +{
6622 +       /* empty */
6623 +}
6624 +static inline void au_dbg_verify_nondir_parent(struct dentry *dentry,
6625 +                                          unsigned int sigen)
6626 +{
6627 +       /* empty */
6628 +}
6629 +static inline void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6630 +{
6631 +       /* empty */
6632 +}
6633 +static inline void au_dbg_verify_hf(struct au_finfo *finfo)
6634 +{
6635 +       /* empty */
6636 +}
6637 +static inline void au_dbg_verify_kthread(void)
6638 +{
6639 +       /* empty */
6640 +}
6641 +
6642 +static inline int au_debug_init(void)
6643 +{
6644 +       return 0;
6645 +}
6646 +static inline void au_debug_sbinfo_init(struct au_sbinfo *sbinfo)
6647 +{
6648 +       /* empty */
6649 +}
6650 +#define AuDbgWhlist(w)         do {} while (0)
6651 +#define AuDbgVdir(v)           do {} while (0)
6652 +#define AuDbgInode(i)          do {} while (0)
6653 +#define AuDbgDentry(d)         do {} while (0)
6654 +#define AuDbgFile(f)           do {} while (0)
6655 +#define AuDbgSb(sb)            do {} while (0)
6656 +#define AuDbgSleep(sec)                do {} while (0)
6657 +#define AuDbgSleepJiffy(jiffy) do {} while (0)
6658 +#define AuDbgIAttr(ia)         do {} while (0)
6659 +#endif /* CONFIG_AUFS_DEBUG */
6660 +
6661 +/* ---------------------------------------------------------------------- */
6662 +
6663 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
6664 +int __init au_sysrq_init(void);
6665 +void au_sysrq_fin(void);
6666 +
6667 +#ifdef CONFIG_HW_CONSOLE
6668 +#define au_dbg_blocked() do { \
6669 +       WARN_ON(1); \
6670 +       handle_sysrq('w', vc_cons[fg_console].d->vc_tty); \
6671 +} while (0)
6672 +#else
6673 +#define au_dbg_blocked()       do {} while (0)
6674 +#endif
6675 +
6676 +#else
6677 +static inline int au_sysrq_init(void)
6678 +{
6679 +       return 0;
6680 +}
6681 +#define au_sysrq_fin()         do {} while (0)
6682 +#define au_dbg_blocked()       do {} while (0)
6683 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
6684 +
6685 +#endif /* __KERNEL__ */
6686 +#endif /* __AUFS_DEBUG_H__ */
6687 diff --git a/fs/aufs/dentry.c b/fs/aufs/dentry.c
6688 new file mode 100644
6689 index 0000000..f36956a
6690 --- /dev/null
6691 +++ b/fs/aufs/dentry.c
6692 @@ -0,0 +1,858 @@
6693 +/*
6694 + * Copyright (C) 2005-2009 Junjiro R. Okajima
6695 + *
6696 + * This program, aufs is free software; you can redistribute it and/or modify
6697 + * it under the terms of the GNU General Public License as published by
6698 + * the Free Software Foundation; either version 2 of the License, or
6699 + * (at your option) any later version.
6700 + */
6701 +
6702 +/*
6703 + * lookup and dentry operations
6704 + */
6705 +
6706 +#include "aufs.h"
6707 +
6708 +static void au_h_nd(struct nameidata *h_nd, struct nameidata *nd)
6709 +{
6710 +       if (nd) {
6711 +               *h_nd = *nd;
6712 +
6713 +               /*
6714 +                * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
6715 +                * due to whiteout and branch permission.
6716 +                */
6717 +               h_nd->flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
6718 +                                | LOOKUP_FOLLOW);
6719 +               /* unnecessary? */
6720 +               h_nd->intent.open.file = NULL;
6721 +       } else
6722 +               memset(h_nd, 0, sizeof(*h_nd));
6723 +}
6724 +
6725 +struct au_lkup_one_args {
6726 +       struct dentry **errp;
6727 +       struct qstr *name;
6728 +       struct dentry *h_parent;
6729 +       struct au_branch *br;
6730 +       struct nameidata *nd;
6731 +};
6732 +
6733 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
6734 +                          struct au_branch *br, struct nameidata *nd)
6735 +{
6736 +       struct dentry *h_dentry;
6737 +       int err;
6738 +       struct nameidata h_nd;
6739 +
6740 +       if (au_test_fs_null_nd(h_parent->d_sb))
6741 +               return vfsub_lookup_one_len(name->name, h_parent, name->len);
6742 +
6743 +       au_h_nd(&h_nd, nd);
6744 +       h_nd.path.dentry = h_parent;
6745 +       h_nd.path.mnt = br->br_mnt;
6746 +
6747 +       err = __lookup_one_len(name->name, &h_nd.last, NULL, name->len);
6748 +       h_dentry = ERR_PTR(err);
6749 +       if (!err) {
6750 +               path_get(&h_nd.path);
6751 +               h_dentry = vfsub_lookup_hash(&h_nd);
6752 +               path_put(&h_nd.path);
6753 +       }
6754 +
6755 +       return h_dentry;
6756 +}
6757 +
6758 +static void au_call_lkup_one(void *args)
6759 +{
6760 +       struct au_lkup_one_args *a = args;
6761 +       *a->errp = au_lkup_one(a->name, a->h_parent, a->br, a->nd);
6762 +}
6763 +
6764 +#define AuLkup_ALLOW_NEG       1
6765 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
6766 +#define au_fset_lkup(flags, name)      { (flags) |= AuLkup_##name; }
6767 +#define au_fclr_lkup(flags, name)      { (flags) &= ~AuLkup_##name; }
6768 +
6769 +struct au_do_lookup_args {
6770 +       unsigned int            flags;
6771 +       mode_t                  type;
6772 +       struct nameidata        *nd;
6773 +};
6774 +
6775 +/*
6776 + * returns positive/negative dentry, NULL or an error.
6777 + * NULL means whiteout-ed or not-found.
6778 + */
6779 +static struct dentry*
6780 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
6781 +            aufs_bindex_t bindex, struct qstr *wh_name,
6782 +            struct au_do_lookup_args *args)
6783 +{
6784 +       struct dentry *h_dentry;
6785 +       struct inode *h_inode, *inode;
6786 +       struct qstr *name;
6787 +       struct au_branch *br;
6788 +       int wh_found, opq;
6789 +       unsigned char wh_able;
6790 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
6791 +
6792 +       name = &dentry->d_name;
6793 +       wh_found = 0;
6794 +       br = au_sbr(dentry->d_sb, bindex);
6795 +       wh_able = !!au_br_whable(br->br_perm);
6796 +       if (wh_able)
6797 +               wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6798 +       h_dentry = ERR_PTR(wh_found);
6799 +       if (!wh_found)
6800 +               goto real_lookup;
6801 +       if (unlikely(wh_found < 0))
6802 +               goto out;
6803 +
6804 +       /* We found a whiteout */
6805 +       /* au_set_dbend(dentry, bindex); */
6806 +       au_set_dbwh(dentry, bindex);
6807 +       if (!allow_neg)
6808 +               return NULL; /* success */
6809 +
6810 + real_lookup:
6811 +       h_dentry = au_lkup_one(name, h_parent, br, args->nd);
6812 +       if (IS_ERR(h_dentry))
6813 +               goto out;
6814 +
6815 +       h_inode = h_dentry->d_inode;
6816 +       if (!h_inode) {
6817 +               if (!allow_neg)
6818 +                       goto out_neg;
6819 +       } else if (wh_found
6820 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
6821 +               goto out_neg;
6822 +
6823 +       if (au_dbend(dentry) <= bindex)
6824 +               au_set_dbend(dentry, bindex);
6825 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
6826 +               au_set_dbstart(dentry, bindex);
6827 +       au_set_h_dptr(dentry, bindex, h_dentry);
6828 +
6829 +       inode = dentry->d_inode;
6830 +       if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
6831 +           || (inode && !S_ISDIR(inode->i_mode)))
6832 +               goto out; /* success */
6833 +
6834 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
6835 +       opq = au_diropq_test(h_dentry, br);
6836 +       mutex_unlock(&h_inode->i_mutex);
6837 +       if (opq > 0)
6838 +               au_set_dbdiropq(dentry, bindex);
6839 +       else if (unlikely(opq < 0)) {
6840 +               au_set_h_dptr(dentry, bindex, NULL);
6841 +               h_dentry = ERR_PTR(opq);
6842 +       }
6843 +       goto out;
6844 +
6845 + out_neg:
6846 +       dput(h_dentry);
6847 +       h_dentry = NULL;
6848 + out:
6849 +       return h_dentry;
6850 +}
6851 +
6852 +/*
6853 + * returns the number of lower positive dentries,
6854 + * otherwise an error.
6855 + * can be called at unlinking with @type is zero.
6856 + */
6857 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
6858 +                  struct nameidata *nd)
6859 +{
6860 +       int npositive, err;
6861 +       aufs_bindex_t bindex, btail, bdiropq;
6862 +       unsigned char isdir;
6863 +       struct qstr whname;
6864 +       struct au_do_lookup_args args = {
6865 +               .flags  = 0,
6866 +               .type   = type,
6867 +               .nd     = nd
6868 +       };
6869 +       const struct qstr *name = &dentry->d_name;
6870 +       struct dentry *parent;
6871 +       struct inode *inode;
6872 +
6873 +       err = -EPERM;
6874 +       parent = dget_parent(dentry);
6875 +       if (unlikely(!strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6876 +               goto out;
6877 +
6878 +       err = au_wh_name_alloc(&whname, name);
6879 +       if (unlikely(err))
6880 +               goto out;
6881 +
6882 +       inode = dentry->d_inode;
6883 +       isdir = !!(inode && S_ISDIR(inode->i_mode));
6884 +       if (!type)
6885 +               au_fset_lkup(args.flags, ALLOW_NEG);
6886 +
6887 +       npositive = 0;
6888 +       btail = au_dbtaildir(parent);
6889 +       for (bindex = bstart; bindex <= btail; bindex++) {
6890 +               struct dentry *h_parent, *h_dentry;
6891 +               struct inode *h_inode, *h_dir;
6892 +
6893 +               h_dentry = au_h_dptr(dentry, bindex);
6894 +               if (h_dentry) {
6895 +                       if (h_dentry->d_inode)
6896 +                               npositive++;
6897 +                       if (type != S_IFDIR)
6898 +                               break;
6899 +                       continue;
6900 +               }
6901 +               h_parent = au_h_dptr(parent, bindex);
6902 +               if (!h_parent)
6903 +                       continue;
6904 +               h_dir = h_parent->d_inode;
6905 +               if (!h_dir || !S_ISDIR(h_dir->i_mode))
6906 +                       continue;
6907 +
6908 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
6909 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
6910 +                                       &args);
6911 +               mutex_unlock(&h_dir->i_mutex);
6912 +               err = PTR_ERR(h_dentry);
6913 +               if (IS_ERR(h_dentry))
6914 +                       goto out_wh;
6915 +               au_fclr_lkup(args.flags, ALLOW_NEG);
6916 +
6917 +               if (au_dbwh(dentry) >= 0)
6918 +                       break;
6919 +               if (!h_dentry)
6920 +                       continue;
6921 +               h_inode = h_dentry->d_inode;
6922 +               if (!h_inode)
6923 +                       continue;
6924 +               npositive++;
6925 +               if (!args.type)
6926 +                       args.type = h_inode->i_mode & S_IFMT;
6927 +               if (args.type != S_IFDIR)
6928 +                       break;
6929 +               else if (isdir) {
6930 +                       /* the type of lower may be different */
6931 +                       bdiropq = au_dbdiropq(dentry);
6932 +                       if (bdiropq >= 0 && bdiropq <= bindex)
6933 +                               break;
6934 +               }
6935 +       }
6936 +
6937 +       if (npositive) {
6938 +               AuLabel(positive);
6939 +               au_update_dbstart(dentry);
6940 +       }
6941 +       err = npositive;
6942 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
6943 +                    && au_dbstart(dentry) < 0))
6944 +               /* both of real entry and whiteout found */
6945 +               err = -EIO;
6946 +
6947 + out_wh:
6948 +       kfree(whname.name);
6949 + out:
6950 +       dput(parent);
6951 +       return err;
6952 +}
6953 +
6954 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
6955 +                              struct au_branch *br)
6956 +{
6957 +       struct dentry *dentry;
6958 +       int wkq_err;
6959 +
6960 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
6961 +               dentry = au_lkup_one(name, parent, br, /*nd*/NULL);
6962 +       else {
6963 +               struct au_lkup_one_args args = {
6964 +                       .errp           = &dentry,
6965 +                       .name           = name,
6966 +                       .h_parent       = parent,
6967 +                       .br             = br,
6968 +                       .nd             = NULL
6969 +               };
6970 +
6971 +               wkq_err = au_wkq_wait(au_call_lkup_one, &args);
6972 +               if (unlikely(wkq_err))
6973 +                       dentry = ERR_PTR(wkq_err);
6974 +       }
6975 +
6976 +       return dentry;
6977 +}
6978 +
6979 +/*
6980 + * lookup @dentry on @bindex which should be negative.
6981 + */
6982 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex)
6983 +{
6984 +       int err;
6985 +       struct dentry *parent, *h_parent, *h_dentry;
6986 +       struct qstr *name;
6987 +
6988 +       name = &dentry->d_name;
6989 +       parent = dget_parent(dentry);
6990 +       h_parent = au_h_dptr(parent, bindex);
6991 +       h_dentry = au_sio_lkup_one(name, h_parent,
6992 +                                  au_sbr(dentry->d_sb, bindex));
6993 +       err = PTR_ERR(h_dentry);
6994 +       if (IS_ERR(h_dentry))
6995 +               goto out;
6996 +       if (unlikely(h_dentry->d_inode)) {
6997 +               err = -EIO;
6998 +               AuIOErr("b%d %.*s should be negative.\n",
6999 +                       bindex, AuDLNPair(h_dentry));
7000 +               dput(h_dentry);
7001 +               goto out;
7002 +       }
7003 +
7004 +       if (bindex < au_dbstart(dentry))
7005 +               au_set_dbstart(dentry, bindex);
7006 +       if (au_dbend(dentry) < bindex)
7007 +               au_set_dbend(dentry, bindex);
7008 +       au_set_h_dptr(dentry, bindex, h_dentry);
7009 +       err = 0;
7010 +
7011 + out:
7012 +       dput(parent);
7013 +       return err;
7014 +}
7015 +
7016 +/* ---------------------------------------------------------------------- */
7017 +
7018 +/* subset of struct inode */
7019 +struct au_iattr {
7020 +       unsigned long           i_ino;
7021 +       /* unsigned int         i_nlink; */
7022 +       uid_t                   i_uid;
7023 +       gid_t                   i_gid;
7024 +       u64                     i_version;
7025 +/*
7026 +       loff_t                  i_size;
7027 +       blkcnt_t                i_blocks;
7028 +*/
7029 +       umode_t                 i_mode;
7030 +};
7031 +
7032 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7033 +{
7034 +       ia->i_ino = h_inode->i_ino;
7035 +       /* ia->i_nlink = h_inode->i_nlink; */
7036 +       ia->i_uid = h_inode->i_uid;
7037 +       ia->i_gid = h_inode->i_gid;
7038 +       ia->i_version = h_inode->i_version;
7039 +/*
7040 +       ia->i_size = h_inode->i_size;
7041 +       ia->i_blocks = h_inode->i_blocks;
7042 +*/
7043 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7044 +}
7045 +
7046 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7047 +{
7048 +       return ia->i_ino != h_inode->i_ino
7049 +               /* || ia->i_nlink != h_inode->i_nlink */
7050 +               || ia->i_uid != h_inode->i_uid
7051 +               || ia->i_gid != h_inode->i_gid
7052 +               || ia->i_version != h_inode->i_version
7053 +/*
7054 +               || ia->i_size != h_inode->i_size
7055 +               || ia->i_blocks != h_inode->i_blocks
7056 +*/
7057 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7058 +}
7059 +
7060 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7061 +                             struct au_branch *br)
7062 +{
7063 +       int err;
7064 +       struct au_iattr ia;
7065 +       struct inode *h_inode;
7066 +       struct dentry *h_d;
7067 +       struct super_block *h_sb;
7068 +
7069 +       err = 0;
7070 +       memset(&ia, -1, sizeof(ia));
7071 +       h_sb = h_dentry->d_sb;
7072 +       h_inode = h_dentry->d_inode;
7073 +       if (h_inode)
7074 +               au_iattr_save(&ia, h_inode);
7075 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7076 +               /* nfs d_revalidate may return 0 for negative dentry */
7077 +               /* fuse d_revalidate always return 0 for negative dentry */
7078 +               goto out;
7079 +
7080 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7081 +       h_d = au_lkup_one(&h_dentry->d_name, h_parent, br, /*nd*/NULL);
7082 +       err = PTR_ERR(h_d);
7083 +       if (IS_ERR(h_d))
7084 +               goto out;
7085 +
7086 +       err = 0;
7087 +       if (unlikely(h_d != h_dentry
7088 +                    || h_d->d_inode != h_inode
7089 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7090 +               err = au_busy_or_stale();
7091 +       dput(h_d);
7092 +
7093 + out:
7094 +       AuTraceErr(err);
7095 +       return err;
7096 +}
7097 +
7098 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7099 +               struct dentry *h_parent, struct au_branch *br)
7100 +{
7101 +       int err;
7102 +
7103 +       err = 0;
7104 +       if (udba == AuOpt_UDBA_REVAL) {
7105 +               IMustLock(h_dir);
7106 +               err = (h_dentry->d_parent->d_inode != h_dir);
7107 +       } else if (udba == AuOpt_UDBA_HINOTIFY)
7108 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7109 +
7110 +       return err;
7111 +}
7112 +
7113 +/* ---------------------------------------------------------------------- */
7114 +
7115 +static void au_do_refresh_hdentry(struct au_hdentry *p, struct au_dinfo *dinfo,
7116 +                                 struct dentry *parent)
7117 +{
7118 +       struct dentry *h_d, *h_dp;
7119 +       struct au_hdentry tmp, *q;
7120 +       struct super_block *sb;
7121 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7122 +
7123 +       bend = dinfo->di_bend;
7124 +       bwh = dinfo->di_bwh;
7125 +       bdiropq = dinfo->di_bdiropq;
7126 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7127 +               h_d = p->hd_dentry;
7128 +               if (!h_d)
7129 +                       continue;
7130 +
7131 +               h_dp = dget_parent(h_d);
7132 +               if (h_dp == au_h_dptr(parent, bindex)) {
7133 +                       dput(h_dp);
7134 +                       continue;
7135 +               }
7136 +
7137 +               new_bindex = au_find_dbindex(parent, h_dp);
7138 +               dput(h_dp);
7139 +               if (dinfo->di_bwh == bindex)
7140 +                       bwh = new_bindex;
7141 +               if (dinfo->di_bdiropq == bindex)
7142 +                       bdiropq = new_bindex;
7143 +               if (new_bindex < 0) {
7144 +                       au_hdput(p);
7145 +                       p->hd_dentry = NULL;
7146 +                       continue;
7147 +               }
7148 +
7149 +               /* swap two lower dentries, and loop again */
7150 +               q = dinfo->di_hdentry + new_bindex;
7151 +               tmp = *q;
7152 +               *q = *p;
7153 +               *p = tmp;
7154 +               if (tmp.hd_dentry) {
7155 +                       bindex--;
7156 +                       p--;
7157 +               }
7158 +       }
7159 +
7160 +       sb = parent->d_sb;
7161 +       dinfo->di_bwh = -1;
7162 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
7163 +               dinfo->di_bwh = bwh;
7164 +
7165 +       dinfo->di_bdiropq = -1;
7166 +       if (bdiropq >= 0
7167 +           && bdiropq <= au_sbend(sb)
7168 +           && au_sbr_whable(sb, bdiropq))
7169 +               dinfo->di_bdiropq = bdiropq;
7170 +
7171 +       bend = au_dbend(parent);
7172 +       p = dinfo->di_hdentry;
7173 +       for (bindex = 0; bindex <= bend; bindex++, p++)
7174 +               if (p->hd_dentry) {
7175 +                       dinfo->di_bstart = bindex;
7176 +                       break;
7177 +               }
7178 +
7179 +       p = dinfo->di_hdentry + bend;
7180 +       for (bindex = bend; bindex >= 0; bindex--, p--)
7181 +               if (p->hd_dentry) {
7182 +                       dinfo->di_bend = bindex;
7183 +                       break;
7184 +               }
7185 +}
7186 +
7187 +/*
7188 + * returns the number of found lower positive dentries,
7189 + * otherwise an error.
7190 + */
7191 +int au_refresh_hdentry(struct dentry *dentry, mode_t type)
7192 +{
7193 +       int npositive, err;
7194 +       unsigned int sigen;
7195 +       aufs_bindex_t bstart;
7196 +       struct au_dinfo *dinfo;
7197 +       struct super_block *sb;
7198 +       struct dentry *parent;
7199 +
7200 +       sb = dentry->d_sb;
7201 +       AuDebugOn(IS_ROOT(dentry));
7202 +       sigen = au_sigen(sb);
7203 +       parent = dget_parent(dentry);
7204 +       AuDebugOn(au_digen(parent) != sigen
7205 +                 || au_iigen(parent->d_inode) != sigen);
7206 +
7207 +       dinfo = au_di(dentry);
7208 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
7209 +       npositive = err;
7210 +       if (unlikely(err))
7211 +               goto out;
7212 +       au_do_refresh_hdentry(dinfo->di_hdentry + dinfo->di_bstart, dinfo,
7213 +                             parent);
7214 +
7215 +       npositive = 0;
7216 +       bstart = au_dbstart(parent);
7217 +       if (type != S_IFDIR && dinfo->di_bstart == bstart)
7218 +               goto out_dgen; /* success */
7219 +
7220 +       npositive = au_lkup_dentry(dentry, bstart, type, /*nd*/NULL);
7221 +       if (npositive < 0)
7222 +               goto out;
7223 +       if (dinfo->di_bwh >= 0 && dinfo->di_bwh <= dinfo->di_bstart)
7224 +               d_drop(dentry);
7225 +
7226 + out_dgen:
7227 +       au_update_digen(dentry);
7228 + out:
7229 +       dput(parent);
7230 +       AuTraceErr(npositive);
7231 +       return npositive;
7232 +}
7233 +
7234 +static noinline_for_stack
7235 +int au_do_h_d_reval(struct dentry *h_dentry, struct nameidata *nd,
7236 +                   struct dentry *dentry, aufs_bindex_t bindex)
7237 +{
7238 +       int err, valid;
7239 +       int (*reval)(struct dentry *, struct nameidata *);
7240 +
7241 +       err = 0;
7242 +       reval = NULL;
7243 +       if (h_dentry->d_op)
7244 +               reval = h_dentry->d_op->d_revalidate;
7245 +       if (!reval)
7246 +               goto out;
7247 +
7248 +       AuDbg("b%d\n", bindex);
7249 +       if (au_test_fs_null_nd(h_dentry->d_sb))
7250 +               /* it may return tri-state */
7251 +               valid = reval(h_dentry, NULL);
7252 +       else {
7253 +               struct nameidata h_nd;
7254 +               int locked;
7255 +               struct dentry *parent;
7256 +
7257 +               au_h_nd(&h_nd, nd);
7258 +               parent = nd->path.dentry;
7259 +               locked = (nd && nd->path.dentry != dentry);
7260 +               if (locked)
7261 +                       di_read_lock_parent(parent, AuLock_IR);
7262 +               BUG_ON(bindex > au_dbend(parent));
7263 +               h_nd.path.dentry = au_h_dptr(parent, bindex);
7264 +               BUG_ON(!h_nd.path.dentry);
7265 +               h_nd.path.mnt = au_sbr(parent->d_sb, bindex)->br_mnt;
7266 +               path_get(&h_nd.path);
7267 +               valid = reval(h_dentry, &h_nd);
7268 +               path_put(&h_nd.path);
7269 +               if (locked)
7270 +                       di_read_unlock(parent, AuLock_IR);
7271 +       }
7272 +
7273 +       if (unlikely(valid < 0))
7274 +               err = valid;
7275 +       else if (!valid)
7276 +               err = -EINVAL;
7277 +
7278 + out:
7279 +       AuTraceErr(err);
7280 +       return err;
7281 +}
7282 +
7283 +/* todo: remove this */
7284 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
7285 +                         struct nameidata *nd, int do_udba)
7286 +{
7287 +       int err;
7288 +       umode_t mode, h_mode;
7289 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
7290 +       unsigned char plus, unhashed, is_root, h_plus;
7291 +       struct inode *first, *h_inode, *h_cached_inode;
7292 +       struct dentry *h_dentry;
7293 +       struct qstr *name, *h_name;
7294 +
7295 +       err = 0;
7296 +       plus = 0;
7297 +       mode = 0;
7298 +       first = NULL;
7299 +       ibs = -1;
7300 +       ibe = -1;
7301 +       unhashed = !!d_unhashed(dentry);
7302 +       is_root = !!IS_ROOT(dentry);
7303 +       name = &dentry->d_name;
7304 +
7305 +       /*
7306 +        * Theoretically, REVAL test should be unnecessary in case of INOTIFY.
7307 +        * But inotify doesn't fire some necessary events,
7308 +        *      IN_ATTRIB for atime/nlink/pageio
7309 +        *      IN_DELETE for NFS dentry
7310 +        * Let's do REVAL test too.
7311 +        */
7312 +       if (do_udba && inode) {
7313 +               mode = (inode->i_mode & S_IFMT);
7314 +               plus = (inode->i_nlink > 0);
7315 +               first = au_h_iptr(inode, au_ibstart(inode));
7316 +               ibs = au_ibstart(inode);
7317 +               ibe = au_ibend(inode);
7318 +       }
7319 +
7320 +       bstart = au_dbstart(dentry);
7321 +       btail = bstart;
7322 +       if (inode && S_ISDIR(inode->i_mode))
7323 +               btail = au_dbtaildir(dentry);
7324 +       for (bindex = bstart; bindex <= btail; bindex++) {
7325 +               h_dentry = au_h_dptr(dentry, bindex);
7326 +               if (!h_dentry)
7327 +                       continue;
7328 +
7329 +               AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
7330 +               h_name = &h_dentry->d_name;
7331 +               if (unlikely(do_udba
7332 +                            && !is_root
7333 +                            && (unhashed != !!d_unhashed(h_dentry)
7334 +                                || name->len != h_name->len
7335 +                                || memcmp(name->name, h_name->name, name->len))
7336 +                           )) {
7337 +                       AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
7338 +                                 unhashed, d_unhashed(h_dentry),
7339 +                                 AuDLNPair(dentry), AuDLNPair(h_dentry));
7340 +                       goto err;
7341 +               }
7342 +
7343 +               err = au_do_h_d_reval(h_dentry, nd, dentry, bindex);
7344 +               if (unlikely(err))
7345 +                       /* do not goto err, to keep the errno */
7346 +                       break;
7347 +
7348 +               /* todo: plink too? */
7349 +               if (!do_udba)
7350 +                       continue;
7351 +
7352 +               /* UDBA tests */
7353 +               h_inode = h_dentry->d_inode;
7354 +               if (unlikely(!!inode != !!h_inode))
7355 +                       goto err;
7356 +
7357 +               h_plus = plus;
7358 +               h_mode = mode;
7359 +               h_cached_inode = h_inode;
7360 +               if (h_inode) {
7361 +                       h_mode = (h_inode->i_mode & S_IFMT);
7362 +                       h_plus = (h_inode->i_nlink > 0);
7363 +               }
7364 +               if (inode && ibs <= bindex && bindex <= ibe)
7365 +                       h_cached_inode = au_h_iptr(inode, bindex);
7366 +
7367 +               if (unlikely(plus != h_plus
7368 +                            || mode != h_mode
7369 +                            || h_cached_inode != h_inode))
7370 +                       goto err;
7371 +               continue;
7372 +
7373 +       err:
7374 +               err = -EINVAL;
7375 +               break;
7376 +       }
7377 +
7378 +       return err;
7379 +}
7380 +
7381 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
7382 +{
7383 +       int err;
7384 +       struct dentry *parent;
7385 +       struct inode *inode;
7386 +
7387 +       inode = dentry->d_inode;
7388 +       if (au_digen(dentry) == sigen && au_iigen(inode) == sigen)
7389 +               return 0;
7390 +
7391 +       parent = dget_parent(dentry);
7392 +       di_read_lock_parent(parent, AuLock_IR);
7393 +       AuDebugOn(au_digen(parent) != sigen
7394 +                 || au_iigen(parent->d_inode) != sigen);
7395 +       au_dbg_verify_gen(parent, sigen);
7396 +
7397 +       /* returns a number of positive dentries */
7398 +       err = au_refresh_hdentry(dentry, inode->i_mode & S_IFMT);
7399 +       if (err >= 0)
7400 +               err = au_refresh_hinode(inode, dentry);
7401 +
7402 +       di_read_unlock(parent, AuLock_IR);
7403 +       dput(parent);
7404 +       return err;
7405 +}
7406 +
7407 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
7408 +{
7409 +       int err;
7410 +       struct dentry *d, *parent;
7411 +       struct inode *inode;
7412 +
7413 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIRS))
7414 +               return simple_reval_dpath(dentry, sigen);
7415 +
7416 +       /* slow loop, keep it simple and stupid */
7417 +       /* cf: au_cpup_dirs() */
7418 +       err = 0;
7419 +       parent = NULL;
7420 +       while (au_digen(dentry) != sigen
7421 +              || au_iigen(dentry->d_inode) != sigen) {
7422 +               d = dentry;
7423 +               while (1) {
7424 +                       dput(parent);
7425 +                       parent = dget_parent(d);
7426 +                       if (au_digen(parent) == sigen
7427 +                           && au_iigen(parent->d_inode) == sigen)
7428 +                               break;
7429 +                       d = parent;
7430 +               }
7431 +
7432 +               inode = d->d_inode;
7433 +               if (d != dentry)
7434 +                       di_write_lock_child(d);
7435 +
7436 +               /* someone might update our dentry while we were sleeping */
7437 +               if (au_digen(d) != sigen || au_iigen(d->d_inode) != sigen) {
7438 +                       di_read_lock_parent(parent, AuLock_IR);
7439 +                       /* returns a number of positive dentries */
7440 +                       err = au_refresh_hdentry(d, inode->i_mode & S_IFMT);
7441 +                       if (err >= 0)
7442 +                               err = au_refresh_hinode(inode, d);
7443 +                       di_read_unlock(parent, AuLock_IR);
7444 +               }
7445 +
7446 +               if (d != dentry)
7447 +                       di_write_unlock(d);
7448 +               dput(parent);
7449 +               if (unlikely(err))
7450 +                       break;
7451 +       }
7452 +
7453 +       return err;
7454 +}
7455 +
7456 +/*
7457 + * if valid returns 1, otherwise 0.
7458 + */
7459 +static int aufs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
7460 +{
7461 +       int valid, err;
7462 +       unsigned int sigen;
7463 +       unsigned char do_udba;
7464 +       struct super_block *sb;
7465 +       struct inode *inode;
7466 +
7467 +       err = -EINVAL;
7468 +       sb = dentry->d_sb;
7469 +       inode = dentry->d_inode;
7470 +       aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW);
7471 +       sigen = au_sigen(sb);
7472 +       if (au_digen(dentry) != sigen) {
7473 +               AuDebugOn(IS_ROOT(dentry));
7474 +               if (inode)
7475 +                       err = au_reval_dpath(dentry, sigen);
7476 +               if (unlikely(err))
7477 +                       goto out_dgrade;
7478 +               AuDebugOn(au_digen(dentry) != sigen);
7479 +       }
7480 +       if (inode && au_iigen(inode) != sigen) {
7481 +               AuDebugOn(IS_ROOT(dentry));
7482 +               err = au_refresh_hinode(inode, dentry);
7483 +               if (unlikely(err))
7484 +                       goto out_dgrade;
7485 +               AuDebugOn(au_iigen(inode) != sigen);
7486 +       }
7487 +       di_downgrade_lock(dentry, AuLock_IR);
7488 +
7489 +       AuDebugOn(au_digen(dentry) != sigen);
7490 +       AuDebugOn(inode && au_iigen(inode) != sigen);
7491 +       err = -EINVAL;
7492 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7493 +       if (do_udba && inode) {
7494 +               aufs_bindex_t bstart = au_ibstart(inode);
7495 +
7496 +               if (bstart >= 0
7497 +                   && au_test_higen(inode, au_h_iptr(inode, bstart)))
7498 +                       goto out;
7499 +       }
7500 +
7501 +       err = h_d_revalidate(dentry, inode, nd, do_udba);
7502 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0))
7503 +               /* both of real entry and whiteout found */
7504 +               err = -EIO;
7505 +       goto out;
7506 +
7507 + out_dgrade:
7508 +       di_downgrade_lock(dentry, AuLock_IR);
7509 + out:
7510 +       au_store_oflag(nd, inode);
7511 +       aufs_read_unlock(dentry, AuLock_IR);
7512 +       AuTraceErr(err);
7513 +       valid = !err;
7514 +       if (!valid)
7515 +               AuDbg("%.*s invalid\n", AuDLNPair(dentry));
7516 +       return valid;
7517 +}
7518 +
7519 +static void aufs_d_release(struct dentry *dentry)
7520 +{
7521 +       struct au_dinfo *dinfo;
7522 +       aufs_bindex_t bend, bindex;
7523 +
7524 +       dinfo = dentry->d_fsdata;
7525 +       if (!dinfo)
7526 +               return;
7527 +
7528 +       /* dentry may not be revalidated */
7529 +       bindex = dinfo->di_bstart;
7530 +       if (bindex >= 0) {
7531 +               struct au_hdentry *p;
7532 +
7533 +               bend = dinfo->di_bend;
7534 +               p = dinfo->di_hdentry + bindex;
7535 +               while (bindex++ <= bend) {
7536 +                       if (p->hd_dentry)
7537 +                               au_hdput(p);
7538 +                       p++;
7539 +               }
7540 +       }
7541 +       kfree(dinfo->di_hdentry);
7542 +       au_rwsem_destroy(&dinfo->di_rwsem);
7543 +       au_cache_free_dinfo(dinfo);
7544 +       au_hin_di_reinit(dentry);
7545 +}
7546 +
7547 +struct dentry_operations aufs_dop = {
7548 +       .d_revalidate   = aufs_d_revalidate,
7549 +       .d_release      = aufs_d_release
7550 +};
7551 diff --git a/fs/aufs/dentry.h b/fs/aufs/dentry.h
7552 new file mode 100644
7553 index 0000000..8708db8
7554 --- /dev/null
7555 +++ b/fs/aufs/dentry.h
7556 @@ -0,0 +1,213 @@
7557 +/*
7558 + * Copyright (C) 2005-2009 Junjiro R. Okajima
7559 + *
7560 + * This program, aufs is free software; you can redistribute it and/or modify
7561 + * it under the terms of the GNU General Public License as published by
7562 + * the Free Software Foundation; either version 2 of the License, or
7563 + * (at your option) any later version.
7564 + */
7565 +
7566 +/*
7567 + * lookup and dentry operations
7568 + */
7569 +
7570 +#ifndef __AUFS_DENTRY_H__
7571 +#define __AUFS_DENTRY_H__
7572 +
7573 +#ifdef __KERNEL__
7574 +
7575 +#include <linux/fs.h>
7576 +#include <linux/namei.h>
7577 +#include <linux/aufs_type.h>
7578 +#include "rwsem.h"
7579 +
7580 +/* make a single member structure for future use */
7581 +/* todo: remove this structure */
7582 +struct au_hdentry {
7583 +       struct dentry           *hd_dentry;
7584 +};
7585 +
7586 +struct au_dinfo {
7587 +       atomic_t                di_generation;
7588 +
7589 +       struct rw_semaphore     di_rwsem;
7590 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
7591 +       struct au_hdentry       *di_hdentry;
7592 +};
7593 +
7594 +/* ---------------------------------------------------------------------- */
7595 +
7596 +/* dentry.c */
7597 +extern struct dentry_operations aufs_dop;
7598 +struct au_branch;
7599 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
7600 +                          struct au_branch *br, struct nameidata *nd);
7601 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
7602 +                              struct au_branch *br);
7603 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7604 +               struct dentry *h_parent, struct au_branch *br);
7605 +
7606 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
7607 +                  struct nameidata *nd);
7608 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex);
7609 +int au_refresh_hdentry(struct dentry *dentry, mode_t type);
7610 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
7611 +
7612 +/* dinfo.c */
7613 +int au_alloc_dinfo(struct dentry *dentry);
7614 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
7615 +
7616 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
7617 +void di_read_unlock(struct dentry *d, int flags);
7618 +void di_downgrade_lock(struct dentry *d, int flags);
7619 +void di_write_lock(struct dentry *d, unsigned int lsc);
7620 +void di_write_unlock(struct dentry *d);
7621 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
7622 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
7623 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
7624 +
7625 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
7626 +aufs_bindex_t au_dbtail(struct dentry *dentry);
7627 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
7628 +
7629 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7630 +                  struct dentry *h_dentry);
7631 +void au_update_digen(struct dentry *dentry);
7632 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
7633 +void au_update_dbstart(struct dentry *dentry);
7634 +void au_update_dbend(struct dentry *dentry);
7635 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
7636 +
7637 +/* ---------------------------------------------------------------------- */
7638 +
7639 +static inline struct au_dinfo *au_di(struct dentry *dentry)
7640 +{
7641 +       return dentry->d_fsdata;
7642 +}
7643 +
7644 +/* ---------------------------------------------------------------------- */
7645 +
7646 +/* lock subclass for dinfo */
7647 +enum {
7648 +       AuLsc_DI_CHILD,         /* child first */
7649 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hinotify */
7650 +       AuLsc_DI_CHILD3,        /* copyup dirs */
7651 +       AuLsc_DI_PARENT,
7652 +       AuLsc_DI_PARENT2,
7653 +       AuLsc_DI_PARENT3
7654 +};
7655 +
7656 +/*
7657 + * di_read_lock_child, di_write_lock_child,
7658 + * di_read_lock_child2, di_write_lock_child2,
7659 + * di_read_lock_child3, di_write_lock_child3,
7660 + * di_read_lock_parent, di_write_lock_parent,
7661 + * di_read_lock_parent2, di_write_lock_parent2,
7662 + * di_read_lock_parent3, di_write_lock_parent3,
7663 + */
7664 +#define AuReadLockFunc(name, lsc) \
7665 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
7666 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
7667 +
7668 +#define AuWriteLockFunc(name, lsc) \
7669 +static inline void di_write_lock_##name(struct dentry *d) \
7670 +{ di_write_lock(d, AuLsc_DI_##lsc); }
7671 +
7672 +#define AuRWLockFuncs(name, lsc) \
7673 +       AuReadLockFunc(name, lsc) \
7674 +       AuWriteLockFunc(name, lsc)
7675 +
7676 +AuRWLockFuncs(child, CHILD);
7677 +AuRWLockFuncs(child2, CHILD2);
7678 +AuRWLockFuncs(child3, CHILD3);
7679 +AuRWLockFuncs(parent, PARENT);
7680 +AuRWLockFuncs(parent2, PARENT2);
7681 +AuRWLockFuncs(parent3, PARENT3);
7682 +
7683 +#undef AuReadLockFunc
7684 +#undef AuWriteLockFunc
7685 +#undef AuRWLockFuncs
7686 +
7687 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
7688 +
7689 +/* ---------------------------------------------------------------------- */
7690 +
7691 +/* todo: memory barrier? */
7692 +static inline unsigned int au_digen(struct dentry *d)
7693 +{
7694 +       return atomic_read(&au_di(d)->di_generation);
7695 +}
7696 +
7697 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
7698 +{
7699 +       hdentry->hd_dentry = NULL;
7700 +}
7701 +
7702 +static inline void au_hdput(struct au_hdentry *hd)
7703 +{
7704 +       dput(hd->hd_dentry);
7705 +}
7706 +
7707 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
7708 +{
7709 +       return au_di(dentry)->di_bstart;
7710 +}
7711 +
7712 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
7713 +{
7714 +       return au_di(dentry)->di_bend;
7715 +}
7716 +
7717 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
7718 +{
7719 +       return au_di(dentry)->di_bwh;
7720 +}
7721 +
7722 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
7723 +{
7724 +       return au_di(dentry)->di_bdiropq;
7725 +}
7726 +
7727 +/* todo: hard/soft set? */
7728 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
7729 +{
7730 +       au_di(dentry)->di_bstart = bindex;
7731 +}
7732 +
7733 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
7734 +{
7735 +       au_di(dentry)->di_bend = bindex;
7736 +}
7737 +
7738 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
7739 +{
7740 +       /* dbwh can be outside of bstart - bend range */
7741 +       au_di(dentry)->di_bwh = bindex;
7742 +}
7743 +
7744 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
7745 +{
7746 +       au_di(dentry)->di_bdiropq = bindex;
7747 +}
7748 +
7749 +/* ---------------------------------------------------------------------- */
7750 +
7751 +#ifdef CONFIG_AUFS_HINOTIFY
7752 +static inline void au_digen_dec(struct dentry *d)
7753 +{
7754 +       atomic_dec(&au_di(d)->di_generation);
7755 +}
7756 +
7757 +static inline void au_hin_di_reinit(struct dentry *dentry)
7758 +{
7759 +       dentry->d_fsdata = NULL;
7760 +}
7761 +#else
7762 +static inline void au_hin_di_reinit(struct dentry *dentry __maybe_unused)
7763 +{
7764 +       /* empty */
7765 +}
7766 +#endif /* CONFIG_AUFS_HINOTIFY */
7767 +
7768 +#endif /* __KERNEL__ */
7769 +#endif /* __AUFS_DENTRY_H__ */
7770 diff --git a/fs/aufs/dinfo.c b/fs/aufs/dinfo.c
7771 new file mode 100644
7772 index 0000000..b0645aa
7773 --- /dev/null
7774 +++ b/fs/aufs/dinfo.c
7775 @@ -0,0 +1,351 @@
7776 +/*
7777 + * Copyright (C) 2005-2009 Junjiro R. Okajima
7778 + *
7779 + * This program, aufs is free software; you can redistribute it and/or modify
7780 + * it under the terms of the GNU General Public License as published by
7781 + * the Free Software Foundation; either version 2 of the License, or
7782 + * (at your option) any later version.
7783 + */
7784 +
7785 +/*
7786 + * dentry private data
7787 + */
7788 +
7789 +#include "aufs.h"
7790 +
7791 +int au_alloc_dinfo(struct dentry *dentry)
7792 +{
7793 +       struct au_dinfo *dinfo;
7794 +       struct super_block *sb;
7795 +       int nbr;
7796 +
7797 +       dinfo = au_cache_alloc_dinfo();
7798 +       if (unlikely(!dinfo))
7799 +               goto out;
7800 +
7801 +       sb = dentry->d_sb;
7802 +       nbr = au_sbend(sb) + 1;
7803 +       if (nbr <= 0)
7804 +               nbr = 1;
7805 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
7806 +       if (unlikely(!dinfo->di_hdentry))
7807 +               goto out_dinfo;
7808 +
7809 +       atomic_set(&dinfo->di_generation, au_sigen(sb));
7810 +       /* smp_mb(); */ /* atomic_set */
7811 +       init_rwsem(&dinfo->di_rwsem);
7812 +       down_write_nested(&dinfo->di_rwsem, AuLsc_DI_CHILD);
7813 +       dinfo->di_bstart = -1;
7814 +       dinfo->di_bend = -1;
7815 +       dinfo->di_bwh = -1;
7816 +       dinfo->di_bdiropq = -1;
7817 +
7818 +       dentry->d_fsdata = dinfo;
7819 +       dentry->d_op = &aufs_dop;
7820 +       return 0; /* success */
7821 +
7822 + out_dinfo:
7823 +       au_cache_free_dinfo(dinfo);
7824 + out:
7825 +       return -ENOMEM;
7826 +}
7827 +
7828 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
7829 +{
7830 +       int err, sz;
7831 +       struct au_hdentry *hdp;
7832 +
7833 +       err = -ENOMEM;
7834 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
7835 +       if (!sz)
7836 +               sz = sizeof(*hdp);
7837 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
7838 +       if (hdp) {
7839 +               dinfo->di_hdentry = hdp;
7840 +               err = 0;
7841 +       }
7842 +
7843 +       return err;
7844 +}
7845 +
7846 +/* ---------------------------------------------------------------------- */
7847 +
7848 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
7849 +{
7850 +       switch (lsc) {
7851 +       case AuLsc_DI_CHILD:
7852 +               ii_write_lock_child(inode);
7853 +               break;
7854 +       case AuLsc_DI_CHILD2:
7855 +               ii_write_lock_child2(inode);
7856 +               break;
7857 +       case AuLsc_DI_CHILD3:
7858 +               ii_write_lock_child3(inode);
7859 +               break;
7860 +       case AuLsc_DI_PARENT:
7861 +               ii_write_lock_parent(inode);
7862 +               break;
7863 +       case AuLsc_DI_PARENT2:
7864 +               ii_write_lock_parent2(inode);
7865 +               break;
7866 +       case AuLsc_DI_PARENT3:
7867 +               ii_write_lock_parent3(inode);
7868 +               break;
7869 +       default:
7870 +               BUG();
7871 +       }
7872 +}
7873 +
7874 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
7875 +{
7876 +       switch (lsc) {
7877 +       case AuLsc_DI_CHILD:
7878 +               ii_read_lock_child(inode);
7879 +               break;
7880 +       case AuLsc_DI_CHILD2:
7881 +               ii_read_lock_child2(inode);
7882 +               break;
7883 +       case AuLsc_DI_CHILD3:
7884 +               ii_read_lock_child3(inode);
7885 +               break;
7886 +       case AuLsc_DI_PARENT:
7887 +               ii_read_lock_parent(inode);
7888 +               break;
7889 +       case AuLsc_DI_PARENT2:
7890 +               ii_read_lock_parent2(inode);
7891 +               break;
7892 +       case AuLsc_DI_PARENT3:
7893 +               ii_read_lock_parent3(inode);
7894 +               break;
7895 +       default:
7896 +               BUG();
7897 +       }
7898 +}
7899 +
7900 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
7901 +{
7902 +       down_read_nested(&au_di(d)->di_rwsem, lsc);
7903 +       if (d->d_inode) {
7904 +               if (au_ftest_lock(flags, IW))
7905 +                       do_ii_write_lock(d->d_inode, lsc);
7906 +               else if (au_ftest_lock(flags, IR))
7907 +                       do_ii_read_lock(d->d_inode, lsc);
7908 +       }
7909 +}
7910 +
7911 +void di_read_unlock(struct dentry *d, int flags)
7912 +{
7913 +       if (d->d_inode) {
7914 +               if (au_ftest_lock(flags, IW))
7915 +                       ii_write_unlock(d->d_inode);
7916 +               else if (au_ftest_lock(flags, IR))
7917 +                       ii_read_unlock(d->d_inode);
7918 +       }
7919 +       up_read(&au_di(d)->di_rwsem);
7920 +}
7921 +
7922 +void di_downgrade_lock(struct dentry *d, int flags)
7923 +{
7924 +       downgrade_write(&au_di(d)->di_rwsem);
7925 +       if (d->d_inode && au_ftest_lock(flags, IR))
7926 +               ii_downgrade_lock(d->d_inode);
7927 +}
7928 +
7929 +void di_write_lock(struct dentry *d, unsigned int lsc)
7930 +{
7931 +       down_write_nested(&au_di(d)->di_rwsem, lsc);
7932 +       if (d->d_inode)
7933 +               do_ii_write_lock(d->d_inode, lsc);
7934 +}
7935 +
7936 +void di_write_unlock(struct dentry *d)
7937 +{
7938 +       if (d->d_inode)
7939 +               ii_write_unlock(d->d_inode);
7940 +       up_write(&au_di(d)->di_rwsem);
7941 +}
7942 +
7943 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
7944 +{
7945 +       AuDebugOn(d1 == d2
7946 +                 || d1->d_inode == d2->d_inode
7947 +                 || d1->d_sb != d2->d_sb);
7948 +
7949 +       if (isdir && au_test_subdir(d1, d2)) {
7950 +               di_write_lock_child(d1);
7951 +               di_write_lock_child2(d2);
7952 +       } else {
7953 +               /* there should be no races */
7954 +               di_write_lock_child(d2);
7955 +               di_write_lock_child2(d1);
7956 +       }
7957 +}
7958 +
7959 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
7960 +{
7961 +       AuDebugOn(d1 == d2
7962 +                 || d1->d_inode == d2->d_inode
7963 +                 || d1->d_sb != d2->d_sb);
7964 +
7965 +       if (isdir && au_test_subdir(d1, d2)) {
7966 +               di_write_lock_parent(d1);
7967 +               di_write_lock_parent2(d2);
7968 +       } else {
7969 +               /* there should be no races */
7970 +               di_write_lock_parent(d2);
7971 +               di_write_lock_parent2(d1);
7972 +       }
7973 +}
7974 +
7975 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
7976 +{
7977 +       di_write_unlock(d1);
7978 +       if (d1->d_inode == d2->d_inode)
7979 +               up_write(&au_di(d2)->di_rwsem);
7980 +       else
7981 +               di_write_unlock(d2);
7982 +}
7983 +
7984 +/* ---------------------------------------------------------------------- */
7985 +
7986 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
7987 +{
7988 +       struct dentry *d;
7989 +
7990 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7991 +               return NULL;
7992 +       AuDebugOn(bindex < 0);
7993 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
7994 +       AuDebugOn(d && (atomic_read(&d->d_count) <= 0));
7995 +       return d;
7996 +}
7997 +
7998 +aufs_bindex_t au_dbtail(struct dentry *dentry)
7999 +{
8000 +       aufs_bindex_t bend, bwh;
8001 +
8002 +       bend = au_dbend(dentry);
8003 +       if (0 <= bend) {
8004 +               bwh = au_dbwh(dentry);
8005 +               if (!bwh)
8006 +                       return bwh;
8007 +               if (0 < bwh && bwh < bend)
8008 +                       return bwh - 1;
8009 +       }
8010 +       return bend;
8011 +}
8012 +
8013 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
8014 +{
8015 +       aufs_bindex_t bend, bopq;
8016 +
8017 +       bend = au_dbtail(dentry);
8018 +       if (0 <= bend) {
8019 +               bopq = au_dbdiropq(dentry);
8020 +               if (0 <= bopq && bopq < bend)
8021 +                       bend = bopq;
8022 +       }
8023 +       return bend;
8024 +}
8025 +
8026 +/* ---------------------------------------------------------------------- */
8027 +
8028 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8029 +                  struct dentry *h_dentry)
8030 +{
8031 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
8032 +
8033 +       if (hd->hd_dentry)
8034 +               au_hdput(hd);
8035 +       hd->hd_dentry = h_dentry;
8036 +}
8037 +
8038 +void au_update_digen(struct dentry *dentry)
8039 +{
8040 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
8041 +       /* smp_mb(); */ /* atomic_set */
8042 +}
8043 +
8044 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
8045 +{
8046 +       struct au_dinfo *dinfo;
8047 +       struct dentry *h_d;
8048 +
8049 +       dinfo = au_di(dentry);
8050 +       if (!dinfo || dinfo->di_bstart < 0)
8051 +               return;
8052 +
8053 +       if (do_put_zero) {
8054 +               aufs_bindex_t bindex, bend;
8055 +
8056 +               bend = dinfo->di_bend;
8057 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
8058 +                       h_d = dinfo->di_hdentry[0 + bindex].hd_dentry;
8059 +                       if (h_d && !h_d->d_inode)
8060 +                               au_set_h_dptr(dentry, bindex, NULL);
8061 +               }
8062 +       }
8063 +
8064 +       dinfo->di_bstart = -1;
8065 +       while (++dinfo->di_bstart <= dinfo->di_bend)
8066 +               if (dinfo->di_hdentry[0 + dinfo->di_bstart].hd_dentry)
8067 +                       break;
8068 +       if (dinfo->di_bstart > dinfo->di_bend) {
8069 +               dinfo->di_bstart = -1;
8070 +               dinfo->di_bend = -1;
8071 +               return;
8072 +       }
8073 +
8074 +       dinfo->di_bend++;
8075 +       while (0 <= --dinfo->di_bend)
8076 +               if (dinfo->di_hdentry[0 + dinfo->di_bend].hd_dentry)
8077 +                       break;
8078 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
8079 +}
8080 +
8081 +void au_update_dbstart(struct dentry *dentry)
8082 +{
8083 +       aufs_bindex_t bindex, bend;
8084 +       struct dentry *h_dentry;
8085 +
8086 +       bend = au_dbend(dentry);
8087 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
8088 +               h_dentry = au_h_dptr(dentry, bindex);
8089 +               if (!h_dentry)
8090 +                       continue;
8091 +               if (h_dentry->d_inode) {
8092 +                       au_set_dbstart(dentry, bindex);
8093 +                       return;
8094 +               }
8095 +               au_set_h_dptr(dentry, bindex, NULL);
8096 +       }
8097 +}
8098 +
8099 +void au_update_dbend(struct dentry *dentry)
8100 +{
8101 +       aufs_bindex_t bindex, bstart;
8102 +       struct dentry *h_dentry;
8103 +
8104 +       bstart = au_dbstart(dentry);
8105 +       for (bindex = au_dbend(dentry); bindex <= bstart; bindex--) {
8106 +               h_dentry = au_h_dptr(dentry, bindex);
8107 +               if (!h_dentry)
8108 +                       continue;
8109 +               if (h_dentry->d_inode) {
8110 +                       au_set_dbend(dentry, bindex);
8111 +                       return;
8112 +               }
8113 +               au_set_h_dptr(dentry, bindex, NULL);
8114 +       }
8115 +}
8116 +
8117 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
8118 +{
8119 +       aufs_bindex_t bindex, bend;
8120 +
8121 +       bend = au_dbend(dentry);
8122 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
8123 +               if (au_h_dptr(dentry, bindex) == h_dentry)
8124 +                       return bindex;
8125 +       return -1;
8126 +}
8127 diff --git a/fs/aufs/dir.c b/fs/aufs/dir.c
8128 new file mode 100644
8129 index 0000000..9fc6196
8130 --- /dev/null
8131 +++ b/fs/aufs/dir.c
8132 @@ -0,0 +1,513 @@
8133 +/*
8134 + * Copyright (C) 2005-2009 Junjiro R. Okajima
8135 + *
8136 + * This program, aufs is free software; you can redistribute it and/or modify
8137 + * it under the terms of the GNU General Public License as published by
8138 + * the Free Software Foundation; either version 2 of the License, or
8139 + * (at your option) any later version.
8140 + */
8141 +
8142 +/*
8143 + * directory operations
8144 + */
8145 +
8146 +#include <linux/fs_stack.h>
8147 +#include "aufs.h"
8148 +
8149 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
8150 +{
8151 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8152 +
8153 +       dir->i_nlink += h_dir->i_nlink - 2;
8154 +       if (h_dir->i_nlink < 2)
8155 +               dir->i_nlink += 2;
8156 +}
8157 +
8158 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
8159 +{
8160 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8161 +
8162 +       dir->i_nlink -= h_dir->i_nlink - 2;
8163 +       if (h_dir->i_nlink < 2)
8164 +               dir->i_nlink -= 2;
8165 +}
8166 +
8167 +/* ---------------------------------------------------------------------- */
8168 +
8169 +static int reopen_dir(struct file *file)
8170 +{
8171 +       int err;
8172 +       unsigned int flags;
8173 +       aufs_bindex_t bindex, btail, bstart;
8174 +       struct dentry *dentry, *h_dentry;
8175 +       struct file *h_file;
8176 +
8177 +       /* open all lower dirs */
8178 +       dentry = file->f_dentry;
8179 +       bstart = au_dbstart(dentry);
8180 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
8181 +               au_set_h_fptr(file, bindex, NULL);
8182 +       au_set_fbstart(file, bstart);
8183 +
8184 +       btail = au_dbtaildir(dentry);
8185 +       for (bindex = au_fbend(file); btail < bindex; bindex--)
8186 +               au_set_h_fptr(file, bindex, NULL);
8187 +       au_set_fbend(file, btail);
8188 +
8189 +       flags = file->f_flags;
8190 +       for (bindex = bstart; bindex <= btail; bindex++) {
8191 +               h_dentry = au_h_dptr(dentry, bindex);
8192 +               if (!h_dentry)
8193 +                       continue;
8194 +               h_file = au_h_fptr(file, bindex);
8195 +               if (h_file)
8196 +                       continue;
8197 +
8198 +               h_file = au_h_open(dentry, bindex, flags, file);
8199 +               err = PTR_ERR(h_file);
8200 +               if (IS_ERR(h_file))
8201 +                       goto out; /* close all? */
8202 +               au_set_h_fptr(file, bindex, h_file);
8203 +       }
8204 +       au_update_figen(file);
8205 +       /* todo: necessary? */
8206 +       /* file->f_ra = h_file->f_ra; */
8207 +       err = 0;
8208 +
8209 + out:
8210 +       return err;
8211 +}
8212 +
8213 +static int do_open_dir(struct file *file, int flags)
8214 +{
8215 +       int err;
8216 +       aufs_bindex_t bindex, btail;
8217 +       struct dentry *dentry, *h_dentry;
8218 +       struct file *h_file;
8219 +
8220 +       err = 0;
8221 +       dentry = file->f_dentry;
8222 +       au_set_fvdir_cache(file, NULL);
8223 +       au_fi(file)->fi_maintain_plink = 0;
8224 +       file->f_version = dentry->d_inode->i_version;
8225 +       bindex = au_dbstart(dentry);
8226 +       au_set_fbstart(file, bindex);
8227 +       btail = au_dbtaildir(dentry);
8228 +       au_set_fbend(file, btail);
8229 +       for (; !err && bindex <= btail; bindex++) {
8230 +               h_dentry = au_h_dptr(dentry, bindex);
8231 +               if (!h_dentry)
8232 +                       continue;
8233 +
8234 +               h_file = au_h_open(dentry, bindex, flags, file);
8235 +               if (IS_ERR(h_file)) {
8236 +                       err = PTR_ERR(h_file);
8237 +                       break;
8238 +               }
8239 +               au_set_h_fptr(file, bindex, h_file);
8240 +       }
8241 +       au_update_figen(file);
8242 +       /* todo: necessary? */
8243 +       /* file->f_ra = h_file->f_ra; */
8244 +       if (!err)
8245 +               return 0; /* success */
8246 +
8247 +       /* close all */
8248 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
8249 +               au_set_h_fptr(file, bindex, NULL);
8250 +       au_set_fbstart(file, -1);
8251 +       au_set_fbend(file, -1);
8252 +       return err;
8253 +}
8254 +
8255 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8256 +                        struct file *file)
8257 +{
8258 +       return au_do_open(file, do_open_dir);
8259 +}
8260 +
8261 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8262 +                           struct file *file)
8263 +{
8264 +       struct au_vdir *vdir_cache;
8265 +       struct super_block *sb;
8266 +       struct au_sbinfo *sbinfo;
8267 +
8268 +       sb = file->f_dentry->d_sb;
8269 +       si_noflush_read_lock(sb);
8270 +       fi_write_lock(file);
8271 +       vdir_cache = au_fvdir_cache(file);
8272 +       if (vdir_cache)
8273 +               au_vdir_free(vdir_cache);
8274 +       if (au_fi(file)->fi_maintain_plink) {
8275 +               sbinfo = au_sbi(sb);
8276 +               au_fclr_si(sbinfo, MAINTAIN_PLINK);
8277 +               wake_up_all(&sbinfo->si_plink_wq);
8278 +       }
8279 +       fi_write_unlock(file);
8280 +       au_finfo_fin(file);
8281 +       si_read_unlock(sb);
8282 +       return 0;
8283 +}
8284 +
8285 +/* ---------------------------------------------------------------------- */
8286 +
8287 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
8288 +{
8289 +       int err;
8290 +       aufs_bindex_t bend, bindex;
8291 +       struct inode *inode;
8292 +       struct super_block *sb;
8293 +
8294 +       err = 0;
8295 +       sb = dentry->d_sb;
8296 +       inode = dentry->d_inode;
8297 +       IMustLock(inode);
8298 +       bend = au_dbend(dentry);
8299 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
8300 +               struct path h_path;
8301 +               struct inode *h_inode;
8302 +
8303 +               if (au_test_ro(sb, bindex, inode))
8304 +                       continue;
8305 +               h_path.dentry = au_h_dptr(dentry, bindex);
8306 +               if (!h_path.dentry)
8307 +                       continue;
8308 +               h_inode = h_path.dentry->d_inode;
8309 +               if (!h_inode)
8310 +                       continue;
8311 +
8312 +               /* no mnt_want_write() */
8313 +               /* cf. fs/nsfd/vfs.c and fs/nfsd/nfs4recover.c */
8314 +               /* todo: inotiry fired? */
8315 +               h_path.mnt = au_sbr_mnt(sb, bindex);
8316 +               mutex_lock(&h_inode->i_mutex);
8317 +               err = filemap_fdatawrite(h_inode->i_mapping);
8318 +               AuDebugOn(!h_inode->i_fop);
8319 +               if (!err && h_inode->i_fop->fsync)
8320 +                       err = h_inode->i_fop->fsync(NULL, h_path.dentry,
8321 +                                                   datasync);
8322 +               if (!err)
8323 +                       err = filemap_fdatawrite(h_inode->i_mapping);
8324 +               if (!err)
8325 +                       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
8326 +               mutex_unlock(&h_inode->i_mutex);
8327 +       }
8328 +
8329 +       return err;
8330 +}
8331 +
8332 +static int au_do_fsync_dir(struct file *file, int datasync)
8333 +{
8334 +       int err;
8335 +       aufs_bindex_t bend, bindex;
8336 +       struct file *h_file;
8337 +       struct super_block *sb;
8338 +       struct inode *inode;
8339 +       struct mutex *h_mtx;
8340 +
8341 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8342 +       if (unlikely(err))
8343 +               goto out;
8344 +
8345 +       sb = file->f_dentry->d_sb;
8346 +       inode = file->f_dentry->d_inode;
8347 +       bend = au_fbend(file);
8348 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8349 +               h_file = au_h_fptr(file, bindex);
8350 +               if (!h_file || au_test_ro(sb, bindex, inode))
8351 +                       continue;
8352 +
8353 +               err = vfs_fsync(h_file, h_file->f_dentry, datasync);
8354 +               if (!err) {
8355 +                       h_mtx = &h_file->f_dentry->d_inode->i_mutex;
8356 +                       mutex_lock(h_mtx);
8357 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
8358 +                       /*ignore*/
8359 +                       mutex_unlock(h_mtx);
8360 +               }
8361 +       }
8362 +
8363 + out:
8364 +       return err;
8365 +}
8366 +
8367 +/*
8368 + * @file may be NULL
8369 + */
8370 +static int aufs_fsync_dir(struct file *file, struct dentry *dentry,
8371 +                         int datasync)
8372 +{
8373 +       int err;
8374 +       struct super_block *sb;
8375 +
8376 +       IMustLock(dentry->d_inode);
8377 +
8378 +       err = 0;
8379 +       sb = dentry->d_sb;
8380 +       si_noflush_read_lock(sb);
8381 +       if (file)
8382 +               err = au_do_fsync_dir(file, datasync);
8383 +       else {
8384 +               di_write_lock_child(dentry);
8385 +               err = au_do_fsync_dir_no_file(dentry, datasync);
8386 +       }
8387 +       au_cpup_attr_timesizes(dentry->d_inode);
8388 +       di_write_unlock(dentry);
8389 +       if (file)
8390 +               fi_write_unlock(file);
8391 +
8392 +       si_read_unlock(sb);
8393 +       return err;
8394 +}
8395 +
8396 +/* ---------------------------------------------------------------------- */
8397 +
8398 +static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
8399 +{
8400 +       int err;
8401 +       struct dentry *dentry;
8402 +       struct inode *inode;
8403 +       struct super_block *sb;
8404 +
8405 +       dentry = file->f_dentry;
8406 +       inode = dentry->d_inode;
8407 +       IMustLock(inode);
8408 +
8409 +       sb = dentry->d_sb;
8410 +       si_read_lock(sb, AuLock_FLUSH);
8411 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8412 +       if (unlikely(err))
8413 +               goto out;
8414 +       err = au_vdir_init(file);
8415 +       di_downgrade_lock(dentry, AuLock_IR);
8416 +       if (unlikely(err))
8417 +               goto out_unlock;
8418 +
8419 +       if (!au_test_nfsd(current)) {
8420 +               err = au_vdir_fill_de(file, dirent, filldir);
8421 +               fsstack_copy_attr_atime(inode,
8422 +                                       au_h_iptr(inode, au_ibstart(inode)));
8423 +       } else {
8424 +               /*
8425 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
8426 +                * encode_fh() and others.
8427 +                */
8428 +               struct inode *h_inode = au_h_iptr(inode, au_ibstart(inode));
8429 +
8430 +               di_read_unlock(dentry, AuLock_IR);
8431 +               si_read_unlock(sb);
8432 +               lockdep_off();
8433 +               err = au_vdir_fill_de(file, dirent, filldir);
8434 +               lockdep_on();
8435 +               fsstack_copy_attr_atime(inode, h_inode);
8436 +               fi_write_unlock(file);
8437 +
8438 +               AuTraceErr(err);
8439 +               return err;
8440 +       }
8441 +
8442 + out_unlock:
8443 +       di_read_unlock(dentry, AuLock_IR);
8444 +       fi_write_unlock(file);
8445 + out:
8446 +       si_read_unlock(sb);
8447 +       return err;
8448 +}
8449 +
8450 +/* ---------------------------------------------------------------------- */
8451 +
8452 +#define AuTestEmpty_WHONLY     1
8453 +#define AuTestEmpty_CALLED     (1 << 2)
8454 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
8455 +#define au_fset_testempty(flags, name) { (flags) |= AuTestEmpty_##name; }
8456 +#define au_fclr_testempty(flags, name) { (flags) &= ~AuTestEmpty_##name; }
8457 +
8458 +struct test_empty_arg {
8459 +       struct au_nhash *whlist;
8460 +       unsigned int flags;
8461 +       int err;
8462 +       aufs_bindex_t bindex;
8463 +};
8464 +
8465 +static int test_empty_cb(void *__arg, const char *__name, int namelen,
8466 +                        loff_t offset __maybe_unused, u64 ino __maybe_unused,
8467 +                        unsigned int d_type __maybe_unused)
8468 +{
8469 +       struct test_empty_arg *arg = __arg;
8470 +       char *name = (void *)__name;
8471 +
8472 +       arg->err = 0;
8473 +       au_fset_testempty(arg->flags, CALLED);
8474 +       /* smp_mb(); */
8475 +       if (name[0] == '.'
8476 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
8477 +               goto out; /* success */
8478 +
8479 +       if (namelen <= AUFS_WH_PFX_LEN
8480 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
8481 +               if (au_ftest_testempty(arg->flags, WHONLY)
8482 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
8483 +                       arg->err = -ENOTEMPTY;
8484 +               goto out;
8485 +       }
8486 +
8487 +       name += AUFS_WH_PFX_LEN;
8488 +       namelen -= AUFS_WH_PFX_LEN;
8489 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
8490 +               arg->err = au_nhash_append_wh
8491 +                       (arg->whlist, name, namelen, arg->bindex);
8492 +
8493 + out:
8494 +       /* smp_mb(); */
8495 +       AuTraceErr(arg->err);
8496 +       return arg->err;
8497 +}
8498 +
8499 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8500 +{
8501 +       int err;
8502 +       struct file *h_file;
8503 +
8504 +       h_file = au_h_open(dentry, arg->bindex,
8505 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
8506 +                          /*file*/NULL);
8507 +       err = PTR_ERR(h_file);
8508 +       if (IS_ERR(h_file))
8509 +               goto out;
8510 +
8511 +       err = 0;
8512 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
8513 +           && !h_file->f_dentry->d_inode->i_nlink)
8514 +               goto out_put;
8515 +
8516 +       do {
8517 +               arg->err = 0;
8518 +               au_fclr_testempty(arg->flags, CALLED);
8519 +               /* smp_mb(); */
8520 +               err = vfsub_readdir(h_file, test_empty_cb, arg);
8521 +               if (err >= 0)
8522 +                       err = arg->err;
8523 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
8524 +
8525 + out_put:
8526 +       fput(h_file);
8527 +       au_sbr_put(dentry->d_sb, arg->bindex);
8528 + out:
8529 +       return err;
8530 +}
8531 +
8532 +struct do_test_empty_args {
8533 +       int *errp;
8534 +       struct dentry *dentry;
8535 +       struct test_empty_arg *arg;
8536 +};
8537 +
8538 +static void call_do_test_empty(void *args)
8539 +{
8540 +       struct do_test_empty_args *a = args;
8541 +       *a->errp = do_test_empty(a->dentry, a->arg);
8542 +}
8543 +
8544 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8545 +{
8546 +       int err, wkq_err;
8547 +       struct dentry *h_dentry;
8548 +       struct inode *h_inode;
8549 +
8550 +       h_dentry = au_h_dptr(dentry, arg->bindex);
8551 +       h_inode = h_dentry->d_inode;
8552 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
8553 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
8554 +       mutex_unlock(&h_inode->i_mutex);
8555 +       if (!err)
8556 +               err = do_test_empty(dentry, arg);
8557 +       else {
8558 +               struct do_test_empty_args args = {
8559 +                       .errp   = &err,
8560 +                       .dentry = dentry,
8561 +                       .arg    = arg
8562 +               };
8563 +               unsigned int flags = arg->flags;
8564 +
8565 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
8566 +               if (unlikely(wkq_err))
8567 +                       err = wkq_err;
8568 +               arg->flags = flags;
8569 +       }
8570 +
8571 +       return err;
8572 +}
8573 +
8574 +int au_test_empty_lower(struct dentry *dentry)
8575 +{
8576 +       int err;
8577 +       aufs_bindex_t bindex, bstart, btail;
8578 +       struct test_empty_arg arg;
8579 +       struct au_nhash *whlist;
8580 +
8581 +       whlist = au_nhash_alloc(dentry->d_sb, /*bend*/0, GFP_NOFS);
8582 +       err = PTR_ERR(whlist);
8583 +       if (IS_ERR(whlist))
8584 +               goto out;
8585 +
8586 +       bstart = au_dbstart(dentry);
8587 +       arg.whlist = whlist;
8588 +       arg.flags = 0;
8589 +       arg.bindex = bstart;
8590 +       err = do_test_empty(dentry, &arg);
8591 +       if (unlikely(err))
8592 +               goto out_whlist;
8593 +
8594 +       au_fset_testempty(arg.flags, WHONLY);
8595 +       btail = au_dbtaildir(dentry);
8596 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
8597 +               struct dentry *h_dentry;
8598 +
8599 +               h_dentry = au_h_dptr(dentry, bindex);
8600 +               if (h_dentry && h_dentry->d_inode) {
8601 +                       arg.bindex = bindex;
8602 +                       err = do_test_empty(dentry, &arg);
8603 +               }
8604 +       }
8605 +
8606 + out_whlist:
8607 +       au_nhash_wh_free(whlist, /*bend*/0);
8608 + out:
8609 +       return err;
8610 +}
8611 +
8612 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
8613 +{
8614 +       int err;
8615 +       struct test_empty_arg arg;
8616 +       aufs_bindex_t bindex, btail;
8617 +
8618 +       err = 0;
8619 +       arg.whlist = whlist;
8620 +       arg.flags = AuTestEmpty_WHONLY;
8621 +       btail = au_dbtaildir(dentry);
8622 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
8623 +               struct dentry *h_dentry;
8624 +
8625 +               h_dentry = au_h_dptr(dentry, bindex);
8626 +               if (h_dentry && h_dentry->d_inode) {
8627 +                       arg.bindex = bindex;
8628 +                       err = sio_test_empty(dentry, &arg);
8629 +               }
8630 +       }
8631 +
8632 +       return err;
8633 +}
8634 +
8635 +/* ---------------------------------------------------------------------- */
8636 +
8637 +const struct file_operations aufs_dir_fop = {
8638 +       .read           = generic_read_dir,
8639 +       .readdir        = aufs_readdir,
8640 +       .unlocked_ioctl = aufs_ioctl_dir,
8641 +       .open           = aufs_open_dir,
8642 +       .release        = aufs_release_dir,
8643 +       .flush          = aufs_flush,
8644 +       .fsync          = aufs_fsync_dir
8645 +};
8646 diff --git a/fs/aufs/dir.h b/fs/aufs/dir.h
8647 new file mode 100644
8648 index 0000000..f41f050
8649 --- /dev/null
8650 +++ b/fs/aufs/dir.h
8651 @@ -0,0 +1,98 @@
8652 +/*
8653 + * Copyright (C) 2005-2009 Junjiro R. Okajima
8654 + *
8655 + * This program, aufs is free software; you can redistribute it and/or modify
8656 + * it under the terms of the GNU General Public License as published by
8657 + * the Free Software Foundation; either version 2 of the License, or
8658 + * (at your option) any later version.
8659 + */
8660 +
8661 +/*
8662 + * directory operations
8663 + */
8664 +
8665 +#ifndef __AUFS_DIR_H__
8666 +#define __AUFS_DIR_H__
8667 +
8668 +#ifdef __KERNEL__
8669 +
8670 +#include <linux/fs.h>
8671 +#include <linux/aufs_type.h>
8672 +
8673 +/* ---------------------------------------------------------------------- */
8674 +
8675 +/* need to be faster and smaller */
8676 +
8677 +struct au_nhash {
8678 +       unsigned int nh_num;
8679 +       struct hlist_head *nh_head;
8680 +};
8681 +
8682 +struct au_vdir_destr {
8683 +       unsigned char   len;
8684 +       unsigned char   name[0];
8685 +} __packed;
8686 +
8687 +struct au_vdir_dehstr {
8688 +       struct hlist_node       hash;
8689 +       struct au_vdir_destr    *str;
8690 +};
8691 +
8692 +struct au_vdir_de {
8693 +       ino_t                   de_ino;
8694 +       unsigned char           de_type;
8695 +       /* caution: packed */
8696 +       struct au_vdir_destr    de_str;
8697 +} __packed;
8698 +
8699 +struct au_vdir_wh {
8700 +       struct hlist_node       wh_hash;
8701 +       aufs_bindex_t           wh_bindex;
8702 +       struct au_vdir_destr    wh_str;
8703 +} __packed;
8704 +
8705 +union au_vdir_deblk_p {
8706 +       unsigned char           *deblk;
8707 +       struct au_vdir_de       *de;
8708 +};
8709 +
8710 +struct au_vdir {
8711 +       unsigned char   **vd_deblk;
8712 +       unsigned long   vd_nblk;
8713 +       unsigned int    vd_deblk_sz;
8714 +       struct {
8715 +               unsigned long           ul;
8716 +               union au_vdir_deblk_p   p;
8717 +       } vd_last;
8718 +
8719 +       unsigned long   vd_version;
8720 +       unsigned long   vd_jiffy;
8721 +};
8722 +
8723 +/* ---------------------------------------------------------------------- */
8724 +
8725 +/* dir.c */
8726 +extern const struct file_operations aufs_dir_fop;
8727 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
8728 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
8729 +int au_test_empty_lower(struct dentry *dentry);
8730 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
8731 +
8732 +/* vdir.c */
8733 +struct au_nhash *au_nhash_alloc(struct super_block *sb, aufs_bindex_t bend,
8734 +                               gfp_t gfp);
8735 +void au_nhash_wh_free(struct au_nhash *whlist, aufs_bindex_t bend);
8736 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
8737 +                           int limit);
8738 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int namelen);
8739 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int namelen,
8740 +                      aufs_bindex_t bindex);
8741 +void au_vdir_free(struct au_vdir *vdir);
8742 +int au_vdir_init(struct file *file);
8743 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir);
8744 +
8745 +/* ioctl.c */
8746 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
8747 +
8748 +#endif /* __KERNEL__ */
8749 +#endif /* __AUFS_DIR_H__ */
8750 diff --git a/fs/aufs/export.c b/fs/aufs/export.c
8751 new file mode 100644
8752 index 0000000..7dd7f33
8753 --- /dev/null
8754 +++ b/fs/aufs/export.c
8755 @@ -0,0 +1,725 @@
8756 +/*
8757 + * Copyright (C) 2005-2009 Junjiro Okajima
8758 + *
8759 + * This program, aufs is free software; you can redistribute it and/or modify
8760 + * it under the terms of the GNU General Public License as published by
8761 + * the Free Software Foundation; either version 2 of the License, or
8762 + * (at your option) any later version.
8763 + */
8764 +
8765 +/*
8766 + * export via nfs
8767 + */
8768 +
8769 +#include <linux/exportfs.h>
8770 +#include <linux/mnt_namespace.h>
8771 +#include <linux/random.h>
8772 +#include "aufs.h"
8773 +
8774 +union conv {
8775 +#ifdef CONFIG_AUFS_INO_T_64
8776 +       __u32 a[2];
8777 +#else
8778 +       __u32 a[1];
8779 +#endif
8780 +       ino_t ino;
8781 +};
8782 +
8783 +static ino_t decode_ino(__u32 *a)
8784 +{
8785 +       union conv u;
8786 +
8787 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
8788 +       u.a[0] = a[0];
8789 +#ifdef CONFIG_AUFS_INO_T_64
8790 +       u.a[1] = a[1];
8791 +#endif
8792 +       return u.ino;
8793 +}
8794 +
8795 +static void encode_ino(__u32 *a, ino_t ino)
8796 +{
8797 +       union conv u;
8798 +
8799 +       u.ino = ino;
8800 +       a[0] = u.a[0];
8801 +#ifdef CONFIG_AUFS_INO_T_64
8802 +       a[1] = u.a[1];
8803 +#endif
8804 +}
8805 +
8806 +/* NFS file handle */
8807 +enum {
8808 +       Fh_br_id,
8809 +       Fh_sigen,
8810 +#ifdef CONFIG_AUFS_INO_T_64
8811 +       /* support 64bit inode number */
8812 +       Fh_ino1,
8813 +       Fh_ino2,
8814 +       Fh_dir_ino1,
8815 +       Fh_dir_ino2,
8816 +#else
8817 +       Fh_ino1,
8818 +       Fh_dir_ino1,
8819 +#endif
8820 +       Fh_igen,
8821 +       Fh_h_type,
8822 +       Fh_tail,
8823 +
8824 +       Fh_ino = Fh_ino1,
8825 +       Fh_dir_ino = Fh_dir_ino1
8826 +};
8827 +
8828 +static int au_test_anon(struct dentry *dentry)
8829 +{
8830 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
8831 +}
8832 +
8833 +/* ---------------------------------------------------------------------- */
8834 +/* inode generation external table */
8835 +
8836 +int au_xigen_inc(struct inode *inode)
8837 +{
8838 +       int err;
8839 +       loff_t pos;
8840 +       ssize_t sz;
8841 +       __u32 igen;
8842 +       struct super_block *sb;
8843 +       struct au_sbinfo *sbinfo;
8844 +
8845 +       err = 0;
8846 +       sb = inode->i_sb;
8847 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
8848 +               goto out;
8849 +
8850 +       pos = inode->i_ino;
8851 +       pos *= sizeof(igen);
8852 +       igen = inode->i_generation + 1;
8853 +       sbinfo = au_sbi(sb);
8854 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
8855 +                        sizeof(igen), &pos);
8856 +       if (sz == sizeof(igen))
8857 +               goto out; /* success */
8858 +
8859 +       err = sz;
8860 +       if (unlikely(sz >= 0)) {
8861 +               err = -EIO;
8862 +               AuIOErr("xigen error (%zd)\n", sz);
8863 +       }
8864 +
8865 + out:
8866 +       return err;
8867 +}
8868 +
8869 +int au_xigen_new(struct inode *inode)
8870 +{
8871 +       int err;
8872 +       loff_t pos;
8873 +       ssize_t sz;
8874 +       struct super_block *sb;
8875 +       struct au_sbinfo *sbinfo;
8876 +       struct file *file;
8877 +
8878 +       err = 0;
8879 +       /* todo: dirty, at mount time */
8880 +       if (inode->i_ino == AUFS_ROOT_INO)
8881 +               goto out;
8882 +       sb = inode->i_sb;
8883 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
8884 +               goto out;
8885 +
8886 +       err = -EFBIG;
8887 +       pos = inode->i_ino;
8888 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
8889 +               AuIOErr1("too large i%lld\n", pos);
8890 +               goto out;
8891 +       }
8892 +       pos *= sizeof(inode->i_generation);
8893 +
8894 +       err = 0;
8895 +       sbinfo = au_sbi(sb);
8896 +       file = sbinfo->si_xigen;
8897 +       BUG_ON(!file);
8898 +
8899 +       if (i_size_read(file->f_dentry->d_inode)
8900 +           < pos + sizeof(inode->i_generation)) {
8901 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
8902 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
8903 +                                sizeof(inode->i_generation), &pos);
8904 +       } else
8905 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
8906 +                               sizeof(inode->i_generation), &pos);
8907 +       if (sz == sizeof(inode->i_generation))
8908 +               goto out; /* success */
8909 +
8910 +       err = sz;
8911 +       if (unlikely(sz >= 0)) {
8912 +               err = -EIO;
8913 +               AuIOErr("xigen error (%zd)\n", sz);
8914 +       }
8915 +
8916 + out:
8917 +       return err;
8918 +}
8919 +
8920 +int au_xigen_set(struct super_block *sb, struct file *base)
8921 +{
8922 +       int err;
8923 +       struct au_sbinfo *sbinfo;
8924 +       struct file *file;
8925 +
8926 +       sbinfo = au_sbi(sb);
8927 +       file = au_xino_create2(base, sbinfo->si_xigen);
8928 +       err = PTR_ERR(file);
8929 +       if (IS_ERR(file))
8930 +               goto out;
8931 +       err = 0;
8932 +       if (sbinfo->si_xigen)
8933 +               fput(sbinfo->si_xigen);
8934 +       sbinfo->si_xigen = file;
8935 +
8936 + out:
8937 +       return err;
8938 +}
8939 +
8940 +void au_xigen_clr(struct super_block *sb)
8941 +{
8942 +       struct au_sbinfo *sbinfo;
8943 +
8944 +       sbinfo = au_sbi(sb);
8945 +       if (sbinfo->si_xigen) {
8946 +               fput(sbinfo->si_xigen);
8947 +               sbinfo->si_xigen = NULL;
8948 +       }
8949 +}
8950 +
8951 +/* ---------------------------------------------------------------------- */
8952 +
8953 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
8954 +                                   ino_t dir_ino)
8955 +{
8956 +       struct dentry *dentry, *d;
8957 +       struct inode *inode;
8958 +       unsigned int sigen;
8959 +
8960 +       dentry = NULL;
8961 +       inode = ilookup(sb, ino);
8962 +       if (!inode)
8963 +               goto out;
8964 +
8965 +       dentry = ERR_PTR(-ESTALE);
8966 +       sigen = au_sigen(sb);
8967 +       if (unlikely(is_bad_inode(inode)
8968 +                    || IS_DEADDIR(inode)
8969 +                    || sigen != au_iigen(inode)))
8970 +               goto out_iput;
8971 +
8972 +       dentry = NULL;
8973 +       if (!dir_ino || S_ISDIR(inode->i_mode))
8974 +               dentry = d_find_alias(inode);
8975 +       else {
8976 +               spin_lock(&dcache_lock);
8977 +               list_for_each_entry(d, &inode->i_dentry, d_alias)
8978 +                       if (!au_test_anon(d)
8979 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
8980 +                               dentry = dget_locked(d);
8981 +                               break;
8982 +                       }
8983 +               spin_unlock(&dcache_lock);
8984 +       }
8985 +       if (unlikely(dentry && sigen != au_digen(dentry))) {
8986 +               dput(dentry);
8987 +               dentry = ERR_PTR(-ESTALE);
8988 +       }
8989 +
8990 + out_iput:
8991 +       iput(inode);
8992 + out:
8993 +       return dentry;
8994 +}
8995 +
8996 +/* ---------------------------------------------------------------------- */
8997 +
8998 +/* todo: dirty? */
8999 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
9000 +static struct vfsmount *au_mnt_get(struct super_block *sb)
9001 +{
9002 +       struct mnt_namespace *ns;
9003 +       struct vfsmount *pos, *mnt;
9004 +
9005 +       spin_lock(&vfsmount_lock);
9006 +       /* no get/put ?? */
9007 +       AuDebugOn(!current->nsproxy);
9008 +       ns = current->nsproxy->mnt_ns;
9009 +       AuDebugOn(!ns);
9010 +       mnt = NULL;
9011 +       /* the order (reverse) will not be a problem */
9012 +       list_for_each_entry(pos, &ns->list, mnt_list)
9013 +               if (pos->mnt_sb == sb) {
9014 +                       mnt = mntget(pos);
9015 +                       break;
9016 +               }
9017 +       spin_unlock(&vfsmount_lock);
9018 +       AuDebugOn(!mnt);
9019 +
9020 +       return mnt;
9021 +}
9022 +
9023 +struct au_nfsd_si_lock {
9024 +       const unsigned int sigen;
9025 +       const aufs_bindex_t br_id;
9026 +       unsigned char force_lock;
9027 +};
9028 +
9029 +static aufs_bindex_t si_nfsd_read_lock(struct super_block *sb,
9030 +                                      struct au_nfsd_si_lock *nsi_lock)
9031 +{
9032 +       aufs_bindex_t bindex;
9033 +
9034 +       si_read_lock(sb, AuLock_FLUSH);
9035 +
9036 +       /* branch id may be wrapped around */
9037 +       bindex = au_br_index(sb, nsi_lock->br_id);
9038 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
9039 +               goto out; /* success */
9040 +
9041 +       if (!nsi_lock->force_lock)
9042 +               si_read_unlock(sb);
9043 +       bindex = -1;
9044 +
9045 + out:
9046 +       return bindex;
9047 +}
9048 +
9049 +struct find_name_by_ino {
9050 +       int called, found;
9051 +       ino_t ino;
9052 +       char *name;
9053 +       int namelen;
9054 +};
9055 +
9056 +static int
9057 +find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
9058 +                u64 ino, unsigned int d_type)
9059 +{
9060 +       struct find_name_by_ino *a = arg;
9061 +
9062 +       a->called++;
9063 +       if (a->ino != ino)
9064 +               return 0;
9065 +
9066 +       memcpy(a->name, name, namelen);
9067 +       a->namelen = namelen;
9068 +       a->found = 1;
9069 +       return 1;
9070 +}
9071 +
9072 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
9073 +                                    struct au_nfsd_si_lock *nsi_lock)
9074 +{
9075 +       struct dentry *dentry, *parent;
9076 +       struct file *file;
9077 +       struct inode *dir;
9078 +       struct find_name_by_ino arg;
9079 +       int err;
9080 +
9081 +       parent = path->dentry;
9082 +       if (nsi_lock)
9083 +               si_read_unlock(parent->d_sb);
9084 +       path_get(path);
9085 +       file = dentry_open(parent, path->mnt, au_dir_roflags, current_cred());
9086 +       dentry = (void *)file;
9087 +       if (IS_ERR(file))
9088 +               goto out;
9089 +
9090 +       dentry = ERR_PTR(-ENOMEM);
9091 +       arg.name = __getname();
9092 +       if (unlikely(!arg.name))
9093 +               goto out_file;
9094 +       arg.ino = ino;
9095 +       arg.found = 0;
9096 +       do {
9097 +               arg.called = 0;
9098 +               /* smp_mb(); */
9099 +               err = vfsub_readdir(file, find_name_by_ino, &arg);
9100 +       } while (!err && !arg.found && arg.called);
9101 +       dentry = ERR_PTR(err);
9102 +       if (unlikely(err))
9103 +               goto out_name;
9104 +       dentry = ERR_PTR(-ENOENT);
9105 +       if (!arg.found)
9106 +               goto out_name;
9107 +
9108 +       /* do not call au_lkup_one() */
9109 +       dir = parent->d_inode;
9110 +       mutex_lock(&dir->i_mutex);
9111 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
9112 +       mutex_unlock(&dir->i_mutex);
9113 +       AuTraceErrPtr(dentry);
9114 +       if (IS_ERR(dentry))
9115 +               goto out_name;
9116 +       AuDebugOn(au_test_anon(dentry));
9117 +       if (unlikely(!dentry->d_inode)) {
9118 +               dput(dentry);
9119 +               dentry = ERR_PTR(-ENOENT);
9120 +       }
9121 +
9122 + out_name:
9123 +       __putname(arg.name);
9124 + out_file:
9125 +       fput(file);
9126 + out:
9127 +       if (unlikely(nsi_lock
9128 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
9129 +               if (!IS_ERR(dentry)) {
9130 +                       dput(dentry);
9131 +                       dentry = ERR_PTR(-ESTALE);
9132 +               }
9133 +       AuTraceErrPtr(dentry);
9134 +       return dentry;
9135 +}
9136 +
9137 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
9138 +                                       ino_t dir_ino,
9139 +                                       struct au_nfsd_si_lock *nsi_lock)
9140 +{
9141 +       struct dentry *dentry;
9142 +       struct path path;
9143 +
9144 +       if (dir_ino != AUFS_ROOT_INO) {
9145 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
9146 +               dentry = path.dentry;
9147 +               if (!path.dentry || IS_ERR(path.dentry))
9148 +                       goto out;
9149 +               AuDebugOn(au_test_anon(path.dentry));
9150 +       } else
9151 +               path.dentry = dget(sb->s_root);
9152 +
9153 +       path.mnt = au_mnt_get(sb);
9154 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
9155 +       path_put(&path);
9156 +
9157 + out:
9158 +       AuTraceErrPtr(dentry);
9159 +       return dentry;
9160 +}
9161 +
9162 +/* ---------------------------------------------------------------------- */
9163 +
9164 +static int h_acceptable(void *expv, struct dentry *dentry)
9165 +{
9166 +       return 1;
9167 +}
9168 +
9169 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
9170 +                          char *buf, int len, struct super_block *sb)
9171 +{
9172 +       char *p;
9173 +       int n;
9174 +       struct path path;
9175 +
9176 +       p = d_path(h_rootpath, buf, len);
9177 +       if (IS_ERR(p))
9178 +               goto out;
9179 +       n = strlen(p);
9180 +
9181 +       path.mnt = h_rootpath->mnt;
9182 +       path.dentry = h_parent;
9183 +       p = d_path(&path, buf, len);
9184 +       if (IS_ERR(p))
9185 +               goto out;
9186 +       if (n != 1)
9187 +               p += n;
9188 +
9189 +       path.mnt = au_mnt_get(sb);
9190 +       path.dentry = sb->s_root;
9191 +       p = d_path(&path, buf, len - strlen(p));
9192 +       mntput(path.mnt);
9193 +       if (IS_ERR(p))
9194 +               goto out;
9195 +       if (n != 1)
9196 +               p[strlen(p)] = '/';
9197 +
9198 + out:
9199 +       AuTraceErrPtr(p);
9200 +       return p;
9201 +}
9202 +
9203 +static
9204 +struct dentry *decode_by_path(struct super_block *sb, aufs_bindex_t bindex,
9205 +                             ino_t ino, __u32 *fh, int fh_len,
9206 +                             struct au_nfsd_si_lock *nsi_lock)
9207 +{
9208 +       struct dentry *dentry, *h_parent, *root;
9209 +       struct super_block *h_sb;
9210 +       char *pathname, *p;
9211 +       struct vfsmount *h_mnt;
9212 +       struct au_branch *br;
9213 +       int err;
9214 +       struct path path;
9215 +
9216 +       br = au_sbr(sb, bindex);
9217 +       /* au_br_get(br); */
9218 +       h_mnt = br->br_mnt;
9219 +       h_sb = h_mnt->mnt_sb;
9220 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
9221 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
9222 +                                     fh_len - Fh_tail, fh[Fh_h_type],
9223 +                                     h_acceptable, /*context*/NULL);
9224 +       dentry = h_parent;
9225 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
9226 +               AuWarn1("%s decode_fh failed, %ld\n",
9227 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
9228 +               goto out;
9229 +       }
9230 +       dentry = NULL;
9231 +       if (unlikely(au_test_anon(h_parent))) {
9232 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
9233 +                       au_sbtype(h_sb));
9234 +               goto out_h_parent;
9235 +       }
9236 +
9237 +       dentry = ERR_PTR(-ENOMEM);
9238 +       pathname = (void *)__get_free_page(GFP_NOFS);
9239 +       if (unlikely(!pathname))
9240 +               goto out_h_parent;
9241 +
9242 +       root = sb->s_root;
9243 +       path.mnt = h_mnt;
9244 +       di_read_lock_parent(root, !AuLock_IR);
9245 +       path.dentry = au_h_dptr(root, bindex);
9246 +       di_read_unlock(root, !AuLock_IR);
9247 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
9248 +       dentry = (void *)p;
9249 +       if (IS_ERR(p))
9250 +               goto out_pathname;
9251 +
9252 +       si_read_unlock(sb);
9253 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
9254 +       dentry = ERR_PTR(err);
9255 +       if (unlikely(err))
9256 +               goto out_relock;
9257 +
9258 +       dentry = ERR_PTR(-ENOENT);
9259 +       AuDebugOn(au_test_anon(path.dentry));
9260 +       if (unlikely(!path.dentry->d_inode))
9261 +               goto out_path;
9262 +
9263 +       if (ino != path.dentry->d_inode->i_ino)
9264 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
9265 +       else
9266 +               dentry = dget(path.dentry);
9267 +
9268 + out_path:
9269 +       path_put(&path);
9270 + out_relock:
9271 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
9272 +               if (!IS_ERR(dentry)) {
9273 +                       dput(dentry);
9274 +                       dentry = ERR_PTR(-ESTALE);
9275 +               }
9276 + out_pathname:
9277 +       free_page((unsigned long)pathname);
9278 + out_h_parent:
9279 +       dput(h_parent);
9280 + out:
9281 +       /* au_br_put(br); */
9282 +       AuTraceErrPtr(dentry);
9283 +       return dentry;
9284 +}
9285 +
9286 +/* ---------------------------------------------------------------------- */
9287 +
9288 +static struct dentry *
9289 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
9290 +                 int fh_type)
9291 +{
9292 +       struct dentry *dentry;
9293 +       __u32 *fh = fid->raw;
9294 +       ino_t ino, dir_ino;
9295 +       aufs_bindex_t bindex;
9296 +       struct au_nfsd_si_lock nsi_lock = {
9297 +               .sigen          = fh[Fh_sigen],
9298 +               .br_id          = fh[Fh_br_id],
9299 +               .force_lock     = 0
9300 +       };
9301 +
9302 +       AuDebugOn(fh_len < Fh_tail);
9303 +
9304 +       dentry = ERR_PTR(-ESTALE);
9305 +       /* branch id may be wrapped around */
9306 +       bindex = si_nfsd_read_lock(sb, &nsi_lock);
9307 +       if (unlikely(bindex < 0))
9308 +               goto out;
9309 +       nsi_lock.force_lock = 1;
9310 +
9311 +       /* is this inode still cached? */
9312 +       ino = decode_ino(fh + Fh_ino);
9313 +       AuDebugOn(ino == AUFS_ROOT_INO);
9314 +       dir_ino = decode_ino(fh + Fh_dir_ino);
9315 +       dentry = decode_by_ino(sb, ino, dir_ino);
9316 +       if (IS_ERR(dentry))
9317 +               goto out_unlock;
9318 +       if (dentry)
9319 +               goto accept;
9320 +
9321 +       /* is the parent dir cached? */
9322 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
9323 +       if (IS_ERR(dentry))
9324 +               goto out_unlock;
9325 +       if (dentry)
9326 +               goto accept;
9327 +
9328 +       /* lookup path */
9329 +       dentry = decode_by_path(sb, bindex, ino, fh, fh_len, &nsi_lock);
9330 +       if (IS_ERR(dentry))
9331 +               goto out_unlock;
9332 +       if (unlikely(!dentry))
9333 +               /* todo?: make it ESTALE */
9334 +               goto out_unlock;
9335 +
9336 + accept:
9337 +       if (dentry->d_inode->i_generation == fh[Fh_igen])
9338 +               goto out_unlock; /* success */
9339 +
9340 +       dput(dentry);
9341 +       dentry = ERR_PTR(-ESTALE);
9342 + out_unlock:
9343 +       si_read_unlock(sb);
9344 + out:
9345 +       AuTraceErrPtr(dentry);
9346 +       return dentry;
9347 +}
9348 +
9349 +#if 0 /* reserved for future use */
9350 +/* support subtreecheck option */
9351 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
9352 +                                       int fh_len, int fh_type)
9353 +{
9354 +       struct dentry *parent;
9355 +       __u32 *fh = fid->raw;
9356 +       ino_t dir_ino;
9357 +
9358 +       dir_ino = decode_ino(fh + Fh_dir_ino);
9359 +       parent = decode_by_ino(sb, dir_ino, 0);
9360 +       if (IS_ERR(parent))
9361 +               goto out;
9362 +       if (!parent)
9363 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
9364 +                                       dir_ino, fh, fh_len);
9365 +
9366 + out:
9367 +       AuTraceErrPtr(parent);
9368 +       return parent;
9369 +}
9370 +#endif
9371 +
9372 +/* ---------------------------------------------------------------------- */
9373 +
9374 +static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
9375 +                         int connectable)
9376 +{
9377 +       int err;
9378 +       aufs_bindex_t bindex, bend;
9379 +       struct super_block *sb, *h_sb;
9380 +       struct inode *inode;
9381 +       struct dentry *parent, *h_parent;
9382 +       struct au_branch *br;
9383 +
9384 +       AuDebugOn(au_test_anon(dentry));
9385 +
9386 +       parent = NULL;
9387 +       err = -ENOSPC;
9388 +       if (unlikely(*max_len <= Fh_tail)) {
9389 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
9390 +               goto out;
9391 +       }
9392 +
9393 +       err = FILEID_ROOT;
9394 +       if (IS_ROOT(dentry)) {
9395 +               AuDebugOn(dentry->d_inode->i_ino != AUFS_ROOT_INO);
9396 +               goto out;
9397 +       }
9398 +
9399 +       err = -EIO;
9400 +       h_parent = NULL;
9401 +       sb = dentry->d_sb;
9402 +       aufs_read_lock(dentry, AuLock_FLUSH | AuLock_IR);
9403 +       parent = dget_parent(dentry);
9404 +       di_read_lock_parent(parent, !AuLock_IR);
9405 +       inode = dentry->d_inode;
9406 +       AuDebugOn(!inode);
9407 +#ifdef CONFIG_AUFS_DEBUG
9408 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9409 +               AuWarn1("NFS-exporting requires xino\n");
9410 +#endif
9411 +
9412 +       bend = au_dbtaildir(parent);
9413 +       for (bindex = au_dbstart(parent); bindex <= bend; bindex++) {
9414 +               h_parent = au_h_dptr(parent, bindex);
9415 +               if (h_parent) {
9416 +                       dget(h_parent);
9417 +                       break;
9418 +               }
9419 +       }
9420 +       if (unlikely(!h_parent))
9421 +               goto out_unlock;
9422 +
9423 +       err = -EPERM;
9424 +       br = au_sbr(sb, bindex);
9425 +       h_sb = br->br_mnt->mnt_sb;
9426 +       if (unlikely(!h_sb->s_export_op)) {
9427 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
9428 +               goto out_dput;
9429 +       }
9430 +
9431 +       fh[Fh_br_id] = br->br_id;
9432 +       fh[Fh_sigen] = au_sigen(sb);
9433 +       encode_ino(fh + Fh_ino, inode->i_ino);
9434 +       encode_ino(fh + Fh_dir_ino, parent->d_inode->i_ino);
9435 +       fh[Fh_igen] = inode->i_generation;
9436 +
9437 +       *max_len -= Fh_tail;
9438 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
9439 +                                          max_len,
9440 +                                          /*connectable or subtreecheck*/0);
9441 +       err = fh[Fh_h_type];
9442 +       *max_len += Fh_tail;
9443 +       /* todo: macros? */
9444 +       if (err != 255)
9445 +               err = 99;
9446 +       else
9447 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
9448 +
9449 + out_dput:
9450 +       dput(h_parent);
9451 + out_unlock:
9452 +       di_read_unlock(parent, !AuLock_IR);
9453 +       dput(parent);
9454 +       aufs_read_unlock(dentry, AuLock_IR);
9455 + out:
9456 +       if (unlikely(err < 0))
9457 +               err = 255;
9458 +       return err;
9459 +}
9460 +
9461 +/* ---------------------------------------------------------------------- */
9462 +
9463 +static struct export_operations aufs_export_op = {
9464 +       .fh_to_dentry   = aufs_fh_to_dentry,
9465 +       /* .fh_to_parent        = aufs_fh_to_parent, */
9466 +       .encode_fh      = aufs_encode_fh
9467 +};
9468 +
9469 +void au_export_init(struct super_block *sb)
9470 +{
9471 +       struct au_sbinfo *sbinfo;
9472 +       __u32 u;
9473 +
9474 +       sb->s_export_op = &aufs_export_op;
9475 +       sbinfo = au_sbi(sb);
9476 +       sbinfo->si_xigen = NULL;
9477 +       get_random_bytes(&u, sizeof(u));
9478 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
9479 +       atomic_set(&sbinfo->si_xigen_next, u);
9480 +}
9481 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
9482 new file mode 100644
9483 index 0000000..7841d99
9484 --- /dev/null
9485 +++ b/fs/aufs/f_op.c
9486 @@ -0,0 +1,556 @@
9487 +/*
9488 + * Copyright (C) 2005-2009 Junjiro R. Okajima
9489 + *
9490 + * This program, aufs is free software; you can redistribute it and/or modify
9491 + * it under the terms of the GNU General Public License as published by
9492 + * the Free Software Foundation; either version 2 of the License, or
9493 + * (at your option) any later version.
9494 + */
9495 +
9496 +/*
9497 + * file and vm operations
9498 + */
9499 +
9500 +#include <linux/fs_stack.h>
9501 +#include <linux/poll.h>
9502 +#include "aufs.h"
9503 +
9504 +/* common function to regular file and dir */
9505 +int aufs_flush(struct file *file, fl_owner_t id)
9506 +{
9507 +       int err;
9508 +       aufs_bindex_t bindex, bend;
9509 +       struct dentry *dentry;
9510 +       struct file *h_file;
9511 +
9512 +       dentry = file->f_dentry;
9513 +       si_noflush_read_lock(dentry->d_sb);
9514 +       fi_read_lock(file);
9515 +       di_read_lock_child(dentry, AuLock_IW);
9516 +
9517 +       err = 0;
9518 +       bend = au_fbend(file);
9519 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9520 +               h_file = au_h_fptr(file, bindex);
9521 +               if (!h_file || !h_file->f_op || !h_file->f_op->flush)
9522 +                       continue;
9523 +
9524 +               err = h_file->f_op->flush(h_file, id);
9525 +               if (!err)
9526 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
9527 +               /*ignore*/
9528 +       }
9529 +       au_cpup_attr_timesizes(dentry->d_inode);
9530 +
9531 +       di_read_unlock(dentry, AuLock_IW);
9532 +       fi_read_unlock(file);
9533 +       si_read_unlock(dentry->d_sb);
9534 +       return err;
9535 +}
9536 +
9537 +/* ---------------------------------------------------------------------- */
9538 +
9539 +static int do_open_nondir(struct file *file, int flags)
9540 +{
9541 +       int err;
9542 +       aufs_bindex_t bindex;
9543 +       struct file *h_file;
9544 +       struct dentry *dentry;
9545 +
9546 +       err = 0;
9547 +       dentry = file->f_dentry;
9548 +       au_fi(file)->fi_h_vm_ops = NULL;
9549 +       bindex = au_dbstart(dentry);
9550 +       /* O_TRUNC is processed already */
9551 +       BUG_ON(au_test_ro(dentry->d_sb, bindex, dentry->d_inode)
9552 +              && (flags & O_TRUNC));
9553 +
9554 +       h_file = au_h_open(dentry, bindex, flags, file);
9555 +       if (IS_ERR(h_file))
9556 +               err = PTR_ERR(h_file);
9557 +       else {
9558 +               au_set_fbstart(file, bindex);
9559 +               au_set_fbend(file, bindex);
9560 +               au_set_h_fptr(file, bindex, h_file);
9561 +               au_update_figen(file);
9562 +               /* todo: necessary? */
9563 +               /* file->f_ra = h_file->f_ra; */
9564 +       }
9565 +       return err;
9566 +}
9567 +
9568 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
9569 +                           struct file *file)
9570 +{
9571 +       return au_do_open(file, do_open_nondir);
9572 +}
9573 +
9574 +static int aufs_release_nondir(struct inode *inode __maybe_unused,
9575 +                              struct file *file)
9576 +{
9577 +       struct super_block *sb = file->f_dentry->d_sb;
9578 +
9579 +       si_noflush_read_lock(sb);
9580 +       au_finfo_fin(file);
9581 +       si_read_unlock(sb);
9582 +       return 0;
9583 +}
9584 +
9585 +/* ---------------------------------------------------------------------- */
9586 +
9587 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
9588 +                        loff_t *ppos)
9589 +{
9590 +       ssize_t err;
9591 +       struct dentry *dentry;
9592 +       struct file *h_file;
9593 +       struct super_block *sb;
9594 +
9595 +       dentry = file->f_dentry;
9596 +       sb = dentry->d_sb;
9597 +       si_read_lock(sb, AuLock_FLUSH);
9598 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
9599 +       if (unlikely(err))
9600 +               goto out;
9601 +
9602 +       h_file = au_h_fptr(file, au_fbstart(file));
9603 +       err = vfsub_read_u(h_file, buf, count, ppos);
9604 +       /* todo: necessary? */
9605 +       /* file->f_ra = h_file->f_ra; */
9606 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
9607 +
9608 +       di_read_unlock(dentry, AuLock_IR);
9609 +       fi_read_unlock(file);
9610 + out:
9611 +       si_read_unlock(sb);
9612 +       return err;
9613 +}
9614 +
9615 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
9616 +                         size_t count, loff_t *ppos)
9617 +{
9618 +       ssize_t err;
9619 +       aufs_bindex_t bstart;
9620 +       struct au_pin pin;
9621 +       struct dentry *dentry;
9622 +       struct inode *inode;
9623 +       struct super_block *sb;
9624 +       struct file *h_file;
9625 +       char __user *buf = (char __user *)ubuf;
9626 +
9627 +       dentry = file->f_dentry;
9628 +       sb = dentry->d_sb;
9629 +       inode = dentry->d_inode;
9630 +       mutex_lock(&inode->i_mutex);
9631 +       si_read_lock(sb, AuLock_FLUSH);
9632 +
9633 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
9634 +       if (unlikely(err))
9635 +               goto out;
9636 +
9637 +       err = au_ready_to_write(file, -1, &pin);
9638 +       di_downgrade_lock(dentry, AuLock_IR);
9639 +       if (unlikely(err))
9640 +               goto out_unlock;
9641 +
9642 +       bstart = au_fbstart(file);
9643 +       h_file = au_h_fptr(file, bstart);
9644 +       au_unpin(&pin);
9645 +       err = vfsub_write_u(h_file, buf, count, ppos);
9646 +       au_cpup_attr_timesizes(inode);
9647 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
9648 +
9649 + out_unlock:
9650 +       di_read_unlock(dentry, AuLock_IR);
9651 +       fi_write_unlock(file);
9652 + out:
9653 +       si_read_unlock(sb);
9654 +       mutex_unlock(&inode->i_mutex);
9655 +       return err;
9656 +}
9657 +
9658 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
9659 +                               struct pipe_inode_info *pipe, size_t len,
9660 +                               unsigned int flags)
9661 +{
9662 +       ssize_t err;
9663 +       struct file *h_file;
9664 +       struct dentry *dentry;
9665 +       struct super_block *sb;
9666 +
9667 +       dentry = file->f_dentry;
9668 +       sb = dentry->d_sb;
9669 +       si_read_lock(sb, AuLock_FLUSH);
9670 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
9671 +       if (unlikely(err))
9672 +               goto out;
9673 +
9674 +       err = -EINVAL;
9675 +       h_file = au_h_fptr(file, au_fbstart(file));
9676 +       if (au_test_loopback_kthread()) {
9677 +               file->f_mapping = h_file->f_mapping;
9678 +               smp_mb(); /* unnecessary? */
9679 +       }
9680 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
9681 +       /* todo: necessasry? */
9682 +       /* file->f_ra = h_file->f_ra; */
9683 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
9684 +
9685 +       di_read_unlock(dentry, AuLock_IR);
9686 +       fi_read_unlock(file);
9687 +
9688 + out:
9689 +       si_read_unlock(sb);
9690 +       return err;
9691 +}
9692 +
9693 +static ssize_t
9694 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
9695 +                 size_t len, unsigned int flags)
9696 +{
9697 +       ssize_t err;
9698 +       struct au_pin pin;
9699 +       struct dentry *dentry;
9700 +       struct inode *inode;
9701 +       struct super_block *sb;
9702 +       struct file *h_file;
9703 +
9704 +       dentry = file->f_dentry;
9705 +       inode = dentry->d_inode;
9706 +       mutex_lock(&inode->i_mutex);
9707 +       sb = dentry->d_sb;
9708 +       si_read_lock(sb, AuLock_FLUSH);
9709 +
9710 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
9711 +       if (unlikely(err))
9712 +               goto out;
9713 +
9714 +       err = au_ready_to_write(file, -1, &pin);
9715 +       di_downgrade_lock(dentry, AuLock_IR);
9716 +       if (unlikely(err))
9717 +               goto out_unlock;
9718 +
9719 +       h_file = au_h_fptr(file, au_fbstart(file));
9720 +       au_unpin(&pin);
9721 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
9722 +       au_cpup_attr_timesizes(inode);
9723 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
9724 +
9725 + out_unlock:
9726 +       di_read_unlock(dentry, AuLock_IR);
9727 +       fi_write_unlock(file);
9728 + out:
9729 +       si_read_unlock(sb);
9730 +       mutex_unlock(&inode->i_mutex);
9731 +       return err;
9732 +}
9733 +
9734 +/* ---------------------------------------------------------------------- */
9735 +
9736 +static struct file *au_safe_file(struct vm_area_struct *vma)
9737 +{
9738 +       struct file *file;
9739 +
9740 +       file = vma->vm_file;
9741 +       if (file->private_data && au_test_aufs(file->f_dentry->d_sb))
9742 +               return file;
9743 +       return NULL;
9744 +}
9745 +
9746 +static void au_reset_file(struct vm_area_struct *vma, struct file *file)
9747 +{
9748 +       vma->vm_file = file;
9749 +       /* smp_mb(); */ /* flush vm_file */
9750 +}
9751 +
9752 +static int aufs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
9753 +{
9754 +       int err;
9755 +       static DECLARE_WAIT_QUEUE_HEAD(wq);
9756 +       struct file *file, *h_file;
9757 +       struct au_finfo *finfo;
9758 +
9759 +       /* todo: non-robr mode, user vm_file as it is? */
9760 +       wait_event(wq, (file = au_safe_file(vma)));
9761 +
9762 +       /* do not revalidate, no si lock */
9763 +       finfo = au_fi(file);
9764 +       h_file = finfo->fi_hfile[0 + finfo->fi_bstart].hf_file;
9765 +       AuDebugOn(!h_file || !au_test_mmapped(file));
9766 +
9767 +       fi_write_lock(file);
9768 +       vma->vm_file = h_file;
9769 +       err = finfo->fi_h_vm_ops->fault(vma, vmf);
9770 +       /* todo: necessary? */
9771 +       /* file->f_ra = h_file->f_ra; */
9772 +       au_reset_file(vma, file);
9773 +       fi_write_unlock(file);
9774 +#if 0 /* def CONFIG_SMP */
9775 +       /* wake_up_nr(&wq, online_cpu - 1); */
9776 +       wake_up_all(&wq);
9777 +#else
9778 +       wake_up(&wq);
9779 +#endif
9780 +
9781 +       return err;
9782 +}
9783 +
9784 +static struct vm_operations_struct aufs_vm_ops = {
9785 +       .fault          = aufs_fault
9786 +};
9787 +
9788 +/* ---------------------------------------------------------------------- */
9789 +
9790 +static struct vm_operations_struct *au_vm_ops(struct file *h_file,
9791 +                                             struct vm_area_struct *vma)
9792 +{
9793 +       struct vm_operations_struct *vm_ops;
9794 +       int err;
9795 +
9796 +       vm_ops = ERR_PTR(-ENODEV);
9797 +       if (!h_file->f_op || !h_file->f_op->mmap)
9798 +               goto out;
9799 +
9800 +       err = h_file->f_op->mmap(h_file, vma);
9801 +       vm_ops = ERR_PTR(err);
9802 +       if (unlikely(err))
9803 +               goto out;
9804 +
9805 +       vm_ops = vma->vm_ops;
9806 +       err = do_munmap(current->mm, vma->vm_start,
9807 +                       vma->vm_end - vma->vm_start);
9808 +       if (unlikely(err)) {
9809 +               AuIOErr("failed internal unmapping %.*s, %d\n",
9810 +                       AuDLNPair(h_file->f_dentry), err);
9811 +               vm_ops = ERR_PTR(-EIO);
9812 +       }
9813 +
9814 + out:
9815 +       return vm_ops;
9816 +}
9817 +
9818 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
9819 +{
9820 +       int err;
9821 +       unsigned char wlock, mmapped;
9822 +       struct dentry *dentry;
9823 +       struct super_block *sb;
9824 +       struct file *h_file;
9825 +       struct vm_operations_struct *vm_ops;
9826 +
9827 +       dentry = file->f_dentry;
9828 +       mmapped = !!au_test_mmapped(file); /* can be harmless race condition */
9829 +       wlock = !!(file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
9830 +       sb = dentry->d_sb;
9831 +       si_read_lock(sb, AuLock_FLUSH);
9832 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, wlock | !mmapped);
9833 +       if (unlikely(err))
9834 +               goto out;
9835 +
9836 +       if (wlock) {
9837 +               struct au_pin pin;
9838 +
9839 +               err = au_ready_to_write(file, -1, &pin);
9840 +               di_downgrade_lock(dentry, AuLock_IR);
9841 +               if (unlikely(err))
9842 +                       goto out_unlock;
9843 +               au_unpin(&pin);
9844 +       } else if (!mmapped)
9845 +               di_downgrade_lock(dentry, AuLock_IR);
9846 +
9847 +       h_file = au_h_fptr(file, au_fbstart(file));
9848 +       if (au_test_fs_bad_mapping(h_file->f_dentry->d_sb)) {
9849 +               /*
9850 +                * by this assignment, f_mapping will differs from aufs inode
9851 +                * i_mapping.
9852 +                * if someone else mixes the use of f_dentry->d_inode and
9853 +                * f_mapping->host, then a problem may arise.
9854 +                */
9855 +               file->f_mapping = h_file->f_mapping;
9856 +       }
9857 +
9858 +       vm_ops = NULL;
9859 +       if (!mmapped) {
9860 +               vm_ops = au_vm_ops(h_file, vma);
9861 +               err = PTR_ERR(vm_ops);
9862 +               if (IS_ERR(vm_ops))
9863 +                       goto out_unlock;
9864 +       }
9865 +
9866 +       /*
9867 +        * unnecessary to handle MAP_DENYWRITE and deny_write_access()?
9868 +        * currently MAP_DENYWRITE from userspace is ignored, but elf loader
9869 +        * sets it. when FMODE_EXEC is set (by open_exec() or sys_uselib()),
9870 +        * both of the aufs file and the lower file is deny_write_access()-ed.
9871 +        * finally I hope we can skip handlling MAP_DENYWRITE here.
9872 +        */
9873 +       err = generic_file_mmap(file, vma);
9874 +       if (unlikely(err))
9875 +               goto out_unlock;
9876 +       vma->vm_ops = &aufs_vm_ops;
9877 +       /* test again */
9878 +       if (!au_test_mmapped(file))
9879 +               au_fi(file)->fi_h_vm_ops = vm_ops;
9880 +
9881 +       vfsub_file_accessed(h_file);
9882 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
9883 +
9884 + out_unlock:
9885 +       di_read_unlock(dentry, AuLock_IR);
9886 +       if (!wlock && mmapped)
9887 +               fi_read_unlock(file);
9888 +       else
9889 +               fi_write_unlock(file);
9890 + out:
9891 +       si_read_unlock(sb);
9892 +       return err;
9893 +}
9894 +
9895 +/* ---------------------------------------------------------------------- */
9896 +
9897 +static unsigned int aufs_poll(struct file *file, poll_table *wait)
9898 +{
9899 +       unsigned int mask;
9900 +       int err;
9901 +       struct file *h_file;
9902 +       struct dentry *dentry;
9903 +       struct super_block *sb;
9904 +
9905 +       /* We should pretend an error happened. */
9906 +       mask = POLLERR /* | POLLIN | POLLOUT */;
9907 +       dentry = file->f_dentry;
9908 +       sb = dentry->d_sb;
9909 +       si_read_lock(sb, AuLock_FLUSH);
9910 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
9911 +       if (unlikely(err))
9912 +               goto out;
9913 +
9914 +       /* it is not an error if h_file has no operation */
9915 +       mask = DEFAULT_POLLMASK;
9916 +       h_file = au_h_fptr(file, au_fbstart(file));
9917 +       if (h_file->f_op && h_file->f_op->poll)
9918 +               mask = h_file->f_op->poll(h_file, wait);
9919 +
9920 +       di_read_unlock(dentry, AuLock_IR);
9921 +       fi_read_unlock(file);
9922 +
9923 + out:
9924 +       si_read_unlock(sb);
9925 +       AuTraceErr((int)mask);
9926 +       return mask;
9927 +}
9928 +
9929 +static int aufs_fsync_nondir(struct file *file, struct dentry *dentry,
9930 +                            int datasync)
9931 +{
9932 +       int err;
9933 +       struct au_pin pin;
9934 +       struct inode *inode;
9935 +       struct file *h_file;
9936 +       struct super_block *sb;
9937 +
9938 +       inode = dentry->d_inode;
9939 +       IMustLock(file->f_mapping->host);
9940 +       if (inode != file->f_mapping->host) {
9941 +               mutex_unlock(&file->f_mapping->host->i_mutex);
9942 +               mutex_lock(&inode->i_mutex);
9943 +       }
9944 +       IMustLock(inode);
9945 +
9946 +       sb = dentry->d_sb;
9947 +       si_read_lock(sb, AuLock_FLUSH);
9948 +
9949 +       err = 0; /* -EBADF; */ /* posix? */
9950 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
9951 +               goto out;
9952 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
9953 +       if (unlikely(err))
9954 +               goto out;
9955 +
9956 +       err = au_ready_to_write(file, -1, &pin);
9957 +       di_downgrade_lock(dentry, AuLock_IR);
9958 +       if (unlikely(err))
9959 +               goto out_unlock;
9960 +       au_unpin(&pin);
9961 +
9962 +       err = -EINVAL;
9963 +       h_file = au_h_fptr(file, au_fbstart(file));
9964 +       if (h_file->f_op && h_file->f_op->fsync) {
9965 +               struct dentry *h_d;
9966 +               struct mutex *h_mtx;
9967 +
9968 +               /*
9969 +                * no filemap_fdatawrite() since aufs file has no its own
9970 +                * mapping, but dir.
9971 +                */
9972 +               h_d = h_file->f_dentry;
9973 +               h_mtx = &h_d->d_inode->i_mutex;
9974 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
9975 +               err = h_file->f_op->fsync(h_file, h_d, datasync);
9976 +               if (!err)
9977 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
9978 +               /*ignore*/
9979 +               au_cpup_attr_timesizes(inode);
9980 +               mutex_unlock(h_mtx);
9981 +       }
9982 +
9983 + out_unlock:
9984 +       di_read_unlock(dentry, AuLock_IR);
9985 +       fi_write_unlock(file);
9986 + out:
9987 +       si_read_unlock(sb);
9988 +       if (inode != file->f_mapping->host) {
9989 +               mutex_unlock(&inode->i_mutex);
9990 +               mutex_lock(&file->f_mapping->host->i_mutex);
9991 +       }
9992 +       return err;
9993 +}
9994 +
9995 +static int aufs_fasync(int fd, struct file *file, int flag)
9996 +{
9997 +       int err;
9998 +       struct file *h_file;
9999 +       struct dentry *dentry;
10000 +       struct super_block *sb;
10001 +
10002 +       dentry = file->f_dentry;
10003 +       sb = dentry->d_sb;
10004 +       si_read_lock(sb, AuLock_FLUSH);
10005 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
10006 +       if (unlikely(err))
10007 +               goto out;
10008 +
10009 +       h_file = au_h_fptr(file, au_fbstart(file));
10010 +       if (h_file->f_op && h_file->f_op->fasync)
10011 +               err = h_file->f_op->fasync(fd, h_file, flag);
10012 +
10013 +       di_read_unlock(dentry, AuLock_IR);
10014 +       fi_read_unlock(file);
10015 +
10016 + out:
10017 +       si_read_unlock(sb);
10018 +       return err;
10019 +}
10020 +
10021 +/* ---------------------------------------------------------------------- */
10022 +
10023 +const struct file_operations aufs_file_fop = {
10024 +       /*
10025 +        * while generic_file_llseek/_unlocked() don't use BKL,
10026 +        * don't use it since it operates file->f_mapping->host.
10027 +        * in aufs, it may be a real file and may confuse users by UDBA.
10028 +        */
10029 +       /* .llseek              = generic_file_llseek, */
10030 +
10031 +       .read           = aufs_read,
10032 +       .write          = aufs_write,
10033 +       .poll           = aufs_poll,
10034 +       .mmap           = aufs_mmap,
10035 +       .open           = aufs_open_nondir,
10036 +       .flush          = aufs_flush,
10037 +       .release        = aufs_release_nondir,
10038 +       .fsync          = aufs_fsync_nondir,
10039 +       .fasync         = aufs_fasync,
10040 +       .splice_write   = aufs_splice_write,
10041 +       .splice_read    = aufs_splice_read
10042 +};
10043 diff --git a/fs/aufs/file.c b/fs/aufs/file.c
10044 new file mode 100644
10045 index 0000000..d472587
10046 --- /dev/null
10047 +++ b/fs/aufs/file.c
10048 @@ -0,0 +1,557 @@
10049 +/*
10050 + * Copyright (C) 2005-2009 Junjiro R. Okajima
10051 + *
10052 + * This program, aufs is free software; you can redistribute it and/or modify
10053 + * it under the terms of the GNU General Public License as published by
10054 + * the Free Software Foundation; either version 2 of the License, or
10055 + * (at your option) any later version.
10056 + */
10057 +
10058 +/*
10059 + * handling file/dir, and address_space operation
10060 + */
10061 +
10062 +#include <linux/fsnotify.h>
10063 +#include <linux/pagemap.h>
10064 +#include "aufs.h"
10065 +
10066 +/*
10067 + * a dirty trick for handling deny_write_access().
10068 + * because FMODE_EXEC flag is not passed to f_op->open(),
10069 + * set it to file->private_data temporary.
10070 + */
10071 +void au_store_oflag(struct nameidata *nd, struct inode *inode)
10072 +{
10073 +       if (nd
10074 +           /* && !(nd->flags & LOOKUP_CONTINUE) */
10075 +           && (nd->flags & LOOKUP_OPEN)
10076 +           && (nd->intent.open.flags & vfsub_fmode_to_uint(FMODE_EXEC))
10077 +           && inode
10078 +           && S_ISREG(inode->i_mode)) {
10079 +               /* suppress a warning in lp64 */
10080 +               unsigned long flags = nd->intent.open.flags;
10081 +               nd->intent.open.file->private_data = (void *)flags;
10082 +               /* smp_mb(); */
10083 +       }
10084 +}
10085 +
10086 +/* drop flags for writing */
10087 +unsigned int au_file_roflags(unsigned int flags)
10088 +{
10089 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
10090 +       flags |= O_RDONLY | O_NOATIME;
10091 +       return flags;
10092 +}
10093 +
10094 +/* common functions to regular file and dir */
10095 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10096 +                      struct file *file)
10097 +{
10098 +       struct file *h_file;
10099 +       struct dentry *h_dentry;
10100 +       struct inode *h_inode;
10101 +       struct super_block *sb;
10102 +       struct au_branch *br;
10103 +       int err;
10104 +
10105 +       h_dentry = au_h_dptr(dentry, bindex);
10106 +       h_inode = h_dentry->d_inode;
10107 +       /* a race condition can happen between open and unlink/rmdir */
10108 +       h_file = ERR_PTR(-ENOENT);
10109 +       if (unlikely((!d_unhashed(dentry) && d_unhashed(h_dentry))
10110 +                    || !h_inode))
10111 +               goto out;
10112 +
10113 +       sb = dentry->d_sb;
10114 +       br = au_sbr(sb, bindex);
10115 +       h_file = ERR_PTR(-EACCES);
10116 +       if (file && (file->f_mode & FMODE_EXEC)
10117 +           && (br->br_mnt->mnt_flags & MNT_NOEXEC))
10118 +               goto out;
10119 +
10120 +       /* drop flags for writing */
10121 +       if (au_test_ro(sb, bindex, dentry->d_inode))
10122 +               flags = au_file_roflags(flags);
10123 +       flags &= ~O_CREAT;
10124 +       atomic_inc(&br->br_count);
10125 +       h_file = dentry_open(dget(h_dentry), mntget(br->br_mnt), flags,
10126 +                            current_cred());
10127 +       if (IS_ERR(h_file))
10128 +               goto out_br;
10129 +
10130 +       if (file && (file->f_mode & FMODE_EXEC)) {
10131 +               h_file->f_mode |= FMODE_EXEC;
10132 +               err = deny_write_access(h_file);
10133 +               if (unlikely(err)) {
10134 +                       fput(h_file);
10135 +                       h_file = ERR_PTR(err);
10136 +                       goto out_br;
10137 +               }
10138 +       }
10139 +       fsnotify_open(h_dentry);
10140 +       goto out; /* success */
10141 +
10142 + out_br:
10143 +       atomic_dec(&br->br_count);
10144 + out:
10145 +       return h_file;
10146 +}
10147 +
10148 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags))
10149 +{
10150 +       int err;
10151 +       struct dentry *dentry;
10152 +       struct super_block *sb;
10153 +
10154 +       dentry = file->f_dentry;
10155 +       sb = dentry->d_sb;
10156 +       si_read_lock(sb, AuLock_FLUSH);
10157 +       err = au_finfo_init(file);
10158 +       if (unlikely(err))
10159 +               goto out;
10160 +
10161 +       di_read_lock_child(dentry, AuLock_IR);
10162 +       err = open(file, file->f_flags);
10163 +       di_read_unlock(dentry, AuLock_IR);
10164 +
10165 +       fi_write_unlock(file);
10166 +       if (unlikely(err))
10167 +               au_finfo_fin(file);
10168 + out:
10169 +       si_read_unlock(sb);
10170 +       return err;
10171 +}
10172 +
10173 +int au_reopen_nondir(struct file *file)
10174 +{
10175 +       int err;
10176 +       aufs_bindex_t bstart, bindex, bend;
10177 +       struct dentry *dentry;
10178 +       struct file *h_file, *h_file_tmp;
10179 +
10180 +       dentry = file->f_dentry;
10181 +       bstart = au_dbstart(dentry);
10182 +       h_file_tmp = NULL;
10183 +       if (au_fbstart(file) == bstart) {
10184 +               h_file = au_h_fptr(file, bstart);
10185 +               if (file->f_mode == h_file->f_mode)
10186 +                       return 0; /* success */
10187 +               h_file_tmp = h_file;
10188 +               get_file(h_file_tmp);
10189 +               au_set_h_fptr(file, bstart, NULL);
10190 +       }
10191 +       AuDebugOn(au_fbstart(file) < bstart
10192 +                 || au_fi(file)->fi_hfile[0 + bstart].hf_file);
10193 +
10194 +       h_file = au_h_open(dentry, bstart, file->f_flags & ~O_TRUNC, file);
10195 +       err = PTR_ERR(h_file);
10196 +       if (IS_ERR(h_file))
10197 +               goto out; /* todo: close all? */
10198 +
10199 +       err = 0;
10200 +       au_set_fbstart(file, bstart);
10201 +       au_set_h_fptr(file, bstart, h_file);
10202 +       au_update_figen(file);
10203 +       /* todo: necessary? */
10204 +       /* file->f_ra = h_file->f_ra; */
10205 +
10206 +       /* close lower files */
10207 +       bend = au_fbend(file);
10208 +       for (bindex = bstart + 1; bindex <= bend; bindex++)
10209 +               au_set_h_fptr(file, bindex, NULL);
10210 +       au_set_fbend(file, bstart);
10211 +
10212 + out:
10213 +       if (h_file_tmp)
10214 +               fput(h_file_tmp);
10215 +       return err;
10216 +}
10217 +
10218 +/* ---------------------------------------------------------------------- */
10219 +
10220 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
10221 +                       struct dentry *hi_wh)
10222 +{
10223 +       int err;
10224 +       aufs_bindex_t bstart;
10225 +       struct au_dinfo *dinfo;
10226 +       struct dentry *h_dentry;
10227 +
10228 +       dinfo = au_di(file->f_dentry);
10229 +       bstart = dinfo->di_bstart;
10230 +       dinfo->di_bstart = btgt;
10231 +       h_dentry = dinfo->di_hdentry[0 + btgt].hd_dentry;
10232 +       dinfo->di_hdentry[0 + btgt].hd_dentry = hi_wh;
10233 +       err = au_reopen_nondir(file);
10234 +       dinfo->di_hdentry[0 + btgt].hd_dentry = h_dentry;
10235 +       dinfo->di_bstart = bstart;
10236 +
10237 +       return err;
10238 +}
10239 +
10240 +static int au_ready_to_write_wh(struct file *file, loff_t len,
10241 +                               aufs_bindex_t bcpup)
10242 +{
10243 +       int err;
10244 +       struct inode *inode;
10245 +       struct dentry *dentry, *hi_wh;
10246 +       struct super_block *sb;
10247 +
10248 +       dentry = file->f_dentry;
10249 +       inode = dentry->d_inode;
10250 +       hi_wh = au_hi_wh(inode, bcpup);
10251 +       if (!hi_wh)
10252 +               err = au_sio_cpup_wh(dentry, bcpup, len, file);
10253 +       else
10254 +               /* already copied-up after unlink */
10255 +               err = au_reopen_wh(file, bcpup, hi_wh);
10256 +
10257 +       sb = dentry->d_sb;
10258 +       if (!err && inode->i_nlink > 1 && au_opt_test(au_mntflags(sb), PLINK))
10259 +               au_plink_append(inode, bcpup, au_h_dptr(dentry, bcpup));
10260 +
10261 +       return err;
10262 +}
10263 +
10264 +/*
10265 + * prepare the @file for writing.
10266 + */
10267 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
10268 +{
10269 +       int err;
10270 +       aufs_bindex_t bstart, bcpup;
10271 +       struct dentry *dentry, *parent, *h_dentry;
10272 +       struct inode *h_inode, *inode;
10273 +       struct super_block *sb;
10274 +
10275 +       dentry = file->f_dentry;
10276 +       sb = dentry->d_sb;
10277 +       bstart = au_fbstart(file);
10278 +       inode = dentry->d_inode;
10279 +       err = au_test_ro(sb, bstart, inode);
10280 +       if (!err && (au_h_fptr(file, bstart)->f_mode & FMODE_WRITE)) {
10281 +               err = au_pin(pin, dentry, bstart, AuOpt_UDBA_NONE, /*flags*/0);
10282 +               goto out;
10283 +       }
10284 +
10285 +       /* need to cpup */
10286 +       parent = dget_parent(dentry);
10287 +       di_write_lock_parent(parent);
10288 +       err = AuWbrCopyup(au_sbi(sb), dentry);
10289 +       bcpup = err;
10290 +       if (unlikely(err < 0))
10291 +               goto out_dgrade;
10292 +       err = 0;
10293 +
10294 +       if (!au_h_dptr(parent, bcpup)) {
10295 +               err = au_cpup_dirs(dentry, bcpup);
10296 +               if (unlikely(err))
10297 +                       goto out_dgrade;
10298 +       }
10299 +
10300 +       err = au_pin(pin, dentry, bcpup, AuOpt_UDBA_NONE,
10301 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10302 +       if (unlikely(err))
10303 +               goto out_dgrade;
10304 +
10305 +       h_dentry = au_h_fptr(file, bstart)->f_dentry;
10306 +       h_inode = h_dentry->d_inode;
10307 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
10308 +       if (d_unhashed(dentry) /* || d_unhashed(h_dentry) */
10309 +           /* || !h_inode->i_nlink */) {
10310 +               err = au_ready_to_write_wh(file, len, bcpup);
10311 +               di_downgrade_lock(parent, AuLock_IR);
10312 +       } else {
10313 +               di_downgrade_lock(parent, AuLock_IR);
10314 +               if (!au_h_dptr(dentry, bcpup))
10315 +                       err = au_sio_cpup_simple(dentry, bcpup, len,
10316 +                                                AuCpup_DTIME);
10317 +               if (!err)
10318 +                       err = au_reopen_nondir(file);
10319 +       }
10320 +       mutex_unlock(&h_inode->i_mutex);
10321 +
10322 +       if (!err) {
10323 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
10324 +               goto out_dput; /* success */
10325 +       }
10326 +       au_unpin(pin);
10327 +       goto out_unlock;
10328 +
10329 + out_dgrade:
10330 +       di_downgrade_lock(parent, AuLock_IR);
10331 + out_unlock:
10332 +       di_read_unlock(parent, AuLock_IR);
10333 + out_dput:
10334 +       dput(parent);
10335 + out:
10336 +       return err;
10337 +}
10338 +
10339 +/* ---------------------------------------------------------------------- */
10340 +
10341 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
10342 +{
10343 +       int err;
10344 +       aufs_bindex_t bstart;
10345 +       struct au_pin pin;
10346 +       struct au_finfo *finfo;
10347 +       struct dentry *dentry, *parent, *hi_wh;
10348 +       struct inode *inode;
10349 +       struct super_block *sb;
10350 +
10351 +       err = 0;
10352 +       finfo = au_fi(file);
10353 +       dentry = file->f_dentry;
10354 +       sb = dentry->d_sb;
10355 +       inode = dentry->d_inode;
10356 +       bstart = au_ibstart(inode);
10357 +       if (bstart == finfo->fi_bstart)
10358 +               goto out;
10359 +
10360 +       parent = dget_parent(dentry);
10361 +       if (au_test_ro(sb, bstart, inode)) {
10362 +               di_read_lock_parent(parent, !AuLock_IR);
10363 +               err = AuWbrCopyup(au_sbi(sb), dentry);
10364 +               bstart = err;
10365 +               di_read_unlock(parent, !AuLock_IR);
10366 +               if (unlikely(err < 0))
10367 +                       goto out_parent;
10368 +               err = 0;
10369 +       }
10370 +
10371 +       di_read_lock_parent(parent, AuLock_IR);
10372 +       hi_wh = au_hi_wh(inode, bstart);
10373 +       if (au_opt_test(au_mntflags(sb), PLINK)
10374 +           && au_plink_test(inode)
10375 +           && !d_unhashed(dentry)) {
10376 +               err = au_test_and_cpup_dirs(dentry, bstart);
10377 +               if (unlikely(err))
10378 +                       goto out_unlock;
10379 +
10380 +               /* always superio. */
10381 +               err = au_pin(&pin, dentry, bstart, AuOpt_UDBA_NONE,
10382 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10383 +               if (!err)
10384 +                       err = au_sio_cpup_simple(dentry, bstart, -1,
10385 +                                                AuCpup_DTIME);
10386 +               au_unpin(&pin);
10387 +       } else if (hi_wh) {
10388 +               /* already copied-up after unlink */
10389 +               err = au_reopen_wh(file, bstart, hi_wh);
10390 +               *need_reopen = 0;
10391 +       }
10392 +
10393 + out_unlock:
10394 +       di_read_unlock(parent, AuLock_IR);
10395 + out_parent:
10396 +       dput(parent);
10397 + out:
10398 +       return err;
10399 +}
10400 +
10401 +static void au_do_refresh_file(struct file *file)
10402 +{
10403 +       aufs_bindex_t bindex, bend, new_bindex, brid;
10404 +       struct au_hfile *p, tmp, *q;
10405 +       struct au_finfo *finfo;
10406 +       struct super_block *sb;
10407 +
10408 +       sb = file->f_dentry->d_sb;
10409 +       finfo = au_fi(file);
10410 +       p = finfo->fi_hfile + finfo->fi_bstart;
10411 +       brid = p->hf_br->br_id;
10412 +       bend = finfo->fi_bend;
10413 +       for (bindex = finfo->fi_bstart; bindex <= bend; bindex++, p++) {
10414 +               if (!p->hf_file)
10415 +                       continue;
10416 +
10417 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
10418 +               if (new_bindex == bindex)
10419 +                       continue;
10420 +               if (new_bindex < 0) {
10421 +                       au_set_h_fptr(file, bindex, NULL);
10422 +                       continue;
10423 +               }
10424 +
10425 +               /* swap two lower inode, and loop again */
10426 +               q = finfo->fi_hfile + new_bindex;
10427 +               tmp = *q;
10428 +               *q = *p;
10429 +               *p = tmp;
10430 +               if (tmp.hf_file) {
10431 +                       bindex--;
10432 +                       p--;
10433 +               }
10434 +       }
10435 +
10436 +       p = finfo->fi_hfile;
10437 +       if (!au_test_mmapped(file) && !d_unhashed(file->f_dentry)) {
10438 +               bend = au_sbend(sb);
10439 +               for (finfo->fi_bstart = 0; finfo->fi_bstart <= bend;
10440 +                    finfo->fi_bstart++, p++)
10441 +                       if (p->hf_file) {
10442 +                               if (p->hf_file->f_dentry
10443 +                                   && p->hf_file->f_dentry->d_inode)
10444 +                                       break;
10445 +                               else
10446 +                                       au_hfput(p, file);
10447 +                       }
10448 +       } else {
10449 +               bend = au_br_index(sb, brid);
10450 +               for (finfo->fi_bstart = 0; finfo->fi_bstart < bend;
10451 +                    finfo->fi_bstart++, p++)
10452 +                       if (p->hf_file)
10453 +                               au_hfput(p, file);
10454 +               bend = au_sbend(sb);
10455 +       }
10456 +
10457 +       p = finfo->fi_hfile + bend;
10458 +       for (finfo->fi_bend = bend; finfo->fi_bend >= finfo->fi_bstart;
10459 +            finfo->fi_bend--, p--)
10460 +               if (p->hf_file) {
10461 +                       if (p->hf_file->f_dentry
10462 +                           && p->hf_file->f_dentry->d_inode)
10463 +                               break;
10464 +                       else
10465 +                               au_hfput(p, file);
10466 +               }
10467 +       AuDebugOn(finfo->fi_bend < finfo->fi_bstart);
10468 +}
10469 +
10470 +/*
10471 + * after branch manipulating, refresh the file.
10472 + */
10473 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
10474 +{
10475 +       int err, need_reopen;
10476 +       struct dentry *dentry;
10477 +       aufs_bindex_t bend, bindex;
10478 +
10479 +       dentry = file->f_dentry;
10480 +       err = au_fi_realloc(au_fi(file), au_sbend(dentry->d_sb) + 1);
10481 +       if (unlikely(err))
10482 +               goto out;
10483 +       au_do_refresh_file(file);
10484 +
10485 +       err = 0;
10486 +       need_reopen = 1;
10487 +       if (!au_test_mmapped(file))
10488 +               err = au_file_refresh_by_inode(file, &need_reopen);
10489 +       if (!err && need_reopen && !d_unhashed(dentry))
10490 +               err = reopen(file);
10491 +       if (!err) {
10492 +               au_update_figen(file);
10493 +               return 0; /* success */
10494 +       }
10495 +
10496 +       /* error, close all lower files */
10497 +       bend = au_fbend(file);
10498 +       for (bindex = au_fbstart(file); bindex <= bend; bindex++)
10499 +               au_set_h_fptr(file, bindex, NULL);
10500 +
10501 + out:
10502 +       return err;
10503 +}
10504 +
10505 +/* common function to regular file and dir */
10506 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10507 +                         int wlock)
10508 +{
10509 +       int err;
10510 +       unsigned int sigen, figen;
10511 +       aufs_bindex_t bstart;
10512 +       unsigned char pseudo_link;
10513 +       struct dentry *dentry;
10514 +
10515 +       err = 0;
10516 +       dentry = file->f_dentry;
10517 +       sigen = au_sigen(dentry->d_sb);
10518 +       fi_write_lock(file);
10519 +       figen = au_figen(file);
10520 +       di_write_lock_child(dentry);
10521 +       bstart = au_dbstart(dentry);
10522 +       pseudo_link = (bstart != au_ibstart(dentry->d_inode));
10523 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
10524 +               if (!wlock) {
10525 +                       di_downgrade_lock(dentry, AuLock_IR);
10526 +                       fi_downgrade_lock(file);
10527 +               }
10528 +               goto out; /* success */
10529 +       }
10530 +
10531 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
10532 +       if (sigen != au_digen(dentry)
10533 +           || sigen != au_iigen(dentry->d_inode)) {
10534 +               err = au_reval_dpath(dentry, sigen);
10535 +               if (unlikely(err < 0))
10536 +                       goto out;
10537 +               AuDebugOn(au_digen(dentry) != sigen
10538 +                         || au_iigen(dentry->d_inode) != sigen);
10539 +       }
10540 +
10541 +       err = refresh_file(file, reopen);
10542 +       if (!err) {
10543 +               if (!wlock) {
10544 +                       di_downgrade_lock(dentry, AuLock_IR);
10545 +                       fi_downgrade_lock(file);
10546 +               }
10547 +       } else {
10548 +               di_write_unlock(dentry);
10549 +               fi_write_unlock(file);
10550 +       }
10551 +
10552 + out:
10553 +       return err;
10554 +}
10555 +
10556 +/* ---------------------------------------------------------------------- */
10557 +
10558 +/* cf. aufs_nopage() */
10559 +/* for madvise(2) */
10560 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
10561 +{
10562 +       unlock_page(page);
10563 +       return 0;
10564 +}
10565 +
10566 +/* they will never be called. */
10567 +#ifdef CONFIG_AUFS_DEBUG
10568 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
10569 +                           loff_t pos, unsigned len, unsigned flags,
10570 +                           struct page **pagep, void **fsdata)
10571 +{ AuUnsupport(); return 0; }
10572 +static int aufs_write_end(struct file *file, struct address_space *mapping,
10573 +                         loff_t pos, unsigned len, unsigned copied,
10574 +                         struct page *page, void *fsdata)
10575 +{ AuUnsupport(); return 0; }
10576 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
10577 +{ AuUnsupport(); return 0; }
10578 +static void aufs_sync_page(struct page *page)
10579 +{ AuUnsupport(); }
10580 +
10581 +static int aufs_set_page_dirty(struct page *page)
10582 +{ AuUnsupport(); return 0; }
10583 +static void aufs_invalidatepage(struct page *page, unsigned long offset)
10584 +{ AuUnsupport(); }
10585 +static int aufs_releasepage(struct page *page, gfp_t gfp)
10586 +{ AuUnsupport(); return 0; }
10587 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
10588 +                             const struct iovec *iov, loff_t offset,
10589 +                             unsigned long nr_segs)
10590 +{ AuUnsupport(); return 0; }
10591 +#endif /* CONFIG_AUFS_DEBUG */
10592 +
10593 +struct address_space_operations aufs_aop = {
10594 +       .readpage       = aufs_readpage,
10595 +#ifdef CONFIG_AUFS_DEBUG
10596 +       .writepage      = aufs_writepage,
10597 +       .sync_page      = aufs_sync_page,
10598 +       .set_page_dirty = aufs_set_page_dirty,
10599 +       .write_begin    = aufs_write_begin,
10600 +       .write_end      = aufs_write_end,
10601 +       .invalidatepage = aufs_invalidatepage,
10602 +       .releasepage    = aufs_releasepage,
10603 +       .direct_IO      = aufs_direct_IO,
10604 +#endif /* CONFIG_AUFS_DEBUG */
10605 +};
10606 diff --git a/fs/aufs/file.h b/fs/aufs/file.h
10607 new file mode 100644
10608 index 0000000..dd9ee61
10609 --- /dev/null
10610 +++ b/fs/aufs/file.h
10611 @@ -0,0 +1,148 @@
10612 +/*
10613 + * Copyright (C) 2005-2009 Junjiro R. Okajima
10614 + *
10615 + * This program, aufs is free software; you can redistribute it and/or modify
10616 + * it under the terms of the GNU General Public License as published by
10617 + * the Free Software Foundation; either version 2 of the License, or
10618 + * (at your option) any later version.
10619 + */
10620 +
10621 +/*
10622 + * file operations
10623 + */
10624 +
10625 +#ifndef __AUFS_FILE_H__
10626 +#define __AUFS_FILE_H__
10627 +
10628 +#ifdef __KERNEL__
10629 +
10630 +#include <linux/file.h>
10631 +#include <linux/fs.h>
10632 +#include <linux/aufs_type.h>
10633 +#include "rwsem.h"
10634 +
10635 +struct au_branch;
10636 +struct au_hfile {
10637 +       struct file             *hf_file;
10638 +       struct au_branch        *hf_br;
10639 +};
10640 +
10641 +struct au_vdir;
10642 +struct au_finfo {
10643 +       atomic_t                fi_generation;
10644 +
10645 +       struct rw_semaphore     fi_rwsem;
10646 +       struct au_hfile         *fi_hfile;
10647 +       aufs_bindex_t           fi_bstart, fi_bend;
10648 +
10649 +       union {
10650 +               /* non-dir only */
10651 +               struct vm_operations_struct     *fi_h_vm_ops;
10652 +
10653 +               /* dir only */
10654 +               struct {
10655 +                       struct au_vdir          *fi_vdir_cache;
10656 +                       int                     fi_maintain_plink;
10657 +               };
10658 +       };
10659 +};
10660 +
10661 +/* ---------------------------------------------------------------------- */
10662 +
10663 +/* file.c */
10664 +extern struct address_space_operations aufs_aop;
10665 +void au_store_oflag(struct nameidata *nd, struct inode *inode);
10666 +unsigned int au_file_roflags(unsigned int flags);
10667 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10668 +                      struct file *file);
10669 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags));
10670 +int au_reopen_nondir(struct file *file);
10671 +struct au_pin;
10672 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
10673 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10674 +                         int wlock);
10675 +
10676 +/* f_op.c */
10677 +extern const struct file_operations aufs_file_fop;
10678 +int aufs_flush(struct file *file, fl_owner_t id);
10679 +
10680 +/* finfo.c */
10681 +void au_hfput(struct au_hfile *hf, struct file *file);
10682 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
10683 +                  struct file *h_file);
10684 +
10685 +void au_update_figen(struct file *file);
10686 +
10687 +void au_finfo_fin(struct file *file);
10688 +int au_finfo_init(struct file *file);
10689 +int au_fi_realloc(struct au_finfo *finfo, int nbr);
10690 +
10691 +/* ---------------------------------------------------------------------- */
10692 +
10693 +static inline struct au_finfo *au_fi(struct file *file)
10694 +{
10695 +       return file->private_data;
10696 +}
10697 +
10698 +/* ---------------------------------------------------------------------- */
10699 +
10700 +/*
10701 + * fi_read_lock, fi_write_lock,
10702 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
10703 + */
10704 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
10705 +
10706 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
10707 +
10708 +/* ---------------------------------------------------------------------- */
10709 +
10710 +/* todo: hard/soft set? */
10711 +static inline aufs_bindex_t au_fbstart(struct file *file)
10712 +{
10713 +       return au_fi(file)->fi_bstart;
10714 +}
10715 +
10716 +static inline aufs_bindex_t au_fbend(struct file *file)
10717 +{
10718 +       return au_fi(file)->fi_bend;
10719 +}
10720 +
10721 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
10722 +{
10723 +       return au_fi(file)->fi_vdir_cache;
10724 +}
10725 +
10726 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
10727 +{
10728 +       au_fi(file)->fi_bstart = bindex;
10729 +}
10730 +
10731 +static inline void au_set_fbend(struct file *file, aufs_bindex_t bindex)
10732 +{
10733 +       au_fi(file)->fi_bend = bindex;
10734 +}
10735 +
10736 +static inline void au_set_fvdir_cache(struct file *file,
10737 +                                     struct au_vdir *vdir_cache)
10738 +{
10739 +       au_fi(file)->fi_vdir_cache = vdir_cache;
10740 +}
10741 +
10742 +static inline struct file *au_h_fptr(struct file *file, aufs_bindex_t bindex)
10743 +{
10744 +       return au_fi(file)->fi_hfile[0 + bindex].hf_file;
10745 +}
10746 +
10747 +/* todo: memory barrier? */
10748 +static inline unsigned int au_figen(struct file *f)
10749 +{
10750 +       return atomic_read(&au_fi(f)->fi_generation);
10751 +}
10752 +
10753 +static inline int au_test_mmapped(struct file *f)
10754 +{
10755 +       return !!(au_fi(f)->fi_h_vm_ops);
10756 +}
10757 +
10758 +#endif /* __KERNEL__ */
10759 +#endif /* __AUFS_FILE_H__ */
10760 diff --git a/fs/aufs/finfo.c b/fs/aufs/finfo.c
10761 new file mode 100644
10762 index 0000000..dfb4851
10763 --- /dev/null
10764 +++ b/fs/aufs/finfo.c
10765 @@ -0,0 +1,124 @@
10766 +/*
10767 + * Copyright (C) 2005-2009 Junjiro R. Okajima
10768 + *
10769 + * This program, aufs is free software; you can redistribute it and/or modify
10770 + * it under the terms of the GNU General Public License as published by
10771 + * the Free Software Foundation; either version 2 of the License, or
10772 + * (at your option) any later version.
10773 + */
10774 +
10775 +/*
10776 + * file private data
10777 + */
10778 +
10779 +#include "aufs.h"
10780 +
10781 +void au_hfput(struct au_hfile *hf, struct file *file)
10782 +{
10783 +       if (file->f_mode & FMODE_EXEC)
10784 +               allow_write_access(hf->hf_file);
10785 +       fput(hf->hf_file);
10786 +       hf->hf_file = NULL;
10787 +       atomic_dec(&hf->hf_br->br_count);
10788 +       hf->hf_br = NULL;
10789 +}
10790 +
10791 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
10792 +{
10793 +       struct au_finfo *finfo = au_fi(file);
10794 +       struct au_hfile *hf;
10795 +
10796 +       hf = finfo->fi_hfile + bindex;
10797 +       if (hf->hf_file)
10798 +               au_hfput(hf, file);
10799 +       if (val) {
10800 +               hf->hf_file = val;
10801 +               hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
10802 +       }
10803 +}
10804 +
10805 +void au_update_figen(struct file *file)
10806 +{
10807 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
10808 +       /* smp_mb(); */ /* atomic_set */
10809 +}
10810 +
10811 +/* ---------------------------------------------------------------------- */
10812 +
10813 +void au_finfo_fin(struct file *file)
10814 +{
10815 +       struct au_finfo *finfo;
10816 +       aufs_bindex_t bindex, bend;
10817 +
10818 +       fi_write_lock(file);
10819 +       bend = au_fbend(file);
10820 +       bindex = au_fbstart(file);
10821 +       if (bindex >= 0)
10822 +               /*
10823 +                * calls fput() instead of filp_close(),
10824 +                * since no dnotify or lock for the lower file.
10825 +                */
10826 +               for (; bindex <= bend; bindex++)
10827 +                       au_set_h_fptr(file, bindex, NULL);
10828 +
10829 +       finfo = au_fi(file);
10830 +       au_dbg_verify_hf(finfo);
10831 +       kfree(finfo->fi_hfile);
10832 +       fi_write_unlock(file);
10833 +       au_rwsem_destroy(&finfo->fi_rwsem);
10834 +       au_cache_free_finfo(finfo);
10835 +}
10836 +
10837 +int au_finfo_init(struct file *file)
10838 +{
10839 +       struct au_finfo *finfo;
10840 +       struct dentry *dentry;
10841 +       unsigned long ul;
10842 +
10843 +       dentry = file->f_dentry;
10844 +       finfo = au_cache_alloc_finfo();
10845 +       if (unlikely(!finfo))
10846 +               goto out;
10847 +
10848 +       finfo->fi_hfile = kcalloc(au_sbend(dentry->d_sb) + 1,
10849 +                                 sizeof(*finfo->fi_hfile), GFP_NOFS);
10850 +       if (unlikely(!finfo->fi_hfile))
10851 +               goto out_finfo;
10852 +
10853 +       init_rwsem(&finfo->fi_rwsem);
10854 +       down_write(&finfo->fi_rwsem);
10855 +       finfo->fi_bstart = -1;
10856 +       finfo->fi_bend = -1;
10857 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
10858 +       /* smp_mb(); */ /* atomic_set */
10859 +
10860 +       /* cf. au_store_oflag() */
10861 +       /* suppress a warning in lp64 */
10862 +       ul = (unsigned long)file->private_data;
10863 +       file->f_mode |= (vfsub_uint_to_fmode(ul) & FMODE_EXEC);
10864 +       file->private_data = finfo;
10865 +       return 0; /* success */
10866 +
10867 + out_finfo:
10868 +       au_cache_free_finfo(finfo);
10869 + out:
10870 +       return -ENOMEM;
10871 +}
10872 +
10873 +int au_fi_realloc(struct au_finfo *finfo, int nbr)
10874 +{
10875 +       int err, sz;
10876 +       struct au_hfile *hfp;
10877 +
10878 +       err = -ENOMEM;
10879 +       sz = sizeof(*hfp) * (finfo->fi_bend + 1);
10880 +       if (!sz)
10881 +               sz = sizeof(*hfp);
10882 +       hfp = au_kzrealloc(finfo->fi_hfile, sz, sizeof(*hfp) * nbr, GFP_NOFS);
10883 +       if (hfp) {
10884 +               finfo->fi_hfile = hfp;
10885 +               err = 0;
10886 +       }
10887 +
10888 +       return err;
10889 +}
10890 diff --git a/fs/aufs/fstype.h b/fs/aufs/fstype.h
10891 new file mode 100644
10892 index 0000000..54896c6
10893 --- /dev/null
10894 +++ b/fs/aufs/fstype.h
10895 @@ -0,0 +1,464 @@
10896 +/*
10897 + * Copyright (C) 2005-2009 Junjiro R. Okajima
10898 + *
10899 + * This program, aufs is free software; you can redistribute it and/or modify
10900 + * it under the terms of the GNU General Public License as published by
10901 + * the Free Software Foundation; either version 2 of the License, or
10902 + * (at your option) any later version.
10903 + */
10904 +
10905 +/*
10906 + * judging filesystem type
10907 + */
10908 +
10909 +#ifndef __AUFS_FSTYPE_H__
10910 +#define __AUFS_FSTYPE_H__
10911 +
10912 +#ifdef __KERNEL__
10913 +
10914 +#include <linux/fs.h>
10915 +#include <linux/cramfs_fs.h>
10916 +#include <linux/magic.h>
10917 +#include <linux/romfs_fs.h>
10918 +#include <linux/aufs_type.h>
10919 +
10920 +static inline int au_test_aufs(struct super_block *sb)
10921 +{
10922 +       return sb->s_magic == AUFS_SUPER_MAGIC;
10923 +}
10924 +
10925 +static inline const char *au_sbtype(struct super_block *sb)
10926 +{
10927 +       return sb->s_type->name;
10928 +}
10929 +
10930 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
10931 +{
10932 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
10933 +       return sb->s_magic == ROMFS_MAGIC;
10934 +#else
10935 +       return 0;
10936 +#endif
10937 +}
10938 +
10939 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
10940 +{
10941 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
10942 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
10943 +#else
10944 +       return 0;
10945 +#endif
10946 +}
10947 +
10948 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
10949 +{
10950 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
10951 +       return sb->s_magic == CRAMFS_MAGIC;
10952 +#endif
10953 +       return 0;
10954 +}
10955 +
10956 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
10957 +{
10958 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
10959 +       return sb->s_magic == NFS_SUPER_MAGIC;
10960 +#else
10961 +       return 0;
10962 +#endif
10963 +}
10964 +
10965 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
10966 +{
10967 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
10968 +       return sb->s_magic == FUSE_SUPER_MAGIC;
10969 +#else
10970 +       return 0;
10971 +#endif
10972 +}
10973 +
10974 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
10975 +{
10976 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
10977 +       return sb->s_magic == XFS_SB_MAGIC;
10978 +#else
10979 +       return 0;
10980 +#endif
10981 +}
10982 +
10983 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
10984 +{
10985 +#ifdef CONFIG_TMPFS
10986 +       return sb->s_magic == TMPFS_MAGIC;
10987 +#else
10988 +       return 0;
10989 +#endif
10990 +}
10991 +
10992 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
10993 +{
10994 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
10995 +       return !strcmp(au_sbtype(sb), "ecryptfs");
10996 +#else
10997 +       return 0;
10998 +#endif
10999 +}
11000 +
11001 +static inline int au_test_smbfs(struct super_block *sb __maybe_unused)
11002 +{
11003 +#if defined(CONFIG_SMB_FS) || defined(CONFIG_SMB_FS_MODULE)
11004 +       return sb->s_magic == SMB_SUPER_MAGIC;
11005 +#else
11006 +       return 0;
11007 +#endif
11008 +}
11009 +
11010 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
11011 +{
11012 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
11013 +       return sb->s_magic == OCFS2_SUPER_MAGIC;
11014 +#else
11015 +       return 0;
11016 +#endif
11017 +}
11018 +
11019 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
11020 +{
11021 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
11022 +       return sb->s_magic == DLMFS_MAGIC;
11023 +#else
11024 +       return 0;
11025 +#endif
11026 +}
11027 +
11028 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
11029 +{
11030 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
11031 +       return sb->s_magic == CODA_SUPER_MAGIC;
11032 +#else
11033 +       return 0;
11034 +#endif
11035 +}
11036 +
11037 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
11038 +{
11039 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
11040 +       return sb->s_magic == V9FS_MAGIC;
11041 +#else
11042 +       return 0;
11043 +#endif
11044 +}
11045 +
11046 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
11047 +{
11048 +#if defined(CONFIG_EXT4DEV_FS) || defined(CONFIG_EXT4DEV_FS_MODULE)
11049 +       return sb->s_magic == EXT4_SUPER_MAGIC;
11050 +#else
11051 +       return 0;
11052 +#endif
11053 +}
11054 +
11055 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
11056 +{
11057 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
11058 +       return !strcmp(au_sbtype(sb), "sysv");
11059 +#else
11060 +       return 0;
11061 +#endif
11062 +}
11063 +
11064 +static inline int au_test_ramfs(struct super_block *sb)
11065 +{
11066 +       return sb->s_magic == RAMFS_MAGIC;
11067 +}
11068 +
11069 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
11070 +{
11071 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
11072 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
11073 +#else
11074 +       return 0;
11075 +#endif
11076 +}
11077 +
11078 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
11079 +{
11080 +#ifdef CONFIG_PROC_FS
11081 +       return sb->s_magic == PROC_SUPER_MAGIC;
11082 +#else
11083 +       return 0;
11084 +#endif
11085 +}
11086 +
11087 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
11088 +{
11089 +#ifdef CONFIG_SYSFS
11090 +       return sb->s_magic == SYSFS_MAGIC;
11091 +#else
11092 +       return 0;
11093 +#endif
11094 +}
11095 +
11096 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
11097 +{
11098 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
11099 +       return sb->s_magic == CONFIGFS_MAGIC;
11100 +#else
11101 +       return 0;
11102 +#endif
11103 +}
11104 +
11105 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
11106 +{
11107 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
11108 +       return sb->s_magic == MINIX3_SUPER_MAGIC
11109 +               || sb->s_magic == MINIX2_SUPER_MAGIC
11110 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
11111 +               || sb->s_magic == MINIX_SUPER_MAGIC
11112 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
11113 +#else
11114 +       return 0;
11115 +#endif
11116 +}
11117 +
11118 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
11119 +{
11120 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
11121 +       return sb->s_magic == CIFS_MAGIC_NUMBER;
11122 +#else
11123 +       return 0;
11124 +#endif
11125 +}
11126 +
11127 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
11128 +{
11129 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
11130 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
11131 +#else
11132 +       return 0;
11133 +#endif
11134 +}
11135 +
11136 +static inline int au_test_msdos(struct super_block *sb)
11137 +{
11138 +       return au_test_fat(sb);
11139 +}
11140 +
11141 +static inline int au_test_vfat(struct super_block *sb)
11142 +{
11143 +       return au_test_fat(sb);
11144 +}
11145 +
11146 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
11147 +{
11148 +#ifdef CONFIG_SECURITYFS
11149 +       return sb->s_magic == SECURITYFS_MAGIC;
11150 +#else
11151 +       return 0;
11152 +#endif
11153 +}
11154 +
11155 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
11156 +{
11157 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
11158 +       return sb->s_magic == SQUASHFS_MAGIC;
11159 +#else
11160 +       return 0;
11161 +#endif
11162 +}
11163 +
11164 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
11165 +{
11166 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
11167 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
11168 +#else
11169 +       return 0;
11170 +#endif
11171 +}
11172 +
11173 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
11174 +{
11175 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
11176 +       return sb->s_magic == XENFS_SUPER_MAGIC;
11177 +#else
11178 +       return 0;
11179 +#endif
11180 +}
11181 +
11182 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
11183 +{
11184 +#ifdef CONFIG_DEBUG_FS
11185 +       return sb->s_magic == DEBUGFS_MAGIC;
11186 +#else
11187 +       return 0;
11188 +#endif
11189 +}
11190 +
11191 +/* ---------------------------------------------------------------------- */
11192 +/*
11193 + * they can't be an aufs branch.
11194 + */
11195 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
11196 +{
11197 +       return
11198 +#ifndef CONFIG_AUFS_BR_RAMFS
11199 +               au_test_ramfs(sb) ||
11200 +#endif
11201 +               au_test_procfs(sb)
11202 +               || au_test_sysfs(sb)
11203 +               || au_test_configfs(sb)
11204 +               || au_test_debugfs(sb)
11205 +               || au_test_securityfs(sb)
11206 +               || au_test_xenfs(sb)
11207 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
11208 +               || au_test_aufs(sb); /* will be supported in next version */
11209 +}
11210 +
11211 +/*
11212 + * If the filesystem supports NFS-export, then it has to support NULL as
11213 + * a nameidata parameter for ->create(), ->lookup() and ->d_revalidate().
11214 + * We can apply this principle when we handle a lower filesystem.
11215 + */
11216 +static inline int au_test_fs_null_nd(struct super_block *sb)
11217 +{
11218 +       return !!sb->s_export_op;
11219 +}
11220 +
11221 +static inline int au_test_fs_remote(struct super_block *sb)
11222 +{
11223 +       return !au_test_tmpfs(sb)
11224 +#ifdef CONFIG_AUFS_BR_RAMFS
11225 +               && !au_test_ramfs(sb)
11226 +#endif
11227 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
11228 +}
11229 +
11230 +/* ---------------------------------------------------------------------- */
11231 +
11232 +/*
11233 + * Note: these functions (below) are created after reading ->getattr() in all
11234 + * filesystems under linux/fs. it means we have to do so in every update...
11235 + */
11236 +
11237 +/*
11238 + * some filesystems require getattr to refresh the inode attributes before
11239 + * referencing.
11240 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
11241 + * and leave the work for d_revalidate()
11242 + */
11243 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
11244 +{
11245 +       return au_test_nfs(sb)
11246 +               || au_test_fuse(sb)
11247 +               /* || au_test_smbfs(sb) */      /* untested */
11248 +               /* || au_test_ocfs2(sb) */      /* untested */
11249 +               /* || au_test_btrfs(sb) */      /* untested */
11250 +               /* || au_test_coda(sb) */       /* untested */
11251 +               /* || au_test_v9fs(sb) */       /* untested */
11252 +               ;
11253 +}
11254 +
11255 +/*
11256 + * filesystems which don't maintain i_size or i_blocks.
11257 + */
11258 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
11259 +{
11260 +       return au_test_xfs(sb)
11261 +               /* || au_test_ext4(sb) */       /* untested */
11262 +               /* || au_test_ocfs2(sb) */      /* untested */
11263 +               /* || au_test_ocfs2_dlmfs(sb) */ /* untested */
11264 +               /* || au_test_sysv(sb) */       /* untested */
11265 +               /* || au_test_ubifs(sb) */      /* untested */
11266 +               /* || au_test_minix(sb) */      /* untested */
11267 +               ;
11268 +}
11269 +
11270 +/*
11271 + * filesystems which don't store the correct value in some of their inode
11272 + * attributes.
11273 + */
11274 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
11275 +{
11276 +       return au_test_fs_bad_iattr_size(sb)
11277 +               /* || au_test_cifs(sb) */       /* untested */
11278 +               || au_test_fat(sb)
11279 +               || au_test_msdos(sb)
11280 +               || au_test_vfat(sb);
11281 +}
11282 +
11283 +/* they don't check i_nlink in link(2) */
11284 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
11285 +{
11286 +       return au_test_tmpfs(sb)
11287 +#ifdef CONFIG_AUFS_BR_RAMFS
11288 +               || au_test_ramfs(sb)
11289 +#endif
11290 +               ;
11291 +}
11292 +
11293 +/*
11294 + * filesystems which sets S_NOATIME and S_NOCMTIME.
11295 + */
11296 +static inline int au_test_fs_notime(struct super_block *sb)
11297 +{
11298 +       return au_test_nfs(sb)
11299 +               || au_test_fuse(sb)
11300 +               /* || au_test_cifs(sb) */       /* untested */
11301 +               /* || au_test_ubifs(sb) */      /* untested */
11302 +               ;
11303 +}
11304 +
11305 +/*
11306 + * filesystems which requires replacing i_mapping.
11307 + */
11308 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
11309 +{
11310 +       return au_test_fuse(sb);
11311 +}
11312 +
11313 +/* temporary support for i#1 in cramfs */
11314 +static inline int au_test_fs_unique_ino(struct inode *inode)
11315 +{
11316 +       if (au_test_cramfs(inode->i_sb))
11317 +               return inode->i_ino != 1;
11318 +       return 1;
11319 +}
11320 +
11321 +/* ---------------------------------------------------------------------- */
11322 +
11323 +/*
11324 + * the filesystem where the xino files placed must support i/o after unlink and
11325 + * maintain i_size and i_blocks.
11326 + */
11327 +static inline int au_test_fs_bad_xino(struct super_block *sb)
11328 +{
11329 +       return au_test_fs_remote(sb)
11330 +               || au_test_fs_bad_iattr_size(sb)
11331 +#ifdef CONFIG_AUFS_BR_RAMFS
11332 +               || !(au_test_ramfs(sb) || au_test_fs_null_nd(sb))
11333 +#else
11334 +               || !au_test_fs_null_nd(sb) /* to keep xino code simple */
11335 +#endif
11336 +               /* don't want unnecessary work for xino */
11337 +               || au_test_aufs(sb)
11338 +               || au_test_ecryptfs(sb);
11339 +}
11340 +
11341 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
11342 +{
11343 +       return au_test_tmpfs(sb)
11344 +               || au_test_ramfs(sb);
11345 +}
11346 +
11347 +/*
11348 + * test if the @sb is real-readonly.
11349 + */
11350 +static inline int au_test_fs_rr(struct super_block *sb)
11351 +{
11352 +       return au_test_squashfs(sb)
11353 +               || au_test_iso9660(sb)
11354 +               || au_test_cramfs(sb)
11355 +               || au_test_romfs(sb);
11356 +}
11357 +
11358 +#endif /* __KERNEL__ */
11359 +#endif /* __AUFS_FSTYPE_H__ */
11360 diff --git a/fs/aufs/hinotify.c b/fs/aufs/hinotify.c
11361 new file mode 100644
11362 index 0000000..af8ebdb
11363 --- /dev/null
11364 +++ b/fs/aufs/hinotify.c
11365 @@ -0,0 +1,746 @@
11366 +/*
11367 + * Copyright (C) 2005-2009 Junjiro R. Okajima
11368 + *
11369 + * This program, aufs is free software; you can redistribute it and/or modify
11370 + * it under the terms of the GNU General Public License as published by
11371 + * the Free Software Foundation; either version 2 of the License, or
11372 + * (at your option) any later version.
11373 + */
11374 +
11375 +/*
11376 + * inotify for the lower directories
11377 + */
11378 +
11379 +#include "aufs.h"
11380 +
11381 +static const __u32 AuHinMask = (IN_MOVE | IN_DELETE | IN_CREATE);
11382 +static struct inotify_handle *au_hin_handle;
11383 +
11384 +AuCacheFuncs(hinotify, HINOTIFY);
11385 +
11386 +int au_hin_alloc(struct au_hinode *hinode, struct inode *inode,
11387 +                struct inode *h_inode)
11388 +{
11389 +       int err;
11390 +       struct au_hinotify *hin;
11391 +       s32 wd;
11392 +
11393 +       err = -ENOMEM;
11394 +       hin = au_cache_alloc_hinotify();
11395 +       if (hin) {
11396 +               AuDebugOn(hinode->hi_notify);
11397 +               hinode->hi_notify = hin;
11398 +               hin->hin_aufs_inode = inode;
11399 +
11400 +               inotify_init_watch(&hin->hin_watch);
11401 +               wd = inotify_add_watch(au_hin_handle, &hin->hin_watch, h_inode,
11402 +                                      AuHinMask);
11403 +               if (wd >= 0)
11404 +                       return 0; /* success */
11405 +
11406 +               err = wd;
11407 +               put_inotify_watch(&hin->hin_watch);
11408 +               au_cache_free_hinotify(hin);
11409 +               hinode->hi_notify = NULL;
11410 +       }
11411 +
11412 +       return err;
11413 +}
11414 +
11415 +void au_hin_free(struct au_hinode *hinode)
11416 +{
11417 +       int err;
11418 +       struct au_hinotify *hin;
11419 +
11420 +       hin = hinode->hi_notify;
11421 +       if (hin) {
11422 +               err = 0;
11423 +               if (atomic_read(&hin->hin_watch.count))
11424 +                       err = inotify_rm_watch(au_hin_handle, &hin->hin_watch);
11425 +               if (unlikely(err))
11426 +                       /* it means the watch is already removed */
11427 +                       AuWarn("failed inotify_rm_watch() %d\n", err);
11428 +               au_cache_free_hinotify(hin);
11429 +               hinode->hi_notify = NULL;
11430 +       }
11431 +}
11432 +
11433 +/* ---------------------------------------------------------------------- */
11434 +
11435 +void au_hin_ctl(struct au_hinode *hinode, int do_set)
11436 +{
11437 +       struct inode *h_inode;
11438 +       struct inotify_watch *watch;
11439 +
11440 +       if (!hinode->hi_notify)
11441 +               return;
11442 +
11443 +       h_inode = hinode->hi_inode;
11444 +       IMustLock(h_inode);
11445 +
11446 +       /* todo: try inotify_find_update_watch()? */
11447 +       watch = &hinode->hi_notify->hin_watch;
11448 +       mutex_lock(&h_inode->inotify_mutex);
11449 +       /* mutex_lock(&watch->ih->mutex); */
11450 +       if (do_set) {
11451 +               AuDebugOn(watch->mask & AuHinMask);
11452 +               watch->mask |= AuHinMask;
11453 +       } else {
11454 +               AuDebugOn(!(watch->mask & AuHinMask));
11455 +               watch->mask &= ~AuHinMask;
11456 +       }
11457 +       /* mutex_unlock(&watch->ih->mutex); */
11458 +       mutex_unlock(&h_inode->inotify_mutex);
11459 +}
11460 +
11461 +void au_reset_hinotify(struct inode *inode, unsigned int flags)
11462 +{
11463 +       aufs_bindex_t bindex, bend;
11464 +       struct inode *hi;
11465 +       struct dentry *iwhdentry;
11466 +
11467 +       bend = au_ibend(inode);
11468 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
11469 +               hi = au_h_iptr(inode, bindex);
11470 +               if (!hi)
11471 +                       continue;
11472 +
11473 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
11474 +               iwhdentry = au_hi_wh(inode, bindex);
11475 +               if (iwhdentry)
11476 +                       dget(iwhdentry);
11477 +               au_igrab(hi);
11478 +               au_set_h_iptr(inode, bindex, NULL, 0);
11479 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
11480 +                             flags & ~AuHi_XINO);
11481 +               iput(hi);
11482 +               dput(iwhdentry);
11483 +               /* mutex_unlock(&hi->i_mutex); */
11484 +       }
11485 +}
11486 +
11487 +/* ---------------------------------------------------------------------- */
11488 +
11489 +static int hin_xino(struct inode *inode, struct inode *h_inode)
11490 +{
11491 +       int err;
11492 +       aufs_bindex_t bindex, bend, bfound, bstart;
11493 +       struct inode *h_i;
11494 +
11495 +       err = 0;
11496 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
11497 +               AuWarn("branch root dir was changed\n");
11498 +               goto out;
11499 +       }
11500 +
11501 +       bfound = -1;
11502 +       bend = au_ibend(inode);
11503 +       bstart = au_ibstart(inode);
11504 +#if 0 /* reserved for future use */
11505 +       if (bindex == bend) {
11506 +               /* keep this ino in rename case */
11507 +               goto out;
11508 +       }
11509 +#endif
11510 +       for (bindex = bstart; bindex <= bend; bindex++) {
11511 +               if (au_h_iptr(inode, bindex) == h_inode) {
11512 +                       bfound = bindex;
11513 +                       break;
11514 +               }
11515 +       }
11516 +       if (bfound < 0)
11517 +               goto out;
11518 +
11519 +       for (bindex = bstart; bindex <= bend; bindex++) {
11520 +               h_i = au_h_iptr(inode, bindex);
11521 +               if (!h_i)
11522 +                       continue;
11523 +
11524 +               err = au_xino_write0(inode->i_sb, bindex, h_i->i_ino, 0);
11525 +               /* ignore this error */
11526 +               /* bad action? */
11527 +       }
11528 +
11529 +       /* children inode number will be broken */
11530 +
11531 + out:
11532 +       AuTraceErr(err);
11533 +       return err;
11534 +}
11535 +
11536 +static int hin_gen_tree(struct dentry *dentry)
11537 +{
11538 +       int err, i, j, ndentry;
11539 +       struct au_dcsub_pages dpages;
11540 +       struct au_dpage *dpage;
11541 +       struct dentry **dentries;
11542 +
11543 +       err = au_dpages_init(&dpages, GFP_NOFS);
11544 +       if (unlikely(err))
11545 +               goto out;
11546 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
11547 +       if (unlikely(err))
11548 +               goto out_dpages;
11549 +
11550 +       for (i = 0; i < dpages.ndpage; i++) {
11551 +               dpage = dpages.dpages + i;
11552 +               dentries = dpage->dentries;
11553 +               ndentry = dpage->ndentry;
11554 +               for (j = 0; j < ndentry; j++) {
11555 +                       struct dentry *d;
11556 +
11557 +                       d = dentries[j];
11558 +                       if (IS_ROOT(d))
11559 +                               continue;
11560 +
11561 +                       d_drop(d);
11562 +                       au_digen_dec(d);
11563 +                       if (d->d_inode)
11564 +                               /* todo: reset children xino?
11565 +                                  cached children only? */
11566 +                               au_iigen_dec(d->d_inode);
11567 +               }
11568 +       }
11569 +
11570 + out_dpages:
11571 +       au_dpages_free(&dpages);
11572 +
11573 +       /* discard children */
11574 +       dentry_unhash(dentry);
11575 +       dput(dentry);
11576 + out:
11577 +       return err;
11578 +}
11579 +
11580 +/*
11581 + * return 0 if processed.
11582 + */
11583 +static int hin_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
11584 +                           const unsigned int isdir)
11585 +{
11586 +       int err;
11587 +       struct dentry *d;
11588 +       struct qstr *dname;
11589 +
11590 +       err = 1;
11591 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
11592 +               AuWarn("branch root dir was changed\n");
11593 +               err = 0;
11594 +               goto out;
11595 +       }
11596 +
11597 +       if (!isdir) {
11598 +               AuDebugOn(!name);
11599 +               au_iigen_dec(inode);
11600 +               spin_lock(&dcache_lock);
11601 +               list_for_each_entry(d, &inode->i_dentry, d_alias) {
11602 +                       dname = &d->d_name;
11603 +                       if (dname->len != nlen
11604 +                           && memcmp(dname->name, name, nlen))
11605 +                               continue;
11606 +                       err = 0;
11607 +                       spin_lock(&d->d_lock);
11608 +                       __d_drop(d);
11609 +                       au_digen_dec(d);
11610 +                       spin_unlock(&d->d_lock);
11611 +                       break;
11612 +               }
11613 +               spin_unlock(&dcache_lock);
11614 +       } else {
11615 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIRS);
11616 +               d = d_find_alias(inode);
11617 +               if (!d) {
11618 +                       au_iigen_dec(inode);
11619 +                       goto out;
11620 +               }
11621 +
11622 +               dname = &d->d_name;
11623 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen))
11624 +                       err = hin_gen_tree(d);
11625 +               dput(d);
11626 +       }
11627 +
11628 + out:
11629 +       AuTraceErr(err);
11630 +       return err;
11631 +}
11632 +
11633 +static int hin_gen_by_name(struct dentry *dentry, const unsigned int isdir)
11634 +{
11635 +       int err;
11636 +       struct inode *inode;
11637 +
11638 +       inode = dentry->d_inode;
11639 +       if (IS_ROOT(dentry)
11640 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
11641 +               ) {
11642 +               AuWarn("branch root dir was changed\n");
11643 +               return 0;
11644 +       }
11645 +
11646 +       err = 0;
11647 +       if (!isdir) {
11648 +               d_drop(dentry);
11649 +               au_digen_dec(dentry);
11650 +               if (inode)
11651 +                       au_iigen_dec(inode);
11652 +       } else {
11653 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIRS);
11654 +               if (inode)
11655 +                       err = hin_gen_tree(dentry);
11656 +       }
11657 +
11658 +       AuTraceErr(err);
11659 +       return err;
11660 +}
11661 +
11662 +/* ---------------------------------------------------------------------- */
11663 +
11664 +/* hinotify job flags */
11665 +#define AuHinJob_XINO0         1
11666 +#define AuHinJob_GEN           (1 << 1)
11667 +#define AuHinJob_DIRENT                (1 << 2)
11668 +#define AuHinJob_ISDIR         (1 << 3)
11669 +#define AuHinJob_TRYXINO0      (1 << 4)
11670 +#define AuHinJob_MNTPNT                (1 << 5)
11671 +#define au_ftest_hinjob(flags, name)   ((flags) & AuHinJob_##name)
11672 +#define au_fset_hinjob(flags, name)    { (flags) |= AuHinJob_##name; }
11673 +#define au_fclr_hinjob(flags, name)    { (flags) &= ~AuHinJob_##name; }
11674 +
11675 +struct hin_job_args {
11676 +       unsigned int flags;
11677 +       struct inode *inode, *h_inode, *dir, *h_dir;
11678 +       struct dentry *dentry;
11679 +       char *h_name;
11680 +       int h_nlen;
11681 +};
11682 +
11683 +static int hin_job(struct hin_job_args *a)
11684 +{
11685 +       const unsigned int isdir = au_ftest_hinjob(a->flags, ISDIR);
11686 +
11687 +       /* reset xino */
11688 +       if (au_ftest_hinjob(a->flags, XINO0) && a->inode)
11689 +               hin_xino(a->inode, a->h_inode); /* ignore this error */
11690 +
11691 +       if (au_ftest_hinjob(a->flags, TRYXINO0)
11692 +           && a->inode
11693 +           && a->h_inode) {
11694 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
11695 +               if (!a->h_inode->i_nlink)
11696 +                       hin_xino(a->inode, a->h_inode); /* ignore this error */
11697 +               mutex_unlock(&a->h_inode->i_mutex);
11698 +       }
11699 +
11700 +       /* make the generation obsolete */
11701 +       if (au_ftest_hinjob(a->flags, GEN)) {
11702 +               int err = -1;
11703 +               if (a->inode)
11704 +                       err = hin_gen_by_inode(a->h_name, a->h_nlen, a->inode,
11705 +                                              isdir);
11706 +               if (err && a->dentry)
11707 +                       hin_gen_by_name(a->dentry, isdir);
11708 +               /* ignore this error */
11709 +       }
11710 +
11711 +       /* make dir entries obsolete */
11712 +       if (au_ftest_hinjob(a->flags, DIRENT) && a->inode) {
11713 +               struct au_vdir *vdir;
11714 +
11715 +               vdir = au_ivdir(a->inode);
11716 +               if (vdir)
11717 +                       vdir->vd_jiffy = 0;
11718 +               /* IMustLock(a->inode); */
11719 +               /* a->inode->i_version++; */
11720 +       }
11721 +
11722 +       /* can do nothing but warn */
11723 +       if (au_ftest_hinjob(a->flags, MNTPNT)
11724 +           && a->dentry
11725 +           && d_mountpoint(a->dentry))
11726 +               AuWarn("mount-point %.*s is removed or renamed\n",
11727 +                      AuDLNPair(a->dentry));
11728 +
11729 +       return 0;
11730 +}
11731 +
11732 +/* ---------------------------------------------------------------------- */
11733 +
11734 +static char *in_name(u32 mask)
11735 +{
11736 +#ifdef CONFIG_AUFS_DEBUG
11737 +#define test_ret(flag) if (mask & flag) \
11738 +                               return #flag;
11739 +       test_ret(IN_ACCESS);
11740 +       test_ret(IN_MODIFY);
11741 +       test_ret(IN_ATTRIB);
11742 +       test_ret(IN_CLOSE_WRITE);
11743 +       test_ret(IN_CLOSE_NOWRITE);
11744 +       test_ret(IN_OPEN);
11745 +       test_ret(IN_MOVED_FROM);
11746 +       test_ret(IN_MOVED_TO);
11747 +       test_ret(IN_CREATE);
11748 +       test_ret(IN_DELETE);
11749 +       test_ret(IN_DELETE_SELF);
11750 +       test_ret(IN_MOVE_SELF);
11751 +       test_ret(IN_UNMOUNT);
11752 +       test_ret(IN_Q_OVERFLOW);
11753 +       test_ret(IN_IGNORED);
11754 +       return "";
11755 +#undef test_ret
11756 +#else
11757 +       return "??";
11758 +#endif
11759 +}
11760 +
11761 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
11762 +                                          struct inode *dir)
11763 +{
11764 +       struct dentry *dentry, *d, *parent;
11765 +       struct qstr *dname;
11766 +
11767 +       parent = d_find_alias(dir);
11768 +       if (!parent)
11769 +               return NULL;
11770 +
11771 +       dentry = NULL;
11772 +       spin_lock(&dcache_lock);
11773 +       list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
11774 +               /* AuDbg("%.*s\n", AuDLNPair(d)); */
11775 +               dname = &d->d_name;
11776 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
11777 +                       continue;
11778 +               if (!atomic_read(&d->d_count) || !d->d_fsdata) {
11779 +                       spin_lock(&d->d_lock);
11780 +                       __d_drop(d);
11781 +                       spin_unlock(&d->d_lock);
11782 +                       continue;
11783 +               }
11784 +
11785 +               dentry = dget(d);
11786 +               break;
11787 +       }
11788 +       spin_unlock(&dcache_lock);
11789 +       dput(parent);
11790 +
11791 +       if (dentry)
11792 +               di_write_lock_child(dentry);
11793 +
11794 +       return dentry;
11795 +}
11796 +
11797 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
11798 +                                        aufs_bindex_t bindex, ino_t h_ino)
11799 +{
11800 +       struct inode *inode;
11801 +       ino_t ino;
11802 +       int err;
11803 +
11804 +       inode = NULL;
11805 +       err = au_xino_read(sb, bindex, h_ino, &ino);
11806 +       if (!err && ino)
11807 +               inode = ilookup(sb, ino);
11808 +       if (!inode)
11809 +               goto out;
11810 +
11811 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
11812 +               AuWarn("wrong root branch\n");
11813 +               iput(inode);
11814 +               inode = NULL;
11815 +               goto out;
11816 +       }
11817 +
11818 +       ii_write_lock_child(inode);
11819 +
11820 + out:
11821 +       return inode;
11822 +}
11823 +
11824 +enum { CHILD, PARENT };
11825 +struct postproc_args {
11826 +       struct inode *h_dir, *dir, *h_child_inode;
11827 +       u32 mask;
11828 +       unsigned int flags[2];
11829 +       unsigned int h_child_nlen;
11830 +       char h_child_name[];
11831 +};
11832 +
11833 +static void postproc(void *_args)
11834 +{
11835 +       struct postproc_args *a = _args;
11836 +       struct super_block *sb;
11837 +       aufs_bindex_t bindex, bend, bfound;
11838 +       unsigned char xino, try_iput;
11839 +       int err;
11840 +       struct inode *inode;
11841 +       ino_t h_ino;
11842 +       struct hin_job_args args;
11843 +       struct dentry *dentry;
11844 +       struct au_sbinfo *sbinfo;
11845 +
11846 +       AuDebugOn(!_args);
11847 +       AuDebugOn(!a->h_dir);
11848 +       AuDebugOn(!a->dir);
11849 +       AuDebugOn(!a->mask);
11850 +       AuDbg("mask 0x%x %s, i%lu, hi%lu, hci%lu\n",
11851 +             a->mask, in_name(a->mask), a->dir->i_ino, a->h_dir->i_ino,
11852 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
11853 +
11854 +       inode = NULL;
11855 +       dentry = NULL;
11856 +       /*
11857 +        * do not lock a->dir->i_mutex here
11858 +        * because of d_revalidate() may cause a deadlock.
11859 +        */
11860 +       sb = a->dir->i_sb;
11861 +       AuDebugOn(!sb);
11862 +       sbinfo = au_sbi(sb);
11863 +       AuDebugOn(!sbinfo);
11864 +       /* big aufs lock */
11865 +       si_noflush_write_lock(sb);
11866 +
11867 +       ii_read_lock_parent(a->dir);
11868 +       bfound = -1;
11869 +       bend = au_ibend(a->dir);
11870 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
11871 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
11872 +                       bfound = bindex;
11873 +                       break;
11874 +               }
11875 +       ii_read_unlock(a->dir);
11876 +       if (unlikely(bfound < 0))
11877 +               goto out;
11878 +
11879 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
11880 +       h_ino = 0;
11881 +       if (a->h_child_inode)
11882 +               h_ino = a->h_child_inode->i_ino;
11883 +
11884 +       if (a->h_child_nlen
11885 +           && (au_ftest_hinjob(a->flags[CHILD], GEN)
11886 +               || au_ftest_hinjob(a->flags[CHILD], MNTPNT)))
11887 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
11888 +                                             a->dir);
11889 +       try_iput = 0;
11890 +       if (dentry)
11891 +               inode = dentry->d_inode;
11892 +       if (xino && !inode && h_ino
11893 +           && (au_ftest_hinjob(a->flags[CHILD], XINO0)
11894 +               || au_ftest_hinjob(a->flags[CHILD], TRYXINO0)
11895 +               || au_ftest_hinjob(a->flags[CHILD], GEN))) {
11896 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
11897 +               try_iput = 1;
11898 +           }
11899 +
11900 +       args.flags = a->flags[CHILD];
11901 +       args.dentry = dentry;
11902 +       args.inode = inode;
11903 +       args.h_inode = a->h_child_inode;
11904 +       args.dir = a->dir;
11905 +       args.h_dir = a->h_dir;
11906 +       args.h_name = a->h_child_name;
11907 +       args.h_nlen = a->h_child_nlen;
11908 +       err = hin_job(&args);
11909 +       if (dentry) {
11910 +               if (dentry->d_fsdata)
11911 +                       di_write_unlock(dentry);
11912 +               dput(dentry);
11913 +       }
11914 +       if (inode && try_iput) {
11915 +               ii_write_unlock(inode);
11916 +               iput(inode);
11917 +       }
11918 +
11919 +       ii_write_lock_parent(a->dir);
11920 +       args.flags = a->flags[PARENT];
11921 +       args.dentry = NULL;
11922 +       args.inode = a->dir;
11923 +       args.h_inode = a->h_dir;
11924 +       args.dir = NULL;
11925 +       args.h_dir = NULL;
11926 +       args.h_name = NULL;
11927 +       args.h_nlen = 0;
11928 +       err = hin_job(&args);
11929 +       ii_write_unlock(a->dir);
11930 +
11931 + out:
11932 +       au_nwt_done(&sbinfo->si_nowait);
11933 +       si_write_unlock(sb);
11934 +
11935 +       iput(a->h_child_inode);
11936 +       iput(a->h_dir);
11937 +       iput(a->dir);
11938 +       kfree(a);
11939 +}
11940 +
11941 +/* ---------------------------------------------------------------------- */
11942 +
11943 +static void aufs_inotify(struct inotify_watch *watch, u32 wd __maybe_unused,
11944 +                        u32 mask, u32 cookie __maybe_unused,
11945 +                        const char *h_child_name, struct inode *h_child_inode)
11946 +{
11947 +       struct au_hinotify *hinotify;
11948 +       struct postproc_args *args;
11949 +       int len, wkq_err;
11950 +       unsigned char isdir, isroot, wh;
11951 +       char *p;
11952 +       struct inode *dir;
11953 +       unsigned int flags[2];
11954 +
11955 +       /* if IN_UNMOUNT happens, there must be another bug */
11956 +       AuDebugOn(mask & IN_UNMOUNT);
11957 +       if (mask & (IN_IGNORED | IN_UNMOUNT)) {
11958 +               put_inotify_watch(watch);
11959 +               return;
11960 +       }
11961 +#ifdef AuDbgHinotify
11962 +       au_debug(1);
11963 +       if (1 || !h_child_name || strcmp(h_child_name, AUFS_XINO_FNAME)) {
11964 +               AuDbg("i%lu, wd %d, mask 0x%x %s, cookie 0x%x, hcname %s,"
11965 +                     " hi%lu\n",
11966 +                     watch->inode->i_ino, wd, mask, in_name(mask), cookie,
11967 +                     h_child_name ? h_child_name : "",
11968 +                     h_child_inode ? h_child_inode->i_ino : 0);
11969 +               WARN_ON(1);
11970 +       }
11971 +       au_debug(0);
11972 +#endif
11973 +
11974 +       hinotify = container_of(watch, struct au_hinotify, hin_watch);
11975 +       AuDebugOn(!hinotify || !hinotify->hin_aufs_inode);
11976 +       dir = igrab(hinotify->hin_aufs_inode);
11977 +       if (!dir)
11978 +               return;
11979 +
11980 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
11981 +       len = 0;
11982 +       wh = 0;
11983 +       if (h_child_name) {
11984 +               len = strlen(h_child_name);
11985 +               if (!memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
11986 +                       h_child_name += AUFS_WH_PFX_LEN;
11987 +                       len -= AUFS_WH_PFX_LEN;
11988 +                       wh = 1;
11989 +               }
11990 +       }
11991 +
11992 +       isdir = 0;
11993 +       if (h_child_inode)
11994 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
11995 +       flags[PARENT] = AuHinJob_ISDIR;
11996 +       flags[CHILD] = 0;
11997 +       if (isdir)
11998 +               flags[CHILD] = AuHinJob_ISDIR;
11999 +       switch (mask & IN_ALL_EVENTS) {
12000 +       case IN_MOVED_FROM:
12001 +       case IN_MOVED_TO:
12002 +               AuDebugOn(!h_child_name || !h_child_inode);
12003 +               au_fset_hinjob(flags[CHILD], GEN);
12004 +               au_fset_hinjob(flags[CHILD], XINO0);
12005 +               au_fset_hinjob(flags[CHILD], MNTPNT);
12006 +               au_fset_hinjob(flags[PARENT], DIRENT);
12007 +               break;
12008 +
12009 +       case IN_CREATE:
12010 +               AuDebugOn(!h_child_name || !h_child_inode);
12011 +               au_fset_hinjob(flags[PARENT], DIRENT);
12012 +               au_fset_hinjob(flags[CHILD], GEN);
12013 +               break;
12014 +
12015 +       case IN_DELETE:
12016 +               /*
12017 +                * aufs never be able to get this child inode.
12018 +                * revalidation should be in d_revalidate()
12019 +                * by checking i_nlink, i_generation or d_unhashed().
12020 +                */
12021 +               AuDebugOn(!h_child_name);
12022 +               au_fset_hinjob(flags[PARENT], DIRENT);
12023 +               au_fset_hinjob(flags[CHILD], GEN);
12024 +               au_fset_hinjob(flags[CHILD], TRYXINO0);
12025 +               au_fset_hinjob(flags[CHILD], MNTPNT);
12026 +               break;
12027 +
12028 +       default:
12029 +               AuDebugOn(1);
12030 +       }
12031 +
12032 +       if (wh)
12033 +               h_child_inode = NULL;
12034 +
12035 +       /* iput() and kfree() will be called in postproc() */
12036 +       /*
12037 +        * inotify_mutex is already acquired and kmalloc/prune_icache may lock
12038 +        * iprune_mutex. strange.
12039 +        */
12040 +       lockdep_off();
12041 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
12042 +       lockdep_on();
12043 +       if (unlikely(!args)) {
12044 +               AuErr1("no memory\n");
12045 +               iput(dir);
12046 +               return;
12047 +       }
12048 +       args->flags[PARENT] = flags[PARENT];
12049 +       args->flags[CHILD] = flags[CHILD];
12050 +       args->mask = mask;
12051 +       args->dir = dir;
12052 +       args->h_dir = igrab(watch->inode);
12053 +       if (h_child_inode)
12054 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
12055 +       args->h_child_inode = h_child_inode;
12056 +       args->h_child_nlen = len;
12057 +       if (len) {
12058 +               p = (void *)args;
12059 +               p += sizeof(*args);
12060 +               memcpy(p, h_child_name, len + 1);
12061 +       }
12062 +
12063 +       lockdep_off();
12064 +       wkq_err = au_wkq_nowait(postproc, args, dir->i_sb);
12065 +       lockdep_on();
12066 +       if (unlikely(wkq_err))
12067 +               AuErr("wkq %d\n", wkq_err);
12068 +}
12069 +
12070 +static void aufs_inotify_destroy(struct inotify_watch *watch __maybe_unused)
12071 +{
12072 +       return;
12073 +}
12074 +
12075 +static struct inotify_operations aufs_inotify_ops = {
12076 +       .handle_event   = aufs_inotify,
12077 +       .destroy_watch  = aufs_inotify_destroy
12078 +};
12079 +
12080 +/* ---------------------------------------------------------------------- */
12081 +
12082 +static void au_hin_destroy_cache(void)
12083 +{
12084 +       kmem_cache_destroy(au_cachep[AuCache_HINOTIFY]);
12085 +       au_cachep[AuCache_HINOTIFY] = NULL;
12086 +}
12087 +
12088 +int __init au_hinotify_init(void)
12089 +{
12090 +       int err;
12091 +
12092 +       err = -ENOMEM;
12093 +       au_cachep[AuCache_HINOTIFY] = AuCache(au_hinotify);
12094 +       if (au_cachep[AuCache_HINOTIFY]) {
12095 +               err = 0;
12096 +               au_hin_handle = inotify_init(&aufs_inotify_ops);
12097 +               if (IS_ERR(au_hin_handle)) {
12098 +                       err = PTR_ERR(au_hin_handle);
12099 +                       au_hin_destroy_cache();
12100 +               }
12101 +       }
12102 +       AuTraceErr(err);
12103 +       return err;
12104 +}
12105 +
12106 +void au_hinotify_fin(void)
12107 +{
12108 +       inotify_destroy(au_hin_handle);
12109 +       if (au_cachep[AuCache_HINOTIFY])
12110 +               au_hin_destroy_cache();
12111 +}
12112 diff --git a/fs/aufs/i_op.c b/fs/aufs/i_op.c
12113 new file mode 100644
12114 index 0000000..a64d6b0
12115 --- /dev/null
12116 +++ b/fs/aufs/i_op.c
12117 @@ -0,0 +1,862 @@
12118 +/*
12119 + * Copyright (C) 2005-2009 Junjiro R. Okajima
12120 + *
12121 + * This program, aufs is free software; you can redistribute it and/or modify
12122 + * it under the terms of the GNU General Public License as published by
12123 + * the Free Software Foundation; either version 2 of the License, or
12124 + * (at your option) any later version.
12125 + */
12126 +
12127 +/*
12128 + * inode operations (except add/del/rename)
12129 + */
12130 +
12131 +#include <linux/device_cgroup.h>
12132 +#include <linux/fs_stack.h>
12133 +#include <linux/uaccess.h>
12134 +#include "aufs.h"
12135 +
12136 +static int h_permission(struct inode *h_inode, int mask,
12137 +                       struct vfsmount *h_mnt, int brperm)
12138 +{
12139 +       int err;
12140 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
12141 +
12142 +       err = -EACCES;
12143 +       if ((write_mask && IS_IMMUTABLE(h_inode))
12144 +           || ((mask & MAY_EXEC)
12145 +               && S_ISREG(h_inode->i_mode)
12146 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
12147 +                   || !(h_inode->i_mode & S_IXUGO))))
12148 +               goto out;
12149 +
12150 +       /*
12151 +        * - skip the lower fs test in the case of write to ro branch.
12152 +        * - nfs dir permission write check is optimized, but a policy for
12153 +        *   link/rename requires a real check.
12154 +        */
12155 +       if ((write_mask && !au_br_writable(brperm))
12156 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
12157 +               && write_mask && !(mask & MAY_READ))
12158 +           || !h_inode->i_op->permission) {
12159 +               /* AuLabel(generic_permission); */
12160 +               err = generic_permission(h_inode, mask, NULL);
12161 +       } else {
12162 +               /* AuLabel(h_inode->permission); */
12163 +               err = h_inode->i_op->permission(h_inode, mask);
12164 +               AuTraceErr(err);
12165 +       }
12166 +
12167 +       if (!err)
12168 +               err = devcgroup_inode_permission(h_inode, mask);
12169 +       if (!err)
12170 +               err = security_inode_permission
12171 +                       (h_inode, mask & (MAY_READ | MAY_WRITE | MAY_EXEC
12172 +                                         | MAY_APPEND));
12173 +
12174 + out:
12175 +       return err;
12176 +}
12177 +
12178 +static int aufs_permission(struct inode *inode, int mask)
12179 +{
12180 +       int err;
12181 +       aufs_bindex_t bindex, bend;
12182 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode);
12183 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
12184 +       struct inode *h_inode;
12185 +       struct super_block *sb;
12186 +       struct au_branch *br;
12187 +
12188 +       sb = inode->i_sb;
12189 +       si_read_lock(sb, AuLock_FLUSH);
12190 +       ii_read_lock_child(inode);
12191 +
12192 +       if (!isdir || write_mask) {
12193 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
12194 +               AuDebugOn(!h_inode
12195 +                         || ((h_inode->i_mode & S_IFMT)
12196 +                             != (inode->i_mode & S_IFMT)));
12197 +               err = 0;
12198 +               bindex = au_ibstart(inode);
12199 +               br = au_sbr(sb, bindex);
12200 +               err = h_permission(h_inode, mask, br->br_mnt, br->br_perm);
12201 +
12202 +               if (write_mask && !err) {
12203 +                       /* test whether the upper writable branch exists */
12204 +                       err = -EROFS;
12205 +                       for (; bindex >= 0; bindex--)
12206 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
12207 +                                       err = 0;
12208 +                                       break;
12209 +                               }
12210 +               }
12211 +               goto out;
12212 +       }
12213 +
12214 +       /* non-write to dir */
12215 +       err = 0;
12216 +       bend = au_ibend(inode);
12217 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
12218 +               h_inode = au_h_iptr(inode, bindex);
12219 +               if (h_inode) {
12220 +                       AuDebugOn(!S_ISDIR(h_inode->i_mode));
12221 +                       br = au_sbr(sb, bindex);
12222 +                       err = h_permission(h_inode, mask, br->br_mnt,
12223 +                                          br->br_perm);
12224 +               }
12225 +       }
12226 +
12227 + out:
12228 +       ii_read_unlock(inode);
12229 +       si_read_unlock(sb);
12230 +       return err;
12231 +}
12232 +
12233 +/* ---------------------------------------------------------------------- */
12234 +
12235 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
12236 +                                 struct nameidata *nd)
12237 +{
12238 +       struct dentry *ret, *parent;
12239 +       struct inode *inode, *h_inode;
12240 +       struct mutex *mtx;
12241 +       struct super_block *sb;
12242 +       int err, npositive;
12243 +       aufs_bindex_t bstart;
12244 +
12245 +       /* temporary workaround for a bug in NFSD readdir */
12246 +       if (!au_test_nfsd(current))
12247 +               IMustLock(dir);
12248 +       else
12249 +               WARN_ONCE(!mutex_is_locked(&dir->i_mutex),
12250 +                         "a known problem of NFSD readdir since 2.6.28\n");
12251 +
12252 +       sb = dir->i_sb;
12253 +       si_read_lock(sb, AuLock_FLUSH);
12254 +       err = au_alloc_dinfo(dentry);
12255 +       ret = ERR_PTR(err);
12256 +       if (unlikely(err))
12257 +               goto out;
12258 +
12259 +       parent = dentry->d_parent; /* dir inode is locked */
12260 +       di_read_lock_parent(parent, AuLock_IR);
12261 +       npositive = au_lkup_dentry(dentry, au_dbstart(parent), /*type*/0, nd);
12262 +       di_read_unlock(parent, AuLock_IR);
12263 +       err = npositive;
12264 +       ret = ERR_PTR(err);
12265 +       if (unlikely(err < 0))
12266 +               goto out_unlock;
12267 +
12268 +       inode = NULL;
12269 +       if (npositive) {
12270 +               bstart = au_dbstart(dentry);
12271 +               h_inode = au_h_dptr(dentry, bstart)->d_inode;
12272 +               if (!S_ISDIR(h_inode->i_mode)) {
12273 +                       /*
12274 +                        * stop 'race'-ing between hardlinks under different
12275 +                        * parents.
12276 +                        */
12277 +                       mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
12278 +                       mutex_lock(mtx);
12279 +                       inode = au_new_inode(dentry, /*must_new*/0);
12280 +                       mutex_unlock(mtx);
12281 +               } else
12282 +                       inode = au_new_inode(dentry, /*must_new*/0);
12283 +               ret = (void *)inode;
12284 +       }
12285 +       if (IS_ERR(inode))
12286 +               goto out_unlock;
12287 +
12288 +       ret = d_splice_alias(inode, dentry);
12289 +       if (unlikely(IS_ERR(ret) && inode))
12290 +               ii_write_unlock(inode);
12291 +       au_store_oflag(nd, inode);
12292 +
12293 + out_unlock:
12294 +       di_write_unlock(dentry);
12295 + out:
12296 +       si_read_unlock(sb);
12297 +       return ret;
12298 +}
12299 +
12300 +/* ---------------------------------------------------------------------- */
12301 +
12302 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
12303 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
12304 +                         aufs_bindex_t bstart)
12305 +{
12306 +       int err;
12307 +       struct dentry *h_parent;
12308 +       struct inode *h_dir;
12309 +
12310 +       if (add_entry) {
12311 +               au_update_dbstart(dentry);
12312 +               IMustLock(parent->d_inode);
12313 +       } else
12314 +               di_write_lock_parent(parent);
12315 +
12316 +       err = 0;
12317 +       if (!au_h_dptr(parent, bcpup)) {
12318 +               if (bstart < bcpup)
12319 +                       err = au_cpdown_dirs(dentry, bcpup);
12320 +               else
12321 +                       err = au_cpup_dirs(dentry, bcpup);
12322 +       }
12323 +       if (!err && add_entry) {
12324 +               h_parent = au_h_dptr(parent, bcpup);
12325 +               h_dir = h_parent->d_inode;
12326 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
12327 +               err = au_lkup_neg(dentry, bcpup);
12328 +               /* todo: no unlock here */
12329 +               mutex_unlock(&h_dir->i_mutex);
12330 +               if (bstart < bcpup && au_dbstart(dentry) < 0) {
12331 +                       au_set_dbstart(dentry, 0);
12332 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
12333 +               }
12334 +       }
12335 +
12336 +       if (!add_entry)
12337 +               di_write_unlock(parent);
12338 +       if (!err)
12339 +               err = bcpup; /* success */
12340 +
12341 +       return err;
12342 +}
12343 +
12344 +/*
12345 + * decide the branch and the parent dir where we will create a new entry.
12346 + * returns new bindex or an error.
12347 + * copyup the parent dir if needed.
12348 + */
12349 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
12350 +             struct au_wr_dir_args *args)
12351 +{
12352 +       int err;
12353 +       aufs_bindex_t bcpup, bstart, src_bstart;
12354 +       const unsigned char add_entry = !!au_ftest_wrdir(args->flags,
12355 +                                                        ADD_ENTRY);
12356 +       struct super_block *sb;
12357 +       struct dentry *parent;
12358 +       struct au_sbinfo *sbinfo;
12359 +
12360 +       sb = dentry->d_sb;
12361 +       sbinfo = au_sbi(sb);
12362 +       parent = dget_parent(dentry);
12363 +       bstart = au_dbstart(dentry);
12364 +       bcpup = bstart;
12365 +       if (args->force_btgt < 0) {
12366 +               if (src_dentry) {
12367 +                       src_bstart = au_dbstart(src_dentry);
12368 +                       if (src_bstart < bstart)
12369 +                               bcpup = src_bstart;
12370 +               } else if (add_entry) {
12371 +                       err = AuWbrCreate(sbinfo, dentry,
12372 +                                         au_ftest_wrdir(args->flags, ISDIR));
12373 +                       bcpup = err;
12374 +               }
12375 +
12376 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
12377 +                       if (add_entry)
12378 +                               err = AuWbrCopyup(sbinfo, dentry);
12379 +                       else {
12380 +                               if (!IS_ROOT(dentry)) {
12381 +                                       di_read_lock_parent(parent, !AuLock_IR);
12382 +                                       err = AuWbrCopyup(sbinfo, dentry);
12383 +                                       di_read_unlock(parent, !AuLock_IR);
12384 +                               } else
12385 +                                       err = AuWbrCopyup(sbinfo, dentry);
12386 +                       }
12387 +                       bcpup = err;
12388 +                       if (unlikely(err < 0))
12389 +                               goto out;
12390 +               }
12391 +       } else {
12392 +               bcpup = args->force_btgt;
12393 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
12394 +       }
12395 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
12396 +       if (bstart < bcpup)
12397 +               au_update_dbrange(dentry, /*do_put_zero*/1);
12398 +
12399 +       err = bcpup;
12400 +       if (bcpup == bstart)
12401 +               goto out; /* success */
12402 +
12403 +       /* copyup the new parent into the branch we process */
12404 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
12405 +
12406 + out:
12407 +       dput(parent);
12408 +       return err;
12409 +}
12410 +
12411 +/* ---------------------------------------------------------------------- */
12412 +
12413 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
12414 +{
12415 +       if (pin && pin->parent)
12416 +               return au_h_dptr(pin->parent, pin->bindex);
12417 +       return NULL;
12418 +}
12419 +
12420 +void au_unpin(struct au_pin *p)
12421 +{
12422 +       if (au_ftest_pin(p->flags, MNT_WRITE))
12423 +               mnt_drop_write(p->h_mnt);
12424 +       if (!p->hdir)
12425 +               return;
12426 +
12427 +       au_hin_imtx_unlock(p->hdir);
12428 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
12429 +               di_read_unlock(p->parent, AuLock_IR);
12430 +       iput(p->hdir->hi_inode);
12431 +       dput(p->parent);
12432 +       p->parent = NULL;
12433 +       p->hdir = NULL;
12434 +       p->h_mnt = NULL;
12435 +}
12436 +
12437 +int au_do_pin(struct au_pin *p)
12438 +{
12439 +       int err;
12440 +       struct super_block *sb;
12441 +       struct dentry *h_dentry, *h_parent;
12442 +       struct au_branch *br;
12443 +       struct inode *h_dir;
12444 +
12445 +       err = 0;
12446 +       sb = p->dentry->d_sb;
12447 +       br = au_sbr(sb, p->bindex);
12448 +       if (IS_ROOT(p->dentry)) {
12449 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
12450 +                       p->h_mnt = br->br_mnt;
12451 +                       err = mnt_want_write(p->h_mnt);
12452 +                       if (unlikely(err)) {
12453 +                               au_fclr_pin(p->flags, MNT_WRITE);
12454 +                               goto out_err;
12455 +                       }
12456 +               }
12457 +               goto out;
12458 +       }
12459 +
12460 +       h_dentry = NULL;
12461 +       if (p->bindex <= au_dbend(p->dentry))
12462 +               h_dentry = au_h_dptr(p->dentry, p->bindex);
12463 +
12464 +       p->parent = dget_parent(p->dentry);
12465 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
12466 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
12467 +
12468 +       h_dir = NULL;
12469 +       h_parent = au_h_dptr(p->parent, p->bindex);
12470 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
12471 +       if (p->hdir)
12472 +               h_dir = p->hdir->hi_inode;
12473 +
12474 +       /* udba case */
12475 +       if (unlikely(!p->hdir || !h_dir)) {
12476 +               err = au_busy_or_stale();
12477 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
12478 +                       di_read_unlock(p->parent, AuLock_IR);
12479 +               dput(p->parent);
12480 +               p->parent = NULL;
12481 +               goto out_err;
12482 +       }
12483 +
12484 +       au_igrab(h_dir);
12485 +       au_hin_imtx_lock_nested(p->hdir, p->lsc_hi);
12486 +
12487 +       if (h_dentry) {
12488 +               err = au_h_verify(h_dentry, p->udba, h_dir, h_parent, br);
12489 +               if (unlikely(err)) {
12490 +                       au_fclr_pin(p->flags, MNT_WRITE);
12491 +                       goto out_unpin;
12492 +               }
12493 +       }
12494 +
12495 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
12496 +               p->h_mnt = br->br_mnt;
12497 +               err = mnt_want_write(p->h_mnt);
12498 +               if (unlikely(err)) {
12499 +                       au_fclr_pin(p->flags, MNT_WRITE);
12500 +                       goto out_unpin;
12501 +               }
12502 +       }
12503 +       goto out; /* success */
12504 +
12505 + out_unpin:
12506 +       au_unpin(p);
12507 + out_err:
12508 +       AuErr("err %d\n", err);
12509 +       err = au_busy_or_stale();
12510 + out:
12511 +       return err;
12512 +}
12513 +
12514 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
12515 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
12516 +                unsigned int udba, unsigned char flags)
12517 +{
12518 +       p->dentry = dentry;
12519 +       p->udba = udba;
12520 +       p->lsc_di = lsc_di;
12521 +       p->lsc_hi = lsc_hi;
12522 +       p->flags = flags;
12523 +       p->bindex = bindex;
12524 +
12525 +       p->parent = NULL;
12526 +       p->hdir = NULL;
12527 +       p->h_mnt = NULL;
12528 +}
12529 +
12530 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
12531 +          unsigned int udba, unsigned char flags)
12532 +{
12533 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
12534 +                   udba, flags);
12535 +       return au_do_pin(pin);
12536 +}
12537 +
12538 +/* ---------------------------------------------------------------------- */
12539 +
12540 +#define AuIcpup_DID_CPUP       1
12541 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
12542 +#define au_fset_icpup(flags, name)     { (flags) |= AuIcpup_##name; }
12543 +#define au_fclr_icpup(flags, name)     { (flags) &= ~AuIcpup_##name; }
12544 +
12545 +struct au_icpup_args {
12546 +       unsigned char flags;
12547 +       unsigned char pin_flags;
12548 +       aufs_bindex_t btgt;
12549 +       struct au_pin pin;
12550 +       struct path h_path;
12551 +       struct inode *h_inode;
12552 +};
12553 +
12554 +static int au_lock_and_icpup(struct dentry *dentry, struct iattr *ia,
12555 +                            struct au_icpup_args *a)
12556 +{
12557 +       int err;
12558 +       unsigned int udba;
12559 +       loff_t sz;
12560 +       aufs_bindex_t bstart;
12561 +       struct dentry *hi_wh, *parent;
12562 +       struct inode *inode;
12563 +       struct au_wr_dir_args wr_dir_args = {
12564 +               .force_btgt     = -1,
12565 +               .flags          = 0
12566 +       };
12567 +
12568 +       di_write_lock_child(dentry);
12569 +       bstart = au_dbstart(dentry);
12570 +       inode = dentry->d_inode;
12571 +       if (S_ISDIR(inode->i_mode))
12572 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
12573 +       /* plink or hi_wh() case */
12574 +       if (bstart != au_ibstart(inode))
12575 +               wr_dir_args.force_btgt = au_ibstart(inode);
12576 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
12577 +       if (unlikely(err < 0))
12578 +               goto out_dentry;
12579 +       a->btgt = err;
12580 +       if (err != bstart)
12581 +               au_fset_icpup(a->flags, DID_CPUP);
12582 +
12583 +       err = 0;
12584 +       a->pin_flags = AuPin_MNT_WRITE;
12585 +       parent = NULL;
12586 +       if (!IS_ROOT(dentry)) {
12587 +               au_fset_pin(a->pin_flags, DI_LOCKED);
12588 +               parent = dget_parent(dentry);
12589 +               di_write_lock_parent(parent);
12590 +       }
12591 +
12592 +       udba = au_opt_udba(dentry->d_sb);
12593 +       if (d_unhashed(dentry) || (ia->ia_valid & ATTR_FILE))
12594 +               udba = AuOpt_UDBA_NONE;
12595 +       err = au_pin(&a->pin, dentry, a->btgt, udba, a->pin_flags);
12596 +       if (unlikely(err)) {
12597 +               if (parent) {
12598 +                       di_write_unlock(parent);
12599 +                       dput(parent);
12600 +               }
12601 +               goto out_dentry;
12602 +       }
12603 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
12604 +       a->h_inode = a->h_path.dentry->d_inode;
12605 +       mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
12606 +       sz = -1;
12607 +       if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
12608 +               sz = ia->ia_size;
12609 +
12610 +       hi_wh = NULL;
12611 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unhashed(dentry)) {
12612 +               hi_wh = au_hi_wh(inode, a->btgt);
12613 +               if (!hi_wh) {
12614 +                       err = au_sio_cpup_wh(dentry, a->btgt, sz, /*file*/NULL);
12615 +                       if (unlikely(err))
12616 +                               goto out_unlock;
12617 +                       hi_wh = au_hi_wh(inode, a->btgt);
12618 +                       /* todo: revalidate hi_wh? */
12619 +               }
12620 +       }
12621 +
12622 +       if (parent) {
12623 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
12624 +               di_downgrade_lock(parent, AuLock_IR);
12625 +               dput(parent);
12626 +       }
12627 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
12628 +               goto out; /* success */
12629 +
12630 +       if (!d_unhashed(dentry)) {
12631 +               err = au_sio_cpup_simple(dentry, a->btgt, sz, AuCpup_DTIME);
12632 +               if (!err)
12633 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
12634 +       } else if (!hi_wh)
12635 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
12636 +       else
12637 +               a->h_path.dentry = hi_wh; /* do not dget here */
12638 +
12639 + out_unlock:
12640 +       mutex_unlock(&a->h_inode->i_mutex);
12641 +       a->h_inode = a->h_path.dentry->d_inode;
12642 +       if (!err) {
12643 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
12644 +               goto out; /* success */
12645 +       }
12646 +
12647 +       au_unpin(&a->pin);
12648 +
12649 + out_dentry:
12650 +       di_write_unlock(dentry);
12651 + out:
12652 +       return err;
12653 +}
12654 +
12655 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
12656 +{
12657 +       int err;
12658 +       struct inode *inode;
12659 +       struct super_block *sb;
12660 +       struct file *file;
12661 +       struct au_icpup_args *a;
12662 +
12663 +       err = -ENOMEM;
12664 +       a = kzalloc(sizeof(*a), GFP_NOFS);
12665 +       if (unlikely(!a))
12666 +               goto out;
12667 +
12668 +       inode = dentry->d_inode;
12669 +       IMustLock(inode);
12670 +       sb = dentry->d_sb;
12671 +       si_read_lock(sb, AuLock_FLUSH);
12672 +
12673 +       file = NULL;
12674 +       if (ia->ia_valid & ATTR_FILE) {
12675 +               /* currently ftruncate(2) only */
12676 +               file = ia->ia_file;
12677 +               fi_write_lock(file);
12678 +               ia->ia_file = au_h_fptr(file, au_fbstart(file));
12679 +       }
12680 +
12681 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
12682 +               ia->ia_valid &= ~ATTR_MODE;
12683 +
12684 +       err = au_lock_and_icpup(dentry, ia, a);
12685 +       if (unlikely(err < 0))
12686 +               goto out_si;
12687 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
12688 +               ia->ia_file = NULL;
12689 +               ia->ia_valid &= ~ATTR_FILE;
12690 +       }
12691 +
12692 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
12693 +       if (ia->ia_valid & ATTR_SIZE) {
12694 +               struct file *f;
12695 +
12696 +               if (ia->ia_size < i_size_read(inode)) {
12697 +                       /* unmap only */
12698 +                       err = vmtruncate(inode, ia->ia_size);
12699 +                       if (unlikely(err))
12700 +                               goto out_unlock;
12701 +               }
12702 +
12703 +               f = NULL;
12704 +               if (ia->ia_valid & ATTR_FILE)
12705 +                       f = ia->ia_file;
12706 +               mutex_unlock(&a->h_inode->i_mutex);
12707 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
12708 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
12709 +       } else
12710 +               err = vfsub_notify_change(&a->h_path, ia);
12711 +       if (!err)
12712 +               au_cpup_attr_changeable(inode);
12713 +
12714 + out_unlock:
12715 +       mutex_unlock(&a->h_inode->i_mutex);
12716 +       au_unpin(&a->pin);
12717 +       di_write_unlock(dentry);
12718 + out_si:
12719 +       if (file) {
12720 +               fi_write_unlock(file);
12721 +               ia->ia_file = file;
12722 +               ia->ia_valid |= ATTR_FILE;
12723 +       }
12724 +       si_read_unlock(sb);
12725 +       kfree(a);
12726 + out:
12727 +       return err;
12728 +}
12729 +
12730 +static int au_getattr_lock_reval(struct dentry *dentry, unsigned int sigen)
12731 +{
12732 +       int err;
12733 +       struct inode *inode;
12734 +       struct dentry *parent;
12735 +
12736 +       err = 0;
12737 +       inode = dentry->d_inode;
12738 +       di_write_lock_child(dentry);
12739 +       if (au_digen(dentry) != sigen || au_iigen(inode) != sigen) {
12740 +               parent = dget_parent(dentry);
12741 +               di_read_lock_parent(parent, AuLock_IR);
12742 +               /* returns a number of positive dentries */
12743 +               err = au_refresh_hdentry(dentry, inode->i_mode & S_IFMT);
12744 +               if (err > 0)
12745 +                       err = au_refresh_hinode(inode, dentry);
12746 +               di_read_unlock(parent, AuLock_IR);
12747 +               dput(parent);
12748 +               if (unlikely(!err))
12749 +                       err = -EIO;
12750 +       }
12751 +       di_downgrade_lock(dentry, AuLock_IR);
12752 +
12753 +       return err;
12754 +}
12755 +
12756 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
12757 +                            unsigned int nlink)
12758 +{
12759 +       inode->i_mode = st->mode;
12760 +       inode->i_uid = st->uid;
12761 +       inode->i_gid = st->gid;
12762 +       inode->i_atime = st->atime;
12763 +       inode->i_mtime = st->mtime;
12764 +       inode->i_ctime = st->ctime;
12765 +
12766 +       au_cpup_attr_nlink(inode, /*force*/0);
12767 +       if (S_ISDIR(inode->i_mode)) {
12768 +               inode->i_nlink -= nlink;
12769 +               inode->i_nlink += st->nlink;
12770 +       }
12771 +
12772 +       spin_lock(&inode->i_lock);
12773 +       inode->i_blocks = st->blocks;
12774 +       i_size_write(inode, st->size);
12775 +       spin_unlock(&inode->i_lock);
12776 +}
12777 +
12778 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
12779 +                       struct dentry *dentry, struct kstat *st)
12780 +{
12781 +       int err;
12782 +       unsigned int mnt_flags;
12783 +       aufs_bindex_t bindex;
12784 +       unsigned char udba_none, positive, did_lock;
12785 +       struct super_block *sb, *h_sb;
12786 +       struct inode *inode;
12787 +       struct vfsmount *h_mnt;
12788 +       struct dentry *h_dentry;
12789 +
12790 +       err = 0;
12791 +       did_lock = 0;
12792 +       sb = dentry->d_sb;
12793 +       inode = dentry->d_inode;
12794 +       si_read_lock(sb, AuLock_FLUSH);
12795 +       if (IS_ROOT(dentry)) {
12796 +               /* lock free root dinfo */
12797 +               h_dentry = dget(au_di(dentry)->di_hdentry->hd_dentry);
12798 +               h_mnt = au_sbr_mnt(sb, 0);
12799 +               goto getattr;
12800 +       }
12801 +
12802 +       did_lock = 1;
12803 +       mnt_flags = au_mntflags(sb);
12804 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
12805 +
12806 +       /* support fstat(2) */
12807 +       if (!d_unhashed(dentry) && !udba_none) {
12808 +               unsigned int sigen = au_sigen(sb);
12809 +               if (au_digen(dentry) == sigen && au_iigen(inode) == sigen)
12810 +                       di_read_lock_child(dentry, AuLock_IR);
12811 +               else {
12812 +                       err = au_getattr_lock_reval(dentry, sigen);
12813 +                       if (unlikely(err))
12814 +                               goto out;
12815 +               }
12816 +       } else
12817 +               di_read_lock_child(dentry, AuLock_IR);
12818 +
12819 +       bindex = au_ibstart(inode);
12820 +       h_mnt = au_sbr_mnt(sb, bindex);
12821 +       h_sb = h_mnt->mnt_sb;
12822 +       if (!au_test_fs_bad_iattr(h_sb) && udba_none)
12823 +               goto out_fill; /* success */
12824 +
12825 +       if (au_dbstart(dentry) == bindex)
12826 +               h_dentry = dget(au_h_dptr(dentry, bindex));
12827 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
12828 +               h_dentry = au_plink_lkup(inode, bindex);
12829 +               if (IS_ERR(h_dentry))
12830 +                       goto out_fill; /* pretending success */
12831 +       } else
12832 +               /* illegally overlapped or something */
12833 +               goto out_fill; /* pretending success */
12834 +
12835 + getattr:
12836 +       positive = !!h_dentry->d_inode;
12837 +       if (positive)
12838 +               err = vfs_getattr(h_mnt, h_dentry, st);
12839 +       dput(h_dentry);
12840 +       if (!err) {
12841 +               if (positive)
12842 +                       au_refresh_iattr(inode, st, h_dentry->d_inode->i_nlink);
12843 +               goto out_fill; /* success */
12844 +       }
12845 +       goto out;
12846 +
12847 + out_fill:
12848 +       generic_fillattr(inode, st);
12849 + out:
12850 +       if (did_lock)
12851 +               di_read_unlock(dentry, AuLock_IR);
12852 +       si_read_unlock(sb);
12853 +       return err;
12854 +}
12855 +
12856 +/* ---------------------------------------------------------------------- */
12857 +
12858 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
12859 +                     int bufsiz)
12860 +{
12861 +       int err;
12862 +       struct super_block *sb;
12863 +       struct dentry *h_dentry;
12864 +
12865 +       err = -EINVAL;
12866 +       h_dentry = au_h_dptr(dentry, bindex);
12867 +       if (unlikely(/* !h_dentry
12868 +                    || !h_dentry->d_inode
12869 +                    || !h_dentry->d_inode->i_op
12870 +                    || */ !h_dentry->d_inode->i_op->readlink))
12871 +               goto out;
12872 +
12873 +       err = security_inode_readlink(h_dentry);
12874 +       if (unlikely(err))
12875 +               goto out;
12876 +
12877 +       sb = dentry->d_sb;
12878 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
12879 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
12880 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
12881 +       }
12882 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
12883 +
12884 + out:
12885 +       return err;
12886 +}
12887 +
12888 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
12889 +{
12890 +       int err;
12891 +
12892 +       aufs_read_lock(dentry, AuLock_IR);
12893 +       err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
12894 +       aufs_read_unlock(dentry, AuLock_IR);
12895 +
12896 +       return err;
12897 +}
12898 +
12899 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
12900 +{
12901 +       int err;
12902 +       char *buf;
12903 +       mm_segment_t old_fs;
12904 +
12905 +       err = -ENOMEM;
12906 +       buf = __getname();
12907 +       if (unlikely(!buf))
12908 +               goto out;
12909 +
12910 +       aufs_read_lock(dentry, AuLock_IR);
12911 +       old_fs = get_fs();
12912 +       set_fs(KERNEL_DS);
12913 +       err = h_readlink(dentry, au_dbstart(dentry), (char __user *)buf,
12914 +                        PATH_MAX);
12915 +       set_fs(old_fs);
12916 +       aufs_read_unlock(dentry, AuLock_IR);
12917 +
12918 +       if (err >= 0) {
12919 +               buf[err] = 0;
12920 +               /* will be freed by put_link */
12921 +               nd_set_link(nd, buf);
12922 +               return NULL; /* success */
12923 +       }
12924 +       __putname(buf);
12925 +
12926 + out:
12927 +       path_put(&nd->path);
12928 +       AuTraceErr(err);
12929 +       return ERR_PTR(err);
12930 +}
12931 +
12932 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
12933 +                         struct nameidata *nd, void *cookie __maybe_unused)
12934 +{
12935 +       __putname(nd_get_link(nd));
12936 +}
12937 +
12938 +/* ---------------------------------------------------------------------- */
12939 +
12940 +static void aufs_truncate_range(struct inode *inode __maybe_unused,
12941 +                               loff_t start __maybe_unused,
12942 +                               loff_t end __maybe_unused)
12943 +{
12944 +       AuUnsupport();
12945 +}
12946 +
12947 +/* ---------------------------------------------------------------------- */
12948 +
12949 +struct inode_operations aufs_symlink_iop = {
12950 +       .permission     = aufs_permission,
12951 +       .setattr        = aufs_setattr,
12952 +       .getattr        = aufs_getattr,
12953 +       .readlink       = aufs_readlink,
12954 +       .follow_link    = aufs_follow_link,
12955 +       .put_link       = aufs_put_link
12956 +};
12957 +
12958 +struct inode_operations aufs_dir_iop = {
12959 +       .create         = aufs_create,
12960 +       .lookup         = aufs_lookup,
12961 +       .link           = aufs_link,
12962 +       .unlink         = aufs_unlink,
12963 +       .symlink        = aufs_symlink,
12964 +       .mkdir          = aufs_mkdir,
12965 +       .rmdir          = aufs_rmdir,
12966 +       .mknod          = aufs_mknod,
12967 +       .rename         = aufs_rename,
12968 +
12969 +       .permission     = aufs_permission,
12970 +       .setattr        = aufs_setattr,
12971 +       .getattr        = aufs_getattr
12972 +};
12973 +
12974 +struct inode_operations aufs_iop = {
12975 +       .permission     = aufs_permission,
12976 +       .setattr        = aufs_setattr,
12977 +       .getattr        = aufs_getattr,
12978 +       .truncate_range = aufs_truncate_range
12979 +};
12980 diff --git a/fs/aufs/i_op_add.c b/fs/aufs/i_op_add.c
12981 new file mode 100644
12982 index 0000000..516120d
12983 --- /dev/null
12984 +++ b/fs/aufs/i_op_add.c
12985 @@ -0,0 +1,625 @@
12986 +/*
12987 + * Copyright (C) 2005-2009 Junjiro R. Okajima
12988 + *
12989 + * This program, aufs is free software; you can redistribute it and/or modify
12990 + * it under the terms of the GNU General Public License as published by
12991 + * the Free Software Foundation; either version 2 of the License, or
12992 + * (at your option) any later version.
12993 + */
12994 +
12995 +/*
12996 + * inode operations (add entry)
12997 + */
12998 +
12999 +#include "aufs.h"
13000 +
13001 +/*
13002 + * final procedure of adding a new entry, except link(2).
13003 + * remove whiteout, instantiate, copyup the parent dir's times and size
13004 + * and update version.
13005 + * if it failed, re-create the removed whiteout.
13006 + */
13007 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
13008 +                 struct dentry *wh_dentry, struct dentry *dentry)
13009 +{
13010 +       int err, rerr;
13011 +       aufs_bindex_t bwh;
13012 +       struct path h_path;
13013 +       struct inode *inode, *h_dir;
13014 +       struct dentry *wh;
13015 +
13016 +       bwh = -1;
13017 +       if (wh_dentry) {
13018 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
13019 +               IMustLock(h_dir);
13020 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
13021 +               bwh = au_dbwh(dentry);
13022 +               h_path.dentry = wh_dentry;
13023 +               h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
13024 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
13025 +                                         dentry);
13026 +               if (unlikely(err))
13027 +                       goto out;
13028 +       }
13029 +
13030 +       inode = au_new_inode(dentry, /*must_new*/1);
13031 +       if (!IS_ERR(inode)) {
13032 +               d_instantiate(dentry, inode);
13033 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
13034 +               IMustLock(dir);
13035 +               if (au_ibstart(dir) == au_dbstart(dentry))
13036 +                       au_cpup_attr_timesizes(dir);
13037 +               dir->i_version++;
13038 +               return 0; /* success */
13039 +       }
13040 +
13041 +       err = PTR_ERR(inode);
13042 +       if (!wh_dentry)
13043 +               goto out;
13044 +
13045 +       /* revert */
13046 +       /* dir inode is locked */
13047 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
13048 +       rerr = PTR_ERR(wh);
13049 +       if (IS_ERR(wh)) {
13050 +               AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
13051 +                       AuDLNPair(dentry), err, rerr);
13052 +               err = -EIO;
13053 +       } else
13054 +               dput(wh);
13055 +
13056 + out:
13057 +       return err;
13058 +}
13059 +
13060 +/*
13061 + * simple tests for the adding inode operations.
13062 + * following the checks in vfs, plus the parent-child relationship.
13063 + */
13064 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
13065 +              struct dentry *h_parent, int isdir)
13066 +{
13067 +       int err;
13068 +       umode_t h_mode;
13069 +       struct dentry *h_dentry;
13070 +       struct inode *h_inode;
13071 +
13072 +       h_dentry = au_h_dptr(dentry, bindex);
13073 +       h_inode = h_dentry->d_inode;
13074 +       if (!dentry->d_inode) {
13075 +               err = -EEXIST;
13076 +               if (unlikely(h_inode))
13077 +                       goto out;
13078 +       } else {
13079 +               /* rename(2) case */
13080 +               err = -EIO;
13081 +               if (unlikely(!h_inode || !h_inode->i_nlink))
13082 +                       goto out;
13083 +
13084 +               h_mode = h_inode->i_mode;
13085 +               if (!isdir) {
13086 +                       err = -EISDIR;
13087 +                       if (unlikely(S_ISDIR(h_mode)))
13088 +                               goto out;
13089 +               } else if (unlikely(!S_ISDIR(h_mode))) {
13090 +                       err = -ENOTDIR;
13091 +                       goto out;
13092 +               }
13093 +       }
13094 +
13095 +       err = -EIO;
13096 +       /* expected parent dir is locked */
13097 +       if (unlikely(h_parent != h_dentry->d_parent))
13098 +               goto out;
13099 +       err = 0;
13100 +
13101 + out:
13102 +       return err;
13103 +}
13104 +
13105 +/*
13106 + * initial procedure of adding a new entry.
13107 + * prepare writable branch and the parent dir, lock it,
13108 + * and lookup whiteout for the new entry.
13109 + */
13110 +static struct dentry*
13111 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
13112 +                 struct dentry *src_dentry, struct au_pin *pin,
13113 +                 struct au_wr_dir_args *wr_dir_args)
13114 +{
13115 +       struct dentry *wh_dentry, *h_parent;
13116 +       struct super_block *sb;
13117 +       struct au_branch *br;
13118 +       int err;
13119 +       unsigned int udba;
13120 +       aufs_bindex_t bcpup;
13121 +
13122 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
13123 +       bcpup = err;
13124 +       wh_dentry = ERR_PTR(err);
13125 +       if (unlikely(err < 0))
13126 +               goto out;
13127 +
13128 +       sb = dentry->d_sb;
13129 +       udba = au_opt_udba(sb);
13130 +       err = au_pin(pin, dentry, bcpup, udba,
13131 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
13132 +       wh_dentry = ERR_PTR(err);
13133 +       if (unlikely(err))
13134 +               goto out;
13135 +
13136 +       h_parent = au_pinned_h_parent(pin);
13137 +       if (udba != AuOpt_UDBA_NONE
13138 +           && au_dbstart(dentry) == bcpup) {
13139 +               err = au_may_add(dentry, bcpup, h_parent,
13140 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
13141 +               wh_dentry = ERR_PTR(err);
13142 +               if (unlikely(err))
13143 +                       goto out_unpin;
13144 +       }
13145 +
13146 +       br = au_sbr(sb, bcpup);
13147 +       if (dt) {
13148 +               struct path tmp = {
13149 +                       .dentry = h_parent,
13150 +                       .mnt    = br->br_mnt
13151 +               };
13152 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
13153 +       }
13154 +
13155 +       wh_dentry = NULL;
13156 +       if (bcpup != au_dbwh(dentry))
13157 +               goto out; /* success */
13158 +
13159 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
13160 +
13161 + out_unpin:
13162 +       if (IS_ERR(wh_dentry))
13163 +               au_unpin(pin);
13164 + out:
13165 +       return wh_dentry;
13166 +}
13167 +
13168 +/* ---------------------------------------------------------------------- */
13169 +
13170 +enum { Mknod, Symlink, Creat };
13171 +struct simple_arg {
13172 +       int type;
13173 +       union {
13174 +               struct {
13175 +                       int mode;
13176 +                       struct nameidata *nd;
13177 +               } c;
13178 +               struct {
13179 +                       const char *symname;
13180 +               } s;
13181 +               struct {
13182 +                       int mode;
13183 +                       dev_t dev;
13184 +               } m;
13185 +       } u;
13186 +};
13187 +
13188 +static int add_simple(struct inode *dir, struct dentry *dentry,
13189 +                     struct simple_arg *arg)
13190 +{
13191 +       int err;
13192 +       aufs_bindex_t bstart;
13193 +       unsigned char created;
13194 +       struct au_dtime dt;
13195 +       struct au_pin pin;
13196 +       struct path h_path;
13197 +       struct dentry *wh_dentry, *parent;
13198 +       struct inode *h_dir;
13199 +       struct au_wr_dir_args wr_dir_args = {
13200 +               .force_btgt     = -1,
13201 +               .flags          = AuWrDir_ADD_ENTRY
13202 +       };
13203 +
13204 +       IMustLock(dir);
13205 +
13206 +       parent = dentry->d_parent; /* dir inode is locked */
13207 +       aufs_read_lock(dentry, AuLock_DW);
13208 +       di_write_lock_parent(parent);
13209 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL, &pin,
13210 +                                     &wr_dir_args);
13211 +       err = PTR_ERR(wh_dentry);
13212 +       if (IS_ERR(wh_dentry))
13213 +               goto out;
13214 +
13215 +       bstart = au_dbstart(dentry);
13216 +       h_path.dentry = au_h_dptr(dentry, bstart);
13217 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
13218 +       h_dir = au_pinned_h_dir(&pin);
13219 +       switch (arg->type) {
13220 +       case Creat:
13221 +               err = vfsub_create(h_dir, &h_path, arg->u.c.mode);
13222 +               break;
13223 +       case Symlink:
13224 +               err = vfsub_symlink(h_dir, &h_path, arg->u.s.symname);
13225 +               break;
13226 +       case Mknod:
13227 +               err = vfsub_mknod(h_dir, &h_path, arg->u.m.mode, arg->u.m.dev);
13228 +               break;
13229 +       default:
13230 +               BUG();
13231 +       }
13232 +       created = !err;
13233 +       if (!err)
13234 +               err = epilog(dir, bstart, wh_dentry, dentry);
13235 +
13236 +       /* revert */
13237 +       if (unlikely(created && err && h_path.dentry->d_inode)) {
13238 +               int rerr;
13239 +               rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
13240 +               if (rerr) {
13241 +                       AuIOErr("%.*s revert failure(%d, %d)\n",
13242 +                               AuDLNPair(dentry), err, rerr);
13243 +                       err = -EIO;
13244 +               }
13245 +               au_dtime_revert(&dt);
13246 +               d_drop(dentry);
13247 +       }
13248 +
13249 +       au_unpin(&pin);
13250 +       dput(wh_dentry);
13251 +
13252 + out:
13253 +       if (unlikely(err)) {
13254 +               au_update_dbstart(dentry);
13255 +               d_drop(dentry);
13256 +       }
13257 +       di_write_unlock(parent);
13258 +       aufs_read_unlock(dentry, AuLock_DW);
13259 +       return err;
13260 +}
13261 +
13262 +int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
13263 +{
13264 +       struct simple_arg arg = {
13265 +               .type = Mknod,
13266 +               .u.m = {
13267 +                       .mode   = mode,
13268 +                       .dev    = dev
13269 +               }
13270 +       };
13271 +       return add_simple(dir, dentry, &arg);
13272 +}
13273 +
13274 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
13275 +{
13276 +       struct simple_arg arg = {
13277 +               .type = Symlink,
13278 +               .u.s.symname = symname
13279 +       };
13280 +       return add_simple(dir, dentry, &arg);
13281 +}
13282 +
13283 +int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
13284 +               struct nameidata *nd)
13285 +{
13286 +       struct simple_arg arg = {
13287 +               .type = Creat,
13288 +               .u.c = {
13289 +                       .mode   = mode,
13290 +                       .nd     = nd
13291 +               }
13292 +       };
13293 +       return add_simple(dir, dentry, &arg);
13294 +}
13295 +
13296 +/* ---------------------------------------------------------------------- */
13297 +
13298 +struct au_link_args {
13299 +       aufs_bindex_t bdst, bsrc;
13300 +       struct au_pin pin;
13301 +       struct path h_path;
13302 +       struct dentry *src_parent, *parent;
13303 +};
13304 +
13305 +static int au_cpup_before_link(struct dentry *src_dentry,
13306 +                              struct au_link_args *a)
13307 +{
13308 +       int err;
13309 +       struct dentry *h_src_dentry;
13310 +       struct mutex *h_mtx;
13311 +
13312 +       di_read_lock_parent(a->src_parent, AuLock_IR);
13313 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
13314 +       if (unlikely(err))
13315 +               goto out;
13316 +
13317 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
13318 +       h_mtx = &h_src_dentry->d_inode->i_mutex;
13319 +       err = au_pin(&a->pin, src_dentry, a->bdst,
13320 +                    au_opt_udba(src_dentry->d_sb),
13321 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
13322 +       if (unlikely(err))
13323 +               goto out;
13324 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13325 +       err = au_sio_cpup_simple(src_dentry, a->bdst, -1,
13326 +                                AuCpup_DTIME /* | AuCpup_KEEPLINO */);
13327 +       mutex_unlock(h_mtx);
13328 +       au_unpin(&a->pin);
13329 +
13330 + out:
13331 +       di_read_unlock(a->src_parent, AuLock_IR);
13332 +       return err;
13333 +}
13334 +
13335 +static int au_cpup_or_link(struct dentry *src_dentry, struct au_link_args *a)
13336 +{
13337 +       int err;
13338 +       unsigned char plink;
13339 +       struct inode *h_inode, *inode;
13340 +       struct dentry *h_src_dentry;
13341 +       struct super_block *sb;
13342 +
13343 +       plink = 0;
13344 +       h_inode = NULL;
13345 +       sb = src_dentry->d_sb;
13346 +       inode = src_dentry->d_inode;
13347 +       if (au_ibstart(inode) <= a->bdst)
13348 +               h_inode = au_h_iptr(inode, a->bdst);
13349 +       if (!h_inode || !h_inode->i_nlink) {
13350 +               /* copyup src_dentry as the name of dentry. */
13351 +               au_set_dbstart(src_dentry, a->bdst);
13352 +               au_set_h_dptr(src_dentry, a->bdst, dget(a->h_path.dentry));
13353 +               h_inode = au_h_dptr(src_dentry, a->bsrc)->d_inode;
13354 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
13355 +               err = au_sio_cpup_single(src_dentry, a->bdst, a->bsrc, -1,
13356 +                                        AuCpup_KEEPLINO, a->parent);
13357 +               mutex_unlock(&h_inode->i_mutex);
13358 +               au_set_h_dptr(src_dentry, a->bdst, NULL);
13359 +               au_set_dbstart(src_dentry, a->bsrc);
13360 +       } else {
13361 +               /* the inode of src_dentry already exists on a.bdst branch */
13362 +               h_src_dentry = d_find_alias(h_inode);
13363 +               if (!h_src_dentry && au_plink_test(inode)) {
13364 +                       plink = 1;
13365 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
13366 +                       err = PTR_ERR(h_src_dentry);
13367 +                       if (IS_ERR(h_src_dentry))
13368 +                               goto out;
13369 +
13370 +                       if (unlikely(!h_src_dentry->d_inode)) {
13371 +                               dput(h_src_dentry);
13372 +                               h_src_dentry = NULL;
13373 +                       }
13374 +
13375 +               }
13376 +               if (h_src_dentry) {
13377 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
13378 +                                        &a->h_path);
13379 +                       dput(h_src_dentry);
13380 +               } else {
13381 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
13382 +                               h_inode->i_ino, a->bdst);
13383 +                       err = -EIO;
13384 +               }
13385 +       }
13386 +
13387 +       if (!err && !plink)
13388 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
13389 +
13390 +out:
13391 +       return err;
13392 +}
13393 +
13394 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
13395 +             struct dentry *dentry)
13396 +{
13397 +       int err, rerr;
13398 +       struct au_dtime dt;
13399 +       struct au_link_args *a;
13400 +       struct dentry *wh_dentry, *h_src_dentry;
13401 +       struct inode *inode;
13402 +       struct super_block *sb;
13403 +       struct au_wr_dir_args wr_dir_args = {
13404 +               /* .force_btgt  = -1, */
13405 +               .flags          = AuWrDir_ADD_ENTRY
13406 +       };
13407 +
13408 +       IMustLock(dir);
13409 +       inode = src_dentry->d_inode;
13410 +       IMustLock(inode);
13411 +
13412 +       err = -ENOMEM;
13413 +       a = kzalloc(sizeof(*a), GFP_NOFS);
13414 +       if (unlikely(!a))
13415 +               goto out;
13416 +
13417 +       a->parent = dentry->d_parent; /* dir inode is locked */
13418 +       aufs_read_and_write_lock2(dentry, src_dentry, /*AuLock_FLUSH*/0);
13419 +       a->src_parent = dget_parent(src_dentry);
13420 +       wr_dir_args.force_btgt = au_dbstart(src_dentry);
13421 +
13422 +       di_write_lock_parent(a->parent);
13423 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
13424 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
13425 +                                     &wr_dir_args);
13426 +       err = PTR_ERR(wh_dentry);
13427 +       if (IS_ERR(wh_dentry))
13428 +               goto out_unlock;
13429 +
13430 +       err = 0;
13431 +       sb = dentry->d_sb;
13432 +       a->bdst = au_dbstart(dentry);
13433 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
13434 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
13435 +       a->bsrc = au_dbstart(src_dentry);
13436 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
13437 +               if (a->bdst < a->bsrc
13438 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
13439 +                       err = au_cpup_or_link(src_dentry, a);
13440 +               else {
13441 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
13442 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
13443 +                                        &a->h_path);
13444 +               }
13445 +       } else {
13446 +               /*
13447 +                * copyup src_dentry to the branch we process,
13448 +                * and then link(2) to it.
13449 +                */
13450 +               if (a->bdst < a->bsrc
13451 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
13452 +                       au_unpin(&a->pin);
13453 +                       di_write_unlock(a->parent);
13454 +                       err = au_cpup_before_link(src_dentry, a);
13455 +                       if (!err) {
13456 +                               di_write_lock_parent(a->parent);
13457 +                               err = au_pin(&a->pin, dentry, a->bdst,
13458 +                                            au_opt_udba(sb),
13459 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
13460 +                               if (unlikely(err))
13461 +                                       goto out_wh;
13462 +                       }
13463 +               }
13464 +               if (!err) {
13465 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
13466 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
13467 +                                        &a->h_path);
13468 +               }
13469 +       }
13470 +       if (unlikely(err))
13471 +               goto out_unpin;
13472 +
13473 +       if (wh_dentry) {
13474 +               a->h_path.dentry = wh_dentry;
13475 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
13476 +                                         dentry);
13477 +               if (unlikely(err))
13478 +                       goto out_revert;
13479 +       }
13480 +
13481 +       dir->i_version++;
13482 +       if (au_ibstart(dir) == au_dbstart(dentry))
13483 +               au_cpup_attr_timesizes(dir);
13484 +       inc_nlink(inode);
13485 +       inode->i_ctime = dir->i_ctime;
13486 +       if (!d_unhashed(a->h_path.dentry))
13487 +               d_instantiate(dentry, au_igrab(inode));
13488 +       else
13489 +               /* some filesystem calls d_drop() */
13490 +               d_drop(dentry);
13491 +       goto out_unpin; /* success */
13492 +
13493 + out_revert:
13494 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
13495 +       if (!rerr)
13496 +               goto out_dt;
13497 +       AuIOErr("%.*s reverting failed(%d, %d)\n",
13498 +               AuDLNPair(dentry), err, rerr);
13499 +       err = -EIO;
13500 + out_dt:
13501 +       d_drop(dentry);
13502 +       au_dtime_revert(&dt);
13503 + out_unpin:
13504 +       au_unpin(&a->pin);
13505 + out_wh:
13506 +       dput(wh_dentry);
13507 + out_unlock:
13508 +       if (unlikely(err)) {
13509 +               au_update_dbstart(dentry);
13510 +               d_drop(dentry);
13511 +       }
13512 +       di_write_unlock(a->parent);
13513 +       dput(a->src_parent);
13514 +       aufs_read_and_write_unlock2(dentry, src_dentry);
13515 +       kfree(a);
13516 + out:
13517 +       return err;
13518 +}
13519 +
13520 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
13521 +{
13522 +       int err, rerr;
13523 +       aufs_bindex_t bindex;
13524 +       unsigned char diropq;
13525 +       struct au_pin pin;
13526 +       struct path h_path;
13527 +       struct dentry *wh_dentry, *parent, *opq_dentry;
13528 +       struct mutex *h_mtx;
13529 +       struct super_block *sb;
13530 +       struct au_dtime dt;
13531 +       struct au_wr_dir_args wr_dir_args = {
13532 +               .force_btgt     = -1,
13533 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
13534 +       };
13535 +
13536 +       IMustLock(dir);
13537 +
13538 +       aufs_read_lock(dentry, AuLock_DW);
13539 +       parent = dentry->d_parent; /* dir inode is locked */
13540 +       di_write_lock_parent(parent);
13541 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL, &pin,
13542 +                                     &wr_dir_args);
13543 +       err = PTR_ERR(wh_dentry);
13544 +       if (IS_ERR(wh_dentry))
13545 +               goto out;
13546 +
13547 +       sb = dentry->d_sb;
13548 +       bindex = au_dbstart(dentry);
13549 +       h_path.dentry = au_h_dptr(dentry, bindex);
13550 +       h_path.mnt = au_sbr_mnt(sb, bindex);
13551 +       err = vfsub_mkdir(au_pinned_h_dir(&pin), &h_path, mode);
13552 +       if (unlikely(err))
13553 +               goto out_unlock;
13554 +
13555 +       /* make the dir opaque */
13556 +       diropq = 0;
13557 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
13558 +       if (wh_dentry
13559 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
13560 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13561 +               opq_dentry = au_diropq_create(dentry, bindex);
13562 +               mutex_unlock(h_mtx);
13563 +               err = PTR_ERR(opq_dentry);
13564 +               if (IS_ERR(opq_dentry))
13565 +                       goto out_dir;
13566 +               dput(opq_dentry);
13567 +               diropq = 1;
13568 +       }
13569 +
13570 +       err = epilog(dir, bindex, wh_dentry, dentry);
13571 +       if (!err) {
13572 +               inc_nlink(dir);
13573 +               goto out_unlock; /* success */
13574 +       }
13575 +
13576 +       /* revert */
13577 +       if (diropq) {
13578 +               AuLabel(revert opq);
13579 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13580 +               rerr = au_diropq_remove(dentry, bindex);
13581 +               mutex_unlock(h_mtx);
13582 +               if (rerr) {
13583 +                       AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
13584 +                               AuDLNPair(dentry), err, rerr);
13585 +                       err = -EIO;
13586 +               }
13587 +       }
13588 +
13589 + out_dir:
13590 +       AuLabel(revert dir);
13591 +       rerr = vfsub_rmdir(au_pinned_h_dir(&pin), &h_path);
13592 +       if (rerr) {
13593 +               AuIOErr("%.*s reverting dir failed(%d, %d)\n",
13594 +                       AuDLNPair(dentry), err, rerr);
13595 +               err = -EIO;
13596 +       }
13597 +       d_drop(dentry);
13598 +       au_dtime_revert(&dt);
13599 + out_unlock:
13600 +       au_unpin(&pin);
13601 +       dput(wh_dentry);
13602 + out:
13603 +       if (unlikely(err)) {
13604 +               au_update_dbstart(dentry);
13605 +               d_drop(dentry);
13606 +       }
13607 +       di_write_unlock(parent);
13608 +       aufs_read_unlock(dentry, AuLock_DW);
13609 +       return err;
13610 +}
13611 diff --git a/fs/aufs/i_op_del.c b/fs/aufs/i_op_del.c
13612 new file mode 100644
13613 index 0000000..a9756ae
13614 --- /dev/null
13615 +++ b/fs/aufs/i_op_del.c
13616 @@ -0,0 +1,463 @@
13617 +/*
13618 + * Copyright (C) 2005-2009 Junjiro R. Okajima
13619 + *
13620 + * This program, aufs is free software; you can redistribute it and/or modify
13621 + * it under the terms of the GNU General Public License as published by
13622 + * the Free Software Foundation; either version 2 of the License, or
13623 + * (at your option) any later version.
13624 + */
13625 +
13626 +/*
13627 + * inode operations (del entry)
13628 + */
13629 +
13630 +#include "aufs.h"
13631 +
13632 +/*
13633 + * decide if a new whiteout for @dentry is necessary or not.
13634 + * when it is necessary, prepare the parent dir for the upper branch whose
13635 + * branch index is @bcpup for creation. the actual creation of the whiteout will
13636 + * be done by caller.
13637 + * return value:
13638 + * 0: wh is unnecessary
13639 + * plus: wh is necessary
13640 + * minus: error
13641 + */
13642 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
13643 +{
13644 +       int need_wh, err;
13645 +       aufs_bindex_t bstart;
13646 +       struct super_block *sb;
13647 +
13648 +       sb = dentry->d_sb;
13649 +       bstart = au_dbstart(dentry);
13650 +       if (*bcpup < 0) {
13651 +               *bcpup = bstart;
13652 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
13653 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
13654 +                       *bcpup = err;
13655 +                       if (unlikely(err < 0))
13656 +                               goto out;
13657 +               }
13658 +       } else
13659 +               AuDebugOn(bstart < *bcpup
13660 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
13661 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
13662 +
13663 +       if (*bcpup != bstart) {
13664 +               err = au_cpup_dirs(dentry, *bcpup);
13665 +               if (unlikely(err))
13666 +                       goto out;
13667 +               need_wh = 1;
13668 +       } else {
13669 +               aufs_bindex_t old_bend, new_bend, bdiropq = -1;
13670 +
13671 +               old_bend = au_dbend(dentry);
13672 +               if (isdir) {
13673 +                       bdiropq = au_dbdiropq(dentry);
13674 +                       au_set_dbdiropq(dentry, -1);
13675 +               }
13676 +               need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0,
13677 +                                        /*nd*/NULL);
13678 +               err = need_wh;
13679 +               if (isdir)
13680 +                       au_set_dbdiropq(dentry, bdiropq);
13681 +               if (unlikely(err < 0))
13682 +                       goto out;
13683 +               new_bend = au_dbend(dentry);
13684 +               if (!need_wh && old_bend != new_bend) {
13685 +                       au_set_h_dptr(dentry, new_bend, NULL);
13686 +                       au_set_dbend(dentry, old_bend);
13687 +               }
13688 +       }
13689 +       AuDbg("need_wh %d\n", need_wh);
13690 +       err = need_wh;
13691 +
13692 + out:
13693 +       return err;
13694 +}
13695 +
13696 +/*
13697 + * simple tests for the del-entry operations.
13698 + * following the checks in vfs, plus the parent-child relationship.
13699 + */
13700 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
13701 +              struct dentry *h_parent, int isdir)
13702 +{
13703 +       int err;
13704 +       umode_t h_mode;
13705 +       struct dentry *h_dentry, *h_latest;
13706 +       struct inode *h_inode;
13707 +
13708 +       h_dentry = au_h_dptr(dentry, bindex);
13709 +       h_inode = h_dentry->d_inode;
13710 +       if (dentry->d_inode) {
13711 +               err = -ENOENT;
13712 +               if (unlikely(!h_inode || !h_inode->i_nlink))
13713 +                       goto out;
13714 +
13715 +               h_mode = h_inode->i_mode;
13716 +               if (!isdir) {
13717 +                       err = -EISDIR;
13718 +                       if (unlikely(S_ISDIR(h_mode)))
13719 +                               goto out;
13720 +               } else if (unlikely(!S_ISDIR(h_mode))) {
13721 +                       err = -ENOTDIR;
13722 +                       goto out;
13723 +               }
13724 +       } else {
13725 +               /* rename(2) case */
13726 +               err = -EIO;
13727 +               if (unlikely(h_inode))
13728 +                       goto out;
13729 +       }
13730 +
13731 +       err = -ENOENT;
13732 +       /* expected parent dir is locked */
13733 +       if (unlikely(h_parent != h_dentry->d_parent))
13734 +               goto out;
13735 +       err = 0;
13736 +
13737 +       /*
13738 +        * rmdir a dir may break the consistency on some filesystem.
13739 +        * let's try heavy test.
13740 +        */
13741 +       err = -EACCES;
13742 +       if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
13743 +               goto out;
13744 +
13745 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
13746 +                                  au_sbr(dentry->d_sb, bindex));
13747 +       err = -EIO;
13748 +       if (IS_ERR(h_latest))
13749 +               goto out;
13750 +       if (h_latest == h_dentry)
13751 +               err = 0;
13752 +       dput(h_latest);
13753 +
13754 + out:
13755 +       return err;
13756 +}
13757 +
13758 +/*
13759 + * decide the branch where we operate for @dentry. the branch index will be set
13760 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
13761 + * dir for reverting.
13762 + * when a new whiteout is necessary, create it.
13763 + */
13764 +static struct dentry*
13765 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
13766 +                   struct au_dtime *dt, struct au_pin *pin)
13767 +{
13768 +       struct dentry *wh_dentry;
13769 +       struct super_block *sb;
13770 +       struct path h_path;
13771 +       int err, need_wh;
13772 +       unsigned int udba;
13773 +       aufs_bindex_t bcpup;
13774 +
13775 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
13776 +       wh_dentry = ERR_PTR(need_wh);
13777 +       if (unlikely(need_wh < 0))
13778 +               goto out;
13779 +
13780 +       sb = dentry->d_sb;
13781 +       udba = au_opt_udba(sb);
13782 +       bcpup = *rbcpup;
13783 +       err = au_pin(pin, dentry, bcpup, udba,
13784 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
13785 +       wh_dentry = ERR_PTR(err);
13786 +       if (unlikely(err))
13787 +               goto out;
13788 +
13789 +       h_path.dentry = au_pinned_h_parent(pin);
13790 +       if (udba != AuOpt_UDBA_NONE
13791 +           && au_dbstart(dentry) == bcpup) {
13792 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
13793 +               wh_dentry = ERR_PTR(err);
13794 +               if (unlikely(err))
13795 +                       goto out_unpin;
13796 +       }
13797 +
13798 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
13799 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
13800 +       wh_dentry = NULL;
13801 +       if (!need_wh)
13802 +               goto out; /* success, no need to create whiteout */
13803 +
13804 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
13805 +       if (!IS_ERR(wh_dentry))
13806 +               goto out; /* success */
13807 +       /* returns with the parent is locked and wh_dentry is dget-ed */
13808 +
13809 + out_unpin:
13810 +       au_unpin(pin);
13811 + out:
13812 +       return wh_dentry;
13813 +}
13814 +
13815 +/*
13816 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
13817 + * in order to be revertible and save time for removing many child whiteouts
13818 + * under the dir.
13819 + * returns 1 when there are too many child whiteout and caller should remove
13820 + * them asynchronously. returns 0 when the number of children is enough small to
13821 + * remove now or the branch fs is a remote fs.
13822 + * otherwise return an error.
13823 + */
13824 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
13825 +                          struct au_nhash *whlist, struct inode *dir)
13826 +{
13827 +       int rmdir_later, err, dirwh;
13828 +       struct dentry *h_dentry;
13829 +       struct super_block *sb;
13830 +
13831 +       sb = dentry->d_sb;
13832 +       h_dentry = au_h_dptr(dentry, bindex);
13833 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
13834 +       if (unlikely(err))
13835 +               goto out;
13836 +
13837 +       /* stop monitoring */
13838 +       au_hin_free(au_hi(dentry->d_inode, bindex));
13839 +
13840 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
13841 +               dirwh = au_sbi(sb)->si_dirwh;
13842 +               rmdir_later = (dirwh <= 1);
13843 +               if (!rmdir_later)
13844 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
13845 +                                                             dirwh);
13846 +               if (rmdir_later)
13847 +                       return rmdir_later;
13848 +       }
13849 +
13850 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
13851 +       if (unlikely(err)) {
13852 +               AuIOErr("rmdir %.*s, b%d failed, %d. ignored\n",
13853 +                       AuDLNPair(h_dentry), bindex, err);
13854 +               err = 0;
13855 +       }
13856 +
13857 + out:
13858 +       return err;
13859 +}
13860 +
13861 +/*
13862 + * final procedure for deleting a entry.
13863 + * maintain dentry and iattr.
13864 + */
13865 +static void epilog(struct inode *dir, struct dentry *dentry,
13866 +                  aufs_bindex_t bindex)
13867 +{
13868 +       struct inode *inode;
13869 +
13870 +       /*
13871 +        * even if this is not a dir,
13872 +        * set S_DEAD here since we need to detect the dead inode.
13873 +        */
13874 +       inode = dentry->d_inode;
13875 +       if (!inode->i_nlink)
13876 +               inode->i_flags |= S_DEAD;
13877 +       d_drop(dentry);
13878 +       inode->i_ctime = dir->i_ctime;
13879 +
13880 +       if (atomic_read(&dentry->d_count) == 1) {
13881 +               au_set_h_dptr(dentry, au_dbstart(dentry), NULL);
13882 +               au_update_dbstart(dentry);
13883 +       }
13884 +       if (au_ibstart(dir) == bindex)
13885 +               au_cpup_attr_timesizes(dir);
13886 +       dir->i_version++;
13887 +}
13888 +
13889 +/*
13890 + * when an error happened, remove the created whiteout and revert everything.
13891 + */
13892 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bwh,
13893 +                    struct dentry *wh_dentry, struct dentry *dentry,
13894 +                    struct au_dtime *dt)
13895 +{
13896 +       int rerr;
13897 +       struct path h_path = {
13898 +               .dentry = wh_dentry,
13899 +               .mnt    = au_sbr_mnt(dir->i_sb, bwh)
13900 +       };
13901 +
13902 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bwh), &h_path, dentry);
13903 +       if (!rerr) {
13904 +               au_set_dbwh(dentry, bwh);
13905 +               au_dtime_revert(dt);
13906 +               return 0;
13907 +       }
13908 +
13909 +       AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
13910 +               AuDLNPair(dentry), err, rerr);
13911 +       return -EIO;
13912 +}
13913 +
13914 +/* ---------------------------------------------------------------------- */
13915 +
13916 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
13917 +{
13918 +       int err;
13919 +       aufs_bindex_t bwh, bindex, bstart;
13920 +       struct au_dtime dt;
13921 +       struct au_pin pin;
13922 +       struct path h_path;
13923 +       struct inode *inode, *h_dir;
13924 +       struct dentry *parent, *wh_dentry;
13925 +
13926 +       IMustLock(dir);
13927 +       inode = dentry->d_inode;
13928 +       if (unlikely(!inode))
13929 +               return -ENOENT; /* possible? */
13930 +       IMustLock(inode);
13931 +
13932 +       aufs_read_lock(dentry, AuLock_DW);
13933 +       parent = dentry->d_parent; /* dir inode is locked */
13934 +       di_write_lock_parent(parent);
13935 +
13936 +       bstart = au_dbstart(dentry);
13937 +       bwh = au_dbwh(dentry);
13938 +       bindex = -1;
13939 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &dt, &pin);
13940 +       err = PTR_ERR(wh_dentry);
13941 +       if (IS_ERR(wh_dentry))
13942 +               goto out;
13943 +
13944 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
13945 +       h_path.dentry = au_h_dptr(dentry, bstart);
13946 +       dget(h_path.dentry);
13947 +       if (bindex == bstart) {
13948 +               h_dir = au_pinned_h_dir(&pin);
13949 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
13950 +       } else {
13951 +               /* dir inode is locked */
13952 +               h_dir = wh_dentry->d_parent->d_inode;
13953 +               IMustLock(h_dir);
13954 +               err = 0;
13955 +       }
13956 +
13957 +       if (!err) {
13958 +               drop_nlink(inode);
13959 +               epilog(dir, dentry, bindex);
13960 +
13961 +               /* update target timestamps */
13962 +               if (bindex == bstart) {
13963 +                       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
13964 +                       inode->i_ctime = h_path.dentry->d_inode->i_ctime;
13965 +               } else
13966 +                       /* todo: this timestamp may be reverted later */
13967 +                       inode->i_ctime = h_dir->i_ctime;
13968 +               goto out_unlock; /* success */
13969 +       }
13970 +
13971 +       /* revert */
13972 +       if (wh_dentry) {
13973 +               int rerr;
13974 +
13975 +               rerr = do_revert(err, dir, bwh, wh_dentry, dentry, &dt);
13976 +               if (rerr)
13977 +                       err = rerr;
13978 +       }
13979 +
13980 + out_unlock:
13981 +       au_unpin(&pin);
13982 +       dput(wh_dentry);
13983 +       dput(h_path.dentry);
13984 + out:
13985 +       di_write_unlock(parent);
13986 +       aufs_read_unlock(dentry, AuLock_DW);
13987 +       return err;
13988 +}
13989 +
13990 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
13991 +{
13992 +       int err, rmdir_later;
13993 +       aufs_bindex_t bwh, bindex, bstart;
13994 +       struct au_dtime dt;
13995 +       struct au_pin pin;
13996 +       struct inode *inode;
13997 +       struct dentry *parent, *wh_dentry, *h_dentry;
13998 +       struct au_whtmp_rmdir *args;
13999 +
14000 +       IMustLock(dir);
14001 +       inode = dentry->d_inode;
14002 +       err = -ENOENT; /* possible? */
14003 +       if (unlikely(!inode))
14004 +               goto out;
14005 +       IMustLock(inode);
14006 +
14007 +       err = -ENOMEM;
14008 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
14009 +       if (unlikely(!args))
14010 +               goto out;
14011 +
14012 +       aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
14013 +       parent = dentry->d_parent; /* dir inode is locked */
14014 +       di_write_lock_parent(parent);
14015 +       err = au_test_empty(dentry, args->whlist);
14016 +       if (unlikely(err))
14017 +               goto out_args;
14018 +
14019 +       bstart = au_dbstart(dentry);
14020 +       bwh = au_dbwh(dentry);
14021 +       bindex = -1;
14022 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &dt, &pin);
14023 +       err = PTR_ERR(wh_dentry);
14024 +       if (IS_ERR(wh_dentry))
14025 +               goto out_args;
14026 +
14027 +       h_dentry = au_h_dptr(dentry, bstart);
14028 +       dget(h_dentry);
14029 +       rmdir_later = 0;
14030 +       if (bindex == bstart) {
14031 +               err = renwh_and_rmdir(dentry, bstart, args->whlist, dir);
14032 +               if (err > 0) {
14033 +                       rmdir_later = err;
14034 +                       err = 0;
14035 +               }
14036 +       } else {
14037 +               /* stop monitoring */
14038 +               au_hin_free(au_hi(inode, bstart));
14039 +
14040 +               /* dir inode is locked */
14041 +               IMustLock(wh_dentry->d_parent->d_inode);
14042 +               err = 0;
14043 +       }
14044 +
14045 +       if (!err) {
14046 +               clear_nlink(inode);
14047 +               au_set_dbdiropq(dentry, -1);
14048 +               epilog(dir, dentry, bindex);
14049 +
14050 +               if (rmdir_later) {
14051 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
14052 +                       args = NULL;
14053 +               }
14054 +
14055 +               goto out_unlock; /* success */
14056 +       }
14057 +
14058 +       /* revert */
14059 +       AuLabel(revert);
14060 +       if (wh_dentry) {
14061 +               int rerr;
14062 +
14063 +               rerr = do_revert(err, dir, bwh, wh_dentry, dentry, &dt);
14064 +               if (rerr)
14065 +                       err = rerr;
14066 +       }
14067 +
14068 + out_unlock:
14069 +       au_unpin(&pin);
14070 +       dput(wh_dentry);
14071 +       dput(h_dentry);
14072 + out_args:
14073 +       di_write_unlock(parent);
14074 +       aufs_read_unlock(dentry, AuLock_DW);
14075 +       if (args)
14076 +               au_whtmp_rmdir_free(args);
14077 + out:
14078 +       return err;
14079 +}
14080 diff --git a/fs/aufs/i_op_ren.c b/fs/aufs/i_op_ren.c
14081 new file mode 100644
14082 index 0000000..3617d94
14083 --- /dev/null
14084 +++ b/fs/aufs/i_op_ren.c
14085 @@ -0,0 +1,940 @@
14086 +/*
14087 + * Copyright (C) 2005-2009 Junjiro R. Okajima
14088 + *
14089 + * This program, aufs is free software; you can redistribute it and/or modify
14090 + * it under the terms of the GNU General Public License as published by
14091 + * the Free Software Foundation; either version 2 of the License, or
14092 + * (at your option) any later version.
14093 + */
14094 +
14095 +/*
14096 + * inode operation (rename entry)
14097 + * todo: this is crazy monster
14098 + */
14099 +
14100 +#include "aufs.h"
14101 +
14102 +enum { AuSRC, AuDST, AuSrcDst };
14103 +enum { AuPARENT, AuCHILD, AuParentChild };
14104 +
14105 +#define AuRen_ISDIR    1
14106 +#define AuRen_ISSAMEDIR        (1 << 1)
14107 +#define AuRen_WHSRC    (1 << 2)
14108 +#define AuRen_WHDST    (1 << 3)
14109 +#define AuRen_MNT_WRITE        (1 << 4)
14110 +#define AuRen_DT_DSTDIR        (1 << 5)
14111 +#define AuRen_DIROPQ   (1 << 6)
14112 +#define AuRen_CPUP     (1 << 7)
14113 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
14114 +#define au_fset_ren(flags, name)       { (flags) |= AuRen_##name; }
14115 +#define au_fclr_ren(flags, name)       { (flags) &= ~AuRen_##name; }
14116 +
14117 +struct au_ren_args {
14118 +       struct {
14119 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
14120 +                       *wh_dentry;
14121 +               struct inode *dir, *inode;
14122 +               struct au_hinode *hdir;
14123 +               struct au_dtime dt[AuParentChild];
14124 +               aufs_bindex_t bstart;
14125 +       } sd[AuSrcDst];
14126 +
14127 +#define src_dentry     sd[AuSRC].dentry
14128 +#define src_dir                sd[AuSRC].dir
14129 +#define src_inode      sd[AuSRC].inode
14130 +#define src_h_dentry   sd[AuSRC].h_dentry
14131 +#define src_parent     sd[AuSRC].parent
14132 +#define src_h_parent   sd[AuSRC].h_parent
14133 +#define src_wh_dentry  sd[AuSRC].wh_dentry
14134 +#define src_hdir       sd[AuSRC].hdir
14135 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
14136 +#define src_dt         sd[AuSRC].dt
14137 +#define src_bstart     sd[AuSRC].bstart
14138 +
14139 +#define dst_dentry     sd[AuDST].dentry
14140 +#define dst_dir                sd[AuDST].dir
14141 +#define dst_inode      sd[AuDST].inode
14142 +#define dst_h_dentry   sd[AuDST].h_dentry
14143 +#define dst_parent     sd[AuDST].parent
14144 +#define dst_h_parent   sd[AuDST].h_parent
14145 +#define dst_wh_dentry  sd[AuDST].wh_dentry
14146 +#define dst_hdir       sd[AuDST].hdir
14147 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
14148 +#define dst_dt         sd[AuDST].dt
14149 +#define dst_bstart     sd[AuDST].bstart
14150 +
14151 +       struct dentry *h_trap;
14152 +       struct au_branch *br;
14153 +       struct au_hinode *src_hinode;
14154 +       struct path h_path;
14155 +       struct au_nhash *whlist;
14156 +       aufs_bindex_t btgt;
14157 +
14158 +       unsigned int flags;
14159 +
14160 +       struct au_whtmp_rmdir *thargs;
14161 +       struct dentry *h_dst;
14162 +};
14163 +
14164 +/* ---------------------------------------------------------------------- */
14165 +
14166 +/*
14167 + * functions for reverting.
14168 + * when an error happened in a single rename systemcall, we should revert
14169 + * everything as if nothing happend.
14170 + * we don't need to revert the copied-up/down the parent dir since they are
14171 + * harmless.
14172 + */
14173 +
14174 +#define RevertFailure(fmt, args...) do { \
14175 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
14176 +               ##args, err, rerr); \
14177 +       err = -EIO; \
14178 +} while (0)
14179 +
14180 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
14181 +{
14182 +       int rerr;
14183 +
14184 +       au_hin_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
14185 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
14186 +       au_hin_imtx_unlock(a->src_hinode);
14187 +       if (rerr)
14188 +               RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
14189 +}
14190 +
14191 +
14192 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
14193 +{
14194 +       int rerr;
14195 +
14196 +       a->h_path.dentry = au_lkup_one(&a->src_dentry->d_name, a->src_h_parent,
14197 +                                      a->br, /*nd*/NULL);
14198 +       rerr = PTR_ERR(a->h_path.dentry);
14199 +       if (IS_ERR(a->h_path.dentry)) {
14200 +               RevertFailure("au_lkup_one %.*s", AuDLNPair(a->src_dentry));
14201 +               return;
14202 +       }
14203 +
14204 +       rerr = vfsub_rename(a->dst_h_dir,
14205 +                           au_h_dptr(a->src_dentry, a->btgt),
14206 +                           a->src_h_dir, &a->h_path);
14207 +       d_drop(a->h_path.dentry);
14208 +       dput(a->h_path.dentry);
14209 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
14210 +       if (rerr)
14211 +               RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
14212 +}
14213 +
14214 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
14215 +{
14216 +       int rerr;
14217 +
14218 +       a->h_path.dentry = a->dst_h_dentry;
14219 +       rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*force*/0);
14220 +       au_set_h_dptr(a->src_dentry, a->btgt, NULL);
14221 +       au_set_dbstart(a->src_dentry, a->src_bstart);
14222 +       if (rerr)
14223 +               RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
14224 +}
14225 +
14226 +
14227 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
14228 +{
14229 +       int rerr;
14230 +
14231 +       a->h_path.dentry = au_lkup_one(&a->dst_dentry->d_name, a->dst_h_parent,
14232 +                                      a->br, /*nd*/NULL);
14233 +       rerr = PTR_ERR(a->h_path.dentry);
14234 +       if (IS_ERR(a->h_path.dentry)) {
14235 +               RevertFailure("lookup %.*s", AuDLNPair(a->dst_dentry));
14236 +               return;
14237 +       }
14238 +       if (a->h_path.dentry->d_inode) {
14239 +               d_drop(a->h_path.dentry);
14240 +               dput(a->h_path.dentry);
14241 +               return;
14242 +       }
14243 +
14244 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path);
14245 +       d_drop(a->h_path.dentry);
14246 +       dput(a->h_path.dentry);
14247 +       if (!rerr) {
14248 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
14249 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
14250 +       } else
14251 +               RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
14252 +}
14253 +
14254 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
14255 +{
14256 +       int rerr;
14257 +
14258 +       a->h_path.dentry = a->src_wh_dentry;
14259 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
14260 +       if (rerr)
14261 +               RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
14262 +}
14263 +
14264 +static void au_ren_rev_drop(struct au_ren_args *a)
14265 +{
14266 +       struct dentry *d, *h_d;
14267 +       int i;
14268 +       aufs_bindex_t bend, bindex;
14269 +
14270 +       for (i = 0; i < AuSrcDst; i++) {
14271 +               d = a->sd[i].dentry;
14272 +               d_drop(d);
14273 +               bend = au_dbend(d);
14274 +               for (bindex = au_dbstart(d); bindex <= bend; bindex++) {
14275 +                       h_d = au_h_dptr(d, bindex);
14276 +                       if (h_d)
14277 +                               d_drop(h_d);
14278 +               }
14279 +       }
14280 +
14281 +       au_update_dbstart(a->dst_dentry);
14282 +       if (a->thargs)
14283 +               d_drop(a->h_dst);
14284 +}
14285 +#undef RevertFailure
14286 +
14287 +/* ---------------------------------------------------------------------- */
14288 +
14289 +/*
14290 + * when we have to copyup the renaming entry, do it with the rename-target name
14291 + * in order to minimize the cost (the later actual rename is unnecessary).
14292 + * otherwise rename it on the target branch.
14293 + */
14294 +static int au_ren_or_cpup(struct au_ren_args *a)
14295 +{
14296 +       int err;
14297 +       struct dentry *d;
14298 +
14299 +       d = a->src_dentry;
14300 +       if (au_dbstart(d) == a->btgt) {
14301 +               a->h_path.dentry = a->dst_h_dentry;
14302 +               if (au_ftest_ren(a->flags, DIROPQ)
14303 +                   && au_dbdiropq(d) == a->btgt)
14304 +                       au_fclr_ren(a->flags, DIROPQ);
14305 +               AuDebugOn(au_dbstart(d) != a->btgt);
14306 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
14307 +                                  a->dst_h_dir, &a->h_path);
14308 +       } else {
14309 +               struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
14310 +
14311 +               au_fset_ren(a->flags, CPUP);
14312 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
14313 +               au_set_dbstart(d, a->btgt);
14314 +               au_set_h_dptr(d, a->btgt, dget(a->dst_h_dentry));
14315 +               err = au_sio_cpup_single(d, a->btgt, a->src_bstart, -1,
14316 +                                        !AuCpup_DTIME, a->dst_parent);
14317 +               if (unlikely(err)) {
14318 +                       au_set_h_dptr(d, a->btgt, NULL);
14319 +                       au_set_dbstart(d, a->src_bstart);
14320 +               }
14321 +               mutex_unlock(h_mtx);
14322 +       }
14323 +
14324 +       return err;
14325 +}
14326 +
14327 +/* cf. aufs_rmdir() */
14328 +static int au_ren_del_whtmp(struct au_ren_args *a)
14329 +{
14330 +       int err;
14331 +       struct inode *dir;
14332 +
14333 +       dir = a->dst_dir;
14334 +       if (!au_nhash_test_longer_wh(a->whlist, a->btgt,
14335 +                                    au_sbi(dir->i_sb)->si_dirwh)
14336 +           || au_test_fs_remote(a->h_dst->d_sb)) {
14337 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, a->whlist);
14338 +               if (unlikely(err))
14339 +                       AuWarn("failed removing whtmp dir %.*s (%d), "
14340 +                              "ignored.\n", AuDLNPair(a->h_dst), err);
14341 +       } else {
14342 +               au_nhash_wh_free(a->thargs->whlist, /*bend*/0);
14343 +               a->thargs->whlist = a->whlist;
14344 +               a->whlist = NULL;
14345 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
14346 +               dput(a->h_dst);
14347 +               a->thargs = NULL;
14348 +       }
14349 +
14350 +       return 0;
14351 +}
14352 +
14353 +/* make it 'opaque' dir. */
14354 +static int au_ren_diropq(struct au_ren_args *a)
14355 +{
14356 +       int err;
14357 +       struct dentry *diropq;
14358 +
14359 +       err = 0;
14360 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
14361 +       au_hin_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
14362 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
14363 +       au_hin_imtx_unlock(a->src_hinode);
14364 +       if (IS_ERR(diropq))
14365 +               err = PTR_ERR(diropq);
14366 +       dput(diropq);
14367 +
14368 +       return err;
14369 +}
14370 +
14371 +static int do_rename(struct au_ren_args *a)
14372 +{
14373 +       int err;
14374 +       struct dentry *d, *h_d;
14375 +
14376 +       /* prepare workqueue args for asynchronous rmdir */
14377 +       h_d = a->dst_h_dentry;
14378 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
14379 +               err = -ENOMEM;
14380 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
14381 +               if (unlikely(!a->thargs))
14382 +                       goto out;
14383 +               a->h_dst = dget(h_d);
14384 +       }
14385 +
14386 +       /* create whiteout for src_dentry */
14387 +       if (au_ftest_ren(a->flags, WHSRC)) {
14388 +               a->src_wh_dentry
14389 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
14390 +               err = PTR_ERR(a->src_wh_dentry);
14391 +               if (IS_ERR(a->src_wh_dentry))
14392 +                       goto out_thargs;
14393 +       }
14394 +
14395 +       /* lookup whiteout for dentry */
14396 +       if (au_ftest_ren(a->flags, WHDST)) {
14397 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
14398 +                                a->br);
14399 +               err = PTR_ERR(h_d);
14400 +               if (IS_ERR(h_d))
14401 +                       goto out_whsrc;
14402 +               if (!h_d->d_inode)
14403 +                       dput(h_d);
14404 +               else
14405 +                       a->dst_wh_dentry = h_d;
14406 +       }
14407 +
14408 +       /* rename dentry to tmpwh */
14409 +       if (a->thargs) {
14410 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
14411 +               if (unlikely(err))
14412 +                       goto out_whdst;
14413 +
14414 +               d = a->dst_dentry;
14415 +               au_set_h_dptr(d, a->btgt, NULL);
14416 +               err = au_lkup_neg(d, a->btgt);
14417 +               if (unlikely(err))
14418 +                       goto out_whtmp;
14419 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
14420 +       }
14421 +
14422 +       /* cpup src */
14423 +       if (a->dst_h_dentry->d_inode && a->src_bstart != a->btgt) {
14424 +               struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
14425 +
14426 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
14427 +               err = au_sio_cpup_simple(a->src_dentry, a->btgt, -1,
14428 +                                        !AuCpup_DTIME);
14429 +               mutex_unlock(h_mtx);
14430 +               if (unlikely(err))
14431 +                       goto out_whtmp;
14432 +       }
14433 +
14434 +       /* rename by vfs_rename or cpup */
14435 +       d = a->dst_dentry;
14436 +       if (au_ftest_ren(a->flags, ISDIR)
14437 +           && (a->dst_wh_dentry
14438 +               || au_dbdiropq(d) == a->btgt
14439 +               /* hide the lower to keep xino */
14440 +               || a->btgt < au_dbend(d)
14441 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
14442 +               au_fset_ren(a->flags, DIROPQ);
14443 +       err = au_ren_or_cpup(a);
14444 +       if (unlikely(err))
14445 +               /* leave the copied-up one */
14446 +               goto out_whtmp;
14447 +
14448 +       /* make dir opaque */
14449 +       if (au_ftest_ren(a->flags, DIROPQ)) {
14450 +               err = au_ren_diropq(a);
14451 +               if (unlikely(err))
14452 +                       goto out_rename;
14453 +       }
14454 +
14455 +       /* update target timestamps */
14456 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
14457 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
14458 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
14459 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
14460 +
14461 +       /* remove whiteout for dentry */
14462 +       if (a->dst_wh_dentry) {
14463 +               a->h_path.dentry = a->dst_wh_dentry;
14464 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
14465 +                                         a->dst_dentry);
14466 +               if (unlikely(err))
14467 +                       goto out_diropq;
14468 +       }
14469 +
14470 +       /* remove whtmp */
14471 +       if (a->thargs)
14472 +               au_ren_del_whtmp(a); /* ignore this error */
14473 +
14474 +       err = 0;
14475 +       goto out_success;
14476 +
14477 + out_diropq:
14478 +       if (au_ftest_ren(a->flags, DIROPQ))
14479 +               au_ren_rev_diropq(err, a);
14480 + out_rename:
14481 +       if (!au_ftest_ren(a->flags, CPUP))
14482 +               au_ren_rev_rename(err, a);
14483 +       else
14484 +               au_ren_rev_cpup(err, a);
14485 + out_whtmp:
14486 +       if (a->thargs)
14487 +               au_ren_rev_whtmp(err, a);
14488 + out_whdst:
14489 +       dput(a->dst_wh_dentry);
14490 +       a->dst_wh_dentry = NULL;
14491 + out_whsrc:
14492 +       if (a->src_wh_dentry)
14493 +               au_ren_rev_whsrc(err, a);
14494 +       au_ren_rev_drop(a);
14495 + out_success:
14496 +       dput(a->src_wh_dentry);
14497 +       dput(a->dst_wh_dentry);
14498 + out_thargs:
14499 +       if (a->thargs) {
14500 +               dput(a->h_dst);
14501 +               au_whtmp_rmdir_free(a->thargs);
14502 +               a->thargs = NULL;
14503 +       }
14504 + out:
14505 +       return err;
14506 +}
14507 +
14508 +/* ---------------------------------------------------------------------- */
14509 +
14510 +/*
14511 + * test if @dentry dir can be rename destination or not.
14512 + * success means, it is a logically empty dir.
14513 + */
14514 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
14515 +{
14516 +       return au_test_empty(dentry, whlist);
14517 +}
14518 +
14519 +/*
14520 + * test if @dentry dir can be rename source or not.
14521 + * if it can, return 0 and @children is filled.
14522 + * success means,
14523 + * - it is a logically empty dir.
14524 + * - or, it exists on writable branch and has no children including whiteouts
14525 + *       on the lower branch.
14526 + */
14527 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
14528 +{
14529 +       int err;
14530 +       aufs_bindex_t bstart;
14531 +
14532 +       bstart = au_dbstart(dentry);
14533 +       if (bstart != btgt) {
14534 +               struct au_nhash *whlist;
14535 +
14536 +               whlist = au_nhash_alloc(dentry->d_sb, /*bend*/0, GFP_NOFS);
14537 +               err = PTR_ERR(whlist);
14538 +               if (IS_ERR(whlist))
14539 +                       goto out;
14540 +               err = au_test_empty(dentry, whlist);
14541 +               au_nhash_wh_free(whlist, /*bend*/0);
14542 +               goto out;
14543 +       }
14544 +
14545 +       if (bstart == au_dbtaildir(dentry))
14546 +               return 0; /* success */
14547 +
14548 +       err = au_test_empty_lower(dentry);
14549 +
14550 + out:
14551 +       if (err == -ENOTEMPTY) {
14552 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
14553 +                       " is not supported\n");
14554 +               err = -EXDEV;
14555 +       }
14556 +       return err;
14557 +}
14558 +
14559 +/* side effect: sets whlist and h_dentry */
14560 +static int au_ren_may_dir(struct au_ren_args *a)
14561 +{
14562 +       int err;
14563 +       struct dentry *d;
14564 +
14565 +       err = -ENOMEM;
14566 +       d = a->dst_dentry;
14567 +       a->whlist = au_nhash_alloc(d->d_sb, /*bend*/0, GFP_NOFS);
14568 +       if (unlikely(!a->whlist))
14569 +               goto out;
14570 +
14571 +       err = 0;
14572 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
14573 +               au_set_dbstart(d, a->dst_bstart);
14574 +               err = may_rename_dstdir(d, a->whlist);
14575 +               au_set_dbstart(d, a->btgt);
14576 +       }
14577 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
14578 +       if (unlikely(err))
14579 +               goto out;
14580 +
14581 +       d = a->src_dentry;
14582 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
14583 +       if (au_ftest_ren(a->flags, ISDIR)) {
14584 +               err = may_rename_srcdir(d, a->btgt);
14585 +               if (unlikely(err)) {
14586 +                       au_nhash_wh_free(a->whlist, /*bend*/0);
14587 +                       a->whlist = NULL;
14588 +               }
14589 +       }
14590 + out:
14591 +       return err;
14592 +}
14593 +
14594 +/* ---------------------------------------------------------------------- */
14595 +
14596 +/*
14597 + * simple tests for rename.
14598 + * following the checks in vfs, plus the parent-child relationship.
14599 + */
14600 +static int au_may_ren(struct au_ren_args *a)
14601 +{
14602 +       int err, isdir;
14603 +       struct inode *h_inode;
14604 +
14605 +       if (a->src_bstart == a->btgt) {
14606 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
14607 +                                au_ftest_ren(a->flags, ISDIR));
14608 +               if (unlikely(err))
14609 +                       goto out;
14610 +               err = -EINVAL;
14611 +               if (unlikely(a->src_h_dentry == a->h_trap))
14612 +                       goto out;
14613 +       }
14614 +
14615 +       err = 0;
14616 +       if (a->dst_bstart != a->btgt)
14617 +               goto out;
14618 +
14619 +       err = -EIO;
14620 +       h_inode = a->dst_h_dentry->d_inode;
14621 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
14622 +       if (!a->dst_dentry->d_inode) {
14623 +               if (unlikely(h_inode))
14624 +                       goto out;
14625 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
14626 +                                isdir);
14627 +       } else {
14628 +               if (unlikely(!h_inode || !h_inode->i_nlink))
14629 +                       goto out;
14630 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
14631 +                                isdir);
14632 +               if (unlikely(err))
14633 +                       goto out;
14634 +               err = -ENOTEMPTY;
14635 +               if (unlikely(a->dst_h_dentry == a->h_trap))
14636 +                       goto out;
14637 +               err = 0;
14638 +       }
14639 +
14640 + out:
14641 +       if (unlikely(err == -ENOENT || err == -EEXIST))
14642 +               err = -EIO;
14643 +       return err;
14644 +}
14645 +
14646 +/* ---------------------------------------------------------------------- */
14647 +
14648 +/*
14649 + * locking order
14650 + * (VFS)
14651 + * - src_dir and dir by lock_rename()
14652 + * - inode if exitsts
14653 + * (aufs)
14654 + * - lock all
14655 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
14656 + *     + si_read_lock
14657 + *     + di_write_lock2_child()
14658 + *       + di_write_lock_child()
14659 + *        + ii_write_lock_child()
14660 + *       + di_write_lock_child2()
14661 + *        + ii_write_lock_child2()
14662 + *     + src_parent and parent
14663 + *       + di_write_lock_parent()
14664 + *        + ii_write_lock_parent()
14665 + *       + di_write_lock_parent2()
14666 + *        + ii_write_lock_parent2()
14667 + *   + lower src_dir and dir by vfsub_lock_rename()
14668 + *   + verify the every relationships between child and parent. if any
14669 + *     of them failed, unlock all and return -EBUSY.
14670 + */
14671 +static void au_ren_unlock(struct au_ren_args *a)
14672 +{
14673 +       struct super_block *sb;
14674 +
14675 +       sb = a->dst_dentry->d_sb;
14676 +       if (au_ftest_ren(a->flags, MNT_WRITE))
14677 +               mnt_drop_write(a->br->br_mnt);
14678 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
14679 +                           a->dst_h_parent, a->dst_hdir);
14680 +}
14681 +
14682 +static int au_ren_lock(struct au_ren_args *a)
14683 +{
14684 +       int err;
14685 +       unsigned int udba;
14686 +
14687 +       err = 0;
14688 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
14689 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
14690 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
14691 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
14692 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
14693 +                                     a->dst_h_parent, a->dst_hdir);
14694 +       udba = au_opt_udba(a->src_dentry->d_sb);
14695 +       if (au_dbstart(a->src_dentry) == a->btgt)
14696 +               err = au_h_verify(a->src_h_dentry, udba,
14697 +                                 a->src_h_parent->d_inode, a->src_h_parent,
14698 +                                 a->br);
14699 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
14700 +               err = au_h_verify(a->dst_h_dentry, udba,
14701 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
14702 +                                 a->br);
14703 +       if (!err) {
14704 +               err = mnt_want_write(a->br->br_mnt);
14705 +               if (unlikely(err))
14706 +                       goto out_unlock;
14707 +               au_fset_ren(a->flags, MNT_WRITE);
14708 +               goto out; /* success */
14709 +       }
14710 +
14711 +       err = au_busy_or_stale();
14712 +
14713 + out_unlock:
14714 +       au_ren_unlock(a);
14715 + out:
14716 +       return err;
14717 +}
14718 +
14719 +/* ---------------------------------------------------------------------- */
14720 +
14721 +static void au_ren_refresh_dir(struct au_ren_args *a)
14722 +{
14723 +       struct inode *dir;
14724 +
14725 +       dir = a->dst_dir;
14726 +       dir->i_version++;
14727 +       if (au_ftest_ren(a->flags, ISDIR)) {
14728 +               /* is this updating defined in POSIX? */
14729 +               au_cpup_attr_timesizes(a->src_inode);
14730 +               au_cpup_attr_nlink(dir, /*force*/1);
14731 +               if (a->dst_inode) {
14732 +                       clear_nlink(a->dst_inode);
14733 +                       au_cpup_attr_timesizes(a->dst_inode);
14734 +               }
14735 +       }
14736 +       if (au_ibstart(dir) == a->btgt)
14737 +               au_cpup_attr_timesizes(dir);
14738 +
14739 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
14740 +               return;
14741 +
14742 +       dir = a->src_dir;
14743 +       dir->i_version++;
14744 +       if (au_ftest_ren(a->flags, ISDIR))
14745 +               au_cpup_attr_nlink(dir, /*force*/1);
14746 +       if (au_ibstart(dir) == a->btgt)
14747 +               au_cpup_attr_timesizes(dir);
14748 +}
14749 +
14750 +static void au_ren_refresh(struct au_ren_args *a)
14751 +{
14752 +       aufs_bindex_t bend, bindex;
14753 +       struct dentry *d, *h_d;
14754 +       struct inode *i, *h_i;
14755 +       struct super_block *sb;
14756 +
14757 +       d = a->src_dentry;
14758 +       au_set_dbwh(d, -1);
14759 +       bend = au_dbend(d);
14760 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
14761 +               h_d = au_h_dptr(d, bindex);
14762 +               if (h_d)
14763 +                       au_set_h_dptr(d, bindex, NULL);
14764 +       }
14765 +       au_set_dbend(d, a->btgt);
14766 +
14767 +       sb = d->d_sb;
14768 +       i = a->src_inode;
14769 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
14770 +               return; /* success */
14771 +
14772 +       bend = au_ibend(i);
14773 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
14774 +               h_i = au_h_iptr(i, bindex);
14775 +               if (h_i) {
14776 +                       au_xino_write0(sb, bindex, h_i->i_ino, 0);
14777 +                       /* ignore this error */
14778 +                       au_set_h_iptr(i, bindex, NULL, 0);
14779 +               }
14780 +       }
14781 +       au_set_ibend(i, a->btgt);
14782 +}
14783 +
14784 +/* ---------------------------------------------------------------------- */
14785 +
14786 +/* mainly for link(2) and rename(2) */
14787 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
14788 +{
14789 +       aufs_bindex_t bdiropq, bwh;
14790 +       struct dentry *parent;
14791 +       struct au_branch *br;
14792 +
14793 +       parent = dentry->d_parent;
14794 +       IMustLock(parent->d_inode); /* dir is locked */
14795 +
14796 +       bdiropq = au_dbdiropq(parent);
14797 +       bwh = au_dbwh(dentry);
14798 +       br = au_sbr(dentry->d_sb, btgt);
14799 +       if (au_br_rdonly(br)
14800 +           || (0 <= bdiropq && bdiropq < btgt)
14801 +           || (0 <= bwh && bwh < btgt))
14802 +               btgt = -1;
14803 +
14804 +       AuDbg("btgt %d\n", btgt);
14805 +       return btgt;
14806 +}
14807 +
14808 +/* sets src_bstart, dst_bstart and btgt */
14809 +static int au_ren_wbr(struct au_ren_args *a)
14810 +{
14811 +       int err;
14812 +       struct au_wr_dir_args wr_dir_args = {
14813 +               /* .force_btgt  = -1, */
14814 +               .flags          = AuWrDir_ADD_ENTRY
14815 +       };
14816 +
14817 +       a->src_bstart = au_dbstart(a->src_dentry);
14818 +       a->dst_bstart = au_dbstart(a->dst_dentry);
14819 +       if (au_ftest_ren(a->flags, ISDIR))
14820 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
14821 +       wr_dir_args.force_btgt = a->src_bstart;
14822 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
14823 +               wr_dir_args.force_btgt = a->dst_bstart;
14824 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
14825 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
14826 +       a->btgt = err;
14827 +
14828 +       return err;
14829 +}
14830 +
14831 +static void au_ren_dt(struct au_ren_args *a)
14832 +{
14833 +       a->h_path.dentry = a->src_h_parent;
14834 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
14835 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
14836 +               a->h_path.dentry = a->dst_h_parent;
14837 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
14838 +       }
14839 +
14840 +       au_fclr_ren(a->flags, DT_DSTDIR);
14841 +       if (!au_ftest_ren(a->flags, ISDIR))
14842 +               return;
14843 +
14844 +       a->h_path.dentry = a->src_h_dentry;
14845 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
14846 +       if (a->dst_h_dentry->d_inode) {
14847 +               au_fset_ren(a->flags, DT_DSTDIR);
14848 +               a->h_path.dentry = a->dst_h_dentry;
14849 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
14850 +       }
14851 +}
14852 +
14853 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
14854 +{
14855 +       struct dentry *h_d;
14856 +       struct mutex *h_mtx;
14857 +
14858 +       au_dtime_revert(a->src_dt + AuPARENT);
14859 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
14860 +               au_dtime_revert(a->dst_dt + AuPARENT);
14861 +
14862 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
14863 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
14864 +               h_mtx = &h_d->d_inode->i_mutex;
14865 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
14866 +               au_dtime_revert(a->src_dt + AuCHILD);
14867 +               mutex_unlock(h_mtx);
14868 +
14869 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
14870 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
14871 +                       h_mtx = &h_d->d_inode->i_mutex;
14872 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
14873 +                       au_dtime_revert(a->dst_dt + AuCHILD);
14874 +                       mutex_unlock(h_mtx);
14875 +               }
14876 +       }
14877 +}
14878 +
14879 +/* ---------------------------------------------------------------------- */
14880 +
14881 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
14882 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
14883 +{
14884 +       int err;
14885 +       /* reduce stack space */
14886 +       struct au_ren_args *a;
14887 +
14888 +       IMustLock(_src_dir);
14889 +       IMustLock(_dst_dir);
14890 +
14891 +       err = -ENOMEM;
14892 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
14893 +       a = kzalloc(sizeof(*a), GFP_NOFS);
14894 +       if (unlikely(!a))
14895 +               goto out;
14896 +
14897 +       a->src_dir = _src_dir;
14898 +       a->src_dentry = _src_dentry;
14899 +       a->src_inode = a->src_dentry->d_inode;
14900 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
14901 +       a->dst_dir = _dst_dir;
14902 +       a->dst_dentry = _dst_dentry;
14903 +       a->dst_inode = a->dst_dentry->d_inode;
14904 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
14905 +       if (a->dst_inode) {
14906 +               IMustLock(a->dst_inode);
14907 +               au_igrab(a->dst_inode);
14908 +       }
14909 +
14910 +       err = -ENOTDIR;
14911 +       if (S_ISDIR(a->src_inode->i_mode)) {
14912 +               au_fset_ren(a->flags, ISDIR);
14913 +               if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
14914 +                       goto out_free;
14915 +               aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
14916 +                                         AuLock_DIR | AuLock_FLUSH);
14917 +       } else
14918 +               aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
14919 +                                         AuLock_FLUSH);
14920 +
14921 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
14922 +       di_write_lock_parent(a->dst_parent);
14923 +
14924 +       /* which branch we process */
14925 +       err = au_ren_wbr(a);
14926 +       if (unlikely(err < 0))
14927 +               goto out_unlock;
14928 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
14929 +       a->h_path.mnt = a->br->br_mnt;
14930 +
14931 +       /* are they available to be renamed */
14932 +       err = au_ren_may_dir(a);
14933 +       if (unlikely(err))
14934 +               goto out_unlock;
14935 +
14936 +       /* prepare the writable parent dir on the same branch */
14937 +       if (a->dst_bstart == a->btgt) {
14938 +               au_fset_ren(a->flags, WHDST);
14939 +       } else {
14940 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
14941 +               if (unlikely(err))
14942 +                       goto out_children;
14943 +       }
14944 +
14945 +       if (a->src_dir != a->dst_dir) {
14946 +               /*
14947 +                * this temporary unlock is safe,
14948 +                * because both dir->i_mutex are locked.
14949 +                */
14950 +               di_write_unlock(a->dst_parent);
14951 +               di_write_lock_parent(a->src_parent);
14952 +               err = au_wr_dir_need_wh(a->src_dentry,
14953 +                                       au_ftest_ren(a->flags, ISDIR),
14954 +                                       &a->btgt);
14955 +               di_write_unlock(a->src_parent);
14956 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
14957 +               au_fclr_ren(a->flags, ISSAMEDIR);
14958 +       } else
14959 +               err = au_wr_dir_need_wh(a->src_dentry,
14960 +                                       au_ftest_ren(a->flags, ISDIR),
14961 +                                       &a->btgt);
14962 +       if (unlikely(err < 0))
14963 +               goto out_children;
14964 +       if (err)
14965 +               au_fset_ren(a->flags, WHSRC);
14966 +
14967 +       /* lock them all */
14968 +       err = au_ren_lock(a);
14969 +       if (unlikely(err))
14970 +               goto out_children;
14971 +
14972 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE)) {
14973 +               err = au_may_ren(a);
14974 +               if (unlikely(err))
14975 +                       goto out_hdir;
14976 +       }
14977 +
14978 +       /* store timestamps to be revertible */
14979 +       au_ren_dt(a);
14980 +
14981 +       /* here we go */
14982 +       err = do_rename(a);
14983 +       if (unlikely(err))
14984 +               goto out_dt;
14985 +
14986 +       /* update dir attributes */
14987 +       au_ren_refresh_dir(a);
14988 +
14989 +       /* dput/iput all lower dentries */
14990 +       au_ren_refresh(a);
14991 +
14992 +       goto out_hdir; /* success */
14993 +
14994 + out_dt:
14995 +       au_ren_rev_dt(err, a);
14996 + out_hdir:
14997 +       au_ren_unlock(a);
14998 + out_children:
14999 +       if (a->whlist)
15000 +               au_nhash_wh_free(a->whlist, /*bend*/0);
15001 + out_unlock:
15002 +       if (unlikely(err && au_ftest_ren(a->flags, ISDIR))) {
15003 +               au_update_dbstart(a->dst_dentry);
15004 +               d_drop(a->dst_dentry);
15005 +       }
15006 +       if (!err) {
15007 +               d_move(a->src_dentry, a->dst_dentry);
15008 +               if (a->dst_inode
15009 +                   && (a->dst_inode->i_nlink <= 1
15010 +                       || au_ftest_ren(a->flags, ISDIR)))
15011 +                       a->dst_inode->i_flags |= S_DEAD;
15012 +       }
15013 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
15014 +               di_write_unlock(a->dst_parent);
15015 +       else
15016 +               di_write_unlock2(a->src_parent, a->dst_parent);
15017 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
15018 + out_free:
15019 +       iput(a->dst_inode);
15020 +       if (a->thargs)
15021 +               au_whtmp_rmdir_free(a->thargs);
15022 +       kfree(a);
15023 + out:
15024 +       return err;
15025 +}
15026 diff --git a/fs/aufs/iinfo.c b/fs/aufs/iinfo.c
15027 new file mode 100644
15028 index 0000000..b1cff91
15029 --- /dev/null
15030 +++ b/fs/aufs/iinfo.c
15031 @@ -0,0 +1,262 @@
15032 +/*
15033 + * Copyright (C) 2005-2009 Junjiro R. Okajima
15034 + *
15035 + * This program, aufs is free software; you can redistribute it and/or modify
15036 + * it under the terms of the GNU General Public License as published by
15037 + * the Free Software Foundation; either version 2 of the License, or
15038 + * (at your option) any later version.
15039 + */
15040 +
15041 +/*
15042 + * inode private data
15043 + */
15044 +
15045 +#include "aufs.h"
15046 +
15047 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15048 +{
15049 +       struct inode *h_inode;
15050 +
15051 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15052 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15053 +       return h_inode;
15054 +}
15055 +
15056 +/* todo: hard/soft set? */
15057 +void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
15058 +{
15059 +       struct au_iinfo *iinfo = au_ii(inode);
15060 +       struct inode *h_inode;
15061 +
15062 +       iinfo->ii_bstart = bindex;
15063 +       h_inode = iinfo->ii_hinode[bindex + 0].hi_inode;
15064 +       if (h_inode)
15065 +               au_cpup_igen(inode, h_inode);
15066 +}
15067 +
15068 +void au_hiput(struct au_hinode *hinode)
15069 +{
15070 +       au_hin_free(hinode);
15071 +       dput(hinode->hi_whdentry);
15072 +       iput(hinode->hi_inode);
15073 +}
15074 +
15075 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15076 +{
15077 +       unsigned int flags;
15078 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15079 +
15080 +       flags = 0;
15081 +       if (au_opt_test(mnt_flags, XINO))
15082 +               au_fset_hi(flags, XINO);
15083 +       if (isdir && au_opt_test(mnt_flags, UDBA_HINOTIFY))
15084 +               au_fset_hi(flags, HINOTIFY);
15085 +       return flags;
15086 +}
15087 +
15088 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15089 +                  struct inode *h_inode, unsigned int flags)
15090 +{
15091 +       struct au_hinode *hinode;
15092 +       struct inode *hi;
15093 +       struct au_iinfo *iinfo = au_ii(inode);
15094 +
15095 +       hinode = iinfo->ii_hinode + bindex;
15096 +       hi = hinode->hi_inode;
15097 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15098 +       AuDebugOn(h_inode && hi);
15099 +
15100 +       if (hi)
15101 +               au_hiput(hinode);
15102 +       hinode->hi_inode = h_inode;
15103 +       if (h_inode) {
15104 +               int err;
15105 +               struct super_block *sb = inode->i_sb;
15106 +               struct au_branch *br;
15107 +
15108 +               if (bindex == iinfo->ii_bstart)
15109 +                       au_cpup_igen(inode, h_inode);
15110 +               br = au_sbr(sb, bindex);
15111 +               hinode->hi_id = br->br_id;
15112 +               if (au_ftest_hi(flags, XINO)) {
15113 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15114 +                                           inode->i_ino);
15115 +                       if (unlikely(err))
15116 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15117 +               }
15118 +
15119 +               if (au_ftest_hi(flags, HINOTIFY)
15120 +                   && au_br_hinotifyable(br->br_perm)) {
15121 +                       err = au_hin_alloc(hinode, inode, h_inode);
15122 +                       if (unlikely(err))
15123 +                               AuIOErr1("au_hin_alloc() %d\n", err);
15124 +               }
15125 +       }
15126 +}
15127 +
15128 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15129 +                 struct dentry *h_wh)
15130 +{
15131 +       struct au_hinode *hinode;
15132 +
15133 +       hinode = au_ii(inode)->ii_hinode + bindex;
15134 +       AuDebugOn(hinode->hi_whdentry);
15135 +       hinode->hi_whdentry = h_wh;
15136 +}
15137 +
15138 +void au_update_iigen(struct inode *inode)
15139 +{
15140 +       atomic_set(&au_ii(inode)->ii_generation, au_sigen(inode->i_sb));
15141 +       /* smp_mb(); */ /* atomic_set */
15142 +}
15143 +
15144 +/* it may be called at remount time, too */
15145 +void au_update_brange(struct inode *inode, int do_put_zero)
15146 +{
15147 +       struct au_iinfo *iinfo;
15148 +
15149 +       iinfo = au_ii(inode);
15150 +       if (!iinfo || iinfo->ii_bstart < 0)
15151 +               return;
15152 +
15153 +       if (do_put_zero) {
15154 +               aufs_bindex_t bindex;
15155 +
15156 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15157 +                    bindex++) {
15158 +                       struct inode *h_i;
15159 +
15160 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
15161 +                       if (h_i && !h_i->i_nlink)
15162 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15163 +               }
15164 +       }
15165 +
15166 +       iinfo->ii_bstart = -1;
15167 +       while (++iinfo->ii_bstart <= iinfo->ii_bend)
15168 +               if (iinfo->ii_hinode[0 + iinfo->ii_bstart].hi_inode)
15169 +                       break;
15170 +       if (iinfo->ii_bstart > iinfo->ii_bend) {
15171 +               iinfo->ii_bstart = -1;
15172 +               iinfo->ii_bend = -1;
15173 +               return;
15174 +       }
15175 +
15176 +       iinfo->ii_bend++;
15177 +       while (0 <= --iinfo->ii_bend)
15178 +               if (iinfo->ii_hinode[0 + iinfo->ii_bend].hi_inode)
15179 +                       break;
15180 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend || iinfo->ii_bend < 0);
15181 +}
15182 +
15183 +/* ---------------------------------------------------------------------- */
15184 +
15185 +int au_iinfo_init(struct inode *inode)
15186 +{
15187 +       struct au_iinfo *iinfo;
15188 +       struct super_block *sb;
15189 +       int nbr, i;
15190 +
15191 +       sb = inode->i_sb;
15192 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15193 +       nbr = au_sbend(sb) + 1;
15194 +       if (unlikely(nbr <= 0))
15195 +               nbr = 1;
15196 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15197 +       if (iinfo->ii_hinode) {
15198 +               for (i = 0; i < nbr; i++)
15199 +                       iinfo->ii_hinode[i].hi_id = -1;
15200 +
15201 +               atomic_set(&iinfo->ii_generation, au_sigen(sb));
15202 +               /* smp_mb(); */ /* atomic_set */
15203 +               init_rwsem(&iinfo->ii_rwsem);
15204 +               iinfo->ii_bstart = -1;
15205 +               iinfo->ii_bend = -1;
15206 +               iinfo->ii_vdir = NULL;
15207 +               return 0;
15208 +       }
15209 +       return -ENOMEM;
15210 +}
15211 +
15212 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15213 +{
15214 +       int err, sz;
15215 +       struct au_hinode *hip;
15216 +
15217 +       err = -ENOMEM;
15218 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15219 +       if (!sz)
15220 +               sz = sizeof(*hip);
15221 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15222 +       if (hip) {
15223 +               iinfo->ii_hinode = hip;
15224 +               err = 0;
15225 +       }
15226 +
15227 +       return err;
15228 +}
15229 +
15230 +static int au_iinfo_write0(struct super_block *sb, struct au_hinode *hinode,
15231 +                          ino_t ino)
15232 +{
15233 +       int err;
15234 +       aufs_bindex_t bindex;
15235 +       unsigned char locked;
15236 +
15237 +       err = 0;
15238 +       locked = !!si_noflush_read_trylock(sb);
15239 +       bindex = au_br_index(sb, hinode->hi_id);
15240 +       if (bindex >= 0)
15241 +               err = au_xino_write0(sb, bindex, hinode->hi_inode->i_ino, ino);
15242 +       /* error action? */
15243 +       if (locked)
15244 +               si_read_unlock(sb);
15245 +       return err;
15246 +}
15247 +
15248 +void au_iinfo_fin(struct inode *inode)
15249 +{
15250 +       ino_t ino;
15251 +       aufs_bindex_t bend;
15252 +       unsigned char unlinked = !inode->i_nlink || IS_DEADDIR(inode);
15253 +       struct au_iinfo *iinfo;
15254 +       struct au_hinode *hi;
15255 +       struct super_block *sb;
15256 +
15257 +       if (unlinked) {
15258 +               int err = au_xigen_inc(inode);
15259 +               if (unlikely(err))
15260 +                       AuWarn1("failed resetting i_generation, %d\n", err);
15261 +       }
15262 +
15263 +       iinfo = au_ii(inode);
15264 +       /* bad_inode case */
15265 +       if (!iinfo)
15266 +               return;
15267 +
15268 +       if (iinfo->ii_vdir)
15269 +               au_vdir_free(iinfo->ii_vdir);
15270 +
15271 +       if (iinfo->ii_bstart >= 0) {
15272 +               sb = inode->i_sb;
15273 +               ino = 0;
15274 +               if (unlinked)
15275 +                       ino = inode->i_ino;
15276 +               hi = iinfo->ii_hinode + iinfo->ii_bstart;
15277 +               bend = iinfo->ii_bend;
15278 +               while (iinfo->ii_bstart++ <= bend) {
15279 +                       if (hi->hi_inode) {
15280 +                               if (unlinked || !hi->hi_inode->i_nlink) {
15281 +                                       au_iinfo_write0(sb, hi, ino);
15282 +                                       /* ignore this error */
15283 +                                       ino = 0;
15284 +                               }
15285 +                               au_hiput(hi);
15286 +                       }
15287 +                       hi++;
15288 +               }
15289 +       }
15290 +
15291 +       kfree(iinfo->ii_hinode);
15292 +       au_rwsem_destroy(&iinfo->ii_rwsem);
15293 +}
15294 diff --git a/fs/aufs/inode.c b/fs/aufs/inode.c
15295 new file mode 100644
15296 index 0000000..c4a962b
15297 --- /dev/null
15298 +++ b/fs/aufs/inode.c
15299 @@ -0,0 +1,356 @@
15300 +/*
15301 + * Copyright (C) 2005-2009 Junjiro R. Okajima
15302 + *
15303 + * This program, aufs is free software; you can redistribute it and/or modify
15304 + * it under the terms of the GNU General Public License as published by
15305 + * the Free Software Foundation; either version 2 of the License, or
15306 + * (at your option) any later version.
15307 + */
15308 +
15309 +/*
15310 + * inode functions
15311 + */
15312 +
15313 +#include "aufs.h"
15314 +
15315 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15316 +{
15317 +       au_cpup_attr_all(inode, /*force*/0);
15318 +       au_update_iigen(inode);
15319 +       if (do_version)
15320 +               inode->i_version++;
15321 +}
15322 +
15323 +int au_refresh_hinode_self(struct inode *inode, int do_attr)
15324 +{
15325 +       int err;
15326 +       aufs_bindex_t bindex, new_bindex;
15327 +       unsigned char update;
15328 +       struct inode *first;
15329 +       struct au_hinode *p, *q, tmp;
15330 +       struct super_block *sb;
15331 +       struct au_iinfo *iinfo;
15332 +
15333 +       update = 0;
15334 +       sb = inode->i_sb;
15335 +       iinfo = au_ii(inode);
15336 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15337 +       if (unlikely(err))
15338 +               goto out;
15339 +
15340 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15341 +       first = p->hi_inode;
15342 +       err = 0;
15343 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15344 +            bindex++, p++) {
15345 +               if (!p->hi_inode)
15346 +                       continue;
15347 +
15348 +               new_bindex = au_br_index(sb, p->hi_id);
15349 +               if (new_bindex == bindex)
15350 +                       continue;
15351 +
15352 +               if (new_bindex < 0) {
15353 +                       update++;
15354 +                       au_hiput(p);
15355 +                       p->hi_inode = NULL;
15356 +                       continue;
15357 +               }
15358 +
15359 +               if (new_bindex < iinfo->ii_bstart)
15360 +                       iinfo->ii_bstart = new_bindex;
15361 +               if (iinfo->ii_bend < new_bindex)
15362 +                       iinfo->ii_bend = new_bindex;
15363 +               /* swap two lower inode, and loop again */
15364 +               q = iinfo->ii_hinode + new_bindex;
15365 +               tmp = *q;
15366 +               *q = *p;
15367 +               *p = tmp;
15368 +               if (tmp.hi_inode) {
15369 +                       bindex--;
15370 +                       p--;
15371 +               }
15372 +       }
15373 +       au_update_brange(inode, /*do_put_zero*/0);
15374 +       if (do_attr)
15375 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
15376 +
15377 + out:
15378 +       return err;
15379 +}
15380 +
15381 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
15382 +{
15383 +       int err, update;
15384 +       unsigned int flags;
15385 +       aufs_bindex_t bindex, bend;
15386 +       unsigned char isdir;
15387 +       struct inode *first;
15388 +       struct au_hinode *p;
15389 +       struct au_iinfo *iinfo;
15390 +
15391 +       err = au_refresh_hinode_self(inode, /*do_attr*/0);
15392 +       if (unlikely(err))
15393 +               goto out;
15394 +
15395 +       update = 0;
15396 +       iinfo = au_ii(inode);
15397 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15398 +       first = p->hi_inode;
15399 +       isdir = S_ISDIR(inode->i_mode);
15400 +       flags = au_hi_flags(inode, isdir);
15401 +       bend = au_dbend(dentry);
15402 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
15403 +               struct inode *h_i;
15404 +               struct dentry *h_d;
15405 +
15406 +               h_d = au_h_dptr(dentry, bindex);
15407 +               if (!h_d || !h_d->d_inode)
15408 +                       continue;
15409 +
15410 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
15411 +                       h_i = au_h_iptr(inode, bindex);
15412 +                       if (h_i) {
15413 +                               if (h_i == h_d->d_inode)
15414 +                                       continue;
15415 +                               err = -EIO;
15416 +                               break;
15417 +                       }
15418 +               }
15419 +               if (bindex < iinfo->ii_bstart)
15420 +                       iinfo->ii_bstart = bindex;
15421 +               if (iinfo->ii_bend < bindex)
15422 +                       iinfo->ii_bend = bindex;
15423 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
15424 +               update = 1;
15425 +       }
15426 +       au_update_brange(inode, /*do_put_zero*/0);
15427 +
15428 +       if (unlikely(err))
15429 +               goto out;
15430 +
15431 +       au_refresh_hinode_attr(inode, update && isdir);
15432 +
15433 + out:
15434 +       return err;
15435 +}
15436 +
15437 +static int set_inode(struct inode *inode, struct dentry *dentry)
15438 +{
15439 +       int err;
15440 +       unsigned int flags;
15441 +       umode_t mode;
15442 +       aufs_bindex_t bindex, bstart, btail;
15443 +       unsigned char isdir;
15444 +       struct dentry *h_dentry;
15445 +       struct inode *h_inode;
15446 +       struct au_iinfo *iinfo;
15447 +
15448 +       err = 0;
15449 +       isdir = 0;
15450 +       bstart = au_dbstart(dentry);
15451 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
15452 +       mode = h_inode->i_mode;
15453 +       switch (mode & S_IFMT) {
15454 +       case S_IFREG:
15455 +               btail = au_dbtail(dentry);
15456 +               inode->i_op = &aufs_iop;
15457 +               inode->i_fop = &aufs_file_fop;
15458 +               inode->i_mapping->a_ops = &aufs_aop;
15459 +               break;
15460 +       case S_IFDIR:
15461 +               isdir = 1;
15462 +               btail = au_dbtaildir(dentry);
15463 +               inode->i_op = &aufs_dir_iop;
15464 +               inode->i_fop = &aufs_dir_fop;
15465 +               break;
15466 +       case S_IFLNK:
15467 +               btail = au_dbtail(dentry);
15468 +               inode->i_op = &aufs_symlink_iop;
15469 +               break;
15470 +       case S_IFBLK:
15471 +       case S_IFCHR:
15472 +       case S_IFIFO:
15473 +       case S_IFSOCK:
15474 +               btail = au_dbtail(dentry);
15475 +               inode->i_op = &aufs_iop;
15476 +               init_special_inode(inode, mode, h_inode->i_rdev);
15477 +               break;
15478 +       default:
15479 +               AuIOErr("Unknown file type 0%o\n", mode);
15480 +               err = -EIO;
15481 +               goto out;
15482 +       }
15483 +
15484 +       flags = au_hi_flags(inode, isdir);
15485 +       iinfo = au_ii(inode);
15486 +       iinfo->ii_bstart = bstart;
15487 +       iinfo->ii_bend = btail;
15488 +       for (bindex = bstart; bindex <= btail; bindex++) {
15489 +               h_dentry = au_h_dptr(dentry, bindex);
15490 +               if (h_dentry)
15491 +                       au_set_h_iptr(inode, bindex,
15492 +                                     au_igrab(h_dentry->d_inode), flags);
15493 +       }
15494 +       au_cpup_attr_all(inode, /*force*/1);
15495 +
15496 + out:
15497 +       return err;
15498 +}
15499 +
15500 +/* successful returns with iinfo write_locked */
15501 +static int reval_inode(struct inode *inode, struct dentry *dentry, int *matched)
15502 +{
15503 +       int err;
15504 +       aufs_bindex_t bindex, bend;
15505 +       struct inode *h_inode, *h_dinode;
15506 +
15507 +       *matched = 0;
15508 +
15509 +       /*
15510 +        * before this function, if aufs got any iinfo lock, it must be only
15511 +        * one, the parent dir.
15512 +        * it can happen by UDBA and the obsoleted inode number.
15513 +        */
15514 +       err = -EIO;
15515 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
15516 +               goto out;
15517 +
15518 +       ii_write_lock_new_child(inode);
15519 +       if (unlikely(IS_DEADDIR(inode)))
15520 +               goto out_unlock;
15521 +
15522 +       err = 0;
15523 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
15524 +       bend = au_ibend(inode);
15525 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
15526 +               h_inode = au_h_iptr(inode, bindex);
15527 +               if (h_inode && h_inode == h_dinode) {
15528 +                       *matched = 1;
15529 +                       err = 0;
15530 +                       if (au_iigen(inode) != au_digen(dentry))
15531 +                               err = au_refresh_hinode(inode, dentry);
15532 +                       break;
15533 +               }
15534 +       }
15535 +
15536 + out_unlock:
15537 +       if (unlikely(err))
15538 +               ii_write_unlock(inode);
15539 + out:
15540 +       return err;
15541 +}
15542 +
15543 +/* successful returns with iinfo write_locked */
15544 +/* todo: return with unlocked? */
15545 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
15546 +{
15547 +       struct inode *inode;
15548 +       struct dentry *h_dentry;
15549 +       struct super_block *sb;
15550 +       ino_t h_ino, ino;
15551 +       int err, match;
15552 +       aufs_bindex_t bstart;
15553 +
15554 +       sb = dentry->d_sb;
15555 +       bstart = au_dbstart(dentry);
15556 +       h_dentry = au_h_dptr(dentry, bstart);
15557 +       h_ino = h_dentry->d_inode->i_ino;
15558 +       err = au_xino_read(sb, bstart, h_ino, &ino);
15559 +       inode = ERR_PTR(err);
15560 +       if (unlikely(err))
15561 +               goto out;
15562 + new_ino:
15563 +       if (!ino) {
15564 +               ino = au_xino_new_ino(sb);
15565 +               if (unlikely(!ino)) {
15566 +                       inode = ERR_PTR(-EIO);
15567 +                       goto out;
15568 +               }
15569 +       }
15570 +
15571 +       AuDbg("i%lu\n", (unsigned long)ino);
15572 +       inode = au_iget_locked(sb, ino);
15573 +       err = PTR_ERR(inode);
15574 +       if (IS_ERR(inode))
15575 +               goto out;
15576 +
15577 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
15578 +       if (inode->i_state & I_NEW) {
15579 +               ii_write_lock_new_child(inode);
15580 +               err = set_inode(inode, dentry);
15581 +               unlock_new_inode(inode);
15582 +               if (!err)
15583 +                       goto out; /* success */
15584 +
15585 +               iget_failed(inode);
15586 +               ii_write_unlock(inode);
15587 +               goto out_iput;
15588 +       } else if (!must_new) {
15589 +               err = reval_inode(inode, dentry, &match);
15590 +               if (!err)
15591 +                       goto out; /* success */
15592 +               else if (match)
15593 +                       goto out_iput;
15594 +       }
15595 +
15596 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
15597 +               AuWarn1("Un-notified UDBA or repeatedly renamed dir,"
15598 +                       " b%d, %s, %.*s, hi%lu, i%lu.\n",
15599 +                       bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
15600 +                       (unsigned long)h_ino, (unsigned long)ino);
15601 +       ino = 0;
15602 +       err = au_xino_write0(sb, bstart, h_ino, 0);
15603 +       if (!err) {
15604 +               iput(inode);
15605 +               goto new_ino;
15606 +       }
15607 +
15608 + out_iput:
15609 +       iput(inode);
15610 +       inode = ERR_PTR(err);
15611 + out:
15612 +       return inode;
15613 +}
15614 +
15615 +/* ---------------------------------------------------------------------- */
15616 +
15617 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15618 +              struct inode *inode)
15619 +{
15620 +       int err;
15621 +
15622 +       err = au_br_rdonly(au_sbr(sb, bindex));
15623 +
15624 +       /* pseudo-link after flushed may happen out of bounds */
15625 +       if (!err
15626 +           && inode
15627 +           && au_ibstart(inode) <= bindex
15628 +           && bindex <= au_ibend(inode)) {
15629 +               /*
15630 +                * permission check is unnecessary since vfsub routine
15631 +                * will be called later
15632 +                */
15633 +               struct inode *hi = au_h_iptr(inode, bindex);
15634 +               if (hi)
15635 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
15636 +       }
15637 +
15638 +       return err;
15639 +}
15640 +
15641 +int au_test_h_perm(struct inode *h_inode, int mask)
15642 +{
15643 +       if (!current_fsuid())
15644 +               return 0;
15645 +       return inode_permission(h_inode, mask);
15646 +}
15647 +
15648 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
15649 +{
15650 +       if (au_test_nfs(h_inode->i_sb)
15651 +           && (mask & MAY_WRITE)
15652 +           && S_ISDIR(h_inode->i_mode))
15653 +               mask |= MAY_READ; /* force permission check */
15654 +       return au_test_h_perm(h_inode, mask);
15655 +}
15656 diff --git a/fs/aufs/inode.h b/fs/aufs/inode.h
15657 new file mode 100644
15658 index 0000000..ffda951
15659 --- /dev/null
15660 +++ b/fs/aufs/inode.h
15661 @@ -0,0 +1,471 @@
15662 +/*
15663 + * Copyright (C) 2005-2009 Junjiro R. Okajima
15664 + *
15665 + * This program, aufs is free software; you can redistribute it and/or modify
15666 + * it under the terms of the GNU General Public License as published by
15667 + * the Free Software Foundation; either version 2 of the License, or
15668 + * (at your option) any later version.
15669 + */
15670 +
15671 +/*
15672 + * inode operations
15673 + */
15674 +
15675 +#ifndef __AUFS_INODE_H__
15676 +#define __AUFS_INODE_H__
15677 +
15678 +#ifdef __KERNEL__
15679 +
15680 +#include <linux/fs.h>
15681 +#include <linux/inotify.h>
15682 +#include <linux/namei.h>
15683 +#include <linux/aufs_type.h>
15684 +#include "rwsem.h"
15685 +
15686 +struct au_hinotify {
15687 +#ifdef CONFIG_AUFS_HINOTIFY
15688 +       struct inotify_watch    hin_watch;
15689 +       struct inode            *hin_aufs_inode;        /* no get/put */
15690 +#endif
15691 +};
15692 +
15693 +struct au_hinode {
15694 +       struct inode            *hi_inode;
15695 +       aufs_bindex_t           hi_id;
15696 +#ifdef CONFIG_AUFS_HINOTIFY
15697 +       struct au_hinotify      *hi_notify;
15698 +#endif
15699 +
15700 +       /* reference to the copied-up whiteout with get/put */
15701 +       struct dentry           *hi_whdentry;
15702 +};
15703 +
15704 +struct au_vdir;
15705 +struct au_iinfo {
15706 +       atomic_t                ii_generation;
15707 +       struct super_block      *ii_hsb1;       /* no get/put */
15708 +
15709 +       struct rw_semaphore     ii_rwsem;
15710 +       aufs_bindex_t           ii_bstart, ii_bend;
15711 +       __u32                   ii_higen;
15712 +       struct au_hinode        *ii_hinode;
15713 +       struct au_vdir          *ii_vdir;
15714 +};
15715 +
15716 +struct au_icntnr {
15717 +       struct au_iinfo iinfo;
15718 +       struct inode vfs_inode;
15719 +};
15720 +
15721 +/* au_pin flags */
15722 +#define AuPin_DI_LOCKED                1
15723 +#define AuPin_MNT_WRITE                (1 << 1)
15724 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
15725 +#define au_fset_pin(flags, name)       { (flags) |= AuPin_##name; }
15726 +#define au_fclr_pin(flags, name)       { (flags) &= ~AuPin_##name; }
15727 +
15728 +struct au_pin {
15729 +       /* input */
15730 +       struct dentry *dentry;
15731 +       unsigned int udba;
15732 +       unsigned char lsc_di, lsc_hi, flags;
15733 +       aufs_bindex_t bindex;
15734 +
15735 +       /* output */
15736 +       struct dentry *parent;
15737 +       struct au_hinode *hdir;
15738 +       struct vfsmount *h_mnt;
15739 +};
15740 +
15741 +/* ---------------------------------------------------------------------- */
15742 +
15743 +static inline struct au_iinfo *au_ii(struct inode *inode)
15744 +{
15745 +       struct au_iinfo *iinfo;
15746 +
15747 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15748 +       if (iinfo->ii_hinode)
15749 +               return iinfo;
15750 +       return NULL; /* debugging bad_inode case */
15751 +}
15752 +
15753 +/* ---------------------------------------------------------------------- */
15754 +
15755 +/* inode.c */
15756 +int au_refresh_hinode_self(struct inode *inode, int do_attr);
15757 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
15758 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
15759 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15760 +              struct inode *inode);
15761 +int au_test_h_perm(struct inode *h_inode, int mask);
15762 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
15763 +
15764 +/* i_op.c */
15765 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
15766 +
15767 +/* au_wr_dir flags */
15768 +#define AuWrDir_ADD_ENTRY      1
15769 +#define AuWrDir_ISDIR          (1 << 1)
15770 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
15771 +#define au_fset_wrdir(flags, name)     { (flags) |= AuWrDir_##name; }
15772 +#define au_fclr_wrdir(flags, name)     { (flags) &= ~AuWrDir_##name; }
15773 +
15774 +struct au_wr_dir_args {
15775 +       aufs_bindex_t force_btgt;
15776 +       unsigned char flags;
15777 +};
15778 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
15779 +             struct au_wr_dir_args *args);
15780 +
15781 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
15782 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
15783 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
15784 +                unsigned int udba, unsigned char flags);
15785 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
15786 +          unsigned int udba, unsigned char flags) __must_check;
15787 +int au_do_pin(struct au_pin *pin) __must_check;
15788 +void au_unpin(struct au_pin *pin);
15789 +
15790 +/* i_op_add.c */
15791 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15792 +              struct dentry *h_parent, int isdir);
15793 +int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);
15794 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
15795 +int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
15796 +               struct nameidata *nd);
15797 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
15798 +             struct dentry *dentry);
15799 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
15800 +
15801 +/* i_op_del.c */
15802 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
15803 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
15804 +              struct dentry *h_parent, int isdir);
15805 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
15806 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
15807 +
15808 +/* i_op_ren.c */
15809 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
15810 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
15811 +               struct inode *dir, struct dentry *dentry);
15812 +
15813 +/* iinfo.c */
15814 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
15815 +void au_hiput(struct au_hinode *hinode);
15816 +void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex);
15817 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15818 +                 struct dentry *h_wh);
15819 +unsigned int au_hi_flags(struct inode *inode, int isdir);
15820 +
15821 +/* hinode flags */
15822 +#define AuHi_XINO      1
15823 +#define AuHi_HINOTIFY  (1 << 1)
15824 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
15825 +#define au_fset_hi(flags, name)                { (flags) |= AuHi_##name; }
15826 +#define au_fclr_hi(flags, name)                { (flags) &= ~AuHi_##name; }
15827 +
15828 +#ifndef CONFIG_AUFS_HINOTIFY
15829 +#undef AuHi_HINOTIFY
15830 +#define AuHi_HINOTIFY  0
15831 +#endif
15832 +
15833 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15834 +                  struct inode *h_inode, unsigned int flags);
15835 +
15836 +void au_update_iigen(struct inode *inode);
15837 +void au_update_brange(struct inode *inode, int do_put_zero);
15838 +
15839 +int au_iinfo_init(struct inode *inode);
15840 +void au_iinfo_fin(struct inode *inode);
15841 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
15842 +
15843 +/* plink.c */
15844 +void au_plink_block_maintain(struct super_block *sb);
15845 +#ifdef CONFIG_AUFS_DEBUG
15846 +void au_plink_list(struct super_block *sb);
15847 +#else
15848 +static inline void au_plink_list(struct super_block *sb)
15849 +{
15850 +       /* nothing */
15851 +}
15852 +#endif
15853 +int au_plink_test(struct inode *inode);
15854 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
15855 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
15856 +                    struct dentry *h_dentry);
15857 +void au_plink_put(struct super_block *sb);
15858 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
15859 +
15860 +/* ---------------------------------------------------------------------- */
15861 +
15862 +/* lock subclass for iinfo */
15863 +enum {
15864 +       AuLsc_II_CHILD,         /* child first */
15865 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hinotify */
15866 +       AuLsc_II_CHILD3,        /* copyup dirs */
15867 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
15868 +       AuLsc_II_PARENT2,
15869 +       AuLsc_II_PARENT3,       /* copyup dirs */
15870 +       AuLsc_II_NEW_CHILD
15871 +};
15872 +
15873 +/*
15874 + * ii_read_lock_child, ii_write_lock_child,
15875 + * ii_read_lock_child2, ii_write_lock_child2,
15876 + * ii_read_lock_child3, ii_write_lock_child3,
15877 + * ii_read_lock_parent, ii_write_lock_parent,
15878 + * ii_read_lock_parent2, ii_write_lock_parent2,
15879 + * ii_read_lock_parent3, ii_write_lock_parent3,
15880 + * ii_read_lock_new_child, ii_write_lock_new_child,
15881 + */
15882 +#define AuReadLockFunc(name, lsc) \
15883 +static inline void ii_read_lock_##name(struct inode *i) \
15884 +{ \
15885 +       down_read_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15886 +}
15887 +
15888 +#define AuWriteLockFunc(name, lsc) \
15889 +static inline void ii_write_lock_##name(struct inode *i) \
15890 +{ \
15891 +       down_write_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15892 +}
15893 +
15894 +#define AuRWLockFuncs(name, lsc) \
15895 +       AuReadLockFunc(name, lsc) \
15896 +       AuWriteLockFunc(name, lsc)
15897 +
15898 +AuRWLockFuncs(child, CHILD);
15899 +AuRWLockFuncs(child2, CHILD2);
15900 +AuRWLockFuncs(child3, CHILD3);
15901 +AuRWLockFuncs(parent, PARENT);
15902 +AuRWLockFuncs(parent2, PARENT2);
15903 +AuRWLockFuncs(parent3, PARENT3);
15904 +AuRWLockFuncs(new_child, NEW_CHILD);
15905 +
15906 +#undef AuReadLockFunc
15907 +#undef AuWriteLockFunc
15908 +#undef AuRWLockFuncs
15909 +
15910 +/*
15911 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
15912 + */
15913 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
15914 +
15915 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
15916 +
15917 +/* ---------------------------------------------------------------------- */
15918 +
15919 +static inline unsigned int au_iigen(struct inode *inode)
15920 +{
15921 +       return atomic_read(&au_ii(inode)->ii_generation);
15922 +}
15923 +
15924 +/* tiny test for inode number */
15925 +/* tmpfs generation is too rough */
15926 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
15927 +{
15928 +       struct au_iinfo *iinfo;
15929 +
15930 +       iinfo = au_ii(inode);
15931 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
15932 +                && iinfo->ii_higen == h_inode->i_generation);
15933 +}
15934 +
15935 +static inline struct inode *au_igrab(struct inode *inode)
15936 +{
15937 +       if (inode) {
15938 +               AuDebugOn(!atomic_read(&inode->i_count));
15939 +               atomic_inc(&inode->i_count);
15940 +       }
15941 +       return inode;
15942 +}
15943 +
15944 +/* ---------------------------------------------------------------------- */
15945 +
15946 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
15947 +                                       aufs_bindex_t bindex)
15948 +{
15949 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
15950 +}
15951 +
15952 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
15953 +{
15954 +       return au_ii(inode)->ii_bstart;
15955 +}
15956 +
15957 +static inline aufs_bindex_t au_ibend(struct inode *inode)
15958 +{
15959 +       return au_ii(inode)->ii_bend;
15960 +}
15961 +
15962 +static inline struct au_vdir *au_ivdir(struct inode *inode)
15963 +{
15964 +       return au_ii(inode)->ii_vdir;
15965 +}
15966 +
15967 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
15968 +{
15969 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
15970 +}
15971 +
15972 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
15973 +{
15974 +       au_ii(inode)->ii_bend = bindex;
15975 +}
15976 +
15977 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
15978 +{
15979 +       au_ii(inode)->ii_vdir = vdir;
15980 +}
15981 +
15982 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
15983 +{
15984 +       return au_ii(inode)->ii_hinode + bindex;
15985 +}
15986 +
15987 +/* ---------------------------------------------------------------------- */
15988 +
15989 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
15990 +{
15991 +       if (pin)
15992 +               return pin->parent;
15993 +       return NULL;
15994 +}
15995 +
15996 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
15997 +{
15998 +       if (pin && pin->hdir)
15999 +               return pin->hdir->hi_inode;
16000 +       return NULL;
16001 +}
16002 +
16003 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
16004 +{
16005 +       if (pin)
16006 +               return pin->hdir;
16007 +       return NULL;
16008 +}
16009 +
16010 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
16011 +{
16012 +       if (pin)
16013 +               pin->dentry = dentry;
16014 +}
16015 +
16016 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
16017 +                                          unsigned char lflag)
16018 +{
16019 +       if (pin) {
16020 +               /* dirty macros require brackets */
16021 +               if (lflag) {
16022 +                       au_fset_pin(pin->flags, DI_LOCKED);
16023 +               } else {
16024 +                       au_fclr_pin(pin->flags, DI_LOCKED);
16025 +               }
16026 +       }
16027 +}
16028 +
16029 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
16030 +{
16031 +       if (pin) {
16032 +               dput(pin->parent);
16033 +               pin->parent = dget(parent);
16034 +       }
16035 +}
16036 +
16037 +/* ---------------------------------------------------------------------- */
16038 +
16039 +#ifdef CONFIG_AUFS_HINOTIFY
16040 +/* hinotify.c */
16041 +int au_hin_alloc(struct au_hinode *hinode, struct inode *inode,
16042 +                struct inode *h_inode);
16043 +void au_hin_free(struct au_hinode *hinode);
16044 +void au_hin_ctl(struct au_hinode *hinode, int do_set);
16045 +void au_reset_hinotify(struct inode *inode, unsigned int flags);
16046 +
16047 +int __init au_hinotify_init(void);
16048 +void au_hinotify_fin(void);
16049 +
16050 +static inline
16051 +void au_hin_init(struct au_hinode *hinode, struct au_hinotify *val)
16052 +{
16053 +       hinode->hi_notify = val;
16054 +}
16055 +
16056 +static inline void au_iigen_dec(struct inode *inode)
16057 +{
16058 +       atomic_dec(&au_ii(inode)->ii_generation);
16059 +}
16060 +
16061 +#else
16062 +static inline
16063 +int au_hin_alloc(struct au_hinode *hinode __maybe_unused,
16064 +                struct inode *inode __maybe_unused,
16065 +                struct inode *h_inode __maybe_unused)
16066 +{
16067 +       return -EOPNOTSUPP;
16068 +}
16069 +
16070 +static inline void au_hin_free(struct au_hinode *hinode __maybe_unused)
16071 +{
16072 +       /* nothing */
16073 +}
16074 +
16075 +static inline void au_hin_ctl(struct au_hinode *hinode __maybe_unused,
16076 +                             int do_set __maybe_unused)
16077 +{
16078 +       /* nothing */
16079 +}
16080 +
16081 +static inline void au_reset_hinotify(struct inode *inode __maybe_unused,
16082 +                                    unsigned int flags __maybe_unused)
16083 +{
16084 +       /* nothing */
16085 +}
16086 +
16087 +static inline int au_hinotify_init(void)
16088 +{
16089 +       return 0;
16090 +}
16091 +
16092 +#define au_hinotify_fin()      do {} while (0)
16093 +
16094 +static inline
16095 +void au_hin_init(struct au_hinode *hinode __maybe_unused,
16096 +                struct au_hinotify *val __maybe_unused)
16097 +{
16098 +       /* empty */
16099 +}
16100 +#endif /* CONFIG_AUFS_HINOTIFY */
16101 +
16102 +static inline void au_hin_suspend(struct au_hinode *hdir)
16103 +{
16104 +       au_hin_ctl(hdir, /*do_set*/0);
16105 +}
16106 +
16107 +static inline void au_hin_resume(struct au_hinode *hdir)
16108 +{
16109 +       au_hin_ctl(hdir, /*do_set*/1);
16110 +}
16111 +
16112 +static inline void au_hin_imtx_lock(struct au_hinode *hdir)
16113 +{
16114 +       mutex_lock(&hdir->hi_inode->i_mutex);
16115 +       au_hin_suspend(hdir);
16116 +}
16117 +
16118 +static inline void au_hin_imtx_lock_nested(struct au_hinode *hdir,
16119 +                                          unsigned int sc __maybe_unused)
16120 +{
16121 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
16122 +       au_hin_suspend(hdir);
16123 +}
16124 +
16125 +static inline void au_hin_imtx_unlock(struct au_hinode *hdir)
16126 +{
16127 +       au_hin_resume(hdir);
16128 +       mutex_unlock(&hdir->hi_inode->i_mutex);
16129 +}
16130 +
16131 +#endif /* __KERNEL__ */
16132 +#endif /* __AUFS_INODE_H__ */
16133 diff --git a/fs/aufs/ioctl.c b/fs/aufs/ioctl.c
16134 new file mode 100644
16135 index 0000000..ff17d77
16136 --- /dev/null
16137 +++ b/fs/aufs/ioctl.c
16138 @@ -0,0 +1,54 @@
16139 +/*
16140 + * Copyright (C) 2005-2009 Junjiro R. Okajima
16141 + *
16142 + * This program, aufs is free software; you can redistribute it and/or modify
16143 + * it under the terms of the GNU General Public License as published by
16144 + * the Free Software Foundation; either version 2 of the License, or
16145 + * (at your option) any later version.
16146 + */
16147 +
16148 +#include <linux/uaccess.h>
16149 +#include "aufs.h"
16150 +
16151 +long aufs_ioctl_dir(struct file *file, unsigned int cmd,
16152 +                   unsigned long arg __maybe_unused)
16153 +{
16154 +       long err;
16155 +       struct super_block *sb;
16156 +       struct au_sbinfo *sbinfo;
16157 +
16158 +       err = -EACCES;
16159 +       if (!capable(CAP_SYS_ADMIN))
16160 +               goto out;
16161 +
16162 +       err = 0;
16163 +       sb = file->f_dentry->d_sb;
16164 +       sbinfo = au_sbi(sb);
16165 +       switch (cmd) {
16166 +       case AUFS_CTL_PLINK_MAINT:
16167 +               /*
16168 +                * pseudo-link maintenance mode,
16169 +                * cleared by aufs_release_dir()
16170 +                */
16171 +               si_write_lock(sb);
16172 +               if (!au_ftest_si(sbinfo, MAINTAIN_PLINK)) {
16173 +                       au_fset_si(sbinfo, MAINTAIN_PLINK);
16174 +                       au_fi(file)->fi_maintain_plink = 1;
16175 +               } else
16176 +                       err = -EBUSY;
16177 +               si_write_unlock(sb);
16178 +               break;
16179 +       case AUFS_CTL_PLINK_CLEAN:
16180 +               if (au_opt_test(sbinfo->si_mntflags, PLINK)) {
16181 +                       aufs_write_lock(sb->s_root);
16182 +                       au_plink_put(sb);
16183 +                       aufs_write_unlock(sb->s_root);
16184 +               }
16185 +               break;
16186 +       default:
16187 +               err = -EINVAL;
16188 +       }
16189 +
16190 + out:
16191 +       return err;
16192 +}
16193 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
16194 new file mode 100644
16195 index 0000000..84a940f
16196 --- /dev/null
16197 +++ b/fs/aufs/loop.c
16198 @@ -0,0 +1,46 @@
16199 +/*
16200 + * Copyright (C) 2005-2009 Junjiro R. Okajima
16201 + *
16202 + * This program, aufs is free software; you can redistribute it and/or modify
16203 + * it under the terms of the GNU General Public License as published by
16204 + * the Free Software Foundation; either version 2 of the License, or
16205 + * (at your option) any later version.
16206 + */
16207 +
16208 +/*
16209 + * support for loopback block device as a branch
16210 + */
16211 +
16212 +#include <linux/loop.h>
16213 +#include "aufs.h"
16214 +
16215 +/*
16216 + * test if two lower dentries have overlapping branches.
16217 + */
16218 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_d1,
16219 +                            struct dentry *h_d2)
16220 +{
16221 +       struct inode *h_inode;
16222 +       struct loop_device *l;
16223 +
16224 +       h_inode = h_d1->d_inode;
16225 +       if (MAJOR(h_inode->i_sb->s_dev) != LOOP_MAJOR)
16226 +               return 0;
16227 +
16228 +       l = h_inode->i_sb->s_bdev->bd_disk->private_data;
16229 +       h_d1 = l->lo_backing_file->f_dentry;
16230 +       /* h_d1 can be local NFS. in this case aufs cannot detect the loop */
16231 +       if (unlikely(h_d1->d_sb == sb))
16232 +               return 1;
16233 +       return !!au_test_subdir(h_d1, h_d2);
16234 +}
16235 +
16236 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
16237 +int au_test_loopback_kthread(void)
16238 +{
16239 +       const char c = current->comm[4];
16240 +
16241 +       return current->mm == NULL
16242 +              && '0' <= c && c <= '9'
16243 +              && strncmp(current->comm, "loop", 4) == 0;
16244 +}
16245 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
16246 new file mode 100644
16247 index 0000000..1c0ee40
16248 --- /dev/null
16249 +++ b/fs/aufs/loop.h
16250 @@ -0,0 +1,41 @@
16251 +/*
16252 + * Copyright (C) 2005-2009 Junjiro R. Okajima
16253 + *
16254 + * This program, aufs is free software; you can redistribute it and/or modify
16255 + * it under the terms of the GNU General Public License as published by
16256 + * the Free Software Foundation; either version 2 of the License, or
16257 + * (at your option) any later version.
16258 + */
16259 +
16260 +/*
16261 + * support for loopback mount as a branch
16262 + */
16263 +
16264 +#ifndef __AUFS_LOOP_H__
16265 +#define __AUFS_LOOP_H__
16266 +
16267 +#ifdef __KERNEL__
16268 +
16269 +#include <linux/fs.h>
16270 +
16271 +#ifdef CONFIG_AUFS_BDEV_LOOP
16272 +/* loop.c */
16273 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_d1,
16274 +                            struct dentry *h_d2);
16275 +int au_test_loopback_kthread(void);
16276 +#else
16277 +static inline
16278 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_d1,
16279 +                            struct dentry *h_d2)
16280 +{
16281 +       return 0;
16282 +}
16283 +
16284 +static inline int au_test_loopback_kthread(void)
16285 +{
16286 +       return 0;
16287 +}
16288 +#endif /* BLK_DEV_LOOP */
16289 +
16290 +#endif /* __KERNEL__ */
16291 +#endif /* __AUFS_LOOP_H__ */
16292 diff --git a/fs/aufs/magic.mk b/fs/aufs/magic.mk
16293 new file mode 100644
16294 index 0000000..6258f57
16295 --- /dev/null
16296 +++ b/fs/aufs/magic.mk
16297 @@ -0,0 +1,58 @@
16298 +
16299 +# defined in ${srctree}/fs/fuse/inode.c
16300 +# tristate
16301 +ifdef CONFIG_FUSE_FS
16302 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
16303 +endif
16304 +
16305 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
16306 +# tristate
16307 +ifdef CONFIG_OCFS2_FS
16308 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
16309 +endif
16310 +
16311 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
16312 +# tristate
16313 +ifdef CONFIG_OCFS2_FS_O2CB
16314 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
16315 +endif
16316 +
16317 +# defined in ${srctree}/fs/ramfs/inode.c
16318 +# always true
16319 +ccflags-y += -DRAMFS_MAGIC=0x858458f6
16320 +
16321 +# defined in ${srctree}/fs/cifs/cifsfs.c
16322 +# tristate
16323 +ifdef CONFIG_CIFS_FS
16324 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
16325 +endif
16326 +
16327 +# defined in ${srctree}/fs/xfs/xfs_sb.h
16328 +# tristate
16329 +ifdef CONFIG_XFS_FS
16330 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
16331 +endif
16332 +
16333 +# defined in ${srctree}/fs/configfs/mount.c
16334 +# tristate
16335 +ifdef CONFIG_CONFIGFS_FS
16336 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
16337 +endif
16338 +
16339 +# defined in ${srctree}/fs/9p/v9fs.h
16340 +# tristate
16341 +ifdef CONFIG_9P_FS
16342 +ccflags-y += -DV9FS_MAGIC=0x01021997
16343 +endif
16344 +
16345 +# defined in ${srctree}/fs/ubifs/ubifs.h
16346 +# tristate
16347 +ifdef CONFIG_UBIFS_FS
16348 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
16349 +endif
16350 +
16351 +# defined in ${srctree}/fs/debugfs/inode.c
16352 +# boolean
16353 +ifdef CONFIG_DEBUG_FS
16354 +ccflags-y += -DDEBUGFS_MAGIC=0x64626720
16355 +endif
16356 diff --git a/fs/aufs/module.c b/fs/aufs/module.c
16357 new file mode 100644
16358 index 0000000..c2219d3
16359 --- /dev/null
16360 +++ b/fs/aufs/module.c
16361 @@ -0,0 +1,164 @@
16362 +/*
16363 + * Copyright (C) 2005-2009 Junjiro R. Okajima
16364 + *
16365 + * This program, aufs is free software; you can redistribute it and/or modify
16366 + * it under the terms of the GNU General Public License as published by
16367 + * the Free Software Foundation; either version 2 of the License, or
16368 + * (at your option) any later version.
16369 + */
16370 +
16371 +/*
16372 + * module global variables and operations
16373 + */
16374 +
16375 +#include <linux/module.h>
16376 +#include <linux/seq_file.h>
16377 +#include "aufs.h"
16378 +
16379 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
16380 +{
16381 +       if (new_sz <= nused)
16382 +               return p;
16383 +
16384 +       p = krealloc(p, new_sz, gfp);
16385 +       if (p)
16386 +               memset(p + nused, 0, new_sz - nused);
16387 +       return p;
16388 +}
16389 +
16390 +/* ---------------------------------------------------------------------- */
16391 +
16392 +/*
16393 + * aufs caches
16394 + */
16395 +struct kmem_cache *au_cachep[AuCache_Last];
16396 +static int __init au_cache_init(void)
16397 +{
16398 +       au_cachep[AuCache_DINFO] = AuCache(au_dinfo);
16399 +       if (au_cachep[AuCache_DINFO])
16400 +               au_cachep[AuCache_ICNTNR] = AuCache(au_icntnr);
16401 +       if (au_cachep[AuCache_ICNTNR])
16402 +               au_cachep[AuCache_FINFO] = AuCache(au_finfo);
16403 +       if (au_cachep[AuCache_FINFO])
16404 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
16405 +       if (au_cachep[AuCache_VDIR])
16406 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
16407 +       if (au_cachep[AuCache_DEHSTR])
16408 +               return 0;
16409 +
16410 +       return -ENOMEM;
16411 +}
16412 +
16413 +static void au_cache_fin(void)
16414 +{
16415 +       int i;
16416 +       for (i = 0; i < AuCache_Last; i++)
16417 +               if (au_cachep[i]) {
16418 +                       kmem_cache_destroy(au_cachep[i]);
16419 +                       au_cachep[i] = NULL;
16420 +               }
16421 +}
16422 +
16423 +/* ---------------------------------------------------------------------- */
16424 +
16425 +int au_dir_roflags;
16426 +
16427 +/*
16428 + * functions for module interface.
16429 + */
16430 +MODULE_LICENSE("GPL");
16431 +/* MODULE_LICENSE("GPL v2"); */
16432 +MODULE_AUTHOR("Junjiro R. Okajima");
16433 +MODULE_DESCRIPTION(AUFS_NAME
16434 +       " -- Advanced multi layered unification filesystem");
16435 +MODULE_VERSION(AUFS_VERSION);
16436 +
16437 +/* it should be 'byte', but param_set_byte() prints it by "%c" */
16438 +short aufs_nwkq = AUFS_NWKQ_DEF;
16439 +MODULE_PARM_DESC(nwkq, "the number of workqueue thread, " AUFS_WKQ_NAME);
16440 +module_param_named(nwkq, aufs_nwkq, short, S_IRUGO);
16441 +
16442 +/* this module parameter has no meaning when SYSFS is disabled */
16443 +int sysaufs_brs = 1;
16444 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
16445 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
16446 +
16447 +/* ---------------------------------------------------------------------- */
16448 +
16449 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
16450 +
16451 +int au_seq_path(struct seq_file *seq, struct path *path)
16452 +{
16453 +       return seq_path(seq, path, au_esc_chars);
16454 +}
16455 +
16456 +/* ---------------------------------------------------------------------- */
16457 +
16458 +static int __init aufs_init(void)
16459 +{
16460 +       int err, i;
16461 +       char *p;
16462 +
16463 +       p = au_esc_chars;
16464 +       for (i = 1; i <= ' '; i++)
16465 +               *p++ = i;
16466 +       *p++ = '\\';
16467 +       *p++ = '\x7f';
16468 +       *p = 0;
16469 +
16470 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
16471 +
16472 +       sysaufs_brs_init();
16473 +       au_debug_init();
16474 +
16475 +       err = -EINVAL;
16476 +       if (unlikely(aufs_nwkq <= 0))
16477 +               goto out;
16478 +
16479 +       err = sysaufs_init();
16480 +       if (unlikely(err))
16481 +               goto out;
16482 +       err = au_wkq_init();
16483 +       if (unlikely(err))
16484 +               goto out_sysaufs;
16485 +       err = au_hinotify_init();
16486 +       if (unlikely(err))
16487 +               goto out_wkq;
16488 +       err = au_sysrq_init();
16489 +       if (unlikely(err))
16490 +               goto out_hin;
16491 +       err = au_cache_init();
16492 +       if (unlikely(err))
16493 +               goto out_sysrq;
16494 +       err = register_filesystem(&aufs_fs_type);
16495 +       if (unlikely(err))
16496 +               goto out_cache;
16497 +       pr_info(AUFS_NAME " " AUFS_VERSION "\n");
16498 +       goto out; /* success */
16499 +
16500 + out_cache:
16501 +       au_cache_fin();
16502 + out_sysrq:
16503 +       au_sysrq_fin();
16504 + out_hin:
16505 +       au_hinotify_fin();
16506 + out_wkq:
16507 +       au_wkq_fin();
16508 + out_sysaufs:
16509 +       sysaufs_fin();
16510 + out:
16511 +       return err;
16512 +}
16513 +
16514 +static void __exit aufs_exit(void)
16515 +{
16516 +       unregister_filesystem(&aufs_fs_type);
16517 +       au_cache_fin();
16518 +       au_sysrq_fin();
16519 +       au_hinotify_fin();
16520 +       au_wkq_fin();
16521 +       sysaufs_fin();
16522 +}
16523 +
16524 +module_init(aufs_init);
16525 +module_exit(aufs_exit);
16526 diff --git a/fs/aufs/module.h b/fs/aufs/module.h
16527 new file mode 100644
16528 index 0000000..85e397a
16529 --- /dev/null
16530 +++ b/fs/aufs/module.h
16531 @@ -0,0 +1,66 @@
16532 +/*
16533 + * Copyright (C) 2005-2009 Junjiro R. Okajima
16534 + *
16535 + * This program, aufs is free software; you can redistribute it and/or modify
16536 + * it under the terms of the GNU General Public License as published by
16537 + * the Free Software Foundation; either version 2 of the License, or
16538 + * (at your option) any later version.
16539 + */
16540 +
16541 +/*
16542 + * module initialization and module-global
16543 + */
16544 +
16545 +#ifndef __AUFS_MODULE_H__
16546 +#define __AUFS_MODULE_H__
16547 +
16548 +#ifdef __KERNEL__
16549 +
16550 +#include <linux/slab.h>
16551 +
16552 +/* module parameters */
16553 +extern short aufs_nwkq;
16554 +extern int sysaufs_brs;
16555 +
16556 +/* ---------------------------------------------------------------------- */
16557 +
16558 +extern int au_dir_roflags;
16559 +
16560 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
16561 +int au_seq_path(struct seq_file *seq, struct path *path);
16562 +
16563 +/* ---------------------------------------------------------------------- */
16564 +
16565 +/* kmem cache */
16566 +enum {
16567 +       AuCache_DINFO,
16568 +       AuCache_ICNTNR,
16569 +       AuCache_FINFO,
16570 +       AuCache_VDIR,
16571 +       AuCache_DEHSTR,
16572 +#ifdef CONFIG_AUFS_HINOTIFY
16573 +       AuCache_HINOTIFY,
16574 +#endif
16575 +       AuCache_Last
16576 +};
16577 +
16578 +#define AuCache(type)  KMEM_CACHE(type, SLAB_RECLAIM_ACCOUNT)
16579 +
16580 +extern struct kmem_cache *au_cachep[];
16581 +
16582 +#define AuCacheFuncs(name, index) \
16583 +static inline void *au_cache_alloc_##name(void) \
16584 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
16585 +static inline void au_cache_free_##name(void *p) \
16586 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
16587 +
16588 +AuCacheFuncs(dinfo, DINFO);
16589 +AuCacheFuncs(icntnr, ICNTNR);
16590 +AuCacheFuncs(finfo, FINFO);
16591 +AuCacheFuncs(vdir, VDIR);
16592 +AuCacheFuncs(dehstr, DEHSTR);
16593 +
16594 +/*  ---------------------------------------------------------------------- */
16595 +
16596 +#endif /* __KERNEL__ */
16597 +#endif /* __AUFS_MODULE_H__ */
16598 diff --git a/fs/aufs/opts.c b/fs/aufs/opts.c
16599 new file mode 100644
16600 index 0000000..630db0e
16601 --- /dev/null
16602 +++ b/fs/aufs/opts.c
16603 @@ -0,0 +1,1478 @@
16604 +/*
16605 + * Copyright (C) 2005-2009 Junjiro R. Okajima
16606 + *
16607 + * This program, aufs is free software; you can redistribute it and/or modify
16608 + * it under the terms of the GNU General Public License as published by
16609 + * the Free Software Foundation; either version 2 of the License, or
16610 + * (at your option) any later version.
16611 + */
16612 +
16613 +/*
16614 + * mount options/flags
16615 + */
16616 +
16617 +#include <linux/types.h> /* a distribution requires */
16618 +#include <linux/parser.h>
16619 +#include "aufs.h"
16620 +
16621 +/* ---------------------------------------------------------------------- */
16622 +
16623 +enum {
16624 +       Opt_br,
16625 +       Opt_add, Opt_del, Opt_mod, Opt_reorder, Opt_append, Opt_prepend,
16626 +       Opt_idel, Opt_imod, Opt_ireorder,
16627 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash, Opt_rendir,
16628 +       Opt_xino, Opt_zxino, Opt_noxino,
16629 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
16630 +       Opt_trunc_xino_path, Opt_itrunc_xino,
16631 +       Opt_trunc_xib, Opt_notrunc_xib,
16632 +       Opt_plink, Opt_noplink, Opt_list_plink,
16633 +       Opt_udba,
16634 +       /* Opt_lock, Opt_unlock, */
16635 +       Opt_cmd, Opt_cmd_args,
16636 +       Opt_diropq_a, Opt_diropq_w,
16637 +       Opt_warn_perm, Opt_nowarn_perm,
16638 +       Opt_wbr_copyup, Opt_wbr_create,
16639 +       Opt_refrof, Opt_norefrof,
16640 +       Opt_verbose, Opt_noverbose,
16641 +       Opt_sum, Opt_nosum, Opt_wsum,
16642 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
16643 +};
16644 +
16645 +static match_table_t options = {
16646 +       {Opt_br, "br=%s"},
16647 +       {Opt_br, "br:%s"},
16648 +
16649 +       {Opt_add, "add=%d:%s"},
16650 +       {Opt_add, "add:%d:%s"},
16651 +       {Opt_add, "ins=%d:%s"},
16652 +       {Opt_add, "ins:%d:%s"},
16653 +       {Opt_append, "append=%s"},
16654 +       {Opt_append, "append:%s"},
16655 +       {Opt_prepend, "prepend=%s"},
16656 +       {Opt_prepend, "prepend:%s"},
16657 +
16658 +       {Opt_del, "del=%s"},
16659 +       {Opt_del, "del:%s"},
16660 +       /* {Opt_idel, "idel:%d"}, */
16661 +       {Opt_mod, "mod=%s"},
16662 +       {Opt_mod, "mod:%s"},
16663 +       /* {Opt_imod, "imod:%d:%s"}, */
16664 +
16665 +       {Opt_dirwh, "dirwh=%d"},
16666 +
16667 +       {Opt_xino, "xino=%s"},
16668 +       {Opt_noxino, "noxino"},
16669 +       {Opt_trunc_xino, "trunc_xino"},
16670 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
16671 +       {Opt_notrunc_xino, "notrunc_xino"},
16672 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
16673 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
16674 +       /* {Opt_zxino, "zxino=%s"}, */
16675 +       {Opt_trunc_xib, "trunc_xib"},
16676 +       {Opt_notrunc_xib, "notrunc_xib"},
16677 +
16678 +       {Opt_plink, "plink"},
16679 +       {Opt_noplink, "noplink"},
16680 +#ifdef CONFIG_AUFS_DEBUG
16681 +       {Opt_list_plink, "list_plink"},
16682 +#endif
16683 +
16684 +       {Opt_udba, "udba=%s"},
16685 +
16686 +       {Opt_diropq_a, "diropq=always"},
16687 +       {Opt_diropq_a, "diropq=a"},
16688 +       {Opt_diropq_w, "diropq=whiteouted"},
16689 +       {Opt_diropq_w, "diropq=w"},
16690 +
16691 +       {Opt_warn_perm, "warn_perm"},
16692 +       {Opt_nowarn_perm, "nowarn_perm"},
16693 +
16694 +       /* keep them temporary */
16695 +       {Opt_ignore_silent, "coo=%s"},
16696 +       {Opt_ignore_silent, "nodlgt"},
16697 +       {Opt_ignore_silent, "nodirperm1"},
16698 +       {Opt_ignore_silent, "noshwh"},
16699 +       {Opt_ignore_silent, "clean_plink"},
16700 +
16701 +       {Opt_rendir, "rendir=%d"},
16702 +
16703 +       {Opt_refrof, "refrof"},
16704 +       {Opt_norefrof, "norefrof"},
16705 +
16706 +       {Opt_verbose, "verbose"},
16707 +       {Opt_verbose, "v"},
16708 +       {Opt_noverbose, "noverbose"},
16709 +       {Opt_noverbose, "quiet"},
16710 +       {Opt_noverbose, "q"},
16711 +       {Opt_noverbose, "silent"},
16712 +
16713 +       {Opt_sum, "sum"},
16714 +       {Opt_nosum, "nosum"},
16715 +       {Opt_wsum, "wsum"},
16716 +
16717 +       {Opt_rdcache, "rdcache=%d"},
16718 +       {Opt_rdblk, "rdblk=%d"},
16719 +       {Opt_rdhash, "rdhash=%d"},
16720 +
16721 +       {Opt_wbr_create, "create=%s"},
16722 +       {Opt_wbr_create, "create_policy=%s"},
16723 +       {Opt_wbr_copyup, "cpup=%s"},
16724 +       {Opt_wbr_copyup, "copyup=%s"},
16725 +       {Opt_wbr_copyup, "copyup_policy=%s"},
16726 +
16727 +       /* internal use for the scripts */
16728 +       {Opt_ignore_silent, "si=%s"},
16729 +
16730 +       {Opt_br, "dirs=%s"},
16731 +       {Opt_ignore, "debug=%d"},
16732 +       {Opt_ignore, "delete=whiteout"},
16733 +       {Opt_ignore, "delete=all"},
16734 +       {Opt_ignore, "imap=%s"},
16735 +
16736 +       {Opt_err, NULL}
16737 +};
16738 +
16739 +/* ---------------------------------------------------------------------- */
16740 +
16741 +static const char *au_parser_pattern(int val, struct match_token *token)
16742 +{
16743 +       while (token->pattern) {
16744 +               if (token->token == val)
16745 +                       return token->pattern;
16746 +               token++;
16747 +       }
16748 +       BUG();
16749 +       return "??";
16750 +}
16751 +
16752 +/* ---------------------------------------------------------------------- */
16753 +
16754 +static match_table_t brperms = {
16755 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
16756 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
16757 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
16758 +
16759 +       {AuBrPerm_ROWH, AUFS_BRPERM_ROWH},
16760 +       {AuBrPerm_RRWH, AUFS_BRPERM_RRWH},
16761 +       {AuBrPerm_RWNoLinkWH, AUFS_BRPERM_RWNLWH},
16762 +
16763 +       {AuBrPerm_ROWH, "nfsro"},
16764 +       {AuBrPerm_RO, NULL}
16765 +};
16766 +
16767 +static int br_perm_val(char *perm)
16768 +{
16769 +       int val;
16770 +       substring_t args[MAX_OPT_ARGS];
16771 +
16772 +       val = match_token(perm, brperms, args);
16773 +       return val;
16774 +}
16775 +
16776 +const char *au_optstr_br_perm(int brperm)
16777 +{
16778 +       return au_parser_pattern(brperm, (void *)brperms);
16779 +}
16780 +
16781 +/* ---------------------------------------------------------------------- */
16782 +
16783 +static match_table_t udbalevel = {
16784 +       {AuOpt_UDBA_REVAL, "reval"},
16785 +       {AuOpt_UDBA_NONE, "none"},
16786 +#ifdef CONFIG_AUFS_HINOTIFY
16787 +       {AuOpt_UDBA_HINOTIFY, "inotify"},
16788 +#endif
16789 +       {-1, NULL}
16790 +};
16791 +
16792 +static int udba_val(char *str)
16793 +{
16794 +       substring_t args[MAX_OPT_ARGS];
16795 +
16796 +       return match_token(str, udbalevel, args);
16797 +}
16798 +
16799 +const char *au_optstr_udba(int udba)
16800 +{
16801 +       return au_parser_pattern(udba, (void *)udbalevel);
16802 +}
16803 +
16804 +/* ---------------------------------------------------------------------- */
16805 +
16806 +static match_table_t au_wbr_create_policy = {
16807 +       {AuWbrCreate_TDP, "tdp"},
16808 +       {AuWbrCreate_TDP, "top-down-parent"},
16809 +       {AuWbrCreate_RR, "rr"},
16810 +       {AuWbrCreate_RR, "round-robin"},
16811 +       {AuWbrCreate_MFS, "mfs"},
16812 +       {AuWbrCreate_MFS, "most-free-space"},
16813 +       {AuWbrCreate_MFSV, "mfs:%d"},
16814 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
16815 +
16816 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
16817 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
16818 +       {AuWbrCreate_PMFS, "pmfs"},
16819 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
16820 +
16821 +       {-1, NULL}
16822 +};
16823 +
16824 +/* cf. linux/lib/parser.c */
16825 +static int au_match_ull(substring_t *s, unsigned long long *result)
16826 +{
16827 +       int err;
16828 +       unsigned int len;
16829 +       char a[32];
16830 +
16831 +       err = -ERANGE;
16832 +       len = s->to - s->from;
16833 +       if (len + 1 <= sizeof(a)) {
16834 +               memcpy(a, s->from, len);
16835 +               a[len] = '\0';
16836 +               err = strict_strtoull(a, 0, result);
16837 +       }
16838 +       return err;
16839 +}
16840 +
16841 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
16842 +                           struct au_opt_wbr_create *create)
16843 +{
16844 +       int err;
16845 +       unsigned long long ull;
16846 +
16847 +       err = 0;
16848 +       if (!au_match_ull(arg, &ull))
16849 +               create->mfsrr_watermark = ull;
16850 +       else {
16851 +               AuErr("bad integer in %s\n", str);
16852 +               err = -EINVAL;
16853 +       }
16854 +
16855 +       return err;
16856 +}
16857 +
16858 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
16859 +                         struct au_opt_wbr_create *create)
16860 +{
16861 +       int n, err;
16862 +
16863 +       err = 0;
16864 +       if (!match_int(arg, &n) && 0 <= n)
16865 +               create->mfs_second = n;
16866 +       else {
16867 +               AuErr("bad integer in %s\n", str);
16868 +               err = -EINVAL;
16869 +       }
16870 +
16871 +       return err;
16872 +}
16873 +
16874 +static int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
16875 +{
16876 +       int err, e;
16877 +       substring_t args[MAX_OPT_ARGS];
16878 +
16879 +       err = match_token(str, au_wbr_create_policy, args);
16880 +       create->wbr_create = err;
16881 +       switch (err) {
16882 +       case AuWbrCreate_MFSRRV:
16883 +               e = au_wbr_mfs_wmark(&args[0], str, create);
16884 +               if (!e)
16885 +                       e = au_wbr_mfs_sec(&args[1], str, create);
16886 +               if (unlikely(e))
16887 +                       err = e;
16888 +               break;
16889 +       case AuWbrCreate_MFSRR:
16890 +               e = au_wbr_mfs_wmark(&args[0], str, create);
16891 +               if (unlikely(e)) {
16892 +                       err = e;
16893 +                       break;
16894 +               }
16895 +               /*FALLTHROUGH*/
16896 +       case AuWbrCreate_MFS:
16897 +       case AuWbrCreate_PMFS:
16898 +               create->mfs_second = AUFS_MFS_SECOND_DEF;
16899 +               break;
16900 +       case AuWbrCreate_MFSV:
16901 +       case AuWbrCreate_PMFSV:
16902 +               e = au_wbr_mfs_sec(&args[0], str, create);
16903 +               if (unlikely(e))
16904 +                       err = e;
16905 +               break;
16906 +       }
16907 +
16908 +       return err;
16909 +}
16910 +
16911 +const char *au_optstr_wbr_create(int wbr_create)
16912 +{
16913 +       return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
16914 +}
16915 +
16916 +static match_table_t au_wbr_copyup_policy = {
16917 +       {AuWbrCopyup_TDP, "tdp"},
16918 +       {AuWbrCopyup_TDP, "top-down-parent"},
16919 +       {AuWbrCopyup_BUP, "bup"},
16920 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
16921 +       {AuWbrCopyup_BU, "bu"},
16922 +       {AuWbrCopyup_BU, "bottom-up"},
16923 +       {-1, NULL}
16924 +};
16925 +
16926 +static int au_wbr_copyup_val(char *str)
16927 +{
16928 +       substring_t args[MAX_OPT_ARGS];
16929 +
16930 +       return match_token(str, au_wbr_copyup_policy, args);
16931 +}
16932 +
16933 +const char *au_optstr_wbr_copyup(int wbr_copyup)
16934 +{
16935 +       return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
16936 +}
16937 +
16938 +/* ---------------------------------------------------------------------- */
16939 +
16940 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
16941 +
16942 +static void dump_opts(struct au_opts *opts)
16943 +{
16944 +#ifdef CONFIG_AUFS_DEBUG
16945 +       /* reduce stack space */
16946 +       union {
16947 +               struct au_opt_add *add;
16948 +               struct au_opt_del *del;
16949 +               struct au_opt_mod *mod;
16950 +               struct au_opt_xino *xino;
16951 +               struct au_opt_xino_itrunc *xino_itrunc;
16952 +               struct au_opt_wbr_create *create;
16953 +       } u;
16954 +       struct au_opt *opt;
16955 +
16956 +       opt = opts->opt;
16957 +       while (opt->type != Opt_tail) {
16958 +               switch (opt->type) {
16959 +               case Opt_add:
16960 +                       u.add = &opt->add;
16961 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16962 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16963 +                                 u.add->path.dentry);
16964 +                       break;
16965 +               case Opt_del:
16966 +               case Opt_idel:
16967 +                       u.del = &opt->del;
16968 +                       AuDbg("del {%s, %p}\n",
16969 +                             u.del->pathname, u.del->h_path.dentry);
16970 +                       break;
16971 +               case Opt_mod:
16972 +               case Opt_imod:
16973 +                       u.mod = &opt->mod;
16974 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16975 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16976 +                       break;
16977 +               case Opt_append:
16978 +                       u.add = &opt->add;
16979 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16980 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16981 +                                 u.add->path.dentry);
16982 +                       break;
16983 +               case Opt_prepend:
16984 +                       u.add = &opt->add;
16985 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16986 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16987 +                                 u.add->path.dentry);
16988 +                       break;
16989 +               case Opt_dirwh:
16990 +                       AuDbg("dirwh %d\n", opt->dirwh);
16991 +                       break;
16992 +               case Opt_rdcache:
16993 +                       AuDbg("rdcache %d\n", opt->rdcache);
16994 +                       break;
16995 +               case Opt_rdblk:
16996 +                       AuDbg("rdblk %u\n", opt->rdblk);
16997 +                       break;
16998 +               case Opt_rdhash:
16999 +                       AuDbg("rdhash %u\n", opt->rdhash);
17000 +                       break;
17001 +               case Opt_xino:
17002 +                       u.xino = &opt->xino;
17003 +                       AuDbg("xino {%s %.*s}\n",
17004 +                                 u.xino->path,
17005 +                                 AuDLNPair(u.xino->file->f_dentry));
17006 +                       break;
17007 +               case Opt_trunc_xino:
17008 +                       AuLabel(trunc_xino);
17009 +                       break;
17010 +               case Opt_notrunc_xino:
17011 +                       AuLabel(notrunc_xino);
17012 +                       break;
17013 +               case Opt_trunc_xino_path:
17014 +               case Opt_itrunc_xino:
17015 +                       u.xino_itrunc = &opt->xino_itrunc;
17016 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
17017 +                       break;
17018 +
17019 +               case Opt_noxino:
17020 +                       AuLabel(noxino);
17021 +                       break;
17022 +               case Opt_trunc_xib:
17023 +                       AuLabel(trunc_xib);
17024 +                       break;
17025 +               case Opt_notrunc_xib:
17026 +                       AuLabel(notrunc_xib);
17027 +                       break;
17028 +               case Opt_plink:
17029 +                       AuLabel(plink);
17030 +                       break;
17031 +               case Opt_noplink:
17032 +                       AuLabel(noplink);
17033 +                       break;
17034 +               case Opt_list_plink:
17035 +                       AuLabel(list_plink);
17036 +                       break;
17037 +               case Opt_udba:
17038 +                       AuDbg("udba %d, %s\n",
17039 +                                 opt->udba, au_optstr_udba(opt->udba));
17040 +                       break;
17041 +               case Opt_diropq_a:
17042 +                       AuLabel(diropq_a);
17043 +                       break;
17044 +               case Opt_diropq_w:
17045 +                       AuLabel(diropq_w);
17046 +                       break;
17047 +               case Opt_warn_perm:
17048 +                       AuLabel(warn_perm);
17049 +                       break;
17050 +               case Opt_nowarn_perm:
17051 +                       AuLabel(nowarn_perm);
17052 +                       break;
17053 +               case Opt_refrof:
17054 +                       AuLabel(refrof);
17055 +                       break;
17056 +               case Opt_norefrof:
17057 +                       AuLabel(norefrof);
17058 +                       break;
17059 +               case Opt_verbose:
17060 +                       AuLabel(verbose);
17061 +                       break;
17062 +               case Opt_noverbose:
17063 +                       AuLabel(noverbose);
17064 +                       break;
17065 +               case Opt_sum:
17066 +                       AuLabel(sum);
17067 +                       break;
17068 +               case Opt_nosum:
17069 +                       AuLabel(nosum);
17070 +                       break;
17071 +               case Opt_wsum:
17072 +                       AuLabel(wsum);
17073 +                       break;
17074 +               case Opt_wbr_create:
17075 +                       u.create = &opt->wbr_create;
17076 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
17077 +                                 au_optstr_wbr_create(u.create->wbr_create));
17078 +                       switch (u.create->wbr_create) {
17079 +                       case AuWbrCreate_MFSV:
17080 +                       case AuWbrCreate_PMFSV:
17081 +                               AuDbg("%d sec\n", u.create->mfs_second);
17082 +                               break;
17083 +                       case AuWbrCreate_MFSRR:
17084 +                               AuDbg("%llu watermark\n",
17085 +                                         u.create->mfsrr_watermark);
17086 +                               break;
17087 +                       case AuWbrCreate_MFSRRV:
17088 +                               AuDbg("%llu watermark, %d sec\n",
17089 +                                         u.create->mfsrr_watermark,
17090 +                                         u.create->mfs_second);
17091 +                               break;
17092 +                       }
17093 +                       break;
17094 +               case Opt_wbr_copyup:
17095 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
17096 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
17097 +                       break;
17098 +               default:
17099 +                       BUG();
17100 +               }
17101 +               opt++;
17102 +       }
17103 +#endif
17104 +}
17105 +
17106 +void au_opts_free(struct au_opts *opts)
17107 +{
17108 +       struct au_opt *opt;
17109 +
17110 +       opt = opts->opt;
17111 +       while (opt->type != Opt_tail) {
17112 +               switch (opt->type) {
17113 +               case Opt_add:
17114 +               case Opt_append:
17115 +               case Opt_prepend:
17116 +                       path_put(&opt->add.path);
17117 +                       break;
17118 +               case Opt_del:
17119 +               case Opt_idel:
17120 +                       path_put(&opt->del.h_path);
17121 +                       break;
17122 +               case Opt_mod:
17123 +               case Opt_imod:
17124 +                       dput(opt->mod.h_root);
17125 +                       break;
17126 +               case Opt_xino:
17127 +                       fput(opt->xino.file);
17128 +                       break;
17129 +               }
17130 +               opt++;
17131 +       }
17132 +}
17133 +
17134 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
17135 +                  aufs_bindex_t bindex)
17136 +{
17137 +       int err;
17138 +       struct au_opt_add *add = &opt->add;
17139 +       char *p;
17140 +
17141 +       add->bindex = bindex;
17142 +       add->perm = AuBrPerm_Last;
17143 +       add->pathname = opt_str;
17144 +       p = strchr(opt_str, '=');
17145 +       if (p) {
17146 +               *p++ = 0;
17147 +               if (*p)
17148 +                       add->perm = br_perm_val(p);
17149 +       }
17150 +
17151 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
17152 +       if (!err) {
17153 +               if (!p) {
17154 +                       add->perm = AuBrPerm_RO;
17155 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
17156 +                               add->perm = AuBrPerm_RR;
17157 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
17158 +                               add->perm = AuBrPerm_RW;
17159 +               }
17160 +               opt->type = Opt_add;
17161 +               goto out;
17162 +       }
17163 +       AuErr("lookup failed %s (%d)\n", add->pathname, err);
17164 +       err = -EINVAL;
17165 +
17166 + out:
17167 +       return err;
17168 +}
17169 +
17170 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
17171 +{
17172 +       int err;
17173 +
17174 +       del->pathname = args[0].from;
17175 +       AuDbg("del path %s\n", del->pathname);
17176 +
17177 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
17178 +       if (unlikely(err))
17179 +               AuErr("lookup failed %s (%d)\n", del->pathname, err);
17180 +
17181 +       return err;
17182 +}
17183 +
17184 +#if 0 /* reserved for future use */
17185 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
17186 +                             struct au_opt_del *del, substring_t args[])
17187 +{
17188 +       int err;
17189 +       struct dentry *root;
17190 +
17191 +       err = -EINVAL;
17192 +       root = sb->s_root;
17193 +       aufs_read_lock(root, AuLock_FLUSH);
17194 +       if (bindex < 0 || au_sbend(sb) < bindex) {
17195 +               AuErr("out of bounds, %d\n", bindex);
17196 +               goto out;
17197 +       }
17198 +
17199 +       err = 0;
17200 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
17201 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
17202 +
17203 + out:
17204 +       aufs_read_unlock(root, !AuLock_IR);
17205 +       return err;
17206 +}
17207 +#endif
17208 +
17209 +static int au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
17210 +{
17211 +       int err;
17212 +       struct path path;
17213 +       char *p;
17214 +
17215 +       err = -EINVAL;
17216 +       mod->path = args[0].from;
17217 +       p = strchr(mod->path, '=');
17218 +       if (unlikely(!p)) {
17219 +               AuErr("no permssion %s\n", args[0].from);
17220 +               goto out;
17221 +       }
17222 +
17223 +       *p++ = 0;
17224 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
17225 +       if (unlikely(err)) {
17226 +               AuErr("lookup failed %s (%d)\n", mod->path, err);
17227 +               goto out;
17228 +       }
17229 +
17230 +       mod->perm = br_perm_val(p);
17231 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
17232 +       mod->h_root = dget(path.dentry);
17233 +       path_put(&path);
17234 +
17235 + out:
17236 +       return err;
17237 +}
17238 +
17239 +#if 0 /* reserved for future use */
17240 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
17241 +                             struct au_opt_mod *mod, substring_t args[])
17242 +{
17243 +       int err;
17244 +       struct dentry *root;
17245 +
17246 +       err = -EINVAL;
17247 +       root = sb->s_root;
17248 +       aufs_read_lock(root, AuLock_FLUSH);
17249 +       if (bindex < 0 || au_sbend(sb) < bindex) {
17250 +               AuErr("out of bounds, %d\n", bindex);
17251 +               goto out;
17252 +       }
17253 +
17254 +       err = 0;
17255 +       mod->perm = br_perm_val(args[1].from);
17256 +       AuDbg("mod path %s, perm 0x%x, %s\n",
17257 +             mod->path, mod->perm, args[1].from);
17258 +       mod->h_root = dget(au_h_dptr(root, bindex));
17259 +
17260 + out:
17261 +       aufs_read_unlock(root, !AuLock_IR);
17262 +       return err;
17263 +}
17264 +#endif
17265 +
17266 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
17267 +                             substring_t args[])
17268 +{
17269 +       int err;
17270 +       struct file *file;
17271 +
17272 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
17273 +       err = PTR_ERR(file);
17274 +       if (IS_ERR(file))
17275 +               goto out;
17276 +
17277 +       err = -EINVAL;
17278 +       if (unlikely(file->f_dentry->d_sb == sb)) {
17279 +               fput(file);
17280 +               AuErr("%s must be outside\n", args[0].from);
17281 +               goto out;
17282 +       }
17283 +
17284 +       err = 0;
17285 +       xino->file = file;
17286 +       xino->path = args[0].from;
17287 +
17288 + out:
17289 +       return err;
17290 +}
17291 +
17292 +static
17293 +int au_opts_parse_xino_itrunc_path(struct super_block *sb,
17294 +                                  struct au_opt_xino_itrunc *xino_itrunc,
17295 +                                  substring_t args[])
17296 +{
17297 +       int err;
17298 +       aufs_bindex_t bend, bindex;
17299 +       struct path path;
17300 +       struct dentry *root;
17301 +
17302 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
17303 +       if (unlikely(err)) {
17304 +               AuErr("lookup failed %s (%d)\n", args[0].from, err);
17305 +               goto out;
17306 +       }
17307 +
17308 +       xino_itrunc->bindex = -1;
17309 +       root = sb->s_root;
17310 +       aufs_read_lock(root, AuLock_FLUSH);
17311 +       bend = au_sbend(sb);
17312 +       for (bindex = 0; bindex <= bend; bindex++) {
17313 +               if (au_h_dptr(root, bindex) == path.dentry) {
17314 +                       xino_itrunc->bindex = bindex;
17315 +                       break;
17316 +               }
17317 +       }
17318 +       aufs_read_unlock(root, !AuLock_IR);
17319 +       path_put(&path);
17320 +
17321 +       if (unlikely(xino_itrunc->bindex < 0)) {
17322 +               AuErr("no such branch %s\n", args[0].from);
17323 +               err = -EINVAL;
17324 +       }
17325 +
17326 + out:
17327 +       return err;
17328 +}
17329 +
17330 +/* called without aufs lock */
17331 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
17332 +{
17333 +       int err, n, token;
17334 +       aufs_bindex_t bindex;
17335 +       unsigned char skipped;
17336 +       struct dentry *root;
17337 +       struct au_opt *opt, *opt_tail;
17338 +       char *opt_str;
17339 +       /* reduce the stack space */
17340 +       union {
17341 +               struct au_opt_xino_itrunc *xino_itrunc;
17342 +               struct au_opt_wbr_create *create;
17343 +       } u;
17344 +       struct {
17345 +               substring_t args[MAX_OPT_ARGS];
17346 +       } *a;
17347 +
17348 +       err = -ENOMEM;
17349 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17350 +       if (unlikely(!a))
17351 +               goto out;
17352 +
17353 +       root = sb->s_root;
17354 +       err = 0;
17355 +       bindex = 0;
17356 +       opt = opts->opt;
17357 +       opt_tail = opt + opts->max_opt - 1;
17358 +       opt->type = Opt_tail;
17359 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
17360 +               err = -EINVAL;
17361 +               skipped = 0;
17362 +               token = match_token(opt_str, options, a->args);
17363 +               switch (token) {
17364 +               case Opt_br:
17365 +                       err = 0;
17366 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
17367 +                              && *opt_str) {
17368 +                               err = opt_add(opt, opt_str, opts->sb_flags,
17369 +                                             bindex++);
17370 +                               if (unlikely(!err && ++opt > opt_tail)) {
17371 +                                       err = -E2BIG;
17372 +                                       break;
17373 +                               }
17374 +                               opt->type = Opt_tail;
17375 +                               skipped = 1;
17376 +                       }
17377 +                       break;
17378 +               case Opt_add:
17379 +                       if (unlikely(match_int(&a->args[0], &n))) {
17380 +                               AuErr("bad integer in %s\n", opt_str);
17381 +                               break;
17382 +                       }
17383 +                       bindex = n;
17384 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
17385 +                                     bindex);
17386 +                       if (!err)
17387 +                               opt->type = token;
17388 +                       break;
17389 +               case Opt_append:
17390 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
17391 +                                     /*dummy bindex*/1);
17392 +                       if (!err)
17393 +                               opt->type = token;
17394 +                       break;
17395 +               case Opt_prepend:
17396 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
17397 +                                     /*bindex*/0);
17398 +                       if (!err)
17399 +                               opt->type = token;
17400 +                       break;
17401 +               case Opt_del:
17402 +                       err = au_opts_parse_del(&opt->del, a->args);
17403 +                       if (!err)
17404 +                               opt->type = token;
17405 +                       break;
17406 +#if 0 /* reserved for future use */
17407 +               case Opt_idel:
17408 +                       del->pathname = "(indexed)";
17409 +                       if (unlikely(match_int(&args[0], &n))) {
17410 +                               AuErr("bad integer in %s\n", opt_str);
17411 +                               break;
17412 +                       }
17413 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
17414 +                       if (!err)
17415 +                               opt->type = token;
17416 +                       break;
17417 +#endif
17418 +               case Opt_mod:
17419 +                       err = au_opts_parse_mod(&opt->mod, a->args);
17420 +                       if (!err)
17421 +                               opt->type = token;
17422 +                       break;
17423 +#ifdef IMOD /* reserved for future use */
17424 +               case Opt_imod:
17425 +                       u.mod->path = "(indexed)";
17426 +                       if (unlikely(match_int(&a->args[0], &n))) {
17427 +                               AuErr("bad integer in %s\n", opt_str);
17428 +                               break;
17429 +                       }
17430 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
17431 +                       if (!err)
17432 +                               opt->type = token;
17433 +                       break;
17434 +#endif
17435 +               case Opt_xino:
17436 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
17437 +                       if (!err)
17438 +                               opt->type = token;
17439 +                       break;
17440 +
17441 +               case Opt_trunc_xino_path:
17442 +                       err = au_opts_parse_xino_itrunc_path
17443 +                               (sb, &opt->xino_itrunc, a->args);
17444 +                       if (!err)
17445 +                               opt->type = token;
17446 +                       break;
17447 +
17448 +               case Opt_itrunc_xino:
17449 +                       u.xino_itrunc = &opt->xino_itrunc;
17450 +                       if (unlikely(match_int(&a->args[0], &n))) {
17451 +                               AuErr("bad integer in %s\n", opt_str);
17452 +                               break;
17453 +                       }
17454 +                       u.xino_itrunc->bindex = n;
17455 +                       aufs_read_lock(root, AuLock_FLUSH);
17456 +                       if (n < 0 || au_sbend(sb) < n) {
17457 +                               AuErr("out of bounds, %d\n", n);
17458 +                               aufs_read_unlock(root, !AuLock_IR);
17459 +                               break;
17460 +                       }
17461 +                       aufs_read_unlock(root, !AuLock_IR);
17462 +                       err = 0;
17463 +                       opt->type = token;
17464 +                       break;
17465 +
17466 +               case Opt_dirwh:
17467 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
17468 +                               break;
17469 +                       err = 0;
17470 +                       opt->type = token;
17471 +                       break;
17472 +
17473 +               case Opt_rdcache:
17474 +                       if (unlikely(match_int(&a->args[0], &opt->rdcache)))
17475 +                               break;
17476 +                       err = 0;
17477 +                       opt->type = token;
17478 +                       break;
17479 +               case Opt_rdblk:
17480 +                       if (unlikely(match_int(&a->args[0], &n)
17481 +                                    || n <= 0
17482 +                                    || n > KMALLOC_MAX_SIZE)) {
17483 +                               AuErr("bad integer in %s\n", opt_str);
17484 +                               break;
17485 +                       }
17486 +                       if (unlikely(n < NAME_MAX)) {
17487 +                               AuErr("rdblk must be larger than %d\n",
17488 +                                     NAME_MAX);
17489 +                               break;
17490 +                       }
17491 +                       opt->rdblk = n;
17492 +                       err = 0;
17493 +                       opt->type = token;
17494 +                       break;
17495 +               case Opt_rdhash:
17496 +                       if (unlikely(match_int(&a->args[0], &n)
17497 +                                    || n <= 0
17498 +                                    || n * sizeof(struct hlist_head)
17499 +                                    > KMALLOC_MAX_SIZE)) {
17500 +                               AuErr("bad integer in %s\n", opt_str);
17501 +                               break;
17502 +                       }
17503 +                       opt->rdhash = n;
17504 +                       err = 0;
17505 +                       opt->type = token;
17506 +                       break;
17507 +
17508 +               case Opt_trunc_xino:
17509 +               case Opt_notrunc_xino:
17510 +               case Opt_noxino:
17511 +               case Opt_trunc_xib:
17512 +               case Opt_notrunc_xib:
17513 +               case Opt_plink:
17514 +               case Opt_noplink:
17515 +               case Opt_list_plink:
17516 +               case Opt_diropq_a:
17517 +               case Opt_diropq_w:
17518 +               case Opt_warn_perm:
17519 +               case Opt_nowarn_perm:
17520 +               case Opt_refrof:
17521 +               case Opt_norefrof:
17522 +               case Opt_verbose:
17523 +               case Opt_noverbose:
17524 +               case Opt_sum:
17525 +               case Opt_nosum:
17526 +               case Opt_wsum:
17527 +                       err = 0;
17528 +                       opt->type = token;
17529 +                       break;
17530 +
17531 +               case Opt_udba:
17532 +                       opt->udba = udba_val(a->args[0].from);
17533 +                       if (opt->udba >= 0) {
17534 +                               err = 0;
17535 +                               opt->type = token;
17536 +                       } else
17537 +                               AuErr("wrong value, %s\n", opt_str);
17538 +                       break;
17539 +
17540 +               case Opt_wbr_create:
17541 +                       u.create = &opt->wbr_create;
17542 +                       u.create->wbr_create
17543 +                               = au_wbr_create_val(a->args[0].from, u.create);
17544 +                       if (u.create->wbr_create >= 0) {
17545 +                               err = 0;
17546 +                               opt->type = token;
17547 +                       } else
17548 +                               AuErr("wrong value, %s\n", opt_str);
17549 +                       break;
17550 +               case Opt_wbr_copyup:
17551 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
17552 +                       if (opt->wbr_copyup >= 0) {
17553 +                               err = 0;
17554 +                               opt->type = token;
17555 +                       } else
17556 +                               AuErr("wrong value, %s\n", opt_str);
17557 +                       break;
17558 +
17559 +               case Opt_ignore:
17560 +                       AuWarn("ignored %s\n", opt_str);
17561 +                       /*FALLTHROUGH*/
17562 +               case Opt_ignore_silent:
17563 +                       skipped = 1;
17564 +                       err = 0;
17565 +                       break;
17566 +               case Opt_err:
17567 +                       AuErr("unknown option %s\n", opt_str);
17568 +                       break;
17569 +               }
17570 +
17571 +               if (!err && !skipped) {
17572 +                       if (unlikely(++opt > opt_tail)) {
17573 +                               err = -E2BIG;
17574 +                               opt--;
17575 +                               opt->type = Opt_tail;
17576 +                               break;
17577 +                       }
17578 +                       opt->type = Opt_tail;
17579 +               }
17580 +       }
17581 +
17582 +       kfree(a);
17583 +       dump_opts(opts);
17584 +       if (unlikely(err))
17585 +               au_opts_free(opts);
17586 +
17587 + out:
17588 +       return err;
17589 +}
17590 +
17591 +static int au_opt_wbr_create(struct super_block *sb,
17592 +                            struct au_opt_wbr_create *create)
17593 +{
17594 +       int err;
17595 +       struct au_sbinfo *sbinfo;
17596 +
17597 +       err = 1; /* handled */
17598 +       sbinfo = au_sbi(sb);
17599 +       if (sbinfo->si_wbr_create_ops->fin) {
17600 +               err = sbinfo->si_wbr_create_ops->fin(sb);
17601 +               if (!err)
17602 +                       err = 1;
17603 +       }
17604 +
17605 +       sbinfo->si_wbr_create = create->wbr_create;
17606 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
17607 +       switch (create->wbr_create) {
17608 +       case AuWbrCreate_MFSRRV:
17609 +       case AuWbrCreate_MFSRR:
17610 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
17611 +               /*FALLTHROUGH*/
17612 +       case AuWbrCreate_MFS:
17613 +       case AuWbrCreate_MFSV:
17614 +       case AuWbrCreate_PMFS:
17615 +       case AuWbrCreate_PMFSV:
17616 +               sbinfo->si_wbr_mfs.mfs_expire = create->mfs_second * HZ;
17617 +               break;
17618 +       }
17619 +
17620 +       if (sbinfo->si_wbr_create_ops->init)
17621 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
17622 +
17623 +       return err;
17624 +}
17625 +
17626 +/*
17627 + * returns,
17628 + * plus: processed without an error
17629 + * zero: unprocessed
17630 + */
17631 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
17632 +                        struct au_opts *opts)
17633 +{
17634 +       int err;
17635 +       struct au_sbinfo *sbinfo;
17636 +
17637 +       err = 1; /* handled */
17638 +       sbinfo = au_sbi(sb);
17639 +       switch (opt->type) {
17640 +       case Opt_udba:
17641 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
17642 +               sbinfo->si_mntflags |= opt->udba;
17643 +               opts->given_udba |= opt->udba;
17644 +               break;
17645 +
17646 +       case Opt_plink:
17647 +               au_opt_set(sbinfo->si_mntflags, PLINK);
17648 +               break;
17649 +       case Opt_noplink:
17650 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
17651 +                       au_plink_put(sb);
17652 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
17653 +               break;
17654 +       case Opt_list_plink:
17655 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
17656 +                       au_plink_list(sb);
17657 +               break;
17658 +
17659 +       case Opt_diropq_a:
17660 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
17661 +               break;
17662 +       case Opt_diropq_w:
17663 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
17664 +               break;
17665 +
17666 +       case Opt_warn_perm:
17667 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
17668 +               break;
17669 +       case Opt_nowarn_perm:
17670 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
17671 +               break;
17672 +
17673 +       case Opt_refrof:
17674 +               au_opt_set(sbinfo->si_mntflags, REFROF);
17675 +               break;
17676 +       case Opt_norefrof:
17677 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
17678 +               break;
17679 +
17680 +       case Opt_verbose:
17681 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
17682 +               break;
17683 +       case Opt_noverbose:
17684 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
17685 +               break;
17686 +
17687 +       case Opt_sum:
17688 +               au_opt_set(sbinfo->si_mntflags, SUM);
17689 +               break;
17690 +       case Opt_wsum:
17691 +               au_opt_clr(sbinfo->si_mntflags, SUM);
17692 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
17693 +       case Opt_nosum:
17694 +               au_opt_clr(sbinfo->si_mntflags, SUM);
17695 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
17696 +               break;
17697 +
17698 +       case Opt_wbr_create:
17699 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
17700 +               break;
17701 +       case Opt_wbr_copyup:
17702 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
17703 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
17704 +               break;
17705 +
17706 +       case Opt_dirwh:
17707 +               sbinfo->si_dirwh = opt->dirwh;
17708 +               break;
17709 +
17710 +       case Opt_rdcache:
17711 +               sbinfo->si_rdcache = opt->rdcache * HZ;
17712 +               break;
17713 +       case Opt_rdblk:
17714 +               sbinfo->si_rdblk = opt->rdblk;
17715 +               break;
17716 +       case Opt_rdhash:
17717 +               sbinfo->si_rdhash = opt->rdhash;
17718 +               break;
17719 +
17720 +       case Opt_trunc_xino:
17721 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
17722 +               break;
17723 +       case Opt_notrunc_xino:
17724 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
17725 +               break;
17726 +
17727 +       case Opt_trunc_xino_path:
17728 +       case Opt_itrunc_xino:
17729 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
17730 +               if (!err)
17731 +                       err = 1;
17732 +               break;
17733 +
17734 +       case Opt_trunc_xib:
17735 +               au_fset_opts(opts->flags, TRUNC_XIB);
17736 +               break;
17737 +       case Opt_notrunc_xib:
17738 +               au_fclr_opts(opts->flags, TRUNC_XIB);
17739 +               break;
17740 +
17741 +       default:
17742 +               err = 0;
17743 +               break;
17744 +       }
17745 +
17746 +       return err;
17747 +}
17748 +
17749 +/*
17750 + * returns tri-state.
17751 + * plus: processed without an error
17752 + * zero: unprocessed
17753 + * minus: error
17754 + */
17755 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
17756 +                    struct au_opts *opts)
17757 +{
17758 +       int err, do_refresh;
17759 +
17760 +       err = 0;
17761 +       switch (opt->type) {
17762 +       case Opt_append:
17763 +               opt->add.bindex = au_sbend(sb) + 1;
17764 +               if (opt->add.bindex < 0)
17765 +                       opt->add.bindex = 0;
17766 +               goto add;
17767 +       case Opt_prepend:
17768 +               opt->add.bindex = 0;
17769 +       add:
17770 +       case Opt_add:
17771 +               err = au_br_add(sb, &opt->add,
17772 +                               au_ftest_opts(opts->flags, REMOUNT));
17773 +               if (!err) {
17774 +                       err = 1;
17775 +                       au_fset_opts(opts->flags, REFRESH_DIR);
17776 +                       if (au_br_whable(opt->add.perm))
17777 +                               au_fset_opts(opts->flags, REFRESH_NONDIR);
17778 +               }
17779 +               break;
17780 +
17781 +       case Opt_del:
17782 +       case Opt_idel:
17783 +               err = au_br_del(sb, &opt->del,
17784 +                               au_ftest_opts(opts->flags, REMOUNT));
17785 +               if (!err) {
17786 +                       err = 1;
17787 +                       au_fset_opts(opts->flags, TRUNC_XIB);
17788 +                       au_fset_opts(opts->flags, REFRESH_DIR);
17789 +                       au_fset_opts(opts->flags, REFRESH_NONDIR);
17790 +               }
17791 +               break;
17792 +
17793 +       case Opt_mod:
17794 +       case Opt_imod:
17795 +               err = au_br_mod(sb, &opt->mod,
17796 +                               au_ftest_opts(opts->flags, REMOUNT),
17797 +                               &do_refresh);
17798 +               if (!err) {
17799 +                       err = 1;
17800 +                       if (do_refresh) {
17801 +                               au_fset_opts(opts->flags, REFRESH_DIR);
17802 +                               au_fset_opts(opts->flags, REFRESH_NONDIR);
17803 +                       }
17804 +               }
17805 +               break;
17806 +       }
17807 +
17808 +       return err;
17809 +}
17810 +
17811 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
17812 +                      struct au_opt_xino **opt_xino,
17813 +                      struct au_opts *opts)
17814 +{
17815 +       int err;
17816 +       aufs_bindex_t bend, bindex;
17817 +       struct dentry *root, *parent, *h_root;
17818 +
17819 +       err = 0;
17820 +       switch (opt->type) {
17821 +       case Opt_xino:
17822 +               err = au_xino_set(sb, &opt->xino,
17823 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
17824 +               if (unlikely(err))
17825 +                       break;
17826 +
17827 +               *opt_xino = &opt->xino;
17828 +               au_xino_brid_set(sb, -1);
17829 +
17830 +               /* safe d_parent access */
17831 +               parent = opt->xino.file->f_dentry->d_parent;
17832 +               root = sb->s_root;
17833 +               bend = au_sbend(sb);
17834 +               for (bindex = 0; bindex <= bend; bindex++) {
17835 +                       h_root = au_h_dptr(root, bindex);
17836 +                       if (h_root == parent) {
17837 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
17838 +                               break;
17839 +                       }
17840 +               }
17841 +               break;
17842 +
17843 +       case Opt_noxino:
17844 +               au_xino_clr(sb);
17845 +               au_xino_brid_set(sb, -1);
17846 +               *opt_xino = (void *)-1;
17847 +               break;
17848 +       }
17849 +
17850 +       return err;
17851 +}
17852 +
17853 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
17854 +                  unsigned int pending)
17855 +{
17856 +       int err;
17857 +       aufs_bindex_t bindex, bend;
17858 +       unsigned char do_plink, skip, do_free;
17859 +       struct au_branch *br;
17860 +       struct au_wbr *wbr;
17861 +       struct dentry *root;
17862 +       struct inode *dir, *h_dir;
17863 +       struct au_sbinfo *sbinfo;
17864 +       struct au_hinode *hdir;
17865 +
17866 +       sbinfo = au_sbi(sb);
17867 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
17868 +
17869 +       if (unlikely(!(sb_flags & MS_RDONLY)
17870 +                    && !au_br_writable(au_sbr_perm(sb, 0))))
17871 +               AuWarn("first branch should be rw\n");
17872 +
17873 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HINOTIFY)
17874 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
17875 +               AuWarn("udba=inotify requires xino\n");
17876 +
17877 +       err = 0;
17878 +       root = sb->s_root;
17879 +       dir = sb->s_root->d_inode;
17880 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
17881 +       bend = au_sbend(sb);
17882 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
17883 +               skip = 0;
17884 +               h_dir = au_h_iptr(dir, bindex);
17885 +               br = au_sbr(sb, bindex);
17886 +               do_free = 0;
17887 +
17888 +               wbr = br->br_wbr;
17889 +               if (wbr)
17890 +                       wbr_wh_read_lock(wbr);
17891 +
17892 +               switch (br->br_perm) {
17893 +               case AuBrPerm_RO:
17894 +               case AuBrPerm_ROWH:
17895 +               case AuBrPerm_RR:
17896 +               case AuBrPerm_RRWH:
17897 +                       do_free = !!wbr;
17898 +                       skip = (!wbr
17899 +                               || (!wbr->wbr_whbase
17900 +                                   && !wbr->wbr_plink
17901 +                                   && !wbr->wbr_orph));
17902 +                       break;
17903 +
17904 +               case AuBrPerm_RWNoLinkWH:
17905 +                       /* skip = (!br->br_whbase && !br->br_orph); */
17906 +                       skip = (!wbr || !wbr->wbr_whbase);
17907 +                       if (skip && wbr) {
17908 +                               if (do_plink)
17909 +                                       skip = !!wbr->wbr_plink;
17910 +                               else
17911 +                                       skip = !wbr->wbr_plink;
17912 +                       }
17913 +                       break;
17914 +
17915 +               case AuBrPerm_RW:
17916 +                       /* skip = (br->br_whbase && br->br_ohph); */
17917 +                       skip = (wbr && wbr->wbr_whbase);
17918 +                       if (skip) {
17919 +                               if (do_plink)
17920 +                                       skip = !!wbr->wbr_plink;
17921 +                               else
17922 +                                       skip = !wbr->wbr_plink;
17923 +                       }
17924 +                       break;
17925 +
17926 +               default:
17927 +                       BUG();
17928 +               }
17929 +               if (wbr)
17930 +                       wbr_wh_read_unlock(wbr);
17931 +
17932 +               if (skip)
17933 +                       continue;
17934 +
17935 +               hdir = au_hi(dir, bindex);
17936 +               au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
17937 +               if (wbr)
17938 +                       wbr_wh_write_lock(wbr);
17939 +               err = au_wh_init(au_h_dptr(root, bindex), br, sb);
17940 +               if (wbr)
17941 +                       wbr_wh_write_unlock(wbr);
17942 +               au_hin_imtx_unlock(hdir);
17943 +
17944 +               if (!err && do_free) {
17945 +                       kfree(wbr);
17946 +                       br->br_wbr = NULL;
17947 +               }
17948 +       }
17949 +
17950 +       return err;
17951 +}
17952 +
17953 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
17954 +{
17955 +       int err;
17956 +       unsigned int tmp;
17957 +       aufs_bindex_t bend;
17958 +       struct au_opt *opt;
17959 +       struct au_opt_xino *opt_xino, xino;
17960 +       struct au_sbinfo *sbinfo;
17961 +
17962 +       err = 0;
17963 +       opt_xino = NULL;
17964 +       opt = opts->opt;
17965 +       while (err >= 0 && opt->type != Opt_tail)
17966 +               err = au_opt_simple(sb, opt++, opts);
17967 +       if (err > 0)
17968 +               err = 0;
17969 +       else if (unlikely(err < 0))
17970 +               goto out;
17971 +
17972 +       /* disable xino and udba temporary */
17973 +       sbinfo = au_sbi(sb);
17974 +       tmp = sbinfo->si_mntflags;
17975 +       au_opt_clr(sbinfo->si_mntflags, XINO);
17976 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
17977 +
17978 +       opt = opts->opt;
17979 +       while (err >= 0 && opt->type != Opt_tail)
17980 +               err = au_opt_br(sb, opt++, opts);
17981 +       if (err > 0)
17982 +               err = 0;
17983 +       else if (unlikely(err < 0))
17984 +               goto out;
17985 +
17986 +       bend = au_sbend(sb);
17987 +       if (unlikely(bend < 0)) {
17988 +               err = -EINVAL;
17989 +               AuErr("no branches\n");
17990 +               goto out;
17991 +       }
17992 +
17993 +       if (au_opt_test(tmp, XINO))
17994 +               au_opt_set(sbinfo->si_mntflags, XINO);
17995 +       opt = opts->opt;
17996 +       while (!err && opt->type != Opt_tail)
17997 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
17998 +       if (unlikely(err))
17999 +               goto out;
18000 +
18001 +       err = au_opts_verify(sb, sb->s_flags, tmp);
18002 +       if (unlikely(err))
18003 +               goto out;
18004 +
18005 +       /* restore xino */
18006 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
18007 +               xino.file = au_xino_def(sb);
18008 +               err = PTR_ERR(xino.file);
18009 +               if (IS_ERR(xino.file))
18010 +                       goto out;
18011 +
18012 +               err = au_xino_set(sb, &xino, /*remount*/0);
18013 +               fput(xino.file);
18014 +               if (unlikely(err))
18015 +                       goto out;
18016 +       }
18017 +
18018 +       /* restore udba */
18019 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
18020 +       sbinfo->si_mntflags |= (tmp & AuOptMask_UDBA);
18021 +       if (au_opt_test(tmp, UDBA_HINOTIFY)) {
18022 +               struct inode *dir = sb->s_root->d_inode;
18023 +               au_reset_hinotify(dir,
18024 +                                 au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
18025 +       }
18026 +
18027 + out:
18028 +       return err;
18029 +}
18030 +
18031 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
18032 +{
18033 +       int err, rerr;
18034 +       struct inode *dir;
18035 +       struct au_opt_xino *opt_xino;
18036 +       struct au_opt *opt;
18037 +       struct au_sbinfo *sbinfo;
18038 +
18039 +       dir = sb->s_root->d_inode;
18040 +       sbinfo = au_sbi(sb);
18041 +       err = 0;
18042 +       opt_xino = NULL;
18043 +       opt = opts->opt;
18044 +       while (err >= 0 && opt->type != Opt_tail) {
18045 +               err = au_opt_simple(sb, opt, opts);
18046 +               if (!err)
18047 +                       err = au_opt_br(sb, opt, opts);
18048 +               if (!err)
18049 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
18050 +               opt++;
18051 +       }
18052 +       if (err > 0)
18053 +               err = 0;
18054 +       AuTraceErr(err);
18055 +       /* go on even err */
18056 +
18057 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
18058 +       if (unlikely(rerr && !err))
18059 +               err = rerr;
18060 +
18061 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
18062 +               rerr = au_xib_trunc(sb);
18063 +               if (unlikely(rerr && !err))
18064 +                       err = rerr;
18065 +       }
18066 +
18067 +       /* will be handled by the caller */
18068 +       if (!au_ftest_opts(opts->flags, REFRESH_DIR)
18069 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
18070 +               au_fset_opts(opts->flags, REFRESH_DIR);
18071 +
18072 +       AuDbg("status 0x%x\n", opts->flags);
18073 +       return err;
18074 +}
18075 +
18076 +/* ---------------------------------------------------------------------- */
18077 +
18078 +unsigned int au_opt_udba(struct super_block *sb)
18079 +{
18080 +       return au_mntflags(sb) & AuOptMask_UDBA;
18081 +}
18082 diff --git a/fs/aufs/opts.h b/fs/aufs/opts.h
18083 new file mode 100644
18084 index 0000000..275702c
18085 --- /dev/null
18086 +++ b/fs/aufs/opts.h
18087 @@ -0,0 +1,180 @@
18088 +/*
18089 + * Copyright (C) 2005-2009 Junjiro R. Okajima
18090 + *
18091 + * This program, aufs is free software; you can redistribute it and/or modify
18092 + * it under the terms of the GNU General Public License as published by
18093 + * the Free Software Foundation; either version 2 of the License, or
18094 + * (at your option) any later version.
18095 + */
18096 +
18097 +/*
18098 + * mount options/flags
18099 + */
18100 +
18101 +#ifndef __AUFS_OPTS_H__
18102 +#define __AUFS_OPTS_H__
18103 +
18104 +#ifdef __KERNEL__
18105 +
18106 +#include <linux/fs.h>
18107 +#include <linux/namei.h>
18108 +#include <linux/aufs_type.h>
18109 +
18110 +/* ---------------------------------------------------------------------- */
18111 +
18112 +/* mount flags */
18113 +#define AuOpt_XINO             1               /* external inode number bitmap
18114 +                                                  and translation table */
18115 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
18116 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
18117 +#define AuOpt_UDBA_REVAL       (1 << 3)
18118 +#define AuOpt_UDBA_HINOTIFY    (1 << 4)
18119 +#define AuOpt_PLINK            (1 << 5)        /* pseudo-link */
18120 +#define AuOpt_DIRPERM1         (1 << 6)        /* unimplemented */
18121 +#define AuOpt_REFROF           (1 << 7)        /* unimplemented */
18122 +#define AuOpt_ALWAYS_DIROPQ    (1 << 8)        /* policy to creating diropq */
18123 +#define AuOpt_SUM              (1 << 9)        /* summation for statfs(2) */
18124 +#define AuOpt_SUM_W            (1 << 10)       /* unimplemented */
18125 +#define AuOpt_WARN_PERM                (1 << 11)       /* warn when add-branch */
18126 +#define AuOpt_VERBOSE          (1 << 12)       /* busy inode when del-branch */
18127 +
18128 +#ifndef CONFIG_AUFS_HINOTIFY
18129 +#undef AuOpt_UDBA_HINOTIFY
18130 +#define AuOpt_UDBA_HINOTIFY    0
18131 +#endif
18132 +
18133 +#define AuOpt_Def      (AuOpt_XINO \
18134 +                        | AuOpt_UDBA_REVAL \
18135 +                        | AuOpt_PLINK \
18136 +                        /* | AuOpt_DIRPERM1 */ \
18137 +                        | AuOpt_WARN_PERM)
18138 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
18139 +                        | AuOpt_UDBA_REVAL \
18140 +                        | AuOpt_UDBA_HINOTIFY)
18141 +
18142 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
18143 +#define au_opt_set(flags, name) do { \
18144 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
18145 +       ((flags) |= AuOpt_##name); \
18146 +} while (0)
18147 +#define au_opt_set_udba(flags, name) do { \
18148 +       (flags) &= ~AuOptMask_UDBA; \
18149 +       ((flags) |= AuOpt_##name); \
18150 +} while (0)
18151 +#define au_opt_clr(flags, name)                { ((flags) &= ~AuOpt_##name); }
18152 +
18153 +/* ---------------------------------------------------------------------- */
18154 +
18155 +/* policies to select one among multiple writable branches */
18156 +enum {
18157 +       AuWbrCreate_TDP,        /* top down parent */
18158 +       AuWbrCreate_RR,         /* round robin */
18159 +       AuWbrCreate_MFS,        /* most free space */
18160 +       AuWbrCreate_MFSV,       /* mfs with seconds */
18161 +       AuWbrCreate_MFSRR,      /* mfs then rr */
18162 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
18163 +       AuWbrCreate_PMFS,       /* parent and mfs */
18164 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
18165 +
18166 +       AuWbrCreate_Def = AuWbrCreate_TDP
18167 +};
18168 +
18169 +enum {
18170 +       AuWbrCopyup_TDP,        /* top down parent */
18171 +       AuWbrCopyup_BUP,        /* bottom up parent */
18172 +       AuWbrCopyup_BU,         /* bottom up */
18173 +
18174 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
18175 +};
18176 +
18177 +/* ---------------------------------------------------------------------- */
18178 +
18179 +struct au_opt_add {
18180 +       aufs_bindex_t   bindex;
18181 +       char            *pathname;
18182 +       int             perm;
18183 +       struct path     path;
18184 +};
18185 +
18186 +struct au_opt_del {
18187 +       char            *pathname;
18188 +       struct path     h_path;
18189 +};
18190 +
18191 +struct au_opt_mod {
18192 +       char            *path;
18193 +       int             perm;
18194 +       struct dentry   *h_root;
18195 +};
18196 +
18197 +struct au_opt_xino {
18198 +       char            *path;
18199 +       struct file     *file;
18200 +};
18201 +
18202 +struct au_opt_xino_itrunc {
18203 +       aufs_bindex_t   bindex;
18204 +};
18205 +
18206 +struct au_opt_wbr_create {
18207 +       int                     wbr_create;
18208 +       int                     mfs_second;
18209 +       unsigned long long      mfsrr_watermark;
18210 +};
18211 +
18212 +struct au_opt {
18213 +       int type;
18214 +       union {
18215 +               struct au_opt_xino      xino;
18216 +               struct au_opt_xino_itrunc xino_itrunc;
18217 +               struct au_opt_add       add;
18218 +               struct au_opt_del       del;
18219 +               struct au_opt_mod       mod;
18220 +               int                     dirwh;
18221 +               int                     rdcache;
18222 +               unsigned int            rdblk;
18223 +               unsigned int            rdhash;
18224 +               int                     udba;
18225 +               struct au_opt_wbr_create wbr_create;
18226 +               int                     wbr_copyup;
18227 +       };
18228 +};
18229 +
18230 +/* opts flags */
18231 +#define AuOpts_REMOUNT         1
18232 +#define AuOpts_REFRESH_DIR     (1 << 1)
18233 +#define AuOpts_REFRESH_NONDIR  (1 << 2)
18234 +#define AuOpts_TRUNC_XIB       (1 << 3)
18235 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
18236 +#define au_fset_opts(flags, name)      { (flags) |= AuOpts_##name; }
18237 +#define au_fclr_opts(flags, name)      { (flags) &= ~AuOpts_##name; }
18238 +
18239 +struct au_opts {
18240 +       struct au_opt   *opt;
18241 +       int             max_opt;
18242 +
18243 +       unsigned int    given_udba;
18244 +       unsigned int    flags;
18245 +       unsigned long   sb_flags;
18246 +};
18247 +
18248 +/* ---------------------------------------------------------------------- */
18249 +
18250 +const char *au_optstr_br_perm(int brperm);
18251 +const char *au_optstr_udba(int udba);
18252 +const char *au_optstr_wbr_copyup(int wbr_copyup);
18253 +const char *au_optstr_wbr_create(int wbr_create);
18254 +
18255 +void au_opts_free(struct au_opts *opts);
18256 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
18257 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
18258 +                  unsigned int pending);
18259 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
18260 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
18261 +
18262 +unsigned int au_opt_udba(struct super_block *sb);
18263 +
18264 +/* ---------------------------------------------------------------------- */
18265 +
18266 +#endif /* __KERNEL__ */
18267 +#endif /* __AUFS_OPTS_H__ */
18268 diff --git a/fs/aufs/plink.c b/fs/aufs/plink.c
18269 new file mode 100644
18270 index 0000000..62d258f
18271 --- /dev/null
18272 +++ b/fs/aufs/plink.c
18273 @@ -0,0 +1,335 @@
18274 +/*
18275 + * Copyright (C) 2005-2009 Junjiro R. Okajima
18276 + *
18277 + * This program, aufs is free software; you can redistribute it and/or modify
18278 + * it under the terms of the GNU General Public License as published by
18279 + * the Free Software Foundation; either version 2 of the License, or
18280 + * (at your option) any later version.
18281 + */
18282 +
18283 +/*
18284 + * pseudo-link
18285 + */
18286 +
18287 +#include "aufs.h"
18288 +
18289 +/*
18290 + * during a user process maintains the pseudo-links,
18291 + * prohibit adding a new plink and branch manipulation.
18292 + */
18293 +void au_plink_block_maintain(struct super_block *sb)
18294 +{
18295 +       struct au_sbinfo *sbi = au_sbi(sb);
18296 +       /* gave up wake_up_bit() */
18297 +       wait_event(sbi->si_plink_wq, !au_ftest_si(sbi, MAINTAIN_PLINK));
18298 +}
18299 +
18300 +/* ---------------------------------------------------------------------- */
18301 +
18302 +struct pseudo_link {
18303 +       struct list_head list;
18304 +       struct inode *inode;
18305 +};
18306 +
18307 +#ifdef CONFIG_AUFS_DEBUG
18308 +void au_plink_list(struct super_block *sb)
18309 +{
18310 +       struct au_sbinfo *sbinfo;
18311 +       struct list_head *plink_list;
18312 +       struct pseudo_link *plink;
18313 +
18314 +       sbinfo = au_sbi(sb);
18315 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
18316 +
18317 +       plink_list = &sbinfo->si_plink.head;
18318 +       spin_lock(&sbinfo->si_plink.spin);
18319 +       list_for_each_entry(plink, plink_list, list)
18320 +               AuDbg("%lu\n", plink->inode->i_ino);
18321 +       spin_unlock(&sbinfo->si_plink.spin);
18322 +}
18323 +#endif
18324 +
18325 +/* is the inode pseudo-linked? */
18326 +int au_plink_test(struct inode *inode)
18327 +{
18328 +       int found;
18329 +       struct au_sbinfo *sbinfo;
18330 +       struct list_head *plink_list;
18331 +       struct pseudo_link *plink;
18332 +
18333 +       sbinfo = au_sbi(inode->i_sb);
18334 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
18335 +
18336 +       found = 0;
18337 +       plink_list = &sbinfo->si_plink.head;
18338 +       spin_lock(&sbinfo->si_plink.spin);
18339 +       list_for_each_entry(plink, plink_list, list)
18340 +               if (plink->inode == inode) {
18341 +                       found = 1;
18342 +                       break;
18343 +               }
18344 +       spin_unlock(&sbinfo->si_plink.spin);
18345 +       return found;
18346 +}
18347 +
18348 +/* ---------------------------------------------------------------------- */
18349 +
18350 +/*
18351 + * generate a name for plink.
18352 + * the file will be stored under AUFS_WH_PLINKDIR.
18353 + */
18354 +/* 20 is max digits length of ulong 64 */
18355 +#define PLINK_NAME_LEN ((20 + 1) * 2)
18356 +
18357 +static int plink_name(char *name, int len, struct inode *inode,
18358 +                     aufs_bindex_t bindex)
18359 +{
18360 +       int rlen;
18361 +       struct inode *h_inode;
18362 +
18363 +       h_inode = au_h_iptr(inode, bindex);
18364 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
18365 +       return rlen;
18366 +}
18367 +
18368 +/* lookup the plink-ed @inode under the branch at @bindex */
18369 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
18370 +{
18371 +       struct dentry *h_dentry, *h_parent;
18372 +       struct au_branch *br;
18373 +       struct inode *h_dir;
18374 +       char a[PLINK_NAME_LEN];
18375 +       struct qstr tgtname = {
18376 +               .name   = a
18377 +       };
18378 +
18379 +       br = au_sbr(inode->i_sb, bindex);
18380 +       h_parent = br->br_wbr->wbr_plink;
18381 +       h_dir = h_parent->d_inode;
18382 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
18383 +
18384 +       /* always superio. */
18385 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
18386 +       h_dentry = au_sio_lkup_one(&tgtname, h_parent, br);
18387 +       mutex_unlock(&h_dir->i_mutex);
18388 +       return h_dentry;
18389 +}
18390 +
18391 +/* create a pseudo-link */
18392 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
18393 +                     struct dentry *h_dentry, struct au_branch *br)
18394 +{
18395 +       int err;
18396 +       struct path h_path = {
18397 +               .mnt = br->br_mnt
18398 +       };
18399 +       struct inode *h_dir;
18400 +
18401 +       h_dir = h_parent->d_inode;
18402 + again:
18403 +       h_path.dentry = au_lkup_one(tgt, h_parent, br, /*nd*/NULL);
18404 +       err = PTR_ERR(h_path.dentry);
18405 +       if (IS_ERR(h_path.dentry))
18406 +               goto out;
18407 +
18408 +       err = 0;
18409 +       /* wh.plink dir is not monitored */
18410 +       if (h_path.dentry->d_inode
18411 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
18412 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
18413 +               dput(h_path.dentry);
18414 +               h_path.dentry = NULL;
18415 +               if (!err)
18416 +                       goto again;
18417 +       }
18418 +       if (!err && !h_path.dentry->d_inode)
18419 +               err = vfsub_link(h_dentry, h_dir, &h_path);
18420 +       dput(h_path.dentry);
18421 +
18422 + out:
18423 +       return err;
18424 +}
18425 +
18426 +struct do_whplink_args {
18427 +       int *errp;
18428 +       struct qstr *tgt;
18429 +       struct dentry *h_parent;
18430 +       struct dentry *h_dentry;
18431 +       struct au_branch *br;
18432 +};
18433 +
18434 +static void call_do_whplink(void *args)
18435 +{
18436 +       struct do_whplink_args *a = args;
18437 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
18438 +}
18439 +
18440 +static int whplink(struct dentry *h_dentry, struct inode *inode,
18441 +                  aufs_bindex_t bindex, struct au_branch *br)
18442 +{
18443 +       int err, wkq_err;
18444 +       struct au_wbr *wbr;
18445 +       struct dentry *h_parent;
18446 +       struct inode *h_dir;
18447 +       char a[PLINK_NAME_LEN];
18448 +       struct qstr tgtname = {
18449 +               .name = a
18450 +       };
18451 +
18452 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
18453 +       h_parent = wbr->wbr_plink;
18454 +       h_dir = h_parent->d_inode;
18455 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
18456 +
18457 +       /* always superio. */
18458 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
18459 +       if (!au_test_wkq(current)) {
18460 +               struct do_whplink_args args = {
18461 +                       .errp           = &err,
18462 +                       .tgt            = &tgtname,
18463 +                       .h_parent       = h_parent,
18464 +                       .h_dentry       = h_dentry,
18465 +                       .br             = br
18466 +               };
18467 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
18468 +               if (unlikely(wkq_err))
18469 +                       err = wkq_err;
18470 +       } else
18471 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
18472 +       mutex_unlock(&h_dir->i_mutex);
18473 +
18474 +       return err;
18475 +}
18476 +
18477 +/* free a single plink */
18478 +static void do_put_plink(struct pseudo_link *plink, int do_del)
18479 +{
18480 +       iput(plink->inode);
18481 +       if (do_del)
18482 +               list_del(&plink->list);
18483 +       kfree(plink);
18484 +}
18485 +
18486 +/*
18487 + * create a new pseudo-link for @h_dentry on @bindex.
18488 + * the linked inode is held in aufs @inode.
18489 + */
18490 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18491 +                    struct dentry *h_dentry)
18492 +{
18493 +       struct super_block *sb;
18494 +       struct au_sbinfo *sbinfo;
18495 +       struct list_head *plink_list;
18496 +       struct pseudo_link *plink;
18497 +       int found, err, cnt;
18498 +
18499 +       sb = inode->i_sb;
18500 +       sbinfo = au_sbi(sb);
18501 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
18502 +
18503 +       err = 0;
18504 +       cnt = 0;
18505 +       found = 0;
18506 +       plink_list = &sbinfo->si_plink.head;
18507 +       spin_lock(&sbinfo->si_plink.spin);
18508 +       list_for_each_entry(plink, plink_list, list) {
18509 +               cnt++;
18510 +               if (plink->inode == inode) {
18511 +                       found = 1;
18512 +                       break;
18513 +               }
18514 +       }
18515 +       if (found) {
18516 +               spin_unlock(&sbinfo->si_plink.spin);
18517 +               return;
18518 +       }
18519 +
18520 +       plink = NULL;
18521 +       if (!found) {
18522 +               plink = kmalloc(sizeof(*plink), GFP_ATOMIC);
18523 +               if (plink) {
18524 +                       plink->inode = au_igrab(inode);
18525 +                       list_add(&plink->list, plink_list);
18526 +                       cnt++;
18527 +               } else
18528 +                       err = -ENOMEM;
18529 +       }
18530 +       spin_unlock(&sbinfo->si_plink.spin);
18531 +
18532 +       if (!err) {
18533 +               au_plink_block_maintain(sb);
18534 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
18535 +       }
18536 +
18537 +       if (unlikely(cnt > AUFS_PLINK_WARN))
18538 +               AuWarn1("unexpectedly many pseudo links, %d\n", cnt);
18539 +       if (unlikely(err)) {
18540 +               AuWarn("err %d, damaged pseudo link.\n", err);
18541 +               if (!found && plink)
18542 +                       do_put_plink(plink, /*do_del*/1);
18543 +       }
18544 +}
18545 +
18546 +/* free all plinks */
18547 +void au_plink_put(struct super_block *sb)
18548 +{
18549 +       struct au_sbinfo *sbinfo;
18550 +       struct list_head *plink_list;
18551 +       struct pseudo_link *plink, *tmp;
18552 +
18553 +       sbinfo = au_sbi(sb);
18554 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
18555 +
18556 +       plink_list = &sbinfo->si_plink.head;
18557 +       /* no spin_lock since sbinfo is write-locked */
18558 +       list_for_each_entry_safe(plink, tmp, plink_list, list)
18559 +               do_put_plink(plink, 0);
18560 +       INIT_LIST_HEAD(plink_list);
18561 +}
18562 +
18563 +/* free the plinks on a branch specified by @br_id */
18564 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
18565 +{
18566 +       struct au_sbinfo *sbinfo;
18567 +       struct list_head *plink_list;
18568 +       struct pseudo_link *plink, *tmp;
18569 +       struct inode *inode;
18570 +       aufs_bindex_t bstart, bend, bindex;
18571 +       unsigned char do_put;
18572 +
18573 +       sbinfo = au_sbi(sb);
18574 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
18575 +
18576 +       plink_list = &sbinfo->si_plink.head;
18577 +       /* no spin_lock since sbinfo is write-locked */
18578 +       list_for_each_entry_safe(plink, tmp, plink_list, list) {
18579 +               do_put = 0;
18580 +               inode = au_igrab(plink->inode);
18581 +               ii_write_lock_child(inode);
18582 +               bstart = au_ibstart(inode);
18583 +               bend = au_ibend(inode);
18584 +               if (bstart >= 0) {
18585 +                       for (bindex = bstart; bindex <= bend; bindex++) {
18586 +                               if (!au_h_iptr(inode, bindex)
18587 +                                   || au_ii_br_id(inode, bindex) != br_id)
18588 +                                       continue;
18589 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18590 +                               do_put = 1;
18591 +                               break;
18592 +                       }
18593 +               } else
18594 +                       do_put_plink(plink, 1);
18595 +
18596 +               if (do_put) {
18597 +                       for (bindex = bstart; bindex <= bend; bindex++)
18598 +                               if (au_h_iptr(inode, bindex)) {
18599 +                                       do_put = 0;
18600 +                                       break;
18601 +                               }
18602 +                       if (do_put)
18603 +                               do_put_plink(plink, 1);
18604 +               }
18605 +               ii_write_unlock(inode);
18606 +               iput(inode);
18607 +       }
18608 +}
18609 diff --git a/fs/aufs/rwsem.h b/fs/aufs/rwsem.h
18610 new file mode 100644
18611 index 0000000..d569243
18612 --- /dev/null
18613 +++ b/fs/aufs/rwsem.h
18614 @@ -0,0 +1,52 @@
18615 +/*
18616 + * Copyright (C) 2005-2009 Junjiro R. Okajima
18617 + *
18618 + * This program, aufs is free software; you can redistribute it and/or modify
18619 + * it under the terms of the GNU General Public License as published by
18620 + * the Free Software Foundation; either version 2 of the License, or
18621 + * (at your option) any later version.
18622 + */
18623 +
18624 +/*
18625 + * simple read-write semaphore wrappers
18626 + */
18627 +
18628 +#ifndef __AUFS_RWSEM_H__
18629 +#define __AUFS_RWSEM_H__
18630 +
18631 +#ifdef __KERNEL__
18632 +
18633 +#include <linux/fs.h>
18634 +
18635 +#define au_rwsem_destroy(rw)   AuDebugOn(rwsem_is_locked(rw))
18636 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->wait_list))
18637 +
18638 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
18639 +static inline void prefix##_read_lock(param) \
18640 +{ down_read(rwsem); } \
18641 +static inline void prefix##_write_lock(param) \
18642 +{ down_write(rwsem); } \
18643 +static inline int prefix##_read_trylock(param) \
18644 +{ return down_read_trylock(rwsem); } \
18645 +static inline int prefix##_write_trylock(param) \
18646 +{ return down_write_trylock(rwsem); }
18647 +/* why is not _nested version defined */
18648 +/* static inline void prefix##_read_trylock_nested(param, lsc)
18649 +{ down_write_trylock_nested(rwsem, lsc)); }
18650 +static inline void prefix##_write_trylock_nestd(param, lsc)
18651 +{ down_write_trylock_nested(rwsem, lsc); } */
18652 +
18653 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
18654 +static inline void prefix##_read_unlock(param) \
18655 +{ up_read(rwsem); } \
18656 +static inline void prefix##_write_unlock(param) \
18657 +{ up_write(rwsem); } \
18658 +static inline void prefix##_downgrade_lock(param) \
18659 +{ downgrade_write(rwsem); }
18660 +
18661 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
18662 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
18663 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
18664 +
18665 +#endif /* __KERNEL__ */
18666 +#endif /* __AUFS_RWSEM_H__ */
18667 diff --git a/fs/aufs/sbinfo.c b/fs/aufs/sbinfo.c
18668 new file mode 100644
18669 index 0000000..37e0649
18670 --- /dev/null
18671 +++ b/fs/aufs/sbinfo.c
18672 @@ -0,0 +1,194 @@
18673 +/*
18674 + * Copyright (C) 2005-2009 Junjiro R. Okajima
18675 + *
18676 + * This program, aufs is free software; you can redistribute it and/or modify
18677 + * it under the terms of the GNU General Public License as published by
18678 + * the Free Software Foundation; either version 2 of the License, or
18679 + * (at your option) any later version.
18680 + */
18681 +
18682 +/*
18683 + * superblock private data
18684 + */
18685 +
18686 +#include "aufs.h"
18687 +
18688 +/*
18689 + * they are necessary regardless sysfs is disabled.
18690 + */
18691 +void au_si_free(struct kobject *kobj)
18692 +{
18693 +       struct au_sbinfo *sbinfo;
18694 +       struct super_block *sb;
18695 +
18696 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
18697 +       AuDebugOn(!list_empty(&sbinfo->si_plink.head));
18698 +
18699 +       sb = sbinfo->si_sb;
18700 +       si_write_lock(sb);
18701 +       au_xino_clr(sb);
18702 +       au_br_free(sbinfo);
18703 +       kfree(sbinfo->si_branch);
18704 +       mutex_destroy(&sbinfo->si_xib_mtx);
18705 +       si_write_unlock(sb);
18706 +       au_rwsem_destroy(&sbinfo->si_rwsem);
18707 +
18708 +       kfree(sbinfo);
18709 +}
18710 +
18711 +int au_si_alloc(struct super_block *sb)
18712 +{
18713 +       int err;
18714 +       struct au_sbinfo *sbinfo;
18715 +
18716 +       err = -ENOMEM;
18717 +       sbinfo = kmalloc(sizeof(*sbinfo), GFP_NOFS);
18718 +       if (unlikely(!sbinfo))
18719 +               goto out;
18720 +
18721 +       /* will be reallocated separately */
18722 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
18723 +       if (unlikely(!sbinfo->si_branch))
18724 +               goto out_sbinfo;
18725 +
18726 +       memset(&sbinfo->si_kobj, 0, sizeof(sbinfo->si_kobj));
18727 +       err = sysaufs_si_init(sbinfo);
18728 +       if (unlikely(err))
18729 +               goto out_br;
18730 +
18731 +       au_nwt_init(&sbinfo->si_nowait);
18732 +       init_rwsem(&sbinfo->si_rwsem);
18733 +       down_write(&sbinfo->si_rwsem);
18734 +       sbinfo->si_generation = 0;
18735 +       sbinfo->au_si_status = 0;
18736 +       sbinfo->si_bend = -1;
18737 +       sbinfo->si_last_br_id = 0;
18738 +
18739 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
18740 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
18741 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + AuWbrCopyup_Def;
18742 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + AuWbrCreate_Def;
18743 +
18744 +       sbinfo->si_mntflags = AuOpt_Def;
18745 +
18746 +       sbinfo->si_xread = NULL;
18747 +       sbinfo->si_xwrite = NULL;
18748 +       sbinfo->si_xib = NULL;
18749 +       mutex_init(&sbinfo->si_xib_mtx);
18750 +       sbinfo->si_xib_buf = NULL;
18751 +       sbinfo->si_xino_brid = -1;
18752 +       /* leave si_xib_last_pindex and si_xib_next_bit */
18753 +
18754 +       sbinfo->si_rdcache = AUFS_RDCACHE_DEF * HZ;
18755 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
18756 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
18757 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
18758 +
18759 +       au_spl_init(&sbinfo->si_plink);
18760 +       init_waitqueue_head(&sbinfo->si_plink_wq);
18761 +
18762 +       /* leave other members for sysaufs and si_mnt. */
18763 +       sbinfo->si_sb = sb;
18764 +       sb->s_fs_info = sbinfo;
18765 +       au_debug_sbinfo_init(sbinfo);
18766 +       return 0; /* success */
18767 +
18768 + out_br:
18769 +       kfree(sbinfo->si_branch);
18770 + out_sbinfo:
18771 +       kfree(sbinfo);
18772 + out:
18773 +       return err;
18774 +}
18775 +
18776 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
18777 +{
18778 +       int err, sz;
18779 +       struct au_branch **brp;
18780 +
18781 +       err = -ENOMEM;
18782 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
18783 +       if (unlikely(!sz))
18784 +               sz = sizeof(*brp);
18785 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
18786 +       if (brp) {
18787 +               sbinfo->si_branch = brp;
18788 +               err = 0;
18789 +       }
18790 +
18791 +       return err;
18792 +}
18793 +
18794 +/* ---------------------------------------------------------------------- */
18795 +
18796 +unsigned int au_sigen_inc(struct super_block *sb)
18797 +{
18798 +       unsigned int gen;
18799 +
18800 +       gen = ++au_sbi(sb)->si_generation;
18801 +       au_update_digen(sb->s_root);
18802 +       au_update_iigen(sb->s_root->d_inode);
18803 +       sb->s_root->d_inode->i_version++;
18804 +       return gen;
18805 +}
18806 +
18807 +aufs_bindex_t au_new_br_id(struct super_block *sb)
18808 +{
18809 +       aufs_bindex_t br_id;
18810 +       int i;
18811 +       struct au_sbinfo *sbinfo;
18812 +
18813 +       sbinfo = au_sbi(sb);
18814 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
18815 +               br_id = ++sbinfo->si_last_br_id;
18816 +               if (br_id && au_br_index(sb, br_id) < 0)
18817 +                       return br_id;
18818 +       }
18819 +
18820 +       return -1;
18821 +}
18822 +
18823 +/* ---------------------------------------------------------------------- */
18824 +
18825 +/* dentry and super_block lock. call at entry point */
18826 +void aufs_read_lock(struct dentry *dentry, int flags)
18827 +{
18828 +       si_read_lock(dentry->d_sb, flags);
18829 +       if (au_ftest_lock(flags, DW))
18830 +               di_write_lock_child(dentry);
18831 +       else
18832 +               di_read_lock_child(dentry, flags);
18833 +}
18834 +
18835 +void aufs_read_unlock(struct dentry *dentry, int flags)
18836 +{
18837 +       if (au_ftest_lock(flags, DW))
18838 +               di_write_unlock(dentry);
18839 +       else
18840 +               di_read_unlock(dentry, flags);
18841 +       si_read_unlock(dentry->d_sb);
18842 +}
18843 +
18844 +void aufs_write_lock(struct dentry *dentry)
18845 +{
18846 +       si_write_lock(dentry->d_sb);
18847 +       di_write_lock_child(dentry);
18848 +}
18849 +
18850 +void aufs_write_unlock(struct dentry *dentry)
18851 +{
18852 +       di_write_unlock(dentry);
18853 +       si_write_unlock(dentry->d_sb);
18854 +}
18855 +
18856 +void aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
18857 +{
18858 +       si_read_lock(d1->d_sb, flags);
18859 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
18860 +}
18861 +
18862 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
18863 +{
18864 +       di_write_unlock2(d1, d2);
18865 +       si_read_unlock(d1->d_sb);
18866 +}
18867 diff --git a/fs/aufs/spl.h b/fs/aufs/spl.h
18868 new file mode 100644
18869 index 0000000..89ea7a2
18870 --- /dev/null
18871 +++ b/fs/aufs/spl.h
18872 @@ -0,0 +1,47 @@
18873 +/*
18874 + * Copyright (C) 2005-2009 Junjiro R. Okajima
18875 + *
18876 + * This program, aufs is free software; you can redistribute it and/or modify
18877 + * it under the terms of the GNU General Public License as published by
18878 + * the Free Software Foundation; either version 2 of the License, or
18879 + * (at your option) any later version.
18880 + */
18881 +
18882 +/*
18883 + * simple list protected by a spinlock
18884 + */
18885 +
18886 +#ifndef __AUFS_SPL_H__
18887 +#define __AUFS_SPL_H__
18888 +
18889 +#ifdef __KERNEL__
18890 +
18891 +#include <linux/fs.h>
18892 +
18893 +struct au_splhead {
18894 +       spinlock_t              spin;
18895 +       struct list_head        head;
18896 +};
18897 +
18898 +static inline void au_spl_init(struct au_splhead *spl)
18899 +{
18900 +       spin_lock_init(&spl->spin);
18901 +       INIT_LIST_HEAD(&spl->head);
18902 +}
18903 +
18904 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
18905 +{
18906 +       spin_lock(&spl->spin);
18907 +       list_add(list, &spl->head);
18908 +       spin_unlock(&spl->spin);
18909 +}
18910 +
18911 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
18912 +{
18913 +       spin_lock(&spl->spin);
18914 +       list_del(list);
18915 +       spin_unlock(&spl->spin);
18916 +}
18917 +
18918 +#endif /* __KERNEL__ */
18919 +#endif /* __AUFS_SPL_H__ */
18920 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
18921 new file mode 100644
18922 index 0000000..0f6ad13
18923 --- /dev/null
18924 +++ b/fs/aufs/super.c
18925 @@ -0,0 +1,859 @@
18926 +/*
18927 + * Copyright (C) 2005-2009 Junjiro R. Okajima
18928 + *
18929 + * This program, aufs is free software; you can redistribute it and/or modify
18930 + * it under the terms of the GNU General Public License as published by
18931 + * the Free Software Foundation; either version 2 of the License, or
18932 + * (at your option) any later version.
18933 + */
18934 +
18935 +/*
18936 + * mount and super_block operations
18937 + */
18938 +
18939 +#include <linux/buffer_head.h>
18940 +#include <linux/seq_file.h>
18941 +#include <linux/statfs.h>
18942 +#include "aufs.h"
18943 +
18944 +/*
18945 + * super_operations
18946 + */
18947 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
18948 +{
18949 +       struct au_icntnr *c;
18950 +
18951 +       c = au_cache_alloc_icntnr();
18952 +       if (c) {
18953 +               inode_init_once(&c->vfs_inode);
18954 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
18955 +               c->iinfo.ii_hinode = NULL;
18956 +               return &c->vfs_inode;
18957 +       }
18958 +       return NULL;
18959 +}
18960 +
18961 +static void aufs_destroy_inode(struct inode *inode)
18962 +{
18963 +       au_iinfo_fin(inode);
18964 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
18965 +}
18966 +
18967 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
18968 +{
18969 +       struct inode *inode;
18970 +       int err;
18971 +
18972 +       inode = iget_locked(sb, ino);
18973 +       if (unlikely(!inode)) {
18974 +               inode = ERR_PTR(-ENOMEM);
18975 +               goto out;
18976 +       }
18977 +       if (!(inode->i_state & I_NEW))
18978 +               goto out;
18979 +
18980 +       err = au_xigen_new(inode);
18981 +       if (!err)
18982 +               err = au_iinfo_init(inode);
18983 +       if (!err)
18984 +               inode->i_version++;
18985 +       else {
18986 +               iget_failed(inode);
18987 +               inode = ERR_PTR(err);
18988 +       }
18989 +
18990 + out:
18991 +       /* never return NULL */
18992 +       AuDebugOn(!inode);
18993 +       AuTraceErrPtr(inode);
18994 +       return inode;
18995 +}
18996 +
18997 +/* lock free root dinfo */
18998 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
18999 +{
19000 +       int err;
19001 +       aufs_bindex_t bindex, bend;
19002 +       struct path path;
19003 +       struct au_hdentry *hd;
19004 +       struct au_branch *br;
19005 +
19006 +       err = 0;
19007 +       bend = au_sbend(sb);
19008 +       hd = au_di(sb->s_root)->di_hdentry;
19009 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
19010 +               br = au_sbr(sb, bindex);
19011 +               path.mnt = br->br_mnt;
19012 +               path.dentry = hd[bindex].hd_dentry;
19013 +               err = au_seq_path(seq, &path);
19014 +               if (err > 0)
19015 +                       err = seq_printf(seq, "=%s",
19016 +                                        au_optstr_br_perm(br->br_perm));
19017 +               if (!err && bindex != bend)
19018 +                       err = seq_putc(seq, ':');
19019 +       }
19020 +
19021 +       return err;
19022 +}
19023 +
19024 +static void au_show_wbr_create(struct seq_file *m, int v,
19025 +                              struct au_sbinfo *sbinfo)
19026 +{
19027 +       const char *pat;
19028 +
19029 +       seq_printf(m, ",create=");
19030 +       pat = au_optstr_wbr_create(v);
19031 +       switch (v) {
19032 +       case AuWbrCreate_TDP:
19033 +       case AuWbrCreate_RR:
19034 +       case AuWbrCreate_MFS:
19035 +       case AuWbrCreate_PMFS:
19036 +               seq_printf(m, pat);
19037 +               break;
19038 +       case AuWbrCreate_MFSV:
19039 +               seq_printf(m, /*pat*/"mfs:%lu",
19040 +                          sbinfo->si_wbr_mfs.mfs_expire / HZ);
19041 +               break;
19042 +       case AuWbrCreate_PMFSV:
19043 +               seq_printf(m, /*pat*/"pmfs:%lu",
19044 +                          sbinfo->si_wbr_mfs.mfs_expire / HZ);
19045 +               break;
19046 +       case AuWbrCreate_MFSRR:
19047 +               seq_printf(m, /*pat*/"mfsrr:%llu",
19048 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
19049 +               break;
19050 +       case AuWbrCreate_MFSRRV:
19051 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
19052 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
19053 +                          sbinfo->si_wbr_mfs.mfs_expire / HZ);
19054 +               break;
19055 +       }
19056 +}
19057 +
19058 +static int au_show_xino(struct seq_file *seq, struct vfsmount *mnt)
19059 +{
19060 +#ifdef CONFIG_SYSFS
19061 +       return 0;
19062 +#else
19063 +       int err;
19064 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
19065 +       aufs_bindex_t bindex, brid;
19066 +       struct super_block *sb;
19067 +       struct qstr *name;
19068 +       struct file *f;
19069 +       struct dentry *d, *h_root;
19070 +
19071 +       err = 0;
19072 +       sb = mnt->mnt_sb;
19073 +       f = au_sbi(sb)->si_xib;
19074 +       if (!f)
19075 +               goto out;
19076 +
19077 +       /* stop printing the default xino path on the first writable branch */
19078 +       h_root = NULL;
19079 +       brid = au_xino_brid(sb);
19080 +       if (brid >= 0) {
19081 +               bindex = au_br_index(sb, brid);
19082 +               h_root = au_di(sb->s_root)->di_hdentry[0 + bindex].hd_dentry;
19083 +       }
19084 +       d = f->f_dentry;
19085 +       name = &d->d_name;
19086 +       /* safe ->d_parent because the file is unlinked */
19087 +       if (d->d_parent == h_root
19088 +           && name->len == len
19089 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
19090 +               goto out;
19091 +
19092 +       seq_puts(seq, ",xino=");
19093 +       err = au_xino_path(seq, f);
19094 +
19095 + out:
19096 +       return err;
19097 +#endif
19098 +}
19099 +
19100 +/* seq_file will re-call me in case of too long string */
19101 +static int aufs_show_options(struct seq_file *m, struct vfsmount *mnt)
19102 +{
19103 +       int err, n;
19104 +       unsigned int mnt_flags, v;
19105 +       struct super_block *sb;
19106 +       struct au_sbinfo *sbinfo;
19107 +
19108 +#define AuBool(name, str) do { \
19109 +       v = au_opt_test(mnt_flags, name); \
19110 +       if (v != au_opt_test(AuOpt_Def, name)) \
19111 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
19112 +} while (0)
19113 +
19114 +#define AuStr(name, str) do { \
19115 +       v = mnt_flags & AuOptMask_##name; \
19116 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
19117 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
19118 +} while (0)
19119 +
19120 +#define AuUInt(name, str, val) do { \
19121 +       if (val != AUFS_##name##_DEF) \
19122 +               seq_printf(m, "," #str "=%u", val); \
19123 +} while (0)
19124 +
19125 +       /* lock free root dinfo */
19126 +       sb = mnt->mnt_sb;
19127 +       si_noflush_read_lock(sb);
19128 +       sbinfo = au_sbi(sb);
19129 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
19130 +
19131 +       mnt_flags = au_mntflags(sb);
19132 +       if (au_opt_test(mnt_flags, XINO)) {
19133 +               err = au_show_xino(m, mnt);
19134 +               if (unlikely(err))
19135 +                       goto out;
19136 +       } else
19137 +               seq_puts(m, ",noxino");
19138 +
19139 +       AuBool(TRUNC_XINO, trunc_xino);
19140 +       AuStr(UDBA, udba);
19141 +       AuBool(PLINK, plink);
19142 +       /* AuBool(DIRPERM1, dirperm1); */
19143 +       /* AuBool(REFROF, refrof); */
19144 +
19145 +       v = sbinfo->si_wbr_create;
19146 +       if (v != AuWbrCreate_Def)
19147 +               au_show_wbr_create(m, v, sbinfo);
19148 +
19149 +       v = sbinfo->si_wbr_copyup;
19150 +       if (v != AuWbrCopyup_Def)
19151 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
19152 +
19153 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
19154 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
19155 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
19156 +
19157 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
19158 +
19159 +       n = sbinfo->si_rdcache / HZ;
19160 +       AuUInt(RDCACHE, rdcache, n);
19161 +
19162 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
19163 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
19164 +
19165 +       AuBool(SUM, sum);
19166 +       /* AuBool(SUM_W, wsum); */
19167 +       AuBool(WARN_PERM, warn_perm);
19168 +       AuBool(VERBOSE, verbose);
19169 +
19170 + out:
19171 +       /* be sure to print "br:" last */
19172 +       if (!sysaufs_brs) {
19173 +               seq_puts(m, ",br:");
19174 +               au_show_brs(m, sb);
19175 +       }
19176 +       si_read_unlock(sb);
19177 +       return 0;
19178 +
19179 +#undef Deleted
19180 +#undef AuBool
19181 +#undef AuStr
19182 +}
19183 +
19184 +/* ---------------------------------------------------------------------- */
19185 +
19186 +/* sum mode which returns the summation for statfs(2) */
19187 +
19188 +static u64 au_add_till_max(u64 a, u64 b)
19189 +{
19190 +       u64 old;
19191 +
19192 +       old = a;
19193 +       a += b;
19194 +       if (old < a)
19195 +               return a;
19196 +       return ULLONG_MAX;
19197 +}
19198 +
19199 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
19200 +{
19201 +       int err;
19202 +       u64 blocks, bfree, bavail, files, ffree;
19203 +       aufs_bindex_t bend, bindex, i;
19204 +       unsigned char shared;
19205 +       struct vfsmount *h_mnt;
19206 +       struct super_block *h_sb;
19207 +
19208 +       blocks = 0;
19209 +       bfree = 0;
19210 +       bavail = 0;
19211 +       files = 0;
19212 +       ffree = 0;
19213 +
19214 +       err = 0;
19215 +       bend = au_sbend(sb);
19216 +       for (bindex = bend; bindex >= 0; bindex--) {
19217 +               h_mnt = au_sbr_mnt(sb, bindex);
19218 +               h_sb = h_mnt->mnt_sb;
19219 +               shared = 0;
19220 +               for (i = bindex + 1; !shared && i <= bend; i++)
19221 +                       shared = (au_sbr_sb(sb, i) == h_sb);
19222 +               if (shared)
19223 +                       continue;
19224 +
19225 +               /* sb->s_root for NFS is unreliable */
19226 +               err = vfs_statfs(h_mnt->mnt_root, buf);
19227 +               if (unlikely(err))
19228 +                       goto out;
19229 +
19230 +               blocks = au_add_till_max(blocks, buf->f_blocks);
19231 +               bfree = au_add_till_max(bfree, buf->f_bfree);
19232 +               bavail = au_add_till_max(bavail, buf->f_bavail);
19233 +               files = au_add_till_max(files, buf->f_files);
19234 +               ffree = au_add_till_max(ffree, buf->f_ffree);
19235 +       }
19236 +
19237 +       buf->f_blocks = blocks;
19238 +       buf->f_bfree = bfree;
19239 +       buf->f_bavail = bavail;
19240 +       buf->f_files = files;
19241 +       buf->f_ffree = ffree;
19242 +
19243 + out:
19244 +       return err;
19245 +}
19246 +
19247 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
19248 +{
19249 +       int err;
19250 +       struct super_block *sb;
19251 +
19252 +       /* lock free root dinfo */
19253 +       sb = dentry->d_sb;
19254 +       si_noflush_read_lock(sb);
19255 +       if (!au_opt_test(au_mntflags(sb), SUM))
19256 +               /* sb->s_root for NFS is unreliable */
19257 +               err = vfs_statfs(au_sbr_mnt(sb, 0)->mnt_root, buf);
19258 +       else
19259 +               err = au_statfs_sum(sb, buf);
19260 +       si_read_unlock(sb);
19261 +
19262 +       if (!err) {
19263 +               buf->f_type = AUFS_SUPER_MAGIC;
19264 +               buf->f_namelen -= AUFS_WH_PFX_LEN;
19265 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
19266 +       }
19267 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
19268 +
19269 +       return err;
19270 +}
19271 +
19272 +/* ---------------------------------------------------------------------- */
19273 +
19274 +/* try flushing the lower fs at aufs remount/unmount time */
19275 +
19276 +static void au_fsync_br(struct super_block *sb)
19277 +{
19278 +       aufs_bindex_t bend, bindex;
19279 +       int brperm;
19280 +       struct au_branch *br;
19281 +       struct super_block *h_sb;
19282 +
19283 +       bend = au_sbend(sb);
19284 +       for (bindex = 0; bindex < bend; bindex++) {
19285 +               br = au_sbr(sb, bindex);
19286 +               brperm = br->br_perm;
19287 +               if (brperm == AuBrPerm_RR || brperm == AuBrPerm_RRWH)
19288 +                       continue;
19289 +               h_sb = br->br_mnt->mnt_sb;
19290 +               if (bdev_read_only(h_sb->s_bdev))
19291 +                       continue;
19292 +
19293 +               lockdep_off();
19294 +               down_write(&h_sb->s_umount);
19295 +               shrink_dcache_sb(h_sb);
19296 +               fsync_super(h_sb);
19297 +               up_write(&h_sb->s_umount);
19298 +               lockdep_on();
19299 +       }
19300 +}
19301 +
19302 +/*
19303 + * this IS NOT for super_operations.
19304 + * I guess it will be reverted someday.
19305 + */
19306 +static void aufs_umount_begin(struct super_block *sb)
19307 +{
19308 +       struct au_sbinfo *sbinfo;
19309 +
19310 +       sbinfo = au_sbi(sb);
19311 +       if (!sbinfo)
19312 +               return;
19313 +
19314 +       si_write_lock(sb);
19315 +       au_fsync_br(sb);
19316 +       if (au_opt_test(au_mntflags(sb), PLINK))
19317 +               au_plink_put(sb);
19318 +       if (sbinfo->si_wbr_create_ops->fin)
19319 +               sbinfo->si_wbr_create_ops->fin(sb);
19320 +       si_write_unlock(sb);
19321 +}
19322 +
19323 +/* final actions when unmounting a file system */
19324 +static void aufs_put_super(struct super_block *sb)
19325 +{
19326 +       struct au_sbinfo *sbinfo;
19327 +
19328 +       sbinfo = au_sbi(sb);
19329 +       if (!sbinfo)
19330 +               return;
19331 +
19332 +       aufs_umount_begin(sb);
19333 +       dbgaufs_si_fin(sbinfo);
19334 +       kobject_put(&sbinfo->si_kobj);
19335 +}
19336 +
19337 +/* ---------------------------------------------------------------------- */
19338 +
19339 +/*
19340 + * refresh dentry and inode at remount time.
19341 + */
19342 +static int do_refresh(struct dentry *dentry, mode_t type,
19343 +                     unsigned int dir_flags)
19344 +{
19345 +       int err;
19346 +       struct dentry *parent;
19347 +
19348 +       di_write_lock_child(dentry);
19349 +       parent = dget_parent(dentry);
19350 +       di_read_lock_parent(parent, AuLock_IR);
19351 +
19352 +       /* returns the number of positive dentries */
19353 +       err = au_refresh_hdentry(dentry, type);
19354 +       if (err >= 0) {
19355 +               struct inode *inode = dentry->d_inode;
19356 +               err = au_refresh_hinode(inode, dentry);
19357 +               if (!err && type == S_IFDIR)
19358 +                       au_reset_hinotify(inode, dir_flags);
19359 +       }
19360 +       if (unlikely(err))
19361 +               AuErr("unrecoverable error %d, %.*s\n", err, AuDLNPair(dentry));
19362 +
19363 +       di_read_unlock(parent, AuLock_IR);
19364 +       dput(parent);
19365 +       di_write_unlock(dentry);
19366 +
19367 +       return err;
19368 +}
19369 +
19370 +static int test_dir(struct dentry *dentry, void *arg __maybe_unused)
19371 +{
19372 +       return S_ISDIR(dentry->d_inode->i_mode);
19373 +}
19374 +
19375 +/* gave up consolidating with refresh_nondir() */
19376 +static int refresh_dir(struct dentry *root, unsigned int sigen)
19377 +{
19378 +       int err, i, j, ndentry, e;
19379 +       struct au_dcsub_pages dpages;
19380 +       struct au_dpage *dpage;
19381 +       struct dentry **dentries;
19382 +       struct inode *inode;
19383 +       const unsigned int flags = au_hi_flags(root->d_inode, /*isdir*/1);
19384 +
19385 +       err = 0;
19386 +       list_for_each_entry(inode, &root->d_sb->s_inodes, i_sb_list)
19387 +               if (S_ISDIR(inode->i_mode) && au_iigen(inode) != sigen) {
19388 +                       ii_write_lock_child(inode);
19389 +                       e = au_refresh_hinode_self(inode, /*do_attr*/1);
19390 +                       ii_write_unlock(inode);
19391 +                       if (unlikely(e)) {
19392 +                               AuDbg("e %d, i%lu\n", e, inode->i_ino);
19393 +                               if (!err)
19394 +                                       err = e;
19395 +                               /* go on even if err */
19396 +                       }
19397 +               }
19398 +
19399 +       e = au_dpages_init(&dpages, GFP_NOFS);
19400 +       if (unlikely(e)) {
19401 +               if (!err)
19402 +                       err = e;
19403 +               goto out;
19404 +       }
19405 +       e = au_dcsub_pages(&dpages, root, test_dir, NULL);
19406 +       if (unlikely(e)) {
19407 +               if (!err)
19408 +                       err = e;
19409 +               goto out_dpages;
19410 +       }
19411 +
19412 +       for (i = 0; !e && i < dpages.ndpage; i++) {
19413 +               dpage = dpages.dpages + i;
19414 +               dentries = dpage->dentries;
19415 +               ndentry = dpage->ndentry;
19416 +               for (j = 0; !e && j < ndentry; j++) {
19417 +                       struct dentry *d;
19418 +
19419 +                       d = dentries[j];
19420 +                       au_dbg_verify_dir_parent(d, sigen);
19421 +                       if (au_digen(d) != sigen) {
19422 +                               e = do_refresh(d, S_IFDIR, flags);
19423 +                               if (unlikely(e && !err))
19424 +                                       err = e;
19425 +                               /* break on err */
19426 +                       }
19427 +               }
19428 +       }
19429 +
19430 + out_dpages:
19431 +       au_dpages_free(&dpages);
19432 + out:
19433 +       return err;
19434 +}
19435 +
19436 +static int test_nondir(struct dentry *dentry, void *arg __maybe_unused)
19437 +{
19438 +       return !S_ISDIR(dentry->d_inode->i_mode);
19439 +}
19440 +
19441 +static int refresh_nondir(struct dentry *root, unsigned int sigen,
19442 +                         int do_dentry)
19443 +{
19444 +       int err, i, j, ndentry, e;
19445 +       struct au_dcsub_pages dpages;
19446 +       struct au_dpage *dpage;
19447 +       struct dentry **dentries;
19448 +       struct inode *inode;
19449 +
19450 +       err = 0;
19451 +       list_for_each_entry(inode, &root->d_sb->s_inodes, i_sb_list)
19452 +               if (!S_ISDIR(inode->i_mode) && au_iigen(inode) != sigen) {
19453 +                       ii_write_lock_child(inode);
19454 +                       e = au_refresh_hinode_self(inode, /*do_attr*/1);
19455 +                       ii_write_unlock(inode);
19456 +                       if (unlikely(e)) {
19457 +                               AuDbg("e %d, i%lu\n", e, inode->i_ino);
19458 +                               if (!err)
19459 +                                       err = e;
19460 +                               /* go on even if err */
19461 +                       }
19462 +               }
19463 +
19464 +       if (!do_dentry)
19465 +               goto out;
19466 +
19467 +       e = au_dpages_init(&dpages, GFP_NOFS);
19468 +       if (unlikely(e)) {
19469 +               if (!err)
19470 +                       err = e;
19471 +               goto out;
19472 +       }
19473 +       e = au_dcsub_pages(&dpages, root, test_nondir, NULL);
19474 +       if (unlikely(e)) {
19475 +               if (!err)
19476 +                       err = e;
19477 +               goto out_dpages;
19478 +       }
19479 +
19480 +       for (i = 0; i < dpages.ndpage; i++) {
19481 +               dpage = dpages.dpages + i;
19482 +               dentries = dpage->dentries;
19483 +               ndentry = dpage->ndentry;
19484 +               for (j = 0; j < ndentry; j++) {
19485 +                       struct dentry *d;
19486 +
19487 +                       d = dentries[j];
19488 +                       au_dbg_verify_nondir_parent(d, sigen);
19489 +                       inode = d->d_inode;
19490 +                       if (inode && au_digen(d) != sigen) {
19491 +                               e = do_refresh(d, inode->i_mode & S_IFMT,
19492 +                                              /*dir_flags*/0);
19493 +                               if (unlikely(e && !err))
19494 +                                       err = e;
19495 +                               /* go on even err */
19496 +                       }
19497 +               }
19498 +       }
19499 +
19500 + out_dpages:
19501 +       au_dpages_free(&dpages);
19502 + out:
19503 +       return err;
19504 +}
19505 +
19506 +static void au_remount_refresh(struct super_block *sb, unsigned int flags)
19507 +{
19508 +       int err;
19509 +       unsigned int sigen;
19510 +       struct au_sbinfo *sbinfo;
19511 +       struct dentry *root;
19512 +       struct inode *inode;
19513 +
19514 +       au_sigen_inc(sb);
19515 +       sigen = au_sigen(sb);
19516 +       sbinfo = au_sbi(sb);
19517 +       au_fclr_si(sbinfo, FAILED_REFRESH_DIRS);
19518 +
19519 +       root = sb->s_root;
19520 +       DiMustNoWaiters(root);
19521 +       inode = root->d_inode;
19522 +       IiMustNoWaiters(inode);
19523 +       au_reset_hinotify(inode, au_hi_flags(inode, /*isdir*/1));
19524 +       di_write_unlock(root);
19525 +
19526 +       err = refresh_dir(root, sigen);
19527 +       if (unlikely(err)) {
19528 +               au_fset_si(sbinfo, FAILED_REFRESH_DIRS);
19529 +               AuWarn("Refreshing directories failed, ignored (%d)\n", err);
19530 +       }
19531 +
19532 +       if (au_ftest_opts(flags, REFRESH_NONDIR)) {
19533 +               err = refresh_nondir(root, sigen, !err);
19534 +               if (unlikely(err))
19535 +                       AuWarn("Refreshing non-directories failed, ignored"
19536 +                              "(%d)\n", err);
19537 +       }
19538 +
19539 +       /* aufs_write_lock() calls ..._child() */
19540 +       di_write_lock_child(root);
19541 +       au_cpup_attr_all(root->d_inode, /*force*/1);
19542 +}
19543 +
19544 +/* stop extra interpretation of errno in mount(8), and strange error messages */
19545 +static int cvt_err(int err)
19546 +{
19547 +       AuTraceErr(err);
19548 +
19549 +       switch (err) {
19550 +       case -ENOENT:
19551 +       case -ENOTDIR:
19552 +       case -EEXIST:
19553 +       case -EIO:
19554 +               err = -EINVAL;
19555 +       }
19556 +       return err;
19557 +}
19558 +
19559 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
19560 +{
19561 +       int err;
19562 +       struct au_opts opts;
19563 +       struct dentry *root;
19564 +       struct inode *inode;
19565 +       struct au_sbinfo *sbinfo;
19566 +
19567 +       err = 0;
19568 +       root = sb->s_root;
19569 +       if (!data || !*data) {
19570 +               aufs_write_lock(root);
19571 +               err = au_opts_verify(sb, *flags, /*pending*/0);
19572 +               if (!err)
19573 +                       au_fsync_br(sb);
19574 +               aufs_write_unlock(root);
19575 +               goto out;
19576 +       }
19577 +
19578 +       err = -ENOMEM;
19579 +       memset(&opts, 0, sizeof(opts));
19580 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
19581 +       if (unlikely(!opts.opt))
19582 +               goto out;
19583 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
19584 +       opts.flags = AuOpts_REMOUNT;
19585 +       opts.sb_flags = *flags;
19586 +
19587 +       /* parse it before aufs lock */
19588 +       err = au_opts_parse(sb, data, &opts);
19589 +       if (unlikely(err))
19590 +               goto out_opts;
19591 +
19592 +       sbinfo = au_sbi(sb);
19593 +       inode = root->d_inode;
19594 +       mutex_lock(&inode->i_mutex);
19595 +       aufs_write_lock(root);
19596 +       au_fsync_br(sb);
19597 +
19598 +       /* au_opts_remount() may return an error */
19599 +       err = au_opts_remount(sb, &opts);
19600 +       au_opts_free(&opts);
19601 +
19602 +       if (au_ftest_opts(opts.flags, REFRESH_DIR)
19603 +           || au_ftest_opts(opts.flags, REFRESH_NONDIR))
19604 +               au_remount_refresh(sb, opts.flags);
19605 +
19606 +       aufs_write_unlock(root);
19607 +       mutex_unlock(&inode->i_mutex);
19608 +
19609 + out_opts:
19610 +       free_page((unsigned long)opts.opt);
19611 + out:
19612 +       err = cvt_err(err);
19613 +       AuTraceErr(err);
19614 +       return err;
19615 +}
19616 +
19617 +static struct super_operations aufs_sop = {
19618 +       .alloc_inode    = aufs_alloc_inode,
19619 +       .destroy_inode  = aufs_destroy_inode,
19620 +       .drop_inode     = generic_delete_inode,
19621 +       .show_options   = aufs_show_options,
19622 +       .statfs         = aufs_statfs,
19623 +       .put_super      = aufs_put_super,
19624 +       .remount_fs     = aufs_remount_fs
19625 +};
19626 +
19627 +/* ---------------------------------------------------------------------- */
19628 +
19629 +static int alloc_root(struct super_block *sb)
19630 +{
19631 +       int err;
19632 +       struct inode *inode;
19633 +       struct dentry *root;
19634 +
19635 +       err = -ENOMEM;
19636 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
19637 +       err = PTR_ERR(inode);
19638 +       if (IS_ERR(inode))
19639 +               goto out;
19640 +
19641 +       inode->i_op = &aufs_dir_iop;
19642 +       inode->i_fop = &aufs_dir_fop;
19643 +       inode->i_mode = S_IFDIR;
19644 +       inode->i_nlink = 2;
19645 +       unlock_new_inode(inode);
19646 +
19647 +       root = d_alloc_root(inode);
19648 +       if (unlikely(!root))
19649 +               goto out_iput;
19650 +       err = PTR_ERR(root);
19651 +       if (IS_ERR(root))
19652 +               goto out_iput;
19653 +
19654 +       err = au_alloc_dinfo(root);
19655 +       if (!err) {
19656 +               sb->s_root = root;
19657 +               return 0; /* success */
19658 +       }
19659 +       dput(root);
19660 +       goto out; /* do not iput */
19661 +
19662 + out_iput:
19663 +       iget_failed(inode);
19664 +       iput(inode);
19665 + out:
19666 +       return err;
19667 +
19668 +}
19669 +
19670 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
19671 +                          int silent __maybe_unused)
19672 +{
19673 +       int err;
19674 +       struct au_opts opts;
19675 +       struct dentry *root;
19676 +       struct inode *inode;
19677 +       char *arg = raw_data;
19678 +
19679 +       if (unlikely(!arg || !*arg)) {
19680 +               err = -EINVAL;
19681 +               AuErr("no arg\n");
19682 +               goto out;
19683 +       }
19684 +
19685 +       err = -ENOMEM;
19686 +       memset(&opts, 0, sizeof(opts));
19687 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
19688 +       if (unlikely(!opts.opt))
19689 +               goto out;
19690 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
19691 +       opts.sb_flags = sb->s_flags;
19692 +
19693 +       err = au_si_alloc(sb);
19694 +       if (unlikely(err))
19695 +               goto out_opts;
19696 +
19697 +       /* all timestamps always follow the ones on the branch */
19698 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
19699 +       sb->s_op = &aufs_sop;
19700 +       sb->s_magic = AUFS_SUPER_MAGIC;
19701 +       sb->s_maxbytes = 0;
19702 +       au_export_init(sb);
19703 +
19704 +       err = alloc_root(sb);
19705 +       if (unlikely(err)) {
19706 +               si_write_unlock(sb);
19707 +               goto out_info;
19708 +       }
19709 +       root = sb->s_root;
19710 +       inode = root->d_inode;
19711 +
19712 +       /*
19713 +        * actually we can parse options regardless aufs lock here.
19714 +        * but at remount time, parsing must be done before aufs lock.
19715 +        * so we follow the same rule.
19716 +        */
19717 +       ii_write_lock_parent(inode);
19718 +       aufs_write_unlock(root);
19719 +       err = au_opts_parse(sb, arg, &opts);
19720 +       if (unlikely(err))
19721 +               goto out_root;
19722 +
19723 +       /* lock vfs_inode first, then aufs. */
19724 +       mutex_lock(&inode->i_mutex);
19725 +       inode->i_op = &aufs_dir_iop;
19726 +       inode->i_fop = &aufs_dir_fop;
19727 +       aufs_write_lock(root);
19728 +       err = au_opts_mount(sb, &opts);
19729 +       au_opts_free(&opts);
19730 +       if (unlikely(err))
19731 +               goto out_unlock;
19732 +       aufs_write_unlock(root);
19733 +       mutex_unlock(&inode->i_mutex);
19734 +       goto out_opts; /* success */
19735 +
19736 + out_unlock:
19737 +       aufs_write_unlock(root);
19738 +       mutex_unlock(&inode->i_mutex);
19739 + out_root:
19740 +       dput(root);
19741 +       sb->s_root = NULL;
19742 + out_info:
19743 +       kobject_put(&au_sbi(sb)->si_kobj);
19744 +       sb->s_fs_info = NULL;
19745 + out_opts:
19746 +       free_page((unsigned long)opts.opt);
19747 + out:
19748 +       AuTraceErr(err);
19749 +       err = cvt_err(err);
19750 +       AuTraceErr(err);
19751 +       return err;
19752 +}
19753 +
19754 +/* ---------------------------------------------------------------------- */
19755 +
19756 +static int aufs_get_sb(struct file_system_type *fs_type, int flags,
19757 +                      const char *dev_name __maybe_unused, void *raw_data,
19758 +                      struct vfsmount *mnt)
19759 +{
19760 +       int err;
19761 +       struct super_block *sb;
19762 +
19763 +       /* all timestamps always follow the ones on the branch */
19764 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
19765 +       err = get_sb_nodev(fs_type, flags, raw_data, aufs_fill_super, mnt);
19766 +       if (!err) {
19767 +               sb = mnt->mnt_sb;
19768 +               si_write_lock(sb);
19769 +               sysaufs_brs_add(sb, 0);
19770 +               si_write_unlock(sb);
19771 +       }
19772 +       return err;
19773 +}
19774 +
19775 +struct file_system_type aufs_fs_type = {
19776 +       .name           = AUFS_FSTYPE,
19777 +       .fs_flags       =
19778 +               FS_RENAME_DOES_D_MOVE   /* a race between rename and others */
19779 +               | FS_REVAL_DOT,         /* for NFS branch and udba */
19780 +       .get_sb         = aufs_get_sb,
19781 +       .kill_sb        = generic_shutdown_super,
19782 +       /* no need to __module_get() and module_put(). */
19783 +       .owner          = THIS_MODULE,
19784 +};
19785 diff --git a/fs/aufs/super.h b/fs/aufs/super.h
19786 new file mode 100644
19787 index 0000000..9897dba
19788 --- /dev/null
19789 +++ b/fs/aufs/super.h
19790 @@ -0,0 +1,351 @@
19791 +/*
19792 + * Copyright (C) 2005-2009 Junjiro R. Okajima
19793 + *
19794 + * This program, aufs is free software; you can redistribute it and/or modify
19795 + * it under the terms of the GNU General Public License as published by
19796 + * the Free Software Foundation; either version 2 of the License, or
19797 + * (at your option) any later version.
19798 + */
19799 +
19800 +/*
19801 + * super_block operations
19802 + */
19803 +
19804 +#ifndef __AUFS_SUPER_H__
19805 +#define __AUFS_SUPER_H__
19806 +
19807 +#ifdef __KERNEL__
19808 +
19809 +#include <linux/fs.h>
19810 +#include <linux/kobject.h>
19811 +#include <linux/aufs_type.h>
19812 +#include "rwsem.h"
19813 +#include "spl.h"
19814 +#include "wkq.h"
19815 +
19816 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
19817 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
19818 +                              loff_t *);
19819 +
19820 +/* policies to select one among multiple writable branches */
19821 +struct au_wbr_copyup_operations {
19822 +       int (*copyup)(struct dentry *dentry);
19823 +};
19824 +
19825 +struct au_wbr_create_operations {
19826 +       int (*create)(struct dentry *dentry, int isdir);
19827 +       int (*init)(struct super_block *sb);
19828 +       int (*fin)(struct super_block *sb);
19829 +};
19830 +
19831 +struct au_wbr_mfs {
19832 +       struct mutex    mfs_lock; /* protect this structure */
19833 +       unsigned long   mfs_jiffy;
19834 +       unsigned long   mfs_expire;
19835 +       aufs_bindex_t   mfs_bindex;
19836 +
19837 +       unsigned long long      mfsrr_bytes;
19838 +       unsigned long long      mfsrr_watermark;
19839 +};
19840 +
19841 +/* sbinfo status flags */
19842 +/*
19843 + * set true when refresh_dirs() failed at remount time.
19844 + * then try refreshing dirs at access time again.
19845 + * if it is false, refreshing dirs at access time is unnecesary
19846 + */
19847 +#define AuSi_FAILED_REFRESH_DIRS       1
19848 +#define AuSi_MAINTAIN_PLINK            (1 << 1)        /* ioctl */
19849 +#define au_ftest_si(sbinfo, name)      ((sbinfo)->au_si_status & AuSi_##name)
19850 +#define au_fset_si(sbinfo, name) \
19851 +       { (sbinfo)->au_si_status |= AuSi_##name; }
19852 +#define au_fclr_si(sbinfo, name) \
19853 +       { (sbinfo)->au_si_status &= ~AuSi_##name; }
19854 +
19855 +struct au_branch;
19856 +struct au_sbinfo {
19857 +       /* nowait tasks in the system-wide workqueue */
19858 +       struct au_nowait_tasks  si_nowait;
19859 +
19860 +       struct rw_semaphore     si_rwsem;
19861 +
19862 +       /* branch management */
19863 +       unsigned int            si_generation;
19864 +
19865 +       /* see above flags */
19866 +       unsigned char           au_si_status;
19867 +
19868 +       aufs_bindex_t           si_bend;
19869 +       aufs_bindex_t           si_last_br_id;
19870 +       struct au_branch        **si_branch;
19871 +
19872 +       /* policy to select a writable branch */
19873 +       unsigned char           si_wbr_copyup;
19874 +       unsigned char           si_wbr_create;
19875 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
19876 +       struct au_wbr_create_operations *si_wbr_create_ops;
19877 +
19878 +       /* round robin */
19879 +       atomic_t                si_wbr_rr_next;
19880 +
19881 +       /* most free space */
19882 +       struct au_wbr_mfs       si_wbr_mfs;
19883 +
19884 +       /* mount flags */
19885 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
19886 +       unsigned int            si_mntflags;
19887 +
19888 +       /* external inode number (bitmap and translation table) */
19889 +       au_readf_t              si_xread;
19890 +       au_writef_t             si_xwrite;
19891 +       struct file             *si_xib;
19892 +       struct mutex            si_xib_mtx; /* protect xib members */
19893 +       unsigned long           *si_xib_buf;
19894 +       unsigned long           si_xib_last_pindex;
19895 +       int                     si_xib_next_bit;
19896 +       aufs_bindex_t           si_xino_brid;
19897 +       /* reserved for future use */
19898 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
19899 +
19900 +#ifdef CONFIG_AUFS_EXPORT
19901 +       /* i_generation */
19902 +       struct file             *si_xigen;
19903 +       atomic_t                si_xigen_next;
19904 +#endif
19905 +
19906 +       /* vdir parameters */
19907 +       unsigned long           si_rdcache;     /* max cache time in HZ */
19908 +       unsigned int            si_rdblk;       /* deblk size */
19909 +       unsigned int            si_rdhash;      /* hash size */
19910 +
19911 +       /*
19912 +        * If the number of whiteouts are larger than si_dirwh, leave all of
19913 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
19914 +        * future fsck.aufs or kernel thread will remove them later.
19915 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
19916 +        */
19917 +       unsigned int            si_dirwh;
19918 +
19919 +       /*
19920 +        * rename(2) a directory with all children.
19921 +        */
19922 +       /* reserved for future use */
19923 +       /* int                  si_rendir; */
19924 +
19925 +       /* pseudo_link list */
19926 +       struct au_splhead       si_plink;
19927 +       wait_queue_head_t       si_plink_wq;
19928 +
19929 +       /*
19930 +        * sysfs and lifetime management.
19931 +        * this is not a small structure and it may be a waste of memory in case
19932 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
19933 +        * but using sysfs is majority.
19934 +        */
19935 +       struct kobject          si_kobj;
19936 +#ifdef CONFIG_DEBUG_FS
19937 +       struct dentry            *si_dbgaufs, *si_dbgaufs_xib;
19938 +#ifdef CONFIG_AUFS_EXPORT
19939 +       struct dentry            *si_dbgaufs_xigen;
19940 +#endif
19941 +#endif
19942 +
19943 +       /* dirty, necessary for unmounting, sysfs and sysrq */
19944 +       struct super_block      *si_sb;
19945 +};
19946 +
19947 +/* ---------------------------------------------------------------------- */
19948 +
19949 +/* policy to select one among writable branches */
19950 +#define AuWbrCopyup(sbinfo, args...) \
19951 +       ((sbinfo)->si_wbr_copyup_ops->copyup(args))
19952 +#define AuWbrCreate(sbinfo, args...) \
19953 +       ((sbinfo)->si_wbr_create_ops->create(args))
19954 +
19955 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
19956 +#define AuLock_DW              1               /* write-lock dentry */
19957 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
19958 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
19959 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
19960 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
19961 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
19962 +#define au_fset_lock(flags, name)      { (flags) |= AuLock_##name; }
19963 +#define au_fclr_lock(flags, name)      { (flags) &= ~AuLock_##name; }
19964 +
19965 +/* ---------------------------------------------------------------------- */
19966 +
19967 +/* super.c */
19968 +extern struct file_system_type aufs_fs_type;
19969 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
19970 +
19971 +/* sbinfo.c */
19972 +void au_si_free(struct kobject *kobj);
19973 +int au_si_alloc(struct super_block *sb);
19974 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
19975 +
19976 +unsigned int au_sigen_inc(struct super_block *sb);
19977 +aufs_bindex_t au_new_br_id(struct super_block *sb);
19978 +
19979 +void aufs_read_lock(struct dentry *dentry, int flags);
19980 +void aufs_read_unlock(struct dentry *dentry, int flags);
19981 +void aufs_write_lock(struct dentry *dentry);
19982 +void aufs_write_unlock(struct dentry *dentry);
19983 +void aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int isdir);
19984 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
19985 +
19986 +/* wbr_policy.c */
19987 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
19988 +extern struct au_wbr_create_operations au_wbr_create_ops[];
19989 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
19990 +
19991 +/* ---------------------------------------------------------------------- */
19992 +
19993 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
19994 +{
19995 +       return sb->s_fs_info;
19996 +}
19997 +
19998 +/* ---------------------------------------------------------------------- */
19999 +
20000 +#ifdef CONFIG_AUFS_EXPORT
20001 +void au_export_init(struct super_block *sb);
20002 +
20003 +static inline int au_test_nfsd(struct task_struct *tsk)
20004 +{
20005 +       return !tsk->mm && !strcmp(tsk->comm, "nfsd");
20006 +}
20007 +
20008 +int au_xigen_inc(struct inode *inode);
20009 +int au_xigen_new(struct inode *inode);
20010 +int au_xigen_set(struct super_block *sb, struct file *base);
20011 +void au_xigen_clr(struct super_block *sb);
20012 +
20013 +static inline int au_busy_or_stale(void)
20014 +{
20015 +       if (!au_test_nfsd(current))
20016 +               return -EBUSY;
20017 +       return -ESTALE;
20018 +}
20019 +#else
20020 +static inline void au_export_init(struct super_block *sb)
20021 +{
20022 +       /* nothing */
20023 +}
20024 +
20025 +static inline int au_test_nfsd(struct task_struct *tsk)
20026 +{
20027 +       return 0;
20028 +}
20029 +
20030 +static inline int au_xigen_inc(struct inode *inode)
20031 +{
20032 +       return 0;
20033 +}
20034 +
20035 +static inline int au_xigen_new(struct inode *inode)
20036 +{
20037 +       return 0;
20038 +}
20039 +
20040 +static inline int au_xigen_set(struct super_block *sb, struct file *base)
20041 +{
20042 +       return 0;
20043 +}
20044 +
20045 +static inline void au_xigen_clr(struct super_block *sb)
20046 +{
20047 +       /* empty */
20048 +}
20049 +
20050 +static inline int au_busy_or_stale(void)
20051 +{
20052 +       return -EBUSY;
20053 +}
20054 +#endif /* CONFIG_AUFS_EXPORT */
20055 +
20056 +/* ---------------------------------------------------------------------- */
20057 +
20058 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
20059 +{
20060 +#ifdef CONFIG_DEBUG_FS
20061 +       sbinfo->si_dbgaufs = NULL;
20062 +       sbinfo->si_dbgaufs_xib = NULL;
20063 +#ifdef CONFIG_AUFS_EXPORT
20064 +       sbinfo->si_dbgaufs_xigen = NULL;
20065 +#endif
20066 +#endif
20067 +}
20068 +
20069 +/* ---------------------------------------------------------------------- */
20070 +
20071 +/* lock superblock. mainly for entry point functions */
20072 +/*
20073 + * si_noflush_read_lock, si_noflush_write_lock,
20074 + * si_read_unlock, si_write_unlock, si_downgrade_lock
20075 + */
20076 +AuSimpleLockRwsemFuncs(si_noflush, struct super_block *sb,
20077 +                      &au_sbi(sb)->si_rwsem);
20078 +AuSimpleUnlockRwsemFuncs(si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
20079 +
20080 +static inline void si_read_lock(struct super_block *sb, int flags)
20081 +{
20082 +       if (au_ftest_lock(flags, FLUSH))
20083 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
20084 +       si_noflush_read_lock(sb);
20085 +}
20086 +
20087 +static inline void si_write_lock(struct super_block *sb)
20088 +{
20089 +       au_nwt_flush(&au_sbi(sb)->si_nowait);
20090 +       si_noflush_write_lock(sb);
20091 +}
20092 +
20093 +static inline int si_read_trylock(struct super_block *sb, int flags)
20094 +{
20095 +       if (au_ftest_lock(flags, FLUSH))
20096 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
20097 +       return si_noflush_read_trylock(sb);
20098 +}
20099 +
20100 +static inline int si_write_trylock(struct super_block *sb, int flags)
20101 +{
20102 +       if (au_ftest_lock(flags, FLUSH))
20103 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
20104 +       return si_noflush_write_trylock(sb);
20105 +}
20106 +
20107 +/* ---------------------------------------------------------------------- */
20108 +
20109 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
20110 +{
20111 +       return au_sbi(sb)->si_bend;
20112 +}
20113 +
20114 +static inline unsigned int au_mntflags(struct super_block *sb)
20115 +{
20116 +       return au_sbi(sb)->si_mntflags;
20117 +}
20118 +
20119 +static inline unsigned int au_sigen(struct super_block *sb)
20120 +{
20121 +       return au_sbi(sb)->si_generation;
20122 +}
20123 +
20124 +static inline struct au_branch *au_sbr(struct super_block *sb,
20125 +                                      aufs_bindex_t bindex)
20126 +{
20127 +       return au_sbi(sb)->si_branch[0 + bindex];
20128 +}
20129 +
20130 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
20131 +{
20132 +       au_sbi(sb)->si_xino_brid = brid;
20133 +}
20134 +
20135 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
20136 +{
20137 +       return au_sbi(sb)->si_xino_brid;
20138 +}
20139 +
20140 +#endif /* __KERNEL__ */
20141 +#endif /* __AUFS_SUPER_H__ */
20142 diff --git a/fs/aufs/sysaufs.c b/fs/aufs/sysaufs.c
20143 new file mode 100644
20144 index 0000000..623f2e6
20145 --- /dev/null
20146 +++ b/fs/aufs/sysaufs.c
20147 @@ -0,0 +1,95 @@
20148 +/*
20149 + * Copyright (C) 2005-2009 Junjiro R. Okajima
20150 + *
20151 + * This program, aufs is free software; you can redistribute it and/or modify
20152 + * it under the terms of the GNU General Public License as published by
20153 + * the Free Software Foundation; either version 2 of the License, or
20154 + * (at your option) any later version.
20155 + */
20156 +
20157 +/*
20158 + * sysfs interface and lifetime management
20159 + * they are necessary regardless sysfs is disabled.
20160 + */
20161 +
20162 +#include <linux/fs.h>
20163 +#include <linux/random.h>
20164 +#include <linux/sysfs.h>
20165 +#include "aufs.h"
20166 +
20167 +unsigned long sysaufs_si_mask;
20168 +struct kset *sysaufs_ket;
20169 +
20170 +#define AuSiAttr(_name) { \
20171 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
20172 +       .show   = sysaufs_si_##_name,                           \
20173 +}
20174 +
20175 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
20176 +struct attribute *sysaufs_si_attrs[] = {
20177 +       &sysaufs_si_attr_xi_path.attr,
20178 +       NULL,
20179 +};
20180 +
20181 +static struct sysfs_ops au_sbi_ops = {
20182 +       .show   = sysaufs_si_show
20183 +};
20184 +
20185 +static struct kobj_type au_sbi_ktype = {
20186 +       .release        = au_si_free,
20187 +       .sysfs_ops      = &au_sbi_ops,
20188 +       .default_attrs  = sysaufs_si_attrs
20189 +};
20190 +
20191 +/* ---------------------------------------------------------------------- */
20192 +
20193 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
20194 +{
20195 +       int err;
20196 +
20197 +       sbinfo->si_kobj.kset = sysaufs_ket;
20198 +       /* cf. sysaufs_name() */
20199 +       err = kobject_init_and_add
20200 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_ket->kobj*/NULL,
20201 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
20202 +
20203 +       dbgaufs_si_null(sbinfo);
20204 +       if (!err) {
20205 +               err = dbgaufs_si_init(sbinfo);
20206 +               if (unlikely(err))
20207 +                       kobject_put(&sbinfo->si_kobj);
20208 +       }
20209 +       return err;
20210 +}
20211 +
20212 +void sysaufs_fin(void)
20213 +{
20214 +       dbgaufs_fin();
20215 +       sysfs_remove_group(&sysaufs_ket->kobj, sysaufs_attr_group);
20216 +       kset_unregister(sysaufs_ket);
20217 +}
20218 +
20219 +int __init sysaufs_init(void)
20220 +{
20221 +       int err;
20222 +
20223 +       do {
20224 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
20225 +       } while (!sysaufs_si_mask);
20226 +
20227 +       sysaufs_ket = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
20228 +       err = PTR_ERR(sysaufs_ket);
20229 +       if (IS_ERR(sysaufs_ket))
20230 +               goto out;
20231 +       err = sysfs_create_group(&sysaufs_ket->kobj, sysaufs_attr_group);
20232 +       if (unlikely(err)) {
20233 +               kset_unregister(sysaufs_ket);
20234 +               goto out;
20235 +       }
20236 +
20237 +       err = dbgaufs_init();
20238 +       if (unlikely(err))
20239 +               sysaufs_fin();
20240 + out:
20241 +       return err;
20242 +}
20243 diff --git a/fs/aufs/sysaufs.h b/fs/aufs/sysaufs.h
20244 new file mode 100644
20245 index 0000000..c1202fa
20246 --- /dev/null
20247 +++ b/fs/aufs/sysaufs.h
20248 @@ -0,0 +1,109 @@
20249 +/*
20250 + * Copyright (C) 2005-2009 Junjiro R. Okajima
20251 + *
20252 + * This program, aufs is free software; you can redistribute it and/or modify
20253 + * it under the terms of the GNU General Public License as published by
20254 + * the Free Software Foundation; either version 2 of the License, or
20255 + * (at your option) any later version.
20256 + */
20257 +
20258 +/*
20259 + * sysfs interface and mount lifetime management
20260 + */
20261 +
20262 +#ifndef __SYSAUFS_H__
20263 +#define __SYSAUFS_H__
20264 +
20265 +#ifdef __KERNEL__
20266 +
20267 +#include <linux/fs.h>
20268 +#include <linux/sysfs.h>
20269 +#include <linux/aufs_type.h>
20270 +#include "module.h"
20271 +
20272 +struct sysaufs_si_attr {
20273 +       struct attribute attr;
20274 +       int (*show)(struct seq_file *seq, struct super_block *sb);
20275 +};
20276 +
20277 +/* ---------------------------------------------------------------------- */
20278 +
20279 +/* sysaufs.c */
20280 +extern unsigned long sysaufs_si_mask;
20281 +extern struct kset *sysaufs_ket;
20282 +extern struct attribute *sysaufs_si_attrs[];
20283 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
20284 +int __init sysaufs_init(void);
20285 +void sysaufs_fin(void);
20286 +
20287 +/* ---------------------------------------------------------------------- */
20288 +
20289 +/* some people doesn't like to show a pointer in kernel */
20290 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
20291 +{
20292 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
20293 +}
20294 +
20295 +#define SysaufsSiNamePrefix    "si_"
20296 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
20297 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
20298 +{
20299 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
20300 +                sysaufs_si_id(sbinfo));
20301 +}
20302 +
20303 +struct au_branch;
20304 +#ifdef CONFIG_SYSFS
20305 +/* sysfs.c */
20306 +extern struct attribute_group *sysaufs_attr_group;
20307 +
20308 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
20309 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
20310 +                        char *buf);
20311 +
20312 +void sysaufs_br_init(struct au_branch *br);
20313 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
20314 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
20315 +
20316 +#define sysaufs_brs_init()     do {} while (0)
20317 +
20318 +#else
20319 +#define sysaufs_attr_group     NULL
20320 +
20321 +static inline
20322 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
20323 +{
20324 +       return 0;
20325 +}
20326 +
20327 +static inline
20328 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
20329 +                        char *buf)
20330 +{
20331 +       return 0;
20332 +}
20333 +
20334 +static inline void sysaufs_br_init(struct au_branch *br)
20335 +{
20336 +       /* empty */
20337 +}
20338 +
20339 +static inline void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
20340 +{
20341 +       /* nothing */
20342 +}
20343 +
20344 +static inline void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
20345 +{
20346 +       /* nothing */
20347 +}
20348 +
20349 +static inline void sysaufs_brs_init(void)
20350 +{
20351 +       sysaufs_brs = 0;
20352 +}
20353 +
20354 +#endif /* CONFIG_SYSFS */
20355 +
20356 +#endif /* __KERNEL__ */
20357 +#endif /* __SYSAUFS_H__ */
20358 diff --git a/fs/aufs/sysfs.c b/fs/aufs/sysfs.c
20359 new file mode 100644
20360 index 0000000..496a2fc
20361 --- /dev/null
20362 +++ b/fs/aufs/sysfs.c
20363 @@ -0,0 +1,198 @@
20364 +/*
20365 + * Copyright (C) 2005-2009 Junjiro R. Okajima
20366 + *
20367 + * This program, aufs is free software; you can redistribute it and/or modify
20368 + * it under the terms of the GNU General Public License as published by
20369 + * the Free Software Foundation; either version 2 of the License, or
20370 + * (at your option) any later version.
20371 + */
20372 +
20373 +/*
20374 + * sysfs interface
20375 + */
20376 +
20377 +#include <linux/fs.h>
20378 +#include <linux/seq_file.h>
20379 +#include <linux/sysfs.h>
20380 +#include "aufs.h"
20381 +
20382 +static struct attribute *au_attr[] = {
20383 +       NULL,   /* need to NULL terminate the list of attributes */
20384 +};
20385 +
20386 +static struct attribute_group sysaufs_attr_group_body = {
20387 +       .attrs = au_attr
20388 +};
20389 +
20390 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
20391 +
20392 +/* ---------------------------------------------------------------------- */
20393 +
20394 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
20395 +{
20396 +       int err;
20397 +
20398 +       err = 0;
20399 +       if (au_opt_test(au_mntflags(sb), XINO)) {
20400 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
20401 +               seq_putc(seq, '\n');
20402 +       }
20403 +       return err;
20404 +}
20405 +
20406 +/*
20407 + * the lifetime of branch is independent from the entry under sysfs.
20408 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
20409 + * unlinked.
20410 + */
20411 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
20412 +                        aufs_bindex_t bindex)
20413 +{
20414 +       struct path path;
20415 +       struct dentry *root;
20416 +       struct au_branch *br;
20417 +
20418 +       AuDbg("b%d\n", bindex);
20419 +
20420 +       root = sb->s_root;
20421 +       di_read_lock_parent(root, !AuLock_IR);
20422 +       br = au_sbr(sb, bindex);
20423 +       path.mnt = br->br_mnt;
20424 +       path.dentry = au_h_dptr(root, bindex);
20425 +       au_seq_path(seq, &path);
20426 +       di_read_unlock(root, !AuLock_IR);
20427 +       seq_printf(seq, "=%s\n", au_optstr_br_perm(br->br_perm));
20428 +       return 0;
20429 +}
20430 +
20431 +/* ---------------------------------------------------------------------- */
20432 +
20433 +static struct seq_file *au_seq(char *p, ssize_t len)
20434 +{
20435 +       struct seq_file *seq;
20436 +
20437 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
20438 +       if (seq) {
20439 +               /* mutex_init(&seq.lock); */
20440 +               seq->buf = p;
20441 +               seq->size = len;
20442 +               return seq; /* success */
20443 +       }
20444 +
20445 +       seq = ERR_PTR(-ENOMEM);
20446 +       return seq;
20447 +}
20448 +
20449 +#define SysaufsBr_PREFIX "br"
20450 +
20451 +/* todo: file size may exceed PAGE_SIZE */
20452 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
20453 +                        char *buf)
20454 +{
20455 +       ssize_t err;
20456 +       long l;
20457 +       aufs_bindex_t bend;
20458 +       struct au_sbinfo *sbinfo;
20459 +       struct super_block *sb;
20460 +       struct seq_file *seq;
20461 +       char *name;
20462 +       struct attribute **cattr;
20463 +
20464 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
20465 +       sb = sbinfo->si_sb;
20466 +       si_noflush_read_lock(sb);
20467 +
20468 +       seq = au_seq(buf, PAGE_SIZE);
20469 +       err = PTR_ERR(seq);
20470 +       if (IS_ERR(seq))
20471 +               goto out;
20472 +
20473 +       name = (void *)attr->name;
20474 +       cattr = sysaufs_si_attrs;
20475 +       while (*cattr) {
20476 +               if (!strcmp(name, (*cattr)->name)) {
20477 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
20478 +                               ->show(seq, sb);
20479 +                       goto out_seq;
20480 +               }
20481 +               cattr++;
20482 +       }
20483 +
20484 +       bend = au_sbend(sb);
20485 +       if (!strncmp(name, SysaufsBr_PREFIX, sizeof(SysaufsBr_PREFIX) - 1)) {
20486 +               name += sizeof(SysaufsBr_PREFIX) - 1;
20487 +               err = strict_strtol(name, 10, &l);
20488 +               if (!err) {
20489 +                       if (l <= bend)
20490 +                               err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l);
20491 +                       else
20492 +                               err = -ENOENT;
20493 +               }
20494 +               goto out_seq;
20495 +       }
20496 +       BUG();
20497 +
20498 + out_seq:
20499 +       if (!err) {
20500 +               err = seq->count;
20501 +               /* sysfs limit */
20502 +               if (unlikely(err == PAGE_SIZE))
20503 +                       err = -EFBIG;
20504 +       }
20505 +       kfree(seq);
20506 + out:
20507 +       si_read_unlock(sb);
20508 +       return err;
20509 +}
20510 +
20511 +/* ---------------------------------------------------------------------- */
20512 +
20513 +void sysaufs_br_init(struct au_branch *br)
20514 +{
20515 +       br->br_attr.name = br->br_name;
20516 +       br->br_attr.mode = S_IRUGO;
20517 +       br->br_attr.owner = THIS_MODULE;
20518 +}
20519 +
20520 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
20521 +{
20522 +       struct au_branch *br;
20523 +       struct kobject *kobj;
20524 +       aufs_bindex_t bend;
20525 +
20526 +       dbgaufs_brs_del(sb, bindex);
20527 +
20528 +       if (!sysaufs_brs)
20529 +               return;
20530 +
20531 +       kobj = &au_sbi(sb)->si_kobj;
20532 +       bend = au_sbend(sb);
20533 +       for (; bindex <= bend; bindex++) {
20534 +               br = au_sbr(sb, bindex);
20535 +               sysfs_remove_file(kobj, &br->br_attr);
20536 +       }
20537 +}
20538 +
20539 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
20540 +{
20541 +       int err;
20542 +       aufs_bindex_t bend;
20543 +       struct kobject *kobj;
20544 +       struct au_branch *br;
20545 +
20546 +       dbgaufs_brs_add(sb, bindex);
20547 +
20548 +       if (!sysaufs_brs)
20549 +               return;
20550 +
20551 +       kobj = &au_sbi(sb)->si_kobj;
20552 +       bend = au_sbend(sb);
20553 +       for (; bindex <= bend; bindex++) {
20554 +               br = au_sbr(sb, bindex);
20555 +               snprintf(br->br_name, sizeof(br->br_name), SysaufsBr_PREFIX
20556 +                        "%d", bindex);
20557 +               err = sysfs_create_file(kobj, &br->br_attr);
20558 +               if (unlikely(err))
20559 +                       AuWarn("failed %s under sysfs(%d)\n", br->br_name, err);
20560 +       }
20561 +}
20562 diff --git a/fs/aufs/sysrq.c b/fs/aufs/sysrq.c
20563 new file mode 100644
20564 index 0000000..419ff2c
20565 --- /dev/null
20566 +++ b/fs/aufs/sysrq.c
20567 @@ -0,0 +1,106 @@
20568 +/*
20569 + * Copyright (C) 2005-2009 Junjiro R. Okajima
20570 + *
20571 + * This program, aufs is free software; you can redistribute it and/or modify
20572 + * it under the terms of the GNU General Public License as published by
20573 + * the Free Software Foundation; either version 2 of the License, or
20574 + * (at your option) any later version.
20575 + */
20576 +
20577 +/*
20578 + * magic sysrq hanlder
20579 + */
20580 +
20581 +#include <linux/fs.h>
20582 +#include <linux/module.h>
20583 +#include <linux/moduleparam.h>
20584 +/* #include <linux/sysrq.h> */
20585 +#include "aufs.h"
20586 +
20587 +/* ---------------------------------------------------------------------- */
20588 +
20589 +static void sysrq_sb(struct super_block *sb)
20590 +{
20591 +       char *plevel;
20592 +       struct au_sbinfo *sbinfo;
20593 +       struct file *file;
20594 +
20595 +       plevel = au_plevel;
20596 +       au_plevel = KERN_WARNING;
20597 +       au_debug(1);
20598 +
20599 +       sbinfo = au_sbi(sb);
20600 +       pr_warning("si=%lx\n", sysaufs_si_id(sbinfo));
20601 +       pr_warning(AUFS_NAME ": superblock\n");
20602 +       au_dpri_sb(sb);
20603 +       pr_warning(AUFS_NAME ": root dentry\n");
20604 +       au_dpri_dentry(sb->s_root);
20605 +       pr_warning(AUFS_NAME ": root inode\n");
20606 +       au_dpri_inode(sb->s_root->d_inode);
20607 +#if 0
20608 +       struct inode *i;
20609 +       pr_warning(AUFS_NAME ": isolated inode\n");
20610 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list)
20611 +               if (list_empty(&i->i_dentry))
20612 +                       au_dpri_inode(i);
20613 +#endif
20614 +       pr_warning(AUFS_NAME ": files\n");
20615 +       list_for_each_entry(file, &sb->s_files, f_u.fu_list)
20616 +               if (!special_file(file->f_dentry->d_inode->i_mode))
20617 +                       au_dpri_file(file);
20618 +
20619 +       au_plevel = plevel;
20620 +       au_debug(0);
20621 +}
20622 +
20623 +/* ---------------------------------------------------------------------- */
20624 +
20625 +/* module parameter */
20626 +static char *aufs_sysrq_key = "a";
20627 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
20628 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
20629 +
20630 +static void au_sysrq(int key __maybe_unused,
20631 +                    struct tty_struct *tty __maybe_unused)
20632 +{
20633 +       struct kobject *kobj;
20634 +       struct au_sbinfo *sbinfo;
20635 +
20636 +       /* spin_lock(&sysaufs_ket->list_lock); */
20637 +       list_for_each_entry(kobj, &sysaufs_ket->list, entry) {
20638 +               sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
20639 +               sysrq_sb(sbinfo->si_sb);
20640 +       }
20641 +       /* spin_unlock(&sysaufs_ket->list_lock); */
20642 +}
20643 +
20644 +static struct sysrq_key_op au_sysrq_op = {
20645 +       .handler        = au_sysrq,
20646 +       .help_msg       = "Aufs",
20647 +       .action_msg     = "Aufs",
20648 +       .enable_mask    = SYSRQ_ENABLE_DUMP
20649 +};
20650 +
20651 +/* ---------------------------------------------------------------------- */
20652 +
20653 +int __init au_sysrq_init(void)
20654 +{
20655 +       int err;
20656 +       char key;
20657 +
20658 +       err = -1;
20659 +       key = *aufs_sysrq_key;
20660 +       if ('a' <= key && key <= 'z')
20661 +               err = register_sysrq_key(key, &au_sysrq_op);
20662 +       if (unlikely(err))
20663 +               AuErr("err %d, sysrq=%c\n", err, key);
20664 +       return err;
20665 +}
20666 +
20667 +void au_sysrq_fin(void)
20668 +{
20669 +       int err;
20670 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
20671 +       if (unlikely(err))
20672 +               AuErr("err %d (ignored)\n", err);
20673 +}
20674 diff --git a/fs/aufs/vdir.c b/fs/aufs/vdir.c
20675 new file mode 100644
20676 index 0000000..9c7b64a
20677 --- /dev/null
20678 +++ b/fs/aufs/vdir.c
20679 @@ -0,0 +1,796 @@
20680 +/*
20681 + * Copyright (C) 2005-2009 Junjiro R. Okajima
20682 + *
20683 + * This program, aufs is free software; you can redistribute it and/or modify
20684 + * it under the terms of the GNU General Public License as published by
20685 + * the Free Software Foundation; either version 2 of the License, or
20686 + * (at your option) any later version.
20687 + */
20688 +
20689 +/*
20690 + * virtual or vertical directory
20691 + */
20692 +
20693 +#include "aufs.h"
20694 +
20695 +static unsigned int calc_size(int namelen)
20696 +{
20697 +       int sz;
20698 +       const int mask = sizeof(ino_t) - 1;
20699 +
20700 +       BUILD_BUG_ON(sizeof(ino_t) != sizeof(long));
20701 +
20702 +       sz = sizeof(struct au_vdir_de) + namelen;
20703 +       if (sz & mask) {
20704 +               sz += sizeof(ino_t);
20705 +               sz &= ~mask;
20706 +       }
20707 +
20708 +       AuDebugOn(sz % sizeof(ino_t));
20709 +       return sz;
20710 +}
20711 +
20712 +static int set_deblk_end(union au_vdir_deblk_p *p,
20713 +                        union au_vdir_deblk_p *deblk_end)
20714 +{
20715 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
20716 +               p->de->de_str.len = 0;
20717 +               /* smp_mb(); */
20718 +               return 0;
20719 +       }
20720 +       return -1; /* error */
20721 +}
20722 +
20723 +/* returns true or false */
20724 +static int is_deblk_end(union au_vdir_deblk_p *p,
20725 +                       union au_vdir_deblk_p *deblk_end)
20726 +{
20727 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
20728 +               return !p->de->de_str.len;
20729 +       return 1;
20730 +}
20731 +
20732 +static unsigned char *last_deblk(struct au_vdir *vdir)
20733 +{
20734 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
20735 +}
20736 +
20737 +/* ---------------------------------------------------------------------- */
20738 +
20739 +/*
20740 + * the allocated memory has to be freed by
20741 + * au_nhash_wh_free() or void au_nhash_de_free().
20742 + */
20743 +struct au_nhash *au_nhash_alloc(struct super_block *sb, aufs_bindex_t bend,
20744 +                               gfp_t gfp)
20745 +{
20746 +       struct au_nhash *nhash;
20747 +       struct hlist_head *head;
20748 +       unsigned int u, n;
20749 +       aufs_bindex_t bindex;
20750 +
20751 +       nhash = kmalloc(sizeof(*nhash) * (bend + 1), gfp);
20752 +       if (unlikely(!nhash))
20753 +               goto out;
20754 +
20755 +       n = au_sbi(sb)->si_rdhash;
20756 +       for (bindex = 0; bindex <= bend; bindex++) {
20757 +               head = kmalloc(sizeof(*nhash->nh_head) * n, gfp);
20758 +               if (unlikely(!head))
20759 +                       goto out_free;
20760 +               nhash[bindex].nh_num = n;
20761 +               nhash[bindex].nh_head = head;
20762 +               for (u = 0; u < n; u++)
20763 +                       INIT_HLIST_HEAD(head++);
20764 +       }
20765 +       return nhash; /* success */
20766 +
20767 + out_free:
20768 +       for (bindex--; bindex >= 0; bindex--)
20769 +               kfree(nhash[bindex].nh_head);
20770 +       kfree(nhash);
20771 + out:
20772 +       return ERR_PTR(-ENOMEM);
20773 +}
20774 +
20775 +static void au_nhash_wh_do_free(struct au_nhash *whlist)
20776 +{
20777 +       unsigned int u, n;
20778 +       struct hlist_head *head;
20779 +       struct au_vdir_wh *tpos;
20780 +       struct hlist_node *pos, *node;
20781 +
20782 +       n = whlist->nh_num;
20783 +       head = whlist->nh_head;
20784 +       for (u = 0; u < n; u++) {
20785 +               hlist_for_each_entry_safe(tpos, pos, node, head, wh_hash) {
20786 +                       /* hlist_del(pos); */
20787 +                       kfree(tpos);
20788 +               }
20789 +               head++;
20790 +       }
20791 +}
20792 +
20793 +void au_nhash_wh_free(struct au_nhash *whlist, aufs_bindex_t bend)
20794 +{
20795 +       aufs_bindex_t bindex;
20796 +
20797 +       for (bindex = 0; bindex <= bend; bindex++) {
20798 +               au_nhash_wh_do_free(whlist + bindex);
20799 +               kfree(whlist[bindex].nh_head);
20800 +       }
20801 +
20802 +       kfree(whlist);
20803 +}
20804 +
20805 +static void au_nhash_de_do_free(struct au_nhash *delist)
20806 +{
20807 +       unsigned int u, n;
20808 +       struct hlist_head *head;
20809 +       struct au_vdir_dehstr *tpos;
20810 +       struct hlist_node *pos, *node;
20811 +
20812 +       n = delist->nh_num;
20813 +       head = delist->nh_head;
20814 +       for (u = 0; u < n; u++) {
20815 +               hlist_for_each_entry_safe(tpos, pos, node, head, hash) {
20816 +                       /* hlist_del(pos); */
20817 +                       au_cache_free_dehstr(tpos);
20818 +               }
20819 +               head++;
20820 +       }
20821 +}
20822 +
20823 +static void au_nhash_de_free(struct au_nhash *delist, aufs_bindex_t bend)
20824 +{
20825 +       aufs_bindex_t bindex;
20826 +
20827 +       for (bindex = 0; bindex <= bend; bindex++) {
20828 +               au_nhash_de_do_free(delist + bindex);
20829 +               kfree(delist[bindex].nh_head);
20830 +       }
20831 +
20832 +       kfree(delist);
20833 +}
20834 +
20835 +/* ---------------------------------------------------------------------- */
20836 +
20837 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
20838 +                           int limit)
20839 +{
20840 +       int num;
20841 +       unsigned int u, n;
20842 +       struct hlist_head *head;
20843 +       struct au_vdir_wh *tpos;
20844 +       struct hlist_node *pos;
20845 +
20846 +       num = 0;
20847 +       n = whlist->nh_num;
20848 +       head = whlist->nh_head;
20849 +       for (u = 0; u < n; u++) {
20850 +               hlist_for_each_entry(tpos, pos, head, wh_hash)
20851 +                       if (tpos->wh_bindex == btgt && ++num > limit)
20852 +                               return 1;
20853 +               head++;
20854 +       }
20855 +       return 0;
20856 +}
20857 +
20858 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
20859 +                                      const unsigned char *name,
20860 +                                      unsigned int len)
20861 +{
20862 +       return nhash->nh_head + full_name_hash(name, len) % nhash->nh_num;
20863 +}
20864 +
20865 +/* returns found or not */
20866 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int namelen)
20867 +{
20868 +       struct hlist_head *head;
20869 +       struct au_vdir_wh *tpos;
20870 +       struct hlist_node *pos;
20871 +       struct au_vdir_destr *str;
20872 +
20873 +       head = au_name_hash(whlist, name, namelen);
20874 +       hlist_for_each_entry(tpos, pos, head, wh_hash) {
20875 +               str = &tpos->wh_str;
20876 +               AuDbg("%.*s\n", str->len, str->name);
20877 +               if (str->len == namelen && !memcmp(str->name, name, namelen))
20878 +                       return 1;
20879 +       }
20880 +       return 0;
20881 +}
20882 +
20883 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int namelen,
20884 +                      aufs_bindex_t bindex)
20885 +{
20886 +       int err;
20887 +       struct au_vdir_destr *str;
20888 +       struct au_vdir_wh *wh;
20889 +
20890 +       err = -ENOMEM;
20891 +       wh = kmalloc(sizeof(*wh) + namelen, GFP_NOFS);
20892 +       if (unlikely(!wh))
20893 +               goto out;
20894 +
20895 +       err = 0;
20896 +       wh->wh_bindex = bindex;
20897 +       str = &wh->wh_str;
20898 +       str->len = namelen;
20899 +       memcpy(str->name, name, namelen);
20900 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, namelen));
20901 +       /* smp_mb(); */
20902 +
20903 + out:
20904 +       return err;
20905 +}
20906 +
20907 +/* ---------------------------------------------------------------------- */
20908 +
20909 +void au_vdir_free(struct au_vdir *vdir)
20910 +{
20911 +       unsigned char **deblk;
20912 +
20913 +       deblk = vdir->vd_deblk;
20914 +       while (vdir->vd_nblk--)
20915 +               kfree(*deblk++);
20916 +       kfree(vdir->vd_deblk);
20917 +       au_cache_free_vdir(vdir);
20918 +}
20919 +
20920 +static int append_deblk(struct au_vdir *vdir)
20921 +{
20922 +       int err;
20923 +       unsigned long sz, ul;
20924 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
20925 +       union au_vdir_deblk_p p, deblk_end;
20926 +       unsigned char **o;
20927 +
20928 +       err = -ENOMEM;
20929 +       sz = sizeof(*o) * vdir->vd_nblk;
20930 +       o = au_kzrealloc(vdir->vd_deblk, sz, sz + sizeof(*o), GFP_NOFS);
20931 +       if (unlikely(!o))
20932 +               goto out;
20933 +
20934 +       vdir->vd_deblk = o;
20935 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
20936 +       if (p.deblk) {
20937 +               ul = vdir->vd_nblk++;
20938 +               vdir->vd_deblk[ul] = p.deblk;
20939 +               vdir->vd_last.ul = ul;
20940 +               vdir->vd_last.p.deblk = p.deblk;
20941 +               deblk_end.deblk = p.deblk + deblk_sz;
20942 +               err = set_deblk_end(&p, &deblk_end);
20943 +       }
20944 +
20945 + out:
20946 +       return err;
20947 +}
20948 +
20949 +static struct au_vdir *alloc_vdir(struct super_block *sb)
20950 +{
20951 +       struct au_vdir *vdir;
20952 +       int err;
20953 +
20954 +       err = -ENOMEM;
20955 +       vdir = au_cache_alloc_vdir();
20956 +       if (unlikely(!vdir))
20957 +               goto out;
20958 +
20959 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
20960 +       if (unlikely(!vdir->vd_deblk))
20961 +               goto out_free;
20962 +
20963 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
20964 +       vdir->vd_nblk = 0;
20965 +       vdir->vd_version = 0;
20966 +       vdir->vd_jiffy = 0;
20967 +       err = append_deblk(vdir);
20968 +       if (!err)
20969 +               return vdir; /* success */
20970 +
20971 +       kfree(vdir->vd_deblk);
20972 +
20973 + out_free:
20974 +       au_cache_free_vdir(vdir);
20975 + out:
20976 +       vdir = ERR_PTR(err);
20977 +       return vdir;
20978 +}
20979 +
20980 +static int reinit_vdir(struct au_vdir *vdir)
20981 +{
20982 +       int err;
20983 +       union au_vdir_deblk_p p, deblk_end;
20984 +
20985 +       while (vdir->vd_nblk > 1) {
20986 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
20987 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
20988 +               vdir->vd_nblk--;
20989 +       }
20990 +       p.deblk = vdir->vd_deblk[0];
20991 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
20992 +       err = set_deblk_end(&p, &deblk_end);
20993 +       /* keep vd_dblk_sz */
20994 +       vdir->vd_last.ul = 0;
20995 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
20996 +       vdir->vd_version = 0;
20997 +       vdir->vd_jiffy = 0;
20998 +       /* smp_mb(); */
20999 +       return err;
21000 +}
21001 +
21002 +/* ---------------------------------------------------------------------- */
21003 +
21004 +/* returns found(true) or not */
21005 +static int test_known(struct au_nhash *delist, char *name, int namelen)
21006 +{
21007 +       struct hlist_head *head;
21008 +       struct au_vdir_dehstr *tpos;
21009 +       struct hlist_node *pos;
21010 +       struct au_vdir_destr *str;
21011 +
21012 +       head = au_name_hash(delist, name, namelen);
21013 +       hlist_for_each_entry(tpos, pos, head, hash) {
21014 +               str = tpos->str;
21015 +               AuDbg("%.*s\n", str->len, str->name);
21016 +               if (str->len == namelen && !memcmp(str->name, name, namelen))
21017 +                       return 1;
21018 +       }
21019 +       return 0;
21020 +
21021 +}
21022 +
21023 +static int append_de(struct au_vdir *vdir, char *name, int namelen, ino_t ino,
21024 +                    unsigned int d_type, struct au_nhash *delist)
21025 +{
21026 +       int err;
21027 +       unsigned int sz;
21028 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
21029 +       union au_vdir_deblk_p p, *room, deblk_end;
21030 +       struct au_vdir_dehstr *dehstr;
21031 +
21032 +       p.deblk = last_deblk(vdir);
21033 +       deblk_end.deblk = p.deblk + deblk_sz;
21034 +       room = &vdir->vd_last.p;
21035 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
21036 +                 || !is_deblk_end(room, &deblk_end));
21037 +
21038 +       sz = calc_size(namelen);
21039 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
21040 +               err = append_deblk(vdir);
21041 +               if (unlikely(err))
21042 +                       goto out;
21043 +
21044 +               p.deblk = last_deblk(vdir);
21045 +               deblk_end.deblk = p.deblk + deblk_sz;
21046 +               /* smp_mb(); */
21047 +               AuDebugOn(room->deblk != p.deblk);
21048 +       }
21049 +
21050 +       err = -ENOMEM;
21051 +       dehstr = au_cache_alloc_dehstr();
21052 +       if (unlikely(!dehstr))
21053 +               goto out;
21054 +
21055 +       dehstr->str = &room->de->de_str;
21056 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, namelen));
21057 +       room->de->de_ino = ino;
21058 +       room->de->de_type = d_type;
21059 +       room->de->de_str.len = namelen;
21060 +       memcpy(room->de->de_str.name, name, namelen);
21061 +
21062 +       err = 0;
21063 +       room->deblk += sz;
21064 +       if (unlikely(set_deblk_end(room, &deblk_end)))
21065 +               err = append_deblk(vdir);
21066 +       /* smp_mb(); */
21067 +
21068 + out:
21069 +       return err;
21070 +}
21071 +
21072 +/* ---------------------------------------------------------------------- */
21073 +
21074 +static int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
21075 +                 unsigned int d_type, ino_t *ino)
21076 +{
21077 +       int err;
21078 +       struct mutex *mtx;
21079 +       const int isdir = (d_type == DT_DIR);
21080 +
21081 +       /* prevent hardlinks from race condition */
21082 +       mtx = NULL;
21083 +       if (!isdir) {
21084 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
21085 +               mutex_lock(mtx);
21086 +       }
21087 +       err = au_xino_read(sb, bindex, h_ino, ino);
21088 +       if (unlikely(err))
21089 +               goto out;
21090 +
21091 +       if (!*ino) {
21092 +               err = -EIO;
21093 +               *ino = au_xino_new_ino(sb);
21094 +               if (unlikely(!*ino))
21095 +                       goto out;
21096 +               err = au_xino_write(sb, bindex, h_ino, *ino);
21097 +               if (unlikely(err))
21098 +                       goto out;
21099 +       }
21100 +
21101 + out:
21102 +       if (!isdir)
21103 +               mutex_unlock(mtx);
21104 +       return err;
21105 +}
21106 +
21107 +#define AuFillVdir_CALLED      1
21108 +#define AuFillVdir_WHABLE      (1 << 1)
21109 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
21110 +#define au_fset_fillvdir(flags, name)  { (flags) |= AuFillVdir_##name; }
21111 +#define au_fclr_fillvdir(flags, name)  { (flags) &= ~AuFillVdir_##name; }
21112 +
21113 +struct fillvdir_arg {
21114 +       struct file             *file;
21115 +       struct au_vdir          *vdir;
21116 +       struct au_nhash         *delist;
21117 +       struct au_nhash         *whlist;
21118 +       aufs_bindex_t           bindex;
21119 +       unsigned int            flags;
21120 +       int                     err;
21121 +};
21122 +
21123 +static int fillvdir(void *__arg, const char *__name, int namelen,
21124 +                   loff_t offset __maybe_unused, u64 h_ino,
21125 +                   unsigned int d_type)
21126 +{
21127 +       struct fillvdir_arg *arg = __arg;
21128 +       char *name = (void *)__name;
21129 +       struct super_block *sb;
21130 +       struct au_nhash *delist, *whlist;
21131 +       ino_t ino;
21132 +       aufs_bindex_t bindex, bend;
21133 +
21134 +       bend = arg->bindex;
21135 +       arg->err = 0;
21136 +       au_fset_fillvdir(arg->flags, CALLED);
21137 +       /* smp_mb(); */
21138 +       if (namelen <= AUFS_WH_PFX_LEN
21139 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
21140 +               delist = arg->delist;
21141 +               whlist = arg->whlist;
21142 +               for (bindex = 0; bindex < bend; bindex++)
21143 +                       if (test_known(delist++, name, namelen)
21144 +                           || au_nhash_test_known_wh(whlist + bindex, name,
21145 +                                                     namelen))
21146 +                               goto out; /* already exists or whiteouted */
21147 +
21148 +               sb = arg->file->f_dentry->d_sb;
21149 +               arg->err = au_ino(sb, bend, h_ino, d_type, &ino);
21150 +               if (!arg->err)
21151 +                       arg->err = append_de(arg->vdir, name, namelen, ino,
21152 +                                            d_type, arg->delist + bend);
21153 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
21154 +               name += AUFS_WH_PFX_LEN;
21155 +               namelen -= AUFS_WH_PFX_LEN;
21156 +               whlist = arg->whlist;
21157 +               for (bindex = 0; bindex < bend; bindex++)
21158 +                       if (au_nhash_test_known_wh(whlist++, name, namelen))
21159 +                               goto out; /* already whiteouted */
21160 +
21161 +               if (!arg->err)
21162 +                       arg->err = au_nhash_append_wh
21163 +                               (arg->whlist + bend, name, namelen, bend);
21164 +       }
21165 +
21166 + out:
21167 +       if (!arg->err)
21168 +               arg->vdir->vd_jiffy = jiffies;
21169 +       /* smp_mb(); */
21170 +       AuTraceErr(arg->err);
21171 +       return arg->err;
21172 +}
21173 +
21174 +static int au_do_read_vdir(struct fillvdir_arg *arg)
21175 +{
21176 +       int err;
21177 +       loff_t offset;
21178 +       aufs_bindex_t bend, bindex;
21179 +       struct file *hf, *file;
21180 +       struct super_block *sb;
21181 +
21182 +       err = -ENOMEM;
21183 +       file = arg->file;
21184 +       sb = file->f_dentry->d_sb;
21185 +       bend = au_fbend(file);
21186 +       arg->delist = au_nhash_alloc(sb, bend, GFP_NOFS);
21187 +       if (unlikely(!arg->delist))
21188 +               goto out;
21189 +       arg->whlist = au_nhash_alloc(sb, bend, GFP_NOFS);
21190 +       if (unlikely(!arg->whlist))
21191 +               goto out_delist;
21192 +
21193 +       err = 0;
21194 +       arg->flags = 0;
21195 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
21196 +               hf = au_h_fptr(file, bindex);
21197 +               if (!hf)
21198 +                       continue;
21199 +
21200 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
21201 +               err = offset;
21202 +               if (unlikely(offset))
21203 +                       break;
21204 +
21205 +               arg->bindex = bindex;
21206 +               au_fclr_fillvdir(arg->flags, WHABLE);
21207 +               if (bindex != bend
21208 +                   && au_br_whable(au_sbr_perm(sb, bindex)))
21209 +                       au_fset_fillvdir(arg->flags, WHABLE);
21210 +               do {
21211 +                       arg->err = 0;
21212 +                       au_fclr_fillvdir(arg->flags, CALLED);
21213 +                       /* smp_mb(); */
21214 +                       err = vfsub_readdir(hf, fillvdir, arg);
21215 +                       if (err >= 0)
21216 +                               err = arg->err;
21217 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
21218 +       }
21219 +       au_nhash_wh_free(arg->whlist, bend);
21220 +
21221 + out_delist:
21222 +       au_nhash_de_free(arg->delist, bend);
21223 + out:
21224 +       return err;
21225 +}
21226 +
21227 +static int read_vdir(struct file *file, int may_read)
21228 +{
21229 +       int err;
21230 +       unsigned long expire;
21231 +       unsigned char do_read;
21232 +       struct fillvdir_arg arg;
21233 +       struct inode *inode;
21234 +       struct au_vdir *vdir, *allocated;
21235 +
21236 +       err = 0;
21237 +       inode = file->f_dentry->d_inode;
21238 +       IMustLock(inode);
21239 +       allocated = NULL;
21240 +       do_read = 0;
21241 +       expire = au_sbi(inode->i_sb)->si_rdcache;
21242 +       vdir = au_ivdir(inode);
21243 +       if (!vdir) {
21244 +               do_read = 1;
21245 +               vdir = alloc_vdir(inode->i_sb);
21246 +               err = PTR_ERR(vdir);
21247 +               if (IS_ERR(vdir))
21248 +                       goto out;
21249 +               err = 0;
21250 +               allocated = vdir;
21251 +       } else if (may_read
21252 +                  && (inode->i_version != vdir->vd_version
21253 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
21254 +               do_read = 1;
21255 +               err = reinit_vdir(vdir);
21256 +               if (unlikely(err))
21257 +                       goto out;
21258 +       }
21259 +
21260 +       if (!do_read)
21261 +               return 0; /* success */
21262 +
21263 +       arg.file = file;
21264 +       arg.vdir = vdir;
21265 +       err = au_do_read_vdir(&arg);
21266 +       if (!err) {
21267 +               /* file->f_pos = 0; */
21268 +               vdir->vd_version = inode->i_version;
21269 +               vdir->vd_last.ul = 0;
21270 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
21271 +               if (allocated)
21272 +                       au_set_ivdir(inode, allocated);
21273 +       } else if (allocated)
21274 +               au_vdir_free(allocated);
21275 +
21276 + out:
21277 +       return err;
21278 +}
21279 +
21280 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
21281 +{
21282 +       int err, rerr;
21283 +       unsigned long ul, n;
21284 +       const unsigned int deblk_sz = src->vd_deblk_sz;
21285 +
21286 +       AuDebugOn(tgt->vd_nblk != 1);
21287 +
21288 +       err = -ENOMEM;
21289 +       if (tgt->vd_nblk < src->vd_nblk) {
21290 +               unsigned char **p;
21291 +
21292 +               p = au_kzrealloc(tgt->vd_deblk, sizeof(*p) * tgt->vd_nblk,
21293 +                                sizeof(*p) * src->vd_nblk, GFP_NOFS);
21294 +               if (unlikely(!p))
21295 +                       goto out;
21296 +               tgt->vd_deblk = p;
21297 +       }
21298 +
21299 +       tgt->vd_nblk = src->vd_nblk;
21300 +       tgt->vd_deblk_sz = deblk_sz;
21301 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
21302 +       /* tgt->vd_last.i = 0; */
21303 +       /* tgt->vd_last.p.deblk = tgt->vd_deblk[0]; */
21304 +       tgt->vd_version = src->vd_version;
21305 +       tgt->vd_jiffy = src->vd_jiffy;
21306 +
21307 +       n = src->vd_nblk;
21308 +       for (ul = 1; ul < n; ul++) {
21309 +               tgt->vd_deblk[ul] = kmalloc(deblk_sz, GFP_NOFS);
21310 +               if (tgt->vd_deblk[ul])
21311 +                       memcpy(tgt->vd_deblk[ul], src->vd_deblk[ul], deblk_sz);
21312 +               else
21313 +                       goto out;
21314 +       }
21315 +       /* smp_mb(); */
21316 +       return 0; /* success */
21317 +
21318 + out:
21319 +       rerr = reinit_vdir(tgt);
21320 +       BUG_ON(rerr);
21321 +       return err;
21322 +}
21323 +
21324 +int au_vdir_init(struct file *file)
21325 +{
21326 +       int err;
21327 +       struct inode *inode;
21328 +       struct au_vdir *vdir_cache, *allocated;
21329 +
21330 +       err = read_vdir(file, !file->f_pos);
21331 +       if (unlikely(err))
21332 +               goto out;
21333 +
21334 +       allocated = NULL;
21335 +       vdir_cache = au_fvdir_cache(file);
21336 +       if (!vdir_cache) {
21337 +               vdir_cache = alloc_vdir(file->f_dentry->d_sb);
21338 +               err = PTR_ERR(vdir_cache);
21339 +               if (IS_ERR(vdir_cache))
21340 +                       goto out;
21341 +               allocated = vdir_cache;
21342 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
21343 +               err = reinit_vdir(vdir_cache);
21344 +               if (unlikely(err))
21345 +                       goto out;
21346 +       } else
21347 +               return 0; /* success */
21348 +
21349 +       inode = file->f_dentry->d_inode;
21350 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
21351 +       if (!err) {
21352 +               file->f_version = inode->i_version;
21353 +               if (allocated)
21354 +                       au_set_fvdir_cache(file, allocated);
21355 +       } else if (allocated)
21356 +               au_vdir_free(allocated);
21357 +
21358 + out:
21359 +       return err;
21360 +}
21361 +
21362 +static loff_t calc_offset(struct au_vdir *vdir)
21363 +{
21364 +       loff_t offset;
21365 +       union au_vdir_deblk_p p;
21366 +
21367 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
21368 +       offset = vdir->vd_last.p.deblk - p.deblk;
21369 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
21370 +       return offset;
21371 +}
21372 +
21373 +/* returns true or false */
21374 +static int seek_vdir(struct file *file)
21375 +{
21376 +       int valid;
21377 +       unsigned int deblk_sz;
21378 +       unsigned long ul, n;
21379 +       loff_t offset;
21380 +       union au_vdir_deblk_p p, deblk_end;
21381 +       struct au_vdir *vdir_cache;
21382 +
21383 +       valid = 1;
21384 +       vdir_cache = au_fvdir_cache(file);
21385 +       offset = calc_offset(vdir_cache);
21386 +       AuDbg("offset %lld\n", offset);
21387 +       if (file->f_pos == offset)
21388 +               goto out;
21389 +
21390 +       vdir_cache->vd_last.ul = 0;
21391 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
21392 +       if (!file->f_pos)
21393 +               goto out;
21394 +
21395 +       valid = 0;
21396 +       deblk_sz = vdir_cache->vd_deblk_sz;
21397 +       ul = div64_u64(file->f_pos, deblk_sz);
21398 +       AuDbg("ul %lu\n", ul);
21399 +       if (ul >= vdir_cache->vd_nblk)
21400 +               goto out;
21401 +
21402 +       n = vdir_cache->vd_nblk;
21403 +       for (; ul < n; ul++) {
21404 +               p.deblk = vdir_cache->vd_deblk[ul];
21405 +               deblk_end.deblk = p.deblk + deblk_sz;
21406 +               offset = ul;
21407 +               offset *= deblk_sz;
21408 +               while (!is_deblk_end(&p, &deblk_end) && offset < file->f_pos) {
21409 +                       unsigned int l;
21410 +
21411 +                       l = calc_size(p.de->de_str.len);
21412 +                       offset += l;
21413 +                       p.deblk += l;
21414 +               }
21415 +               if (!is_deblk_end(&p, &deblk_end)) {
21416 +                       valid = 1;
21417 +                       vdir_cache->vd_last.ul = ul;
21418 +                       vdir_cache->vd_last.p = p;
21419 +                       break;
21420 +               }
21421 +       }
21422 +
21423 + out:
21424 +       /* smp_mb(); */
21425 +       AuTraceErr(!valid);
21426 +       return valid;
21427 +}
21428 +
21429 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir)
21430 +{
21431 +       int err;
21432 +       unsigned int l, deblk_sz;
21433 +       union au_vdir_deblk_p deblk_end;
21434 +       struct au_vdir *vdir_cache;
21435 +       struct au_vdir_de *de;
21436 +
21437 +       vdir_cache = au_fvdir_cache(file);
21438 +       if (!seek_vdir(file))
21439 +               return 0;
21440 +
21441 +       deblk_sz = vdir_cache->vd_deblk_sz;
21442 +       while (1) {
21443 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
21444 +               deblk_end.deblk += deblk_sz;
21445 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
21446 +                       de = vdir_cache->vd_last.p.de;
21447 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
21448 +                             de->de_str.len, de->de_str.name, file->f_pos,
21449 +                             (unsigned long)de->de_ino, de->de_type);
21450 +                       err = filldir(dirent, de->de_str.name, de->de_str.len,
21451 +                                     file->f_pos, de->de_ino, de->de_type);
21452 +                       if (unlikely(err)) {
21453 +                               AuTraceErr(err);
21454 +                               /* todo: ignore the error caused by udba? */
21455 +                               /* return err; */
21456 +                               return 0;
21457 +                       }
21458 +
21459 +                       l = calc_size(de->de_str.len);
21460 +                       vdir_cache->vd_last.p.deblk += l;
21461 +                       file->f_pos += l;
21462 +               }
21463 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
21464 +                       vdir_cache->vd_last.ul++;
21465 +                       vdir_cache->vd_last.p.deblk
21466 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
21467 +                       file->f_pos = deblk_sz * vdir_cache->vd_last.ul;
21468 +                       continue;
21469 +               }
21470 +               break;
21471 +       }
21472 +
21473 +       /* smp_mb(); */
21474 +       return 0;
21475 +}
21476 diff --git a/fs/aufs/vfsub.c b/fs/aufs/vfsub.c
21477 new file mode 100644
21478 index 0000000..f58d07d
21479 --- /dev/null
21480 +++ b/fs/aufs/vfsub.c
21481 @@ -0,0 +1,724 @@
21482 +/*
21483 + * Copyright (C) 2005-2009 Junjiro R. Okajima
21484 + *
21485 + * This program, aufs is free software; you can redistribute it and/or modify
21486 + * it under the terms of the GNU General Public License as published by
21487 + * the Free Software Foundation; either version 2 of the License, or
21488 + * (at your option) any later version.
21489 + */
21490 +
21491 +/*
21492 + * sub-routines for VFS
21493 + */
21494 +
21495 +#include <linux/uaccess.h>
21496 +#include "aufs.h"
21497 +
21498 +int vfsub_update_h_iattr(struct path *h_path, int *did)
21499 +{
21500 +       int err;
21501 +       struct kstat st;
21502 +       struct super_block *h_sb;
21503 +
21504 +       /* for remote fs, leave work for its getattr or d_revalidate */
21505 +       /* for bad i_attr fs, handle them in aufs_getattr() */
21506 +       /* still some fs may acquire i_mutex. we need to skip them */
21507 +       err = 0;
21508 +       if (!did)
21509 +               did = &err;
21510 +       h_sb = h_path->dentry->d_sb;
21511 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
21512 +       if (*did)
21513 +               err = vfs_getattr(h_path->mnt, h_path->dentry, &st);
21514 +
21515 +       return err;
21516 +}
21517 +
21518 +/* ---------------------------------------------------------------------- */
21519 +
21520 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
21521 +{
21522 +       struct file *file;
21523 +
21524 +       lockdep_off();
21525 +       file = filp_open(path, oflags, mode);
21526 +       lockdep_on();
21527 +       if (IS_ERR(file))
21528 +               goto out;
21529 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21530 +
21531 + out:
21532 +       return file;
21533 +}
21534 +
21535 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
21536 +{
21537 +       int err;
21538 +
21539 +       /* lockdep_off(); */
21540 +       err = kern_path(name, flags, path);
21541 +       /* lockdep_on(); */
21542 +       if (!err && path->dentry->d_inode)
21543 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
21544 +       return err;
21545 +}
21546 +
21547 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
21548 +                                   int len)
21549 +{
21550 +       struct path path = {
21551 +               .mnt = NULL
21552 +       };
21553 +
21554 +       IMustLock(parent->d_inode);
21555 +
21556 +       path.dentry = lookup_one_len(name, parent, len);
21557 +       if (IS_ERR(path.dentry))
21558 +               goto out;
21559 +       if (path.dentry->d_inode)
21560 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
21561 +
21562 + out:
21563 +       return path.dentry;
21564 +}
21565 +
21566 +struct dentry *vfsub_lookup_hash(struct nameidata *nd)
21567 +{
21568 +       struct path path = {
21569 +               .mnt = nd->path.mnt
21570 +       };
21571 +
21572 +       IMustLock(nd->path.dentry->d_inode);
21573 +
21574 +       path.dentry = lookup_hash(nd);
21575 +       if (!IS_ERR(path.dentry) && path.dentry->d_inode)
21576 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
21577 +
21578 +       return path.dentry;
21579 +}
21580 +
21581 +/* ---------------------------------------------------------------------- */
21582 +
21583 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
21584 +                                struct dentry *d2, struct au_hinode *hdir2)
21585 +{
21586 +       struct dentry *d;
21587 +
21588 +       lockdep_off();
21589 +       d = lock_rename(d1, d2);
21590 +       lockdep_on();
21591 +       au_hin_suspend(hdir1);
21592 +       if (hdir1 != hdir2)
21593 +               au_hin_suspend(hdir2);
21594 +
21595 +       return d;
21596 +}
21597 +
21598 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
21599 +                        struct dentry *d2, struct au_hinode *hdir2)
21600 +{
21601 +       au_hin_resume(hdir1);
21602 +       if (hdir1 != hdir2)
21603 +               au_hin_resume(hdir2);
21604 +       lockdep_off();
21605 +       unlock_rename(d1, d2);
21606 +       lockdep_on();
21607 +}
21608 +
21609 +/* ---------------------------------------------------------------------- */
21610 +
21611 +int vfsub_create(struct inode *dir, struct path *path, int mode)
21612 +{
21613 +       int err;
21614 +       struct dentry *d;
21615 +
21616 +       IMustLock(dir);
21617 +
21618 +       d = path->dentry;
21619 +       path->dentry = d->d_parent;
21620 +       err = security_path_mknod(path, path->dentry, mode, 0);
21621 +       path->dentry = d;
21622 +       if (unlikely(err))
21623 +               goto out;
21624 +
21625 +       if (au_test_fs_null_nd(dir->i_sb))
21626 +               err = vfs_create(dir, path->dentry, mode, NULL);
21627 +       else {
21628 +               struct nameidata h_nd;
21629 +
21630 +               memset(&h_nd, 0, sizeof(h_nd));
21631 +               h_nd.flags = LOOKUP_CREATE;
21632 +               h_nd.intent.open.flags = O_CREAT
21633 +                       | vfsub_fmode_to_uint(FMODE_READ);
21634 +               h_nd.intent.open.create_mode = mode;
21635 +               h_nd.path.dentry = path->dentry->d_parent;
21636 +               h_nd.path.mnt = path->mnt;
21637 +               path_get(&h_nd.path);
21638 +               err = vfs_create(dir, path->dentry, mode, &h_nd);
21639 +               path_put(&h_nd.path);
21640 +       }
21641 +
21642 +       if (!err) {
21643 +               struct path tmp = *path;
21644 +               int did;
21645 +
21646 +               vfsub_update_h_iattr(&tmp, &did);
21647 +               if (did) {
21648 +                       tmp.dentry = path->dentry->d_parent;
21649 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21650 +               }
21651 +               /*ignore*/
21652 +       }
21653 +
21654 + out:
21655 +       return err;
21656 +}
21657 +
21658 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
21659 +{
21660 +       int err;
21661 +       struct dentry *d;
21662 +
21663 +       IMustLock(dir);
21664 +
21665 +       d = path->dentry;
21666 +       path->dentry = d->d_parent;
21667 +       err = security_path_symlink(path, path->dentry, symname);
21668 +       path->dentry = d;
21669 +       if (unlikely(err))
21670 +               goto out;
21671 +
21672 +       err = vfs_symlink(dir, path->dentry, symname);
21673 +       if (!err) {
21674 +               struct path tmp = *path;
21675 +               int did;
21676 +
21677 +               vfsub_update_h_iattr(&tmp, &did);
21678 +               if (did) {
21679 +                       tmp.dentry = path->dentry->d_parent;
21680 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21681 +               }
21682 +               /*ignore*/
21683 +       }
21684 +
21685 + out:
21686 +       return err;
21687 +}
21688 +
21689 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
21690 +{
21691 +       int err;
21692 +       struct dentry *d;
21693 +
21694 +       IMustLock(dir);
21695 +
21696 +       d = path->dentry;
21697 +       path->dentry = d->d_parent;
21698 +       err = security_path_mknod(path, path->dentry, mode, dev);
21699 +       path->dentry = d;
21700 +       if (unlikely(err))
21701 +               goto out;
21702 +
21703 +       err = vfs_mknod(dir, path->dentry, mode, dev);
21704 +       if (!err) {
21705 +               struct path tmp = *path;
21706 +               int did;
21707 +
21708 +               vfsub_update_h_iattr(&tmp, &did);
21709 +               if (did) {
21710 +                       tmp.dentry = path->dentry->d_parent;
21711 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21712 +               }
21713 +               /*ignore*/
21714 +       }
21715 +
21716 + out:
21717 +       return err;
21718 +}
21719 +
21720 +static int au_test_nlink(struct inode *inode)
21721 +{
21722 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
21723 +
21724 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
21725 +           || inode->i_nlink < link_max)
21726 +               return 0;
21727 +       return -EMLINK;
21728 +}
21729 +
21730 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
21731 +{
21732 +       int err;
21733 +       struct dentry *d;
21734 +
21735 +       IMustLock(dir);
21736 +
21737 +       err = au_test_nlink(src_dentry->d_inode);
21738 +       if (unlikely(err))
21739 +               return err;
21740 +
21741 +       d = path->dentry;
21742 +       path->dentry = d->d_parent;
21743 +       err = security_path_link(src_dentry, path, path->dentry);
21744 +       path->dentry = d;
21745 +       if (unlikely(err))
21746 +               goto out;
21747 +
21748 +       lockdep_off();
21749 +       err = vfs_link(src_dentry, dir, path->dentry);
21750 +       lockdep_on();
21751 +       if (!err) {
21752 +               struct path tmp = *path;
21753 +               int did;
21754 +
21755 +               /* fuse has different memory inode for the same inumber */
21756 +               vfsub_update_h_iattr(&tmp, &did);
21757 +               if (did) {
21758 +                       tmp.dentry = path->dentry->d_parent;
21759 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21760 +                       tmp.dentry = src_dentry;
21761 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21762 +               }
21763 +               /*ignore*/
21764 +       }
21765 +
21766 + out:
21767 +       return err;
21768 +}
21769 +
21770 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
21771 +                struct inode *dir, struct path *path)
21772 +{
21773 +       int err;
21774 +       struct path tmp = {
21775 +               .mnt    = path->mnt
21776 +       };
21777 +       struct dentry *d;
21778 +
21779 +       IMustLock(dir);
21780 +       IMustLock(src_dir);
21781 +
21782 +       d = path->dentry;
21783 +       path->dentry = d->d_parent;
21784 +       tmp.dentry = src_dentry->d_parent;
21785 +       err = security_path_rename(&tmp, src_dentry, path, path->dentry);
21786 +       path->dentry = d;
21787 +       if (unlikely(err))
21788 +               goto out;
21789 +
21790 +       lockdep_off();
21791 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
21792 +       lockdep_on();
21793 +       if (!err) {
21794 +               int did;
21795 +
21796 +               tmp.dentry = d->d_parent;
21797 +               vfsub_update_h_iattr(&tmp, &did);
21798 +               if (did) {
21799 +                       tmp.dentry = src_dentry;
21800 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21801 +                       tmp.dentry = src_dentry->d_parent;
21802 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21803 +               }
21804 +               /*ignore*/
21805 +       }
21806 +
21807 + out:
21808 +       return err;
21809 +}
21810 +
21811 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
21812 +{
21813 +       int err;
21814 +       struct dentry *d;
21815 +
21816 +       IMustLock(dir);
21817 +
21818 +       d = path->dentry;
21819 +       path->dentry = d->d_parent;
21820 +       err = security_path_mkdir(path, path->dentry, mode);
21821 +       path->dentry = d;
21822 +       if (unlikely(err))
21823 +               goto out;
21824 +
21825 +       err = vfs_mkdir(dir, path->dentry, mode);
21826 +       if (!err) {
21827 +               struct path tmp = *path;
21828 +               int did;
21829 +
21830 +               vfsub_update_h_iattr(&tmp, &did);
21831 +               if (did) {
21832 +                       tmp.dentry = path->dentry->d_parent;
21833 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
21834 +               }
21835 +               /*ignore*/
21836 +       }
21837 +
21838 + out:
21839 +       return err;
21840 +}
21841 +
21842 +int vfsub_rmdir(struct inode *dir, struct path *path)
21843 +{
21844 +       int err;
21845 +       struct dentry *d;
21846 +
21847 +       IMustLock(dir);
21848 +
21849 +       d = path->dentry;
21850 +       path->dentry = d->d_parent;
21851 +       err = security_path_rmdir(path, path->dentry);
21852 +       path->dentry = d;
21853 +       if (unlikely(err))
21854 +               goto out;
21855 +
21856 +       lockdep_off();
21857 +       err = vfs_rmdir(dir, path->dentry);
21858 +       lockdep_on();
21859 +       if (!err) {
21860 +               struct path tmp = {
21861 +                       .dentry = path->dentry->d_parent,
21862 +                       .mnt    = path->mnt
21863 +               };
21864 +
21865 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
21866 +       }
21867 +
21868 + out:
21869 +       return err;
21870 +}
21871 +
21872 +/* ---------------------------------------------------------------------- */
21873 +
21874 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
21875 +                    loff_t *ppos)
21876 +{
21877 +       ssize_t err;
21878 +
21879 +       err = vfs_read(file, ubuf, count, ppos);
21880 +       if (err >= 0)
21881 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21882 +       return err;
21883 +}
21884 +
21885 +/* todo: kernel_read()? */
21886 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
21887 +                    loff_t *ppos)
21888 +{
21889 +       ssize_t err;
21890 +       mm_segment_t oldfs;
21891 +
21892 +       oldfs = get_fs();
21893 +       set_fs(KERNEL_DS);
21894 +       err = vfsub_read_u(file, (char __user *)kbuf, count, ppos);
21895 +       set_fs(oldfs);
21896 +       return err;
21897 +}
21898 +
21899 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
21900 +                     loff_t *ppos)
21901 +{
21902 +       ssize_t err;
21903 +
21904 +       lockdep_off();
21905 +       err = vfs_write(file, ubuf, count, ppos);
21906 +       lockdep_on();
21907 +       if (err >= 0)
21908 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21909 +       return err;
21910 +}
21911 +
21912 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
21913 +{
21914 +       ssize_t err;
21915 +       mm_segment_t oldfs;
21916 +
21917 +       oldfs = get_fs();
21918 +       set_fs(KERNEL_DS);
21919 +       err = vfsub_write_u(file, (const char __user *)kbuf, count, ppos);
21920 +       set_fs(oldfs);
21921 +       return err;
21922 +}
21923 +
21924 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
21925 +{
21926 +       int err;
21927 +
21928 +       lockdep_off();
21929 +       err = vfs_readdir(file, filldir, arg);
21930 +       lockdep_on();
21931 +       if (err >= 0)
21932 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21933 +       return err;
21934 +}
21935 +
21936 +long vfsub_splice_to(struct file *in, loff_t *ppos,
21937 +                    struct pipe_inode_info *pipe, size_t len,
21938 +                    unsigned int flags)
21939 +{
21940 +       long err;
21941 +
21942 +       lockdep_off();
21943 +       err = do_splice_to(in, ppos, pipe, len, flags);
21944 +       lockdep_on();
21945 +       if (err >= 0)
21946 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
21947 +       return err;
21948 +}
21949 +
21950 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
21951 +                      loff_t *ppos, size_t len, unsigned int flags)
21952 +{
21953 +       long err;
21954 +
21955 +       lockdep_off();
21956 +       err = do_splice_from(pipe, out, ppos, len, flags);
21957 +       lockdep_on();
21958 +       if (err >= 0)
21959 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
21960 +       return err;
21961 +}
21962 +
21963 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
21964 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
21965 +               struct file *h_file)
21966 +{
21967 +       int err;
21968 +       struct inode *h_inode;
21969 +
21970 +       h_inode = h_path->dentry->d_inode;
21971 +       if (!h_file) {
21972 +               err = mnt_want_write(h_path->mnt);
21973 +               if (err)
21974 +                       goto out;
21975 +               err = inode_permission(h_inode, MAY_WRITE);
21976 +               if (err)
21977 +                       goto out_mnt;
21978 +               err = get_write_access(h_inode);
21979 +               if (err)
21980 +                       goto out_mnt;
21981 +               err = break_lease(h_inode, vfsub_fmode_to_uint(FMODE_WRITE));
21982 +               if (err)
21983 +                       goto out_inode;
21984 +       }
21985 +
21986 +       err = locks_verify_truncate(h_inode, h_file, length);
21987 +       if (!err)
21988 +               err = security_path_truncate(h_path, length, attr);
21989 +       if (!err) {
21990 +               lockdep_off();
21991 +               err = do_truncate(h_path->dentry, length, attr, h_file);
21992 +               lockdep_on();
21993 +       }
21994 +
21995 + out_inode:
21996 +       if (!h_file)
21997 +               put_write_access(h_inode);
21998 + out_mnt:
21999 +       if (!h_file)
22000 +               mnt_drop_write(h_path->mnt);
22001 + out:
22002 +       return err;
22003 +}
22004 +
22005 +/* ---------------------------------------------------------------------- */
22006 +
22007 +struct au_vfsub_mkdir_args {
22008 +       int *errp;
22009 +       struct inode *dir;
22010 +       struct path *path;
22011 +       int mode;
22012 +};
22013 +
22014 +static void au_call_vfsub_mkdir(void *args)
22015 +{
22016 +       struct au_vfsub_mkdir_args *a = args;
22017 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
22018 +}
22019 +
22020 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
22021 +{
22022 +       int err, do_sio, wkq_err;
22023 +
22024 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
22025 +       if (!do_sio)
22026 +               err = vfsub_mkdir(dir, path, mode);
22027 +       else {
22028 +               struct au_vfsub_mkdir_args args = {
22029 +                       .errp   = &err,
22030 +                       .dir    = dir,
22031 +                       .path   = path,
22032 +                       .mode   = mode
22033 +               };
22034 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
22035 +               if (unlikely(wkq_err))
22036 +                       err = wkq_err;
22037 +       }
22038 +
22039 +       return err;
22040 +}
22041 +
22042 +struct au_vfsub_rmdir_args {
22043 +       int *errp;
22044 +       struct inode *dir;
22045 +       struct path *path;
22046 +};
22047 +
22048 +static void au_call_vfsub_rmdir(void *args)
22049 +{
22050 +       struct au_vfsub_rmdir_args *a = args;
22051 +       *a->errp = vfsub_rmdir(a->dir, a->path);
22052 +}
22053 +
22054 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
22055 +{
22056 +       int err, do_sio, wkq_err;
22057 +
22058 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
22059 +       if (!do_sio)
22060 +               err = vfsub_rmdir(dir, path);
22061 +       else {
22062 +               struct au_vfsub_rmdir_args args = {
22063 +                       .errp   = &err,
22064 +                       .dir    = dir,
22065 +                       .path   = path
22066 +               };
22067 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
22068 +               if (unlikely(wkq_err))
22069 +                       err = wkq_err;
22070 +       }
22071 +
22072 +       return err;
22073 +}
22074 +
22075 +/* ---------------------------------------------------------------------- */
22076 +
22077 +struct notify_change_args {
22078 +       int *errp;
22079 +       struct path *path;
22080 +       struct iattr *ia;
22081 +};
22082 +
22083 +static void call_notify_change(void *args)
22084 +{
22085 +       struct notify_change_args *a = args;
22086 +       struct inode *h_inode;
22087 +
22088 +       h_inode = a->path->dentry->d_inode;
22089 +       IMustLock(h_inode);
22090 +
22091 +       *a->errp = -EPERM;
22092 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
22093 +               lockdep_off();
22094 +               *a->errp = notify_change(a->path->dentry, a->ia);
22095 +               lockdep_on();
22096 +               if (!*a->errp)
22097 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
22098 +       }
22099 +       AuTraceErr(*a->errp);
22100 +}
22101 +
22102 +int vfsub_notify_change(struct path *path, struct iattr *ia)
22103 +{
22104 +       int err;
22105 +       struct notify_change_args args = {
22106 +               .errp   = &err,
22107 +               .path   = path,
22108 +               .ia     = ia
22109 +       };
22110 +
22111 +       call_notify_change(&args);
22112 +
22113 +       return err;
22114 +}
22115 +
22116 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
22117 +{
22118 +       int err, wkq_err;
22119 +       struct notify_change_args args = {
22120 +               .errp   = &err,
22121 +               .path   = path,
22122 +               .ia     = ia
22123 +       };
22124 +
22125 +       wkq_err = au_wkq_wait(call_notify_change, &args);
22126 +       if (unlikely(wkq_err))
22127 +               err = wkq_err;
22128 +
22129 +       return err;
22130 +}
22131 +
22132 +/* ---------------------------------------------------------------------- */
22133 +
22134 +struct unlink_args {
22135 +       int *errp;
22136 +       struct inode *dir;
22137 +       struct path *path;
22138 +};
22139 +
22140 +static void call_unlink(void *args)
22141 +{
22142 +       struct unlink_args *a = args;
22143 +       struct dentry *d = a->path->dentry;
22144 +       struct inode *h_inode;
22145 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
22146 +                                     && atomic_read(&d->d_count) == 1);
22147 +
22148 +       IMustLock(a->dir);
22149 +
22150 +       a->path->dentry = d->d_parent;
22151 +       *a->errp = security_path_unlink(a->path, d);
22152 +       a->path->dentry = d;
22153 +       if (unlikely(*a->errp))
22154 +               return;
22155 +
22156 +       if (!stop_sillyrename)
22157 +               dget(d);
22158 +       h_inode = d->d_inode;
22159 +       if (h_inode)
22160 +               atomic_inc(&h_inode->i_count);
22161 +
22162 +       lockdep_off();
22163 +       *a->errp = vfs_unlink(a->dir, d);
22164 +       lockdep_on();
22165 +       if (!*a->errp) {
22166 +               struct path tmp = {
22167 +                       .dentry = d->d_parent,
22168 +                       .mnt    = a->path->mnt
22169 +               };
22170 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
22171 +       }
22172 +
22173 +       if (!stop_sillyrename)
22174 +               dput(d);
22175 +       if (h_inode)
22176 +               iput(h_inode);
22177 +
22178 +       AuTraceErr(*a->errp);
22179 +}
22180 +
22181 +/*
22182 + * @dir: must be locked.
22183 + * @dentry: target dentry.
22184 + */
22185 +int vfsub_unlink(struct inode *dir, struct path *path, int force)
22186 +{
22187 +       int err;
22188 +       struct unlink_args args = {
22189 +               .errp   = &err,
22190 +               .dir    = dir,
22191 +               .path   = path
22192 +       };
22193 +
22194 +       if (!force)
22195 +               call_unlink(&args);
22196 +       else {
22197 +               int wkq_err;
22198 +
22199 +               wkq_err = au_wkq_wait(call_unlink, &args);
22200 +               if (unlikely(wkq_err))
22201 +                       err = wkq_err;
22202 +       }
22203 +
22204 +       return err;
22205 +}
22206 diff --git a/fs/aufs/vfsub.h b/fs/aufs/vfsub.h
22207 new file mode 100644
22208 index 0000000..d0172c7
22209 --- /dev/null
22210 +++ b/fs/aufs/vfsub.h
22211 @@ -0,0 +1,165 @@
22212 +/*
22213 + * Copyright (C) 2005-2009 Junjiro R. Okajima
22214 + *
22215 + * This program, aufs is free software; you can redistribute it and/or modify
22216 + * it under the terms of the GNU General Public License as published by
22217 + * the Free Software Foundation; either version 2 of the License, or
22218 + * (at your option) any later version.
22219 + */
22220 +
22221 +/*
22222 + * sub-routines for VFS
22223 + */
22224 +
22225 +#ifndef __AUFS_VFSUB_H__
22226 +#define __AUFS_VFSUB_H__
22227 +
22228 +#ifdef __KERNEL__
22229 +
22230 +#include <linux/fs.h>
22231 +#include <linux/fs_stack.h>
22232 +#include <linux/namei.h>
22233 +#include <linux/security.h>
22234 +#include <linux/splice.h>
22235 +
22236 +/* ---------------------------------------------------------------------- */
22237 +
22238 +/* lock subclass for lower inode */
22239 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
22240 +/* reduce? gave up. */
22241 +enum {
22242 +       AuLsc_I_Begin = I_MUTEX_QUOTA, /* 4 */
22243 +       AuLsc_I_PARENT,         /* lower inode, parent first */
22244 +       AuLsc_I_PARENT2,        /* copyup dirs */
22245 +       AuLsc_I_CHILD,
22246 +       AuLsc_I_CHILD2,
22247 +       AuLsc_I_End
22248 +};
22249 +
22250 +/* to debug easier, do not make them inlined functions */
22251 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
22252 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
22253 +
22254 +/* ---------------------------------------------------------------------- */
22255 +
22256 +static inline void vfsub_copy_inode_size(struct inode *inode,
22257 +                                        struct inode *h_inode)
22258 +{
22259 +       spin_lock(&inode->i_lock);
22260 +       fsstack_copy_inode_size(inode, h_inode);
22261 +       spin_unlock(&inode->i_lock);
22262 +}
22263 +
22264 +int vfsub_update_h_iattr(struct path *h_path, int *did);
22265 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
22266 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
22267 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
22268 +                                   int len);
22269 +struct dentry *vfsub_lookup_hash(struct nameidata *nd);
22270 +
22271 +/* ---------------------------------------------------------------------- */
22272 +
22273 +struct au_hinode;
22274 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
22275 +                                struct dentry *d2, struct au_hinode *hdir2);
22276 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
22277 +                        struct dentry *d2, struct au_hinode *hdir2);
22278 +
22279 +int vfsub_create(struct inode *dir, struct path *path, int mode);
22280 +int vfsub_symlink(struct inode *dir, struct path *path,
22281 +                 const char *symname);
22282 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
22283 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
22284 +              struct path *path);
22285 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
22286 +                struct inode *hdir, struct path *path);
22287 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
22288 +int vfsub_rmdir(struct inode *dir, struct path *path);
22289 +
22290 +/* ---------------------------------------------------------------------- */
22291 +
22292 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
22293 +                    loff_t *ppos);
22294 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
22295 +                       loff_t *ppos);
22296 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
22297 +                     loff_t *ppos);
22298 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
22299 +                     loff_t *ppos);
22300 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg);
22301 +
22302 +static inline void vfsub_file_accessed(struct file *h_file)
22303 +{
22304 +       file_accessed(h_file);
22305 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
22306 +}
22307 +
22308 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
22309 +                                    struct dentry *h_dentry)
22310 +{
22311 +       struct path h_path = {
22312 +               .dentry = h_dentry,
22313 +               .mnt    = h_mnt
22314 +       };
22315 +       touch_atime(h_mnt, h_dentry);
22316 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
22317 +}
22318 +
22319 +long vfsub_splice_to(struct file *in, loff_t *ppos,
22320 +                    struct pipe_inode_info *pipe, size_t len,
22321 +                    unsigned int flags);
22322 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
22323 +                      loff_t *ppos, size_t len, unsigned int flags);
22324 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
22325 +               struct file *h_file);
22326 +
22327 +/* ---------------------------------------------------------------------- */
22328 +
22329 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
22330 +{
22331 +       loff_t err;
22332 +
22333 +       lockdep_off();
22334 +       err = vfs_llseek(file, offset, origin);
22335 +       lockdep_on();
22336 +       return err;
22337 +}
22338 +
22339 +/* ---------------------------------------------------------------------- */
22340 +
22341 +/* dirty workaround for strict type of fmode_t */
22342 +union vfsub_fmu {
22343 +       fmode_t fm;
22344 +       unsigned int ui;
22345 +};
22346 +
22347 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
22348 +{
22349 +       union vfsub_fmu u = {
22350 +               .fm = fm
22351 +       };
22352 +
22353 +       BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
22354 +
22355 +       return u.ui;
22356 +}
22357 +
22358 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
22359 +{
22360 +       union vfsub_fmu u = {
22361 +               .ui = ui
22362 +       };
22363 +
22364 +       return u.fm;
22365 +}
22366 +
22367 +/* ---------------------------------------------------------------------- */
22368 +
22369 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
22370 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
22371 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia);
22372 +int vfsub_notify_change(struct path *path, struct iattr *ia);
22373 +int vfsub_unlink(struct inode *dir, struct path *path, int force);
22374 +
22375 +#endif /* __KERNEL__ */
22376 +#endif /* __AUFS_VFSUB_H__ */
22377 diff --git a/fs/aufs/wbr_policy.c b/fs/aufs/wbr_policy.c
22378 new file mode 100644
22379 index 0000000..436ebfc
22380 --- /dev/null
22381 +++ b/fs/aufs/wbr_policy.c
22382 @@ -0,0 +1,628 @@
22383 +/*
22384 + * Copyright (C) 2005-2009 Junjiro R. Okajima
22385 + *
22386 + * This program, aufs is free software; you can redistribute it and/or modify
22387 + * it under the terms of the GNU General Public License as published by
22388 + * the Free Software Foundation; either version 2 of the License, or
22389 + * (at your option) any later version.
22390 + */
22391 +
22392 +/*
22393 + * policies for selecting one among multiple writable branches
22394 + */
22395 +
22396 +#include <linux/statfs.h>
22397 +#include "aufs.h"
22398 +
22399 +/* subset of cpup_attr() */
22400 +static noinline_for_stack
22401 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
22402 +{
22403 +       int err, sbits;
22404 +       struct iattr ia;
22405 +       struct inode *h_isrc;
22406 +
22407 +       h_isrc = h_src->d_inode;
22408 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
22409 +       ia.ia_mode = h_isrc->i_mode;
22410 +       ia.ia_uid = h_isrc->i_uid;
22411 +       ia.ia_gid = h_isrc->i_gid;
22412 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
22413 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc);
22414 +       err = vfsub_sio_notify_change(h_path, &ia);
22415 +
22416 +       /* is this nfs only? */
22417 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
22418 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
22419 +               ia.ia_mode = h_isrc->i_mode;
22420 +               err = vfsub_sio_notify_change(h_path, &ia);
22421 +       }
22422 +
22423 +       return err;
22424 +}
22425 +
22426 +#define AuCpdown_PARENT_OPQ    1
22427 +#define AuCpdown_WHED          (1 << 1)
22428 +#define AuCpdown_MADE_DIR      (1 << 2)
22429 +#define AuCpdown_DIROPQ                (1 << 3)
22430 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
22431 +#define au_fset_cpdown(flags, name)    { (flags) |= AuCpdown_##name; }
22432 +#define au_fclr_cpdown(flags, name)    { (flags) &= ~AuCpdown_##name; }
22433 +
22434 +struct au_cpdown_dir_args {
22435 +       struct dentry *parent;
22436 +       unsigned int flags;
22437 +};
22438 +
22439 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
22440 +                            struct au_cpdown_dir_args *a)
22441 +{
22442 +       int err;
22443 +       struct dentry *opq_dentry;
22444 +
22445 +       opq_dentry = au_diropq_create(dentry, bdst);
22446 +       err = PTR_ERR(opq_dentry);
22447 +       if (IS_ERR(opq_dentry))
22448 +               goto out;
22449 +       dput(opq_dentry);
22450 +       au_fset_cpdown(a->flags, DIROPQ);
22451 +
22452 + out:
22453 +       return err;
22454 +}
22455 +
22456 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
22457 +                           struct inode *dir, aufs_bindex_t bdst)
22458 +{
22459 +       int err;
22460 +       struct path h_path;
22461 +       struct au_branch *br;
22462 +
22463 +       br = au_sbr(dentry->d_sb, bdst);
22464 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
22465 +       err = PTR_ERR(h_path.dentry);
22466 +       if (IS_ERR(h_path.dentry))
22467 +               goto out;
22468 +
22469 +       err = 0;
22470 +       if (h_path.dentry->d_inode) {
22471 +               h_path.mnt = br->br_mnt;
22472 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
22473 +                                         dentry);
22474 +       }
22475 +       dput(h_path.dentry);
22476 +
22477 + out:
22478 +       return err;
22479 +}
22480 +
22481 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
22482 +                        struct dentry *h_parent, void *arg)
22483 +{
22484 +       int err, rerr;
22485 +       aufs_bindex_t bend, bopq, bstart;
22486 +       unsigned char parent_opq;
22487 +       struct path h_path;
22488 +       struct dentry *parent;
22489 +       struct inode *h_dir, *h_inode, *inode, *dir;
22490 +       struct au_cpdown_dir_args *args = arg;
22491 +
22492 +       bstart = au_dbstart(dentry);
22493 +       /* dentry is di-locked */
22494 +       parent = dget_parent(dentry);
22495 +       dir = parent->d_inode;
22496 +       h_dir = h_parent->d_inode;
22497 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
22498 +       IMustLock(h_dir);
22499 +
22500 +       err = au_lkup_neg(dentry, bdst);
22501 +       if (unlikely(err < 0))
22502 +               goto out;
22503 +       h_path.dentry = au_h_dptr(dentry, bdst);
22504 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
22505 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
22506 +                             S_IRWXU | S_IRUGO | S_IXUGO);
22507 +       if (unlikely(err))
22508 +               goto out_put;
22509 +       au_fset_cpdown(args->flags, MADE_DIR);
22510 +
22511 +       bend = au_dbend(dentry);
22512 +       bopq = au_dbdiropq(dentry);
22513 +       au_fclr_cpdown(args->flags, WHED);
22514 +       au_fclr_cpdown(args->flags, DIROPQ);
22515 +       if (au_dbwh(dentry) == bdst)
22516 +               au_fset_cpdown(args->flags, WHED);
22517 +       if (!au_ftest_cpdown(args->flags, PARENT_OPQ) && bopq <= bdst)
22518 +               au_fset_cpdown(args->flags, PARENT_OPQ);
22519 +       parent_opq = (au_ftest_cpdown(args->flags, PARENT_OPQ)
22520 +                     && args->parent == dentry);
22521 +       h_inode = h_path.dentry->d_inode;
22522 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
22523 +       if (au_ftest_cpdown(args->flags, WHED)) {
22524 +               err = au_cpdown_dir_opq(dentry, bdst, args);
22525 +               if (unlikely(err)) {
22526 +                       mutex_unlock(&h_inode->i_mutex);
22527 +                       goto out_dir;
22528 +               }
22529 +       }
22530 +
22531 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
22532 +       mutex_unlock(&h_inode->i_mutex);
22533 +       if (unlikely(err))
22534 +               goto out_opq;
22535 +
22536 +       if (au_ftest_cpdown(args->flags, WHED)) {
22537 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
22538 +               if (unlikely(err))
22539 +                       goto out_opq;
22540 +       }
22541 +
22542 +       inode = dentry->d_inode;
22543 +       if (au_ibend(inode) < bdst)
22544 +               au_set_ibend(inode, bdst);
22545 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
22546 +                     au_hi_flags(inode, /*isdir*/1));
22547 +       goto out; /* success */
22548 +
22549 +       /* revert */
22550 + out_opq:
22551 +       if (au_ftest_cpdown(args->flags, DIROPQ)) {
22552 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
22553 +               rerr = au_diropq_remove(dentry, bdst);
22554 +               mutex_unlock(&h_inode->i_mutex);
22555 +               if (unlikely(rerr)) {
22556 +                       AuIOErr("failed removing diropq for %.*s b%d (%d)\n",
22557 +                               AuDLNPair(dentry), bdst, rerr);
22558 +                       err = -EIO;
22559 +                       goto out;
22560 +               }
22561 +       }
22562 + out_dir:
22563 +       if (au_ftest_cpdown(args->flags, MADE_DIR)) {
22564 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
22565 +               if (unlikely(rerr)) {
22566 +                       AuIOErr("failed removing %.*s b%d (%d)\n",
22567 +                               AuDLNPair(dentry), bdst, rerr);
22568 +                       err = -EIO;
22569 +               }
22570 +       }
22571 + out_put:
22572 +       au_set_h_dptr(dentry, bdst, NULL);
22573 +       if (au_dbend(dentry) == bdst)
22574 +               au_update_dbend(dentry);
22575 + out:
22576 +       dput(parent);
22577 +       return err;
22578 +}
22579 +
22580 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
22581 +{
22582 +       int err;
22583 +       struct au_cpdown_dir_args args = {
22584 +               .parent = dget_parent(dentry),
22585 +               .flags  = 0
22586 +       };
22587 +
22588 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &args);
22589 +       dput(args.parent);
22590 +
22591 +       return err;
22592 +}
22593 +
22594 +/* ---------------------------------------------------------------------- */
22595 +
22596 +/* policies for create */
22597 +
22598 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
22599 +{
22600 +       for (; bindex >= 0; bindex--)
22601 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
22602 +                       return bindex;
22603 +       return -EROFS;
22604 +}
22605 +
22606 +/* top down parent */
22607 +static int au_wbr_create_tdp(struct dentry *dentry, int isdir __maybe_unused)
22608 +{
22609 +       int err;
22610 +       aufs_bindex_t bstart, bindex;
22611 +       struct super_block *sb;
22612 +       struct dentry *parent, *h_parent;
22613 +
22614 +       sb = dentry->d_sb;
22615 +       bstart = au_dbstart(dentry);
22616 +       err = bstart;
22617 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
22618 +               goto out;
22619 +
22620 +       err = -EROFS;
22621 +       parent = dget_parent(dentry);
22622 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
22623 +               h_parent = au_h_dptr(parent, bindex);
22624 +               if (!h_parent || !h_parent->d_inode)
22625 +                       continue;
22626 +
22627 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
22628 +                       err = bindex;
22629 +                       break;
22630 +               }
22631 +       }
22632 +       dput(parent);
22633 +
22634 +       /* bottom up here */
22635 +       if (unlikely(err < 0))
22636 +               err = au_wbr_bu(sb, bstart - 1);
22637 +
22638 + out:
22639 +       AuDbg("b%d\n", err);
22640 +       return err;
22641 +}
22642 +
22643 +/* ---------------------------------------------------------------------- */
22644 +
22645 +/* an exception for the policy other than tdp */
22646 +static int au_wbr_create_exp(struct dentry *dentry)
22647 +{
22648 +       int err;
22649 +       aufs_bindex_t bwh, bdiropq;
22650 +       struct dentry *parent;
22651 +
22652 +       err = -1;
22653 +       bwh = au_dbwh(dentry);
22654 +       parent = dget_parent(dentry);
22655 +       bdiropq = au_dbdiropq(parent);
22656 +       if (bwh >= 0) {
22657 +               if (bdiropq >= 0)
22658 +                       err = min(bdiropq, bwh);
22659 +               else
22660 +                       err = bwh;
22661 +               AuDbg("%d\n", err);
22662 +       } else if (bdiropq >= 0) {
22663 +               err = bdiropq;
22664 +               AuDbg("%d\n", err);
22665 +       }
22666 +       dput(parent);
22667 +
22668 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
22669 +               err = -1;
22670 +
22671 +       AuDbg("%d\n", err);
22672 +       return err;
22673 +}
22674 +
22675 +/* ---------------------------------------------------------------------- */
22676 +
22677 +/* round robin */
22678 +static int au_wbr_create_init_rr(struct super_block *sb)
22679 +{
22680 +       int err;
22681 +
22682 +       err = au_wbr_bu(sb, au_sbend(sb));
22683 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
22684 +
22685 +       AuDbg("b%d\n", err);
22686 +       return err;
22687 +}
22688 +
22689 +static int au_wbr_create_rr(struct dentry *dentry, int isdir)
22690 +{
22691 +       int err, nbr;
22692 +       unsigned int u;
22693 +       aufs_bindex_t bindex, bend;
22694 +       struct super_block *sb;
22695 +       atomic_t *next;
22696 +
22697 +       err = au_wbr_create_exp(dentry);
22698 +       if (err >= 0)
22699 +               goto out;
22700 +
22701 +       sb = dentry->d_sb;
22702 +       next = &au_sbi(sb)->si_wbr_rr_next;
22703 +       bend = au_sbend(sb);
22704 +       nbr = bend + 1;
22705 +       for (bindex = 0; bindex <= bend; bindex++) {
22706 +               if (!isdir) {
22707 +                       err = atomic_dec_return(next) + 1;
22708 +                       /* modulo for 0 is meaningless */
22709 +                       if (unlikely(!err))
22710 +                               err = atomic_dec_return(next) + 1;
22711 +               } else
22712 +                       err = atomic_read(next);
22713 +               AuDbg("%d\n", err);
22714 +               u = err;
22715 +               err = u % nbr;
22716 +               AuDbg("%d\n", err);
22717 +               if (!au_br_rdonly(au_sbr(sb, err)))
22718 +                       break;
22719 +               err = -EROFS;
22720 +       }
22721 +
22722 + out:
22723 +       AuDbg("%d\n", err);
22724 +       return err;
22725 +}
22726 +
22727 +/* ---------------------------------------------------------------------- */
22728 +
22729 +/* most free space */
22730 +static void au_mfs(struct dentry *dentry)
22731 +{
22732 +       struct super_block *sb;
22733 +       struct au_branch *br;
22734 +       struct au_wbr_mfs *mfs;
22735 +       aufs_bindex_t bindex, bend;
22736 +       int err;
22737 +       unsigned long long b, bavail;
22738 +       /* reduce the stack usage */
22739 +       struct kstatfs *st;
22740 +
22741 +       st = kmalloc(sizeof(*st), GFP_NOFS);
22742 +       if (unlikely(!st)) {
22743 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
22744 +               return;
22745 +       }
22746 +
22747 +       bavail = 0;
22748 +       sb = dentry->d_sb;
22749 +       mfs = &au_sbi(sb)->si_wbr_mfs;
22750 +       mfs->mfs_bindex = -EROFS;
22751 +       mfs->mfsrr_bytes = 0;
22752 +       bend = au_sbend(sb);
22753 +       for (bindex = 0; bindex <= bend; bindex++) {
22754 +               br = au_sbr(sb, bindex);
22755 +               if (au_br_rdonly(br))
22756 +                       continue;
22757 +
22758 +               /* sb->s_root for NFS is unreliable */
22759 +               err = vfs_statfs(br->br_mnt->mnt_root, st);
22760 +               if (unlikely(err)) {
22761 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
22762 +                       continue;
22763 +               }
22764 +
22765 +               /* when the available size is equal, select the lower one */
22766 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
22767 +                            || sizeof(b) < sizeof(st->f_bsize));
22768 +               b = st->f_bavail * st->f_bsize;
22769 +               br->br_wbr->wbr_bytes = b;
22770 +               if (b >= bavail) {
22771 +                       bavail = b;
22772 +                       mfs->mfs_bindex = bindex;
22773 +                       mfs->mfs_jiffy = jiffies;
22774 +               }
22775 +       }
22776 +
22777 +       mfs->mfsrr_bytes = bavail;
22778 +       AuDbg("b%d\n", mfs->mfs_bindex);
22779 +       kfree(st);
22780 +}
22781 +
22782 +static int au_wbr_create_mfs(struct dentry *dentry, int isdir __maybe_unused)
22783 +{
22784 +       int err;
22785 +       struct super_block *sb;
22786 +       struct au_wbr_mfs *mfs;
22787 +
22788 +       err = au_wbr_create_exp(dentry);
22789 +       if (err >= 0)
22790 +               goto out;
22791 +
22792 +       sb = dentry->d_sb;
22793 +       mfs = &au_sbi(sb)->si_wbr_mfs;
22794 +       mutex_lock(&mfs->mfs_lock);
22795 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
22796 +           || mfs->mfs_bindex < 0
22797 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
22798 +               au_mfs(dentry);
22799 +       mutex_unlock(&mfs->mfs_lock);
22800 +       err = mfs->mfs_bindex;
22801 +
22802 + out:
22803 +       AuDbg("b%d\n", err);
22804 +       return err;
22805 +}
22806 +
22807 +static int au_wbr_create_init_mfs(struct super_block *sb)
22808 +{
22809 +       struct au_wbr_mfs *mfs;
22810 +
22811 +       mfs = &au_sbi(sb)->si_wbr_mfs;
22812 +       mutex_init(&mfs->mfs_lock);
22813 +       mfs->mfs_jiffy = 0;
22814 +       mfs->mfs_bindex = -EROFS;
22815 +
22816 +       return 0;
22817 +}
22818 +
22819 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
22820 +{
22821 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
22822 +       return 0;
22823 +}
22824 +
22825 +/* ---------------------------------------------------------------------- */
22826 +
22827 +/* most free space and then round robin */
22828 +static int au_wbr_create_mfsrr(struct dentry *dentry, int isdir)
22829 +{
22830 +       int err;
22831 +       struct au_wbr_mfs *mfs;
22832 +
22833 +       err = au_wbr_create_mfs(dentry, isdir);
22834 +       if (err >= 0) {
22835 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
22836 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
22837 +                       err = au_wbr_create_rr(dentry, isdir);
22838 +       }
22839 +
22840 +       AuDbg("b%d\n", err);
22841 +       return err;
22842 +}
22843 +
22844 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
22845 +{
22846 +       int err;
22847 +
22848 +       au_wbr_create_init_mfs(sb); /* ignore */
22849 +       err = au_wbr_create_init_rr(sb);
22850 +
22851 +       return err;
22852 +}
22853 +
22854 +/* ---------------------------------------------------------------------- */
22855 +
22856 +/* top down parent and most free space */
22857 +static int au_wbr_create_pmfs(struct dentry *dentry, int isdir)
22858 +{
22859 +       int err, e2;
22860 +       unsigned long long b;
22861 +       aufs_bindex_t bindex, bstart, bend;
22862 +       struct super_block *sb;
22863 +       struct dentry *parent, *h_parent;
22864 +       struct au_branch *br;
22865 +
22866 +       err = au_wbr_create_tdp(dentry, isdir);
22867 +       if (unlikely(err < 0))
22868 +               goto out;
22869 +       parent = dget_parent(dentry);
22870 +       bstart = au_dbstart(parent);
22871 +       bend = au_dbtaildir(parent);
22872 +       if (bstart == bend)
22873 +               goto out_parent; /* success */
22874 +
22875 +       e2 = au_wbr_create_mfs(dentry, isdir);
22876 +       if (e2 < 0)
22877 +               goto out_parent; /* success */
22878 +
22879 +       /* when the available size is equal, select upper one */
22880 +       sb = dentry->d_sb;
22881 +       br = au_sbr(sb, err);
22882 +       b = br->br_wbr->wbr_bytes;
22883 +       AuDbg("b%d, %llu\n", err, b);
22884 +
22885 +       for (bindex = bstart; bindex <= bend; bindex++) {
22886 +               h_parent = au_h_dptr(parent, bindex);
22887 +               if (!h_parent || !h_parent->d_inode)
22888 +                       continue;
22889 +
22890 +               br = au_sbr(sb, bindex);
22891 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
22892 +                       b = br->br_wbr->wbr_bytes;
22893 +                       err = bindex;
22894 +                       AuDbg("b%d, %llu\n", err, b);
22895 +               }
22896 +       }
22897 +
22898 + out_parent:
22899 +       dput(parent);
22900 + out:
22901 +       AuDbg("b%d\n", err);
22902 +       return err;
22903 +}
22904 +
22905 +/* ---------------------------------------------------------------------- */
22906 +
22907 +/* policies for copyup */
22908 +
22909 +/* top down parent */
22910 +static int au_wbr_copyup_tdp(struct dentry *dentry)
22911 +{
22912 +       return au_wbr_create_tdp(dentry, /*isdir, anything is ok*/0);
22913 +}
22914 +
22915 +/* bottom up parent */
22916 +static int au_wbr_copyup_bup(struct dentry *dentry)
22917 +{
22918 +       int err;
22919 +       aufs_bindex_t bindex, bstart;
22920 +       struct dentry *parent, *h_parent;
22921 +       struct super_block *sb;
22922 +
22923 +       err = -EROFS;
22924 +       sb = dentry->d_sb;
22925 +       parent = dget_parent(dentry);
22926 +       bstart = au_dbstart(parent);
22927 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
22928 +               h_parent = au_h_dptr(parent, bindex);
22929 +               if (!h_parent || !h_parent->d_inode)
22930 +                       continue;
22931 +
22932 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
22933 +                       err = bindex;
22934 +                       break;
22935 +               }
22936 +       }
22937 +       dput(parent);
22938 +
22939 +       /* bottom up here */
22940 +       if (unlikely(err < 0))
22941 +               err = au_wbr_bu(sb, bstart - 1);
22942 +
22943 +       AuDbg("b%d\n", err);
22944 +       return err;
22945 +}
22946 +
22947 +/* bottom up */
22948 +static int au_wbr_copyup_bu(struct dentry *dentry)
22949 +{
22950 +       int err;
22951 +
22952 +       err = au_wbr_bu(dentry->d_sb, au_dbstart(dentry));
22953 +
22954 +       AuDbg("b%d\n", err);
22955 +       return err;
22956 +}
22957 +
22958 +/* ---------------------------------------------------------------------- */
22959 +
22960 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
22961 +       [AuWbrCopyup_TDP] = {
22962 +               .copyup = au_wbr_copyup_tdp
22963 +       },
22964 +       [AuWbrCopyup_BUP] = {
22965 +               .copyup = au_wbr_copyup_bup
22966 +       },
22967 +       [AuWbrCopyup_BU] = {
22968 +               .copyup = au_wbr_copyup_bu
22969 +       }
22970 +};
22971 +
22972 +struct au_wbr_create_operations au_wbr_create_ops[] = {
22973 +       [AuWbrCreate_TDP] = {
22974 +               .create = au_wbr_create_tdp
22975 +       },
22976 +       [AuWbrCreate_RR] = {
22977 +               .create = au_wbr_create_rr,
22978 +               .init   = au_wbr_create_init_rr
22979 +       },
22980 +       [AuWbrCreate_MFS] = {
22981 +               .create = au_wbr_create_mfs,
22982 +               .init   = au_wbr_create_init_mfs,
22983 +               .fin    = au_wbr_create_fin_mfs
22984 +       },
22985 +       [AuWbrCreate_MFSV] = {
22986 +               .create = au_wbr_create_mfs,
22987 +               .init   = au_wbr_create_init_mfs,
22988 +               .fin    = au_wbr_create_fin_mfs
22989 +       },
22990 +       [AuWbrCreate_MFSRR] = {
22991 +               .create = au_wbr_create_mfsrr,
22992 +               .init   = au_wbr_create_init_mfsrr,
22993 +               .fin    = au_wbr_create_fin_mfs
22994 +       },
22995 +       [AuWbrCreate_MFSRRV] = {
22996 +               .create = au_wbr_create_mfsrr,
22997 +               .init   = au_wbr_create_init_mfsrr,
22998 +               .fin    = au_wbr_create_fin_mfs
22999 +       },
23000 +       [AuWbrCreate_PMFS] = {
23001 +               .create = au_wbr_create_pmfs,
23002 +               .init   = au_wbr_create_init_mfs,
23003 +               .fin    = au_wbr_create_fin_mfs
23004 +       },
23005 +       [AuWbrCreate_PMFSV] = {
23006 +               .create = au_wbr_create_pmfs,
23007 +               .init   = au_wbr_create_init_mfs,
23008 +               .fin    = au_wbr_create_fin_mfs
23009 +       }
23010 +};
23011 diff --git a/fs/aufs/whout.c b/fs/aufs/whout.c
23012 new file mode 100644
23013 index 0000000..a45e126
23014 --- /dev/null
23015 +++ b/fs/aufs/whout.c
23016 @@ -0,0 +1,1030 @@
23017 +/*
23018 + * Copyright (C) 2005-2009 Junjiro R. Okajima
23019 + *
23020 + * This program, aufs is free software; you can redistribute it and/or modify
23021 + * it under the terms of the GNU General Public License as published by
23022 + * the Free Software Foundation; either version 2 of the License, or
23023 + * (at your option) any later version.
23024 + */
23025 +
23026 +/*
23027 + * whiteout for logical deletion and opaque directory
23028 + */
23029 +
23030 +#include <linux/fs.h>
23031 +#include "aufs.h"
23032 +
23033 +#define WH_MASK                        S_IRUGO
23034 +
23035 +/*
23036 + * If a directory contains this file, then it is opaque.  We start with the
23037 + * .wh. flag so that it is blocked by lookup.
23038 + */
23039 +static struct qstr diropq_name = {
23040 +       .name = AUFS_WH_DIROPQ,
23041 +       .len = sizeof(AUFS_WH_DIROPQ) - 1
23042 +};
23043 +
23044 +/*
23045 + * generate whiteout name, which is NOT terminated by NULL.
23046 + * @name: original d_name.name
23047 + * @len: original d_name.len
23048 + * @wh: whiteout qstr
23049 + * returns zero when succeeds, otherwise error.
23050 + * succeeded value as wh->name should be freed by kfree().
23051 + */
23052 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
23053 +{
23054 +       char *p;
23055 +
23056 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
23057 +               return -ENAMETOOLONG;
23058 +
23059 +       wh->len = name->len + AUFS_WH_PFX_LEN;
23060 +       p = kmalloc(wh->len, GFP_NOFS);
23061 +       wh->name = p;
23062 +       if (p) {
23063 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
23064 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
23065 +               /* smp_mb(); */
23066 +               return 0;
23067 +       }
23068 +       return -ENOMEM;
23069 +}
23070 +
23071 +/* ---------------------------------------------------------------------- */
23072 +
23073 +/*
23074 + * test if the @wh_name exists under @h_parent.
23075 + * @try_sio specifies the necessary of super-io.
23076 + */
23077 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
23078 +              struct au_branch *br, int try_sio)
23079 +{
23080 +       int err;
23081 +       struct dentry *wh_dentry;
23082 +       struct inode *h_dir;
23083 +
23084 +       h_dir = h_parent->d_inode;
23085 +       if (!try_sio)
23086 +               wh_dentry = au_lkup_one(wh_name, h_parent, br, /*nd*/NULL);
23087 +       else
23088 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
23089 +       err = PTR_ERR(wh_dentry);
23090 +       if (IS_ERR(wh_dentry))
23091 +               goto out;
23092 +
23093 +       err = 0;
23094 +       if (!wh_dentry->d_inode)
23095 +               goto out_wh; /* success */
23096 +
23097 +       err = 1;
23098 +       if (S_ISREG(wh_dentry->d_inode->i_mode))
23099 +               goto out_wh; /* success */
23100 +
23101 +       err = -EIO;
23102 +       AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
23103 +               AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
23104 +
23105 + out_wh:
23106 +       dput(wh_dentry);
23107 + out:
23108 +       return err;
23109 +}
23110 +
23111 +/*
23112 + * test if the @h_dentry sets opaque or not.
23113 + */
23114 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
23115 +{
23116 +       int err;
23117 +       struct inode *h_dir;
23118 +
23119 +       h_dir = h_dentry->d_inode;
23120 +       err = au_wh_test(h_dentry, &diropq_name, br,
23121 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
23122 +       return err;
23123 +}
23124 +
23125 +/*
23126 + * returns a negative dentry whose name is unique and temporary.
23127 + */
23128 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
23129 +                            struct qstr *prefix)
23130 +{
23131 +#define HEX_LEN        4
23132 +       struct dentry *dentry;
23133 +       int i;
23134 +       char defname[AUFS_WH_PFX_LEN * 2 + DNAME_INLINE_LEN_MIN + 1
23135 +                    + HEX_LEN + 1], *name, *p;
23136 +       static unsigned short cnt;
23137 +       struct qstr qs;
23138 +
23139 +       name = defname;
23140 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN_MIN + prefix->len - 1;
23141 +       if (unlikely(prefix->len > DNAME_INLINE_LEN_MIN)) {
23142 +               dentry = ERR_PTR(-ENAMETOOLONG);
23143 +               if (unlikely(qs.len >= PATH_MAX))
23144 +                       goto out;
23145 +               dentry = ERR_PTR(-ENOMEM);
23146 +               name = kmalloc(qs.len + 1, GFP_NOFS);
23147 +               if (unlikely(!name))
23148 +                       goto out;
23149 +       }
23150 +
23151 +       /* doubly whiteout-ed */
23152 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
23153 +       p = name + AUFS_WH_PFX_LEN * 2;
23154 +       memcpy(p, prefix->name, prefix->len);
23155 +       p += prefix->len;
23156 +       *p++ = '.';
23157 +       AuDebugOn(name + qs.len + 1 - p <= HEX_LEN);
23158 +
23159 +       qs.name = name;
23160 +       for (i = 0; i < 3; i++) {
23161 +               sprintf(p, "%.*d", HEX_LEN, cnt++);
23162 +               dentry = au_sio_lkup_one(&qs, h_parent, br);
23163 +               if (IS_ERR(dentry) || !dentry->d_inode)
23164 +                       goto out_name;
23165 +               dput(dentry);
23166 +       }
23167 +       /* AuWarn("could not get random name\n"); */
23168 +       dentry = ERR_PTR(-EEXIST);
23169 +       AuDbg("%.*s\n", AuLNPair(&qs));
23170 +       BUG();
23171 +
23172 + out_name:
23173 +       if (name != defname)
23174 +               kfree(name);
23175 + out:
23176 +       return dentry;
23177 +#undef HEX_LEN
23178 +}
23179 +
23180 +/*
23181 + * rename the @h_dentry on @br to the whiteouted temporary name.
23182 + */
23183 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
23184 +{
23185 +       int err;
23186 +       struct path h_path = {
23187 +               .mnt = br->br_mnt
23188 +       };
23189 +       struct inode *h_dir;
23190 +       struct dentry *h_parent;
23191 +
23192 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
23193 +       h_dir = h_parent->d_inode;
23194 +       IMustLock(h_dir);
23195 +
23196 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
23197 +       err = PTR_ERR(h_path.dentry);
23198 +       if (IS_ERR(h_path.dentry))
23199 +               goto out;
23200 +
23201 +       /* under the same dir, no need to lock_rename() */
23202 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
23203 +       AuTraceErr(err);
23204 +       dput(h_path.dentry);
23205 +
23206 + out:
23207 +       return err;
23208 +}
23209 +
23210 +/* ---------------------------------------------------------------------- */
23211 +/*
23212 + * functions for removing a whiteout
23213 + */
23214 +
23215 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
23216 +{
23217 +       int force;
23218 +
23219 +       /*
23220 +        * forces superio when the dir has a sticky bit.
23221 +        * this may be a violation of unix fs semantics.
23222 +        */
23223 +       force = (h_dir->i_mode & S_ISVTX)
23224 +               && h_path->dentry->d_inode->i_uid != current_fsuid();
23225 +       return vfsub_unlink(h_dir, h_path, force);
23226 +}
23227 +
23228 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
23229 +                       struct dentry *dentry)
23230 +{
23231 +       int err;
23232 +
23233 +       err = do_unlink_wh(h_dir, h_path);
23234 +       if (!err && dentry)
23235 +               au_set_dbwh(dentry, -1);
23236 +
23237 +       return err;
23238 +}
23239 +
23240 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
23241 +                         struct au_branch *br)
23242 +{
23243 +       int err;
23244 +       struct path h_path = {
23245 +               .mnt = br->br_mnt
23246 +       };
23247 +
23248 +       err = 0;
23249 +       h_path.dentry = au_lkup_one(wh, h_parent, br, /*nd*/NULL);
23250 +       if (IS_ERR(h_path.dentry))
23251 +               err = PTR_ERR(h_path.dentry);
23252 +       else {
23253 +               if (h_path.dentry->d_inode
23254 +                   && S_ISREG(h_path.dentry->d_inode->i_mode))
23255 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
23256 +               dput(h_path.dentry);
23257 +       }
23258 +
23259 +       return err;
23260 +}
23261 +
23262 +/* ---------------------------------------------------------------------- */
23263 +/*
23264 + * initialize/clean whiteout for a branch
23265 + */
23266 +
23267 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
23268 +                       const int isdir)
23269 +{
23270 +       int err;
23271 +
23272 +       if (!whpath->dentry->d_inode)
23273 +               return;
23274 +
23275 +       err = mnt_want_write(whpath->mnt);
23276 +       if (!err) {
23277 +               if (isdir)
23278 +                       err = vfsub_rmdir(h_dir, whpath);
23279 +               else
23280 +                       err = vfsub_unlink(h_dir, whpath, /*force*/0);
23281 +               mnt_drop_write(whpath->mnt);
23282 +       }
23283 +       if (unlikely(err))
23284 +               AuWarn("failed removing %.*s (%d), ignored.\n",
23285 +                      AuDLNPair(whpath->dentry), err);
23286 +}
23287 +
23288 +static int test_linkable(struct dentry *h_root)
23289 +{
23290 +       struct inode *h_dir = h_root->d_inode;
23291 +
23292 +       if (h_dir->i_op->link)
23293 +               return 0;
23294 +
23295 +       AuErr("%.*s (%s) doesn't support link(2), use noplink and rw+nolwh\n",
23296 +             AuDLNPair(h_root), au_sbtype(h_root->d_sb));
23297 +       return -ENOSYS;
23298 +}
23299 +
23300 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
23301 +static int au_whdir(struct inode *h_dir, struct path *path)
23302 +{
23303 +       int err;
23304 +
23305 +       err = -EEXIST;
23306 +       if (!path->dentry->d_inode) {
23307 +               int mode = S_IRWXU;
23308 +
23309 +               if (au_test_nfs(path->dentry->d_sb))
23310 +                       mode |= S_IXUGO;
23311 +               err = mnt_want_write(path->mnt);
23312 +               if (!err) {
23313 +                       err = vfsub_mkdir(h_dir, path, mode);
23314 +                       mnt_drop_write(path->mnt);
23315 +               }
23316 +       } else if (S_ISDIR(path->dentry->d_inode->i_mode))
23317 +               err = 0;
23318 +       else
23319 +               AuErr("unknown %.*s exists\n", AuDLNPair(path->dentry));
23320 +
23321 +       return err;
23322 +}
23323 +
23324 +struct au_wh_base {
23325 +       const struct qstr *name;
23326 +       struct dentry *dentry;
23327 +};
23328 +
23329 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
23330 +                         struct path *h_path)
23331 +{
23332 +       h_path->dentry = base[AuBrWh_BASE].dentry;
23333 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
23334 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
23335 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
23336 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
23337 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
23338 +}
23339 +
23340 +/*
23341 + * returns tri-state,
23342 + * minus: error, caller should print the mesage
23343 + * zero: succuess
23344 + * plus: error, caller should NOT print the mesage
23345 + */
23346 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
23347 +                               int do_plink, struct au_wh_base base[],
23348 +                               struct path *h_path)
23349 +{
23350 +       int err;
23351 +       struct inode *h_dir;
23352 +
23353 +       h_dir = h_root->d_inode;
23354 +       h_path->dentry = base[AuBrWh_BASE].dentry;
23355 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
23356 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
23357 +       if (do_plink) {
23358 +               err = test_linkable(h_root);
23359 +               if (unlikely(err)) {
23360 +                       err = 1;
23361 +                       goto out;
23362 +               }
23363 +
23364 +               err = au_whdir(h_dir, h_path);
23365 +               if (unlikely(err))
23366 +                       goto out;
23367 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
23368 +       } else
23369 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
23370 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
23371 +       err = au_whdir(h_dir, h_path);
23372 +       if (unlikely(err))
23373 +               goto out;
23374 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
23375 +
23376 + out:
23377 +       return err;
23378 +}
23379 +
23380 +/*
23381 + * for the moment, aufs supports the branch filesystem which does not support
23382 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
23383 + * copyup failed. finally, such filesystem will not be used as the writable
23384 + * branch.
23385 + *
23386 + * returns tri-state, see above.
23387 + */
23388 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
23389 +                        int do_plink, struct au_wh_base base[],
23390 +                        struct path *h_path)
23391 +{
23392 +       int err;
23393 +       struct inode *h_dir;
23394 +
23395 +       err = test_linkable(h_root);
23396 +       if (unlikely(err)) {
23397 +               err = 1;
23398 +               goto out;
23399 +       }
23400 +
23401 +       /*
23402 +        * todo: should this create be done in /sbin/mount.aufs helper?
23403 +        */
23404 +       err = -EEXIST;
23405 +       h_dir = h_root->d_inode;
23406 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
23407 +               err = mnt_want_write(h_path->mnt);
23408 +               if (!err) {
23409 +                       h_path->dentry = base[AuBrWh_BASE].dentry;
23410 +                       err = vfsub_create(h_dir, h_path, WH_MASK);
23411 +                       mnt_drop_write(h_path->mnt);
23412 +               }
23413 +       } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
23414 +               err = 0;
23415 +       else
23416 +               AuErr("unknown %.*s/%.*s exists\n",
23417 +                     AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
23418 +       if (unlikely(err))
23419 +               goto out;
23420 +
23421 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
23422 +       if (do_plink) {
23423 +               err = au_whdir(h_dir, h_path);
23424 +               if (unlikely(err))
23425 +                       goto out;
23426 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
23427 +       } else
23428 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
23429 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
23430 +
23431 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
23432 +       err = au_whdir(h_dir, h_path);
23433 +       if (unlikely(err))
23434 +               goto out;
23435 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
23436 +
23437 + out:
23438 +       return err;
23439 +}
23440 +
23441 +/*
23442 + * initialize the whiteout base file/dir for @br.
23443 + */
23444 +int au_wh_init(struct dentry *h_root, struct au_branch *br,
23445 +              struct super_block *sb)
23446 +{
23447 +       int err, i;
23448 +       const unsigned char do_plink
23449 +               = !!au_opt_test(au_mntflags(sb), PLINK);
23450 +       struct path path = {
23451 +               .mnt = br->br_mnt
23452 +       };
23453 +       struct inode *h_dir;
23454 +       struct au_wbr *wbr = br->br_wbr;
23455 +       static const struct qstr base_name[] = {
23456 +               [AuBrWh_BASE] = {
23457 +                       .name   = AUFS_BASE_NAME,
23458 +                       .len    = sizeof(AUFS_BASE_NAME) - 1
23459 +               },
23460 +               [AuBrWh_PLINK] = {
23461 +                       .name   = AUFS_PLINKDIR_NAME,
23462 +                       .len    = sizeof(AUFS_PLINKDIR_NAME) - 1
23463 +               },
23464 +               [AuBrWh_ORPH] = {
23465 +                       .name   = AUFS_ORPHDIR_NAME,
23466 +                       .len    = sizeof(AUFS_ORPHDIR_NAME) - 1
23467 +               }
23468 +       };
23469 +       struct au_wh_base base[] = {
23470 +               [AuBrWh_BASE] = {
23471 +                       .name   = base_name + AuBrWh_BASE,
23472 +                       .dentry = NULL
23473 +               },
23474 +               [AuBrWh_PLINK] = {
23475 +                       .name   = base_name + AuBrWh_PLINK,
23476 +                       .dentry = NULL
23477 +               },
23478 +               [AuBrWh_ORPH] = {
23479 +                       .name   = base_name + AuBrWh_ORPH,
23480 +                       .dentry = NULL
23481 +               }
23482 +       };
23483 +
23484 +
23485 +       h_dir = h_root->d_inode;
23486 +       for (i = 0; i < AuBrWh_Last; i++) {
23487 +               /* doubly whiteouted */
23488 +               struct dentry *d;
23489 +
23490 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
23491 +               err = PTR_ERR(d);
23492 +               if (IS_ERR(d))
23493 +                       goto out;
23494 +
23495 +               base[i].dentry = d;
23496 +               AuDebugOn(wbr
23497 +                         && wbr->wbr_wh[i]
23498 +                         && wbr->wbr_wh[i] != base[i].dentry);
23499 +       }
23500 +
23501 +       if (wbr)
23502 +               for (i = 0; i < AuBrWh_Last; i++) {
23503 +                       dput(wbr->wbr_wh[i]);
23504 +                       wbr->wbr_wh[i] = NULL;
23505 +               }
23506 +
23507 +       err = 0;
23508 +
23509 +       switch (br->br_perm) {
23510 +       case AuBrPerm_RO:
23511 +       case AuBrPerm_ROWH:
23512 +       case AuBrPerm_RR:
23513 +       case AuBrPerm_RRWH:
23514 +               au_wh_init_ro(h_dir, base, &path);
23515 +               break;
23516 +
23517 +       case AuBrPerm_RWNoLinkWH:
23518 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
23519 +               if (err > 0)
23520 +                       goto out;
23521 +               else if (err)
23522 +                       goto out_err;
23523 +               break;
23524 +
23525 +       case AuBrPerm_RW:
23526 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
23527 +               if (err > 0)
23528 +                       goto out;
23529 +               else if (err)
23530 +                       goto out_err;
23531 +               break;
23532 +
23533 +       default:
23534 +               BUG();
23535 +       }
23536 +       goto out; /* success */
23537 +
23538 + out_err:
23539 +       AuErr("an error(%d) on the writable branch %.*s(%s)\n",
23540 +             err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
23541 + out:
23542 +       for (i = 0; i < AuBrWh_Last; i++)
23543 +               dput(base[i].dentry);
23544 +       return err;
23545 +}
23546 +
23547 +/* ---------------------------------------------------------------------- */
23548 +/*
23549 + * whiteouts are all hard-linked usually.
23550 + * when its link count reaches a ceiling, we create a new whiteout base
23551 + * asynchronously.
23552 + */
23553 +
23554 +struct reinit_br_wh {
23555 +       struct super_block *sb;
23556 +       struct au_branch *br;
23557 +};
23558 +
23559 +static void reinit_br_wh(void *arg)
23560 +{
23561 +       int err;
23562 +       aufs_bindex_t bindex;
23563 +       struct path h_path;
23564 +       struct reinit_br_wh *a = arg;
23565 +       struct au_wbr *wbr;
23566 +       struct inode *dir;
23567 +       struct dentry *h_root;
23568 +       struct au_hinode *hdir;
23569 +
23570 +       err = 0;
23571 +       wbr = a->br->br_wbr;
23572 +       /* big aufs lock */
23573 +       si_noflush_write_lock(a->sb);
23574 +       if (!au_br_writable(a->br->br_perm))
23575 +               goto out;
23576 +       bindex = au_br_index(a->sb, a->br->br_id);
23577 +       if (unlikely(bindex < 0))
23578 +               goto out;
23579 +
23580 +       dir = a->sb->s_root->d_inode;
23581 +       /* ii_read_lock_parent(dir); */
23582 +       hdir = au_hi(dir, bindex);
23583 +       h_root = au_h_dptr(a->sb->s_root, bindex);
23584 +
23585 +       au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
23586 +       wbr_wh_write_lock(wbr);
23587 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
23588 +                         h_root, a->br);
23589 +       if (!err) {
23590 +               err = mnt_want_write(a->br->br_mnt);
23591 +               if (!err) {
23592 +                       h_path.dentry = wbr->wbr_whbase;
23593 +                       h_path.mnt = a->br->br_mnt;
23594 +                       err = vfsub_unlink(hdir->hi_inode, &h_path, /*force*/0);
23595 +                       mnt_drop_write(a->br->br_mnt);
23596 +               }
23597 +       } else {
23598 +               AuWarn("%.*s is moved, ignored\n", AuDLNPair(wbr->wbr_whbase));
23599 +               err = 0;
23600 +       }
23601 +       dput(wbr->wbr_whbase);
23602 +       wbr->wbr_whbase = NULL;
23603 +       if (!err)
23604 +               err = au_wh_init(h_root, a->br, a->sb);
23605 +       wbr_wh_write_unlock(wbr);
23606 +       au_hin_imtx_unlock(hdir);
23607 +       /* ii_read_unlock(dir); */
23608 +
23609 + out:
23610 +       if (wbr)
23611 +               atomic_dec(&wbr->wbr_wh_running);
23612 +       atomic_dec(&a->br->br_count);
23613 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
23614 +       si_write_unlock(a->sb);
23615 +       kfree(arg);
23616 +       if (unlikely(err))
23617 +               AuIOErr("err %d\n", err);
23618 +}
23619 +
23620 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
23621 +{
23622 +       int do_dec, wkq_err;
23623 +       struct reinit_br_wh *arg;
23624 +
23625 +       do_dec = 1;
23626 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
23627 +               goto out;
23628 +
23629 +       /* ignore ENOMEM */
23630 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
23631 +       if (arg) {
23632 +               /*
23633 +                * dec(wh_running), kfree(arg) and dec(br_count)
23634 +                * in reinit function
23635 +                */
23636 +               arg->sb = sb;
23637 +               arg->br = br;
23638 +               atomic_inc(&br->br_count);
23639 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb);
23640 +               if (unlikely(wkq_err)) {
23641 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
23642 +                       atomic_dec(&br->br_count);
23643 +                       kfree(arg);
23644 +               }
23645 +               do_dec = 0;
23646 +       }
23647 +
23648 + out:
23649 +       if (do_dec)
23650 +               atomic_dec(&br->br_wbr->wbr_wh_running);
23651 +}
23652 +
23653 +/* ---------------------------------------------------------------------- */
23654 +
23655 +/*
23656 + * create the whiteout @wh.
23657 + */
23658 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
23659 +                            struct dentry *wh)
23660 +{
23661 +       int err;
23662 +       struct path h_path = {
23663 +               .dentry = wh
23664 +       };
23665 +       struct au_branch *br;
23666 +       struct au_wbr *wbr;
23667 +       struct dentry *h_parent;
23668 +       struct inode *h_dir;
23669 +
23670 +       h_parent = wh->d_parent; /* dir inode is locked */
23671 +       h_dir = h_parent->d_inode;
23672 +       IMustLock(h_dir);
23673 +
23674 +       br = au_sbr(sb, bindex);
23675 +       h_path.mnt = br->br_mnt;
23676 +       wbr = br->br_wbr;
23677 +       wbr_wh_read_lock(wbr);
23678 +       if (wbr->wbr_whbase) {
23679 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path);
23680 +               if (!err || err != -EMLINK)
23681 +                       goto out;
23682 +
23683 +               /* link count full. re-initialize br_whbase. */
23684 +               kick_reinit_br_wh(sb, br);
23685 +       }
23686 +
23687 +       /* return this error in this context */
23688 +       err = vfsub_create(h_dir, &h_path, WH_MASK);
23689 +
23690 + out:
23691 +       wbr_wh_read_unlock(wbr);
23692 +       return err;
23693 +}
23694 +
23695 +/* ---------------------------------------------------------------------- */
23696 +
23697 +/*
23698 + * create or remove the diropq.
23699 + */
23700 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
23701 +                               unsigned int flags)
23702 +{
23703 +       struct dentry *opq_dentry, *h_dentry;
23704 +       struct super_block *sb;
23705 +       struct au_branch *br;
23706 +       int err;
23707 +
23708 +       sb = dentry->d_sb;
23709 +       br = au_sbr(sb, bindex);
23710 +       h_dentry = au_h_dptr(dentry, bindex);
23711 +       opq_dentry = au_lkup_one(&diropq_name, h_dentry, br, /*nd*/NULL);
23712 +       if (IS_ERR(opq_dentry))
23713 +               goto out;
23714 +
23715 +       if (au_ftest_diropq(flags, CREATE)) {
23716 +               err = link_or_create_wh(sb, bindex, opq_dentry);
23717 +               if (!err) {
23718 +                       au_set_dbdiropq(dentry, bindex);
23719 +                       goto out; /* success */
23720 +               }
23721 +       } else {
23722 +               struct path tmp = {
23723 +                       .dentry = opq_dentry,
23724 +                       .mnt    = br->br_mnt
23725 +               };
23726 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
23727 +               if (!err)
23728 +                       au_set_dbdiropq(dentry, -1);
23729 +       }
23730 +       dput(opq_dentry);
23731 +       opq_dentry = ERR_PTR(err);
23732 +
23733 + out:
23734 +       return opq_dentry;
23735 +}
23736 +
23737 +struct do_diropq_args {
23738 +       struct dentry **errp;
23739 +       struct dentry *dentry;
23740 +       aufs_bindex_t bindex;
23741 +       unsigned int flags;
23742 +};
23743 +
23744 +static void call_do_diropq(void *args)
23745 +{
23746 +       struct do_diropq_args *a = args;
23747 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
23748 +}
23749 +
23750 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
23751 +                            unsigned int flags)
23752 +{
23753 +       struct dentry *diropq, *h_dentry;
23754 +
23755 +       h_dentry = au_h_dptr(dentry, bindex);
23756 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
23757 +               diropq = do_diropq(dentry, bindex, flags);
23758 +       else {
23759 +               int wkq_err;
23760 +               struct do_diropq_args args = {
23761 +                       .errp           = &diropq,
23762 +                       .dentry         = dentry,
23763 +                       .bindex         = bindex,
23764 +                       .flags          = flags
23765 +               };
23766 +
23767 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
23768 +               if (unlikely(wkq_err))
23769 +                       diropq = ERR_PTR(wkq_err);
23770 +       }
23771 +
23772 +       return diropq;
23773 +}
23774 +
23775 +/* ---------------------------------------------------------------------- */
23776 +
23777 +/*
23778 + * lookup whiteout dentry.
23779 + * @h_parent: lower parent dentry which must exist and be locked
23780 + * @base_name: name of dentry which will be whiteouted
23781 + * returns dentry for whiteout.
23782 + */
23783 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
23784 +                         struct au_branch *br)
23785 +{
23786 +       int err;
23787 +       struct qstr wh_name;
23788 +       struct dentry *wh_dentry;
23789 +
23790 +       err = au_wh_name_alloc(&wh_name, base_name);
23791 +       wh_dentry = ERR_PTR(err);
23792 +       if (!err) {
23793 +               wh_dentry = au_lkup_one(&wh_name, h_parent, br, /*nd*/NULL);
23794 +               kfree(wh_name.name);
23795 +       }
23796 +       return wh_dentry;
23797 +}
23798 +
23799 +/*
23800 + * link/create a whiteout for @dentry on @bindex.
23801 + */
23802 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
23803 +                           struct dentry *h_parent)
23804 +{
23805 +       struct dentry *wh_dentry;
23806 +       struct super_block *sb;
23807 +       int err;
23808 +
23809 +       sb = dentry->d_sb;
23810 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
23811 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
23812 +               err = link_or_create_wh(sb, bindex, wh_dentry);
23813 +               if (!err)
23814 +                       au_set_dbwh(dentry, bindex);
23815 +               else {
23816 +                       dput(wh_dentry);
23817 +                       wh_dentry = ERR_PTR(err);
23818 +               }
23819 +       }
23820 +
23821 +       return wh_dentry;
23822 +}
23823 +
23824 +/* ---------------------------------------------------------------------- */
23825 +
23826 +/* Delete all whiteouts in this directory on branch bindex. */
23827 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
23828 +                          aufs_bindex_t bindex, struct au_branch *br)
23829 +{
23830 +       int err;
23831 +       unsigned long ul, n;
23832 +       struct qstr wh_name;
23833 +       char *p;
23834 +       struct hlist_head *head;
23835 +       struct au_vdir_wh *tpos;
23836 +       struct hlist_node *pos;
23837 +       struct au_vdir_destr *str;
23838 +
23839 +       err = -ENOMEM;
23840 +       p = __getname();
23841 +       wh_name.name = p;
23842 +       if (unlikely(!wh_name.name))
23843 +               goto out;
23844 +
23845 +       err = 0;
23846 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
23847 +       p += AUFS_WH_PFX_LEN;
23848 +       n = whlist->nh_num;
23849 +       head = whlist->nh_head;
23850 +       for (ul = 0; !err && ul < n; ul++, head++) {
23851 +               hlist_for_each_entry(tpos, pos, head, wh_hash) {
23852 +                       if (tpos->wh_bindex != bindex)
23853 +                               continue;
23854 +
23855 +                       str = &tpos->wh_str;
23856 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
23857 +                               memcpy(p, str->name, str->len);
23858 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
23859 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
23860 +                               if (!err)
23861 +                                       continue;
23862 +                               break;
23863 +                       }
23864 +                       AuIOErr("whiteout name too long %.*s\n",
23865 +                               str->len, str->name);
23866 +                       err = -EIO;
23867 +                       break;
23868 +               }
23869 +       }
23870 +       __putname(wh_name.name);
23871 +
23872 + out:
23873 +       return err;
23874 +}
23875 +
23876 +struct del_wh_children_args {
23877 +       int *errp;
23878 +       struct dentry *h_dentry;
23879 +       struct au_nhash *whlist;
23880 +       aufs_bindex_t bindex;
23881 +       struct au_branch *br;
23882 +};
23883 +
23884 +static void call_del_wh_children(void *args)
23885 +{
23886 +       struct del_wh_children_args *a = args;
23887 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
23888 +}
23889 +
23890 +/* ---------------------------------------------------------------------- */
23891 +
23892 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
23893 +{
23894 +       struct au_whtmp_rmdir *whtmp;
23895 +
23896 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
23897 +       if (unlikely(!whtmp))
23898 +               goto out;
23899 +
23900 +       whtmp->dir = NULL;
23901 +       whtmp->wh_dentry = NULL;
23902 +       whtmp->whlist = au_nhash_alloc(sb, /*bend*/0, gfp);
23903 +       if (unlikely(!whtmp->whlist))
23904 +               goto out_whtmp;
23905 +       return whtmp; /* success */
23906 +
23907 + out_whtmp:
23908 +       kfree(whtmp);
23909 + out:
23910 +       return ERR_PTR(-ENOMEM);
23911 +}
23912 +
23913 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
23914 +{
23915 +       dput(whtmp->wh_dentry);
23916 +       iput(whtmp->dir);
23917 +       au_nhash_wh_free(whtmp->whlist, /*bend*/0);
23918 +       kfree(whtmp);
23919 +}
23920 +
23921 +/*
23922 + * rmdir the whiteouted temporary named dir @h_dentry.
23923 + * @whlist: whiteouted children.
23924 + */
23925 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
23926 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
23927 +{
23928 +       int err;
23929 +       struct path h_tmp;
23930 +       struct inode *wh_inode, *h_dir;
23931 +       struct au_branch *br;
23932 +
23933 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
23934 +       IMustLock(h_dir);
23935 +
23936 +       br = au_sbr(dir->i_sb, bindex);
23937 +       wh_inode = wh_dentry->d_inode;
23938 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
23939 +
23940 +       /*
23941 +        * someone else might change some whiteouts while we were sleeping.
23942 +        * it means this whlist may have an obsoleted entry.
23943 +        */
23944 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
23945 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
23946 +       else {
23947 +               int wkq_err;
23948 +               struct del_wh_children_args args = {
23949 +                       .errp           = &err,
23950 +                       .h_dentry       = wh_dentry,
23951 +                       .whlist         = whlist,
23952 +                       .bindex         = bindex,
23953 +                       .br             = br
23954 +               };
23955 +
23956 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
23957 +               if (unlikely(wkq_err))
23958 +                       err = wkq_err;
23959 +       }
23960 +       mutex_unlock(&wh_inode->i_mutex);
23961 +
23962 +       if (!err) {
23963 +               h_tmp.dentry = wh_dentry;
23964 +               h_tmp.mnt = br->br_mnt;
23965 +               err = vfsub_rmdir(h_dir, &h_tmp);
23966 +               /* d_drop(h_dentry); */
23967 +       }
23968 +
23969 +       if (!err) {
23970 +               if (au_ibstart(dir) == bindex) {
23971 +                       au_cpup_attr_timesizes(dir);
23972 +                       drop_nlink(dir);
23973 +               }
23974 +               return 0; /* success */
23975 +       }
23976 +
23977 +       AuWarn("failed removing %.*s(%d), ignored\n",
23978 +              AuDLNPair(wh_dentry), err);
23979 +       return err;
23980 +}
23981 +
23982 +static void call_rmdir_whtmp(void *args)
23983 +{
23984 +       int err;
23985 +       struct au_whtmp_rmdir *a = args;
23986 +       struct super_block *sb;
23987 +       struct dentry *h_parent;
23988 +       struct inode *h_dir;
23989 +       struct au_branch *br;
23990 +       struct au_hinode *hdir;
23991 +
23992 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
23993 +       /* mutex_lock(&a->dir->i_mutex); */
23994 +       sb = a->dir->i_sb;
23995 +       si_noflush_read_lock(sb);
23996 +       err = au_test_ro(sb, a->bindex, NULL);
23997 +       if (unlikely(err))
23998 +               goto out;
23999 +
24000 +       err = -EIO;
24001 +       br = au_sbr(sb, a->bindex);
24002 +       ii_write_lock_parent(a->dir);
24003 +       h_parent = dget_parent(a->wh_dentry);
24004 +       h_dir = h_parent->d_inode;
24005 +       hdir = au_hi(a->dir, a->bindex);
24006 +       au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
24007 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent, br);
24008 +       if (!err) {
24009 +               err = mnt_want_write(br->br_mnt);
24010 +               if (!err) {
24011 +                       err = au_whtmp_rmdir(a->dir, a->bindex, a->wh_dentry,
24012 +                                            a->whlist);
24013 +                       mnt_drop_write(br->br_mnt);
24014 +               }
24015 +       }
24016 +       au_hin_imtx_unlock(hdir);
24017 +       dput(h_parent);
24018 +       ii_write_unlock(a->dir);
24019 +
24020 + out:
24021 +       /* mutex_unlock(&a->dir->i_mutex); */
24022 +       au_nwt_done(&au_sbi(sb)->si_nowait);
24023 +       si_read_unlock(sb);
24024 +       au_whtmp_rmdir_free(a);
24025 +       if (unlikely(err))
24026 +               AuIOErr("err %d\n", err);
24027 +}
24028 +
24029 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
24030 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
24031 +{
24032 +       int wkq_err;
24033 +
24034 +       IMustLock(dir);
24035 +
24036 +       /* all post-process will be done in do_rmdir_whtmp(). */
24037 +       args->dir = au_igrab(dir);
24038 +       args->bindex = bindex;
24039 +       args->wh_dentry = dget(wh_dentry);
24040 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, dir->i_sb);
24041 +       if (unlikely(wkq_err)) {
24042 +               AuWarn("rmdir error %.*s (%d), ignored\n",
24043 +                      AuDLNPair(wh_dentry), wkq_err);
24044 +               au_whtmp_rmdir_free(args);
24045 +       }
24046 +}
24047 diff --git a/fs/aufs/whout.h b/fs/aufs/whout.h
24048 new file mode 100644
24049 index 0000000..3247a4f
24050 --- /dev/null
24051 +++ b/fs/aufs/whout.h
24052 @@ -0,0 +1,79 @@
24053 +/*
24054 + * Copyright (C) 2005-2009 Junjiro R. Okajima
24055 + *
24056 + * This program, aufs is free software; you can redistribute it and/or modify
24057 + * it under the terms of the GNU General Public License as published by
24058 + * the Free Software Foundation; either version 2 of the License, or
24059 + * (at your option) any later version.
24060 + */
24061 +
24062 +/*
24063 + * whiteout for logical deletion and opaque directory
24064 + */
24065 +
24066 +#ifndef __AUFS_WHOUT_H__
24067 +#define __AUFS_WHOUT_H__
24068 +
24069 +#ifdef __KERNEL__
24070 +
24071 +#include <linux/fs.h>
24072 +#include <linux/aufs_type.h>
24073 +#include "dir.h"
24074 +
24075 +/* whout.c */
24076 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
24077 +struct au_branch;
24078 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
24079 +              struct au_branch *br, int try_sio);
24080 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br);
24081 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
24082 +                            struct qstr *prefix);
24083 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
24084 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
24085 +                       struct dentry *dentry);
24086 +int au_wh_init(struct dentry *h_parent, struct au_branch *br,
24087 +              struct super_block *sb);
24088 +
24089 +/* diropq flags */
24090 +#define AuDiropq_CREATE        1
24091 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
24092 +#define au_fset_diropq(flags, name)    { (flags) |= AuDiropq_##name; }
24093 +#define au_fclr_diropq(flags, name)    { (flags) &= ~AuDiropq_##name; }
24094 +
24095 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
24096 +                            unsigned int flags);
24097 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
24098 +                         struct au_branch *br);
24099 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
24100 +                           struct dentry *h_parent);
24101 +
24102 +/* real rmdir for the whiteout-ed dir */
24103 +struct au_whtmp_rmdir {
24104 +       struct inode *dir;
24105 +       aufs_bindex_t bindex;
24106 +       struct dentry *wh_dentry;
24107 +       struct au_nhash *whlist;
24108 +};
24109 +
24110 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
24111 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
24112 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
24113 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
24114 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
24115 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
24116 +
24117 +/* ---------------------------------------------------------------------- */
24118 +
24119 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
24120 +                                             aufs_bindex_t bindex)
24121 +{
24122 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
24123 +}
24124 +
24125 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
24126 +{
24127 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
24128 +}
24129 +
24130 +#endif /* __KERNEL__ */
24131 +#endif /* __AUFS_WHOUT_H__ */
24132 diff --git a/fs/aufs/wkq.c b/fs/aufs/wkq.c
24133 new file mode 100644
24134 index 0000000..41e30be
24135 --- /dev/null
24136 +++ b/fs/aufs/wkq.c
24137 @@ -0,0 +1,249 @@
24138 +/*
24139 + * Copyright (C) 2005-2009 Junjiro R. Okajima
24140 + *
24141 + * This program, aufs is free software; you can redistribute it and/or modify
24142 + * it under the terms of the GNU General Public License as published by
24143 + * the Free Software Foundation; either version 2 of the License, or
24144 + * (at your option) any later version.
24145 + */
24146 +
24147 +/*
24148 + * workqueue for asynchronous/super-io operations
24149 + * todo: try new dredential scheme
24150 + */
24151 +
24152 +#include "aufs.h"
24153 +
24154 +/* internal workqueue named AUFS_WKQ_NAME */
24155 +static struct au_wkq {
24156 +       struct workqueue_struct *q;
24157 +
24158 +       /* balancing */
24159 +       atomic_t                busy;
24160 +} *au_wkq;
24161 +
24162 +struct au_wkinfo {
24163 +       struct work_struct wk;
24164 +       struct super_block *sb;
24165 +
24166 +       unsigned int flags; /* see wkq.h */
24167 +
24168 +       au_wkq_func_t func;
24169 +       void *args;
24170 +
24171 +       atomic_t *busyp;
24172 +       struct completion *comp;
24173 +};
24174 +
24175 +/* ---------------------------------------------------------------------- */
24176 +
24177 +static int enqueue(struct au_wkq *wkq, struct au_wkinfo *wkinfo)
24178 +{
24179 +       wkinfo->busyp = &wkq->busy;
24180 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
24181 +               return !queue_work(wkq->q, &wkinfo->wk);
24182 +       else
24183 +               return !schedule_work(&wkinfo->wk);
24184 +}
24185 +
24186 +static void do_wkq(struct au_wkinfo *wkinfo)
24187 +{
24188 +       unsigned int idle, n;
24189 +       int i, idle_idx;
24190 +
24191 +       while (1) {
24192 +               if (au_ftest_wkq(wkinfo->flags, WAIT)) {
24193 +                       idle_idx = 0;
24194 +                       idle = UINT_MAX;
24195 +                       for (i = 0; i < aufs_nwkq; i++) {
24196 +                               n = atomic_inc_return(&au_wkq[i].busy);
24197 +                               if (n == 1 && !enqueue(au_wkq + i, wkinfo))
24198 +                                       return; /* success */
24199 +
24200 +                               if (n < idle) {
24201 +                                       idle_idx = i;
24202 +                                       idle = n;
24203 +                               }
24204 +                               atomic_dec(&au_wkq[i].busy);
24205 +                       }
24206 +               } else
24207 +                       idle_idx = aufs_nwkq;
24208 +
24209 +               atomic_inc(&au_wkq[idle_idx].busy);
24210 +               if (!enqueue(au_wkq + idle_idx, wkinfo))
24211 +                       return; /* success */
24212 +
24213 +               /* impossible? */
24214 +               AuWarn1("failed to queue_work()\n");
24215 +               yield();
24216 +       }
24217 +}
24218 +
24219 +static void wkq_func(struct work_struct *wk)
24220 +{
24221 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
24222 +
24223 +       wkinfo->func(wkinfo->args);
24224 +       atomic_dec(wkinfo->busyp);
24225 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
24226 +               complete(wkinfo->comp);
24227 +       else {
24228 +               kobject_put(&au_sbi(wkinfo->sb)->si_kobj);
24229 +               module_put(THIS_MODULE);
24230 +               kfree(wkinfo);
24231 +       }
24232 +}
24233 +
24234 +/*
24235 + * Since struct completion is large, try allocating it dynamically.
24236 + */
24237 +#if defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS)
24238 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
24239 +
24240 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
24241 +{
24242 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
24243 +       if (*comp) {
24244 +               init_completion(*comp);
24245 +               wkinfo->comp = *comp;
24246 +               return 0;
24247 +       }
24248 +       return -ENOMEM;
24249 +}
24250 +
24251 +static void au_wkq_comp_free(struct completion *comp)
24252 +{
24253 +       kfree(comp);
24254 +}
24255 +
24256 +#else
24257 +
24258 +/* no braces */
24259 +#define AuWkqCompDeclare(name) \
24260 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
24261 +       struct completion *comp = &_ ## name
24262 +
24263 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
24264 +{
24265 +       wkinfo->comp = *comp;
24266 +       return 0;
24267 +}
24268 +
24269 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
24270 +{
24271 +       /* empty */
24272 +}
24273 +#endif /* 4KSTACKS */
24274 +
24275 +static void au_wkq_run(struct au_wkinfo *wkinfo)
24276 +{
24277 +       au_dbg_verify_kthread();
24278 +       INIT_WORK(&wkinfo->wk, wkq_func);
24279 +       do_wkq(wkinfo);
24280 +}
24281 +
24282 +int au_wkq_wait(au_wkq_func_t func, void *args)
24283 +{
24284 +       int err;
24285 +       AuWkqCompDeclare(comp);
24286 +       struct au_wkinfo wkinfo = {
24287 +               .flags  = AuWkq_WAIT,
24288 +               .func   = func,
24289 +               .args   = args
24290 +       };
24291 +
24292 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
24293 +       if (!err) {
24294 +               au_wkq_run(&wkinfo);
24295 +               /* no timeout, no interrupt */
24296 +               wait_for_completion(wkinfo.comp);
24297 +               au_wkq_comp_free(comp);
24298 +       }
24299 +
24300 +       return err;
24301 +
24302 +}
24303 +
24304 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb)
24305 +{
24306 +       int err;
24307 +       struct au_wkinfo *wkinfo;
24308 +
24309 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
24310 +
24311 +       /*
24312 +        * wkq_func() must free this wkinfo.
24313 +        * it highly depends upon the implementation of workqueue.
24314 +        */
24315 +       err = 0;
24316 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
24317 +       if (wkinfo) {
24318 +               wkinfo->sb = sb;
24319 +               wkinfo->flags = !AuWkq_WAIT;
24320 +               wkinfo->func = func;
24321 +               wkinfo->args = args;
24322 +               wkinfo->comp = NULL;
24323 +               kobject_get(&au_sbi(sb)->si_kobj);
24324 +               __module_get(THIS_MODULE);
24325 +
24326 +               au_wkq_run(wkinfo);
24327 +       } else {
24328 +               err = -ENOMEM;
24329 +               atomic_dec(&au_sbi(sb)->si_nowait.nw_len);
24330 +       }
24331 +
24332 +       return err;
24333 +}
24334 +
24335 +/* ---------------------------------------------------------------------- */
24336 +
24337 +void au_nwt_init(struct au_nowait_tasks *nwt)
24338 +{
24339 +       atomic_set(&nwt->nw_len, 0);
24340 +       /* smp_mb();*/ /* atomic_set */
24341 +       init_waitqueue_head(&nwt->nw_wq);
24342 +}
24343 +
24344 +void au_wkq_fin(void)
24345 +{
24346 +       int i;
24347 +
24348 +       for (i = 0; i < aufs_nwkq; i++)
24349 +               if (au_wkq[i].q && !IS_ERR(au_wkq[i].q))
24350 +                       destroy_workqueue(au_wkq[i].q);
24351 +       kfree(au_wkq);
24352 +}
24353 +
24354 +int __init au_wkq_init(void)
24355 +{
24356 +       int err, i;
24357 +       struct au_wkq *nowaitq;
24358 +
24359 +       /* '+1' is for accounting of nowait queue */
24360 +       err = -ENOMEM;
24361 +       au_wkq = kcalloc(aufs_nwkq + 1, sizeof(*au_wkq), GFP_NOFS);
24362 +       if (unlikely(!au_wkq))
24363 +               goto out;
24364 +
24365 +       err = 0;
24366 +       for (i = 0; i < aufs_nwkq; i++) {
24367 +               au_wkq[i].q = create_singlethread_workqueue(AUFS_WKQ_NAME);
24368 +               if (au_wkq[i].q && !IS_ERR(au_wkq[i].q)) {
24369 +                       atomic_set(&au_wkq[i].busy, 0);
24370 +                       continue;
24371 +               }
24372 +
24373 +               err = PTR_ERR(au_wkq[i].q);
24374 +               au_wkq_fin();
24375 +               goto out;
24376 +       }
24377 +
24378 +       /* nowait accounting */
24379 +       nowaitq = au_wkq + aufs_nwkq;
24380 +       atomic_set(&nowaitq->busy, 0);
24381 +       nowaitq->q = NULL;
24382 +       /* smp_mb(); */ /* atomic_set */
24383 +
24384 + out:
24385 +       return err;
24386 +}
24387 diff --git a/fs/aufs/wkq.h b/fs/aufs/wkq.h
24388 new file mode 100644
24389 index 0000000..17d24d8
24390 --- /dev/null
24391 +++ b/fs/aufs/wkq.h
24392 @@ -0,0 +1,72 @@
24393 +/*
24394 + * Copyright (C) 2005-2009 Junjiro R. Okajima
24395 + *
24396 + * This program, aufs is free software; you can redistribute it and/or modify
24397 + * it under the terms of the GNU General Public License as published by
24398 + * the Free Software Foundation; either version 2 of the License, or
24399 + * (at your option) any later version.
24400 + */
24401 +
24402 +/*
24403 + * workqueue for asynchronous/super-io operations
24404 + * todo: try new credentials management scheme
24405 + */
24406 +
24407 +#ifndef __AUFS_WKQ_H__
24408 +#define __AUFS_WKQ_H__
24409 +
24410 +#ifdef __KERNEL__
24411 +
24412 +#include <linux/fs.h>
24413 +#include <linux/sched.h>
24414 +#include <linux/workqueue.h>
24415 +#include <linux/aufs_type.h>
24416 +
24417 +/* ---------------------------------------------------------------------- */
24418 +
24419 +/*
24420 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
24421 + */
24422 +struct au_nowait_tasks {
24423 +       atomic_t                nw_len;
24424 +       wait_queue_head_t       nw_wq;
24425 +};
24426 +
24427 +/* ---------------------------------------------------------------------- */
24428 +
24429 +typedef void (*au_wkq_func_t)(void *args);
24430 +
24431 +/* wkq flags */
24432 +#define AuWkq_WAIT     1
24433 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
24434 +#define au_fset_wkq(flags, name)       { (flags) |= AuWkq_##name; }
24435 +#define au_fclr_wkq(flags, name)       { (flags) &= ~AuWkq_##name; }
24436 +
24437 +/* wkq.c */
24438 +int au_wkq_wait(au_wkq_func_t func, void *args);
24439 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb);
24440 +void au_nwt_init(struct au_nowait_tasks *nwt);
24441 +int __init au_wkq_init(void);
24442 +void au_wkq_fin(void);
24443 +
24444 +/* ---------------------------------------------------------------------- */
24445 +
24446 +static inline int au_test_wkq(struct task_struct *tsk)
24447 +{
24448 +       return !tsk->mm && !strcmp(tsk->comm, AUFS_WKQ_NAME);
24449 +}
24450 +
24451 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
24452 +{
24453 +       if (!atomic_dec_return(&nwt->nw_len))
24454 +               wake_up_all(&nwt->nw_wq);
24455 +}
24456 +
24457 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
24458 +{
24459 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
24460 +       return 0;
24461 +}
24462 +
24463 +#endif /* __KERNEL__ */
24464 +#endif /* __AUFS_WKQ_H__ */
24465 diff --git a/fs/aufs/xino.c b/fs/aufs/xino.c
24466 new file mode 100644
24467 index 0000000..7d11111
24468 --- /dev/null
24469 +++ b/fs/aufs/xino.c
24470 @@ -0,0 +1,1181 @@
24471 +/*
24472 + * Copyright (C) 2005-2009 Junjiro R. Okajima
24473 + *
24474 + * This program, aufs is free software; you can redistribute it and/or modify
24475 + * it under the terms of the GNU General Public License as published by
24476 + * the Free Software Foundation; either version 2 of the License, or
24477 + * (at your option) any later version.
24478 + */
24479 +
24480 +/*
24481 + * external inode number translation table and bitmap
24482 + */
24483 +
24484 +#include <linux/seq_file.h>
24485 +#include <linux/uaccess.h>
24486 +#include "aufs.h"
24487 +
24488 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
24489 +                  loff_t *pos)
24490 +{
24491 +       ssize_t err;
24492 +       mm_segment_t oldfs;
24493 +
24494 +       oldfs = get_fs();
24495 +       set_fs(KERNEL_DS);
24496 +       do {
24497 +               /* todo: signal_pending? */
24498 +               err = func(file, (char __user *)buf, size, pos);
24499 +       } while (err == -EAGAIN || err == -EINTR);
24500 +       set_fs(oldfs);
24501 +
24502 +#if 0 /* reserved for future use */
24503 +       if (err > 0)
24504 +               fsnotify_access(file->f_dentry);
24505 +#endif
24506 +
24507 +       return err;
24508 +}
24509 +
24510 +/* ---------------------------------------------------------------------- */
24511 +
24512 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *buf,
24513 +                             size_t size, loff_t *pos)
24514 +{
24515 +       ssize_t err;
24516 +       mm_segment_t oldfs;
24517 +
24518 +       oldfs = get_fs();
24519 +       set_fs(KERNEL_DS);
24520 +       lockdep_off();
24521 +       do {
24522 +               /* todo: signal_pending? */
24523 +               err = func(file, (const char __user *)buf, size, pos);
24524 +       } while (err == -EAGAIN || err == -EINTR);
24525 +       lockdep_on();
24526 +       set_fs(oldfs);
24527 +
24528 +#if 0 /* reserved for future use */
24529 +       if (err > 0)
24530 +               fsnotify_modify(file->f_dentry);
24531 +#endif
24532 +
24533 +       return err;
24534 +}
24535 +
24536 +struct do_xino_fwrite_args {
24537 +       ssize_t *errp;
24538 +       au_writef_t func;
24539 +       struct file *file;
24540 +       void *buf;
24541 +       size_t size;
24542 +       loff_t *pos;
24543 +};
24544 +
24545 +static void call_do_xino_fwrite(void *args)
24546 +{
24547 +       struct do_xino_fwrite_args *a = args;
24548 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
24549 +}
24550 +
24551 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
24552 +                   loff_t *pos)
24553 +{
24554 +       ssize_t err;
24555 +
24556 +       /* todo: signal block and no wkq? */
24557 +       /* todo: new credential scheme */
24558 +       /*
24559 +        * it breaks RLIMIT_FSIZE and normal user's limit,
24560 +        * users should care about quota and real 'filesystem full.'
24561 +        */
24562 +       if (!au_test_wkq(current)) {
24563 +               int wkq_err;
24564 +               struct do_xino_fwrite_args args = {
24565 +                       .errp   = &err,
24566 +                       .func   = func,
24567 +                       .file   = file,
24568 +                       .buf    = buf,
24569 +                       .size   = size,
24570 +                       .pos    = pos
24571 +               };
24572 +
24573 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
24574 +               if (unlikely(wkq_err))
24575 +                       err = wkq_err;
24576 +       } else
24577 +               err = do_xino_fwrite(func, file, buf, size, pos);
24578 +
24579 +       return err;
24580 +}
24581 +
24582 +/* ---------------------------------------------------------------------- */
24583 +
24584 +/*
24585 + * create a new xinofile at the same place/path as @base_file.
24586 + */
24587 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
24588 +{
24589 +       struct file *file;
24590 +       struct dentry *base, *dentry, *parent;
24591 +       struct inode *dir;
24592 +       struct qstr *name;
24593 +       int err;
24594 +
24595 +       base = base_file->f_dentry;
24596 +       parent = base->d_parent; /* dir inode is locked */
24597 +       dir = parent->d_inode;
24598 +       IMustLock(dir);
24599 +
24600 +       file = ERR_PTR(-EINVAL);
24601 +       name = &base->d_name;
24602 +       dentry = vfsub_lookup_one_len(name->name, parent, name->len);
24603 +       if (IS_ERR(dentry)) {
24604 +               file = (void *)dentry;
24605 +               AuErr("%.*s lookup err %ld\n", AuLNPair(name), PTR_ERR(dentry));
24606 +               goto out;
24607 +       }
24608 +
24609 +       /* no need to mnt_want_write() since we call dentry_open() later */
24610 +       err = vfs_create(dir, dentry, S_IRUGO | S_IWUGO, NULL);
24611 +       if (unlikely(err)) {
24612 +               file = ERR_PTR(err);
24613 +               AuErr("%.*s create err %d\n", AuLNPair(name), err);
24614 +               goto out_dput;
24615 +       }
24616 +
24617 +       file = dentry_open(dget(dentry), mntget(base_file->f_vfsmnt),
24618 +                          O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE,
24619 +                          current_cred());
24620 +       if (IS_ERR(file)) {
24621 +               AuErr("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
24622 +               goto out_dput;
24623 +       }
24624 +
24625 +       err = vfsub_unlink(dir, &file->f_path, /*force*/0);
24626 +       if (unlikely(err)) {
24627 +               AuErr("%.*s unlink err %d\n", AuLNPair(name), err);
24628 +               goto out_fput;
24629 +       }
24630 +
24631 +       if (copy_src) {
24632 +               /* no one can touch copy_src xino */
24633 +               err = au_copy_file(file, copy_src,
24634 +                                  i_size_read(copy_src->f_dentry->d_inode));
24635 +               if (unlikely(err)) {
24636 +                       AuErr("%.*s copy err %d\n", AuLNPair(name), err);
24637 +                       goto out_fput;
24638 +               }
24639 +       }
24640 +       goto out_dput; /* success */
24641 +
24642 + out_fput:
24643 +       fput(file);
24644 +       file = ERR_PTR(err);
24645 + out_dput:
24646 +       dput(dentry);
24647 + out:
24648 +       return file;
24649 +}
24650 +
24651 +struct au_xino_lock_dir {
24652 +       struct au_hinode *hdir;
24653 +       struct dentry *parent;
24654 +       struct mutex *mtx;
24655 +};
24656 +
24657 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
24658 +                            struct au_xino_lock_dir *ldir)
24659 +{
24660 +       aufs_bindex_t brid, bindex;
24661 +
24662 +       ldir->hdir = NULL;
24663 +       bindex = -1;
24664 +       brid = au_xino_brid(sb);
24665 +       if (brid >= 0)
24666 +               bindex = au_br_index(sb, brid);
24667 +       if (bindex >= 0) {
24668 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
24669 +               au_hin_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
24670 +       } else {
24671 +               ldir->parent = dget_parent(xino->f_dentry);
24672 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
24673 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
24674 +       }
24675 +}
24676 +
24677 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
24678 +{
24679 +       if (ldir->hdir)
24680 +               au_hin_imtx_unlock(ldir->hdir);
24681 +       else {
24682 +               mutex_unlock(ldir->mtx);
24683 +               dput(ldir->parent);
24684 +       }
24685 +}
24686 +
24687 +/* ---------------------------------------------------------------------- */
24688 +
24689 +/* trucate xino files asynchronously */
24690 +
24691 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
24692 +{
24693 +       int err;
24694 +       aufs_bindex_t bi, bend;
24695 +       struct au_branch *br;
24696 +       struct file *new_xino, *file;
24697 +       struct super_block *h_sb;
24698 +       struct au_xino_lock_dir ldir;
24699 +
24700 +       err = -EINVAL;
24701 +       bend = au_sbend(sb);
24702 +       if (unlikely(bindex < 0 || bend < bindex))
24703 +               goto out;
24704 +       br = au_sbr(sb, bindex);
24705 +       file = br->br_xino.xi_file;
24706 +       if (!file)
24707 +               goto out;
24708 +
24709 +       au_xino_lock_dir(sb, file, &ldir);
24710 +       /* mnt_want_write() is unnecessary here */
24711 +       new_xino = au_xino_create2(file, file);
24712 +       au_xino_unlock_dir(&ldir);
24713 +       err = PTR_ERR(new_xino);
24714 +       if (IS_ERR(new_xino))
24715 +               goto out;
24716 +       err = 0;
24717 +       fput(file);
24718 +       br->br_xino.xi_file = new_xino;
24719 +
24720 +       h_sb = br->br_mnt->mnt_sb;
24721 +       for (bi = 0; bi <= bend; bi++) {
24722 +               if (unlikely(bi == bindex))
24723 +                       continue;
24724 +               br = au_sbr(sb, bi);
24725 +               if (br->br_mnt->mnt_sb != h_sb)
24726 +                       continue;
24727 +
24728 +               fput(br->br_xino.xi_file);
24729 +               br->br_xino.xi_file = new_xino;
24730 +               get_file(new_xino);
24731 +       }
24732 +
24733 + out:
24734 +       return err;
24735 +}
24736 +
24737 +struct xino_do_trunc_args {
24738 +       struct super_block *sb;
24739 +       struct au_branch *br;
24740 +};
24741 +
24742 +static void xino_do_trunc(void *_args)
24743 +{
24744 +       struct xino_do_trunc_args *args = _args;
24745 +       struct super_block *sb;
24746 +       struct au_branch *br;
24747 +       struct inode *dir;
24748 +       int err;
24749 +       aufs_bindex_t bindex;
24750 +
24751 +       err = 0;
24752 +       sb = args->sb;
24753 +       dir = sb->s_root->d_inode;
24754 +       br = args->br;
24755 +
24756 +       si_noflush_write_lock(sb);
24757 +       ii_read_lock_parent(dir);
24758 +       bindex = au_br_index(sb, br->br_id);
24759 +       err = au_xino_trunc(sb, bindex);
24760 +       if (unlikely(err))
24761 +               goto out;
24762 +
24763 +       if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
24764 +           >= br->br_xino_upper)
24765 +               br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
24766 +
24767 + out:
24768 +       ii_read_unlock(dir);
24769 +       if (unlikely(err))
24770 +               AuWarn("err b%d, (%d)\n", bindex, err);
24771 +       atomic_dec(&br->br_xino_running);
24772 +       atomic_dec(&br->br_count);
24773 +       au_nwt_done(&au_sbi(sb)->si_nowait);
24774 +       si_write_unlock(sb);
24775 +       kfree(args);
24776 +}
24777 +
24778 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
24779 +{
24780 +       struct xino_do_trunc_args *args;
24781 +       int wkq_err;
24782 +
24783 +       if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
24784 +           < br->br_xino_upper)
24785 +               return;
24786 +
24787 +       if (atomic_inc_return(&br->br_xino_running) > 1)
24788 +               goto out;
24789 +
24790 +       /* lock and kfree() will be called in trunc_xino() */
24791 +       args = kmalloc(sizeof(*args), GFP_NOFS);
24792 +       if (unlikely(!args)) {
24793 +               AuErr1("no memory\n");
24794 +               goto out_args;
24795 +       }
24796 +
24797 +       atomic_inc(&br->br_count);
24798 +       args->sb = sb;
24799 +       args->br = br;
24800 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb);
24801 +       if (!wkq_err)
24802 +               return; /* success */
24803 +
24804 +       AuErr("wkq %d\n", wkq_err);
24805 +       atomic_dec(&br->br_count);
24806 +
24807 + out_args:
24808 +       kfree(args);
24809 + out:
24810 +       atomic_dec(&br->br_xino_running);
24811 +}
24812 +
24813 +/* ---------------------------------------------------------------------- */
24814 +
24815 +static int au_xino_do_write(au_writef_t write, struct file *file,
24816 +                           ino_t h_ino, ino_t ino)
24817 +{
24818 +       loff_t pos;
24819 +       ssize_t sz;
24820 +
24821 +       pos = h_ino;
24822 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
24823 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
24824 +               return -EFBIG;
24825 +       }
24826 +       pos *= sizeof(ino);
24827 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
24828 +       if (sz == sizeof(ino))
24829 +               return 0; /* success */
24830 +
24831 +       AuIOErr("write failed (%zd)\n", sz);
24832 +       return -EIO;
24833 +}
24834 +
24835 +/*
24836 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
24837 + * at the position of @h_ino.
24838 + * even if @ino is zero, it is written to the xinofile and means no entry.
24839 + * if the size of the xino file on a specific filesystem exceeds the watermark,
24840 + * try truncating it.
24841 + */
24842 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
24843 +                 ino_t ino)
24844 +{
24845 +       int err;
24846 +       unsigned int mnt_flags;
24847 +       struct au_branch *br;
24848 +
24849 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
24850 +                    || ((loff_t)-1) > 0);
24851 +
24852 +       mnt_flags = au_mntflags(sb);
24853 +       if (!au_opt_test(mnt_flags, XINO))
24854 +               return 0;
24855 +
24856 +       br = au_sbr(sb, bindex);
24857 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
24858 +                              h_ino, ino);
24859 +       if (!err) {
24860 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
24861 +                   && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
24862 +                       xino_try_trunc(sb, br);
24863 +               return 0; /* success */
24864 +       }
24865 +
24866 +       AuIOErr("write failed (%d)\n", err);
24867 +       return -EIO;
24868 +}
24869 +
24870 +/* ---------------------------------------------------------------------- */
24871 +
24872 +/* aufs inode number bitmap */
24873 +
24874 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
24875 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
24876 +{
24877 +       ino_t ino;
24878 +
24879 +       AuDebugOn(bit < 0 || page_bits <= bit);
24880 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
24881 +       return ino;
24882 +}
24883 +
24884 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
24885 +{
24886 +       AuDebugOn(ino < AUFS_FIRST_INO);
24887 +       ino -= AUFS_FIRST_INO;
24888 +       *pindex = ino / page_bits;
24889 +       *bit = ino % page_bits;
24890 +}
24891 +
24892 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
24893 +{
24894 +       int err;
24895 +       loff_t pos;
24896 +       ssize_t sz;
24897 +       struct au_sbinfo *sbinfo;
24898 +       struct file *xib;
24899 +       unsigned long *p;
24900 +
24901 +       sbinfo = au_sbi(sb);
24902 +       MtxMustLock(&sbinfo->si_xib_mtx);
24903 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
24904 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
24905 +
24906 +       if (pindex == sbinfo->si_xib_last_pindex)
24907 +               return 0;
24908 +
24909 +       xib = sbinfo->si_xib;
24910 +       p = sbinfo->si_xib_buf;
24911 +       pos = sbinfo->si_xib_last_pindex;
24912 +       pos *= PAGE_SIZE;
24913 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
24914 +       if (unlikely(sz != PAGE_SIZE))
24915 +               goto out;
24916 +
24917 +       pos = pindex;
24918 +       pos *= PAGE_SIZE;
24919 +       if (i_size_read(xib->f_dentry->d_inode) >= pos + PAGE_SIZE)
24920 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
24921 +       else {
24922 +               memset(p, 0, PAGE_SIZE);
24923 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
24924 +       }
24925 +       if (sz == PAGE_SIZE) {
24926 +               sbinfo->si_xib_last_pindex = pindex;
24927 +               return 0; /* success */
24928 +       }
24929 +
24930 + out:
24931 +       AuIOErr1("write failed (%zd)\n", sz);
24932 +       err = sz;
24933 +       if (sz >= 0)
24934 +               err = -EIO;
24935 +       return err;
24936 +}
24937 +
24938 +/* ---------------------------------------------------------------------- */
24939 +
24940 +int au_xino_write0(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
24941 +                  ino_t ino)
24942 +{
24943 +       int err, bit;
24944 +       unsigned long pindex;
24945 +       struct au_sbinfo *sbinfo;
24946 +
24947 +       if (!au_opt_test(au_mntflags(sb), XINO))
24948 +               return 0;
24949 +
24950 +       err = 0;
24951 +       if (ino) {
24952 +               sbinfo = au_sbi(sb);
24953 +               xib_calc_bit(ino, &pindex, &bit);
24954 +               AuDebugOn(page_bits <= bit);
24955 +               mutex_lock(&sbinfo->si_xib_mtx);
24956 +               err = xib_pindex(sb, pindex);
24957 +               if (!err) {
24958 +                       clear_bit(bit, sbinfo->si_xib_buf);
24959 +                       sbinfo->si_xib_next_bit = bit;
24960 +               }
24961 +               mutex_unlock(&sbinfo->si_xib_mtx);
24962 +       }
24963 +
24964 +       if (!err)
24965 +               err = au_xino_write(sb, bindex, h_ino, 0);
24966 +       return err;
24967 +}
24968 +
24969 +/* get an unused inode number from bitmap */
24970 +ino_t au_xino_new_ino(struct super_block *sb)
24971 +{
24972 +       ino_t ino;
24973 +       unsigned long *p, pindex, ul, pend;
24974 +       struct au_sbinfo *sbinfo;
24975 +       struct file *file;
24976 +       int free_bit, err;
24977 +
24978 +       if (!au_opt_test(au_mntflags(sb), XINO))
24979 +               return iunique(sb, AUFS_FIRST_INO);
24980 +
24981 +       sbinfo = au_sbi(sb);
24982 +       mutex_lock(&sbinfo->si_xib_mtx);
24983 +       p = sbinfo->si_xib_buf;
24984 +       free_bit = sbinfo->si_xib_next_bit;
24985 +       if (free_bit < page_bits && !test_bit(free_bit, p))
24986 +               goto out; /* success */
24987 +       free_bit = find_first_zero_bit(p, page_bits);
24988 +       if (free_bit < page_bits)
24989 +               goto out; /* success */
24990 +
24991 +       pindex = sbinfo->si_xib_last_pindex;
24992 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
24993 +               err = xib_pindex(sb, ul);
24994 +               if (unlikely(err))
24995 +                       goto out_err;
24996 +               free_bit = find_first_zero_bit(p, page_bits);
24997 +               if (free_bit < page_bits)
24998 +                       goto out; /* success */
24999 +       }
25000 +
25001 +       file = sbinfo->si_xib;
25002 +       pend = i_size_read(file->f_dentry->d_inode) / PAGE_SIZE;
25003 +       for (ul = pindex + 1; ul <= pend; ul++) {
25004 +               err = xib_pindex(sb, ul);
25005 +               if (unlikely(err))
25006 +                       goto out_err;
25007 +               free_bit = find_first_zero_bit(p, page_bits);
25008 +               if (free_bit < page_bits)
25009 +                       goto out; /* success */
25010 +       }
25011 +       BUG();
25012 +
25013 + out:
25014 +       set_bit(free_bit, p);
25015 +       sbinfo->si_xib_next_bit++;
25016 +       pindex = sbinfo->si_xib_last_pindex;
25017 +       mutex_unlock(&sbinfo->si_xib_mtx);
25018 +       ino = xib_calc_ino(pindex, free_bit);
25019 +       AuDbg("i%lu\n", (unsigned long)ino);
25020 +       return ino;
25021 + out_err:
25022 +       mutex_unlock(&sbinfo->si_xib_mtx);
25023 +       AuDbg("i0\n");
25024 +       return 0;
25025 +}
25026 +
25027 +/*
25028 + * read @ino from xinofile for the specified branch{@sb, @bindex}
25029 + * at the position of @h_ino.
25030 + * if @ino does not exist and @do_new is true, get new one.
25031 + */
25032 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
25033 +                ino_t *ino)
25034 +{
25035 +       int err;
25036 +       ssize_t sz;
25037 +       loff_t pos;
25038 +       struct file *file;
25039 +       struct au_sbinfo *sbinfo;
25040 +
25041 +       *ino = 0;
25042 +       if (!au_opt_test(au_mntflags(sb), XINO))
25043 +               return 0; /* no xino */
25044 +
25045 +       err = 0;
25046 +       sbinfo = au_sbi(sb);
25047 +       pos = h_ino;
25048 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
25049 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
25050 +               return -EFBIG;
25051 +       }
25052 +       pos *= sizeof(*ino);
25053 +
25054 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
25055 +       if (i_size_read(file->f_dentry->d_inode) < pos + sizeof(*ino))
25056 +               return 0; /* no ino */
25057 +
25058 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
25059 +       if (sz == sizeof(*ino))
25060 +               return 0; /* success */
25061 +
25062 +       err = sz;
25063 +       if (unlikely(sz >= 0)) {
25064 +               err = -EIO;
25065 +               AuIOErr("xino read error (%zd)\n", sz);
25066 +       }
25067 +
25068 +       return err;
25069 +}
25070 +
25071 +/* ---------------------------------------------------------------------- */
25072 +
25073 +/* create and set a new xino file */
25074 +
25075 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
25076 +{
25077 +       struct file *file;
25078 +       struct dentry *h_parent, *d;
25079 +       struct inode *h_dir;
25080 +       int err;
25081 +
25082 +       /*
25083 +        * at mount-time, and the xino file is the default path,
25084 +        * hinotify is disabled so we have no inotify events to ignore.
25085 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
25086 +        */
25087 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE,
25088 +                              S_IRUGO | S_IWUGO);
25089 +       if (IS_ERR(file)) {
25090 +               if (!silent)
25091 +                       AuErr("open %s(%ld)\n", fname, PTR_ERR(file));
25092 +               return file;
25093 +       }
25094 +
25095 +       /* keep file count */
25096 +       h_parent = dget_parent(file->f_dentry);
25097 +       h_dir = h_parent->d_inode;
25098 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
25099 +       /* mnt_want_write() is unnecessary here */
25100 +       err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
25101 +       mutex_unlock(&h_dir->i_mutex);
25102 +       dput(h_parent);
25103 +       if (unlikely(err)) {
25104 +               if (!silent)
25105 +                       AuErr("unlink %s(%d)\n", fname, err);
25106 +               goto out;
25107 +       }
25108 +
25109 +       err = -EINVAL;
25110 +       d = file->f_dentry;
25111 +       if (unlikely(sb == d->d_sb)) {
25112 +               if (!silent)
25113 +                       AuErr("%s must be outside\n", fname);
25114 +               goto out;
25115 +       }
25116 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
25117 +               if (!silent)
25118 +                       AuErr("xino doesn't support %s(%s)\n",
25119 +                             fname, au_sbtype(d->d_sb));
25120 +               goto out;
25121 +       }
25122 +       return file; /* success */
25123 +
25124 + out:
25125 +       fput(file);
25126 +       file = ERR_PTR(err);
25127 +       return file;
25128 +}
25129 +
25130 +/*
25131 + * find another branch who is on the same filesystem of the specified
25132 + * branch{@btgt}. search until @bend.
25133 + */
25134 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
25135 +                       aufs_bindex_t bend)
25136 +{
25137 +       aufs_bindex_t bindex;
25138 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
25139 +
25140 +       for (bindex = 0; bindex < btgt; bindex++)
25141 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
25142 +                       return bindex;
25143 +       for (bindex++; bindex <= bend; bindex++)
25144 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
25145 +                       return bindex;
25146 +       return -1;
25147 +}
25148 +
25149 +/* ---------------------------------------------------------------------- */
25150 +
25151 +/*
25152 + * initialize the xinofile for the specified branch @br
25153 + * at the place/path where @base_file indicates.
25154 + * test whether another branch is on the same filesystem or not,
25155 + * if @do_test is true.
25156 + */
25157 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
25158 +              struct file *base_file, int do_test)
25159 +{
25160 +       int err;
25161 +       ino_t ino;
25162 +       aufs_bindex_t bend, bindex;
25163 +       struct au_branch *shared_br, *b;
25164 +       struct file *file;
25165 +       struct super_block *tgt_sb;
25166 +
25167 +       shared_br = NULL;
25168 +       bend = au_sbend(sb);
25169 +       if (do_test) {
25170 +               tgt_sb = br->br_mnt->mnt_sb;
25171 +               for (bindex = 0; bindex <= bend; bindex++) {
25172 +                       b = au_sbr(sb, bindex);
25173 +                       if (tgt_sb == b->br_mnt->mnt_sb) {
25174 +                               shared_br = b;
25175 +                               break;
25176 +                       }
25177 +               }
25178 +       }
25179 +
25180 +       if (!shared_br || !shared_br->br_xino.xi_file) {
25181 +               struct au_xino_lock_dir ldir;
25182 +
25183 +               au_xino_lock_dir(sb, base_file, &ldir);
25184 +               /* mnt_want_write() is unnecessary here */
25185 +               file = au_xino_create2(base_file, NULL);
25186 +               au_xino_unlock_dir(&ldir);
25187 +               err = PTR_ERR(file);
25188 +               if (IS_ERR(file))
25189 +                       goto out;
25190 +               br->br_xino.xi_file = file;
25191 +       } else {
25192 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
25193 +               get_file(br->br_xino.xi_file);
25194 +       }
25195 +
25196 +       ino = AUFS_ROOT_INO;
25197 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
25198 +                              h_ino, ino);
25199 +       if (!err)
25200 +               return 0; /* success */
25201 +
25202 +
25203 + out:
25204 +       return err;
25205 +}
25206 +
25207 +/* ---------------------------------------------------------------------- */
25208 +
25209 +/* trucate a xino bitmap file */
25210 +
25211 +/* todo: slow */
25212 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
25213 +{
25214 +       int err, bit;
25215 +       ssize_t sz;
25216 +       unsigned long pindex;
25217 +       loff_t pos, pend;
25218 +       struct au_sbinfo *sbinfo;
25219 +       au_readf_t func;
25220 +       ino_t *ino;
25221 +       unsigned long *p;
25222 +
25223 +       err = 0;
25224 +       sbinfo = au_sbi(sb);
25225 +       p = sbinfo->si_xib_buf;
25226 +       func = sbinfo->si_xread;
25227 +       pend = i_size_read(file->f_dentry->d_inode);
25228 +       pos = 0;
25229 +       while (pos < pend) {
25230 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
25231 +               err = sz;
25232 +               if (unlikely(sz <= 0))
25233 +                       goto out;
25234 +
25235 +               err = 0;
25236 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
25237 +                       if (unlikely(*ino < AUFS_FIRST_INO))
25238 +                               continue;
25239 +
25240 +                       xib_calc_bit(*ino, &pindex, &bit);
25241 +                       AuDebugOn(page_bits <= bit);
25242 +                       err = xib_pindex(sb, pindex);
25243 +                       if (!err)
25244 +                               set_bit(bit, p);
25245 +                       else
25246 +                               goto out;
25247 +               }
25248 +       }
25249 +
25250 + out:
25251 +       return err;
25252 +}
25253 +
25254 +static int xib_restore(struct super_block *sb)
25255 +{
25256 +       int err;
25257 +       aufs_bindex_t bindex, bend;
25258 +       void *page;
25259 +
25260 +       err = -ENOMEM;
25261 +       page = (void *)__get_free_page(GFP_NOFS);
25262 +       if (unlikely(!page))
25263 +               goto out;
25264 +
25265 +       err = 0;
25266 +       bend = au_sbend(sb);
25267 +       for (bindex = 0; !err && bindex <= bend; bindex++)
25268 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
25269 +                       err = do_xib_restore
25270 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
25271 +               else
25272 +                       AuDbg("b%d\n", bindex);
25273 +       free_page((unsigned long)page);
25274 +
25275 + out:
25276 +       return err;
25277 +}
25278 +
25279 +int au_xib_trunc(struct super_block *sb)
25280 +{
25281 +       int err;
25282 +       ssize_t sz;
25283 +       loff_t pos;
25284 +       struct au_xino_lock_dir ldir;
25285 +       struct au_sbinfo *sbinfo;
25286 +       unsigned long *p;
25287 +       struct file *file;
25288 +
25289 +       err = 0;
25290 +       sbinfo = au_sbi(sb);
25291 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
25292 +               goto out;
25293 +
25294 +       file = sbinfo->si_xib;
25295 +       if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
25296 +               goto out;
25297 +
25298 +       au_xino_lock_dir(sb, file, &ldir);
25299 +       /* mnt_want_write() is unnecessary here */
25300 +       file = au_xino_create2(sbinfo->si_xib, NULL);
25301 +       au_xino_unlock_dir(&ldir);
25302 +       err = PTR_ERR(file);
25303 +       if (IS_ERR(file))
25304 +               goto out;
25305 +       fput(sbinfo->si_xib);
25306 +       sbinfo->si_xib = file;
25307 +
25308 +       p = sbinfo->si_xib_buf;
25309 +       memset(p, 0, PAGE_SIZE);
25310 +       pos = 0;
25311 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
25312 +       if (unlikely(sz != PAGE_SIZE)) {
25313 +               err = sz;
25314 +               AuIOErr("err %d\n", err);
25315 +               if (sz >= 0)
25316 +                       err = -EIO;
25317 +               goto out;
25318 +       }
25319 +
25320 +       mutex_lock(&sbinfo->si_xib_mtx);
25321 +       /* mnt_want_write() is unnecessary here */
25322 +       err = xib_restore(sb);
25323 +       mutex_unlock(&sbinfo->si_xib_mtx);
25324 +
25325 +out:
25326 +       return err;
25327 +}
25328 +
25329 +/* ---------------------------------------------------------------------- */
25330 +
25331 +/*
25332 + * xino mount option handlers
25333 + */
25334 +static au_readf_t find_readf(struct file *h_file)
25335 +{
25336 +       const struct file_operations *fop = h_file->f_op;
25337 +
25338 +       if (fop) {
25339 +               if (fop->read)
25340 +                       return fop->read;
25341 +               if (fop->aio_read)
25342 +                       return do_sync_read;
25343 +       }
25344 +       return ERR_PTR(-ENOSYS);
25345 +}
25346 +
25347 +static au_writef_t find_writef(struct file *h_file)
25348 +{
25349 +       const struct file_operations *fop = h_file->f_op;
25350 +
25351 +       if (fop) {
25352 +               if (fop->write)
25353 +                       return fop->write;
25354 +               if (fop->aio_write)
25355 +                       return do_sync_write;
25356 +       }
25357 +       return ERR_PTR(-ENOSYS);
25358 +}
25359 +
25360 +/* xino bitmap */
25361 +static void xino_clear_xib(struct super_block *sb)
25362 +{
25363 +       struct au_sbinfo *sbinfo;
25364 +
25365 +       sbinfo = au_sbi(sb);
25366 +       sbinfo->si_xread = NULL;
25367 +       sbinfo->si_xwrite = NULL;
25368 +       if (sbinfo->si_xib)
25369 +               fput(sbinfo->si_xib);
25370 +       sbinfo->si_xib = NULL;
25371 +       free_page((unsigned long)sbinfo->si_xib_buf);
25372 +       sbinfo->si_xib_buf = NULL;
25373 +}
25374 +
25375 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
25376 +{
25377 +       int err;
25378 +       loff_t pos;
25379 +       struct au_sbinfo *sbinfo;
25380 +       struct file *file;
25381 +
25382 +       sbinfo = au_sbi(sb);
25383 +       file = au_xino_create2(base, sbinfo->si_xib);
25384 +       err = PTR_ERR(file);
25385 +       if (IS_ERR(file))
25386 +               goto out;
25387 +       if (sbinfo->si_xib)
25388 +               fput(sbinfo->si_xib);
25389 +       sbinfo->si_xib = file;
25390 +       sbinfo->si_xread = find_readf(file);
25391 +       sbinfo->si_xwrite = find_writef(file);
25392 +
25393 +       err = -ENOMEM;
25394 +       if (!sbinfo->si_xib_buf)
25395 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
25396 +       if (unlikely(!sbinfo->si_xib_buf))
25397 +               goto out_unset;
25398 +
25399 +       sbinfo->si_xib_last_pindex = 0;
25400 +       sbinfo->si_xib_next_bit = 0;
25401 +       if (i_size_read(file->f_dentry->d_inode) < PAGE_SIZE) {
25402 +               pos = 0;
25403 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
25404 +                                 PAGE_SIZE, &pos);
25405 +               if (unlikely(err != PAGE_SIZE))
25406 +                       goto out_free;
25407 +       }
25408 +       err = 0;
25409 +       goto out; /* success */
25410 +
25411 + out_free:
25412 +       free_page((unsigned long)sbinfo->si_xib_buf);
25413 +       sbinfo->si_xib_buf = NULL;
25414 +       if (err >= 0)
25415 +               err = -EIO;
25416 + out_unset:
25417 +       fput(sbinfo->si_xib);
25418 +       sbinfo->si_xib = NULL;
25419 +       sbinfo->si_xread = NULL;
25420 +       sbinfo->si_xwrite = NULL;
25421 + out:
25422 +       return err;
25423 +}
25424 +
25425 +/* xino for each branch */
25426 +static void xino_clear_br(struct super_block *sb)
25427 +{
25428 +       aufs_bindex_t bindex, bend;
25429 +       struct au_branch *br;
25430 +
25431 +       bend = au_sbend(sb);
25432 +       for (bindex = 0; bindex <= bend; bindex++) {
25433 +               br = au_sbr(sb, bindex);
25434 +               if (!br || !br->br_xino.xi_file)
25435 +                       continue;
25436 +
25437 +               fput(br->br_xino.xi_file);
25438 +               br->br_xino.xi_file = NULL;
25439 +       }
25440 +}
25441 +
25442 +static int au_xino_set_br(struct super_block *sb, struct file *base)
25443 +{
25444 +       int err;
25445 +       ino_t ino;
25446 +       aufs_bindex_t bindex, bend, bshared;
25447 +       struct {
25448 +               struct file *old, *new;
25449 +       } *fpair, *p;
25450 +       struct au_branch *br;
25451 +       struct inode *inode;
25452 +       au_writef_t writef;
25453 +
25454 +       err = -ENOMEM;
25455 +       bend = au_sbend(sb);
25456 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
25457 +       if (unlikely(!fpair))
25458 +               goto out;
25459 +
25460 +       inode = sb->s_root->d_inode;
25461 +       ino = AUFS_ROOT_INO;
25462 +       writef = au_sbi(sb)->si_xwrite;
25463 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
25464 +               br = au_sbr(sb, bindex);
25465 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
25466 +               if (bshared >= 0) {
25467 +                       /* shared xino */
25468 +                       *p = fpair[bshared];
25469 +                       get_file(p->new);
25470 +               }
25471 +
25472 +               if (!p->new) {
25473 +                       /* new xino */
25474 +                       p->old = br->br_xino.xi_file;
25475 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
25476 +                       err = PTR_ERR(p->new);
25477 +                       if (IS_ERR(p->new)) {
25478 +                               p->new = NULL;
25479 +                               goto out_pair;
25480 +                       }
25481 +               }
25482 +
25483 +               err = au_xino_do_write(writef, p->new,
25484 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
25485 +               if (unlikely(err))
25486 +                       goto out_pair;
25487 +       }
25488 +
25489 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
25490 +               br = au_sbr(sb, bindex);
25491 +               if (br->br_xino.xi_file)
25492 +                       fput(br->br_xino.xi_file);
25493 +               get_file(p->new);
25494 +               br->br_xino.xi_file = p->new;
25495 +       }
25496 +
25497 + out_pair:
25498 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
25499 +               if (p->new)
25500 +                       fput(p->new);
25501 +               else
25502 +                       break;
25503 +       kfree(fpair);
25504 + out:
25505 +       return err;
25506 +}
25507 +
25508 +void au_xino_clr(struct super_block *sb)
25509 +{
25510 +       struct au_sbinfo *sbinfo;
25511 +
25512 +       au_xigen_clr(sb);
25513 +       xino_clear_xib(sb);
25514 +       xino_clear_br(sb);
25515 +       sbinfo = au_sbi(sb);
25516 +       /* lvalue, do not call au_mntflags() */
25517 +       au_opt_clr(sbinfo->si_mntflags, XINO);
25518 +}
25519 +
25520 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
25521 +{
25522 +       int err, skip;
25523 +       struct dentry *parent, *cur_parent;
25524 +       struct qstr *dname, *cur_name;
25525 +       struct file *cur_xino;
25526 +       struct inode *dir;
25527 +       struct au_sbinfo *sbinfo;
25528 +
25529 +       err = 0;
25530 +       sbinfo = au_sbi(sb);
25531 +       parent = dget_parent(xino->file->f_dentry);
25532 +       if (remount) {
25533 +               skip = 0;
25534 +               dname = &xino->file->f_dentry->d_name;
25535 +               cur_xino = sbinfo->si_xib;
25536 +               if (cur_xino) {
25537 +                       cur_parent = dget_parent(cur_xino->f_dentry);
25538 +                       cur_name = &cur_xino->f_dentry->d_name;
25539 +                       skip = (cur_parent == parent
25540 +                               && dname->len == cur_name->len
25541 +                               && !memcmp(dname->name, cur_name->name,
25542 +                                          dname->len));
25543 +                       dput(cur_parent);
25544 +               }
25545 +               if (skip)
25546 +                       goto out;
25547 +       }
25548 +
25549 +       au_opt_set(sbinfo->si_mntflags, XINO);
25550 +       dir = parent->d_inode;
25551 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
25552 +       /* mnt_want_write() is unnecessary here */
25553 +       err = au_xino_set_xib(sb, xino->file);
25554 +       if (!err)
25555 +               err = au_xigen_set(sb, xino->file);
25556 +       if (!err)
25557 +               err = au_xino_set_br(sb, xino->file);
25558 +       mutex_unlock(&dir->i_mutex);
25559 +       if (!err)
25560 +               goto out; /* success */
25561 +
25562 +       /* reset all */
25563 +       AuIOErr("failed creating xino(%d).\n", err);
25564 +
25565 + out:
25566 +       dput(parent);
25567 +       return err;
25568 +}
25569 +
25570 +/* ---------------------------------------------------------------------- */
25571 +
25572 +/*
25573 + * create a xinofile at the default place/path.
25574 + */
25575 +struct file *au_xino_def(struct super_block *sb)
25576 +{
25577 +       struct file *file;
25578 +       char *page, *p;
25579 +       struct au_branch *br;
25580 +       struct super_block *h_sb;
25581 +       struct path path;
25582 +       aufs_bindex_t bend, bindex, bwr;
25583 +
25584 +       br = NULL;
25585 +       bend = au_sbend(sb);
25586 +       bwr = -1;
25587 +       for (bindex = 0; bindex <= bend; bindex++) {
25588 +               br = au_sbr(sb, bindex);
25589 +               if (au_br_writable(br->br_perm)
25590 +                   && !au_test_fs_bad_xino(br->br_mnt->mnt_sb)) {
25591 +                       bwr = bindex;
25592 +                       break;
25593 +               }
25594 +       }
25595 +
25596 +       if (bwr >= 0) {
25597 +               file = ERR_PTR(-ENOMEM);
25598 +               page = __getname();
25599 +               if (unlikely(!page))
25600 +                       goto out;
25601 +               path.mnt = br->br_mnt;
25602 +               path.dentry = au_h_dptr(sb->s_root, bwr);
25603 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
25604 +               file = (void *)p;
25605 +               if (!IS_ERR(p)) {
25606 +                       strcat(p, "/" AUFS_XINO_FNAME);
25607 +                       AuDbg("%s\n", p);
25608 +                       file = au_xino_create(sb, p, /*silent*/0);
25609 +                       if (!IS_ERR(file))
25610 +                               au_xino_brid_set(sb, br->br_id);
25611 +               }
25612 +               __putname(page);
25613 +       } else {
25614 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
25615 +               if (IS_ERR(file))
25616 +                       goto out;
25617 +               h_sb = file->f_dentry->d_sb;
25618 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
25619 +                       AuErr("xino doesn't support %s(%s)\n",
25620 +                             AUFS_XINO_DEFPATH, au_sbtype(h_sb));
25621 +                       fput(file);
25622 +                       file = ERR_PTR(-EINVAL);
25623 +               }
25624 +               if (!IS_ERR(file))
25625 +                       au_xino_brid_set(sb, -1);
25626 +       }
25627 +
25628 + out:
25629 +       return file;
25630 +}
25631 +
25632 +/* ---------------------------------------------------------------------- */
25633 +
25634 +int au_xino_path(struct seq_file *seq, struct file *file)
25635 +{
25636 +       int err;
25637 +
25638 +       err = au_seq_path(seq, &file->f_path);
25639 +       if (unlikely(err < 0))
25640 +               goto out;
25641 +
25642 +       err = 0;
25643 +#define Deleted "\\040(deleted)"
25644 +       seq->count -= sizeof(Deleted) - 1;
25645 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
25646 +                        sizeof(Deleted) - 1));
25647 +#undef Deleted
25648 +
25649 + out:
25650 +       return err;
25651 +}
25652 diff --git a/fs/namei.c b/fs/namei.c
25653 index bbc15c2..db581b4 100644
25654 --- a/fs/namei.c
25655 +++ b/fs/namei.c
25656 @@ -1196,7 +1196,7 @@ out:
25657   * needs parent already locked. Doesn't follow mounts.
25658   * SMP-safe.
25659   */
25660 -static struct dentry *lookup_hash(struct nameidata *nd)
25661 +struct dentry *lookup_hash(struct nameidata *nd)
25662  {
25663         int err;
25664  
25665 @@ -1206,7 +1206,7 @@ static struct dentry *lookup_hash(struct nameidata *nd)
25666         return __lookup_hash(&nd->last, nd->path.dentry, nd);
25667  }
25668  
25669 -static int __lookup_one_len(const char *name, struct qstr *this,
25670 +int __lookup_one_len(const char *name, struct qstr *this,
25671                 struct dentry *base, int len)
25672  {
25673         unsigned long hash;
25674 diff --git a/fs/splice.c b/fs/splice.c
25675 index 4ed0ba4..2fb3d17 100644
25676 --- a/fs/splice.c
25677 +++ b/fs/splice.c
25678 @@ -888,8 +888,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
25679  /*
25680   * Attempt to initiate a splice from pipe to file.
25681   */
25682 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
25683 -                          loff_t *ppos, size_t len, unsigned int flags)
25684 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
25685 +                   loff_t *ppos, size_t len, unsigned int flags)
25686  {
25687         int ret;
25688  
25689 @@ -912,9 +912,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
25690  /*
25691   * Attempt to initiate a splice from a file to a pipe.
25692   */
25693 -static long do_splice_to(struct file *in, loff_t *ppos,
25694 -                        struct pipe_inode_info *pipe, size_t len,
25695 -                        unsigned int flags)
25696 +long do_splice_to(struct file *in, loff_t *ppos,
25697 +                 struct pipe_inode_info *pipe, size_t len,
25698 +                 unsigned int flags)
25699  {
25700         int ret;
25701  
25702 diff --git a/include/linux/Kbuild b/include/linux/Kbuild
25703 index 106c3ba..d0c7262 100644
25704 --- a/include/linux/Kbuild
25705 +++ b/include/linux/Kbuild
25706 @@ -34,6 +34,7 @@ header-y += atmppp.h
25707  header-y += atmsap.h
25708  header-y += atmsvc.h
25709  header-y += atm_zatm.h
25710 +header-y += aufs_type.h
25711  header-y += auto_fs4.h
25712  header-y += ax25.h
25713  header-y += b1lli.h
25714 diff --git a/include/linux/aufs_type.h b/include/linux/aufs_type.h
25715 new file mode 100644
25716 index 0000000..5c98a5f
25717 --- /dev/null
25718 +++ b/include/linux/aufs_type.h
25719 @@ -0,0 +1,100 @@
25720 +/*
25721 + * Copyright (C) 2005-2009 Junjiro R. Okajima
25722 + *
25723 + * This program, aufs is free software; you can redistribute it and/or modify
25724 + * it under the terms of the GNU General Public License as published by
25725 + * the Free Software Foundation; either version 2 of the License, or
25726 + * (at your option) any later version.
25727 + */
25728 +
25729 +#ifndef __AUFS_TYPE_H__
25730 +#define __AUFS_TYPE_H__
25731 +
25732 +#include <linux/ioctl.h>
25733 +
25734 +#define AUFS_VERSION   "2-29"
25735 +
25736 +/* todo? move this to linux-2.6.19/include/magic.h */
25737 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
25738 +
25739 +/* ---------------------------------------------------------------------- */
25740 +
25741 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
25742 +/* some environments treat 'char' as 'unsigned char' by default */
25743 +typedef signed char aufs_bindex_t;
25744 +#define AUFS_BRANCH_MAX 127
25745 +#else
25746 +typedef short aufs_bindex_t;
25747 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
25748 +#define AUFS_BRANCH_MAX 511
25749 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
25750 +#define AUFS_BRANCH_MAX 1023
25751 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
25752 +#define AUFS_BRANCH_MAX 32767
25753 +#endif
25754 +#endif
25755 +
25756 +#ifdef __KERNEL__
25757 +#ifndef AUFS_BRANCH_MAX
25758 +#error unknown CONFIG_AUFS_BRANCH_MAX value
25759 +#endif
25760 +#endif /* __KERNEL__ */
25761 +
25762 +/* ---------------------------------------------------------------------- */
25763 +
25764 +#define AUFS_NAME              "aufs"
25765 +#define AUFS_FSTYPE            AUFS_NAME
25766 +
25767 +#define AUFS_ROOT_INO          2
25768 +#define AUFS_FIRST_INO         11
25769 +
25770 +#define AUFS_WH_PFX            ".wh."
25771 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
25772 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
25773 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
25774 +#define AUFS_XINO_TRUNC_INIT   64 /* blocks */
25775 +#define AUFS_XINO_TRUNC_STEP   4  /* blocks */
25776 +#define AUFS_DIRWH_DEF         3
25777 +#define AUFS_RDCACHE_DEF       10 /* seconds */
25778 +#define AUFS_RDBLK_DEF         512 /* bytes */
25779 +#define AUFS_RDHASH_DEF                32
25780 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
25781 +#define AUFS_NWKQ_DEF          4
25782 +#define AUFS_MFS_SECOND_DEF    30 /* seconds */
25783 +#define AUFS_PLINK_WARN                100 /* number of plinks */
25784 +
25785 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
25786 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
25787 +
25788 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
25789 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
25790 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
25791 +
25792 +/* doubly whiteouted */
25793 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
25794 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
25795 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
25796 +
25797 +/* branch permission */
25798 +#define AUFS_BRPERM_RW         "rw"
25799 +#define AUFS_BRPERM_RO         "ro"
25800 +#define AUFS_BRPERM_RR         "rr"
25801 +#define AUFS_BRPERM_WH         "wh"
25802 +#define AUFS_BRPERM_NLWH       "nolwh"
25803 +#define AUFS_BRPERM_ROWH       AUFS_BRPERM_RO "+" AUFS_BRPERM_WH
25804 +#define AUFS_BRPERM_RRWH       AUFS_BRPERM_RR "+" AUFS_BRPERM_WH
25805 +#define AUFS_BRPERM_RWNLWH     AUFS_BRPERM_RW "+" AUFS_BRPERM_NLWH
25806 +
25807 +/* ---------------------------------------------------------------------- */
25808 +
25809 +/* ioctl */
25810 +enum {
25811 +       AuCtl_PLINK_MAINT,
25812 +       AuCtl_PLINK_CLEAN
25813 +};
25814 +
25815 +#define AuCtlType              'A'
25816 +#define AUFS_CTL_PLINK_MAINT   _IO(AuCtlType, AuCtl_PLINK_MAINT)
25817 +#define AUFS_CTL_PLINK_CLEAN   _IO(AuCtlType, AuCtl_PLINK_CLEAN)
25818 +
25819 +#endif /* __AUFS_TYPE_H__ */
25820 diff --git a/include/linux/namei.h b/include/linux/namei.h
25821 index fc2e035..182d43b 100644
25822 --- a/include/linux/namei.h
25823 +++ b/include/linux/namei.h
25824 @@ -75,6 +75,9 @@ extern struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry
25825  extern struct file *nameidata_to_filp(struct nameidata *nd, int flags);
25826  extern void release_open_intent(struct nameidata *);
25827  
25828 +extern struct dentry *lookup_hash(struct nameidata *nd);
25829 +extern int __lookup_one_len(const char *name, struct qstr *this,
25830 +                           struct dentry *base, int len);
25831  extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
25832  extern struct dentry *lookup_one_noperm(const char *, struct dentry *);
25833  
25834 diff --git a/include/linux/splice.h b/include/linux/splice.h
25835 index 528dcb9..5123bc6 100644
25836 --- a/include/linux/splice.h
25837 +++ b/include/linux/splice.h
25838 @@ -71,4 +71,10 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
25839  extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
25840                                       splice_direct_actor *);
25841  
25842 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
25843 +                          loff_t *ppos, size_t len, unsigned int flags);
25844 +extern long do_splice_to(struct file *in, loff_t *ppos,
25845 +                        struct pipe_inode_info *pipe, size_t len,
25846 +                        unsigned int flags);
25847 +
25848  #endif
This page took 1.917991 seconds and 4 git commands to generate.