1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index f15621e..55e2f07 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -112,6 +112,8 @@ udf.txt
6 - info and mount options for the UDF filesystem.
8 - info on the ufs filesystem.
10 + - info on the unionfs filesystem
12 - info on using the VFAT filesystem used in Windows NT and Windows 95
14 diff --git a/Documentation/filesystems/unionfs/00-INDEX b/Documentation/filesystems/unionfs/00-INDEX
16 index 0000000..96fdf67
18 +++ b/Documentation/filesystems/unionfs/00-INDEX
23 + - A brief introduction of concepts.
25 + - A summary of known issues with unionfs.
27 + - Information regarding rename operations.
29 + - Usage information and examples.
30 diff --git a/Documentation/filesystems/unionfs/concepts.txt b/Documentation/filesystems/unionfs/concepts.txt
32 index 0000000..b853788
34 +++ b/Documentation/filesystems/unionfs/concepts.txt
36 +Unionfs 2.x CONCEPTS:
37 +=====================
39 +This file describes the concepts needed by a namespace unification file
46 +Each branch is assigned a unique priority - starting from 0 (highest
47 +priority). No two branches can have the same priority.
53 +Each branch is assigned a mode - read-write or read-only. This allows
54 +directories on media mounted read-write to be used in a read-only manner.
60 +A whiteout removes a file name from the namespace. Whiteouts are needed when
61 +one attempts to remove a file on a read-only branch.
63 +Suppose we have a two-branch union, where branch 0 is read-write and branch
64 +1 is read-only. And a file 'foo' on branch 1:
70 +The unified view would simply be:
75 +Since 'foo' is stored on a read-only branch, it cannot be removed. A
76 +whiteout is used to remove the name 'foo' from the unified namespace. Again,
77 +since branch 1 is read-only, the whiteout cannot be created there. So, we
78 +try on a higher priority (lower numerically) branch and create the whiteout
86 +Later, when Unionfs traverses branches (due to lookup or readdir), it
87 +eliminate 'foo' from the namespace (as well as the whiteout itself.)
93 +Assume we have a unionfs mount comprising of two branches. Branch 0 is
94 +empty; branch 1 has the directory /a and file /a/f. Let's say we mount a
95 +union of branch 0 as read-write and branch 1 as read-only. Now, let's say
96 +we try to perform the following operation in the union:
100 +Because branch 1 is not writable, we cannot physically remove the file /a/f
101 +or the directory /a. So instead, we will create a whiteout in branch 0
102 +named /.wh.a, masking out the name "a" from branch 1. Next, let's say we
103 +try to create a directory named "a" as follows:
107 +Because we have a whiteout for "a" already, Unionfs behaves as if "a"
108 +doesn't exist, and thus will delete the whiteout and replace it with an
109 +actual directory named "a".
111 +The problem now is that if you try to "ls" in the union, Unionfs will
112 +perform is normal directory name unification, for *all* directories named
113 +"a" in all branches. This will cause the file /a/f from branch 1 to
114 +re-appear in the union's namespace, which violates Unix semantics.
116 +To avoid this problem, we have a different form of whiteouts for
117 +directories, called "opaque directories" (same as BSD Union Mount does).
118 +Whenever we replace a whiteout with a directory, that directory is marked as
119 +opaque. In Unionfs 2.x, it means that we create a file named
120 +/a/.wh.__dir_opaque in branch 0, after having created directory /a there.
121 +When unionfs notices that a directory is opaque, it stops all namespace
122 +operations (including merging readdir contents) at that opaque directory.
123 +This prevents re-exposing names from masked out directories.
126 +Duplicate Elimination:
127 +======================
129 +It is possible for files on different branches to have the same name.
130 +Unionfs then has to select which instance of the file to show to the user.
131 +Given the fact that each branch has a priority associated with it, the
132 +simplest solution is to take the instance from the highest priority
133 +(numerically lowest value) and "hide" the others.
139 +Unlink operation on non-directory instances is optimized to remove the
140 +maximum possible objects in case multiple underlying branches have the same
141 +file name. The unlink operation will first try to delete file instances
142 +from highest priority branch and then move further to delete from remaining
143 +branches in order of their decreasing priority. Consider a case (F..D..F),
144 +where F is a file and D is a directory of the same name; here, some
145 +intermediate branch could have an empty directory instance with the same
146 +name, so this operation also tries to delete this directory instance and
147 +proceed further to delete from next possible lower priority branch. The
148 +unionfs unlink operation will smoothly delete the files with same name from
149 +all possible underlying branches. In case if some error occurs, it creates
150 +whiteout in highest priority branch that will hide file instance in rest of
151 +the branches. An error could occur either if an unlink operations in any of
152 +the underlying branch failed or if a branch has no write permission.
154 +This unlinking policy is known as "delete all" and it has the benefit of
155 +overall reducing the number of inodes used by duplicate files, and further
156 +reducing the total number of inodes consumed by whiteouts. The cost is of
157 +extra processing, but testing shows this extra processing is well worth the
164 +When a change is made to the contents of a file's data or meta-data, they
165 +have to be stored somewhere. The best way is to create a copy of the
166 +original file on a branch that is writable, and then redirect the write
167 +though to this copy. The copy must be made on a higher priority branch so
168 +that lookup and readdir return this newer "version" of the file rather than
169 +the original (see duplicate elimination).
171 +An entire unionfs mount can be read-only or read-write. If it's read-only,
172 +then none of the branches will be written to, even if some of the branches
173 +are physically writeable. If the unionfs mount is read-write, then the
174 +leftmost (highest priority) branch must be writeable (for copyup to take
175 +place); the remaining branches can be any mix of read-write and read-only.
177 +In a writeable mount, unionfs will create new files/dir in the leftmost
178 +branch. If one tries to modify a file in a read-only branch/media, unionfs
179 +will copyup the file to the leftmost branch and modify it there. If you try
180 +to modify a file from a writeable branch which is not the leftmost branch,
181 +then unionfs will modify it in that branch; this is useful if you, say,
182 +unify differnet packages (e.g., apache, sendmail, ftpd, etc.) and you want
183 +changes to specific package files to remain logically in the directory where
189 +Unionfs users often want to be able to modify files and directories directly
190 +on the lower branches, and have those changes be visible at the Unionfs
191 +level. This means that data (e.g., pages) and meta-data (dentries, inodes,
192 +open files, etc.) have to be synchronized between the upper and lower
193 +layers. In other words, the newest changes from a layer below have to be
194 +propagated to the Unionfs layer above. If the two layers are not in sync, a
195 +cache incoherency ensues, which could lead to application failures and even
196 +oopses. The Linux kernel, however, has a rather limited set of mechanisms
197 +to ensure this inter-layer cache coherency---so Unionfs has to do most of
198 +the hard work on its own.
200 +Maintaining Invariants:
202 +The way Unionfs ensures cache coherency is as follows. At each entry point
203 +to a Unionfs file system method, we call a utility function to validate the
204 +primary objects of this method. Generally, we call unionfs_file_revalidate
205 +on open files, and __unionfs_d_revalidate_chain on dentries (which also
206 +validates inodes). These utility functions check to see whether the upper
207 +Unionfs object is in sync with any of the lower objects that it represents.
208 +The checks we perform include whether the Unionfs superblock has a newer
209 +generation number, or if any of the lower objects mtime's or ctime's are
210 +newer. (Note: generation numbers change when branch-management commands are
211 +issued, so in a way, maintaining cache coherency is also very important for
212 +branch-management.) If indeed we determine that any Unionfs object is no
213 +longer in sync with its lower counterparts, then we rebuild that object
214 +similarly to how we do so for branch-management.
216 +While rebuilding Unionfs's objects, we also purge any page mappings and
217 +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data). This is to
218 +ensure that Unionfs will re-get the newer data from the lower branches. We
219 +perform this purging only if the Unionfs operation in question is a reading
220 +operation; if Unionfs is performing a data writing operation (e.g., ->write,
221 +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
222 +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
223 +considered more authoritative anyway, as they are newer and will overwrite
226 +Unionfs maintains the following important invariant regarding mtime's,
227 +ctime's, and atime's: the upper inode object's times are the max() of all of
228 +the lower ones. For non-directory objects, there's only one object below,
229 +so the mapping is simple; for directory objects, there could me multiple
230 +lower objects and we have to sync up with the newest one of all the lower
231 +ones. This invariant is important to maintain, especially for directories
232 +(besides, we need this to be POSIX compliant). A union could comprise
233 +multiple writable branches, each of which could change. If we don't reflect
234 +the newest possible mtime/ctime, some applications could fail. For example,
235 +NFSv2/v3 exports check for newer directory mtimes on the server to determine
236 +if the client-side attribute cache should be purged.
238 +To maintain these important invariants, of course, Unionfs carefully
239 +synchronizes upper and lower times in various places. For example, if we
240 +copy-up a file to a top-level branch, the parent directory where the file
241 +was copied up to will now have a new mtime: so after a successful copy-up,
242 +we sync up with the new top-level branch's parent directory mtime.
246 +This cache-coherency implementation is efficient because it defers any
247 +synchronizing between the upper and lower layers until absolutely needed.
248 +Consider the example a common situation where users perform a lot of lower
249 +changes, such as untarring a whole package. While these take place,
250 +typically the user doesn't access the files via Unionfs; only after the
251 +lower changes are done, does the user try to access the lower files. With
252 +our cache-coherency implementation, the entirety of the changes to the lower
253 +branches will not result in a single CPU cycle spent at the Unionfs level
254 +until the user invokes a system call that goes through Unionfs.
256 +We have considered two alternate cache-coherency designs. (1) Using the
257 +dentry/inode notify functionality to register interest in finding out about
258 +any lower changes. This is a somewhat limited and also a heavy-handed
259 +approach which could result in many notifications to the Unionfs layer upon
260 +each small change at the lower layer (imagine a file being modified multiple
261 +times in rapid succession). (2) Rewriting the VFS to support explicit
262 +callbacks from lower objects to upper objects. We began exploring such an
263 +implementation, but found it to be very complicated--it would have resulted
264 +in massive VFS/MM changes which are unlikely to be accepted by the LKML
265 +community. We therefore believe that our current cache-coherency design and
266 +implementation represent the best approach at this time.
270 +Our implementation works in that as long as a user process will have caused
271 +Unionfs to be called, directly or indirectly, even to just do
272 +->d_revalidate; then we will have purged the current Unionfs data and the
273 +process will see the new data. For example, a process that continually
274 +re-reads the same file's data will see the NEW data as soon as the lower
275 +file had changed, upon the next read(2) syscall (even if the file is still
276 +open!) However, this doesn't work when the process re-reads the open file's
277 +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
278 +it). Once we respond to ->readpage(s), then the kernel maps the page into
279 +the process's address space and there doesn't appear to be a way to force
280 +the kernel to invalidate those pages/mappings, and force the process to
281 +re-issue ->readpage. If there's a way to invalidate active mappings and
282 +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
285 +Our current Unionfs code has to perform many file-revalidation calls. It
286 +would be really nice if the VFS would export an optional file system hook
287 +->file_revalidate (similarly to dentry->d_revalidate) that will be called
288 +before each VFS op that has a "struct file" in it.
290 +Certain file systems have micro-second granularity (or better) for inode
291 +times, and asynchronous actions could cause those times to change with some
292 +small delay. In such cases, Unionfs may see a changed inode time that only
293 +differs by a tiny fraction of a second: such a change may be a false
294 +positive indication that the lower object has changed, whereas if unionfs
295 +waits a little longer, that false indication will not be seen. (These false
296 +positives are harmless, because they would at most cause unionfs to
297 +re-validate an object that may need no revalidation, and print a debugging
298 +message that clutters the console/logs.) Therefore, to minimize the chances
299 +of these situations, we delay the detection of changed times by a small
300 +factor of a few seconds, called UNIONFS_MIN_CC_TIME (which defaults to 3
301 +seconds, as does NFS). This means that we will detect the change, only a
302 +couple of seconds later, if indeed the time change persists in the lower
303 +file object. This delayed detection has an added performance benefit: we
304 +reduce the number of times that unionfs has to revalidate objects, in case
305 +there's a lot of concurrent activity on both the upper and lower objects,
306 +for the same file(s). Lastly, this delayed time attribute detection is
307 +similar to how NFS clients operate (e.g., acregmin).
309 +Finally, there is no way currently in Linux to prevent lower directories
310 +from being moved around (i.e., topology changes); there's no way to prevent
311 +modifications to directory sub-trees of whole file systems which are mounted
312 +read-write. It is therefore possible for in-flight operations in unionfs to
313 +take place, while a lower directory is being moved around. Therefore, if
314 +you try to, say, create a new file in a directory through unionfs, while the
315 +directory is being moved around directly, then the new file may get created
316 +in the new location where that directory was moved to. This is a somewhat
317 +similar behaviour in NFS: an NFS client could be creating a new file while
318 +th NFS server is moving th directory around; the file will get successfully
319 +created in the new location. (The one exception in unionfs is that if the
320 +branch is marked read-only by unionfs, then a copyup will take place.)
322 +For more information, see <http://unionfs.filesystems.org/>.
323 diff --git a/Documentation/filesystems/unionfs/issues.txt b/Documentation/filesystems/unionfs/issues.txt
325 index 0000000..f4b7e7e
327 +++ b/Documentation/filesystems/unionfs/issues.txt
329 +KNOWN Unionfs 2.x ISSUES:
330 +=========================
332 +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
333 + confuses NFSv4. Currently, unionfs_lookup() passes lookup intents to the
334 + lower file-system, this eliminates part of the problem. The remaining
335 + calls to lookup_one_len may need to be changed to pass an intent. We are
336 + currently introducing VFS changes to fs/namei.c's do_path_lookup() to
337 + allow proper file lookup and opening in stackable file systems.
339 +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
340 + incorrectly complains about locking problems. The problem boils down to
341 + this: Lockdep considers all objects of a certain type to be in the same
342 + class, for example, all inodes. Lockdep doesn't like to see a lock held
343 + on two inodes within the same task, and warns that it could lead to a
344 + deadlock. However, stackable file systems do precisely that: they lock
345 + an upper object, and then a lower object, in a strict order to avoid
346 + locking problems; in addition, Unionfs, as a fan-out file system, may
347 + have to lock several lower inodes. We are currently looking into Lockdep
348 + to see how to make it aware of stackable file systems. For now, we
349 + temporarily disable lockdep when calling vfs methods on lower objects,
350 + but only for those places where lockdep complained. While this solution
351 + may seem unclean, it is not without precedent: other places in the kernel
352 + also do similar temporary disabling, of course after carefully having
353 + checked that it is the right thing to do. Anyway, you get any warnings
354 + from Lockdep, please report them to the Unionfs maintainers.
356 +For more information, see <http://unionfs.filesystems.org/>.
357 diff --git a/Documentation/filesystems/unionfs/rename.txt b/Documentation/filesystems/unionfs/rename.txt
359 index 0000000..e20bb82
361 +++ b/Documentation/filesystems/unionfs/rename.txt
363 +Rename is a complex beast. The following table shows which rename(2) operations
364 +should succeed and which should fail.
367 +E: error (either unionfs or vfs)
370 +none = file does not exist
371 +file = file is a file
372 +dir = file is a empty directory
373 +child= file is a non-empty directory
374 +wh = file is a directory containing only whiteouts; this makes it logically
377 + none file dir child wh
384 +Renaming directories:
385 +=====================
387 +Whenever a empty (either physically or logically) directory is being renamed,
388 +the following sequence of events should take place:
390 +1) Remove whiteouts from both source and destination directory
391 +2) Rename source to destination
392 +3) Make destination opaque to prevent anything under it from showing up
394 diff --git a/Documentation/filesystems/unionfs/usage.txt b/Documentation/filesystems/unionfs/usage.txt
396 index 0000000..1adde69
398 +++ b/Documentation/filesystems/unionfs/usage.txt
400 +Unionfs is a stackable unification file system, which can appear to merge
401 +the contents of several directories (branches), while keeping their physical
402 +content separate. Unionfs is useful for unified source tree management,
403 +merged contents of split CD-ROM, merged separate software package
404 +directories, data grids, and more. Unionfs allows any mix of read-only and
405 +read-write branches, as well as insertion and deletion of branches anywhere
406 +in the fan-out. To maintain Unix semantics, Unionfs handles elimination of
407 +duplicates, partial-error conditions, and more.
412 +# mount -t unionfs -o <OPTIONS>,<BRANCH-OPTIONS> none MOUNTPOINT
414 +OPTIONS can be any legal combination of:
416 +- ro # mount file system read-only
417 +- rw # mount file system read-write
418 +- remount # remount the file system (see Branch Management below)
419 +- incgen # increment generation no. (see Cache Consistency below)
421 +BRANCH-OPTIONS can be either (1) a list of branches given to the "dirs="
422 +option, or (2) a list of individual branch manipulation commands, combined
423 +with the "remount" option, and is further described in the "Branch
424 +Management" section below.
426 +The syntax for the "dirs=" mount option is:
428 + dirs=branch[=ro|=rw][:...]
430 +The "dirs=" option takes a colon-delimited list of directories to compose
431 +the union, with an optional branch mode for each of those directories.
432 +Directories that come earlier (specified first, on the left) in the list
433 +have a higher precedence than those which come later. Additionally,
434 +read-only or read-write permissions of the branch can be specified by
435 +appending =ro or =rw (default) to each directory. See the Copyup section in
436 +concepts.txt, for a description of Unionfs's behavior when mixing read-only
437 +and read-write branches and mounts.
441 + dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
445 + dirs=/writable_branch=rw:/read-only_branch=ro
451 +Once you mount your union for the first time, using the "dirs=" option, you
452 +can then change the union's overall mode or reconfigure the branches, using
453 +the remount option, as follows.
455 +To downgrade a union from read-write to read-only:
457 +# mount -t unionfs -o remount,ro none MOUNTPOINT
459 +To upgrade a union from read-only to read-write:
461 +# mount -t unionfs -o remount,rw none MOUNTPOINT
463 +To delete a branch /foo, regardless where it is in the current union:
465 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
467 +To insert (add) a branch /foo before /bar:
469 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
471 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
473 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
475 +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
476 +new highest-priority branch), you can use the above syntax, or use a short
477 +hand version as follows:
479 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
481 +To append a branch to the very end (new lowest-priority branch):
483 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
485 +To append a branch to the very end (new lowest-priority branch), in
488 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
490 +Finally, to change the mode of one existing branch, say /foo, from read-only
491 +to read-write, and change /bar from read-write to read-only:
493 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
495 +Note: in Unionfs 2.x, you cannot set the leftmost branch to readonly because
496 +then Unionfs won't have any writable place for copyups to take place.
497 +Moreover, the VFS can get confused when it tries to modify something in a
498 +file system mounted read-write, but isn't permitted to write to it.
499 +Instead, you should set the whole union as readonly, as described above.
500 +If, however, you must set the leftmost branch as readonly, perhaps so you
501 +can get a snapshot of it at a point in time, then you should insert a new
502 +writable top-level branch, and mark the one you want as readonly. This can
503 +be accomplished as follows, assuming that /foo is your current leftmost
506 +# mount -t tmpfs -o size=NNN /new
507 +# mount -t unionfs -o remount,add=/new,mode=/foo=ro none MOUNTPOINT
508 +<do what you want safely in /foo>
509 +# mount -t unionfs -o remount,del=/new,mode=/foo=rw none MOUNTPOINT
510 +<check if there's anything in /new you want to preserve>
516 +If you modify any file on any of the lower branches directly, while there is
517 +a Unionfs 2.x mounted above any of those branches, you should tell Unionfs
518 +to purge its caches and re-get the objects. To do that, you have to
519 +increment the generation number of the superblock using the following
522 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
524 +Note that the older way of incrementing the generation number using an
525 +ioctl, is no longer supported in Unionfs 2.0 and newer. Ioctls in general
526 +are not encouraged. Plus, an ioctl is per-file concept, whereas the
527 +generation number is a per-file-system concept. Worse, such an ioctl
528 +requires an open file, which then has to be invalidated by the very nature
529 +of the generation number increase (read: the old generation increase ioctl
533 +For more information, see <http://unionfs.filesystems.org/>.
534 diff --git a/MAINTAINERS b/MAINTAINERS
535 index 8dca9d8..4421543 100644
538 @@ -5095,6 +5095,14 @@ F: Documentation/cdrom/
539 F: drivers/cdrom/cdrom.c
540 F: include/linux/cdrom.h
544 +M: ezk@cs.sunysb.edu
545 +L: unionfs@filesystems.org
546 +W: http://unionfs.filesystems.org/
547 +T: git git.kernel.org/pub/scm/linux/kernel/git/ezk/unionfs.git
550 UNSORTED BLOCK IMAGES (UBI)
551 M: Artem Bityutskiy <dedekind@infradead.org>
552 W: http://www.linux-mtd.infradead.org/
553 diff --git a/fs/Kconfig b/fs/Kconfig
554 index 0e7da7b..b69b6bd 100644
557 @@ -167,6 +167,7 @@ if MISC_FILESYSTEMS
558 source "fs/adfs/Kconfig"
559 source "fs/affs/Kconfig"
560 source "fs/ecryptfs/Kconfig"
561 +source "fs/unionfs/Kconfig"
562 source "fs/hfs/Kconfig"
563 source "fs/hfsplus/Kconfig"
564 source "fs/befs/Kconfig"
565 diff --git a/fs/Makefile b/fs/Makefile
566 index af6d047..6c254d5 100644
569 @@ -84,6 +84,7 @@ obj-$(CONFIG_ISO9660_FS) += isofs/
570 obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
571 obj-$(CONFIG_HFS_FS) += hfs/
572 obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
573 +obj-$(CONFIG_UNION_FS) += unionfs/
574 obj-$(CONFIG_VXFS_FS) += freevxfs/
575 obj-$(CONFIG_NFS_FS) += nfs/
576 obj-$(CONFIG_EXPORTFS) += exportfs/
577 diff --git a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
578 index 2dda5ad..8f006a0 100644
579 --- a/fs/ecryptfs/dentry.c
580 +++ b/fs/ecryptfs/dentry.c
581 @@ -62,7 +62,7 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
582 struct inode *lower_inode =
583 ecryptfs_inode_to_lower(dentry->d_inode);
585 - fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
586 + fsstack_copy_attr_all(dentry->d_inode, lower_inode);
590 diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
591 index 2f0945d..e884c3b 100644
592 --- a/fs/ecryptfs/inode.c
593 +++ b/fs/ecryptfs/inode.c
594 @@ -624,9 +624,9 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
595 lower_new_dir_dentry->d_inode, lower_new_dentry);
598 - fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
599 + fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
600 if (new_dir != old_dir)
601 - fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
602 + fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
604 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
605 dput(lower_new_dentry->d_parent);
606 @@ -965,7 +965,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
607 rc = notify_change(lower_dentry, ia);
608 mutex_unlock(&lower_dentry->d_inode->i_mutex);
610 - fsstack_copy_attr_all(inode, lower_inode, NULL);
611 + fsstack_copy_attr_all(inode, lower_inode);
615 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
616 index 9f0aa98..3d94155 100644
617 --- a/fs/ecryptfs/main.c
618 +++ b/fs/ecryptfs/main.c
619 @@ -190,7 +190,7 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
620 init_special_inode(inode, lower_inode->i_mode,
621 lower_inode->i_rdev);
622 dentry->d_op = &ecryptfs_dops;
623 - fsstack_copy_attr_all(inode, lower_inode, NULL);
624 + fsstack_copy_attr_all(inode, lower_inode);
625 /* This size will be overwritten for real files w/ headers and
627 fsstack_copy_inode_size(inode, lower_inode);
628 diff --git a/fs/namei.c b/fs/namei.c
629 index 1f13751..88584c4 100644
632 @@ -375,6 +375,7 @@ void release_open_intent(struct nameidata *nd)
634 fput(nd->intent.open.file);
636 +EXPORT_SYMBOL_GPL(release_open_intent);
638 static inline struct dentry *
639 do_revalidate(struct dentry *dentry, struct nameidata *nd)
640 diff --git a/fs/splice.c b/fs/splice.c
641 index 73766d2..3753029 100644
644 @@ -1057,8 +1057,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
646 * Attempt to initiate a splice from pipe to file.
648 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
649 - loff_t *ppos, size_t len, unsigned int flags)
650 +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
651 + loff_t *ppos, size_t len, unsigned int flags)
653 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
654 loff_t *, size_t, unsigned int);
655 @@ -1080,13 +1080,14 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
657 return splice_write(pipe, out, ppos, len, flags);
659 +EXPORT_SYMBOL_GPL(vfs_splice_from);
662 * Attempt to initiate a splice from a file to a pipe.
664 -static long do_splice_to(struct file *in, loff_t *ppos,
665 - struct pipe_inode_info *pipe, size_t len,
666 - unsigned int flags)
667 +long vfs_splice_to(struct file *in, loff_t *ppos,
668 + struct pipe_inode_info *pipe, size_t len,
669 + unsigned int flags)
671 ssize_t (*splice_read)(struct file *, loff_t *,
672 struct pipe_inode_info *, size_t, unsigned int);
673 @@ -1105,6 +1106,7 @@ static long do_splice_to(struct file *in, loff_t *ppos,
675 return splice_read(in, ppos, pipe, len, flags);
677 +EXPORT_SYMBOL_GPL(vfs_splice_to);
680 * splice_direct_to_actor - splices data directly between two non-pipes
681 @@ -1174,7 +1176,7 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
683 loff_t pos = sd->pos, prev_pos = pos;
685 - ret = do_splice_to(in, &pos, pipe, len, flags);
686 + ret = vfs_splice_to(in, &pos, pipe, len, flags);
687 if (unlikely(ret <= 0))
690 @@ -1233,7 +1235,7 @@ static int direct_splice_actor(struct pipe_inode_info *pipe,
692 struct file *file = sd->u.file;
694 - return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
695 + return vfs_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
699 @@ -1330,7 +1332,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
703 - ret = do_splice_from(ipipe, out, off, len, flags);
704 + ret = vfs_splice_from(ipipe, out, off, len, flags);
706 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
708 @@ -1350,7 +1352,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
712 - ret = do_splice_to(in, off, opipe, len, flags);
713 + ret = vfs_splice_to(in, off, opipe, len, flags);
715 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
717 diff --git a/fs/stack.c b/fs/stack.c
718 index 67716f6..cc1443d 100644
723 + * Copyright (c) 2006-2009 Erez Zadok
724 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
725 + * Copyright (c) 2006-2009 Stony Brook University
726 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
728 + * This program is free software; you can redistribute it and/or modify
729 + * it under the terms of the GNU General Public License version 2 as
730 + * published by the Free Software Foundation.
733 #include <linux/module.h>
734 #include <linux/fs.h>
735 #include <linux/fs_stack.h>
737 -/* does _NOT_ require i_mutex to be held.
739 + * does _NOT_ require i_mutex to be held.
741 * This function cannot be inlined since i_size_{read,write} is rather
742 * heavy-weight on 32-bit systems
744 -void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
745 +void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
747 - i_size_write(dst, i_size_read((struct inode *)src));
748 - dst->i_blocks = src->i_blocks;
753 + * i_size_read() includes its own seqlocking and protection from
754 + * preemption (see include/linux/fs.h): we need nothing extra for
755 + * that here, and prefer to avoid nesting locks than attempt to
756 + * keep i_size and i_blocks in synch together.
758 + i_size = i_size_read(src);
761 + * But if CONFIG_LSF (on 32-bit), we ought to make an effort to keep
762 + * the two halves of i_blocks in synch despite SMP or PREEMPT - though
763 + * stat's generic_fillattr() doesn't bother, and we won't be applying
764 + * quotas (where i_blocks does become important) at the upper level.
766 + * We don't actually know what locking is used at the lower level; but
767 + * if it's a filesystem that supports quotas, it will be using i_lock
768 + * as in inode_add_bytes(). tmpfs uses other locking, and its 32-bit
769 + * is (just) able to exceed 2TB i_size with the aid of holes; but its
770 + * i_blocks cannot carry into the upper long without almost 2TB swap -
771 + * let's ignore that case.
773 + if (sizeof(i_blocks) > sizeof(long))
774 + spin_lock(&src->i_lock);
775 + i_blocks = src->i_blocks;
776 + if (sizeof(i_blocks) > sizeof(long))
777 + spin_unlock(&src->i_lock);
780 + * If CONFIG_SMP on 32-bit, it's vital for fsstack_copy_inode_size()
781 + * to hold some lock around i_size_write(), otherwise i_size_read()
782 + * may spin forever (see include/linux/fs.h). We don't necessarily
783 + * hold i_mutex when this is called, so take i_lock for that case.
785 + * And if CONFIG_LSF (on 32-bit), continue our effort to keep the
786 + * two halves of i_blocks in synch despite SMP or PREEMPT: use i_lock
787 + * for that case too, and do both at once by combining the tests.
789 + * There is none of this locking overhead in the 64-bit case.
791 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
792 + spin_lock(&dst->i_lock);
793 + i_size_write(dst, i_size);
794 + dst->i_blocks = i_blocks;
795 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
796 + spin_unlock(&dst->i_lock);
798 EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
800 -/* copy all attributes; get_nlinks is optional way to override the i_nlink
802 + * copy all attributes; get_nlinks is optional way to override the i_nlink
805 -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
806 - int (*get_nlinks)(struct inode *))
807 +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
809 dest->i_mode = src->i_mode;
810 dest->i_uid = src->i_uid;
811 @@ -29,14 +87,6 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
812 dest->i_ctime = src->i_ctime;
813 dest->i_blkbits = src->i_blkbits;
814 dest->i_flags = src->i_flags;
817 - * Update the nlinks AFTER updating the above fields, because the
818 - * get_links callback may depend on them.
821 - dest->i_nlink = src->i_nlink;
823 - dest->i_nlink = (*get_nlinks)(dest);
824 + dest->i_nlink = src->i_nlink;
826 EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
827 diff --git a/fs/unionfs/Kconfig b/fs/unionfs/Kconfig
829 index 0000000..f3c1ac4
831 +++ b/fs/unionfs/Kconfig
834 + tristate "Union file system (EXPERIMENTAL)"
835 + depends on EXPERIMENTAL
837 + Unionfs is a stackable unification file system, which appears to
838 + merge the contents of several directories (branches), while keeping
839 + their physical content separate.
841 + See <http://unionfs.filesystems.org> for details
843 +config UNION_FS_XATTR
844 + bool "Unionfs extended attributes"
845 + depends on UNION_FS
847 + Extended attributes are name:value pairs associated with inodes by
848 + the kernel or by users (see the attr(5) manual page).
852 +config UNION_FS_DEBUG
853 + bool "Debug Unionfs"
854 + depends on UNION_FS
856 + If you say Y here, you can turn on debugging output from Unionfs.
857 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
859 index 0000000..9c466a5
861 +++ b/fs/unionfs/Makefile
863 +UNIONFS_VERSION="2.5.3 (for 2.6.31)"
865 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
867 +obj-$(CONFIG_UNION_FS) += unionfs.o
869 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
870 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
871 + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
873 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
875 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
877 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
878 +EXTRA_CFLAGS += -DDEBUG
880 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
882 index 0000000..587f984
884 +++ b/fs/unionfs/commonfops.c
887 + * Copyright (c) 2003-2009 Erez Zadok
888 + * Copyright (c) 2003-2006 Charles P. Wright
889 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
890 + * Copyright (c) 2005-2006 Junjiro Okajima
891 + * Copyright (c) 2005 Arun M. Krishnakumar
892 + * Copyright (c) 2004-2006 David P. Quigley
893 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
894 + * Copyright (c) 2003 Puja Gupta
895 + * Copyright (c) 2003 Harikesavan Krishnan
896 + * Copyright (c) 2003-2009 Stony Brook University
897 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
899 + * This program is free software; you can redistribute it and/or modify
900 + * it under the terms of the GNU General Public License version 2 as
901 + * published by the Free Software Foundation.
907 + * 1) Copyup the file
908 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
909 + * stolen from NFS's silly rename
911 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
912 + struct dentry *parent, int bstart, int bindex)
914 + static unsigned int counter;
915 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
916 + const int countersize = sizeof(counter) * 2;
917 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
918 + char name[nlen + 1];
920 + struct dentry *tmp_dentry = NULL;
921 + struct dentry *lower_dentry;
922 + struct dentry *lower_dir_dentry = NULL;
924 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
926 + sprintf(name, ".unionfs%*.*lx",
927 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
930 + * Loop, looking for an unused temp name to copyup to.
932 + * It's somewhat silly that we look for a free temp tmp name in the
933 + * source branch (bstart) instead of the dest branch (bindex), where
934 + * the final name will be created. We _will_ catch it if somehow
935 + * the name exists in the dest branch, but it'd be nice to catch it
936 + * sooner than later.
941 + char *suffix = name + nlen - countersize;
945 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
947 + pr_debug("unionfs: trying to rename %s to %s\n",
948 + dentry->d_name.name, name);
950 + tmp_dentry = lookup_lck_len(name, lower_dentry->d_parent,
952 + if (IS_ERR(tmp_dentry)) {
953 + err = PTR_ERR(tmp_dentry);
956 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
959 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
960 + i_size_read(file->f_path.dentry->d_inode));
962 + if (unlikely(err == -EEXIST))
967 + /* bring it to the same state as an unlinked file */
968 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
969 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
970 + atomic_inc(&lower_dentry->d_inode->i_count);
971 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
972 + lower_dentry->d_inode);
974 + lower_dir_dentry = lock_parent(lower_dentry);
975 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
976 + unlock_dir(lower_dir_dentry);
980 + unionfs_check_dentry(dentry);
985 + * put all references held by upper struct file and free lower file pointer
988 +static void cleanup_file(struct file *file)
990 + int bindex, bstart, bend;
991 + struct file **lower_files;
992 + struct file *lower_file;
993 + struct super_block *sb = file->f_path.dentry->d_sb;
995 + lower_files = UNIONFS_F(file)->lower_files;
996 + bstart = fbstart(file);
997 + bend = fbend(file);
999 + for (bindex = bstart; bindex <= bend; bindex++) {
1000 + int i; /* holds (possibly) updated branch index */
1003 + lower_file = unionfs_lower_file_idx(file, bindex);
1008 + * Find new index of matching branch with an open
1009 + * file, since branches could have been added or
1010 + * deleted causing the one with open files to shift.
1012 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
1013 + i = branch_id_to_idx(sb, old_bid);
1014 + if (unlikely(i < 0)) {
1015 + printk(KERN_ERR "unionfs: no superblock for "
1016 + "file %p\n", file);
1020 + /* decrement count of open files */
1023 + * fput will perform an mntput for us on the correct branch.
1024 + * Although we're using the file's old branch configuration,
1025 + * bindex, which is the old index, correctly points to the
1026 + * right branch in the file's branch list. In other words,
1027 + * we're going to mntput the correct branch even if branches
1028 + * have been added/removed.
1031 + UNIONFS_F(file)->lower_files[bindex] = NULL;
1032 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
1035 + UNIONFS_F(file)->lower_files = NULL;
1036 + kfree(lower_files);
1037 + kfree(UNIONFS_F(file)->saved_branch_ids);
1038 + /* set to NULL because caller needs to know if to kfree on error */
1039 + UNIONFS_F(file)->saved_branch_ids = NULL;
1042 +/* open all lower files for a given file */
1043 +static int open_all_files(struct file *file)
1045 + int bindex, bstart, bend, err = 0;
1046 + struct file *lower_file;
1047 + struct dentry *lower_dentry;
1048 + struct dentry *dentry = file->f_path.dentry;
1049 + struct super_block *sb = dentry->d_sb;
1051 + bstart = dbstart(dentry);
1052 + bend = dbend(dentry);
1054 + for (bindex = bstart; bindex <= bend; bindex++) {
1055 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1056 + if (!lower_dentry)
1059 + dget(lower_dentry);
1060 + unionfs_mntget(dentry, bindex);
1061 + branchget(sb, bindex);
1064 + dentry_open(lower_dentry,
1065 + unionfs_lower_mnt_idx(dentry, bindex),
1066 + file->f_flags, current_cred());
1067 + if (IS_ERR(lower_file)) {
1068 + branchput(sb, bindex);
1069 + err = PTR_ERR(lower_file);
1072 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1079 +/* open the highest priority file for a given upper file */
1080 +static int open_highest_file(struct file *file, bool willwrite)
1082 + int bindex, bstart, bend, err = 0;
1083 + struct file *lower_file;
1084 + struct dentry *lower_dentry;
1085 + struct dentry *dentry = file->f_path.dentry;
1086 + struct dentry *parent = dget_parent(dentry);
1087 + struct inode *parent_inode = parent->d_inode;
1088 + struct super_block *sb = dentry->d_sb;
1090 + bstart = dbstart(dentry);
1091 + bend = dbend(dentry);
1093 + lower_dentry = unionfs_lower_dentry(dentry);
1094 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
1095 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1096 + err = copyup_file(parent_inode, file, bstart, bindex,
1097 + i_size_read(dentry->d_inode));
1101 + atomic_set(&UNIONFS_F(file)->generation,
1102 + atomic_read(&UNIONFS_I(dentry->d_inode)->
1107 + dget(lower_dentry);
1108 + unionfs_mntget(dentry, bstart);
1109 + lower_file = dentry_open(lower_dentry,
1110 + unionfs_lower_mnt_idx(dentry, bstart),
1111 + file->f_flags, current_cred());
1112 + if (IS_ERR(lower_file)) {
1113 + err = PTR_ERR(lower_file);
1116 + branchget(sb, bstart);
1117 + unionfs_set_lower_file(file, lower_file);
1118 + /* Fix up the position. */
1119 + lower_file->f_pos = file->f_pos;
1121 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1127 +/* perform a delayed copyup of a read-write file on a read-only branch */
1128 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1130 + int bindex, bstart, bend, err = 0;
1131 + struct dentry *dentry = file->f_path.dentry;
1132 + struct inode *parent_inode = parent->d_inode;
1134 + bstart = fbstart(file);
1135 + bend = fbend(file);
1137 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1139 + unionfs_check_file(file);
1140 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1141 + if (!d_deleted(dentry))
1142 + err = copyup_file(parent_inode, file, bstart,
1144 + i_size_read(dentry->d_inode));
1146 + err = copyup_deleted_file(file, dentry, parent,
1148 + /* if succeeded, set lower open-file flags and break */
1150 + struct file *lower_file;
1151 + lower_file = unionfs_lower_file_idx(file, bindex);
1152 + lower_file->f_flags = file->f_flags;
1156 + if (err || (bstart <= fbstart(file)))
1158 + bend = fbend(file);
1159 + for (bindex = bstart; bindex <= bend; bindex++) {
1160 + if (unionfs_lower_file_idx(file, bindex)) {
1161 + branchput(dentry->d_sb, bindex);
1162 + fput(unionfs_lower_file_idx(file, bindex));
1163 + unionfs_set_lower_file_idx(file, bindex, NULL);
1166 + path_put_lowers(dentry, bstart, bend, false);
1167 + iput_lowers(dentry->d_inode, bstart, bend, false);
1168 + /* for reg file, we only open it "once" */
1169 + fbend(file) = fbstart(file);
1170 + dbend(dentry) = dbstart(dentry);
1171 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1174 + unionfs_check_file(file);
1179 + * Helper function for unionfs_file_revalidate/locked.
1180 + * Expects dentry/parent to be locked already, and revalidated.
1182 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1183 + struct dentry *parent,
1184 + struct super_block *sb, int sbgen,
1185 + int dgen, bool willwrite)
1188 + int bstart, bend, orig_brid;
1192 + fgen = atomic_read(&UNIONFS_F(file)->generation);
1195 + * There are two cases we are interested in. The first is if the
1196 + * generation is lower than the super-block. The second is if
1197 + * someone has copied up this file from underneath us, we also need
1198 + * to refresh things.
1200 + if (d_deleted(dentry) ||
1202 + dbstart(dentry) == fbstart(file) &&
1203 + unionfs_lower_file(file)))
1204 + goto out_may_copyup;
1206 + /* save orig branch ID */
1207 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1209 + /* First we throw out the existing files. */
1210 + cleanup_file(file);
1212 + /* Now we reopen the file(s) as in unionfs_open. */
1213 + bstart = fbstart(file) = dbstart(dentry);
1214 + bend = fbend(file) = dbend(dentry);
1216 + size = sizeof(struct file *) * sbmax(sb);
1217 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1218 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1222 + size = sizeof(int) * sbmax(sb);
1223 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1224 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1229 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1230 + /* We need to open all the files. */
1231 + err = open_all_files(file);
1236 + /* We only open the highest priority branch. */
1237 + err = open_highest_file(file, willwrite);
1240 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1241 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1243 + * If we re-opened the file on a different branch
1244 + * than the original one, and this was due to a new
1245 + * branch inserted, then update the mnt counts of
1246 + * the old and new branches accordingly.
1248 + unionfs_mntget(dentry, bstart);
1249 + unionfs_mntput(sb->s_root,
1250 + branch_id_to_idx(sb, orig_brid));
1252 + /* regular files have only one open lower file */
1253 + fbend(file) = fbstart(file);
1255 + atomic_set(&UNIONFS_F(file)->generation,
1256 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1259 + /* Copyup on the first write to a file on a readonly branch. */
1260 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1261 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1262 + is_robranch(dentry)) {
1263 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1264 + dentry->d_name.name);
1265 + err = do_delayed_copyup(file, parent);
1266 + /* regular files have only one open lower file */
1267 + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1268 + fbend(file) = fbstart(file);
1273 + kfree(UNIONFS_F(file)->lower_files);
1274 + kfree(UNIONFS_F(file)->saved_branch_ids);
1280 + * Revalidate the struct file
1281 + * @file: file to revalidate
1282 + * @parent: parent dentry (locked by caller)
1283 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1284 + * Caller must lock/unlock dentry's branch configuration.
1286 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1289 + struct super_block *sb;
1290 + struct dentry *dentry;
1294 + dentry = file->f_path.dentry;
1295 + sb = dentry->d_sb;
1296 + verify_locked(dentry);
1297 + verify_locked(parent);
1300 + * First revalidate the dentry inside struct file,
1301 + * but not unhashed dentries.
1303 + if (!d_deleted(dentry) &&
1304 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1309 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1310 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1312 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1313 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1314 + dentry->d_name.name);
1319 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1320 + sbgen, dgen, willwrite);
1325 +/* unionfs_open helper function: open a directory */
1326 +static int __open_dir(struct inode *inode, struct file *file)
1328 + struct dentry *lower_dentry;
1329 + struct file *lower_file;
1330 + int bindex, bstart, bend;
1331 + struct vfsmount *mnt;
1333 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1334 + bend = fbend(file) = dbend(file->f_path.dentry);
1336 + for (bindex = bstart; bindex <= bend; bindex++) {
1338 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1339 + if (!lower_dentry)
1342 + dget(lower_dentry);
1343 + unionfs_mntget(file->f_path.dentry, bindex);
1344 + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1345 + lower_file = dentry_open(lower_dentry, mnt, file->f_flags,
1347 + if (IS_ERR(lower_file))
1348 + return PTR_ERR(lower_file);
1350 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1353 + * The branchget goes after the open, because otherwise
1354 + * we would miss the reference on release.
1356 + branchget(inode->i_sb, bindex);
1362 +/* unionfs_open helper function: open a file */
1363 +static int __open_file(struct inode *inode, struct file *file,
1364 + struct dentry *parent)
1366 + struct dentry *lower_dentry;
1367 + struct file *lower_file;
1369 + int bindex, bstart, bend;
1371 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1372 + lower_flags = file->f_flags;
1374 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1375 + bend = fbend(file) = dbend(file->f_path.dentry);
1378 + * check for the permission for lower file. If the error is
1379 + * COPYUP_ERR, copyup the file.
1381 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1383 + * if the open will change the file, copy it up otherwise
1386 + if (lower_flags & O_TRUNC) {
1390 + /* copyup the file */
1391 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1392 + err = copyup_file(parent->d_inode, file,
1393 + bstart, bindex, size);
1400 + * turn off writeable flags, to force delayed copyup
1403 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1407 + dget(lower_dentry);
1410 + * dentry_open will decrement mnt refcnt if err.
1411 + * otherwise fput() will do an mntput() for us upon file close.
1413 + unionfs_mntget(file->f_path.dentry, bstart);
1415 + dentry_open(lower_dentry,
1416 + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1417 + lower_flags, current_cred());
1418 + if (IS_ERR(lower_file))
1419 + return PTR_ERR(lower_file);
1421 + unionfs_set_lower_file(file, lower_file);
1422 + branchget(inode->i_sb, bstart);
1427 +int unionfs_open(struct inode *inode, struct file *file)
1430 + struct file *lower_file = NULL;
1431 + struct dentry *dentry = file->f_path.dentry;
1432 + struct dentry *parent;
1433 + int bindex = 0, bstart = 0, bend = 0;
1437 + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1438 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1439 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1441 + /* don't open unhashed/deleted files */
1442 + if (d_deleted(dentry)) {
1447 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1448 + valid = __unionfs_d_revalidate(dentry, parent, false);
1449 + if (unlikely(!valid)) {
1454 + file->private_data =
1455 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1456 + if (unlikely(!UNIONFS_F(file))) {
1460 + fbstart(file) = -1;
1462 + atomic_set(&UNIONFS_F(file)->generation,
1463 + atomic_read(&UNIONFS_I(inode)->generation));
1465 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1466 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1467 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1471 + size = sizeof(int) * sbmax(inode->i_sb);
1472 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1473 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1478 + bstart = fbstart(file) = dbstart(dentry);
1479 + bend = fbend(file) = dbend(dentry);
1482 + * open all directories and make the unionfs file struct point to
1483 + * these lower file structs
1485 + if (S_ISDIR(inode->i_mode))
1486 + err = __open_dir(inode, file); /* open a dir */
1488 + err = __open_file(inode, file, parent); /* open a file */
1490 + /* freeing the allocated resources, and fput the opened files */
1492 + for (bindex = bstart; bindex <= bend; bindex++) {
1493 + lower_file = unionfs_lower_file_idx(file, bindex);
1497 + branchput(dentry->d_sb, bindex);
1498 + /* fput calls dput for lower_dentry */
1505 + kfree(UNIONFS_F(file)->lower_files);
1506 + kfree(UNIONFS_F(file)->saved_branch_ids);
1507 + kfree(UNIONFS_F(file));
1511 + unionfs_postcopyup_setmnt(dentry);
1512 + unionfs_copy_attr_times(inode);
1513 + unionfs_check_file(file);
1514 + unionfs_check_inode(inode);
1516 + unionfs_unlock_dentry(dentry);
1517 + unionfs_unlock_parent(dentry, parent);
1518 + unionfs_read_unlock(inode->i_sb);
1523 + * release all lower object references & free the file info structure
1525 + * No need to grab sb info's rwsem.
1527 +int unionfs_file_release(struct inode *inode, struct file *file)
1529 + struct file *lower_file = NULL;
1530 + struct unionfs_file_info *fileinfo;
1531 + struct unionfs_inode_info *inodeinfo;
1532 + struct super_block *sb = inode->i_sb;
1533 + struct dentry *dentry = file->f_path.dentry;
1534 + struct dentry *parent;
1535 + int bindex, bstart, bend;
1536 + int fgen, err = 0;
1539 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
1540 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
1541 + * has been causing false positives in file system stacking layers.
1542 + * In particular, our ->mmap is called after sys_mmap2 already holds
1543 + * mmap_sem, then we lock our own mutexes; but earlier, it's
1544 + * possible for lockdep to have locked our mutexes first, and then
1545 + * we call a lower ->readdir which could call might_fault. The
1546 + * different ordering of the locks is what lockdep complains about
1547 + * -- unnecessarily. Therefore, we have no choice but to tell
1548 + * lockdep to temporarily turn off lockdep here. Note: the comments
1549 + * inside might_sleep also suggest that it would have been
1550 + * nicer to only annotate paths that needs that might_lock_read.
1553 + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1554 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1555 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1558 + * We try to revalidate, but the VFS ignores return return values
1559 + * from file->release, so we must always try to succeed here,
1560 + * including to do the kfree and dput below. So if revalidation
1561 + * failed, all we can do is print some message and keep going.
1563 + err = unionfs_file_revalidate(file, parent,
1564 + UNIONFS_F(file)->wrote_to_file);
1566 + unionfs_check_file(file);
1567 + fileinfo = UNIONFS_F(file);
1568 + BUG_ON(file->f_path.dentry->d_inode != inode);
1569 + inodeinfo = UNIONFS_I(inode);
1571 + /* fput all the lower files */
1572 + fgen = atomic_read(&fileinfo->generation);
1573 + bstart = fbstart(file);
1574 + bend = fbend(file);
1576 + for (bindex = bstart; bindex <= bend; bindex++) {
1577 + lower_file = unionfs_lower_file_idx(file, bindex);
1580 + unionfs_set_lower_file_idx(file, bindex, NULL);
1582 + branchput(sb, bindex);
1585 + /* if there are no more refs to the dentry, dput it */
1586 + if (d_deleted(dentry)) {
1587 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1588 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1592 + kfree(fileinfo->lower_files);
1593 + kfree(fileinfo->saved_branch_ids);
1595 + if (fileinfo->rdstate) {
1596 + fileinfo->rdstate->access = jiffies;
1597 + spin_lock(&inodeinfo->rdlock);
1598 + inodeinfo->rdcount++;
1599 + list_add_tail(&fileinfo->rdstate->cache,
1600 + &inodeinfo->readdircache);
1601 + mark_inode_dirty(inode);
1602 + spin_unlock(&inodeinfo->rdlock);
1603 + fileinfo->rdstate = NULL;
1607 + unionfs_unlock_dentry(dentry);
1608 + unionfs_unlock_parent(dentry, parent);
1609 + unionfs_read_unlock(sb);
1614 +/* pass the ioctl to the lower fs */
1615 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1617 + struct file *lower_file;
1620 + lower_file = unionfs_lower_file(file);
1623 + if (!lower_file || !lower_file->f_op)
1625 + if (lower_file->f_op->unlocked_ioctl) {
1626 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1627 + } else if (lower_file->f_op->ioctl) {
1629 + err = lower_file->f_op->ioctl(
1630 + lower_file->f_path.dentry->d_inode,
1631 + lower_file, cmd, arg);
1640 + * return to user-space the branch indices containing the file in question
1642 + * We use fd_set and therefore we are limited to the number of the branches
1643 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1645 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1646 + unsigned int cmd, unsigned long arg)
1649 + fd_set branchlist;
1650 + int bstart = 0, bend = 0, bindex = 0;
1651 + int orig_bstart, orig_bend;
1652 + struct dentry *dentry, *lower_dentry;
1653 + struct vfsmount *mnt;
1655 + dentry = file->f_path.dentry;
1656 + orig_bstart = dbstart(dentry);
1657 + orig_bend = dbend(dentry);
1658 + err = unionfs_partial_lookup(dentry, parent);
1661 + bstart = dbstart(dentry);
1662 + bend = dbend(dentry);
1664 + FD_ZERO(&branchlist);
1666 + for (bindex = bstart; bindex <= bend; bindex++) {
1667 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1668 + if (!lower_dentry)
1670 + if (likely(lower_dentry->d_inode))
1671 + FD_SET(bindex, &branchlist);
1672 + /* purge any lower objects after partial_lookup */
1673 + if (bindex < orig_bstart || bindex > orig_bend) {
1674 + dput(lower_dentry);
1675 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1676 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1677 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1679 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1682 + unionfs_mntput(dentry, bindex);
1683 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1686 + /* restore original dentry's offsets */
1687 + dbstart(dentry) = orig_bstart;
1688 + dbend(dentry) = orig_bend;
1689 + ibstart(dentry->d_inode) = orig_bstart;
1690 + ibend(dentry->d_inode) = orig_bend;
1692 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1693 + if (unlikely(err))
1697 + return err < 0 ? err : bend;
1700 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1703 + struct dentry *dentry = file->f_path.dentry;
1704 + struct dentry *parent;
1706 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1707 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1708 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1710 + err = unionfs_file_revalidate(file, parent, true);
1711 + if (unlikely(err))
1714 + /* check if asked for local commands */
1716 + case UNIONFS_IOCTL_INCGEN:
1717 + /* Increment the superblock generation count */
1718 + pr_info("unionfs: incgen ioctl deprecated; "
1719 + "use \"-o remount,incgen\"\n");
1723 + case UNIONFS_IOCTL_QUERYFILE:
1724 + /* Return list of branches containing the given file */
1725 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1729 + /* pass the ioctl down */
1730 + err = do_ioctl(file, cmd, arg);
1735 + unionfs_check_file(file);
1736 + unionfs_unlock_dentry(dentry);
1737 + unionfs_unlock_parent(dentry, parent);
1738 + unionfs_read_unlock(dentry->d_sb);
1742 +int unionfs_flush(struct file *file, fl_owner_t id)
1745 + struct file *lower_file = NULL;
1746 + struct dentry *dentry = file->f_path.dentry;
1747 + struct dentry *parent;
1748 + int bindex, bstart, bend;
1750 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1751 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1752 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1754 + err = unionfs_file_revalidate(file, parent,
1755 + UNIONFS_F(file)->wrote_to_file);
1756 + if (unlikely(err))
1758 + unionfs_check_file(file);
1760 + bstart = fbstart(file);
1761 + bend = fbend(file);
1762 + for (bindex = bstart; bindex <= bend; bindex++) {
1763 + lower_file = unionfs_lower_file_idx(file, bindex);
1765 + if (lower_file && lower_file->f_op &&
1766 + lower_file->f_op->flush) {
1767 + err = lower_file->f_op->flush(lower_file, id);
1776 + unionfs_check_file(file);
1777 + unionfs_unlock_dentry(dentry);
1778 + unionfs_unlock_parent(dentry, parent);
1779 + unionfs_read_unlock(dentry->d_sb);
1782 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1783 new file mode 100644
1784 index 0000000..c43cc7f
1786 +++ b/fs/unionfs/copyup.c
1789 + * Copyright (c) 2003-2009 Erez Zadok
1790 + * Copyright (c) 2003-2006 Charles P. Wright
1791 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1792 + * Copyright (c) 2005-2006 Junjiro Okajima
1793 + * Copyright (c) 2005 Arun M. Krishnakumar
1794 + * Copyright (c) 2004-2006 David P. Quigley
1795 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1796 + * Copyright (c) 2003 Puja Gupta
1797 + * Copyright (c) 2003 Harikesavan Krishnan
1798 + * Copyright (c) 2003-2009 Stony Brook University
1799 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
1801 + * This program is free software; you can redistribute it and/or modify
1802 + * it under the terms of the GNU General Public License version 2 as
1803 + * published by the Free Software Foundation.
1809 + * For detailed explanation of copyup see:
1810 + * Documentation/filesystems/unionfs/concepts.txt
1813 +#ifdef CONFIG_UNION_FS_XATTR
1814 +/* copyup all extended attrs for a given dentry */
1815 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1816 + struct dentry *new_lower_dentry)
1819 + ssize_t list_size = -1;
1820 + char *name_list = NULL;
1821 + char *attr_value = NULL;
1822 + char *name_list_buf = NULL;
1824 + /* query the actual size of the xattr list */
1825 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1826 + if (list_size <= 0) {
1831 + /* allocate space for the actual list */
1832 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1833 + if (unlikely(!name_list || IS_ERR(name_list))) {
1834 + err = PTR_ERR(name_list);
1838 + name_list_buf = name_list; /* save for kfree at end */
1840 + /* now get the actual xattr list of the source file */
1841 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1842 + if (list_size <= 0) {
1847 + /* allocate space to hold each xattr's value */
1848 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1849 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1850 + err = PTR_ERR(name_list);
1854 + /* in a loop, get and set each xattr from src to dst file */
1855 + while (*name_list) {
1858 + /* Lock here since vfs_getxattr doesn't lock for us */
1859 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1860 + size = vfs_getxattr(old_lower_dentry, name_list,
1861 + attr_value, XATTR_SIZE_MAX);
1862 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1867 + if (size > XATTR_SIZE_MAX) {
1871 + /* Don't lock here since vfs_setxattr does it for us. */
1872 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1875 + * Selinux depends on "security.*" xattrs, so to maintain
1876 + * the security of copied-up files, if Selinux is active,
1877 + * then we must copy these xattrs as well. So we need to
1878 + * temporarily get FOWNER privileges.
1879 + * XXX: move entire copyup code to SIOQ.
1881 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1882 + const struct cred *old_creds;
1883 + struct cred *new_creds;
1885 + new_creds = prepare_creds();
1886 + if (unlikely(!new_creds)) {
1890 + cap_raise(new_creds->cap_effective, CAP_FOWNER);
1891 + old_creds = override_creds(new_creds);
1892 + err = vfs_setxattr(new_lower_dentry, name_list,
1893 + attr_value, size, 0);
1894 + revert_creds(old_creds);
1898 + name_list += strlen(name_list) + 1;
1901 + unionfs_xattr_kfree(name_list_buf);
1902 + unionfs_xattr_kfree(attr_value);
1903 + /* Ignore if xattr isn't supported */
1904 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1908 +#endif /* CONFIG_UNION_FS_XATTR */
1911 + * Determine the mode based on the copyup flags, and the existing dentry.
1913 + * Handle file systems which may not support certain options. For example
1914 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1915 + * errors, rather than propagating them up, which results in copyup errors
1916 + * and errors returned back to users.
1918 +static int copyup_permissions(struct super_block *sb,
1919 + struct dentry *old_lower_dentry,
1920 + struct dentry *new_lower_dentry)
1922 + struct inode *i = old_lower_dentry->d_inode;
1923 + struct iattr newattrs;
1926 + newattrs.ia_atime = i->i_atime;
1927 + newattrs.ia_mtime = i->i_mtime;
1928 + newattrs.ia_ctime = i->i_ctime;
1929 + newattrs.ia_gid = i->i_gid;
1930 + newattrs.ia_uid = i->i_uid;
1931 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1932 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1933 + ATTR_GID | ATTR_UID;
1934 + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1935 + err = notify_change(new_lower_dentry, &newattrs);
1939 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1940 + newattrs.ia_mode = i->i_mode;
1941 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1942 + err = notify_change(new_lower_dentry, &newattrs);
1943 + if (err == -EOPNOTSUPP &&
1944 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1945 + printk(KERN_WARNING
1946 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1947 + new_lower_dentry->d_name.name);
1952 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1957 + * create the new device/file/directory - use copyup_permission to copyup
1960 + * if the object being copied up is a regular file, the file is only created,
1961 + * the contents have to be copied up separately
1963 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1964 + struct dentry *new_lower_dentry,
1965 + struct dentry *new_lower_parent_dentry,
1969 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1970 + struct sioq_args args;
1972 + if (S_ISDIR(old_mode)) {
1973 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1974 + args.mkdir.dentry = new_lower_dentry;
1975 + args.mkdir.mode = old_mode;
1977 + run_sioq(__unionfs_mkdir, &args);
1979 + } else if (S_ISLNK(old_mode)) {
1980 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1981 + args.symlink.dentry = new_lower_dentry;
1982 + args.symlink.symbuf = symbuf;
1984 + run_sioq(__unionfs_symlink, &args);
1986 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1987 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1988 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1989 + args.mknod.dentry = new_lower_dentry;
1990 + args.mknod.mode = old_mode;
1991 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1993 + run_sioq(__unionfs_mknod, &args);
1995 + } else if (S_ISREG(old_mode)) {
1996 + struct nameidata nd;
1997 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1998 + if (unlikely(err < 0))
2000 + args.create.nd = &nd;
2001 + args.create.parent = new_lower_parent_dentry->d_inode;
2002 + args.create.dentry = new_lower_dentry;
2003 + args.create.mode = old_mode;
2005 + run_sioq(__unionfs_create, &args);
2007 + release_lower_nd(&nd, err);
2009 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
2018 +static int __copyup_reg_data(struct dentry *dentry,
2019 + struct dentry *new_lower_dentry, int new_bindex,
2020 + struct dentry *old_lower_dentry, int old_bindex,
2021 + struct file **copyup_file, loff_t len)
2023 + struct super_block *sb = dentry->d_sb;
2024 + struct file *input_file;
2025 + struct file *output_file;
2026 + struct vfsmount *output_mnt;
2027 + mm_segment_t old_fs;
2029 + ssize_t read_bytes, write_bytes;
2033 + /* open old file */
2034 + unionfs_mntget(dentry, old_bindex);
2035 + branchget(sb, old_bindex);
2036 + /* dentry_open calls dput and mntput if it returns an error */
2037 + input_file = dentry_open(old_lower_dentry,
2038 + unionfs_lower_mnt_idx(dentry, old_bindex),
2039 + O_RDONLY | O_LARGEFILE, current_cred());
2040 + if (IS_ERR(input_file)) {
2041 + dput(old_lower_dentry);
2042 + err = PTR_ERR(input_file);
2045 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
2047 + goto out_close_in;
2050 + /* open new file */
2051 + dget(new_lower_dentry);
2052 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
2053 + branchget(sb, new_bindex);
2054 + output_file = dentry_open(new_lower_dentry, output_mnt,
2055 + O_RDWR | O_LARGEFILE, current_cred());
2056 + if (IS_ERR(output_file)) {
2057 + err = PTR_ERR(output_file);
2058 + goto out_close_in2;
2060 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
2062 + goto out_close_out;
2065 + /* allocating a buffer */
2066 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2067 + if (unlikely(!buf)) {
2069 + goto out_close_out;
2072 + input_file->f_pos = 0;
2073 + output_file->f_pos = 0;
2075 + old_fs = get_fs();
2076 + set_fs(KERNEL_DS);
2081 + if (len >= PAGE_SIZE)
2083 + else if ((len < PAGE_SIZE) && (len > 0))
2089 + input_file->f_op->read(input_file,
2090 + (char __user *)buf, size,
2091 + &input_file->f_pos);
2092 + if (read_bytes <= 0) {
2097 + /* see Documentation/filesystems/unionfs/issues.txt */
2100 + output_file->f_op->write(output_file,
2101 + (char __user *)buf,
2103 + &output_file->f_pos);
2105 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2106 + err = write_bytes;
2109 + } while ((read_bytes > 0) && (len > 0));
2116 + err = output_file->f_op->fsync(output_file,
2117 + new_lower_dentry, 0);
2120 + goto out_close_out;
2122 + if (copyup_file) {
2123 + *copyup_file = output_file;
2124 + goto out_close_in;
2128 + fput(output_file);
2131 + branchput(sb, new_bindex);
2137 + branchput(sb, old_bindex);
2143 + * dput the lower references for old and new dentry & clear a lower dentry
2146 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2147 + int old_bstart, int old_bend,
2148 + struct dentry *new_lower_dentry, int new_bindex)
2150 + /* get rid of the lower dentry and all its traces */
2151 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2152 + dbstart(dentry) = old_bstart;
2153 + dbend(dentry) = old_bend;
2155 + dput(new_lower_dentry);
2156 + dput(old_lower_dentry);
2160 + * Copy up a dentry to a file of specified name.
2162 + * @dir: used to pull the ->i_sb to access other branches
2163 + * @dentry: the non-negative dentry whose lower_inode we should copy
2164 + * @bstart: the branch of the lower_inode to copy from
2165 + * @new_bindex: the branch to create the new file in
2166 + * @name: the name of the file to create
2167 + * @namelen: length of @name
2168 + * @copyup_file: the "struct file" to return (optional)
2169 + * @len: how many bytes to copy-up?
2171 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2172 + int new_bindex, const char *name, int namelen,
2173 + struct file **copyup_file, loff_t len)
2175 + struct dentry *new_lower_dentry;
2176 + struct dentry *old_lower_dentry = NULL;
2177 + struct super_block *sb;
2182 + struct dentry *new_lower_parent_dentry = NULL;
2183 + mm_segment_t oldfs;
2184 + char *symbuf = NULL;
2186 + verify_locked(dentry);
2188 + old_bindex = bstart;
2189 + old_bstart = dbstart(dentry);
2190 + old_bend = dbend(dentry);
2192 + BUG_ON(new_bindex < 0);
2193 + BUG_ON(new_bindex >= old_bindex);
2197 + err = is_robranch_super(sb, new_bindex);
2201 + /* Create the directory structure above this dentry. */
2202 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2203 + if (IS_ERR(new_lower_dentry)) {
2204 + err = PTR_ERR(new_lower_dentry);
2208 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2209 + /* we conditionally dput this old_lower_dentry at end of function */
2210 + dget(old_lower_dentry);
2212 + /* For symlinks, we must read the link before we lock the directory. */
2213 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2215 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2216 + if (unlikely(!symbuf)) {
2217 + __clear(dentry, old_lower_dentry,
2218 + old_bstart, old_bend,
2219 + new_lower_dentry, new_bindex);
2225 + set_fs(KERNEL_DS);
2226 + err = old_lower_dentry->d_inode->i_op->readlink(
2228 + (char __user *)symbuf,
2232 + __clear(dentry, old_lower_dentry,
2233 + old_bstart, old_bend,
2234 + new_lower_dentry, new_bindex);
2237 + symbuf[err] = '\0';
2240 + /* Now we lock the parent, and create the object in the new branch. */
2241 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2243 + /* create the new inode */
2244 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2245 + new_lower_parent_dentry, symbuf);
2248 + __clear(dentry, old_lower_dentry,
2249 + old_bstart, old_bend,
2250 + new_lower_dentry, new_bindex);
2254 + /* We actually copyup the file here. */
2255 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2256 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2257 + old_lower_dentry, old_bindex,
2258 + copyup_file, len);
2262 + /* Set permissions. */
2263 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2267 +#ifdef CONFIG_UNION_FS_XATTR
2268 + /* Selinux uses extended attributes for permissions. */
2269 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2272 +#endif /* CONFIG_UNION_FS_XATTR */
2274 + /* do not allow files getting deleted to be re-interposed */
2275 + if (!d_deleted(dentry))
2276 + unionfs_reinterpose(dentry);
2282 + * copyup failed, because we possibly ran out of space or
2283 + * quota, or something else happened so let's unlink; we don't
2284 + * really care about the return value of vfs_unlink
2286 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2288 + if (copyup_file) {
2289 + /* need to close the file */
2291 + fput(*copyup_file);
2292 + branchput(sb, new_bindex);
2296 + * TODO: should we reset the error to something like -EIO?
2298 + * If we don't reset, the user may get some nonsensical errors, but
2299 + * on the other hand, if we reset to EIO, we guarantee that the user
2300 + * will get a "confusing" error message.
2304 + unlock_dir(new_lower_parent_dentry);
2308 + * If old_lower_dentry was not a file, then we need to dput it. If
2309 + * it was a file, then it was already dput indirectly by other
2310 + * functions we call above which operate on regular files.
2312 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2313 + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2314 + dput(old_lower_dentry);
2319 + * if directory creation succeeded, but inode copyup failed,
2320 + * then purge new dentries.
2322 + if (dbstart(dentry) < old_bstart &&
2323 + ibstart(dentry->d_inode) > dbstart(dentry))
2324 + __clear(dentry, NULL, old_bstart, old_bend,
2325 + unionfs_lower_dentry(dentry), dbstart(dentry));
2328 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2329 + unionfs_postcopyup_release(dentry);
2330 + if (!unionfs_lower_inode(dentry->d_inode)) {
2332 + * If we got here, then we copied up to an
2333 + * unlinked-open file, whose name is .unionfsXXXXX.
2335 + struct inode *inode = new_lower_dentry->d_inode;
2336 + atomic_inc(&inode->i_count);
2337 + unionfs_set_lower_inode_idx(dentry->d_inode,
2338 + ibstart(dentry->d_inode),
2342 + unionfs_postcopyup_setmnt(dentry);
2343 + /* sync inode times from copied-up inode to our inode */
2344 + unionfs_copy_attr_times(dentry->d_inode);
2345 + unionfs_check_inode(dir);
2346 + unionfs_check_dentry(dentry);
2352 + * This function creates a copy of a file represented by 'file' which
2353 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2354 + * will be named "name".
2356 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2357 + int bstart, int new_bindex, loff_t len)
2360 + struct file *output_file = NULL;
2362 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2363 + name, strlen(name), &output_file, len);
2365 + fbstart(file) = new_bindex;
2366 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2373 + * This function creates a copy of a file represented by 'file' which
2374 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2376 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2377 + int new_bindex, loff_t len)
2380 + struct file *output_file = NULL;
2381 + struct dentry *dentry = file->f_path.dentry;
2383 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2384 + dentry->d_name.name, dentry->d_name.len,
2385 + &output_file, len);
2387 + fbstart(file) = new_bindex;
2388 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2394 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2395 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2396 + int old_bstart, int old_bend)
2400 + int new_bstart = -1;
2401 + int new_bend = -1;
2404 + loop_start = min(old_bstart, bindex);
2405 + loop_end = max(old_bend, bindex);
2408 + * This loop sets the bstart and bend for the new dentry by
2409 + * traversing from left to right. It also dputs all negative
2410 + * dentries except bindex
2412 + for (i = loop_start; i <= loop_end; i++) {
2413 + if (!unionfs_lower_dentry_idx(dentry, i))
2416 + if (i == bindex) {
2418 + if (new_bstart < 0)
2423 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2424 + dput(unionfs_lower_dentry_idx(dentry, i));
2425 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2427 + unionfs_mntput(dentry, i);
2428 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2430 + if (new_bstart < 0)
2436 + if (new_bstart < 0)
2437 + new_bstart = bindex;
2439 + new_bend = bindex;
2440 + dbstart(dentry) = new_bstart;
2441 + dbend(dentry) = new_bend;
2445 +/* set lower inode ptr and update bstart & bend if necessary */
2446 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2449 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2450 + igrab(lower->d_inode));
2451 + if (likely(ibstart(upper->d_inode) > bindex))
2452 + ibstart(upper->d_inode) = bindex;
2453 + if (likely(ibend(upper->d_inode) < bindex))
2454 + ibend(upper->d_inode) = bindex;
2458 +/* set lower dentry ptr and update bstart & bend if necessary */
2459 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2462 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2463 + if (likely(dbstart(upper) > bindex))
2464 + dbstart(upper) = bindex;
2465 + if (likely(dbend(upper) < bindex))
2466 + dbend(upper) = bindex;
2470 + * This function replicates the directory structure up-to given dentry
2471 + * in the bindex branch.
2473 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2474 + const char *name, int bindex)
2477 + struct dentry *child_dentry;
2478 + struct dentry *parent_dentry;
2479 + struct dentry *lower_parent_dentry = NULL;
2480 + struct dentry *lower_dentry = NULL;
2481 + const char *childname;
2482 + unsigned int childnamelen;
2487 + struct dentry **path = NULL;
2488 + struct super_block *sb;
2490 + verify_locked(dentry);
2492 + err = is_robranch_super(dir->i_sb, bindex);
2494 + lower_dentry = ERR_PTR(err);
2498 + old_bstart = dbstart(dentry);
2499 + old_bend = dbend(dentry);
2501 + lower_dentry = ERR_PTR(-ENOMEM);
2503 + /* There is no sense allocating any less than the minimum. */
2505 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2506 + if (unlikely(!path))
2509 + /* assume the negative dentry of unionfs as the parent dentry */
2510 + parent_dentry = dentry;
2513 + * This loop finds the first parent that exists in the given branch.
2514 + * We start building the directory structure from there. At the end
2515 + * of the loop, the following should hold:
2516 + * - child_dentry is the first nonexistent child
2517 + * - parent_dentry is the first existent parent
2518 + * - path[0] is the = deepest child
2519 + * - path[count] is the first child to create
2522 + child_dentry = parent_dentry;
2524 + /* find the parent directory dentry in unionfs */
2525 + parent_dentry = dget_parent(child_dentry);
2527 + /* find out the lower_parent_dentry in the given branch */
2528 + lower_parent_dentry =
2529 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2531 + /* grow path table */
2532 + if (count == nr_dentry) {
2536 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2538 + if (unlikely(!p)) {
2539 + lower_dentry = ERR_PTR(-ENOMEM);
2545 + /* store the child dentry */
2546 + path[count++] = child_dentry;
2547 + } while (!lower_parent_dentry);
2550 + sb = dentry->d_sb;
2553 + * This code goes between the begin/end labels and basically
2554 + * emulates a while(child_dentry != dentry), only cleaner and
2555 + * shorter than what would be a much longer while loop.
2558 + /* get lower parent dir in the current branch */
2559 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2560 + dput(parent_dentry);
2562 + /* init the values to lookup */
2563 + childname = child_dentry->d_name.name;
2564 + childnamelen = child_dentry->d_name.len;
2566 + if (child_dentry != dentry) {
2567 + /* lookup child in the underlying file system */
2568 + lower_dentry = lookup_lck_len(childname, lower_parent_dentry,
2570 + if (IS_ERR(lower_dentry))
2574 + * Is the name a whiteout of the child name ? lookup the
2575 + * whiteout child in the underlying file system
2577 + lower_dentry = lookup_lck_len(name, lower_parent_dentry,
2579 + if (IS_ERR(lower_dentry))
2582 + /* Replace the current dentry (if any) with the new one */
2583 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2584 + unionfs_set_lower_dentry_idx(dentry, bindex,
2587 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2591 + if (lower_dentry->d_inode) {
2593 + * since this already exists we dput to avoid
2594 + * multiple references on the same dentry
2596 + dput(lower_dentry);
2598 + struct sioq_args args;
2600 + /* it's a negative dentry, create a new dir */
2601 + lower_parent_dentry = lock_parent(lower_dentry);
2603 + args.mkdir.parent = lower_parent_dentry->d_inode;
2604 + args.mkdir.dentry = lower_dentry;
2605 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2607 + run_sioq(__unionfs_mkdir, &args);
2611 + err = copyup_permissions(dir->i_sb, child_dentry,
2613 + unlock_dir(lower_parent_dentry);
2615 + dput(lower_dentry);
2616 + lower_dentry = ERR_PTR(err);
2622 + __set_inode(child_dentry, lower_dentry, bindex);
2623 + __set_dentry(child_dentry, lower_dentry, bindex);
2625 + * update times of this dentry, but also the parent, because if
2626 + * we changed, the parent may have changed too.
2628 + fsstack_copy_attr_times(parent_dentry->d_inode,
2629 + lower_parent_dentry->d_inode);
2630 + unionfs_copy_attr_times(child_dentry->d_inode);
2632 + parent_dentry = child_dentry;
2633 + child_dentry = path[--count];
2636 + /* cleanup any leftover locks from the do/while loop above */
2637 + if (IS_ERR(lower_dentry))
2639 + dput(path[count--]);
2641 + return lower_dentry;
2645 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2646 + * dentry+parents to the first parent node that has an mnt.
2648 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2650 + struct dentry *parent, *hasone;
2651 + int bindex = dbstart(dentry);
2653 + if (unionfs_lower_mnt_idx(dentry, bindex))
2655 + hasone = dentry->d_parent;
2656 + /* this loop should stop at root dentry */
2657 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2658 + hasone = hasone->d_parent;
2660 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2661 + unionfs_set_lower_mnt_idx(parent, bindex,
2662 + unionfs_mntget(hasone, bindex));
2663 + parent = parent->d_parent;
2668 + * Post-copyup helper to release all non-directory source objects of a
2669 + * copied-up file. Regular files should have only one lower object.
2671 +void unionfs_postcopyup_release(struct dentry *dentry)
2675 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2676 + bstart = dbstart(dentry);
2677 + bend = dbend(dentry);
2679 + path_put_lowers(dentry, bstart + 1, bend, false);
2680 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2682 + dbend(dentry) = bstart;
2683 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2685 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2686 new file mode 100644
2687 index 0000000..3fd641a
2689 +++ b/fs/unionfs/debug.c
2692 + * Copyright (c) 2003-2009 Erez Zadok
2693 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2694 + * Copyright (c) 2003-2009 Stony Brook University
2695 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
2697 + * This program is free software; you can redistribute it and/or modify
2698 + * it under the terms of the GNU General Public License version 2 as
2699 + * published by the Free Software Foundation.
2705 + * Helper debugging functions for maintainers (and for users to report back
2706 + * useful information back to maintainers)
2709 +/* it's always useful to know what part of the code called us */
2710 +#define PRINT_CALLER(fname, fxn, line) \
2712 + if (!printed_caller) { \
2713 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2714 + printed_caller = 1; \
2719 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2720 + * the fan-out of various Unionfs objects. We check that no lower objects
2721 + * exist outside the start/end branch range; that all objects within are
2722 + * non-NULL (with some allowed exceptions); that for every lower file
2723 + * there's a lower dentry+inode; that the start/end ranges match for all
2724 + * corresponding lower objects; that open files/symlinks have only one lower
2725 + * objects, but directories can have several; and more.
2727 +void __unionfs_check_inode(const struct inode *inode,
2728 + const char *fname, const char *fxn, int line)
2732 + struct inode *lower_inode;
2733 + struct super_block *sb;
2734 + int printed_caller = 0;
2737 + /* for inodes now */
2740 + istart = ibstart(inode);
2741 + iend = ibend(inode);
2742 + /* don't check inode if no lower branches */
2743 + if (istart < 0 && iend < 0)
2745 + if (unlikely(istart > iend)) {
2746 + PRINT_CALLER(fname, fxn, line);
2747 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2748 + inode, istart, iend);
2750 + if (unlikely((istart == -1 && iend != -1) ||
2751 + (istart != -1 && iend == -1))) {
2752 + PRINT_CALLER(fname, fxn, line);
2753 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2754 + inode, istart, iend);
2756 + if (!S_ISDIR(inode->i_mode)) {
2757 + if (unlikely(iend != istart)) {
2758 + PRINT_CALLER(fname, fxn, line);
2759 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2760 + inode, istart, iend);
2764 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2765 + if (unlikely(!UNIONFS_I(inode))) {
2766 + PRINT_CALLER(fname, fxn, line);
2767 + pr_debug(" Ci3: no inode_info %p\n", inode);
2770 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2771 + PRINT_CALLER(fname, fxn, line);
2772 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2775 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2776 + if (lower_inode) {
2777 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2778 + if (unlikely(bindex < istart || bindex > iend)) {
2779 + PRINT_CALLER(fname, fxn, line);
2780 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2781 + "istart/end=%d:%d\n", inode,
2782 + lower_inode, bindex, istart, iend);
2783 + } else if (unlikely(lower_inode == poison_ptr)) {
2784 + /* freed inode! */
2785 + PRINT_CALLER(fname, fxn, line);
2786 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2787 + "istart/end=%d:%d\n", inode,
2788 + lower_inode, bindex, istart, iend);
2792 + /* if we get here, then lower_inode == NULL */
2793 + if (bindex < istart || bindex > iend)
2796 + * directories can have NULL lower inodes in b/t start/end,
2797 + * but NOT if at the start/end range.
2799 + if (unlikely(S_ISDIR(inode->i_mode) &&
2800 + bindex > istart && bindex < iend))
2802 + PRINT_CALLER(fname, fxn, line);
2803 + pr_debug(" Ci7: inode/linode=%p:%p "
2804 + "bindex=%d istart/end=%d:%d\n",
2805 + inode, lower_inode, bindex, istart, iend);
2809 +void __unionfs_check_dentry(const struct dentry *dentry,
2810 + const char *fname, const char *fxn, int line)
2813 + int dstart, dend, istart, iend;
2814 + struct dentry *lower_dentry;
2815 + struct inode *inode, *lower_inode;
2816 + struct super_block *sb;
2817 + struct vfsmount *lower_mnt;
2818 + int printed_caller = 0;
2822 + sb = dentry->d_sb;
2823 + inode = dentry->d_inode;
2824 + dstart = dbstart(dentry);
2825 + dend = dbend(dentry);
2826 + /* don't check dentry/mnt if no lower branches */
2827 + if (dstart < 0 && dend < 0)
2829 + BUG_ON(dstart > dend);
2831 + if (unlikely((dstart == -1 && dend != -1) ||
2832 + (dstart != -1 && dend == -1))) {
2833 + PRINT_CALLER(fname, fxn, line);
2834 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2835 + dentry, dstart, dend);
2838 + * check for NULL dentries inside the start/end range, or
2839 + * non-NULL dentries outside the start/end range.
2841 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2842 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2843 + if (lower_dentry) {
2844 + if (unlikely(bindex < dstart || bindex > dend)) {
2845 + PRINT_CALLER(fname, fxn, line);
2846 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2847 + "bindex=%d dstart/end=%d:%d\n",
2848 + dentry, lower_dentry,
2849 + (lower_dentry ? lower_dentry->d_inode :
2851 + bindex, dstart, dend);
2853 + } else { /* lower_dentry == NULL */
2854 + if (bindex < dstart || bindex > dend)
2857 + * Directories can have NULL lower inodes in b/t
2858 + * start/end, but NOT if at the start/end range.
2859 + * Ignore this rule, however, if this is a NULL
2860 + * dentry or a deleted dentry.
2862 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2864 + !(inode && S_ISDIR(inode->i_mode) &&
2865 + bindex > dstart && bindex < dend))) {
2866 + PRINT_CALLER(fname, fxn, line);
2867 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2868 + "bindex=%d dstart/end=%d:%d\n",
2869 + dentry, lower_dentry,
2871 + lower_dentry->d_inode :
2873 + bindex, dstart, dend);
2878 + /* check for vfsmounts same as for dentries */
2879 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2880 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2882 + if (unlikely(bindex < dstart || bindex > dend)) {
2883 + PRINT_CALLER(fname, fxn, line);
2884 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2885 + "dstart/end=%d:%d\n", dentry,
2886 + lower_mnt, bindex, dstart, dend);
2888 + } else { /* lower_mnt == NULL */
2889 + if (bindex < dstart || bindex > dend)
2892 + * Directories can have NULL lower inodes in b/t
2893 + * start/end, but NOT if at the start/end range.
2894 + * Ignore this rule, however, if this is a NULL
2897 + if (unlikely(inode &&
2898 + !(inode && S_ISDIR(inode->i_mode) &&
2899 + bindex > dstart && bindex < dend))) {
2900 + PRINT_CALLER(fname, fxn, line);
2901 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2902 + "bindex=%d dstart/end=%d:%d\n",
2903 + dentry, lower_mnt, bindex,
2910 + /* for inodes now */
2913 + istart = ibstart(inode);
2914 + iend = ibend(inode);
2915 + /* don't check inode if no lower branches */
2916 + if (istart < 0 && iend < 0)
2918 + BUG_ON(istart > iend);
2919 + if (unlikely((istart == -1 && iend != -1) ||
2920 + (istart != -1 && iend == -1))) {
2921 + PRINT_CALLER(fname, fxn, line);
2922 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2923 + dentry, inode, istart, iend);
2925 + if (unlikely(istart != dstart)) {
2926 + PRINT_CALLER(fname, fxn, line);
2927 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2928 + dentry, inode, istart, dstart);
2930 + if (unlikely(iend != dend)) {
2931 + PRINT_CALLER(fname, fxn, line);
2932 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2933 + dentry, inode, iend, dend);
2936 + if (!S_ISDIR(inode->i_mode)) {
2937 + if (unlikely(dend != dstart)) {
2938 + PRINT_CALLER(fname, fxn, line);
2939 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2940 + dentry, inode, dstart, dend);
2942 + if (unlikely(iend != istart)) {
2943 + PRINT_CALLER(fname, fxn, line);
2944 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2945 + dentry, inode, istart, iend);
2949 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2950 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2951 + if (lower_inode) {
2952 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2953 + if (unlikely(bindex < istart || bindex > iend)) {
2954 + PRINT_CALLER(fname, fxn, line);
2955 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2956 + "istart/end=%d:%d\n", dentry,
2957 + lower_inode, bindex, istart, iend);
2958 + } else if (unlikely(lower_inode == poison_ptr)) {
2959 + /* freed inode! */
2960 + PRINT_CALLER(fname, fxn, line);
2961 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2962 + "istart/end=%d:%d\n", dentry,
2963 + lower_inode, bindex, istart, iend);
2967 + /* if we get here, then lower_inode == NULL */
2968 + if (bindex < istart || bindex > iend)
2971 + * directories can have NULL lower inodes in b/t start/end,
2972 + * but NOT if at the start/end range.
2974 + if (unlikely(S_ISDIR(inode->i_mode) &&
2975 + bindex > istart && bindex < iend))
2977 + PRINT_CALLER(fname, fxn, line);
2978 + pr_debug(" CI7: dentry/linode=%p:%p "
2979 + "bindex=%d istart/end=%d:%d\n",
2980 + dentry, lower_inode, bindex, istart, iend);
2984 + * If it's a directory, then intermediate objects b/t start/end can
2985 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2988 + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2989 + for (bindex = dstart+1; bindex < dend; bindex++) {
2990 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2991 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2993 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2994 + if (unlikely(!((lower_inode && lower_dentry &&
2997 + !lower_dentry && !lower_mnt)))) {
2998 + PRINT_CALLER(fname, fxn, line);
2999 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
3000 + "bindex=%d dstart/end=%d:%d\n",
3001 + lower_mnt, lower_dentry, lower_inode,
3002 + bindex, dstart, dend);
3005 + /* check if lower inode is newer than upper one (it shouldn't) */
3006 + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
3007 + PRINT_CALLER(fname, fxn, line);
3008 + for (bindex = ibstart(inode); bindex <= ibend(inode);
3010 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3011 + if (unlikely(!lower_inode))
3013 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
3014 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
3016 + inode->i_mtime.tv_sec,
3017 + inode->i_mtime.tv_nsec,
3018 + lower_inode->i_mtime.tv_sec,
3019 + lower_inode->i_mtime.tv_nsec,
3020 + inode->i_ctime.tv_sec,
3021 + inode->i_ctime.tv_nsec,
3022 + lower_inode->i_ctime.tv_sec,
3023 + lower_inode->i_ctime.tv_nsec);
3028 +void __unionfs_check_file(const struct file *file,
3029 + const char *fname, const char *fxn, int line)
3032 + int dstart, dend, fstart, fend;
3033 + struct dentry *dentry;
3034 + struct file *lower_file;
3035 + struct inode *inode;
3036 + struct super_block *sb;
3037 + int printed_caller = 0;
3040 + dentry = file->f_path.dentry;
3041 + sb = dentry->d_sb;
3042 + dstart = dbstart(dentry);
3043 + dend = dbend(dentry);
3044 + BUG_ON(dstart > dend);
3045 + fstart = fbstart(file);
3046 + fend = fbend(file);
3047 + BUG_ON(fstart > fend);
3049 + if (unlikely((fstart == -1 && fend != -1) ||
3050 + (fstart != -1 && fend == -1))) {
3051 + PRINT_CALLER(fname, fxn, line);
3052 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
3053 + file, dentry, fstart, fend);
3055 + if (unlikely(fstart != dstart)) {
3056 + PRINT_CALLER(fname, fxn, line);
3057 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
3058 + file, dentry, fstart, dstart);
3060 + if (unlikely(fend != dend)) {
3061 + PRINT_CALLER(fname, fxn, line);
3062 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
3063 + file, dentry, fend, dend);
3065 + inode = dentry->d_inode;
3066 + if (!S_ISDIR(inode->i_mode)) {
3067 + if (unlikely(fend != fstart)) {
3068 + PRINT_CALLER(fname, fxn, line);
3069 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
3070 + file, inode, fstart, fend);
3072 + if (unlikely(dend != dstart)) {
3073 + PRINT_CALLER(fname, fxn, line);
3074 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
3075 + file, dentry, dstart, dend);
3080 + * check for NULL dentries inside the start/end range, or
3081 + * non-NULL dentries outside the start/end range.
3083 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
3084 + lower_file = unionfs_lower_file_idx(file, bindex);
3086 + if (unlikely(bindex < fstart || bindex > fend)) {
3087 + PRINT_CALLER(fname, fxn, line);
3088 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
3089 + "fstart/end=%d:%d\n", file,
3090 + lower_file, bindex, fstart, fend);
3092 + } else { /* lower_file == NULL */
3093 + if (bindex >= fstart && bindex <= fend) {
3095 + * directories can have NULL lower inodes in
3096 + * b/t start/end, but NOT if at the
3097 + * start/end range.
3099 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3100 + bindex > fstart &&
3101 + bindex < fend))) {
3102 + PRINT_CALLER(fname, fxn, line);
3103 + pr_debug(" CF6: file/lower=%p:%p "
3104 + "bindex=%d fstart/end=%d:%d\n",
3105 + file, lower_file, bindex,
3112 + __unionfs_check_dentry(dentry, fname, fxn, line);
3115 +void __unionfs_check_nd(const struct nameidata *nd,
3116 + const char *fname, const char *fxn, int line)
3118 + struct file *file;
3119 + int printed_caller = 0;
3121 + if (unlikely(!nd))
3123 + if (nd->flags & LOOKUP_OPEN) {
3124 + file = nd->intent.open.file;
3125 + if (unlikely(file->f_path.dentry &&
3126 + strcmp(file->f_path.dentry->d_sb->s_type->name,
3128 + PRINT_CALLER(fname, fxn, line);
3129 + pr_debug(" CND1: lower_file of type %s\n",
3130 + file->f_path.dentry->d_sb->s_type->name);
3136 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3137 +void __show_branch_counts(const struct super_block *sb,
3138 + const char *file, const char *fxn, int line)
3141 + struct vfsmount *mnt;
3144 + for (i = 0; i < sbmax(sb); i++) {
3145 + if (likely(sb->s_root))
3146 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3149 + printk(KERN_CONT "%d:",
3150 + (mnt ? atomic_read(&mnt->mnt_count) : -99));
3152 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3155 +void __show_inode_times(const struct inode *inode,
3156 + const char *file, const char *fxn, int line)
3158 + struct inode *lower_inode;
3161 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3162 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3163 + if (unlikely(!lower_inode))
3165 + pr_debug("IT(%lu:%d): %s:%s:%d "
3166 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3167 + inode->i_ino, bindex,
3169 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3170 + lower_inode->i_mtime.tv_sec,
3171 + lower_inode->i_mtime.tv_nsec,
3172 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3173 + lower_inode->i_ctime.tv_sec,
3174 + lower_inode->i_ctime.tv_nsec);
3178 +void __show_dinode_times(const struct dentry *dentry,
3179 + const char *file, const char *fxn, int line)
3181 + struct inode *inode = dentry->d_inode;
3182 + struct inode *lower_inode;
3185 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3186 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3189 + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3190 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3191 + dentry->d_name.name, inode->i_ino, bindex,
3193 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3194 + lower_inode->i_mtime.tv_sec,
3195 + lower_inode->i_mtime.tv_nsec,
3196 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3197 + lower_inode->i_ctime.tv_sec,
3198 + lower_inode->i_ctime.tv_nsec);
3202 +void __show_inode_counts(const struct inode *inode,
3203 + const char *file, const char *fxn, int line)
3205 + struct inode *lower_inode;
3208 + if (unlikely(!inode)) {
3209 + pr_debug("SiC: Null inode\n");
3212 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3214 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3215 + if (unlikely(!lower_inode))
3217 + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3218 + inode->i_ino, bindex,
3219 + atomic_read(&(inode)->i_count),
3220 + atomic_read(&(lower_inode)->i_count),
3224 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
3225 new file mode 100644
3226 index 0000000..85b5d3c
3228 +++ b/fs/unionfs/dentry.c
3231 + * Copyright (c) 2003-2009 Erez Zadok
3232 + * Copyright (c) 2003-2006 Charles P. Wright
3233 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3234 + * Copyright (c) 2005-2006 Junjiro Okajima
3235 + * Copyright (c) 2005 Arun M. Krishnakumar
3236 + * Copyright (c) 2004-2006 David P. Quigley
3237 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3238 + * Copyright (c) 2003 Puja Gupta
3239 + * Copyright (c) 2003 Harikesavan Krishnan
3240 + * Copyright (c) 2003-2009 Stony Brook University
3241 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3243 + * This program is free software; you can redistribute it and/or modify
3244 + * it under the terms of the GNU General Public License version 2 as
3245 + * published by the Free Software Foundation.
3250 +bool is_negative_lower(const struct dentry *dentry)
3253 + struct dentry *lower_dentry;
3256 + /* cache coherency: check if file was deleted on lower branch */
3257 + if (dbstart(dentry) < 0)
3259 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3260 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3261 + /* unhashed (i.e., unlinked) lower dentries don't count */
3262 + if (lower_dentry && lower_dentry->d_inode &&
3263 + !d_deleted(lower_dentry) &&
3264 + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3270 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3272 + struct dentry *lower_dentry;
3277 + for (bindex = start; bindex <= end; bindex++) {
3278 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3279 + if (!lower_dentry)
3281 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3282 + dput(lower_dentry);
3287 + * Purge and invalidate as many data pages of a unionfs inode. This is
3288 + * called when the lower inode has changed, and we want to force processes
3289 + * to re-get the new data.
3291 +static inline void purge_inode_data(struct inode *inode)
3293 + /* remove all non-private mappings */
3294 + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3295 + /* invalidate as many pages as possible */
3296 + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3298 + * Don't try to truncate_inode_pages here, because this could lead
3299 + * to a deadlock between some of address_space ops and dentry
3300 + * revalidation: the address space op is invoked with a lock on our
3301 + * own page, and truncate_inode_pages will block on locked pages.
3306 + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3307 + * of the @dentry and its @parent are locked. Assume parent is valid,
3308 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3309 + * dentry). Returns true if valid, false otherwise.
3311 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3314 + bool valid = true; /* default is valid */
3315 + struct dentry *lower_dentry;
3316 + struct dentry *result;
3317 + int bindex, bstart, bend;
3318 + int sbgen, dgen, pdgen;
3320 + int interpose_flag;
3322 + verify_locked(dentry);
3323 + verify_locked(parent);
3325 + /* if the dentry is unhashed, do NOT revalidate */
3326 + if (d_deleted(dentry))
3329 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3331 + if (is_newer_lower(dentry)) {
3332 + /* root dentry is always valid */
3333 + if (IS_ROOT(dentry)) {
3334 + unionfs_copy_attr_times(dentry->d_inode);
3337 + * reset generation number to zero, guaranteed to be
3341 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3344 + purge_inode_data(dentry->d_inode);
3347 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3349 + BUG_ON(dbstart(dentry) == -1);
3350 + if (dentry->d_inode)
3353 + /* if our dentry is valid, then validate all lower ones */
3354 + if (sbgen == dgen)
3355 + goto validate_lowers;
3357 + /* The root entry should always be valid */
3358 + BUG_ON(IS_ROOT(dentry));
3360 + /* We can't work correctly if our parent isn't valid. */
3361 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3363 + /* Free the pointers for our inodes and this dentry. */
3364 + path_put_lowers_all(dentry, false);
3366 + interpose_flag = INTERPOSE_REVAL_NEG;
3368 + interpose_flag = INTERPOSE_REVAL;
3369 + iput_lowers_all(dentry->d_inode, true);
3372 + if (realloc_dentry_private_data(dentry) != 0) {
3377 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3379 + if (IS_ERR(result)) {
3384 + * current unionfs_lookup_backend() doesn't return
3391 + if (unlikely(positive && is_negative_lower(dentry))) {
3392 + /* call make_bad_inode here ? */
3399 + * if we got here then we have revalidated our dentry and all lower
3400 + * ones, so we can return safely.
3402 + if (!valid) /* lower dentry revalidation failed */
3406 + * If the parent's gen no. matches the superblock's gen no., then
3407 + * we can update our denty's gen no. If they didn't match, then it
3408 + * was OK to revalidate this dentry with a stale parent, but we'll
3409 + * purposely not update our dentry's gen no. (so it can be redone);
3410 + * and, we'll mark our parent dentry as invalid so it'll force it
3411 + * (and our dentry) to be revalidated.
3413 + if (pdgen == sbgen)
3414 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3419 + /* The revalidation must occur across all branches */
3420 + bstart = dbstart(dentry);
3421 + bend = dbend(dentry);
3422 + BUG_ON(bstart == -1);
3423 + for (bindex = bstart; bindex <= bend; bindex++) {
3424 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3425 + if (!lower_dentry || !lower_dentry->d_op
3426 + || !lower_dentry->d_op->d_revalidate)
3429 + * Don't pass nameidata to lower file system, because we
3430 + * don't want an arbitrary lower file being opened or
3431 + * returned to us: it may be useless to us because of the
3432 + * fanout nature of unionfs (cf. file/directory open-file
3433 + * invariants). We will open lower files as and when needed
3436 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3440 + if (!dentry->d_inode ||
3441 + ibstart(dentry->d_inode) < 0 ||
3442 + ibend(dentry->d_inode) < 0) {
3449 + * If we get here, and we copy the meta-data from the lower
3450 + * inode to our inode, then it is vital that we have already
3451 + * purged all unionfs-level file data. We do that in the
3452 + * caller (__unionfs_d_revalidate) by calling
3453 + * purge_inode_data.
3455 + unionfs_copy_attr_all(dentry->d_inode,
3456 + unionfs_lower_inode(dentry->d_inode));
3457 + fsstack_copy_inode_size(dentry->d_inode,
3458 + unionfs_lower_inode(dentry->d_inode));
3466 + * Determine if the lower inode objects have changed from below the unionfs
3467 + * inode. Return true if changed, false otherwise.
3469 + * We check if the mtime or ctime have changed. However, the inode times
3470 + * can be changed by anyone without much protection, including
3471 + * asynchronously. This can sometimes cause unionfs to find that the lower
3472 + * file system doesn't change its inode times quick enough, resulting in a
3473 + * false positive indication (which is harmless, it just makes unionfs do
3474 + * extra work in re-validating the objects). To minimize the chances of
3475 + * these situations, we still consider such small time changes valid, but we
3476 + * don't print debugging messages unless the time changes are greater than
3477 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3478 + * because significant changes are more likely due to users manually
3479 + * touching lower files.
3481 +bool is_newer_lower(const struct dentry *dentry)
3484 + struct inode *inode;
3485 + struct inode *lower_inode;
3487 + /* ignore if we're called on semi-initialized dentries/inodes */
3488 + if (!dentry || !UNIONFS_D(dentry))
3490 + inode = dentry->d_inode;
3491 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3492 + ibstart(inode) < 0 || ibend(inode) < 0)
3495 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3496 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3500 + /* check if mtime/ctime have changed */
3501 + if (unlikely(timespec_compare(&inode->i_mtime,
3502 + &lower_inode->i_mtime) < 0)) {
3503 + if ((lower_inode->i_mtime.tv_sec -
3504 + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3505 + pr_info("unionfs: new lower inode mtime "
3506 + "(bindex=%d, name=%s)\n", bindex,
3507 + dentry->d_name.name);
3508 + show_dinode_times(dentry);
3512 + if (unlikely(timespec_compare(&inode->i_ctime,
3513 + &lower_inode->i_ctime) < 0)) {
3514 + if ((lower_inode->i_ctime.tv_sec -
3515 + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3516 + pr_info("unionfs: new lower inode ctime "
3517 + "(bindex=%d, name=%s)\n", bindex,
3518 + dentry->d_name.name);
3519 + show_dinode_times(dentry);
3526 + * Last check: if this is a positive dentry, but somehow all lower
3527 + * dentries are negative or unhashed, then this dentry needs to be
3528 + * revalidated, because someone probably deleted the objects from
3529 + * the lower branches directly.
3531 + if (is_negative_lower(dentry))
3534 + return false; /* default: lower is not newer */
3537 +static int unionfs_d_revalidate(struct dentry *dentry,
3538 + struct nameidata *nd_unused)
3540 + bool valid = true;
3541 + int err = 1; /* 1 means valid for the VFS */
3542 + struct dentry *parent;
3544 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3545 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3546 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3548 + valid = __unionfs_d_revalidate(dentry, parent, false);
3550 + unionfs_postcopyup_setmnt(dentry);
3551 + unionfs_check_dentry(dentry);
3556 + unionfs_unlock_dentry(dentry);
3557 + unionfs_unlock_parent(dentry, parent);
3558 + unionfs_read_unlock(dentry->d_sb);
3563 +static void unionfs_d_release(struct dentry *dentry)
3565 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3566 + if (unlikely(!UNIONFS_D(dentry)))
3567 + goto out; /* skip if no lower branches */
3568 + /* must lock our branch configuration here */
3569 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3571 + unionfs_check_dentry(dentry);
3572 + /* this could be a negative dentry, so check first */
3573 + if (dbstart(dentry) < 0) {
3574 + unionfs_unlock_dentry(dentry);
3575 + goto out; /* due to a (normal) failed lookup */
3578 + /* Release all the lower dentries */
3579 + path_put_lowers_all(dentry, true);
3581 + unionfs_unlock_dentry(dentry);
3584 + free_dentry_private_data(dentry);
3585 + unionfs_read_unlock(dentry->d_sb);
3590 + * Called when we're removing the last reference to our dentry. So we
3591 + * should drop all lower references too.
3593 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3598 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3599 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3601 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3602 + goto drop_lower_inodes;
3603 + path_put_lowers_all(dentry, false);
3606 + rc = atomic_read(&inode->i_count);
3607 + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3608 + /* see Documentation/filesystems/unionfs/issues.txt */
3610 + iput(unionfs_lower_inode(inode));
3612 + unionfs_set_lower_inode(inode, NULL);
3613 + /* XXX: may need to set start/end to -1? */
3618 + unionfs_unlock_dentry(dentry);
3619 + unionfs_read_unlock(dentry->d_sb);
3622 +struct dentry_operations unionfs_dops = {
3623 + .d_revalidate = unionfs_d_revalidate,
3624 + .d_release = unionfs_d_release,
3625 + .d_iput = unionfs_d_iput,
3627 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3628 new file mode 100644
3629 index 0000000..eccb9ae
3631 +++ b/fs/unionfs/dirfops.c
3634 + * Copyright (c) 2003-2009 Erez Zadok
3635 + * Copyright (c) 2003-2006 Charles P. Wright
3636 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3637 + * Copyright (c) 2005-2006 Junjiro Okajima
3638 + * Copyright (c) 2005 Arun M. Krishnakumar
3639 + * Copyright (c) 2004-2006 David P. Quigley
3640 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3641 + * Copyright (c) 2003 Puja Gupta
3642 + * Copyright (c) 2003 Harikesavan Krishnan
3643 + * Copyright (c) 2003-2009 Stony Brook University
3644 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3646 + * This program is free software; you can redistribute it and/or modify
3647 + * it under the terms of the GNU General Public License version 2 as
3648 + * published by the Free Software Foundation.
3653 +/* Make sure our rdstate is playing by the rules. */
3654 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3656 + BUG_ON(rdstate->offset >= DIREOF);
3657 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3660 +struct unionfs_getdents_callback {
3661 + struct unionfs_dir_state *rdstate;
3663 + int entries_written;
3664 + int filldir_called;
3665 + int filldir_error;
3666 + filldir_t filldir;
3667 + struct super_block *sb;
3670 +/* based on generic filldir in fs/readir.c */
3671 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3672 + loff_t offset, u64 ino, unsigned int d_type)
3674 + struct unionfs_getdents_callback *buf = dirent;
3675 + struct filldir_node *found = NULL;
3678 + char *name = (char *) oname;
3680 + buf->filldir_called++;
3682 + is_whiteout = is_whiteout_name(&name, &namelen);
3684 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3688 + * If we had non-whiteout entry in dir cache, then mark it
3689 + * as a whiteout and but leave it in the dir cache.
3691 + if (is_whiteout && !found->whiteout)
3692 + found->whiteout = is_whiteout;
3696 + /* if 'name' isn't a whiteout, filldir it. */
3697 + if (!is_whiteout) {
3698 + off_t pos = rdstate2offset(buf->rdstate);
3699 + u64 unionfs_ino = ino;
3701 + err = buf->filldir(buf->dirent, name, namelen, pos,
3702 + unionfs_ino, d_type);
3703 + buf->rdstate->offset++;
3704 + verify_rdstate_offset(buf->rdstate);
3707 + * If we did fill it, stuff it in our hash, otherwise return an
3711 + buf->filldir_error = err;
3714 + buf->entries_written++;
3715 + err = add_filldir_node(buf->rdstate, name, namelen,
3716 + buf->rdstate->bindex, is_whiteout);
3718 + buf->filldir_error = err;
3724 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3727 + struct file *lower_file = NULL;
3728 + struct dentry *dentry = file->f_path.dentry;
3729 + struct dentry *parent;
3730 + struct inode *inode = NULL;
3731 + struct unionfs_getdents_callback buf;
3732 + struct unionfs_dir_state *uds;
3736 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3737 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3738 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3740 + err = unionfs_file_revalidate(file, parent, false);
3741 + if (unlikely(err))
3744 + inode = dentry->d_inode;
3746 + uds = UNIONFS_F(file)->rdstate;
3748 + if (file->f_pos == DIREOF) {
3750 + } else if (file->f_pos > 0) {
3751 + uds = find_rdstate(inode, file->f_pos);
3752 + if (unlikely(!uds)) {
3756 + UNIONFS_F(file)->rdstate = uds;
3758 + init_rdstate(file);
3759 + uds = UNIONFS_F(file)->rdstate;
3762 + bend = fbend(file);
3764 + while (uds->bindex <= bend) {
3765 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3766 + if (!lower_file) {
3772 + /* prepare callback buffer */
3773 + buf.filldir_called = 0;
3774 + buf.filldir_error = 0;
3775 + buf.entries_written = 0;
3776 + buf.dirent = dirent;
3777 + buf.filldir = filldir;
3778 + buf.rdstate = uds;
3779 + buf.sb = inode->i_sb;
3781 + /* Read starting from where we last left off. */
3782 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3787 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3789 + /* Save the position for when we continue. */
3790 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3795 + uds->dirpos = offset;
3797 + /* Copy the atime. */
3798 + fsstack_copy_attr_atime(inode,
3799 + lower_file->f_path.dentry->d_inode);
3804 + if (buf.filldir_error)
3807 + if (!buf.entries_written) {
3813 + if (!buf.filldir_error && uds->bindex >= bend) {
3814 + /* Save the number of hash entries for next time. */
3815 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3816 + free_rdstate(uds);
3817 + UNIONFS_F(file)->rdstate = NULL;
3818 + file->f_pos = DIREOF;
3820 + file->f_pos = rdstate2offset(uds);
3825 + unionfs_check_file(file);
3826 + unionfs_unlock_dentry(dentry);
3827 + unionfs_unlock_parent(dentry, parent);
3828 + unionfs_read_unlock(dentry->d_sb);
3833 + * This is not meant to be a generic repositioning function. If you do
3834 + * things that aren't supported, then we return EINVAL.
3836 + * What is allowed:
3837 + * (1) seeking to the same position that you are currently at
3838 + * This really has no effect, but returns where you are.
3839 + * (2) seeking to the beginning of the file
3840 + * This throws out all state, and lets you begin again.
3842 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3844 + struct unionfs_dir_state *rdstate;
3845 + struct dentry *dentry = file->f_path.dentry;
3846 + struct dentry *parent;
3849 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3850 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3851 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3853 + err = unionfs_file_revalidate(file, parent, false);
3854 + if (unlikely(err))
3857 + rdstate = UNIONFS_F(file)->rdstate;
3860 + * we let users seek to their current position, but not anywhere
3867 + free_rdstate(rdstate);
3868 + UNIONFS_F(file)->rdstate = NULL;
3870 + init_rdstate(file);
3874 + err = file->f_pos;
3877 + /* Unsupported, because we would break everything. */
3885 + if (offset == rdstate2offset(rdstate))
3887 + else if (file->f_pos == DIREOF)
3892 + struct inode *inode;
3893 + inode = dentry->d_inode;
3894 + rdstate = find_rdstate(inode, offset);
3896 + UNIONFS_F(file)->rdstate = rdstate;
3897 + err = rdstate->offset;
3905 + /* Unsupported, because we would break everything. */
3913 + unionfs_check_file(file);
3914 + unionfs_unlock_dentry(dentry);
3915 + unionfs_unlock_parent(dentry, parent);
3916 + unionfs_read_unlock(dentry->d_sb);
3921 + * Trimmed directory options, we shouldn't pass everything down since
3922 + * we don't want to operate on partial directories.
3924 +struct file_operations unionfs_dir_fops = {
3925 + .llseek = unionfs_dir_llseek,
3926 + .read = generic_read_dir,
3927 + .readdir = unionfs_readdir,
3928 + .unlocked_ioctl = unionfs_ioctl,
3929 + .open = unionfs_open,
3930 + .release = unionfs_file_release,
3931 + .flush = unionfs_flush,
3932 + .fsync = unionfs_fsync,
3933 + .fasync = unionfs_fasync,
3935 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3936 new file mode 100644
3937 index 0000000..2ecaafa
3939 +++ b/fs/unionfs/dirhelper.c
3942 + * Copyright (c) 2003-2009 Erez Zadok
3943 + * Copyright (c) 2003-2006 Charles P. Wright
3944 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3945 + * Copyright (c) 2005-2006 Junjiro Okajima
3946 + * Copyright (c) 2005 Arun M. Krishnakumar
3947 + * Copyright (c) 2004-2006 David P. Quigley
3948 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3949 + * Copyright (c) 2003 Puja Gupta
3950 + * Copyright (c) 2003 Harikesavan Krishnan
3951 + * Copyright (c) 2003-2009 Stony Brook University
3952 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3954 + * This program is free software; you can redistribute it and/or modify
3955 + * it under the terms of the GNU General Public License version 2 as
3956 + * published by the Free Software Foundation.
3962 +#define RD_CHECK_EMPTY 1
3963 +/* The callback structure for check_empty. */
3964 +struct unionfs_rdutil_callback {
3966 + int filldir_called;
3967 + struct unionfs_dir_state *rdstate;
3971 +/* This filldir function makes sure only whiteouts exist within a directory. */
3972 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3973 + loff_t offset, u64 ino, unsigned int d_type)
3976 + struct unionfs_rdutil_callback *buf = dirent;
3978 + struct filldir_node *found;
3979 + char *name = (char *) oname;
3981 + buf->filldir_called = 1;
3983 + if (name[0] == '.' && (namelen == 1 ||
3984 + (name[1] == '.' && namelen == 2)))
3987 + is_whiteout = is_whiteout_name(&name, &namelen);
3989 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3990 + /* If it was found in the table there was a previous whiteout. */
3995 + * if it wasn't found and isn't a whiteout, the directory isn't
3999 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
4002 + err = add_filldir_node(buf->rdstate, name, namelen,
4003 + buf->rdstate->bindex, is_whiteout);
4010 +/* Is a directory logically empty? */
4011 +int check_empty(struct dentry *dentry, struct dentry *parent,
4012 + struct unionfs_dir_state **namelist)
4015 + struct dentry *lower_dentry = NULL;
4016 + struct vfsmount *mnt;
4017 + struct super_block *sb;
4018 + struct file *lower_file;
4019 + struct unionfs_rdutil_callback *buf = NULL;
4020 + int bindex, bstart, bend, bopaque;
4022 + sb = dentry->d_sb;
4025 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
4027 + err = unionfs_partial_lookup(dentry, parent);
4031 + bstart = dbstart(dentry);
4032 + bend = dbend(dentry);
4033 + bopaque = dbopaque(dentry);
4034 + if (0 <= bopaque && bopaque < bend)
4037 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
4038 + if (unlikely(!buf)) {
4043 + buf->mode = RD_CHECK_EMPTY;
4044 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
4045 + if (unlikely(!buf->rdstate)) {
4050 + /* Process the lower directories with rdutil_callback as a filldir. */
4051 + for (bindex = bstart; bindex <= bend; bindex++) {
4052 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4053 + if (!lower_dentry)
4055 + if (!lower_dentry->d_inode)
4057 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
4060 + dget(lower_dentry);
4061 + mnt = unionfs_mntget(dentry, bindex);
4062 + branchget(sb, bindex);
4063 + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
4064 + if (IS_ERR(lower_file)) {
4065 + err = PTR_ERR(lower_file);
4066 + branchput(sb, bindex);
4071 + buf->filldir_called = 0;
4072 + buf->rdstate->bindex = bindex;
4073 + err = vfs_readdir(lower_file,
4074 + readdir_util_callback, buf);
4077 + } while ((err >= 0) && buf->filldir_called);
4079 + /* fput calls dput for lower_dentry */
4081 + branchput(sb, bindex);
4089 + if (namelist && !err)
4090 + *namelist = buf->rdstate;
4091 + else if (buf->rdstate)
4092 + free_rdstate(buf->rdstate);
4099 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4100 new file mode 100644
4101 index 0000000..04ffa85
4103 +++ b/fs/unionfs/fanout.h
4106 + * Copyright (c) 2003-2009 Erez Zadok
4107 + * Copyright (c) 2003-2006 Charles P. Wright
4108 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4109 + * Copyright (c) 2005 Arun M. Krishnakumar
4110 + * Copyright (c) 2004-2006 David P. Quigley
4111 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4112 + * Copyright (c) 2003 Puja Gupta
4113 + * Copyright (c) 2003 Harikesavan Krishnan
4114 + * Copyright (c) 2003-2009 Stony Brook University
4115 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4117 + * This program is free software; you can redistribute it and/or modify
4118 + * it under the terms of the GNU General Public License version 2 as
4119 + * published by the Free Software Foundation.
4126 + * Inode to private data
4128 + * Since we use containers and the struct inode is _inside_ the
4129 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4130 + * inode pointer), return a valid non-NULL pointer.
4132 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4134 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4137 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4138 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4140 +/* Dentry to private data */
4141 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4142 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4143 +#define dbend(dent) (UNIONFS_D(dent)->bend)
4144 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4146 +/* Superblock to private data */
4147 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4148 +#define sbstart(sb) 0
4149 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4150 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4151 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4153 +/* File to private Data */
4154 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4155 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4156 +#define fbend(file) (UNIONFS_F(file)->bend)
4158 +/* macros to manipulate branch IDs in stored in our superblock */
4159 +static inline int branch_id(struct super_block *sb, int index)
4161 + BUG_ON(!sb || index < 0);
4162 + return UNIONFS_SB(sb)->data[index].branch_id;
4165 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4167 + BUG_ON(!sb || index < 0);
4168 + UNIONFS_SB(sb)->data[index].branch_id = val;
4171 +static inline void new_branch_id(struct super_block *sb, int index)
4173 + BUG_ON(!sb || index < 0);
4174 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4178 + * Find new index of matching branch with an existing superblock of a known
4179 + * (possibly old) id. This is needed because branches could have been
4180 + * added/deleted causing the branches of any open files to shift.
4182 + * @sb: the new superblock which may have new/different branch IDs
4183 + * @id: the old/existing id we're looking for
4184 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4186 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4189 + for (i = 0; i < sbmax(sb); i++) {
4190 + if (branch_id(sb, i) == id)
4193 + /* in the non-ODF code, this should really never happen */
4194 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4198 +/* File to lower file. */
4199 +static inline struct file *unionfs_lower_file(const struct file *f)
4202 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4205 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4208 + BUG_ON(!f || index < 0);
4209 + return UNIONFS_F(f)->lower_files[index];
4212 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4215 + BUG_ON(!f || index < 0);
4216 + UNIONFS_F(f)->lower_files[index] = val;
4217 + /* save branch ID (may be redundant?) */
4218 + UNIONFS_F(f)->saved_branch_ids[index] =
4219 + branch_id((f)->f_path.dentry->d_sb, index);
4222 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4225 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4228 +/* Inode to lower inode. */
4229 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4232 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4235 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4238 + BUG_ON(!i || index < 0);
4239 + return UNIONFS_I(i)->lower_inodes[index];
4242 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4243 + struct inode *val)
4245 + BUG_ON(!i || index < 0);
4246 + UNIONFS_I(i)->lower_inodes[index] = val;
4249 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4252 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4255 +/* Superblock to lower superblock. */
4256 +static inline struct super_block *unionfs_lower_super(
4257 + const struct super_block *sb)
4260 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4263 +static inline struct super_block *unionfs_lower_super_idx(
4264 + const struct super_block *sb,
4267 + BUG_ON(!sb || index < 0);
4268 + return UNIONFS_SB(sb)->data[index].sb;
4271 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4273 + struct super_block *val)
4275 + BUG_ON(!sb || index < 0);
4276 + UNIONFS_SB(sb)->data[index].sb = val;
4279 +static inline void unionfs_set_lower_super(struct super_block *sb,
4280 + struct super_block *val)
4283 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4286 +/* Branch count macros. */
4287 +static inline int branch_count(const struct super_block *sb, int index)
4289 + BUG_ON(!sb || index < 0);
4290 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4293 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4295 + BUG_ON(!sb || index < 0);
4296 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4299 +static inline void branchget(struct super_block *sb, int index)
4301 + BUG_ON(!sb || index < 0);
4302 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4305 +static inline void branchput(struct super_block *sb, int index)
4307 + BUG_ON(!sb || index < 0);
4308 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4311 +/* Dentry macros */
4312 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4313 + struct dentry *val)
4315 + BUG_ON(!dent || index < 0);
4316 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4319 +static inline struct dentry *unionfs_lower_dentry_idx(
4320 + const struct dentry *dent,
4323 + BUG_ON(!dent || index < 0);
4324 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4327 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4330 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4333 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4334 + struct vfsmount *mnt)
4336 + BUG_ON(!dent || index < 0);
4337 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4340 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4341 + const struct dentry *dent,
4344 + BUG_ON(!dent || index < 0);
4345 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4348 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4351 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4354 +/* Macros for locking a dentry. */
4355 +enum unionfs_dentry_lock_class {
4356 + UNIONFS_DMUTEX_NORMAL,
4357 + UNIONFS_DMUTEX_ROOT,
4358 + UNIONFS_DMUTEX_PARENT,
4359 + UNIONFS_DMUTEX_CHILD,
4360 + UNIONFS_DMUTEX_WHITEOUT,
4361 + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4362 + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4365 +static inline void unionfs_lock_dentry(struct dentry *d,
4366 + unsigned int subclass)
4369 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4372 +static inline void unionfs_unlock_dentry(struct dentry *d)
4375 + mutex_unlock(&UNIONFS_D(d)->lock);
4378 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4379 + unsigned int subclass)
4384 + p = dget_parent(d);
4386 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4390 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4395 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4396 + mutex_unlock(&UNIONFS_D(p)->lock);
4401 +static inline void verify_locked(struct dentry *d)
4404 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4407 +/* macros to put lower objects */
4410 + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4411 + * @free_lower is true, then also kfree the memory used to hold the lower
4412 + * object pointers.
4414 +static inline void iput_lowers(struct inode *inode,
4415 + int bstart, int bend, bool free_lower)
4417 + struct inode *lower_inode;
4421 + BUG_ON(!UNIONFS_I(inode));
4422 + BUG_ON(bstart < 0);
4424 + for (bindex = bstart; bindex <= bend; bindex++) {
4425 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4426 + if (lower_inode) {
4427 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4428 + /* see Documentation/filesystems/unionfs/issues.txt */
4430 + iput(lower_inode);
4436 + kfree(UNIONFS_I(inode)->lower_inodes);
4437 + UNIONFS_I(inode)->lower_inodes = NULL;
4441 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4442 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4447 + BUG_ON(!UNIONFS_I(inode));
4448 + bstart = ibstart(inode);
4449 + bend = ibend(inode);
4450 + BUG_ON(bstart < 0);
4452 + iput_lowers(inode, bstart, bend, free_lower);
4453 + ibstart(inode) = ibend(inode) = -1;
4457 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4458 + * bstart to bend. If @free_lower is true, then also kfree the memory used
4459 + * to hold the lower object pointers.
4461 + * XXX: implement using path_put VFS macros
4463 +static inline void path_put_lowers(struct dentry *dentry,
4464 + int bstart, int bend, bool free_lower)
4466 + struct dentry *lower_dentry;
4467 + struct vfsmount *lower_mnt;
4471 + BUG_ON(!UNIONFS_D(dentry));
4472 + BUG_ON(bstart < 0);
4474 + for (bindex = bstart; bindex <= bend; bindex++) {
4475 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4476 + if (lower_dentry) {
4477 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4478 + dput(lower_dentry);
4480 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4482 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4483 + mntput(lower_mnt);
4488 + kfree(UNIONFS_D(dentry)->lower_paths);
4489 + UNIONFS_D(dentry)->lower_paths = NULL;
4494 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4497 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4502 + BUG_ON(!UNIONFS_D(dentry));
4503 + bstart = dbstart(dentry);
4504 + bend = dbend(dentry);
4505 + BUG_ON(bstart < 0);
4507 + path_put_lowers(dentry, bstart, bend, free_lower);
4508 + dbstart(dentry) = dbend(dentry) = -1;
4511 +#endif /* not _FANOUT_H */
4512 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4513 new file mode 100644
4514 index 0000000..281169e
4516 +++ b/fs/unionfs/file.c
4519 + * Copyright (c) 2003-2009 Erez Zadok
4520 + * Copyright (c) 2003-2006 Charles P. Wright
4521 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4522 + * Copyright (c) 2005-2006 Junjiro Okajima
4523 + * Copyright (c) 2005 Arun M. Krishnakumar
4524 + * Copyright (c) 2004-2006 David P. Quigley
4525 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4526 + * Copyright (c) 2003 Puja Gupta
4527 + * Copyright (c) 2003 Harikesavan Krishnan
4528 + * Copyright (c) 2003-2009 Stony Brook University
4529 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4531 + * This program is free software; you can redistribute it and/or modify
4532 + * it under the terms of the GNU General Public License version 2 as
4533 + * published by the Free Software Foundation.
4538 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4539 + size_t count, loff_t *ppos)
4542 + struct file *lower_file;
4543 + struct dentry *dentry = file->f_path.dentry;
4544 + struct dentry *parent;
4546 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4547 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4548 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4550 + err = unionfs_file_revalidate(file, parent, false);
4551 + if (unlikely(err))
4554 + lower_file = unionfs_lower_file(file);
4555 + err = vfs_read(lower_file, buf, count, ppos);
4556 + /* update our inode atime upon a successful lower read */
4558 + fsstack_copy_attr_atime(dentry->d_inode,
4559 + lower_file->f_path.dentry->d_inode);
4560 + unionfs_check_file(file);
4564 + unionfs_unlock_dentry(dentry);
4565 + unionfs_unlock_parent(dentry, parent);
4566 + unionfs_read_unlock(dentry->d_sb);
4570 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4571 + size_t count, loff_t *ppos)
4574 + struct file *lower_file;
4575 + struct dentry *dentry = file->f_path.dentry;
4576 + struct dentry *parent;
4578 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4579 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4580 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4582 + err = unionfs_file_revalidate(file, parent, true);
4583 + if (unlikely(err))
4586 + lower_file = unionfs_lower_file(file);
4587 + err = vfs_write(lower_file, buf, count, ppos);
4588 + /* update our inode times+sizes upon a successful lower write */
4590 + fsstack_copy_inode_size(dentry->d_inode,
4591 + lower_file->f_path.dentry->d_inode);
4592 + fsstack_copy_attr_times(dentry->d_inode,
4593 + lower_file->f_path.dentry->d_inode);
4594 + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4595 + unionfs_check_file(file);
4599 + unionfs_unlock_dentry(dentry);
4600 + unionfs_unlock_parent(dentry, parent);
4601 + unionfs_read_unlock(dentry->d_sb);
4605 +static int unionfs_file_readdir(struct file *file, void *dirent,
4606 + filldir_t filldir)
4611 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4615 + struct file *lower_file;
4616 + struct dentry *dentry = file->f_path.dentry;
4617 + struct dentry *parent;
4618 + struct vm_operations_struct *saved_vm_ops = NULL;
4621 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4622 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4623 + * has been causing false positives in file system stacking layers.
4624 + * In particular, our ->mmap is called after sys_mmap2 already holds
4625 + * mmap_sem, then we lock our own mutexes; but earlier, it's
4626 + * possible for lockdep to have locked our mutexes first, and then
4627 + * we call a lower ->readdir which could call might_fault. The
4628 + * different ordering of the locks is what lockdep complains about
4629 + * -- unnecessarily. Therefore, we have no choice but to tell
4630 + * lockdep to temporarily turn off lockdep here. Note: the comments
4631 + * inside might_sleep also suggest that it would have been
4632 + * nicer to only annotate paths that needs that might_lock_read.
4635 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4636 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4637 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4639 + /* This might be deferred to mmap's writepage */
4640 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4641 + err = unionfs_file_revalidate(file, parent, willwrite);
4642 + if (unlikely(err))
4644 + unionfs_check_file(file);
4647 + * File systems which do not implement ->writepage may use
4648 + * generic_file_readonly_mmap as their ->mmap op. If you call
4649 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4650 + * But we cannot call the lower ->mmap op, so we can't tell that
4651 + * writeable mappings won't work. Therefore, our only choice is to
4652 + * check if the lower file system supports the ->writepage, and if
4653 + * not, return EINVAL (the same error that
4654 + * generic_file_readonly_mmap returns in that case).
4656 + lower_file = unionfs_lower_file(file);
4657 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4659 + printk(KERN_ERR "unionfs: branch %d file system does not "
4660 + "support writeable mmap\n", fbstart(file));
4665 + * find and save lower vm_ops.
4667 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4669 + if (!UNIONFS_F(file)->lower_vm_ops) {
4670 + err = lower_file->f_op->mmap(lower_file, vma);
4672 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4675 + saved_vm_ops = vma->vm_ops;
4676 + err = do_munmap(current->mm, vma->vm_start,
4677 + vma->vm_end - vma->vm_start);
4679 + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4684 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4685 + err = generic_file_mmap(file, vma);
4686 + file->f_mapping->a_ops = &unionfs_aops;
4688 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4691 + vma->vm_ops = &unionfs_vm_ops;
4692 + if (!UNIONFS_F(file)->lower_vm_ops)
4693 + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4697 + /* copyup could cause parent dir times to change */
4698 + unionfs_copy_attr_times(parent->d_inode);
4699 + unionfs_check_file(file);
4701 + unionfs_unlock_dentry(dentry);
4702 + unionfs_unlock_parent(dentry, parent);
4703 + unionfs_read_unlock(dentry->d_sb);
4708 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4710 + int bindex, bstart, bend;
4711 + struct file *lower_file;
4712 + struct dentry *lower_dentry;
4713 + struct dentry *parent;
4714 + struct inode *lower_inode, *inode;
4715 + int err = -EINVAL;
4717 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4718 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4719 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4721 + err = unionfs_file_revalidate(file, parent, true);
4722 + if (unlikely(err))
4724 + unionfs_check_file(file);
4726 + bstart = fbstart(file);
4727 + bend = fbend(file);
4728 + if (bstart < 0 || bend < 0)
4731 + inode = dentry->d_inode;
4732 + if (unlikely(!inode)) {
4734 + "unionfs: null lower inode in unionfs_fsync\n");
4737 + for (bindex = bstart; bindex <= bend; bindex++) {
4738 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4739 + if (!lower_inode || !lower_inode->i_fop->fsync)
4741 + lower_file = unionfs_lower_file_idx(file, bindex);
4742 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4743 + mutex_lock(&lower_inode->i_mutex);
4744 + err = lower_inode->i_fop->fsync(lower_file,
4747 + if (!err && bindex == bstart)
4748 + fsstack_copy_attr_times(inode, lower_inode);
4749 + mutex_unlock(&lower_inode->i_mutex);
4756 + unionfs_check_file(file);
4757 + unionfs_unlock_dentry(dentry);
4758 + unionfs_unlock_parent(dentry, parent);
4759 + unionfs_read_unlock(dentry->d_sb);
4763 +int unionfs_fasync(int fd, struct file *file, int flag)
4765 + int bindex, bstart, bend;
4766 + struct file *lower_file;
4767 + struct dentry *dentry = file->f_path.dentry;
4768 + struct dentry *parent;
4769 + struct inode *lower_inode, *inode;
4772 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4773 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4774 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4776 + err = unionfs_file_revalidate(file, parent, true);
4777 + if (unlikely(err))
4779 + unionfs_check_file(file);
4781 + bstart = fbstart(file);
4782 + bend = fbend(file);
4783 + if (bstart < 0 || bend < 0)
4786 + inode = dentry->d_inode;
4787 + if (unlikely(!inode)) {
4789 + "unionfs: null lower inode in unionfs_fasync\n");
4792 + for (bindex = bstart; bindex <= bend; bindex++) {
4793 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4794 + if (!lower_inode || !lower_inode->i_fop->fasync)
4796 + lower_file = unionfs_lower_file_idx(file, bindex);
4797 + mutex_lock(&lower_inode->i_mutex);
4798 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4799 + if (!err && bindex == bstart)
4800 + fsstack_copy_attr_times(inode, lower_inode);
4801 + mutex_unlock(&lower_inode->i_mutex);
4808 + unionfs_check_file(file);
4809 + unionfs_unlock_dentry(dentry);
4810 + unionfs_unlock_parent(dentry, parent);
4811 + unionfs_read_unlock(dentry->d_sb);
4815 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4816 + struct pipe_inode_info *pipe, size_t len,
4817 + unsigned int flags)
4820 + struct file *lower_file;
4821 + struct dentry *dentry = file->f_path.dentry;
4822 + struct dentry *parent;
4824 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4825 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4826 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4828 + err = unionfs_file_revalidate(file, parent, false);
4829 + if (unlikely(err))
4832 + lower_file = unionfs_lower_file(file);
4833 + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4834 + /* update our inode atime upon a successful lower splice-read */
4836 + fsstack_copy_attr_atime(dentry->d_inode,
4837 + lower_file->f_path.dentry->d_inode);
4838 + unionfs_check_file(file);
4842 + unionfs_unlock_dentry(dentry);
4843 + unionfs_unlock_parent(dentry, parent);
4844 + unionfs_read_unlock(dentry->d_sb);
4848 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4849 + struct file *file, loff_t *ppos,
4850 + size_t len, unsigned int flags)
4853 + struct file *lower_file;
4854 + struct dentry *dentry = file->f_path.dentry;
4855 + struct dentry *parent;
4857 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4858 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4859 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4861 + err = unionfs_file_revalidate(file, parent, true);
4862 + if (unlikely(err))
4865 + lower_file = unionfs_lower_file(file);
4866 + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4867 + /* update our inode times+sizes upon a successful lower write */
4869 + fsstack_copy_inode_size(dentry->d_inode,
4870 + lower_file->f_path.dentry->d_inode);
4871 + fsstack_copy_attr_times(dentry->d_inode,
4872 + lower_file->f_path.dentry->d_inode);
4873 + unionfs_check_file(file);
4877 + unionfs_unlock_dentry(dentry);
4878 + unionfs_unlock_parent(dentry, parent);
4879 + unionfs_read_unlock(dentry->d_sb);
4883 +struct file_operations unionfs_main_fops = {
4884 + .llseek = generic_file_llseek,
4885 + .read = unionfs_read,
4886 + .write = unionfs_write,
4887 + .readdir = unionfs_file_readdir,
4888 + .unlocked_ioctl = unionfs_ioctl,
4889 + .mmap = unionfs_mmap,
4890 + .open = unionfs_open,
4891 + .flush = unionfs_flush,
4892 + .release = unionfs_file_release,
4893 + .fsync = unionfs_fsync,
4894 + .fasync = unionfs_fasync,
4895 + .splice_read = unionfs_splice_read,
4896 + .splice_write = unionfs_splice_write,
4898 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4899 new file mode 100644
4900 index 0000000..bd5a3b3
4902 +++ b/fs/unionfs/inode.c
4905 + * Copyright (c) 2003-2009 Erez Zadok
4906 + * Copyright (c) 2003-2006 Charles P. Wright
4907 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4908 + * Copyright (c) 2005-2006 Junjiro Okajima
4909 + * Copyright (c) 2005 Arun M. Krishnakumar
4910 + * Copyright (c) 2004-2006 David P. Quigley
4911 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4912 + * Copyright (c) 2003 Puja Gupta
4913 + * Copyright (c) 2003 Harikesavan Krishnan
4914 + * Copyright (c) 2003-2009 Stony Brook University
4915 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4917 + * This program is free software; you can redistribute it and/or modify
4918 + * it under the terms of the GNU General Public License version 2 as
4919 + * published by the Free Software Foundation.
4925 + * Find a writeable branch to create new object in. Checks all writeble
4926 + * branches of the parent inode, from istart to iend order; if none are
4927 + * suitable, also tries branch 0 (which may require a copyup).
4929 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4931 +static struct dentry *find_writeable_branch(struct inode *parent,
4932 + struct dentry *dentry)
4934 + int err = -EINVAL;
4935 + int bindex, istart, iend;
4936 + struct dentry *lower_dentry = NULL;
4938 + istart = ibstart(parent);
4939 + iend = ibend(parent);
4944 + for (bindex = istart; bindex <= iend; bindex++) {
4945 + /* skip non-writeable branches */
4946 + err = is_robranch_super(dentry->d_sb, bindex);
4951 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4952 + if (!lower_dentry)
4955 + * check for whiteouts in writeable branch, and remove them
4958 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4959 + if (err > 0) /* ignore if whiteout found and removed */
4963 + /* if get here, we can write to the branch */
4967 + * If istart wasn't already branch 0, and we got any error, then try
4968 + * branch 0 (which may require copyup)
4970 + if (err && istart > 0) {
4971 + istart = iend = 0;
4976 + * If we tried even branch 0, and still got an error, abort. But if
4977 + * the error was an EROFS, then we should try to copyup.
4979 + if (err && err != -EROFS)
4983 + * If we get here, then check if copyup needed. If lower_dentry is
4984 + * NULL, create the entire dentry directory structure in branch 0.
4986 + if (!lower_dentry) {
4988 + lower_dentry = create_parents(parent, dentry,
4989 + dentry->d_name.name, bindex);
4990 + if (IS_ERR(lower_dentry)) {
4991 + err = PTR_ERR(lower_dentry);
4995 + err = 0; /* all's well */
4998 + return ERR_PTR(err);
4999 + return lower_dentry;
5002 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
5003 + int mode, struct nameidata *nd_unused)
5006 + struct dentry *lower_dentry = NULL;
5007 + struct dentry *lower_parent_dentry = NULL;
5008 + struct dentry *parent;
5010 + struct nameidata lower_nd;
5012 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5013 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5014 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5016 + valid = __unionfs_d_revalidate(dentry, parent, false);
5017 + if (unlikely(!valid)) {
5018 + err = -ESTALE; /* same as what real_lookup does */
5022 + lower_dentry = find_writeable_branch(dir, dentry);
5023 + if (IS_ERR(lower_dentry)) {
5024 + err = PTR_ERR(lower_dentry);
5028 + lower_parent_dentry = lock_parent(lower_dentry);
5029 + if (IS_ERR(lower_parent_dentry)) {
5030 + err = PTR_ERR(lower_parent_dentry);
5034 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
5035 + if (unlikely(err < 0))
5037 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
5039 + release_lower_nd(&lower_nd, err);
5042 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5044 + unionfs_copy_attr_times(dir);
5045 + fsstack_copy_inode_size(dir,
5046 + lower_parent_dentry->d_inode);
5047 + /* update no. of links on parent directory */
5048 + dir->i_nlink = unionfs_get_nlinks(dir);
5052 + unlock_dir(lower_parent_dentry);
5056 + unionfs_postcopyup_setmnt(dentry);
5057 + unionfs_check_inode(dir);
5058 + unionfs_check_dentry(dentry);
5060 + unionfs_unlock_dentry(dentry);
5061 + unionfs_unlock_parent(dentry, parent);
5062 + unionfs_read_unlock(dentry->d_sb);
5067 + * unionfs_lookup is the only special function which takes a dentry, yet we
5068 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
5069 + * we don't have a valid dentry here yet.
5071 +static struct dentry *unionfs_lookup(struct inode *dir,
5072 + struct dentry *dentry,
5073 + struct nameidata *nd_unused)
5075 + struct dentry *ret, *parent;
5078 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5079 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5082 + * As long as we lock/dget the parent, then can skip validating the
5083 + * parent now; we may have to rebuild this dentry on the next
5084 + * ->d_revalidate, however.
5087 + /* allocate dentry private data. We free it in ->d_release */
5088 + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
5089 + if (unlikely(err)) {
5090 + ret = ERR_PTR(err);
5094 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
5096 + if (!IS_ERR(ret)) {
5099 + /* lookup_full can return multiple positive dentries */
5100 + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5101 + BUG_ON(dbstart(dentry) < 0);
5102 + unionfs_postcopyup_release(dentry);
5104 + unionfs_copy_attr_times(dentry->d_inode);
5107 + unionfs_check_inode(dir);
5109 + unionfs_check_dentry(dentry);
5110 + unionfs_check_dentry(parent);
5111 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5114 + unionfs_unlock_parent(dentry, parent);
5115 + unionfs_read_unlock(dentry->d_sb);
5120 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5121 + struct dentry *new_dentry)
5124 + struct dentry *lower_old_dentry = NULL;
5125 + struct dentry *lower_new_dentry = NULL;
5126 + struct dentry *lower_dir_dentry = NULL;
5127 + struct dentry *old_parent, *new_parent;
5128 + char *name = NULL;
5131 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5132 + old_parent = dget_parent(old_dentry);
5133 + new_parent = dget_parent(new_dentry);
5134 + unionfs_double_lock_parents(old_parent, new_parent);
5135 + unionfs_double_lock_dentry(old_dentry, new_dentry);
5137 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5138 + if (unlikely(!valid)) {
5142 + if (new_dentry->d_inode) {
5143 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5144 + if (unlikely(!valid)) {
5150 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5152 + /* check for a whiteout in new dentry branch, and delete it */
5153 + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5154 + dbstart(new_dentry));
5155 + if (err > 0) { /* whiteout found and removed successfully */
5156 + lower_dir_dentry = dget_parent(lower_new_dentry);
5157 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5158 + dput(lower_dir_dentry);
5159 + dir->i_nlink = unionfs_get_nlinks(dir);
5165 + /* check if parent hierachy is needed, then link in same branch */
5166 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5167 + lower_new_dentry = create_parents(dir, new_dentry,
5168 + new_dentry->d_name.name,
5169 + dbstart(old_dentry));
5170 + err = PTR_ERR(lower_new_dentry);
5171 + if (IS_COPYUP_ERR(err))
5173 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5176 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5177 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5179 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5180 + lower_dir_dentry = lock_parent(lower_new_dentry);
5181 + err = is_robranch(old_dentry);
5183 + /* see Documentation/filesystems/unionfs/issues.txt */
5185 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5186 + lower_new_dentry);
5189 + unlock_dir(lower_dir_dentry);
5192 + if (IS_COPYUP_ERR(err)) {
5193 + int old_bstart = dbstart(old_dentry);
5196 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5197 + err = copyup_dentry(old_parent->d_inode,
5198 + old_dentry, old_bstart,
5199 + bindex, old_dentry->d_name.name,
5200 + old_dentry->d_name.len, NULL,
5201 + i_size_read(old_dentry->d_inode));
5204 + lower_new_dentry =
5205 + create_parents(dir, new_dentry,
5206 + new_dentry->d_name.name,
5208 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5209 + lower_dir_dentry = lock_parent(lower_new_dentry);
5210 + /* see Documentation/filesystems/unionfs/issues.txt */
5213 + err = vfs_link(lower_old_dentry,
5214 + lower_dir_dentry->d_inode,
5215 + lower_new_dentry);
5217 + unlock_dir(lower_dir_dentry);
5224 + if (err || !lower_new_dentry->d_inode)
5227 + /* Its a hard link, so use the same inode */
5228 + new_dentry->d_inode = igrab(old_dentry->d_inode);
5229 + d_add(new_dentry, new_dentry->d_inode);
5230 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5231 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5233 + /* propagate number of hard-links */
5234 + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5235 + /* new dentry's ctime may have changed due to hard-link counts */
5236 + unionfs_copy_attr_times(new_dentry->d_inode);
5239 + if (!new_dentry->d_inode)
5240 + d_drop(new_dentry);
5244 + unionfs_postcopyup_setmnt(new_dentry);
5246 + unionfs_check_inode(dir);
5247 + unionfs_check_dentry(new_dentry);
5248 + unionfs_check_dentry(old_dentry);
5250 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5251 + unionfs_double_unlock_parents(old_parent, new_parent);
5254 + unionfs_read_unlock(old_dentry->d_sb);
5259 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5260 + const char *symname)
5263 + struct dentry *lower_dentry = NULL;
5264 + struct dentry *wh_dentry = NULL;
5265 + struct dentry *lower_parent_dentry = NULL;
5266 + struct dentry *parent;
5267 + char *name = NULL;
5271 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5272 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5273 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5275 + valid = __unionfs_d_revalidate(dentry, parent, false);
5276 + if (unlikely(!valid)) {
5282 + * It's only a bug if this dentry was not negative and couldn't be
5283 + * revalidated (shouldn't happen).
5285 + BUG_ON(!valid && dentry->d_inode);
5287 + lower_dentry = find_writeable_branch(dir, dentry);
5288 + if (IS_ERR(lower_dentry)) {
5289 + err = PTR_ERR(lower_dentry);
5293 + lower_parent_dentry = lock_parent(lower_dentry);
5294 + if (IS_ERR(lower_parent_dentry)) {
5295 + err = PTR_ERR(lower_parent_dentry);
5300 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5302 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5304 + unionfs_copy_attr_times(dir);
5305 + fsstack_copy_inode_size(dir,
5306 + lower_parent_dentry->d_inode);
5307 + /* update no. of links on parent directory */
5308 + dir->i_nlink = unionfs_get_nlinks(dir);
5312 + unlock_dir(lower_parent_dentry);
5319 + unionfs_postcopyup_setmnt(dentry);
5320 + unionfs_check_inode(dir);
5321 + unionfs_check_dentry(dentry);
5323 + unionfs_unlock_dentry(dentry);
5324 + unionfs_unlock_parent(dentry, parent);
5325 + unionfs_read_unlock(dentry->d_sb);
5329 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5332 + struct dentry *lower_dentry = NULL;
5333 + struct dentry *lower_parent_dentry = NULL;
5334 + struct dentry *parent;
5335 + int bindex = 0, bstart;
5336 + char *name = NULL;
5339 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5340 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5341 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5343 + valid = __unionfs_d_revalidate(dentry, parent, false);
5344 + if (unlikely(!valid)) {
5345 + err = -ESTALE; /* same as what real_lookup does */
5349 + bstart = dbstart(dentry);
5351 + lower_dentry = unionfs_lower_dentry(dentry);
5353 + /* check for a whiteout in new dentry branch, and delete it */
5354 + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5355 + if (err > 0) /* whiteout found and removed successfully */
5358 + /* exit if the error returned was NOT -EROFS */
5359 + if (!IS_COPYUP_ERR(err))
5364 + /* check if copyup's needed, and mkdir */
5365 + for (bindex = bstart; bindex >= 0; bindex--) {
5367 + int bend = dbend(dentry);
5369 + if (is_robranch_super(dentry->d_sb, bindex))
5372 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5373 + if (!lower_dentry) {
5374 + lower_dentry = create_parents(dir, dentry,
5375 + dentry->d_name.name,
5377 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5378 + printk(KERN_ERR "unionfs: lower dentry "
5379 + " NULL for bindex = %d\n", bindex);
5384 + lower_parent_dentry = lock_parent(lower_dentry);
5386 + if (IS_ERR(lower_parent_dentry)) {
5387 + err = PTR_ERR(lower_parent_dentry);
5391 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5394 + unlock_dir(lower_parent_dentry);
5396 + /* did the mkdir succeed? */
5400 + for (i = bindex + 1; i <= bend; i++) {
5401 + /* XXX: use path_put_lowers? */
5402 + if (unionfs_lower_dentry_idx(dentry, i)) {
5403 + dput(unionfs_lower_dentry_idx(dentry, i));
5404 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5407 + dbend(dentry) = bindex;
5410 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5413 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5415 + unionfs_copy_attr_times(dir);
5416 + fsstack_copy_inode_size(dir,
5417 + lower_parent_dentry->d_inode);
5419 + /* update number of links on parent directory */
5420 + dir->i_nlink = unionfs_get_nlinks(dir);
5423 + err = make_dir_opaque(dentry, dbstart(dentry));
5425 + printk(KERN_ERR "unionfs: mkdir: error creating "
5426 + ".wh.__dir_opaque: %d\n", err);
5430 + /* we are done! */
5435 + if (!dentry->d_inode)
5441 + unionfs_copy_attr_times(dentry->d_inode);
5442 + unionfs_postcopyup_setmnt(dentry);
5444 + unionfs_check_inode(dir);
5445 + unionfs_check_dentry(dentry);
5446 + unionfs_unlock_dentry(dentry);
5447 + unionfs_unlock_parent(dentry, parent);
5448 + unionfs_read_unlock(dentry->d_sb);
5453 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5457 + struct dentry *lower_dentry = NULL;
5458 + struct dentry *wh_dentry = NULL;
5459 + struct dentry *lower_parent_dentry = NULL;
5460 + struct dentry *parent;
5461 + char *name = NULL;
5464 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5465 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5466 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5468 + valid = __unionfs_d_revalidate(dentry, parent, false);
5469 + if (unlikely(!valid)) {
5475 + * It's only a bug if this dentry was not negative and couldn't be
5476 + * revalidated (shouldn't happen).
5478 + BUG_ON(!valid && dentry->d_inode);
5480 + lower_dentry = find_writeable_branch(dir, dentry);
5481 + if (IS_ERR(lower_dentry)) {
5482 + err = PTR_ERR(lower_dentry);
5486 + lower_parent_dentry = lock_parent(lower_dentry);
5487 + if (IS_ERR(lower_parent_dentry)) {
5488 + err = PTR_ERR(lower_parent_dentry);
5492 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5494 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5496 + unionfs_copy_attr_times(dir);
5497 + fsstack_copy_inode_size(dir,
5498 + lower_parent_dentry->d_inode);
5499 + /* update no. of links on parent directory */
5500 + dir->i_nlink = unionfs_get_nlinks(dir);
5504 + unlock_dir(lower_parent_dentry);
5511 + unionfs_postcopyup_setmnt(dentry);
5512 + unionfs_check_inode(dir);
5513 + unionfs_check_dentry(dentry);
5515 + unionfs_unlock_dentry(dentry);
5516 + unionfs_unlock_parent(dentry, parent);
5517 + unionfs_read_unlock(dentry->d_sb);
5521 +/* requires sb, dentry, and parent to already be locked */
5522 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5526 + struct dentry *lower_dentry;
5528 + lower_dentry = unionfs_lower_dentry(dentry);
5530 + if (!lower_dentry->d_inode->i_op ||
5531 + !lower_dentry->d_inode->i_op->readlink) {
5536 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5539 + fsstack_copy_attr_atime(dentry->d_inode,
5540 + lower_dentry->d_inode);
5546 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5550 + struct dentry *parent;
5552 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5553 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5554 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5556 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5561 + err = __unionfs_readlink(dentry, buf, bufsiz);
5564 + unionfs_check_dentry(dentry);
5565 + unionfs_unlock_dentry(dentry);
5566 + unionfs_unlock_parent(dentry, parent);
5567 + unionfs_read_unlock(dentry->d_sb);
5572 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5575 + int len = PAGE_SIZE, err;
5576 + mm_segment_t old_fs;
5577 + struct dentry *parent;
5579 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5580 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5581 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5583 + /* This is freed by the put_link method assuming a successful call. */
5584 + buf = kmalloc(len, GFP_KERNEL);
5585 + if (unlikely(!buf)) {
5590 + /* read the symlink, and then we will follow it */
5591 + old_fs = get_fs();
5592 + set_fs(KERNEL_DS);
5593 + err = __unionfs_readlink(dentry, buf, len);
5601 + nd_set_link(nd, buf);
5606 + unionfs_check_nd(nd);
5607 + unionfs_check_dentry(dentry);
5610 + unionfs_unlock_dentry(dentry);
5611 + unionfs_unlock_parent(dentry, parent);
5612 + unionfs_read_unlock(dentry->d_sb);
5614 + return ERR_PTR(err);
5617 +/* this @nd *IS* still used */
5618 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5621 + struct dentry *parent;
5623 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5624 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5625 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5627 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5629 + "unionfs: put_link failed to revalidate dentry\n");
5631 + unionfs_check_dentry(dentry);
5632 + unionfs_check_nd(nd);
5633 + kfree(nd_get_link(nd));
5634 + unionfs_unlock_dentry(dentry);
5635 + unionfs_unlock_parent(dentry, parent);
5636 + unionfs_read_unlock(dentry->d_sb);
5640 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5641 + * skips over EROFS tests (because we perform copyup on EROFS).
5643 +static int __inode_permission(struct inode *inode, int mask)
5647 + /* nobody gets write access to an immutable file */
5648 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5651 + /* Ordinary permission routines do not understand MAY_APPEND. */
5652 + if (inode->i_op && inode->i_op->permission) {
5653 + retval = inode->i_op->permission(inode, mask);
5656 + * Exec permission on a regular file is denied if none
5657 + * of the execute bits are set.
5659 + * This check should be done by the ->permission()
5662 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5663 + !(inode->i_mode & S_IXUGO))
5667 + retval = generic_permission(inode, mask, NULL);
5672 + return security_inode_permission(inode,
5673 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5677 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5678 + * a deadlock with the branch-management "add branch" code (which grabbed
5679 + * the write lock). It is safe to not grab the read lock here, because even
5680 + * with branch management taking place, there is no chance that
5681 + * unionfs_permission, or anything it calls, will use stale branch
5684 +static int unionfs_permission(struct inode *inode, int mask)
5686 + struct inode *lower_inode = NULL;
5688 + int bindex, bstart, bend;
5689 + const int is_file = !S_ISDIR(inode->i_mode);
5690 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5691 + struct inode *inode_grabbed = igrab(inode);
5692 + struct dentry *dentry = d_find_alias(inode);
5695 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5697 + if (!UNIONFS_I(inode)->lower_inodes) {
5698 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5699 + err = -ESTALE; /* force revalidate */
5702 + bstart = ibstart(inode);
5703 + bend = ibend(inode);
5704 + if (unlikely(bstart < 0 || bend < 0)) {
5706 + * With branch-management, we can get a stale inode here.
5707 + * If so, we return ESTALE back to link_path_walk, which
5708 + * would discard the dcache entry and re-lookup the
5709 + * dentry+inode. This should be equivalent to issuing
5710 + * __unionfs_d_revalidate_chain on nd.dentry here.
5712 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5713 + err = -ESTALE; /* force revalidate */
5717 + for (bindex = bstart; bindex <= bend; bindex++) {
5718 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5723 + * check the condition for D-F-D underlying files/directories,
5724 + * we don't have to check for files, if we are checking for
5727 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5731 + * We check basic permissions, but we ignore any conditions
5732 + * such as readonly file systems or branches marked as
5733 + * readonly, because those conditions should lead to a
5734 + * copyup taking place later on. However, if user never had
5735 + * access to the file, then no copyup could ever take place.
5737 + err = __inode_permission(lower_inode, mask);
5738 + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5739 + umode_t mode = lower_inode->i_mode;
5740 + if ((is_robranch_super(inode->i_sb, bindex) ||
5741 + __is_rdonly(lower_inode)) &&
5742 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5744 + if (IS_COPYUP_ERR(err))
5749 + * NFS HACK: NFSv2/3 return EACCES on readonly-exported,
5750 + * locally readonly-mounted file systems, instead of EROFS
5751 + * like other file systems do. So we have no choice here
5752 + * but to intercept this and ignore it for NFS branches
5753 + * marked readonly. Specifically, we avoid using NFS's own
5754 + * "broken" ->permission method, and rely on
5755 + * generic_permission() to do basic checking for us.
5757 + if (err && err == -EACCES &&
5758 + is_robranch_super(inode->i_sb, bindex) &&
5759 + lower_inode->i_sb->s_magic == NFS_SUPER_MAGIC)
5760 + err = generic_permission(lower_inode, mask, NULL);
5763 + * The permissions are an intersection of the overall directory
5764 + * permissions, so we fail if one fails.
5769 + /* only the leftmost file matters. */
5770 + if (is_file || write_mask) {
5771 + if (is_file && write_mask) {
5772 + err = get_write_access(lower_inode);
5774 + put_write_access(lower_inode);
5779 + /* sync times which may have changed (asynchronously) below */
5780 + unionfs_copy_attr_times(inode);
5783 + unionfs_check_inode(inode);
5785 + unionfs_unlock_dentry(dentry);
5788 + iput(inode_grabbed);
5792 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5795 + struct dentry *lower_dentry;
5796 + struct dentry *parent;
5797 + struct inode *inode;
5798 + struct inode *lower_inode;
5799 + int bstart, bend, bindex;
5802 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5803 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5804 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5806 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5811 + bstart = dbstart(dentry);
5812 + bend = dbend(dentry);
5813 + inode = dentry->d_inode;
5816 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5817 + * to reinterpret it in its own way.
5819 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5820 + ia->ia_valid &= ~ATTR_MODE;
5822 + lower_dentry = unionfs_lower_dentry(dentry);
5823 + if (!lower_dentry) { /* should never happen after above revalidate */
5827 + lower_inode = unionfs_lower_inode(inode);
5829 + /* check if user has permission to change lower inode */
5830 + err = inode_change_ok(lower_inode, ia);
5834 + /* copyup if the file is on a read only branch */
5835 + if (is_robranch_super(dentry->d_sb, bstart)
5836 + || __is_rdonly(lower_inode)) {
5837 + /* check if we have a branch to copy up to */
5838 + if (bstart <= 0) {
5843 + if (ia->ia_valid & ATTR_SIZE)
5844 + size = ia->ia_size;
5846 + size = i_size_read(inode);
5847 + /* copyup to next available branch */
5848 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5849 + err = copyup_dentry(parent->d_inode,
5850 + dentry, bstart, bindex,
5851 + dentry->d_name.name,
5852 + dentry->d_name.len,
5859 + /* get updated lower_dentry/inode after copyup */
5860 + lower_dentry = unionfs_lower_dentry(dentry);
5861 + lower_inode = unionfs_lower_inode(inode);
5865 + * If shrinking, first truncate upper level to cancel writing dirty
5866 + * pages beyond the new eof; and also if its' maxbytes is more
5867 + * limiting (fail with -EFBIG before making any change to the lower
5868 + * level). There is no need to vmtruncate the upper level
5869 + * afterwards in the other cases: we fsstack_copy_inode_size from
5870 + * the lower level.
5872 + if (ia->ia_valid & ATTR_SIZE) {
5873 + size = i_size_read(inode);
5874 + if (ia->ia_size < size || (ia->ia_size > size &&
5875 + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5876 + err = vmtruncate(inode, ia->ia_size);
5882 + /* notify the (possibly copied-up) lower inode */
5884 + * Note: we use lower_dentry->d_inode, because lower_inode may be
5885 + * unlinked (no inode->i_sb and i_ino==0. This happens if someone
5886 + * tries to open(), unlink(), then ftruncate() a file.
5888 + mutex_lock(&lower_dentry->d_inode->i_mutex);
5889 + err = notify_change(lower_dentry, ia);
5890 + mutex_unlock(&lower_dentry->d_inode->i_mutex);
5894 + /* get attributes from the first lower inode */
5895 + if (ibstart(inode) >= 0)
5896 + unionfs_copy_attr_all(inode, lower_inode);
5898 + * unionfs_copy_attr_all will copy the lower times to our inode if
5899 + * the lower ones are newer (useful for cache coherency). However,
5900 + * ->setattr is the only place in which we may have to copy the
5901 + * lower inode times absolutely, to support utimes(2).
5903 + if (ia->ia_valid & ATTR_MTIME_SET)
5904 + inode->i_mtime = lower_inode->i_mtime;
5905 + if (ia->ia_valid & ATTR_CTIME)
5906 + inode->i_ctime = lower_inode->i_ctime;
5907 + if (ia->ia_valid & ATTR_ATIME_SET)
5908 + inode->i_atime = lower_inode->i_atime;
5909 + fsstack_copy_inode_size(inode, lower_inode);
5913 + unionfs_check_dentry(dentry);
5914 + unionfs_unlock_dentry(dentry);
5915 + unionfs_unlock_parent(dentry, parent);
5916 + unionfs_read_unlock(dentry->d_sb);
5921 +struct inode_operations unionfs_symlink_iops = {
5922 + .readlink = unionfs_readlink,
5923 + .permission = unionfs_permission,
5924 + .follow_link = unionfs_follow_link,
5925 + .setattr = unionfs_setattr,
5926 + .put_link = unionfs_put_link,
5929 +struct inode_operations unionfs_dir_iops = {
5930 + .create = unionfs_create,
5931 + .lookup = unionfs_lookup,
5932 + .link = unionfs_link,
5933 + .unlink = unionfs_unlink,
5934 + .symlink = unionfs_symlink,
5935 + .mkdir = unionfs_mkdir,
5936 + .rmdir = unionfs_rmdir,
5937 + .mknod = unionfs_mknod,
5938 + .rename = unionfs_rename,
5939 + .permission = unionfs_permission,
5940 + .setattr = unionfs_setattr,
5941 +#ifdef CONFIG_UNION_FS_XATTR
5942 + .setxattr = unionfs_setxattr,
5943 + .getxattr = unionfs_getxattr,
5944 + .removexattr = unionfs_removexattr,
5945 + .listxattr = unionfs_listxattr,
5946 +#endif /* CONFIG_UNION_FS_XATTR */
5949 +struct inode_operations unionfs_main_iops = {
5950 + .permission = unionfs_permission,
5951 + .setattr = unionfs_setattr,
5952 +#ifdef CONFIG_UNION_FS_XATTR
5953 + .setxattr = unionfs_setxattr,
5954 + .getxattr = unionfs_getxattr,
5955 + .removexattr = unionfs_removexattr,
5956 + .listxattr = unionfs_listxattr,
5957 +#endif /* CONFIG_UNION_FS_XATTR */
5959 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5960 new file mode 100644
5961 index 0000000..6361541
5963 +++ b/fs/unionfs/lookup.c
5966 + * Copyright (c) 2003-2009 Erez Zadok
5967 + * Copyright (c) 2003-2006 Charles P. Wright
5968 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5969 + * Copyright (c) 2005-2006 Junjiro Okajima
5970 + * Copyright (c) 2005 Arun M. Krishnakumar
5971 + * Copyright (c) 2004-2006 David P. Quigley
5972 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5973 + * Copyright (c) 2003 Puja Gupta
5974 + * Copyright (c) 2003 Harikesavan Krishnan
5975 + * Copyright (c) 2003-2009 Stony Brook University
5976 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
5978 + * This program is free software; you can redistribute it and/or modify
5979 + * it under the terms of the GNU General Public License version 2 as
5980 + * published by the Free Software Foundation.
5986 + * Lookup one path component @name relative to a <base,mnt> path pair.
5987 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5988 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5989 + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5990 + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5991 + * @dentry and @new_mnt.
5993 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5994 + const char *name, struct vfsmount **new_mnt)
5996 + struct dentry *dentry = NULL;
5997 + struct nameidata lower_nd;
6000 + /* we use flags=0 to get basic lookup */
6001 + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
6004 + case 0: /* no error */
6005 + dentry = lower_nd.path.dentry;
6007 + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
6011 + * We don't consider ENOENT an error, and we want to return
6012 + * a negative dentry (ala lookup_one_len). As we know
6013 + * there was no inode for this name before (-ENOENT), then
6014 + * it's safe to call lookup_one_len (which doesn't take a
6017 + dentry = lookup_lck_len(name, base, strlen(name));
6019 + *new_mnt = mntget(lower_nd.path.mnt);
6021 + default: /* all other real errors */
6022 + dentry = ERR_PTR(err);
6030 + * This is a utility function that fills in a unionfs dentry.
6031 + * Caller must lock this dentry with unionfs_lock_dentry.
6033 + * Returns: 0 (ok), or -ERRNO if an error occurred.
6034 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
6036 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
6038 + struct dentry *tmp;
6039 + int err = -ENOSYS;
6041 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
6047 + if (IS_ERR(tmp)) {
6048 + err = PTR_ERR(tmp);
6051 + /* XXX: need to change the interface */
6052 + BUG_ON(tmp != dentry);
6057 +/* The dentry cache is just so we have properly sized dentries. */
6058 +static struct kmem_cache *unionfs_dentry_cachep;
6059 +int unionfs_init_dentry_cache(void)
6061 + unionfs_dentry_cachep =
6062 + kmem_cache_create("unionfs_dentry",
6063 + sizeof(struct unionfs_dentry_info),
6064 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6066 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6069 +void unionfs_destroy_dentry_cache(void)
6071 + if (unionfs_dentry_cachep)
6072 + kmem_cache_destroy(unionfs_dentry_cachep);
6075 +void free_dentry_private_data(struct dentry *dentry)
6077 + if (!dentry || !dentry->d_fsdata)
6079 + kfree(UNIONFS_D(dentry)->lower_paths);
6080 + UNIONFS_D(dentry)->lower_paths = NULL;
6081 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6082 + dentry->d_fsdata = NULL;
6085 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6087 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6093 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6094 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6098 + info->lower_paths = p;
6100 + info->bstart = -1;
6102 + info->bopaque = -1;
6103 + info->bcount = sbmax(dentry->d_sb);
6104 + atomic_set(&info->generation,
6105 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6107 + memset(info->lower_paths, 0, size);
6112 +/* UNIONFS_D(dentry)->lock must be locked */
6113 +int realloc_dentry_private_data(struct dentry *dentry)
6115 + if (!__realloc_dentry_private_data(dentry))
6118 + kfree(UNIONFS_D(dentry)->lower_paths);
6119 + free_dentry_private_data(dentry);
6123 +/* allocate new dentry private data */
6124 +int new_dentry_private_data(struct dentry *dentry, int subclass)
6126 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6130 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6131 + if (unlikely(!info))
6134 + mutex_init(&info->lock);
6135 + mutex_lock_nested(&info->lock, subclass);
6137 + info->lower_paths = NULL;
6139 + dentry->d_fsdata = info;
6141 + if (!__realloc_dentry_private_data(dentry))
6144 + mutex_unlock(&info->lock);
6145 + free_dentry_private_data(dentry);
6150 + * scan through the lower dentry objects, and set bstart to reflect the
6153 +void update_bstart(struct dentry *dentry)
6156 + int bstart = dbstart(dentry);
6157 + int bend = dbend(dentry);
6158 + struct dentry *lower_dentry;
6160 + for (bindex = bstart; bindex <= bend; bindex++) {
6161 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6162 + if (!lower_dentry)
6164 + if (lower_dentry->d_inode) {
6165 + dbstart(dentry) = bindex;
6168 + dput(lower_dentry);
6169 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6175 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6176 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6177 + * Inside that nd structure, this function may also return an allocated
6178 + * struct file (for open intents). The caller, when done with this nd, must
6179 + * kfree the intent file (using release_lower_nd).
6181 + * XXX: this code, and the callers of this code, should be redone using
6182 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6183 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6184 + * function and a method that other file systems can call.
6186 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6189 +#ifdef ALLOC_LOWER_ND_FILE
6191 + * XXX: one day we may need to have the lower return an open file
6192 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6193 + * very well be needed for nfs4.
6195 + struct file *file;
6196 +#endif /* ALLOC_LOWER_ND_FILE */
6198 + memset(nd, 0, sizeof(struct nameidata));
6203 + case LOOKUP_CREATE:
6204 + nd->intent.open.flags |= O_CREAT;
6205 + /* fall through: shared code for create/open cases */
6207 + nd->flags = flags;
6208 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6209 +#ifdef ALLOC_LOWER_ND_FILE
6210 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6211 + if (unlikely(!file)) {
6213 + break; /* exit switch statement and thus return */
6215 + nd->intent.open.file = file;
6216 +#endif /* ALLOC_LOWER_ND_FILE */
6220 + * We should never get here, for now.
6221 + * We can add new cases here later on.
6223 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6231 +void release_lower_nd(struct nameidata *nd, int err)
6233 + if (!nd->intent.open.file)
6236 + release_open_intent(nd);
6237 +#ifdef ALLOC_LOWER_ND_FILE
6238 + kfree(nd->intent.open.file);
6239 +#endif /* ALLOC_LOWER_ND_FILE */
6243 + * Main (and complex) driver function for Unionfs's lookup
6245 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6246 + * PTR if d_splice returned a different dentry.
6248 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6249 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6250 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6251 + * dentry's info, which the caller must unlock.
6253 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6254 + struct dentry *parent, int lookupmode)
6257 + struct dentry *lower_dentry = NULL;
6258 + struct vfsmount *lower_mnt;
6259 + struct vfsmount *lower_dir_mnt;
6260 + struct dentry *wh_lower_dentry = NULL;
6261 + struct dentry *lower_dir_dentry = NULL;
6262 + struct dentry *d_interposed = NULL;
6263 + int bindex, bstart, bend, bopaque;
6264 + int opaque, num_positive = 0;
6267 + int pos_start, pos_end;
6270 + * We should already have a lock on this dentry in the case of a
6271 + * partial lookup, or a revalidation. Otherwise it is returned from
6272 + * new_dentry_private_data already locked.
6274 + verify_locked(dentry);
6275 + verify_locked(parent);
6277 + /* must initialize dentry operations */
6278 + dentry->d_op = &unionfs_dops;
6280 + /* We never partial lookup the root directory. */
6281 + if (IS_ROOT(dentry))
6284 + name = dentry->d_name.name;
6285 + namelen = dentry->d_name.len;
6287 + /* No dentries should get created for possible whiteout names. */
6288 + if (!is_validname(name)) {
6293 + /* Now start the actual lookup procedure. */
6294 + bstart = dbstart(parent);
6295 + bend = dbend(parent);
6296 + bopaque = dbopaque(parent);
6297 + BUG_ON(bstart < 0);
6299 + /* adjust bend to bopaque if needed */
6300 + if ((bopaque >= 0) && (bopaque < bend))
6303 + /* lookup all possible dentries */
6304 + for (bindex = bstart; bindex <= bend; bindex++) {
6306 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6307 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6309 + /* skip if we already have a positive lower dentry */
6310 + if (lower_dentry) {
6311 + if (dbstart(dentry) < 0)
6312 + dbstart(dentry) = bindex;
6313 + if (bindex > dbend(dentry))
6314 + dbend(dentry) = bindex;
6315 + if (lower_dentry->d_inode)
6320 + lower_dir_dentry =
6321 + unionfs_lower_dentry_idx(parent, bindex);
6322 + /* if the lower dentry's parent does not exist, skip this */
6323 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6326 + /* also skip it if the parent isn't a directory. */
6327 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6328 + continue; /* XXX: should be BUG_ON */
6330 + /* check for whiteouts: stop lookup if found */
6331 + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6332 + if (IS_ERR(wh_lower_dentry)) {
6333 + err = PTR_ERR(wh_lower_dentry);
6336 + if (wh_lower_dentry->d_inode) {
6337 + dbend(dentry) = dbopaque(dentry) = bindex;
6338 + if (dbstart(dentry) < 0)
6339 + dbstart(dentry) = bindex;
6340 + dput(wh_lower_dentry);
6343 + dput(wh_lower_dentry);
6345 + /* Now do regular lookup; lookup @name */
6346 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6347 + lower_mnt = NULL; /* XXX: needed? */
6349 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6350 + name, &lower_mnt);
6352 + if (IS_ERR(lower_dentry)) {
6353 + err = PTR_ERR(lower_dentry);
6356 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6358 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6360 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6362 + /* adjust dbstart/end */
6363 + if (dbstart(dentry) < 0)
6364 + dbstart(dentry) = bindex;
6365 + if (bindex > dbend(dentry))
6366 + dbend(dentry) = bindex;
6368 + * We always store the lower dentries above, and update
6369 + * dbstart/dbend, even if the whole unionfs dentry is
6370 + * negative (i.e., no lower inodes).
6372 + if (!lower_dentry->d_inode)
6377 + * check if we just found an opaque directory, if so, stop
6380 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6382 + opaque = is_opaque_dir(dentry, bindex);
6386 + } else if (opaque) {
6387 + dbend(dentry) = dbopaque(dentry) = bindex;
6390 + dbend(dentry) = bindex;
6392 + /* update parent directory's atime with the bindex */
6393 + fsstack_copy_attr_atime(parent->d_inode,
6394 + lower_dir_dentry->d_inode);
6397 + /* sanity checks, then decide if to process a negative dentry */
6398 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6399 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6401 + if (num_positive > 0)
6402 + goto out_positive;
6404 + /*** handle NEGATIVE dentries ***/
6407 + * If negative, keep only first lower negative dentry, to save on
6410 + if (dbstart(dentry) < dbend(dentry)) {
6411 + path_put_lowers(dentry, dbstart(dentry) + 1,
6412 + dbend(dentry), false);
6413 + dbend(dentry) = dbstart(dentry);
6415 + if (lookupmode == INTERPOSE_PARTIAL)
6417 + if (lookupmode == INTERPOSE_LOOKUP) {
6419 + * If all we found was a whiteout in the first available
6420 + * branch, then create a negative dentry for a possibly new
6421 + * file to be created.
6423 + if (dbopaque(dentry) < 0)
6425 + /* XXX: need to get mnt here */
6426 + bindex = dbstart(dentry);
6427 + if (unionfs_lower_dentry_idx(dentry, bindex))
6429 + lower_dir_dentry =
6430 + unionfs_lower_dentry_idx(parent, bindex);
6431 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6433 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6434 + goto out; /* XXX: should be BUG_ON */
6435 + /* XXX: do we need to cross bind mounts here? */
6436 + lower_dentry = lookup_lck_len(name, lower_dir_dentry, namelen);
6437 + if (IS_ERR(lower_dentry)) {
6438 + err = PTR_ERR(lower_dentry);
6441 + /* XXX: need to mntget/mntput as needed too! */
6442 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6443 + /* XXX: wrong mnt for crossing bind mounts! */
6444 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6445 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6450 + /* if we're revalidating a positive dentry, don't make it negative */
6451 + if (lookupmode != INTERPOSE_REVAL)
6452 + d_add(dentry, NULL);
6457 + /*** handle POSITIVE dentries ***/
6460 + * This unionfs dentry is positive (at least one lower inode
6461 + * exists), so scan entire dentry from beginning to end, and remove
6462 + * any negative lower dentries, if any. Then, update dbstart/dbend
6463 + * to reflect the start/end of positive dentries.
6465 + pos_start = pos_end = -1;
6466 + for (bindex = bstart; bindex <= bend; bindex++) {
6467 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6469 + if (lower_dentry && lower_dentry->d_inode) {
6470 + if (pos_start < 0)
6471 + pos_start = bindex;
6472 + if (bindex > pos_end)
6476 + path_put_lowers(dentry, bindex, bindex, false);
6478 + if (pos_start >= 0)
6479 + dbstart(dentry) = pos_start;
6481 + dbend(dentry) = pos_end;
6483 + /* Partial lookups need to re-interpose, or throw away older negs. */
6484 + if (lookupmode == INTERPOSE_PARTIAL) {
6485 + if (dentry->d_inode) {
6486 + unionfs_reinterpose(dentry);
6491 + * This dentry was positive, so it is as if we had a
6492 + * negative revalidation.
6494 + lookupmode = INTERPOSE_REVAL_NEG;
6495 + update_bstart(dentry);
6499 + * Interpose can return a dentry if d_splice returned a different
6502 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6503 + if (IS_ERR(d_interposed))
6504 + err = PTR_ERR(d_interposed);
6505 + else if (d_interposed)
6506 + dentry = d_interposed;
6513 + /* should dput/mntput all the underlying dentries on error condition */
6514 + if (dbstart(dentry) >= 0)
6515 + path_put_lowers_all(dentry, false);
6516 + /* free lower_paths unconditionally */
6517 + kfree(UNIONFS_D(dentry)->lower_paths);
6518 + UNIONFS_D(dentry)->lower_paths = NULL;
6521 + if (dentry && UNIONFS_D(dentry)) {
6522 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6523 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6525 + if (d_interposed && UNIONFS_D(d_interposed)) {
6526 + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6527 + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6530 + if (!err && d_interposed)
6531 + return d_interposed;
6532 + return ERR_PTR(err);
6534 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6535 new file mode 100644
6536 index 0000000..c58405c
6538 +++ b/fs/unionfs/main.c
6541 + * Copyright (c) 2003-2009 Erez Zadok
6542 + * Copyright (c) 2003-2006 Charles P. Wright
6543 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6544 + * Copyright (c) 2005-2006 Junjiro Okajima
6545 + * Copyright (c) 2005 Arun M. Krishnakumar
6546 + * Copyright (c) 2004-2006 David P. Quigley
6547 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6548 + * Copyright (c) 2003 Puja Gupta
6549 + * Copyright (c) 2003 Harikesavan Krishnan
6550 + * Copyright (c) 2003-2009 Stony Brook University
6551 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
6553 + * This program is free software; you can redistribute it and/or modify
6554 + * it under the terms of the GNU General Public License version 2 as
6555 + * published by the Free Software Foundation.
6559 +#include <linux/module.h>
6560 +#include <linux/moduleparam.h>
6562 +static void unionfs_fill_inode(struct dentry *dentry,
6563 + struct inode *inode)
6565 + struct inode *lower_inode;
6566 + struct dentry *lower_dentry;
6567 + int bindex, bstart, bend;
6569 + bstart = dbstart(dentry);
6570 + bend = dbend(dentry);
6572 + for (bindex = bstart; bindex <= bend; bindex++) {
6573 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6574 + if (!lower_dentry) {
6575 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6579 + /* Initialize the lower inode to the new lower inode. */
6580 + if (!lower_dentry->d_inode)
6583 + unionfs_set_lower_inode_idx(inode, bindex,
6584 + igrab(lower_dentry->d_inode));
6587 + ibstart(inode) = dbstart(dentry);
6588 + ibend(inode) = dbend(dentry);
6590 + /* Use attributes from the first branch. */
6591 + lower_inode = unionfs_lower_inode(inode);
6593 + /* Use different set of inode ops for symlinks & directories */
6594 + if (S_ISLNK(lower_inode->i_mode))
6595 + inode->i_op = &unionfs_symlink_iops;
6596 + else if (S_ISDIR(lower_inode->i_mode))
6597 + inode->i_op = &unionfs_dir_iops;
6599 + /* Use different set of file ops for directories */
6600 + if (S_ISDIR(lower_inode->i_mode))
6601 + inode->i_fop = &unionfs_dir_fops;
6603 + /* properly initialize special inodes */
6604 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6605 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6606 + init_special_inode(inode, lower_inode->i_mode,
6607 + lower_inode->i_rdev);
6609 + /* all well, copy inode attributes */
6610 + unionfs_copy_attr_all(inode, lower_inode);
6611 + fsstack_copy_inode_size(inode, lower_inode);
6615 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6616 + * the classic stackable file system "vnode interposition" action.
6618 + * @sb: unionfs's super_block
6620 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6624 + struct inode *inode;
6625 + int need_fill_inode = 1;
6626 + struct dentry *spliced = NULL;
6628 + verify_locked(dentry);
6631 + * We allocate our new inode below by calling unionfs_iget,
6632 + * which will initialize some of the new inode's fields
6636 + * On revalidate we've already got our own inode and just need
6639 + if (flag == INTERPOSE_REVAL) {
6640 + inode = dentry->d_inode;
6641 + UNIONFS_I(inode)->bstart = -1;
6642 + UNIONFS_I(inode)->bend = -1;
6643 + atomic_set(&UNIONFS_I(inode)->generation,
6644 + atomic_read(&UNIONFS_SB(sb)->generation));
6646 + UNIONFS_I(inode)->lower_inodes =
6647 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6648 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6653 + /* get unique inode number for unionfs */
6654 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6655 + if (IS_ERR(inode)) {
6656 + err = PTR_ERR(inode);
6659 + if (atomic_read(&inode->i_count) > 1)
6663 + need_fill_inode = 0;
6664 + unionfs_fill_inode(dentry, inode);
6667 + /* only (our) lookup wants to do a d_add */
6669 + case INTERPOSE_DEFAULT:
6670 + /* for operations which create new inodes */
6671 + d_add(dentry, inode);
6673 + case INTERPOSE_REVAL_NEG:
6674 + d_instantiate(dentry, inode);
6676 + case INTERPOSE_LOOKUP:
6677 + spliced = d_splice_alias(inode, dentry);
6678 + if (spliced && spliced != dentry) {
6680 + * d_splice can return a dentry if it was
6681 + * disconnected and had to be moved. We must ensure
6682 + * that the private data of the new dentry is
6683 + * correct and that the inode info was filled
6684 + * properly. Finally we must return this new
6687 + spliced->d_op = &unionfs_dops;
6688 + spliced->d_fsdata = dentry->d_fsdata;
6689 + dentry->d_fsdata = NULL;
6691 + if (need_fill_inode) {
6692 + need_fill_inode = 0;
6693 + unionfs_fill_inode(dentry, inode);
6696 + } else if (!spliced) {
6697 + if (need_fill_inode) {
6698 + need_fill_inode = 0;
6699 + unionfs_fill_inode(dentry, inode);
6704 + case INTERPOSE_REVAL:
6708 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6717 + return ERR_PTR(err);
6720 +/* like interpose above, but for an already existing dentry */
6721 +void unionfs_reinterpose(struct dentry *dentry)
6723 + struct dentry *lower_dentry;
6724 + struct inode *inode;
6725 + int bindex, bstart, bend;
6727 + verify_locked(dentry);
6729 + /* This is pre-allocated inode */
6730 + inode = dentry->d_inode;
6732 + bstart = dbstart(dentry);
6733 + bend = dbend(dentry);
6734 + for (bindex = bstart; bindex <= bend; bindex++) {
6735 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6736 + if (!lower_dentry)
6739 + if (!lower_dentry->d_inode)
6741 + if (unionfs_lower_inode_idx(inode, bindex))
6743 + unionfs_set_lower_inode_idx(inode, bindex,
6744 + igrab(lower_dentry->d_inode));
6746 + ibstart(inode) = dbstart(dentry);
6747 + ibend(inode) = dbend(dentry);
6751 + * make sure the branch we just looked up (nd) makes sense:
6753 + * 1) we're not trying to stack unionfs on top of unionfs
6755 + * 3) is a directory
6757 +int check_branch(struct nameidata *nd)
6759 + /* XXX: remove in ODF code -- stacking unions allowed there */
6760 + if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6762 + if (!nd->path.dentry->d_inode)
6764 + if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6769 +/* checks if two lower_dentries have overlapping branches */
6770 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6772 + struct dentry *dent = NULL;
6775 + while ((dent != dent2) && (dent->d_parent != dent))
6776 + dent = dent->d_parent;
6778 + if (dent == dent2)
6782 + while ((dent != dent1) && (dent->d_parent != dent))
6783 + dent = dent->d_parent;
6785 + return (dent == dent1);
6789 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6790 + * specified. Fill the mode bits in @perms. If encounter an unknown
6791 + * string, return -EINVAL. Otherwise return 0.
6793 +int parse_branch_mode(const char *name, int *perms)
6795 + if (!name || !strcmp(name, "rw")) {
6796 + *perms = MAY_READ | MAY_WRITE;
6799 + if (!strcmp(name, "ro")) {
6800 + *perms = MAY_READ;
6807 + * parse the dirs= mount argument
6809 + * We don't need to lock the superblock private data's rwsem, as we get
6810 + * called only by unionfs_read_super - it is still a long time before anyone
6811 + * can even get a reference to us.
6813 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6814 + *lower_root_info, char *options)
6816 + struct nameidata nd;
6823 + struct dentry *dent1;
6824 + struct dentry *dent2;
6826 + if (options[0] == '\0') {
6827 + printk(KERN_ERR "unionfs: no branches specified\n");
6833 + * Each colon means we have a separator, this is really just a rough
6834 + * guess, since strsep will handle empty fields for us.
6836 + for (i = 0; options[i]; i++)
6837 + if (options[i] == ':')
6840 + /* allocate space for underlying pointers to lower dentry */
6841 + UNIONFS_SB(sb)->data =
6842 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6843 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6848 + lower_root_info->lower_paths =
6849 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6850 + if (unlikely(!lower_root_info->lower_paths)) {
6855 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6857 + while ((name = strsep(&options, ":")) != NULL) {
6859 + char *mode = strchr(name, '=');
6863 + if (!*name) { /* bad use of ':' (extra colons) */
6870 + /* strip off '=' if any */
6874 + err = parse_branch_mode(mode, &perms);
6876 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6877 + "branch %d\n", mode, bindex);
6880 + /* ensure that leftmost branch is writeable */
6881 + if (!bindex && !(perms & MAY_WRITE)) {
6882 + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6883 + "read-only (use \"-o ro\" to create a "
6884 + "read-only union)\n");
6889 + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6891 + printk(KERN_ERR "unionfs: error accessing "
6892 + "lower directory '%s' (error %d)\n",
6897 + err = check_branch(&nd);
6899 + printk(KERN_ERR "unionfs: lower directory "
6900 + "'%s' is not a valid branch\n", name);
6901 + path_put(&nd.path);
6905 + lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6906 + lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6908 + set_branchperms(sb, bindex, perms);
6909 + set_branch_count(sb, bindex, 0);
6910 + new_branch_id(sb, bindex);
6912 + if (lower_root_info->bstart < 0)
6913 + lower_root_info->bstart = bindex;
6914 + lower_root_info->bend = bindex;
6918 + if (branches == 0) {
6919 + printk(KERN_ERR "unionfs: no branches specified\n");
6924 + BUG_ON(branches != (lower_root_info->bend + 1));
6927 + * Ensure that no overlaps exist in the branches.
6929 + * This test is required because the Linux kernel has no support
6930 + * currently for ensuring coherency between stackable layers and
6931 + * branches. If we were to allow overlapping branches, it would be
6932 + * possible, for example, to delete a file via one branch, which
6933 + * would not be reflected in another branch. Such incoherency could
6934 + * lead to inconsistencies and even kernel oopses. Rather than
6935 + * implement hacks to work around some of these cache-coherency
6936 + * problems, we prevent branch overlapping, for now. A complete
6937 + * solution will involve proper kernel/VFS support for cache
6938 + * coherency, at which time we could safely remove this
6939 + * branch-overlapping test.
6941 + for (i = 0; i < branches; i++) {
6942 + dent1 = lower_root_info->lower_paths[i].dentry;
6943 + for (j = i + 1; j < branches; j++) {
6944 + dent2 = lower_root_info->lower_paths[j].dentry;
6945 + if (is_branch_overlap(dent1, dent2)) {
6946 + printk(KERN_ERR "unionfs: branches %d and "
6947 + "%d overlap\n", i, j);
6956 + for (i = 0; i < branches; i++)
6957 + path_put(&lower_root_info->lower_paths[i]);
6959 + kfree(lower_root_info->lower_paths);
6960 + kfree(UNIONFS_SB(sb)->data);
6963 + * MUST clear the pointers to prevent potential double free if
6964 + * the caller dies later on
6966 + lower_root_info->lower_paths = NULL;
6967 + UNIONFS_SB(sb)->data = NULL;
6973 + * Parse mount options. See the manual page for usage instructions.
6975 + * Returns the dentry object of the lower-level (lower) directory;
6976 + * We want to mount our stackable file system on top of that lower directory.
6978 +static struct unionfs_dentry_info *unionfs_parse_options(
6979 + struct super_block *sb,
6982 + struct unionfs_dentry_info *lower_root_info;
6986 + int dirsfound = 0;
6988 + /* allocate private data area */
6991 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6992 + if (unlikely(!lower_root_info))
6994 + lower_root_info->bstart = -1;
6995 + lower_root_info->bend = -1;
6996 + lower_root_info->bopaque = -1;
6998 + while ((optname = strsep(&options, ",")) != NULL) {
7001 + if (!optname || !*optname)
7004 + optarg = strchr(optname, '=');
7009 + * All of our options take an argument now. Insert ones that
7010 + * don't, above this check.
7013 + printk(KERN_ERR "unionfs: %s requires an argument\n",
7019 + if (!strcmp("dirs", optname)) {
7020 + if (++dirsfound > 1) {
7022 + "unionfs: multiple dirs specified\n");
7026 + err = parse_dirs_option(sb, lower_root_info, optarg);
7034 + "unionfs: unrecognized option '%s'\n", optname);
7037 + if (dirsfound != 1) {
7038 + printk(KERN_ERR "unionfs: dirs option required\n");
7045 + if (lower_root_info && lower_root_info->lower_paths) {
7046 + for (bindex = lower_root_info->bstart;
7047 + bindex >= 0 && bindex <= lower_root_info->bend;
7049 + path_put(&lower_root_info->lower_paths[bindex]);
7052 + kfree(lower_root_info->lower_paths);
7053 + kfree(lower_root_info);
7055 + kfree(UNIONFS_SB(sb)->data);
7056 + UNIONFS_SB(sb)->data = NULL;
7058 + lower_root_info = ERR_PTR(err);
7060 + return lower_root_info;
7064 + * our custom d_alloc_root work-alike
7066 + * we can't use d_alloc_root if we want to use our own interpose function
7067 + * unchanged, so we simply call our own "fake" d_alloc_root
7069 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7071 + struct dentry *ret = NULL;
7074 + static const struct qstr name = {
7079 + ret = d_alloc(NULL, &name);
7080 + if (likely(ret)) {
7081 + ret->d_op = &unionfs_dops;
7083 + ret->d_parent = ret;
7090 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7091 + * way anyone can have a reference to the superblock at this point in time.
7093 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7097 + struct unionfs_dentry_info *lower_root_info = NULL;
7098 + int bindex, bstart, bend;
7102 + "unionfs: read_super: missing data argument\n");
7107 + /* Allocate superblock private data */
7108 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7109 + if (unlikely(!UNIONFS_SB(sb))) {
7110 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7115 + UNIONFS_SB(sb)->bend = -1;
7116 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7117 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7118 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7120 + lower_root_info = unionfs_parse_options(sb, raw_data);
7121 + if (IS_ERR(lower_root_info)) {
7123 + "unionfs: read_super: error while parsing options "
7124 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7125 + err = PTR_ERR(lower_root_info);
7126 + lower_root_info = NULL;
7129 + if (lower_root_info->bstart == -1) {
7134 + /* set the lower superblock field of upper superblock */
7135 + bstart = lower_root_info->bstart;
7136 + BUG_ON(bstart != 0);
7137 + sbend(sb) = bend = lower_root_info->bend;
7138 + for (bindex = bstart; bindex <= bend; bindex++) {
7139 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7140 + atomic_inc(&d->d_sb->s_active);
7141 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7144 + /* max Bytes is the maximum bytes from highest priority branch */
7145 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7148 + * Our c/m/atime granularity is 1 ns because we may stack on file
7149 + * systems whose granularity is as good. This is important for our
7150 + * time-based cache coherency.
7152 + sb->s_time_gran = 1;
7154 + sb->s_op = &unionfs_sops;
7156 + /* See comment next to the definition of unionfs_d_alloc_root */
7157 + sb->s_root = unionfs_d_alloc_root(sb);
7158 + if (unlikely(!sb->s_root)) {
7163 + /* link the upper and lower dentries */
7164 + sb->s_root->d_fsdata = NULL;
7165 + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7166 + if (unlikely(err))
7169 + /* Set the lower dentries for s_root */
7170 + for (bindex = bstart; bindex <= bend; bindex++) {
7172 + struct vfsmount *m;
7174 + d = lower_root_info->lower_paths[bindex].dentry;
7175 + m = lower_root_info->lower_paths[bindex].mnt;
7177 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7178 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7180 + dbstart(sb->s_root) = bstart;
7181 + dbend(sb->s_root) = bend;
7183 + /* Set the generation number to one, since this is for the mount. */
7184 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7187 + * Call interpose to create the upper level inode. Only
7188 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7190 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7191 + unionfs_unlock_dentry(sb->s_root);
7194 + /* else fall through */
7197 + if (UNIONFS_D(sb->s_root)) {
7198 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7199 + free_dentry_private_data(sb->s_root);
7204 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7205 + for (bindex = lower_root_info->bstart;
7206 + bindex <= lower_root_info->bend; bindex++) {
7208 + d = lower_root_info->lower_paths[bindex].dentry;
7209 + /* drop refs we took earlier */
7210 + atomic_dec(&d->d_sb->s_active);
7211 + path_put(&lower_root_info->lower_paths[bindex]);
7213 + kfree(lower_root_info->lower_paths);
7214 + kfree(lower_root_info);
7215 + lower_root_info = NULL;
7219 + kfree(UNIONFS_SB(sb)->data);
7220 + kfree(UNIONFS_SB(sb));
7221 + sb->s_fs_info = NULL;
7224 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7225 + kfree(lower_root_info->lower_paths);
7226 + kfree(lower_root_info);
7231 +static int unionfs_get_sb(struct file_system_type *fs_type,
7232 + int flags, const char *dev_name,
7233 + void *raw_data, struct vfsmount *mnt)
7236 + err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7238 + UNIONFS_SB(mnt->mnt_sb)->dev_name =
7239 + kstrdup(dev_name, GFP_KERNEL);
7243 +static struct file_system_type unionfs_fs_type = {
7244 + .owner = THIS_MODULE,
7245 + .name = UNIONFS_NAME,
7246 + .get_sb = unionfs_get_sb,
7247 + .kill_sb = generic_shutdown_super,
7248 + .fs_flags = FS_REVAL_DOT,
7251 +static int __init init_unionfs_fs(void)
7255 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7257 + err = unionfs_init_filldir_cache();
7258 + if (unlikely(err))
7260 + err = unionfs_init_inode_cache();
7261 + if (unlikely(err))
7263 + err = unionfs_init_dentry_cache();
7264 + if (unlikely(err))
7266 + err = init_sioq();
7267 + if (unlikely(err))
7269 + err = register_filesystem(&unionfs_fs_type);
7271 + if (unlikely(err)) {
7273 + unionfs_destroy_filldir_cache();
7274 + unionfs_destroy_inode_cache();
7275 + unionfs_destroy_dentry_cache();
7280 +static void __exit exit_unionfs_fs(void)
7283 + unionfs_destroy_filldir_cache();
7284 + unionfs_destroy_inode_cache();
7285 + unionfs_destroy_dentry_cache();
7286 + unregister_filesystem(&unionfs_fs_type);
7287 + pr_info("Completed unionfs module unload\n");
7290 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7291 + " (http://www.fsl.cs.sunysb.edu)");
7292 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7293 + " (http://unionfs.filesystems.org)");
7294 +MODULE_LICENSE("GPL");
7296 +module_init(init_unionfs_fs);
7297 +module_exit(exit_unionfs_fs);
7298 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7299 new file mode 100644
7300 index 0000000..18b05d5
7302 +++ b/fs/unionfs/mmap.c
7305 + * Copyright (c) 2003-2009 Erez Zadok
7306 + * Copyright (c) 2003-2006 Charles P. Wright
7307 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7308 + * Copyright (c) 2005-2006 Junjiro Okajima
7309 + * Copyright (c) 2006 Shaya Potter
7310 + * Copyright (c) 2005 Arun M. Krishnakumar
7311 + * Copyright (c) 2004-2006 David P. Quigley
7312 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7313 + * Copyright (c) 2003 Puja Gupta
7314 + * Copyright (c) 2003 Harikesavan Krishnan
7315 + * Copyright (c) 2003-2009 Stony Brook University
7316 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7318 + * This program is free software; you can redistribute it and/or modify
7319 + * it under the terms of the GNU General Public License version 2 as
7320 + * published by the Free Software Foundation.
7327 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7328 + * use in unionfs_mmap) checks for the existence of
7329 + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7330 + * be fixed to allow a file system to define vm_ops->fault without any
7331 + * address_space_ops whatsoever.
7333 + * Otherwise, we don't want to use our readpage method at all.
7335 +static int unionfs_readpage(struct file *file, struct page *page)
7341 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7344 + struct file *file, *lower_file;
7345 + struct vm_operations_struct *lower_vm_ops;
7346 + struct vm_area_struct lower_vma;
7349 + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7350 + file = lower_vma.vm_file;
7351 + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7352 + BUG_ON(!lower_vm_ops);
7354 + lower_file = unionfs_lower_file(file);
7355 + BUG_ON(!lower_file);
7357 + * XXX: vm_ops->fault may be called in parallel. Because we have to
7358 + * resort to temporarily changing the vma->vm_file to point to the
7359 + * lower file, a concurrent invocation of unionfs_fault could see a
7360 + * different value. In this workaround, we keep a different copy of
7361 + * the vma structure in our stack, so we never expose a different
7362 + * value of the vma->vm_file called to us, even temporarily. A
7363 + * better fix would be to change the calling semantics of ->fault to
7364 + * take an explicit file pointer.
7366 + lower_vma.vm_file = lower_file;
7367 + err = lower_vm_ops->fault(&lower_vma, vmf);
7372 + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7373 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7374 + * the a_ops vector to be non-NULL.
7376 +struct address_space_operations unionfs_aops = {
7377 + /* empty on purpose */
7381 + * XXX: we need a second, dummy address_space_ops vector, to be used
7382 + * temporarily during unionfs_mmap, because the latter calls
7383 + * generic_file_mmap, which checks if ->readpage exists, else returns
7386 +struct address_space_operations unionfs_dummy_aops = {
7387 + .readpage = unionfs_readpage,
7390 +struct vm_operations_struct unionfs_vm_ops = {
7391 + .fault = unionfs_fault,
7393 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7394 new file mode 100644
7395 index 0000000..485464b
7397 +++ b/fs/unionfs/rdstate.c
7400 + * Copyright (c) 2003-2009 Erez Zadok
7401 + * Copyright (c) 2003-2006 Charles P. Wright
7402 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7403 + * Copyright (c) 2005-2006 Junjiro Okajima
7404 + * Copyright (c) 2005 Arun M. Krishnakumar
7405 + * Copyright (c) 2004-2006 David P. Quigley
7406 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7407 + * Copyright (c) 2003 Puja Gupta
7408 + * Copyright (c) 2003 Harikesavan Krishnan
7409 + * Copyright (c) 2003-2009 Stony Brook University
7410 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7412 + * This program is free software; you can redistribute it and/or modify
7413 + * it under the terms of the GNU General Public License version 2 as
7414 + * published by the Free Software Foundation.
7419 +/* This file contains the routines for maintaining readdir state. */
7422 + * There are two structures here, rdstate which is a hash table
7423 + * of the second structure which is a filldir_node.
7427 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7428 + * of them and they shouldn't waste memory. If the node has a small name
7429 + * (as defined by the dentry structure), then we use an inline name to
7430 + * preserve kmalloc space.
7432 +static struct kmem_cache *unionfs_filldir_cachep;
7434 +int unionfs_init_filldir_cache(void)
7436 + unionfs_filldir_cachep =
7437 + kmem_cache_create("unionfs_filldir",
7438 + sizeof(struct filldir_node), 0,
7439 + SLAB_RECLAIM_ACCOUNT, NULL);
7441 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7444 +void unionfs_destroy_filldir_cache(void)
7446 + if (unionfs_filldir_cachep)
7447 + kmem_cache_destroy(unionfs_filldir_cachep);
7451 + * This is a tuning parameter that tells us roughly how big to make the
7452 + * hash table in directory entries per page. This isn't perfect, but
7453 + * at least we get a hash table size that shouldn't be too overloaded.
7454 + * The following averages are based on my home directory.
7455 + * 14.44693 Overall
7456 + * 12.29 Single Page Directories
7457 + * 117.93 Multi-page directories
7459 +#define DENTPAGE 4096
7460 +#define DENTPERONEPAGE 12
7461 +#define DENTPERPAGE 118
7462 +#define MINHASHSIZE 1
7463 +static int guesstimate_hash_size(struct inode *inode)
7465 + struct inode *lower_inode;
7467 + int hashsize = MINHASHSIZE;
7469 + if (UNIONFS_I(inode)->hashsize > 0)
7470 + return UNIONFS_I(inode)->hashsize;
7472 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7473 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7477 + if (i_size_read(lower_inode) == DENTPAGE)
7478 + hashsize += DENTPERONEPAGE;
7480 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7487 +int init_rdstate(struct file *file)
7489 + BUG_ON(sizeof(loff_t) !=
7490 + (sizeof(unsigned int) + sizeof(unsigned int)));
7491 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7493 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7496 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7499 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7501 + struct unionfs_dir_state *rdstate = NULL;
7502 + struct list_head *pos;
7504 + spin_lock(&UNIONFS_I(inode)->rdlock);
7505 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7506 + struct unionfs_dir_state *r =
7507 + list_entry(pos, struct unionfs_dir_state, cache);
7508 + if (fpos == rdstate2offset(r)) {
7509 + UNIONFS_I(inode)->rdcount--;
7510 + list_del(&r->cache);
7515 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7519 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7523 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7524 + struct unionfs_dir_state *rdstate;
7526 + hashsize = guesstimate_hash_size(inode);
7527 + mallocsize += hashsize * sizeof(struct list_head);
7528 + mallocsize = __roundup_pow_of_two(mallocsize);
7530 + /* This should give us about 500 entries anyway. */
7531 + if (mallocsize > PAGE_SIZE)
7532 + mallocsize = PAGE_SIZE;
7534 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7535 + sizeof(struct list_head);
7537 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7538 + if (unlikely(!rdstate))
7541 + spin_lock(&UNIONFS_I(inode)->rdlock);
7542 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7543 + UNIONFS_I(inode)->cookie = 1;
7545 + UNIONFS_I(inode)->cookie++;
7547 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7548 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7549 + rdstate->offset = 1;
7550 + rdstate->access = jiffies;
7551 + rdstate->bindex = bindex;
7552 + rdstate->dirpos = 0;
7553 + rdstate->hashentries = 0;
7554 + rdstate->size = hashsize;
7555 + for (i = 0; i < rdstate->size; i++)
7556 + INIT_LIST_HEAD(&rdstate->list[i]);
7561 +static void free_filldir_node(struct filldir_node *node)
7563 + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7564 + kfree(node->name);
7565 + kmem_cache_free(unionfs_filldir_cachep, node);
7568 +void free_rdstate(struct unionfs_dir_state *state)
7570 + struct filldir_node *tmp;
7573 + for (i = 0; i < state->size; i++) {
7574 + struct list_head *head = &(state->list[i]);
7575 + struct list_head *pos, *n;
7577 + /* traverse the list and deallocate space */
7578 + list_for_each_safe(pos, n, head) {
7579 + tmp = list_entry(pos, struct filldir_node, file_list);
7580 + list_del(&tmp->file_list);
7581 + free_filldir_node(tmp);
7588 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7589 + const char *name, int namelen,
7593 + unsigned int hash;
7594 + struct list_head *head;
7595 + struct list_head *pos;
7596 + struct filldir_node *cursor = NULL;
7599 + BUG_ON(namelen <= 0);
7601 + hash = full_name_hash(name, namelen);
7602 + index = hash % rdstate->size;
7604 + head = &(rdstate->list[index]);
7605 + list_for_each(pos, head) {
7606 + cursor = list_entry(pos, struct filldir_node, file_list);
7608 + if (cursor->namelen == namelen && cursor->hash == hash &&
7609 + !strncmp(cursor->name, name, namelen)) {
7611 + * a duplicate exists, and hence no need to create
7612 + * entry to the list
7617 + * if a duplicate is found in this branch, and is
7618 + * not due to the caller looking for an entry to
7619 + * whiteout, then the file system may be corrupted.
7621 + if (unlikely(!is_whiteout &&
7622 + cursor->bindex == rdstate->bindex))
7623 + printk(KERN_ERR "unionfs: filldir: possible "
7624 + "I/O error: a file is duplicated "
7625 + "in the same branch %d: %s\n",
7626 + rdstate->bindex, cursor->name);
7637 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7638 + int namelen, int bindex, int whiteout)
7640 + struct filldir_node *new;
7641 + unsigned int hash;
7644 + struct list_head *head;
7646 + BUG_ON(namelen <= 0);
7648 + hash = full_name_hash(name, namelen);
7649 + index = hash % rdstate->size;
7650 + head = &(rdstate->list[index]);
7652 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7653 + if (unlikely(!new)) {
7658 + INIT_LIST_HEAD(&new->file_list);
7659 + new->namelen = namelen;
7661 + new->bindex = bindex;
7662 + new->whiteout = whiteout;
7664 + if (namelen < DNAME_INLINE_LEN_MIN) {
7665 + new->name = new->iname;
7667 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7668 + if (unlikely(!new->name)) {
7669 + kmem_cache_free(unionfs_filldir_cachep, new);
7675 + memcpy(new->name, name, namelen);
7676 + new->name[namelen] = '\0';
7678 + rdstate->hashentries++;
7680 + list_add(&(new->file_list), head);
7684 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7685 new file mode 100644
7686 index 0000000..ed13260
7688 +++ b/fs/unionfs/rename.c
7691 + * Copyright (c) 2003-2009 Erez Zadok
7692 + * Copyright (c) 2003-2006 Charles P. Wright
7693 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7694 + * Copyright (c) 2005-2006 Junjiro Okajima
7695 + * Copyright (c) 2005 Arun M. Krishnakumar
7696 + * Copyright (c) 2004-2006 David P. Quigley
7697 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7698 + * Copyright (c) 2003 Puja Gupta
7699 + * Copyright (c) 2003 Harikesavan Krishnan
7700 + * Copyright (c) 2003-2009 Stony Brook University
7701 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7703 + * This program is free software; you can redistribute it and/or modify
7704 + * it under the terms of the GNU General Public License version 2 as
7705 + * published by the Free Software Foundation.
7711 + * This is a helper function for rename, used when rename ends up with hosed
7712 + * over dentries and we need to revert.
7714 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7715 + struct dentry *parent, int bindex)
7717 + struct dentry *lower_dentry;
7718 + struct dentry *lower_parent;
7721 + verify_locked(dentry);
7723 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7725 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7727 + lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
7728 + dentry->d_name.len);
7729 + if (IS_ERR(lower_dentry)) {
7730 + err = PTR_ERR(lower_dentry);
7734 + dput(unionfs_lower_dentry_idx(dentry, bindex));
7735 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7736 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7738 + if (!lower_dentry->d_inode) {
7739 + dput(lower_dentry);
7740 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7742 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7743 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7744 + igrab(lower_dentry->d_inode));
7751 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7752 + struct dentry *old_parent,
7753 + struct inode *new_dir, struct dentry *new_dentry,
7754 + struct dentry *new_parent,
7758 + struct dentry *lower_old_dentry;
7759 + struct dentry *lower_new_dentry;
7760 + struct dentry *lower_old_dir_dentry;
7761 + struct dentry *lower_new_dir_dentry;
7762 + struct dentry *trap;
7764 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7765 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7767 + if (!lower_new_dentry) {
7768 + lower_new_dentry =
7769 + create_parents(new_parent->d_inode,
7770 + new_dentry, new_dentry->d_name.name,
7772 + if (IS_ERR(lower_new_dentry)) {
7773 + err = PTR_ERR(lower_new_dentry);
7774 + if (IS_COPYUP_ERR(err))
7776 + printk(KERN_ERR "unionfs: error creating directory "
7777 + "tree for rename, bindex=%d err=%d\n",
7783 + /* check for and remove whiteout, if any */
7784 + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7785 + if (err > 0) /* ignore if whiteout found and successfully removed */
7790 + /* check of old_dentry branch is writable */
7791 + err = is_robranch_super(old_dentry->d_sb, bindex);
7795 + dget(lower_old_dentry);
7796 + dget(lower_new_dentry);
7797 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7798 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7800 + /* see Documentation/filesystems/unionfs/issues.txt */
7802 + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7803 + /* source should not be ancenstor of target */
7804 + if (trap == lower_old_dentry) {
7806 + goto out_err_unlock;
7808 + /* target should not be ancenstor of source */
7809 + if (trap == lower_new_dentry) {
7811 + goto out_err_unlock;
7813 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7814 + lower_new_dir_dentry->d_inode, lower_new_dentry);
7817 + /* update parent dir times */
7818 + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7819 + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7821 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7824 + dput(lower_old_dir_dentry);
7825 + dput(lower_new_dir_dentry);
7826 + dput(lower_old_dentry);
7827 + dput(lower_new_dentry);
7831 + /* Fixup the new_dentry. */
7832 + if (bindex < dbstart(new_dentry))
7833 + dbstart(new_dentry) = bindex;
7834 + else if (bindex > dbend(new_dentry))
7835 + dbend(new_dentry) = bindex;
7842 + * Main rename code. This is sufficiently complex, that it's documented in
7843 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7844 + * __unionfs_rename() above to perform some of the work.
7846 +static int do_unionfs_rename(struct inode *old_dir,
7847 + struct dentry *old_dentry,
7848 + struct dentry *old_parent,
7849 + struct inode *new_dir,
7850 + struct dentry *new_dentry,
7851 + struct dentry *new_parent)
7855 + int old_bstart, old_bend;
7856 + int new_bstart, new_bend;
7857 + int do_copyup = -1;
7858 + int local_err = 0;
7862 + old_bstart = dbstart(old_dentry);
7863 + old_bend = dbend(old_dentry);
7865 + new_bstart = dbstart(new_dentry);
7866 + new_bend = dbend(new_dentry);
7868 + /* Rename source to destination. */
7869 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7870 + new_dir, new_dentry, new_parent,
7873 + if (!IS_COPYUP_ERR(err))
7875 + do_copyup = old_bstart - 1;
7881 + * Unlink all instances of destination that exist to the left of
7882 + * bstart of source. On error, revert back, goto out.
7884 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7885 + struct dentry *unlink_dentry;
7886 + struct dentry *unlink_dir_dentry;
7888 + BUG_ON(bindex < 0);
7889 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7890 + if (!unlink_dentry)
7893 + unlink_dir_dentry = lock_parent(unlink_dentry);
7894 + err = is_robranch_super(old_dir->i_sb, bindex);
7896 + err = vfs_unlink(unlink_dir_dentry->d_inode,
7899 + fsstack_copy_attr_times(new_parent->d_inode,
7900 + unlink_dir_dentry->d_inode);
7901 + /* propagate number of hard-links */
7902 + new_parent->d_inode->i_nlink =
7903 + unionfs_get_nlinks(new_parent->d_inode);
7905 + unlock_dir(unlink_dir_dentry);
7907 + if (bindex != new_bstart) {
7908 + dput(unlink_dentry);
7909 + unionfs_set_lower_dentry_idx(new_dentry,
7912 + } else if (IS_COPYUP_ERR(err)) {
7913 + do_copyup = bindex - 1;
7914 + } else if (revert) {
7919 + if (do_copyup != -1) {
7920 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7922 + * copyup the file into some left directory, so that
7923 + * you can rename it
7925 + err = copyup_dentry(old_parent->d_inode,
7926 + old_dentry, old_bstart, bindex,
7927 + old_dentry->d_name.name,
7928 + old_dentry->d_name.len, NULL,
7929 + i_size_read(old_dentry->d_inode));
7930 + /* if copyup failed, try next branch to the left */
7934 + * create whiteout before calling __unionfs_rename
7935 + * because the latter will change the old_dentry's
7936 + * lower name and parent dir, resulting in the
7937 + * whiteout getting created in the wrong dir.
7939 + err = create_whiteout(old_dentry, bindex);
7941 + printk(KERN_ERR "unionfs: can't create a "
7942 + "whiteout for %s in rename (err=%d)\n",
7943 + old_dentry->d_name.name, err);
7946 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7947 + new_dir, new_dentry, new_parent,
7953 + /* make it opaque */
7954 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7955 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7961 + * Create whiteout for source, only if:
7962 + * (1) There is more than one underlying instance of source.
7963 + * (We did a copy_up is taken care of above).
7965 + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7966 + err = create_whiteout(old_dentry, old_bstart);
7968 + /* can't fix anything now, so we exit with -EIO */
7969 + printk(KERN_ERR "unionfs: can't create a whiteout for "
7970 + "%s in rename!\n", old_dentry->d_name.name);
7979 + /* Do revert here. */
7980 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7983 + printk(KERN_ERR "unionfs: revert failed in rename: "
7984 + "the new refresh failed\n");
7988 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7991 + printk(KERN_ERR "unionfs: revert failed in rename: "
7992 + "the old refresh failed\n");
7997 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7998 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7999 + printk(KERN_ERR "unionfs: revert failed in rename: "
8000 + "the object disappeared from under us!\n");
8005 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
8006 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
8007 + printk(KERN_ERR "unionfs: revert failed in rename: "
8008 + "the object was created underneath us!\n");
8013 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
8014 + old_dir, old_dentry, old_parent,
8017 + /* If we can't fix it, then we cop-out with -EIO. */
8019 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
8023 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
8027 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
8039 + * We can't copyup a directory, because it may involve huge numbers of
8040 + * children, etc. Doing that in the kernel would be bad, so instead we
8041 + * return EXDEV to the user-space utility that caused this, and let the
8042 + * user-space recurse and ask us to copy up each file separately.
8044 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
8048 + err = check_empty(dentry, parent, NULL);
8049 + if (err == -ENOTEMPTY) {
8050 + if (is_robranch(dentry))
8056 + bstart = dbstart(dentry);
8057 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8060 + dbstart(dentry) = bstart + 1;
8061 + err = check_empty(dentry, parent, NULL);
8062 + dbstart(dentry) = bstart;
8063 + if (err == -ENOTEMPTY)
8069 + * The locking rules in unionfs_rename are complex. We could use a simpler
8070 + * superblock-level name-space lock for renames and copy-ups.
8072 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8073 + struct inode *new_dir, struct dentry *new_dentry)
8076 + struct dentry *wh_dentry;
8077 + struct dentry *old_parent, *new_parent;
8080 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
8081 + old_parent = dget_parent(old_dentry);
8082 + new_parent = dget_parent(new_dentry);
8083 + /* un/lock parent dentries only if they differ from old/new_dentry */
8084 + if (old_parent != old_dentry &&
8085 + old_parent != new_dentry)
8086 + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
8087 + if (new_parent != old_dentry &&
8088 + new_parent != new_dentry &&
8089 + new_parent != old_parent)
8090 + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
8091 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8093 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
8098 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
8099 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
8106 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8107 + err = unionfs_partial_lookup(old_dentry, old_parent);
8109 + err = may_rename_dir(old_dentry, old_parent);
8114 + err = unionfs_partial_lookup(new_dentry, new_parent);
8119 + * if new_dentry is already lower because of whiteout,
8120 + * simply override it even if the whited-out dir is not empty.
8122 + wh_dentry = find_first_whiteout(new_dentry);
8123 + if (!IS_ERR(wh_dentry)) {
8125 + } else if (new_dentry->d_inode) {
8126 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8127 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8128 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8129 + -ENOTDIR : -EISDIR;
8133 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8134 + struct unionfs_dir_state *namelist = NULL;
8135 + /* check if this unionfs directory is empty or not */
8136 + err = check_empty(new_dentry, new_parent, &namelist);
8140 + if (!is_robranch(new_dentry))
8141 + err = delete_whiteouts(new_dentry,
8142 + dbstart(new_dentry),
8145 + free_rdstate(namelist);
8152 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8153 + new_dir, new_dentry, new_parent);
8158 + * force re-lookup since the dir on ro branch is not renamed, and
8159 + * lower dentries still indicate the un-renamed ones.
8161 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8162 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8164 + unionfs_postcopyup_release(old_dentry);
8165 + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8166 + unionfs_postcopyup_release(new_dentry);
8167 + unionfs_postcopyup_setmnt(new_dentry);
8168 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8170 + * If we get here, it means that no copyup was
8171 + * needed, and that a file by the old name already
8172 + * existing on the destination branch; that file got
8173 + * renamed earlier in this function, so all we need
8174 + * to do here is set the lower inode.
8176 + struct inode *inode;
8177 + inode = unionfs_lower_inode(old_dentry->d_inode);
8179 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8180 + dbstart(new_dentry),
8184 + /* if all of this renaming succeeded, update our times */
8185 + unionfs_copy_attr_times(old_dentry->d_inode);
8186 + unionfs_copy_attr_times(new_dentry->d_inode);
8187 + unionfs_check_inode(old_dir);
8188 + unionfs_check_inode(new_dir);
8189 + unionfs_check_dentry(old_dentry);
8190 + unionfs_check_dentry(new_dentry);
8193 + if (err) /* clear the new_dentry stuff created */
8194 + d_drop(new_dentry);
8196 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8197 + if (new_parent != old_dentry &&
8198 + new_parent != new_dentry &&
8199 + new_parent != old_parent)
8200 + unionfs_unlock_dentry(new_parent);
8201 + if (old_parent != old_dentry &&
8202 + old_parent != new_dentry)
8203 + unionfs_unlock_dentry(old_parent);
8206 + unionfs_read_unlock(old_dentry->d_sb);
8210 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8211 new file mode 100644
8212 index 0000000..5dd487a
8214 +++ b/fs/unionfs/sioq.c
8217 + * Copyright (c) 2006-2009 Erez Zadok
8218 + * Copyright (c) 2006 Charles P. Wright
8219 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8220 + * Copyright (c) 2006 Junjiro Okajima
8221 + * Copyright (c) 2006 David P. Quigley
8222 + * Copyright (c) 2006-2009 Stony Brook University
8223 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8225 + * This program is free software; you can redistribute it and/or modify
8226 + * it under the terms of the GNU General Public License version 2 as
8227 + * published by the Free Software Foundation.
8233 + * Super-user IO work Queue - sometimes we need to perform actions which
8234 + * would fail due to the unix permissions on the parent directory (e.g.,
8235 + * rmdir a directory which appears empty, but in reality contains
8239 +static struct workqueue_struct *superio_workqueue;
8241 +int __init init_sioq(void)
8245 + superio_workqueue = create_workqueue("unionfs_siod");
8246 + if (!IS_ERR(superio_workqueue))
8249 + err = PTR_ERR(superio_workqueue);
8250 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8251 + superio_workqueue = NULL;
8255 +void stop_sioq(void)
8257 + if (superio_workqueue)
8258 + destroy_workqueue(superio_workqueue);
8261 +void run_sioq(work_func_t func, struct sioq_args *args)
8263 + INIT_WORK(&args->work, func);
8265 + init_completion(&args->comp);
8266 + while (!queue_work(superio_workqueue, &args->work)) {
8267 + /* TODO: do accounting if needed */
8270 + wait_for_completion(&args->comp);
8273 +void __unionfs_create(struct work_struct *work)
8275 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8276 + struct create_args *c = &args->create;
8278 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8279 + complete(&args->comp);
8282 +void __unionfs_mkdir(struct work_struct *work)
8284 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8285 + struct mkdir_args *m = &args->mkdir;
8287 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8288 + complete(&args->comp);
8291 +void __unionfs_mknod(struct work_struct *work)
8293 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8294 + struct mknod_args *m = &args->mknod;
8296 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8297 + complete(&args->comp);
8300 +void __unionfs_symlink(struct work_struct *work)
8302 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8303 + struct symlink_args *s = &args->symlink;
8305 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8306 + complete(&args->comp);
8309 +void __unionfs_unlink(struct work_struct *work)
8311 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8312 + struct unlink_args *u = &args->unlink;
8314 + args->err = vfs_unlink(u->parent, u->dentry);
8315 + complete(&args->comp);
8317 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8318 new file mode 100644
8319 index 0000000..3d7869a
8321 +++ b/fs/unionfs/sioq.h
8324 + * Copyright (c) 2006-2009 Erez Zadok
8325 + * Copyright (c) 2006 Charles P. Wright
8326 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8327 + * Copyright (c) 2006 Junjiro Okajima
8328 + * Copyright (c) 2006 David P. Quigley
8329 + * Copyright (c) 2006-2009 Stony Brook University
8330 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8332 + * This program is free software; you can redistribute it and/or modify
8333 + * it under the terms of the GNU General Public License version 2 as
8334 + * published by the Free Software Foundation.
8340 +struct deletewh_args {
8341 + struct unionfs_dir_state *namelist;
8342 + struct dentry *dentry;
8346 +struct is_opaque_args {
8347 + struct dentry *dentry;
8350 +struct create_args {
8351 + struct inode *parent;
8352 + struct dentry *dentry;
8354 + struct nameidata *nd;
8357 +struct mkdir_args {
8358 + struct inode *parent;
8359 + struct dentry *dentry;
8363 +struct mknod_args {
8364 + struct inode *parent;
8365 + struct dentry *dentry;
8370 +struct symlink_args {
8371 + struct inode *parent;
8372 + struct dentry *dentry;
8376 +struct unlink_args {
8377 + struct inode *parent;
8378 + struct dentry *dentry;
8383 + struct completion comp;
8384 + struct work_struct work;
8389 + struct deletewh_args deletewh;
8390 + struct is_opaque_args is_opaque;
8391 + struct create_args create;
8392 + struct mkdir_args mkdir;
8393 + struct mknod_args mknod;
8394 + struct symlink_args symlink;
8395 + struct unlink_args unlink;
8399 +/* Extern definitions for SIOQ functions */
8400 +extern int __init init_sioq(void);
8401 +extern void stop_sioq(void);
8402 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8404 +/* Extern definitions for our privilege escalation helpers */
8405 +extern void __unionfs_create(struct work_struct *work);
8406 +extern void __unionfs_mkdir(struct work_struct *work);
8407 +extern void __unionfs_mknod(struct work_struct *work);
8408 +extern void __unionfs_symlink(struct work_struct *work);
8409 +extern void __unionfs_unlink(struct work_struct *work);
8410 +extern void __delete_whiteouts(struct work_struct *work);
8411 +extern void __is_opaque_dir(struct work_struct *work);
8413 +#endif /* not _SIOQ_H */
8414 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8415 new file mode 100644
8416 index 0000000..018b4fd
8418 +++ b/fs/unionfs/subr.c
8421 + * Copyright (c) 2003-2009 Erez Zadok
8422 + * Copyright (c) 2003-2006 Charles P. Wright
8423 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8424 + * Copyright (c) 2005-2006 Junjiro Okajima
8425 + * Copyright (c) 2005 Arun M. Krishnakumar
8426 + * Copyright (c) 2004-2006 David P. Quigley
8427 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8428 + * Copyright (c) 2003 Puja Gupta
8429 + * Copyright (c) 2003 Harikesavan Krishnan
8430 + * Copyright (c) 2003-2009 Stony Brook University
8431 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8433 + * This program is free software; you can redistribute it and/or modify
8434 + * it under the terms of the GNU General Public License version 2 as
8435 + * published by the Free Software Foundation.
8441 + * returns the right n_link value based on the inode type
8443 +int unionfs_get_nlinks(const struct inode *inode)
8445 + /* don't bother to do all the work since we're unlinked */
8446 + if (inode->i_nlink == 0)
8449 + if (!S_ISDIR(inode->i_mode))
8450 + return unionfs_lower_inode(inode)->i_nlink;
8453 + * For directories, we return 1. The only place that could cares
8454 + * about links is readdir, and there's d_type there so even that
8460 +/* copy a/m/ctime from the lower branch with the newest times */
8461 +void unionfs_copy_attr_times(struct inode *upper)
8464 + struct inode *lower;
8468 + if (ibstart(upper) < 0) {
8469 +#ifdef CONFIG_UNION_FS_DEBUG
8470 + WARN_ON(ibstart(upper) < 0);
8471 +#endif /* CONFIG_UNION_FS_DEBUG */
8474 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8475 + lower = unionfs_lower_inode_idx(upper, bindex);
8477 + continue; /* not all lower dir objects may exist */
8478 + if (unlikely(timespec_compare(&upper->i_mtime,
8479 + &lower->i_mtime) < 0))
8480 + upper->i_mtime = lower->i_mtime;
8481 + if (unlikely(timespec_compare(&upper->i_ctime,
8482 + &lower->i_ctime) < 0))
8483 + upper->i_ctime = lower->i_ctime;
8484 + if (unlikely(timespec_compare(&upper->i_atime,
8485 + &lower->i_atime) < 0))
8486 + upper->i_atime = lower->i_atime;
8491 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8492 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8493 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8494 + * important if the lower inode is a directory type)
8496 +void unionfs_copy_attr_all(struct inode *dest,
8497 + const struct inode *src)
8499 + dest->i_mode = src->i_mode;
8500 + dest->i_uid = src->i_uid;
8501 + dest->i_gid = src->i_gid;
8502 + dest->i_rdev = src->i_rdev;
8504 + unionfs_copy_attr_times(dest);
8506 + dest->i_blkbits = src->i_blkbits;
8507 + dest->i_flags = src->i_flags;
8510 + * Update the nlinks AFTER updating the above fields, because the
8511 + * get_links callback may depend on them.
8513 + dest->i_nlink = unionfs_get_nlinks(dest);
8515 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8516 new file mode 100644
8517 index 0000000..ded7b84
8519 +++ b/fs/unionfs/super.c
8522 + * Copyright (c) 2003-2009 Erez Zadok
8523 + * Copyright (c) 2003-2006 Charles P. Wright
8524 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8525 + * Copyright (c) 2005-2006 Junjiro Okajima
8526 + * Copyright (c) 2005 Arun M. Krishnakumar
8527 + * Copyright (c) 2004-2006 David P. Quigley
8528 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8529 + * Copyright (c) 2003 Puja Gupta
8530 + * Copyright (c) 2003 Harikesavan Krishnan
8531 + * Copyright (c) 2003-2009 Stony Brook University
8532 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8534 + * This program is free software; you can redistribute it and/or modify
8535 + * it under the terms of the GNU General Public License version 2 as
8536 + * published by the Free Software Foundation.
8542 + * The inode cache is used with alloc_inode for both our inode info and the
8545 +static struct kmem_cache *unionfs_inode_cachep;
8547 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8550 + struct unionfs_inode_info *info;
8551 + struct inode *inode;
8553 + inode = iget_locked(sb, ino);
8555 + return ERR_PTR(-ENOMEM);
8556 + if (!(inode->i_state & I_NEW))
8559 + info = UNIONFS_I(inode);
8560 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8561 + info->bstart = -1;
8563 + atomic_set(&info->generation,
8564 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8565 + spin_lock_init(&info->rdlock);
8566 + info->rdcount = 1;
8567 + info->hashsize = -1;
8568 + INIT_LIST_HEAD(&info->readdircache);
8570 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8571 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8572 + if (unlikely(!info->lower_inodes)) {
8573 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8574 + "lower-pointer array!\n");
8575 + iget_failed(inode);
8576 + return ERR_PTR(-ENOMEM);
8579 + inode->i_version++;
8580 + inode->i_op = &unionfs_main_iops;
8581 + inode->i_fop = &unionfs_main_fops;
8583 + inode->i_mapping->a_ops = &unionfs_aops;
8586 + * reset times so unionfs_copy_attr_all can keep out time invariants
8587 + * right (upper inode time being the max of all lower ones).
8589 + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8590 + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8591 + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8592 + unlock_new_inode(inode);
8597 + * we now define delete_inode, because there are two VFS paths that may
8598 + * destroy an inode: one of them calls clear inode before doing everything
8599 + * else that's needed, and the other is fine. This way we truncate the inode
8600 + * size (and its pages) and then clear our own inode, which will do an iput
8601 + * on our and the lower inode.
8603 + * No need to lock sb info's rwsem.
8605 +static void unionfs_delete_inode(struct inode *inode)
8607 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8608 + spin_lock(&inode->i_lock);
8610 + i_size_write(inode, 0); /* every f/s seems to do that */
8611 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8612 + spin_unlock(&inode->i_lock);
8615 + if (inode->i_data.nrpages)
8616 + truncate_inode_pages(&inode->i_data, 0);
8618 + clear_inode(inode);
8622 + * final actions when unmounting a file system
8624 + * No need to lock rwsem.
8626 +static void unionfs_put_super(struct super_block *sb)
8628 + int bindex, bstart, bend;
8629 + struct unionfs_sb_info *spd;
8632 + spd = UNIONFS_SB(sb);
8636 + bstart = sbstart(sb);
8639 + /* Make sure we have no leaks of branchget/branchput. */
8640 + for (bindex = bstart; bindex <= bend; bindex++)
8641 + if (unlikely(branch_count(sb, bindex) != 0)) {
8643 + "unionfs: branch %d has %d references left!\n",
8644 + bindex, branch_count(sb, bindex));
8647 + WARN_ON(leaks != 0);
8649 + /* decrement lower super references */
8650 + for (bindex = bstart; bindex <= bend; bindex++) {
8651 + struct super_block *s;
8652 + s = unionfs_lower_super_idx(sb, bindex);
8653 + unionfs_set_lower_super_idx(sb, bindex, NULL);
8654 + atomic_dec(&s->s_active);
8657 + kfree(spd->dev_name);
8660 + sb->s_fs_info = NULL;
8664 + * Since people use this to answer the "How big of a file can I write?"
8665 + * question, we report the size of the highest priority branch as the size of
8668 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8671 + struct super_block *sb;
8672 + struct dentry *lower_dentry;
8673 + struct dentry *parent;
8676 + sb = dentry->d_sb;
8678 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8679 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8680 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8682 + valid = __unionfs_d_revalidate(dentry, parent, false);
8683 + if (unlikely(!valid)) {
8687 + unionfs_check_dentry(dentry);
8689 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8690 + err = vfs_statfs(lower_dentry, buf);
8692 + /* set return buf to our f/s to avoid confusing user-level utils */
8693 + buf->f_type = UNIONFS_SUPER_MAGIC;
8695 + * Our maximum file name can is shorter by a few bytes because every
8696 + * file name could potentially be whited-out.
8698 + * XXX: this restriction goes away with ODF.
8700 + unionfs_set_max_namelen(&buf->f_namelen);
8703 + * reset two fields to avoid confusing user-land.
8704 + * XXX: is this still necessary?
8706 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8707 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8710 + unionfs_check_dentry(dentry);
8711 + unionfs_unlock_dentry(dentry);
8712 + unionfs_unlock_parent(dentry, parent);
8713 + unionfs_read_unlock(sb);
8717 +/* handle mode changing during remount */
8718 +static noinline_for_stack int do_remount_mode_option(
8721 + struct unionfs_data *new_data,
8722 + struct path *new_lower_paths)
8724 + int err = -EINVAL;
8726 + char *modename = strchr(optarg, '=');
8727 + struct nameidata nd;
8729 + /* by now, optarg contains the branch name */
8732 + "unionfs: no branch specified for mode change\n");
8736 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8740 + *modename++ = '\0';
8741 + err = parse_branch_mode(modename, &perms);
8743 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8744 + modename, optarg);
8749 + * Find matching branch index. For now, this assumes that nothing
8750 + * has been mounted on top of this Unionfs stack. Once we have /odf
8751 + * and cache-coherency resolved, we'll address the branch-path
8754 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8756 + printk(KERN_ERR "unionfs: error accessing "
8757 + "lower directory \"%s\" (error %d)\n",
8761 + for (idx = 0; idx < cur_branches; idx++)
8762 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8763 + nd.path.dentry == new_lower_paths[idx].dentry)
8765 + path_put(&nd.path); /* no longer needed */
8766 + if (idx == cur_branches) {
8767 + err = -ENOENT; /* err may have been reset above */
8768 + printk(KERN_ERR "unionfs: branch \"%s\" "
8769 + "not found\n", optarg);
8772 + /* check/change mode for existing branch */
8773 + /* we don't warn if perms==branchperms */
8774 + new_data[idx].branchperms = perms;
8780 +/* handle branch deletion during remount */
8781 +static noinline_for_stack int do_remount_del_option(
8782 + char *optarg, int cur_branches,
8783 + struct unionfs_data *new_data,
8784 + struct path *new_lower_paths)
8786 + int err = -EINVAL;
8788 + struct nameidata nd;
8790 + /* optarg contains the branch name to delete */
8793 + * Find matching branch index. For now, this assumes that nothing
8794 + * has been mounted on top of this Unionfs stack. Once we have /odf
8795 + * and cache-coherency resolved, we'll address the branch-path
8798 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8800 + printk(KERN_ERR "unionfs: error accessing "
8801 + "lower directory \"%s\" (error %d)\n",
8805 + for (idx = 0; idx < cur_branches; idx++)
8806 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8807 + nd.path.dentry == new_lower_paths[idx].dentry)
8809 + path_put(&nd.path); /* no longer needed */
8810 + if (idx == cur_branches) {
8811 + printk(KERN_ERR "unionfs: branch \"%s\" "
8812 + "not found\n", optarg);
8816 + /* check if there are any open files on the branch to be deleted */
8817 + if (atomic_read(&new_data[idx].open_files) > 0) {
8823 + * Now we have to delete the branch. First, release any handles it
8824 + * has. Then, move the remaining array indexes past "idx" in
8825 + * new_data and new_lower_paths one to the left. Finally, adjust
8828 + path_put(&new_lower_paths[idx]);
8830 + if (idx < cur_branches - 1) {
8831 + /* if idx==cur_branches-1, we delete last branch: easy */
8832 + memmove(&new_data[idx], &new_data[idx+1],
8833 + (cur_branches - 1 - idx) *
8834 + sizeof(struct unionfs_data));
8835 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8836 + (cur_branches - 1 - idx) * sizeof(struct path));
8844 +/* handle branch insertion during remount */
8845 +static noinline_for_stack int do_remount_add_option(
8846 + char *optarg, int cur_branches,
8847 + struct unionfs_data *new_data,
8848 + struct path *new_lower_paths,
8849 + int *high_branch_id)
8851 + int err = -EINVAL;
8853 + int idx = 0; /* default: insert at beginning */
8854 + char *new_branch , *modename = NULL;
8855 + struct nameidata nd;
8858 + * optarg can be of several forms:
8860 + * /bar:/foo insert /foo before /bar
8861 + * /bar:/foo=ro insert /foo in ro mode before /bar
8862 + * /foo insert /foo in the beginning (prepend)
8863 + * :/foo insert /foo at the end (append)
8865 + if (*optarg == ':') { /* append? */
8866 + new_branch = optarg + 1; /* skip ':' */
8867 + idx = cur_branches;
8868 + goto found_insertion_point;
8870 + new_branch = strchr(optarg, ':');
8871 + if (!new_branch) { /* prepend? */
8872 + new_branch = optarg;
8873 + goto found_insertion_point;
8875 + *new_branch++ = '\0'; /* holds path+mode of new branch */
8878 + * Find matching branch index. For now, this assumes that nothing
8879 + * has been mounted on top of this Unionfs stack. Once we have /odf
8880 + * and cache-coherency resolved, we'll address the branch-path
8883 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8885 + printk(KERN_ERR "unionfs: error accessing "
8886 + "lower directory \"%s\" (error %d)\n",
8890 + for (idx = 0; idx < cur_branches; idx++)
8891 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8892 + nd.path.dentry == new_lower_paths[idx].dentry)
8894 + path_put(&nd.path); /* no longer needed */
8895 + if (idx == cur_branches) {
8896 + printk(KERN_ERR "unionfs: branch \"%s\" "
8897 + "not found\n", optarg);
8903 + * At this point idx will hold the index where the new branch should
8904 + * be inserted before.
8906 +found_insertion_point:
8907 + /* find the mode for the new branch */
8909 + modename = strchr(new_branch, '=');
8911 + *modename++ = '\0';
8912 + if (!new_branch || !*new_branch) {
8913 + printk(KERN_ERR "unionfs: null new branch\n");
8917 + err = parse_branch_mode(modename, &perms);
8919 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8920 + "branch \"%s\"\n", modename, new_branch);
8923 + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8925 + printk(KERN_ERR "unionfs: error accessing "
8926 + "lower directory \"%s\" (error %d)\n",
8931 + * It's probably safe to check_mode the new branch to insert. Note:
8932 + * we don't allow inserting branches which are unionfs's by
8933 + * themselves (check_branch returns EINVAL in that case). This is
8934 + * because this code base doesn't support stacking unionfs: the ODF
8935 + * code base supports that correctly.
8937 + err = check_branch(&nd);
8939 + printk(KERN_ERR "unionfs: lower directory "
8940 + "\"%s\" is not a valid branch\n", optarg);
8941 + path_put(&nd.path);
8946 + * Now we have to insert the new branch. But first, move the bits
8947 + * to make space for the new branch, if needed. Finally, adjust
8949 + * We don't release nd here; it's kept until umount/remount.
8951 + if (idx < cur_branches) {
8952 + /* if idx==cur_branches, we append: easy */
8953 + memmove(&new_data[idx+1], &new_data[idx],
8954 + (cur_branches - idx) * sizeof(struct unionfs_data));
8955 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8956 + (cur_branches - idx) * sizeof(struct path));
8958 + new_lower_paths[idx].dentry = nd.path.dentry;
8959 + new_lower_paths[idx].mnt = nd.path.mnt;
8961 + new_data[idx].sb = nd.path.dentry->d_sb;
8962 + atomic_set(&new_data[idx].open_files, 0);
8963 + new_data[idx].branchperms = perms;
8964 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8973 + * Support branch management options on remount.
8975 + * See Documentation/filesystems/unionfs/ for details.
8977 + * @flags: numeric mount options
8978 + * @options: mount options string
8980 + * This function can rearrange a mounted union dynamically, adding and
8981 + * removing branches, including changing branch modes. Clearly this has to
8982 + * be done safely and atomically. Luckily, the VFS already calls this
8983 + * function with lock_super(sb) and lock_kernel() held, preventing
8984 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8985 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8986 + * to purge dentries/inodes from our superblock, and also called
8987 + * fsync_super(sb) to purge any dirty pages. So we're good.
8989 + * XXX: however, our remount code may also need to invalidate mapped pages
8990 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8991 + * branches. This has to wait for proper mmap and cache coherency support
8995 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
9000 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
9002 + int cur_branches = 0; /* no. of current branches */
9003 + int new_branches = 0; /* no. of branches actually left in the end */
9004 + int add_branches; /* est. no. of branches to add */
9005 + int del_branches; /* est. no. of branches to del */
9006 + int max_branches; /* max possible no. of branches */
9007 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
9008 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
9009 + struct inode **new_lower_inodes = NULL;
9010 + int new_high_branch_id; /* new high branch ID */
9011 + int size; /* memory allocation size, temp var */
9012 + int old_ibstart, old_ibend;
9014 + unionfs_write_lock(sb);
9017 + * The VFS will take care of "ro" and "rw" flags, and we can safely
9018 + * ignore MS_SILENT, but anything else left over is an error. So we
9019 + * need to check if any other flags may have been passed (none are
9020 + * allowed/supported as of now).
9022 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
9024 + "unionfs: remount flags 0x%x unsupported\n", *flags);
9030 + * If 'options' is NULL, it's probably because the user just changed
9031 + * the union to a "ro" or "rw" and the VFS took care of it. So
9032 + * nothing to do and we're done.
9034 + if (!options || options[0] == '\0')
9038 + * Find out how many branches we will have in the end, counting
9039 + * "add" and "del" commands. Copy the "options" string because
9040 + * strsep modifies the string and we need it later.
9042 + tmp_to_free = kstrdup(options, GFP_KERNEL);
9043 + optionstmp = tmp_to_free;
9044 + if (unlikely(!optionstmp)) {
9048 + cur_branches = sbmax(sb); /* current no. branches */
9049 + new_branches = sbmax(sb);
9052 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9053 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
9056 + if (!optname || !*optname)
9059 + optarg = strchr(optname, '=');
9063 + if (!strcmp("add", optname))
9065 + else if (!strcmp("del", optname))
9068 + kfree(tmp_to_free);
9069 + /* after all changes, will we have at least one branch left? */
9070 + if ((new_branches + add_branches - del_branches) < 1) {
9072 + "unionfs: no branches left after remount\n");
9078 + * Since we haven't actually parsed all the add/del options, nor
9079 + * have we checked them for errors, we don't know for sure how many
9080 + * branches we will have after all changes have taken place. In
9081 + * fact, the total number of branches left could be less than what
9082 + * we have now. So we need to allocate space for a temporary
9083 + * placeholder that is at least as large as the maximum number of
9084 + * branches we *could* have, which is the current number plus all
9085 + * the additions. Once we're done with these temp placeholders, we
9086 + * may have to re-allocate the final size, copy over from the temp,
9087 + * and then free the temps (done near the end of this function).
9089 + max_branches = cur_branches + add_branches;
9090 + /* allocate space for new pointers to lower dentry */
9091 + tmp_data = kcalloc(max_branches,
9092 + sizeof(struct unionfs_data), GFP_KERNEL);
9093 + if (unlikely(!tmp_data)) {
9097 + /* allocate space for new pointers to lower paths */
9098 + tmp_lower_paths = kcalloc(max_branches,
9099 + sizeof(struct path), GFP_KERNEL);
9100 + if (unlikely(!tmp_lower_paths)) {
9104 + /* copy current info into new placeholders, incrementing refcnts */
9105 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9106 + cur_branches * sizeof(struct unionfs_data));
9107 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9108 + cur_branches * sizeof(struct path));
9109 + for (i = 0; i < cur_branches; i++)
9110 + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9112 + /*******************************************************************
9113 + * For each branch command, do path_lookup on the requested branch,
9114 + * and apply the change to a temp branch list. To handle errors, we
9115 + * already dup'ed the old arrays (above), and increased the refcnts
9116 + * on various f/s objects. So now we can do all the path_lookups
9117 + * and branch-management commands on the new arrays. If it fail mid
9118 + * way, we free the tmp arrays and *put all objects. If we succeed,
9119 + * then we free old arrays and *put its objects, and then replace
9120 + * the arrays with the new tmp list (we may have to re-allocate the
9121 + * memory because the temp lists could have been larger than what we
9122 + * actually needed).
9123 + *******************************************************************/
9125 + while ((optname = strsep(&options, ",")) != NULL) {
9128 + if (!optname || !*optname)
9131 + * At this stage optname holds a comma-delimited option, but
9132 + * without the commas. Next, we need to break the string on
9133 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9134 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9135 + * KEY is "/foo", and VAL is "rw".
9137 + optarg = strchr(optname, '=');
9140 + /* incgen remount option (instead of old ioctl) */
9141 + if (!strcmp("incgen", optname)) {
9143 + goto out_no_change;
9147 + * All of our options take an argument now. (Insert ones
9148 + * that don't above this check.) So at this stage optname
9149 + * contains the CMD part and optarg contains the ARG part.
9151 + if (!optarg || !*optarg) {
9152 + printk(KERN_ERR "unionfs: all remount options require "
9153 + "an argument (%s)\n", optname);
9158 + if (!strcmp("add", optname)) {
9159 + err = do_remount_add_option(optarg, new_branches,
9162 + &new_high_branch_id);
9166 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9167 + printk(KERN_ERR "unionfs: command exceeds "
9168 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9174 + if (!strcmp("del", optname)) {
9175 + err = do_remount_del_option(optarg, new_branches,
9183 + if (!strcmp("mode", optname)) {
9184 + err = do_remount_mode_option(optarg, new_branches,
9193 + * When you use "mount -o remount,ro", mount(8) will
9194 + * reportedly pass the original dirs= string from
9195 + * /proc/mounts. So for now, we have to ignore dirs= and
9196 + * not consider it an error, unless we want to allow users
9197 + * to pass dirs= in remount. Note that to allow the VFS to
9198 + * actually process the ro/rw remount options, we have to
9199 + * return 0 from this function.
9201 + if (!strcmp("dirs", optname)) {
9202 + printk(KERN_WARNING
9203 + "unionfs: remount ignoring option \"%s\"\n",
9210 + "unionfs: unrecognized option \"%s\"\n", optname);
9216 + /******************************************************************
9217 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9218 + * see if we need to allocate a small-sized new vector, copy the
9219 + * vectors to their correct place, release the refcnt of the older
9220 + * ones, and return. Also handle invalidating any pages that will
9221 + * have to be re-read.
9222 + *******************************************************************/
9224 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9225 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9226 + "(use \"remount,ro\" to create a read-only union)\n");
9231 + /* (re)allocate space for new pointers to lower dentry */
9232 + size = new_branches * sizeof(struct unionfs_data);
9233 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9234 + if (unlikely(!new_data)) {
9239 + /* allocate space for new pointers to lower paths */
9240 + size = new_branches * sizeof(struct path);
9241 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9242 + if (unlikely(!new_lower_paths)) {
9247 + /* allocate space for new pointers to lower inodes */
9248 + new_lower_inodes = kcalloc(new_branches,
9249 + sizeof(struct inode *), GFP_KERNEL);
9250 + if (unlikely(!new_lower_inodes)) {
9256 + * OK, just before we actually put the new set of branches in place,
9257 + * we need to ensure that our own f/s has no dirty objects left.
9258 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9259 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9260 + * pages. So all that's left is for us to invalidate any leftover
9261 + * (non-dirty) pages to ensure that they will be re-read from the
9262 + * new lower branches (and to support mmap).
9266 + * Once we finish the remounting successfully, our superblock
9267 + * generation number will have increased. This will be detected by
9268 + * our dentry-revalidation code upon subsequent f/s operations
9269 + * through unionfs. The revalidation code will rebuild the union of
9270 + * lower inodes for a given unionfs inode and invalidate any pages
9271 + * of such "stale" inodes (by calling our purge_inode_data
9272 + * function). This revalidation will happen lazily and
9273 + * incrementally, as users perform operations on cached inodes. We
9274 + * would like to encourage this revalidation to happen sooner if
9275 + * possible, so we like to try to invalidate as many other pages in
9276 + * our superblock as we can. We used to call drop_pagecache_sb() or
9277 + * a variant thereof, but either method was racy (drop_caches alone
9278 + * is known to be racy). So now we let the revalidation happen on a
9279 + * per file basis in ->d_revalidate.
9282 + /* grab new lower super references; release old ones */
9283 + for (i = 0; i < new_branches; i++)
9284 + atomic_inc(&new_data[i].sb->s_active);
9285 + for (i = 0; i < sbmax(sb); i++)
9286 + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9288 + /* copy new vectors into their correct place */
9289 + tmp_data = UNIONFS_SB(sb)->data;
9290 + UNIONFS_SB(sb)->data = new_data;
9291 + new_data = NULL; /* so don't free good pointers below */
9292 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9293 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9294 + new_lower_paths = NULL; /* so don't free good pointers below */
9296 + /* update our unionfs_sb_info and root dentry index of last branch */
9297 + i = sbmax(sb); /* save no. of branches to release at end */
9298 + sbend(sb) = new_branches - 1;
9299 + dbend(sb->s_root) = new_branches - 1;
9300 + old_ibstart = ibstart(sb->s_root->d_inode);
9301 + old_ibend = ibend(sb->s_root->d_inode);
9302 + ibend(sb->s_root->d_inode) = new_branches - 1;
9303 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9304 + new_branches = i; /* no. of branches to release below */
9307 + * Update lower inodes: 3 steps
9308 + * 1. grab ref on all new lower inodes
9310 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9311 + struct dentry *lower_dentry =
9312 + unionfs_lower_dentry_idx(sb->s_root, i);
9313 + igrab(lower_dentry->d_inode);
9314 + new_lower_inodes[i] = lower_dentry->d_inode;
9316 + /* 2. release reference on all older lower inodes */
9317 + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9318 + /* 3. update root dentry's inode to new lower_inodes array */
9319 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9320 + new_lower_inodes = NULL;
9322 + /* maxbytes may have changed */
9323 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9324 + /* update high branch ID */
9325 + sbhbid(sb) = new_high_branch_id;
9327 + /* update our sb->generation for revalidating objects */
9328 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9329 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9330 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9331 + if (!(*flags & MS_SILENT))
9332 + pr_info("unionfs: %s: new generation number %d\n",
9333 + UNIONFS_SB(sb)->dev_name, i);
9334 + /* finally, update the root dentry's times */
9335 + unionfs_copy_attr_times(sb->s_root->d_inode);
9336 + err = 0; /* reset to success */
9339 + * The code above falls through to the next label, and releases the
9340 + * refcnts of the older ones (stored in tmp_*): if we fell through
9341 + * here, it means success. However, if we jump directly to this
9342 + * label from any error above, then an error occurred after we
9343 + * grabbed various refcnts, and so we have to release the
9344 + * temporarily constructed structures.
9347 + /* no need to cleanup/release anything in tmp_data */
9348 + if (tmp_lower_paths)
9349 + for (i = 0; i < new_branches; i++)
9350 + path_put(&tmp_lower_paths[i]);
9352 + kfree(tmp_lower_paths);
9354 + kfree(new_lower_paths);
9356 + kfree(new_lower_inodes);
9358 + unionfs_check_dentry(sb->s_root);
9359 + unionfs_write_unlock(sb);
9364 + * Called by iput() when the inode reference count reached zero
9365 + * and the inode is not hashed anywhere. Used to clear anything
9366 + * that needs to be, before the inode is completely destroyed and put
9367 + * on the inode free list.
9369 + * No need to lock sb info's rwsem.
9371 +static void unionfs_clear_inode(struct inode *inode)
9373 + int bindex, bstart, bend;
9374 + struct inode *lower_inode;
9375 + struct list_head *pos, *n;
9376 + struct unionfs_dir_state *rdstate;
9378 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9379 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9380 + list_del(&rdstate->cache);
9381 + free_rdstate(rdstate);
9385 + * Decrement a reference to a lower_inode, which was incremented
9386 + * by our read_inode when it was created initially.
9388 + bstart = ibstart(inode);
9389 + bend = ibend(inode);
9390 + if (bstart >= 0) {
9391 + for (bindex = bstart; bindex <= bend; bindex++) {
9392 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9395 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9396 + /* see Documentation/filesystems/unionfs/issues.txt */
9398 + iput(lower_inode);
9403 + kfree(UNIONFS_I(inode)->lower_inodes);
9404 + UNIONFS_I(inode)->lower_inodes = NULL;
9407 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9409 + struct unionfs_inode_info *i;
9411 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9415 + /* memset everything up to the inode to 0 */
9416 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9418 + i->vfs_inode.i_version = 1;
9419 + return &i->vfs_inode;
9422 +static void unionfs_destroy_inode(struct inode *inode)
9424 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9427 +/* unionfs inode cache constructor */
9428 +static void init_once(void *obj)
9430 + struct unionfs_inode_info *i = obj;
9432 + inode_init_once(&i->vfs_inode);
9435 +int unionfs_init_inode_cache(void)
9439 + unionfs_inode_cachep =
9440 + kmem_cache_create("unionfs_inode_cache",
9441 + sizeof(struct unionfs_inode_info), 0,
9442 + SLAB_RECLAIM_ACCOUNT, init_once);
9443 + if (unlikely(!unionfs_inode_cachep))
9448 +/* unionfs inode cache destructor */
9449 +void unionfs_destroy_inode_cache(void)
9451 + if (unionfs_inode_cachep)
9452 + kmem_cache_destroy(unionfs_inode_cachep);
9456 + * Called when we have a dirty inode, right here we only throw out
9457 + * parts of our readdir list that are too old.
9459 + * No need to grab sb info's rwsem.
9461 +static int unionfs_write_inode(struct inode *inode, int sync)
9463 + struct list_head *pos, *n;
9464 + struct unionfs_dir_state *rdstate;
9466 + spin_lock(&UNIONFS_I(inode)->rdlock);
9467 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9468 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9469 + /* We keep this list in LRU order. */
9470 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9472 + UNIONFS_I(inode)->rdcount--;
9473 + list_del(&rdstate->cache);
9474 + free_rdstate(rdstate);
9476 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9482 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9483 + * code can actually succeed and won't leave tasks that need handling.
9485 +static void unionfs_umount_begin(struct super_block *sb)
9487 + struct super_block *lower_sb;
9488 + int bindex, bstart, bend;
9490 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9492 + bstart = sbstart(sb);
9494 + for (bindex = bstart; bindex <= bend; bindex++) {
9495 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9497 + if (lower_sb && lower_sb->s_op &&
9498 + lower_sb->s_op->umount_begin)
9499 + lower_sb->s_op->umount_begin(lower_sb);
9502 + unionfs_read_unlock(sb);
9505 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9507 + struct super_block *sb = mnt->mnt_sb;
9511 + int bindex, bstart, bend;
9514 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9516 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9518 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9519 + if (unlikely(!tmp_page)) {
9524 + bstart = sbstart(sb);
9527 + seq_printf(m, ",dirs=");
9528 + for (bindex = bstart; bindex <= bend; bindex++) {
9530 + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9531 + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9532 + path = d_path(&p, tmp_page, PAGE_SIZE);
9533 + if (IS_ERR(path)) {
9534 + ret = PTR_ERR(path);
9538 + perms = branchperms(sb, bindex);
9540 + seq_printf(m, "%s=%s", path,
9541 + perms & MAY_WRITE ? "rw" : "ro");
9542 + if (bindex != bend)
9543 + seq_printf(m, ":");
9547 + free_page((unsigned long) tmp_page);
9549 + unionfs_unlock_dentry(sb->s_root);
9551 + unionfs_read_unlock(sb);
9556 +struct super_operations unionfs_sops = {
9557 + .delete_inode = unionfs_delete_inode,
9558 + .put_super = unionfs_put_super,
9559 + .statfs = unionfs_statfs,
9560 + .remount_fs = unionfs_remount_fs,
9561 + .clear_inode = unionfs_clear_inode,
9562 + .umount_begin = unionfs_umount_begin,
9563 + .show_options = unionfs_show_options,
9564 + .write_inode = unionfs_write_inode,
9565 + .alloc_inode = unionfs_alloc_inode,
9566 + .destroy_inode = unionfs_destroy_inode,
9568 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9569 new file mode 100644
9570 index 0000000..17a0056
9572 +++ b/fs/unionfs/union.h
9575 + * Copyright (c) 2003-2009 Erez Zadok
9576 + * Copyright (c) 2003-2006 Charles P. Wright
9577 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9578 + * Copyright (c) 2005 Arun M. Krishnakumar
9579 + * Copyright (c) 2004-2006 David P. Quigley
9580 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9581 + * Copyright (c) 2003 Puja Gupta
9582 + * Copyright (c) 2003 Harikesavan Krishnan
9583 + * Copyright (c) 2003-2009 Stony Brook University
9584 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
9586 + * This program is free software; you can redistribute it and/or modify
9587 + * it under the terms of the GNU General Public License version 2 as
9588 + * published by the Free Software Foundation.
9594 +#include <linux/dcache.h>
9595 +#include <linux/file.h>
9596 +#include <linux/list.h>
9597 +#include <linux/fs.h>
9598 +#include <linux/mm.h>
9599 +#include <linux/module.h>
9600 +#include <linux/mount.h>
9601 +#include <linux/namei.h>
9602 +#include <linux/page-flags.h>
9603 +#include <linux/pagemap.h>
9604 +#include <linux/poll.h>
9605 +#include <linux/security.h>
9606 +#include <linux/seq_file.h>
9607 +#include <linux/slab.h>
9608 +#include <linux/spinlock.h>
9609 +#include <linux/smp_lock.h>
9610 +#include <linux/statfs.h>
9611 +#include <linux/string.h>
9612 +#include <linux/vmalloc.h>
9613 +#include <linux/writeback.h>
9614 +#include <linux/buffer_head.h>
9615 +#include <linux/xattr.h>
9616 +#include <linux/fs_stack.h>
9617 +#include <linux/magic.h>
9618 +#include <linux/log2.h>
9619 +#include <linux/poison.h>
9620 +#include <linux/mman.h>
9621 +#include <linux/backing-dev.h>
9622 +#include <linux/splice.h>
9624 +#include <asm/system.h>
9626 +#include <linux/union_fs.h>
9628 +/* the file system name */
9629 +#define UNIONFS_NAME "unionfs"
9631 +/* unionfs root inode number */
9632 +#define UNIONFS_ROOT_INO 1
9634 +/* number of times we try to get a unique temporary file name */
9635 +#define GET_TMPNAM_MAX_RETRY 5
9637 +/* maximum number of branches we support, to avoid memory blowup */
9638 +#define UNIONFS_MAX_BRANCHES 128
9640 +/* minimum time (seconds) required for time-based cache-coherency */
9641 +#define UNIONFS_MIN_CC_TIME 3
9643 +/* Operations vectors defined in specific files. */
9644 +extern struct file_operations unionfs_main_fops;
9645 +extern struct file_operations unionfs_dir_fops;
9646 +extern struct inode_operations unionfs_main_iops;
9647 +extern struct inode_operations unionfs_dir_iops;
9648 +extern struct inode_operations unionfs_symlink_iops;
9649 +extern struct super_operations unionfs_sops;
9650 +extern struct dentry_operations unionfs_dops;
9651 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9652 +extern struct vm_operations_struct unionfs_vm_ops;
9654 +/* How long should an entry be allowed to persist */
9655 +#define RDCACHE_JIFFIES (5*HZ)
9657 +/* compatibility with Real-Time patches */
9658 +#ifdef CONFIG_PREEMPT_RT
9659 +# define unionfs_rw_semaphore compat_rw_semaphore
9660 +#else /* not CONFIG_PREEMPT_RT */
9661 +# define unionfs_rw_semaphore rw_semaphore
9662 +#endif /* not CONFIG_PREEMPT_RT */
9664 +/* file private data. */
9665 +struct unionfs_file_info {
9668 + atomic_t generation;
9670 + struct unionfs_dir_state *rdstate;
9671 + struct file **lower_files;
9672 + int *saved_branch_ids; /* IDs of branches when file was opened */
9673 + struct vm_operations_struct *lower_vm_ops;
9674 + bool wrote_to_file; /* for delayed copyup */
9677 +/* unionfs inode data in memory */
9678 +struct unionfs_inode_info {
9681 + atomic_t generation;
9682 + /* Stuff for readdir over NFS. */
9683 + spinlock_t rdlock;
9684 + struct list_head readdircache;
9689 + /* The lower inodes */
9690 + struct inode **lower_inodes;
9692 + struct inode vfs_inode;
9695 +/* unionfs dentry data in memory */
9696 +struct unionfs_dentry_info {
9698 + * The semaphore is used to lock the dentry as soon as we get into a
9699 + * unionfs function from the VFS. Our lock ordering is that children
9700 + * go before their parents.
9702 + struct mutex lock;
9707 + atomic_t generation;
9708 + struct path *lower_paths;
9711 +/* These are the pointers to our various objects. */
9712 +struct unionfs_data {
9713 + struct super_block *sb; /* lower super_block */
9714 + atomic_t open_files; /* number of open files on branch */
9716 + int branch_id; /* unique branch ID at re/mount time */
9719 +/* unionfs super-block data in memory */
9720 +struct unionfs_sb_info {
9723 + atomic_t generation;
9726 + * This rwsem is used to make sure that a branch management
9728 + * 1) will not begin before all currently in-flight operations
9730 + * 2) any new operations do not execute until the currently
9731 + * running branch management operation completes.
9733 + * The write_lock_owner records the PID of the task which grabbed
9734 + * the rw_sem for writing. If the same task also tries to grab the
9735 + * read lock, we allow it. This prevents a self-deadlock when
9736 + * branch-management is used on a pivot_root'ed union, because we
9737 + * have to ->lookup paths which belong to the same union.
9739 + struct unionfs_rw_semaphore rwsem;
9740 + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9741 + int high_branch_id; /* last unique branch ID given */
9742 + char *dev_name; /* to identify different unions in pr_debug */
9743 + struct unionfs_data *data;
9747 + * structure for making the linked list of entries by readdir on left branch
9748 + * to compare with entries on right branch
9750 +struct filldir_node {
9751 + struct list_head file_list; /* list for directory entries */
9752 + char *name; /* name entry */
9753 + int hash; /* name hash */
9754 + int namelen; /* name len since name is not 0 terminated */
9757 + * we can check for duplicate whiteouts and files in the same branch
9758 + * in order to return -EIO.
9762 + /* is this a whiteout entry? */
9765 + /* Inline name, so we don't need to separately kmalloc small ones */
9766 + char iname[DNAME_INLINE_LEN_MIN];
9769 +/* Directory hash table. */
9770 +struct unionfs_dir_state {
9771 + unsigned int cookie; /* the cookie, based off of rdversion */
9772 + unsigned int offset; /* The entry we have returned. */
9774 + loff_t dirpos; /* offset within the lower level directory */
9775 + int size; /* How big is the hash table? */
9776 + int hashentries; /* How many entries have been inserted? */
9777 + unsigned long access;
9779 + /* This cache list is used when the inode keeps us around. */
9780 + struct list_head cache;
9781 + struct list_head list[0];
9784 +/* externs needed for fanout.h or sioq.h */
9785 +extern int unionfs_get_nlinks(const struct inode *inode);
9786 +extern void unionfs_copy_attr_times(struct inode *upper);
9787 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9789 +/* include miscellaneous macros */
9790 +#include "fanout.h"
9793 +/* externs for cache creation/deletion routines */
9794 +extern void unionfs_destroy_filldir_cache(void);
9795 +extern int unionfs_init_filldir_cache(void);
9796 +extern int unionfs_init_inode_cache(void);
9797 +extern void unionfs_destroy_inode_cache(void);
9798 +extern int unionfs_init_dentry_cache(void);
9799 +extern void unionfs_destroy_dentry_cache(void);
9801 +/* Initialize and free readdir-specific state. */
9802 +extern int init_rdstate(struct file *file);
9803 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9805 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9807 +extern void free_rdstate(struct unionfs_dir_state *state);
9808 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9809 + const char *name, int namelen, int bindex,
9811 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9812 + const char *name, int namelen,
9815 +extern struct dentry **alloc_new_dentries(int objs);
9816 +extern struct unionfs_data *alloc_new_data(int objs);
9818 +/* We can only use 32-bits of offset for rdstate --- blech! */
9819 +#define DIREOF (0xfffff)
9820 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9821 +#define MAXRDCOOKIE (0xfff)
9822 +/* Turn an rdstate into an offset. */
9823 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9827 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9828 + | (buf->offset & DIREOF);
9832 +/* Macros for locking a super_block. */
9833 +enum unionfs_super_lock_class {
9834 + UNIONFS_SMUTEX_NORMAL,
9835 + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9836 + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9838 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9840 + if (UNIONFS_SB(sb)->write_lock_owner &&
9841 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9843 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9845 +static inline void unionfs_read_unlock(struct super_block *sb)
9847 + if (UNIONFS_SB(sb)->write_lock_owner &&
9848 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9850 + up_read(&UNIONFS_SB(sb)->rwsem);
9852 +static inline void unionfs_write_lock(struct super_block *sb)
9854 + down_write(&UNIONFS_SB(sb)->rwsem);
9855 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9857 +static inline void unionfs_write_unlock(struct super_block *sb)
9859 + up_write(&UNIONFS_SB(sb)->rwsem);
9860 + UNIONFS_SB(sb)->write_lock_owner = 0;
9863 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9864 + struct dentry *d2)
9868 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9869 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9871 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9872 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9876 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9877 + struct dentry *d2)
9880 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9881 + unionfs_unlock_dentry(d1);
9882 + unionfs_unlock_dentry(d2);
9884 + unionfs_unlock_dentry(d2);
9885 + unionfs_unlock_dentry(d1);
9889 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9890 + struct dentry *p2)
9893 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9897 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9898 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9900 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9901 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9905 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9906 + struct dentry *p2)
9909 + unionfs_unlock_dentry(p1);
9912 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9913 + unionfs_unlock_dentry(p1);
9914 + unionfs_unlock_dentry(p2);
9916 + unionfs_unlock_dentry(p2);
9917 + unionfs_unlock_dentry(p1);
9921 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9922 +extern int realloc_dentry_private_data(struct dentry *dentry);
9923 +extern void free_dentry_private_data(struct dentry *dentry);
9924 +extern void update_bstart(struct dentry *dentry);
9925 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9926 +extern void release_lower_nd(struct nameidata *nd, int err);
9932 +/* replicates the directory structure up to given dentry in given branch */
9933 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9934 + const char *name, int bindex);
9936 +/* partial lookup */
9937 +extern int unionfs_partial_lookup(struct dentry *dentry,
9938 + struct dentry *parent);
9939 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9940 + struct dentry *parent,
9943 +/* copies a file from dbstart to newbindex branch */
9944 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9945 + int newbindex, loff_t size);
9946 +extern int copyup_named_file(struct inode *dir, struct file *file,
9947 + char *name, int bstart, int new_bindex,
9949 +/* copies a dentry from dbstart to newbindex branch */
9950 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9951 + int bstart, int new_bindex, const char *name,
9952 + int namelen, struct file **copyup_file, loff_t len);
9953 +/* helper functions for post-copyup actions */
9954 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9955 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9957 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9958 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9959 + struct unionfs_dir_state **namelist);
9960 +/* whiteout and opaque directory helpers */
9961 +extern char *alloc_whname(const char *name, int len);
9962 +extern bool is_whiteout_name(char **namep, int *namelenp);
9963 +extern bool is_validname(const char *name);
9964 +extern struct dentry *lookup_whiteout(const char *name,
9965 + struct dentry *lower_parent);
9966 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9967 +extern int unlink_whiteout(struct dentry *wh_dentry);
9968 +extern int check_unlink_whiteout(struct dentry *dentry,
9969 + struct dentry *lower_dentry, int bindex);
9970 +extern int create_whiteout(struct dentry *dentry, int start);
9971 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9972 + struct unionfs_dir_state *namelist);
9973 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9974 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9975 +extern void unionfs_set_max_namelen(long *namelen);
9977 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9978 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9980 +/* Locking functions. */
9981 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9982 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9984 +/* Common file operations. */
9985 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9987 +extern int unionfs_open(struct inode *inode, struct file *file);
9988 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9989 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9990 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9991 + unsigned long arg);
9992 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
9994 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9996 +/* Inode operations */
9997 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9998 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9999 + struct inode *new_dir, struct dentry *new_dentry);
10000 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
10001 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
10003 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
10004 + struct dentry *parent, bool willwrite);
10005 +extern bool is_negative_lower(const struct dentry *dentry);
10006 +extern bool is_newer_lower(const struct dentry *dentry);
10007 +extern void purge_sb_data(struct super_block *sb);
10009 +/* The values for unionfs_interpose's flag. */
10010 +#define INTERPOSE_DEFAULT 0
10011 +#define INTERPOSE_LOOKUP 1
10012 +#define INTERPOSE_REVAL 2
10013 +#define INTERPOSE_REVAL_NEG 3
10014 +#define INTERPOSE_PARTIAL 4
10016 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
10017 + struct super_block *sb, int flag);
10019 +#ifdef CONFIG_UNION_FS_XATTR
10020 +/* Extended attribute functions. */
10021 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
10022 +static inline void unionfs_xattr_kfree(const void *p)
10026 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
10027 + void *value, size_t size);
10028 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
10029 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
10031 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
10032 + const void *value, size_t size, int flags);
10033 +#endif /* CONFIG_UNION_FS_XATTR */
10035 +/* The root directory is unhashed, but isn't deleted. */
10036 +static inline int d_deleted(struct dentry *d)
10038 + return d_unhashed(d) && (d != d->d_sb->s_root);
10041 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10042 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10044 +/* unionfs_open, check if we need to copyup the file */
10045 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10046 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10048 +static inline int branchperms(const struct super_block *sb, int index)
10050 + BUG_ON(index < 0);
10051 + return UNIONFS_SB(sb)->data[index].branchperms;
10054 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10056 + BUG_ON(index < 0);
10057 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10061 +/* check if readonly lower inode, but possibly unlinked (no inode->i_sb) */
10062 +static inline int __is_rdonly(const struct inode *inode)
10064 + /* if unlinked, can't be readonly (?) */
10065 + if (!inode->i_sb)
10067 + return IS_RDONLY(inode);
10070 +/* Is this file on a read-only branch? */
10071 +static inline int is_robranch_super(const struct super_block *sb, int index)
10075 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10079 +/* Is this file on a read-only branch? */
10080 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10082 + struct super_block *lower_sb;
10084 + BUG_ON(index < 0);
10086 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10089 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10090 + BUG_ON(lower_sb == NULL);
10092 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10093 + * lower_dentry could be a negative.
10095 + if (lower_sb->s_flags & MS_RDONLY)
10101 +static inline int is_robranch(const struct dentry *dentry)
10105 + index = UNIONFS_D(dentry)->bstart;
10106 + BUG_ON(index < 0);
10108 + return is_robranch_idx(dentry, index);
10114 +extern int check_branch(struct nameidata *nd);
10115 +extern int parse_branch_mode(const char *name, int *perms);
10117 +/* locking helpers */
10118 +static inline struct dentry *lock_parent(struct dentry *dentry)
10120 + struct dentry *dir = dget_parent(dentry);
10121 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
10124 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10126 + struct dentry *dir = dget_parent(dentry);
10128 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10132 +static inline void unlock_dir(struct dentry *dir)
10134 + mutex_unlock(&dir->d_inode->i_mutex);
10138 +/* lock base inode mutex before calling lookup_one_len */
10139 +static inline struct dentry *lookup_lck_len(const char *name,
10140 + struct dentry *base, int len)
10142 + struct dentry *d;
10143 + mutex_lock(&base->d_inode->i_mutex);
10144 + d = lookup_one_len(name, base, len);
10145 + mutex_unlock(&base->d_inode->i_mutex);
10149 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10152 + struct vfsmount *mnt;
10154 + BUG_ON(!dentry || bindex < 0);
10156 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10157 +#ifdef CONFIG_UNION_FS_DEBUG
10159 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10161 +#endif /* CONFIG_UNION_FS_DEBUG */
10166 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10168 + struct vfsmount *mnt;
10170 + if (!dentry && bindex < 0)
10172 + BUG_ON(!dentry || bindex < 0);
10174 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10175 +#ifdef CONFIG_UNION_FS_DEBUG
10177 + * Directories can have NULL lower objects in between start/end, but
10178 + * NOT if at the start/end range. We cannot verify that this dentry
10179 + * is a type=DIR, because it may already be a negative dentry. But
10180 + * if dbstart is greater than dbend, we know that this couldn't have
10181 + * been a regular file: it had to have been a directory.
10183 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10184 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10185 +#endif /* CONFIG_UNION_FS_DEBUG */
10189 +#ifdef CONFIG_UNION_FS_DEBUG
10191 +/* useful for tracking code reachability */
10192 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10194 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10195 + __FILE__, __func__, __LINE__)
10196 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10197 + __FILE__, __func__, __LINE__)
10198 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10199 + __FILE__, __func__, __LINE__)
10200 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10201 + __FILE__, __func__, __LINE__)
10202 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10203 + __FILE__, __func__, __LINE__)
10204 +#define show_inode_times(i) __show_inode_times((i), \
10205 + __FILE__, __func__, __LINE__)
10206 +#define show_dinode_times(d) __show_dinode_times((d), \
10207 + __FILE__, __func__, __LINE__)
10208 +#define show_inode_counts(i) __show_inode_counts((i), \
10209 + __FILE__, __func__, __LINE__)
10211 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10212 + const char *fxn, int line);
10213 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10214 + const char *fname, const char *fxn,
10216 +extern void __unionfs_check_file(const struct file *file,
10217 + const char *fname, const char *fxn, int line);
10218 +extern void __unionfs_check_nd(const struct nameidata *nd,
10219 + const char *fname, const char *fxn, int line);
10220 +extern void __show_branch_counts(const struct super_block *sb,
10221 + const char *file, const char *fxn, int line);
10222 +extern void __show_inode_times(const struct inode *inode,
10223 + const char *file, const char *fxn, int line);
10224 +extern void __show_dinode_times(const struct dentry *dentry,
10225 + const char *file, const char *fxn, int line);
10226 +extern void __show_inode_counts(const struct inode *inode,
10227 + const char *file, const char *fxn, int line);
10229 +#else /* not CONFIG_UNION_FS_DEBUG */
10231 +/* we leave useful hooks for these check functions throughout the code */
10232 +#define unionfs_check_inode(i) do { } while (0)
10233 +#define unionfs_check_dentry(d) do { } while (0)
10234 +#define unionfs_check_file(f) do { } while (0)
10235 +#define unionfs_check_nd(n) do { } while (0)
10236 +#define show_branch_counts(sb) do { } while (0)
10237 +#define show_inode_times(i) do { } while (0)
10238 +#define show_dinode_times(d) do { } while (0)
10239 +#define show_inode_counts(i) do { } while (0)
10241 +#endif /* not CONFIG_UNION_FS_DEBUG */
10243 +#endif /* not _UNION_H_ */
10244 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10245 new file mode 100644
10246 index 0000000..b6d8e10
10248 +++ b/fs/unionfs/unlink.c
10251 + * Copyright (c) 2003-2009 Erez Zadok
10252 + * Copyright (c) 2003-2006 Charles P. Wright
10253 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10254 + * Copyright (c) 2005-2006 Junjiro Okajima
10255 + * Copyright (c) 2005 Arun M. Krishnakumar
10256 + * Copyright (c) 2004-2006 David P. Quigley
10257 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10258 + * Copyright (c) 2003 Puja Gupta
10259 + * Copyright (c) 2003 Harikesavan Krishnan
10260 + * Copyright (c) 2003-2009 Stony Brook University
10261 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10263 + * This program is free software; you can redistribute it and/or modify
10264 + * it under the terms of the GNU General Public License version 2 as
10265 + * published by the Free Software Foundation.
10268 +#include "union.h"
10271 + * Helper function for Unionfs's unlink operation.
10273 + * The main goal of this function is to optimize the unlinking of non-dir
10274 + * objects in unionfs by deleting all possible lower inode objects from the
10275 + * underlying branches having same dentry name as the non-dir dentry on
10276 + * which this unlink operation is called. This way we delete as many lower
10277 + * inodes as possible, and save space. Whiteouts need to be created in
10278 + * branch0 only if unlinking fails on any of the lower branch other than
10279 + * branch0, or if a lower branch is marked read-only.
10281 + * Also, while unlinking a file, if we encounter any dir type entry in any
10282 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10283 + * The following special cases are also handled:
10285 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10286 + * appropriate error.
10288 + * (2) If we get an error during unlink in any of other lower branch other
10289 + * than branch0, then we create a whiteout in branch0.
10291 + * (3) If a whiteout already exists in any intermediate branch, we delete
10292 + * all possible inodes only up to that branch (this is an "opaqueness"
10293 + * as as per Documentation/filesystems/unionfs/concepts.txt).
10296 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10297 + struct dentry *parent)
10299 + struct dentry *lower_dentry;
10300 + struct dentry *lower_dir_dentry;
10304 + err = unionfs_partial_lookup(dentry, parent);
10308 + /* trying to unlink all possible valid instances */
10309 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10310 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10311 + if (!lower_dentry || !lower_dentry->d_inode)
10314 + lower_dir_dentry = lock_parent(lower_dentry);
10316 + /* avoid destroying the lower inode if the object is in use */
10317 + dget(lower_dentry);
10318 + err = is_robranch_super(dentry->d_sb, bindex);
10320 + /* see Documentation/filesystems/unionfs/issues.txt */
10322 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10323 + err = vfs_unlink(lower_dir_dentry->d_inode,
10326 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10331 + /* if lower object deletion succeeds, update inode's times */
10333 + unionfs_copy_attr_times(dentry->d_inode);
10334 + dput(lower_dentry);
10335 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10336 + unlock_dir(lower_dir_dentry);
10343 + * Create the whiteout in branch 0 (highest priority) only if (a)
10344 + * there was an error in any intermediate branch other than branch 0
10345 + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10346 + * mounted read-only.
10349 + if ((bindex == 0) ||
10350 + ((bindex == dbstart(dentry)) &&
10351 + (!IS_COPYUP_ERR(err))))
10354 + if (!IS_COPYUP_ERR(err))
10355 + pr_debug("unionfs: lower object deletion "
10356 + "failed in branch:%d\n", bindex);
10357 + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10363 + inode_dec_link_count(dentry->d_inode);
10365 + /* We don't want to leave negative leftover dentries for revalidate. */
10366 + if (!err && (dbopaque(dentry) != -1))
10367 + update_bstart(dentry);
10372 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10375 + struct inode *inode = dentry->d_inode;
10376 + struct dentry *parent;
10379 + BUG_ON(S_ISDIR(inode->i_mode));
10380 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10381 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10382 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10384 + valid = __unionfs_d_revalidate(dentry, parent, false);
10385 + if (unlikely(!valid)) {
10389 + unionfs_check_dentry(dentry);
10391 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10392 + /* call d_drop so the system "forgets" about us */
10394 + unionfs_postcopyup_release(dentry);
10395 + unionfs_postcopyup_setmnt(parent);
10396 + if (inode->i_nlink == 0) /* drop lower inodes */
10397 + iput_lowers_all(inode, false);
10400 + * if unlink/whiteout succeeded, parent dir mtime has
10403 + unionfs_copy_attr_times(dir);
10408 + unionfs_check_dentry(dentry);
10409 + unionfs_check_inode(dir);
10411 + unionfs_unlock_dentry(dentry);
10412 + unionfs_unlock_parent(dentry, parent);
10413 + unionfs_read_unlock(dentry->d_sb);
10417 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10418 + struct unionfs_dir_state *namelist)
10421 + struct dentry *lower_dentry;
10422 + struct dentry *lower_dir_dentry = NULL;
10424 + /* Here we need to remove whiteout entries. */
10425 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10429 + lower_dentry = unionfs_lower_dentry(dentry);
10431 + lower_dir_dentry = lock_parent(lower_dentry);
10433 + /* avoid destroying the lower inode if the file is in use */
10434 + dget(lower_dentry);
10435 + err = is_robranch(dentry);
10437 + /* see Documentation/filesystems/unionfs/issues.txt */
10439 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10442 + dput(lower_dentry);
10444 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10445 + /* propagate number of hard-links */
10446 + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10449 + if (lower_dir_dentry)
10450 + unlock_dir(lower_dir_dentry);
10454 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10457 + struct unionfs_dir_state *namelist = NULL;
10458 + struct dentry *parent;
10459 + int dstart, dend;
10462 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10463 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10464 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10466 + valid = __unionfs_d_revalidate(dentry, parent, false);
10467 + if (unlikely(!valid)) {
10471 + unionfs_check_dentry(dentry);
10473 + /* check if this unionfs directory is empty or not */
10474 + err = check_empty(dentry, parent, &namelist);
10478 + err = unionfs_rmdir_first(dir, dentry, namelist);
10479 + dstart = dbstart(dentry);
10480 + dend = dbend(dentry);
10482 + * We create a whiteout for the directory if there was an error to
10483 + * rmdir the first directory entry in the union. Otherwise, we
10484 + * create a whiteout only if there is no chance that a lower
10485 + * priority branch might also have the same named directory. IOW,
10486 + * if there is not another same-named directory at a lower priority
10487 + * branch, then we don't need to create a whiteout for it.
10490 + if (dstart < dend)
10491 + err = create_whiteout(dentry, dstart);
10498 + /* exit if the error returned was NOT -EROFS */
10499 + if (!IS_COPYUP_ERR(err))
10502 + new_err = create_whiteout(dentry, dstart - 1);
10503 + if (new_err != -EEXIST)
10509 + * Drop references to lower dentry/inode so storage space for them
10510 + * can be reclaimed. Then, call d_drop so the system "forgets"
10514 + iput_lowers_all(dentry->d_inode, false);
10515 + dput(unionfs_lower_dentry_idx(dentry, dstart));
10516 + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10518 + /* update our lower vfsmnts, in case a copyup took place */
10519 + unionfs_postcopyup_setmnt(dentry);
10520 + unionfs_check_dentry(dentry);
10521 + unionfs_check_inode(dir);
10525 + free_rdstate(namelist);
10527 + unionfs_unlock_dentry(dentry);
10528 + unionfs_unlock_parent(dentry, parent);
10529 + unionfs_read_unlock(dentry->d_sb);
10532 diff --git a/fs/unionfs/whiteout.c b/fs/unionfs/whiteout.c
10533 new file mode 100644
10534 index 0000000..626006a
10536 +++ b/fs/unionfs/whiteout.c
10539 + * Copyright (c) 2003-2009 Erez Zadok
10540 + * Copyright (c) 2003-2006 Charles P. Wright
10541 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10542 + * Copyright (c) 2005-2006 Junjiro Okajima
10543 + * Copyright (c) 2005 Arun M. Krishnakumar
10544 + * Copyright (c) 2004-2006 David P. Quigley
10545 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10546 + * Copyright (c) 2003 Puja Gupta
10547 + * Copyright (c) 2003 Harikesavan Krishnan
10548 + * Copyright (c) 2003-2009 Stony Brook University
10549 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10551 + * This program is free software; you can redistribute it and/or modify
10552 + * it under the terms of the GNU General Public License version 2 as
10553 + * published by the Free Software Foundation.
10556 +#include "union.h"
10559 + * whiteout and opaque directory helpers
10562 +/* What do we use for whiteouts. */
10563 +#define UNIONFS_WHPFX ".wh."
10564 +#define UNIONFS_WHLEN 4
10566 + * If a directory contains this file, then it is opaque. We start with the
10567 + * .wh. flag so that it is blocked by lookup.
10569 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10570 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10572 +/* construct whiteout filename */
10573 +char *alloc_whname(const char *name, int len)
10577 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10578 + if (unlikely(!buf))
10579 + return ERR_PTR(-ENOMEM);
10581 + strcpy(buf, UNIONFS_WHPFX);
10582 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10588 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10589 + * code in one place.
10591 +void unionfs_set_max_namelen(long *namelen)
10593 + *namelen -= UNIONFS_WHLEN;
10596 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10597 +bool is_whiteout_name(char **namep, int *namelenp)
10599 + if (*namelenp > UNIONFS_WHLEN &&
10600 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10601 + *namep += UNIONFS_WHLEN;
10602 + *namelenp -= UNIONFS_WHLEN;
10608 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10609 +bool is_validname(const char *name)
10611 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10613 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10614 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10620 + * Look for a whiteout @name in @lower_parent directory. If error, return
10621 + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10623 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10624 + * free then re-malloc calls. Need to provide a different API for those
10627 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10629 + char *whname = NULL;
10630 + int err = 0, namelen;
10631 + struct dentry *wh_dentry = NULL;
10633 + namelen = strlen(name);
10634 + whname = alloc_whname(name, namelen);
10635 + if (unlikely(IS_ERR(whname))) {
10636 + err = PTR_ERR(whname);
10640 + /* check if whiteout exists in this branch: lookup .wh.foo */
10641 + wh_dentry = lookup_lck_len(whname, lower_parent, strlen(whname));
10642 + if (IS_ERR(wh_dentry)) {
10643 + err = PTR_ERR(wh_dentry);
10647 + /* check if negative dentry (ENOENT) */
10648 + if (!wh_dentry->d_inode)
10651 + /* whiteout found: check if valid type */
10652 + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10653 + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10654 + whname, wh_dentry->d_inode->i_mode);
10663 + wh_dentry = ERR_PTR(err);
10664 + return wh_dentry;
10667 +/* find and return first whiteout in parent directory, else ENOENT */
10668 +struct dentry *find_first_whiteout(struct dentry *dentry)
10670 + int bindex, bstart, bend;
10671 + struct dentry *parent, *lower_parent, *wh_dentry;
10673 + parent = dget_parent(dentry);
10675 + bstart = dbstart(parent);
10676 + bend = dbend(parent);
10677 + wh_dentry = ERR_PTR(-ENOENT);
10679 + for (bindex = bstart; bindex <= bend; bindex++) {
10680 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10681 + if (!lower_parent)
10683 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10684 + if (IS_ERR(wh_dentry))
10686 + if (wh_dentry->d_inode)
10689 + wh_dentry = ERR_PTR(-ENOENT);
10694 + return wh_dentry;
10698 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10699 + * release dentry reference.
10701 +int unlink_whiteout(struct dentry *wh_dentry)
10704 + struct dentry *lower_dir_dentry;
10706 + /* dget and lock parent dentry */
10707 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10709 + /* see Documentation/filesystems/unionfs/issues.txt */
10711 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10713 + unlock_dir(lower_dir_dentry);
10716 + * Whiteouts are special files and should be deleted no matter what
10717 + * (as if they never existed), in order to allow this create
10718 + * operation to succeed. This is especially important in sticky
10719 + * directories: a whiteout may have been created by one user, but
10720 + * the newly created file may be created by another user.
10721 + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10722 + * above failed, then we have to try to directly unlink the
10723 + * whiteout. Note: in the ODF version of unionfs, whiteout are
10724 + * handled much more cleanly.
10726 + if (err == -EPERM) {
10727 + struct inode *inode = lower_dir_dentry->d_inode;
10728 + err = inode->i_op->unlink(inode, wh_dentry);
10731 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10732 + "err = %d\n", wh_dentry->d_name.name, err);
10739 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10740 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10741 + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10742 + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10743 + * If it finds both a regular file and a whiteout, return -EIO (this should
10746 + * Return 0 if no whiteout was found. Return 1 if one was found and
10747 + * successfully removed. Therefore a value >= 0 tells the caller that
10748 + * @lower_dentry belongs to a good branch to create the new object in).
10749 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10750 + * unlink the whiteout.
10752 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10756 + struct dentry *wh_dentry = NULL;
10757 + struct dentry *lower_dir_dentry = NULL;
10759 + /* look for whiteout dentry first */
10760 + lower_dir_dentry = dget_parent(lower_dentry);
10761 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10762 + dput(lower_dir_dentry);
10763 + if (IS_ERR(wh_dentry)) {
10764 + err = PTR_ERR(wh_dentry);
10768 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10773 + /* check if regular file and whiteout were both found */
10774 + if (unlikely(lower_dentry->d_inode)) {
10776 + printk(KERN_ERR "unionfs: found both whiteout and regular "
10777 + "file in directory %s (branch %d)\n",
10778 + lower_dir_dentry->d_name.name, bindex);
10782 + /* check if branch is writeable */
10783 + err = is_robranch_super(dentry->d_sb, bindex);
10787 + /* .wh.foo has been found, so let's unlink it */
10788 + err = unlink_whiteout(wh_dentry);
10790 + err = 1; /* a whiteout was found and successfully removed */
10798 + * Pass an unionfs dentry and an index. It will try to create a whiteout
10799 + * for the filename in dentry, and will try in branch 'index'. On error,
10800 + * it will proceed to a branch to the left.
10802 +int create_whiteout(struct dentry *dentry, int start)
10804 + int bstart, bend, bindex;
10805 + struct dentry *lower_dir_dentry;
10806 + struct dentry *lower_dentry;
10807 + struct dentry *lower_wh_dentry;
10808 + struct nameidata nd;
10809 + char *name = NULL;
10810 + int err = -EINVAL;
10812 + verify_locked(dentry);
10814 + bstart = dbstart(dentry);
10815 + bend = dbend(dentry);
10817 + /* create dentry's whiteout equivalent */
10818 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10819 + if (unlikely(IS_ERR(name))) {
10820 + err = PTR_ERR(name);
10824 + for (bindex = start; bindex >= 0; bindex--) {
10825 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10827 + if (!lower_dentry) {
10829 + * if lower dentry is not present, create the
10830 + * entire lower dentry directory structure and go
10831 + * ahead. Since we want to just create whiteout, we
10832 + * only want the parent dentry, and hence get rid of
10835 + lower_dentry = create_parents(dentry->d_inode,
10837 + dentry->d_name.name,
10839 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10840 + int ret = PTR_ERR(lower_dentry);
10841 + if (!IS_COPYUP_ERR(ret))
10843 + "unionfs: create_parents for "
10844 + "whiteout failed: bindex=%d "
10845 + "err=%d\n", bindex, ret);
10850 + lower_wh_dentry =
10851 + lookup_lck_len(name, lower_dentry->d_parent,
10852 + dentry->d_name.len + UNIONFS_WHLEN);
10853 + if (IS_ERR(lower_wh_dentry))
10857 + * The whiteout already exists. This used to be impossible,
10858 + * but now is possible because of opaqueness.
10860 + if (lower_wh_dentry->d_inode) {
10861 + dput(lower_wh_dentry);
10866 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10867 + if (unlikely(err < 0))
10869 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10870 + err = is_robranch_super(dentry->d_sb, bindex);
10872 + err = vfs_create(lower_dir_dentry->d_inode,
10874 + current_umask() & S_IRUGO,
10876 + unlock_dir(lower_dir_dentry);
10877 + dput(lower_wh_dentry);
10878 + release_lower_nd(&nd, err);
10880 + if (!err || !IS_COPYUP_ERR(err))
10884 + /* set dbopaque so that lookup will not proceed after this branch */
10886 + dbopaque(dentry) = bindex;
10894 + * Delete all of the whiteouts in a given directory for rmdir.
10896 + * lower directory inode should be locked
10898 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10899 + struct unionfs_dir_state *namelist)
10902 + struct dentry *lower_dir_dentry = NULL;
10903 + struct dentry *lower_dentry;
10904 + char *name = NULL, *p;
10905 + struct inode *lower_dir;
10907 + struct list_head *pos;
10908 + struct filldir_node *cursor;
10910 + /* Find out lower parent dentry */
10911 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10912 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10913 + lower_dir = lower_dir_dentry->d_inode;
10914 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10917 + name = __getname();
10918 + if (unlikely(!name))
10920 + strcpy(name, UNIONFS_WHPFX);
10921 + p = name + UNIONFS_WHLEN;
10924 + for (i = 0; !err && i < namelist->size; i++) {
10925 + list_for_each(pos, &namelist->list[i]) {
10927 + list_entry(pos, struct filldir_node,
10929 + /* Only operate on whiteouts in this branch. */
10930 + if (cursor->bindex != bindex)
10932 + if (!cursor->whiteout)
10935 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10937 + lookup_lck_len(name, lower_dir_dentry,
10938 + cursor->namelen +
10940 + if (IS_ERR(lower_dentry)) {
10941 + err = PTR_ERR(lower_dentry);
10944 + if (lower_dentry->d_inode)
10945 + err = vfs_unlink(lower_dir, lower_dentry);
10946 + dput(lower_dentry);
10954 + /* After all of the removals, we should copy the attributes once. */
10955 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10962 +void __delete_whiteouts(struct work_struct *work)
10964 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10965 + struct deletewh_args *d = &args->deletewh;
10967 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10968 + complete(&args->comp);
10971 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10972 +int delete_whiteouts(struct dentry *dentry, int bindex,
10973 + struct unionfs_dir_state *namelist)
10976 + struct super_block *sb;
10977 + struct dentry *lower_dir_dentry;
10978 + struct inode *lower_dir;
10979 + struct sioq_args args;
10981 + sb = dentry->d_sb;
10983 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10984 + BUG_ON(bindex < dbstart(dentry));
10985 + BUG_ON(bindex > dbend(dentry));
10986 + err = is_robranch_super(sb, bindex);
10990 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10991 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10992 + lower_dir = lower_dir_dentry->d_inode;
10993 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10995 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10996 + err = do_delete_whiteouts(dentry, bindex, namelist);
10998 + args.deletewh.namelist = namelist;
10999 + args.deletewh.dentry = dentry;
11000 + args.deletewh.bindex = bindex;
11001 + run_sioq(__delete_whiteouts, &args);
11009 +/****************************************************************************
11010 + * Opaque directory helpers *
11011 + ****************************************************************************/
11014 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
11015 + * -errno if an error occurred trying to figure this out.
11017 +int is_opaque_dir(struct dentry *dentry, int bindex)
11020 + struct dentry *lower_dentry;
11021 + struct dentry *wh_lower_dentry;
11022 + struct inode *lower_inode;
11023 + struct sioq_args args;
11025 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11026 + lower_inode = lower_dentry->d_inode;
11028 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
11030 + mutex_lock(&lower_inode->i_mutex);
11032 + if (!inode_permission(lower_inode, MAY_EXEC)) {
11033 + wh_lower_dentry =
11034 + lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
11035 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11037 + args.is_opaque.dentry = lower_dentry;
11038 + run_sioq(__is_opaque_dir, &args);
11039 + wh_lower_dentry = args.ret;
11042 + mutex_unlock(&lower_inode->i_mutex);
11044 + if (IS_ERR(wh_lower_dentry)) {
11045 + err = PTR_ERR(wh_lower_dentry);
11049 + /* This is an opaque dir iff wh_lower_dentry is positive */
11050 + err = !!wh_lower_dentry->d_inode;
11052 + dput(wh_lower_dentry);
11057 +void __is_opaque_dir(struct work_struct *work)
11059 + struct sioq_args *args = container_of(work, struct sioq_args, work);
11061 + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
11062 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11063 + complete(&args->comp);
11066 +int make_dir_opaque(struct dentry *dentry, int bindex)
11069 + struct dentry *lower_dentry, *diropq;
11070 + struct inode *lower_dir;
11071 + struct nameidata nd;
11072 + const struct cred *old_creds;
11073 + struct cred *new_creds;
11076 + * Opaque directory whiteout markers are special files (like regular
11077 + * whiteouts), and should appear to the users as if they don't
11078 + * exist. They should be created/deleted regardless of directory
11079 + * search/create permissions, but only for the duration of this
11080 + * creation of the .wh.__dir_opaque: file. Note, this does not
11081 + * circumvent normal ->permission).
11083 + new_creds = prepare_creds();
11084 + if (unlikely(!new_creds)) {
11088 + cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
11089 + cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
11090 + old_creds = override_creds(new_creds);
11092 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11093 + lower_dir = lower_dentry->d_inode;
11094 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
11095 + !S_ISDIR(lower_dir->i_mode));
11097 + mutex_lock(&lower_dir->i_mutex);
11098 + diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
11099 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11100 + if (IS_ERR(diropq)) {
11101 + err = PTR_ERR(diropq);
11105 + err = init_lower_nd(&nd, LOOKUP_CREATE);
11106 + if (unlikely(err < 0))
11108 + if (!diropq->d_inode)
11109 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
11111 + dbopaque(dentry) = bindex;
11112 + release_lower_nd(&nd, err);
11117 + mutex_unlock(&lower_dir->i_mutex);
11118 + revert_creds(old_creds);
11122 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
11123 new file mode 100644
11124 index 0000000..af72cca
11126 +++ b/fs/unionfs/xattr.c
11129 + * Copyright (c) 2003-2009 Erez Zadok
11130 + * Copyright (c) 2003-2006 Charles P. Wright
11131 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11132 + * Copyright (c) 2005-2006 Junjiro Okajima
11133 + * Copyright (c) 2005 Arun M. Krishnakumar
11134 + * Copyright (c) 2004-2006 David P. Quigley
11135 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
11136 + * Copyright (c) 2003 Puja Gupta
11137 + * Copyright (c) 2003 Harikesavan Krishnan
11138 + * Copyright (c) 2003-2009 Stony Brook University
11139 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11141 + * This program is free software; you can redistribute it and/or modify
11142 + * it under the terms of the GNU General Public License version 2 as
11143 + * published by the Free Software Foundation.
11146 +#include "union.h"
11148 +/* This is lifted from fs/xattr.c */
11149 +void *unionfs_xattr_alloc(size_t size, size_t limit)
11153 + if (size > limit)
11154 + return ERR_PTR(-E2BIG);
11156 + if (!size) /* size request, no buffer is needed */
11159 + ptr = kmalloc(size, GFP_KERNEL);
11160 + if (unlikely(!ptr))
11161 + return ERR_PTR(-ENOMEM);
11166 + * BKL held by caller.
11167 + * dentry->d_inode->i_mutex locked
11169 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11172 + struct dentry *lower_dentry = NULL;
11173 + struct dentry *parent;
11174 + int err = -EOPNOTSUPP;
11177 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11178 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11179 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11181 + valid = __unionfs_d_revalidate(dentry, parent, false);
11182 + if (unlikely(!valid)) {
11187 + lower_dentry = unionfs_lower_dentry(dentry);
11189 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11192 + unionfs_check_dentry(dentry);
11193 + unionfs_unlock_dentry(dentry);
11194 + unionfs_unlock_parent(dentry, parent);
11195 + unionfs_read_unlock(dentry->d_sb);
11200 + * BKL held by caller.
11201 + * dentry->d_inode->i_mutex locked
11203 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11204 + const void *value, size_t size, int flags)
11206 + struct dentry *lower_dentry = NULL;
11207 + struct dentry *parent;
11208 + int err = -EOPNOTSUPP;
11211 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11212 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11213 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11215 + valid = __unionfs_d_revalidate(dentry, parent, false);
11216 + if (unlikely(!valid)) {
11221 + lower_dentry = unionfs_lower_dentry(dentry);
11223 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11227 + unionfs_check_dentry(dentry);
11228 + unionfs_unlock_dentry(dentry);
11229 + unionfs_unlock_parent(dentry, parent);
11230 + unionfs_read_unlock(dentry->d_sb);
11235 + * BKL held by caller.
11236 + * dentry->d_inode->i_mutex locked
11238 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11240 + struct dentry *lower_dentry = NULL;
11241 + struct dentry *parent;
11242 + int err = -EOPNOTSUPP;
11245 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11246 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11247 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11249 + valid = __unionfs_d_revalidate(dentry, parent, false);
11250 + if (unlikely(!valid)) {
11255 + lower_dentry = unionfs_lower_dentry(dentry);
11257 + err = vfs_removexattr(lower_dentry, (char *) name);
11260 + unionfs_check_dentry(dentry);
11261 + unionfs_unlock_dentry(dentry);
11262 + unionfs_unlock_parent(dentry, parent);
11263 + unionfs_read_unlock(dentry->d_sb);
11268 + * BKL held by caller.
11269 + * dentry->d_inode->i_mutex locked
11271 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11273 + struct dentry *lower_dentry = NULL;
11274 + struct dentry *parent;
11275 + int err = -EOPNOTSUPP;
11276 + char *encoded_list = NULL;
11279 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11280 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11281 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11283 + valid = __unionfs_d_revalidate(dentry, parent, false);
11284 + if (unlikely(!valid)) {
11289 + lower_dentry = unionfs_lower_dentry(dentry);
11291 + encoded_list = list;
11292 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11295 + unionfs_check_dentry(dentry);
11296 + unionfs_unlock_dentry(dentry);
11297 + unionfs_unlock_parent(dentry, parent);
11298 + unionfs_read_unlock(dentry->d_sb);
11301 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
11302 index bb516ce..64f1ced 100644
11303 --- a/include/linux/fs_stack.h
11304 +++ b/include/linux/fs_stack.h
11307 + * Copyright (c) 2006-2009 Erez Zadok
11308 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11309 + * Copyright (c) 2006-2009 Stony Brook University
11310 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11312 + * This program is free software; you can redistribute it and/or modify
11313 + * it under the terms of the GNU General Public License version 2 as
11314 + * published by the Free Software Foundation.
11317 #ifndef _LINUX_FS_STACK_H
11318 #define _LINUX_FS_STACK_H
11320 -/* This file defines generic functions used primarily by stackable
11322 + * This file defines generic functions used primarily by stackable
11323 * filesystems; none of these functions require i_mutex to be held.
11326 #include <linux/fs.h>
11328 /* externs for fs/stack.c */
11329 -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
11330 - int (*get_nlinks)(struct inode *));
11332 -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
11333 +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
11334 +extern void fsstack_copy_inode_size(struct inode *dst, struct inode *src);
11337 static inline void fsstack_copy_attr_atime(struct inode *dest,
11338 diff --git a/include/linux/magic.h b/include/linux/magic.h
11339 index 1923327..c3fc308 100644
11340 --- a/include/linux/magic.h
11341 +++ b/include/linux/magic.h
11343 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11344 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11346 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11348 #define SMB_SUPER_MAGIC 0x517B
11349 #define USBDEVICE_SUPER_MAGIC 0x9fa2
11350 #define CGROUP_SUPER_MAGIC 0x27e0eb
11351 diff --git a/include/linux/splice.h b/include/linux/splice.h
11352 index 18e7c7c..af56841 100644
11353 --- a/include/linux/splice.h
11354 +++ b/include/linux/splice.h
11355 @@ -81,5 +81,10 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
11356 struct splice_pipe_desc *);
11357 extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11358 splice_direct_actor *);
11359 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11360 + loff_t *ppos, size_t len, unsigned int flags);
11361 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11362 + struct pipe_inode_info *pipe, size_t len,
11363 + unsigned int flags);
11366 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
11367 new file mode 100644
11368 index 0000000..c84d97e
11370 +++ b/include/linux/union_fs.h
11373 + * Copyright (c) 2003-2009 Erez Zadok
11374 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11375 + * Copyright (c) 2003-2009 Stony Brook University
11376 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11378 + * This program is free software; you can redistribute it and/or modify
11379 + * it under the terms of the GNU General Public License version 2 as
11380 + * published by the Free Software Foundation.
11383 +#ifndef _LINUX_UNION_FS_H
11384 +#define _LINUX_UNION_FS_H
11387 + * DEFINITIONS FOR USER AND KERNEL CODE:
11389 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11390 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11392 +#endif /* _LINUX_UNIONFS_H */
11394 diff --git a/security/security.c b/security/security.c
11395 index dc7674f..aa3f061 100644
11396 --- a/security/security.c
11397 +++ b/security/security.c
11398 @@ -519,6 +519,7 @@ int security_inode_permission(struct inode *inode, int mask)
11400 return security_ops->inode_permission(inode, mask);
11402 +EXPORT_SYMBOL(security_inode_permission);
11404 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)