1 diff --git a/Documentation/ABI/testing/debugfs-aufs b/Documentation/ABI/testing/debugfs-aufs
5 +++ b/Documentation/ABI/testing/debugfs-aufs
7 +What: /debug/aufs/si_<id>/
9 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
11 + Under /debug/aufs, a directory named si_<id> is created
12 + per aufs mount, where <id> is a unique id generated
15 +What: /debug/aufs/si_<id>/xib
17 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
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.
25 +What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
27 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
29 + It shows the consumed blocks by xino (External Inode Number
30 + Translation Table), its link count, block size and file
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.
36 +What: /debug/aufs/si_<id>/xigen
38 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
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
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
49 index 0000000..ca49330
51 +++ b/Documentation/ABI/testing/sysfs-aufs
53 +What: /sys/fs/aufs/si_<id>/
55 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
57 + Under /sys/fs/aufs, a directory named si_<id> is created
58 + per aufs mount, where <id> is a unique id generated
61 +What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
63 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
65 + It shows the abolute path of a member directory (which
66 + is called branch) in aufs, and its permission.
68 +What: /sys/fs/aufs/si_<id>/xi_path
70 +Contact: J. R. Okajima <hooanon05@yahoo.co.jp>
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
80 index 0000000..ed68e31
82 +++ b/Documentation/filesystems/aufs/README
85 +Aufs2 -- advanced multi layered unification filesystem version 2
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.
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
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
109 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
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.
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.
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
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
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
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.
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
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
170 +- intent.open/create (file open in a single lookup)
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.
179 +- light version, without branch manipulation. (unnecessary?)
180 +- copyup in userspace
181 +- inotify in userspace
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
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.
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.
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.
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,
216 +$ git clone --reference /your/linux-2.6/git/tree \
217 + http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-2.6.git \
219 +- if you don't have linux-2.6 GIT tree, then remove "--reference ..."
221 +$ git checkout origin/aufs2-xx # for instance, aufs2-27 for linux-2.6.27
222 + # aufs2 (no -xx) for the latest -rc version.
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.
233 +$ git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-util.git \
236 +- no particular tag/branch currently.
239 +3. Configuration and Compilation
240 +----------------------------------------
242 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS.
243 +- set other aufs configurations if necessary.
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.
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
256 +----------------------------------------
257 +At first, make sure aufs2-util are installed, and please read the aufs
258 +manual, ./Documentation/filesystems/aufs/aufs.5.
262 +$ mkdir /tmp/rw /tmp/aufs
263 +# mount -t aufs -o br=/tmp/rw:${HOME}=ro none /tmp/aufs
265 +Here is another example. The result is equivalent.
266 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
268 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
269 +# mount -o remount,append:${HOME} /tmp/aufs
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
277 +Aufs mount options are described in aufs.5.
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.
285 +----------------------------------------
286 +When you have any problems or strange behaviour in aufs, please let me
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
303 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
304 +and Feature Requests) on SourceForge. Please join and write to
309 +----------------------------------------
310 +Thanks to everyone who have tried and are using aufs, whoever
311 +have reported a bug or any feedback.
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
322 +William Grant made a donation (2009/3).
324 +Thank you very much.
325 +Donations are always, including future donations, very important and
326 +helpful for me to keep on developing aufs.
330 +----------------------------------------
331 +If you are an experienced user, no explanation is needed. Aufs is
332 +just a linux filesystem.
337 +# Local variables: ;
340 diff --git a/Documentation/filesystems/aufs/aufs.5 b/Documentation/filesystems/aufs/aufs.5
342 index 0000000..0b9732f
344 +++ b/Documentation/filesystems/aufs/aufs.5
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
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
373 +.\" end of macro definitions
375 +.\" ----------------------------------------------------------------------
376 +.TH aufs 5 \*[AUFS_VERSION] Linux "Linux Aufs User\[aq]s Manual"
378 +aufs \- advanced multi layered unification filesystem. version \*[AUFS_VERSION]
380 +.\" ----------------------------------------------------------------------
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.
392 +.\" ----------------------------------------------------------------------
394 +At mount-time, the order of interpreting options is,
397 +simple flags, except xino/noxino and udba=inotify
407 +the options are interpreted in the given order,
412 +whiteout-base(\*[AUFS_WH_BASE]) and
413 +whplink-dir(\*[AUFS_WH_PLINKDIR]) if necessary
417 +.B br:BRANCH[:BRANCH ...] (dirs=BRANCH[:BRANCH ...])
419 +(cf. Branch Syntax).
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
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
432 +.B [ add | ins ]:index:BRANCH
434 +The index begins with 0.
436 +whiteout-base(\*[AUFS_WH_BASE]) and
437 +whplink-dir(\*[AUFS_WH_PLINKDIR]) if necessary.
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.
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).
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.
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
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.
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)
478 +on the branch, the process may or may not
479 +be able to modify its mapped memory region after modifying branch
481 +(cf. Branch Syntax).
485 +equivalent to \[oq]add:(last index + 1):BRANCH\[cq].
486 +(cf. Branch Syntax).
490 +equivalent to \[oq]add:0:BRANCH.\[cq]
491 +(cf. Branch Syntax).
495 +Use external inode number bitmap and translation table.
496 +When CONFIG_AUFS_EXPORT is enabled, external inode generation table too.
498 +<FirstWritableBranch>/\*[AUFS_XINO_FNAME] by default, or
499 +\*[AUFS_XINO_DEFPATH].
500 +Comma character in filename is not allowed.
502 +The files are created per an aufs and per a branch filesystem, and
504 +cannot find this file, but it exists and is read/written frequently by
506 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
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
516 +Stop using external inode number bitmap and translation table.
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
522 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
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).
533 +Stop truncating the external inode number bitmap file when you delete
535 +(cf. External Inode Number Bitmap, Translation Table and Generation Table).
538 +.B create_policy | create=CREATE_POLICY
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).
549 +Print some information.
550 +Currently, it is only busy file (or inode) at deleting a branch.
553 +.B noverbose | quiet | q | silent
554 +Disable \[oq]verbose\[cq] option.
555 +This is default value.
559 +df(1)/statfs(2) returns the total number of blocks and inodes of
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.
566 +Disable \[oq]sum\[cq] option.
567 +This is default value.
571 +Watermark to remove a dir actually at rmdir(2) and rename(2).
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.
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.
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
589 +This value is ignored when the branch is NFS.
590 +The default value is \*[AUFS_DIRWH_DEF].
597 +Specifies a size of internal VDIR block which is allocated at a time in
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
605 +(cf. Virtual or Vertical Directory Block).
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
618 +(the maximum limit depends on your system. at least 128KB is available
620 +(cf. Virtual or Vertical Directory Block).
626 +Specifies to use \[oq]pseudo link\[cq] feature or not.
627 +The default is \[oq]plink\[cq] which means use this feature.
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,
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.
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).
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
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
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
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.
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).
681 +.\" ----------------------------------------------------------------------
682 +.SH Module Parameters
685 +The number of kernel thread named \*[AUFS_WKQ_NAME].
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].
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.
701 +If you enable CONFIG_SYSFS, you can check this value through
702 +<sysfs>/module/aufs/parameters/nwkq.
707 +Specifies to use the branch path data file under sysfs or not.
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.
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.
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.
731 +If CONFIG_SYSFS is disable, this paramater is always set to 0.
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].
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).
748 +.\" ----------------------------------------------------------------------
749 +.SH Entries under Sysfs and Debugfs
750 +See linux/Documentation/ABI/*/{sys,debug}fs-aufs.
752 +.\" ----------------------------------------------------------------------
755 +.B dir_path[ =permission [ + attribute ] ]
757 +.B permission := rw | ro | rr
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
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.
771 +Cramfs in linux stable release has strange inodes and it makes aufs
772 +confused. For example,
774 +$ mkdir -p w/d1 w/d2
778 +$ sudo mount -t cramfs -o ro,loop cramfs /mnt
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
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.
795 +A branch should not be shared as the writable branch between multiple
796 +aufs. A readonly branch can be shared.
798 +The maximum number of branches is configurable at compile time.
799 +The current value is \*[AUFS_BRANCH_MAX] which depends upon
802 +When an unknown permission or attribute is given, aufs sets ro to that
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.
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.
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].
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.
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].
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
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.
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
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
875 +.\" create/modify a file just under the aufs root dir.
877 +.\" aufs aquires a write\-lock for the parent directory, ie. the root dir.
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
883 +.\" FUSE in kernel\-space converts and redirects the statfs request to the
886 +.\" the user\-space statfs handler will call [f]statvfs(3).
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
892 +.\" Then a deadlock occurs.
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.
899 +.\" struct statvfs stvfs;
903 +.\" statvfs(..., &stvfs)
905 +.\" fstatvfs(..., &stvfs)
906 +.\" stvfs.f_fsid = 0
909 +.\" statfs_handler(const char *path, struct statvfs *arg)
911 +.\" struct statfs stfs
913 +.\" memcpy(arg, &stvfs, sizeof(stvfs))
915 +.\" statfs(..., &stfs)
917 +.\" fstatfs(..., &stfs)
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 */
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
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
940 +will be \*[AUFS_XINO_DEFPATH].
941 +.\" A user who executes mount(8) needs the privilege to create xino
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
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]
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
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).
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,
970 +/dev/hda14 2599968 203127 2396841 8% /branch_fs
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
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.
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
994 +xib and xigen are in the format of,
997 +<blocks>x<block size> <file size>
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.
1008 +The rests are hidden xino file information in the format of,
1011 +<file count>, <blocks>x<block size> <file size>
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.
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
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.
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
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
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.
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.
1046 +rmdir failure, recursive chmod/chown/etc to a large and deep directory
1048 +And some applications will not work correctly.
1049 +.\" When the inode number has been changed, your system
1051 +If you want to change the xino default path, use xino mount option.
1053 +After you add branches, the persistence of inode number may not be
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.
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.
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.
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.
1082 +Once you unmount aufs, the plink info for that aufs kept in memory are totally
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.
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
1096 +Since this utility access your branches directly, you cannot hide them by
1097 +\[oq]mount \-\-bind /tmp /branch\[cq] or something.
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.
1106 +# auplink /your/aufs/root flush
1107 +# umount /your/aufs/root
1109 +# auplink /your/aufs/root flush
1110 +# mount -o remount,mod:/your/writable/branch=ro /your/aufs/root
1112 +# auplink /your/aufs/root flush
1113 +# mount -o remount,noplink /your/aufs/root
1115 +# auplink /your/aufs/root flush
1116 +# mount -o remount,del:/your/aufs/branch /your/aufs/root
1118 +# auplink /your/aufs/root flush
1119 +# mount -o remount,append:/your/aufs/branch /your/aufs/root
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.
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.
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.
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.
1150 +If you do not want this feature, use \[oq]noplink\[cq] mount option.
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
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
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
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
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.
1212 +This feature will not work for squashfs before version 3.2 since its
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.
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
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.
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.
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.
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
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
1256 +# d=/tmp/.aufs.hide
1258 +# for i in $branches_you_want_to_hide
1260 +> mount -n --bind $d $i
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
1269 +# umount -n $branches_you_want_to_unbound
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.
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.
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
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.
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.
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.
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.
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
1325 +So it is recommended to leave udba default option usually, and set it
1326 +to inotify by remount when you need it.
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
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.
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.
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
1350 +Aufs cannot stop such direct rmdir, but produces a warning about it.
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.
1356 +.\" ----------------------------------------------------------------------
1357 +.SH Linux Inotify Limitation
1358 +Unfortunately, current inotify (linux\-2.6.18) has some limitations,
1359 +and aufs must derive it.
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.
1366 +.SS IN_ATTRIB, updating nlink
1367 +When the link count of a file on a branch is incremented by link(2)
1369 +inotify fires IN_CREATE to the parent
1370 +directory, but IN_ATTRIB to the file. So the aufs inode nlink may
1373 +.SS IN_DELETE, removing file on NFS
1374 +When a file on a NFS branch is deleted directly, inotify may or may
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
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
1387 +And aufs cannot receive the events anymore. So aufs may show you
1388 +incorrect data about the file/dir.
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.
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.
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.
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.
1418 +For instance, there are 97 files under my /bin, and the total name
1419 +length is 597 bytes.
1422 +$ \\ls -1 /bin | wc
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.
1437 +If your directory has millions of files, the you will need to specify
1438 +rdblk= and rdhash=.
1441 +$ ls -U /mnt/rotating-rust | wc -l
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
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.
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
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.
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
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.
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.
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.
1500 +/aufs = /rw1 + /ro+wh/diropq + /rw2
1501 +/aufs = /rw1 + /ro+wh/wh.tgt + /rw2
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.
1513 +/aufs = /rw1/diropq + /rw2
1514 +/aufs = /rw1/wh.tgt + /rw2
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
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
1532 +.SS Policies for Creating
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.
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
1546 +mkdir(2) systemcall is an exception. When you create 10 new directories,
1547 +all are created on the same branch.
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.
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.
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.
1577 +.SS Policies for Copy-Up
1580 +.B cpup=tdp | top\-down\-parent
1581 +Equivalent to the same named policy for create.
1582 +This is the default policy.
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.
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.
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.
1600 +There are some limitations or requirements.
1603 +The branch filesystem must support NFS-exporting.
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.
1609 +If the size of the NFS file handle on your branch filesystem is large,
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.
1616 +The External Inode Number Bitmap, Translation Table and Generation Table
1618 +required since NFS file
1619 +handle is based upon inode number. The mount option \[oq]xino\[cq] is enabled
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
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).
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
1641 +But when you have large ram or you do not have such large
1642 +directory, it is not effective.
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.
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.
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.
1667 +And there is a simple shell
1668 +script \[oq]unionctl\[cq] under sample subdirectory, which is compatible with
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
1683 +Aufs uses the external inode number bitmap and translation table by
1686 +The default branch permission for the first branch is \[oq]rw\[cq], and the
1687 +rest is \[oq]ro.\[cq]
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
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]
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]
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]
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.
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
1730 +The test for a non-directory is more simple. It checks only the
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).
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.
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.
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
1752 +Remember this utility is highly depending upon mount(8) in
1753 +util\-linux\-2.12p package, and you need to mount /proc.
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
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.
1762 +When you open a directory, aufs will open several directories
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.
1768 +The sub-mount under the branch
1769 +of local filesystem
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.
1777 +# sudo mount /dev/sdaXX /ro_branch
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
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
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.
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.
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.
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.
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.
1811 +# mount -no remount,ro /
1812 +# for i in $writable_branches
1813 +# do mount -no remount,ro $i
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
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).
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
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.
1849 +.\" ----------------------------------------------------------------------
1851 +The mount options are interpreted from left to right at remount-time.
1853 +shows how the options are handled. (assuming /sbin/mount.aufs was
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,\\
1861 + xino=/day1/xino,\\
1865 +none on /u type aufs (rw,xino=/day1/xino,br:/day1=rw:/base=ro)
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
1875 +(there is no warning)
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
1884 +.\" And the next example shows the modifications to the files under /rw branch
1885 +.\" will be copied-up to /new/a.
1887 +.\" Todo: test multiple writable branches policy. cpup=nearest, cpup=exist_parent.
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)
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)
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)
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)
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
1911 +In order to exclude the specific dir from aufs, try bind mounting.
1913 +And there is a good sample which is for network booted diskless machines. See
1916 +.\" ----------------------------------------------------------------------
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
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
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.
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.
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.
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]
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.
1955 +.\" .SH Current Limitation
1957 +.\" ----------------------------------------------------------------------
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.
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).
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.
1982 +.\" lists the possible exit status values or a program and the conditions that
1983 +.\" cause these values to be returned.
1986 +.\" describes the grammar of any sublanguage this implements.
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.
1997 +.\" lists all environment variables that affect your program or function and how
1998 +.\" they affect it.
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!
2009 +.\" describes any standards or conventions this implements.
2012 +.\" provides miscellaneous notes.
2015 +.\" lists limitations, known defects or inconveniences, and other questionable
2019 +Copyright \(co 2005\-2009 Junjiro R. Okajima
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
2031 +++ b/Documentation/filesystems/aufs/design/01intro.txt
2034 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
2042 +----------------------------------------
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.
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
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.
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
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
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).
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.
2108 +Several characters/aspects of aufs
2109 +----------------------------------------------------------------------
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
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.
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
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.
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.
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
2165 +++ b/Documentation/filesystems/aufs/design/02struct.txt
2168 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
2175 +Basic Aufs Internal Structure
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.
2186 +- /ro/fileA exists but /rw/fileA
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,
2193 +This style of an array is essentially same to the aufs
2194 +superblock/inode/dentry/file objects.
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
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
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
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.
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.
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
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.
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
2244 + See below, Pseudo-link.
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.
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.
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.
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
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
2281 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
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.
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.
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.
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.
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
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
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.
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.
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.
2347 +Basically the whiteout represents "logical deletion" which stops aufs to
2348 +lookup further, but also it represents "dir is opaque" which also stop
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
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
2376 +++ b/Documentation/filesystems/aufs/design/03lookup.txt
2379 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
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.
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.
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.
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
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.
2428 +4. call the actual operation (create, unlink, etc.)
2429 +5. unlock the parent dir
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
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
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.
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
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
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
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
2477 +++ b/Documentation/filesystems/aufs/design/04branch.txt
2480 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
2487 +Branch Manipulation
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.
2494 +----------------------------------------------------------------------
2495 +o Confirm the adding dir exists outside of aufs, including loopback
2497 +- and other various attributes...
2498 +o Initialize the xino file and whiteout bases if necessary.
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
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.
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).
2525 + For the cached one, aufs checks whether the same named entry exists on
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.
2532 + If any file on the deleting branch is opened by aufs, then aufs
2536 +Modify the Permission of a Branch
2537 +----------------------------------------------------------------------
2538 +o Re-initialize or remove the xino file and whiteout bases if necessary.
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
2550 +++ b/Documentation/filesystems/aufs/design/05wbr_policy.txt
2553 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
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.
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.
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.
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
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
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
2613 +++ b/Documentation/filesystems/aufs/design/06fmode_exec.txt
2616 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
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.
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
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
2643 +++ b/Documentation/filesystems/aufs/design/07mmap.txt
2646 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
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
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
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
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.
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.
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
2693 +++ b/Documentation/filesystems/aufs/design/08export.txt
2696 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
2704 +Export Aufs via NFS
2705 +----------------------------------------------------------------------
2706 +Here is an approach.
2707 +- like xino/xib, add a new file 'xigen' which stores aufs inode
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
2723 + + file handle for a branch (by exportfs_encode_fh())
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
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
2750 +++ b/Documentation/filesystems/aufs/design/99plan.txt
2753 +# Copyright (C) 2005-2009 Junjiro R. Okajima
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.
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.
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.
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?
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
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
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.
2789 +As a side effect, dirperm1 option improves the performance of aufs
2790 +because the number of permission check is reduced.
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.
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
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.
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
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
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
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.
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
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.
2846 +Aufs1 implemented "coo=none | leaf | all" option.
2849 +Refresh the Opened File (refrof)
2850 +----------------------------------------------------------------------
2851 +This option is implemented in aufs1 but incomplete.
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.
2857 +Let's try case study again.
2858 +- aufs has two branches.
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?
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.
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
2876 +Otherwise from /new.
2877 diff --git a/fs/Kconfig b/fs/Kconfig
2878 index 93945dd..75156dd 100644
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"
2887 endif # MISC_FILESYSTEMS
2889 diff --git a/fs/Makefile b/fs/Makefile
2890 index dc20db3..a4e9a65 100644
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
2902 +++ b/fs/aufs/Kconfig
2905 + bool "Aufs (Advanced multi layered unification filesystem) support"
2906 + depends on EXPERIMENTAL
2908 + Aufs is a stackable unification filesystem such as Unionfs,
2909 + which unifies several directories and provides a merged single
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.
2918 + prompt "Maximum number of branches"
2919 + default AUFS_BRANCH_MAX_127
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
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
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
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
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.
2950 +config AUFS_HINOTIFY
2951 + bool "Use inotify to detect actions on a branch"
2952 + depends on INOTIFY
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.
2961 + bool "NFS-exportable aufs"
2962 + depends on EXPORTFS = y
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.
2968 +config AUFS_BR_RAMFS
2969 + bool "Ramfs (initramfs/rootfs) as an aufs branch"
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.
2984 + Enable this to compile aufs internal debug code.
2985 + It will have a negative impact to the performance.
2987 +config AUFS_MAGIC_SYSRQ
2989 + depends on AUFS_DEBUG && MAGIC_SYSRQ
2992 + Automatic configuration for internal use.
2993 + When aufs supports Magic SysRq, enabled automatically.
2995 +config AUFS_BDEV_LOOP
2997 + depends on BLK_DEV_LOOP
3000 + Automatic configuration for internal use.
3001 + Convert =[ym] into =y.
3003 +config AUFS_INO_T_64
3005 + depends on AUFS_EXPORT
3006 + depends on 64BIT && !(ALPHA || S390)
3009 + Automatic configuration for internal use.
3010 + /* typedef unsigned long/int __kernel_ino_t */
3011 + /* alpha and s390x are int */
3013 diff --git a/fs/aufs/Makefile b/fs/aufs/Makefile
3014 new file mode 100644
3015 index 0000000..477d245
3017 +++ b/fs/aufs/Makefile
3020 +include ${srctree}/${src}/magic.mk
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 \
3029 + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
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
3044 +++ b/fs/aufs/aufs.h
3047 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
3056 + * all header files
3064 +#include <linux/bug.h>
3068 +#include "branch.h"
3071 +#include "dbgaufs.h"
3072 +#include "dentry.h"
3075 +#include "fstype.h"
3078 +#include "module.h"
3083 +#include "sysaufs.h"
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
3094 +++ b/fs/aufs/branch.c
3097 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
3106 + * branch management
3112 + * free a single branch
3114 +static void au_br_do_free(struct au_branch *br)
3117 + struct au_wbr *wbr;
3119 + if (br->br_xino.xi_file)
3120 + fput(br->br_xino.xi_file);
3121 + mutex_destroy(&br->br_xino.xi_nondir_mtx);
3123 + AuDebugOn(atomic_read(&br->br_count));
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);
3133 + /* some filesystems acquire extra lock */
3135 + mntput(br->br_mnt);
3143 + * frees all branches
3145 +void au_br_free(struct au_sbinfo *sbinfo)
3147 + aufs_bindex_t bmax;
3148 + struct au_branch **br;
3150 + bmax = sbinfo->si_bend + 1;
3151 + br = sbinfo->si_branch;
3153 + au_br_do_free(*br++);
3157 + * find the index of a branch which is specified by @br_id.
3159 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3161 + aufs_bindex_t bindex, bend;
3163 + bend = au_sbend(sb);
3164 + for (bindex = 0; bindex <= bend; bindex++)
3165 + if (au_sbr_id(sb, bindex) == br_id)
3170 +/* ---------------------------------------------------------------------- */
3176 +static int test_overlap(struct super_block *sb, struct dentry *h_d1,
3177 + struct dentry *h_d2)
3179 + if (unlikely(h_d1 == h_d2))
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);
3188 + * returns a newly allocated branch. @new_nbranch is a number of branches
3189 + * after adding a branch.
3191 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3194 + struct au_branch *add_branch;
3195 + struct dentry *root;
3197 + root = sb->s_root;
3198 + add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
3199 + if (unlikely(!add_branch))
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),
3207 + if (unlikely(!add_branch->br_wbr))
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)))
3215 + return add_branch; /* success */
3218 + kfree(add_branch->br_wbr);
3220 + kfree(add_branch);
3222 + return ERR_PTR(-ENOMEM);
3226 + * test if the branch permission is legal or not.
3228 +static int test_br(struct inode *inode, int brperm, char *path)
3233 + if (unlikely(au_br_writable(brperm) && IS_RDONLY(inode))) {
3234 + AuErr("write permission for readonly mount or inode, %s\n",
3244 + * 0: success, the caller will add it
3245 + * plus: success, it is already unified, the caller should ignore it
3248 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3251 + aufs_bindex_t bend, bindex;
3252 + struct dentry *root;
3253 + struct inode *inode, *h_inode;
3255 + root = sb->s_root;
3256 + bend = au_sbend(sb);
3257 + if (unlikely(bend >= 0
3258 + && au_find_dbindex(root, add->path.dentry) >= 0)) {
3262 + AuErr("%s duplicated\n", add->pathname);
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);
3275 + if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
3276 + AuErr("bad index %d\n", add->bindex);
3280 + inode = add->path.dentry->d_inode;
3282 + if (unlikely(!inode->i_nlink)) {
3283 + AuErr("no existence %s\n", add->pathname);
3288 + if (unlikely(inode->i_sb == sb)) {
3289 + AuErr("%s must be outside\n", add->pathname);
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));
3299 + err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
3300 + if (unlikely(err))
3304 + return 0; /* success */
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);
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",
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));
3333 + * initialize or clean the whiteouts for an adding branch
3335 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3336 + int new_perm, struct dentry *h_root)
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;
3345 + old_perm = br->br_perm;
3346 + br->br_perm = new_perm;
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);
3354 + h_mtx = &h_root->d_inode->i_mutex;
3355 + mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
3358 + err = au_wh_init(h_root, br, sb);
3360 + wbr_wh_write_lock(wbr);
3361 + err = au_wh_init(h_root, br, sb);
3362 + wbr_wh_write_unlock(wbr);
3365 + au_hin_imtx_unlock(hdir);
3367 + mutex_unlock(h_mtx);
3368 + br->br_perm = old_perm;
3370 + if (!err && wbr && !au_br_writable(new_perm)) {
3372 + br->br_wbr = NULL;
3378 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3379 + int perm, struct path *path)
3382 + struct au_wbr *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;
3390 + err = au_br_init_wh(sb, br, perm, path->dentry);
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)
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);
3411 + if (au_br_writable(add->perm)) {
3412 + err = au_wbr_init(br, sb, add->perm, &add->path);
3413 + if (unlikely(err))
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);
3426 + sysaufs_br_init(br);
3427 + mntget(add->path.mnt);
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)
3437 + struct au_branch **brp;
3439 + brp = sbinfo->si_branch + bindex;
3440 + memmove(brp + 1, brp, sizeof(*brp) * amount);
3442 + sbinfo->si_bend++;
3443 + if (unlikely(bend < 0))
3444 + sbinfo->si_bend = 0;
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)
3450 + struct au_hdentry *hdp;
3452 + hdp = dinfo->di_hdentry + bindex;
3453 + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3454 + au_h_dentry_init(hdp);
3456 + if (unlikely(bend < 0))
3457 + dinfo->di_bstart = 0;
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)
3463 + struct au_hinode *hip;
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);
3470 + if (unlikely(bend < 0))
3471 + iinfo->ii_bstart = 0;
3474 +static void au_br_do_add(struct super_block *sb, struct dentry *h_dentry,
3475 + struct au_branch *br, aufs_bindex_t bindex)
3477 + struct dentry *root;
3478 + struct inode *root_inode;
3479 + aufs_bindex_t bend, amount;
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),
3494 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
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;
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))
3511 + goto out; /* success */
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))
3520 + err = au_br_init(add_branch, sb, add);
3521 + if (unlikely(err)) {
3522 + au_br_do_free(add_branch);
3526 + add_bindex = add->bindex;
3527 + h_dentry = add->path.dentry;
3529 + au_br_do_add(sb, h_dentry, add_branch, add_bindex);
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);
3537 + au_cpup_attr_all(root_inode, /*force*/1);
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;
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.
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);
3560 +/* ---------------------------------------------------------------------- */
3566 +/* to show the line number, do not make it inlined function */
3567 +#define AuVerbose(do_info, fmt, args...) do { \
3569 + AuInfo(fmt, ##args); \
3573 + * test if the branch is deletable or not.
3575 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3576 + unsigned int sigen)
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;
3584 + struct inode *inode;
3586 + err = au_dpages_init(&dpages, GFP_NOFS);
3587 + if (unlikely(err))
3589 + err = au_dcsub_pages(&dpages, root, NULL, NULL);
3590 + if (unlikely(err))
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);
3604 + di_write_lock_child(d);
3605 + err = au_reval_dpath(d, sigen);
3607 + di_downgrade_lock(d, AuLock_IR);
3609 + di_write_unlock(d);
3614 + bstart = au_dbstart(d);
3615 + bend = au_dbend(d);
3616 + if (bstart <= bindex
3618 + && au_h_dptr(d, bindex)
3619 + && (!S_ISDIR(inode->i_mode) || bstart == bend)) {
3621 + AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
3623 + di_read_unlock(d, AuLock_IR);
3628 + au_dpages_free(&dpages);
3633 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3634 + unsigned int sigen)
3638 + aufs_bindex_t bstart, bend;
3639 + unsigned char verbose;
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))
3648 + if (au_iigen(i) == sigen)
3649 + ii_read_lock_child(i);
3651 + ii_write_lock_child(i);
3652 + err = au_refresh_hinode_self(i, /*do_attr*/1);
3654 + ii_downgrade_lock(i);
3656 + ii_write_unlock(i);
3661 + bstart = au_ibstart(i);
3662 + bend = au_ibend(i);
3663 + if (bstart <= bindex
3665 + && au_h_iptr(i, bindex)
3666 + && (!S_ISDIR(i->i_mode) || bstart == bend)) {
3668 + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3669 + ii_read_unlock(i);
3672 + ii_read_unlock(i);
3678 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex)
3681 + unsigned int sigen;
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);
3689 + err = test_inode_busy(root->d_sb, bindex, sigen);
3690 + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3695 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3696 + const aufs_bindex_t bindex,
3697 + const aufs_bindex_t bend)
3699 + struct au_branch **brp, **p;
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--;
3707 + p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, GFP_NOFS);
3709 + sbinfo->si_branch = p;
3712 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3713 + const aufs_bindex_t bend)
3715 + struct au_hdentry *hdp, *p;
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;
3723 + p = krealloc(dinfo->di_hdentry, sizeof(*p) * bend, GFP_NOFS);
3725 + dinfo->di_hdentry = p;
3728 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3729 + const aufs_bindex_t bend)
3731 + struct au_hinode *hip, *p;
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);
3740 + p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, GFP_NOFS);
3742 + iinfo->ii_hinode = p;
3745 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3746 + struct au_branch *br)
3748 + aufs_bindex_t bend;
3749 + struct au_sbinfo *sbinfo;
3750 + struct dentry *root;
3751 + struct inode *inode;
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;
3759 + dput(au_h_dptr(root, bindex));
3760 + au_hiput(au_hi(inode, bindex));
3761 + au_br_do_free(br);
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);
3768 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
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;
3778 + bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
3781 + goto out; /* success */
3783 + AuErr("%s no such branch\n", del->pathname);
3786 + AuDbg("bindex b%d\n", bindex);
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");
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);
3804 + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3806 + for (i = 0; i < AuBrWh_Last; i++) {
3807 + dput(wbr->wbr_wh[i]);
3808 + wbr->wbr_wh[i] = NULL;
3812 + err = test_children_busy(sb->s_root, bindex);
3813 + if (unlikely(err)) {
3820 + br_id = br->br_id;
3822 + au_br_do_del(sb, bindex, br);
3824 + sysaufs_brs_del(sb, bindex);
3825 + au_br_do_del(sb, bindex, br);
3826 + sysaufs_brs_add(sb, bindex);
3830 + au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
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);
3836 + if (sb->s_maxbytes == del->h_path.dentry->d_sb->s_maxbytes) {
3838 + sb->s_maxbytes = 0;
3839 + for (bindex = 0; bindex <= bend; bindex++) {
3840 + unsigned long long maxb;
3842 + maxb = au_sbr_sb(sb, bindex)->s_maxbytes;
3843 + if (sb->s_maxbytes < maxb)
3844 + sb->s_maxbytes = maxb;
3848 + if (au_xino_brid(sb) == br->br_id)
3849 + au_xino_brid_set(sb, -1);
3850 + goto out; /* success */
3854 + rerr = au_br_init_wh(sb, br, br->br_perm, del->h_path.dentry);
3856 + AuWarn("failed re-creating base whiteout, %s. (%d)\n",
3857 + del->pathname, rerr);
3862 +/* ---------------------------------------------------------------------- */
3865 + * change a branch permission
3868 +static int do_need_sigen_inc(int a, int b)
3870 + return au_br_whable(a) && !au_br_whable(b);
3873 +static int need_sigen_inc(int old, int new)
3875 + return do_need_sigen_inc(old, new)
3876 + || do_need_sigen_inc(new, old);
3879 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
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);
3890 + bytes = step_bytes;
3891 + files = step_files;
3892 + a = kmalloc(bytes, GFP_NOFS);
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))
3901 + AuDbg("%.*s\n", AuDLNPair(file->f_dentry));
3902 + fi_read_lock(file);
3903 + if (unlikely(au_test_mmapped(file))) {
3905 + FiMustNoWaiters(file);
3906 + fi_read_unlock(file);
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);
3919 + hf = au_h_fptr(file, bstart);
3920 + FiMustNoWaiters(file);
3921 + fi_read_unlock(file);
3929 + bytes += step_bytes;
3930 + files += step_files;
3931 + p = krealloc(a, bytes, GFP_NOFS);
3941 + for (ul = 0; ul < n; ul++) {
3942 + /* todo: already flushed? */
3943 + /* cf. fs/super.c:mark_files_ro() */
3945 + hf->f_mode &= ~FMODE_WRITE;
3946 + if (!file_check_writeable(hf)) {
3947 + file_release_write(hf);
3948 + mnt_drop_write(hf->f_vfsmnt);
3958 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3962 + aufs_bindex_t bindex;
3963 + struct dentry *root;
3964 + struct au_branch *br;
3966 + root = sb->s_root;
3967 + au_plink_block_maintain(sb);
3968 + bindex = au_find_dbindex(root, mod->h_root);
3971 + return 0; /* success */
3973 + AuErr("%s no such branch\n", mod->path);
3976 + AuDbg("bindex b%d\n", bindex);
3978 + err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
3979 + if (unlikely(err))
3982 + br = au_sbr(sb, bindex);
3983 + if (br->br_perm == mod->perm)
3984 + return 0; /* success */
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))
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);
4001 + if (unlikely(err)) {
4003 + br->br_wbr = kmalloc(sizeof(*br->br_wbr),
4006 + rerr = au_br_init_wh
4007 + (sb, br, br->br_perm,
4009 + if (unlikely(rerr)) {
4010 + AuIOErr("nested error %d (%d)\n",
4012 + br->br_perm = mod->perm;
4016 + } else if (au_br_writable(mod->perm)) {
4019 + br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
4021 + struct path path = {
4022 + .mnt = br->br_mnt,
4023 + .dentry = mod->h_root
4026 + err = au_wbr_init(br, sb, mod->perm, &path);
4027 + if (unlikely(err)) {
4028 + kfree(br->br_wbr);
4029 + br->br_wbr = NULL;
4035 + *do_update |= need_sigen_inc(br->br_perm, mod->perm);
4036 + br->br_perm = mod->perm;
4042 diff --git a/fs/aufs/branch.h b/fs/aufs/branch.h
4043 new file mode 100644
4044 index 0000000..e448692
4046 +++ b/fs/aufs/branch.h
4049 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
4058 + * branch filesystems and xino for them
4061 +#ifndef __AUFS_BRANCH_H__
4062 +#define __AUFS_BRANCH_H__
4066 +#include <linux/fs.h>
4067 +#include <linux/mount.h>
4068 +#include <linux/sysfs.h>
4069 +#include <linux/aufs_type.h>
4073 +/* ---------------------------------------------------------------------- */
4076 +struct au_xino_file {
4077 + struct file *xi_file;
4078 + struct mutex xi_nondir_mtx;
4080 + /* todo: make xino files an array to support huge inode number */
4082 +#ifdef CONFIG_DEBUG_FS
4083 + struct dentry *xi_dbgaufs;
4087 +/* members for writable branch only */
4088 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
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 */
4098 + unsigned long long wbr_bytes;
4101 +/* protected by superblock rwsem */
4103 + struct au_xino_file br_xino;
4105 + aufs_bindex_t br_id;
4108 + struct vfsmount *br_mnt;
4109 + atomic_t br_count;
4111 + struct au_wbr *br_wbr;
4113 + /* xino truncation */
4114 + blkcnt_t br_xino_upper; /* watermark in blocks */
4115 + atomic_t br_xino_running;
4117 +#ifdef CONFIG_SYSFS
4118 + /* an entry under sysfs per mount-point */
4120 + struct attribute br_attr;
4124 +/* ---------------------------------------------------------------------- */
4126 +/* branch permission and attribute */
4128 + AuBrPerm_RW, /* writable, linkable wh */
4129 + AuBrPerm_RO, /* readonly, no wh */
4130 + AuBrPerm_RR, /* natively readonly, no wh */
4132 + AuBrPerm_RWNoLinkWH, /* un-linkable whiteouts */
4134 + AuBrPerm_ROWH, /* whiteout-able */
4135 + AuBrPerm_RRWH, /* whiteout-able */
4140 +static inline int au_br_writable(int brperm)
4142 + return brperm == AuBrPerm_RW || brperm == AuBrPerm_RWNoLinkWH;
4145 +static inline int au_br_whable(int brperm)
4147 + return brperm == AuBrPerm_RW
4148 + || brperm == AuBrPerm_ROWH
4149 + || brperm == AuBrPerm_RRWH;
4152 +static inline int au_br_rdonly(struct au_branch *br)
4154 + return ((br->br_mnt->mnt_sb->s_flags & MS_RDONLY)
4155 + || !au_br_writable(br->br_perm))
4159 +static inline int au_br_hinotifyable(int brperm __maybe_unused)
4161 +#ifdef CONFIG_AUFS_HINOTIFY
4162 + return brperm != AuBrPerm_RR && brperm != AuBrPerm_RRWH;
4168 +/* ---------------------------------------------------------------------- */
4172 +void au_br_free(struct au_sbinfo *sinfo);
4173 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4175 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4177 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4179 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4183 +static const loff_t au_loff_max = LLONG_MAX;
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,
4188 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
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,
4195 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4197 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_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);
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);
4209 +/* ---------------------------------------------------------------------- */
4211 +/* Superblock to branch */
4213 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4215 + return au_sbr(sb, bindex)->br_id;
4219 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4221 + return au_sbr(sb, bindex)->br_mnt;
4225 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4227 + return au_sbr_mnt(sb, bindex)->mnt_sb;
4230 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4232 + atomic_dec(&au_sbr(sb, bindex)->br_count);
4235 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4237 + return au_sbr(sb, bindex)->br_perm;
4240 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4242 + return au_br_whable(au_sbr_perm(sb, bindex));
4245 +/* ---------------------------------------------------------------------- */
4248 + * wbr_wh_read_lock, wbr_wh_write_lock
4249 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4251 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
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
4259 +++ b/fs/aufs/cpup.c
4262 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
4271 + * copy-up functions, see wbr_policy.c for copy-down
4274 +#include <linux/fs_stack.h>
4275 +#include <linux/uaccess.h>
4278 +void au_cpup_attr_flags(struct inode *dst, struct inode *src)
4280 + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4281 + | S_NOATIME | S_NOCMTIME;
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;
4288 +void au_cpup_attr_timesizes(struct inode *inode)
4290 + struct inode *h_inode;
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);
4297 +void au_cpup_attr_nlink(struct inode *inode, int force)
4299 + struct inode *h_inode;
4300 + struct super_block *sb;
4301 + aufs_bindex_t bindex, bend;
4304 + bindex = au_ibstart(inode);
4305 + h_inode = au_h_iptr(inode, bindex);
4307 + && !S_ISDIR(h_inode->i_mode)
4308 + && au_opt_test(au_mntflags(sb), PLINK)
4309 + && au_plink_test(inode))
4312 + inode->i_nlink = h_inode->i_nlink;
4315 + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4316 + * it may includes whplink directory.
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);
4323 + au_add_nlink(inode, h_inode);
4328 +void au_cpup_attr_changeable(struct inode *inode)
4330 + struct inode *h_inode;
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);
4340 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4342 + struct au_iinfo *iinfo = au_ii(inode);
4344 + iinfo->ii_higen = h_inode->i_generation;
4345 + iinfo->ii_hsb1 = h_inode->i_sb;
4348 +void au_cpup_attr_all(struct inode *inode, int force)
4350 + struct inode *h_inode;
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);
4361 +/* ---------------------------------------------------------------------- */
4363 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
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)
4369 + struct inode *h_inode;
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;
4379 +void au_dtime_revert(struct au_dtime *dt)
4381 + struct iattr attr;
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;
4389 + err = vfsub_notify_change(&dt->dt_h_path, &attr);
4390 + if (unlikely(err))
4391 + AuWarn("restoring timestamps failed(%d). ignored\n", err);
4394 +/* ---------------------------------------------------------------------- */
4396 +static noinline_for_stack
4397 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src)
4401 + struct path h_path;
4402 + struct inode *h_isrc;
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);
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);
4429 +/* ---------------------------------------------------------------------- */
4431 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4432 + char *buf, unsigned long blksize)
4435 + size_t sz, rbytes, wbytes;
4436 + unsigned char all_zero;
4438 + struct mutex *h_mtx;
4439 + /* reduce stack usage */
4442 + zp = page_address(ZERO_PAGE(0));
4443 + if (unlikely(!zp))
4444 + return -ENOMEM; /* possible? */
4449 + AuDbg("len %lld\n", len);
4451 + if (len < blksize)
4455 + /* todo: signal_pending? */
4456 + while (!rbytes || err == -EAGAIN || err == -EINTR) {
4457 + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4460 + if (unlikely(err < 0))
4464 + if (len >= rbytes && rbytes == blksize)
4465 + all_zero = !memcmp(buf, zp, rbytes);
4472 + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4474 + /* todo: signal_pending? */
4475 + if (unlikely(err == -EAGAIN || err == -EINTR))
4477 + if (unlikely(err < 0))
4486 + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4488 + if (unlikely(res < 0))
4495 + /* the last block may be a hole */
4496 + if (!err && all_zero) {
4497 + AuLabel(last hole);
4500 + if (au_test_nfs(dst->f_dentry->d_sb)) {
4501 + /* nfs requires this step to make last hole */
4502 + /* is this only nfs? */
4504 + /* todo: signal_pending? */
4505 + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4506 + } while (err == -EAGAIN || err == -EINTR);
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);
4526 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4529 + unsigned long blksize;
4530 + unsigned char do_kfree;
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 *));
4540 + buf = kmalloc(blksize, GFP_NOFS);
4542 + buf = (void *)__get_free_page(GFP_NOFS);
4543 + if (unlikely(!buf))
4546 + if (len > (1 << 22))
4547 + AuDbg("copying a large file %lld\n", (long long)len);
4551 + err = au_do_copy_file(dst, src, len, buf, blksize);
4555 + free_page((unsigned long)buf);
4562 + * to support a sparse file which is opened with O_APPEND,
4563 + * we need to close the file.
4565 +static int au_cp_regular(struct dentry *dentry, aufs_bindex_t bdst,
4566 + aufs_bindex_t bsrc, loff_t len)
4569 + enum { SRC, DST };
4571 + aufs_bindex_t bindex;
4572 + unsigned int flags;
4573 + struct dentry *dentry;
4574 + struct file *file;
4575 + void *label, *label_file;
4579 + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4582 + .label_file = &&out_src
4586 + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4588 + .label = &&out_src,
4589 + .label_file = &&out_dst
4592 + struct super_block *sb;
4594 + /* bsrc branch can be ro/rw. */
4595 + sb = dentry->d_sb;
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))
4604 + if (unlikely(!f->file->f_op))
4605 + goto *f->label_file;
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);
4613 + fput(file[DST].file);
4614 + au_sbr_put(sb, file[DST].bindex);
4616 + fput(file[SRC].file);
4617 + au_sbr_put(sb, file[SRC].bindex);
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)
4630 + l = i_size_read(au_h_iptr(dentry->d_inode, bsrc));
4631 + if (len == -1 || l < len)
4634 + err = au_cp_regular(dentry, bdst, bsrc, len);
4636 + goto out; /* success */
4638 + rerr = vfsub_unlink(h_dir, h_path, /*force*/0);
4640 + AuIOErr("failed unlinking cpup-ed %.*s(%d, %d)\n",
4641 + AuDLNPair(h_path->dentry), err, rerr);
4649 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4650 + struct inode *h_dir)
4653 + mm_segment_t old_fs;
4657 + if (unlikely(!h_src->d_inode->i_op->readlink))
4661 + sym = __getname();
4662 + if (unlikely(!sym))
4665 + old_fs = get_fs();
4666 + set_fs(KERNEL_DS);
4667 + symlen = h_src->d_inode->i_op->readlink(h_src, (char __user *)sym,
4674 + err = vfsub_symlink(h_dir, h_path, sym);
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)
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;
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));
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;
4709 + AuDebugOn(h_parent != h_dst->d_parent);
4711 + sb = dentry->d_sb;
4712 + h_path.mnt = au_sbr_mnt(sb, bdst);
4714 + h_path.dentry = h_parent;
4715 + au_dtime_store(&dt, dst_parent, &h_path);
4717 + h_path.dentry = h_dst;
4720 + mode = h_inode->i_mode;
4721 + switch (mode & S_IFMT) {
4723 + /* try stopping to update while we are referencing */
4724 + IMustLock(h_inode);
4725 + err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
4727 + err = au_do_cpup_regular
4728 + (dentry, bdst, bsrc, len,
4729 + au_h_iptr(dst_parent->d_inode, bdst), &h_path);
4733 + err = vfsub_mkdir(h_dir, &h_path, mode);
4736 + * strange behaviour from the users view,
4737 + * particularry setattr case
4739 + if (au_ibstart(dst_parent->d_inode) == bdst)
4740 + au_cpup_attr_nlink(dst_parent->d_inode,
4742 + au_cpup_attr_nlink(dentry->d_inode, /*force*/1);
4746 + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
4750 + AuDebugOn(!capable(CAP_MKNOD));
4754 + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
4757 + AuIOErr("Unknown inode type 0%o\n", mode);
4761 + mnt_flags = au_mntflags(sb);
4762 + if (!au_opt_test(mnt_flags, UDBA_NONE)
4764 + && au_opt_test(mnt_flags, XINO)
4765 + && h_inode->i_nlink == 1
4766 + /* todo: unnecessary? */
4767 + /* && dentry->d_inode->i_nlink == 1 */
4769 + && !au_ftest_cpup(flags, KEEPLINO))
4770 + au_xino_write0(sb, bsrc, h_inode->i_ino, /*ino*/0);
4771 + /* ignore this error */
4774 + au_dtime_revert(&dt);
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.
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)
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;
4797 + AuDebugOn(bsrc <= bdst);
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;
4806 + h_src = au_h_dptr(dentry, bsrc);
4807 + inode = dentry->d_inode;
4810 + dst_parent = dget_parent(dentry);
4814 + plink = !!au_opt_test(au_mntflags(sb), PLINK);
4815 + dst_inode = au_h_iptr(inode, bdst);
4817 + if (unlikely(!plink)) {
4819 + AuIOErr("i%lu exists on a upper branch "
4820 + "but plink is disabled\n", inode->i_ino);
4824 + if (dst_inode->i_nlink) {
4825 + const int do_dt = au_ftest_cpup(flags, DTIME);
4827 + h_src = au_plink_lkup(inode, bdst);
4828 + err = PTR_ERR(h_src);
4829 + if (IS_ERR(h_src))
4831 + if (unlikely(!h_src->d_inode)) {
4833 + AuIOErr("i%lu exists on a upper branch "
4834 + "but plink is broken\n", inode->i_ino);
4840 + h_path.dentry = h_parent;
4841 + au_dtime_store(&dt, dst_parent, &h_path);
4843 + h_path.dentry = h_dst;
4844 + err = vfsub_link(h_src, h_dir, &h_path);
4846 + au_dtime_revert(&dt);
4850 + /* todo: cpup_wh_file? */
4852 + au_update_brange(inode, 1);
4855 + old_ibstart = au_ibstart(inode);
4856 + err = cpup_entry(dentry, bdst, bsrc, len, flags, dst_parent);
4857 + if (unlikely(err))
4859 + dst_inode = h_dst->d_inode;
4860 + mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
4862 + err = cpup_iattr(dentry, bdst, h_src);
4863 + isdir = S_ISDIR(dst_inode->i_mode);
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);
4871 + && h_src->d_inode->i_nlink > 1
4873 + au_plink_append(inode, bdst, h_dst);
4874 + goto out; /* success */
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;
4883 + rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
4885 + rerr = vfsub_rmdir(h_dir, &h_path);
4886 + au_dtime_revert(&dt);
4888 + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
4897 +struct au_cpup_single_args {
4899 + struct dentry *dentry;
4900 + aufs_bindex_t bdst, bsrc;
4902 + unsigned int flags;
4903 + struct dentry *dst_parent;
4906 +static void au_call_cpup_single(void *args)
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);
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)
4919 + struct dentry *h_dentry;
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,
4928 + struct au_cpup_single_args args = {
4935 + .dst_parent = dst_parent
4937 + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
4938 + if (unlikely(wkq_err))
4946 + * copyup the @dentry from the first active lower branch to @bdst,
4947 + * using au_cpup_single().
4949 +static int au_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4950 + unsigned int flags)
4953 + aufs_bindex_t bsrc, bend;
4955 + bend = au_dbend(dentry);
4956 + for (bsrc = bdst + 1; bsrc <= bend; bsrc++)
4957 + if (au_h_dptr(dentry, bsrc))
4960 + err = au_lkup_neg(dentry, bdst);
4962 + err = au_cpup_single(dentry, bdst, bsrc, len, flags, NULL);
4964 + return 0; /* success */
4967 + au_set_h_dptr(dentry, bdst, NULL);
4968 + au_set_dbstart(dentry, bsrc);
4974 +struct au_cpup_simple_args {
4976 + struct dentry *dentry;
4977 + aufs_bindex_t bdst;
4979 + unsigned int flags;
4982 +static void au_call_cpup_simple(void *args)
4984 + struct au_cpup_simple_args *a = args;
4985 + *a->errp = au_cpup_simple(a->dentry, a->bdst, a->len, a->flags);
4988 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4989 + unsigned int flags)
4992 + unsigned char do_sio;
4993 + struct dentry *parent;
4994 + struct inode *h_dir;
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);
5001 + * testing CAP_MKNOD is for generic fs,
5002 + * but CAP_FSETID is for xfs only, currently.
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)));
5011 + err = au_cpup_simple(dentry, bdst, len, flags);
5013 + struct au_cpup_simple_args args = {
5020 + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5021 + if (unlikely(wkq_err))
5029 +/* ---------------------------------------------------------------------- */
5032 + * copyup the deleted file for writing.
5034 +static int au_do_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst,
5035 + struct dentry *wh_dentry, struct file *file,
5039 + aufs_bindex_t bstart;
5040 + struct au_dinfo *dinfo;
5041 + struct dentry *h_d_dst, *h_d_start;
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;
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;
5058 + dinfo->di_hdentry[0 + bdst].hd_dentry = h_d_dst;
5059 + dinfo->di_bstart = bstart;
5064 +static int au_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
5065 + struct file *file)
5068 + struct au_dtime dt;
5069 + struct dentry *parent, *h_parent, *wh_dentry;
5070 + struct au_branch *br;
5071 + struct path h_path;
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))
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))
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);
5096 + au_dtime_revert(&dt);
5097 + au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
5106 +struct au_cpup_wh_args {
5108 + struct dentry *dentry;
5109 + aufs_bindex_t bdst;
5111 + struct file *file;
5114 +static void au_call_cpup_wh(void *args)
5116 + struct au_cpup_wh_args *a = args;
5117 + *a->errp = au_cpup_wh(a->dentry, a->bdst, a->len, a->file);
5120 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
5121 + struct file *file)
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;
5128 + parent = dget_parent(dentry);
5129 + dir = parent->d_inode;
5132 + h_dir = au_igrab(au_h_iptr(dir, bdst));
5134 + if (!h_dir->i_nlink) {
5135 + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5136 + h_orph = wbr->wbr_orph;
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);
5145 + /* this temporary unlock is safe */
5147 + h_dentry = au_h_fptr(file, au_fbstart(file))->f_dentry;
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);
5157 + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE))
5158 + err = au_cpup_wh(dentry, bdst, len, file);
5160 + struct au_cpup_wh_args args = {
5167 + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5168 + if (unlikely(wkq_err))
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);
5185 +/* ---------------------------------------------------------------------- */
5188 + * generic routine for both of copy-up and copy-down.
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),
5197 + struct au_pin pin;
5198 + struct dentry *d, *parent, *h_parent, *real_parent;
5201 + parent = dget_parent(dentry);
5202 + if (IS_ROOT(parent))
5205 + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5206 + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5208 + /* do not use au_dpage */
5209 + real_parent = parent;
5212 + parent = dget_parent(dentry);
5213 + h_parent = au_h_dptr(parent, bdst);
5215 + goto out; /* success */
5217 + /* find top dir which is necessary to cpup */
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);
5227 + if (d != real_parent)
5228 + di_write_lock_child3(d);
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);
5235 + au_pin_set_dentry(&pin, d);
5236 + err = au_do_pin(&pin);
5238 + err = cp(d, bdst, h_parent, arg);
5243 + if (d != real_parent)
5244 + di_write_unlock(d);
5245 + if (unlikely(err))
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)
5258 + return au_sio_cpup_simple(dentry, bdst, -1, AuCpup_DTIME);
5261 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5263 + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5266 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5269 + struct dentry *parent;
5270 + struct inode *dir;
5272 + parent = dget_parent(dentry);
5273 + dir = parent->d_inode;
5275 + if (au_h_iptr(dir, bdst))
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);
5289 diff --git a/fs/aufs/cpup.h b/fs/aufs/cpup.h
5290 new file mode 100644
5291 index 0000000..240014a
5293 +++ b/fs/aufs/cpup.h
5296 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
5305 + * copy-up/down functions
5308 +#ifndef __AUFS_CPUP_H__
5309 +#define __AUFS_CPUP_H__
5313 +#include <linux/fs.h>
5314 +#include <linux/aufs_type.h>
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);
5323 +/* ---------------------------------------------------------------------- */
5326 +#define AuCpup_DTIME 1 /* do dtime_store/revert */
5327 +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
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; }
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);
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),
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);
5349 +/* ---------------------------------------------------------------------- */
5351 +/* keep timestamps when copyup */
5353 + struct dentry *dt_dentry;
5354 + struct path dt_h_path;
5355 + struct timespec dt_atime, dt_mtime;
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);
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
5367 +++ b/fs/aufs/dbgaufs.c
5370 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
5379 + * debugfs interface
5382 +#include <linux/debugfs.h>
5385 +#ifndef CONFIG_SYSFS
5386 +#error DEBUG_FS depends upon SYSFS
5389 +static struct dentry *dbgaufs;
5390 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5392 +/* 20 is max digits length of ulong 64 */
5393 +struct dbgaufs_arg {
5399 + * common function for all XINO files
5401 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5402 + struct file *file)
5404 + kfree(file->private_data);
5408 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5412 + struct dbgaufs_arg *p;
5415 + p = kmalloc(sizeof(*p), GFP_NOFS);
5421 + file->private_data = p;
5425 + err = vfs_getattr(xf->f_vfsmnt, xf->f_dentry, &st);
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);
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));
5438 + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5447 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
5448 + size_t count, loff_t *ppos)
5450 + struct dbgaufs_arg *p;
5452 + p = file->private_data;
5453 + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5456 +/* ---------------------------------------------------------------------- */
5458 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
5461 + struct au_sbinfo *sbinfo;
5462 + struct super_block *sb;
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);
5472 +static const struct file_operations dbgaufs_xib_fop = {
5473 + .open = dbgaufs_xib_open,
5474 + .release = dbgaufs_xi_release,
5475 + .read = dbgaufs_xi_read
5478 +/* ---------------------------------------------------------------------- */
5480 +#define DbgaufsXi_PREFIX "xi"
5482 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
5486 + struct au_sbinfo *sbinfo;
5487 + struct super_block *sb;
5489 + struct qstr *name;
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)))
5498 + err = strict_strtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
5499 + if (unlikely(err))
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);
5510 + si_read_unlock(sb);
5516 +static const struct file_operations dbgaufs_xino_fop = {
5517 + .open = dbgaufs_xino_open,
5518 + .release = dbgaufs_xi_release,
5519 + .read = dbgaufs_xi_read
5522 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5524 + aufs_bindex_t bend;
5525 + struct au_branch *br;
5526 + struct au_xino_file *xi;
5528 + if (!au_sbi(sb)->si_dbgaufs)
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;
5542 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
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 */
5551 + sbinfo = au_sbi(sb);
5552 + parent = sbinfo->si_dbgaufs;
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);
5570 +/* ---------------------------------------------------------------------- */
5572 +#ifdef CONFIG_AUFS_EXPORT
5573 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
5576 + struct au_sbinfo *sbinfo;
5577 + struct super_block *sb;
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);
5587 +static const struct file_operations dbgaufs_xigen_fop = {
5588 + .open = dbgaufs_xigen_open,
5589 + .release = dbgaufs_xi_release,
5590 + .read = dbgaufs_xi_read
5593 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
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)
5607 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5611 +#endif /* CONFIG_AUFS_EXPORT */
5613 +/* ---------------------------------------------------------------------- */
5615 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5617 + debugfs_remove_recursive(sbinfo->si_dbgaufs);
5618 + sbinfo->si_dbgaufs = NULL;
5619 + kobject_put(&sbinfo->si_kobj);
5622 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5625 + char name[SysaufsSiNameLen];
5629 + AuErr1("/debug/aufs is uninitialized\n");
5634 + sysaufs_name(sbinfo, name);
5635 + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
5636 + if (unlikely(!sbinfo->si_dbgaufs))
5638 + kobject_get(&sbinfo->si_kobj);
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))
5646 + err = dbgaufs_xigen_init(sbinfo);
5648 + goto out; /* success */
5651 + dbgaufs_si_fin(sbinfo);
5656 +/* ---------------------------------------------------------------------- */
5658 +void dbgaufs_fin(void)
5660 + debugfs_remove(dbgaufs);
5663 +int __init dbgaufs_init(void)
5668 + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
5673 diff --git a/fs/aufs/dbgaufs.h b/fs/aufs/dbgaufs.h
5674 new file mode 100644
5675 index 0000000..dfb0f0a
5677 +++ b/fs/aufs/dbgaufs.h
5680 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
5689 + * debugfs interface
5692 +#ifndef __DBGAUFS_H__
5693 +#define __DBGAUFS_H__
5697 +#include <linux/fs.h>
5698 +#include <linux/aufs_type.h>
5701 +#ifdef CONFIG_DEBUG_FS
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);
5713 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5719 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
5725 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5731 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5736 +#define dbgaufs_fin() do {} while (0)
5739 +int __init dbgaufs_init(void)
5743 +#endif /* CONFIG_DEBUG_FS */
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
5751 +++ b/fs/aufs/dcsub.c
5754 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
5763 + * sub-routines for dentry cache
5768 +static void au_dpage_free(struct au_dpage *dpage)
5771 + struct dentry **p;
5773 + p = dpage->dentries;
5774 + for (i = 0; i < dpage->ndentry; i++)
5776 + free_page((unsigned long)dpage->dentries);
5779 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
5785 + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
5786 + if (unlikely(!dpages->dpages))
5789 + p = (void *)__get_free_page(gfp);
5793 + dpages->dpages[0].ndentry = 0;
5794 + dpages->dpages[0].dentries = p;
5795 + dpages->ndpage = 1;
5796 + return 0; /* success */
5799 + kfree(dpages->dpages);
5804 +void au_dpages_free(struct au_dcsub_pages *dpages)
5807 + struct au_dpage *p;
5809 + p = dpages->dpages;
5810 + for (i = 0; i < dpages->ndpage; i++)
5811 + au_dpage_free(p++);
5812 + kfree(dpages->dpages);
5815 +static int au_dpages_append(struct au_dcsub_pages *dpages,
5816 + struct dentry *dentry, gfp_t gfp)
5819 + struct au_dpage *dpage;
5822 + dpage = dpages->dpages + dpages->ndpage - 1;
5823 + sz = PAGE_SIZE / sizeof(dentry);
5824 + if (unlikely(dpage->ndentry >= sz)) {
5825 + AuLabel(new dpage);
5827 + sz = dpages->ndpage * sizeof(*dpages->dpages);
5828 + p = au_kzrealloc(dpages->dpages, sz,
5829 + sz + sizeof(*dpages->dpages), gfp);
5833 + dpages->dpages = p;
5834 + dpage = dpages->dpages + dpages->ndpage;
5835 + p = (void *)__get_free_page(gfp);
5839 + dpage->ndentry = 0;
5840 + dpage->dentries = p;
5844 + dpage->dentries[dpage->ndentry++] = dget(dentry);
5845 + return 0; /* success */
5851 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5852 + au_dpages_test test, void *arg)
5855 + struct dentry *this_parent = root;
5856 + struct list_head *next;
5857 + struct super_block *sb = root->d_sb;
5860 + spin_lock(&dcache_lock);
5862 + next = this_parent->d_subdirs.next;
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))
5874 + while (next != &this_parent->d_subdirs) {
5875 + struct list_head *tmp = next;
5876 + struct dentry *dentry = list_entry(tmp, struct dentry,
5879 + if (/*d_unhashed(dentry) || */!dentry->d_inode)
5881 + if (!list_empty(&dentry->d_subdirs)) {
5882 + this_parent = dentry;
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))
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 */
5900 + spin_unlock(&dcache_lock);
5904 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5905 + int do_include, au_dpages_test test, void *arg)
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))
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))
5926 + spin_unlock(&dcache_lock);
5931 +struct dentry *au_test_subdir(struct dentry *d1, struct dentry *d2)
5933 + struct dentry *trap, **dentries;
5935 + struct au_dcsub_pages dpages;
5936 + struct au_dpage *dpage;
5938 + trap = ERR_PTR(-ENOMEM);
5939 + err = au_dpages_init(&dpages, GFP_NOFS);
5940 + if (unlikely(err))
5942 + err = au_dcsub_pages_rev(&dpages, d1, /*do_include*/1, NULL, NULL);
5943 + if (unlikely(err))
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++) {
5963 + au_dpages_free(&dpages);
5967 diff --git a/fs/aufs/dcsub.h b/fs/aufs/dcsub.h
5968 new file mode 100644
5969 index 0000000..89a7745
5971 +++ b/fs/aufs/dcsub.h
5974 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
5983 + * sub-routines for dentry cache
5986 +#ifndef __AUFS_DCSUB_H__
5987 +#define __AUFS_DCSUB_H__
5991 +#include <linux/dcache.h>
5995 + struct dentry **dentries;
5998 +struct au_dcsub_pages {
6000 + struct au_dpage *dpages;
6003 +/* ---------------------------------------------------------------------- */
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);
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
6020 +++ b/fs/aufs/debug.c
6023 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
6032 + * debug print functions
6038 +MODULE_PARM_DESC(debug, "debug print");
6039 +module_param_named(debug, aufs_debug, int, S_IRUGO | S_IWUSR | S_IWGRP);
6041 +char *au_plevel = KERN_DEBUG;
6042 +#define dpri(fmt, arg...) do { \
6043 + if (au_debug_test()) \
6044 + printk("%s" fmt, au_plevel, ##arg); \
6047 +/* ---------------------------------------------------------------------- */
6049 +void au_dpri_whlist(struct au_nhash *whlist)
6051 + unsigned long ul, n;
6052 + struct hlist_head *head;
6053 + struct au_vdir_wh *tpos;
6054 + struct hlist_node *pos;
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",
6062 + tpos->wh_str.len, tpos->wh_str.name,
6063 + tpos->wh_str.len);
6068 +void au_dpri_vdir(struct au_vdir *vdir)
6071 + union au_vdir_deblk_p p;
6074 + if (!vdir || IS_ERR(vdir)) {
6075 + dpri("err %ld\n", PTR_ERR(vdir));
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];
6085 + dpri("[%lu]: %p\n", ul, o);
6089 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode,
6090 + struct dentry *wh)
6095 + if (!inode || IS_ERR(inode)) {
6096 + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
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));
6104 + n = (void *)wh->d_name.name;
6105 + l = wh->d_name.len;
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",
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);
6121 +void au_dpri_inode(struct inode *inode)
6123 + struct au_iinfo *iinfo;
6124 + aufs_bindex_t bindex;
6127 + err = do_pri_inode(-1, inode, NULL);
6128 + if (err || !au_test_aufs(inode->i_sb))
6131 + iinfo = au_ii(inode);
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)
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);
6143 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6145 + struct dentry *wh = NULL;
6147 + if (!dentry || IS_ERR(dentry)) {
6148 + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6151 + /* do not call dget_parent() here */
6152 + dpri("d%d: %.*s?/%.*s, %s, cnt %d, flags 0x%x\n",
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);
6160 + wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6162 + do_pri_inode(bindex, dentry->d_inode, wh);
6166 +void au_dpri_dentry(struct dentry *dentry)
6168 + struct au_dinfo *dinfo;
6169 + aufs_bindex_t bindex;
6172 + err = do_pri_dentry(-1, dentry);
6173 + if (err || !au_test_aufs(dentry->d_sb))
6176 + dinfo = au_di(dentry);
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)
6184 + for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6185 + do_pri_dentry(bindex, dinfo->di_hdentry[0 + bindex].hd_dentry);
6188 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
6192 + if (!file || IS_ERR(file)) {
6193 + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
6199 + && au_test_aufs(file->f_dentry->d_sb)
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),
6205 + if (file->f_dentry)
6206 + do_pri_dentry(bindex, file->f_dentry);
6210 +void au_dpri_file(struct file *file)
6212 + struct au_finfo *finfo;
6213 + aufs_bindex_t bindex;
6216 + err = do_pri_file(-1, file);
6217 + if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
6220 + finfo = au_fi(file);
6223 + if (finfo->fi_bstart < 0)
6225 + for (bindex = finfo->fi_bstart; bindex <= finfo->fi_bend; bindex++) {
6226 + struct au_hfile *hf;
6228 + hf = finfo->fi_hfile + bindex;
6229 + do_pri_file(bindex, hf ? hf->hf_file : NULL);
6233 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
6235 + struct vfsmount *mnt;
6236 + struct super_block *sb;
6238 + if (!br || IS_ERR(br))
6241 + if (!mnt || IS_ERR(mnt))
6244 + if (!sb || IS_ERR(sb))
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, "
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);
6257 + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
6261 +void au_dpri_sb(struct super_block *sb)
6263 + struct au_sbinfo *sbinfo;
6264 + aufs_bindex_t bindex;
6266 + /* to reuduce stack size */
6268 + struct vfsmount mnt;
6269 + struct au_branch fake;
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");
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);
6287 + dpri("dev 0x%x\n", sb->s_dev);
6288 + if (err || !au_test_aufs(sb))
6291 + sbinfo = au_sbi(sb);
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]);
6301 +/* ---------------------------------------------------------------------- */
6303 +void au_dbg_sleep_jiffy(int jiffy)
6306 + jiffy = schedule_timeout_uninterruptible(jiffy);
6309 +void au_dbg_iattr(struct iattr *ia)
6311 +#define AuBit(name) if (ia->ia_valid & ATTR_ ## name) \
6331 + dpri("ia_file %p\n", ia->ia_file);
6334 +/* ---------------------------------------------------------------------- */
6336 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
6338 + struct dentry *parent;
6340 + parent = dget_parent(dentry);
6341 + AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode)
6342 + || IS_ROOT(dentry)
6343 + || au_digen(parent) != sigen);
6347 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
6349 + struct dentry *parent;
6351 + parent = dget_parent(dentry);
6352 + AuDebugOn(S_ISDIR(dentry->d_inode->i_mode)
6353 + || au_digen(parent) != sigen);
6357 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6360 + struct au_dcsub_pages dpages;
6361 + struct au_dpage *dpage;
6362 + struct dentry **dentries;
6364 + err = au_dpages_init(&dpages, GFP_NOFS);
6366 + err = au_dcsub_pages_rev(&dpages, parent, /*do_include*/1, NULL, NULL);
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);
6374 + au_dpages_free(&dpages);
6377 +void au_dbg_verify_hf(struct au_finfo *finfo)
6379 + struct au_hfile *hf;
6380 + aufs_bindex_t bend, bindex;
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);
6391 +void au_dbg_verify_kthread(void)
6393 + if (au_test_wkq(current)) {
6399 +/* ---------------------------------------------------------------------- */
6401 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
6403 +#ifdef AuForceNoPlink
6404 + au_opt_clr(sbinfo->si_mntflags, PLINK);
6406 +#ifdef AuForceNoXino
6407 + au_opt_clr(sbinfo->si_mntflags, XINO);
6409 +#ifdef AuForceNoRefrof
6410 + au_opt_clr(sbinfo->si_mntflags, REFROF);
6412 +#ifdef AuForceHinotify
6413 + au_opt_set_udba(sbinfo->si_mntflags, UDBA_HINOTIFY);
6417 +int __init au_debug_init(void)
6419 + aufs_bindex_t bindex;
6420 + struct au_vdir_destr destr;
6423 + AuDebugOn(bindex >= 0);
6426 + AuDebugOn(destr.len < NAME_MAX);
6428 +#ifdef CONFIG_4KSTACKS
6429 + AuWarn("CONFIG_4KSTACKS is defined.\n");
6432 +#ifdef AuForceNoBrs
6438 diff --git a/fs/aufs/debug.h b/fs/aufs/debug.h
6439 new file mode 100644
6440 index 0000000..36ff0ea
6442 +++ b/fs/aufs/debug.h
6445 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
6454 + * debug print functions
6457 +#ifndef __AUFS_DEBUG_H__
6458 +#define __AUFS_DEBUG_H__
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>
6469 +#ifdef CONFIG_AUFS_DEBUG
6470 +#define AuDebugOn(a) BUG_ON(a)
6472 +/* module parameter */
6473 +extern int aufs_debug;
6474 +static inline void au_debug(int n)
6480 +static inline int au_debug_test(void)
6482 + return aufs_debug;
6485 +#define AuDebugOn(a) do {} while (0)
6486 +#define au_debug() do {} while (0)
6487 +static inline int au_debug_test(void)
6491 +#endif /* CONFIG_AUFS_DEBUG */
6493 +/* ---------------------------------------------------------------------- */
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); \
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; \
6512 + AuWarn(fmt, ##arg); \
6515 +#define AuErr1(fmt, arg...) do { \
6516 + static unsigned char _c; \
6518 + AuErr(fmt, ##arg); \
6521 +#define AuIOErr1(fmt, arg...) do { \
6522 + static unsigned char _c; \
6524 + AuIOErr(fmt, ##arg); \
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); \
6534 +#define AuTraceErr(e) do { \
6535 + if (unlikely((e) < 0)) \
6536 + AuDbg("err %d\n", (int)(e)); \
6539 +#define AuTraceErrPtr(p) do { \
6541 + AuDbg("err %ld\n", PTR_ERR(p)); \
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)
6548 +/* ---------------------------------------------------------------------- */
6552 +#ifdef CONFIG_AUFS_DEBUG
6553 +extern char *au_plevel;
6555 +void au_dpri_whlist(struct au_nhash *whlist);
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);
6563 +void au_dbg_sleep_jiffy(int jiffy);
6564 +void au_dbg_iattr(struct iattr *ia);
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);
6572 +int __init au_debug_init(void);
6573 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
6574 +#define AuDbgWhlist(w) do { \
6576 + au_dpri_whlist(w); \
6579 +#define AuDbgVdir(v) do { \
6581 + au_dpri_vdir(v); \
6584 +#define AuDbgInode(i) do { \
6586 + au_dpri_inode(i); \
6589 +#define AuDbgDentry(d) do { \
6591 + au_dpri_dentry(d); \
6594 +#define AuDbgFile(f) do { \
6596 + au_dpri_file(f); \
6599 +#define AuDbgSb(sb) do { \
6600 + AuDbg(#sb "\n"); \
6604 +#define AuDbgSleep(sec) do { \
6605 + AuDbg("sleep %d sec\n", sec); \
6609 +#define AuDbgSleepJiffy(jiffy) do { \
6610 + AuDbg("sleep %d jiffies\n", jiffy); \
6611 + au_dbg_sleep_jiffy(jiffy); \
6614 +#define AuDbgIAttr(ia) do { \
6615 + AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
6616 + au_dbg_iattr(ia); \
6619 +static inline void au_dbg_verify_dir_parent(struct dentry *dentry,
6620 + unsigned int sigen)
6624 +static inline void au_dbg_verify_nondir_parent(struct dentry *dentry,
6625 + unsigned int sigen)
6629 +static inline void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6633 +static inline void au_dbg_verify_hf(struct au_finfo *finfo)
6637 +static inline void au_dbg_verify_kthread(void)
6642 +static inline int au_debug_init(void)
6646 +static inline void au_debug_sbinfo_init(struct au_sbinfo *sbinfo)
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 */
6661 +/* ---------------------------------------------------------------------- */
6663 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
6664 +int __init au_sysrq_init(void);
6665 +void au_sysrq_fin(void);
6667 +#ifdef CONFIG_HW_CONSOLE
6668 +#define au_dbg_blocked() do { \
6670 + handle_sysrq('w', vc_cons[fg_console].d->vc_tty); \
6673 +#define au_dbg_blocked() do {} while (0)
6677 +static inline int au_sysrq_init(void)
6681 +#define au_sysrq_fin() do {} while (0)
6682 +#define au_dbg_blocked() do {} while (0)
6683 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
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
6691 +++ b/fs/aufs/dentry.c
6694 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
6703 + * lookup and dentry operations
6708 +static void au_h_nd(struct nameidata *h_nd, struct nameidata *nd)
6714 + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
6715 + * due to whiteout and branch permission.
6717 + h_nd->flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
6719 + /* unnecessary? */
6720 + h_nd->intent.open.file = NULL;
6722 + memset(h_nd, 0, sizeof(*h_nd));
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;
6733 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
6734 + struct au_branch *br, struct nameidata *nd)
6736 + struct dentry *h_dentry;
6738 + struct nameidata h_nd;
6740 + if (au_test_fs_null_nd(h_parent->d_sb))
6741 + return vfsub_lookup_one_len(name->name, h_parent, name->len);
6743 + au_h_nd(&h_nd, nd);
6744 + h_nd.path.dentry = h_parent;
6745 + h_nd.path.mnt = br->br_mnt;
6747 + err = __lookup_one_len(name->name, &h_nd.last, NULL, name->len);
6748 + h_dentry = ERR_PTR(err);
6750 + path_get(&h_nd.path);
6751 + h_dentry = vfsub_lookup_hash(&h_nd);
6752 + path_put(&h_nd.path);
6758 +static void au_call_lkup_one(void *args)
6760 + struct au_lkup_one_args *a = args;
6761 + *a->errp = au_lkup_one(a->name, a->h_parent, a->br, a->nd);
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; }
6769 +struct au_do_lookup_args {
6770 + unsigned int flags;
6772 + struct nameidata *nd;
6776 + * returns positive/negative dentry, NULL or an error.
6777 + * NULL means whiteout-ed or not-found.
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)
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);
6792 + name = &dentry->d_name;
6794 + br = au_sbr(dentry->d_sb, bindex);
6795 + wh_able = !!au_br_whable(br->br_perm);
6797 + wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6798 + h_dentry = ERR_PTR(wh_found);
6801 + if (unlikely(wh_found < 0))
6804 + /* We found a whiteout */
6805 + /* au_set_dbend(dentry, bindex); */
6806 + au_set_dbwh(dentry, bindex);
6808 + return NULL; /* success */
6811 + h_dentry = au_lkup_one(name, h_parent, br, args->nd);
6812 + if (IS_ERR(h_dentry))
6815 + h_inode = h_dentry->d_inode;
6819 + } else if (wh_found
6820 + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
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);
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 */
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);
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);
6853 + * returns the number of lower positive dentries,
6854 + * otherwise an error.
6855 + * can be called at unlinking with @type is zero.
6857 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
6858 + struct nameidata *nd)
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 = {
6869 + const struct qstr *name = &dentry->d_name;
6870 + struct dentry *parent;
6871 + struct inode *inode;
6874 + parent = dget_parent(dentry);
6875 + if (unlikely(!strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6878 + err = au_wh_name_alloc(&whname, name);
6879 + if (unlikely(err))
6882 + inode = dentry->d_inode;
6883 + isdir = !!(inode && S_ISDIR(inode->i_mode));
6885 + au_fset_lkup(args.flags, ALLOW_NEG);
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;
6893 + h_dentry = au_h_dptr(dentry, bindex);
6895 + if (h_dentry->d_inode)
6897 + if (type != S_IFDIR)
6901 + h_parent = au_h_dptr(parent, bindex);
6904 + h_dir = h_parent->d_inode;
6905 + if (!h_dir || !S_ISDIR(h_dir->i_mode))
6908 + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
6909 + h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
6911 + mutex_unlock(&h_dir->i_mutex);
6912 + err = PTR_ERR(h_dentry);
6913 + if (IS_ERR(h_dentry))
6915 + au_fclr_lkup(args.flags, ALLOW_NEG);
6917 + if (au_dbwh(dentry) >= 0)
6921 + h_inode = h_dentry->d_inode;
6926 + args.type = h_inode->i_mode & S_IFMT;
6927 + if (args.type != S_IFDIR)
6930 + /* the type of lower may be different */
6931 + bdiropq = au_dbdiropq(dentry);
6932 + if (bdiropq >= 0 && bdiropq <= bindex)
6938 + AuLabel(positive);
6939 + au_update_dbstart(dentry);
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 */
6948 + kfree(whname.name);
6954 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
6955 + struct au_branch *br)
6957 + struct dentry *dentry;
6960 + if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
6961 + dentry = au_lkup_one(name, parent, br, /*nd*/NULL);
6963 + struct au_lkup_one_args args = {
6966 + .h_parent = parent,
6971 + wkq_err = au_wkq_wait(au_call_lkup_one, &args);
6972 + if (unlikely(wkq_err))
6973 + dentry = ERR_PTR(wkq_err);
6980 + * lookup @dentry on @bindex which should be negative.
6982 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex)
6985 + struct dentry *parent, *h_parent, *h_dentry;
6986 + struct qstr *name;
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))
6996 + if (unlikely(h_dentry->d_inode)) {
6998 + AuIOErr("b%d %.*s should be negative.\n",
6999 + bindex, AuDLNPair(h_dentry));
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);
7016 +/* ---------------------------------------------------------------------- */
7018 +/* subset of struct inode */
7020 + unsigned long i_ino;
7021 + /* unsigned int i_nlink; */
7027 + blkcnt_t i_blocks;
7032 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
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;
7040 + ia->i_size = h_inode->i_size;
7041 + ia->i_blocks = h_inode->i_blocks;
7043 + ia->i_mode = (h_inode->i_mode & S_IFMT);
7046 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
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
7054 + || ia->i_size != h_inode->i_size
7055 + || ia->i_blocks != h_inode->i_blocks
7057 + || ia->i_mode != (h_inode->i_mode & S_IFMT);
7060 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7061 + struct au_branch *br)
7064 + struct au_iattr ia;
7065 + struct inode *h_inode;
7066 + struct dentry *h_d;
7067 + struct super_block *h_sb;
7070 + memset(&ia, -1, sizeof(ia));
7071 + h_sb = h_dentry->d_sb;
7072 + h_inode = h_dentry->d_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 */
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);
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();
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)
7104 + if (udba == AuOpt_UDBA_REVAL) {
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);
7113 +/* ---------------------------------------------------------------------- */
7115 +static void au_do_refresh_hdentry(struct au_hdentry *p, struct au_dinfo *dinfo,
7116 + struct dentry *parent)
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;
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;
7131 + h_dp = dget_parent(h_d);
7132 + if (h_dp == au_h_dptr(parent, bindex)) {
7137 + new_bindex = au_find_dbindex(parent, h_dp);
7139 + if (dinfo->di_bwh == bindex)
7141 + if (dinfo->di_bdiropq == bindex)
7142 + bdiropq = new_bindex;
7143 + if (new_bindex < 0) {
7145 + p->hd_dentry = NULL;
7149 + /* swap two lower dentries, and loop again */
7150 + q = dinfo->di_hdentry + new_bindex;
7154 + if (tmp.hd_dentry) {
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;
7165 + dinfo->di_bdiropq = -1;
7167 + && bdiropq <= au_sbend(sb)
7168 + && au_sbr_whable(sb, bdiropq))
7169 + dinfo->di_bdiropq = bdiropq;
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;
7179 + p = dinfo->di_hdentry + bend;
7180 + for (bindex = bend; bindex >= 0; bindex--, p--)
7181 + if (p->hd_dentry) {
7182 + dinfo->di_bend = bindex;
7188 + * returns the number of found lower positive dentries,
7189 + * otherwise an error.
7191 +int au_refresh_hdentry(struct dentry *dentry, mode_t type)
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;
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);
7207 + dinfo = au_di(dentry);
7208 + err = au_di_realloc(dinfo, au_sbend(sb) + 1);
7210 + if (unlikely(err))
7212 + au_do_refresh_hdentry(dinfo->di_hdentry + dinfo->di_bstart, dinfo,
7216 + bstart = au_dbstart(parent);
7217 + if (type != S_IFDIR && dinfo->di_bstart == bstart)
7218 + goto out_dgen; /* success */
7220 + npositive = au_lkup_dentry(dentry, bstart, type, /*nd*/NULL);
7221 + if (npositive < 0)
7223 + if (dinfo->di_bwh >= 0 && dinfo->di_bwh <= dinfo->di_bstart)
7227 + au_update_digen(dentry);
7230 + AuTraceErr(npositive);
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)
7239 + int (*reval)(struct dentry *, struct nameidata *);
7243 + if (h_dentry->d_op)
7244 + reval = h_dentry->d_op->d_revalidate;
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);
7253 + struct nameidata h_nd;
7255 + struct dentry *parent;
7257 + au_h_nd(&h_nd, nd);
7258 + parent = nd->path.dentry;
7259 + locked = (nd && nd->path.dentry != dentry);
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);
7270 + di_read_unlock(parent, AuLock_IR);
7273 + if (unlikely(valid < 0))
7283 +/* todo: remove this */
7284 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
7285 + struct nameidata *nd, int do_udba)
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;
7301 + unhashed = !!d_unhashed(dentry);
7302 + is_root = !!IS_ROOT(dentry);
7303 + name = &dentry->d_name;
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.
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);
7320 + bstart = au_dbstart(dentry);
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);
7329 + AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
7330 + h_name = &h_dentry->d_name;
7331 + if (unlikely(do_udba
7333 + && (unhashed != !!d_unhashed(h_dentry)
7334 + || name->len != h_name->len
7335 + || memcmp(name->name, h_name->name, name->len))
7337 + AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
7338 + unhashed, d_unhashed(h_dentry),
7339 + AuDLNPair(dentry), AuDLNPair(h_dentry));
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 */
7348 + /* todo: plink too? */
7353 + h_inode = h_dentry->d_inode;
7354 + if (unlikely(!!inode != !!h_inode))
7359 + h_cached_inode = h_inode;
7361 + h_mode = (h_inode->i_mode & S_IFMT);
7362 + h_plus = (h_inode->i_nlink > 0);
7364 + if (inode && ibs <= bindex && bindex <= ibe)
7365 + h_cached_inode = au_h_iptr(inode, bindex);
7367 + if (unlikely(plus != h_plus
7369 + || h_cached_inode != h_inode))
7381 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
7384 + struct dentry *parent;
7385 + struct inode *inode;
7387 + inode = dentry->d_inode;
7388 + if (au_digen(dentry) == sigen && au_iigen(inode) == sigen)
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);
7397 + /* returns a number of positive dentries */
7398 + err = au_refresh_hdentry(dentry, inode->i_mode & S_IFMT);
7400 + err = au_refresh_hinode(inode, dentry);
7402 + di_read_unlock(parent, AuLock_IR);
7407 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
7410 + struct dentry *d, *parent;
7411 + struct inode *inode;
7413 + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIRS))
7414 + return simple_reval_dpath(dentry, sigen);
7416 + /* slow loop, keep it simple and stupid */
7417 + /* cf: au_cpup_dirs() */
7420 + while (au_digen(dentry) != sigen
7421 + || au_iigen(dentry->d_inode) != sigen) {
7425 + parent = dget_parent(d);
7426 + if (au_digen(parent) == sigen
7427 + && au_iigen(parent->d_inode) == sigen)
7432 + inode = d->d_inode;
7434 + di_write_lock_child(d);
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);
7442 + err = au_refresh_hinode(inode, d);
7443 + di_read_unlock(parent, AuLock_IR);
7447 + di_write_unlock(d);
7449 + if (unlikely(err))
7457 + * if valid returns 1, otherwise 0.
7459 +static int aufs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
7462 + unsigned int sigen;
7463 + unsigned char do_udba;
7464 + struct super_block *sb;
7465 + struct inode *inode;
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));
7475 + err = au_reval_dpath(dentry, sigen);
7476 + if (unlikely(err))
7478 + AuDebugOn(au_digen(dentry) != sigen);
7480 + if (inode && au_iigen(inode) != sigen) {
7481 + AuDebugOn(IS_ROOT(dentry));
7482 + err = au_refresh_hinode(inode, dentry);
7483 + if (unlikely(err))
7485 + AuDebugOn(au_iigen(inode) != sigen);
7487 + di_downgrade_lock(dentry, AuLock_IR);
7489 + AuDebugOn(au_digen(dentry) != sigen);
7490 + AuDebugOn(inode && au_iigen(inode) != sigen);
7492 + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7493 + if (do_udba && inode) {
7494 + aufs_bindex_t bstart = au_ibstart(inode);
7497 + && au_test_higen(inode, au_h_iptr(inode, bstart)))
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 */
7508 + di_downgrade_lock(dentry, AuLock_IR);
7510 + au_store_oflag(nd, inode);
7511 + aufs_read_unlock(dentry, AuLock_IR);
7515 + AuDbg("%.*s invalid\n", AuDLNPair(dentry));
7519 +static void aufs_d_release(struct dentry *dentry)
7521 + struct au_dinfo *dinfo;
7522 + aufs_bindex_t bend, bindex;
7524 + dinfo = dentry->d_fsdata;
7528 + /* dentry may not be revalidated */
7529 + bindex = dinfo->di_bstart;
7530 + if (bindex >= 0) {
7531 + struct au_hdentry *p;
7533 + bend = dinfo->di_bend;
7534 + p = dinfo->di_hdentry + bindex;
7535 + while (bindex++ <= bend) {
7541 + kfree(dinfo->di_hdentry);
7542 + au_rwsem_destroy(&dinfo->di_rwsem);
7543 + au_cache_free_dinfo(dinfo);
7544 + au_hin_di_reinit(dentry);
7547 +struct dentry_operations aufs_dop = {
7548 + .d_revalidate = aufs_d_revalidate,
7549 + .d_release = aufs_d_release
7551 diff --git a/fs/aufs/dentry.h b/fs/aufs/dentry.h
7552 new file mode 100644
7553 index 0000000..8708db8
7555 +++ b/fs/aufs/dentry.h
7558 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
7567 + * lookup and dentry operations
7570 +#ifndef __AUFS_DENTRY_H__
7571 +#define __AUFS_DENTRY_H__
7575 +#include <linux/fs.h>
7576 +#include <linux/namei.h>
7577 +#include <linux/aufs_type.h>
7580 +/* make a single member structure for future use */
7581 +/* todo: remove this structure */
7582 +struct au_hdentry {
7583 + struct dentry *hd_dentry;
7587 + atomic_t di_generation;
7589 + struct rw_semaphore di_rwsem;
7590 + aufs_bindex_t di_bstart, di_bend, di_bwh, di_bdiropq;
7591 + struct au_hdentry *di_hdentry;
7594 +/* ---------------------------------------------------------------------- */
7597 +extern struct dentry_operations aufs_dop;
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);
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);
7613 +int au_alloc_dinfo(struct dentry *dentry);
7614 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
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);
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);
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);
7637 +/* ---------------------------------------------------------------------- */
7639 +static inline struct au_dinfo *au_di(struct dentry *dentry)
7641 + return dentry->d_fsdata;
7644 +/* ---------------------------------------------------------------------- */
7646 +/* lock subclass for dinfo */
7648 + AuLsc_DI_CHILD, /* child first */
7649 + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hinotify */
7650 + AuLsc_DI_CHILD3, /* copyup dirs */
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,
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); }
7668 +#define AuWriteLockFunc(name, lsc) \
7669 +static inline void di_write_lock_##name(struct dentry *d) \
7670 +{ di_write_lock(d, AuLsc_DI_##lsc); }
7672 +#define AuRWLockFuncs(name, lsc) \
7673 + AuReadLockFunc(name, lsc) \
7674 + AuWriteLockFunc(name, lsc)
7676 +AuRWLockFuncs(child, CHILD);
7677 +AuRWLockFuncs(child2, CHILD2);
7678 +AuRWLockFuncs(child3, CHILD3);
7679 +AuRWLockFuncs(parent, PARENT);
7680 +AuRWLockFuncs(parent2, PARENT2);
7681 +AuRWLockFuncs(parent3, PARENT3);
7683 +#undef AuReadLockFunc
7684 +#undef AuWriteLockFunc
7685 +#undef AuRWLockFuncs
7687 +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
7689 +/* ---------------------------------------------------------------------- */
7691 +/* todo: memory barrier? */
7692 +static inline unsigned int au_digen(struct dentry *d)
7694 + return atomic_read(&au_di(d)->di_generation);
7697 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
7699 + hdentry->hd_dentry = NULL;
7702 +static inline void au_hdput(struct au_hdentry *hd)
7704 + dput(hd->hd_dentry);
7707 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
7709 + return au_di(dentry)->di_bstart;
7712 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
7714 + return au_di(dentry)->di_bend;
7717 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
7719 + return au_di(dentry)->di_bwh;
7722 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
7724 + return au_di(dentry)->di_bdiropq;
7727 +/* todo: hard/soft set? */
7728 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
7730 + au_di(dentry)->di_bstart = bindex;
7733 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
7735 + au_di(dentry)->di_bend = bindex;
7738 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
7740 + /* dbwh can be outside of bstart - bend range */
7741 + au_di(dentry)->di_bwh = bindex;
7744 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
7746 + au_di(dentry)->di_bdiropq = bindex;
7749 +/* ---------------------------------------------------------------------- */
7751 +#ifdef CONFIG_AUFS_HINOTIFY
7752 +static inline void au_digen_dec(struct dentry *d)
7754 + atomic_dec(&au_di(d)->di_generation);
7757 +static inline void au_hin_di_reinit(struct dentry *dentry)
7759 + dentry->d_fsdata = NULL;
7762 +static inline void au_hin_di_reinit(struct dentry *dentry __maybe_unused)
7766 +#endif /* CONFIG_AUFS_HINOTIFY */
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
7774 +++ b/fs/aufs/dinfo.c
7777 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
7786 + * dentry private data
7791 +int au_alloc_dinfo(struct dentry *dentry)
7793 + struct au_dinfo *dinfo;
7794 + struct super_block *sb;
7797 + dinfo = au_cache_alloc_dinfo();
7798 + if (unlikely(!dinfo))
7801 + sb = dentry->d_sb;
7802 + nbr = au_sbend(sb) + 1;
7805 + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
7806 + if (unlikely(!dinfo->di_hdentry))
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;
7818 + dentry->d_fsdata = dinfo;
7819 + dentry->d_op = &aufs_dop;
7820 + return 0; /* success */
7823 + au_cache_free_dinfo(dinfo);
7828 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
7831 + struct au_hdentry *hdp;
7834 + sz = sizeof(*hdp) * (dinfo->di_bend + 1);
7836 + sz = sizeof(*hdp);
7837 + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
7839 + dinfo->di_hdentry = hdp;
7846 +/* ---------------------------------------------------------------------- */
7848 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
7851 + case AuLsc_DI_CHILD:
7852 + ii_write_lock_child(inode);
7854 + case AuLsc_DI_CHILD2:
7855 + ii_write_lock_child2(inode);
7857 + case AuLsc_DI_CHILD3:
7858 + ii_write_lock_child3(inode);
7860 + case AuLsc_DI_PARENT:
7861 + ii_write_lock_parent(inode);
7863 + case AuLsc_DI_PARENT2:
7864 + ii_write_lock_parent2(inode);
7866 + case AuLsc_DI_PARENT3:
7867 + ii_write_lock_parent3(inode);
7874 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
7877 + case AuLsc_DI_CHILD:
7878 + ii_read_lock_child(inode);
7880 + case AuLsc_DI_CHILD2:
7881 + ii_read_lock_child2(inode);
7883 + case AuLsc_DI_CHILD3:
7884 + ii_read_lock_child3(inode);
7886 + case AuLsc_DI_PARENT:
7887 + ii_read_lock_parent(inode);
7889 + case AuLsc_DI_PARENT2:
7890 + ii_read_lock_parent2(inode);
7892 + case AuLsc_DI_PARENT3:
7893 + ii_read_lock_parent3(inode);
7900 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
7902 + down_read_nested(&au_di(d)->di_rwsem, lsc);
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);
7911 +void di_read_unlock(struct dentry *d, int flags)
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);
7919 + up_read(&au_di(d)->di_rwsem);
7922 +void di_downgrade_lock(struct dentry *d, int flags)
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);
7929 +void di_write_lock(struct dentry *d, unsigned int lsc)
7931 + down_write_nested(&au_di(d)->di_rwsem, lsc);
7933 + do_ii_write_lock(d->d_inode, lsc);
7936 +void di_write_unlock(struct dentry *d)
7939 + ii_write_unlock(d->d_inode);
7940 + up_write(&au_di(d)->di_rwsem);
7943 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
7945 + AuDebugOn(d1 == d2
7946 + || d1->d_inode == d2->d_inode
7947 + || d1->d_sb != d2->d_sb);
7949 + if (isdir && au_test_subdir(d1, d2)) {
7950 + di_write_lock_child(d1);
7951 + di_write_lock_child2(d2);
7953 + /* there should be no races */
7954 + di_write_lock_child(d2);
7955 + di_write_lock_child2(d1);
7959 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
7961 + AuDebugOn(d1 == d2
7962 + || d1->d_inode == d2->d_inode
7963 + || d1->d_sb != d2->d_sb);
7965 + if (isdir && au_test_subdir(d1, d2)) {
7966 + di_write_lock_parent(d1);
7967 + di_write_lock_parent2(d2);
7969 + /* there should be no races */
7970 + di_write_lock_parent(d2);
7971 + di_write_lock_parent2(d1);
7975 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
7977 + di_write_unlock(d1);
7978 + if (d1->d_inode == d2->d_inode)
7979 + up_write(&au_di(d2)->di_rwsem);
7981 + di_write_unlock(d2);
7984 +/* ---------------------------------------------------------------------- */
7986 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
7990 + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7992 + AuDebugOn(bindex < 0);
7993 + d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
7994 + AuDebugOn(d && (atomic_read(&d->d_count) <= 0));
7998 +aufs_bindex_t au_dbtail(struct dentry *dentry)
8000 + aufs_bindex_t bend, bwh;
8002 + bend = au_dbend(dentry);
8004 + bwh = au_dbwh(dentry);
8007 + if (0 < bwh && bwh < bend)
8013 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
8015 + aufs_bindex_t bend, bopq;
8017 + bend = au_dbtail(dentry);
8019 + bopq = au_dbdiropq(dentry);
8020 + if (0 <= bopq && bopq < bend)
8026 +/* ---------------------------------------------------------------------- */
8028 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8029 + struct dentry *h_dentry)
8031 + struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
8033 + if (hd->hd_dentry)
8035 + hd->hd_dentry = h_dentry;
8038 +void au_update_digen(struct dentry *dentry)
8040 + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
8041 + /* smp_mb(); */ /* atomic_set */
8044 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
8046 + struct au_dinfo *dinfo;
8047 + struct dentry *h_d;
8049 + dinfo = au_di(dentry);
8050 + if (!dinfo || dinfo->di_bstart < 0)
8053 + if (do_put_zero) {
8054 + aufs_bindex_t bindex, bend;
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);
8064 + dinfo->di_bstart = -1;
8065 + while (++dinfo->di_bstart <= dinfo->di_bend)
8066 + if (dinfo->di_hdentry[0 + dinfo->di_bstart].hd_dentry)
8068 + if (dinfo->di_bstart > dinfo->di_bend) {
8069 + dinfo->di_bstart = -1;
8070 + dinfo->di_bend = -1;
8075 + while (0 <= --dinfo->di_bend)
8076 + if (dinfo->di_hdentry[0 + dinfo->di_bend].hd_dentry)
8078 + AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
8081 +void au_update_dbstart(struct dentry *dentry)
8083 + aufs_bindex_t bindex, bend;
8084 + struct dentry *h_dentry;
8086 + bend = au_dbend(dentry);
8087 + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
8088 + h_dentry = au_h_dptr(dentry, bindex);
8091 + if (h_dentry->d_inode) {
8092 + au_set_dbstart(dentry, bindex);
8095 + au_set_h_dptr(dentry, bindex, NULL);
8099 +void au_update_dbend(struct dentry *dentry)
8101 + aufs_bindex_t bindex, bstart;
8102 + struct dentry *h_dentry;
8104 + bstart = au_dbstart(dentry);
8105 + for (bindex = au_dbend(dentry); bindex <= bstart; bindex--) {
8106 + h_dentry = au_h_dptr(dentry, bindex);
8109 + if (h_dentry->d_inode) {
8110 + au_set_dbend(dentry, bindex);
8113 + au_set_h_dptr(dentry, bindex, NULL);
8117 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
8119 + aufs_bindex_t bindex, bend;
8121 + bend = au_dbend(dentry);
8122 + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
8123 + if (au_h_dptr(dentry, bindex) == h_dentry)
8127 diff --git a/fs/aufs/dir.c b/fs/aufs/dir.c
8128 new file mode 100644
8129 index 0000000..9fc6196
8134 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
8143 + * directory operations
8146 +#include <linux/fs_stack.h>
8149 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
8151 + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8153 + dir->i_nlink += h_dir->i_nlink - 2;
8154 + if (h_dir->i_nlink < 2)
8155 + dir->i_nlink += 2;
8158 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
8160 + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8162 + dir->i_nlink -= h_dir->i_nlink - 2;
8163 + if (h_dir->i_nlink < 2)
8164 + dir->i_nlink -= 2;
8167 +/* ---------------------------------------------------------------------- */
8169 +static int reopen_dir(struct file *file)
8172 + unsigned int flags;
8173 + aufs_bindex_t bindex, btail, bstart;
8174 + struct dentry *dentry, *h_dentry;
8175 + struct file *h_file;
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);
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);
8189 + flags = file->f_flags;
8190 + for (bindex = bstart; bindex <= btail; bindex++) {
8191 + h_dentry = au_h_dptr(dentry, bindex);
8194 + h_file = au_h_fptr(file, bindex);
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);
8204 + au_update_figen(file);
8205 + /* todo: necessary? */
8206 + /* file->f_ra = h_file->f_ra; */
8213 +static int do_open_dir(struct file *file, int flags)
8216 + aufs_bindex_t bindex, btail;
8217 + struct dentry *dentry, *h_dentry;
8218 + struct file *h_file;
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);
8234 + h_file = au_h_open(dentry, bindex, flags, file);
8235 + if (IS_ERR(h_file)) {
8236 + err = PTR_ERR(h_file);
8239 + au_set_h_fptr(file, bindex, h_file);
8241 + au_update_figen(file);
8242 + /* todo: necessary? */
8243 + /* file->f_ra = h_file->f_ra; */
8245 + return 0; /* success */
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);
8255 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8256 + struct file *file)
8258 + return au_do_open(file, do_open_dir);
8261 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8262 + struct file *file)
8264 + struct au_vdir *vdir_cache;
8265 + struct super_block *sb;
8266 + struct au_sbinfo *sbinfo;
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);
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);
8279 + fi_write_unlock(file);
8280 + au_finfo_fin(file);
8281 + si_read_unlock(sb);
8285 +/* ---------------------------------------------------------------------- */
8287 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
8290 + aufs_bindex_t bend, bindex;
8291 + struct inode *inode;
8292 + struct super_block *sb;
8295 + sb = dentry->d_sb;
8296 + inode = dentry->d_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;
8303 + if (au_test_ro(sb, bindex, inode))
8305 + h_path.dentry = au_h_dptr(dentry, bindex);
8306 + if (!h_path.dentry)
8308 + h_inode = h_path.dentry->d_inode;
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,
8323 + err = filemap_fdatawrite(h_inode->i_mapping);
8325 + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
8326 + mutex_unlock(&h_inode->i_mutex);
8332 +static int au_do_fsync_dir(struct file *file, int datasync)
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;
8341 + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8342 + if (unlikely(err))
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))
8353 + err = vfs_fsync(h_file, h_file->f_dentry, datasync);
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);
8359 + mutex_unlock(h_mtx);
8368 + * @file may be NULL
8370 +static int aufs_fsync_dir(struct file *file, struct dentry *dentry,
8374 + struct super_block *sb;
8376 + IMustLock(dentry->d_inode);
8379 + sb = dentry->d_sb;
8380 + si_noflush_read_lock(sb);
8382 + err = au_do_fsync_dir(file, datasync);
8384 + di_write_lock_child(dentry);
8385 + err = au_do_fsync_dir_no_file(dentry, datasync);
8387 + au_cpup_attr_timesizes(dentry->d_inode);
8388 + di_write_unlock(dentry);
8390 + fi_write_unlock(file);
8392 + si_read_unlock(sb);
8396 +/* ---------------------------------------------------------------------- */
8398 +static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
8401 + struct dentry *dentry;
8402 + struct inode *inode;
8403 + struct super_block *sb;
8405 + dentry = file->f_dentry;
8406 + inode = dentry->d_inode;
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))
8414 + err = au_vdir_init(file);
8415 + di_downgrade_lock(dentry, AuLock_IR);
8416 + if (unlikely(err))
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)));
8425 + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
8426 + * encode_fh() and others.
8428 + struct inode *h_inode = au_h_iptr(inode, au_ibstart(inode));
8430 + di_read_unlock(dentry, AuLock_IR);
8431 + si_read_unlock(sb);
8433 + err = au_vdir_fill_de(file, dirent, filldir);
8435 + fsstack_copy_attr_atime(inode, h_inode);
8436 + fi_write_unlock(file);
8443 + di_read_unlock(dentry, AuLock_IR);
8444 + fi_write_unlock(file);
8446 + si_read_unlock(sb);
8450 +/* ---------------------------------------------------------------------- */
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; }
8458 +struct test_empty_arg {
8459 + struct au_nhash *whlist;
8460 + unsigned int flags;
8462 + aufs_bindex_t bindex;
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)
8469 + struct test_empty_arg *arg = __arg;
8470 + char *name = (void *)__name;
8473 + au_fset_testempty(arg->flags, CALLED);
8475 + if (name[0] == '.'
8476 + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
8477 + goto out; /* success */
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;
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);
8495 + AuTraceErr(arg->err);
8499 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8502 + struct file *h_file;
8504 + h_file = au_h_open(dentry, arg->bindex,
8505 + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
8507 + err = PTR_ERR(h_file);
8508 + if (IS_ERR(h_file))
8512 + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
8513 + && !h_file->f_dentry->d_inode->i_nlink)
8518 + au_fclr_testempty(arg->flags, CALLED);
8520 + err = vfsub_readdir(h_file, test_empty_cb, arg);
8523 + } while (!err && au_ftest_testempty(arg->flags, CALLED));
8527 + au_sbr_put(dentry->d_sb, arg->bindex);
8532 +struct do_test_empty_args {
8534 + struct dentry *dentry;
8535 + struct test_empty_arg *arg;
8538 +static void call_do_test_empty(void *args)
8540 + struct do_test_empty_args *a = args;
8541 + *a->errp = do_test_empty(a->dentry, a->arg);
8544 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8547 + struct dentry *h_dentry;
8548 + struct inode *h_inode;
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);
8556 + err = do_test_empty(dentry, arg);
8558 + struct do_test_empty_args args = {
8563 + unsigned int flags = arg->flags;
8565 + wkq_err = au_wkq_wait(call_do_test_empty, &args);
8566 + if (unlikely(wkq_err))
8568 + arg->flags = flags;
8574 +int au_test_empty_lower(struct dentry *dentry)
8577 + aufs_bindex_t bindex, bstart, btail;
8578 + struct test_empty_arg arg;
8579 + struct au_nhash *whlist;
8581 + whlist = au_nhash_alloc(dentry->d_sb, /*bend*/0, GFP_NOFS);
8582 + err = PTR_ERR(whlist);
8583 + if (IS_ERR(whlist))
8586 + bstart = au_dbstart(dentry);
8587 + arg.whlist = whlist;
8589 + arg.bindex = bstart;
8590 + err = do_test_empty(dentry, &arg);
8591 + if (unlikely(err))
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;
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);
8607 + au_nhash_wh_free(whlist, /*bend*/0);
8612 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
8615 + struct test_empty_arg arg;
8616 + aufs_bindex_t bindex, btail;
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;
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);
8635 +/* ---------------------------------------------------------------------- */
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
8646 diff --git a/fs/aufs/dir.h b/fs/aufs/dir.h
8647 new file mode 100644
8648 index 0000000..f41f050
8653 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
8662 + * directory operations
8665 +#ifndef __AUFS_DIR_H__
8666 +#define __AUFS_DIR_H__
8670 +#include <linux/fs.h>
8671 +#include <linux/aufs_type.h>
8673 +/* ---------------------------------------------------------------------- */
8675 +/* need to be faster and smaller */
8678 + unsigned int nh_num;
8679 + struct hlist_head *nh_head;
8682 +struct au_vdir_destr {
8683 + unsigned char len;
8684 + unsigned char name[0];
8687 +struct au_vdir_dehstr {
8688 + struct hlist_node hash;
8689 + struct au_vdir_destr *str;
8692 +struct au_vdir_de {
8694 + unsigned char de_type;
8695 + /* caution: packed */
8696 + struct au_vdir_destr de_str;
8699 +struct au_vdir_wh {
8700 + struct hlist_node wh_hash;
8701 + aufs_bindex_t wh_bindex;
8702 + struct au_vdir_destr wh_str;
8705 +union au_vdir_deblk_p {
8706 + unsigned char *deblk;
8707 + struct au_vdir_de *de;
8711 + unsigned char **vd_deblk;
8712 + unsigned long vd_nblk;
8713 + unsigned int vd_deblk_sz;
8716 + union au_vdir_deblk_p p;
8719 + unsigned long vd_version;
8720 + unsigned long vd_jiffy;
8723 +/* ---------------------------------------------------------------------- */
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);
8733 +struct au_nhash *au_nhash_alloc(struct super_block *sb, aufs_bindex_t bend,
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,
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);
8746 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
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
8754 +++ b/fs/aufs/export.c
8757 + * Copyright (C) 2005-2009 Junjiro Okajima
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.
8769 +#include <linux/exportfs.h>
8770 +#include <linux/mnt_namespace.h>
8771 +#include <linux/random.h>
8775 +#ifdef CONFIG_AUFS_INO_T_64
8783 +static ino_t decode_ino(__u32 *a)
8787 + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
8789 +#ifdef CONFIG_AUFS_INO_T_64
8795 +static void encode_ino(__u32 *a, ino_t ino)
8801 +#ifdef CONFIG_AUFS_INO_T_64
8806 +/* NFS file handle */
8810 +#ifdef CONFIG_AUFS_INO_T_64
8811 + /* support 64bit inode number */
8825 + Fh_dir_ino = Fh_dir_ino1
8828 +static int au_test_anon(struct dentry *dentry)
8830 + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
8833 +/* ---------------------------------------------------------------------- */
8834 +/* inode generation external table */
8836 +int au_xigen_inc(struct inode *inode)
8842 + struct super_block *sb;
8843 + struct au_sbinfo *sbinfo;
8847 + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
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 */
8860 + if (unlikely(sz >= 0)) {
8862 + AuIOErr("xigen error (%zd)\n", sz);
8869 +int au_xigen_new(struct inode *inode)
8874 + struct super_block *sb;
8875 + struct au_sbinfo *sbinfo;
8876 + struct file *file;
8879 + /* todo: dirty, at mount time */
8880 + if (inode->i_ino == AUFS_ROOT_INO)
8883 + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
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);
8892 + pos *= sizeof(inode->i_generation);
8895 + sbinfo = au_sbi(sb);
8896 + file = sbinfo->si_xigen;
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);
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 */
8911 + if (unlikely(sz >= 0)) {
8913 + AuIOErr("xigen error (%zd)\n", sz);
8920 +int au_xigen_set(struct super_block *sb, struct file *base)
8923 + struct au_sbinfo *sbinfo;
8924 + struct file *file;
8926 + sbinfo = au_sbi(sb);
8927 + file = au_xino_create2(base, sbinfo->si_xigen);
8928 + err = PTR_ERR(file);
8932 + if (sbinfo->si_xigen)
8933 + fput(sbinfo->si_xigen);
8934 + sbinfo->si_xigen = file;
8940 +void au_xigen_clr(struct super_block *sb)
8942 + struct au_sbinfo *sbinfo;
8944 + sbinfo = au_sbi(sb);
8945 + if (sbinfo->si_xigen) {
8946 + fput(sbinfo->si_xigen);
8947 + sbinfo->si_xigen = NULL;
8951 +/* ---------------------------------------------------------------------- */
8953 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
8956 + struct dentry *dentry, *d;
8957 + struct inode *inode;
8958 + unsigned int sigen;
8961 + inode = ilookup(sb, ino);
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)))
8973 + if (!dir_ino || S_ISDIR(inode->i_mode))
8974 + dentry = d_find_alias(inode);
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);
8983 + spin_unlock(&dcache_lock);
8985 + if (unlikely(dentry && sigen != au_digen(dentry))) {
8987 + dentry = ERR_PTR(-ESTALE);
8996 +/* ---------------------------------------------------------------------- */
8999 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
9000 +static struct vfsmount *au_mnt_get(struct super_block *sb)
9002 + struct mnt_namespace *ns;
9003 + struct vfsmount *pos, *mnt;
9005 + spin_lock(&vfsmount_lock);
9006 + /* no get/put ?? */
9007 + AuDebugOn(!current->nsproxy);
9008 + ns = current->nsproxy->mnt_ns;
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);
9017 + spin_unlock(&vfsmount_lock);
9023 +struct au_nfsd_si_lock {
9024 + const unsigned int sigen;
9025 + const aufs_bindex_t br_id;
9026 + unsigned char force_lock;
9029 +static aufs_bindex_t si_nfsd_read_lock(struct super_block *sb,
9030 + struct au_nfsd_si_lock *nsi_lock)
9032 + aufs_bindex_t bindex;
9034 + si_read_lock(sb, AuLock_FLUSH);
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 */
9041 + if (!nsi_lock->force_lock)
9042 + si_read_unlock(sb);
9049 +struct find_name_by_ino {
9050 + int called, found;
9057 +find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
9058 + u64 ino, unsigned int d_type)
9060 + struct find_name_by_ino *a = arg;
9063 + if (a->ino != ino)
9066 + memcpy(a->name, name, namelen);
9067 + a->namelen = namelen;
9072 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
9073 + struct au_nfsd_si_lock *nsi_lock)
9075 + struct dentry *dentry, *parent;
9076 + struct file *file;
9077 + struct inode *dir;
9078 + struct find_name_by_ino arg;
9081 + parent = path->dentry;
9083 + si_read_unlock(parent->d_sb);
9085 + file = dentry_open(parent, path->mnt, au_dir_roflags, current_cred());
9086 + dentry = (void *)file;
9090 + dentry = ERR_PTR(-ENOMEM);
9091 + arg.name = __getname();
9092 + if (unlikely(!arg.name))
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))
9104 + dentry = ERR_PTR(-ENOENT);
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))
9116 + AuDebugOn(au_test_anon(dentry));
9117 + if (unlikely(!dentry->d_inode)) {
9119 + dentry = ERR_PTR(-ENOENT);
9123 + __putname(arg.name);
9127 + if (unlikely(nsi_lock
9128 + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
9129 + if (!IS_ERR(dentry)) {
9131 + dentry = ERR_PTR(-ESTALE);
9133 + AuTraceErrPtr(dentry);
9137 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
9139 + struct au_nfsd_si_lock *nsi_lock)
9141 + struct dentry *dentry;
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))
9149 + AuDebugOn(au_test_anon(path.dentry));
9151 + path.dentry = dget(sb->s_root);
9153 + path.mnt = au_mnt_get(sb);
9154 + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
9158 + AuTraceErrPtr(dentry);
9162 +/* ---------------------------------------------------------------------- */
9164 +static int h_acceptable(void *expv, struct dentry *dentry)
9169 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
9170 + char *buf, int len, struct super_block *sb)
9176 + p = d_path(h_rootpath, buf, len);
9181 + path.mnt = h_rootpath->mnt;
9182 + path.dentry = h_parent;
9183 + p = d_path(&path, buf, len);
9189 + path.mnt = au_mnt_get(sb);
9190 + path.dentry = sb->s_root;
9191 + p = d_path(&path, buf, len - strlen(p));
9196 + p[strlen(p)] = '/';
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)
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;
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));
9231 + if (unlikely(au_test_anon(h_parent))) {
9232 + AuWarn1("%s decode_fh returned a disconnected dentry\n",
9234 + goto out_h_parent;
9237 + dentry = ERR_PTR(-ENOMEM);
9238 + pathname = (void *)__get_free_page(GFP_NOFS);
9239 + if (unlikely(!pathname))
9240 + goto out_h_parent;
9242 + root = sb->s_root;
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;
9250 + goto out_pathname;
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))
9258 + dentry = ERR_PTR(-ENOENT);
9259 + AuDebugOn(au_test_anon(path.dentry));
9260 + if (unlikely(!path.dentry->d_inode))
9263 + if (ino != path.dentry->d_inode->i_ino)
9264 + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
9266 + dentry = dget(path.dentry);
9271 + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
9272 + if (!IS_ERR(dentry)) {
9274 + dentry = ERR_PTR(-ESTALE);
9277 + free_page((unsigned long)pathname);
9281 + /* au_br_put(br); */
9282 + AuTraceErrPtr(dentry);
9286 +/* ---------------------------------------------------------------------- */
9288 +static struct dentry *
9289 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
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],
9302 + AuDebugOn(fh_len < Fh_tail);
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))
9309 + nsi_lock.force_lock = 1;
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))
9321 + /* is the parent dir cached? */
9322 + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
9323 + if (IS_ERR(dentry))
9329 + dentry = decode_by_path(sb, bindex, ino, fh, fh_len, &nsi_lock);
9330 + if (IS_ERR(dentry))
9332 + if (unlikely(!dentry))
9333 + /* todo?: make it ESTALE */
9337 + if (dentry->d_inode->i_generation == fh[Fh_igen])
9338 + goto out_unlock; /* success */
9341 + dentry = ERR_PTR(-ESTALE);
9343 + si_read_unlock(sb);
9345 + AuTraceErrPtr(dentry);
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)
9354 + struct dentry *parent;
9355 + __u32 *fh = fid->raw;
9358 + dir_ino = decode_ino(fh + Fh_dir_ino);
9359 + parent = decode_by_ino(sb, dir_ino, 0);
9360 + if (IS_ERR(parent))
9363 + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
9364 + dir_ino, fh, fh_len);
9367 + AuTraceErrPtr(parent);
9372 +/* ---------------------------------------------------------------------- */
9374 +static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
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;
9384 + AuDebugOn(au_test_anon(dentry));
9388 + if (unlikely(*max_len <= Fh_tail)) {
9389 + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
9393 + err = FILEID_ROOT;
9394 + if (IS_ROOT(dentry)) {
9395 + AuDebugOn(dentry->d_inode->i_ino != AUFS_ROOT_INO);
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");
9412 + bend = au_dbtaildir(parent);
9413 + for (bindex = au_dbstart(parent); bindex <= bend; bindex++) {
9414 + h_parent = au_h_dptr(parent, bindex);
9420 + if (unlikely(!h_parent))
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));
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;
9437 + *max_len -= Fh_tail;
9438 + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
9440 + /*connectable or subtreecheck*/0);
9441 + err = fh[Fh_h_type];
9442 + *max_len += Fh_tail;
9443 + /* todo: macros? */
9447 + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
9452 + di_read_unlock(parent, !AuLock_IR);
9454 + aufs_read_unlock(dentry, AuLock_IR);
9456 + if (unlikely(err < 0))
9461 +/* ---------------------------------------------------------------------- */
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
9469 +void au_export_init(struct super_block *sb)
9471 + struct au_sbinfo *sbinfo;
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);
9481 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
9482 new file mode 100644
9483 index 0000000..7841d99
9485 +++ b/fs/aufs/f_op.c
9488 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
9497 + * file and vm operations
9500 +#include <linux/fs_stack.h>
9501 +#include <linux/poll.h>
9504 +/* common function to regular file and dir */
9505 +int aufs_flush(struct file *file, fl_owner_t id)
9508 + aufs_bindex_t bindex, bend;
9509 + struct dentry *dentry;
9510 + struct file *h_file;
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);
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)
9524 + err = h_file->f_op->flush(h_file, id);
9526 + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
9529 + au_cpup_attr_timesizes(dentry->d_inode);
9531 + di_read_unlock(dentry, AuLock_IW);
9532 + fi_read_unlock(file);
9533 + si_read_unlock(dentry->d_sb);
9537 +/* ---------------------------------------------------------------------- */
9539 +static int do_open_nondir(struct file *file, int flags)
9542 + aufs_bindex_t bindex;
9543 + struct file *h_file;
9544 + struct dentry *dentry;
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));
9554 + h_file = au_h_open(dentry, bindex, flags, file);
9555 + if (IS_ERR(h_file))
9556 + err = PTR_ERR(h_file);
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; */
9568 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
9569 + struct file *file)
9571 + return au_do_open(file, do_open_nondir);
9574 +static int aufs_release_nondir(struct inode *inode __maybe_unused,
9575 + struct file *file)
9577 + struct super_block *sb = file->f_dentry->d_sb;
9579 + si_noflush_read_lock(sb);
9580 + au_finfo_fin(file);
9581 + si_read_unlock(sb);
9585 +/* ---------------------------------------------------------------------- */
9587 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
9591 + struct dentry *dentry;
9592 + struct file *h_file;
9593 + struct super_block *sb;
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))
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);
9608 + di_read_unlock(dentry, AuLock_IR);
9609 + fi_read_unlock(file);
9611 + si_read_unlock(sb);
9615 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
9616 + size_t count, loff_t *ppos)
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;
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);
9633 + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
9634 + if (unlikely(err))
9637 + err = au_ready_to_write(file, -1, &pin);
9638 + di_downgrade_lock(dentry, AuLock_IR);
9639 + if (unlikely(err))
9642 + bstart = au_fbstart(file);
9643 + h_file = au_h_fptr(file, bstart);
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;
9650 + di_read_unlock(dentry, AuLock_IR);
9651 + fi_write_unlock(file);
9653 + si_read_unlock(sb);
9654 + mutex_unlock(&inode->i_mutex);
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)
9663 + struct file *h_file;
9664 + struct dentry *dentry;
9665 + struct super_block *sb;
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))
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? */
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);
9685 + di_read_unlock(dentry, AuLock_IR);
9686 + fi_read_unlock(file);
9689 + si_read_unlock(sb);
9694 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
9695 + size_t len, unsigned int flags)
9698 + struct au_pin pin;
9699 + struct dentry *dentry;
9700 + struct inode *inode;
9701 + struct super_block *sb;
9702 + struct file *h_file;
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);
9710 + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
9711 + if (unlikely(err))
9714 + err = au_ready_to_write(file, -1, &pin);
9715 + di_downgrade_lock(dentry, AuLock_IR);
9716 + if (unlikely(err))
9719 + h_file = au_h_fptr(file, au_fbstart(file));
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;
9726 + di_read_unlock(dentry, AuLock_IR);
9727 + fi_write_unlock(file);
9729 + si_read_unlock(sb);
9730 + mutex_unlock(&inode->i_mutex);
9734 +/* ---------------------------------------------------------------------- */
9736 +static struct file *au_safe_file(struct vm_area_struct *vma)
9738 + struct file *file;
9740 + file = vma->vm_file;
9741 + if (file->private_data && au_test_aufs(file->f_dentry->d_sb))
9746 +static void au_reset_file(struct vm_area_struct *vma, struct file *file)
9748 + vma->vm_file = file;
9749 + /* smp_mb(); */ /* flush vm_file */
9752 +static int aufs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
9755 + static DECLARE_WAIT_QUEUE_HEAD(wq);
9756 + struct file *file, *h_file;
9757 + struct au_finfo *finfo;
9759 + /* todo: non-robr mode, user vm_file as it is? */
9760 + wait_event(wq, (file = au_safe_file(vma)));
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));
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); */
9784 +static struct vm_operations_struct aufs_vm_ops = {
9785 + .fault = aufs_fault
9788 +/* ---------------------------------------------------------------------- */
9790 +static struct vm_operations_struct *au_vm_ops(struct file *h_file,
9791 + struct vm_area_struct *vma)
9793 + struct vm_operations_struct *vm_ops;
9796 + vm_ops = ERR_PTR(-ENODEV);
9797 + if (!h_file->f_op || !h_file->f_op->mmap)
9800 + err = h_file->f_op->mmap(h_file, vma);
9801 + vm_ops = ERR_PTR(err);
9802 + if (unlikely(err))
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);
9818 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
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;
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))
9837 + struct au_pin pin;
9839 + err = au_ready_to_write(file, -1, &pin);
9840 + di_downgrade_lock(dentry, AuLock_IR);
9841 + if (unlikely(err))
9844 + } else if (!mmapped)
9845 + di_downgrade_lock(dentry, AuLock_IR);
9847 + h_file = au_h_fptr(file, au_fbstart(file));
9848 + if (au_test_fs_bad_mapping(h_file->f_dentry->d_sb)) {
9850 + * by this assignment, f_mapping will differs from aufs inode
9852 + * if someone else mixes the use of f_dentry->d_inode and
9853 + * f_mapping->host, then a problem may arise.
9855 + file->f_mapping = h_file->f_mapping;
9860 + vm_ops = au_vm_ops(h_file, vma);
9861 + err = PTR_ERR(vm_ops);
9862 + if (IS_ERR(vm_ops))
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.
9873 + err = generic_file_mmap(file, vma);
9874 + if (unlikely(err))
9876 + vma->vm_ops = &aufs_vm_ops;
9878 + if (!au_test_mmapped(file))
9879 + au_fi(file)->fi_h_vm_ops = vm_ops;
9881 + vfsub_file_accessed(h_file);
9882 + fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
9885 + di_read_unlock(dentry, AuLock_IR);
9886 + if (!wlock && mmapped)
9887 + fi_read_unlock(file);
9889 + fi_write_unlock(file);
9891 + si_read_unlock(sb);
9895 +/* ---------------------------------------------------------------------- */
9897 +static unsigned int aufs_poll(struct file *file, poll_table *wait)
9899 + unsigned int mask;
9901 + struct file *h_file;
9902 + struct dentry *dentry;
9903 + struct super_block *sb;
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))
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);
9920 + di_read_unlock(dentry, AuLock_IR);
9921 + fi_read_unlock(file);
9924 + si_read_unlock(sb);
9925 + AuTraceErr((int)mask);
9929 +static int aufs_fsync_nondir(struct file *file, struct dentry *dentry,
9933 + struct au_pin pin;
9934 + struct inode *inode;
9935 + struct file *h_file;
9936 + struct super_block *sb;
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);
9946 + sb = dentry->d_sb;
9947 + si_read_lock(sb, AuLock_FLUSH);
9949 + err = 0; /* -EBADF; */ /* posix? */
9950 + if (unlikely(!(file->f_mode & FMODE_WRITE)))
9952 + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
9953 + if (unlikely(err))
9956 + err = au_ready_to_write(file, -1, &pin);
9957 + di_downgrade_lock(dentry, AuLock_IR);
9958 + if (unlikely(err))
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;
9969 + * no filemap_fdatawrite() since aufs file has no its own
9970 + * mapping, but dir.
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);
9977 + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
9979 + au_cpup_attr_timesizes(inode);
9980 + mutex_unlock(h_mtx);
9984 + di_read_unlock(dentry, AuLock_IR);
9985 + fi_write_unlock(file);
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);
9995 +static int aufs_fasync(int fd, struct file *file, int flag)
9998 + struct file *h_file;
9999 + struct dentry *dentry;
10000 + struct super_block *sb;
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))
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);
10013 + di_read_unlock(dentry, AuLock_IR);
10014 + fi_read_unlock(file);
10017 + si_read_unlock(sb);
10021 +/* ---------------------------------------------------------------------- */
10023 +const struct file_operations aufs_file_fop = {
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.
10029 + /* .llseek = generic_file_llseek, */
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
10043 diff --git a/fs/aufs/file.c b/fs/aufs/file.c
10044 new file mode 100644
10045 index 0000000..d472587
10047 +++ b/fs/aufs/file.c
10050 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
10059 + * handling file/dir, and address_space operation
10062 +#include <linux/fsnotify.h>
10063 +#include <linux/pagemap.h>
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.
10071 +void au_store_oflag(struct nameidata *nd, struct inode *inode)
10074 + /* && !(nd->flags & LOOKUP_CONTINUE) */
10075 + && (nd->flags & LOOKUP_OPEN)
10076 + && (nd->intent.open.flags & vfsub_fmode_to_uint(FMODE_EXEC))
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;
10086 +/* drop flags for writing */
10087 +unsigned int au_file_roflags(unsigned int flags)
10089 + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
10090 + flags |= O_RDONLY | O_NOATIME;
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)
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;
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))
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))
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,
10127 + if (IS_ERR(h_file))
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)) {
10135 + h_file = ERR_PTR(err);
10139 + fsnotify_open(h_dentry);
10140 + goto out; /* success */
10143 + atomic_dec(&br->br_count);
10148 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags))
10151 + struct dentry *dentry;
10152 + struct super_block *sb;
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))
10161 + di_read_lock_child(dentry, AuLock_IR);
10162 + err = open(file, file->f_flags);
10163 + di_read_unlock(dentry, AuLock_IR);
10165 + fi_write_unlock(file);
10166 + if (unlikely(err))
10167 + au_finfo_fin(file);
10169 + si_read_unlock(sb);
10173 +int au_reopen_nondir(struct file *file)
10176 + aufs_bindex_t bstart, bindex, bend;
10177 + struct dentry *dentry;
10178 + struct file *h_file, *h_file_tmp;
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);
10191 + AuDebugOn(au_fbstart(file) < bstart
10192 + || au_fi(file)->fi_hfile[0 + bstart].hf_file);
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? */
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; */
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);
10214 + fput(h_file_tmp);
10218 +/* ---------------------------------------------------------------------- */
10220 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
10221 + struct dentry *hi_wh)
10224 + aufs_bindex_t bstart;
10225 + struct au_dinfo *dinfo;
10226 + struct dentry *h_dentry;
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;
10240 +static int au_ready_to_write_wh(struct file *file, loff_t len,
10241 + aufs_bindex_t bcpup)
10244 + struct inode *inode;
10245 + struct dentry *dentry, *hi_wh;
10246 + struct super_block *sb;
10248 + dentry = file->f_dentry;
10249 + inode = dentry->d_inode;
10250 + hi_wh = au_hi_wh(inode, bcpup);
10252 + err = au_sio_cpup_wh(dentry, bcpup, len, file);
10254 + /* already copied-up after unlink */
10255 + err = au_reopen_wh(file, bcpup, hi_wh);
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));
10265 + * prepare the @file for writing.
10267 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
10270 + aufs_bindex_t bstart, bcpup;
10271 + struct dentry *dentry, *parent, *h_dentry;
10272 + struct inode *h_inode, *inode;
10273 + struct super_block *sb;
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);
10285 + /* need to cpup */
10286 + parent = dget_parent(dentry);
10287 + di_write_lock_parent(parent);
10288 + err = AuWbrCopyup(au_sbi(sb), dentry);
10290 + if (unlikely(err < 0))
10294 + if (!au_h_dptr(parent, bcpup)) {
10295 + err = au_cpup_dirs(dentry, bcpup);
10296 + if (unlikely(err))
10300 + err = au_pin(pin, dentry, bcpup, AuOpt_UDBA_NONE,
10301 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10302 + if (unlikely(err))
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);
10313 + di_downgrade_lock(parent, AuLock_IR);
10314 + if (!au_h_dptr(dentry, bcpup))
10315 + err = au_sio_cpup_simple(dentry, bcpup, len,
10318 + err = au_reopen_nondir(file);
10320 + mutex_unlock(&h_inode->i_mutex);
10323 + au_pin_set_parent_lflag(pin, /*lflag*/0);
10324 + goto out_dput; /* success */
10330 + di_downgrade_lock(parent, AuLock_IR);
10332 + di_read_unlock(parent, AuLock_IR);
10339 +/* ---------------------------------------------------------------------- */
10341 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
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;
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)
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);
10365 + di_read_unlock(parent, !AuLock_IR);
10366 + if (unlikely(err < 0))
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))
10380 + /* always superio. */
10381 + err = au_pin(&pin, dentry, bstart, AuOpt_UDBA_NONE,
10382 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10384 + err = au_sio_cpup_simple(dentry, bstart, -1,
10387 + } else if (hi_wh) {
10388 + /* already copied-up after unlink */
10389 + err = au_reopen_wh(file, bstart, hi_wh);
10390 + *need_reopen = 0;
10394 + di_read_unlock(parent, AuLock_IR);
10401 +static void au_do_refresh_file(struct file *file)
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;
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++) {
10417 + new_bindex = au_br_index(sb, p->hf_br->br_id);
10418 + if (new_bindex == bindex)
10420 + if (new_bindex < 0) {
10421 + au_set_h_fptr(file, bindex, NULL);
10425 + /* swap two lower inode, and loop again */
10426 + q = finfo->fi_hfile + new_bindex;
10430 + if (tmp.hf_file) {
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)
10446 + au_hfput(p, file);
10449 + bend = au_br_index(sb, brid);
10450 + for (finfo->fi_bstart = 0; finfo->fi_bstart < bend;
10451 + finfo->fi_bstart++, p++)
10453 + au_hfput(p, file);
10454 + bend = au_sbend(sb);
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)
10465 + au_hfput(p, file);
10467 + AuDebugOn(finfo->fi_bend < finfo->fi_bstart);
10471 + * after branch manipulating, refresh the file.
10473 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
10475 + int err, need_reopen;
10476 + struct dentry *dentry;
10477 + aufs_bindex_t bend, bindex;
10479 + dentry = file->f_dentry;
10480 + err = au_fi_realloc(au_fi(file), au_sbend(dentry->d_sb) + 1);
10481 + if (unlikely(err))
10483 + au_do_refresh_file(file);
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);
10492 + au_update_figen(file);
10493 + return 0; /* success */
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);
10505 +/* common function to regular file and dir */
10506 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10510 + unsigned int sigen, figen;
10511 + aufs_bindex_t bstart;
10512 + unsigned char pseudo_link;
10513 + struct dentry *dentry;
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) {
10525 + di_downgrade_lock(dentry, AuLock_IR);
10526 + fi_downgrade_lock(file);
10528 + goto out; /* success */
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))
10537 + AuDebugOn(au_digen(dentry) != sigen
10538 + || au_iigen(dentry->d_inode) != sigen);
10541 + err = refresh_file(file, reopen);
10544 + di_downgrade_lock(dentry, AuLock_IR);
10545 + fi_downgrade_lock(file);
10548 + di_write_unlock(dentry);
10549 + fi_write_unlock(file);
10556 +/* ---------------------------------------------------------------------- */
10558 +/* cf. aufs_nopage() */
10559 +/* for madvise(2) */
10560 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
10562 + unlock_page(page);
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(); }
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 */
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 */
10606 diff --git a/fs/aufs/file.h b/fs/aufs/file.h
10607 new file mode 100644
10608 index 0000000..dd9ee61
10610 +++ b/fs/aufs/file.h
10613 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
10622 + * file operations
10625 +#ifndef __AUFS_FILE_H__
10626 +#define __AUFS_FILE_H__
10630 +#include <linux/file.h>
10631 +#include <linux/fs.h>
10632 +#include <linux/aufs_type.h>
10633 +#include "rwsem.h"
10637 + struct file *hf_file;
10638 + struct au_branch *hf_br;
10643 + atomic_t fi_generation;
10645 + struct rw_semaphore fi_rwsem;
10646 + struct au_hfile *fi_hfile;
10647 + aufs_bindex_t fi_bstart, fi_bend;
10650 + /* non-dir only */
10651 + struct vm_operations_struct *fi_h_vm_ops;
10655 + struct au_vdir *fi_vdir_cache;
10656 + int fi_maintain_plink;
10661 +/* ---------------------------------------------------------------------- */
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);
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),
10677 +extern const struct file_operations aufs_file_fop;
10678 +int aufs_flush(struct file *file, fl_owner_t id);
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);
10685 +void au_update_figen(struct file *file);
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);
10691 +/* ---------------------------------------------------------------------- */
10693 +static inline struct au_finfo *au_fi(struct file *file)
10695 + return file->private_data;
10698 +/* ---------------------------------------------------------------------- */
10701 + * fi_read_lock, fi_write_lock,
10702 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
10704 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
10706 +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
10708 +/* ---------------------------------------------------------------------- */
10710 +/* todo: hard/soft set? */
10711 +static inline aufs_bindex_t au_fbstart(struct file *file)
10713 + return au_fi(file)->fi_bstart;
10716 +static inline aufs_bindex_t au_fbend(struct file *file)
10718 + return au_fi(file)->fi_bend;
10721 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
10723 + return au_fi(file)->fi_vdir_cache;
10726 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
10728 + au_fi(file)->fi_bstart = bindex;
10731 +static inline void au_set_fbend(struct file *file, aufs_bindex_t bindex)
10733 + au_fi(file)->fi_bend = bindex;
10736 +static inline void au_set_fvdir_cache(struct file *file,
10737 + struct au_vdir *vdir_cache)
10739 + au_fi(file)->fi_vdir_cache = vdir_cache;
10742 +static inline struct file *au_h_fptr(struct file *file, aufs_bindex_t bindex)
10744 + return au_fi(file)->fi_hfile[0 + bindex].hf_file;
10747 +/* todo: memory barrier? */
10748 +static inline unsigned int au_figen(struct file *f)
10750 + return atomic_read(&au_fi(f)->fi_generation);
10753 +static inline int au_test_mmapped(struct file *f)
10755 + return !!(au_fi(f)->fi_h_vm_ops);
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
10764 +++ b/fs/aufs/finfo.c
10767 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
10776 + * file private data
10781 +void au_hfput(struct au_hfile *hf, struct file *file)
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;
10791 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
10793 + struct au_finfo *finfo = au_fi(file);
10794 + struct au_hfile *hf;
10796 + hf = finfo->fi_hfile + bindex;
10798 + au_hfput(hf, file);
10800 + hf->hf_file = val;
10801 + hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
10805 +void au_update_figen(struct file *file)
10807 + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
10808 + /* smp_mb(); */ /* atomic_set */
10811 +/* ---------------------------------------------------------------------- */
10813 +void au_finfo_fin(struct file *file)
10815 + struct au_finfo *finfo;
10816 + aufs_bindex_t bindex, bend;
10818 + fi_write_lock(file);
10819 + bend = au_fbend(file);
10820 + bindex = au_fbstart(file);
10823 + * calls fput() instead of filp_close(),
10824 + * since no dnotify or lock for the lower file.
10826 + for (; bindex <= bend; bindex++)
10827 + au_set_h_fptr(file, bindex, NULL);
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);
10837 +int au_finfo_init(struct file *file)
10839 + struct au_finfo *finfo;
10840 + struct dentry *dentry;
10841 + unsigned long ul;
10843 + dentry = file->f_dentry;
10844 + finfo = au_cache_alloc_finfo();
10845 + if (unlikely(!finfo))
10848 + finfo->fi_hfile = kcalloc(au_sbend(dentry->d_sb) + 1,
10849 + sizeof(*finfo->fi_hfile), GFP_NOFS);
10850 + if (unlikely(!finfo->fi_hfile))
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 */
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 */
10868 + au_cache_free_finfo(finfo);
10873 +int au_fi_realloc(struct au_finfo *finfo, int nbr)
10876 + struct au_hfile *hfp;
10879 + sz = sizeof(*hfp) * (finfo->fi_bend + 1);
10881 + sz = sizeof(*hfp);
10882 + hfp = au_kzrealloc(finfo->fi_hfile, sz, sizeof(*hfp) * nbr, GFP_NOFS);
10884 + finfo->fi_hfile = hfp;
10890 diff --git a/fs/aufs/fstype.h b/fs/aufs/fstype.h
10891 new file mode 100644
10892 index 0000000..54896c6
10894 +++ b/fs/aufs/fstype.h
10897 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
10906 + * judging filesystem type
10909 +#ifndef __AUFS_FSTYPE_H__
10910 +#define __AUFS_FSTYPE_H__
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>
10920 +static inline int au_test_aufs(struct super_block *sb)
10922 + return sb->s_magic == AUFS_SUPER_MAGIC;
10925 +static inline const char *au_sbtype(struct super_block *sb)
10927 + return sb->s_type->name;
10930 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
10932 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
10933 + return sb->s_magic == ROMFS_MAGIC;
10939 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
10941 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
10942 + return sb->s_magic == ISOFS_SUPER_MAGIC;
10948 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
10950 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
10951 + return sb->s_magic == CRAMFS_MAGIC;
10956 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
10958 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
10959 + return sb->s_magic == NFS_SUPER_MAGIC;
10965 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
10967 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
10968 + return sb->s_magic == FUSE_SUPER_MAGIC;
10974 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
10976 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
10977 + return sb->s_magic == XFS_SB_MAGIC;
10983 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
10985 +#ifdef CONFIG_TMPFS
10986 + return sb->s_magic == TMPFS_MAGIC;
10992 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
10994 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
10995 + return !strcmp(au_sbtype(sb), "ecryptfs");
11001 +static inline int au_test_smbfs(struct super_block *sb __maybe_unused)
11003 +#if defined(CONFIG_SMB_FS) || defined(CONFIG_SMB_FS_MODULE)
11004 + return sb->s_magic == SMB_SUPER_MAGIC;
11010 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
11012 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
11013 + return sb->s_magic == OCFS2_SUPER_MAGIC;
11019 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
11021 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
11022 + return sb->s_magic == DLMFS_MAGIC;
11028 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
11030 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
11031 + return sb->s_magic == CODA_SUPER_MAGIC;
11037 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
11039 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
11040 + return sb->s_magic == V9FS_MAGIC;
11046 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
11048 +#if defined(CONFIG_EXT4DEV_FS) || defined(CONFIG_EXT4DEV_FS_MODULE)
11049 + return sb->s_magic == EXT4_SUPER_MAGIC;
11055 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
11057 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
11058 + return !strcmp(au_sbtype(sb), "sysv");
11064 +static inline int au_test_ramfs(struct super_block *sb)
11066 + return sb->s_magic == RAMFS_MAGIC;
11069 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
11071 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
11072 + return sb->s_magic == UBIFS_SUPER_MAGIC;
11078 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
11080 +#ifdef CONFIG_PROC_FS
11081 + return sb->s_magic == PROC_SUPER_MAGIC;
11087 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
11089 +#ifdef CONFIG_SYSFS
11090 + return sb->s_magic == SYSFS_MAGIC;
11096 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
11098 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
11099 + return sb->s_magic == CONFIGFS_MAGIC;
11105 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
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;
11118 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
11120 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
11121 + return sb->s_magic == CIFS_MAGIC_NUMBER;
11127 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
11129 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
11130 + return sb->s_magic == MSDOS_SUPER_MAGIC;
11136 +static inline int au_test_msdos(struct super_block *sb)
11138 + return au_test_fat(sb);
11141 +static inline int au_test_vfat(struct super_block *sb)
11143 + return au_test_fat(sb);
11146 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
11148 +#ifdef CONFIG_SECURITYFS
11149 + return sb->s_magic == SECURITYFS_MAGIC;
11155 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
11157 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
11158 + return sb->s_magic == SQUASHFS_MAGIC;
11164 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
11166 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
11167 + return sb->s_magic == BTRFS_SUPER_MAGIC;
11173 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
11175 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
11176 + return sb->s_magic == XENFS_SUPER_MAGIC;
11182 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
11184 +#ifdef CONFIG_DEBUG_FS
11185 + return sb->s_magic == DEBUGFS_MAGIC;
11191 +/* ---------------------------------------------------------------------- */
11193 + * they can't be an aufs branch.
11195 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
11198 +#ifndef CONFIG_AUFS_BR_RAMFS
11199 + au_test_ramfs(sb) ||
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 */
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.
11216 +static inline int au_test_fs_null_nd(struct super_block *sb)
11218 + return !!sb->s_export_op;
11221 +static inline int au_test_fs_remote(struct super_block *sb)
11223 + return !au_test_tmpfs(sb)
11224 +#ifdef CONFIG_AUFS_BR_RAMFS
11225 + && !au_test_ramfs(sb)
11227 + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
11230 +/* ---------------------------------------------------------------------- */
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...
11238 + * some filesystems require getattr to refresh the inode attributes before
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()
11243 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
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 */
11256 + * filesystems which don't maintain i_size or i_blocks.
11258 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
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 */
11271 + * filesystems which don't store the correct value in some of their inode
11274 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
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);
11283 +/* they don't check i_nlink in link(2) */
11284 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
11286 + return au_test_tmpfs(sb)
11287 +#ifdef CONFIG_AUFS_BR_RAMFS
11288 + || au_test_ramfs(sb)
11294 + * filesystems which sets S_NOATIME and S_NOCMTIME.
11296 +static inline int au_test_fs_notime(struct super_block *sb)
11298 + return au_test_nfs(sb)
11299 + || au_test_fuse(sb)
11300 + /* || au_test_cifs(sb) */ /* untested */
11301 + /* || au_test_ubifs(sb) */ /* untested */
11306 + * filesystems which requires replacing i_mapping.
11308 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
11310 + return au_test_fuse(sb);
11313 +/* temporary support for i#1 in cramfs */
11314 +static inline int au_test_fs_unique_ino(struct inode *inode)
11316 + if (au_test_cramfs(inode->i_sb))
11317 + return inode->i_ino != 1;
11321 +/* ---------------------------------------------------------------------- */
11324 + * the filesystem where the xino files placed must support i/o after unlink and
11325 + * maintain i_size and i_blocks.
11327 +static inline int au_test_fs_bad_xino(struct super_block *sb)
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))
11334 + || !au_test_fs_null_nd(sb) /* to keep xino code simple */
11336 + /* don't want unnecessary work for xino */
11337 + || au_test_aufs(sb)
11338 + || au_test_ecryptfs(sb);
11341 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
11343 + return au_test_tmpfs(sb)
11344 + || au_test_ramfs(sb);
11348 + * test if the @sb is real-readonly.
11350 +static inline int au_test_fs_rr(struct super_block *sb)
11352 + return au_test_squashfs(sb)
11353 + || au_test_iso9660(sb)
11354 + || au_test_cramfs(sb)
11355 + || au_test_romfs(sb);
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
11364 +++ b/fs/aufs/hinotify.c
11367 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
11376 + * inotify for the lower directories
11381 +static const __u32 AuHinMask = (IN_MOVE | IN_DELETE | IN_CREATE);
11382 +static struct inotify_handle *au_hin_handle;
11384 +AuCacheFuncs(hinotify, HINOTIFY);
11386 +int au_hin_alloc(struct au_hinode *hinode, struct inode *inode,
11387 + struct inode *h_inode)
11390 + struct au_hinotify *hin;
11394 + hin = au_cache_alloc_hinotify();
11396 + AuDebugOn(hinode->hi_notify);
11397 + hinode->hi_notify = hin;
11398 + hin->hin_aufs_inode = inode;
11400 + inotify_init_watch(&hin->hin_watch);
11401 + wd = inotify_add_watch(au_hin_handle, &hin->hin_watch, h_inode,
11404 + return 0; /* success */
11407 + put_inotify_watch(&hin->hin_watch);
11408 + au_cache_free_hinotify(hin);
11409 + hinode->hi_notify = NULL;
11415 +void au_hin_free(struct au_hinode *hinode)
11418 + struct au_hinotify *hin;
11420 + hin = hinode->hi_notify;
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;
11433 +/* ---------------------------------------------------------------------- */
11435 +void au_hin_ctl(struct au_hinode *hinode, int do_set)
11437 + struct inode *h_inode;
11438 + struct inotify_watch *watch;
11440 + if (!hinode->hi_notify)
11443 + h_inode = hinode->hi_inode;
11444 + IMustLock(h_inode);
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); */
11451 + AuDebugOn(watch->mask & AuHinMask);
11452 + watch->mask |= AuHinMask;
11454 + AuDebugOn(!(watch->mask & AuHinMask));
11455 + watch->mask &= ~AuHinMask;
11457 + /* mutex_unlock(&watch->ih->mutex); */
11458 + mutex_unlock(&h_inode->inotify_mutex);
11461 +void au_reset_hinotify(struct inode *inode, unsigned int flags)
11463 + aufs_bindex_t bindex, bend;
11464 + struct inode *hi;
11465 + struct dentry *iwhdentry;
11467 + bend = au_ibend(inode);
11468 + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
11469 + hi = au_h_iptr(inode, bindex);
11473 + /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
11474 + iwhdentry = au_hi_wh(inode, bindex);
11478 + au_set_h_iptr(inode, bindex, NULL, 0);
11479 + au_set_h_iptr(inode, bindex, au_igrab(hi),
11480 + flags & ~AuHi_XINO);
11483 + /* mutex_unlock(&hi->i_mutex); */
11487 +/* ---------------------------------------------------------------------- */
11489 +static int hin_xino(struct inode *inode, struct inode *h_inode)
11492 + aufs_bindex_t bindex, bend, bfound, bstart;
11493 + struct inode *h_i;
11496 + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
11497 + AuWarn("branch root dir was changed\n");
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 */
11510 + for (bindex = bstart; bindex <= bend; bindex++) {
11511 + if (au_h_iptr(inode, bindex) == h_inode) {
11519 + for (bindex = bstart; bindex <= bend; bindex++) {
11520 + h_i = au_h_iptr(inode, bindex);
11524 + err = au_xino_write0(inode->i_sb, bindex, h_i->i_ino, 0);
11525 + /* ignore this error */
11526 + /* bad action? */
11529 + /* children inode number will be broken */
11536 +static int hin_gen_tree(struct dentry *dentry)
11538 + int err, i, j, ndentry;
11539 + struct au_dcsub_pages dpages;
11540 + struct au_dpage *dpage;
11541 + struct dentry **dentries;
11543 + err = au_dpages_init(&dpages, GFP_NOFS);
11544 + if (unlikely(err))
11546 + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
11547 + if (unlikely(err))
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;
11564 + /* todo: reset children xino?
11565 + cached children only? */
11566 + au_iigen_dec(d->d_inode);
11571 + au_dpages_free(&dpages);
11573 + /* discard children */
11574 + dentry_unhash(dentry);
11581 + * return 0 if processed.
11583 +static int hin_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
11584 + const unsigned int isdir)
11587 + struct dentry *d;
11588 + struct qstr *dname;
11591 + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
11592 + AuWarn("branch root dir was changed\n");
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))
11607 + spin_lock(&d->d_lock);
11610 + spin_unlock(&d->d_lock);
11613 + spin_unlock(&dcache_lock);
11615 + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIRS);
11616 + d = d_find_alias(inode);
11618 + au_iigen_dec(inode);
11622 + dname = &d->d_name;
11623 + if (dname->len == nlen && !memcmp(dname->name, name, nlen))
11624 + err = hin_gen_tree(d);
11633 +static int hin_gen_by_name(struct dentry *dentry, const unsigned int isdir)
11636 + struct inode *inode;
11638 + inode = dentry->d_inode;
11639 + if (IS_ROOT(dentry)
11640 + /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
11642 + AuWarn("branch root dir was changed\n");
11649 + au_digen_dec(dentry);
11651 + au_iigen_dec(inode);
11653 + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIRS);
11655 + err = hin_gen_tree(dentry);
11662 +/* ---------------------------------------------------------------------- */
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; }
11675 +struct hin_job_args {
11676 + unsigned int flags;
11677 + struct inode *inode, *h_inode, *dir, *h_dir;
11678 + struct dentry *dentry;
11683 +static int hin_job(struct hin_job_args *a)
11685 + const unsigned int isdir = au_ftest_hinjob(a->flags, ISDIR);
11688 + if (au_ftest_hinjob(a->flags, XINO0) && a->inode)
11689 + hin_xino(a->inode, a->h_inode); /* ignore this error */
11691 + if (au_ftest_hinjob(a->flags, TRYXINO0)
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);
11700 + /* make the generation obsolete */
11701 + if (au_ftest_hinjob(a->flags, GEN)) {
11704 + err = hin_gen_by_inode(a->h_name, a->h_nlen, a->inode,
11706 + if (err && a->dentry)
11707 + hin_gen_by_name(a->dentry, isdir);
11708 + /* ignore this error */
11711 + /* make dir entries obsolete */
11712 + if (au_ftest_hinjob(a->flags, DIRENT) && a->inode) {
11713 + struct au_vdir *vdir;
11715 + vdir = au_ivdir(a->inode);
11717 + vdir->vd_jiffy = 0;
11718 + /* IMustLock(a->inode); */
11719 + /* a->inode->i_version++; */
11722 + /* can do nothing but warn */
11723 + if (au_ftest_hinjob(a->flags, MNTPNT)
11725 + && d_mountpoint(a->dentry))
11726 + AuWarn("mount-point %.*s is removed or renamed\n",
11727 + AuDLNPair(a->dentry));
11732 +/* ---------------------------------------------------------------------- */
11734 +static char *in_name(u32 mask)
11736 +#ifdef CONFIG_AUFS_DEBUG
11737 +#define test_ret(flag) if (mask & 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);
11761 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
11762 + struct inode *dir)
11764 + struct dentry *dentry, *d, *parent;
11765 + struct qstr *dname;
11767 + parent = d_find_alias(dir);
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))
11778 + if (!atomic_read(&d->d_count) || !d->d_fsdata) {
11779 + spin_lock(&d->d_lock);
11781 + spin_unlock(&d->d_lock);
11785 + dentry = dget(d);
11788 + spin_unlock(&dcache_lock);
11792 + di_write_lock_child(dentry);
11797 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
11798 + aufs_bindex_t bindex, ino_t h_ino)
11800 + struct inode *inode;
11805 + err = au_xino_read(sb, bindex, h_ino, &ino);
11807 + inode = ilookup(sb, ino);
11811 + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
11812 + AuWarn("wrong root branch\n");
11818 + ii_write_lock_child(inode);
11824 +enum { CHILD, PARENT };
11825 +struct postproc_args {
11826 + struct inode *h_dir, *dir, *h_child_inode;
11828 + unsigned int flags[2];
11829 + unsigned int h_child_nlen;
11830 + char h_child_name[];
11833 +static void postproc(void *_args)
11835 + struct postproc_args *a = _args;
11836 + struct super_block *sb;
11837 + aufs_bindex_t bindex, bend, bfound;
11838 + unsigned char xino, try_iput;
11840 + struct inode *inode;
11842 + struct hin_job_args args;
11843 + struct dentry *dentry;
11844 + struct au_sbinfo *sbinfo;
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);
11857 + * do not lock a->dir->i_mutex here
11858 + * because of d_revalidate() may cause a deadlock.
11860 + sb = a->dir->i_sb;
11862 + sbinfo = au_sbi(sb);
11863 + AuDebugOn(!sbinfo);
11864 + /* big aufs lock */
11865 + si_noflush_write_lock(sb);
11867 + ii_read_lock_parent(a->dir);
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) {
11875 + ii_read_unlock(a->dir);
11876 + if (unlikely(bfound < 0))
11879 + xino = !!au_opt_test(au_mntflags(sb), XINO);
11881 + if (a->h_child_inode)
11882 + h_ino = a->h_child_inode->i_ino;
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,
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);
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);
11910 + if (dentry->d_fsdata)
11911 + di_write_unlock(dentry);
11914 + if (inode && try_iput) {
11915 + ii_write_unlock(inode);
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;
11925 + args.h_dir = NULL;
11926 + args.h_name = NULL;
11928 + err = hin_job(&args);
11929 + ii_write_unlock(a->dir);
11932 + au_nwt_done(&sbinfo->si_nowait);
11933 + si_write_unlock(sb);
11935 + iput(a->h_child_inode);
11941 +/* ---------------------------------------------------------------------- */
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)
11947 + struct au_hinotify *hinotify;
11948 + struct postproc_args *args;
11949 + int len, wkq_err;
11950 + unsigned char isdir, isroot, wh;
11952 + struct inode *dir;
11953 + unsigned int flags[2];
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);
11961 +#ifdef AuDbgHinotify
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,"
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);
11974 + hinotify = container_of(watch, struct au_hinotify, hin_watch);
11975 + AuDebugOn(!hinotify || !hinotify->hin_aufs_inode);
11976 + dir = igrab(hinotify->hin_aufs_inode);
11980 + isroot = (dir->i_ino == AUFS_ROOT_INO);
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;
11993 + if (h_child_inode)
11994 + isdir = !!S_ISDIR(h_child_inode->i_mode);
11995 + flags[PARENT] = AuHinJob_ISDIR;
11996 + flags[CHILD] = 0;
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);
12010 + AuDebugOn(!h_child_name || !h_child_inode);
12011 + au_fset_hinjob(flags[PARENT], DIRENT);
12012 + au_fset_hinjob(flags[CHILD], GEN);
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().
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);
12033 + h_child_inode = NULL;
12035 + /* iput() and kfree() will be called in postproc() */
12037 + * inotify_mutex is already acquired and kmalloc/prune_icache may lock
12038 + * iprune_mutex. strange.
12041 + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
12043 + if (unlikely(!args)) {
12044 + AuErr1("no memory\n");
12048 + args->flags[PARENT] = flags[PARENT];
12049 + args->flags[CHILD] = flags[CHILD];
12050 + args->mask = mask;
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;
12058 + p = (void *)args;
12059 + p += sizeof(*args);
12060 + memcpy(p, h_child_name, len + 1);
12064 + wkq_err = au_wkq_nowait(postproc, args, dir->i_sb);
12066 + if (unlikely(wkq_err))
12067 + AuErr("wkq %d\n", wkq_err);
12070 +static void aufs_inotify_destroy(struct inotify_watch *watch __maybe_unused)
12075 +static struct inotify_operations aufs_inotify_ops = {
12076 + .handle_event = aufs_inotify,
12077 + .destroy_watch = aufs_inotify_destroy
12080 +/* ---------------------------------------------------------------------- */
12082 +static void au_hin_destroy_cache(void)
12084 + kmem_cache_destroy(au_cachep[AuCache_HINOTIFY]);
12085 + au_cachep[AuCache_HINOTIFY] = NULL;
12088 +int __init au_hinotify_init(void)
12093 + au_cachep[AuCache_HINOTIFY] = AuCache(au_hinotify);
12094 + if (au_cachep[AuCache_HINOTIFY]) {
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();
12106 +void au_hinotify_fin(void)
12108 + inotify_destroy(au_hin_handle);
12109 + if (au_cachep[AuCache_HINOTIFY])
12110 + au_hin_destroy_cache();
12112 diff --git a/fs/aufs/i_op.c b/fs/aufs/i_op.c
12113 new file mode 100644
12114 index 0000000..a64d6b0
12116 +++ b/fs/aufs/i_op.c
12119 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
12128 + * inode operations (except add/del/rename)
12131 +#include <linux/device_cgroup.h>
12132 +#include <linux/fs_stack.h>
12133 +#include <linux/uaccess.h>
12136 +static int h_permission(struct inode *h_inode, int mask,
12137 + struct vfsmount *h_mnt, int brperm)
12140 + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
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))))
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.
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);
12162 + /* AuLabel(h_inode->permission); */
12163 + err = h_inode->i_op->permission(h_inode, mask);
12168 + err = devcgroup_inode_permission(h_inode, mask);
12170 + err = security_inode_permission
12171 + (h_inode, mask & (MAY_READ | MAY_WRITE | MAY_EXEC
12178 +static int aufs_permission(struct inode *inode, int mask)
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;
12188 + sb = inode->i_sb;
12189 + si_read_lock(sb, AuLock_FLUSH);
12190 + ii_read_lock_child(inode);
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)));
12198 + bindex = au_ibstart(inode);
12199 + br = au_sbr(sb, bindex);
12200 + err = h_permission(h_inode, mask, br->br_mnt, br->br_perm);
12202 + if (write_mask && !err) {
12203 + /* test whether the upper writable branch exists */
12205 + for (; bindex >= 0; bindex--)
12206 + if (!au_br_rdonly(au_sbr(sb, bindex))) {
12214 + /* non-write to dir */
12216 + bend = au_ibend(inode);
12217 + for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
12218 + h_inode = au_h_iptr(inode, bindex);
12220 + AuDebugOn(!S_ISDIR(h_inode->i_mode));
12221 + br = au_sbr(sb, bindex);
12222 + err = h_permission(h_inode, mask, br->br_mnt,
12228 + ii_read_unlock(inode);
12229 + si_read_unlock(sb);
12233 +/* ---------------------------------------------------------------------- */
12235 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
12236 + struct nameidata *nd)
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;
12245 + /* temporary workaround for a bug in NFSD readdir */
12246 + if (!au_test_nfsd(current))
12249 + WARN_ONCE(!mutex_is_locked(&dir->i_mutex),
12250 + "a known problem of NFSD readdir since 2.6.28\n");
12253 + si_read_lock(sb, AuLock_FLUSH);
12254 + err = au_alloc_dinfo(dentry);
12255 + ret = ERR_PTR(err);
12256 + if (unlikely(err))
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);
12264 + ret = ERR_PTR(err);
12265 + if (unlikely(err < 0))
12270 + bstart = au_dbstart(dentry);
12271 + h_inode = au_h_dptr(dentry, bstart)->d_inode;
12272 + if (!S_ISDIR(h_inode->i_mode)) {
12274 + * stop 'race'-ing between hardlinks under different
12277 + mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
12279 + inode = au_new_inode(dentry, /*must_new*/0);
12280 + mutex_unlock(mtx);
12282 + inode = au_new_inode(dentry, /*must_new*/0);
12283 + ret = (void *)inode;
12285 + if (IS_ERR(inode))
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);
12294 + di_write_unlock(dentry);
12296 + si_read_unlock(sb);
12300 +/* ---------------------------------------------------------------------- */
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)
12307 + struct dentry *h_parent;
12308 + struct inode *h_dir;
12311 + au_update_dbstart(dentry);
12312 + IMustLock(parent->d_inode);
12314 + di_write_lock_parent(parent);
12317 + if (!au_h_dptr(parent, bcpup)) {
12318 + if (bstart < bcpup)
12319 + err = au_cpdown_dirs(dentry, bcpup);
12321 + err = au_cpup_dirs(dentry, bcpup);
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);
12337 + di_write_unlock(parent);
12339 + err = bcpup; /* success */
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.
12349 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
12350 + struct au_wr_dir_args *args)
12353 + aufs_bindex_t bcpup, bstart, src_bstart;
12354 + const unsigned char add_entry = !!au_ftest_wrdir(args->flags,
12356 + struct super_block *sb;
12357 + struct dentry *parent;
12358 + struct au_sbinfo *sbinfo;
12360 + sb = dentry->d_sb;
12361 + sbinfo = au_sbi(sb);
12362 + parent = dget_parent(dentry);
12363 + bstart = au_dbstart(dentry);
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));
12376 + if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
12378 + err = AuWbrCopyup(sbinfo, dentry);
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);
12385 + err = AuWbrCopyup(sbinfo, dentry);
12388 + if (unlikely(err < 0))
12392 + bcpup = args->force_btgt;
12393 + AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
12395 + AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
12396 + if (bstart < bcpup)
12397 + au_update_dbrange(dentry, /*do_put_zero*/1);
12400 + if (bcpup == bstart)
12401 + goto out; /* success */
12403 + /* copyup the new parent into the branch we process */
12404 + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
12411 +/* ---------------------------------------------------------------------- */
12413 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
12415 + if (pin && pin->parent)
12416 + return au_h_dptr(pin->parent, pin->bindex);
12420 +void au_unpin(struct au_pin *p)
12422 + if (au_ftest_pin(p->flags, MNT_WRITE))
12423 + mnt_drop_write(p->h_mnt);
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);
12432 + p->parent = NULL;
12437 +int au_do_pin(struct au_pin *p)
12440 + struct super_block *sb;
12441 + struct dentry *h_dentry, *h_parent;
12442 + struct au_branch *br;
12443 + struct inode *h_dir;
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);
12461 + if (p->bindex <= au_dbend(p->dentry))
12462 + h_dentry = au_h_dptr(p->dentry, p->bindex);
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);
12469 + h_parent = au_h_dptr(p->parent, p->bindex);
12470 + p->hdir = au_hi(p->parent->d_inode, p->bindex);
12472 + h_dir = p->hdir->hi_inode;
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);
12480 + p->parent = NULL;
12485 + au_hin_imtx_lock_nested(p->hdir, p->lsc_hi);
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);
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);
12503 + goto out; /* success */
12508 + AuErr("err %d\n", err);
12509 + err = au_busy_or_stale();
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)
12518 + p->dentry = dentry;
12520 + p->lsc_di = lsc_di;
12521 + p->lsc_hi = lsc_hi;
12522 + p->flags = flags;
12523 + p->bindex = bindex;
12525 + p->parent = NULL;
12530 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
12531 + unsigned int udba, unsigned char flags)
12533 + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
12535 + return au_do_pin(pin);
12538 +/* ---------------------------------------------------------------------- */
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; }
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;
12554 +static int au_lock_and_icpup(struct dentry *dentry, struct iattr *ia,
12555 + struct au_icpup_args *a)
12558 + unsigned int udba;
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,
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))
12580 + if (err != bstart)
12581 + au_fset_icpup(a->flags, DID_CPUP);
12584 + a->pin_flags = AuPin_MNT_WRITE;
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);
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)) {
12598 + di_write_unlock(parent);
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);
12607 + if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
12608 + sz = ia->ia_size;
12611 + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unhashed(dentry)) {
12612 + hi_wh = au_hi_wh(inode, a->btgt);
12614 + err = au_sio_cpup_wh(dentry, a->btgt, sz, /*file*/NULL);
12615 + if (unlikely(err))
12617 + hi_wh = au_hi_wh(inode, a->btgt);
12618 + /* todo: revalidate hi_wh? */
12623 + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
12624 + di_downgrade_lock(parent, AuLock_IR);
12627 + if (!au_ftest_icpup(a->flags, DID_CPUP))
12628 + goto out; /* success */
12630 + if (!d_unhashed(dentry)) {
12631 + err = au_sio_cpup_simple(dentry, a->btgt, sz, AuCpup_DTIME);
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);
12637 + a->h_path.dentry = hi_wh; /* do not dget here */
12640 + mutex_unlock(&a->h_inode->i_mutex);
12641 + a->h_inode = a->h_path.dentry->d_inode;
12643 + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
12644 + goto out; /* success */
12647 + au_unpin(&a->pin);
12650 + di_write_unlock(dentry);
12655 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
12658 + struct inode *inode;
12659 + struct super_block *sb;
12660 + struct file *file;
12661 + struct au_icpup_args *a;
12664 + a = kzalloc(sizeof(*a), GFP_NOFS);
12665 + if (unlikely(!a))
12668 + inode = dentry->d_inode;
12669 + IMustLock(inode);
12670 + sb = dentry->d_sb;
12671 + si_read_lock(sb, AuLock_FLUSH);
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));
12681 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
12682 + ia->ia_valid &= ~ATTR_MODE;
12684 + err = au_lock_and_icpup(dentry, ia, a);
12685 + if (unlikely(err < 0))
12687 + if (au_ftest_icpup(a->flags, DID_CPUP)) {
12688 + ia->ia_file = NULL;
12689 + ia->ia_valid &= ~ATTR_FILE;
12692 + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
12693 + if (ia->ia_valid & ATTR_SIZE) {
12696 + if (ia->ia_size < i_size_read(inode)) {
12698 + err = vmtruncate(inode, ia->ia_size);
12699 + if (unlikely(err))
12704 + if (ia->ia_valid & ATTR_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);
12710 + err = vfsub_notify_change(&a->h_path, ia);
12712 + au_cpup_attr_changeable(inode);
12715 + mutex_unlock(&a->h_inode->i_mutex);
12716 + au_unpin(&a->pin);
12717 + di_write_unlock(dentry);
12720 + fi_write_unlock(file);
12721 + ia->ia_file = file;
12722 + ia->ia_valid |= ATTR_FILE;
12724 + si_read_unlock(sb);
12730 +static int au_getattr_lock_reval(struct dentry *dentry, unsigned int sigen)
12733 + struct inode *inode;
12734 + struct dentry *parent;
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);
12745 + err = au_refresh_hinode(inode, dentry);
12746 + di_read_unlock(parent, AuLock_IR);
12748 + if (unlikely(!err))
12751 + di_downgrade_lock(dentry, AuLock_IR);
12756 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
12757 + unsigned int nlink)
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;
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;
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);
12778 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
12779 + struct dentry *dentry, struct kstat *st)
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;
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);
12803 + mnt_flags = au_mntflags(sb);
12804 + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
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);
12812 + err = au_getattr_lock_reval(dentry, sigen);
12813 + if (unlikely(err))
12817 + di_read_lock_child(dentry, AuLock_IR);
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 */
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 */
12832 + /* illegally overlapped or something */
12833 + goto out_fill; /* pretending success */
12836 + positive = !!h_dentry->d_inode;
12838 + err = vfs_getattr(h_mnt, h_dentry, st);
12842 + au_refresh_iattr(inode, st, h_dentry->d_inode->i_nlink);
12843 + goto out_fill; /* success */
12848 + generic_fillattr(inode, st);
12851 + di_read_unlock(dentry, AuLock_IR);
12852 + si_read_unlock(sb);
12856 +/* ---------------------------------------------------------------------- */
12858 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
12862 + struct super_block *sb;
12863 + struct dentry *h_dentry;
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))
12873 + err = security_inode_readlink(h_dentry);
12874 + if (unlikely(err))
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);
12882 + err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
12888 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
12892 + aufs_read_lock(dentry, AuLock_IR);
12893 + err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
12894 + aufs_read_unlock(dentry, AuLock_IR);
12899 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
12903 + mm_segment_t old_fs;
12906 + buf = __getname();
12907 + if (unlikely(!buf))
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,
12916 + aufs_read_unlock(dentry, AuLock_IR);
12920 + /* will be freed by put_link */
12921 + nd_set_link(nd, buf);
12922 + return NULL; /* success */
12927 + path_put(&nd->path);
12929 + return ERR_PTR(err);
12932 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
12933 + struct nameidata *nd, void *cookie __maybe_unused)
12935 + __putname(nd_get_link(nd));
12938 +/* ---------------------------------------------------------------------- */
12940 +static void aufs_truncate_range(struct inode *inode __maybe_unused,
12941 + loff_t start __maybe_unused,
12942 + loff_t end __maybe_unused)
12947 +/* ---------------------------------------------------------------------- */
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
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,
12969 + .permission = aufs_permission,
12970 + .setattr = aufs_setattr,
12971 + .getattr = aufs_getattr
12974 +struct inode_operations aufs_iop = {
12975 + .permission = aufs_permission,
12976 + .setattr = aufs_setattr,
12977 + .getattr = aufs_getattr,
12978 + .truncate_range = aufs_truncate_range
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
12984 +++ b/fs/aufs/i_op_add.c
12987 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
12996 + * inode operations (add entry)
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.
13007 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
13008 + struct dentry *wh_dentry, struct dentry *dentry)
13011 + aufs_bindex_t bwh;
13012 + struct path h_path;
13013 + struct inode *inode, *h_dir;
13014 + struct dentry *wh;
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,
13026 + if (unlikely(err))
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 */
13035 + if (au_ibstart(dir) == au_dbstart(dentry))
13036 + au_cpup_attr_timesizes(dir);
13037 + dir->i_version++;
13038 + return 0; /* success */
13041 + err = PTR_ERR(inode);
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);
13061 + * simple tests for the adding inode operations.
13062 + * following the checks in vfs, plus the parent-child relationship.
13064 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
13065 + struct dentry *h_parent, int isdir)
13069 + struct dentry *h_dentry;
13070 + struct inode *h_inode;
13072 + h_dentry = au_h_dptr(dentry, bindex);
13073 + h_inode = h_dentry->d_inode;
13074 + if (!dentry->d_inode) {
13076 + if (unlikely(h_inode))
13079 + /* rename(2) case */
13081 + if (unlikely(!h_inode || !h_inode->i_nlink))
13084 + h_mode = h_inode->i_mode;
13087 + if (unlikely(S_ISDIR(h_mode)))
13089 + } else if (unlikely(!S_ISDIR(h_mode))) {
13096 + /* expected parent dir is locked */
13097 + if (unlikely(h_parent != h_dentry->d_parent))
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.
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)
13115 + struct dentry *wh_dentry, *h_parent;
13116 + struct super_block *sb;
13117 + struct au_branch *br;
13119 + unsigned int udba;
13120 + aufs_bindex_t bcpup;
13122 + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
13124 + wh_dentry = ERR_PTR(err);
13125 + if (unlikely(err < 0))
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))
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))
13146 + br = au_sbr(sb, bcpup);
13148 + struct path tmp = {
13149 + .dentry = h_parent,
13150 + .mnt = br->br_mnt
13152 + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
13155 + wh_dentry = NULL;
13156 + if (bcpup != au_dbwh(dentry))
13157 + goto out; /* success */
13159 + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
13162 + if (IS_ERR(wh_dentry))
13165 + return wh_dentry;
13168 +/* ---------------------------------------------------------------------- */
13170 +enum { Mknod, Symlink, Creat };
13171 +struct simple_arg {
13176 + struct nameidata *nd;
13179 + const char *symname;
13188 +static int add_simple(struct inode *dir, struct dentry *dentry,
13189 + struct simple_arg *arg)
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
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,
13211 + err = PTR_ERR(wh_dentry);
13212 + if (IS_ERR(wh_dentry))
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) {
13221 + err = vfsub_create(h_dir, &h_path, arg->u.c.mode);
13224 + err = vfsub_symlink(h_dir, &h_path, arg->u.s.symname);
13227 + err = vfsub_mknod(h_dir, &h_path, arg->u.m.mode, arg->u.m.dev);
13234 + err = epilog(dir, bstart, wh_dentry, dentry);
13237 + if (unlikely(created && err && h_path.dentry->d_inode)) {
13239 + rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
13241 + AuIOErr("%.*s revert failure(%d, %d)\n",
13242 + AuDLNPair(dentry), err, rerr);
13245 + au_dtime_revert(&dt);
13253 + if (unlikely(err)) {
13254 + au_update_dbstart(dentry);
13257 + di_write_unlock(parent);
13258 + aufs_read_unlock(dentry, AuLock_DW);
13262 +int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
13264 + struct simple_arg arg = {
13271 + return add_simple(dir, dentry, &arg);
13274 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
13276 + struct simple_arg arg = {
13278 + .u.s.symname = symname
13280 + return add_simple(dir, dentry, &arg);
13283 +int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
13284 + struct nameidata *nd)
13286 + struct simple_arg arg = {
13293 + return add_simple(dir, dentry, &arg);
13296 +/* ---------------------------------------------------------------------- */
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;
13305 +static int au_cpup_before_link(struct dentry *src_dentry,
13306 + struct au_link_args *a)
13309 + struct dentry *h_src_dentry;
13310 + struct mutex *h_mtx;
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))
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))
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);
13331 + di_read_unlock(a->src_parent, AuLock_IR);
13335 +static int au_cpup_or_link(struct dentry *src_dentry, struct au_link_args *a)
13338 + unsigned char plink;
13339 + struct inode *h_inode, *inode;
13340 + struct dentry *h_src_dentry;
13341 + struct super_block *sb;
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);
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)) {
13365 + h_src_dentry = au_plink_lkup(inode, a->bdst);
13366 + err = PTR_ERR(h_src_dentry);
13367 + if (IS_ERR(h_src_dentry))
13370 + if (unlikely(!h_src_dentry->d_inode)) {
13371 + dput(h_src_dentry);
13372 + h_src_dentry = NULL;
13376 + if (h_src_dentry) {
13377 + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
13379 + dput(h_src_dentry);
13381 + AuIOErr("no dentry found for hi%lu on b%d\n",
13382 + h_inode->i_ino, a->bdst);
13387 + if (!err && !plink)
13388 + au_plink_append(inode, a->bdst, a->h_path.dentry);
13394 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
13395 + struct dentry *dentry)
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
13409 + inode = src_dentry->d_inode;
13410 + IMustLock(inode);
13413 + a = kzalloc(sizeof(*a), GFP_NOFS);
13414 + if (unlikely(!a))
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);
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,
13426 + err = PTR_ERR(wh_dentry);
13427 + if (IS_ERR(wh_dentry))
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);
13441 + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
13442 + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
13447 + * copyup src_dentry to the branch we process,
13448 + * and then link(2) to it.
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);
13456 + di_write_lock_parent(a->parent);
13457 + err = au_pin(&a->pin, dentry, a->bdst,
13459 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
13460 + if (unlikely(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),
13470 + if (unlikely(err))
13474 + a->h_path.dentry = wh_dentry;
13475 + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
13477 + if (unlikely(err))
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));
13489 + /* some filesystem calls d_drop() */
13491 + goto out_unpin; /* success */
13494 + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
13497 + AuIOErr("%.*s reverting failed(%d, %d)\n",
13498 + AuDLNPair(dentry), err, rerr);
13502 + au_dtime_revert(&dt);
13504 + au_unpin(&a->pin);
13508 + if (unlikely(err)) {
13509 + au_update_dbstart(dentry);
13512 + di_write_unlock(a->parent);
13513 + dput(a->src_parent);
13514 + aufs_read_and_write_unlock2(dentry, src_dentry);
13520 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
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
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,
13543 + err = PTR_ERR(wh_dentry);
13544 + if (IS_ERR(wh_dentry))
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))
13555 + /* make the dir opaque */
13557 + h_mtx = &h_path.dentry->d_inode->i_mutex;
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))
13566 + dput(opq_dentry);
13570 + err = epilog(dir, bindex, wh_dentry, dentry);
13573 + goto out_unlock; /* success */
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);
13583 + AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
13584 + AuDLNPair(dentry), err, rerr);
13590 + AuLabel(revert dir);
13591 + rerr = vfsub_rmdir(au_pinned_h_dir(&pin), &h_path);
13593 + AuIOErr("%.*s reverting dir failed(%d, %d)\n",
13594 + AuDLNPair(dentry), err, rerr);
13598 + au_dtime_revert(&dt);
13603 + if (unlikely(err)) {
13604 + au_update_dbstart(dentry);
13607 + di_write_unlock(parent);
13608 + aufs_read_unlock(dentry, AuLock_DW);
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
13615 +++ b/fs/aufs/i_op_del.c
13618 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
13627 + * inode operations (del entry)
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.
13638 + * 0: wh is unnecessary
13639 + * plus: wh is necessary
13642 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
13644 + int need_wh, err;
13645 + aufs_bindex_t bstart;
13646 + struct super_block *sb;
13648 + sb = dentry->d_sb;
13649 + bstart = au_dbstart(dentry);
13650 + if (*bcpup < 0) {
13652 + if (au_test_ro(sb, bstart, dentry->d_inode)) {
13653 + err = AuWbrCopyup(au_sbi(sb), dentry);
13655 + if (unlikely(err < 0))
13659 + AuDebugOn(bstart < *bcpup
13660 + || au_test_ro(sb, *bcpup, dentry->d_inode));
13661 + AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
13663 + if (*bcpup != bstart) {
13664 + err = au_cpup_dirs(dentry, *bcpup);
13665 + if (unlikely(err))
13669 + aufs_bindex_t old_bend, new_bend, bdiropq = -1;
13671 + old_bend = au_dbend(dentry);
13673 + bdiropq = au_dbdiropq(dentry);
13674 + au_set_dbdiropq(dentry, -1);
13676 + need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0,
13680 + au_set_dbdiropq(dentry, bdiropq);
13681 + if (unlikely(err < 0))
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);
13689 + AuDbg("need_wh %d\n", need_wh);
13697 + * simple tests for the del-entry operations.
13698 + * following the checks in vfs, plus the parent-child relationship.
13700 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
13701 + struct dentry *h_parent, int isdir)
13705 + struct dentry *h_dentry, *h_latest;
13706 + struct inode *h_inode;
13708 + h_dentry = au_h_dptr(dentry, bindex);
13709 + h_inode = h_dentry->d_inode;
13710 + if (dentry->d_inode) {
13712 + if (unlikely(!h_inode || !h_inode->i_nlink))
13715 + h_mode = h_inode->i_mode;
13718 + if (unlikely(S_ISDIR(h_mode)))
13720 + } else if (unlikely(!S_ISDIR(h_mode))) {
13725 + /* rename(2) case */
13727 + if (unlikely(h_inode))
13732 + /* expected parent dir is locked */
13733 + if (unlikely(h_parent != h_dentry->d_parent))
13738 + * rmdir a dir may break the consistency on some filesystem.
13739 + * let's try heavy test.
13742 + if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
13745 + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
13746 + au_sbr(dentry->d_sb, bindex));
13748 + if (IS_ERR(h_latest))
13750 + if (h_latest == h_dentry)
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.
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)
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;
13775 + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
13776 + wh_dentry = ERR_PTR(need_wh);
13777 + if (unlikely(need_wh < 0))
13780 + sb = dentry->d_sb;
13781 + udba = au_opt_udba(sb);
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))
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))
13798 + h_path.mnt = au_sbr_mnt(sb, bcpup);
13799 + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
13800 + wh_dentry = NULL;
13802 + goto out; /* success, no need to create whiteout */
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 */
13812 + return wh_dentry;
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
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.
13824 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
13825 + struct au_nhash *whlist, struct inode *dir)
13827 + int rmdir_later, err, dirwh;
13828 + struct dentry *h_dentry;
13829 + struct super_block *sb;
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))
13837 + /* stop monitoring */
13838 + au_hin_free(au_hi(dentry->d_inode, bindex));
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,
13847 + return rmdir_later;
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);
13862 + * final procedure for deleting a entry.
13863 + * maintain dentry and iattr.
13865 +static void epilog(struct inode *dir, struct dentry *dentry,
13866 + aufs_bindex_t bindex)
13868 + struct inode *inode;
13871 + * even if this is not a dir,
13872 + * set S_DEAD here since we need to detect the dead inode.
13874 + inode = dentry->d_inode;
13875 + if (!inode->i_nlink)
13876 + inode->i_flags |= S_DEAD;
13878 + inode->i_ctime = dir->i_ctime;
13880 + if (atomic_read(&dentry->d_count) == 1) {
13881 + au_set_h_dptr(dentry, au_dbstart(dentry), NULL);
13882 + au_update_dbstart(dentry);
13884 + if (au_ibstart(dir) == bindex)
13885 + au_cpup_attr_timesizes(dir);
13886 + dir->i_version++;
13890 + * when an error happened, remove the created whiteout and revert everything.
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)
13897 + struct path h_path = {
13898 + .dentry = wh_dentry,
13899 + .mnt = au_sbr_mnt(dir->i_sb, bwh)
13902 + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bwh), &h_path, dentry);
13904 + au_set_dbwh(dentry, bwh);
13905 + au_dtime_revert(dt);
13909 + AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
13910 + AuDLNPair(dentry), err, rerr);
13914 +/* ---------------------------------------------------------------------- */
13916 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
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;
13927 + inode = dentry->d_inode;
13928 + if (unlikely(!inode))
13929 + return -ENOENT; /* possible? */
13930 + IMustLock(inode);
13932 + aufs_read_lock(dentry, AuLock_DW);
13933 + parent = dentry->d_parent; /* dir inode is locked */
13934 + di_write_lock_parent(parent);
13936 + bstart = au_dbstart(dentry);
13937 + bwh = au_dbwh(dentry);
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))
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);
13951 + /* dir inode is locked */
13952 + h_dir = wh_dentry->d_parent->d_inode;
13953 + IMustLock(h_dir);
13958 + drop_nlink(inode);
13959 + epilog(dir, dentry, bindex);
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;
13966 + /* todo: this timestamp may be reverted later */
13967 + inode->i_ctime = h_dir->i_ctime;
13968 + goto out_unlock; /* success */
13975 + rerr = do_revert(err, dir, bwh, wh_dentry, dentry, &dt);
13983 + dput(h_path.dentry);
13985 + di_write_unlock(parent);
13986 + aufs_read_unlock(dentry, AuLock_DW);
13990 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
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;
14001 + inode = dentry->d_inode;
14002 + err = -ENOENT; /* possible? */
14003 + if (unlikely(!inode))
14005 + IMustLock(inode);
14008 + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
14009 + if (unlikely(!args))
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))
14019 + bstart = au_dbstart(dentry);
14020 + bwh = au_dbwh(dentry);
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))
14027 + h_dentry = au_h_dptr(dentry, bstart);
14030 + if (bindex == bstart) {
14031 + err = renwh_and_rmdir(dentry, bstart, args->whlist, dir);
14033 + rmdir_later = err;
14037 + /* stop monitoring */
14038 + au_hin_free(au_hi(inode, bstart));
14040 + /* dir inode is locked */
14041 + IMustLock(wh_dentry->d_parent->d_inode);
14046 + clear_nlink(inode);
14047 + au_set_dbdiropq(dentry, -1);
14048 + epilog(dir, dentry, bindex);
14050 + if (rmdir_later) {
14051 + au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
14055 + goto out_unlock; /* success */
14063 + rerr = do_revert(err, dir, bwh, wh_dentry, dentry, &dt);
14073 + di_write_unlock(parent);
14074 + aufs_read_unlock(dentry, AuLock_DW);
14076 + au_whtmp_rmdir_free(args);
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
14084 +++ b/fs/aufs/i_op_ren.c
14087 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
14096 + * inode operation (rename entry)
14097 + * todo: this is crazy monster
14102 +enum { AuSRC, AuDST, AuSrcDst };
14103 +enum { AuPARENT, AuCHILD, AuParentChild };
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; }
14117 +struct au_ren_args {
14119 + struct dentry *dentry, *h_dentry, *parent, *h_parent,
14121 + struct inode *dir, *inode;
14122 + struct au_hinode *hdir;
14123 + struct au_dtime dt[AuParentChild];
14124 + aufs_bindex_t bstart;
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
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
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;
14158 + unsigned int flags;
14160 + struct au_whtmp_rmdir *thargs;
14161 + struct dentry *h_dst;
14164 +/* ---------------------------------------------------------------------- */
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
14174 +#define RevertFailure(fmt, args...) do { \
14175 + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
14176 + ##args, err, rerr); \
14180 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
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);
14188 + RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
14192 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
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));
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); */
14211 + RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
14214 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
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);
14223 + RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
14227 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
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));
14238 + if (a->h_path.dentry->d_inode) {
14239 + d_drop(a->h_path.dentry);
14240 + dput(a->h_path.dentry);
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);
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));
14251 + RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
14254 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
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);
14261 + RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
14264 +static void au_ren_rev_drop(struct au_ren_args *a)
14266 + struct dentry *d, *h_d;
14268 + aufs_bindex_t bend, bindex;
14270 + for (i = 0; i < AuSrcDst; i++) {
14271 + d = a->sd[i].dentry;
14273 + bend = au_dbend(d);
14274 + for (bindex = au_dbstart(d); bindex <= bend; bindex++) {
14275 + h_d = au_h_dptr(d, bindex);
14281 + au_update_dbstart(a->dst_dentry);
14283 + d_drop(a->h_dst);
14285 +#undef RevertFailure
14287 +/* ---------------------------------------------------------------------- */
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.
14294 +static int au_ren_or_cpup(struct au_ren_args *a)
14297 + struct dentry *d;
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);
14309 + struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
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);
14321 + mutex_unlock(h_mtx);
14327 +/* cf. aufs_rmdir() */
14328 +static int au_ren_del_whtmp(struct au_ren_args *a)
14331 + struct inode *dir;
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);
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);
14347 + a->thargs = NULL;
14353 +/* make it 'opaque' dir. */
14354 +static int au_ren_diropq(struct au_ren_args *a)
14357 + struct dentry *diropq;
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);
14371 +static int do_rename(struct au_ren_args *a)
14374 + struct dentry *d, *h_d;
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) {
14380 + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
14381 + if (unlikely(!a->thargs))
14383 + a->h_dst = dget(h_d);
14386 + /* create whiteout for src_dentry */
14387 + if (au_ftest_ren(a->flags, WHSRC)) {
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))
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,
14399 + err = PTR_ERR(h_d);
14402 + if (!h_d->d_inode)
14405 + a->dst_wh_dentry = h_d;
14408 + /* rename dentry to tmpwh */
14410 + err = au_whtmp_ren(a->dst_h_dentry, a->br);
14411 + if (unlikely(err))
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))
14419 + a->dst_h_dentry = au_h_dptr(d, a->btgt);
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;
14426 + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
14427 + err = au_sio_cpup_simple(a->src_dentry, a->btgt, -1,
14429 + mutex_unlock(h_mtx);
14430 + if (unlikely(err))
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 */
14448 + /* make dir opaque */
14449 + if (au_ftest_ren(a->flags, DIROPQ)) {
14450 + err = au_ren_diropq(a);
14451 + if (unlikely(err))
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;
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,
14466 + if (unlikely(err))
14470 + /* remove whtmp */
14472 + au_ren_del_whtmp(a); /* ignore this error */
14475 + goto out_success;
14478 + if (au_ftest_ren(a->flags, DIROPQ))
14479 + au_ren_rev_diropq(err, a);
14481 + if (!au_ftest_ren(a->flags, CPUP))
14482 + au_ren_rev_rename(err, a);
14484 + au_ren_rev_cpup(err, a);
14487 + au_ren_rev_whtmp(err, a);
14489 + dput(a->dst_wh_dentry);
14490 + a->dst_wh_dentry = NULL;
14492 + if (a->src_wh_dentry)
14493 + au_ren_rev_whsrc(err, a);
14494 + au_ren_rev_drop(a);
14496 + dput(a->src_wh_dentry);
14497 + dput(a->dst_wh_dentry);
14501 + au_whtmp_rmdir_free(a->thargs);
14502 + a->thargs = NULL;
14508 +/* ---------------------------------------------------------------------- */
14511 + * test if @dentry dir can be rename destination or not.
14512 + * success means, it is a logically empty dir.
14514 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
14516 + return au_test_empty(dentry, whlist);
14520 + * test if @dentry dir can be rename source or not.
14521 + * if it can, return 0 and @children is filled.
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.
14527 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
14530 + aufs_bindex_t bstart;
14532 + bstart = au_dbstart(dentry);
14533 + if (bstart != btgt) {
14534 + struct au_nhash *whlist;
14536 + whlist = au_nhash_alloc(dentry->d_sb, /*bend*/0, GFP_NOFS);
14537 + err = PTR_ERR(whlist);
14538 + if (IS_ERR(whlist))
14540 + err = au_test_empty(dentry, whlist);
14541 + au_nhash_wh_free(whlist, /*bend*/0);
14545 + if (bstart == au_dbtaildir(dentry))
14546 + return 0; /* success */
14548 + err = au_test_empty_lower(dentry);
14551 + if (err == -ENOTEMPTY) {
14552 + AuWarn1("renaming dir who has child(ren) on multiple branches,"
14553 + " is not supported\n");
14559 +/* side effect: sets whlist and h_dentry */
14560 +static int au_ren_may_dir(struct au_ren_args *a)
14563 + struct dentry *d;
14566 + d = a->dst_dentry;
14567 + a->whlist = au_nhash_alloc(d->d_sb, /*bend*/0, GFP_NOFS);
14568 + if (unlikely(!a->whlist))
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);
14577 + a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
14578 + if (unlikely(err))
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;
14594 +/* ---------------------------------------------------------------------- */
14597 + * simple tests for rename.
14598 + * following the checks in vfs, plus the parent-child relationship.
14600 +static int au_may_ren(struct au_ren_args *a)
14603 + struct inode *h_inode;
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))
14611 + if (unlikely(a->src_h_dentry == a->h_trap))
14616 + if (a->dst_bstart != a->btgt)
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))
14625 + err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
14628 + if (unlikely(!h_inode || !h_inode->i_nlink))
14630 + err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
14632 + if (unlikely(err))
14634 + err = -ENOTEMPTY;
14635 + if (unlikely(a->dst_h_dentry == a->h_trap))
14641 + if (unlikely(err == -ENOENT || err == -EEXIST))
14646 +/* ---------------------------------------------------------------------- */
14651 + * - src_dir and dir by lock_rename()
14652 + * - inode if exitsts
14655 + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
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.
14671 +static void au_ren_unlock(struct au_ren_args *a)
14673 + struct super_block *sb;
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);
14682 +static int au_ren_lock(struct au_ren_args *a)
14685 + unsigned int udba;
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,
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,
14704 + err = mnt_want_write(a->br->br_mnt);
14705 + if (unlikely(err))
14707 + au_fset_ren(a->flags, MNT_WRITE);
14708 + goto out; /* success */
14711 + err = au_busy_or_stale();
14714 + au_ren_unlock(a);
14719 +/* ---------------------------------------------------------------------- */
14721 +static void au_ren_refresh_dir(struct au_ren_args *a)
14723 + struct inode *dir;
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);
14736 + if (au_ibstart(dir) == a->btgt)
14737 + au_cpup_attr_timesizes(dir);
14739 + if (au_ftest_ren(a->flags, ISSAMEDIR))
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);
14750 +static void au_ren_refresh(struct au_ren_args *a)
14752 + aufs_bindex_t bend, bindex;
14753 + struct dentry *d, *h_d;
14754 + struct inode *i, *h_i;
14755 + struct super_block *sb;
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);
14763 + au_set_h_dptr(d, bindex, NULL);
14765 + au_set_dbend(d, a->btgt);
14768 + i = a->src_inode;
14769 + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
14770 + return; /* success */
14772 + bend = au_ibend(i);
14773 + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
14774 + h_i = au_h_iptr(i, bindex);
14776 + au_xino_write0(sb, bindex, h_i->i_ino, 0);
14777 + /* ignore this error */
14778 + au_set_h_iptr(i, bindex, NULL, 0);
14781 + au_set_ibend(i, a->btgt);
14784 +/* ---------------------------------------------------------------------- */
14786 +/* mainly for link(2) and rename(2) */
14787 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
14789 + aufs_bindex_t bdiropq, bwh;
14790 + struct dentry *parent;
14791 + struct au_branch *br;
14793 + parent = dentry->d_parent;
14794 + IMustLock(parent->d_inode); /* dir is locked */
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))
14804 + AuDbg("btgt %d\n", btgt);
14808 +/* sets src_bstart, dst_bstart and btgt */
14809 +static int au_ren_wbr(struct au_ren_args *a)
14812 + struct au_wr_dir_args wr_dir_args = {
14813 + /* .force_btgt = -1, */
14814 + .flags = AuWrDir_ADD_ENTRY
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);
14831 +static void au_ren_dt(struct au_ren_args *a)
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);
14840 + au_fclr_ren(a->flags, DT_DSTDIR);
14841 + if (!au_ftest_ren(a->flags, ISDIR))
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);
14853 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
14855 + struct dentry *h_d;
14856 + struct mutex *h_mtx;
14858 + au_dtime_revert(a->src_dt + AuPARENT);
14859 + if (!au_ftest_ren(a->flags, ISSAMEDIR))
14860 + au_dtime_revert(a->dst_dt + AuPARENT);
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);
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);
14879 +/* ---------------------------------------------------------------------- */
14881 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
14882 + struct inode *_dst_dir, struct dentry *_dst_dentry)
14885 + /* reduce stack space */
14886 + struct au_ren_args *a;
14888 + IMustLock(_src_dir);
14889 + IMustLock(_dst_dir);
14892 + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
14893 + a = kzalloc(sizeof(*a), GFP_NOFS);
14894 + if (unlikely(!a))
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);
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)))
14915 + aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
14916 + AuLock_DIR | AuLock_FLUSH);
14918 + aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
14921 + au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
14922 + di_write_lock_parent(a->dst_parent);
14924 + /* which branch we process */
14925 + err = au_ren_wbr(a);
14926 + if (unlikely(err < 0))
14928 + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
14929 + a->h_path.mnt = a->br->br_mnt;
14931 + /* are they available to be renamed */
14932 + err = au_ren_may_dir(a);
14933 + if (unlikely(err))
14936 + /* prepare the writable parent dir on the same branch */
14937 + if (a->dst_bstart == a->btgt) {
14938 + au_fset_ren(a->flags, WHDST);
14940 + err = au_cpup_dirs(a->dst_dentry, a->btgt);
14941 + if (unlikely(err))
14942 + goto out_children;
14945 + if (a->src_dir != a->dst_dir) {
14947 + * this temporary unlock is safe,
14948 + * because both dir->i_mutex are locked.
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),
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);
14959 + err = au_wr_dir_need_wh(a->src_dentry,
14960 + au_ftest_ren(a->flags, ISDIR),
14962 + if (unlikely(err < 0))
14963 + goto out_children;
14965 + au_fset_ren(a->flags, WHSRC);
14967 + /* lock them all */
14968 + err = au_ren_lock(a);
14969 + if (unlikely(err))
14970 + goto out_children;
14972 + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE)) {
14973 + err = au_may_ren(a);
14974 + if (unlikely(err))
14978 + /* store timestamps to be revertible */
14982 + err = do_rename(a);
14983 + if (unlikely(err))
14986 + /* update dir attributes */
14987 + au_ren_refresh_dir(a);
14989 + /* dput/iput all lower dentries */
14990 + au_ren_refresh(a);
14992 + goto out_hdir; /* success */
14995 + au_ren_rev_dt(err, a);
14997 + au_ren_unlock(a);
15000 + au_nhash_wh_free(a->whlist, /*bend*/0);
15002 + if (unlikely(err && au_ftest_ren(a->flags, ISDIR))) {
15003 + au_update_dbstart(a->dst_dentry);
15004 + d_drop(a->dst_dentry);
15007 + d_move(a->src_dentry, a->dst_dentry);
15009 + && (a->dst_inode->i_nlink <= 1
15010 + || au_ftest_ren(a->flags, ISDIR)))
15011 + a->dst_inode->i_flags |= S_DEAD;
15013 + if (au_ftest_ren(a->flags, ISSAMEDIR))
15014 + di_write_unlock(a->dst_parent);
15016 + di_write_unlock2(a->src_parent, a->dst_parent);
15017 + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
15019 + iput(a->dst_inode);
15021 + au_whtmp_rmdir_free(a->thargs);
15026 diff --git a/fs/aufs/iinfo.c b/fs/aufs/iinfo.c
15027 new file mode 100644
15028 index 0000000..b1cff91
15030 +++ b/fs/aufs/iinfo.c
15033 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
15042 + * inode private data
15047 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15049 + struct inode *h_inode;
15051 + h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15052 + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15056 +/* todo: hard/soft set? */
15057 +void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
15059 + struct au_iinfo *iinfo = au_ii(inode);
15060 + struct inode *h_inode;
15062 + iinfo->ii_bstart = bindex;
15063 + h_inode = iinfo->ii_hinode[bindex + 0].hi_inode;
15065 + au_cpup_igen(inode, h_inode);
15068 +void au_hiput(struct au_hinode *hinode)
15070 + au_hin_free(hinode);
15071 + dput(hinode->hi_whdentry);
15072 + iput(hinode->hi_inode);
15075 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15077 + unsigned int flags;
15078 + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
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);
15088 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15089 + struct inode *h_inode, unsigned int flags)
15091 + struct au_hinode *hinode;
15092 + struct inode *hi;
15093 + struct au_iinfo *iinfo = au_ii(inode);
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);
15101 + au_hiput(hinode);
15102 + hinode->hi_inode = h_inode;
15105 + struct super_block *sb = inode->i_sb;
15106 + struct au_branch *br;
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,
15115 + if (unlikely(err))
15116 + AuIOErr1("failed au_xino_write() %d\n", err);
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);
15128 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15129 + struct dentry *h_wh)
15131 + struct au_hinode *hinode;
15133 + hinode = au_ii(inode)->ii_hinode + bindex;
15134 + AuDebugOn(hinode->hi_whdentry);
15135 + hinode->hi_whdentry = h_wh;
15138 +void au_update_iigen(struct inode *inode)
15140 + atomic_set(&au_ii(inode)->ii_generation, au_sigen(inode->i_sb));
15141 + /* smp_mb(); */ /* atomic_set */
15144 +/* it may be called at remount time, too */
15145 +void au_update_brange(struct inode *inode, int do_put_zero)
15147 + struct au_iinfo *iinfo;
15149 + iinfo = au_ii(inode);
15150 + if (!iinfo || iinfo->ii_bstart < 0)
15153 + if (do_put_zero) {
15154 + aufs_bindex_t bindex;
15156 + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15158 + struct inode *h_i;
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);
15166 + iinfo->ii_bstart = -1;
15167 + while (++iinfo->ii_bstart <= iinfo->ii_bend)
15168 + if (iinfo->ii_hinode[0 + iinfo->ii_bstart].hi_inode)
15170 + if (iinfo->ii_bstart > iinfo->ii_bend) {
15171 + iinfo->ii_bstart = -1;
15172 + iinfo->ii_bend = -1;
15176 + iinfo->ii_bend++;
15177 + while (0 <= --iinfo->ii_bend)
15178 + if (iinfo->ii_hinode[0 + iinfo->ii_bend].hi_inode)
15180 + AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend || iinfo->ii_bend < 0);
15183 +/* ---------------------------------------------------------------------- */
15185 +int au_iinfo_init(struct inode *inode)
15187 + struct au_iinfo *iinfo;
15188 + struct super_block *sb;
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))
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;
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;
15212 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15215 + struct au_hinode *hip;
15218 + sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15220 + sz = sizeof(*hip);
15221 + hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15223 + iinfo->ii_hinode = hip;
15230 +static int au_iinfo_write0(struct super_block *sb, struct au_hinode *hinode,
15234 + aufs_bindex_t bindex;
15235 + unsigned char locked;
15238 + locked = !!si_noflush_read_trylock(sb);
15239 + bindex = au_br_index(sb, hinode->hi_id);
15241 + err = au_xino_write0(sb, bindex, hinode->hi_inode->i_ino, ino);
15242 + /* error action? */
15244 + si_read_unlock(sb);
15248 +void au_iinfo_fin(struct inode *inode)
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;
15258 + int err = au_xigen_inc(inode);
15259 + if (unlikely(err))
15260 + AuWarn1("failed resetting i_generation, %d\n", err);
15263 + iinfo = au_ii(inode);
15264 + /* bad_inode case */
15268 + if (iinfo->ii_vdir)
15269 + au_vdir_free(iinfo->ii_vdir);
15271 + if (iinfo->ii_bstart >= 0) {
15272 + sb = inode->i_sb;
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 */
15291 + kfree(iinfo->ii_hinode);
15292 + au_rwsem_destroy(&iinfo->ii_rwsem);
15294 diff --git a/fs/aufs/inode.c b/fs/aufs/inode.c
15295 new file mode 100644
15296 index 0000000..c4a962b
15298 +++ b/fs/aufs/inode.c
15301 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
15310 + * inode functions
15315 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15317 + au_cpup_attr_all(inode, /*force*/0);
15318 + au_update_iigen(inode);
15320 + inode->i_version++;
15323 +int au_refresh_hinode_self(struct inode *inode, int do_attr)
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;
15334 + sb = inode->i_sb;
15335 + iinfo = au_ii(inode);
15336 + err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15337 + if (unlikely(err))
15340 + p = iinfo->ii_hinode + iinfo->ii_bstart;
15341 + first = p->hi_inode;
15343 + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15345 + if (!p->hi_inode)
15348 + new_bindex = au_br_index(sb, p->hi_id);
15349 + if (new_bindex == bindex)
15352 + if (new_bindex < 0) {
15355 + p->hi_inode = NULL;
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;
15368 + if (tmp.hi_inode) {
15373 + au_update_brange(inode, /*do_put_zero*/0);
15375 + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
15381 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
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;
15391 + err = au_refresh_hinode_self(inode, /*do_attr*/0);
15392 + if (unlikely(err))
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;
15406 + h_d = au_h_dptr(dentry, bindex);
15407 + if (!h_d || !h_d->d_inode)
15410 + if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
15411 + h_i = au_h_iptr(inode, bindex);
15413 + if (h_i == h_d->d_inode)
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);
15426 + au_update_brange(inode, /*do_put_zero*/0);
15428 + if (unlikely(err))
15431 + au_refresh_hinode_attr(inode, update && isdir);
15437 +static int set_inode(struct inode *inode, struct dentry *dentry)
15440 + unsigned int flags;
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;
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) {
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;
15462 + btail = au_dbtaildir(dentry);
15463 + inode->i_op = &aufs_dir_iop;
15464 + inode->i_fop = &aufs_dir_fop;
15467 + btail = au_dbtail(dentry);
15468 + inode->i_op = &aufs_symlink_iop;
15474 + btail = au_dbtail(dentry);
15475 + inode->i_op = &aufs_iop;
15476 + init_special_inode(inode, mode, h_inode->i_rdev);
15479 + AuIOErr("Unknown file type 0%o\n", mode);
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);
15491 + au_set_h_iptr(inode, bindex,
15492 + au_igrab(h_dentry->d_inode), flags);
15494 + au_cpup_attr_all(inode, /*force*/1);
15500 +/* successful returns with iinfo write_locked */
15501 +static int reval_inode(struct inode *inode, struct dentry *dentry, int *matched)
15504 + aufs_bindex_t bindex, bend;
15505 + struct inode *h_inode, *h_dinode;
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.
15515 + if (unlikely(inode->i_ino == parent_ino(dentry)))
15518 + ii_write_lock_new_child(inode);
15519 + if (unlikely(IS_DEADDIR(inode)))
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) {
15530 + if (au_iigen(inode) != au_digen(dentry))
15531 + err = au_refresh_hinode(inode, dentry);
15537 + if (unlikely(err))
15538 + ii_write_unlock(inode);
15543 +/* successful returns with iinfo write_locked */
15544 +/* todo: return with unlocked? */
15545 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
15547 + struct inode *inode;
15548 + struct dentry *h_dentry;
15549 + struct super_block *sb;
15550 + ino_t h_ino, ino;
15552 + aufs_bindex_t bstart;
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))
15564 + ino = au_xino_new_ino(sb);
15565 + if (unlikely(!ino)) {
15566 + inode = ERR_PTR(-EIO);
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))
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);
15583 + goto out; /* success */
15585 + iget_failed(inode);
15586 + ii_write_unlock(inode);
15588 + } else if (!must_new) {
15589 + err = reval_inode(inode, dentry, &match);
15591 + goto out; /* success */
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);
15602 + err = au_xino_write0(sb, bstart, h_ino, 0);
15610 + inode = ERR_PTR(err);
15615 +/* ---------------------------------------------------------------------- */
15617 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15618 + struct inode *inode)
15622 + err = au_br_rdonly(au_sbr(sb, bindex));
15624 + /* pseudo-link after flushed may happen out of bounds */
15627 + && au_ibstart(inode) <= bindex
15628 + && bindex <= au_ibend(inode)) {
15630 + * permission check is unnecessary since vfsub routine
15631 + * will be called later
15633 + struct inode *hi = au_h_iptr(inode, bindex);
15635 + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
15641 +int au_test_h_perm(struct inode *h_inode, int mask)
15643 + if (!current_fsuid())
15645 + return inode_permission(h_inode, mask);
15648 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
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);
15656 diff --git a/fs/aufs/inode.h b/fs/aufs/inode.h
15657 new file mode 100644
15658 index 0000000..ffda951
15660 +++ b/fs/aufs/inode.h
15663 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
15672 + * inode operations
15675 +#ifndef __AUFS_INODE_H__
15676 +#define __AUFS_INODE_H__
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"
15686 +struct au_hinotify {
15687 +#ifdef CONFIG_AUFS_HINOTIFY
15688 + struct inotify_watch hin_watch;
15689 + struct inode *hin_aufs_inode; /* no get/put */
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;
15700 + /* reference to the copied-up whiteout with get/put */
15701 + struct dentry *hi_whdentry;
15706 + atomic_t ii_generation;
15707 + struct super_block *ii_hsb1; /* no get/put */
15709 + struct rw_semaphore ii_rwsem;
15710 + aufs_bindex_t ii_bstart, ii_bend;
15712 + struct au_hinode *ii_hinode;
15713 + struct au_vdir *ii_vdir;
15716 +struct au_icntnr {
15717 + struct au_iinfo iinfo;
15718 + struct inode vfs_inode;
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; }
15730 + struct dentry *dentry;
15731 + unsigned int udba;
15732 + unsigned char lsc_di, lsc_hi, flags;
15733 + aufs_bindex_t bindex;
15736 + struct dentry *parent;
15737 + struct au_hinode *hdir;
15738 + struct vfsmount *h_mnt;
15741 +/* ---------------------------------------------------------------------- */
15743 +static inline struct au_iinfo *au_ii(struct inode *inode)
15745 + struct au_iinfo *iinfo;
15747 + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15748 + if (iinfo->ii_hinode)
15750 + return NULL; /* debugging bad_inode case */
15753 +/* ---------------------------------------------------------------------- */
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);
15765 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
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; }
15774 +struct au_wr_dir_args {
15775 + aufs_bindex_t force_btgt;
15776 + unsigned char flags;
15778 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
15779 + struct au_wr_dir_args *args);
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);
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);
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);
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);
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);
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; }
15828 +#ifndef CONFIG_AUFS_HINOTIFY
15829 +#undef AuHi_HINOTIFY
15830 +#define AuHi_HINOTIFY 0
15833 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15834 + struct inode *h_inode, unsigned int flags);
15836 +void au_update_iigen(struct inode *inode);
15837 +void au_update_brange(struct inode *inode, int do_put_zero);
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);
15844 +void au_plink_block_maintain(struct super_block *sb);
15845 +#ifdef CONFIG_AUFS_DEBUG
15846 +void au_plink_list(struct super_block *sb);
15848 +static inline void au_plink_list(struct super_block *sb)
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);
15860 +/* ---------------------------------------------------------------------- */
15862 +/* lock subclass for iinfo */
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
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,
15882 +#define AuReadLockFunc(name, lsc) \
15883 +static inline void ii_read_lock_##name(struct inode *i) \
15885 + down_read_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15888 +#define AuWriteLockFunc(name, lsc) \
15889 +static inline void ii_write_lock_##name(struct inode *i) \
15891 + down_write_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15894 +#define AuRWLockFuncs(name, lsc) \
15895 + AuReadLockFunc(name, lsc) \
15896 + AuWriteLockFunc(name, lsc)
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);
15906 +#undef AuReadLockFunc
15907 +#undef AuWriteLockFunc
15908 +#undef AuRWLockFuncs
15911 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
15913 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
15915 +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
15917 +/* ---------------------------------------------------------------------- */
15919 +static inline unsigned int au_iigen(struct inode *inode)
15921 + return atomic_read(&au_ii(inode)->ii_generation);
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)
15928 + struct au_iinfo *iinfo;
15930 + iinfo = au_ii(inode);
15931 + return !(iinfo->ii_hsb1 == h_inode->i_sb
15932 + && iinfo->ii_higen == h_inode->i_generation);
15935 +static inline struct inode *au_igrab(struct inode *inode)
15938 + AuDebugOn(!atomic_read(&inode->i_count));
15939 + atomic_inc(&inode->i_count);
15944 +/* ---------------------------------------------------------------------- */
15946 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
15947 + aufs_bindex_t bindex)
15949 + return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
15952 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
15954 + return au_ii(inode)->ii_bstart;
15957 +static inline aufs_bindex_t au_ibend(struct inode *inode)
15959 + return au_ii(inode)->ii_bend;
15962 +static inline struct au_vdir *au_ivdir(struct inode *inode)
15964 + return au_ii(inode)->ii_vdir;
15967 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
15969 + return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
15972 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
15974 + au_ii(inode)->ii_bend = bindex;
15977 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
15979 + au_ii(inode)->ii_vdir = vdir;
15982 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
15984 + return au_ii(inode)->ii_hinode + bindex;
15987 +/* ---------------------------------------------------------------------- */
15989 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
15992 + return pin->parent;
15996 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
15998 + if (pin && pin->hdir)
15999 + return pin->hdir->hi_inode;
16003 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
16006 + return pin->hdir;
16010 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
16013 + pin->dentry = dentry;
16016 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
16017 + unsigned char lflag)
16020 + /* dirty macros require brackets */
16022 + au_fset_pin(pin->flags, DI_LOCKED);
16024 + au_fclr_pin(pin->flags, DI_LOCKED);
16029 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
16032 + dput(pin->parent);
16033 + pin->parent = dget(parent);
16037 +/* ---------------------------------------------------------------------- */
16039 +#ifdef CONFIG_AUFS_HINOTIFY
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);
16047 +int __init au_hinotify_init(void);
16048 +void au_hinotify_fin(void);
16051 +void au_hin_init(struct au_hinode *hinode, struct au_hinotify *val)
16053 + hinode->hi_notify = val;
16056 +static inline void au_iigen_dec(struct inode *inode)
16058 + atomic_dec(&au_ii(inode)->ii_generation);
16063 +int au_hin_alloc(struct au_hinode *hinode __maybe_unused,
16064 + struct inode *inode __maybe_unused,
16065 + struct inode *h_inode __maybe_unused)
16067 + return -EOPNOTSUPP;
16070 +static inline void au_hin_free(struct au_hinode *hinode __maybe_unused)
16075 +static inline void au_hin_ctl(struct au_hinode *hinode __maybe_unused,
16076 + int do_set __maybe_unused)
16081 +static inline void au_reset_hinotify(struct inode *inode __maybe_unused,
16082 + unsigned int flags __maybe_unused)
16087 +static inline int au_hinotify_init(void)
16092 +#define au_hinotify_fin() do {} while (0)
16095 +void au_hin_init(struct au_hinode *hinode __maybe_unused,
16096 + struct au_hinotify *val __maybe_unused)
16100 +#endif /* CONFIG_AUFS_HINOTIFY */
16102 +static inline void au_hin_suspend(struct au_hinode *hdir)
16104 + au_hin_ctl(hdir, /*do_set*/0);
16107 +static inline void au_hin_resume(struct au_hinode *hdir)
16109 + au_hin_ctl(hdir, /*do_set*/1);
16112 +static inline void au_hin_imtx_lock(struct au_hinode *hdir)
16114 + mutex_lock(&hdir->hi_inode->i_mutex);
16115 + au_hin_suspend(hdir);
16118 +static inline void au_hin_imtx_lock_nested(struct au_hinode *hdir,
16119 + unsigned int sc __maybe_unused)
16121 + mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
16122 + au_hin_suspend(hdir);
16125 +static inline void au_hin_imtx_unlock(struct au_hinode *hdir)
16127 + au_hin_resume(hdir);
16128 + mutex_unlock(&hdir->hi_inode->i_mutex);
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
16137 +++ b/fs/aufs/ioctl.c
16140 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
16148 +#include <linux/uaccess.h>
16151 +long aufs_ioctl_dir(struct file *file, unsigned int cmd,
16152 + unsigned long arg __maybe_unused)
16155 + struct super_block *sb;
16156 + struct au_sbinfo *sbinfo;
16159 + if (!capable(CAP_SYS_ADMIN))
16163 + sb = file->f_dentry->d_sb;
16164 + sbinfo = au_sbi(sb);
16166 + case AUFS_CTL_PLINK_MAINT:
16168 + * pseudo-link maintenance mode,
16169 + * cleared by aufs_release_dir()
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;
16177 + si_write_unlock(sb);
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);
16193 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
16194 new file mode 100644
16195 index 0000000..84a940f
16197 +++ b/fs/aufs/loop.c
16200 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
16209 + * support for loopback block device as a branch
16212 +#include <linux/loop.h>
16216 + * test if two lower dentries have overlapping branches.
16218 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_d1,
16219 + struct dentry *h_d2)
16221 + struct inode *h_inode;
16222 + struct loop_device *l;
16224 + h_inode = h_d1->d_inode;
16225 + if (MAJOR(h_inode->i_sb->s_dev) != LOOP_MAJOR)
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))
16233 + return !!au_test_subdir(h_d1, h_d2);
16236 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
16237 +int au_test_loopback_kthread(void)
16239 + const char c = current->comm[4];
16241 + return current->mm == NULL
16242 + && '0' <= c && c <= '9'
16243 + && strncmp(current->comm, "loop", 4) == 0;
16245 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
16246 new file mode 100644
16247 index 0000000..1c0ee40
16249 +++ b/fs/aufs/loop.h
16252 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
16261 + * support for loopback mount as a branch
16264 +#ifndef __AUFS_LOOP_H__
16265 +#define __AUFS_LOOP_H__
16269 +#include <linux/fs.h>
16271 +#ifdef CONFIG_AUFS_BDEV_LOOP
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);
16278 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_d1,
16279 + struct dentry *h_d2)
16284 +static inline int au_test_loopback_kthread(void)
16288 +#endif /* BLK_DEV_LOOP */
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
16296 +++ b/fs/aufs/magic.mk
16299 +# defined in ${srctree}/fs/fuse/inode.c
16301 +ifdef CONFIG_FUSE_FS
16302 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
16305 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
16307 +ifdef CONFIG_OCFS2_FS
16308 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
16311 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
16313 +ifdef CONFIG_OCFS2_FS_O2CB
16314 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
16317 +# defined in ${srctree}/fs/ramfs/inode.c
16319 +ccflags-y += -DRAMFS_MAGIC=0x858458f6
16321 +# defined in ${srctree}/fs/cifs/cifsfs.c
16323 +ifdef CONFIG_CIFS_FS
16324 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
16327 +# defined in ${srctree}/fs/xfs/xfs_sb.h
16329 +ifdef CONFIG_XFS_FS
16330 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
16333 +# defined in ${srctree}/fs/configfs/mount.c
16335 +ifdef CONFIG_CONFIGFS_FS
16336 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
16339 +# defined in ${srctree}/fs/9p/v9fs.h
16341 +ifdef CONFIG_9P_FS
16342 +ccflags-y += -DV9FS_MAGIC=0x01021997
16345 +# defined in ${srctree}/fs/ubifs/ubifs.h
16347 +ifdef CONFIG_UBIFS_FS
16348 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
16351 +# defined in ${srctree}/fs/debugfs/inode.c
16353 +ifdef CONFIG_DEBUG_FS
16354 +ccflags-y += -DDEBUGFS_MAGIC=0x64626720
16356 diff --git a/fs/aufs/module.c b/fs/aufs/module.c
16357 new file mode 100644
16358 index 0000000..c2219d3
16360 +++ b/fs/aufs/module.c
16363 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
16372 + * module global variables and operations
16375 +#include <linux/module.h>
16376 +#include <linux/seq_file.h>
16379 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
16381 + if (new_sz <= nused)
16384 + p = krealloc(p, new_sz, gfp);
16386 + memset(p + nused, 0, new_sz - nused);
16390 +/* ---------------------------------------------------------------------- */
16395 +struct kmem_cache *au_cachep[AuCache_Last];
16396 +static int __init au_cache_init(void)
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])
16413 +static void au_cache_fin(void)
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;
16423 +/* ---------------------------------------------------------------------- */
16425 +int au_dir_roflags;
16428 + * functions for module interface.
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);
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);
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);
16447 +/* ---------------------------------------------------------------------- */
16449 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
16451 +int au_seq_path(struct seq_file *seq, struct path *path)
16453 + return seq_path(seq, path, au_esc_chars);
16456 +/* ---------------------------------------------------------------------- */
16458 +static int __init aufs_init(void)
16463 + p = au_esc_chars;
16464 + for (i = 1; i <= ' '; i++)
16470 + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
16472 + sysaufs_brs_init();
16476 + if (unlikely(aufs_nwkq <= 0))
16479 + err = sysaufs_init();
16480 + if (unlikely(err))
16482 + err = au_wkq_init();
16483 + if (unlikely(err))
16484 + goto out_sysaufs;
16485 + err = au_hinotify_init();
16486 + if (unlikely(err))
16488 + err = au_sysrq_init();
16489 + if (unlikely(err))
16491 + err = au_cache_init();
16492 + if (unlikely(err))
16494 + err = register_filesystem(&aufs_fs_type);
16495 + if (unlikely(err))
16497 + pr_info(AUFS_NAME " " AUFS_VERSION "\n");
16498 + goto out; /* success */
16505 + au_hinotify_fin();
16514 +static void __exit aufs_exit(void)
16516 + unregister_filesystem(&aufs_fs_type);
16519 + au_hinotify_fin();
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
16530 +++ b/fs/aufs/module.h
16533 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
16542 + * module initialization and module-global
16545 +#ifndef __AUFS_MODULE_H__
16546 +#define __AUFS_MODULE_H__
16550 +#include <linux/slab.h>
16552 +/* module parameters */
16553 +extern short aufs_nwkq;
16554 +extern int sysaufs_brs;
16556 +/* ---------------------------------------------------------------------- */
16558 +extern int au_dir_roflags;
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);
16563 +/* ---------------------------------------------------------------------- */
16572 +#ifdef CONFIG_AUFS_HINOTIFY
16573 + AuCache_HINOTIFY,
16578 +#define AuCache(type) KMEM_CACHE(type, SLAB_RECLAIM_ACCOUNT)
16580 +extern struct kmem_cache *au_cachep[];
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); }
16588 +AuCacheFuncs(dinfo, DINFO);
16589 +AuCacheFuncs(icntnr, ICNTNR);
16590 +AuCacheFuncs(finfo, FINFO);
16591 +AuCacheFuncs(vdir, VDIR);
16592 +AuCacheFuncs(dehstr, DEHSTR);
16594 +/* ---------------------------------------------------------------------- */
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
16602 +++ b/fs/aufs/opts.c
16605 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
16614 + * mount options/flags
16617 +#include <linux/types.h> /* a distribution requires */
16618 +#include <linux/parser.h>
16621 +/* ---------------------------------------------------------------------- */
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,
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
16645 +static match_table_t options = {
16646 + {Opt_br, "br=%s"},
16647 + {Opt_br, "br:%s"},
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"},
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"}, */
16665 + {Opt_dirwh, "dirwh=%d"},
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"},
16678 + {Opt_plink, "plink"},
16679 + {Opt_noplink, "noplink"},
16680 +#ifdef CONFIG_AUFS_DEBUG
16681 + {Opt_list_plink, "list_plink"},
16684 + {Opt_udba, "udba=%s"},
16686 + {Opt_diropq_a, "diropq=always"},
16687 + {Opt_diropq_a, "diropq=a"},
16688 + {Opt_diropq_w, "diropq=whiteouted"},
16689 + {Opt_diropq_w, "diropq=w"},
16691 + {Opt_warn_perm, "warn_perm"},
16692 + {Opt_nowarn_perm, "nowarn_perm"},
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"},
16701 + {Opt_rendir, "rendir=%d"},
16703 + {Opt_refrof, "refrof"},
16704 + {Opt_norefrof, "norefrof"},
16706 + {Opt_verbose, "verbose"},
16707 + {Opt_verbose, "v"},
16708 + {Opt_noverbose, "noverbose"},
16709 + {Opt_noverbose, "quiet"},
16710 + {Opt_noverbose, "q"},
16711 + {Opt_noverbose, "silent"},
16713 + {Opt_sum, "sum"},
16714 + {Opt_nosum, "nosum"},
16715 + {Opt_wsum, "wsum"},
16717 + {Opt_rdcache, "rdcache=%d"},
16718 + {Opt_rdblk, "rdblk=%d"},
16719 + {Opt_rdhash, "rdhash=%d"},
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"},
16727 + /* internal use for the scripts */
16728 + {Opt_ignore_silent, "si=%s"},
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"},
16739 +/* ---------------------------------------------------------------------- */
16741 +static const char *au_parser_pattern(int val, struct match_token *token)
16743 + while (token->pattern) {
16744 + if (token->token == val)
16745 + return token->pattern;
16752 +/* ---------------------------------------------------------------------- */
16754 +static match_table_t brperms = {
16755 + {AuBrPerm_RO, AUFS_BRPERM_RO},
16756 + {AuBrPerm_RR, AUFS_BRPERM_RR},
16757 + {AuBrPerm_RW, AUFS_BRPERM_RW},
16759 + {AuBrPerm_ROWH, AUFS_BRPERM_ROWH},
16760 + {AuBrPerm_RRWH, AUFS_BRPERM_RRWH},
16761 + {AuBrPerm_RWNoLinkWH, AUFS_BRPERM_RWNLWH},
16763 + {AuBrPerm_ROWH, "nfsro"},
16764 + {AuBrPerm_RO, NULL}
16767 +static int br_perm_val(char *perm)
16770 + substring_t args[MAX_OPT_ARGS];
16772 + val = match_token(perm, brperms, args);
16776 +const char *au_optstr_br_perm(int brperm)
16778 + return au_parser_pattern(brperm, (void *)brperms);
16781 +/* ---------------------------------------------------------------------- */
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"},
16792 +static int udba_val(char *str)
16794 + substring_t args[MAX_OPT_ARGS];
16796 + return match_token(str, udbalevel, args);
16799 +const char *au_optstr_udba(int udba)
16801 + return au_parser_pattern(udba, (void *)udbalevel);
16804 +/* ---------------------------------------------------------------------- */
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"},
16816 + {AuWbrCreate_MFSRR, "mfsrr:%d"},
16817 + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
16818 + {AuWbrCreate_PMFS, "pmfs"},
16819 + {AuWbrCreate_PMFSV, "pmfs:%d"},
16824 +/* cf. linux/lib/parser.c */
16825 +static int au_match_ull(substring_t *s, unsigned long long *result)
16828 + unsigned int len;
16832 + len = s->to - s->from;
16833 + if (len + 1 <= sizeof(a)) {
16834 + memcpy(a, s->from, len);
16836 + err = strict_strtoull(a, 0, result);
16841 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
16842 + struct au_opt_wbr_create *create)
16845 + unsigned long long ull;
16848 + if (!au_match_ull(arg, &ull))
16849 + create->mfsrr_watermark = ull;
16851 + AuErr("bad integer in %s\n", str);
16858 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
16859 + struct au_opt_wbr_create *create)
16864 + if (!match_int(arg, &n) && 0 <= n)
16865 + create->mfs_second = n;
16867 + AuErr("bad integer in %s\n", str);
16874 +static int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
16877 + substring_t args[MAX_OPT_ARGS];
16879 + err = match_token(str, au_wbr_create_policy, args);
16880 + create->wbr_create = err;
16882 + case AuWbrCreate_MFSRRV:
16883 + e = au_wbr_mfs_wmark(&args[0], str, create);
16885 + e = au_wbr_mfs_sec(&args[1], str, create);
16889 + case AuWbrCreate_MFSRR:
16890 + e = au_wbr_mfs_wmark(&args[0], str, create);
16891 + if (unlikely(e)) {
16896 + case AuWbrCreate_MFS:
16897 + case AuWbrCreate_PMFS:
16898 + create->mfs_second = AUFS_MFS_SECOND_DEF;
16900 + case AuWbrCreate_MFSV:
16901 + case AuWbrCreate_PMFSV:
16902 + e = au_wbr_mfs_sec(&args[0], str, create);
16911 +const char *au_optstr_wbr_create(int wbr_create)
16913 + return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
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"},
16926 +static int au_wbr_copyup_val(char *str)
16928 + substring_t args[MAX_OPT_ARGS];
16930 + return match_token(str, au_wbr_copyup_policy, args);
16933 +const char *au_optstr_wbr_copyup(int wbr_copyup)
16935 + return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
16938 +/* ---------------------------------------------------------------------- */
16940 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
16942 +static void dump_opts(struct au_opts *opts)
16944 +#ifdef CONFIG_AUFS_DEBUG
16945 + /* reduce stack space */
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;
16954 + struct au_opt *opt;
16957 + while (opt->type != Opt_tail) {
16958 + switch (opt->type) {
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);
16967 + u.del = &opt->del;
16968 + AuDbg("del {%s, %p}\n",
16969 + u.del->pathname, u.del->h_path.dentry);
16973 + u.mod = &opt->mod;
16974 + AuDbg("mod {%s, 0x%x, %p}\n",
16975 + u.mod->path, u.mod->perm, u.mod->h_root);
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);
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);
16990 + AuDbg("dirwh %d\n", opt->dirwh);
16992 + case Opt_rdcache:
16993 + AuDbg("rdcache %d\n", opt->rdcache);
16996 + AuDbg("rdblk %u\n", opt->rdblk);
16999 + AuDbg("rdhash %u\n", opt->rdhash);
17002 + u.xino = &opt->xino;
17003 + AuDbg("xino {%s %.*s}\n",
17005 + AuDLNPair(u.xino->file->f_dentry));
17007 + case Opt_trunc_xino:
17008 + AuLabel(trunc_xino);
17010 + case Opt_notrunc_xino:
17011 + AuLabel(notrunc_xino);
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);
17022 + case Opt_trunc_xib:
17023 + AuLabel(trunc_xib);
17025 + case Opt_notrunc_xib:
17026 + AuLabel(notrunc_xib);
17031 + case Opt_noplink:
17032 + AuLabel(noplink);
17034 + case Opt_list_plink:
17035 + AuLabel(list_plink);
17038 + AuDbg("udba %d, %s\n",
17039 + opt->udba, au_optstr_udba(opt->udba));
17041 + case Opt_diropq_a:
17042 + AuLabel(diropq_a);
17044 + case Opt_diropq_w:
17045 + AuLabel(diropq_w);
17047 + case Opt_warn_perm:
17048 + AuLabel(warn_perm);
17050 + case Opt_nowarn_perm:
17051 + AuLabel(nowarn_perm);
17056 + case Opt_norefrof:
17057 + AuLabel(norefrof);
17059 + case Opt_verbose:
17060 + AuLabel(verbose);
17062 + case Opt_noverbose:
17063 + AuLabel(noverbose);
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);
17083 + case AuWbrCreate_MFSRR:
17084 + AuDbg("%llu watermark\n",
17085 + u.create->mfsrr_watermark);
17087 + case AuWbrCreate_MFSRRV:
17088 + AuDbg("%llu watermark, %d sec\n",
17089 + u.create->mfsrr_watermark,
17090 + u.create->mfs_second);
17094 + case Opt_wbr_copyup:
17095 + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
17096 + au_optstr_wbr_copyup(opt->wbr_copyup));
17106 +void au_opts_free(struct au_opts *opts)
17108 + struct au_opt *opt;
17111 + while (opt->type != Opt_tail) {
17112 + switch (opt->type) {
17115 + case Opt_prepend:
17116 + path_put(&opt->add.path);
17120 + path_put(&opt->del.h_path);
17124 + dput(opt->mod.h_root);
17127 + fput(opt->xino.file);
17134 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
17135 + aufs_bindex_t bindex)
17138 + struct au_opt_add *add = &opt->add;
17141 + add->bindex = bindex;
17142 + add->perm = AuBrPerm_Last;
17143 + add->pathname = opt_str;
17144 + p = strchr(opt_str, '=');
17148 + add->perm = br_perm_val(p);
17151 + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
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;
17160 + opt->type = Opt_add;
17163 + AuErr("lookup failed %s (%d)\n", add->pathname, err);
17170 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
17174 + del->pathname = args[0].from;
17175 + AuDbg("del path %s\n", del->pathname);
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);
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[])
17189 + struct dentry *root;
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);
17200 + del->h_path.dentry = dget(au_h_dptr(root, bindex));
17201 + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
17204 + aufs_read_unlock(root, !AuLock_IR);
17209 +static int au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
17212 + struct path path;
17216 + mod->path = args[0].from;
17217 + p = strchr(mod->path, '=');
17218 + if (unlikely(!p)) {
17219 + AuErr("no permssion %s\n", args[0].from);
17224 + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
17225 + if (unlikely(err)) {
17226 + AuErr("lookup failed %s (%d)\n", mod->path, err);
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);
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[])
17244 + struct dentry *root;
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);
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));
17261 + aufs_read_unlock(root, !AuLock_IR);
17266 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
17267 + substring_t args[])
17270 + struct file *file;
17272 + file = au_xino_create(sb, args[0].from, /*silent*/0);
17273 + err = PTR_ERR(file);
17274 + if (IS_ERR(file))
17278 + if (unlikely(file->f_dentry->d_sb == sb)) {
17280 + AuErr("%s must be outside\n", args[0].from);
17285 + xino->file = file;
17286 + xino->path = args[0].from;
17293 +int au_opts_parse_xino_itrunc_path(struct super_block *sb,
17294 + struct au_opt_xino_itrunc *xino_itrunc,
17295 + substring_t args[])
17298 + aufs_bindex_t bend, bindex;
17299 + struct path path;
17300 + struct dentry *root;
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);
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;
17318 + aufs_read_unlock(root, !AuLock_IR);
17321 + if (unlikely(xino_itrunc->bindex < 0)) {
17322 + AuErr("no such branch %s\n", args[0].from);
17330 +/* called without aufs lock */
17331 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
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;
17339 + /* reduce the stack space */
17341 + struct au_opt_xino_itrunc *xino_itrunc;
17342 + struct au_opt_wbr_create *create;
17345 + substring_t args[MAX_OPT_ARGS];
17349 + a = kmalloc(sizeof(*a), GFP_NOFS);
17350 + if (unlikely(!a))
17353 + root = sb->s_root;
17357 + opt_tail = opt + opts->max_opt - 1;
17358 + opt->type = Opt_tail;
17359 + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
17362 + token = match_token(opt_str, options, a->args);
17366 + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
17368 + err = opt_add(opt, opt_str, opts->sb_flags,
17370 + if (unlikely(!err && ++opt > opt_tail)) {
17374 + opt->type = Opt_tail;
17379 + if (unlikely(match_int(&a->args[0], &n))) {
17380 + AuErr("bad integer in %s\n", opt_str);
17384 + err = opt_add(opt, a->args[1].from, opts->sb_flags,
17387 + opt->type = token;
17390 + err = opt_add(opt, a->args[0].from, opts->sb_flags,
17391 + /*dummy bindex*/1);
17393 + opt->type = token;
17395 + case Opt_prepend:
17396 + err = opt_add(opt, a->args[0].from, opts->sb_flags,
17399 + opt->type = token;
17402 + err = au_opts_parse_del(&opt->del, a->args);
17404 + opt->type = token;
17406 +#if 0 /* reserved for future use */
17408 + del->pathname = "(indexed)";
17409 + if (unlikely(match_int(&args[0], &n))) {
17410 + AuErr("bad integer in %s\n", opt_str);
17413 + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
17415 + opt->type = token;
17419 + err = au_opts_parse_mod(&opt->mod, a->args);
17421 + opt->type = token;
17423 +#ifdef IMOD /* reserved for future use */
17425 + u.mod->path = "(indexed)";
17426 + if (unlikely(match_int(&a->args[0], &n))) {
17427 + AuErr("bad integer in %s\n", opt_str);
17430 + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
17432 + opt->type = token;
17436 + err = au_opts_parse_xino(sb, &opt->xino, a->args);
17438 + opt->type = token;
17441 + case Opt_trunc_xino_path:
17442 + err = au_opts_parse_xino_itrunc_path
17443 + (sb, &opt->xino_itrunc, a->args);
17445 + opt->type = token;
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);
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);
17461 + aufs_read_unlock(root, !AuLock_IR);
17463 + opt->type = token;
17467 + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
17470 + opt->type = token;
17473 + case Opt_rdcache:
17474 + if (unlikely(match_int(&a->args[0], &opt->rdcache)))
17477 + opt->type = token;
17480 + if (unlikely(match_int(&a->args[0], &n)
17482 + || n > KMALLOC_MAX_SIZE)) {
17483 + AuErr("bad integer in %s\n", opt_str);
17486 + if (unlikely(n < NAME_MAX)) {
17487 + AuErr("rdblk must be larger than %d\n",
17493 + opt->type = token;
17496 + if (unlikely(match_int(&a->args[0], &n)
17498 + || n * sizeof(struct hlist_head)
17499 + > KMALLOC_MAX_SIZE)) {
17500 + AuErr("bad integer in %s\n", opt_str);
17505 + opt->type = token;
17508 + case Opt_trunc_xino:
17509 + case Opt_notrunc_xino:
17511 + case Opt_trunc_xib:
17512 + case Opt_notrunc_xib:
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:
17521 + case Opt_norefrof:
17522 + case Opt_verbose:
17523 + case Opt_noverbose:
17528 + opt->type = token;
17532 + opt->udba = udba_val(a->args[0].from);
17533 + if (opt->udba >= 0) {
17535 + opt->type = token;
17537 + AuErr("wrong value, %s\n", opt_str);
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) {
17546 + opt->type = token;
17548 + AuErr("wrong value, %s\n", opt_str);
17550 + case Opt_wbr_copyup:
17551 + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
17552 + if (opt->wbr_copyup >= 0) {
17554 + opt->type = token;
17556 + AuErr("wrong value, %s\n", opt_str);
17560 + AuWarn("ignored %s\n", opt_str);
17562 + case Opt_ignore_silent:
17567 + AuErr("unknown option %s\n", opt_str);
17571 + if (!err && !skipped) {
17572 + if (unlikely(++opt > opt_tail)) {
17575 + opt->type = Opt_tail;
17578 + opt->type = Opt_tail;
17584 + if (unlikely(err))
17585 + au_opts_free(opts);
17591 +static int au_opt_wbr_create(struct super_block *sb,
17592 + struct au_opt_wbr_create *create)
17595 + struct au_sbinfo *sbinfo;
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);
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;
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;
17620 + if (sbinfo->si_wbr_create_ops->init)
17621 + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
17628 + * plus: processed without an error
17629 + * zero: unprocessed
17631 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
17632 + struct au_opts *opts)
17635 + struct au_sbinfo *sbinfo;
17637 + err = 1; /* handled */
17638 + sbinfo = au_sbi(sb);
17639 + switch (opt->type) {
17641 + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
17642 + sbinfo->si_mntflags |= opt->udba;
17643 + opts->given_udba |= opt->udba;
17647 + au_opt_set(sbinfo->si_mntflags, PLINK);
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);
17654 + case Opt_list_plink:
17655 + if (au_opt_test(sbinfo->si_mntflags, PLINK))
17656 + au_plink_list(sb);
17659 + case Opt_diropq_a:
17660 + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
17662 + case Opt_diropq_w:
17663 + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
17666 + case Opt_warn_perm:
17667 + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
17669 + case Opt_nowarn_perm:
17670 + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
17674 + au_opt_set(sbinfo->si_mntflags, REFROF);
17676 + case Opt_norefrof:
17677 + au_opt_clr(sbinfo->si_mntflags, REFROF);
17680 + case Opt_verbose:
17681 + au_opt_set(sbinfo->si_mntflags, VERBOSE);
17683 + case Opt_noverbose:
17684 + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
17688 + au_opt_set(sbinfo->si_mntflags, SUM);
17691 + au_opt_clr(sbinfo->si_mntflags, SUM);
17692 + au_opt_set(sbinfo->si_mntflags, SUM_W);
17694 + au_opt_clr(sbinfo->si_mntflags, SUM);
17695 + au_opt_clr(sbinfo->si_mntflags, SUM_W);
17698 + case Opt_wbr_create:
17699 + err = au_opt_wbr_create(sb, &opt->wbr_create);
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;
17707 + sbinfo->si_dirwh = opt->dirwh;
17710 + case Opt_rdcache:
17711 + sbinfo->si_rdcache = opt->rdcache * HZ;
17714 + sbinfo->si_rdblk = opt->rdblk;
17717 + sbinfo->si_rdhash = opt->rdhash;
17720 + case Opt_trunc_xino:
17721 + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
17723 + case Opt_notrunc_xino:
17724 + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
17727 + case Opt_trunc_xino_path:
17728 + case Opt_itrunc_xino:
17729 + err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
17734 + case Opt_trunc_xib:
17735 + au_fset_opts(opts->flags, TRUNC_XIB);
17737 + case Opt_notrunc_xib:
17738 + au_fclr_opts(opts->flags, TRUNC_XIB);
17750 + * returns tri-state.
17751 + * plus: processed without an error
17752 + * zero: unprocessed
17755 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
17756 + struct au_opts *opts)
17758 + int err, do_refresh;
17761 + switch (opt->type) {
17763 + opt->add.bindex = au_sbend(sb) + 1;
17764 + if (opt->add.bindex < 0)
17765 + opt->add.bindex = 0;
17767 + case Opt_prepend:
17768 + opt->add.bindex = 0;
17771 + err = au_br_add(sb, &opt->add,
17772 + au_ftest_opts(opts->flags, REMOUNT));
17775 + au_fset_opts(opts->flags, REFRESH_DIR);
17776 + if (au_br_whable(opt->add.perm))
17777 + au_fset_opts(opts->flags, REFRESH_NONDIR);
17783 + err = au_br_del(sb, &opt->del,
17784 + au_ftest_opts(opts->flags, REMOUNT));
17787 + au_fset_opts(opts->flags, TRUNC_XIB);
17788 + au_fset_opts(opts->flags, REFRESH_DIR);
17789 + au_fset_opts(opts->flags, REFRESH_NONDIR);
17795 + err = au_br_mod(sb, &opt->mod,
17796 + au_ftest_opts(opts->flags, REMOUNT),
17800 + if (do_refresh) {
17801 + au_fset_opts(opts->flags, REFRESH_DIR);
17802 + au_fset_opts(opts->flags, REFRESH_NONDIR);
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)
17816 + aufs_bindex_t bend, bindex;
17817 + struct dentry *root, *parent, *h_root;
17820 + switch (opt->type) {
17822 + err = au_xino_set(sb, &opt->xino,
17823 + !!au_ftest_opts(opts->flags, REMOUNT));
17824 + if (unlikely(err))
17827 + *opt_xino = &opt->xino;
17828 + au_xino_brid_set(sb, -1);
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));
17845 + au_xino_brid_set(sb, -1);
17846 + *opt_xino = (void *)-1;
17853 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
17854 + unsigned int pending)
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;
17866 + sbinfo = au_sbi(sb);
17867 + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
17869 + if (unlikely(!(sb_flags & MS_RDONLY)
17870 + && !au_br_writable(au_sbr_perm(sb, 0))))
17871 + AuWarn("first branch should be rw\n");
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");
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++) {
17884 + h_dir = au_h_iptr(dir, bindex);
17885 + br = au_sbr(sb, bindex);
17888 + wbr = br->br_wbr;
17890 + wbr_wh_read_lock(wbr);
17892 + switch (br->br_perm) {
17893 + case AuBrPerm_RO:
17894 + case AuBrPerm_ROWH:
17895 + case AuBrPerm_RR:
17896 + case AuBrPerm_RRWH:
17899 + || (!wbr->wbr_whbase
17900 + && !wbr->wbr_plink
17901 + && !wbr->wbr_orph));
17904 + case AuBrPerm_RWNoLinkWH:
17905 + /* skip = (!br->br_whbase && !br->br_orph); */
17906 + skip = (!wbr || !wbr->wbr_whbase);
17907 + if (skip && wbr) {
17909 + skip = !!wbr->wbr_plink;
17911 + skip = !wbr->wbr_plink;
17915 + case AuBrPerm_RW:
17916 + /* skip = (br->br_whbase && br->br_ohph); */
17917 + skip = (wbr && wbr->wbr_whbase);
17920 + skip = !!wbr->wbr_plink;
17922 + skip = !wbr->wbr_plink;
17930 + wbr_wh_read_unlock(wbr);
17935 + hdir = au_hi(dir, bindex);
17936 + au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
17938 + wbr_wh_write_lock(wbr);
17939 + err = au_wh_init(au_h_dptr(root, bindex), br, sb);
17941 + wbr_wh_write_unlock(wbr);
17942 + au_hin_imtx_unlock(hdir);
17944 + if (!err && do_free) {
17946 + br->br_wbr = NULL;
17953 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
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;
17965 + while (err >= 0 && opt->type != Opt_tail)
17966 + err = au_opt_simple(sb, opt++, opts);
17969 + else if (unlikely(err < 0))
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);
17979 + while (err >= 0 && opt->type != Opt_tail)
17980 + err = au_opt_br(sb, opt++, opts);
17983 + else if (unlikely(err < 0))
17986 + bend = au_sbend(sb);
17987 + if (unlikely(bend < 0)) {
17989 + AuErr("no branches\n");
17993 + if (au_opt_test(tmp, XINO))
17994 + au_opt_set(sbinfo->si_mntflags, XINO);
17996 + while (!err && opt->type != Opt_tail)
17997 + err = au_opt_xino(sb, opt++, &opt_xino, opts);
17998 + if (unlikely(err))
18001 + err = au_opts_verify(sb, sb->s_flags, tmp);
18002 + if (unlikely(err))
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))
18012 + err = au_xino_set(sb, &xino, /*remount*/0);
18014 + if (unlikely(err))
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);
18031 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
18034 + struct inode *dir;
18035 + struct au_opt_xino *opt_xino;
18036 + struct au_opt *opt;
18037 + struct au_sbinfo *sbinfo;
18039 + dir = sb->s_root->d_inode;
18040 + sbinfo = au_sbi(sb);
18044 + while (err >= 0 && opt->type != Opt_tail) {
18045 + err = au_opt_simple(sb, opt, opts);
18047 + err = au_opt_br(sb, opt, opts);
18049 + err = au_opt_xino(sb, opt, &opt_xino, opts);
18055 + /* go on even err */
18057 + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
18058 + if (unlikely(rerr && !err))
18061 + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
18062 + rerr = au_xib_trunc(sb);
18063 + if (unlikely(rerr && !err))
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);
18072 + AuDbg("status 0x%x\n", opts->flags);
18076 +/* ---------------------------------------------------------------------- */
18078 +unsigned int au_opt_udba(struct super_block *sb)
18080 + return au_mntflags(sb) & AuOptMask_UDBA;
18082 diff --git a/fs/aufs/opts.h b/fs/aufs/opts.h
18083 new file mode 100644
18084 index 0000000..275702c
18086 +++ b/fs/aufs/opts.h
18089 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
18098 + * mount options/flags
18101 +#ifndef __AUFS_OPTS_H__
18102 +#define __AUFS_OPTS_H__
18106 +#include <linux/fs.h>
18107 +#include <linux/namei.h>
18108 +#include <linux/aufs_type.h>
18110 +/* ---------------------------------------------------------------------- */
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 */
18128 +#ifndef CONFIG_AUFS_HINOTIFY
18129 +#undef AuOpt_UDBA_HINOTIFY
18130 +#define AuOpt_UDBA_HINOTIFY 0
18133 +#define AuOpt_Def (AuOpt_XINO \
18134 + | AuOpt_UDBA_REVAL \
18136 + /* | AuOpt_DIRPERM1 */ \
18137 + | AuOpt_WARN_PERM)
18138 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
18139 + | AuOpt_UDBA_REVAL \
18140 + | AuOpt_UDBA_HINOTIFY)
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); \
18147 +#define au_opt_set_udba(flags, name) do { \
18148 + (flags) &= ~AuOptMask_UDBA; \
18149 + ((flags) |= AuOpt_##name); \
18151 +#define au_opt_clr(flags, name) { ((flags) &= ~AuOpt_##name); }
18153 +/* ---------------------------------------------------------------------- */
18155 +/* policies to select one among multiple writable branches */
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 */
18166 + AuWbrCreate_Def = AuWbrCreate_TDP
18170 + AuWbrCopyup_TDP, /* top down parent */
18171 + AuWbrCopyup_BUP, /* bottom up parent */
18172 + AuWbrCopyup_BU, /* bottom up */
18174 + AuWbrCopyup_Def = AuWbrCopyup_TDP
18177 +/* ---------------------------------------------------------------------- */
18179 +struct au_opt_add {
18180 + aufs_bindex_t bindex;
18183 + struct path path;
18186 +struct au_opt_del {
18188 + struct path h_path;
18191 +struct au_opt_mod {
18194 + struct dentry *h_root;
18197 +struct au_opt_xino {
18199 + struct file *file;
18202 +struct au_opt_xino_itrunc {
18203 + aufs_bindex_t bindex;
18206 +struct au_opt_wbr_create {
18209 + unsigned long long mfsrr_watermark;
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;
18222 + unsigned int rdblk;
18223 + unsigned int rdhash;
18225 + struct au_opt_wbr_create wbr_create;
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; }
18240 + struct au_opt *opt;
18243 + unsigned int given_udba;
18244 + unsigned int flags;
18245 + unsigned long sb_flags;
18248 +/* ---------------------------------------------------------------------- */
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);
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);
18262 +unsigned int au_opt_udba(struct super_block *sb);
18264 +/* ---------------------------------------------------------------------- */
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
18272 +++ b/fs/aufs/plink.c
18275 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
18290 + * during a user process maintains the pseudo-links,
18291 + * prohibit adding a new plink and branch manipulation.
18293 +void au_plink_block_maintain(struct super_block *sb)
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));
18300 +/* ---------------------------------------------------------------------- */
18302 +struct pseudo_link {
18303 + struct list_head list;
18304 + struct inode *inode;
18307 +#ifdef CONFIG_AUFS_DEBUG
18308 +void au_plink_list(struct super_block *sb)
18310 + struct au_sbinfo *sbinfo;
18311 + struct list_head *plink_list;
18312 + struct pseudo_link *plink;
18314 + sbinfo = au_sbi(sb);
18315 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
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);
18325 +/* is the inode pseudo-linked? */
18326 +int au_plink_test(struct inode *inode)
18329 + struct au_sbinfo *sbinfo;
18330 + struct list_head *plink_list;
18331 + struct pseudo_link *plink;
18333 + sbinfo = au_sbi(inode->i_sb);
18334 + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
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) {
18344 + spin_unlock(&sbinfo->si_plink.spin);
18348 +/* ---------------------------------------------------------------------- */
18351 + * generate a name for plink.
18352 + * the file will be stored under AUFS_WH_PLINKDIR.
18354 +/* 20 is max digits length of ulong 64 */
18355 +#define PLINK_NAME_LEN ((20 + 1) * 2)
18357 +static int plink_name(char *name, int len, struct inode *inode,
18358 + aufs_bindex_t bindex)
18361 + struct inode *h_inode;
18363 + h_inode = au_h_iptr(inode, bindex);
18364 + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
18368 +/* lookup the plink-ed @inode under the branch at @bindex */
18369 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
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 = {
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);
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);
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)
18396 + struct path h_path = {
18397 + .mnt = br->br_mnt
18399 + struct inode *h_dir;
18401 + h_dir = h_parent->d_inode;
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))
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;
18418 + if (!err && !h_path.dentry->d_inode)
18419 + err = vfsub_link(h_dentry, h_dir, &h_path);
18420 + dput(h_path.dentry);
18426 +struct do_whplink_args {
18428 + struct qstr *tgt;
18429 + struct dentry *h_parent;
18430 + struct dentry *h_dentry;
18431 + struct au_branch *br;
18434 +static void call_do_whplink(void *args)
18436 + struct do_whplink_args *a = args;
18437 + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
18440 +static int whplink(struct dentry *h_dentry, struct inode *inode,
18441 + aufs_bindex_t bindex, struct au_branch *br)
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 = {
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);
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 = {
18463 + .h_parent = h_parent,
18464 + .h_dentry = h_dentry,
18467 + wkq_err = au_wkq_wait(call_do_whplink, &args);
18468 + if (unlikely(wkq_err))
18471 + err = do_whplink(&tgtname, h_parent, h_dentry, br);
18472 + mutex_unlock(&h_dir->i_mutex);
18477 +/* free a single plink */
18478 +static void do_put_plink(struct pseudo_link *plink, int do_del)
18480 + iput(plink->inode);
18482 + list_del(&plink->list);
18487 + * create a new pseudo-link for @h_dentry on @bindex.
18488 + * the linked inode is held in aufs @inode.
18490 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18491 + struct dentry *h_dentry)
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;
18499 + sb = inode->i_sb;
18500 + sbinfo = au_sbi(sb);
18501 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
18506 + plink_list = &sbinfo->si_plink.head;
18507 + spin_lock(&sbinfo->si_plink.spin);
18508 + list_for_each_entry(plink, plink_list, list) {
18510 + if (plink->inode == inode) {
18516 + spin_unlock(&sbinfo->si_plink.spin);
18522 + plink = kmalloc(sizeof(*plink), GFP_ATOMIC);
18524 + plink->inode = au_igrab(inode);
18525 + list_add(&plink->list, plink_list);
18530 + spin_unlock(&sbinfo->si_plink.spin);
18533 + au_plink_block_maintain(sb);
18534 + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
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);
18546 +/* free all plinks */
18547 +void au_plink_put(struct super_block *sb)
18549 + struct au_sbinfo *sbinfo;
18550 + struct list_head *plink_list;
18551 + struct pseudo_link *plink, *tmp;
18553 + sbinfo = au_sbi(sb);
18554 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
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);
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)
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;
18573 + sbinfo = au_sbi(sb);
18574 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
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) {
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)
18589 + au_set_h_iptr(inode, bindex, NULL, 0);
18594 + do_put_plink(plink, 1);
18597 + for (bindex = bstart; bindex <= bend; bindex++)
18598 + if (au_h_iptr(inode, bindex)) {
18603 + do_put_plink(plink, 1);
18605 + ii_write_unlock(inode);
18609 diff --git a/fs/aufs/rwsem.h b/fs/aufs/rwsem.h
18610 new file mode 100644
18611 index 0000000..d569243
18613 +++ b/fs/aufs/rwsem.h
18616 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
18625 + * simple read-write semaphore wrappers
18628 +#ifndef __AUFS_RWSEM_H__
18629 +#define __AUFS_RWSEM_H__
18633 +#include <linux/fs.h>
18635 +#define au_rwsem_destroy(rw) AuDebugOn(rwsem_is_locked(rw))
18636 +#define AuRwMustNoWaiters(rw) AuDebugOn(!list_empty(&(rw)->wait_list))
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); } */
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); }
18661 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
18662 + AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
18663 + AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
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
18671 +++ b/fs/aufs/sbinfo.c
18674 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
18683 + * superblock private data
18689 + * they are necessary regardless sysfs is disabled.
18691 +void au_si_free(struct kobject *kobj)
18693 + struct au_sbinfo *sbinfo;
18694 + struct super_block *sb;
18696 + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
18697 + AuDebugOn(!list_empty(&sbinfo->si_plink.head));
18699 + sb = sbinfo->si_sb;
18700 + si_write_lock(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);
18711 +int au_si_alloc(struct super_block *sb)
18714 + struct au_sbinfo *sbinfo;
18717 + sbinfo = kmalloc(sizeof(*sbinfo), GFP_NOFS);
18718 + if (unlikely(!sbinfo))
18721 + /* will be reallocated separately */
18722 + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
18723 + if (unlikely(!sbinfo->si_branch))
18726 + memset(&sbinfo->si_kobj, 0, sizeof(sbinfo->si_kobj));
18727 + err = sysaufs_si_init(sbinfo);
18728 + if (unlikely(err))
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;
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;
18744 + sbinfo->si_mntflags = AuOpt_Def;
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 */
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;
18759 + au_spl_init(&sbinfo->si_plink);
18760 + init_waitqueue_head(&sbinfo->si_plink_wq);
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 */
18769 + kfree(sbinfo->si_branch);
18776 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
18779 + struct au_branch **brp;
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);
18787 + sbinfo->si_branch = brp;
18794 +/* ---------------------------------------------------------------------- */
18796 +unsigned int au_sigen_inc(struct super_block *sb)
18798 + unsigned int gen;
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++;
18807 +aufs_bindex_t au_new_br_id(struct super_block *sb)
18809 + aufs_bindex_t br_id;
18811 + struct au_sbinfo *sbinfo;
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)
18823 +/* ---------------------------------------------------------------------- */
18825 +/* dentry and super_block lock. call at entry point */
18826 +void aufs_read_lock(struct dentry *dentry, int flags)
18828 + si_read_lock(dentry->d_sb, flags);
18829 + if (au_ftest_lock(flags, DW))
18830 + di_write_lock_child(dentry);
18832 + di_read_lock_child(dentry, flags);
18835 +void aufs_read_unlock(struct dentry *dentry, int flags)
18837 + if (au_ftest_lock(flags, DW))
18838 + di_write_unlock(dentry);
18840 + di_read_unlock(dentry, flags);
18841 + si_read_unlock(dentry->d_sb);
18844 +void aufs_write_lock(struct dentry *dentry)
18846 + si_write_lock(dentry->d_sb);
18847 + di_write_lock_child(dentry);
18850 +void aufs_write_unlock(struct dentry *dentry)
18852 + di_write_unlock(dentry);
18853 + si_write_unlock(dentry->d_sb);
18856 +void aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
18858 + si_read_lock(d1->d_sb, flags);
18859 + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
18862 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
18864 + di_write_unlock2(d1, d2);
18865 + si_read_unlock(d1->d_sb);
18867 diff --git a/fs/aufs/spl.h b/fs/aufs/spl.h
18868 new file mode 100644
18869 index 0000000..89ea7a2
18871 +++ b/fs/aufs/spl.h
18874 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
18883 + * simple list protected by a spinlock
18886 +#ifndef __AUFS_SPL_H__
18887 +#define __AUFS_SPL_H__
18891 +#include <linux/fs.h>
18893 +struct au_splhead {
18895 + struct list_head head;
18898 +static inline void au_spl_init(struct au_splhead *spl)
18900 + spin_lock_init(&spl->spin);
18901 + INIT_LIST_HEAD(&spl->head);
18904 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
18906 + spin_lock(&spl->spin);
18907 + list_add(list, &spl->head);
18908 + spin_unlock(&spl->spin);
18911 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
18913 + spin_lock(&spl->spin);
18915 + spin_unlock(&spl->spin);
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
18924 +++ b/fs/aufs/super.c
18927 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
18936 + * mount and super_block operations
18939 +#include <linux/buffer_head.h>
18940 +#include <linux/seq_file.h>
18941 +#include <linux/statfs.h>
18945 + * super_operations
18947 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
18949 + struct au_icntnr *c;
18951 + c = au_cache_alloc_icntnr();
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;
18961 +static void aufs_destroy_inode(struct inode *inode)
18963 + au_iinfo_fin(inode);
18964 + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
18967 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
18969 + struct inode *inode;
18972 + inode = iget_locked(sb, ino);
18973 + if (unlikely(!inode)) {
18974 + inode = ERR_PTR(-ENOMEM);
18977 + if (!(inode->i_state & I_NEW))
18980 + err = au_xigen_new(inode);
18982 + err = au_iinfo_init(inode);
18984 + inode->i_version++;
18986 + iget_failed(inode);
18987 + inode = ERR_PTR(err);
18991 + /* never return NULL */
18992 + AuDebugOn(!inode);
18993 + AuTraceErrPtr(inode);
18997 +/* lock free root dinfo */
18998 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
19001 + aufs_bindex_t bindex, bend;
19002 + struct path path;
19003 + struct au_hdentry *hd;
19004 + struct au_branch *br;
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);
19015 + err = seq_printf(seq, "=%s",
19016 + au_optstr_br_perm(br->br_perm));
19017 + if (!err && bindex != bend)
19018 + err = seq_putc(seq, ':');
19024 +static void au_show_wbr_create(struct seq_file *m, int v,
19025 + struct au_sbinfo *sbinfo)
19029 + seq_printf(m, ",create=");
19030 + pat = au_optstr_wbr_create(v);
19032 + case AuWbrCreate_TDP:
19033 + case AuWbrCreate_RR:
19034 + case AuWbrCreate_MFS:
19035 + case AuWbrCreate_PMFS:
19036 + seq_printf(m, pat);
19038 + case AuWbrCreate_MFSV:
19039 + seq_printf(m, /*pat*/"mfs:%lu",
19040 + sbinfo->si_wbr_mfs.mfs_expire / HZ);
19042 + case AuWbrCreate_PMFSV:
19043 + seq_printf(m, /*pat*/"pmfs:%lu",
19044 + sbinfo->si_wbr_mfs.mfs_expire / HZ);
19046 + case AuWbrCreate_MFSRR:
19047 + seq_printf(m, /*pat*/"mfsrr:%llu",
19048 + sbinfo->si_wbr_mfs.mfsrr_watermark);
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);
19058 +static int au_show_xino(struct seq_file *seq, struct vfsmount *mnt)
19060 +#ifdef CONFIG_SYSFS
19064 + const int len = sizeof(AUFS_XINO_FNAME) - 1;
19065 + aufs_bindex_t bindex, brid;
19066 + struct super_block *sb;
19067 + struct qstr *name;
19069 + struct dentry *d, *h_root;
19072 + sb = mnt->mnt_sb;
19073 + f = au_sbi(sb)->si_xib;
19077 + /* stop printing the default xino path on the first writable branch */
19079 + brid = au_xino_brid(sb);
19081 + bindex = au_br_index(sb, brid);
19082 + h_root = au_di(sb->s_root)->di_hdentry[0 + bindex].hd_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))
19092 + seq_puts(seq, ",xino=");
19093 + err = au_xino_path(seq, f);
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)
19104 + unsigned int mnt_flags, v;
19105 + struct super_block *sb;
19106 + struct au_sbinfo *sbinfo;
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"); \
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)); \
19120 +#define AuUInt(name, str, val) do { \
19121 + if (val != AUFS_##name##_DEF) \
19122 + seq_printf(m, "," #str "=%u", val); \
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));
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))
19137 + seq_puts(m, ",noxino");
19139 + AuBool(TRUNC_XINO, trunc_xino);
19140 + AuStr(UDBA, udba);
19141 + AuBool(PLINK, plink);
19142 + /* AuBool(DIRPERM1, dirperm1); */
19143 + /* AuBool(REFROF, refrof); */
19145 + v = sbinfo->si_wbr_create;
19146 + if (v != AuWbrCreate_Def)
19147 + au_show_wbr_create(m, v, sbinfo);
19149 + v = sbinfo->si_wbr_copyup;
19150 + if (v != AuWbrCopyup_Def)
19151 + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
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');
19157 + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
19159 + n = sbinfo->si_rdcache / HZ;
19160 + AuUInt(RDCACHE, rdcache, n);
19162 + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
19163 + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
19165 + AuBool(SUM, sum);
19166 + /* AuBool(SUM_W, wsum); */
19167 + AuBool(WARN_PERM, warn_perm);
19168 + AuBool(VERBOSE, verbose);
19171 + /* be sure to print "br:" last */
19172 + if (!sysaufs_brs) {
19173 + seq_puts(m, ",br:");
19174 + au_show_brs(m, sb);
19176 + si_read_unlock(sb);
19184 +/* ---------------------------------------------------------------------- */
19186 +/* sum mode which returns the summation for statfs(2) */
19188 +static u64 au_add_till_max(u64 a, u64 b)
19196 + return ULLONG_MAX;
19199 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
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;
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;
19220 + for (i = bindex + 1; !shared && i <= bend; i++)
19221 + shared = (au_sbr_sb(sb, i) == h_sb);
19225 + /* sb->s_root for NFS is unreliable */
19226 + err = vfs_statfs(h_mnt->mnt_root, buf);
19227 + if (unlikely(err))
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);
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;
19247 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
19250 + struct super_block *sb;
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);
19259 + err = au_statfs_sum(sb, buf);
19260 + si_read_unlock(sb);
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));
19267 + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
19272 +/* ---------------------------------------------------------------------- */
19274 +/* try flushing the lower fs at aufs remount/unmount time */
19276 +static void au_fsync_br(struct super_block *sb)
19278 + aufs_bindex_t bend, bindex;
19280 + struct au_branch *br;
19281 + struct super_block *h_sb;
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)
19289 + h_sb = br->br_mnt->mnt_sb;
19290 + if (bdev_read_only(h_sb->s_bdev))
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);
19303 + * this IS NOT for super_operations.
19304 + * I guess it will be reverted someday.
19306 +static void aufs_umount_begin(struct super_block *sb)
19308 + struct au_sbinfo *sbinfo;
19310 + sbinfo = au_sbi(sb);
19314 + si_write_lock(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);
19323 +/* final actions when unmounting a file system */
19324 +static void aufs_put_super(struct super_block *sb)
19326 + struct au_sbinfo *sbinfo;
19328 + sbinfo = au_sbi(sb);
19332 + aufs_umount_begin(sb);
19333 + dbgaufs_si_fin(sbinfo);
19334 + kobject_put(&sbinfo->si_kobj);
19337 +/* ---------------------------------------------------------------------- */
19340 + * refresh dentry and inode at remount time.
19342 +static int do_refresh(struct dentry *dentry, mode_t type,
19343 + unsigned int dir_flags)
19346 + struct dentry *parent;
19348 + di_write_lock_child(dentry);
19349 + parent = dget_parent(dentry);
19350 + di_read_lock_parent(parent, AuLock_IR);
19352 + /* returns the number of positive dentries */
19353 + err = au_refresh_hdentry(dentry, type);
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);
19360 + if (unlikely(err))
19361 + AuErr("unrecoverable error %d, %.*s\n", err, AuDLNPair(dentry));
19363 + di_read_unlock(parent, AuLock_IR);
19365 + di_write_unlock(dentry);
19370 +static int test_dir(struct dentry *dentry, void *arg __maybe_unused)
19372 + return S_ISDIR(dentry->d_inode->i_mode);
19375 +/* gave up consolidating with refresh_nondir() */
19376 +static int refresh_dir(struct dentry *root, unsigned int sigen)
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);
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);
19395 + /* go on even if err */
19399 + e = au_dpages_init(&dpages, GFP_NOFS);
19400 + if (unlikely(e)) {
19405 + e = au_dcsub_pages(&dpages, root, test_dir, NULL);
19406 + if (unlikely(e)) {
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;
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))
19425 + /* break on err */
19431 + au_dpages_free(&dpages);
19436 +static int test_nondir(struct dentry *dentry, void *arg __maybe_unused)
19438 + return !S_ISDIR(dentry->d_inode->i_mode);
19441 +static int refresh_nondir(struct dentry *root, unsigned int sigen,
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;
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);
19460 + /* go on even if err */
19467 + e = au_dpages_init(&dpages, GFP_NOFS);
19468 + if (unlikely(e)) {
19473 + e = au_dcsub_pages(&dpages, root, test_nondir, NULL);
19474 + if (unlikely(e)) {
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;
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,
19493 + if (unlikely(e && !err))
19495 + /* go on even err */
19501 + au_dpages_free(&dpages);
19506 +static void au_remount_refresh(struct super_block *sb, unsigned int flags)
19509 + unsigned int sigen;
19510 + struct au_sbinfo *sbinfo;
19511 + struct dentry *root;
19512 + struct inode *inode;
19514 + au_sigen_inc(sb);
19515 + sigen = au_sigen(sb);
19516 + sbinfo = au_sbi(sb);
19517 + au_fclr_si(sbinfo, FAILED_REFRESH_DIRS);
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);
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);
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"
19539 + /* aufs_write_lock() calls ..._child() */
19540 + di_write_lock_child(root);
19541 + au_cpup_attr_all(root->d_inode, /*force*/1);
19544 +/* stop extra interpretation of errno in mount(8), and strange error messages */
19545 +static int cvt_err(int err)
19559 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
19562 + struct au_opts opts;
19563 + struct dentry *root;
19564 + struct inode *inode;
19565 + struct au_sbinfo *sbinfo;
19568 + root = sb->s_root;
19569 + if (!data || !*data) {
19570 + aufs_write_lock(root);
19571 + err = au_opts_verify(sb, *flags, /*pending*/0);
19574 + aufs_write_unlock(root);
19579 + memset(&opts, 0, sizeof(opts));
19580 + opts.opt = (void *)__get_free_page(GFP_NOFS);
19581 + if (unlikely(!opts.opt))
19583 + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
19584 + opts.flags = AuOpts_REMOUNT;
19585 + opts.sb_flags = *flags;
19587 + /* parse it before aufs lock */
19588 + err = au_opts_parse(sb, data, &opts);
19589 + if (unlikely(err))
19592 + sbinfo = au_sbi(sb);
19593 + inode = root->d_inode;
19594 + mutex_lock(&inode->i_mutex);
19595 + aufs_write_lock(root);
19598 + /* au_opts_remount() may return an error */
19599 + err = au_opts_remount(sb, &opts);
19600 + au_opts_free(&opts);
19602 + if (au_ftest_opts(opts.flags, REFRESH_DIR)
19603 + || au_ftest_opts(opts.flags, REFRESH_NONDIR))
19604 + au_remount_refresh(sb, opts.flags);
19606 + aufs_write_unlock(root);
19607 + mutex_unlock(&inode->i_mutex);
19610 + free_page((unsigned long)opts.opt);
19612 + err = cvt_err(err);
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
19627 +/* ---------------------------------------------------------------------- */
19629 +static int alloc_root(struct super_block *sb)
19632 + struct inode *inode;
19633 + struct dentry *root;
19636 + inode = au_iget_locked(sb, AUFS_ROOT_INO);
19637 + err = PTR_ERR(inode);
19638 + if (IS_ERR(inode))
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);
19647 + root = d_alloc_root(inode);
19648 + if (unlikely(!root))
19650 + err = PTR_ERR(root);
19651 + if (IS_ERR(root))
19654 + err = au_alloc_dinfo(root);
19656 + sb->s_root = root;
19657 + return 0; /* success */
19660 + goto out; /* do not iput */
19663 + iget_failed(inode);
19670 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
19671 + int silent __maybe_unused)
19674 + struct au_opts opts;
19675 + struct dentry *root;
19676 + struct inode *inode;
19677 + char *arg = raw_data;
19679 + if (unlikely(!arg || !*arg)) {
19681 + AuErr("no arg\n");
19686 + memset(&opts, 0, sizeof(opts));
19687 + opts.opt = (void *)__get_free_page(GFP_NOFS);
19688 + if (unlikely(!opts.opt))
19690 + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
19691 + opts.sb_flags = sb->s_flags;
19693 + err = au_si_alloc(sb);
19694 + if (unlikely(err))
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);
19704 + err = alloc_root(sb);
19705 + if (unlikely(err)) {
19706 + si_write_unlock(sb);
19709 + root = sb->s_root;
19710 + inode = root->d_inode;
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.
19717 + ii_write_lock_parent(inode);
19718 + aufs_write_unlock(root);
19719 + err = au_opts_parse(sb, arg, &opts);
19720 + if (unlikely(err))
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))
19732 + aufs_write_unlock(root);
19733 + mutex_unlock(&inode->i_mutex);
19734 + goto out_opts; /* success */
19737 + aufs_write_unlock(root);
19738 + mutex_unlock(&inode->i_mutex);
19741 + sb->s_root = NULL;
19743 + kobject_put(&au_sbi(sb)->si_kobj);
19744 + sb->s_fs_info = NULL;
19746 + free_page((unsigned long)opts.opt);
19749 + err = cvt_err(err);
19754 +/* ---------------------------------------------------------------------- */
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)
19761 + struct super_block *sb;
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);
19767 + sb = mnt->mnt_sb;
19768 + si_write_lock(sb);
19769 + sysaufs_brs_add(sb, 0);
19770 + si_write_unlock(sb);
19775 +struct file_system_type aufs_fs_type = {
19776 + .name = AUFS_FSTYPE,
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,
19785 diff --git a/fs/aufs/super.h b/fs/aufs/super.h
19786 new file mode 100644
19787 index 0000000..9897dba
19789 +++ b/fs/aufs/super.h
19792 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
19801 + * super_block operations
19804 +#ifndef __AUFS_SUPER_H__
19805 +#define __AUFS_SUPER_H__
19809 +#include <linux/fs.h>
19810 +#include <linux/kobject.h>
19811 +#include <linux/aufs_type.h>
19812 +#include "rwsem.h"
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,
19820 +/* policies to select one among multiple writable branches */
19821 +struct au_wbr_copyup_operations {
19822 + int (*copyup)(struct dentry *dentry);
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);
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;
19837 + unsigned long long mfsrr_bytes;
19838 + unsigned long long mfsrr_watermark;
19841 +/* sbinfo status flags */
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
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; }
19856 +struct au_sbinfo {
19857 + /* nowait tasks in the system-wide workqueue */
19858 + struct au_nowait_tasks si_nowait;
19860 + struct rw_semaphore si_rwsem;
19862 + /* branch management */
19863 + unsigned int si_generation;
19865 + /* see above flags */
19866 + unsigned char au_si_status;
19868 + aufs_bindex_t si_bend;
19869 + aufs_bindex_t si_last_br_id;
19870 + struct au_branch **si_branch;
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;
19878 + /* round robin */
19879 + atomic_t si_wbr_rr_next;
19881 + /* most free space */
19882 + struct au_wbr_mfs si_wbr_mfs;
19884 + /* mount flags */
19885 + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
19886 + unsigned int si_mntflags;
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 */
19900 +#ifdef CONFIG_AUFS_EXPORT
19901 + /* i_generation */
19902 + struct file *si_xigen;
19903 + atomic_t si_xigen_next;
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 */
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).
19917 + unsigned int si_dirwh;
19920 + * rename(2) a directory with all children.
19922 + /* reserved for future use */
19923 + /* int si_rendir; */
19925 + /* pseudo_link list */
19926 + struct au_splhead si_plink;
19927 + wait_queue_head_t si_plink_wq;
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.
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;
19943 + /* dirty, necessary for unmounting, sysfs and sysrq */
19944 + struct super_block *si_sb;
19947 +/* ---------------------------------------------------------------------- */
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))
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; }
19965 +/* ---------------------------------------------------------------------- */
19968 +extern struct file_system_type aufs_fs_type;
19969 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
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);
19976 +unsigned int au_sigen_inc(struct super_block *sb);
19977 +aufs_bindex_t au_new_br_id(struct super_block *sb);
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);
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);
19991 +/* ---------------------------------------------------------------------- */
19993 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
19995 + return sb->s_fs_info;
19998 +/* ---------------------------------------------------------------------- */
20000 +#ifdef CONFIG_AUFS_EXPORT
20001 +void au_export_init(struct super_block *sb);
20003 +static inline int au_test_nfsd(struct task_struct *tsk)
20005 + return !tsk->mm && !strcmp(tsk->comm, "nfsd");
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);
20013 +static inline int au_busy_or_stale(void)
20015 + if (!au_test_nfsd(current))
20020 +static inline void au_export_init(struct super_block *sb)
20025 +static inline int au_test_nfsd(struct task_struct *tsk)
20030 +static inline int au_xigen_inc(struct inode *inode)
20035 +static inline int au_xigen_new(struct inode *inode)
20040 +static inline int au_xigen_set(struct super_block *sb, struct file *base)
20045 +static inline void au_xigen_clr(struct super_block *sb)
20050 +static inline int au_busy_or_stale(void)
20054 +#endif /* CONFIG_AUFS_EXPORT */
20056 +/* ---------------------------------------------------------------------- */
20058 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
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;
20069 +/* ---------------------------------------------------------------------- */
20071 +/* lock superblock. mainly for entry point functions */
20073 + * si_noflush_read_lock, si_noflush_write_lock,
20074 + * si_read_unlock, si_write_unlock, si_downgrade_lock
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);
20080 +static inline void si_read_lock(struct super_block *sb, int flags)
20082 + if (au_ftest_lock(flags, FLUSH))
20083 + au_nwt_flush(&au_sbi(sb)->si_nowait);
20084 + si_noflush_read_lock(sb);
20087 +static inline void si_write_lock(struct super_block *sb)
20089 + au_nwt_flush(&au_sbi(sb)->si_nowait);
20090 + si_noflush_write_lock(sb);
20093 +static inline int si_read_trylock(struct super_block *sb, int flags)
20095 + if (au_ftest_lock(flags, FLUSH))
20096 + au_nwt_flush(&au_sbi(sb)->si_nowait);
20097 + return si_noflush_read_trylock(sb);
20100 +static inline int si_write_trylock(struct super_block *sb, int flags)
20102 + if (au_ftest_lock(flags, FLUSH))
20103 + au_nwt_flush(&au_sbi(sb)->si_nowait);
20104 + return si_noflush_write_trylock(sb);
20107 +/* ---------------------------------------------------------------------- */
20109 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
20111 + return au_sbi(sb)->si_bend;
20114 +static inline unsigned int au_mntflags(struct super_block *sb)
20116 + return au_sbi(sb)->si_mntflags;
20119 +static inline unsigned int au_sigen(struct super_block *sb)
20121 + return au_sbi(sb)->si_generation;
20124 +static inline struct au_branch *au_sbr(struct super_block *sb,
20125 + aufs_bindex_t bindex)
20127 + return au_sbi(sb)->si_branch[0 + bindex];
20130 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
20132 + au_sbi(sb)->si_xino_brid = brid;
20135 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
20137 + return au_sbi(sb)->si_xino_brid;
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
20146 +++ b/fs/aufs/sysaufs.c
20149 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
20158 + * sysfs interface and lifetime management
20159 + * they are necessary regardless sysfs is disabled.
20162 +#include <linux/fs.h>
20163 +#include <linux/random.h>
20164 +#include <linux/sysfs.h>
20167 +unsigned long sysaufs_si_mask;
20168 +struct kset *sysaufs_ket;
20170 +#define AuSiAttr(_name) { \
20171 + .attr = { .name = __stringify(_name), .mode = 0444 }, \
20172 + .show = sysaufs_si_##_name, \
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,
20181 +static struct sysfs_ops au_sbi_ops = {
20182 + .show = sysaufs_si_show
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
20191 +/* ---------------------------------------------------------------------- */
20193 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
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));
20203 + dbgaufs_si_null(sbinfo);
20205 + err = dbgaufs_si_init(sbinfo);
20206 + if (unlikely(err))
20207 + kobject_put(&sbinfo->si_kobj);
20212 +void sysaufs_fin(void)
20215 + sysfs_remove_group(&sysaufs_ket->kobj, sysaufs_attr_group);
20216 + kset_unregister(sysaufs_ket);
20219 +int __init sysaufs_init(void)
20224 + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
20225 + } while (!sysaufs_si_mask);
20227 + sysaufs_ket = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
20228 + err = PTR_ERR(sysaufs_ket);
20229 + if (IS_ERR(sysaufs_ket))
20231 + err = sysfs_create_group(&sysaufs_ket->kobj, sysaufs_attr_group);
20232 + if (unlikely(err)) {
20233 + kset_unregister(sysaufs_ket);
20237 + err = dbgaufs_init();
20238 + if (unlikely(err))
20243 diff --git a/fs/aufs/sysaufs.h b/fs/aufs/sysaufs.h
20244 new file mode 100644
20245 index 0000000..c1202fa
20247 +++ b/fs/aufs/sysaufs.h
20250 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
20259 + * sysfs interface and mount lifetime management
20262 +#ifndef __SYSAUFS_H__
20263 +#define __SYSAUFS_H__
20267 +#include <linux/fs.h>
20268 +#include <linux/sysfs.h>
20269 +#include <linux/aufs_type.h>
20270 +#include "module.h"
20272 +struct sysaufs_si_attr {
20273 + struct attribute attr;
20274 + int (*show)(struct seq_file *seq, struct super_block *sb);
20277 +/* ---------------------------------------------------------------------- */
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);
20287 +/* ---------------------------------------------------------------------- */
20289 +/* some people doesn't like to show a pointer in kernel */
20290 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
20292 + return sysaufs_si_mask ^ (unsigned long)sbinfo;
20295 +#define SysaufsSiNamePrefix "si_"
20296 +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
20297 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
20299 + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
20300 + sysaufs_si_id(sbinfo));
20304 +#ifdef CONFIG_SYSFS
20306 +extern struct attribute_group *sysaufs_attr_group;
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,
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);
20316 +#define sysaufs_brs_init() do {} while (0)
20319 +#define sysaufs_attr_group NULL
20322 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
20328 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
20334 +static inline void sysaufs_br_init(struct au_branch *br)
20339 +static inline void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
20344 +static inline void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
20349 +static inline void sysaufs_brs_init(void)
20354 +#endif /* CONFIG_SYSFS */
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
20362 +++ b/fs/aufs/sysfs.c
20365 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
20374 + * sysfs interface
20377 +#include <linux/fs.h>
20378 +#include <linux/seq_file.h>
20379 +#include <linux/sysfs.h>
20382 +static struct attribute *au_attr[] = {
20383 + NULL, /* need to NULL terminate the list of attributes */
20386 +static struct attribute_group sysaufs_attr_group_body = {
20390 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
20392 +/* ---------------------------------------------------------------------- */
20394 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
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');
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
20411 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
20412 + aufs_bindex_t bindex)
20414 + struct path path;
20415 + struct dentry *root;
20416 + struct au_branch *br;
20418 + AuDbg("b%d\n", bindex);
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));
20431 +/* ---------------------------------------------------------------------- */
20433 +static struct seq_file *au_seq(char *p, ssize_t len)
20435 + struct seq_file *seq;
20437 + seq = kzalloc(sizeof(*seq), GFP_NOFS);
20439 + /* mutex_init(&seq.lock); */
20442 + return seq; /* success */
20445 + seq = ERR_PTR(-ENOMEM);
20449 +#define SysaufsBr_PREFIX "br"
20451 +/* todo: file size may exceed PAGE_SIZE */
20452 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
20457 + aufs_bindex_t bend;
20458 + struct au_sbinfo *sbinfo;
20459 + struct super_block *sb;
20460 + struct seq_file *seq;
20462 + struct attribute **cattr;
20464 + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
20465 + sb = sbinfo->si_sb;
20466 + si_noflush_read_lock(sb);
20468 + seq = au_seq(buf, PAGE_SIZE);
20469 + err = PTR_ERR(seq);
20473 + name = (void *)attr->name;
20474 + cattr = sysaufs_si_attrs;
20476 + if (!strcmp(name, (*cattr)->name)) {
20477 + err = container_of(*cattr, struct sysaufs_si_attr, attr)
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);
20490 + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l);
20500 + err = seq->count;
20501 + /* sysfs limit */
20502 + if (unlikely(err == PAGE_SIZE))
20507 + si_read_unlock(sb);
20511 +/* ---------------------------------------------------------------------- */
20513 +void sysaufs_br_init(struct au_branch *br)
20515 + br->br_attr.name = br->br_name;
20516 + br->br_attr.mode = S_IRUGO;
20517 + br->br_attr.owner = THIS_MODULE;
20520 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
20522 + struct au_branch *br;
20523 + struct kobject *kobj;
20524 + aufs_bindex_t bend;
20526 + dbgaufs_brs_del(sb, bindex);
20528 + if (!sysaufs_brs)
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);
20539 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
20542 + aufs_bindex_t bend;
20543 + struct kobject *kobj;
20544 + struct au_branch *br;
20546 + dbgaufs_brs_add(sb, bindex);
20548 + if (!sysaufs_brs)
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
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);
20562 diff --git a/fs/aufs/sysrq.c b/fs/aufs/sysrq.c
20563 new file mode 100644
20564 index 0000000..419ff2c
20566 +++ b/fs/aufs/sysrq.c
20569 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
20578 + * magic sysrq hanlder
20581 +#include <linux/fs.h>
20582 +#include <linux/module.h>
20583 +#include <linux/moduleparam.h>
20584 +/* #include <linux/sysrq.h> */
20587 +/* ---------------------------------------------------------------------- */
20589 +static void sysrq_sb(struct super_block *sb)
20592 + struct au_sbinfo *sbinfo;
20593 + struct file *file;
20595 + plevel = au_plevel;
20596 + au_plevel = KERN_WARNING;
20599 + sbinfo = au_sbi(sb);
20600 + pr_warning("si=%lx\n", sysaufs_si_id(sbinfo));
20601 + pr_warning(AUFS_NAME ": superblock\n");
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);
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);
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);
20619 + au_plevel = plevel;
20623 +/* ---------------------------------------------------------------------- */
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);
20630 +static void au_sysrq(int key __maybe_unused,
20631 + struct tty_struct *tty __maybe_unused)
20633 + struct kobject *kobj;
20634 + struct au_sbinfo *sbinfo;
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);
20641 + /* spin_unlock(&sysaufs_ket->list_lock); */
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
20651 +/* ---------------------------------------------------------------------- */
20653 +int __init au_sysrq_init(void)
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);
20667 +void au_sysrq_fin(void)
20670 + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
20671 + if (unlikely(err))
20672 + AuErr("err %d (ignored)\n", err);
20674 diff --git a/fs/aufs/vdir.c b/fs/aufs/vdir.c
20675 new file mode 100644
20676 index 0000000..9c7b64a
20678 +++ b/fs/aufs/vdir.c
20681 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
20690 + * virtual or vertical directory
20695 +static unsigned int calc_size(int namelen)
20698 + const int mask = sizeof(ino_t) - 1;
20700 + BUILD_BUG_ON(sizeof(ino_t) != sizeof(long));
20702 + sz = sizeof(struct au_vdir_de) + namelen;
20704 + sz += sizeof(ino_t);
20708 + AuDebugOn(sz % sizeof(ino_t));
20712 +static int set_deblk_end(union au_vdir_deblk_p *p,
20713 + union au_vdir_deblk_p *deblk_end)
20715 + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
20716 + p->de->de_str.len = 0;
20720 + return -1; /* error */
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)
20727 + if (calc_size(0) <= deblk_end->deblk - p->deblk)
20728 + return !p->de->de_str.len;
20732 +static unsigned char *last_deblk(struct au_vdir *vdir)
20734 + return vdir->vd_deblk[vdir->vd_nblk - 1];
20737 +/* ---------------------------------------------------------------------- */
20740 + * the allocated memory has to be freed by
20741 + * au_nhash_wh_free() or void au_nhash_de_free().
20743 +struct au_nhash *au_nhash_alloc(struct super_block *sb, aufs_bindex_t bend,
20746 + struct au_nhash *nhash;
20747 + struct hlist_head *head;
20748 + unsigned int u, n;
20749 + aufs_bindex_t bindex;
20751 + nhash = kmalloc(sizeof(*nhash) * (bend + 1), gfp);
20752 + if (unlikely(!nhash))
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))
20760 + nhash[bindex].nh_num = n;
20761 + nhash[bindex].nh_head = head;
20762 + for (u = 0; u < n; u++)
20763 + INIT_HLIST_HEAD(head++);
20765 + return nhash; /* success */
20768 + for (bindex--; bindex >= 0; bindex--)
20769 + kfree(nhash[bindex].nh_head);
20772 + return ERR_PTR(-ENOMEM);
20775 +static void au_nhash_wh_do_free(struct au_nhash *whlist)
20777 + unsigned int u, n;
20778 + struct hlist_head *head;
20779 + struct au_vdir_wh *tpos;
20780 + struct hlist_node *pos, *node;
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); */
20793 +void au_nhash_wh_free(struct au_nhash *whlist, aufs_bindex_t bend)
20795 + aufs_bindex_t bindex;
20797 + for (bindex = 0; bindex <= bend; bindex++) {
20798 + au_nhash_wh_do_free(whlist + bindex);
20799 + kfree(whlist[bindex].nh_head);
20805 +static void au_nhash_de_do_free(struct au_nhash *delist)
20807 + unsigned int u, n;
20808 + struct hlist_head *head;
20809 + struct au_vdir_dehstr *tpos;
20810 + struct hlist_node *pos, *node;
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);
20823 +static void au_nhash_de_free(struct au_nhash *delist, aufs_bindex_t bend)
20825 + aufs_bindex_t bindex;
20827 + for (bindex = 0; bindex <= bend; bindex++) {
20828 + au_nhash_de_do_free(delist + bindex);
20829 + kfree(delist[bindex].nh_head);
20835 +/* ---------------------------------------------------------------------- */
20837 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
20841 + unsigned int u, n;
20842 + struct hlist_head *head;
20843 + struct au_vdir_wh *tpos;
20844 + struct hlist_node *pos;
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)
20858 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
20859 + const unsigned char *name,
20860 + unsigned int len)
20862 + return nhash->nh_head + full_name_hash(name, len) % nhash->nh_num;
20865 +/* returns found or not */
20866 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int namelen)
20868 + struct hlist_head *head;
20869 + struct au_vdir_wh *tpos;
20870 + struct hlist_node *pos;
20871 + struct au_vdir_destr *str;
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))
20883 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int namelen,
20884 + aufs_bindex_t bindex)
20887 + struct au_vdir_destr *str;
20888 + struct au_vdir_wh *wh;
20891 + wh = kmalloc(sizeof(*wh) + namelen, GFP_NOFS);
20892 + if (unlikely(!wh))
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));
20907 +/* ---------------------------------------------------------------------- */
20909 +void au_vdir_free(struct au_vdir *vdir)
20911 + unsigned char **deblk;
20913 + deblk = vdir->vd_deblk;
20914 + while (vdir->vd_nblk--)
20916 + kfree(vdir->vd_deblk);
20917 + au_cache_free_vdir(vdir);
20920 +static int append_deblk(struct au_vdir *vdir)
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;
20929 + sz = sizeof(*o) * vdir->vd_nblk;
20930 + o = au_kzrealloc(vdir->vd_deblk, sz, sz + sizeof(*o), GFP_NOFS);
20931 + if (unlikely(!o))
20934 + vdir->vd_deblk = o;
20935 + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
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);
20949 +static struct au_vdir *alloc_vdir(struct super_block *sb)
20951 + struct au_vdir *vdir;
20955 + vdir = au_cache_alloc_vdir();
20956 + if (unlikely(!vdir))
20959 + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
20960 + if (unlikely(!vdir->vd_deblk))
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);
20969 + return vdir; /* success */
20971 + kfree(vdir->vd_deblk);
20974 + au_cache_free_vdir(vdir);
20976 + vdir = ERR_PTR(err);
20980 +static int reinit_vdir(struct au_vdir *vdir)
20983 + union au_vdir_deblk_p p, deblk_end;
20985 + while (vdir->vd_nblk > 1) {
20986 + kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
20987 + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
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;
21002 +/* ---------------------------------------------------------------------- */
21004 +/* returns found(true) or not */
21005 +static int test_known(struct au_nhash *delist, char *name, int namelen)
21007 + struct hlist_head *head;
21008 + struct au_vdir_dehstr *tpos;
21009 + struct hlist_node *pos;
21010 + struct au_vdir_destr *str;
21012 + head = au_name_hash(delist, name, namelen);
21013 + hlist_for_each_entry(tpos, pos, head, hash) {
21015 + AuDbg("%.*s\n", str->len, str->name);
21016 + if (str->len == namelen && !memcmp(str->name, name, namelen))
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)
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;
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));
21038 + sz = calc_size(namelen);
21039 + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
21040 + err = append_deblk(vdir);
21041 + if (unlikely(err))
21044 + p.deblk = last_deblk(vdir);
21045 + deblk_end.deblk = p.deblk + deblk_sz;
21047 + AuDebugOn(room->deblk != p.deblk);
21051 + dehstr = au_cache_alloc_dehstr();
21052 + if (unlikely(!dehstr))
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);
21063 + room->deblk += sz;
21064 + if (unlikely(set_deblk_end(room, &deblk_end)))
21065 + err = append_deblk(vdir);
21072 +/* ---------------------------------------------------------------------- */
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)
21078 + struct mutex *mtx;
21079 + const int isdir = (d_type == DT_DIR);
21081 + /* prevent hardlinks from race condition */
21084 + mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
21087 + err = au_xino_read(sb, bindex, h_ino, ino);
21088 + if (unlikely(err))
21093 + *ino = au_xino_new_ino(sb);
21094 + if (unlikely(!*ino))
21096 + err = au_xino_write(sb, bindex, h_ino, *ino);
21097 + if (unlikely(err))
21103 + mutex_unlock(mtx);
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; }
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;
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)
21127 + struct fillvdir_arg *arg = __arg;
21128 + char *name = (void *)__name;
21129 + struct super_block *sb;
21130 + struct au_nhash *delist, *whlist;
21132 + aufs_bindex_t bindex, bend;
21134 + bend = arg->bindex;
21136 + au_fset_fillvdir(arg->flags, CALLED);
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,
21146 + goto out; /* already exists or whiteouted */
21148 + sb = arg->file->f_dentry->d_sb;
21149 + arg->err = au_ino(sb, bend, h_ino, d_type, &ino);
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 */
21162 + arg->err = au_nhash_append_wh
21163 + (arg->whlist + bend, name, namelen, bend);
21168 + arg->vdir->vd_jiffy = jiffies;
21170 + AuTraceErr(arg->err);
21174 +static int au_do_read_vdir(struct fillvdir_arg *arg)
21178 + aufs_bindex_t bend, bindex;
21179 + struct file *hf, *file;
21180 + struct super_block *sb;
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))
21189 + arg->whlist = au_nhash_alloc(sb, bend, GFP_NOFS);
21190 + if (unlikely(!arg->whlist))
21195 + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
21196 + hf = au_h_fptr(file, bindex);
21200 + offset = vfsub_llseek(hf, 0, SEEK_SET);
21202 + if (unlikely(offset))
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);
21212 + au_fclr_fillvdir(arg->flags, CALLED);
21214 + err = vfsub_readdir(hf, fillvdir, arg);
21217 + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
21219 + au_nhash_wh_free(arg->whlist, bend);
21222 + au_nhash_de_free(arg->delist, bend);
21227 +static int read_vdir(struct file *file, int may_read)
21230 + unsigned long expire;
21231 + unsigned char do_read;
21232 + struct fillvdir_arg arg;
21233 + struct inode *inode;
21234 + struct au_vdir *vdir, *allocated;
21237 + inode = file->f_dentry->d_inode;
21238 + IMustLock(inode);
21239 + allocated = NULL;
21241 + expire = au_sbi(inode->i_sb)->si_rdcache;
21242 + vdir = au_ivdir(inode);
21245 + vdir = alloc_vdir(inode->i_sb);
21246 + err = PTR_ERR(vdir);
21247 + if (IS_ERR(vdir))
21250 + allocated = vdir;
21251 + } else if (may_read
21252 + && (inode->i_version != vdir->vd_version
21253 + || time_after(jiffies, vdir->vd_jiffy + expire))) {
21255 + err = reinit_vdir(vdir);
21256 + if (unlikely(err))
21261 + return 0; /* success */
21265 + err = au_do_read_vdir(&arg);
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];
21272 + au_set_ivdir(inode, allocated);
21273 + } else if (allocated)
21274 + au_vdir_free(allocated);
21280 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
21283 + unsigned long ul, n;
21284 + const unsigned int deblk_sz = src->vd_deblk_sz;
21286 + AuDebugOn(tgt->vd_nblk != 1);
21289 + if (tgt->vd_nblk < src->vd_nblk) {
21290 + unsigned char **p;
21292 + p = au_kzrealloc(tgt->vd_deblk, sizeof(*p) * tgt->vd_nblk,
21293 + sizeof(*p) * src->vd_nblk, GFP_NOFS);
21294 + if (unlikely(!p))
21296 + tgt->vd_deblk = p;
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;
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);
21316 + return 0; /* success */
21319 + rerr = reinit_vdir(tgt);
21324 +int au_vdir_init(struct file *file)
21327 + struct inode *inode;
21328 + struct au_vdir *vdir_cache, *allocated;
21330 + err = read_vdir(file, !file->f_pos);
21331 + if (unlikely(err))
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))
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))
21347 + return 0; /* success */
21349 + inode = file->f_dentry->d_inode;
21350 + err = copy_vdir(vdir_cache, au_ivdir(inode));
21352 + file->f_version = inode->i_version;
21354 + au_set_fvdir_cache(file, allocated);
21355 + } else if (allocated)
21356 + au_vdir_free(allocated);
21362 +static loff_t calc_offset(struct au_vdir *vdir)
21365 + union au_vdir_deblk_p p;
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;
21373 +/* returns true or false */
21374 +static int seek_vdir(struct file *file)
21377 + unsigned int deblk_sz;
21378 + unsigned long ul, n;
21380 + union au_vdir_deblk_p p, deblk_end;
21381 + struct au_vdir *vdir_cache;
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)
21390 + vdir_cache->vd_last.ul = 0;
21391 + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
21392 + if (!file->f_pos)
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)
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;
21407 + offset *= deblk_sz;
21408 + while (!is_deblk_end(&p, &deblk_end) && offset < file->f_pos) {
21411 + l = calc_size(p.de->de_str.len);
21415 + if (!is_deblk_end(&p, &deblk_end)) {
21417 + vdir_cache->vd_last.ul = ul;
21418 + vdir_cache->vd_last.p = p;
21425 + AuTraceErr(!valid);
21429 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir)
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;
21437 + vdir_cache = au_fvdir_cache(file);
21438 + if (!seek_vdir(file))
21441 + deblk_sz = vdir_cache->vd_deblk_sz;
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)) {
21454 + /* todo: ignore the error caused by udba? */
21455 + /* return err; */
21459 + l = calc_size(de->de_str.len);
21460 + vdir_cache->vd_last.p.deblk += l;
21461 + file->f_pos += l;
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;
21476 diff --git a/fs/aufs/vfsub.c b/fs/aufs/vfsub.c
21477 new file mode 100644
21478 index 0000000..f58d07d
21480 +++ b/fs/aufs/vfsub.c
21483 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
21492 + * sub-routines for VFS
21495 +#include <linux/uaccess.h>
21498 +int vfsub_update_h_iattr(struct path *h_path, int *did)
21502 + struct super_block *h_sb;
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 */
21510 + h_sb = h_path->dentry->d_sb;
21511 + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
21513 + err = vfs_getattr(h_path->mnt, h_path->dentry, &st);
21518 +/* ---------------------------------------------------------------------- */
21520 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
21522 + struct file *file;
21525 + file = filp_open(path, oflags, mode);
21527 + if (IS_ERR(file))
21529 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21535 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
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*/
21547 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
21550 + struct path path = {
21554 + IMustLock(parent->d_inode);
21556 + path.dentry = lookup_one_len(name, parent, len);
21557 + if (IS_ERR(path.dentry))
21559 + if (path.dentry->d_inode)
21560 + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
21563 + return path.dentry;
21566 +struct dentry *vfsub_lookup_hash(struct nameidata *nd)
21568 + struct path path = {
21569 + .mnt = nd->path.mnt
21572 + IMustLock(nd->path.dentry->d_inode);
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*/
21578 + return path.dentry;
21581 +/* ---------------------------------------------------------------------- */
21583 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
21584 + struct dentry *d2, struct au_hinode *hdir2)
21586 + struct dentry *d;
21589 + d = lock_rename(d1, d2);
21591 + au_hin_suspend(hdir1);
21592 + if (hdir1 != hdir2)
21593 + au_hin_suspend(hdir2);
21598 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
21599 + struct dentry *d2, struct au_hinode *hdir2)
21601 + au_hin_resume(hdir1);
21602 + if (hdir1 != hdir2)
21603 + au_hin_resume(hdir2);
21605 + unlock_rename(d1, d2);
21609 +/* ---------------------------------------------------------------------- */
21611 +int vfsub_create(struct inode *dir, struct path *path, int mode)
21614 + struct dentry *d;
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))
21625 + if (au_test_fs_null_nd(dir->i_sb))
21626 + err = vfs_create(dir, path->dentry, mode, NULL);
21628 + struct nameidata h_nd;
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);
21643 + struct path tmp = *path;
21646 + vfsub_update_h_iattr(&tmp, &did);
21648 + tmp.dentry = path->dentry->d_parent;
21649 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
21658 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
21661 + struct dentry *d;
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))
21672 + err = vfs_symlink(dir, path->dentry, symname);
21674 + struct path tmp = *path;
21677 + vfsub_update_h_iattr(&tmp, &did);
21679 + tmp.dentry = path->dentry->d_parent;
21680 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
21689 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
21692 + struct dentry *d;
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))
21703 + err = vfs_mknod(dir, path->dentry, mode, dev);
21705 + struct path tmp = *path;
21708 + vfsub_update_h_iattr(&tmp, &did);
21710 + tmp.dentry = path->dentry->d_parent;
21711 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
21720 +static int au_test_nlink(struct inode *inode)
21722 + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
21724 + if (!au_test_fs_no_limit_nlink(inode->i_sb)
21725 + || inode->i_nlink < link_max)
21730 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
21733 + struct dentry *d;
21737 + err = au_test_nlink(src_dentry->d_inode);
21738 + if (unlikely(err))
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))
21749 + err = vfs_link(src_dentry, dir, path->dentry);
21752 + struct path tmp = *path;
21755 + /* fuse has different memory inode for the same inumber */
21756 + vfsub_update_h_iattr(&tmp, &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);
21770 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
21771 + struct inode *dir, struct path *path)
21774 + struct path tmp = {
21777 + struct dentry *d;
21780 + IMustLock(src_dir);
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))
21791 + err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
21796 + tmp.dentry = d->d_parent;
21797 + vfsub_update_h_iattr(&tmp, &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);
21811 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
21814 + struct dentry *d;
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))
21825 + err = vfs_mkdir(dir, path->dentry, mode);
21827 + struct path tmp = *path;
21830 + vfsub_update_h_iattr(&tmp, &did);
21832 + tmp.dentry = path->dentry->d_parent;
21833 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
21842 +int vfsub_rmdir(struct inode *dir, struct path *path)
21845 + struct dentry *d;
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))
21857 + err = vfs_rmdir(dir, path->dentry);
21860 + struct path tmp = {
21861 + .dentry = path->dentry->d_parent,
21865 + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
21872 +/* ---------------------------------------------------------------------- */
21874 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
21879 + err = vfs_read(file, ubuf, count, ppos);
21881 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21885 +/* todo: kernel_read()? */
21886 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
21890 + mm_segment_t oldfs;
21892 + oldfs = get_fs();
21893 + set_fs(KERNEL_DS);
21894 + err = vfsub_read_u(file, (char __user *)kbuf, count, ppos);
21899 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
21905 + err = vfs_write(file, ubuf, count, ppos);
21908 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21912 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
21915 + mm_segment_t oldfs;
21917 + oldfs = get_fs();
21918 + set_fs(KERNEL_DS);
21919 + err = vfsub_write_u(file, (const char __user *)kbuf, count, ppos);
21924 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
21929 + err = vfs_readdir(file, filldir, arg);
21932 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
21936 +long vfsub_splice_to(struct file *in, loff_t *ppos,
21937 + struct pipe_inode_info *pipe, size_t len,
21938 + unsigned int flags)
21943 + err = do_splice_to(in, ppos, pipe, len, flags);
21946 + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
21950 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
21951 + loff_t *ppos, size_t len, unsigned int flags)
21956 + err = do_splice_from(pipe, out, ppos, len, flags);
21959 + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
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)
21968 + struct inode *h_inode;
21970 + h_inode = h_path->dentry->d_inode;
21972 + err = mnt_want_write(h_path->mnt);
21975 + err = inode_permission(h_inode, MAY_WRITE);
21978 + err = get_write_access(h_inode);
21981 + err = break_lease(h_inode, vfsub_fmode_to_uint(FMODE_WRITE));
21986 + err = locks_verify_truncate(h_inode, h_file, length);
21988 + err = security_path_truncate(h_path, length, attr);
21991 + err = do_truncate(h_path->dentry, length, attr, h_file);
21997 + put_write_access(h_inode);
22000 + mnt_drop_write(h_path->mnt);
22005 +/* ---------------------------------------------------------------------- */
22007 +struct au_vfsub_mkdir_args {
22009 + struct inode *dir;
22010 + struct path *path;
22014 +static void au_call_vfsub_mkdir(void *args)
22016 + struct au_vfsub_mkdir_args *a = args;
22017 + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
22020 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
22022 + int err, do_sio, wkq_err;
22024 + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
22026 + err = vfsub_mkdir(dir, path, mode);
22028 + struct au_vfsub_mkdir_args args = {
22034 + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
22035 + if (unlikely(wkq_err))
22042 +struct au_vfsub_rmdir_args {
22044 + struct inode *dir;
22045 + struct path *path;
22048 +static void au_call_vfsub_rmdir(void *args)
22050 + struct au_vfsub_rmdir_args *a = args;
22051 + *a->errp = vfsub_rmdir(a->dir, a->path);
22054 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
22056 + int err, do_sio, wkq_err;
22058 + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
22060 + err = vfsub_rmdir(dir, path);
22062 + struct au_vfsub_rmdir_args args = {
22067 + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
22068 + if (unlikely(wkq_err))
22075 +/* ---------------------------------------------------------------------- */
22077 +struct notify_change_args {
22079 + struct path *path;
22080 + struct iattr *ia;
22083 +static void call_notify_change(void *args)
22085 + struct notify_change_args *a = args;
22086 + struct inode *h_inode;
22088 + h_inode = a->path->dentry->d_inode;
22089 + IMustLock(h_inode);
22091 + *a->errp = -EPERM;
22092 + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
22094 + *a->errp = notify_change(a->path->dentry, a->ia);
22097 + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
22099 + AuTraceErr(*a->errp);
22102 +int vfsub_notify_change(struct path *path, struct iattr *ia)
22105 + struct notify_change_args args = {
22111 + call_notify_change(&args);
22116 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
22118 + int err, wkq_err;
22119 + struct notify_change_args args = {
22125 + wkq_err = au_wkq_wait(call_notify_change, &args);
22126 + if (unlikely(wkq_err))
22132 +/* ---------------------------------------------------------------------- */
22134 +struct unlink_args {
22136 + struct inode *dir;
22137 + struct path *path;
22140 +static void call_unlink(void *args)
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);
22148 + IMustLock(a->dir);
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))
22156 + if (!stop_sillyrename)
22158 + h_inode = d->d_inode;
22160 + atomic_inc(&h_inode->i_count);
22163 + *a->errp = vfs_unlink(a->dir, d);
22166 + struct path tmp = {
22167 + .dentry = d->d_parent,
22168 + .mnt = a->path->mnt
22170 + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
22173 + if (!stop_sillyrename)
22178 + AuTraceErr(*a->errp);
22182 + * @dir: must be locked.
22183 + * @dentry: target dentry.
22185 +int vfsub_unlink(struct inode *dir, struct path *path, int force)
22188 + struct unlink_args args = {
22195 + call_unlink(&args);
22199 + wkq_err = au_wkq_wait(call_unlink, &args);
22200 + if (unlikely(wkq_err))
22206 diff --git a/fs/aufs/vfsub.h b/fs/aufs/vfsub.h
22207 new file mode 100644
22208 index 0000000..d0172c7
22210 +++ b/fs/aufs/vfsub.h
22213 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
22222 + * sub-routines for VFS
22225 +#ifndef __AUFS_VFSUB_H__
22226 +#define __AUFS_VFSUB_H__
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>
22236 +/* ---------------------------------------------------------------------- */
22238 +/* lock subclass for lower inode */
22239 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
22240 +/* reduce? gave up. */
22242 + AuLsc_I_Begin = I_MUTEX_QUOTA, /* 4 */
22243 + AuLsc_I_PARENT, /* lower inode, parent first */
22244 + AuLsc_I_PARENT2, /* copyup dirs */
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)
22254 +/* ---------------------------------------------------------------------- */
22256 +static inline void vfsub_copy_inode_size(struct inode *inode,
22257 + struct inode *h_inode)
22259 + spin_lock(&inode->i_lock);
22260 + fsstack_copy_inode_size(inode, h_inode);
22261 + spin_unlock(&inode->i_lock);
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,
22269 +struct dentry *vfsub_lookup_hash(struct nameidata *nd);
22271 +/* ---------------------------------------------------------------------- */
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);
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);
22290 +/* ---------------------------------------------------------------------- */
22292 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
22294 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
22296 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
22298 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
22300 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg);
22302 +static inline void vfsub_file_accessed(struct file *h_file)
22304 + file_accessed(h_file);
22305 + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
22308 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
22309 + struct dentry *h_dentry)
22311 + struct path h_path = {
22312 + .dentry = h_dentry,
22315 + touch_atime(h_mnt, h_dentry);
22316 + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
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);
22327 +/* ---------------------------------------------------------------------- */
22329 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
22334 + err = vfs_llseek(file, offset, origin);
22339 +/* ---------------------------------------------------------------------- */
22341 +/* dirty workaround for strict type of fmode_t */
22347 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
22349 + union vfsub_fmu u = {
22353 + BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
22358 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
22360 + union vfsub_fmu u = {
22367 +/* ---------------------------------------------------------------------- */
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);
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
22381 +++ b/fs/aufs/wbr_policy.c
22384 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
22393 + * policies for selecting one among multiple writable branches
22396 +#include <linux/statfs.h>
22399 +/* subset of cpup_attr() */
22400 +static noinline_for_stack
22401 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
22405 + struct inode *h_isrc;
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);
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);
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; }
22434 +struct au_cpdown_dir_args {
22435 + struct dentry *parent;
22436 + unsigned int flags;
22439 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
22440 + struct au_cpdown_dir_args *a)
22443 + struct dentry *opq_dentry;
22445 + opq_dentry = au_diropq_create(dentry, bdst);
22446 + err = PTR_ERR(opq_dentry);
22447 + if (IS_ERR(opq_dentry))
22449 + dput(opq_dentry);
22450 + au_fset_cpdown(a->flags, DIROPQ);
22456 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
22457 + struct inode *dir, aufs_bindex_t bdst)
22460 + struct path h_path;
22461 + struct au_branch *br;
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))
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,
22475 + dput(h_path.dentry);
22481 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
22482 + struct dentry *h_parent, void *arg)
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;
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);
22500 + err = au_lkup_neg(dentry, bdst);
22501 + if (unlikely(err < 0))
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))
22509 + au_fset_cpdown(args->flags, MADE_DIR);
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);
22531 + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
22532 + mutex_unlock(&h_inode->i_mutex);
22533 + if (unlikely(err))
22536 + if (au_ftest_cpdown(args->flags, WHED)) {
22537 + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
22538 + if (unlikely(err))
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 */
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);
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);
22572 + au_set_h_dptr(dentry, bdst, NULL);
22573 + if (au_dbend(dentry) == bdst)
22574 + au_update_dbend(dentry);
22580 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
22583 + struct au_cpdown_dir_args args = {
22584 + .parent = dget_parent(dentry),
22588 + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &args);
22589 + dput(args.parent);
22594 +/* ---------------------------------------------------------------------- */
22596 +/* policies for create */
22598 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
22600 + for (; bindex >= 0; bindex--)
22601 + if (!au_br_rdonly(au_sbr(sb, bindex)))
22606 +/* top down parent */
22607 +static int au_wbr_create_tdp(struct dentry *dentry, int isdir __maybe_unused)
22610 + aufs_bindex_t bstart, bindex;
22611 + struct super_block *sb;
22612 + struct dentry *parent, *h_parent;
22614 + sb = dentry->d_sb;
22615 + bstart = au_dbstart(dentry);
22617 + if (!au_br_rdonly(au_sbr(sb, bstart)))
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)
22627 + if (!au_br_rdonly(au_sbr(sb, bindex))) {
22634 + /* bottom up here */
22635 + if (unlikely(err < 0))
22636 + err = au_wbr_bu(sb, bstart - 1);
22639 + AuDbg("b%d\n", err);
22643 +/* ---------------------------------------------------------------------- */
22645 +/* an exception for the policy other than tdp */
22646 +static int au_wbr_create_exp(struct dentry *dentry)
22649 + aufs_bindex_t bwh, bdiropq;
22650 + struct dentry *parent;
22653 + bwh = au_dbwh(dentry);
22654 + parent = dget_parent(dentry);
22655 + bdiropq = au_dbdiropq(parent);
22657 + if (bdiropq >= 0)
22658 + err = min(bdiropq, bwh);
22661 + AuDbg("%d\n", err);
22662 + } else if (bdiropq >= 0) {
22664 + AuDbg("%d\n", err);
22668 + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
22671 + AuDbg("%d\n", err);
22675 +/* ---------------------------------------------------------------------- */
22678 +static int au_wbr_create_init_rr(struct super_block *sb)
22682 + err = au_wbr_bu(sb, au_sbend(sb));
22683 + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
22685 + AuDbg("b%d\n", err);
22689 +static int au_wbr_create_rr(struct dentry *dentry, int isdir)
22693 + aufs_bindex_t bindex, bend;
22694 + struct super_block *sb;
22697 + err = au_wbr_create_exp(dentry);
22701 + sb = dentry->d_sb;
22702 + next = &au_sbi(sb)->si_wbr_rr_next;
22703 + bend = au_sbend(sb);
22705 + for (bindex = 0; bindex <= bend; bindex++) {
22707 + err = atomic_dec_return(next) + 1;
22708 + /* modulo for 0 is meaningless */
22709 + if (unlikely(!err))
22710 + err = atomic_dec_return(next) + 1;
22712 + err = atomic_read(next);
22713 + AuDbg("%d\n", err);
22716 + AuDbg("%d\n", err);
22717 + if (!au_br_rdonly(au_sbr(sb, err)))
22723 + AuDbg("%d\n", err);
22727 +/* ---------------------------------------------------------------------- */
22729 +/* most free space */
22730 +static void au_mfs(struct dentry *dentry)
22732 + struct super_block *sb;
22733 + struct au_branch *br;
22734 + struct au_wbr_mfs *mfs;
22735 + aufs_bindex_t bindex, bend;
22737 + unsigned long long b, bavail;
22738 + /* reduce the stack usage */
22739 + struct kstatfs *st;
22741 + st = kmalloc(sizeof(*st), GFP_NOFS);
22742 + if (unlikely(!st)) {
22743 + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
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))
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);
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) {
22772 + mfs->mfs_bindex = bindex;
22773 + mfs->mfs_jiffy = jiffies;
22777 + mfs->mfsrr_bytes = bavail;
22778 + AuDbg("b%d\n", mfs->mfs_bindex);
22782 +static int au_wbr_create_mfs(struct dentry *dentry, int isdir __maybe_unused)
22785 + struct super_block *sb;
22786 + struct au_wbr_mfs *mfs;
22788 + err = au_wbr_create_exp(dentry);
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)))
22799 + mutex_unlock(&mfs->mfs_lock);
22800 + err = mfs->mfs_bindex;
22803 + AuDbg("b%d\n", err);
22807 +static int au_wbr_create_init_mfs(struct super_block *sb)
22809 + struct au_wbr_mfs *mfs;
22811 + mfs = &au_sbi(sb)->si_wbr_mfs;
22812 + mutex_init(&mfs->mfs_lock);
22813 + mfs->mfs_jiffy = 0;
22814 + mfs->mfs_bindex = -EROFS;
22819 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
22821 + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
22825 +/* ---------------------------------------------------------------------- */
22827 +/* most free space and then round robin */
22828 +static int au_wbr_create_mfsrr(struct dentry *dentry, int isdir)
22831 + struct au_wbr_mfs *mfs;
22833 + err = au_wbr_create_mfs(dentry, isdir);
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);
22840 + AuDbg("b%d\n", err);
22844 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
22848 + au_wbr_create_init_mfs(sb); /* ignore */
22849 + err = au_wbr_create_init_rr(sb);
22854 +/* ---------------------------------------------------------------------- */
22856 +/* top down parent and most free space */
22857 +static int au_wbr_create_pmfs(struct dentry *dentry, int isdir)
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;
22866 + err = au_wbr_create_tdp(dentry, isdir);
22867 + if (unlikely(err < 0))
22869 + parent = dget_parent(dentry);
22870 + bstart = au_dbstart(parent);
22871 + bend = au_dbtaildir(parent);
22872 + if (bstart == bend)
22873 + goto out_parent; /* success */
22875 + e2 = au_wbr_create_mfs(dentry, isdir);
22877 + goto out_parent; /* success */
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);
22885 + for (bindex = bstart; bindex <= bend; bindex++) {
22886 + h_parent = au_h_dptr(parent, bindex);
22887 + if (!h_parent || !h_parent->d_inode)
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;
22894 + AuDbg("b%d, %llu\n", err, b);
22901 + AuDbg("b%d\n", err);
22905 +/* ---------------------------------------------------------------------- */
22907 +/* policies for copyup */
22909 +/* top down parent */
22910 +static int au_wbr_copyup_tdp(struct dentry *dentry)
22912 + return au_wbr_create_tdp(dentry, /*isdir, anything is ok*/0);
22915 +/* bottom up parent */
22916 +static int au_wbr_copyup_bup(struct dentry *dentry)
22919 + aufs_bindex_t bindex, bstart;
22920 + struct dentry *parent, *h_parent;
22921 + struct super_block *sb;
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)
22932 + if (!au_br_rdonly(au_sbr(sb, bindex))) {
22939 + /* bottom up here */
22940 + if (unlikely(err < 0))
22941 + err = au_wbr_bu(sb, bstart - 1);
22943 + AuDbg("b%d\n", err);
22948 +static int au_wbr_copyup_bu(struct dentry *dentry)
22952 + err = au_wbr_bu(dentry->d_sb, au_dbstart(dentry));
22954 + AuDbg("b%d\n", err);
22958 +/* ---------------------------------------------------------------------- */
22960 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
22961 + [AuWbrCopyup_TDP] = {
22962 + .copyup = au_wbr_copyup_tdp
22964 + [AuWbrCopyup_BUP] = {
22965 + .copyup = au_wbr_copyup_bup
22967 + [AuWbrCopyup_BU] = {
22968 + .copyup = au_wbr_copyup_bu
22972 +struct au_wbr_create_operations au_wbr_create_ops[] = {
22973 + [AuWbrCreate_TDP] = {
22974 + .create = au_wbr_create_tdp
22976 + [AuWbrCreate_RR] = {
22977 + .create = au_wbr_create_rr,
22978 + .init = au_wbr_create_init_rr
22980 + [AuWbrCreate_MFS] = {
22981 + .create = au_wbr_create_mfs,
22982 + .init = au_wbr_create_init_mfs,
22983 + .fin = au_wbr_create_fin_mfs
22985 + [AuWbrCreate_MFSV] = {
22986 + .create = au_wbr_create_mfs,
22987 + .init = au_wbr_create_init_mfs,
22988 + .fin = au_wbr_create_fin_mfs
22990 + [AuWbrCreate_MFSRR] = {
22991 + .create = au_wbr_create_mfsrr,
22992 + .init = au_wbr_create_init_mfsrr,
22993 + .fin = au_wbr_create_fin_mfs
22995 + [AuWbrCreate_MFSRRV] = {
22996 + .create = au_wbr_create_mfsrr,
22997 + .init = au_wbr_create_init_mfsrr,
22998 + .fin = au_wbr_create_fin_mfs
23000 + [AuWbrCreate_PMFS] = {
23001 + .create = au_wbr_create_pmfs,
23002 + .init = au_wbr_create_init_mfs,
23003 + .fin = au_wbr_create_fin_mfs
23005 + [AuWbrCreate_PMFSV] = {
23006 + .create = au_wbr_create_pmfs,
23007 + .init = au_wbr_create_init_mfs,
23008 + .fin = au_wbr_create_fin_mfs
23011 diff --git a/fs/aufs/whout.c b/fs/aufs/whout.c
23012 new file mode 100644
23013 index 0000000..a45e126
23015 +++ b/fs/aufs/whout.c
23018 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
23027 + * whiteout for logical deletion and opaque directory
23030 +#include <linux/fs.h>
23033 +#define WH_MASK S_IRUGO
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.
23039 +static struct qstr diropq_name = {
23040 + .name = AUFS_WH_DIROPQ,
23041 + .len = sizeof(AUFS_WH_DIROPQ) - 1
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().
23052 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
23056 + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
23057 + return -ENAMETOOLONG;
23059 + wh->len = name->len + AUFS_WH_PFX_LEN;
23060 + p = kmalloc(wh->len, GFP_NOFS);
23063 + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
23064 + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
23071 +/* ---------------------------------------------------------------------- */
23074 + * test if the @wh_name exists under @h_parent.
23075 + * @try_sio specifies the necessary of super-io.
23077 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
23078 + struct au_branch *br, int try_sio)
23081 + struct dentry *wh_dentry;
23082 + struct inode *h_dir;
23084 + h_dir = h_parent->d_inode;
23086 + wh_dentry = au_lkup_one(wh_name, h_parent, br, /*nd*/NULL);
23088 + wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
23089 + err = PTR_ERR(wh_dentry);
23090 + if (IS_ERR(wh_dentry))
23094 + if (!wh_dentry->d_inode)
23095 + goto out_wh; /* success */
23098 + if (S_ISREG(wh_dentry->d_inode->i_mode))
23099 + goto out_wh; /* success */
23102 + AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
23103 + AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
23112 + * test if the @h_dentry sets opaque or not.
23114 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
23117 + struct inode *h_dir;
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));
23126 + * returns a negative dentry whose name is unique and temporary.
23128 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
23129 + struct qstr *prefix)
23132 + struct dentry *dentry;
23134 + char defname[AUFS_WH_PFX_LEN * 2 + DNAME_INLINE_LEN_MIN + 1
23135 + + HEX_LEN + 1], *name, *p;
23136 + static unsigned short cnt;
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))
23145 + dentry = ERR_PTR(-ENOMEM);
23146 + name = kmalloc(qs.len + 1, GFP_NOFS);
23147 + if (unlikely(!name))
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;
23157 + AuDebugOn(name + qs.len + 1 - p <= HEX_LEN);
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)
23167 + /* AuWarn("could not get random name\n"); */
23168 + dentry = ERR_PTR(-EEXIST);
23169 + AuDbg("%.*s\n", AuLNPair(&qs));
23173 + if (name != defname)
23181 + * rename the @h_dentry on @br to the whiteouted temporary name.
23183 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
23186 + struct path h_path = {
23187 + .mnt = br->br_mnt
23189 + struct inode *h_dir;
23190 + struct dentry *h_parent;
23192 + h_parent = h_dentry->d_parent; /* dir inode is locked */
23193 + h_dir = h_parent->d_inode;
23194 + IMustLock(h_dir);
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))
23201 + /* under the same dir, no need to lock_rename() */
23202 + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
23204 + dput(h_path.dentry);
23210 +/* ---------------------------------------------------------------------- */
23212 + * functions for removing a whiteout
23215 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
23220 + * forces superio when the dir has a sticky bit.
23221 + * this may be a violation of unix fs semantics.
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);
23228 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
23229 + struct dentry *dentry)
23233 + err = do_unlink_wh(h_dir, h_path);
23234 + if (!err && dentry)
23235 + au_set_dbwh(dentry, -1);
23240 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
23241 + struct au_branch *br)
23244 + struct path h_path = {
23245 + .mnt = br->br_mnt
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);
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);
23262 +/* ---------------------------------------------------------------------- */
23264 + * initialize/clean whiteout for a branch
23267 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
23272 + if (!whpath->dentry->d_inode)
23275 + err = mnt_want_write(whpath->mnt);
23278 + err = vfsub_rmdir(h_dir, whpath);
23280 + err = vfsub_unlink(h_dir, whpath, /*force*/0);
23281 + mnt_drop_write(whpath->mnt);
23283 + if (unlikely(err))
23284 + AuWarn("failed removing %.*s (%d), ignored.\n",
23285 + AuDLNPair(whpath->dentry), err);
23288 +static int test_linkable(struct dentry *h_root)
23290 + struct inode *h_dir = h_root->d_inode;
23292 + if (h_dir->i_op->link)
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));
23300 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
23301 +static int au_whdir(struct inode *h_dir, struct path *path)
23306 + if (!path->dentry->d_inode) {
23307 + int mode = S_IRWXU;
23309 + if (au_test_nfs(path->dentry->d_sb))
23311 + err = mnt_want_write(path->mnt);
23313 + err = vfsub_mkdir(h_dir, path, mode);
23314 + mnt_drop_write(path->mnt);
23316 + } else if (S_ISDIR(path->dentry->d_inode->i_mode))
23319 + AuErr("unknown %.*s exists\n", AuDLNPair(path->dentry));
23324 +struct au_wh_base {
23325 + const struct qstr *name;
23326 + struct dentry *dentry;
23329 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
23330 + struct path *h_path)
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);
23341 + * returns tri-state,
23342 + * minus: error, caller should print the mesage
23344 + * plus: error, caller should NOT print the mesage
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)
23351 + struct inode *h_dir;
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;
23358 + err = test_linkable(h_root);
23359 + if (unlikely(err)) {
23364 + err = au_whdir(h_dir, h_path);
23365 + if (unlikely(err))
23367 + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
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))
23374 + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
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
23386 + * returns tri-state, see above.
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)
23393 + struct inode *h_dir;
23395 + err = test_linkable(h_root);
23396 + if (unlikely(err)) {
23402 + * todo: should this create be done in /sbin/mount.aufs helper?
23405 + h_dir = h_root->d_inode;
23406 + if (!base[AuBrWh_BASE].dentry->d_inode) {
23407 + err = mnt_want_write(h_path->mnt);
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);
23413 + } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
23416 + AuErr("unknown %.*s/%.*s exists\n",
23417 + AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
23418 + if (unlikely(err))
23421 + h_path->dentry = base[AuBrWh_PLINK].dentry;
23423 + err = au_whdir(h_dir, h_path);
23424 + if (unlikely(err))
23426 + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
23428 + au_wh_clean(h_dir, h_path, /*isdir*/1);
23429 + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
23431 + h_path->dentry = base[AuBrWh_ORPH].dentry;
23432 + err = au_whdir(h_dir, h_path);
23433 + if (unlikely(err))
23435 + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
23442 + * initialize the whiteout base file/dir for @br.
23444 +int au_wh_init(struct dentry *h_root, struct au_branch *br,
23445 + struct super_block *sb)
23448 + const unsigned char do_plink
23449 + = !!au_opt_test(au_mntflags(sb), PLINK);
23450 + struct path path = {
23451 + .mnt = br->br_mnt
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
23460 + [AuBrWh_PLINK] = {
23461 + .name = AUFS_PLINKDIR_NAME,
23462 + .len = sizeof(AUFS_PLINKDIR_NAME) - 1
23464 + [AuBrWh_ORPH] = {
23465 + .name = AUFS_ORPHDIR_NAME,
23466 + .len = sizeof(AUFS_ORPHDIR_NAME) - 1
23469 + struct au_wh_base base[] = {
23470 + [AuBrWh_BASE] = {
23471 + .name = base_name + AuBrWh_BASE,
23474 + [AuBrWh_PLINK] = {
23475 + .name = base_name + AuBrWh_PLINK,
23478 + [AuBrWh_ORPH] = {
23479 + .name = base_name + AuBrWh_ORPH,
23485 + h_dir = h_root->d_inode;
23486 + for (i = 0; i < AuBrWh_Last; i++) {
23487 + /* doubly whiteouted */
23488 + struct dentry *d;
23490 + d = au_wh_lkup(h_root, (void *)base[i].name, br);
23491 + err = PTR_ERR(d);
23495 + base[i].dentry = d;
23497 + && wbr->wbr_wh[i]
23498 + && wbr->wbr_wh[i] != base[i].dentry);
23502 + for (i = 0; i < AuBrWh_Last; i++) {
23503 + dput(wbr->wbr_wh[i]);
23504 + wbr->wbr_wh[i] = NULL;
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);
23517 + case AuBrPerm_RWNoLinkWH:
23518 + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
23525 + case AuBrPerm_RW:
23526 + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
23536 + goto out; /* success */
23539 + AuErr("an error(%d) on the writable branch %.*s(%s)\n",
23540 + err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
23542 + for (i = 0; i < AuBrWh_Last; i++)
23543 + dput(base[i].dentry);
23547 +/* ---------------------------------------------------------------------- */
23549 + * whiteouts are all hard-linked usually.
23550 + * when its link count reaches a ceiling, we create a new whiteout base
23551 + * asynchronously.
23554 +struct reinit_br_wh {
23555 + struct super_block *sb;
23556 + struct au_branch *br;
23559 +static void reinit_br_wh(void *arg)
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;
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))
23576 + bindex = au_br_index(a->sb, a->br->br_id);
23577 + if (unlikely(bindex < 0))
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);
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,
23590 + err = mnt_want_write(a->br->br_mnt);
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);
23598 + AuWarn("%.*s is moved, ignored\n", AuDLNPair(wbr->wbr_whbase));
23601 + dput(wbr->wbr_whbase);
23602 + wbr->wbr_whbase = NULL;
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); */
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);
23616 + if (unlikely(err))
23617 + AuIOErr("err %d\n", err);
23620 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
23622 + int do_dec, wkq_err;
23623 + struct reinit_br_wh *arg;
23626 + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
23629 + /* ignore ENOMEM */
23630 + arg = kmalloc(sizeof(*arg), GFP_NOFS);
23633 + * dec(wh_running), kfree(arg) and dec(br_count)
23634 + * in reinit function
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);
23650 + atomic_dec(&br->br_wbr->wbr_wh_running);
23653 +/* ---------------------------------------------------------------------- */
23656 + * create the whiteout @wh.
23658 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
23659 + struct dentry *wh)
23662 + struct path h_path = {
23665 + struct au_branch *br;
23666 + struct au_wbr *wbr;
23667 + struct dentry *h_parent;
23668 + struct inode *h_dir;
23670 + h_parent = wh->d_parent; /* dir inode is locked */
23671 + h_dir = h_parent->d_inode;
23672 + IMustLock(h_dir);
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)
23683 + /* link count full. re-initialize br_whbase. */
23684 + kick_reinit_br_wh(sb, br);
23687 + /* return this error in this context */
23688 + err = vfsub_create(h_dir, &h_path, WH_MASK);
23691 + wbr_wh_read_unlock(wbr);
23695 +/* ---------------------------------------------------------------------- */
23698 + * create or remove the diropq.
23700 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
23701 + unsigned int flags)
23703 + struct dentry *opq_dentry, *h_dentry;
23704 + struct super_block *sb;
23705 + struct au_branch *br;
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))
23715 + if (au_ftest_diropq(flags, CREATE)) {
23716 + err = link_or_create_wh(sb, bindex, opq_dentry);
23718 + au_set_dbdiropq(dentry, bindex);
23719 + goto out; /* success */
23722 + struct path tmp = {
23723 + .dentry = opq_dentry,
23724 + .mnt = br->br_mnt
23726 + err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
23728 + au_set_dbdiropq(dentry, -1);
23730 + dput(opq_dentry);
23731 + opq_dentry = ERR_PTR(err);
23734 + return opq_dentry;
23737 +struct do_diropq_args {
23738 + struct dentry **errp;
23739 + struct dentry *dentry;
23740 + aufs_bindex_t bindex;
23741 + unsigned int flags;
23744 +static void call_do_diropq(void *args)
23746 + struct do_diropq_args *a = args;
23747 + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
23750 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
23751 + unsigned int flags)
23753 + struct dentry *diropq, *h_dentry;
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);
23760 + struct do_diropq_args args = {
23762 + .dentry = dentry,
23763 + .bindex = bindex,
23767 + wkq_err = au_wkq_wait(call_do_diropq, &args);
23768 + if (unlikely(wkq_err))
23769 + diropq = ERR_PTR(wkq_err);
23775 +/* ---------------------------------------------------------------------- */
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.
23783 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
23784 + struct au_branch *br)
23787 + struct qstr wh_name;
23788 + struct dentry *wh_dentry;
23790 + err = au_wh_name_alloc(&wh_name, base_name);
23791 + wh_dentry = ERR_PTR(err);
23793 + wh_dentry = au_lkup_one(&wh_name, h_parent, br, /*nd*/NULL);
23794 + kfree(wh_name.name);
23796 + return wh_dentry;
23800 + * link/create a whiteout for @dentry on @bindex.
23802 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
23803 + struct dentry *h_parent)
23805 + struct dentry *wh_dentry;
23806 + struct super_block *sb;
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);
23814 + au_set_dbwh(dentry, bindex);
23817 + wh_dentry = ERR_PTR(err);
23821 + return wh_dentry;
23824 +/* ---------------------------------------------------------------------- */
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)
23831 + unsigned long ul, n;
23832 + struct qstr wh_name;
23834 + struct hlist_head *head;
23835 + struct au_vdir_wh *tpos;
23836 + struct hlist_node *pos;
23837 + struct au_vdir_destr *str;
23841 + wh_name.name = p;
23842 + if (unlikely(!wh_name.name))
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)
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);
23864 + AuIOErr("whiteout name too long %.*s\n",
23865 + str->len, str->name);
23870 + __putname(wh_name.name);
23876 +struct del_wh_children_args {
23878 + struct dentry *h_dentry;
23879 + struct au_nhash *whlist;
23880 + aufs_bindex_t bindex;
23881 + struct au_branch *br;
23884 +static void call_del_wh_children(void *args)
23886 + struct del_wh_children_args *a = args;
23887 + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
23890 +/* ---------------------------------------------------------------------- */
23892 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
23894 + struct au_whtmp_rmdir *whtmp;
23896 + whtmp = kmalloc(sizeof(*whtmp), gfp);
23897 + if (unlikely(!whtmp))
23900 + whtmp->dir = NULL;
23901 + whtmp->wh_dentry = NULL;
23902 + whtmp->whlist = au_nhash_alloc(sb, /*bend*/0, gfp);
23903 + if (unlikely(!whtmp->whlist))
23905 + return whtmp; /* success */
23910 + return ERR_PTR(-ENOMEM);
23913 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
23915 + dput(whtmp->wh_dentry);
23916 + iput(whtmp->dir);
23917 + au_nhash_wh_free(whtmp->whlist, /*bend*/0);
23922 + * rmdir the whiteouted temporary named dir @h_dentry.
23923 + * @whlist: whiteouted children.
23925 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
23926 + struct dentry *wh_dentry, struct au_nhash *whlist)
23929 + struct path h_tmp;
23930 + struct inode *wh_inode, *h_dir;
23931 + struct au_branch *br;
23933 + h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
23934 + IMustLock(h_dir);
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);
23941 + * someone else might change some whiteouts while we were sleeping.
23942 + * it means this whlist may have an obsoleted entry.
23944 + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
23945 + err = del_wh_children(wh_dentry, whlist, bindex, br);
23948 + struct del_wh_children_args args = {
23950 + .h_dentry = wh_dentry,
23951 + .whlist = whlist,
23952 + .bindex = bindex,
23956 + wkq_err = au_wkq_wait(call_del_wh_children, &args);
23957 + if (unlikely(wkq_err))
23960 + mutex_unlock(&wh_inode->i_mutex);
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); */
23970 + if (au_ibstart(dir) == bindex) {
23971 + au_cpup_attr_timesizes(dir);
23974 + return 0; /* success */
23977 + AuWarn("failed removing %.*s(%d), ignored\n",
23978 + AuDLNPair(wh_dentry), err);
23982 +static void call_rmdir_whtmp(void *args)
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;
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))
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);
24009 + err = mnt_want_write(br->br_mnt);
24011 + err = au_whtmp_rmdir(a->dir, a->bindex, a->wh_dentry,
24013 + mnt_drop_write(br->br_mnt);
24016 + au_hin_imtx_unlock(hdir);
24018 + ii_write_unlock(a->dir);
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);
24029 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
24030 + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
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);
24047 diff --git a/fs/aufs/whout.h b/fs/aufs/whout.h
24048 new file mode 100644
24049 index 0000000..3247a4f
24051 +++ b/fs/aufs/whout.h
24054 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
24063 + * whiteout for logical deletion and opaque directory
24066 +#ifndef __AUFS_WHOUT_H__
24067 +#define __AUFS_WHOUT_H__
24071 +#include <linux/fs.h>
24072 +#include <linux/aufs_type.h>
24076 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
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);
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; }
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);
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;
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);
24117 +/* ---------------------------------------------------------------------- */
24119 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
24120 + aufs_bindex_t bindex)
24122 + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
24125 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
24127 + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
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
24136 +++ b/fs/aufs/wkq.c
24139 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
24148 + * workqueue for asynchronous/super-io operations
24149 + * todo: try new dredential scheme
24154 +/* internal workqueue named AUFS_WKQ_NAME */
24155 +static struct au_wkq {
24156 + struct workqueue_struct *q;
24162 +struct au_wkinfo {
24163 + struct work_struct wk;
24164 + struct super_block *sb;
24166 + unsigned int flags; /* see wkq.h */
24168 + au_wkq_func_t func;
24172 + struct completion *comp;
24175 +/* ---------------------------------------------------------------------- */
24177 +static int enqueue(struct au_wkq *wkq, struct au_wkinfo *wkinfo)
24179 + wkinfo->busyp = &wkq->busy;
24180 + if (au_ftest_wkq(wkinfo->flags, WAIT))
24181 + return !queue_work(wkq->q, &wkinfo->wk);
24183 + return !schedule_work(&wkinfo->wk);
24186 +static void do_wkq(struct au_wkinfo *wkinfo)
24188 + unsigned int idle, n;
24192 + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
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 */
24204 + atomic_dec(&au_wkq[i].busy);
24207 + idle_idx = aufs_nwkq;
24209 + atomic_inc(&au_wkq[idle_idx].busy);
24210 + if (!enqueue(au_wkq + idle_idx, wkinfo))
24211 + return; /* success */
24213 + /* impossible? */
24214 + AuWarn1("failed to queue_work()\n");
24219 +static void wkq_func(struct work_struct *wk)
24221 + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
24223 + wkinfo->func(wkinfo->args);
24224 + atomic_dec(wkinfo->busyp);
24225 + if (au_ftest_wkq(wkinfo->flags, WAIT))
24226 + complete(wkinfo->comp);
24228 + kobject_put(&au_sbi(wkinfo->sb)->si_kobj);
24229 + module_put(THIS_MODULE);
24235 + * Since struct completion is large, try allocating it dynamically.
24237 +#if defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS)
24238 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
24240 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
24242 + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
24244 + init_completion(*comp);
24245 + wkinfo->comp = *comp;
24251 +static void au_wkq_comp_free(struct completion *comp)
24259 +#define AuWkqCompDeclare(name) \
24260 + DECLARE_COMPLETION_ONSTACK(_ ## name); \
24261 + struct completion *comp = &_ ## name
24263 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
24265 + wkinfo->comp = *comp;
24269 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
24273 +#endif /* 4KSTACKS */
24275 +static void au_wkq_run(struct au_wkinfo *wkinfo)
24277 + au_dbg_verify_kthread();
24278 + INIT_WORK(&wkinfo->wk, wkq_func);
24282 +int au_wkq_wait(au_wkq_func_t func, void *args)
24285 + AuWkqCompDeclare(comp);
24286 + struct au_wkinfo wkinfo = {
24287 + .flags = AuWkq_WAIT,
24292 + err = au_wkq_comp_alloc(&wkinfo, &comp);
24294 + au_wkq_run(&wkinfo);
24295 + /* no timeout, no interrupt */
24296 + wait_for_completion(wkinfo.comp);
24297 + au_wkq_comp_free(comp);
24304 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb)
24307 + struct au_wkinfo *wkinfo;
24309 + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
24312 + * wkq_func() must free this wkinfo.
24313 + * it highly depends upon the implementation of workqueue.
24316 + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
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);
24326 + au_wkq_run(wkinfo);
24329 + atomic_dec(&au_sbi(sb)->si_nowait.nw_len);
24335 +/* ---------------------------------------------------------------------- */
24337 +void au_nwt_init(struct au_nowait_tasks *nwt)
24339 + atomic_set(&nwt->nw_len, 0);
24340 + /* smp_mb();*/ /* atomic_set */
24341 + init_waitqueue_head(&nwt->nw_wq);
24344 +void au_wkq_fin(void)
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);
24354 +int __init au_wkq_init(void)
24357 + struct au_wkq *nowaitq;
24359 + /* '+1' is for accounting of nowait queue */
24361 + au_wkq = kcalloc(aufs_nwkq + 1, sizeof(*au_wkq), GFP_NOFS);
24362 + if (unlikely(!au_wkq))
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);
24373 + err = PTR_ERR(au_wkq[i].q);
24378 + /* nowait accounting */
24379 + nowaitq = au_wkq + aufs_nwkq;
24380 + atomic_set(&nowaitq->busy, 0);
24381 + nowaitq->q = NULL;
24382 + /* smp_mb(); */ /* atomic_set */
24387 diff --git a/fs/aufs/wkq.h b/fs/aufs/wkq.h
24388 new file mode 100644
24389 index 0000000..17d24d8
24391 +++ b/fs/aufs/wkq.h
24394 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
24403 + * workqueue for asynchronous/super-io operations
24404 + * todo: try new credentials management scheme
24407 +#ifndef __AUFS_WKQ_H__
24408 +#define __AUFS_WKQ_H__
24412 +#include <linux/fs.h>
24413 +#include <linux/sched.h>
24414 +#include <linux/workqueue.h>
24415 +#include <linux/aufs_type.h>
24417 +/* ---------------------------------------------------------------------- */
24420 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
24422 +struct au_nowait_tasks {
24424 + wait_queue_head_t nw_wq;
24427 +/* ---------------------------------------------------------------------- */
24429 +typedef void (*au_wkq_func_t)(void *args);
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; }
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);
24444 +/* ---------------------------------------------------------------------- */
24446 +static inline int au_test_wkq(struct task_struct *tsk)
24448 + return !tsk->mm && !strcmp(tsk->comm, AUFS_WKQ_NAME);
24451 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
24453 + if (!atomic_dec_return(&nwt->nw_len))
24454 + wake_up_all(&nwt->nw_wq);
24457 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
24459 + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
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
24469 +++ b/fs/aufs/xino.c
24472 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
24481 + * external inode number translation table and bitmap
24484 +#include <linux/seq_file.h>
24485 +#include <linux/uaccess.h>
24488 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
24492 + mm_segment_t oldfs;
24494 + oldfs = get_fs();
24495 + set_fs(KERNEL_DS);
24497 + /* todo: signal_pending? */
24498 + err = func(file, (char __user *)buf, size, pos);
24499 + } while (err == -EAGAIN || err == -EINTR);
24502 +#if 0 /* reserved for future use */
24504 + fsnotify_access(file->f_dentry);
24510 +/* ---------------------------------------------------------------------- */
24512 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *buf,
24513 + size_t size, loff_t *pos)
24516 + mm_segment_t oldfs;
24518 + oldfs = get_fs();
24519 + set_fs(KERNEL_DS);
24522 + /* todo: signal_pending? */
24523 + err = func(file, (const char __user *)buf, size, pos);
24524 + } while (err == -EAGAIN || err == -EINTR);
24528 +#if 0 /* reserved for future use */
24530 + fsnotify_modify(file->f_dentry);
24536 +struct do_xino_fwrite_args {
24538 + au_writef_t func;
24539 + struct file *file;
24545 +static void call_do_xino_fwrite(void *args)
24547 + struct do_xino_fwrite_args *a = args;
24548 + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
24551 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
24556 + /* todo: signal block and no wkq? */
24557 + /* todo: new credential scheme */
24559 + * it breaks RLIMIT_FSIZE and normal user's limit,
24560 + * users should care about quota and real 'filesystem full.'
24562 + if (!au_test_wkq(current)) {
24564 + struct do_xino_fwrite_args args = {
24573 + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
24574 + if (unlikely(wkq_err))
24577 + err = do_xino_fwrite(func, file, buf, size, pos);
24582 +/* ---------------------------------------------------------------------- */
24585 + * create a new xinofile at the same place/path as @base_file.
24587 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
24589 + struct file *file;
24590 + struct dentry *base, *dentry, *parent;
24591 + struct inode *dir;
24592 + struct qstr *name;
24595 + base = base_file->f_dentry;
24596 + parent = base->d_parent; /* dir inode is locked */
24597 + dir = parent->d_inode;
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));
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);
24617 + file = dentry_open(dget(dentry), mntget(base_file->f_vfsmnt),
24618 + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE,
24620 + if (IS_ERR(file)) {
24621 + AuErr("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
24625 + err = vfsub_unlink(dir, &file->f_path, /*force*/0);
24626 + if (unlikely(err)) {
24627 + AuErr("%.*s unlink err %d\n", AuLNPair(name), err);
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);
24640 + goto out_dput; /* success */
24644 + file = ERR_PTR(err);
24651 +struct au_xino_lock_dir {
24652 + struct au_hinode *hdir;
24653 + struct dentry *parent;
24654 + struct mutex *mtx;
24657 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
24658 + struct au_xino_lock_dir *ldir)
24660 + aufs_bindex_t brid, bindex;
24662 + ldir->hdir = NULL;
24664 + brid = au_xino_brid(sb);
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);
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);
24677 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
24680 + au_hin_imtx_unlock(ldir->hdir);
24682 + mutex_unlock(ldir->mtx);
24683 + dput(ldir->parent);
24687 +/* ---------------------------------------------------------------------- */
24689 +/* trucate xino files asynchronously */
24691 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
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;
24701 + bend = au_sbend(sb);
24702 + if (unlikely(bindex < 0 || bend < bindex))
24704 + br = au_sbr(sb, bindex);
24705 + file = br->br_xino.xi_file;
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))
24718 + br->br_xino.xi_file = new_xino;
24720 + h_sb = br->br_mnt->mnt_sb;
24721 + for (bi = 0; bi <= bend; bi++) {
24722 + if (unlikely(bi == bindex))
24724 + br = au_sbr(sb, bi);
24725 + if (br->br_mnt->mnt_sb != h_sb)
24728 + fput(br->br_xino.xi_file);
24729 + br->br_xino.xi_file = new_xino;
24730 + get_file(new_xino);
24737 +struct xino_do_trunc_args {
24738 + struct super_block *sb;
24739 + struct au_branch *br;
24742 +static void xino_do_trunc(void *_args)
24744 + struct xino_do_trunc_args *args = _args;
24745 + struct super_block *sb;
24746 + struct au_branch *br;
24747 + struct inode *dir;
24749 + aufs_bindex_t bindex;
24753 + dir = sb->s_root->d_inode;
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))
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;
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);
24778 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
24780 + struct xino_do_trunc_args *args;
24783 + if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
24784 + < br->br_xino_upper)
24787 + if (atomic_inc_return(&br->br_xino_running) > 1)
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");
24797 + atomic_inc(&br->br_count);
24800 + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb);
24802 + return; /* success */
24804 + AuErr("wkq %d\n", wkq_err);
24805 + atomic_dec(&br->br_count);
24810 + atomic_dec(&br->br_xino_running);
24813 +/* ---------------------------------------------------------------------- */
24815 +static int au_xino_do_write(au_writef_t write, struct file *file,
24816 + ino_t h_ino, ino_t ino)
24822 + if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
24823 + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
24826 + pos *= sizeof(ino);
24827 + sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
24828 + if (sz == sizeof(ino))
24829 + return 0; /* success */
24831 + AuIOErr("write failed (%zd)\n", sz);
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.
24842 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
24846 + unsigned int mnt_flags;
24847 + struct au_branch *br;
24849 + BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
24850 + || ((loff_t)-1) > 0);
24852 + mnt_flags = au_mntflags(sb);
24853 + if (!au_opt_test(mnt_flags, XINO))
24856 + br = au_sbr(sb, bindex);
24857 + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
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 */
24866 + AuIOErr("write failed (%d)\n", err);
24870 +/* ---------------------------------------------------------------------- */
24872 +/* aufs inode number bitmap */
24874 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
24875 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
24879 + AuDebugOn(bit < 0 || page_bits <= bit);
24880 + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
24884 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
24886 + AuDebugOn(ino < AUFS_FIRST_INO);
24887 + ino -= AUFS_FIRST_INO;
24888 + *pindex = ino / page_bits;
24889 + *bit = ino % page_bits;
24892 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
24897 + struct au_sbinfo *sbinfo;
24898 + struct file *xib;
24899 + unsigned long *p;
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));
24906 + if (pindex == sbinfo->si_xib_last_pindex)
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))
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);
24922 + memset(p, 0, PAGE_SIZE);
24923 + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
24925 + if (sz == PAGE_SIZE) {
24926 + sbinfo->si_xib_last_pindex = pindex;
24927 + return 0; /* success */
24931 + AuIOErr1("write failed (%zd)\n", sz);
24938 +/* ---------------------------------------------------------------------- */
24940 +int au_xino_write0(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
24944 + unsigned long pindex;
24945 + struct au_sbinfo *sbinfo;
24947 + if (!au_opt_test(au_mntflags(sb), XINO))
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);
24958 + clear_bit(bit, sbinfo->si_xib_buf);
24959 + sbinfo->si_xib_next_bit = bit;
24961 + mutex_unlock(&sbinfo->si_xib_mtx);
24965 + err = au_xino_write(sb, bindex, h_ino, 0);
24969 +/* get an unused inode number from bitmap */
24970 +ino_t au_xino_new_ino(struct super_block *sb)
24973 + unsigned long *p, pindex, ul, pend;
24974 + struct au_sbinfo *sbinfo;
24975 + struct file *file;
24976 + int free_bit, err;
24978 + if (!au_opt_test(au_mntflags(sb), XINO))
24979 + return iunique(sb, AUFS_FIRST_INO);
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 */
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))
24996 + free_bit = find_first_zero_bit(p, page_bits);
24997 + if (free_bit < page_bits)
24998 + goto out; /* success */
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))
25007 + free_bit = find_first_zero_bit(p, page_bits);
25008 + if (free_bit < page_bits)
25009 + goto out; /* success */
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);
25022 + mutex_unlock(&sbinfo->si_xib_mtx);
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.
25032 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
25038 + struct file *file;
25039 + struct au_sbinfo *sbinfo;
25042 + if (!au_opt_test(au_mntflags(sb), XINO))
25043 + return 0; /* no xino */
25046 + sbinfo = au_sbi(sb);
25048 + if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
25049 + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
25052 + pos *= sizeof(*ino);
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 */
25058 + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
25059 + if (sz == sizeof(*ino))
25060 + return 0; /* success */
25063 + if (unlikely(sz >= 0)) {
25065 + AuIOErr("xino read error (%zd)\n", sz);
25071 +/* ---------------------------------------------------------------------- */
25073 +/* create and set a new xino file */
25075 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
25077 + struct file *file;
25078 + struct dentry *h_parent, *d;
25079 + struct inode *h_dir;
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.
25087 + file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE,
25088 + S_IRUGO | S_IWUGO);
25089 + if (IS_ERR(file)) {
25091 + AuErr("open %s(%ld)\n", fname, PTR_ERR(file));
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);
25103 + if (unlikely(err)) {
25105 + AuErr("unlink %s(%d)\n", fname, err);
25110 + d = file->f_dentry;
25111 + if (unlikely(sb == d->d_sb)) {
25113 + AuErr("%s must be outside\n", fname);
25116 + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
25118 + AuErr("xino doesn't support %s(%s)\n",
25119 + fname, au_sbtype(d->d_sb));
25122 + return file; /* success */
25126 + file = ERR_PTR(err);
25131 + * find another branch who is on the same filesystem of the specified
25132 + * branch{@btgt}. search until @bend.
25134 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
25135 + aufs_bindex_t bend)
25137 + aufs_bindex_t bindex;
25138 + struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
25140 + for (bindex = 0; bindex < btgt; bindex++)
25141 + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
25143 + for (bindex++; bindex <= bend; bindex++)
25144 + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
25149 +/* ---------------------------------------------------------------------- */
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.
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)
25162 + aufs_bindex_t bend, bindex;
25163 + struct au_branch *shared_br, *b;
25164 + struct file *file;
25165 + struct super_block *tgt_sb;
25167 + shared_br = NULL;
25168 + bend = au_sbend(sb);
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) {
25180 + if (!shared_br || !shared_br->br_xino.xi_file) {
25181 + struct au_xino_lock_dir ldir;
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))
25190 + br->br_xino.xi_file = file;
25192 + br->br_xino.xi_file = shared_br->br_xino.xi_file;
25193 + get_file(br->br_xino.xi_file);
25196 + ino = AUFS_ROOT_INO;
25197 + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
25200 + return 0; /* success */
25207 +/* ---------------------------------------------------------------------- */
25209 +/* trucate a xino bitmap file */
25212 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
25216 + unsigned long pindex;
25217 + loff_t pos, pend;
25218 + struct au_sbinfo *sbinfo;
25221 + unsigned long *p;
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);
25229 + while (pos < pend) {
25230 + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
25232 + if (unlikely(sz <= 0))
25236 + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
25237 + if (unlikely(*ino < AUFS_FIRST_INO))
25240 + xib_calc_bit(*ino, &pindex, &bit);
25241 + AuDebugOn(page_bits <= bit);
25242 + err = xib_pindex(sb, pindex);
25254 +static int xib_restore(struct super_block *sb)
25257 + aufs_bindex_t bindex, bend;
25261 + page = (void *)__get_free_page(GFP_NOFS);
25262 + if (unlikely(!page))
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);
25272 + AuDbg("b%d\n", bindex);
25273 + free_page((unsigned long)page);
25279 +int au_xib_trunc(struct super_block *sb)
25284 + struct au_xino_lock_dir ldir;
25285 + struct au_sbinfo *sbinfo;
25286 + unsigned long *p;
25287 + struct file *file;
25290 + sbinfo = au_sbi(sb);
25291 + if (!au_opt_test(sbinfo->si_mntflags, XINO))
25294 + file = sbinfo->si_xib;
25295 + if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
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))
25305 + fput(sbinfo->si_xib);
25306 + sbinfo->si_xib = file;
25308 + p = sbinfo->si_xib_buf;
25309 + memset(p, 0, PAGE_SIZE);
25311 + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
25312 + if (unlikely(sz != PAGE_SIZE)) {
25314 + AuIOErr("err %d\n", err);
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);
25329 +/* ---------------------------------------------------------------------- */
25332 + * xino mount option handlers
25334 +static au_readf_t find_readf(struct file *h_file)
25336 + const struct file_operations *fop = h_file->f_op;
25340 + return fop->read;
25341 + if (fop->aio_read)
25342 + return do_sync_read;
25344 + return ERR_PTR(-ENOSYS);
25347 +static au_writef_t find_writef(struct file *h_file)
25349 + const struct file_operations *fop = h_file->f_op;
25353 + return fop->write;
25354 + if (fop->aio_write)
25355 + return do_sync_write;
25357 + return ERR_PTR(-ENOSYS);
25361 +static void xino_clear_xib(struct super_block *sb)
25363 + struct au_sbinfo *sbinfo;
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;
25375 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
25379 + struct au_sbinfo *sbinfo;
25380 + struct file *file;
25382 + sbinfo = au_sbi(sb);
25383 + file = au_xino_create2(base, sbinfo->si_xib);
25384 + err = PTR_ERR(file);
25385 + if (IS_ERR(file))
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);
25394 + if (!sbinfo->si_xib_buf)
25395 + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
25396 + if (unlikely(!sbinfo->si_xib_buf))
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) {
25403 + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
25404 + PAGE_SIZE, &pos);
25405 + if (unlikely(err != PAGE_SIZE))
25409 + goto out; /* success */
25412 + free_page((unsigned long)sbinfo->si_xib_buf);
25413 + sbinfo->si_xib_buf = NULL;
25417 + fput(sbinfo->si_xib);
25418 + sbinfo->si_xib = NULL;
25419 + sbinfo->si_xread = NULL;
25420 + sbinfo->si_xwrite = NULL;
25425 +/* xino for each branch */
25426 +static void xino_clear_br(struct super_block *sb)
25428 + aufs_bindex_t bindex, bend;
25429 + struct au_branch *br;
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)
25437 + fput(br->br_xino.xi_file);
25438 + br->br_xino.xi_file = NULL;
25442 +static int au_xino_set_br(struct super_block *sb, struct file *base)
25446 + aufs_bindex_t bindex, bend, bshared;
25448 + struct file *old, *new;
25450 + struct au_branch *br;
25451 + struct inode *inode;
25452 + au_writef_t writef;
25455 + bend = au_sbend(sb);
25456 + fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
25457 + if (unlikely(!fpair))
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);
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)) {
25483 + err = au_xino_do_write(writef, p->new,
25484 + au_h_iptr(inode, bindex)->i_ino, ino);
25485 + if (unlikely(err))
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;
25498 + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
25508 +void au_xino_clr(struct super_block *sb)
25510 + struct au_sbinfo *sbinfo;
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);
25520 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
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;
25530 + sbinfo = au_sbi(sb);
25531 + parent = dget_parent(xino->file->f_dentry);
25534 + dname = &xino->file->f_dentry->d_name;
25535 + cur_xino = sbinfo->si_xib;
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,
25543 + dput(cur_parent);
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);
25555 + err = au_xigen_set(sb, xino->file);
25557 + err = au_xino_set_br(sb, xino->file);
25558 + mutex_unlock(&dir->i_mutex);
25560 + goto out; /* success */
25563 + AuIOErr("failed creating xino(%d).\n", err);
25570 +/* ---------------------------------------------------------------------- */
25573 + * create a xinofile at the default place/path.
25575 +struct file *au_xino_def(struct super_block *sb)
25577 + struct file *file;
25579 + struct au_branch *br;
25580 + struct super_block *h_sb;
25581 + struct path path;
25582 + aufs_bindex_t bend, bindex, bwr;
25585 + bend = au_sbend(sb);
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)) {
25597 + file = ERR_PTR(-ENOMEM);
25598 + page = __getname();
25599 + if (unlikely(!page))
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);
25614 + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
25615 + if (IS_ERR(file))
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));
25622 + file = ERR_PTR(-EINVAL);
25624 + if (!IS_ERR(file))
25625 + au_xino_brid_set(sb, -1);
25632 +/* ---------------------------------------------------------------------- */
25634 +int au_xino_path(struct seq_file *seq, struct file *file)
25638 + err = au_seq_path(seq, &file->f_path);
25639 + if (unlikely(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));
25652 diff --git a/fs/namei.c b/fs/namei.c
25653 index bbc15c2..db581b4 100644
25656 @@ -1196,7 +1196,7 @@ out:
25657 * needs parent already locked. Doesn't follow mounts.
25660 -static struct dentry *lookup_hash(struct nameidata *nd)
25661 +struct dentry *lookup_hash(struct nameidata *nd)
25665 @@ -1206,7 +1206,7 @@ static struct dentry *lookup_hash(struct nameidata *nd)
25666 return __lookup_hash(&nd->last, nd->path.dentry, nd);
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)
25673 unsigned long hash;
25674 diff --git a/fs/splice.c b/fs/splice.c
25675 index 4ed0ba4..2fb3d17 100644
25678 @@ -888,8 +888,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
25680 * Attempt to initiate a splice from pipe to file.
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)
25689 @@ -912,9 +912,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
25691 * Attempt to initiate a splice from a file to a pipe.
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)
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
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
25718 +++ b/include/linux/aufs_type.h
25721 + * Copyright (C) 2005-2009 Junjiro R. Okajima
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.
25729 +#ifndef __AUFS_TYPE_H__
25730 +#define __AUFS_TYPE_H__
25732 +#include <linux/ioctl.h>
25734 +#define AUFS_VERSION "2-29"
25736 +/* todo? move this to linux-2.6.19/include/magic.h */
25737 +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
25739 +/* ---------------------------------------------------------------------- */
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
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
25757 +#ifndef AUFS_BRANCH_MAX
25758 +#error unknown CONFIG_AUFS_BRANCH_MAX value
25760 +#endif /* __KERNEL__ */
25762 +/* ---------------------------------------------------------------------- */
25764 +#define AUFS_NAME "aufs"
25765 +#define AUFS_FSTYPE AUFS_NAME
25767 +#define AUFS_ROOT_INO 2
25768 +#define AUFS_FIRST_INO 11
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 */
25785 +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
25786 +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
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"
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
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
25807 +/* ---------------------------------------------------------------------- */
25811 + AuCtl_PLINK_MAINT,
25812 + AuCtl_PLINK_CLEAN
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)
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 *);
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 *);
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 *);
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);