1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index 52cd611..bc6b437 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -106,6 +106,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 3fe4dc2..168cc4d 100644
538 @@ -4370,6 +4370,14 @@ L: linux-kernel@vger.kernel.org
539 W: http://www.kernel.dk
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)
552 M: dedekind@infradead.org
553 --- /dev/null 2007-02-13 17:29:53.000000000 +0100
554 +++ linux-2.6.29/fs/unionfs/Kconfig 2009-03-24 13:07:49.116382161 +0100
557 + tristate "Union file system (EXPERIMENTAL)"
558 + depends on EXPERIMENTAL
560 + Unionfs is a stackable unification file system, which appears to
561 + merge the contents of several directories (branches), while keeping
562 + their physical content separate.
564 + See <http://unionfs.filesystems.org> for details
566 +config UNION_FS_XATTR
567 + bool "Unionfs extended attributes"
568 + depends on UNION_FS
570 + Extended attributes are name:value pairs associated with inodes by
571 + the kernel or by users (see the attr(5) manual page).
575 +config UNION_FS_DEBUG
576 + bool "Debug Unionfs"
577 + depends on UNION_FS
579 + If you say Y here, you can turn on debugging output from Unionfs.
581 --- linux-2.6.29/fs/Kconfig~ 2009-03-24 13:06:08.609715416 +0100
582 +++ linux-2.6.29/fs/Kconfig 2009-03-24 13:08:40.063033558 +0100
584 source "fs/adfs/Kconfig"
585 source "fs/affs/Kconfig"
586 source "fs/ecryptfs/Kconfig"
587 +source "fs/unionfs/Kconfig"
588 source "fs/hfs/Kconfig"
589 source "fs/hfsplus/Kconfig"
590 source "fs/befs/Kconfig"
591 diff --git a/fs/Makefile b/fs/Makefile
592 diff --git a/fs/Makefile b/fs/Makefile
593 index 38bc735..a1b3bda 100644
596 @@ -85,6 +85,7 @@ obj-$(CONFIG_ISO9660_FS) += isofs/
597 obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
598 obj-$(CONFIG_HFS_FS) += hfs/
599 obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
600 +obj-$(CONFIG_UNION_FS) += unionfs/
601 obj-$(CONFIG_VXFS_FS) += freevxfs/
602 obj-$(CONFIG_NFS_FS) += nfs/
603 obj-$(CONFIG_EXPORTFS) += exportfs/
604 diff --git a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
605 index 5e59658..4621f89 100644
606 --- a/fs/ecryptfs/dentry.c
607 +++ b/fs/ecryptfs/dentry.c
608 @@ -62,7 +62,7 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
609 struct inode *lower_inode =
610 ecryptfs_inode_to_lower(dentry->d_inode);
612 - fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
613 + fsstack_copy_attr_all(dentry->d_inode, lower_inode);
617 diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
618 index 5697899..8a370a1 100644
619 --- a/fs/ecryptfs/inode.c
620 +++ b/fs/ecryptfs/inode.c
621 @@ -628,9 +628,9 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
622 lower_new_dir_dentry->d_inode, lower_new_dentry);
625 - fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
626 + fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
627 if (new_dir != old_dir)
628 - fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
629 + fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
631 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
632 dput(lower_new_dentry->d_parent);
633 @@ -952,7 +952,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
634 rc = notify_change(lower_dentry, ia);
635 mutex_unlock(&lower_dentry->d_inode->i_mutex);
637 - fsstack_copy_attr_all(inode, lower_inode, NULL);
638 + fsstack_copy_attr_all(inode, lower_inode);
642 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
643 index 789cf2e..7f82ed3 100644
644 --- a/fs/ecryptfs/main.c
645 +++ b/fs/ecryptfs/main.c
646 @@ -190,7 +190,7 @@ int ecryptfs_interpose(struct dentry *lo
647 init_special_inode(inode, lower_inode->i_mode,
648 lower_inode->i_rdev);
649 dentry->d_op = &ecryptfs_dops;
650 - fsstack_copy_attr_all(inode, lower_inode, NULL);
651 + fsstack_copy_attr_all(inode, lower_inode);
652 /* This size will be overwritten for real files w/ headers and
654 fsstack_copy_inode_size(inode, lower_inode);
655 diff --git a/fs/namei.c b/fs/namei.c
656 index bbc15c2..7983d41 100644
659 @@ -373,6 +373,7 @@ void release_open_intent(struct nameidata *nd)
661 fput(nd->intent.open.file);
663 +EXPORT_SYMBOL_GPL(release_open_intent);
665 static inline struct dentry *
666 do_revalidate(struct dentry *dentry, struct nameidata *nd)
667 diff --git a/fs/splice.c b/fs/splice.c
668 index 4ed0ba4..6637e35 100644
671 @@ -888,8 +888,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
673 * Attempt to initiate a splice from pipe to file.
675 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
676 - loff_t *ppos, size_t len, unsigned int flags)
677 +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
678 + loff_t *ppos, size_t len, unsigned int flags)
682 @@ -908,13 +908,14 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
684 return out->f_op->splice_write(pipe, out, ppos, len, flags);
686 +EXPORT_SYMBOL_GPL(vfs_splice_from);
689 * Attempt to initiate a splice from a file to a pipe.
691 -static long do_splice_to(struct file *in, loff_t *ppos,
692 - struct pipe_inode_info *pipe, size_t len,
693 - unsigned int flags)
694 +long vfs_splice_to(struct file *in, loff_t *ppos,
695 + struct pipe_inode_info *pipe, size_t len,
696 + unsigned int flags)
700 @@ -930,6 +931,7 @@ static long do_splice_to(struct file *in, loff_t *ppos,
702 return in->f_op->splice_read(in, ppos, pipe, len, flags);
704 +EXPORT_SYMBOL_GPL(vfs_splice_to);
707 * splice_direct_to_actor - splices data directly between two non-pipes
708 @@ -999,7 +1001,7 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
710 loff_t pos = sd->pos, prev_pos = pos;
712 - ret = do_splice_to(in, &pos, pipe, len, flags);
713 + ret = vfs_splice_to(in, &pos, pipe, len, flags);
714 if (unlikely(ret <= 0))
717 @@ -1058,7 +1060,7 @@ static int direct_splice_actor(struct pipe_inode_info *pipe,
719 struct file *file = sd->u.file;
721 - return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
722 + return vfs_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
726 @@ -1132,7 +1134,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
730 - ret = do_splice_from(ipipe, out, off, len, flags);
731 + ret = vfs_splice_from(ipipe, out, off, len, flags);
733 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
735 @@ -1153,7 +1155,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
739 - ret = do_splice_to(in, off, opipe, len, flags);
740 + ret = vfs_splice_to(in, off, opipe, len, flags);
742 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
744 diff --git a/fs/stack.c b/fs/stack.c
745 index 67716f6..cc1443d 100644
750 + * Copyright (c) 2006-2009 Erez Zadok
751 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
752 + * Copyright (c) 2006-2009 Stony Brook University
753 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
755 + * This program is free software; you can redistribute it and/or modify
756 + * it under the terms of the GNU General Public License version 2 as
757 + * published by the Free Software Foundation.
760 #include <linux/module.h>
761 #include <linux/fs.h>
762 #include <linux/fs_stack.h>
764 -/* does _NOT_ require i_mutex to be held.
766 + * does _NOT_ require i_mutex to be held.
768 * This function cannot be inlined since i_size_{read,write} is rather
769 * heavy-weight on 32-bit systems
771 -void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
772 +void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
774 - i_size_write(dst, i_size_read((struct inode *)src));
775 - dst->i_blocks = src->i_blocks;
780 + * i_size_read() includes its own seqlocking and protection from
781 + * preemption (see include/linux/fs.h): we need nothing extra for
782 + * that here, and prefer to avoid nesting locks than attempt to
783 + * keep i_size and i_blocks in synch together.
785 + i_size = i_size_read(src);
788 + * But if CONFIG_LSF (on 32-bit), we ought to make an effort to keep
789 + * the two halves of i_blocks in synch despite SMP or PREEMPT - though
790 + * stat's generic_fillattr() doesn't bother, and we won't be applying
791 + * quotas (where i_blocks does become important) at the upper level.
793 + * We don't actually know what locking is used at the lower level; but
794 + * if it's a filesystem that supports quotas, it will be using i_lock
795 + * as in inode_add_bytes(). tmpfs uses other locking, and its 32-bit
796 + * is (just) able to exceed 2TB i_size with the aid of holes; but its
797 + * i_blocks cannot carry into the upper long without almost 2TB swap -
798 + * let's ignore that case.
800 + if (sizeof(i_blocks) > sizeof(long))
801 + spin_lock(&src->i_lock);
802 + i_blocks = src->i_blocks;
803 + if (sizeof(i_blocks) > sizeof(long))
804 + spin_unlock(&src->i_lock);
807 + * If CONFIG_SMP on 32-bit, it's vital for fsstack_copy_inode_size()
808 + * to hold some lock around i_size_write(), otherwise i_size_read()
809 + * may spin forever (see include/linux/fs.h). We don't necessarily
810 + * hold i_mutex when this is called, so take i_lock for that case.
812 + * And if CONFIG_LSF (on 32-bit), continue our effort to keep the
813 + * two halves of i_blocks in synch despite SMP or PREEMPT: use i_lock
814 + * for that case too, and do both at once by combining the tests.
816 + * There is none of this locking overhead in the 64-bit case.
818 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
819 + spin_lock(&dst->i_lock);
820 + i_size_write(dst, i_size);
821 + dst->i_blocks = i_blocks;
822 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
823 + spin_unlock(&dst->i_lock);
825 EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
827 -/* copy all attributes; get_nlinks is optional way to override the i_nlink
829 + * copy all attributes; get_nlinks is optional way to override the i_nlink
832 -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
833 - int (*get_nlinks)(struct inode *))
834 +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
836 dest->i_mode = src->i_mode;
837 dest->i_uid = src->i_uid;
838 @@ -29,14 +87,6 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
839 dest->i_ctime = src->i_ctime;
840 dest->i_blkbits = src->i_blkbits;
841 dest->i_flags = src->i_flags;
844 - * Update the nlinks AFTER updating the above fields, because the
845 - * get_links callback may depend on them.
848 - dest->i_nlink = src->i_nlink;
850 - dest->i_nlink = (*get_nlinks)(dest);
851 + dest->i_nlink = src->i_nlink;
853 EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
854 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
856 index 0000000..27a4781
858 +++ b/fs/unionfs/Makefile
860 +UNIONFS_VERSION="2.5.1 (for 2.6.29-rc2)"
862 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
864 +obj-$(CONFIG_UNION_FS) += unionfs.o
866 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
867 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
868 + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
870 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
872 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
874 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
875 +EXTRA_CFLAGS += -DDEBUG
877 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
879 index 0000000..e9dc23c
881 +++ b/fs/unionfs/commonfops.c
884 + * Copyright (c) 2003-2009 Erez Zadok
885 + * Copyright (c) 2003-2006 Charles P. Wright
886 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
887 + * Copyright (c) 2005-2006 Junjiro Okajima
888 + * Copyright (c) 2005 Arun M. Krishnakumar
889 + * Copyright (c) 2004-2006 David P. Quigley
890 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
891 + * Copyright (c) 2003 Puja Gupta
892 + * Copyright (c) 2003 Harikesavan Krishnan
893 + * Copyright (c) 2003-2009 Stony Brook University
894 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
896 + * This program is free software; you can redistribute it and/or modify
897 + * it under the terms of the GNU General Public License version 2 as
898 + * published by the Free Software Foundation.
904 + * 1) Copyup the file
905 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
906 + * stolen from NFS's silly rename
908 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
909 + struct dentry *parent, int bstart, int bindex)
911 + static unsigned int counter;
912 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
913 + const int countersize = sizeof(counter) * 2;
914 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
915 + char name[nlen + 1];
917 + struct dentry *tmp_dentry = NULL;
918 + struct dentry *lower_dentry;
919 + struct dentry *lower_dir_dentry = NULL;
921 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
923 + sprintf(name, ".unionfs%*.*lx",
924 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
927 + * Loop, looking for an unused temp name to copyup to.
929 + * It's somewhat silly that we look for a free temp tmp name in the
930 + * source branch (bstart) instead of the dest branch (bindex), where
931 + * the final name will be created. We _will_ catch it if somehow
932 + * the name exists in the dest branch, but it'd be nice to catch it
933 + * sooner than later.
938 + char *suffix = name + nlen - countersize;
942 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
944 + pr_debug("unionfs: trying to rename %s to %s\n",
945 + dentry->d_name.name, name);
947 + tmp_dentry = lookup_one_len(name, lower_dentry->d_parent,
949 + if (IS_ERR(tmp_dentry)) {
950 + err = PTR_ERR(tmp_dentry);
953 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
956 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
957 + i_size_read(file->f_path.dentry->d_inode));
959 + if (unlikely(err == -EEXIST))
964 + /* bring it to the same state as an unlinked file */
965 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
966 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
967 + atomic_inc(&lower_dentry->d_inode->i_count);
968 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
969 + lower_dentry->d_inode);
971 + lower_dir_dentry = lock_parent(lower_dentry);
972 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
973 + unlock_dir(lower_dir_dentry);
977 + unionfs_check_dentry(dentry);
982 + * put all references held by upper struct file and free lower file pointer
985 +static void cleanup_file(struct file *file)
987 + int bindex, bstart, bend;
988 + struct file **lower_files;
989 + struct file *lower_file;
990 + struct super_block *sb = file->f_path.dentry->d_sb;
992 + lower_files = UNIONFS_F(file)->lower_files;
993 + bstart = fbstart(file);
994 + bend = fbend(file);
996 + for (bindex = bstart; bindex <= bend; bindex++) {
997 + int i; /* holds (possibly) updated branch index */
1000 + lower_file = unionfs_lower_file_idx(file, bindex);
1005 + * Find new index of matching branch with an open
1006 + * file, since branches could have been added or
1007 + * deleted causing the one with open files to shift.
1009 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
1010 + i = branch_id_to_idx(sb, old_bid);
1011 + if (unlikely(i < 0)) {
1012 + printk(KERN_ERR "unionfs: no superblock for "
1013 + "file %p\n", file);
1017 + /* decrement count of open files */
1020 + * fput will perform an mntput for us on the correct branch.
1021 + * Although we're using the file's old branch configuration,
1022 + * bindex, which is the old index, correctly points to the
1023 + * right branch in the file's branch list. In other words,
1024 + * we're going to mntput the correct branch even if branches
1025 + * have been added/removed.
1028 + UNIONFS_F(file)->lower_files[bindex] = NULL;
1029 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
1032 + UNIONFS_F(file)->lower_files = NULL;
1033 + kfree(lower_files);
1034 + kfree(UNIONFS_F(file)->saved_branch_ids);
1035 + /* set to NULL because caller needs to know if to kfree on error */
1036 + UNIONFS_F(file)->saved_branch_ids = NULL;
1039 +/* open all lower files for a given file */
1040 +static int open_all_files(struct file *file)
1042 + int bindex, bstart, bend, err = 0;
1043 + struct file *lower_file;
1044 + struct dentry *lower_dentry;
1045 + struct dentry *dentry = file->f_path.dentry;
1046 + struct super_block *sb = dentry->d_sb;
1048 + bstart = dbstart(dentry);
1049 + bend = dbend(dentry);
1051 + for (bindex = bstart; bindex <= bend; bindex++) {
1052 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1053 + if (!lower_dentry)
1056 + dget(lower_dentry);
1057 + unionfs_mntget(dentry, bindex);
1058 + branchget(sb, bindex);
1061 + dentry_open(lower_dentry,
1062 + unionfs_lower_mnt_idx(dentry, bindex),
1063 + file->f_flags, current_cred());
1064 + if (IS_ERR(lower_file)) {
1065 + branchput(sb, bindex);
1066 + err = PTR_ERR(lower_file);
1069 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1076 +/* open the highest priority file for a given upper file */
1077 +static int open_highest_file(struct file *file, bool willwrite)
1079 + int bindex, bstart, bend, err = 0;
1080 + struct file *lower_file;
1081 + struct dentry *lower_dentry;
1082 + struct dentry *dentry = file->f_path.dentry;
1083 + struct dentry *parent = dget_parent(dentry);
1084 + struct inode *parent_inode = parent->d_inode;
1085 + struct super_block *sb = dentry->d_sb;
1087 + bstart = dbstart(dentry);
1088 + bend = dbend(dentry);
1090 + lower_dentry = unionfs_lower_dentry(dentry);
1091 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
1092 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1093 + err = copyup_file(parent_inode, file, bstart, bindex,
1094 + i_size_read(dentry->d_inode));
1098 + atomic_set(&UNIONFS_F(file)->generation,
1099 + atomic_read(&UNIONFS_I(dentry->d_inode)->
1104 + dget(lower_dentry);
1105 + unionfs_mntget(dentry, bstart);
1106 + lower_file = dentry_open(lower_dentry,
1107 + unionfs_lower_mnt_idx(dentry, bstart),
1108 + file->f_flags, current_cred());
1109 + if (IS_ERR(lower_file)) {
1110 + err = PTR_ERR(lower_file);
1113 + branchget(sb, bstart);
1114 + unionfs_set_lower_file(file, lower_file);
1115 + /* Fix up the position. */
1116 + lower_file->f_pos = file->f_pos;
1118 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1124 +/* perform a delayed copyup of a read-write file on a read-only branch */
1125 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1127 + int bindex, bstart, bend, err = 0;
1128 + struct dentry *dentry = file->f_path.dentry;
1129 + struct inode *parent_inode = parent->d_inode;
1131 + bstart = fbstart(file);
1132 + bend = fbend(file);
1134 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1136 + unionfs_check_file(file);
1137 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1138 + if (!d_deleted(dentry))
1139 + err = copyup_file(parent_inode, file, bstart,
1141 + i_size_read(dentry->d_inode));
1143 + err = copyup_deleted_file(file, dentry, parent,
1145 + /* if succeeded, set lower open-file flags and break */
1147 + struct file *lower_file;
1148 + lower_file = unionfs_lower_file_idx(file, bindex);
1149 + lower_file->f_flags = file->f_flags;
1153 + if (err || (bstart <= fbstart(file)))
1155 + bend = fbend(file);
1156 + for (bindex = bstart; bindex <= bend; bindex++) {
1157 + if (unionfs_lower_file_idx(file, bindex)) {
1158 + branchput(dentry->d_sb, bindex);
1159 + fput(unionfs_lower_file_idx(file, bindex));
1160 + unionfs_set_lower_file_idx(file, bindex, NULL);
1163 + path_put_lowers(dentry, bstart, bend, false);
1164 + iput_lowers(dentry->d_inode, bstart, bend, false);
1165 + /* for reg file, we only open it "once" */
1166 + fbend(file) = fbstart(file);
1167 + dbend(dentry) = dbstart(dentry);
1168 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1171 + unionfs_check_file(file);
1176 + * Helper function for unionfs_file_revalidate/locked.
1177 + * Expects dentry/parent to be locked already, and revalidated.
1179 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1180 + struct dentry *parent,
1181 + struct super_block *sb, int sbgen,
1182 + int dgen, bool willwrite)
1185 + int bstart, bend, orig_brid;
1189 + fgen = atomic_read(&UNIONFS_F(file)->generation);
1192 + * There are two cases we are interested in. The first is if the
1193 + * generation is lower than the super-block. The second is if
1194 + * someone has copied up this file from underneath us, we also need
1195 + * to refresh things.
1197 + if (d_deleted(dentry) ||
1199 + dbstart(dentry) == fbstart(file) &&
1200 + unionfs_lower_file(file)))
1201 + goto out_may_copyup;
1203 + /* save orig branch ID */
1204 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1206 + /* First we throw out the existing files. */
1207 + cleanup_file(file);
1209 + /* Now we reopen the file(s) as in unionfs_open. */
1210 + bstart = fbstart(file) = dbstart(dentry);
1211 + bend = fbend(file) = dbend(dentry);
1213 + size = sizeof(struct file *) * sbmax(sb);
1214 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1215 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1219 + size = sizeof(int) * sbmax(sb);
1220 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1221 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1226 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1227 + /* We need to open all the files. */
1228 + err = open_all_files(file);
1233 + /* We only open the highest priority branch. */
1234 + err = open_highest_file(file, willwrite);
1237 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1238 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1240 + * If we re-opened the file on a different branch
1241 + * than the original one, and this was due to a new
1242 + * branch inserted, then update the mnt counts of
1243 + * the old and new branches accordingly.
1245 + unionfs_mntget(dentry, bstart);
1246 + unionfs_mntput(sb->s_root,
1247 + branch_id_to_idx(sb, orig_brid));
1249 + /* regular files have only one open lower file */
1250 + fbend(file) = fbstart(file);
1252 + atomic_set(&UNIONFS_F(file)->generation,
1253 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1256 + /* Copyup on the first write to a file on a readonly branch. */
1257 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1258 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1259 + is_robranch(dentry)) {
1260 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1261 + dentry->d_name.name);
1262 + err = do_delayed_copyup(file, parent);
1263 + /* regular files have only one open lower file */
1264 + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1265 + fbend(file) = fbstart(file);
1270 + kfree(UNIONFS_F(file)->lower_files);
1271 + kfree(UNIONFS_F(file)->saved_branch_ids);
1277 + * Revalidate the struct file
1278 + * @file: file to revalidate
1279 + * @parent: parent dentry (locked by caller)
1280 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1281 + * Caller must lock/unlock dentry's branch configuration.
1283 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1286 + struct super_block *sb;
1287 + struct dentry *dentry;
1291 + dentry = file->f_path.dentry;
1292 + sb = dentry->d_sb;
1293 + verify_locked(dentry);
1294 + verify_locked(parent);
1297 + * First revalidate the dentry inside struct file,
1298 + * but not unhashed dentries.
1300 + if (!d_deleted(dentry) &&
1301 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1306 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1307 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1309 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1310 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1311 + dentry->d_name.name);
1316 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1317 + sbgen, dgen, willwrite);
1322 +/* unionfs_open helper function: open a directory */
1323 +static int __open_dir(struct inode *inode, struct file *file)
1325 + struct dentry *lower_dentry;
1326 + struct file *lower_file;
1327 + int bindex, bstart, bend;
1328 + struct vfsmount *mnt;
1330 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1331 + bend = fbend(file) = dbend(file->f_path.dentry);
1333 + for (bindex = bstart; bindex <= bend; bindex++) {
1335 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1336 + if (!lower_dentry)
1339 + dget(lower_dentry);
1340 + unionfs_mntget(file->f_path.dentry, bindex);
1341 + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1342 + lower_file = dentry_open(lower_dentry, mnt, file->f_flags,
1344 + if (IS_ERR(lower_file))
1345 + return PTR_ERR(lower_file);
1347 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1350 + * The branchget goes after the open, because otherwise
1351 + * we would miss the reference on release.
1353 + branchget(inode->i_sb, bindex);
1359 +/* unionfs_open helper function: open a file */
1360 +static int __open_file(struct inode *inode, struct file *file,
1361 + struct dentry *parent)
1363 + struct dentry *lower_dentry;
1364 + struct file *lower_file;
1366 + int bindex, bstart, bend;
1368 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1369 + lower_flags = file->f_flags;
1371 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1372 + bend = fbend(file) = dbend(file->f_path.dentry);
1375 + * check for the permission for lower file. If the error is
1376 + * COPYUP_ERR, copyup the file.
1378 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1380 + * if the open will change the file, copy it up otherwise
1383 + if (lower_flags & O_TRUNC) {
1387 + /* copyup the file */
1388 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1389 + err = copyup_file(parent->d_inode, file,
1390 + bstart, bindex, size);
1397 + * turn off writeable flags, to force delayed copyup
1400 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1404 + dget(lower_dentry);
1407 + * dentry_open will decrement mnt refcnt if err.
1408 + * otherwise fput() will do an mntput() for us upon file close.
1410 + unionfs_mntget(file->f_path.dentry, bstart);
1412 + dentry_open(lower_dentry,
1413 + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1414 + lower_flags, current_cred());
1415 + if (IS_ERR(lower_file))
1416 + return PTR_ERR(lower_file);
1418 + unionfs_set_lower_file(file, lower_file);
1419 + branchget(inode->i_sb, bstart);
1424 +int unionfs_open(struct inode *inode, struct file *file)
1427 + struct file *lower_file = NULL;
1428 + struct dentry *dentry = file->f_path.dentry;
1429 + struct dentry *parent;
1430 + int bindex = 0, bstart = 0, bend = 0;
1434 + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1435 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1436 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1438 + /* don't open unhashed/deleted files */
1439 + if (d_deleted(dentry)) {
1444 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1445 + valid = __unionfs_d_revalidate(dentry, parent, false);
1446 + if (unlikely(!valid)) {
1451 + file->private_data =
1452 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1453 + if (unlikely(!UNIONFS_F(file))) {
1457 + fbstart(file) = -1;
1459 + atomic_set(&UNIONFS_F(file)->generation,
1460 + atomic_read(&UNIONFS_I(inode)->generation));
1462 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1463 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1464 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1468 + size = sizeof(int) * sbmax(inode->i_sb);
1469 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1470 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1475 + bstart = fbstart(file) = dbstart(dentry);
1476 + bend = fbend(file) = dbend(dentry);
1479 + * open all directories and make the unionfs file struct point to
1480 + * these lower file structs
1482 + if (S_ISDIR(inode->i_mode))
1483 + err = __open_dir(inode, file); /* open a dir */
1485 + err = __open_file(inode, file, parent); /* open a file */
1487 + /* freeing the allocated resources, and fput the opened files */
1489 + for (bindex = bstart; bindex <= bend; bindex++) {
1490 + lower_file = unionfs_lower_file_idx(file, bindex);
1494 + branchput(dentry->d_sb, bindex);
1495 + /* fput calls dput for lower_dentry */
1502 + kfree(UNIONFS_F(file)->lower_files);
1503 + kfree(UNIONFS_F(file)->saved_branch_ids);
1504 + kfree(UNIONFS_F(file));
1508 + unionfs_postcopyup_setmnt(dentry);
1509 + unionfs_copy_attr_times(inode);
1510 + unionfs_check_file(file);
1511 + unionfs_check_inode(inode);
1513 + unionfs_unlock_dentry(dentry);
1514 + unionfs_unlock_parent(dentry, parent);
1515 + unionfs_read_unlock(inode->i_sb);
1520 + * release all lower object references & free the file info structure
1522 + * No need to grab sb info's rwsem.
1524 +int unionfs_file_release(struct inode *inode, struct file *file)
1526 + struct file *lower_file = NULL;
1527 + struct unionfs_file_info *fileinfo;
1528 + struct unionfs_inode_info *inodeinfo;
1529 + struct super_block *sb = inode->i_sb;
1530 + struct dentry *dentry = file->f_path.dentry;
1531 + struct dentry *parent;
1532 + int bindex, bstart, bend;
1533 + int fgen, err = 0;
1535 + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1536 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1537 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1540 + * We try to revalidate, but the VFS ignores return return values
1541 + * from file->release, so we must always try to succeed here,
1542 + * including to do the kfree and dput below. So if revalidation
1543 + * failed, all we can do is print some message and keep going.
1545 + err = unionfs_file_revalidate(file, parent,
1546 + UNIONFS_F(file)->wrote_to_file);
1548 + unionfs_check_file(file);
1549 + fileinfo = UNIONFS_F(file);
1550 + BUG_ON(file->f_path.dentry->d_inode != inode);
1551 + inodeinfo = UNIONFS_I(inode);
1553 + /* fput all the lower files */
1554 + fgen = atomic_read(&fileinfo->generation);
1555 + bstart = fbstart(file);
1556 + bend = fbend(file);
1558 + for (bindex = bstart; bindex <= bend; bindex++) {
1559 + lower_file = unionfs_lower_file_idx(file, bindex);
1562 + unionfs_set_lower_file_idx(file, bindex, NULL);
1564 + branchput(sb, bindex);
1567 + /* if there are no more refs to the dentry, dput it */
1568 + if (d_deleted(dentry)) {
1569 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1570 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1574 + kfree(fileinfo->lower_files);
1575 + kfree(fileinfo->saved_branch_ids);
1577 + if (fileinfo->rdstate) {
1578 + fileinfo->rdstate->access = jiffies;
1579 + spin_lock(&inodeinfo->rdlock);
1580 + inodeinfo->rdcount++;
1581 + list_add_tail(&fileinfo->rdstate->cache,
1582 + &inodeinfo->readdircache);
1583 + mark_inode_dirty(inode);
1584 + spin_unlock(&inodeinfo->rdlock);
1585 + fileinfo->rdstate = NULL;
1589 + unionfs_unlock_dentry(dentry);
1590 + unionfs_unlock_parent(dentry, parent);
1591 + unionfs_read_unlock(sb);
1595 +/* pass the ioctl to the lower fs */
1596 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1598 + struct file *lower_file;
1601 + lower_file = unionfs_lower_file(file);
1604 + if (!lower_file || !lower_file->f_op)
1606 + if (lower_file->f_op->unlocked_ioctl) {
1607 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1608 + } else if (lower_file->f_op->ioctl) {
1610 + err = lower_file->f_op->ioctl(
1611 + lower_file->f_path.dentry->d_inode,
1612 + lower_file, cmd, arg);
1621 + * return to user-space the branch indices containing the file in question
1623 + * We use fd_set and therefore we are limited to the number of the branches
1624 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1626 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1627 + unsigned int cmd, unsigned long arg)
1630 + fd_set branchlist;
1631 + int bstart = 0, bend = 0, bindex = 0;
1632 + int orig_bstart, orig_bend;
1633 + struct dentry *dentry, *lower_dentry;
1634 + struct vfsmount *mnt;
1636 + dentry = file->f_path.dentry;
1637 + orig_bstart = dbstart(dentry);
1638 + orig_bend = dbend(dentry);
1639 + err = unionfs_partial_lookup(dentry, parent);
1642 + bstart = dbstart(dentry);
1643 + bend = dbend(dentry);
1645 + FD_ZERO(&branchlist);
1647 + for (bindex = bstart; bindex <= bend; bindex++) {
1648 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1649 + if (!lower_dentry)
1651 + if (likely(lower_dentry->d_inode))
1652 + FD_SET(bindex, &branchlist);
1653 + /* purge any lower objects after partial_lookup */
1654 + if (bindex < orig_bstart || bindex > orig_bend) {
1655 + dput(lower_dentry);
1656 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1657 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1658 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1660 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1663 + unionfs_mntput(dentry, bindex);
1664 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1667 + /* restore original dentry's offsets */
1668 + dbstart(dentry) = orig_bstart;
1669 + dbend(dentry) = orig_bend;
1670 + ibstart(dentry->d_inode) = orig_bstart;
1671 + ibend(dentry->d_inode) = orig_bend;
1673 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1674 + if (unlikely(err))
1678 + return err < 0 ? err : bend;
1681 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1684 + struct dentry *dentry = file->f_path.dentry;
1685 + struct dentry *parent;
1687 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1688 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1689 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1691 + err = unionfs_file_revalidate(file, parent, true);
1692 + if (unlikely(err))
1695 + /* check if asked for local commands */
1697 + case UNIONFS_IOCTL_INCGEN:
1698 + /* Increment the superblock generation count */
1699 + pr_info("unionfs: incgen ioctl deprecated; "
1700 + "use \"-o remount,incgen\"\n");
1704 + case UNIONFS_IOCTL_QUERYFILE:
1705 + /* Return list of branches containing the given file */
1706 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1710 + /* pass the ioctl down */
1711 + err = do_ioctl(file, cmd, arg);
1716 + unionfs_check_file(file);
1717 + unionfs_unlock_dentry(dentry);
1718 + unionfs_unlock_parent(dentry, parent);
1719 + unionfs_read_unlock(dentry->d_sb);
1723 +int unionfs_flush(struct file *file, fl_owner_t id)
1726 + struct file *lower_file = NULL;
1727 + struct dentry *dentry = file->f_path.dentry;
1728 + struct dentry *parent;
1729 + int bindex, bstart, bend;
1731 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1732 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1733 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1735 + err = unionfs_file_revalidate(file, parent,
1736 + UNIONFS_F(file)->wrote_to_file);
1737 + if (unlikely(err))
1739 + unionfs_check_file(file);
1741 + bstart = fbstart(file);
1742 + bend = fbend(file);
1743 + for (bindex = bstart; bindex <= bend; bindex++) {
1744 + lower_file = unionfs_lower_file_idx(file, bindex);
1746 + if (lower_file && lower_file->f_op &&
1747 + lower_file->f_op->flush) {
1748 + err = lower_file->f_op->flush(lower_file, id);
1757 + unionfs_check_file(file);
1758 + unionfs_unlock_dentry(dentry);
1759 + unionfs_unlock_parent(dentry, parent);
1760 + unionfs_read_unlock(dentry->d_sb);
1763 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1764 new file mode 100644
1765 index 0000000..3d0c0ca
1767 +++ b/fs/unionfs/copyup.c
1770 + * Copyright (c) 2003-2009 Erez Zadok
1771 + * Copyright (c) 2003-2006 Charles P. Wright
1772 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1773 + * Copyright (c) 2005-2006 Junjiro Okajima
1774 + * Copyright (c) 2005 Arun M. Krishnakumar
1775 + * Copyright (c) 2004-2006 David P. Quigley
1776 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1777 + * Copyright (c) 2003 Puja Gupta
1778 + * Copyright (c) 2003 Harikesavan Krishnan
1779 + * Copyright (c) 2003-2009 Stony Brook University
1780 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
1782 + * This program is free software; you can redistribute it and/or modify
1783 + * it under the terms of the GNU General Public License version 2 as
1784 + * published by the Free Software Foundation.
1790 + * For detailed explanation of copyup see:
1791 + * Documentation/filesystems/unionfs/concepts.txt
1794 +#ifdef CONFIG_UNION_FS_XATTR
1795 +/* copyup all extended attrs for a given dentry */
1796 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1797 + struct dentry *new_lower_dentry)
1800 + ssize_t list_size = -1;
1801 + char *name_list = NULL;
1802 + char *attr_value = NULL;
1803 + char *name_list_buf = NULL;
1805 + /* query the actual size of the xattr list */
1806 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1807 + if (list_size <= 0) {
1812 + /* allocate space for the actual list */
1813 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1814 + if (unlikely(!name_list || IS_ERR(name_list))) {
1815 + err = PTR_ERR(name_list);
1819 + name_list_buf = name_list; /* save for kfree at end */
1821 + /* now get the actual xattr list of the source file */
1822 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1823 + if (list_size <= 0) {
1828 + /* allocate space to hold each xattr's value */
1829 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1830 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1831 + err = PTR_ERR(name_list);
1835 + /* in a loop, get and set each xattr from src to dst file */
1836 + while (*name_list) {
1839 + /* Lock here since vfs_getxattr doesn't lock for us */
1840 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1841 + size = vfs_getxattr(old_lower_dentry, name_list,
1842 + attr_value, XATTR_SIZE_MAX);
1843 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1848 + if (size > XATTR_SIZE_MAX) {
1852 + /* Don't lock here since vfs_setxattr does it for us. */
1853 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1856 + * Selinux depends on "security.*" xattrs, so to maintain
1857 + * the security of copied-up files, if Selinux is active,
1858 + * then we must copy these xattrs as well. So we need to
1859 + * temporarily get FOWNER privileges.
1860 + * XXX: move entire copyup code to SIOQ.
1862 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1863 + const struct cred *old_creds;
1864 + struct cred *new_creds;
1866 + new_creds = prepare_creds();
1867 + if (unlikely(!new_creds)) {
1871 + cap_raise(new_creds->cap_effective, CAP_FOWNER);
1872 + old_creds = override_creds(new_creds);
1873 + err = vfs_setxattr(new_lower_dentry, name_list,
1874 + attr_value, size, 0);
1875 + revert_creds(old_creds);
1879 + name_list += strlen(name_list) + 1;
1882 + unionfs_xattr_kfree(name_list_buf);
1883 + unionfs_xattr_kfree(attr_value);
1884 + /* Ignore if xattr isn't supported */
1885 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1889 +#endif /* CONFIG_UNION_FS_XATTR */
1892 + * Determine the mode based on the copyup flags, and the existing dentry.
1894 + * Handle file systems which may not support certain options. For example
1895 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1896 + * errors, rather than propagating them up, which results in copyup errors
1897 + * and errors returned back to users.
1899 +static int copyup_permissions(struct super_block *sb,
1900 + struct dentry *old_lower_dentry,
1901 + struct dentry *new_lower_dentry)
1903 + struct inode *i = old_lower_dentry->d_inode;
1904 + struct iattr newattrs;
1907 + newattrs.ia_atime = i->i_atime;
1908 + newattrs.ia_mtime = i->i_mtime;
1909 + newattrs.ia_ctime = i->i_ctime;
1910 + newattrs.ia_gid = i->i_gid;
1911 + newattrs.ia_uid = i->i_uid;
1912 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1913 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1914 + ATTR_GID | ATTR_UID;
1915 + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1916 + err = notify_change(new_lower_dentry, &newattrs);
1920 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1921 + newattrs.ia_mode = i->i_mode;
1922 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1923 + err = notify_change(new_lower_dentry, &newattrs);
1924 + if (err == -EOPNOTSUPP &&
1925 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1926 + printk(KERN_WARNING
1927 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1928 + new_lower_dentry->d_name.name);
1933 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1938 + * create the new device/file/directory - use copyup_permission to copyup
1941 + * if the object being copied up is a regular file, the file is only created,
1942 + * the contents have to be copied up separately
1944 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1945 + struct dentry *new_lower_dentry,
1946 + struct dentry *new_lower_parent_dentry,
1950 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1951 + struct sioq_args args;
1953 + if (S_ISDIR(old_mode)) {
1954 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1955 + args.mkdir.dentry = new_lower_dentry;
1956 + args.mkdir.mode = old_mode;
1958 + run_sioq(__unionfs_mkdir, &args);
1960 + } else if (S_ISLNK(old_mode)) {
1961 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1962 + args.symlink.dentry = new_lower_dentry;
1963 + args.symlink.symbuf = symbuf;
1965 + run_sioq(__unionfs_symlink, &args);
1967 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1968 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1969 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1970 + args.mknod.dentry = new_lower_dentry;
1971 + args.mknod.mode = old_mode;
1972 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1974 + run_sioq(__unionfs_mknod, &args);
1976 + } else if (S_ISREG(old_mode)) {
1977 + struct nameidata nd;
1978 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1979 + if (unlikely(err < 0))
1981 + args.create.nd = &nd;
1982 + args.create.parent = new_lower_parent_dentry->d_inode;
1983 + args.create.dentry = new_lower_dentry;
1984 + args.create.mode = old_mode;
1986 + run_sioq(__unionfs_create, &args);
1988 + release_lower_nd(&nd, err);
1990 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1999 +static int __copyup_reg_data(struct dentry *dentry,
2000 + struct dentry *new_lower_dentry, int new_bindex,
2001 + struct dentry *old_lower_dentry, int old_bindex,
2002 + struct file **copyup_file, loff_t len)
2004 + struct super_block *sb = dentry->d_sb;
2005 + struct file *input_file;
2006 + struct file *output_file;
2007 + struct vfsmount *output_mnt;
2008 + mm_segment_t old_fs;
2010 + ssize_t read_bytes, write_bytes;
2014 + /* open old file */
2015 + unionfs_mntget(dentry, old_bindex);
2016 + branchget(sb, old_bindex);
2017 + /* dentry_open calls dput and mntput if it returns an error */
2018 + input_file = dentry_open(old_lower_dentry,
2019 + unionfs_lower_mnt_idx(dentry, old_bindex),
2020 + O_RDONLY | O_LARGEFILE, current_cred());
2021 + if (IS_ERR(input_file)) {
2022 + dput(old_lower_dentry);
2023 + err = PTR_ERR(input_file);
2026 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
2028 + goto out_close_in;
2031 + /* open new file */
2032 + dget(new_lower_dentry);
2033 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
2034 + branchget(sb, new_bindex);
2035 + output_file = dentry_open(new_lower_dentry, output_mnt,
2036 + O_RDWR | O_LARGEFILE, current_cred());
2037 + if (IS_ERR(output_file)) {
2038 + err = PTR_ERR(output_file);
2039 + goto out_close_in2;
2041 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
2043 + goto out_close_out;
2046 + /* allocating a buffer */
2047 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2048 + if (unlikely(!buf)) {
2050 + goto out_close_out;
2053 + input_file->f_pos = 0;
2054 + output_file->f_pos = 0;
2056 + old_fs = get_fs();
2057 + set_fs(KERNEL_DS);
2062 + if (len >= PAGE_SIZE)
2064 + else if ((len < PAGE_SIZE) && (len > 0))
2070 + input_file->f_op->read(input_file,
2071 + (char __user *)buf, size,
2072 + &input_file->f_pos);
2073 + if (read_bytes <= 0) {
2078 + /* see Documentation/filesystems/unionfs/issues.txt */
2081 + output_file->f_op->write(output_file,
2082 + (char __user *)buf,
2084 + &output_file->f_pos);
2086 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2087 + err = write_bytes;
2090 + } while ((read_bytes > 0) && (len > 0));
2097 + err = output_file->f_op->fsync(output_file,
2098 + new_lower_dentry, 0);
2101 + goto out_close_out;
2103 + if (copyup_file) {
2104 + *copyup_file = output_file;
2105 + goto out_close_in;
2109 + fput(output_file);
2112 + branchput(sb, new_bindex);
2118 + branchput(sb, old_bindex);
2124 + * dput the lower references for old and new dentry & clear a lower dentry
2127 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2128 + int old_bstart, int old_bend,
2129 + struct dentry *new_lower_dentry, int new_bindex)
2131 + /* get rid of the lower dentry and all its traces */
2132 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2133 + dbstart(dentry) = old_bstart;
2134 + dbend(dentry) = old_bend;
2136 + dput(new_lower_dentry);
2137 + dput(old_lower_dentry);
2141 + * Copy up a dentry to a file of specified name.
2143 + * @dir: used to pull the ->i_sb to access other branches
2144 + * @dentry: the non-negative dentry whose lower_inode we should copy
2145 + * @bstart: the branch of the lower_inode to copy from
2146 + * @new_bindex: the branch to create the new file in
2147 + * @name: the name of the file to create
2148 + * @namelen: length of @name
2149 + * @copyup_file: the "struct file" to return (optional)
2150 + * @len: how many bytes to copy-up?
2152 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2153 + int new_bindex, const char *name, int namelen,
2154 + struct file **copyup_file, loff_t len)
2156 + struct dentry *new_lower_dentry;
2157 + struct dentry *old_lower_dentry = NULL;
2158 + struct super_block *sb;
2163 + struct dentry *new_lower_parent_dentry = NULL;
2164 + mm_segment_t oldfs;
2165 + char *symbuf = NULL;
2167 + verify_locked(dentry);
2169 + old_bindex = bstart;
2170 + old_bstart = dbstart(dentry);
2171 + old_bend = dbend(dentry);
2173 + BUG_ON(new_bindex < 0);
2174 + BUG_ON(new_bindex >= old_bindex);
2178 + err = is_robranch_super(sb, new_bindex);
2182 + /* Create the directory structure above this dentry. */
2183 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2184 + if (IS_ERR(new_lower_dentry)) {
2185 + err = PTR_ERR(new_lower_dentry);
2189 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2190 + /* we conditionally dput this old_lower_dentry at end of function */
2191 + dget(old_lower_dentry);
2193 + /* For symlinks, we must read the link before we lock the directory. */
2194 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2196 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2197 + if (unlikely(!symbuf)) {
2198 + __clear(dentry, old_lower_dentry,
2199 + old_bstart, old_bend,
2200 + new_lower_dentry, new_bindex);
2206 + set_fs(KERNEL_DS);
2207 + err = old_lower_dentry->d_inode->i_op->readlink(
2209 + (char __user *)symbuf,
2213 + __clear(dentry, old_lower_dentry,
2214 + old_bstart, old_bend,
2215 + new_lower_dentry, new_bindex);
2218 + symbuf[err] = '\0';
2221 + /* Now we lock the parent, and create the object in the new branch. */
2222 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2224 + /* create the new inode */
2225 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2226 + new_lower_parent_dentry, symbuf);
2229 + __clear(dentry, old_lower_dentry,
2230 + old_bstart, old_bend,
2231 + new_lower_dentry, new_bindex);
2235 + /* We actually copyup the file here. */
2236 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2237 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2238 + old_lower_dentry, old_bindex,
2239 + copyup_file, len);
2243 + /* Set permissions. */
2244 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2248 +#ifdef CONFIG_UNION_FS_XATTR
2249 + /* Selinux uses extended attributes for permissions. */
2250 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2253 +#endif /* CONFIG_UNION_FS_XATTR */
2255 + /* do not allow files getting deleted to be re-interposed */
2256 + if (!d_deleted(dentry))
2257 + unionfs_reinterpose(dentry);
2263 + * copyup failed, because we possibly ran out of space or
2264 + * quota, or something else happened so let's unlink; we don't
2265 + * really care about the return value of vfs_unlink
2267 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2269 + if (copyup_file) {
2270 + /* need to close the file */
2272 + fput(*copyup_file);
2273 + branchput(sb, new_bindex);
2277 + * TODO: should we reset the error to something like -EIO?
2279 + * If we don't reset, the user may get some nonsensical errors, but
2280 + * on the other hand, if we reset to EIO, we guarantee that the user
2281 + * will get a "confusing" error message.
2285 + unlock_dir(new_lower_parent_dentry);
2289 + * If old_lower_dentry was not a file, then we need to dput it. If
2290 + * it was a file, then it was already dput indirectly by other
2291 + * functions we call above which operate on regular files.
2293 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2294 + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2295 + dput(old_lower_dentry);
2300 + * if directory creation succeeded, but inode copyup failed,
2301 + * then purge new dentries.
2303 + if (dbstart(dentry) < old_bstart &&
2304 + ibstart(dentry->d_inode) > dbstart(dentry))
2305 + __clear(dentry, NULL, old_bstart, old_bend,
2306 + unionfs_lower_dentry(dentry), dbstart(dentry));
2309 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2310 + unionfs_postcopyup_release(dentry);
2311 + if (!unionfs_lower_inode(dentry->d_inode)) {
2313 + * If we got here, then we copied up to an
2314 + * unlinked-open file, whose name is .unionfsXXXXX.
2316 + struct inode *inode = new_lower_dentry->d_inode;
2317 + atomic_inc(&inode->i_count);
2318 + unionfs_set_lower_inode_idx(dentry->d_inode,
2319 + ibstart(dentry->d_inode),
2323 + unionfs_postcopyup_setmnt(dentry);
2324 + /* sync inode times from copied-up inode to our inode */
2325 + unionfs_copy_attr_times(dentry->d_inode);
2326 + unionfs_check_inode(dir);
2327 + unionfs_check_dentry(dentry);
2333 + * This function creates a copy of a file represented by 'file' which
2334 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2335 + * will be named "name".
2337 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2338 + int bstart, int new_bindex, loff_t len)
2341 + struct file *output_file = NULL;
2343 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2344 + name, strlen(name), &output_file, len);
2346 + fbstart(file) = new_bindex;
2347 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2354 + * This function creates a copy of a file represented by 'file' which
2355 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2357 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2358 + int new_bindex, loff_t len)
2361 + struct file *output_file = NULL;
2362 + struct dentry *dentry = file->f_path.dentry;
2364 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2365 + dentry->d_name.name, dentry->d_name.len,
2366 + &output_file, len);
2368 + fbstart(file) = new_bindex;
2369 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2375 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2376 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2377 + int old_bstart, int old_bend)
2381 + int new_bstart = -1;
2382 + int new_bend = -1;
2385 + loop_start = min(old_bstart, bindex);
2386 + loop_end = max(old_bend, bindex);
2389 + * This loop sets the bstart and bend for the new dentry by
2390 + * traversing from left to right. It also dputs all negative
2391 + * dentries except bindex
2393 + for (i = loop_start; i <= loop_end; i++) {
2394 + if (!unionfs_lower_dentry_idx(dentry, i))
2397 + if (i == bindex) {
2399 + if (new_bstart < 0)
2404 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2405 + dput(unionfs_lower_dentry_idx(dentry, i));
2406 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2408 + unionfs_mntput(dentry, i);
2409 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2411 + if (new_bstart < 0)
2417 + if (new_bstart < 0)
2418 + new_bstart = bindex;
2420 + new_bend = bindex;
2421 + dbstart(dentry) = new_bstart;
2422 + dbend(dentry) = new_bend;
2426 +/* set lower inode ptr and update bstart & bend if necessary */
2427 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2430 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2431 + igrab(lower->d_inode));
2432 + if (likely(ibstart(upper->d_inode) > bindex))
2433 + ibstart(upper->d_inode) = bindex;
2434 + if (likely(ibend(upper->d_inode) < bindex))
2435 + ibend(upper->d_inode) = bindex;
2439 +/* set lower dentry ptr and update bstart & bend if necessary */
2440 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2443 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2444 + if (likely(dbstart(upper) > bindex))
2445 + dbstart(upper) = bindex;
2446 + if (likely(dbend(upper) < bindex))
2447 + dbend(upper) = bindex;
2451 + * This function replicates the directory structure up-to given dentry
2452 + * in the bindex branch.
2454 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2455 + const char *name, int bindex)
2458 + struct dentry *child_dentry;
2459 + struct dentry *parent_dentry;
2460 + struct dentry *lower_parent_dentry = NULL;
2461 + struct dentry *lower_dentry = NULL;
2462 + const char *childname;
2463 + unsigned int childnamelen;
2468 + struct dentry **path = NULL;
2469 + struct super_block *sb;
2471 + verify_locked(dentry);
2473 + err = is_robranch_super(dir->i_sb, bindex);
2475 + lower_dentry = ERR_PTR(err);
2479 + old_bstart = dbstart(dentry);
2480 + old_bend = dbend(dentry);
2482 + lower_dentry = ERR_PTR(-ENOMEM);
2484 + /* There is no sense allocating any less than the minimum. */
2486 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2487 + if (unlikely(!path))
2490 + /* assume the negative dentry of unionfs as the parent dentry */
2491 + parent_dentry = dentry;
2494 + * This loop finds the first parent that exists in the given branch.
2495 + * We start building the directory structure from there. At the end
2496 + * of the loop, the following should hold:
2497 + * - child_dentry is the first nonexistent child
2498 + * - parent_dentry is the first existent parent
2499 + * - path[0] is the = deepest child
2500 + * - path[count] is the first child to create
2503 + child_dentry = parent_dentry;
2505 + /* find the parent directory dentry in unionfs */
2506 + parent_dentry = dget_parent(child_dentry);
2508 + /* find out the lower_parent_dentry in the given branch */
2509 + lower_parent_dentry =
2510 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2512 + /* grow path table */
2513 + if (count == nr_dentry) {
2517 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2519 + if (unlikely(!p)) {
2520 + lower_dentry = ERR_PTR(-ENOMEM);
2526 + /* store the child dentry */
2527 + path[count++] = child_dentry;
2528 + } while (!lower_parent_dentry);
2531 + sb = dentry->d_sb;
2534 + * This code goes between the begin/end labels and basically
2535 + * emulates a while(child_dentry != dentry), only cleaner and
2536 + * shorter than what would be a much longer while loop.
2539 + /* get lower parent dir in the current branch */
2540 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2541 + dput(parent_dentry);
2543 + /* init the values to lookup */
2544 + childname = child_dentry->d_name.name;
2545 + childnamelen = child_dentry->d_name.len;
2547 + if (child_dentry != dentry) {
2548 + /* lookup child in the underlying file system */
2549 + lower_dentry = lookup_one_len(childname, lower_parent_dentry,
2551 + if (IS_ERR(lower_dentry))
2555 + * Is the name a whiteout of the child name ? lookup the
2556 + * whiteout child in the underlying file system
2558 + lower_dentry = lookup_one_len(name, lower_parent_dentry,
2560 + if (IS_ERR(lower_dentry))
2563 + /* Replace the current dentry (if any) with the new one */
2564 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2565 + unionfs_set_lower_dentry_idx(dentry, bindex,
2568 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2572 + if (lower_dentry->d_inode) {
2574 + * since this already exists we dput to avoid
2575 + * multiple references on the same dentry
2577 + dput(lower_dentry);
2579 + struct sioq_args args;
2581 + /* it's a negative dentry, create a new dir */
2582 + lower_parent_dentry = lock_parent(lower_dentry);
2584 + args.mkdir.parent = lower_parent_dentry->d_inode;
2585 + args.mkdir.dentry = lower_dentry;
2586 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2588 + run_sioq(__unionfs_mkdir, &args);
2592 + err = copyup_permissions(dir->i_sb, child_dentry,
2594 + unlock_dir(lower_parent_dentry);
2596 + dput(lower_dentry);
2597 + lower_dentry = ERR_PTR(err);
2603 + __set_inode(child_dentry, lower_dentry, bindex);
2604 + __set_dentry(child_dentry, lower_dentry, bindex);
2606 + * update times of this dentry, but also the parent, because if
2607 + * we changed, the parent may have changed too.
2609 + fsstack_copy_attr_times(parent_dentry->d_inode,
2610 + lower_parent_dentry->d_inode);
2611 + unionfs_copy_attr_times(child_dentry->d_inode);
2613 + parent_dentry = child_dentry;
2614 + child_dentry = path[--count];
2617 + /* cleanup any leftover locks from the do/while loop above */
2618 + if (IS_ERR(lower_dentry))
2620 + dput(path[count--]);
2622 + return lower_dentry;
2626 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2627 + * dentry+parents to the first parent node that has an mnt.
2629 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2631 + struct dentry *parent, *hasone;
2632 + int bindex = dbstart(dentry);
2634 + if (unionfs_lower_mnt_idx(dentry, bindex))
2636 + hasone = dentry->d_parent;
2637 + /* this loop should stop at root dentry */
2638 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2639 + hasone = hasone->d_parent;
2641 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2642 + unionfs_set_lower_mnt_idx(parent, bindex,
2643 + unionfs_mntget(hasone, bindex));
2644 + parent = parent->d_parent;
2649 + * Post-copyup helper to release all non-directory source objects of a
2650 + * copied-up file. Regular files should have only one lower object.
2652 +void unionfs_postcopyup_release(struct dentry *dentry)
2656 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2657 + bstart = dbstart(dentry);
2658 + bend = dbend(dentry);
2660 + path_put_lowers(dentry, bstart + 1, bend, false);
2661 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2663 + dbend(dentry) = bstart;
2664 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2666 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2667 new file mode 100644
2668 index 0000000..3fd641a
2670 +++ b/fs/unionfs/debug.c
2673 + * Copyright (c) 2003-2009 Erez Zadok
2674 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2675 + * Copyright (c) 2003-2009 Stony Brook University
2676 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
2678 + * This program is free software; you can redistribute it and/or modify
2679 + * it under the terms of the GNU General Public License version 2 as
2680 + * published by the Free Software Foundation.
2686 + * Helper debugging functions for maintainers (and for users to report back
2687 + * useful information back to maintainers)
2690 +/* it's always useful to know what part of the code called us */
2691 +#define PRINT_CALLER(fname, fxn, line) \
2693 + if (!printed_caller) { \
2694 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2695 + printed_caller = 1; \
2700 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2701 + * the fan-out of various Unionfs objects. We check that no lower objects
2702 + * exist outside the start/end branch range; that all objects within are
2703 + * non-NULL (with some allowed exceptions); that for every lower file
2704 + * there's a lower dentry+inode; that the start/end ranges match for all
2705 + * corresponding lower objects; that open files/symlinks have only one lower
2706 + * objects, but directories can have several; and more.
2708 +void __unionfs_check_inode(const struct inode *inode,
2709 + const char *fname, const char *fxn, int line)
2713 + struct inode *lower_inode;
2714 + struct super_block *sb;
2715 + int printed_caller = 0;
2718 + /* for inodes now */
2721 + istart = ibstart(inode);
2722 + iend = ibend(inode);
2723 + /* don't check inode if no lower branches */
2724 + if (istart < 0 && iend < 0)
2726 + if (unlikely(istart > iend)) {
2727 + PRINT_CALLER(fname, fxn, line);
2728 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2729 + inode, istart, iend);
2731 + if (unlikely((istart == -1 && iend != -1) ||
2732 + (istart != -1 && iend == -1))) {
2733 + PRINT_CALLER(fname, fxn, line);
2734 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2735 + inode, istart, iend);
2737 + if (!S_ISDIR(inode->i_mode)) {
2738 + if (unlikely(iend != istart)) {
2739 + PRINT_CALLER(fname, fxn, line);
2740 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2741 + inode, istart, iend);
2745 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2746 + if (unlikely(!UNIONFS_I(inode))) {
2747 + PRINT_CALLER(fname, fxn, line);
2748 + pr_debug(" Ci3: no inode_info %p\n", inode);
2751 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2752 + PRINT_CALLER(fname, fxn, line);
2753 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2756 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2757 + if (lower_inode) {
2758 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2759 + if (unlikely(bindex < istart || bindex > iend)) {
2760 + PRINT_CALLER(fname, fxn, line);
2761 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2762 + "istart/end=%d:%d\n", inode,
2763 + lower_inode, bindex, istart, iend);
2764 + } else if (unlikely(lower_inode == poison_ptr)) {
2765 + /* freed inode! */
2766 + PRINT_CALLER(fname, fxn, line);
2767 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2768 + "istart/end=%d:%d\n", inode,
2769 + lower_inode, bindex, istart, iend);
2773 + /* if we get here, then lower_inode == NULL */
2774 + if (bindex < istart || bindex > iend)
2777 + * directories can have NULL lower inodes in b/t start/end,
2778 + * but NOT if at the start/end range.
2780 + if (unlikely(S_ISDIR(inode->i_mode) &&
2781 + bindex > istart && bindex < iend))
2783 + PRINT_CALLER(fname, fxn, line);
2784 + pr_debug(" Ci7: inode/linode=%p:%p "
2785 + "bindex=%d istart/end=%d:%d\n",
2786 + inode, lower_inode, bindex, istart, iend);
2790 +void __unionfs_check_dentry(const struct dentry *dentry,
2791 + const char *fname, const char *fxn, int line)
2794 + int dstart, dend, istart, iend;
2795 + struct dentry *lower_dentry;
2796 + struct inode *inode, *lower_inode;
2797 + struct super_block *sb;
2798 + struct vfsmount *lower_mnt;
2799 + int printed_caller = 0;
2803 + sb = dentry->d_sb;
2804 + inode = dentry->d_inode;
2805 + dstart = dbstart(dentry);
2806 + dend = dbend(dentry);
2807 + /* don't check dentry/mnt if no lower branches */
2808 + if (dstart < 0 && dend < 0)
2810 + BUG_ON(dstart > dend);
2812 + if (unlikely((dstart == -1 && dend != -1) ||
2813 + (dstart != -1 && dend == -1))) {
2814 + PRINT_CALLER(fname, fxn, line);
2815 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2816 + dentry, dstart, dend);
2819 + * check for NULL dentries inside the start/end range, or
2820 + * non-NULL dentries outside the start/end range.
2822 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2823 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2824 + if (lower_dentry) {
2825 + if (unlikely(bindex < dstart || bindex > dend)) {
2826 + PRINT_CALLER(fname, fxn, line);
2827 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2828 + "bindex=%d dstart/end=%d:%d\n",
2829 + dentry, lower_dentry,
2830 + (lower_dentry ? lower_dentry->d_inode :
2832 + bindex, dstart, dend);
2834 + } else { /* lower_dentry == NULL */
2835 + if (bindex < dstart || bindex > dend)
2838 + * Directories can have NULL lower inodes in b/t
2839 + * start/end, but NOT if at the start/end range.
2840 + * Ignore this rule, however, if this is a NULL
2841 + * dentry or a deleted dentry.
2843 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2845 + !(inode && S_ISDIR(inode->i_mode) &&
2846 + bindex > dstart && bindex < dend))) {
2847 + PRINT_CALLER(fname, fxn, line);
2848 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2849 + "bindex=%d dstart/end=%d:%d\n",
2850 + dentry, lower_dentry,
2852 + lower_dentry->d_inode :
2854 + bindex, dstart, dend);
2859 + /* check for vfsmounts same as for dentries */
2860 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2861 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2863 + if (unlikely(bindex < dstart || bindex > dend)) {
2864 + PRINT_CALLER(fname, fxn, line);
2865 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2866 + "dstart/end=%d:%d\n", dentry,
2867 + lower_mnt, bindex, dstart, dend);
2869 + } else { /* lower_mnt == NULL */
2870 + if (bindex < dstart || bindex > dend)
2873 + * Directories can have NULL lower inodes in b/t
2874 + * start/end, but NOT if at the start/end range.
2875 + * Ignore this rule, however, if this is a NULL
2878 + if (unlikely(inode &&
2879 + !(inode && S_ISDIR(inode->i_mode) &&
2880 + bindex > dstart && bindex < dend))) {
2881 + PRINT_CALLER(fname, fxn, line);
2882 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2883 + "bindex=%d dstart/end=%d:%d\n",
2884 + dentry, lower_mnt, bindex,
2891 + /* for inodes now */
2894 + istart = ibstart(inode);
2895 + iend = ibend(inode);
2896 + /* don't check inode if no lower branches */
2897 + if (istart < 0 && iend < 0)
2899 + BUG_ON(istart > iend);
2900 + if (unlikely((istart == -1 && iend != -1) ||
2901 + (istart != -1 && iend == -1))) {
2902 + PRINT_CALLER(fname, fxn, line);
2903 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2904 + dentry, inode, istart, iend);
2906 + if (unlikely(istart != dstart)) {
2907 + PRINT_CALLER(fname, fxn, line);
2908 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2909 + dentry, inode, istart, dstart);
2911 + if (unlikely(iend != dend)) {
2912 + PRINT_CALLER(fname, fxn, line);
2913 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2914 + dentry, inode, iend, dend);
2917 + if (!S_ISDIR(inode->i_mode)) {
2918 + if (unlikely(dend != dstart)) {
2919 + PRINT_CALLER(fname, fxn, line);
2920 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2921 + dentry, inode, dstart, dend);
2923 + if (unlikely(iend != istart)) {
2924 + PRINT_CALLER(fname, fxn, line);
2925 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2926 + dentry, inode, istart, iend);
2930 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2931 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2932 + if (lower_inode) {
2933 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2934 + if (unlikely(bindex < istart || bindex > iend)) {
2935 + PRINT_CALLER(fname, fxn, line);
2936 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2937 + "istart/end=%d:%d\n", dentry,
2938 + lower_inode, bindex, istart, iend);
2939 + } else if (unlikely(lower_inode == poison_ptr)) {
2940 + /* freed inode! */
2941 + PRINT_CALLER(fname, fxn, line);
2942 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2943 + "istart/end=%d:%d\n", dentry,
2944 + lower_inode, bindex, istart, iend);
2948 + /* if we get here, then lower_inode == NULL */
2949 + if (bindex < istart || bindex > iend)
2952 + * directories can have NULL lower inodes in b/t start/end,
2953 + * but NOT if at the start/end range.
2955 + if (unlikely(S_ISDIR(inode->i_mode) &&
2956 + bindex > istart && bindex < iend))
2958 + PRINT_CALLER(fname, fxn, line);
2959 + pr_debug(" CI7: dentry/linode=%p:%p "
2960 + "bindex=%d istart/end=%d:%d\n",
2961 + dentry, lower_inode, bindex, istart, iend);
2965 + * If it's a directory, then intermediate objects b/t start/end can
2966 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2969 + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2970 + for (bindex = dstart+1; bindex < dend; bindex++) {
2971 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2972 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2974 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2975 + if (unlikely(!((lower_inode && lower_dentry &&
2978 + !lower_dentry && !lower_mnt)))) {
2979 + PRINT_CALLER(fname, fxn, line);
2980 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2981 + "bindex=%d dstart/end=%d:%d\n",
2982 + lower_mnt, lower_dentry, lower_inode,
2983 + bindex, dstart, dend);
2986 + /* check if lower inode is newer than upper one (it shouldn't) */
2987 + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
2988 + PRINT_CALLER(fname, fxn, line);
2989 + for (bindex = ibstart(inode); bindex <= ibend(inode);
2991 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2992 + if (unlikely(!lower_inode))
2994 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2995 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
2997 + inode->i_mtime.tv_sec,
2998 + inode->i_mtime.tv_nsec,
2999 + lower_inode->i_mtime.tv_sec,
3000 + lower_inode->i_mtime.tv_nsec,
3001 + inode->i_ctime.tv_sec,
3002 + inode->i_ctime.tv_nsec,
3003 + lower_inode->i_ctime.tv_sec,
3004 + lower_inode->i_ctime.tv_nsec);
3009 +void __unionfs_check_file(const struct file *file,
3010 + const char *fname, const char *fxn, int line)
3013 + int dstart, dend, fstart, fend;
3014 + struct dentry *dentry;
3015 + struct file *lower_file;
3016 + struct inode *inode;
3017 + struct super_block *sb;
3018 + int printed_caller = 0;
3021 + dentry = file->f_path.dentry;
3022 + sb = dentry->d_sb;
3023 + dstart = dbstart(dentry);
3024 + dend = dbend(dentry);
3025 + BUG_ON(dstart > dend);
3026 + fstart = fbstart(file);
3027 + fend = fbend(file);
3028 + BUG_ON(fstart > fend);
3030 + if (unlikely((fstart == -1 && fend != -1) ||
3031 + (fstart != -1 && fend == -1))) {
3032 + PRINT_CALLER(fname, fxn, line);
3033 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
3034 + file, dentry, fstart, fend);
3036 + if (unlikely(fstart != dstart)) {
3037 + PRINT_CALLER(fname, fxn, line);
3038 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
3039 + file, dentry, fstart, dstart);
3041 + if (unlikely(fend != dend)) {
3042 + PRINT_CALLER(fname, fxn, line);
3043 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
3044 + file, dentry, fend, dend);
3046 + inode = dentry->d_inode;
3047 + if (!S_ISDIR(inode->i_mode)) {
3048 + if (unlikely(fend != fstart)) {
3049 + PRINT_CALLER(fname, fxn, line);
3050 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
3051 + file, inode, fstart, fend);
3053 + if (unlikely(dend != dstart)) {
3054 + PRINT_CALLER(fname, fxn, line);
3055 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
3056 + file, dentry, dstart, dend);
3061 + * check for NULL dentries inside the start/end range, or
3062 + * non-NULL dentries outside the start/end range.
3064 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
3065 + lower_file = unionfs_lower_file_idx(file, bindex);
3067 + if (unlikely(bindex < fstart || bindex > fend)) {
3068 + PRINT_CALLER(fname, fxn, line);
3069 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
3070 + "fstart/end=%d:%d\n", file,
3071 + lower_file, bindex, fstart, fend);
3073 + } else { /* lower_file == NULL */
3074 + if (bindex >= fstart && bindex <= fend) {
3076 + * directories can have NULL lower inodes in
3077 + * b/t start/end, but NOT if at the
3078 + * start/end range.
3080 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3081 + bindex > fstart &&
3082 + bindex < fend))) {
3083 + PRINT_CALLER(fname, fxn, line);
3084 + pr_debug(" CF6: file/lower=%p:%p "
3085 + "bindex=%d fstart/end=%d:%d\n",
3086 + file, lower_file, bindex,
3093 + __unionfs_check_dentry(dentry, fname, fxn, line);
3096 +void __unionfs_check_nd(const struct nameidata *nd,
3097 + const char *fname, const char *fxn, int line)
3099 + struct file *file;
3100 + int printed_caller = 0;
3102 + if (unlikely(!nd))
3104 + if (nd->flags & LOOKUP_OPEN) {
3105 + file = nd->intent.open.file;
3106 + if (unlikely(file->f_path.dentry &&
3107 + strcmp(file->f_path.dentry->d_sb->s_type->name,
3109 + PRINT_CALLER(fname, fxn, line);
3110 + pr_debug(" CND1: lower_file of type %s\n",
3111 + file->f_path.dentry->d_sb->s_type->name);
3117 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3118 +void __show_branch_counts(const struct super_block *sb,
3119 + const char *file, const char *fxn, int line)
3122 + struct vfsmount *mnt;
3125 + for (i = 0; i < sbmax(sb); i++) {
3126 + if (likely(sb->s_root))
3127 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3130 + printk(KERN_CONT "%d:",
3131 + (mnt ? atomic_read(&mnt->mnt_count) : -99));
3133 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3136 +void __show_inode_times(const struct inode *inode,
3137 + const char *file, const char *fxn, int line)
3139 + struct inode *lower_inode;
3142 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3143 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3144 + if (unlikely(!lower_inode))
3146 + pr_debug("IT(%lu:%d): %s:%s:%d "
3147 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3148 + inode->i_ino, bindex,
3150 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3151 + lower_inode->i_mtime.tv_sec,
3152 + lower_inode->i_mtime.tv_nsec,
3153 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3154 + lower_inode->i_ctime.tv_sec,
3155 + lower_inode->i_ctime.tv_nsec);
3159 +void __show_dinode_times(const struct dentry *dentry,
3160 + const char *file, const char *fxn, int line)
3162 + struct inode *inode = dentry->d_inode;
3163 + struct inode *lower_inode;
3166 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3167 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3170 + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3171 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3172 + dentry->d_name.name, inode->i_ino, bindex,
3174 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3175 + lower_inode->i_mtime.tv_sec,
3176 + lower_inode->i_mtime.tv_nsec,
3177 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3178 + lower_inode->i_ctime.tv_sec,
3179 + lower_inode->i_ctime.tv_nsec);
3183 +void __show_inode_counts(const struct inode *inode,
3184 + const char *file, const char *fxn, int line)
3186 + struct inode *lower_inode;
3189 + if (unlikely(!inode)) {
3190 + pr_debug("SiC: Null inode\n");
3193 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3195 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3196 + if (unlikely(!lower_inode))
3198 + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3199 + inode->i_ino, bindex,
3200 + atomic_read(&(inode)->i_count),
3201 + atomic_read(&(lower_inode)->i_count),
3205 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
3206 new file mode 100644
3207 index 0000000..85b5d3c
3209 +++ b/fs/unionfs/dentry.c
3212 + * Copyright (c) 2003-2009 Erez Zadok
3213 + * Copyright (c) 2003-2006 Charles P. Wright
3214 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3215 + * Copyright (c) 2005-2006 Junjiro Okajima
3216 + * Copyright (c) 2005 Arun M. Krishnakumar
3217 + * Copyright (c) 2004-2006 David P. Quigley
3218 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3219 + * Copyright (c) 2003 Puja Gupta
3220 + * Copyright (c) 2003 Harikesavan Krishnan
3221 + * Copyright (c) 2003-2009 Stony Brook University
3222 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3224 + * This program is free software; you can redistribute it and/or modify
3225 + * it under the terms of the GNU General Public License version 2 as
3226 + * published by the Free Software Foundation.
3231 +bool is_negative_lower(const struct dentry *dentry)
3234 + struct dentry *lower_dentry;
3237 + /* cache coherency: check if file was deleted on lower branch */
3238 + if (dbstart(dentry) < 0)
3240 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3241 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3242 + /* unhashed (i.e., unlinked) lower dentries don't count */
3243 + if (lower_dentry && lower_dentry->d_inode &&
3244 + !d_deleted(lower_dentry) &&
3245 + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3251 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3253 + struct dentry *lower_dentry;
3258 + for (bindex = start; bindex <= end; bindex++) {
3259 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3260 + if (!lower_dentry)
3262 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3263 + dput(lower_dentry);
3268 + * Purge and invalidate as many data pages of a unionfs inode. This is
3269 + * called when the lower inode has changed, and we want to force processes
3270 + * to re-get the new data.
3272 +static inline void purge_inode_data(struct inode *inode)
3274 + /* remove all non-private mappings */
3275 + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3276 + /* invalidate as many pages as possible */
3277 + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3279 + * Don't try to truncate_inode_pages here, because this could lead
3280 + * to a deadlock between some of address_space ops and dentry
3281 + * revalidation: the address space op is invoked with a lock on our
3282 + * own page, and truncate_inode_pages will block on locked pages.
3287 + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3288 + * of the @dentry and its @parent are locked. Assume parent is valid,
3289 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3290 + * dentry). Returns true if valid, false otherwise.
3292 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3295 + bool valid = true; /* default is valid */
3296 + struct dentry *lower_dentry;
3297 + struct dentry *result;
3298 + int bindex, bstart, bend;
3299 + int sbgen, dgen, pdgen;
3301 + int interpose_flag;
3303 + verify_locked(dentry);
3304 + verify_locked(parent);
3306 + /* if the dentry is unhashed, do NOT revalidate */
3307 + if (d_deleted(dentry))
3310 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3312 + if (is_newer_lower(dentry)) {
3313 + /* root dentry is always valid */
3314 + if (IS_ROOT(dentry)) {
3315 + unionfs_copy_attr_times(dentry->d_inode);
3318 + * reset generation number to zero, guaranteed to be
3322 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3325 + purge_inode_data(dentry->d_inode);
3328 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3330 + BUG_ON(dbstart(dentry) == -1);
3331 + if (dentry->d_inode)
3334 + /* if our dentry is valid, then validate all lower ones */
3335 + if (sbgen == dgen)
3336 + goto validate_lowers;
3338 + /* The root entry should always be valid */
3339 + BUG_ON(IS_ROOT(dentry));
3341 + /* We can't work correctly if our parent isn't valid. */
3342 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3344 + /* Free the pointers for our inodes and this dentry. */
3345 + path_put_lowers_all(dentry, false);
3347 + interpose_flag = INTERPOSE_REVAL_NEG;
3349 + interpose_flag = INTERPOSE_REVAL;
3350 + iput_lowers_all(dentry->d_inode, true);
3353 + if (realloc_dentry_private_data(dentry) != 0) {
3358 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3360 + if (IS_ERR(result)) {
3365 + * current unionfs_lookup_backend() doesn't return
3372 + if (unlikely(positive && is_negative_lower(dentry))) {
3373 + /* call make_bad_inode here ? */
3380 + * if we got here then we have revalidated our dentry and all lower
3381 + * ones, so we can return safely.
3383 + if (!valid) /* lower dentry revalidation failed */
3387 + * If the parent's gen no. matches the superblock's gen no., then
3388 + * we can update our denty's gen no. If they didn't match, then it
3389 + * was OK to revalidate this dentry with a stale parent, but we'll
3390 + * purposely not update our dentry's gen no. (so it can be redone);
3391 + * and, we'll mark our parent dentry as invalid so it'll force it
3392 + * (and our dentry) to be revalidated.
3394 + if (pdgen == sbgen)
3395 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3400 + /* The revalidation must occur across all branches */
3401 + bstart = dbstart(dentry);
3402 + bend = dbend(dentry);
3403 + BUG_ON(bstart == -1);
3404 + for (bindex = bstart; bindex <= bend; bindex++) {
3405 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3406 + if (!lower_dentry || !lower_dentry->d_op
3407 + || !lower_dentry->d_op->d_revalidate)
3410 + * Don't pass nameidata to lower file system, because we
3411 + * don't want an arbitrary lower file being opened or
3412 + * returned to us: it may be useless to us because of the
3413 + * fanout nature of unionfs (cf. file/directory open-file
3414 + * invariants). We will open lower files as and when needed
3417 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3421 + if (!dentry->d_inode ||
3422 + ibstart(dentry->d_inode) < 0 ||
3423 + ibend(dentry->d_inode) < 0) {
3430 + * If we get here, and we copy the meta-data from the lower
3431 + * inode to our inode, then it is vital that we have already
3432 + * purged all unionfs-level file data. We do that in the
3433 + * caller (__unionfs_d_revalidate) by calling
3434 + * purge_inode_data.
3436 + unionfs_copy_attr_all(dentry->d_inode,
3437 + unionfs_lower_inode(dentry->d_inode));
3438 + fsstack_copy_inode_size(dentry->d_inode,
3439 + unionfs_lower_inode(dentry->d_inode));
3447 + * Determine if the lower inode objects have changed from below the unionfs
3448 + * inode. Return true if changed, false otherwise.
3450 + * We check if the mtime or ctime have changed. However, the inode times
3451 + * can be changed by anyone without much protection, including
3452 + * asynchronously. This can sometimes cause unionfs to find that the lower
3453 + * file system doesn't change its inode times quick enough, resulting in a
3454 + * false positive indication (which is harmless, it just makes unionfs do
3455 + * extra work in re-validating the objects). To minimize the chances of
3456 + * these situations, we still consider such small time changes valid, but we
3457 + * don't print debugging messages unless the time changes are greater than
3458 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3459 + * because significant changes are more likely due to users manually
3460 + * touching lower files.
3462 +bool is_newer_lower(const struct dentry *dentry)
3465 + struct inode *inode;
3466 + struct inode *lower_inode;
3468 + /* ignore if we're called on semi-initialized dentries/inodes */
3469 + if (!dentry || !UNIONFS_D(dentry))
3471 + inode = dentry->d_inode;
3472 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3473 + ibstart(inode) < 0 || ibend(inode) < 0)
3476 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3477 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3481 + /* check if mtime/ctime have changed */
3482 + if (unlikely(timespec_compare(&inode->i_mtime,
3483 + &lower_inode->i_mtime) < 0)) {
3484 + if ((lower_inode->i_mtime.tv_sec -
3485 + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3486 + pr_info("unionfs: new lower inode mtime "
3487 + "(bindex=%d, name=%s)\n", bindex,
3488 + dentry->d_name.name);
3489 + show_dinode_times(dentry);
3493 + if (unlikely(timespec_compare(&inode->i_ctime,
3494 + &lower_inode->i_ctime) < 0)) {
3495 + if ((lower_inode->i_ctime.tv_sec -
3496 + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3497 + pr_info("unionfs: new lower inode ctime "
3498 + "(bindex=%d, name=%s)\n", bindex,
3499 + dentry->d_name.name);
3500 + show_dinode_times(dentry);
3507 + * Last check: if this is a positive dentry, but somehow all lower
3508 + * dentries are negative or unhashed, then this dentry needs to be
3509 + * revalidated, because someone probably deleted the objects from
3510 + * the lower branches directly.
3512 + if (is_negative_lower(dentry))
3515 + return false; /* default: lower is not newer */
3518 +static int unionfs_d_revalidate(struct dentry *dentry,
3519 + struct nameidata *nd_unused)
3521 + bool valid = true;
3522 + int err = 1; /* 1 means valid for the VFS */
3523 + struct dentry *parent;
3525 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3526 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3527 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3529 + valid = __unionfs_d_revalidate(dentry, parent, false);
3531 + unionfs_postcopyup_setmnt(dentry);
3532 + unionfs_check_dentry(dentry);
3537 + unionfs_unlock_dentry(dentry);
3538 + unionfs_unlock_parent(dentry, parent);
3539 + unionfs_read_unlock(dentry->d_sb);
3544 +static void unionfs_d_release(struct dentry *dentry)
3546 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3547 + if (unlikely(!UNIONFS_D(dentry)))
3548 + goto out; /* skip if no lower branches */
3549 + /* must lock our branch configuration here */
3550 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3552 + unionfs_check_dentry(dentry);
3553 + /* this could be a negative dentry, so check first */
3554 + if (dbstart(dentry) < 0) {
3555 + unionfs_unlock_dentry(dentry);
3556 + goto out; /* due to a (normal) failed lookup */
3559 + /* Release all the lower dentries */
3560 + path_put_lowers_all(dentry, true);
3562 + unionfs_unlock_dentry(dentry);
3565 + free_dentry_private_data(dentry);
3566 + unionfs_read_unlock(dentry->d_sb);
3571 + * Called when we're removing the last reference to our dentry. So we
3572 + * should drop all lower references too.
3574 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3579 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3580 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3582 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3583 + goto drop_lower_inodes;
3584 + path_put_lowers_all(dentry, false);
3587 + rc = atomic_read(&inode->i_count);
3588 + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3589 + /* see Documentation/filesystems/unionfs/issues.txt */
3591 + iput(unionfs_lower_inode(inode));
3593 + unionfs_set_lower_inode(inode, NULL);
3594 + /* XXX: may need to set start/end to -1? */
3599 + unionfs_unlock_dentry(dentry);
3600 + unionfs_read_unlock(dentry->d_sb);
3603 +struct dentry_operations unionfs_dops = {
3604 + .d_revalidate = unionfs_d_revalidate,
3605 + .d_release = unionfs_d_release,
3606 + .d_iput = unionfs_d_iput,
3608 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3609 new file mode 100644
3610 index 0000000..eccb9ae
3612 +++ b/fs/unionfs/dirfops.c
3615 + * Copyright (c) 2003-2009 Erez Zadok
3616 + * Copyright (c) 2003-2006 Charles P. Wright
3617 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3618 + * Copyright (c) 2005-2006 Junjiro Okajima
3619 + * Copyright (c) 2005 Arun M. Krishnakumar
3620 + * Copyright (c) 2004-2006 David P. Quigley
3621 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3622 + * Copyright (c) 2003 Puja Gupta
3623 + * Copyright (c) 2003 Harikesavan Krishnan
3624 + * Copyright (c) 2003-2009 Stony Brook University
3625 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3627 + * This program is free software; you can redistribute it and/or modify
3628 + * it under the terms of the GNU General Public License version 2 as
3629 + * published by the Free Software Foundation.
3634 +/* Make sure our rdstate is playing by the rules. */
3635 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3637 + BUG_ON(rdstate->offset >= DIREOF);
3638 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3641 +struct unionfs_getdents_callback {
3642 + struct unionfs_dir_state *rdstate;
3644 + int entries_written;
3645 + int filldir_called;
3646 + int filldir_error;
3647 + filldir_t filldir;
3648 + struct super_block *sb;
3651 +/* based on generic filldir in fs/readir.c */
3652 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3653 + loff_t offset, u64 ino, unsigned int d_type)
3655 + struct unionfs_getdents_callback *buf = dirent;
3656 + struct filldir_node *found = NULL;
3659 + char *name = (char *) oname;
3661 + buf->filldir_called++;
3663 + is_whiteout = is_whiteout_name(&name, &namelen);
3665 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3669 + * If we had non-whiteout entry in dir cache, then mark it
3670 + * as a whiteout and but leave it in the dir cache.
3672 + if (is_whiteout && !found->whiteout)
3673 + found->whiteout = is_whiteout;
3677 + /* if 'name' isn't a whiteout, filldir it. */
3678 + if (!is_whiteout) {
3679 + off_t pos = rdstate2offset(buf->rdstate);
3680 + u64 unionfs_ino = ino;
3682 + err = buf->filldir(buf->dirent, name, namelen, pos,
3683 + unionfs_ino, d_type);
3684 + buf->rdstate->offset++;
3685 + verify_rdstate_offset(buf->rdstate);
3688 + * If we did fill it, stuff it in our hash, otherwise return an
3692 + buf->filldir_error = err;
3695 + buf->entries_written++;
3696 + err = add_filldir_node(buf->rdstate, name, namelen,
3697 + buf->rdstate->bindex, is_whiteout);
3699 + buf->filldir_error = err;
3705 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3708 + struct file *lower_file = NULL;
3709 + struct dentry *dentry = file->f_path.dentry;
3710 + struct dentry *parent;
3711 + struct inode *inode = NULL;
3712 + struct unionfs_getdents_callback buf;
3713 + struct unionfs_dir_state *uds;
3717 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3718 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3719 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3721 + err = unionfs_file_revalidate(file, parent, false);
3722 + if (unlikely(err))
3725 + inode = dentry->d_inode;
3727 + uds = UNIONFS_F(file)->rdstate;
3729 + if (file->f_pos == DIREOF) {
3731 + } else if (file->f_pos > 0) {
3732 + uds = find_rdstate(inode, file->f_pos);
3733 + if (unlikely(!uds)) {
3737 + UNIONFS_F(file)->rdstate = uds;
3739 + init_rdstate(file);
3740 + uds = UNIONFS_F(file)->rdstate;
3743 + bend = fbend(file);
3745 + while (uds->bindex <= bend) {
3746 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3747 + if (!lower_file) {
3753 + /* prepare callback buffer */
3754 + buf.filldir_called = 0;
3755 + buf.filldir_error = 0;
3756 + buf.entries_written = 0;
3757 + buf.dirent = dirent;
3758 + buf.filldir = filldir;
3759 + buf.rdstate = uds;
3760 + buf.sb = inode->i_sb;
3762 + /* Read starting from where we last left off. */
3763 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3768 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3770 + /* Save the position for when we continue. */
3771 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3776 + uds->dirpos = offset;
3778 + /* Copy the atime. */
3779 + fsstack_copy_attr_atime(inode,
3780 + lower_file->f_path.dentry->d_inode);
3785 + if (buf.filldir_error)
3788 + if (!buf.entries_written) {
3794 + if (!buf.filldir_error && uds->bindex >= bend) {
3795 + /* Save the number of hash entries for next time. */
3796 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3797 + free_rdstate(uds);
3798 + UNIONFS_F(file)->rdstate = NULL;
3799 + file->f_pos = DIREOF;
3801 + file->f_pos = rdstate2offset(uds);
3806 + unionfs_check_file(file);
3807 + unionfs_unlock_dentry(dentry);
3808 + unionfs_unlock_parent(dentry, parent);
3809 + unionfs_read_unlock(dentry->d_sb);
3814 + * This is not meant to be a generic repositioning function. If you do
3815 + * things that aren't supported, then we return EINVAL.
3817 + * What is allowed:
3818 + * (1) seeking to the same position that you are currently at
3819 + * This really has no effect, but returns where you are.
3820 + * (2) seeking to the beginning of the file
3821 + * This throws out all state, and lets you begin again.
3823 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3825 + struct unionfs_dir_state *rdstate;
3826 + struct dentry *dentry = file->f_path.dentry;
3827 + struct dentry *parent;
3830 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3831 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3832 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3834 + err = unionfs_file_revalidate(file, parent, false);
3835 + if (unlikely(err))
3838 + rdstate = UNIONFS_F(file)->rdstate;
3841 + * we let users seek to their current position, but not anywhere
3848 + free_rdstate(rdstate);
3849 + UNIONFS_F(file)->rdstate = NULL;
3851 + init_rdstate(file);
3855 + err = file->f_pos;
3858 + /* Unsupported, because we would break everything. */
3866 + if (offset == rdstate2offset(rdstate))
3868 + else if (file->f_pos == DIREOF)
3873 + struct inode *inode;
3874 + inode = dentry->d_inode;
3875 + rdstate = find_rdstate(inode, offset);
3877 + UNIONFS_F(file)->rdstate = rdstate;
3878 + err = rdstate->offset;
3886 + /* Unsupported, because we would break everything. */
3894 + unionfs_check_file(file);
3895 + unionfs_unlock_dentry(dentry);
3896 + unionfs_unlock_parent(dentry, parent);
3897 + unionfs_read_unlock(dentry->d_sb);
3902 + * Trimmed directory options, we shouldn't pass everything down since
3903 + * we don't want to operate on partial directories.
3905 +struct file_operations unionfs_dir_fops = {
3906 + .llseek = unionfs_dir_llseek,
3907 + .read = generic_read_dir,
3908 + .readdir = unionfs_readdir,
3909 + .unlocked_ioctl = unionfs_ioctl,
3910 + .open = unionfs_open,
3911 + .release = unionfs_file_release,
3912 + .flush = unionfs_flush,
3913 + .fsync = unionfs_fsync,
3914 + .fasync = unionfs_fasync,
3916 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3917 new file mode 100644
3918 index 0000000..2ecaafa
3920 +++ b/fs/unionfs/dirhelper.c
3923 + * Copyright (c) 2003-2009 Erez Zadok
3924 + * Copyright (c) 2003-2006 Charles P. Wright
3925 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3926 + * Copyright (c) 2005-2006 Junjiro Okajima
3927 + * Copyright (c) 2005 Arun M. Krishnakumar
3928 + * Copyright (c) 2004-2006 David P. Quigley
3929 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3930 + * Copyright (c) 2003 Puja Gupta
3931 + * Copyright (c) 2003 Harikesavan Krishnan
3932 + * Copyright (c) 2003-2009 Stony Brook University
3933 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3935 + * This program is free software; you can redistribute it and/or modify
3936 + * it under the terms of the GNU General Public License version 2 as
3937 + * published by the Free Software Foundation.
3943 +#define RD_CHECK_EMPTY 1
3944 +/* The callback structure for check_empty. */
3945 +struct unionfs_rdutil_callback {
3947 + int filldir_called;
3948 + struct unionfs_dir_state *rdstate;
3952 +/* This filldir function makes sure only whiteouts exist within a directory. */
3953 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3954 + loff_t offset, u64 ino, unsigned int d_type)
3957 + struct unionfs_rdutil_callback *buf = dirent;
3959 + struct filldir_node *found;
3960 + char *name = (char *) oname;
3962 + buf->filldir_called = 1;
3964 + if (name[0] == '.' && (namelen == 1 ||
3965 + (name[1] == '.' && namelen == 2)))
3968 + is_whiteout = is_whiteout_name(&name, &namelen);
3970 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3971 + /* If it was found in the table there was a previous whiteout. */
3976 + * if it wasn't found and isn't a whiteout, the directory isn't
3980 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3983 + err = add_filldir_node(buf->rdstate, name, namelen,
3984 + buf->rdstate->bindex, is_whiteout);
3991 +/* Is a directory logically empty? */
3992 +int check_empty(struct dentry *dentry, struct dentry *parent,
3993 + struct unionfs_dir_state **namelist)
3996 + struct dentry *lower_dentry = NULL;
3997 + struct vfsmount *mnt;
3998 + struct super_block *sb;
3999 + struct file *lower_file;
4000 + struct unionfs_rdutil_callback *buf = NULL;
4001 + int bindex, bstart, bend, bopaque;
4003 + sb = dentry->d_sb;
4006 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
4008 + err = unionfs_partial_lookup(dentry, parent);
4012 + bstart = dbstart(dentry);
4013 + bend = dbend(dentry);
4014 + bopaque = dbopaque(dentry);
4015 + if (0 <= bopaque && bopaque < bend)
4018 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
4019 + if (unlikely(!buf)) {
4024 + buf->mode = RD_CHECK_EMPTY;
4025 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
4026 + if (unlikely(!buf->rdstate)) {
4031 + /* Process the lower directories with rdutil_callback as a filldir. */
4032 + for (bindex = bstart; bindex <= bend; bindex++) {
4033 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4034 + if (!lower_dentry)
4036 + if (!lower_dentry->d_inode)
4038 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
4041 + dget(lower_dentry);
4042 + mnt = unionfs_mntget(dentry, bindex);
4043 + branchget(sb, bindex);
4044 + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
4045 + if (IS_ERR(lower_file)) {
4046 + err = PTR_ERR(lower_file);
4047 + branchput(sb, bindex);
4052 + buf->filldir_called = 0;
4053 + buf->rdstate->bindex = bindex;
4054 + err = vfs_readdir(lower_file,
4055 + readdir_util_callback, buf);
4058 + } while ((err >= 0) && buf->filldir_called);
4060 + /* fput calls dput for lower_dentry */
4062 + branchput(sb, bindex);
4070 + if (namelist && !err)
4071 + *namelist = buf->rdstate;
4072 + else if (buf->rdstate)
4073 + free_rdstate(buf->rdstate);
4080 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4081 new file mode 100644
4082 index 0000000..04ffa85
4084 +++ b/fs/unionfs/fanout.h
4087 + * Copyright (c) 2003-2009 Erez Zadok
4088 + * Copyright (c) 2003-2006 Charles P. Wright
4089 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4090 + * Copyright (c) 2005 Arun M. Krishnakumar
4091 + * Copyright (c) 2004-2006 David P. Quigley
4092 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4093 + * Copyright (c) 2003 Puja Gupta
4094 + * Copyright (c) 2003 Harikesavan Krishnan
4095 + * Copyright (c) 2003-2009 Stony Brook University
4096 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4098 + * This program is free software; you can redistribute it and/or modify
4099 + * it under the terms of the GNU General Public License version 2 as
4100 + * published by the Free Software Foundation.
4107 + * Inode to private data
4109 + * Since we use containers and the struct inode is _inside_ the
4110 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4111 + * inode pointer), return a valid non-NULL pointer.
4113 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4115 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4118 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4119 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4121 +/* Dentry to private data */
4122 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4123 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4124 +#define dbend(dent) (UNIONFS_D(dent)->bend)
4125 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4127 +/* Superblock to private data */
4128 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4129 +#define sbstart(sb) 0
4130 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4131 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4132 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4134 +/* File to private Data */
4135 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4136 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4137 +#define fbend(file) (UNIONFS_F(file)->bend)
4139 +/* macros to manipulate branch IDs in stored in our superblock */
4140 +static inline int branch_id(struct super_block *sb, int index)
4142 + BUG_ON(!sb || index < 0);
4143 + return UNIONFS_SB(sb)->data[index].branch_id;
4146 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4148 + BUG_ON(!sb || index < 0);
4149 + UNIONFS_SB(sb)->data[index].branch_id = val;
4152 +static inline void new_branch_id(struct super_block *sb, int index)
4154 + BUG_ON(!sb || index < 0);
4155 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4159 + * Find new index of matching branch with an existing superblock of a known
4160 + * (possibly old) id. This is needed because branches could have been
4161 + * added/deleted causing the branches of any open files to shift.
4163 + * @sb: the new superblock which may have new/different branch IDs
4164 + * @id: the old/existing id we're looking for
4165 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4167 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4170 + for (i = 0; i < sbmax(sb); i++) {
4171 + if (branch_id(sb, i) == id)
4174 + /* in the non-ODF code, this should really never happen */
4175 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4179 +/* File to lower file. */
4180 +static inline struct file *unionfs_lower_file(const struct file *f)
4183 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4186 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4189 + BUG_ON(!f || index < 0);
4190 + return UNIONFS_F(f)->lower_files[index];
4193 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4196 + BUG_ON(!f || index < 0);
4197 + UNIONFS_F(f)->lower_files[index] = val;
4198 + /* save branch ID (may be redundant?) */
4199 + UNIONFS_F(f)->saved_branch_ids[index] =
4200 + branch_id((f)->f_path.dentry->d_sb, index);
4203 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4206 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4209 +/* Inode to lower inode. */
4210 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4213 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4216 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4219 + BUG_ON(!i || index < 0);
4220 + return UNIONFS_I(i)->lower_inodes[index];
4223 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4224 + struct inode *val)
4226 + BUG_ON(!i || index < 0);
4227 + UNIONFS_I(i)->lower_inodes[index] = val;
4230 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4233 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4236 +/* Superblock to lower superblock. */
4237 +static inline struct super_block *unionfs_lower_super(
4238 + const struct super_block *sb)
4241 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4244 +static inline struct super_block *unionfs_lower_super_idx(
4245 + const struct super_block *sb,
4248 + BUG_ON(!sb || index < 0);
4249 + return UNIONFS_SB(sb)->data[index].sb;
4252 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4254 + struct super_block *val)
4256 + BUG_ON(!sb || index < 0);
4257 + UNIONFS_SB(sb)->data[index].sb = val;
4260 +static inline void unionfs_set_lower_super(struct super_block *sb,
4261 + struct super_block *val)
4264 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4267 +/* Branch count macros. */
4268 +static inline int branch_count(const struct super_block *sb, int index)
4270 + BUG_ON(!sb || index < 0);
4271 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4274 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4276 + BUG_ON(!sb || index < 0);
4277 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4280 +static inline void branchget(struct super_block *sb, int index)
4282 + BUG_ON(!sb || index < 0);
4283 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4286 +static inline void branchput(struct super_block *sb, int index)
4288 + BUG_ON(!sb || index < 0);
4289 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4292 +/* Dentry macros */
4293 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4294 + struct dentry *val)
4296 + BUG_ON(!dent || index < 0);
4297 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4300 +static inline struct dentry *unionfs_lower_dentry_idx(
4301 + const struct dentry *dent,
4304 + BUG_ON(!dent || index < 0);
4305 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4308 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4311 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4314 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4315 + struct vfsmount *mnt)
4317 + BUG_ON(!dent || index < 0);
4318 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4321 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4322 + const struct dentry *dent,
4325 + BUG_ON(!dent || index < 0);
4326 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4329 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4332 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4335 +/* Macros for locking a dentry. */
4336 +enum unionfs_dentry_lock_class {
4337 + UNIONFS_DMUTEX_NORMAL,
4338 + UNIONFS_DMUTEX_ROOT,
4339 + UNIONFS_DMUTEX_PARENT,
4340 + UNIONFS_DMUTEX_CHILD,
4341 + UNIONFS_DMUTEX_WHITEOUT,
4342 + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4343 + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4346 +static inline void unionfs_lock_dentry(struct dentry *d,
4347 + unsigned int subclass)
4350 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4353 +static inline void unionfs_unlock_dentry(struct dentry *d)
4356 + mutex_unlock(&UNIONFS_D(d)->lock);
4359 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4360 + unsigned int subclass)
4365 + p = dget_parent(d);
4367 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4371 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4376 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4377 + mutex_unlock(&UNIONFS_D(p)->lock);
4382 +static inline void verify_locked(struct dentry *d)
4385 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4388 +/* macros to put lower objects */
4391 + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4392 + * @free_lower is true, then also kfree the memory used to hold the lower
4393 + * object pointers.
4395 +static inline void iput_lowers(struct inode *inode,
4396 + int bstart, int bend, bool free_lower)
4398 + struct inode *lower_inode;
4402 + BUG_ON(!UNIONFS_I(inode));
4403 + BUG_ON(bstart < 0);
4405 + for (bindex = bstart; bindex <= bend; bindex++) {
4406 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4407 + if (lower_inode) {
4408 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4409 + /* see Documentation/filesystems/unionfs/issues.txt */
4411 + iput(lower_inode);
4417 + kfree(UNIONFS_I(inode)->lower_inodes);
4418 + UNIONFS_I(inode)->lower_inodes = NULL;
4422 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4423 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4428 + BUG_ON(!UNIONFS_I(inode));
4429 + bstart = ibstart(inode);
4430 + bend = ibend(inode);
4431 + BUG_ON(bstart < 0);
4433 + iput_lowers(inode, bstart, bend, free_lower);
4434 + ibstart(inode) = ibend(inode) = -1;
4438 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4439 + * bstart to bend. If @free_lower is true, then also kfree the memory used
4440 + * to hold the lower object pointers.
4442 + * XXX: implement using path_put VFS macros
4444 +static inline void path_put_lowers(struct dentry *dentry,
4445 + int bstart, int bend, bool free_lower)
4447 + struct dentry *lower_dentry;
4448 + struct vfsmount *lower_mnt;
4452 + BUG_ON(!UNIONFS_D(dentry));
4453 + BUG_ON(bstart < 0);
4455 + for (bindex = bstart; bindex <= bend; bindex++) {
4456 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4457 + if (lower_dentry) {
4458 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4459 + dput(lower_dentry);
4461 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4463 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4464 + mntput(lower_mnt);
4469 + kfree(UNIONFS_D(dentry)->lower_paths);
4470 + UNIONFS_D(dentry)->lower_paths = NULL;
4475 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4478 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4483 + BUG_ON(!UNIONFS_D(dentry));
4484 + bstart = dbstart(dentry);
4485 + bend = dbend(dentry);
4486 + BUG_ON(bstart < 0);
4488 + path_put_lowers(dentry, bstart, bend, free_lower);
4489 + dbstart(dentry) = dbend(dentry) = -1;
4492 +#endif /* not _FANOUT_H */
4493 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4494 new file mode 100644
4495 index 0000000..281169e
4497 +++ b/fs/unionfs/file.c
4500 + * Copyright (c) 2003-2009 Erez Zadok
4501 + * Copyright (c) 2003-2006 Charles P. Wright
4502 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4503 + * Copyright (c) 2005-2006 Junjiro Okajima
4504 + * Copyright (c) 2005 Arun M. Krishnakumar
4505 + * Copyright (c) 2004-2006 David P. Quigley
4506 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4507 + * Copyright (c) 2003 Puja Gupta
4508 + * Copyright (c) 2003 Harikesavan Krishnan
4509 + * Copyright (c) 2003-2009 Stony Brook University
4510 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4512 + * This program is free software; you can redistribute it and/or modify
4513 + * it under the terms of the GNU General Public License version 2 as
4514 + * published by the Free Software Foundation.
4519 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4520 + size_t count, loff_t *ppos)
4523 + struct file *lower_file;
4524 + struct dentry *dentry = file->f_path.dentry;
4525 + struct dentry *parent;
4527 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4528 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4529 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4531 + err = unionfs_file_revalidate(file, parent, false);
4532 + if (unlikely(err))
4535 + lower_file = unionfs_lower_file(file);
4536 + err = vfs_read(lower_file, buf, count, ppos);
4537 + /* update our inode atime upon a successful lower read */
4539 + fsstack_copy_attr_atime(dentry->d_inode,
4540 + lower_file->f_path.dentry->d_inode);
4541 + unionfs_check_file(file);
4545 + unionfs_unlock_dentry(dentry);
4546 + unionfs_unlock_parent(dentry, parent);
4547 + unionfs_read_unlock(dentry->d_sb);
4551 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4552 + size_t count, loff_t *ppos)
4555 + struct file *lower_file;
4556 + struct dentry *dentry = file->f_path.dentry;
4557 + struct dentry *parent;
4559 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4560 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4561 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4563 + err = unionfs_file_revalidate(file, parent, true);
4564 + if (unlikely(err))
4567 + lower_file = unionfs_lower_file(file);
4568 + err = vfs_write(lower_file, buf, count, ppos);
4569 + /* update our inode times+sizes upon a successful lower write */
4571 + fsstack_copy_inode_size(dentry->d_inode,
4572 + lower_file->f_path.dentry->d_inode);
4573 + fsstack_copy_attr_times(dentry->d_inode,
4574 + lower_file->f_path.dentry->d_inode);
4575 + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4576 + unionfs_check_file(file);
4580 + unionfs_unlock_dentry(dentry);
4581 + unionfs_unlock_parent(dentry, parent);
4582 + unionfs_read_unlock(dentry->d_sb);
4586 +static int unionfs_file_readdir(struct file *file, void *dirent,
4587 + filldir_t filldir)
4592 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4596 + struct file *lower_file;
4597 + struct dentry *dentry = file->f_path.dentry;
4598 + struct dentry *parent;
4599 + struct vm_operations_struct *saved_vm_ops = NULL;
4602 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4603 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4604 + * has been causing false positives in file system stacking layers.
4605 + * In particular, our ->mmap is called after sys_mmap2 already holds
4606 + * mmap_sem, then we lock our own mutexes; but earlier, it's
4607 + * possible for lockdep to have locked our mutexes first, and then
4608 + * we call a lower ->readdir which could call might_fault. The
4609 + * different ordering of the locks is what lockdep complains about
4610 + * -- unnecessarily. Therefore, we have no choice but to tell
4611 + * lockdep to temporarily turn off lockdep here. Note: the comments
4612 + * inside might_sleep also suggest that it would have been
4613 + * nicer to only annotate paths that needs that might_lock_read.
4616 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4617 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4618 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4620 + /* This might be deferred to mmap's writepage */
4621 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4622 + err = unionfs_file_revalidate(file, parent, willwrite);
4623 + if (unlikely(err))
4625 + unionfs_check_file(file);
4628 + * File systems which do not implement ->writepage may use
4629 + * generic_file_readonly_mmap as their ->mmap op. If you call
4630 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4631 + * But we cannot call the lower ->mmap op, so we can't tell that
4632 + * writeable mappings won't work. Therefore, our only choice is to
4633 + * check if the lower file system supports the ->writepage, and if
4634 + * not, return EINVAL (the same error that
4635 + * generic_file_readonly_mmap returns in that case).
4637 + lower_file = unionfs_lower_file(file);
4638 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4640 + printk(KERN_ERR "unionfs: branch %d file system does not "
4641 + "support writeable mmap\n", fbstart(file));
4646 + * find and save lower vm_ops.
4648 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4650 + if (!UNIONFS_F(file)->lower_vm_ops) {
4651 + err = lower_file->f_op->mmap(lower_file, vma);
4653 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4656 + saved_vm_ops = vma->vm_ops;
4657 + err = do_munmap(current->mm, vma->vm_start,
4658 + vma->vm_end - vma->vm_start);
4660 + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4665 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4666 + err = generic_file_mmap(file, vma);
4667 + file->f_mapping->a_ops = &unionfs_aops;
4669 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4672 + vma->vm_ops = &unionfs_vm_ops;
4673 + if (!UNIONFS_F(file)->lower_vm_ops)
4674 + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4678 + /* copyup could cause parent dir times to change */
4679 + unionfs_copy_attr_times(parent->d_inode);
4680 + unionfs_check_file(file);
4682 + unionfs_unlock_dentry(dentry);
4683 + unionfs_unlock_parent(dentry, parent);
4684 + unionfs_read_unlock(dentry->d_sb);
4689 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4691 + int bindex, bstart, bend;
4692 + struct file *lower_file;
4693 + struct dentry *lower_dentry;
4694 + struct dentry *parent;
4695 + struct inode *lower_inode, *inode;
4696 + int err = -EINVAL;
4698 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4699 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4700 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4702 + err = unionfs_file_revalidate(file, parent, true);
4703 + if (unlikely(err))
4705 + unionfs_check_file(file);
4707 + bstart = fbstart(file);
4708 + bend = fbend(file);
4709 + if (bstart < 0 || bend < 0)
4712 + inode = dentry->d_inode;
4713 + if (unlikely(!inode)) {
4715 + "unionfs: null lower inode in unionfs_fsync\n");
4718 + for (bindex = bstart; bindex <= bend; bindex++) {
4719 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4720 + if (!lower_inode || !lower_inode->i_fop->fsync)
4722 + lower_file = unionfs_lower_file_idx(file, bindex);
4723 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4724 + mutex_lock(&lower_inode->i_mutex);
4725 + err = lower_inode->i_fop->fsync(lower_file,
4728 + if (!err && bindex == bstart)
4729 + fsstack_copy_attr_times(inode, lower_inode);
4730 + mutex_unlock(&lower_inode->i_mutex);
4737 + unionfs_check_file(file);
4738 + unionfs_unlock_dentry(dentry);
4739 + unionfs_unlock_parent(dentry, parent);
4740 + unionfs_read_unlock(dentry->d_sb);
4744 +int unionfs_fasync(int fd, struct file *file, int flag)
4746 + int bindex, bstart, bend;
4747 + struct file *lower_file;
4748 + struct dentry *dentry = file->f_path.dentry;
4749 + struct dentry *parent;
4750 + struct inode *lower_inode, *inode;
4753 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4754 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4755 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4757 + err = unionfs_file_revalidate(file, parent, true);
4758 + if (unlikely(err))
4760 + unionfs_check_file(file);
4762 + bstart = fbstart(file);
4763 + bend = fbend(file);
4764 + if (bstart < 0 || bend < 0)
4767 + inode = dentry->d_inode;
4768 + if (unlikely(!inode)) {
4770 + "unionfs: null lower inode in unionfs_fasync\n");
4773 + for (bindex = bstart; bindex <= bend; bindex++) {
4774 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4775 + if (!lower_inode || !lower_inode->i_fop->fasync)
4777 + lower_file = unionfs_lower_file_idx(file, bindex);
4778 + mutex_lock(&lower_inode->i_mutex);
4779 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4780 + if (!err && bindex == bstart)
4781 + fsstack_copy_attr_times(inode, lower_inode);
4782 + mutex_unlock(&lower_inode->i_mutex);
4789 + unionfs_check_file(file);
4790 + unionfs_unlock_dentry(dentry);
4791 + unionfs_unlock_parent(dentry, parent);
4792 + unionfs_read_unlock(dentry->d_sb);
4796 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4797 + struct pipe_inode_info *pipe, size_t len,
4798 + unsigned int flags)
4801 + struct file *lower_file;
4802 + struct dentry *dentry = file->f_path.dentry;
4803 + struct dentry *parent;
4805 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4806 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4807 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4809 + err = unionfs_file_revalidate(file, parent, false);
4810 + if (unlikely(err))
4813 + lower_file = unionfs_lower_file(file);
4814 + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4815 + /* update our inode atime upon a successful lower splice-read */
4817 + fsstack_copy_attr_atime(dentry->d_inode,
4818 + lower_file->f_path.dentry->d_inode);
4819 + unionfs_check_file(file);
4823 + unionfs_unlock_dentry(dentry);
4824 + unionfs_unlock_parent(dentry, parent);
4825 + unionfs_read_unlock(dentry->d_sb);
4829 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4830 + struct file *file, loff_t *ppos,
4831 + size_t len, unsigned int flags)
4834 + struct file *lower_file;
4835 + struct dentry *dentry = file->f_path.dentry;
4836 + struct dentry *parent;
4838 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4839 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4840 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4842 + err = unionfs_file_revalidate(file, parent, true);
4843 + if (unlikely(err))
4846 + lower_file = unionfs_lower_file(file);
4847 + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4848 + /* update our inode times+sizes upon a successful lower write */
4850 + fsstack_copy_inode_size(dentry->d_inode,
4851 + lower_file->f_path.dentry->d_inode);
4852 + fsstack_copy_attr_times(dentry->d_inode,
4853 + lower_file->f_path.dentry->d_inode);
4854 + unionfs_check_file(file);
4858 + unionfs_unlock_dentry(dentry);
4859 + unionfs_unlock_parent(dentry, parent);
4860 + unionfs_read_unlock(dentry->d_sb);
4864 +struct file_operations unionfs_main_fops = {
4865 + .llseek = generic_file_llseek,
4866 + .read = unionfs_read,
4867 + .write = unionfs_write,
4868 + .readdir = unionfs_file_readdir,
4869 + .unlocked_ioctl = unionfs_ioctl,
4870 + .mmap = unionfs_mmap,
4871 + .open = unionfs_open,
4872 + .flush = unionfs_flush,
4873 + .release = unionfs_file_release,
4874 + .fsync = unionfs_fsync,
4875 + .fasync = unionfs_fasync,
4876 + .splice_read = unionfs_splice_read,
4877 + .splice_write = unionfs_splice_write,
4879 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4880 new file mode 100644
4881 index 0000000..7c17093
4883 +++ b/fs/unionfs/inode.c
4886 + * Copyright (c) 2003-2009 Erez Zadok
4887 + * Copyright (c) 2003-2006 Charles P. Wright
4888 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4889 + * Copyright (c) 2005-2006 Junjiro Okajima
4890 + * Copyright (c) 2005 Arun M. Krishnakumar
4891 + * Copyright (c) 2004-2006 David P. Quigley
4892 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4893 + * Copyright (c) 2003 Puja Gupta
4894 + * Copyright (c) 2003 Harikesavan Krishnan
4895 + * Copyright (c) 2003-2009 Stony Brook University
4896 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4898 + * This program is free software; you can redistribute it and/or modify
4899 + * it under the terms of the GNU General Public License version 2 as
4900 + * published by the Free Software Foundation.
4906 + * Find a writeable branch to create new object in. Checks all writeble
4907 + * branches of the parent inode, from istart to iend order; if none are
4908 + * suitable, also tries branch 0 (which may require a copyup).
4910 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4912 +static struct dentry *find_writeable_branch(struct inode *parent,
4913 + struct dentry *dentry)
4915 + int err = -EINVAL;
4916 + int bindex, istart, iend;
4917 + struct dentry *lower_dentry = NULL;
4919 + istart = ibstart(parent);
4920 + iend = ibend(parent);
4925 + for (bindex = istart; bindex <= iend; bindex++) {
4926 + /* skip non-writeable branches */
4927 + err = is_robranch_super(dentry->d_sb, bindex);
4932 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4933 + if (!lower_dentry)
4936 + * check for whiteouts in writeable branch, and remove them
4939 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4940 + if (err > 0) /* ignore if whiteout found and removed */
4944 + /* if get here, we can write to the branch */
4948 + * If istart wasn't already branch 0, and we got any error, then try
4949 + * branch 0 (which may require copyup)
4951 + if (err && istart > 0) {
4952 + istart = iend = 0;
4957 + * If we tried even branch 0, and still got an error, abort. But if
4958 + * the error was an EROFS, then we should try to copyup.
4960 + if (err && err != -EROFS)
4964 + * If we get here, then check if copyup needed. If lower_dentry is
4965 + * NULL, create the entire dentry directory structure in branch 0.
4967 + if (!lower_dentry) {
4969 + lower_dentry = create_parents(parent, dentry,
4970 + dentry->d_name.name, bindex);
4971 + if (IS_ERR(lower_dentry)) {
4972 + err = PTR_ERR(lower_dentry);
4976 + err = 0; /* all's well */
4979 + return ERR_PTR(err);
4980 + return lower_dentry;
4983 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4984 + int mode, struct nameidata *nd_unused)
4987 + struct dentry *lower_dentry = NULL;
4988 + struct dentry *lower_parent_dentry = NULL;
4989 + struct dentry *parent;
4991 + struct nameidata lower_nd;
4993 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4994 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4995 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4997 + valid = __unionfs_d_revalidate(dentry, parent, false);
4998 + if (unlikely(!valid)) {
4999 + err = -ESTALE; /* same as what real_lookup does */
5003 + lower_dentry = find_writeable_branch(dir, dentry);
5004 + if (IS_ERR(lower_dentry)) {
5005 + err = PTR_ERR(lower_dentry);
5009 + lower_parent_dentry = lock_parent(lower_dentry);
5010 + if (IS_ERR(lower_parent_dentry)) {
5011 + err = PTR_ERR(lower_parent_dentry);
5015 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
5016 + if (unlikely(err < 0))
5018 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
5020 + release_lower_nd(&lower_nd, err);
5023 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5025 + unionfs_copy_attr_times(dir);
5026 + fsstack_copy_inode_size(dir,
5027 + lower_parent_dentry->d_inode);
5028 + /* update no. of links on parent directory */
5029 + dir->i_nlink = unionfs_get_nlinks(dir);
5033 + unlock_dir(lower_parent_dentry);
5037 + unionfs_postcopyup_setmnt(dentry);
5038 + unionfs_check_inode(dir);
5039 + unionfs_check_dentry(dentry);
5041 + unionfs_unlock_dentry(dentry);
5042 + unionfs_unlock_parent(dentry, parent);
5043 + unionfs_read_unlock(dentry->d_sb);
5048 + * unionfs_lookup is the only special function which takes a dentry, yet we
5049 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
5050 + * we don't have a valid dentry here yet.
5052 +static struct dentry *unionfs_lookup(struct inode *dir,
5053 + struct dentry *dentry,
5054 + struct nameidata *nd_unused)
5056 + struct dentry *ret, *parent;
5059 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5060 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5063 + * As long as we lock/dget the parent, then can skip validating the
5064 + * parent now; we may have to rebuild this dentry on the next
5065 + * ->d_revalidate, however.
5068 + /* allocate dentry private data. We free it in ->d_release */
5069 + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
5070 + if (unlikely(err)) {
5071 + ret = ERR_PTR(err);
5075 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
5077 + if (!IS_ERR(ret)) {
5080 + /* lookup_full can return multiple positive dentries */
5081 + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5082 + BUG_ON(dbstart(dentry) < 0);
5083 + unionfs_postcopyup_release(dentry);
5085 + unionfs_copy_attr_times(dentry->d_inode);
5088 + unionfs_check_inode(dir);
5090 + unionfs_check_dentry(dentry);
5091 + unionfs_check_dentry(parent);
5092 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5095 + unionfs_unlock_parent(dentry, parent);
5096 + unionfs_read_unlock(dentry->d_sb);
5101 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5102 + struct dentry *new_dentry)
5105 + struct dentry *lower_old_dentry = NULL;
5106 + struct dentry *lower_new_dentry = NULL;
5107 + struct dentry *lower_dir_dentry = NULL;
5108 + struct dentry *old_parent, *new_parent;
5109 + char *name = NULL;
5112 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5113 + old_parent = dget_parent(old_dentry);
5114 + new_parent = dget_parent(new_dentry);
5115 + unionfs_double_lock_parents(old_parent, new_parent);
5116 + unionfs_double_lock_dentry(old_dentry, new_dentry);
5118 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5119 + if (unlikely(!valid)) {
5123 + if (new_dentry->d_inode) {
5124 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5125 + if (unlikely(!valid)) {
5131 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5133 + /* check for a whiteout in new dentry branch, and delete it */
5134 + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5135 + dbstart(new_dentry));
5136 + if (err > 0) { /* whiteout found and removed successfully */
5137 + lower_dir_dentry = dget_parent(lower_new_dentry);
5138 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5139 + dput(lower_dir_dentry);
5140 + dir->i_nlink = unionfs_get_nlinks(dir);
5146 + /* check if parent hierachy is needed, then link in same branch */
5147 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5148 + lower_new_dentry = create_parents(dir, new_dentry,
5149 + new_dentry->d_name.name,
5150 + dbstart(old_dentry));
5151 + err = PTR_ERR(lower_new_dentry);
5152 + if (IS_COPYUP_ERR(err))
5154 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5157 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5158 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5160 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5161 + lower_dir_dentry = lock_parent(lower_new_dentry);
5162 + err = is_robranch(old_dentry);
5164 + /* see Documentation/filesystems/unionfs/issues.txt */
5166 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5167 + lower_new_dentry);
5170 + unlock_dir(lower_dir_dentry);
5173 + if (IS_COPYUP_ERR(err)) {
5174 + int old_bstart = dbstart(old_dentry);
5177 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5178 + err = copyup_dentry(old_parent->d_inode,
5179 + old_dentry, old_bstart,
5180 + bindex, old_dentry->d_name.name,
5181 + old_dentry->d_name.len, NULL,
5182 + i_size_read(old_dentry->d_inode));
5185 + lower_new_dentry =
5186 + create_parents(dir, new_dentry,
5187 + new_dentry->d_name.name,
5189 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5190 + lower_dir_dentry = lock_parent(lower_new_dentry);
5191 + /* see Documentation/filesystems/unionfs/issues.txt */
5194 + err = vfs_link(lower_old_dentry,
5195 + lower_dir_dentry->d_inode,
5196 + lower_new_dentry);
5198 + unlock_dir(lower_dir_dentry);
5205 + if (err || !lower_new_dentry->d_inode)
5208 + /* Its a hard link, so use the same inode */
5209 + new_dentry->d_inode = igrab(old_dentry->d_inode);
5210 + d_add(new_dentry, new_dentry->d_inode);
5211 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5212 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5214 + /* propagate number of hard-links */
5215 + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5216 + /* new dentry's ctime may have changed due to hard-link counts */
5217 + unionfs_copy_attr_times(new_dentry->d_inode);
5220 + if (!new_dentry->d_inode)
5221 + d_drop(new_dentry);
5225 + unionfs_postcopyup_setmnt(new_dentry);
5227 + unionfs_check_inode(dir);
5228 + unionfs_check_dentry(new_dentry);
5229 + unionfs_check_dentry(old_dentry);
5231 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5232 + unionfs_double_unlock_parents(old_parent, new_parent);
5235 + unionfs_read_unlock(old_dentry->d_sb);
5240 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5241 + const char *symname)
5244 + struct dentry *lower_dentry = NULL;
5245 + struct dentry *wh_dentry = NULL;
5246 + struct dentry *lower_parent_dentry = NULL;
5247 + struct dentry *parent;
5248 + char *name = NULL;
5252 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5253 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5254 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5256 + valid = __unionfs_d_revalidate(dentry, parent, false);
5257 + if (unlikely(!valid)) {
5263 + * It's only a bug if this dentry was not negative and couldn't be
5264 + * revalidated (shouldn't happen).
5266 + BUG_ON(!valid && dentry->d_inode);
5268 + lower_dentry = find_writeable_branch(dir, dentry);
5269 + if (IS_ERR(lower_dentry)) {
5270 + err = PTR_ERR(lower_dentry);
5274 + lower_parent_dentry = lock_parent(lower_dentry);
5275 + if (IS_ERR(lower_parent_dentry)) {
5276 + err = PTR_ERR(lower_parent_dentry);
5281 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5283 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5285 + unionfs_copy_attr_times(dir);
5286 + fsstack_copy_inode_size(dir,
5287 + lower_parent_dentry->d_inode);
5288 + /* update no. of links on parent directory */
5289 + dir->i_nlink = unionfs_get_nlinks(dir);
5293 + unlock_dir(lower_parent_dentry);
5300 + unionfs_postcopyup_setmnt(dentry);
5301 + unionfs_check_inode(dir);
5302 + unionfs_check_dentry(dentry);
5304 + unionfs_unlock_dentry(dentry);
5305 + unionfs_unlock_parent(dentry, parent);
5306 + unionfs_read_unlock(dentry->d_sb);
5310 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5313 + struct dentry *lower_dentry = NULL;
5314 + struct dentry *lower_parent_dentry = NULL;
5315 + struct dentry *parent;
5316 + int bindex = 0, bstart;
5317 + char *name = NULL;
5320 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5321 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5322 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5324 + valid = __unionfs_d_revalidate(dentry, parent, false);
5325 + if (unlikely(!valid)) {
5326 + err = -ESTALE; /* same as what real_lookup does */
5330 + bstart = dbstart(dentry);
5332 + lower_dentry = unionfs_lower_dentry(dentry);
5334 + /* check for a whiteout in new dentry branch, and delete it */
5335 + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5336 + if (err > 0) /* whiteout found and removed successfully */
5339 + /* exit if the error returned was NOT -EROFS */
5340 + if (!IS_COPYUP_ERR(err))
5345 + /* check if copyup's needed, and mkdir */
5346 + for (bindex = bstart; bindex >= 0; bindex--) {
5348 + int bend = dbend(dentry);
5350 + if (is_robranch_super(dentry->d_sb, bindex))
5353 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5354 + if (!lower_dentry) {
5355 + lower_dentry = create_parents(dir, dentry,
5356 + dentry->d_name.name,
5358 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5359 + printk(KERN_ERR "unionfs: lower dentry "
5360 + " NULL for bindex = %d\n", bindex);
5365 + lower_parent_dentry = lock_parent(lower_dentry);
5367 + if (IS_ERR(lower_parent_dentry)) {
5368 + err = PTR_ERR(lower_parent_dentry);
5372 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5375 + unlock_dir(lower_parent_dentry);
5377 + /* did the mkdir succeed? */
5381 + for (i = bindex + 1; i <= bend; i++) {
5382 + /* XXX: use path_put_lowers? */
5383 + if (unionfs_lower_dentry_idx(dentry, i)) {
5384 + dput(unionfs_lower_dentry_idx(dentry, i));
5385 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5388 + dbend(dentry) = bindex;
5391 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5394 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5396 + unionfs_copy_attr_times(dir);
5397 + fsstack_copy_inode_size(dir,
5398 + lower_parent_dentry->d_inode);
5400 + /* update number of links on parent directory */
5401 + dir->i_nlink = unionfs_get_nlinks(dir);
5404 + err = make_dir_opaque(dentry, dbstart(dentry));
5406 + printk(KERN_ERR "unionfs: mkdir: error creating "
5407 + ".wh.__dir_opaque: %d\n", err);
5411 + /* we are done! */
5416 + if (!dentry->d_inode)
5422 + unionfs_copy_attr_times(dentry->d_inode);
5423 + unionfs_postcopyup_setmnt(dentry);
5425 + unionfs_check_inode(dir);
5426 + unionfs_check_dentry(dentry);
5427 + unionfs_unlock_dentry(dentry);
5428 + unionfs_unlock_parent(dentry, parent);
5429 + unionfs_read_unlock(dentry->d_sb);
5434 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5438 + struct dentry *lower_dentry = NULL;
5439 + struct dentry *wh_dentry = NULL;
5440 + struct dentry *lower_parent_dentry = NULL;
5441 + struct dentry *parent;
5442 + char *name = NULL;
5445 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5446 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5447 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5449 + valid = __unionfs_d_revalidate(dentry, parent, false);
5450 + if (unlikely(!valid)) {
5456 + * It's only a bug if this dentry was not negative and couldn't be
5457 + * revalidated (shouldn't happen).
5459 + BUG_ON(!valid && dentry->d_inode);
5461 + lower_dentry = find_writeable_branch(dir, dentry);
5462 + if (IS_ERR(lower_dentry)) {
5463 + err = PTR_ERR(lower_dentry);
5467 + lower_parent_dentry = lock_parent(lower_dentry);
5468 + if (IS_ERR(lower_parent_dentry)) {
5469 + err = PTR_ERR(lower_parent_dentry);
5473 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5475 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5477 + unionfs_copy_attr_times(dir);
5478 + fsstack_copy_inode_size(dir,
5479 + lower_parent_dentry->d_inode);
5480 + /* update no. of links on parent directory */
5481 + dir->i_nlink = unionfs_get_nlinks(dir);
5485 + unlock_dir(lower_parent_dentry);
5492 + unionfs_postcopyup_setmnt(dentry);
5493 + unionfs_check_inode(dir);
5494 + unionfs_check_dentry(dentry);
5496 + unionfs_unlock_dentry(dentry);
5497 + unionfs_unlock_parent(dentry, parent);
5498 + unionfs_read_unlock(dentry->d_sb);
5502 +/* requires sb, dentry, and parent to already be locked */
5503 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5507 + struct dentry *lower_dentry;
5509 + lower_dentry = unionfs_lower_dentry(dentry);
5511 + if (!lower_dentry->d_inode->i_op ||
5512 + !lower_dentry->d_inode->i_op->readlink) {
5517 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5520 + fsstack_copy_attr_atime(dentry->d_inode,
5521 + lower_dentry->d_inode);
5527 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5531 + struct dentry *parent;
5533 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5534 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5535 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5537 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5542 + err = __unionfs_readlink(dentry, buf, bufsiz);
5545 + unionfs_check_dentry(dentry);
5546 + unionfs_unlock_dentry(dentry);
5547 + unionfs_unlock_parent(dentry, parent);
5548 + unionfs_read_unlock(dentry->d_sb);
5553 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5556 + int len = PAGE_SIZE, err;
5557 + mm_segment_t old_fs;
5558 + struct dentry *parent;
5560 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5561 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5562 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5564 + /* This is freed by the put_link method assuming a successful call. */
5565 + buf = kmalloc(len, GFP_KERNEL);
5566 + if (unlikely(!buf)) {
5571 + /* read the symlink, and then we will follow it */
5572 + old_fs = get_fs();
5573 + set_fs(KERNEL_DS);
5574 + err = __unionfs_readlink(dentry, buf, len);
5582 + nd_set_link(nd, buf);
5587 + unionfs_check_nd(nd);
5588 + unionfs_check_dentry(dentry);
5591 + unionfs_unlock_dentry(dentry);
5592 + unionfs_unlock_parent(dentry, parent);
5593 + unionfs_read_unlock(dentry->d_sb);
5595 + return ERR_PTR(err);
5598 +/* this @nd *IS* still used */
5599 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5602 + struct dentry *parent;
5604 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5605 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5606 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5608 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5610 + "unionfs: put_link failed to revalidate dentry\n");
5612 + unionfs_check_dentry(dentry);
5613 + unionfs_check_nd(nd);
5614 + kfree(nd_get_link(nd));
5615 + unionfs_unlock_dentry(dentry);
5616 + unionfs_unlock_parent(dentry, parent);
5617 + unionfs_read_unlock(dentry->d_sb);
5621 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5622 + * skips over EROFS tests (because we perform copyup on EROFS).
5624 +static int __inode_permission(struct inode *inode, int mask)
5628 + /* nobody gets write access to an immutable file */
5629 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5632 + /* Ordinary permission routines do not understand MAY_APPEND. */
5633 + if (inode->i_op && inode->i_op->permission) {
5634 + retval = inode->i_op->permission(inode, mask);
5637 + * Exec permission on a regular file is denied if none
5638 + * of the execute bits are set.
5640 + * This check should be done by the ->permission()
5643 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5644 + !(inode->i_mode & S_IXUGO))
5648 + retval = generic_permission(inode, mask, NULL);
5653 + return security_inode_permission(inode,
5654 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5658 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5659 + * a deadlock with the branch-management "add branch" code (which grabbed
5660 + * the write lock). It is safe to not grab the read lock here, because even
5661 + * with branch management taking place, there is no chance that
5662 + * unionfs_permission, or anything it calls, will use stale branch
5665 +static int unionfs_permission(struct inode *inode, int mask)
5667 + struct inode *lower_inode = NULL;
5669 + int bindex, bstart, bend;
5670 + const int is_file = !S_ISDIR(inode->i_mode);
5671 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5672 + struct inode *inode_grabbed = igrab(inode);
5673 + struct dentry *dentry = d_find_alias(inode);
5676 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5678 + if (!UNIONFS_I(inode)->lower_inodes) {
5679 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5680 + err = -ESTALE; /* force revalidate */
5683 + bstart = ibstart(inode);
5684 + bend = ibend(inode);
5685 + if (unlikely(bstart < 0 || bend < 0)) {
5687 + * With branch-management, we can get a stale inode here.
5688 + * If so, we return ESTALE back to link_path_walk, which
5689 + * would discard the dcache entry and re-lookup the
5690 + * dentry+inode. This should be equivalent to issuing
5691 + * __unionfs_d_revalidate_chain on nd.dentry here.
5693 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5694 + err = -ESTALE; /* force revalidate */
5698 + for (bindex = bstart; bindex <= bend; bindex++) {
5699 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5704 + * check the condition for D-F-D underlying files/directories,
5705 + * we don't have to check for files, if we are checking for
5708 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5712 + * We check basic permissions, but we ignore any conditions
5713 + * such as readonly file systems or branches marked as
5714 + * readonly, because those conditions should lead to a
5715 + * copyup taking place later on. However, if user never had
5716 + * access to the file, then no copyup could ever take place.
5718 + err = __inode_permission(lower_inode, mask);
5719 + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5720 + umode_t mode = lower_inode->i_mode;
5721 + if ((is_robranch_super(inode->i_sb, bindex) ||
5722 + IS_RDONLY(lower_inode)) &&
5723 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5725 + if (IS_COPYUP_ERR(err))
5730 + * The permissions are an intersection of the overall directory
5731 + * permissions, so we fail if one fails.
5736 + /* only the leftmost file matters. */
5737 + if (is_file || write_mask) {
5738 + if (is_file && write_mask) {
5739 + err = get_write_access(lower_inode);
5741 + put_write_access(lower_inode);
5746 + /* sync times which may have changed (asynchronously) below */
5747 + unionfs_copy_attr_times(inode);
5750 + unionfs_check_inode(inode);
5752 + unionfs_unlock_dentry(dentry);
5755 + iput(inode_grabbed);
5759 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5762 + struct dentry *lower_dentry;
5763 + struct dentry *parent;
5764 + struct inode *inode;
5765 + struct inode *lower_inode;
5766 + int bstart, bend, bindex;
5769 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5770 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5771 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5773 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5778 + bstart = dbstart(dentry);
5779 + bend = dbend(dentry);
5780 + inode = dentry->d_inode;
5783 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5784 + * to reinterpret it in its own way.
5786 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5787 + ia->ia_valid &= ~ATTR_MODE;
5789 + lower_dentry = unionfs_lower_dentry(dentry);
5790 + if (!lower_dentry) { /* should never happen after above revalidate */
5794 + lower_inode = unionfs_lower_inode(inode);
5796 + /* check if user has permission to change lower inode */
5797 + err = inode_change_ok(lower_inode, ia);
5801 + /* copyup if the file is on a read only branch */
5802 + if (is_robranch_super(dentry->d_sb, bstart)
5803 + || IS_RDONLY(lower_inode)) {
5804 + /* check if we have a branch to copy up to */
5805 + if (bstart <= 0) {
5810 + if (ia->ia_valid & ATTR_SIZE)
5811 + size = ia->ia_size;
5813 + size = i_size_read(inode);
5814 + /* copyup to next available branch */
5815 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5816 + err = copyup_dentry(parent->d_inode,
5817 + dentry, bstart, bindex,
5818 + dentry->d_name.name,
5819 + dentry->d_name.len,
5826 + /* get updated lower_dentry/inode after copyup */
5827 + lower_dentry = unionfs_lower_dentry(dentry);
5828 + lower_inode = unionfs_lower_inode(inode);
5832 + * If shrinking, first truncate upper level to cancel writing dirty
5833 + * pages beyond the new eof; and also if its' maxbytes is more
5834 + * limiting (fail with -EFBIG before making any change to the lower
5835 + * level). There is no need to vmtruncate the upper level
5836 + * afterwards in the other cases: we fsstack_copy_inode_size from
5837 + * the lower level.
5839 + if (ia->ia_valid & ATTR_SIZE) {
5840 + size = i_size_read(inode);
5841 + if (ia->ia_size < size || (ia->ia_size > size &&
5842 + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5843 + err = vmtruncate(inode, ia->ia_size);
5849 + /* notify the (possibly copied-up) lower inode */
5850 + mutex_lock(&lower_inode->i_mutex);
5851 + err = notify_change(lower_dentry, ia);
5852 + mutex_unlock(&lower_inode->i_mutex);
5856 + /* get attributes from the first lower inode */
5857 + unionfs_copy_attr_all(inode, lower_inode);
5859 + * unionfs_copy_attr_all will copy the lower times to our inode if
5860 + * the lower ones are newer (useful for cache coherency). However,
5861 + * ->setattr is the only place in which we may have to copy the
5862 + * lower inode times absolutely, to support utimes(2).
5864 + if (ia->ia_valid & ATTR_MTIME_SET)
5865 + inode->i_mtime = lower_inode->i_mtime;
5866 + if (ia->ia_valid & ATTR_CTIME)
5867 + inode->i_ctime = lower_inode->i_ctime;
5868 + if (ia->ia_valid & ATTR_ATIME_SET)
5869 + inode->i_atime = lower_inode->i_atime;
5870 + fsstack_copy_inode_size(inode, lower_inode);
5874 + unionfs_check_dentry(dentry);
5875 + unionfs_unlock_dentry(dentry);
5876 + unionfs_unlock_parent(dentry, parent);
5877 + unionfs_read_unlock(dentry->d_sb);
5882 +struct inode_operations unionfs_symlink_iops = {
5883 + .readlink = unionfs_readlink,
5884 + .permission = unionfs_permission,
5885 + .follow_link = unionfs_follow_link,
5886 + .setattr = unionfs_setattr,
5887 + .put_link = unionfs_put_link,
5890 +struct inode_operations unionfs_dir_iops = {
5891 + .create = unionfs_create,
5892 + .lookup = unionfs_lookup,
5893 + .link = unionfs_link,
5894 + .unlink = unionfs_unlink,
5895 + .symlink = unionfs_symlink,
5896 + .mkdir = unionfs_mkdir,
5897 + .rmdir = unionfs_rmdir,
5898 + .mknod = unionfs_mknod,
5899 + .rename = unionfs_rename,
5900 + .permission = unionfs_permission,
5901 + .setattr = unionfs_setattr,
5902 +#ifdef CONFIG_UNION_FS_XATTR
5903 + .setxattr = unionfs_setxattr,
5904 + .getxattr = unionfs_getxattr,
5905 + .removexattr = unionfs_removexattr,
5906 + .listxattr = unionfs_listxattr,
5907 +#endif /* CONFIG_UNION_FS_XATTR */
5910 +struct inode_operations unionfs_main_iops = {
5911 + .permission = unionfs_permission,
5912 + .setattr = unionfs_setattr,
5913 +#ifdef CONFIG_UNION_FS_XATTR
5914 + .setxattr = unionfs_setxattr,
5915 + .getxattr = unionfs_getxattr,
5916 + .removexattr = unionfs_removexattr,
5917 + .listxattr = unionfs_listxattr,
5918 +#endif /* CONFIG_UNION_FS_XATTR */
5920 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5921 new file mode 100644
5922 index 0000000..9d887ac
5924 +++ b/fs/unionfs/lookup.c
5927 + * Copyright (c) 2003-2009 Erez Zadok
5928 + * Copyright (c) 2003-2006 Charles P. Wright
5929 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5930 + * Copyright (c) 2005-2006 Junjiro Okajima
5931 + * Copyright (c) 2005 Arun M. Krishnakumar
5932 + * Copyright (c) 2004-2006 David P. Quigley
5933 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5934 + * Copyright (c) 2003 Puja Gupta
5935 + * Copyright (c) 2003 Harikesavan Krishnan
5936 + * Copyright (c) 2003-2009 Stony Brook University
5937 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
5939 + * This program is free software; you can redistribute it and/or modify
5940 + * it under the terms of the GNU General Public License version 2 as
5941 + * published by the Free Software Foundation.
5947 + * Lookup one path component @name relative to a <base,mnt> path pair.
5948 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5949 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5950 + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5951 + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5952 + * @dentry and @new_mnt.
5954 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5955 + const char *name, struct vfsmount **new_mnt)
5957 + struct dentry *dentry = NULL;
5958 + struct nameidata lower_nd;
5961 + /* we use flags=0 to get basic lookup */
5962 + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
5965 + case 0: /* no error */
5966 + dentry = lower_nd.path.dentry;
5968 + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
5972 + * We don't consider ENOENT an error, and we want to return
5973 + * a negative dentry (ala lookup_one_len). As we know
5974 + * there was no inode for this name before (-ENOENT), then
5975 + * it's safe to call lookup_one_len (which doesn't take a
5978 + dentry = lookup_one_len(name, base, strlen(name));
5980 + *new_mnt = mntget(lower_nd.path.mnt);
5982 + default: /* all other real errors */
5983 + dentry = ERR_PTR(err);
5991 + * This is a utility function that fills in a unionfs dentry.
5992 + * Caller must lock this dentry with unionfs_lock_dentry.
5994 + * Returns: 0 (ok), or -ERRNO if an error occurred.
5995 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
5997 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
5999 + struct dentry *tmp;
6000 + int err = -ENOSYS;
6002 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
6008 + if (IS_ERR(tmp)) {
6009 + err = PTR_ERR(tmp);
6012 + /* XXX: need to change the interface */
6013 + BUG_ON(tmp != dentry);
6018 +/* The dentry cache is just so we have properly sized dentries. */
6019 +static struct kmem_cache *unionfs_dentry_cachep;
6020 +int unionfs_init_dentry_cache(void)
6022 + unionfs_dentry_cachep =
6023 + kmem_cache_create("unionfs_dentry",
6024 + sizeof(struct unionfs_dentry_info),
6025 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6027 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6030 +void unionfs_destroy_dentry_cache(void)
6032 + if (unionfs_dentry_cachep)
6033 + kmem_cache_destroy(unionfs_dentry_cachep);
6036 +void free_dentry_private_data(struct dentry *dentry)
6038 + if (!dentry || !dentry->d_fsdata)
6040 + kfree(UNIONFS_D(dentry)->lower_paths);
6041 + UNIONFS_D(dentry)->lower_paths = NULL;
6042 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6043 + dentry->d_fsdata = NULL;
6046 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6048 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6054 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6055 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6059 + info->lower_paths = p;
6061 + info->bstart = -1;
6063 + info->bopaque = -1;
6064 + info->bcount = sbmax(dentry->d_sb);
6065 + atomic_set(&info->generation,
6066 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6068 + memset(info->lower_paths, 0, size);
6073 +/* UNIONFS_D(dentry)->lock must be locked */
6074 +int realloc_dentry_private_data(struct dentry *dentry)
6076 + if (!__realloc_dentry_private_data(dentry))
6079 + kfree(UNIONFS_D(dentry)->lower_paths);
6080 + free_dentry_private_data(dentry);
6084 +/* allocate new dentry private data */
6085 +int new_dentry_private_data(struct dentry *dentry, int subclass)
6087 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6091 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6092 + if (unlikely(!info))
6095 + mutex_init(&info->lock);
6096 + mutex_lock_nested(&info->lock, subclass);
6098 + info->lower_paths = NULL;
6100 + dentry->d_fsdata = info;
6102 + if (!__realloc_dentry_private_data(dentry))
6105 + mutex_unlock(&info->lock);
6106 + free_dentry_private_data(dentry);
6111 + * scan through the lower dentry objects, and set bstart to reflect the
6114 +void update_bstart(struct dentry *dentry)
6117 + int bstart = dbstart(dentry);
6118 + int bend = dbend(dentry);
6119 + struct dentry *lower_dentry;
6121 + for (bindex = bstart; bindex <= bend; bindex++) {
6122 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6123 + if (!lower_dentry)
6125 + if (lower_dentry->d_inode) {
6126 + dbstart(dentry) = bindex;
6129 + dput(lower_dentry);
6130 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6136 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6137 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6138 + * Inside that nd structure, this function may also return an allocated
6139 + * struct file (for open intents). The caller, when done with this nd, must
6140 + * kfree the intent file (using release_lower_nd).
6142 + * XXX: this code, and the callers of this code, should be redone using
6143 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6144 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6145 + * function and a method that other file systems can call.
6147 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6150 +#ifdef ALLOC_LOWER_ND_FILE
6152 + * XXX: one day we may need to have the lower return an open file
6153 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6154 + * very well be needed for nfs4.
6156 + struct file *file;
6157 +#endif /* ALLOC_LOWER_ND_FILE */
6159 + memset(nd, 0, sizeof(struct nameidata));
6164 + case LOOKUP_CREATE:
6165 + nd->intent.open.flags |= O_CREAT;
6166 + /* fall through: shared code for create/open cases */
6168 + nd->flags = flags;
6169 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6170 +#ifdef ALLOC_LOWER_ND_FILE
6171 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6172 + if (unlikely(!file)) {
6174 + break; /* exit switch statement and thus return */
6176 + nd->intent.open.file = file;
6177 +#endif /* ALLOC_LOWER_ND_FILE */
6181 + * We should never get here, for now.
6182 + * We can add new cases here later on.
6184 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6192 +void release_lower_nd(struct nameidata *nd, int err)
6194 + if (!nd->intent.open.file)
6197 + release_open_intent(nd);
6198 +#ifdef ALLOC_LOWER_ND_FILE
6199 + kfree(nd->intent.open.file);
6200 +#endif /* ALLOC_LOWER_ND_FILE */
6204 + * Main (and complex) driver function for Unionfs's lookup
6206 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6207 + * PTR if d_splice returned a different dentry.
6209 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6210 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6211 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6212 + * dentry's info, which the caller must unlock.
6214 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6215 + struct dentry *parent, int lookupmode)
6218 + struct dentry *lower_dentry = NULL;
6219 + struct vfsmount *lower_mnt;
6220 + struct vfsmount *lower_dir_mnt;
6221 + struct dentry *wh_lower_dentry = NULL;
6222 + struct dentry *lower_dir_dentry = NULL;
6223 + struct dentry *d_interposed = NULL;
6224 + int bindex, bstart, bend, bopaque;
6225 + int opaque, num_positive = 0;
6228 + int pos_start, pos_end;
6231 + * We should already have a lock on this dentry in the case of a
6232 + * partial lookup, or a revalidation. Otherwise it is returned from
6233 + * new_dentry_private_data already locked.
6235 + verify_locked(dentry);
6236 + verify_locked(parent);
6238 + /* must initialize dentry operations */
6239 + dentry->d_op = &unionfs_dops;
6241 + /* We never partial lookup the root directory. */
6242 + if (IS_ROOT(dentry))
6245 + name = dentry->d_name.name;
6246 + namelen = dentry->d_name.len;
6248 + /* No dentries should get created for possible whiteout names. */
6249 + if (!is_validname(name)) {
6254 + /* Now start the actual lookup procedure. */
6255 + bstart = dbstart(parent);
6256 + bend = dbend(parent);
6257 + bopaque = dbopaque(parent);
6258 + BUG_ON(bstart < 0);
6260 + /* adjust bend to bopaque if needed */
6261 + if ((bopaque >= 0) && (bopaque < bend))
6264 + /* lookup all possible dentries */
6265 + for (bindex = bstart; bindex <= bend; bindex++) {
6267 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6268 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6270 + /* skip if we already have a positive lower dentry */
6271 + if (lower_dentry) {
6272 + if (dbstart(dentry) < 0)
6273 + dbstart(dentry) = bindex;
6274 + if (bindex > dbend(dentry))
6275 + dbend(dentry) = bindex;
6276 + if (lower_dentry->d_inode)
6281 + lower_dir_dentry =
6282 + unionfs_lower_dentry_idx(parent, bindex);
6283 + /* if the lower dentry's parent does not exist, skip this */
6284 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6287 + /* also skip it if the parent isn't a directory. */
6288 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6289 + continue; /* XXX: should be BUG_ON */
6291 + /* check for whiteouts: stop lookup if found */
6292 + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6293 + if (IS_ERR(wh_lower_dentry)) {
6294 + err = PTR_ERR(wh_lower_dentry);
6297 + if (wh_lower_dentry->d_inode) {
6298 + dbend(dentry) = dbopaque(dentry) = bindex;
6299 + if (dbstart(dentry) < 0)
6300 + dbstart(dentry) = bindex;
6301 + dput(wh_lower_dentry);
6304 + dput(wh_lower_dentry);
6306 + /* Now do regular lookup; lookup @name */
6307 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6308 + lower_mnt = NULL; /* XXX: needed? */
6310 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6311 + name, &lower_mnt);
6313 + if (IS_ERR(lower_dentry)) {
6314 + err = PTR_ERR(lower_dentry);
6317 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6319 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6321 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6323 + /* adjust dbstart/end */
6324 + if (dbstart(dentry) < 0)
6325 + dbstart(dentry) = bindex;
6326 + if (bindex > dbend(dentry))
6327 + dbend(dentry) = bindex;
6329 + * We always store the lower dentries above, and update
6330 + * dbstart/dbend, even if the whole unionfs dentry is
6331 + * negative (i.e., no lower inodes).
6333 + if (!lower_dentry->d_inode)
6338 + * check if we just found an opaque directory, if so, stop
6341 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6343 + opaque = is_opaque_dir(dentry, bindex);
6347 + } else if (opaque) {
6348 + dbend(dentry) = dbopaque(dentry) = bindex;
6351 + dbend(dentry) = bindex;
6353 + /* update parent directory's atime with the bindex */
6354 + fsstack_copy_attr_atime(parent->d_inode,
6355 + lower_dir_dentry->d_inode);
6358 + /* sanity checks, then decide if to process a negative dentry */
6359 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6360 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6362 + if (num_positive > 0)
6363 + goto out_positive;
6365 + /*** handle NEGATIVE dentries ***/
6368 + * If negative, keep only first lower negative dentry, to save on
6371 + if (dbstart(dentry) < dbend(dentry)) {
6372 + path_put_lowers(dentry, dbstart(dentry) + 1,
6373 + dbend(dentry), false);
6374 + dbend(dentry) = dbstart(dentry);
6376 + if (lookupmode == INTERPOSE_PARTIAL)
6378 + if (lookupmode == INTERPOSE_LOOKUP) {
6380 + * If all we found was a whiteout in the first available
6381 + * branch, then create a negative dentry for a possibly new
6382 + * file to be created.
6384 + if (dbopaque(dentry) < 0)
6386 + /* XXX: need to get mnt here */
6387 + bindex = dbstart(dentry);
6388 + if (unionfs_lower_dentry_idx(dentry, bindex))
6390 + lower_dir_dentry =
6391 + unionfs_lower_dentry_idx(parent, bindex);
6392 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6394 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6395 + goto out; /* XXX: should be BUG_ON */
6396 + /* XXX: do we need to cross bind mounts here? */
6397 + lower_dentry = lookup_one_len(name, lower_dir_dentry, namelen);
6398 + if (IS_ERR(lower_dentry)) {
6399 + err = PTR_ERR(lower_dentry);
6402 + /* XXX: need to mntget/mntput as needed too! */
6403 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6404 + /* XXX: wrong mnt for crossing bind mounts! */
6405 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6406 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6411 + /* if we're revalidating a positive dentry, don't make it negative */
6412 + if (lookupmode != INTERPOSE_REVAL)
6413 + d_add(dentry, NULL);
6418 + /*** handle POSITIVE dentries ***/
6421 + * This unionfs dentry is positive (at least one lower inode
6422 + * exists), so scan entire dentry from beginning to end, and remove
6423 + * any negative lower dentries, if any. Then, update dbstart/dbend
6424 + * to reflect the start/end of positive dentries.
6426 + pos_start = pos_end = -1;
6427 + for (bindex = bstart; bindex <= bend; bindex++) {
6428 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6430 + if (lower_dentry && lower_dentry->d_inode) {
6431 + if (pos_start < 0)
6432 + pos_start = bindex;
6433 + if (bindex > pos_end)
6437 + path_put_lowers(dentry, bindex, bindex, false);
6439 + if (pos_start >= 0)
6440 + dbstart(dentry) = pos_start;
6442 + dbend(dentry) = pos_end;
6444 + /* Partial lookups need to re-interpose, or throw away older negs. */
6445 + if (lookupmode == INTERPOSE_PARTIAL) {
6446 + if (dentry->d_inode) {
6447 + unionfs_reinterpose(dentry);
6452 + * This dentry was positive, so it is as if we had a
6453 + * negative revalidation.
6455 + lookupmode = INTERPOSE_REVAL_NEG;
6456 + update_bstart(dentry);
6460 + * Interpose can return a dentry if d_splice returned a different
6463 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6464 + if (IS_ERR(d_interposed))
6465 + err = PTR_ERR(d_interposed);
6466 + else if (d_interposed)
6467 + dentry = d_interposed;
6474 + /* should dput/mntput all the underlying dentries on error condition */
6475 + if (dbstart(dentry) >= 0)
6476 + path_put_lowers_all(dentry, false);
6477 + /* free lower_paths unconditionally */
6478 + kfree(UNIONFS_D(dentry)->lower_paths);
6479 + UNIONFS_D(dentry)->lower_paths = NULL;
6482 + if (dentry && UNIONFS_D(dentry)) {
6483 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6484 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6486 + if (d_interposed && UNIONFS_D(d_interposed)) {
6487 + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6488 + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6491 + if (!err && d_interposed)
6492 + return d_interposed;
6493 + return ERR_PTR(err);
6495 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6496 new file mode 100644
6497 index 0000000..c58405c
6499 +++ b/fs/unionfs/main.c
6502 + * Copyright (c) 2003-2009 Erez Zadok
6503 + * Copyright (c) 2003-2006 Charles P. Wright
6504 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6505 + * Copyright (c) 2005-2006 Junjiro Okajima
6506 + * Copyright (c) 2005 Arun M. Krishnakumar
6507 + * Copyright (c) 2004-2006 David P. Quigley
6508 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6509 + * Copyright (c) 2003 Puja Gupta
6510 + * Copyright (c) 2003 Harikesavan Krishnan
6511 + * Copyright (c) 2003-2009 Stony Brook University
6512 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
6514 + * This program is free software; you can redistribute it and/or modify
6515 + * it under the terms of the GNU General Public License version 2 as
6516 + * published by the Free Software Foundation.
6520 +#include <linux/module.h>
6521 +#include <linux/moduleparam.h>
6523 +static void unionfs_fill_inode(struct dentry *dentry,
6524 + struct inode *inode)
6526 + struct inode *lower_inode;
6527 + struct dentry *lower_dentry;
6528 + int bindex, bstart, bend;
6530 + bstart = dbstart(dentry);
6531 + bend = dbend(dentry);
6533 + for (bindex = bstart; bindex <= bend; bindex++) {
6534 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6535 + if (!lower_dentry) {
6536 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6540 + /* Initialize the lower inode to the new lower inode. */
6541 + if (!lower_dentry->d_inode)
6544 + unionfs_set_lower_inode_idx(inode, bindex,
6545 + igrab(lower_dentry->d_inode));
6548 + ibstart(inode) = dbstart(dentry);
6549 + ibend(inode) = dbend(dentry);
6551 + /* Use attributes from the first branch. */
6552 + lower_inode = unionfs_lower_inode(inode);
6554 + /* Use different set of inode ops for symlinks & directories */
6555 + if (S_ISLNK(lower_inode->i_mode))
6556 + inode->i_op = &unionfs_symlink_iops;
6557 + else if (S_ISDIR(lower_inode->i_mode))
6558 + inode->i_op = &unionfs_dir_iops;
6560 + /* Use different set of file ops for directories */
6561 + if (S_ISDIR(lower_inode->i_mode))
6562 + inode->i_fop = &unionfs_dir_fops;
6564 + /* properly initialize special inodes */
6565 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6566 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6567 + init_special_inode(inode, lower_inode->i_mode,
6568 + lower_inode->i_rdev);
6570 + /* all well, copy inode attributes */
6571 + unionfs_copy_attr_all(inode, lower_inode);
6572 + fsstack_copy_inode_size(inode, lower_inode);
6576 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6577 + * the classic stackable file system "vnode interposition" action.
6579 + * @sb: unionfs's super_block
6581 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6585 + struct inode *inode;
6586 + int need_fill_inode = 1;
6587 + struct dentry *spliced = NULL;
6589 + verify_locked(dentry);
6592 + * We allocate our new inode below by calling unionfs_iget,
6593 + * which will initialize some of the new inode's fields
6597 + * On revalidate we've already got our own inode and just need
6600 + if (flag == INTERPOSE_REVAL) {
6601 + inode = dentry->d_inode;
6602 + UNIONFS_I(inode)->bstart = -1;
6603 + UNIONFS_I(inode)->bend = -1;
6604 + atomic_set(&UNIONFS_I(inode)->generation,
6605 + atomic_read(&UNIONFS_SB(sb)->generation));
6607 + UNIONFS_I(inode)->lower_inodes =
6608 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6609 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6614 + /* get unique inode number for unionfs */
6615 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6616 + if (IS_ERR(inode)) {
6617 + err = PTR_ERR(inode);
6620 + if (atomic_read(&inode->i_count) > 1)
6624 + need_fill_inode = 0;
6625 + unionfs_fill_inode(dentry, inode);
6628 + /* only (our) lookup wants to do a d_add */
6630 + case INTERPOSE_DEFAULT:
6631 + /* for operations which create new inodes */
6632 + d_add(dentry, inode);
6634 + case INTERPOSE_REVAL_NEG:
6635 + d_instantiate(dentry, inode);
6637 + case INTERPOSE_LOOKUP:
6638 + spliced = d_splice_alias(inode, dentry);
6639 + if (spliced && spliced != dentry) {
6641 + * d_splice can return a dentry if it was
6642 + * disconnected and had to be moved. We must ensure
6643 + * that the private data of the new dentry is
6644 + * correct and that the inode info was filled
6645 + * properly. Finally we must return this new
6648 + spliced->d_op = &unionfs_dops;
6649 + spliced->d_fsdata = dentry->d_fsdata;
6650 + dentry->d_fsdata = NULL;
6652 + if (need_fill_inode) {
6653 + need_fill_inode = 0;
6654 + unionfs_fill_inode(dentry, inode);
6657 + } else if (!spliced) {
6658 + if (need_fill_inode) {
6659 + need_fill_inode = 0;
6660 + unionfs_fill_inode(dentry, inode);
6665 + case INTERPOSE_REVAL:
6669 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6678 + return ERR_PTR(err);
6681 +/* like interpose above, but for an already existing dentry */
6682 +void unionfs_reinterpose(struct dentry *dentry)
6684 + struct dentry *lower_dentry;
6685 + struct inode *inode;
6686 + int bindex, bstart, bend;
6688 + verify_locked(dentry);
6690 + /* This is pre-allocated inode */
6691 + inode = dentry->d_inode;
6693 + bstart = dbstart(dentry);
6694 + bend = dbend(dentry);
6695 + for (bindex = bstart; bindex <= bend; bindex++) {
6696 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6697 + if (!lower_dentry)
6700 + if (!lower_dentry->d_inode)
6702 + if (unionfs_lower_inode_idx(inode, bindex))
6704 + unionfs_set_lower_inode_idx(inode, bindex,
6705 + igrab(lower_dentry->d_inode));
6707 + ibstart(inode) = dbstart(dentry);
6708 + ibend(inode) = dbend(dentry);
6712 + * make sure the branch we just looked up (nd) makes sense:
6714 + * 1) we're not trying to stack unionfs on top of unionfs
6716 + * 3) is a directory
6718 +int check_branch(struct nameidata *nd)
6720 + /* XXX: remove in ODF code -- stacking unions allowed there */
6721 + if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6723 + if (!nd->path.dentry->d_inode)
6725 + if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6730 +/* checks if two lower_dentries have overlapping branches */
6731 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6733 + struct dentry *dent = NULL;
6736 + while ((dent != dent2) && (dent->d_parent != dent))
6737 + dent = dent->d_parent;
6739 + if (dent == dent2)
6743 + while ((dent != dent1) && (dent->d_parent != dent))
6744 + dent = dent->d_parent;
6746 + return (dent == dent1);
6750 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6751 + * specified. Fill the mode bits in @perms. If encounter an unknown
6752 + * string, return -EINVAL. Otherwise return 0.
6754 +int parse_branch_mode(const char *name, int *perms)
6756 + if (!name || !strcmp(name, "rw")) {
6757 + *perms = MAY_READ | MAY_WRITE;
6760 + if (!strcmp(name, "ro")) {
6761 + *perms = MAY_READ;
6768 + * parse the dirs= mount argument
6770 + * We don't need to lock the superblock private data's rwsem, as we get
6771 + * called only by unionfs_read_super - it is still a long time before anyone
6772 + * can even get a reference to us.
6774 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6775 + *lower_root_info, char *options)
6777 + struct nameidata nd;
6784 + struct dentry *dent1;
6785 + struct dentry *dent2;
6787 + if (options[0] == '\0') {
6788 + printk(KERN_ERR "unionfs: no branches specified\n");
6794 + * Each colon means we have a separator, this is really just a rough
6795 + * guess, since strsep will handle empty fields for us.
6797 + for (i = 0; options[i]; i++)
6798 + if (options[i] == ':')
6801 + /* allocate space for underlying pointers to lower dentry */
6802 + UNIONFS_SB(sb)->data =
6803 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6804 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6809 + lower_root_info->lower_paths =
6810 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6811 + if (unlikely(!lower_root_info->lower_paths)) {
6816 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6818 + while ((name = strsep(&options, ":")) != NULL) {
6820 + char *mode = strchr(name, '=');
6824 + if (!*name) { /* bad use of ':' (extra colons) */
6831 + /* strip off '=' if any */
6835 + err = parse_branch_mode(mode, &perms);
6837 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6838 + "branch %d\n", mode, bindex);
6841 + /* ensure that leftmost branch is writeable */
6842 + if (!bindex && !(perms & MAY_WRITE)) {
6843 + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6844 + "read-only (use \"-o ro\" to create a "
6845 + "read-only union)\n");
6850 + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6852 + printk(KERN_ERR "unionfs: error accessing "
6853 + "lower directory '%s' (error %d)\n",
6858 + err = check_branch(&nd);
6860 + printk(KERN_ERR "unionfs: lower directory "
6861 + "'%s' is not a valid branch\n", name);
6862 + path_put(&nd.path);
6866 + lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6867 + lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6869 + set_branchperms(sb, bindex, perms);
6870 + set_branch_count(sb, bindex, 0);
6871 + new_branch_id(sb, bindex);
6873 + if (lower_root_info->bstart < 0)
6874 + lower_root_info->bstart = bindex;
6875 + lower_root_info->bend = bindex;
6879 + if (branches == 0) {
6880 + printk(KERN_ERR "unionfs: no branches specified\n");
6885 + BUG_ON(branches != (lower_root_info->bend + 1));
6888 + * Ensure that no overlaps exist in the branches.
6890 + * This test is required because the Linux kernel has no support
6891 + * currently for ensuring coherency between stackable layers and
6892 + * branches. If we were to allow overlapping branches, it would be
6893 + * possible, for example, to delete a file via one branch, which
6894 + * would not be reflected in another branch. Such incoherency could
6895 + * lead to inconsistencies and even kernel oopses. Rather than
6896 + * implement hacks to work around some of these cache-coherency
6897 + * problems, we prevent branch overlapping, for now. A complete
6898 + * solution will involve proper kernel/VFS support for cache
6899 + * coherency, at which time we could safely remove this
6900 + * branch-overlapping test.
6902 + for (i = 0; i < branches; i++) {
6903 + dent1 = lower_root_info->lower_paths[i].dentry;
6904 + for (j = i + 1; j < branches; j++) {
6905 + dent2 = lower_root_info->lower_paths[j].dentry;
6906 + if (is_branch_overlap(dent1, dent2)) {
6907 + printk(KERN_ERR "unionfs: branches %d and "
6908 + "%d overlap\n", i, j);
6917 + for (i = 0; i < branches; i++)
6918 + path_put(&lower_root_info->lower_paths[i]);
6920 + kfree(lower_root_info->lower_paths);
6921 + kfree(UNIONFS_SB(sb)->data);
6924 + * MUST clear the pointers to prevent potential double free if
6925 + * the caller dies later on
6927 + lower_root_info->lower_paths = NULL;
6928 + UNIONFS_SB(sb)->data = NULL;
6934 + * Parse mount options. See the manual page for usage instructions.
6936 + * Returns the dentry object of the lower-level (lower) directory;
6937 + * We want to mount our stackable file system on top of that lower directory.
6939 +static struct unionfs_dentry_info *unionfs_parse_options(
6940 + struct super_block *sb,
6943 + struct unionfs_dentry_info *lower_root_info;
6947 + int dirsfound = 0;
6949 + /* allocate private data area */
6952 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6953 + if (unlikely(!lower_root_info))
6955 + lower_root_info->bstart = -1;
6956 + lower_root_info->bend = -1;
6957 + lower_root_info->bopaque = -1;
6959 + while ((optname = strsep(&options, ",")) != NULL) {
6962 + if (!optname || !*optname)
6965 + optarg = strchr(optname, '=');
6970 + * All of our options take an argument now. Insert ones that
6971 + * don't, above this check.
6974 + printk(KERN_ERR "unionfs: %s requires an argument\n",
6980 + if (!strcmp("dirs", optname)) {
6981 + if (++dirsfound > 1) {
6983 + "unionfs: multiple dirs specified\n");
6987 + err = parse_dirs_option(sb, lower_root_info, optarg);
6995 + "unionfs: unrecognized option '%s'\n", optname);
6998 + if (dirsfound != 1) {
6999 + printk(KERN_ERR "unionfs: dirs option required\n");
7006 + if (lower_root_info && lower_root_info->lower_paths) {
7007 + for (bindex = lower_root_info->bstart;
7008 + bindex >= 0 && bindex <= lower_root_info->bend;
7010 + path_put(&lower_root_info->lower_paths[bindex]);
7013 + kfree(lower_root_info->lower_paths);
7014 + kfree(lower_root_info);
7016 + kfree(UNIONFS_SB(sb)->data);
7017 + UNIONFS_SB(sb)->data = NULL;
7019 + lower_root_info = ERR_PTR(err);
7021 + return lower_root_info;
7025 + * our custom d_alloc_root work-alike
7027 + * we can't use d_alloc_root if we want to use our own interpose function
7028 + * unchanged, so we simply call our own "fake" d_alloc_root
7030 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7032 + struct dentry *ret = NULL;
7035 + static const struct qstr name = {
7040 + ret = d_alloc(NULL, &name);
7041 + if (likely(ret)) {
7042 + ret->d_op = &unionfs_dops;
7044 + ret->d_parent = ret;
7051 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7052 + * way anyone can have a reference to the superblock at this point in time.
7054 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7058 + struct unionfs_dentry_info *lower_root_info = NULL;
7059 + int bindex, bstart, bend;
7063 + "unionfs: read_super: missing data argument\n");
7068 + /* Allocate superblock private data */
7069 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7070 + if (unlikely(!UNIONFS_SB(sb))) {
7071 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7076 + UNIONFS_SB(sb)->bend = -1;
7077 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7078 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7079 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7081 + lower_root_info = unionfs_parse_options(sb, raw_data);
7082 + if (IS_ERR(lower_root_info)) {
7084 + "unionfs: read_super: error while parsing options "
7085 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7086 + err = PTR_ERR(lower_root_info);
7087 + lower_root_info = NULL;
7090 + if (lower_root_info->bstart == -1) {
7095 + /* set the lower superblock field of upper superblock */
7096 + bstart = lower_root_info->bstart;
7097 + BUG_ON(bstart != 0);
7098 + sbend(sb) = bend = lower_root_info->bend;
7099 + for (bindex = bstart; bindex <= bend; bindex++) {
7100 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7101 + atomic_inc(&d->d_sb->s_active);
7102 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7105 + /* max Bytes is the maximum bytes from highest priority branch */
7106 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7109 + * Our c/m/atime granularity is 1 ns because we may stack on file
7110 + * systems whose granularity is as good. This is important for our
7111 + * time-based cache coherency.
7113 + sb->s_time_gran = 1;
7115 + sb->s_op = &unionfs_sops;
7117 + /* See comment next to the definition of unionfs_d_alloc_root */
7118 + sb->s_root = unionfs_d_alloc_root(sb);
7119 + if (unlikely(!sb->s_root)) {
7124 + /* link the upper and lower dentries */
7125 + sb->s_root->d_fsdata = NULL;
7126 + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7127 + if (unlikely(err))
7130 + /* Set the lower dentries for s_root */
7131 + for (bindex = bstart; bindex <= bend; bindex++) {
7133 + struct vfsmount *m;
7135 + d = lower_root_info->lower_paths[bindex].dentry;
7136 + m = lower_root_info->lower_paths[bindex].mnt;
7138 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7139 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7141 + dbstart(sb->s_root) = bstart;
7142 + dbend(sb->s_root) = bend;
7144 + /* Set the generation number to one, since this is for the mount. */
7145 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7148 + * Call interpose to create the upper level inode. Only
7149 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7151 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7152 + unionfs_unlock_dentry(sb->s_root);
7155 + /* else fall through */
7158 + if (UNIONFS_D(sb->s_root)) {
7159 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7160 + free_dentry_private_data(sb->s_root);
7165 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7166 + for (bindex = lower_root_info->bstart;
7167 + bindex <= lower_root_info->bend; bindex++) {
7169 + d = lower_root_info->lower_paths[bindex].dentry;
7170 + /* drop refs we took earlier */
7171 + atomic_dec(&d->d_sb->s_active);
7172 + path_put(&lower_root_info->lower_paths[bindex]);
7174 + kfree(lower_root_info->lower_paths);
7175 + kfree(lower_root_info);
7176 + lower_root_info = NULL;
7180 + kfree(UNIONFS_SB(sb)->data);
7181 + kfree(UNIONFS_SB(sb));
7182 + sb->s_fs_info = NULL;
7185 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7186 + kfree(lower_root_info->lower_paths);
7187 + kfree(lower_root_info);
7192 +static int unionfs_get_sb(struct file_system_type *fs_type,
7193 + int flags, const char *dev_name,
7194 + void *raw_data, struct vfsmount *mnt)
7197 + err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7199 + UNIONFS_SB(mnt->mnt_sb)->dev_name =
7200 + kstrdup(dev_name, GFP_KERNEL);
7204 +static struct file_system_type unionfs_fs_type = {
7205 + .owner = THIS_MODULE,
7206 + .name = UNIONFS_NAME,
7207 + .get_sb = unionfs_get_sb,
7208 + .kill_sb = generic_shutdown_super,
7209 + .fs_flags = FS_REVAL_DOT,
7212 +static int __init init_unionfs_fs(void)
7216 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7218 + err = unionfs_init_filldir_cache();
7219 + if (unlikely(err))
7221 + err = unionfs_init_inode_cache();
7222 + if (unlikely(err))
7224 + err = unionfs_init_dentry_cache();
7225 + if (unlikely(err))
7227 + err = init_sioq();
7228 + if (unlikely(err))
7230 + err = register_filesystem(&unionfs_fs_type);
7232 + if (unlikely(err)) {
7234 + unionfs_destroy_filldir_cache();
7235 + unionfs_destroy_inode_cache();
7236 + unionfs_destroy_dentry_cache();
7241 +static void __exit exit_unionfs_fs(void)
7244 + unionfs_destroy_filldir_cache();
7245 + unionfs_destroy_inode_cache();
7246 + unionfs_destroy_dentry_cache();
7247 + unregister_filesystem(&unionfs_fs_type);
7248 + pr_info("Completed unionfs module unload\n");
7251 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7252 + " (http://www.fsl.cs.sunysb.edu)");
7253 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7254 + " (http://unionfs.filesystems.org)");
7255 +MODULE_LICENSE("GPL");
7257 +module_init(init_unionfs_fs);
7258 +module_exit(exit_unionfs_fs);
7259 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7260 new file mode 100644
7261 index 0000000..18b05d5
7263 +++ b/fs/unionfs/mmap.c
7266 + * Copyright (c) 2003-2009 Erez Zadok
7267 + * Copyright (c) 2003-2006 Charles P. Wright
7268 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7269 + * Copyright (c) 2005-2006 Junjiro Okajima
7270 + * Copyright (c) 2006 Shaya Potter
7271 + * Copyright (c) 2005 Arun M. Krishnakumar
7272 + * Copyright (c) 2004-2006 David P. Quigley
7273 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7274 + * Copyright (c) 2003 Puja Gupta
7275 + * Copyright (c) 2003 Harikesavan Krishnan
7276 + * Copyright (c) 2003-2009 Stony Brook University
7277 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7279 + * This program is free software; you can redistribute it and/or modify
7280 + * it under the terms of the GNU General Public License version 2 as
7281 + * published by the Free Software Foundation.
7288 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7289 + * use in unionfs_mmap) checks for the existence of
7290 + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7291 + * be fixed to allow a file system to define vm_ops->fault without any
7292 + * address_space_ops whatsoever.
7294 + * Otherwise, we don't want to use our readpage method at all.
7296 +static int unionfs_readpage(struct file *file, struct page *page)
7302 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7305 + struct file *file, *lower_file;
7306 + struct vm_operations_struct *lower_vm_ops;
7307 + struct vm_area_struct lower_vma;
7310 + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7311 + file = lower_vma.vm_file;
7312 + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7313 + BUG_ON(!lower_vm_ops);
7315 + lower_file = unionfs_lower_file(file);
7316 + BUG_ON(!lower_file);
7318 + * XXX: vm_ops->fault may be called in parallel. Because we have to
7319 + * resort to temporarily changing the vma->vm_file to point to the
7320 + * lower file, a concurrent invocation of unionfs_fault could see a
7321 + * different value. In this workaround, we keep a different copy of
7322 + * the vma structure in our stack, so we never expose a different
7323 + * value of the vma->vm_file called to us, even temporarily. A
7324 + * better fix would be to change the calling semantics of ->fault to
7325 + * take an explicit file pointer.
7327 + lower_vma.vm_file = lower_file;
7328 + err = lower_vm_ops->fault(&lower_vma, vmf);
7333 + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7334 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7335 + * the a_ops vector to be non-NULL.
7337 +struct address_space_operations unionfs_aops = {
7338 + /* empty on purpose */
7342 + * XXX: we need a second, dummy address_space_ops vector, to be used
7343 + * temporarily during unionfs_mmap, because the latter calls
7344 + * generic_file_mmap, which checks if ->readpage exists, else returns
7347 +struct address_space_operations unionfs_dummy_aops = {
7348 + .readpage = unionfs_readpage,
7351 +struct vm_operations_struct unionfs_vm_ops = {
7352 + .fault = unionfs_fault,
7354 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7355 new file mode 100644
7356 index 0000000..485464b
7358 +++ b/fs/unionfs/rdstate.c
7361 + * Copyright (c) 2003-2009 Erez Zadok
7362 + * Copyright (c) 2003-2006 Charles P. Wright
7363 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7364 + * Copyright (c) 2005-2006 Junjiro Okajima
7365 + * Copyright (c) 2005 Arun M. Krishnakumar
7366 + * Copyright (c) 2004-2006 David P. Quigley
7367 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7368 + * Copyright (c) 2003 Puja Gupta
7369 + * Copyright (c) 2003 Harikesavan Krishnan
7370 + * Copyright (c) 2003-2009 Stony Brook University
7371 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7373 + * This program is free software; you can redistribute it and/or modify
7374 + * it under the terms of the GNU General Public License version 2 as
7375 + * published by the Free Software Foundation.
7380 +/* This file contains the routines for maintaining readdir state. */
7383 + * There are two structures here, rdstate which is a hash table
7384 + * of the second structure which is a filldir_node.
7388 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7389 + * of them and they shouldn't waste memory. If the node has a small name
7390 + * (as defined by the dentry structure), then we use an inline name to
7391 + * preserve kmalloc space.
7393 +static struct kmem_cache *unionfs_filldir_cachep;
7395 +int unionfs_init_filldir_cache(void)
7397 + unionfs_filldir_cachep =
7398 + kmem_cache_create("unionfs_filldir",
7399 + sizeof(struct filldir_node), 0,
7400 + SLAB_RECLAIM_ACCOUNT, NULL);
7402 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7405 +void unionfs_destroy_filldir_cache(void)
7407 + if (unionfs_filldir_cachep)
7408 + kmem_cache_destroy(unionfs_filldir_cachep);
7412 + * This is a tuning parameter that tells us roughly how big to make the
7413 + * hash table in directory entries per page. This isn't perfect, but
7414 + * at least we get a hash table size that shouldn't be too overloaded.
7415 + * The following averages are based on my home directory.
7416 + * 14.44693 Overall
7417 + * 12.29 Single Page Directories
7418 + * 117.93 Multi-page directories
7420 +#define DENTPAGE 4096
7421 +#define DENTPERONEPAGE 12
7422 +#define DENTPERPAGE 118
7423 +#define MINHASHSIZE 1
7424 +static int guesstimate_hash_size(struct inode *inode)
7426 + struct inode *lower_inode;
7428 + int hashsize = MINHASHSIZE;
7430 + if (UNIONFS_I(inode)->hashsize > 0)
7431 + return UNIONFS_I(inode)->hashsize;
7433 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7434 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7438 + if (i_size_read(lower_inode) == DENTPAGE)
7439 + hashsize += DENTPERONEPAGE;
7441 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7448 +int init_rdstate(struct file *file)
7450 + BUG_ON(sizeof(loff_t) !=
7451 + (sizeof(unsigned int) + sizeof(unsigned int)));
7452 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7454 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7457 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7460 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7462 + struct unionfs_dir_state *rdstate = NULL;
7463 + struct list_head *pos;
7465 + spin_lock(&UNIONFS_I(inode)->rdlock);
7466 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7467 + struct unionfs_dir_state *r =
7468 + list_entry(pos, struct unionfs_dir_state, cache);
7469 + if (fpos == rdstate2offset(r)) {
7470 + UNIONFS_I(inode)->rdcount--;
7471 + list_del(&r->cache);
7476 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7480 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7484 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7485 + struct unionfs_dir_state *rdstate;
7487 + hashsize = guesstimate_hash_size(inode);
7488 + mallocsize += hashsize * sizeof(struct list_head);
7489 + mallocsize = __roundup_pow_of_two(mallocsize);
7491 + /* This should give us about 500 entries anyway. */
7492 + if (mallocsize > PAGE_SIZE)
7493 + mallocsize = PAGE_SIZE;
7495 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7496 + sizeof(struct list_head);
7498 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7499 + if (unlikely(!rdstate))
7502 + spin_lock(&UNIONFS_I(inode)->rdlock);
7503 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7504 + UNIONFS_I(inode)->cookie = 1;
7506 + UNIONFS_I(inode)->cookie++;
7508 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7509 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7510 + rdstate->offset = 1;
7511 + rdstate->access = jiffies;
7512 + rdstate->bindex = bindex;
7513 + rdstate->dirpos = 0;
7514 + rdstate->hashentries = 0;
7515 + rdstate->size = hashsize;
7516 + for (i = 0; i < rdstate->size; i++)
7517 + INIT_LIST_HEAD(&rdstate->list[i]);
7522 +static void free_filldir_node(struct filldir_node *node)
7524 + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7525 + kfree(node->name);
7526 + kmem_cache_free(unionfs_filldir_cachep, node);
7529 +void free_rdstate(struct unionfs_dir_state *state)
7531 + struct filldir_node *tmp;
7534 + for (i = 0; i < state->size; i++) {
7535 + struct list_head *head = &(state->list[i]);
7536 + struct list_head *pos, *n;
7538 + /* traverse the list and deallocate space */
7539 + list_for_each_safe(pos, n, head) {
7540 + tmp = list_entry(pos, struct filldir_node, file_list);
7541 + list_del(&tmp->file_list);
7542 + free_filldir_node(tmp);
7549 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7550 + const char *name, int namelen,
7554 + unsigned int hash;
7555 + struct list_head *head;
7556 + struct list_head *pos;
7557 + struct filldir_node *cursor = NULL;
7560 + BUG_ON(namelen <= 0);
7562 + hash = full_name_hash(name, namelen);
7563 + index = hash % rdstate->size;
7565 + head = &(rdstate->list[index]);
7566 + list_for_each(pos, head) {
7567 + cursor = list_entry(pos, struct filldir_node, file_list);
7569 + if (cursor->namelen == namelen && cursor->hash == hash &&
7570 + !strncmp(cursor->name, name, namelen)) {
7572 + * a duplicate exists, and hence no need to create
7573 + * entry to the list
7578 + * if a duplicate is found in this branch, and is
7579 + * not due to the caller looking for an entry to
7580 + * whiteout, then the file system may be corrupted.
7582 + if (unlikely(!is_whiteout &&
7583 + cursor->bindex == rdstate->bindex))
7584 + printk(KERN_ERR "unionfs: filldir: possible "
7585 + "I/O error: a file is duplicated "
7586 + "in the same branch %d: %s\n",
7587 + rdstate->bindex, cursor->name);
7598 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7599 + int namelen, int bindex, int whiteout)
7601 + struct filldir_node *new;
7602 + unsigned int hash;
7605 + struct list_head *head;
7607 + BUG_ON(namelen <= 0);
7609 + hash = full_name_hash(name, namelen);
7610 + index = hash % rdstate->size;
7611 + head = &(rdstate->list[index]);
7613 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7614 + if (unlikely(!new)) {
7619 + INIT_LIST_HEAD(&new->file_list);
7620 + new->namelen = namelen;
7622 + new->bindex = bindex;
7623 + new->whiteout = whiteout;
7625 + if (namelen < DNAME_INLINE_LEN_MIN) {
7626 + new->name = new->iname;
7628 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7629 + if (unlikely(!new->name)) {
7630 + kmem_cache_free(unionfs_filldir_cachep, new);
7636 + memcpy(new->name, name, namelen);
7637 + new->name[namelen] = '\0';
7639 + rdstate->hashentries++;
7641 + list_add(&(new->file_list), head);
7645 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7646 new file mode 100644
7647 index 0000000..ed13260
7649 +++ b/fs/unionfs/rename.c
7652 + * Copyright (c) 2003-2009 Erez Zadok
7653 + * Copyright (c) 2003-2006 Charles P. Wright
7654 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7655 + * Copyright (c) 2005-2006 Junjiro Okajima
7656 + * Copyright (c) 2005 Arun M. Krishnakumar
7657 + * Copyright (c) 2004-2006 David P. Quigley
7658 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7659 + * Copyright (c) 2003 Puja Gupta
7660 + * Copyright (c) 2003 Harikesavan Krishnan
7661 + * Copyright (c) 2003-2009 Stony Brook University
7662 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7664 + * This program is free software; you can redistribute it and/or modify
7665 + * it under the terms of the GNU General Public License version 2 as
7666 + * published by the Free Software Foundation.
7672 + * This is a helper function for rename, used when rename ends up with hosed
7673 + * over dentries and we need to revert.
7675 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7676 + struct dentry *parent, int bindex)
7678 + struct dentry *lower_dentry;
7679 + struct dentry *lower_parent;
7682 + verify_locked(dentry);
7684 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7686 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7688 + lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
7689 + dentry->d_name.len);
7690 + if (IS_ERR(lower_dentry)) {
7691 + err = PTR_ERR(lower_dentry);
7695 + dput(unionfs_lower_dentry_idx(dentry, bindex));
7696 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7697 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7699 + if (!lower_dentry->d_inode) {
7700 + dput(lower_dentry);
7701 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7703 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7704 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7705 + igrab(lower_dentry->d_inode));
7712 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7713 + struct dentry *old_parent,
7714 + struct inode *new_dir, struct dentry *new_dentry,
7715 + struct dentry *new_parent,
7719 + struct dentry *lower_old_dentry;
7720 + struct dentry *lower_new_dentry;
7721 + struct dentry *lower_old_dir_dentry;
7722 + struct dentry *lower_new_dir_dentry;
7723 + struct dentry *trap;
7725 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7726 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7728 + if (!lower_new_dentry) {
7729 + lower_new_dentry =
7730 + create_parents(new_parent->d_inode,
7731 + new_dentry, new_dentry->d_name.name,
7733 + if (IS_ERR(lower_new_dentry)) {
7734 + err = PTR_ERR(lower_new_dentry);
7735 + if (IS_COPYUP_ERR(err))
7737 + printk(KERN_ERR "unionfs: error creating directory "
7738 + "tree for rename, bindex=%d err=%d\n",
7744 + /* check for and remove whiteout, if any */
7745 + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7746 + if (err > 0) /* ignore if whiteout found and successfully removed */
7751 + /* check of old_dentry branch is writable */
7752 + err = is_robranch_super(old_dentry->d_sb, bindex);
7756 + dget(lower_old_dentry);
7757 + dget(lower_new_dentry);
7758 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7759 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7761 + /* see Documentation/filesystems/unionfs/issues.txt */
7763 + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7764 + /* source should not be ancenstor of target */
7765 + if (trap == lower_old_dentry) {
7767 + goto out_err_unlock;
7769 + /* target should not be ancenstor of source */
7770 + if (trap == lower_new_dentry) {
7772 + goto out_err_unlock;
7774 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7775 + lower_new_dir_dentry->d_inode, lower_new_dentry);
7778 + /* update parent dir times */
7779 + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7780 + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7782 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7785 + dput(lower_old_dir_dentry);
7786 + dput(lower_new_dir_dentry);
7787 + dput(lower_old_dentry);
7788 + dput(lower_new_dentry);
7792 + /* Fixup the new_dentry. */
7793 + if (bindex < dbstart(new_dentry))
7794 + dbstart(new_dentry) = bindex;
7795 + else if (bindex > dbend(new_dentry))
7796 + dbend(new_dentry) = bindex;
7803 + * Main rename code. This is sufficiently complex, that it's documented in
7804 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7805 + * __unionfs_rename() above to perform some of the work.
7807 +static int do_unionfs_rename(struct inode *old_dir,
7808 + struct dentry *old_dentry,
7809 + struct dentry *old_parent,
7810 + struct inode *new_dir,
7811 + struct dentry *new_dentry,
7812 + struct dentry *new_parent)
7816 + int old_bstart, old_bend;
7817 + int new_bstart, new_bend;
7818 + int do_copyup = -1;
7819 + int local_err = 0;
7823 + old_bstart = dbstart(old_dentry);
7824 + old_bend = dbend(old_dentry);
7826 + new_bstart = dbstart(new_dentry);
7827 + new_bend = dbend(new_dentry);
7829 + /* Rename source to destination. */
7830 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7831 + new_dir, new_dentry, new_parent,
7834 + if (!IS_COPYUP_ERR(err))
7836 + do_copyup = old_bstart - 1;
7842 + * Unlink all instances of destination that exist to the left of
7843 + * bstart of source. On error, revert back, goto out.
7845 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7846 + struct dentry *unlink_dentry;
7847 + struct dentry *unlink_dir_dentry;
7849 + BUG_ON(bindex < 0);
7850 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7851 + if (!unlink_dentry)
7854 + unlink_dir_dentry = lock_parent(unlink_dentry);
7855 + err = is_robranch_super(old_dir->i_sb, bindex);
7857 + err = vfs_unlink(unlink_dir_dentry->d_inode,
7860 + fsstack_copy_attr_times(new_parent->d_inode,
7861 + unlink_dir_dentry->d_inode);
7862 + /* propagate number of hard-links */
7863 + new_parent->d_inode->i_nlink =
7864 + unionfs_get_nlinks(new_parent->d_inode);
7866 + unlock_dir(unlink_dir_dentry);
7868 + if (bindex != new_bstart) {
7869 + dput(unlink_dentry);
7870 + unionfs_set_lower_dentry_idx(new_dentry,
7873 + } else if (IS_COPYUP_ERR(err)) {
7874 + do_copyup = bindex - 1;
7875 + } else if (revert) {
7880 + if (do_copyup != -1) {
7881 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7883 + * copyup the file into some left directory, so that
7884 + * you can rename it
7886 + err = copyup_dentry(old_parent->d_inode,
7887 + old_dentry, old_bstart, bindex,
7888 + old_dentry->d_name.name,
7889 + old_dentry->d_name.len, NULL,
7890 + i_size_read(old_dentry->d_inode));
7891 + /* if copyup failed, try next branch to the left */
7895 + * create whiteout before calling __unionfs_rename
7896 + * because the latter will change the old_dentry's
7897 + * lower name and parent dir, resulting in the
7898 + * whiteout getting created in the wrong dir.
7900 + err = create_whiteout(old_dentry, bindex);
7902 + printk(KERN_ERR "unionfs: can't create a "
7903 + "whiteout for %s in rename (err=%d)\n",
7904 + old_dentry->d_name.name, err);
7907 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7908 + new_dir, new_dentry, new_parent,
7914 + /* make it opaque */
7915 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7916 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7922 + * Create whiteout for source, only if:
7923 + * (1) There is more than one underlying instance of source.
7924 + * (We did a copy_up is taken care of above).
7926 + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7927 + err = create_whiteout(old_dentry, old_bstart);
7929 + /* can't fix anything now, so we exit with -EIO */
7930 + printk(KERN_ERR "unionfs: can't create a whiteout for "
7931 + "%s in rename!\n", old_dentry->d_name.name);
7940 + /* Do revert here. */
7941 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7944 + printk(KERN_ERR "unionfs: revert failed in rename: "
7945 + "the new refresh failed\n");
7949 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7952 + printk(KERN_ERR "unionfs: revert failed in rename: "
7953 + "the old refresh failed\n");
7958 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7959 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7960 + printk(KERN_ERR "unionfs: revert failed in rename: "
7961 + "the object disappeared from under us!\n");
7966 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
7967 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
7968 + printk(KERN_ERR "unionfs: revert failed in rename: "
7969 + "the object was created underneath us!\n");
7974 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
7975 + old_dir, old_dentry, old_parent,
7978 + /* If we can't fix it, then we cop-out with -EIO. */
7980 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
7984 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7988 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
8000 + * We can't copyup a directory, because it may involve huge numbers of
8001 + * children, etc. Doing that in the kernel would be bad, so instead we
8002 + * return EXDEV to the user-space utility that caused this, and let the
8003 + * user-space recurse and ask us to copy up each file separately.
8005 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
8009 + err = check_empty(dentry, parent, NULL);
8010 + if (err == -ENOTEMPTY) {
8011 + if (is_robranch(dentry))
8017 + bstart = dbstart(dentry);
8018 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8021 + dbstart(dentry) = bstart + 1;
8022 + err = check_empty(dentry, parent, NULL);
8023 + dbstart(dentry) = bstart;
8024 + if (err == -ENOTEMPTY)
8030 + * The locking rules in unionfs_rename are complex. We could use a simpler
8031 + * superblock-level name-space lock for renames and copy-ups.
8033 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8034 + struct inode *new_dir, struct dentry *new_dentry)
8037 + struct dentry *wh_dentry;
8038 + struct dentry *old_parent, *new_parent;
8041 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
8042 + old_parent = dget_parent(old_dentry);
8043 + new_parent = dget_parent(new_dentry);
8044 + /* un/lock parent dentries only if they differ from old/new_dentry */
8045 + if (old_parent != old_dentry &&
8046 + old_parent != new_dentry)
8047 + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
8048 + if (new_parent != old_dentry &&
8049 + new_parent != new_dentry &&
8050 + new_parent != old_parent)
8051 + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
8052 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8054 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
8059 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
8060 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
8067 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8068 + err = unionfs_partial_lookup(old_dentry, old_parent);
8070 + err = may_rename_dir(old_dentry, old_parent);
8075 + err = unionfs_partial_lookup(new_dentry, new_parent);
8080 + * if new_dentry is already lower because of whiteout,
8081 + * simply override it even if the whited-out dir is not empty.
8083 + wh_dentry = find_first_whiteout(new_dentry);
8084 + if (!IS_ERR(wh_dentry)) {
8086 + } else if (new_dentry->d_inode) {
8087 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8088 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8089 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8090 + -ENOTDIR : -EISDIR;
8094 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8095 + struct unionfs_dir_state *namelist = NULL;
8096 + /* check if this unionfs directory is empty or not */
8097 + err = check_empty(new_dentry, new_parent, &namelist);
8101 + if (!is_robranch(new_dentry))
8102 + err = delete_whiteouts(new_dentry,
8103 + dbstart(new_dentry),
8106 + free_rdstate(namelist);
8113 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8114 + new_dir, new_dentry, new_parent);
8119 + * force re-lookup since the dir on ro branch is not renamed, and
8120 + * lower dentries still indicate the un-renamed ones.
8122 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8123 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8125 + unionfs_postcopyup_release(old_dentry);
8126 + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8127 + unionfs_postcopyup_release(new_dentry);
8128 + unionfs_postcopyup_setmnt(new_dentry);
8129 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8131 + * If we get here, it means that no copyup was
8132 + * needed, and that a file by the old name already
8133 + * existing on the destination branch; that file got
8134 + * renamed earlier in this function, so all we need
8135 + * to do here is set the lower inode.
8137 + struct inode *inode;
8138 + inode = unionfs_lower_inode(old_dentry->d_inode);
8140 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8141 + dbstart(new_dentry),
8145 + /* if all of this renaming succeeded, update our times */
8146 + unionfs_copy_attr_times(old_dentry->d_inode);
8147 + unionfs_copy_attr_times(new_dentry->d_inode);
8148 + unionfs_check_inode(old_dir);
8149 + unionfs_check_inode(new_dir);
8150 + unionfs_check_dentry(old_dentry);
8151 + unionfs_check_dentry(new_dentry);
8154 + if (err) /* clear the new_dentry stuff created */
8155 + d_drop(new_dentry);
8157 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8158 + if (new_parent != old_dentry &&
8159 + new_parent != new_dentry &&
8160 + new_parent != old_parent)
8161 + unionfs_unlock_dentry(new_parent);
8162 + if (old_parent != old_dentry &&
8163 + old_parent != new_dentry)
8164 + unionfs_unlock_dentry(old_parent);
8167 + unionfs_read_unlock(old_dentry->d_sb);
8171 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8172 new file mode 100644
8173 index 0000000..5dd487a
8175 +++ b/fs/unionfs/sioq.c
8178 + * Copyright (c) 2006-2009 Erez Zadok
8179 + * Copyright (c) 2006 Charles P. Wright
8180 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8181 + * Copyright (c) 2006 Junjiro Okajima
8182 + * Copyright (c) 2006 David P. Quigley
8183 + * Copyright (c) 2006-2009 Stony Brook University
8184 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8186 + * This program is free software; you can redistribute it and/or modify
8187 + * it under the terms of the GNU General Public License version 2 as
8188 + * published by the Free Software Foundation.
8194 + * Super-user IO work Queue - sometimes we need to perform actions which
8195 + * would fail due to the unix permissions on the parent directory (e.g.,
8196 + * rmdir a directory which appears empty, but in reality contains
8200 +static struct workqueue_struct *superio_workqueue;
8202 +int __init init_sioq(void)
8206 + superio_workqueue = create_workqueue("unionfs_siod");
8207 + if (!IS_ERR(superio_workqueue))
8210 + err = PTR_ERR(superio_workqueue);
8211 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8212 + superio_workqueue = NULL;
8216 +void stop_sioq(void)
8218 + if (superio_workqueue)
8219 + destroy_workqueue(superio_workqueue);
8222 +void run_sioq(work_func_t func, struct sioq_args *args)
8224 + INIT_WORK(&args->work, func);
8226 + init_completion(&args->comp);
8227 + while (!queue_work(superio_workqueue, &args->work)) {
8228 + /* TODO: do accounting if needed */
8231 + wait_for_completion(&args->comp);
8234 +void __unionfs_create(struct work_struct *work)
8236 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8237 + struct create_args *c = &args->create;
8239 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8240 + complete(&args->comp);
8243 +void __unionfs_mkdir(struct work_struct *work)
8245 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8246 + struct mkdir_args *m = &args->mkdir;
8248 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8249 + complete(&args->comp);
8252 +void __unionfs_mknod(struct work_struct *work)
8254 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8255 + struct mknod_args *m = &args->mknod;
8257 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8258 + complete(&args->comp);
8261 +void __unionfs_symlink(struct work_struct *work)
8263 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8264 + struct symlink_args *s = &args->symlink;
8266 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8267 + complete(&args->comp);
8270 +void __unionfs_unlink(struct work_struct *work)
8272 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8273 + struct unlink_args *u = &args->unlink;
8275 + args->err = vfs_unlink(u->parent, u->dentry);
8276 + complete(&args->comp);
8278 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8279 new file mode 100644
8280 index 0000000..3d7869a
8282 +++ b/fs/unionfs/sioq.h
8285 + * Copyright (c) 2006-2009 Erez Zadok
8286 + * Copyright (c) 2006 Charles P. Wright
8287 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8288 + * Copyright (c) 2006 Junjiro Okajima
8289 + * Copyright (c) 2006 David P. Quigley
8290 + * Copyright (c) 2006-2009 Stony Brook University
8291 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8293 + * This program is free software; you can redistribute it and/or modify
8294 + * it under the terms of the GNU General Public License version 2 as
8295 + * published by the Free Software Foundation.
8301 +struct deletewh_args {
8302 + struct unionfs_dir_state *namelist;
8303 + struct dentry *dentry;
8307 +struct is_opaque_args {
8308 + struct dentry *dentry;
8311 +struct create_args {
8312 + struct inode *parent;
8313 + struct dentry *dentry;
8315 + struct nameidata *nd;
8318 +struct mkdir_args {
8319 + struct inode *parent;
8320 + struct dentry *dentry;
8324 +struct mknod_args {
8325 + struct inode *parent;
8326 + struct dentry *dentry;
8331 +struct symlink_args {
8332 + struct inode *parent;
8333 + struct dentry *dentry;
8337 +struct unlink_args {
8338 + struct inode *parent;
8339 + struct dentry *dentry;
8344 + struct completion comp;
8345 + struct work_struct work;
8350 + struct deletewh_args deletewh;
8351 + struct is_opaque_args is_opaque;
8352 + struct create_args create;
8353 + struct mkdir_args mkdir;
8354 + struct mknod_args mknod;
8355 + struct symlink_args symlink;
8356 + struct unlink_args unlink;
8360 +/* Extern definitions for SIOQ functions */
8361 +extern int __init init_sioq(void);
8362 +extern void stop_sioq(void);
8363 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8365 +/* Extern definitions for our privilege escalation helpers */
8366 +extern void __unionfs_create(struct work_struct *work);
8367 +extern void __unionfs_mkdir(struct work_struct *work);
8368 +extern void __unionfs_mknod(struct work_struct *work);
8369 +extern void __unionfs_symlink(struct work_struct *work);
8370 +extern void __unionfs_unlink(struct work_struct *work);
8371 +extern void __delete_whiteouts(struct work_struct *work);
8372 +extern void __is_opaque_dir(struct work_struct *work);
8374 +#endif /* not _SIOQ_H */
8375 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8376 new file mode 100644
8377 index 0000000..018b4fd
8379 +++ b/fs/unionfs/subr.c
8382 + * Copyright (c) 2003-2009 Erez Zadok
8383 + * Copyright (c) 2003-2006 Charles P. Wright
8384 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8385 + * Copyright (c) 2005-2006 Junjiro Okajima
8386 + * Copyright (c) 2005 Arun M. Krishnakumar
8387 + * Copyright (c) 2004-2006 David P. Quigley
8388 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8389 + * Copyright (c) 2003 Puja Gupta
8390 + * Copyright (c) 2003 Harikesavan Krishnan
8391 + * Copyright (c) 2003-2009 Stony Brook University
8392 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8394 + * This program is free software; you can redistribute it and/or modify
8395 + * it under the terms of the GNU General Public License version 2 as
8396 + * published by the Free Software Foundation.
8402 + * returns the right n_link value based on the inode type
8404 +int unionfs_get_nlinks(const struct inode *inode)
8406 + /* don't bother to do all the work since we're unlinked */
8407 + if (inode->i_nlink == 0)
8410 + if (!S_ISDIR(inode->i_mode))
8411 + return unionfs_lower_inode(inode)->i_nlink;
8414 + * For directories, we return 1. The only place that could cares
8415 + * about links is readdir, and there's d_type there so even that
8421 +/* copy a/m/ctime from the lower branch with the newest times */
8422 +void unionfs_copy_attr_times(struct inode *upper)
8425 + struct inode *lower;
8429 + if (ibstart(upper) < 0) {
8430 +#ifdef CONFIG_UNION_FS_DEBUG
8431 + WARN_ON(ibstart(upper) < 0);
8432 +#endif /* CONFIG_UNION_FS_DEBUG */
8435 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8436 + lower = unionfs_lower_inode_idx(upper, bindex);
8438 + continue; /* not all lower dir objects may exist */
8439 + if (unlikely(timespec_compare(&upper->i_mtime,
8440 + &lower->i_mtime) < 0))
8441 + upper->i_mtime = lower->i_mtime;
8442 + if (unlikely(timespec_compare(&upper->i_ctime,
8443 + &lower->i_ctime) < 0))
8444 + upper->i_ctime = lower->i_ctime;
8445 + if (unlikely(timespec_compare(&upper->i_atime,
8446 + &lower->i_atime) < 0))
8447 + upper->i_atime = lower->i_atime;
8452 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8453 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8454 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8455 + * important if the lower inode is a directory type)
8457 +void unionfs_copy_attr_all(struct inode *dest,
8458 + const struct inode *src)
8460 + dest->i_mode = src->i_mode;
8461 + dest->i_uid = src->i_uid;
8462 + dest->i_gid = src->i_gid;
8463 + dest->i_rdev = src->i_rdev;
8465 + unionfs_copy_attr_times(dest);
8467 + dest->i_blkbits = src->i_blkbits;
8468 + dest->i_flags = src->i_flags;
8471 + * Update the nlinks AFTER updating the above fields, because the
8472 + * get_links callback may depend on them.
8474 + dest->i_nlink = unionfs_get_nlinks(dest);
8476 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8477 new file mode 100644
8478 index 0000000..ded7b84
8480 +++ b/fs/unionfs/super.c
8483 + * Copyright (c) 2003-2009 Erez Zadok
8484 + * Copyright (c) 2003-2006 Charles P. Wright
8485 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8486 + * Copyright (c) 2005-2006 Junjiro Okajima
8487 + * Copyright (c) 2005 Arun M. Krishnakumar
8488 + * Copyright (c) 2004-2006 David P. Quigley
8489 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8490 + * Copyright (c) 2003 Puja Gupta
8491 + * Copyright (c) 2003 Harikesavan Krishnan
8492 + * Copyright (c) 2003-2009 Stony Brook University
8493 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8495 + * This program is free software; you can redistribute it and/or modify
8496 + * it under the terms of the GNU General Public License version 2 as
8497 + * published by the Free Software Foundation.
8503 + * The inode cache is used with alloc_inode for both our inode info and the
8506 +static struct kmem_cache *unionfs_inode_cachep;
8508 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8511 + struct unionfs_inode_info *info;
8512 + struct inode *inode;
8514 + inode = iget_locked(sb, ino);
8516 + return ERR_PTR(-ENOMEM);
8517 + if (!(inode->i_state & I_NEW))
8520 + info = UNIONFS_I(inode);
8521 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8522 + info->bstart = -1;
8524 + atomic_set(&info->generation,
8525 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8526 + spin_lock_init(&info->rdlock);
8527 + info->rdcount = 1;
8528 + info->hashsize = -1;
8529 + INIT_LIST_HEAD(&info->readdircache);
8531 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8532 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8533 + if (unlikely(!info->lower_inodes)) {
8534 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8535 + "lower-pointer array!\n");
8536 + iget_failed(inode);
8537 + return ERR_PTR(-ENOMEM);
8540 + inode->i_version++;
8541 + inode->i_op = &unionfs_main_iops;
8542 + inode->i_fop = &unionfs_main_fops;
8544 + inode->i_mapping->a_ops = &unionfs_aops;
8547 + * reset times so unionfs_copy_attr_all can keep out time invariants
8548 + * right (upper inode time being the max of all lower ones).
8550 + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8551 + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8552 + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8553 + unlock_new_inode(inode);
8558 + * we now define delete_inode, because there are two VFS paths that may
8559 + * destroy an inode: one of them calls clear inode before doing everything
8560 + * else that's needed, and the other is fine. This way we truncate the inode
8561 + * size (and its pages) and then clear our own inode, which will do an iput
8562 + * on our and the lower inode.
8564 + * No need to lock sb info's rwsem.
8566 +static void unionfs_delete_inode(struct inode *inode)
8568 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8569 + spin_lock(&inode->i_lock);
8571 + i_size_write(inode, 0); /* every f/s seems to do that */
8572 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8573 + spin_unlock(&inode->i_lock);
8576 + if (inode->i_data.nrpages)
8577 + truncate_inode_pages(&inode->i_data, 0);
8579 + clear_inode(inode);
8583 + * final actions when unmounting a file system
8585 + * No need to lock rwsem.
8587 +static void unionfs_put_super(struct super_block *sb)
8589 + int bindex, bstart, bend;
8590 + struct unionfs_sb_info *spd;
8593 + spd = UNIONFS_SB(sb);
8597 + bstart = sbstart(sb);
8600 + /* Make sure we have no leaks of branchget/branchput. */
8601 + for (bindex = bstart; bindex <= bend; bindex++)
8602 + if (unlikely(branch_count(sb, bindex) != 0)) {
8604 + "unionfs: branch %d has %d references left!\n",
8605 + bindex, branch_count(sb, bindex));
8608 + WARN_ON(leaks != 0);
8610 + /* decrement lower super references */
8611 + for (bindex = bstart; bindex <= bend; bindex++) {
8612 + struct super_block *s;
8613 + s = unionfs_lower_super_idx(sb, bindex);
8614 + unionfs_set_lower_super_idx(sb, bindex, NULL);
8615 + atomic_dec(&s->s_active);
8618 + kfree(spd->dev_name);
8621 + sb->s_fs_info = NULL;
8625 + * Since people use this to answer the "How big of a file can I write?"
8626 + * question, we report the size of the highest priority branch as the size of
8629 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8632 + struct super_block *sb;
8633 + struct dentry *lower_dentry;
8634 + struct dentry *parent;
8637 + sb = dentry->d_sb;
8639 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8640 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8641 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8643 + valid = __unionfs_d_revalidate(dentry, parent, false);
8644 + if (unlikely(!valid)) {
8648 + unionfs_check_dentry(dentry);
8650 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8651 + err = vfs_statfs(lower_dentry, buf);
8653 + /* set return buf to our f/s to avoid confusing user-level utils */
8654 + buf->f_type = UNIONFS_SUPER_MAGIC;
8656 + * Our maximum file name can is shorter by a few bytes because every
8657 + * file name could potentially be whited-out.
8659 + * XXX: this restriction goes away with ODF.
8661 + unionfs_set_max_namelen(&buf->f_namelen);
8664 + * reset two fields to avoid confusing user-land.
8665 + * XXX: is this still necessary?
8667 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8668 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8671 + unionfs_check_dentry(dentry);
8672 + unionfs_unlock_dentry(dentry);
8673 + unionfs_unlock_parent(dentry, parent);
8674 + unionfs_read_unlock(sb);
8678 +/* handle mode changing during remount */
8679 +static noinline_for_stack int do_remount_mode_option(
8682 + struct unionfs_data *new_data,
8683 + struct path *new_lower_paths)
8685 + int err = -EINVAL;
8687 + char *modename = strchr(optarg, '=');
8688 + struct nameidata nd;
8690 + /* by now, optarg contains the branch name */
8693 + "unionfs: no branch specified for mode change\n");
8697 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8701 + *modename++ = '\0';
8702 + err = parse_branch_mode(modename, &perms);
8704 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8705 + modename, optarg);
8710 + * Find matching branch index. For now, this assumes that nothing
8711 + * has been mounted on top of this Unionfs stack. Once we have /odf
8712 + * and cache-coherency resolved, we'll address the branch-path
8715 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8717 + printk(KERN_ERR "unionfs: error accessing "
8718 + "lower directory \"%s\" (error %d)\n",
8722 + for (idx = 0; idx < cur_branches; idx++)
8723 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8724 + nd.path.dentry == new_lower_paths[idx].dentry)
8726 + path_put(&nd.path); /* no longer needed */
8727 + if (idx == cur_branches) {
8728 + err = -ENOENT; /* err may have been reset above */
8729 + printk(KERN_ERR "unionfs: branch \"%s\" "
8730 + "not found\n", optarg);
8733 + /* check/change mode for existing branch */
8734 + /* we don't warn if perms==branchperms */
8735 + new_data[idx].branchperms = perms;
8741 +/* handle branch deletion during remount */
8742 +static noinline_for_stack int do_remount_del_option(
8743 + char *optarg, int cur_branches,
8744 + struct unionfs_data *new_data,
8745 + struct path *new_lower_paths)
8747 + int err = -EINVAL;
8749 + struct nameidata nd;
8751 + /* optarg contains the branch name to delete */
8754 + * Find matching branch index. For now, this assumes that nothing
8755 + * has been mounted on top of this Unionfs stack. Once we have /odf
8756 + * and cache-coherency resolved, we'll address the branch-path
8759 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8761 + printk(KERN_ERR "unionfs: error accessing "
8762 + "lower directory \"%s\" (error %d)\n",
8766 + for (idx = 0; idx < cur_branches; idx++)
8767 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8768 + nd.path.dentry == new_lower_paths[idx].dentry)
8770 + path_put(&nd.path); /* no longer needed */
8771 + if (idx == cur_branches) {
8772 + printk(KERN_ERR "unionfs: branch \"%s\" "
8773 + "not found\n", optarg);
8777 + /* check if there are any open files on the branch to be deleted */
8778 + if (atomic_read(&new_data[idx].open_files) > 0) {
8784 + * Now we have to delete the branch. First, release any handles it
8785 + * has. Then, move the remaining array indexes past "idx" in
8786 + * new_data and new_lower_paths one to the left. Finally, adjust
8789 + path_put(&new_lower_paths[idx]);
8791 + if (idx < cur_branches - 1) {
8792 + /* if idx==cur_branches-1, we delete last branch: easy */
8793 + memmove(&new_data[idx], &new_data[idx+1],
8794 + (cur_branches - 1 - idx) *
8795 + sizeof(struct unionfs_data));
8796 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8797 + (cur_branches - 1 - idx) * sizeof(struct path));
8805 +/* handle branch insertion during remount */
8806 +static noinline_for_stack int do_remount_add_option(
8807 + char *optarg, int cur_branches,
8808 + struct unionfs_data *new_data,
8809 + struct path *new_lower_paths,
8810 + int *high_branch_id)
8812 + int err = -EINVAL;
8814 + int idx = 0; /* default: insert at beginning */
8815 + char *new_branch , *modename = NULL;
8816 + struct nameidata nd;
8819 + * optarg can be of several forms:
8821 + * /bar:/foo insert /foo before /bar
8822 + * /bar:/foo=ro insert /foo in ro mode before /bar
8823 + * /foo insert /foo in the beginning (prepend)
8824 + * :/foo insert /foo at the end (append)
8826 + if (*optarg == ':') { /* append? */
8827 + new_branch = optarg + 1; /* skip ':' */
8828 + idx = cur_branches;
8829 + goto found_insertion_point;
8831 + new_branch = strchr(optarg, ':');
8832 + if (!new_branch) { /* prepend? */
8833 + new_branch = optarg;
8834 + goto found_insertion_point;
8836 + *new_branch++ = '\0'; /* holds path+mode of new branch */
8839 + * Find matching branch index. For now, this assumes that nothing
8840 + * has been mounted on top of this Unionfs stack. Once we have /odf
8841 + * and cache-coherency resolved, we'll address the branch-path
8844 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8846 + printk(KERN_ERR "unionfs: error accessing "
8847 + "lower directory \"%s\" (error %d)\n",
8851 + for (idx = 0; idx < cur_branches; idx++)
8852 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8853 + nd.path.dentry == new_lower_paths[idx].dentry)
8855 + path_put(&nd.path); /* no longer needed */
8856 + if (idx == cur_branches) {
8857 + printk(KERN_ERR "unionfs: branch \"%s\" "
8858 + "not found\n", optarg);
8864 + * At this point idx will hold the index where the new branch should
8865 + * be inserted before.
8867 +found_insertion_point:
8868 + /* find the mode for the new branch */
8870 + modename = strchr(new_branch, '=');
8872 + *modename++ = '\0';
8873 + if (!new_branch || !*new_branch) {
8874 + printk(KERN_ERR "unionfs: null new branch\n");
8878 + err = parse_branch_mode(modename, &perms);
8880 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8881 + "branch \"%s\"\n", modename, new_branch);
8884 + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8886 + printk(KERN_ERR "unionfs: error accessing "
8887 + "lower directory \"%s\" (error %d)\n",
8892 + * It's probably safe to check_mode the new branch to insert. Note:
8893 + * we don't allow inserting branches which are unionfs's by
8894 + * themselves (check_branch returns EINVAL in that case). This is
8895 + * because this code base doesn't support stacking unionfs: the ODF
8896 + * code base supports that correctly.
8898 + err = check_branch(&nd);
8900 + printk(KERN_ERR "unionfs: lower directory "
8901 + "\"%s\" is not a valid branch\n", optarg);
8902 + path_put(&nd.path);
8907 + * Now we have to insert the new branch. But first, move the bits
8908 + * to make space for the new branch, if needed. Finally, adjust
8910 + * We don't release nd here; it's kept until umount/remount.
8912 + if (idx < cur_branches) {
8913 + /* if idx==cur_branches, we append: easy */
8914 + memmove(&new_data[idx+1], &new_data[idx],
8915 + (cur_branches - idx) * sizeof(struct unionfs_data));
8916 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8917 + (cur_branches - idx) * sizeof(struct path));
8919 + new_lower_paths[idx].dentry = nd.path.dentry;
8920 + new_lower_paths[idx].mnt = nd.path.mnt;
8922 + new_data[idx].sb = nd.path.dentry->d_sb;
8923 + atomic_set(&new_data[idx].open_files, 0);
8924 + new_data[idx].branchperms = perms;
8925 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8934 + * Support branch management options on remount.
8936 + * See Documentation/filesystems/unionfs/ for details.
8938 + * @flags: numeric mount options
8939 + * @options: mount options string
8941 + * This function can rearrange a mounted union dynamically, adding and
8942 + * removing branches, including changing branch modes. Clearly this has to
8943 + * be done safely and atomically. Luckily, the VFS already calls this
8944 + * function with lock_super(sb) and lock_kernel() held, preventing
8945 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8946 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8947 + * to purge dentries/inodes from our superblock, and also called
8948 + * fsync_super(sb) to purge any dirty pages. So we're good.
8950 + * XXX: however, our remount code may also need to invalidate mapped pages
8951 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8952 + * branches. This has to wait for proper mmap and cache coherency support
8956 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8961 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
8963 + int cur_branches = 0; /* no. of current branches */
8964 + int new_branches = 0; /* no. of branches actually left in the end */
8965 + int add_branches; /* est. no. of branches to add */
8966 + int del_branches; /* est. no. of branches to del */
8967 + int max_branches; /* max possible no. of branches */
8968 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
8969 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
8970 + struct inode **new_lower_inodes = NULL;
8971 + int new_high_branch_id; /* new high branch ID */
8972 + int size; /* memory allocation size, temp var */
8973 + int old_ibstart, old_ibend;
8975 + unionfs_write_lock(sb);
8978 + * The VFS will take care of "ro" and "rw" flags, and we can safely
8979 + * ignore MS_SILENT, but anything else left over is an error. So we
8980 + * need to check if any other flags may have been passed (none are
8981 + * allowed/supported as of now).
8983 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8985 + "unionfs: remount flags 0x%x unsupported\n", *flags);
8991 + * If 'options' is NULL, it's probably because the user just changed
8992 + * the union to a "ro" or "rw" and the VFS took care of it. So
8993 + * nothing to do and we're done.
8995 + if (!options || options[0] == '\0')
8999 + * Find out how many branches we will have in the end, counting
9000 + * "add" and "del" commands. Copy the "options" string because
9001 + * strsep modifies the string and we need it later.
9003 + tmp_to_free = kstrdup(options, GFP_KERNEL);
9004 + optionstmp = tmp_to_free;
9005 + if (unlikely(!optionstmp)) {
9009 + cur_branches = sbmax(sb); /* current no. branches */
9010 + new_branches = sbmax(sb);
9013 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9014 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
9017 + if (!optname || !*optname)
9020 + optarg = strchr(optname, '=');
9024 + if (!strcmp("add", optname))
9026 + else if (!strcmp("del", optname))
9029 + kfree(tmp_to_free);
9030 + /* after all changes, will we have at least one branch left? */
9031 + if ((new_branches + add_branches - del_branches) < 1) {
9033 + "unionfs: no branches left after remount\n");
9039 + * Since we haven't actually parsed all the add/del options, nor
9040 + * have we checked them for errors, we don't know for sure how many
9041 + * branches we will have after all changes have taken place. In
9042 + * fact, the total number of branches left could be less than what
9043 + * we have now. So we need to allocate space for a temporary
9044 + * placeholder that is at least as large as the maximum number of
9045 + * branches we *could* have, which is the current number plus all
9046 + * the additions. Once we're done with these temp placeholders, we
9047 + * may have to re-allocate the final size, copy over from the temp,
9048 + * and then free the temps (done near the end of this function).
9050 + max_branches = cur_branches + add_branches;
9051 + /* allocate space for new pointers to lower dentry */
9052 + tmp_data = kcalloc(max_branches,
9053 + sizeof(struct unionfs_data), GFP_KERNEL);
9054 + if (unlikely(!tmp_data)) {
9058 + /* allocate space for new pointers to lower paths */
9059 + tmp_lower_paths = kcalloc(max_branches,
9060 + sizeof(struct path), GFP_KERNEL);
9061 + if (unlikely(!tmp_lower_paths)) {
9065 + /* copy current info into new placeholders, incrementing refcnts */
9066 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9067 + cur_branches * sizeof(struct unionfs_data));
9068 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9069 + cur_branches * sizeof(struct path));
9070 + for (i = 0; i < cur_branches; i++)
9071 + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9073 + /*******************************************************************
9074 + * For each branch command, do path_lookup on the requested branch,
9075 + * and apply the change to a temp branch list. To handle errors, we
9076 + * already dup'ed the old arrays (above), and increased the refcnts
9077 + * on various f/s objects. So now we can do all the path_lookups
9078 + * and branch-management commands on the new arrays. If it fail mid
9079 + * way, we free the tmp arrays and *put all objects. If we succeed,
9080 + * then we free old arrays and *put its objects, and then replace
9081 + * the arrays with the new tmp list (we may have to re-allocate the
9082 + * memory because the temp lists could have been larger than what we
9083 + * actually needed).
9084 + *******************************************************************/
9086 + while ((optname = strsep(&options, ",")) != NULL) {
9089 + if (!optname || !*optname)
9092 + * At this stage optname holds a comma-delimited option, but
9093 + * without the commas. Next, we need to break the string on
9094 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9095 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9096 + * KEY is "/foo", and VAL is "rw".
9098 + optarg = strchr(optname, '=');
9101 + /* incgen remount option (instead of old ioctl) */
9102 + if (!strcmp("incgen", optname)) {
9104 + goto out_no_change;
9108 + * All of our options take an argument now. (Insert ones
9109 + * that don't above this check.) So at this stage optname
9110 + * contains the CMD part and optarg contains the ARG part.
9112 + if (!optarg || !*optarg) {
9113 + printk(KERN_ERR "unionfs: all remount options require "
9114 + "an argument (%s)\n", optname);
9119 + if (!strcmp("add", optname)) {
9120 + err = do_remount_add_option(optarg, new_branches,
9123 + &new_high_branch_id);
9127 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9128 + printk(KERN_ERR "unionfs: command exceeds "
9129 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9135 + if (!strcmp("del", optname)) {
9136 + err = do_remount_del_option(optarg, new_branches,
9144 + if (!strcmp("mode", optname)) {
9145 + err = do_remount_mode_option(optarg, new_branches,
9154 + * When you use "mount -o remount,ro", mount(8) will
9155 + * reportedly pass the original dirs= string from
9156 + * /proc/mounts. So for now, we have to ignore dirs= and
9157 + * not consider it an error, unless we want to allow users
9158 + * to pass dirs= in remount. Note that to allow the VFS to
9159 + * actually process the ro/rw remount options, we have to
9160 + * return 0 from this function.
9162 + if (!strcmp("dirs", optname)) {
9163 + printk(KERN_WARNING
9164 + "unionfs: remount ignoring option \"%s\"\n",
9171 + "unionfs: unrecognized option \"%s\"\n", optname);
9177 + /******************************************************************
9178 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9179 + * see if we need to allocate a small-sized new vector, copy the
9180 + * vectors to their correct place, release the refcnt of the older
9181 + * ones, and return. Also handle invalidating any pages that will
9182 + * have to be re-read.
9183 + *******************************************************************/
9185 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9186 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9187 + "(use \"remount,ro\" to create a read-only union)\n");
9192 + /* (re)allocate space for new pointers to lower dentry */
9193 + size = new_branches * sizeof(struct unionfs_data);
9194 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9195 + if (unlikely(!new_data)) {
9200 + /* allocate space for new pointers to lower paths */
9201 + size = new_branches * sizeof(struct path);
9202 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9203 + if (unlikely(!new_lower_paths)) {
9208 + /* allocate space for new pointers to lower inodes */
9209 + new_lower_inodes = kcalloc(new_branches,
9210 + sizeof(struct inode *), GFP_KERNEL);
9211 + if (unlikely(!new_lower_inodes)) {
9217 + * OK, just before we actually put the new set of branches in place,
9218 + * we need to ensure that our own f/s has no dirty objects left.
9219 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9220 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9221 + * pages. So all that's left is for us to invalidate any leftover
9222 + * (non-dirty) pages to ensure that they will be re-read from the
9223 + * new lower branches (and to support mmap).
9227 + * Once we finish the remounting successfully, our superblock
9228 + * generation number will have increased. This will be detected by
9229 + * our dentry-revalidation code upon subsequent f/s operations
9230 + * through unionfs. The revalidation code will rebuild the union of
9231 + * lower inodes for a given unionfs inode and invalidate any pages
9232 + * of such "stale" inodes (by calling our purge_inode_data
9233 + * function). This revalidation will happen lazily and
9234 + * incrementally, as users perform operations on cached inodes. We
9235 + * would like to encourage this revalidation to happen sooner if
9236 + * possible, so we like to try to invalidate as many other pages in
9237 + * our superblock as we can. We used to call drop_pagecache_sb() or
9238 + * a variant thereof, but either method was racy (drop_caches alone
9239 + * is known to be racy). So now we let the revalidation happen on a
9240 + * per file basis in ->d_revalidate.
9243 + /* grab new lower super references; release old ones */
9244 + for (i = 0; i < new_branches; i++)
9245 + atomic_inc(&new_data[i].sb->s_active);
9246 + for (i = 0; i < sbmax(sb); i++)
9247 + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9249 + /* copy new vectors into their correct place */
9250 + tmp_data = UNIONFS_SB(sb)->data;
9251 + UNIONFS_SB(sb)->data = new_data;
9252 + new_data = NULL; /* so don't free good pointers below */
9253 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9254 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9255 + new_lower_paths = NULL; /* so don't free good pointers below */
9257 + /* update our unionfs_sb_info and root dentry index of last branch */
9258 + i = sbmax(sb); /* save no. of branches to release at end */
9259 + sbend(sb) = new_branches - 1;
9260 + dbend(sb->s_root) = new_branches - 1;
9261 + old_ibstart = ibstart(sb->s_root->d_inode);
9262 + old_ibend = ibend(sb->s_root->d_inode);
9263 + ibend(sb->s_root->d_inode) = new_branches - 1;
9264 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9265 + new_branches = i; /* no. of branches to release below */
9268 + * Update lower inodes: 3 steps
9269 + * 1. grab ref on all new lower inodes
9271 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9272 + struct dentry *lower_dentry =
9273 + unionfs_lower_dentry_idx(sb->s_root, i);
9274 + igrab(lower_dentry->d_inode);
9275 + new_lower_inodes[i] = lower_dentry->d_inode;
9277 + /* 2. release reference on all older lower inodes */
9278 + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9279 + /* 3. update root dentry's inode to new lower_inodes array */
9280 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9281 + new_lower_inodes = NULL;
9283 + /* maxbytes may have changed */
9284 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9285 + /* update high branch ID */
9286 + sbhbid(sb) = new_high_branch_id;
9288 + /* update our sb->generation for revalidating objects */
9289 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9290 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9291 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9292 + if (!(*flags & MS_SILENT))
9293 + pr_info("unionfs: %s: new generation number %d\n",
9294 + UNIONFS_SB(sb)->dev_name, i);
9295 + /* finally, update the root dentry's times */
9296 + unionfs_copy_attr_times(sb->s_root->d_inode);
9297 + err = 0; /* reset to success */
9300 + * The code above falls through to the next label, and releases the
9301 + * refcnts of the older ones (stored in tmp_*): if we fell through
9302 + * here, it means success. However, if we jump directly to this
9303 + * label from any error above, then an error occurred after we
9304 + * grabbed various refcnts, and so we have to release the
9305 + * temporarily constructed structures.
9308 + /* no need to cleanup/release anything in tmp_data */
9309 + if (tmp_lower_paths)
9310 + for (i = 0; i < new_branches; i++)
9311 + path_put(&tmp_lower_paths[i]);
9313 + kfree(tmp_lower_paths);
9315 + kfree(new_lower_paths);
9317 + kfree(new_lower_inodes);
9319 + unionfs_check_dentry(sb->s_root);
9320 + unionfs_write_unlock(sb);
9325 + * Called by iput() when the inode reference count reached zero
9326 + * and the inode is not hashed anywhere. Used to clear anything
9327 + * that needs to be, before the inode is completely destroyed and put
9328 + * on the inode free list.
9330 + * No need to lock sb info's rwsem.
9332 +static void unionfs_clear_inode(struct inode *inode)
9334 + int bindex, bstart, bend;
9335 + struct inode *lower_inode;
9336 + struct list_head *pos, *n;
9337 + struct unionfs_dir_state *rdstate;
9339 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9340 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9341 + list_del(&rdstate->cache);
9342 + free_rdstate(rdstate);
9346 + * Decrement a reference to a lower_inode, which was incremented
9347 + * by our read_inode when it was created initially.
9349 + bstart = ibstart(inode);
9350 + bend = ibend(inode);
9351 + if (bstart >= 0) {
9352 + for (bindex = bstart; bindex <= bend; bindex++) {
9353 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9356 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9357 + /* see Documentation/filesystems/unionfs/issues.txt */
9359 + iput(lower_inode);
9364 + kfree(UNIONFS_I(inode)->lower_inodes);
9365 + UNIONFS_I(inode)->lower_inodes = NULL;
9368 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9370 + struct unionfs_inode_info *i;
9372 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9376 + /* memset everything up to the inode to 0 */
9377 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9379 + i->vfs_inode.i_version = 1;
9380 + return &i->vfs_inode;
9383 +static void unionfs_destroy_inode(struct inode *inode)
9385 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9388 +/* unionfs inode cache constructor */
9389 +static void init_once(void *obj)
9391 + struct unionfs_inode_info *i = obj;
9393 + inode_init_once(&i->vfs_inode);
9396 +int unionfs_init_inode_cache(void)
9400 + unionfs_inode_cachep =
9401 + kmem_cache_create("unionfs_inode_cache",
9402 + sizeof(struct unionfs_inode_info), 0,
9403 + SLAB_RECLAIM_ACCOUNT, init_once);
9404 + if (unlikely(!unionfs_inode_cachep))
9409 +/* unionfs inode cache destructor */
9410 +void unionfs_destroy_inode_cache(void)
9412 + if (unionfs_inode_cachep)
9413 + kmem_cache_destroy(unionfs_inode_cachep);
9417 + * Called when we have a dirty inode, right here we only throw out
9418 + * parts of our readdir list that are too old.
9420 + * No need to grab sb info's rwsem.
9422 +static int unionfs_write_inode(struct inode *inode, int sync)
9424 + struct list_head *pos, *n;
9425 + struct unionfs_dir_state *rdstate;
9427 + spin_lock(&UNIONFS_I(inode)->rdlock);
9428 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9429 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9430 + /* We keep this list in LRU order. */
9431 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9433 + UNIONFS_I(inode)->rdcount--;
9434 + list_del(&rdstate->cache);
9435 + free_rdstate(rdstate);
9437 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9443 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9444 + * code can actually succeed and won't leave tasks that need handling.
9446 +static void unionfs_umount_begin(struct super_block *sb)
9448 + struct super_block *lower_sb;
9449 + int bindex, bstart, bend;
9451 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9453 + bstart = sbstart(sb);
9455 + for (bindex = bstart; bindex <= bend; bindex++) {
9456 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9458 + if (lower_sb && lower_sb->s_op &&
9459 + lower_sb->s_op->umount_begin)
9460 + lower_sb->s_op->umount_begin(lower_sb);
9463 + unionfs_read_unlock(sb);
9466 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9468 + struct super_block *sb = mnt->mnt_sb;
9472 + int bindex, bstart, bend;
9475 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9477 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9479 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9480 + if (unlikely(!tmp_page)) {
9485 + bstart = sbstart(sb);
9488 + seq_printf(m, ",dirs=");
9489 + for (bindex = bstart; bindex <= bend; bindex++) {
9491 + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9492 + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9493 + path = d_path(&p, tmp_page, PAGE_SIZE);
9494 + if (IS_ERR(path)) {
9495 + ret = PTR_ERR(path);
9499 + perms = branchperms(sb, bindex);
9501 + seq_printf(m, "%s=%s", path,
9502 + perms & MAY_WRITE ? "rw" : "ro");
9503 + if (bindex != bend)
9504 + seq_printf(m, ":");
9508 + free_page((unsigned long) tmp_page);
9510 + unionfs_unlock_dentry(sb->s_root);
9512 + unionfs_read_unlock(sb);
9517 +struct super_operations unionfs_sops = {
9518 + .delete_inode = unionfs_delete_inode,
9519 + .put_super = unionfs_put_super,
9520 + .statfs = unionfs_statfs,
9521 + .remount_fs = unionfs_remount_fs,
9522 + .clear_inode = unionfs_clear_inode,
9523 + .umount_begin = unionfs_umount_begin,
9524 + .show_options = unionfs_show_options,
9525 + .write_inode = unionfs_write_inode,
9526 + .alloc_inode = unionfs_alloc_inode,
9527 + .destroy_inode = unionfs_destroy_inode,
9529 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9530 new file mode 100644
9531 index 0000000..1b2b86f
9533 +++ b/fs/unionfs/union.h
9536 + * Copyright (c) 2003-2009 Erez Zadok
9537 + * Copyright (c) 2003-2006 Charles P. Wright
9538 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9539 + * Copyright (c) 2005 Arun M. Krishnakumar
9540 + * Copyright (c) 2004-2006 David P. Quigley
9541 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9542 + * Copyright (c) 2003 Puja Gupta
9543 + * Copyright (c) 2003 Harikesavan Krishnan
9544 + * Copyright (c) 2003-2009 Stony Brook University
9545 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
9547 + * This program is free software; you can redistribute it and/or modify
9548 + * it under the terms of the GNU General Public License version 2 as
9549 + * published by the Free Software Foundation.
9555 +#include <linux/dcache.h>
9556 +#include <linux/file.h>
9557 +#include <linux/list.h>
9558 +#include <linux/fs.h>
9559 +#include <linux/mm.h>
9560 +#include <linux/module.h>
9561 +#include <linux/mount.h>
9562 +#include <linux/namei.h>
9563 +#include <linux/page-flags.h>
9564 +#include <linux/pagemap.h>
9565 +#include <linux/poll.h>
9566 +#include <linux/security.h>
9567 +#include <linux/seq_file.h>
9568 +#include <linux/slab.h>
9569 +#include <linux/spinlock.h>
9570 +#include <linux/smp_lock.h>
9571 +#include <linux/statfs.h>
9572 +#include <linux/string.h>
9573 +#include <linux/vmalloc.h>
9574 +#include <linux/writeback.h>
9575 +#include <linux/buffer_head.h>
9576 +#include <linux/xattr.h>
9577 +#include <linux/fs_stack.h>
9578 +#include <linux/magic.h>
9579 +#include <linux/log2.h>
9580 +#include <linux/poison.h>
9581 +#include <linux/mman.h>
9582 +#include <linux/backing-dev.h>
9583 +#include <linux/splice.h>
9585 +#include <asm/system.h>
9587 +#include <linux/union_fs.h>
9589 +/* the file system name */
9590 +#define UNIONFS_NAME "unionfs"
9592 +/* unionfs root inode number */
9593 +#define UNIONFS_ROOT_INO 1
9595 +/* number of times we try to get a unique temporary file name */
9596 +#define GET_TMPNAM_MAX_RETRY 5
9598 +/* maximum number of branches we support, to avoid memory blowup */
9599 +#define UNIONFS_MAX_BRANCHES 128
9601 +/* minimum time (seconds) required for time-based cache-coherency */
9602 +#define UNIONFS_MIN_CC_TIME 3
9604 +/* Operations vectors defined in specific files. */
9605 +extern struct file_operations unionfs_main_fops;
9606 +extern struct file_operations unionfs_dir_fops;
9607 +extern struct inode_operations unionfs_main_iops;
9608 +extern struct inode_operations unionfs_dir_iops;
9609 +extern struct inode_operations unionfs_symlink_iops;
9610 +extern struct super_operations unionfs_sops;
9611 +extern struct dentry_operations unionfs_dops;
9612 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9613 +extern struct vm_operations_struct unionfs_vm_ops;
9615 +/* How long should an entry be allowed to persist */
9616 +#define RDCACHE_JIFFIES (5*HZ)
9618 +/* compatibility with Real-Time patches */
9619 +#ifdef CONFIG_PREEMPT_RT
9620 +# define unionfs_rw_semaphore compat_rw_semaphore
9621 +#else /* not CONFIG_PREEMPT_RT */
9622 +# define unionfs_rw_semaphore rw_semaphore
9623 +#endif /* not CONFIG_PREEMPT_RT */
9625 +/* file private data. */
9626 +struct unionfs_file_info {
9629 + atomic_t generation;
9631 + struct unionfs_dir_state *rdstate;
9632 + struct file **lower_files;
9633 + int *saved_branch_ids; /* IDs of branches when file was opened */
9634 + struct vm_operations_struct *lower_vm_ops;
9635 + bool wrote_to_file; /* for delayed copyup */
9638 +/* unionfs inode data in memory */
9639 +struct unionfs_inode_info {
9642 + atomic_t generation;
9643 + /* Stuff for readdir over NFS. */
9644 + spinlock_t rdlock;
9645 + struct list_head readdircache;
9650 + /* The lower inodes */
9651 + struct inode **lower_inodes;
9653 + struct inode vfs_inode;
9656 +/* unionfs dentry data in memory */
9657 +struct unionfs_dentry_info {
9659 + * The semaphore is used to lock the dentry as soon as we get into a
9660 + * unionfs function from the VFS. Our lock ordering is that children
9661 + * go before their parents.
9663 + struct mutex lock;
9668 + atomic_t generation;
9669 + struct path *lower_paths;
9672 +/* These are the pointers to our various objects. */
9673 +struct unionfs_data {
9674 + struct super_block *sb; /* lower super_block */
9675 + atomic_t open_files; /* number of open files on branch */
9677 + int branch_id; /* unique branch ID at re/mount time */
9680 +/* unionfs super-block data in memory */
9681 +struct unionfs_sb_info {
9684 + atomic_t generation;
9687 + * This rwsem is used to make sure that a branch management
9689 + * 1) will not begin before all currently in-flight operations
9691 + * 2) any new operations do not execute until the currently
9692 + * running branch management operation completes.
9694 + * The write_lock_owner records the PID of the task which grabbed
9695 + * the rw_sem for writing. If the same task also tries to grab the
9696 + * read lock, we allow it. This prevents a self-deadlock when
9697 + * branch-management is used on a pivot_root'ed union, because we
9698 + * have to ->lookup paths which belong to the same union.
9700 + struct unionfs_rw_semaphore rwsem;
9701 + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9702 + int high_branch_id; /* last unique branch ID given */
9703 + char *dev_name; /* to identify different unions in pr_debug */
9704 + struct unionfs_data *data;
9708 + * structure for making the linked list of entries by readdir on left branch
9709 + * to compare with entries on right branch
9711 +struct filldir_node {
9712 + struct list_head file_list; /* list for directory entries */
9713 + char *name; /* name entry */
9714 + int hash; /* name hash */
9715 + int namelen; /* name len since name is not 0 terminated */
9718 + * we can check for duplicate whiteouts and files in the same branch
9719 + * in order to return -EIO.
9723 + /* is this a whiteout entry? */
9726 + /* Inline name, so we don't need to separately kmalloc small ones */
9727 + char iname[DNAME_INLINE_LEN_MIN];
9730 +/* Directory hash table. */
9731 +struct unionfs_dir_state {
9732 + unsigned int cookie; /* the cookie, based off of rdversion */
9733 + unsigned int offset; /* The entry we have returned. */
9735 + loff_t dirpos; /* offset within the lower level directory */
9736 + int size; /* How big is the hash table? */
9737 + int hashentries; /* How many entries have been inserted? */
9738 + unsigned long access;
9740 + /* This cache list is used when the inode keeps us around. */
9741 + struct list_head cache;
9742 + struct list_head list[0];
9745 +/* externs needed for fanout.h or sioq.h */
9746 +extern int unionfs_get_nlinks(const struct inode *inode);
9747 +extern void unionfs_copy_attr_times(struct inode *upper);
9748 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9750 +/* include miscellaneous macros */
9751 +#include "fanout.h"
9754 +/* externs for cache creation/deletion routines */
9755 +extern void unionfs_destroy_filldir_cache(void);
9756 +extern int unionfs_init_filldir_cache(void);
9757 +extern int unionfs_init_inode_cache(void);
9758 +extern void unionfs_destroy_inode_cache(void);
9759 +extern int unionfs_init_dentry_cache(void);
9760 +extern void unionfs_destroy_dentry_cache(void);
9762 +/* Initialize and free readdir-specific state. */
9763 +extern int init_rdstate(struct file *file);
9764 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9766 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9768 +extern void free_rdstate(struct unionfs_dir_state *state);
9769 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9770 + const char *name, int namelen, int bindex,
9772 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9773 + const char *name, int namelen,
9776 +extern struct dentry **alloc_new_dentries(int objs);
9777 +extern struct unionfs_data *alloc_new_data(int objs);
9779 +/* We can only use 32-bits of offset for rdstate --- blech! */
9780 +#define DIREOF (0xfffff)
9781 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9782 +#define MAXRDCOOKIE (0xfff)
9783 +/* Turn an rdstate into an offset. */
9784 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9788 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9789 + | (buf->offset & DIREOF);
9793 +/* Macros for locking a super_block. */
9794 +enum unionfs_super_lock_class {
9795 + UNIONFS_SMUTEX_NORMAL,
9796 + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9797 + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9799 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9801 + if (UNIONFS_SB(sb)->write_lock_owner &&
9802 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9804 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9806 +static inline void unionfs_read_unlock(struct super_block *sb)
9808 + if (UNIONFS_SB(sb)->write_lock_owner &&
9809 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9811 + up_read(&UNIONFS_SB(sb)->rwsem);
9813 +static inline void unionfs_write_lock(struct super_block *sb)
9815 + down_write(&UNIONFS_SB(sb)->rwsem);
9816 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9818 +static inline void unionfs_write_unlock(struct super_block *sb)
9820 + up_write(&UNIONFS_SB(sb)->rwsem);
9821 + UNIONFS_SB(sb)->write_lock_owner = 0;
9824 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9825 + struct dentry *d2)
9829 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9830 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9832 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9833 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9837 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9838 + struct dentry *d2)
9841 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9842 + unionfs_unlock_dentry(d1);
9843 + unionfs_unlock_dentry(d2);
9845 + unionfs_unlock_dentry(d2);
9846 + unionfs_unlock_dentry(d1);
9850 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9851 + struct dentry *p2)
9854 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9858 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9859 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9861 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9862 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9866 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9867 + struct dentry *p2)
9870 + unionfs_unlock_dentry(p1);
9873 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9874 + unionfs_unlock_dentry(p1);
9875 + unionfs_unlock_dentry(p2);
9877 + unionfs_unlock_dentry(p2);
9878 + unionfs_unlock_dentry(p1);
9882 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9883 +extern int realloc_dentry_private_data(struct dentry *dentry);
9884 +extern void free_dentry_private_data(struct dentry *dentry);
9885 +extern void update_bstart(struct dentry *dentry);
9886 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9887 +extern void release_lower_nd(struct nameidata *nd, int err);
9893 +/* replicates the directory structure up to given dentry in given branch */
9894 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9895 + const char *name, int bindex);
9897 +/* partial lookup */
9898 +extern int unionfs_partial_lookup(struct dentry *dentry,
9899 + struct dentry *parent);
9900 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9901 + struct dentry *parent,
9904 +/* copies a file from dbstart to newbindex branch */
9905 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9906 + int newbindex, loff_t size);
9907 +extern int copyup_named_file(struct inode *dir, struct file *file,
9908 + char *name, int bstart, int new_bindex,
9910 +/* copies a dentry from dbstart to newbindex branch */
9911 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9912 + int bstart, int new_bindex, const char *name,
9913 + int namelen, struct file **copyup_file, loff_t len);
9914 +/* helper functions for post-copyup actions */
9915 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9916 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9918 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9919 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9920 + struct unionfs_dir_state **namelist);
9921 +/* whiteout and opaque directory helpers */
9922 +extern char *alloc_whname(const char *name, int len);
9923 +extern bool is_whiteout_name(char **namep, int *namelenp);
9924 +extern bool is_validname(const char *name);
9925 +extern struct dentry *lookup_whiteout(const char *name,
9926 + struct dentry *lower_parent);
9927 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9928 +extern int unlink_whiteout(struct dentry *wh_dentry);
9929 +extern int check_unlink_whiteout(struct dentry *dentry,
9930 + struct dentry *lower_dentry, int bindex);
9931 +extern int create_whiteout(struct dentry *dentry, int start);
9932 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9933 + struct unionfs_dir_state *namelist);
9934 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9935 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9936 +extern void unionfs_set_max_namelen(long *namelen);
9938 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9939 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9941 +/* Locking functions. */
9942 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9943 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9945 +/* Common file operations. */
9946 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9948 +extern int unionfs_open(struct inode *inode, struct file *file);
9949 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9950 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9951 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9952 + unsigned long arg);
9953 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
9955 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9957 +/* Inode operations */
9958 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9959 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9960 + struct inode *new_dir, struct dentry *new_dentry);
9961 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
9962 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
9964 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
9965 + struct dentry *parent, bool willwrite);
9966 +extern bool is_negative_lower(const struct dentry *dentry);
9967 +extern bool is_newer_lower(const struct dentry *dentry);
9968 +extern void purge_sb_data(struct super_block *sb);
9970 +/* The values for unionfs_interpose's flag. */
9971 +#define INTERPOSE_DEFAULT 0
9972 +#define INTERPOSE_LOOKUP 1
9973 +#define INTERPOSE_REVAL 2
9974 +#define INTERPOSE_REVAL_NEG 3
9975 +#define INTERPOSE_PARTIAL 4
9977 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9978 + struct super_block *sb, int flag);
9980 +#ifdef CONFIG_UNION_FS_XATTR
9981 +/* Extended attribute functions. */
9982 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
9983 +static inline void unionfs_xattr_kfree(const void *p)
9987 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
9988 + void *value, size_t size);
9989 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
9990 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
9992 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
9993 + const void *value, size_t size, int flags);
9994 +#endif /* CONFIG_UNION_FS_XATTR */
9996 +/* The root directory is unhashed, but isn't deleted. */
9997 +static inline int d_deleted(struct dentry *d)
9999 + return d_unhashed(d) && (d != d->d_sb->s_root);
10002 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10003 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10005 +/* unionfs_open, check if we need to copyup the file */
10006 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10007 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10009 +static inline int branchperms(const struct super_block *sb, int index)
10011 + BUG_ON(index < 0);
10012 + return UNIONFS_SB(sb)->data[index].branchperms;
10015 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10017 + BUG_ON(index < 0);
10018 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10022 +/* Is this file on a read-only branch? */
10023 +static inline int is_robranch_super(const struct super_block *sb, int index)
10027 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10031 +/* Is this file on a read-only branch? */
10032 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10034 + struct super_block *lower_sb;
10036 + BUG_ON(index < 0);
10038 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10041 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10042 + BUG_ON(lower_sb == NULL);
10044 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10045 + * lower_dentry could be a negative.
10047 + if (lower_sb->s_flags & MS_RDONLY)
10053 +static inline int is_robranch(const struct dentry *dentry)
10057 + index = UNIONFS_D(dentry)->bstart;
10058 + BUG_ON(index < 0);
10060 + return is_robranch_idx(dentry, index);
10066 +extern int check_branch(struct nameidata *nd);
10067 +extern int parse_branch_mode(const char *name, int *perms);
10069 +/* locking helpers */
10070 +static inline struct dentry *lock_parent(struct dentry *dentry)
10072 + struct dentry *dir = dget_parent(dentry);
10073 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
10076 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10078 + struct dentry *dir = dget_parent(dentry);
10080 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10084 +static inline void unlock_dir(struct dentry *dir)
10086 + mutex_unlock(&dir->d_inode->i_mutex);
10090 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10093 + struct vfsmount *mnt;
10095 + BUG_ON(!dentry || bindex < 0);
10097 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10098 +#ifdef CONFIG_UNION_FS_DEBUG
10100 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10102 +#endif /* CONFIG_UNION_FS_DEBUG */
10107 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10109 + struct vfsmount *mnt;
10111 + if (!dentry && bindex < 0)
10113 + BUG_ON(!dentry || bindex < 0);
10115 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10116 +#ifdef CONFIG_UNION_FS_DEBUG
10118 + * Directories can have NULL lower objects in between start/end, but
10119 + * NOT if at the start/end range. We cannot verify that this dentry
10120 + * is a type=DIR, because it may already be a negative dentry. But
10121 + * if dbstart is greater than dbend, we know that this couldn't have
10122 + * been a regular file: it had to have been a directory.
10124 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10125 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10126 +#endif /* CONFIG_UNION_FS_DEBUG */
10130 +#ifdef CONFIG_UNION_FS_DEBUG
10132 +/* useful for tracking code reachability */
10133 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10135 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10136 + __FILE__, __func__, __LINE__)
10137 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10138 + __FILE__, __func__, __LINE__)
10139 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10140 + __FILE__, __func__, __LINE__)
10141 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10142 + __FILE__, __func__, __LINE__)
10143 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10144 + __FILE__, __func__, __LINE__)
10145 +#define show_inode_times(i) __show_inode_times((i), \
10146 + __FILE__, __func__, __LINE__)
10147 +#define show_dinode_times(d) __show_dinode_times((d), \
10148 + __FILE__, __func__, __LINE__)
10149 +#define show_inode_counts(i) __show_inode_counts((i), \
10150 + __FILE__, __func__, __LINE__)
10152 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10153 + const char *fxn, int line);
10154 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10155 + const char *fname, const char *fxn,
10157 +extern void __unionfs_check_file(const struct file *file,
10158 + const char *fname, const char *fxn, int line);
10159 +extern void __unionfs_check_nd(const struct nameidata *nd,
10160 + const char *fname, const char *fxn, int line);
10161 +extern void __show_branch_counts(const struct super_block *sb,
10162 + const char *file, const char *fxn, int line);
10163 +extern void __show_inode_times(const struct inode *inode,
10164 + const char *file, const char *fxn, int line);
10165 +extern void __show_dinode_times(const struct dentry *dentry,
10166 + const char *file, const char *fxn, int line);
10167 +extern void __show_inode_counts(const struct inode *inode,
10168 + const char *file, const char *fxn, int line);
10170 +#else /* not CONFIG_UNION_FS_DEBUG */
10172 +/* we leave useful hooks for these check functions throughout the code */
10173 +#define unionfs_check_inode(i) do { } while (0)
10174 +#define unionfs_check_dentry(d) do { } while (0)
10175 +#define unionfs_check_file(f) do { } while (0)
10176 +#define unionfs_check_nd(n) do { } while (0)
10177 +#define show_branch_counts(sb) do { } while (0)
10178 +#define show_inode_times(i) do { } while (0)
10179 +#define show_dinode_times(d) do { } while (0)
10180 +#define show_inode_counts(i) do { } while (0)
10182 +#endif /* not CONFIG_UNION_FS_DEBUG */
10184 +#endif /* not _UNION_H_ */
10185 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10186 new file mode 100644
10187 index 0000000..b6d8e10
10189 +++ b/fs/unionfs/unlink.c
10192 + * Copyright (c) 2003-2009 Erez Zadok
10193 + * Copyright (c) 2003-2006 Charles P. Wright
10194 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10195 + * Copyright (c) 2005-2006 Junjiro Okajima
10196 + * Copyright (c) 2005 Arun M. Krishnakumar
10197 + * Copyright (c) 2004-2006 David P. Quigley
10198 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10199 + * Copyright (c) 2003 Puja Gupta
10200 + * Copyright (c) 2003 Harikesavan Krishnan
10201 + * Copyright (c) 2003-2009 Stony Brook University
10202 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10204 + * This program is free software; you can redistribute it and/or modify
10205 + * it under the terms of the GNU General Public License version 2 as
10206 + * published by the Free Software Foundation.
10209 +#include "union.h"
10212 + * Helper function for Unionfs's unlink operation.
10214 + * The main goal of this function is to optimize the unlinking of non-dir
10215 + * objects in unionfs by deleting all possible lower inode objects from the
10216 + * underlying branches having same dentry name as the non-dir dentry on
10217 + * which this unlink operation is called. This way we delete as many lower
10218 + * inodes as possible, and save space. Whiteouts need to be created in
10219 + * branch0 only if unlinking fails on any of the lower branch other than
10220 + * branch0, or if a lower branch is marked read-only.
10222 + * Also, while unlinking a file, if we encounter any dir type entry in any
10223 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10224 + * The following special cases are also handled:
10226 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10227 + * appropriate error.
10229 + * (2) If we get an error during unlink in any of other lower branch other
10230 + * than branch0, then we create a whiteout in branch0.
10232 + * (3) If a whiteout already exists in any intermediate branch, we delete
10233 + * all possible inodes only up to that branch (this is an "opaqueness"
10234 + * as as per Documentation/filesystems/unionfs/concepts.txt).
10237 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10238 + struct dentry *parent)
10240 + struct dentry *lower_dentry;
10241 + struct dentry *lower_dir_dentry;
10245 + err = unionfs_partial_lookup(dentry, parent);
10249 + /* trying to unlink all possible valid instances */
10250 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10251 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10252 + if (!lower_dentry || !lower_dentry->d_inode)
10255 + lower_dir_dentry = lock_parent(lower_dentry);
10257 + /* avoid destroying the lower inode if the object is in use */
10258 + dget(lower_dentry);
10259 + err = is_robranch_super(dentry->d_sb, bindex);
10261 + /* see Documentation/filesystems/unionfs/issues.txt */
10263 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10264 + err = vfs_unlink(lower_dir_dentry->d_inode,
10267 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10272 + /* if lower object deletion succeeds, update inode's times */
10274 + unionfs_copy_attr_times(dentry->d_inode);
10275 + dput(lower_dentry);
10276 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10277 + unlock_dir(lower_dir_dentry);
10284 + * Create the whiteout in branch 0 (highest priority) only if (a)
10285 + * there was an error in any intermediate branch other than branch 0
10286 + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10287 + * mounted read-only.
10290 + if ((bindex == 0) ||
10291 + ((bindex == dbstart(dentry)) &&
10292 + (!IS_COPYUP_ERR(err))))
10295 + if (!IS_COPYUP_ERR(err))
10296 + pr_debug("unionfs: lower object deletion "
10297 + "failed in branch:%d\n", bindex);
10298 + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10304 + inode_dec_link_count(dentry->d_inode);
10306 + /* We don't want to leave negative leftover dentries for revalidate. */
10307 + if (!err && (dbopaque(dentry) != -1))
10308 + update_bstart(dentry);
10313 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10316 + struct inode *inode = dentry->d_inode;
10317 + struct dentry *parent;
10320 + BUG_ON(S_ISDIR(inode->i_mode));
10321 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10322 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10323 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10325 + valid = __unionfs_d_revalidate(dentry, parent, false);
10326 + if (unlikely(!valid)) {
10330 + unionfs_check_dentry(dentry);
10332 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10333 + /* call d_drop so the system "forgets" about us */
10335 + unionfs_postcopyup_release(dentry);
10336 + unionfs_postcopyup_setmnt(parent);
10337 + if (inode->i_nlink == 0) /* drop lower inodes */
10338 + iput_lowers_all(inode, false);
10341 + * if unlink/whiteout succeeded, parent dir mtime has
10344 + unionfs_copy_attr_times(dir);
10349 + unionfs_check_dentry(dentry);
10350 + unionfs_check_inode(dir);
10352 + unionfs_unlock_dentry(dentry);
10353 + unionfs_unlock_parent(dentry, parent);
10354 + unionfs_read_unlock(dentry->d_sb);
10358 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10359 + struct unionfs_dir_state *namelist)
10362 + struct dentry *lower_dentry;
10363 + struct dentry *lower_dir_dentry = NULL;
10365 + /* Here we need to remove whiteout entries. */
10366 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10370 + lower_dentry = unionfs_lower_dentry(dentry);
10372 + lower_dir_dentry = lock_parent(lower_dentry);
10374 + /* avoid destroying the lower inode if the file is in use */
10375 + dget(lower_dentry);
10376 + err = is_robranch(dentry);
10378 + /* see Documentation/filesystems/unionfs/issues.txt */
10380 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10383 + dput(lower_dentry);
10385 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10386 + /* propagate number of hard-links */
10387 + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10390 + if (lower_dir_dentry)
10391 + unlock_dir(lower_dir_dentry);
10395 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10398 + struct unionfs_dir_state *namelist = NULL;
10399 + struct dentry *parent;
10400 + int dstart, dend;
10403 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10404 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10405 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10407 + valid = __unionfs_d_revalidate(dentry, parent, false);
10408 + if (unlikely(!valid)) {
10412 + unionfs_check_dentry(dentry);
10414 + /* check if this unionfs directory is empty or not */
10415 + err = check_empty(dentry, parent, &namelist);
10419 + err = unionfs_rmdir_first(dir, dentry, namelist);
10420 + dstart = dbstart(dentry);
10421 + dend = dbend(dentry);
10423 + * We create a whiteout for the directory if there was an error to
10424 + * rmdir the first directory entry in the union. Otherwise, we
10425 + * create a whiteout only if there is no chance that a lower
10426 + * priority branch might also have the same named directory. IOW,
10427 + * if there is not another same-named directory at a lower priority
10428 + * branch, then we don't need to create a whiteout for it.
10431 + if (dstart < dend)
10432 + err = create_whiteout(dentry, dstart);
10439 + /* exit if the error returned was NOT -EROFS */
10440 + if (!IS_COPYUP_ERR(err))
10443 + new_err = create_whiteout(dentry, dstart - 1);
10444 + if (new_err != -EEXIST)
10450 + * Drop references to lower dentry/inode so storage space for them
10451 + * can be reclaimed. Then, call d_drop so the system "forgets"
10455 + iput_lowers_all(dentry->d_inode, false);
10456 + dput(unionfs_lower_dentry_idx(dentry, dstart));
10457 + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10459 + /* update our lower vfsmnts, in case a copyup took place */
10460 + unionfs_postcopyup_setmnt(dentry);
10461 + unionfs_check_dentry(dentry);
10462 + unionfs_check_inode(dir);
10466 + free_rdstate(namelist);
10468 + unionfs_unlock_dentry(dentry);
10469 + unionfs_unlock_parent(dentry, parent);
10470 + unionfs_read_unlock(dentry->d_sb);
10473 diff --git a/fs/unionfs/whiteout.c b/fs/unionfs/whiteout.c
10474 new file mode 100644
10475 index 0000000..a55684d
10477 +++ b/fs/unionfs/whiteout.c
10480 + * Copyright (c) 2003-2009 Erez Zadok
10481 + * Copyright (c) 2003-2006 Charles P. Wright
10482 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10483 + * Copyright (c) 2005-2006 Junjiro Okajima
10484 + * Copyright (c) 2005 Arun M. Krishnakumar
10485 + * Copyright (c) 2004-2006 David P. Quigley
10486 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10487 + * Copyright (c) 2003 Puja Gupta
10488 + * Copyright (c) 2003 Harikesavan Krishnan
10489 + * Copyright (c) 2003-2009 Stony Brook University
10490 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10492 + * This program is free software; you can redistribute it and/or modify
10493 + * it under the terms of the GNU General Public License version 2 as
10494 + * published by the Free Software Foundation.
10497 +#include "union.h"
10500 + * whiteout and opaque directory helpers
10503 +/* What do we use for whiteouts. */
10504 +#define UNIONFS_WHPFX ".wh."
10505 +#define UNIONFS_WHLEN 4
10507 + * If a directory contains this file, then it is opaque. We start with the
10508 + * .wh. flag so that it is blocked by lookup.
10510 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10511 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10513 +/* construct whiteout filename */
10514 +char *alloc_whname(const char *name, int len)
10518 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10519 + if (unlikely(!buf))
10520 + return ERR_PTR(-ENOMEM);
10522 + strcpy(buf, UNIONFS_WHPFX);
10523 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10529 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10530 + * code in one place.
10532 +void unionfs_set_max_namelen(long *namelen)
10534 + *namelen -= UNIONFS_WHLEN;
10537 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10538 +bool is_whiteout_name(char **namep, int *namelenp)
10540 + if (*namelenp > UNIONFS_WHLEN &&
10541 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10542 + *namep += UNIONFS_WHLEN;
10543 + *namelenp -= UNIONFS_WHLEN;
10549 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10550 +bool is_validname(const char *name)
10552 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10554 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10555 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10561 + * Look for a whiteout @name in @lower_parent directory. If error, return
10562 + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10564 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10565 + * free then re-malloc calls. Need to provide a different API for those
10568 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10570 + char *whname = NULL;
10571 + int err = 0, namelen;
10572 + struct dentry *wh_dentry = NULL;
10574 + namelen = strlen(name);
10575 + whname = alloc_whname(name, namelen);
10576 + if (unlikely(IS_ERR(whname))) {
10577 + err = PTR_ERR(whname);
10581 + /* check if whiteout exists in this branch: lookup .wh.foo */
10582 + wh_dentry = lookup_one_len(whname, lower_parent, strlen(whname));
10583 + if (IS_ERR(wh_dentry)) {
10584 + err = PTR_ERR(wh_dentry);
10588 + /* check if negative dentry (ENOENT) */
10589 + if (!wh_dentry->d_inode)
10592 + /* whiteout found: check if valid type */
10593 + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10594 + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10595 + whname, wh_dentry->d_inode->i_mode);
10604 + wh_dentry = ERR_PTR(err);
10605 + return wh_dentry;
10608 +/* find and return first whiteout in parent directory, else ENOENT */
10609 +struct dentry *find_first_whiteout(struct dentry *dentry)
10611 + int bindex, bstart, bend;
10612 + struct dentry *parent, *lower_parent, *wh_dentry;
10614 + parent = dget_parent(dentry);
10616 + bstart = dbstart(parent);
10617 + bend = dbend(parent);
10618 + wh_dentry = ERR_PTR(-ENOENT);
10620 + for (bindex = bstart; bindex <= bend; bindex++) {
10621 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10622 + if (!lower_parent)
10624 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10625 + if (IS_ERR(wh_dentry))
10627 + if (wh_dentry->d_inode)
10630 + wh_dentry = ERR_PTR(-ENOENT);
10635 + return wh_dentry;
10639 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10640 + * release dentry reference.
10642 +int unlink_whiteout(struct dentry *wh_dentry)
10645 + struct dentry *lower_dir_dentry;
10647 + /* dget and lock parent dentry */
10648 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10650 + /* see Documentation/filesystems/unionfs/issues.txt */
10652 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10654 + unlock_dir(lower_dir_dentry);
10657 + * Whiteouts are special files and should be deleted no matter what
10658 + * (as if they never existed), in order to allow this create
10659 + * operation to succeed. This is especially important in sticky
10660 + * directories: a whiteout may have been created by one user, but
10661 + * the newly created file may be created by another user.
10662 + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10663 + * above failed, then we have to try to directly unlink the
10664 + * whiteout. Note: in the ODF version of unionfs, whiteout are
10665 + * handled much more cleanly.
10667 + if (err == -EPERM) {
10668 + struct inode *inode = lower_dir_dentry->d_inode;
10669 + err = inode->i_op->unlink(inode, wh_dentry);
10672 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10673 + "err = %d\n", wh_dentry->d_name.name, err);
10680 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10681 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10682 + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10683 + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10684 + * If it finds both a regular file and a whiteout, return -EIO (this should
10687 + * Return 0 if no whiteout was found. Return 1 if one was found and
10688 + * successfully removed. Therefore a value >= 0 tells the caller that
10689 + * @lower_dentry belongs to a good branch to create the new object in).
10690 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10691 + * unlink the whiteout.
10693 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10697 + struct dentry *wh_dentry = NULL;
10698 + struct dentry *lower_dir_dentry = NULL;
10700 + /* look for whiteout dentry first */
10701 + lower_dir_dentry = dget_parent(lower_dentry);
10702 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10703 + dput(lower_dir_dentry);
10704 + if (IS_ERR(wh_dentry)) {
10705 + err = PTR_ERR(wh_dentry);
10709 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10714 + /* check if regular file and whiteout were both found */
10715 + if (unlikely(lower_dentry->d_inode)) {
10717 + printk(KERN_ERR "unionfs: found both whiteout and regular "
10718 + "file in directory %s (branch %d)\n",
10719 + lower_dir_dentry->d_name.name, bindex);
10723 + /* check if branch is writeable */
10724 + err = is_robranch_super(dentry->d_sb, bindex);
10728 + /* .wh.foo has been found, so let's unlink it */
10729 + err = unlink_whiteout(wh_dentry);
10731 + err = 1; /* a whiteout was found and successfully removed */
10739 + * Pass an unionfs dentry and an index. It will try to create a whiteout
10740 + * for the filename in dentry, and will try in branch 'index'. On error,
10741 + * it will proceed to a branch to the left.
10743 +int create_whiteout(struct dentry *dentry, int start)
10745 + int bstart, bend, bindex;
10746 + struct dentry *lower_dir_dentry;
10747 + struct dentry *lower_dentry;
10748 + struct dentry *lower_wh_dentry;
10749 + struct nameidata nd;
10750 + char *name = NULL;
10751 + int err = -EINVAL;
10753 + verify_locked(dentry);
10755 + bstart = dbstart(dentry);
10756 + bend = dbend(dentry);
10758 + /* create dentry's whiteout equivalent */
10759 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10760 + if (unlikely(IS_ERR(name))) {
10761 + err = PTR_ERR(name);
10765 + for (bindex = start; bindex >= 0; bindex--) {
10766 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10768 + if (!lower_dentry) {
10770 + * if lower dentry is not present, create the
10771 + * entire lower dentry directory structure and go
10772 + * ahead. Since we want to just create whiteout, we
10773 + * only want the parent dentry, and hence get rid of
10776 + lower_dentry = create_parents(dentry->d_inode,
10778 + dentry->d_name.name,
10780 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10781 + int ret = PTR_ERR(lower_dentry);
10782 + if (!IS_COPYUP_ERR(ret))
10784 + "unionfs: create_parents for "
10785 + "whiteout failed: bindex=%d "
10786 + "err=%d\n", bindex, ret);
10791 + lower_wh_dentry =
10792 + lookup_one_len(name, lower_dentry->d_parent,
10793 + dentry->d_name.len + UNIONFS_WHLEN);
10794 + if (IS_ERR(lower_wh_dentry))
10798 + * The whiteout already exists. This used to be impossible,
10799 + * but now is possible because of opaqueness.
10801 + if (lower_wh_dentry->d_inode) {
10802 + dput(lower_wh_dentry);
10807 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10808 + if (unlikely(err < 0))
10810 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10811 + err = is_robranch_super(dentry->d_sb, bindex);
10813 + err = vfs_create(lower_dir_dentry->d_inode,
10815 + ~current_umask() & S_IRUGO,
10817 + unlock_dir(lower_dir_dentry);
10818 + dput(lower_wh_dentry);
10819 + release_lower_nd(&nd, err);
10821 + if (!err || !IS_COPYUP_ERR(err))
10825 + /* set dbopaque so that lookup will not proceed after this branch */
10827 + dbopaque(dentry) = bindex;
10835 + * Delete all of the whiteouts in a given directory for rmdir.
10837 + * lower directory inode should be locked
10839 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10840 + struct unionfs_dir_state *namelist)
10843 + struct dentry *lower_dir_dentry = NULL;
10844 + struct dentry *lower_dentry;
10845 + char *name = NULL, *p;
10846 + struct inode *lower_dir;
10848 + struct list_head *pos;
10849 + struct filldir_node *cursor;
10851 + /* Find out lower parent dentry */
10852 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10853 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10854 + lower_dir = lower_dir_dentry->d_inode;
10855 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10858 + name = __getname();
10859 + if (unlikely(!name))
10861 + strcpy(name, UNIONFS_WHPFX);
10862 + p = name + UNIONFS_WHLEN;
10865 + for (i = 0; !err && i < namelist->size; i++) {
10866 + list_for_each(pos, &namelist->list[i]) {
10868 + list_entry(pos, struct filldir_node,
10870 + /* Only operate on whiteouts in this branch. */
10871 + if (cursor->bindex != bindex)
10873 + if (!cursor->whiteout)
10876 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10878 + lookup_one_len(name, lower_dir_dentry,
10879 + cursor->namelen +
10881 + if (IS_ERR(lower_dentry)) {
10882 + err = PTR_ERR(lower_dentry);
10885 + if (lower_dentry->d_inode)
10886 + err = vfs_unlink(lower_dir, lower_dentry);
10887 + dput(lower_dentry);
10895 + /* After all of the removals, we should copy the attributes once. */
10896 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10903 +void __delete_whiteouts(struct work_struct *work)
10905 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10906 + struct deletewh_args *d = &args->deletewh;
10908 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10909 + complete(&args->comp);
10912 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10913 +int delete_whiteouts(struct dentry *dentry, int bindex,
10914 + struct unionfs_dir_state *namelist)
10917 + struct super_block *sb;
10918 + struct dentry *lower_dir_dentry;
10919 + struct inode *lower_dir;
10920 + struct sioq_args args;
10922 + sb = dentry->d_sb;
10924 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10925 + BUG_ON(bindex < dbstart(dentry));
10926 + BUG_ON(bindex > dbend(dentry));
10927 + err = is_robranch_super(sb, bindex);
10931 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10932 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10933 + lower_dir = lower_dir_dentry->d_inode;
10934 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10936 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10937 + err = do_delete_whiteouts(dentry, bindex, namelist);
10939 + args.deletewh.namelist = namelist;
10940 + args.deletewh.dentry = dentry;
10941 + args.deletewh.bindex = bindex;
10942 + run_sioq(__delete_whiteouts, &args);
10950 +/****************************************************************************
10951 + * Opaque directory helpers *
10952 + ****************************************************************************/
10955 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
10956 + * -errno if an error occurred trying to figure this out.
10958 +int is_opaque_dir(struct dentry *dentry, int bindex)
10961 + struct dentry *lower_dentry;
10962 + struct dentry *wh_lower_dentry;
10963 + struct inode *lower_inode;
10964 + struct sioq_args args;
10966 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10967 + lower_inode = lower_dentry->d_inode;
10969 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
10971 + mutex_lock(&lower_inode->i_mutex);
10973 + if (!inode_permission(lower_inode, MAY_EXEC)) {
10974 + wh_lower_dentry =
10975 + lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
10976 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
10978 + args.is_opaque.dentry = lower_dentry;
10979 + run_sioq(__is_opaque_dir, &args);
10980 + wh_lower_dentry = args.ret;
10983 + mutex_unlock(&lower_inode->i_mutex);
10985 + if (IS_ERR(wh_lower_dentry)) {
10986 + err = PTR_ERR(wh_lower_dentry);
10990 + /* This is an opaque dir iff wh_lower_dentry is positive */
10991 + err = !!wh_lower_dentry->d_inode;
10993 + dput(wh_lower_dentry);
10998 +void __is_opaque_dir(struct work_struct *work)
11000 + struct sioq_args *args = container_of(work, struct sioq_args, work);
11002 + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
11003 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11004 + complete(&args->comp);
11007 +int make_dir_opaque(struct dentry *dentry, int bindex)
11010 + struct dentry *lower_dentry, *diropq;
11011 + struct inode *lower_dir;
11012 + struct nameidata nd;
11013 + const struct cred *old_creds;
11014 + struct cred *new_creds;
11017 + * Opaque directory whiteout markers are special files (like regular
11018 + * whiteouts), and should appear to the users as if they don't
11019 + * exist. They should be created/deleted regardless of directory
11020 + * search/create permissions, but only for the duration of this
11021 + * creation of the .wh.__dir_opaque: file. Note, this does not
11022 + * circumvent normal ->permission).
11024 + new_creds = prepare_creds();
11025 + if (unlikely(!new_creds)) {
11029 + cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
11030 + cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
11031 + old_creds = override_creds(new_creds);
11033 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11034 + lower_dir = lower_dentry->d_inode;
11035 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
11036 + !S_ISDIR(lower_dir->i_mode));
11038 + mutex_lock(&lower_dir->i_mutex);
11039 + diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
11040 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11041 + if (IS_ERR(diropq)) {
11042 + err = PTR_ERR(diropq);
11046 + err = init_lower_nd(&nd, LOOKUP_CREATE);
11047 + if (unlikely(err < 0))
11049 + if (!diropq->d_inode)
11050 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
11052 + dbopaque(dentry) = bindex;
11053 + release_lower_nd(&nd, err);
11058 + mutex_unlock(&lower_dir->i_mutex);
11059 + revert_creds(old_creds);
11063 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
11064 new file mode 100644
11065 index 0000000..af72cca
11067 +++ b/fs/unionfs/xattr.c
11070 + * Copyright (c) 2003-2009 Erez Zadok
11071 + * Copyright (c) 2003-2006 Charles P. Wright
11072 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11073 + * Copyright (c) 2005-2006 Junjiro Okajima
11074 + * Copyright (c) 2005 Arun M. Krishnakumar
11075 + * Copyright (c) 2004-2006 David P. Quigley
11076 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
11077 + * Copyright (c) 2003 Puja Gupta
11078 + * Copyright (c) 2003 Harikesavan Krishnan
11079 + * Copyright (c) 2003-2009 Stony Brook University
11080 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11082 + * This program is free software; you can redistribute it and/or modify
11083 + * it under the terms of the GNU General Public License version 2 as
11084 + * published by the Free Software Foundation.
11087 +#include "union.h"
11089 +/* This is lifted from fs/xattr.c */
11090 +void *unionfs_xattr_alloc(size_t size, size_t limit)
11094 + if (size > limit)
11095 + return ERR_PTR(-E2BIG);
11097 + if (!size) /* size request, no buffer is needed */
11100 + ptr = kmalloc(size, GFP_KERNEL);
11101 + if (unlikely(!ptr))
11102 + return ERR_PTR(-ENOMEM);
11107 + * BKL held by caller.
11108 + * dentry->d_inode->i_mutex locked
11110 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11113 + struct dentry *lower_dentry = NULL;
11114 + struct dentry *parent;
11115 + int err = -EOPNOTSUPP;
11118 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11119 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11120 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11122 + valid = __unionfs_d_revalidate(dentry, parent, false);
11123 + if (unlikely(!valid)) {
11128 + lower_dentry = unionfs_lower_dentry(dentry);
11130 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11133 + unionfs_check_dentry(dentry);
11134 + unionfs_unlock_dentry(dentry);
11135 + unionfs_unlock_parent(dentry, parent);
11136 + unionfs_read_unlock(dentry->d_sb);
11141 + * BKL held by caller.
11142 + * dentry->d_inode->i_mutex locked
11144 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11145 + const void *value, size_t size, int flags)
11147 + struct dentry *lower_dentry = NULL;
11148 + struct dentry *parent;
11149 + int err = -EOPNOTSUPP;
11152 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11153 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11154 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11156 + valid = __unionfs_d_revalidate(dentry, parent, false);
11157 + if (unlikely(!valid)) {
11162 + lower_dentry = unionfs_lower_dentry(dentry);
11164 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11168 + unionfs_check_dentry(dentry);
11169 + unionfs_unlock_dentry(dentry);
11170 + unionfs_unlock_parent(dentry, parent);
11171 + unionfs_read_unlock(dentry->d_sb);
11176 + * BKL held by caller.
11177 + * dentry->d_inode->i_mutex locked
11179 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11181 + struct dentry *lower_dentry = NULL;
11182 + struct dentry *parent;
11183 + int err = -EOPNOTSUPP;
11186 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11187 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11188 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11190 + valid = __unionfs_d_revalidate(dentry, parent, false);
11191 + if (unlikely(!valid)) {
11196 + lower_dentry = unionfs_lower_dentry(dentry);
11198 + err = vfs_removexattr(lower_dentry, (char *) name);
11201 + unionfs_check_dentry(dentry);
11202 + unionfs_unlock_dentry(dentry);
11203 + unionfs_unlock_parent(dentry, parent);
11204 + unionfs_read_unlock(dentry->d_sb);
11209 + * BKL held by caller.
11210 + * dentry->d_inode->i_mutex locked
11212 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11214 + struct dentry *lower_dentry = NULL;
11215 + struct dentry *parent;
11216 + int err = -EOPNOTSUPP;
11217 + char *encoded_list = NULL;
11220 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11221 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11222 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11224 + valid = __unionfs_d_revalidate(dentry, parent, false);
11225 + if (unlikely(!valid)) {
11230 + lower_dentry = unionfs_lower_dentry(dentry);
11232 + encoded_list = list;
11233 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11236 + unionfs_check_dentry(dentry);
11237 + unionfs_unlock_dentry(dentry);
11238 + unionfs_unlock_parent(dentry, parent);
11239 + unionfs_read_unlock(dentry->d_sb);
11242 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
11243 index bb516ce..64f1ced 100644
11244 --- a/include/linux/fs_stack.h
11245 +++ b/include/linux/fs_stack.h
11248 + * Copyright (c) 2006-2009 Erez Zadok
11249 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11250 + * Copyright (c) 2006-2009 Stony Brook University
11251 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11253 + * This program is free software; you can redistribute it and/or modify
11254 + * it under the terms of the GNU General Public License version 2 as
11255 + * published by the Free Software Foundation.
11258 #ifndef _LINUX_FS_STACK_H
11259 #define _LINUX_FS_STACK_H
11261 -/* This file defines generic functions used primarily by stackable
11263 + * This file defines generic functions used primarily by stackable
11264 * filesystems; none of these functions require i_mutex to be held.
11267 #include <linux/fs.h>
11269 /* externs for fs/stack.c */
11270 -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
11271 - int (*get_nlinks)(struct inode *));
11273 -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
11274 +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
11275 +extern void fsstack_copy_inode_size(struct inode *dst, struct inode *src);
11278 static inline void fsstack_copy_attr_atime(struct inode *dest,
11279 diff --git a/include/linux/magic.h b/include/linux/magic.h
11280 index 0b4df7e..2170b8a 100644
11281 --- a/include/linux/magic.h
11282 +++ b/include/linux/magic.h
11284 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11285 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11287 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11289 #define SMB_SUPER_MAGIC 0x517B
11290 #define USBDEVICE_SUPER_MAGIC 0x9fa2
11291 #define CGROUP_SUPER_MAGIC 0x27e0eb
11292 diff --git a/include/linux/splice.h b/include/linux/splice.h
11293 index 528dcb9..4b5727c 100644
11294 --- a/include/linux/splice.h
11295 +++ b/include/linux/splice.h
11296 @@ -70,5 +70,10 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
11297 struct splice_pipe_desc *);
11298 extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11299 splice_direct_actor *);
11300 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11301 + loff_t *ppos, size_t len, unsigned int flags);
11302 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11303 + struct pipe_inode_info *pipe, size_t len,
11304 + unsigned int flags);
11307 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
11308 new file mode 100644
11309 index 0000000..c84d97e
11311 +++ b/include/linux/union_fs.h
11314 + * Copyright (c) 2003-2009 Erez Zadok
11315 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11316 + * Copyright (c) 2003-2009 Stony Brook University
11317 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11319 + * This program is free software; you can redistribute it and/or modify
11320 + * it under the terms of the GNU General Public License version 2 as
11321 + * published by the Free Software Foundation.
11324 +#ifndef _LINUX_UNION_FS_H
11325 +#define _LINUX_UNION_FS_H
11328 + * DEFINITIONS FOR USER AND KERNEL CODE:
11330 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11331 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11333 +#endif /* _LINUX_UNIONFS_H */
11335 diff --git a/security/security.c b/security/security.c
11336 index c3586c0..17c2cdf 100644
11337 --- a/security/security.c
11338 +++ b/security/security.c
11339 @@ -520,6 +520,7 @@ int security_inode_permission(struct inode *inode, int mask)
11341 return security_ops->inode_permission(inode, mask);
11343 +EXPORT_SYMBOL(security_inode_permission);
11345 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)