1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index 8c624a1..4aa288b 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -110,6 +110,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 f0358cd..7ae0669 100644
538 @@ -6375,6 +6375,14 @@ F: Documentation/cdrom/
539 F: drivers/cdrom/cdrom.c
540 F: include/linux/cdrom.h
544 +M: ezk@cs.sunysb.edu
545 +L: unionfs@filesystems.org
546 +W: http://unionfs.filesystems.org/
547 +T: git git.kernel.org/pub/scm/linux/kernel/git/ezk/unionfs.git
550 UNSORTED BLOCK IMAGES (UBI)
551 M: Artem Bityutskiy <dedekind1@gmail.com>
552 W: http://www.linux-mtd.infradead.org/
553 diff --git a/fs/Kconfig b/fs/Kconfig
554 index 19891aa..ac8a074 100644
557 @@ -187,6 +187,7 @@ if MISC_FILESYSTEMS
558 source "fs/adfs/Kconfig"
559 source "fs/affs/Kconfig"
560 source "fs/ecryptfs/Kconfig"
561 +source "fs/unionfs/Kconfig"
562 source "fs/hfs/Kconfig"
563 source "fs/hfsplus/Kconfig"
564 source "fs/befs/Kconfig"
565 diff --git a/fs/Makefile b/fs/Makefile
566 index fb68c2b..8ca9290 100644
569 @@ -83,6 +83,7 @@ obj-$(CONFIG_ISO9660_FS) += isofs/
570 obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
571 obj-$(CONFIG_HFS_FS) += hfs/
572 obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
573 +obj-$(CONFIG_UNION_FS) += unionfs/
574 obj-$(CONFIG_VXFS_FS) += freevxfs/
575 obj-$(CONFIG_NFS_FS) += nfs/
576 obj-$(CONFIG_EXPORTFS) += exportfs/
577 diff --git a/fs/namei.c b/fs/namei.c
578 index 0223c41..5d0261e 100644
581 @@ -484,6 +484,7 @@ void release_open_intent(struct nameidata *nd)
585 +EXPORT_SYMBOL_GPL(release_open_intent);
587 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
589 @@ -1740,6 +1741,42 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
590 return __lookup_hash(&this, base, NULL);
593 +/* pass nameidata from caller (useful for NFS) */
594 +struct dentry *lookup_one_len_nd(const char *name, struct dentry *base,
595 + int len, struct nameidata *nd)
598 + unsigned long hash;
601 + WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
606 + return ERR_PTR(-EACCES);
608 + hash = init_name_hash();
610 + c = *(const unsigned char *)name++;
611 + if (c == '/' || c == '\0')
612 + return ERR_PTR(-EACCES);
613 + hash = partial_name_hash(c, hash);
615 + this.hash = end_name_hash(hash);
617 + * See if the low-level filesystem might want
618 + * to use its own hash..
620 + if (base->d_flags & DCACHE_OP_HASH) {
621 + int err = base->d_op->d_hash(base, base->d_inode, &this);
623 + return ERR_PTR(err);
626 + return __lookup_hash(&this, base, nd);
629 int user_path_at(int dfd, const char __user *name, unsigned flags,
632 @@ -3339,6 +3376,7 @@ EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
633 EXPORT_SYMBOL(getname);
634 EXPORT_SYMBOL(lock_rename);
635 EXPORT_SYMBOL(lookup_one_len);
636 +EXPORT_SYMBOL(lookup_one_len_nd);
637 EXPORT_SYMBOL(page_follow_link_light);
638 EXPORT_SYMBOL(page_put_link);
639 EXPORT_SYMBOL(page_readlink);
640 diff --git a/fs/splice.c b/fs/splice.c
641 index aa866d3..421ab86 100644
644 @@ -1085,8 +1085,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
646 * Attempt to initiate a splice from pipe to file.
648 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
649 - loff_t *ppos, size_t len, unsigned int flags)
650 +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
651 + loff_t *ppos, size_t len, unsigned int flags)
653 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
654 loff_t *, size_t, unsigned int);
655 @@ -1109,13 +1109,14 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
657 return splice_write(pipe, out, ppos, len, flags);
659 +EXPORT_SYMBOL_GPL(vfs_splice_from);
662 * Attempt to initiate a splice from a file to a pipe.
664 -static long do_splice_to(struct file *in, loff_t *ppos,
665 - struct pipe_inode_info *pipe, size_t len,
666 - unsigned int flags)
667 +long vfs_splice_to(struct file *in, loff_t *ppos,
668 + struct pipe_inode_info *pipe, size_t len,
669 + unsigned int flags)
671 ssize_t (*splice_read)(struct file *, loff_t *,
672 struct pipe_inode_info *, size_t, unsigned int);
673 @@ -1135,6 +1136,7 @@ static long do_splice_to(struct file *in, loff_t *ppos,
675 return splice_read(in, ppos, pipe, len, flags);
677 +EXPORT_SYMBOL_GPL(vfs_splice_to);
680 * splice_direct_to_actor - splices data directly between two non-pipes
681 @@ -1204,7 +1206,7 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
683 loff_t pos = sd->pos, prev_pos = pos;
685 - ret = do_splice_to(in, &pos, pipe, len, flags);
686 + ret = vfs_splice_to(in, &pos, pipe, len, flags);
687 if (unlikely(ret <= 0))
690 @@ -1263,8 +1265,8 @@ static int direct_splice_actor(struct pipe_inode_info *pipe,
692 struct file *file = sd->u.file;
694 - return do_splice_from(pipe, file, &file->f_pos, sd->total_len,
696 + return vfs_splice_from(pipe, file, &file->f_pos, sd->total_len,
701 @@ -1349,7 +1351,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
705 - ret = do_splice_from(ipipe, out, off, len, flags);
706 + ret = vfs_splice_from(ipipe, out, off, len, flags);
708 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
710 @@ -1369,7 +1371,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
714 - ret = do_splice_to(in, off, opipe, len, flags);
715 + ret = vfs_splice_to(in, off, opipe, len, flags);
717 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
719 diff --git a/fs/stack.c b/fs/stack.c
720 index 4a6f7f4..7eeef12 100644
725 + * Copyright (c) 2006-2009 Erez Zadok
726 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
727 + * Copyright (c) 2006-2009 Stony Brook University
728 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
730 + * This program is free software; you can redistribute it and/or modify
731 + * it under the terms of the GNU General Public License version 2 as
732 + * published by the Free Software Foundation.
735 #include <linux/module.h>
736 #include <linux/fs.h>
737 #include <linux/fs_stack.h>
739 -/* does _NOT_ require i_mutex to be held.
741 + * does _NOT_ require i_mutex to be held.
743 * This function cannot be inlined since i_size_{read,write} is rather
744 * heavy-weight on 32-bit systems
745 diff --git a/fs/unionfs/Kconfig b/fs/unionfs/Kconfig
747 index 0000000..f3c1ac4
749 +++ b/fs/unionfs/Kconfig
752 + tristate "Union file system (EXPERIMENTAL)"
753 + depends on EXPERIMENTAL
755 + Unionfs is a stackable unification file system, which appears to
756 + merge the contents of several directories (branches), while keeping
757 + their physical content separate.
759 + See <http://unionfs.filesystems.org> for details
761 +config UNION_FS_XATTR
762 + bool "Unionfs extended attributes"
763 + depends on UNION_FS
765 + Extended attributes are name:value pairs associated with inodes by
766 + the kernel or by users (see the attr(5) manual page).
770 +config UNION_FS_DEBUG
771 + bool "Debug Unionfs"
772 + depends on UNION_FS
774 + If you say Y here, you can turn on debugging output from Unionfs.
775 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
777 index 0000000..0ece303
779 +++ b/fs/unionfs/Makefile
781 +UNIONFS_VERSION="2.5.9.2 (for 3.0.0-rc4)"
783 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
785 +obj-$(CONFIG_UNION_FS) += unionfs.o
787 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
788 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
789 + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
791 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
793 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
795 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
796 +EXTRA_CFLAGS += -DDEBUG
798 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
800 index 0000000..0a271f4
802 +++ b/fs/unionfs/commonfops.c
805 + * Copyright (c) 2003-2011 Erez Zadok
806 + * Copyright (c) 2003-2006 Charles P. Wright
807 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
808 + * Copyright (c) 2005-2006 Junjiro Okajima
809 + * Copyright (c) 2005 Arun M. Krishnakumar
810 + * Copyright (c) 2004-2006 David P. Quigley
811 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
812 + * Copyright (c) 2003 Puja Gupta
813 + * Copyright (c) 2003 Harikesavan Krishnan
814 + * Copyright (c) 2003-2011 Stony Brook University
815 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
817 + * This program is free software; you can redistribute it and/or modify
818 + * it under the terms of the GNU General Public License version 2 as
819 + * published by the Free Software Foundation.
825 + * 1) Copyup the file
826 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
827 + * stolen from NFS's silly rename
829 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
830 + struct dentry *parent, int bstart, int bindex)
832 + static unsigned int counter;
833 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
834 + const int countersize = sizeof(counter) * 2;
835 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
836 + char name[nlen + 1];
838 + struct dentry *tmp_dentry = NULL;
839 + struct dentry *lower_dentry;
840 + struct dentry *lower_dir_dentry = NULL;
842 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
844 + sprintf(name, ".unionfs%*.*lx",
845 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
848 + * Loop, looking for an unused temp name to copyup to.
850 + * It's somewhat silly that we look for a free temp tmp name in the
851 + * source branch (bstart) instead of the dest branch (bindex), where
852 + * the final name will be created. We _will_ catch it if somehow
853 + * the name exists in the dest branch, but it'd be nice to catch it
854 + * sooner than later.
859 + char *suffix = name + nlen - countersize;
863 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
865 + pr_debug("unionfs: trying to rename %s to %s\n",
866 + dentry->d_name.name, name);
868 + tmp_dentry = lookup_lck_len(name, lower_dentry->d_parent,
870 + if (IS_ERR(tmp_dentry)) {
871 + err = PTR_ERR(tmp_dentry);
874 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
877 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
878 + i_size_read(file->f_path.dentry->d_inode));
880 + if (unlikely(err == -EEXIST))
885 + /* bring it to the same state as an unlinked file */
886 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
887 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
888 + atomic_inc(&lower_dentry->d_inode->i_count);
889 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
890 + lower_dentry->d_inode);
892 + lower_dir_dentry = lock_parent(lower_dentry);
893 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
894 + unlock_dir(lower_dir_dentry);
898 + unionfs_check_dentry(dentry);
903 + * put all references held by upper struct file and free lower file pointer
906 +static void cleanup_file(struct file *file)
908 + int bindex, bstart, bend;
909 + struct file **lower_files;
910 + struct file *lower_file;
911 + struct super_block *sb = file->f_path.dentry->d_sb;
913 + lower_files = UNIONFS_F(file)->lower_files;
914 + bstart = fbstart(file);
915 + bend = fbend(file);
917 + for (bindex = bstart; bindex <= bend; bindex++) {
918 + int i; /* holds (possibly) updated branch index */
921 + lower_file = unionfs_lower_file_idx(file, bindex);
926 + * Find new index of matching branch with an open
927 + * file, since branches could have been added or
928 + * deleted causing the one with open files to shift.
930 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
931 + i = branch_id_to_idx(sb, old_bid);
932 + if (unlikely(i < 0)) {
933 + printk(KERN_ERR "unionfs: no superblock for "
934 + "file %p\n", file);
938 + /* decrement count of open files */
941 + * fput will perform an mntput for us on the correct branch.
942 + * Although we're using the file's old branch configuration,
943 + * bindex, which is the old index, correctly points to the
944 + * right branch in the file's branch list. In other words,
945 + * we're going to mntput the correct branch even if branches
946 + * have been added/removed.
949 + UNIONFS_F(file)->lower_files[bindex] = NULL;
950 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
953 + UNIONFS_F(file)->lower_files = NULL;
954 + kfree(lower_files);
955 + kfree(UNIONFS_F(file)->saved_branch_ids);
956 + /* set to NULL because caller needs to know if to kfree on error */
957 + UNIONFS_F(file)->saved_branch_ids = NULL;
960 +/* open all lower files for a given file */
961 +static int open_all_files(struct file *file)
963 + int bindex, bstart, bend, err = 0;
964 + struct file *lower_file;
965 + struct dentry *lower_dentry;
966 + struct dentry *dentry = file->f_path.dentry;
967 + struct super_block *sb = dentry->d_sb;
969 + bstart = dbstart(dentry);
970 + bend = dbend(dentry);
972 + for (bindex = bstart; bindex <= bend; bindex++) {
973 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
977 + dget(lower_dentry);
978 + unionfs_mntget(dentry, bindex);
979 + branchget(sb, bindex);
982 + dentry_open(lower_dentry,
983 + unionfs_lower_mnt_idx(dentry, bindex),
984 + file->f_flags, current_cred());
985 + if (IS_ERR(lower_file)) {
986 + branchput(sb, bindex);
987 + err = PTR_ERR(lower_file);
990 + unionfs_set_lower_file_idx(file, bindex, lower_file);
997 +/* open the highest priority file for a given upper file */
998 +static int open_highest_file(struct file *file, bool willwrite)
1000 + int bindex, bstart, bend, err = 0;
1001 + struct file *lower_file;
1002 + struct dentry *lower_dentry;
1003 + struct dentry *dentry = file->f_path.dentry;
1004 + struct dentry *parent = dget_parent(dentry);
1005 + struct inode *parent_inode = parent->d_inode;
1006 + struct super_block *sb = dentry->d_sb;
1008 + bstart = dbstart(dentry);
1009 + bend = dbend(dentry);
1011 + lower_dentry = unionfs_lower_dentry(dentry);
1012 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
1013 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1014 + err = copyup_file(parent_inode, file, bstart, bindex,
1015 + i_size_read(dentry->d_inode));
1019 + atomic_set(&UNIONFS_F(file)->generation,
1020 + atomic_read(&UNIONFS_I(dentry->d_inode)->
1025 + dget(lower_dentry);
1026 + unionfs_mntget(dentry, bstart);
1027 + lower_file = dentry_open(lower_dentry,
1028 + unionfs_lower_mnt_idx(dentry, bstart),
1029 + file->f_flags, current_cred());
1030 + if (IS_ERR(lower_file)) {
1031 + err = PTR_ERR(lower_file);
1034 + branchget(sb, bstart);
1035 + unionfs_set_lower_file(file, lower_file);
1036 + /* Fix up the position. */
1037 + lower_file->f_pos = file->f_pos;
1039 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1045 +/* perform a delayed copyup of a read-write file on a read-only branch */
1046 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1048 + int bindex, bstart, bend, err = 0;
1049 + struct dentry *dentry = file->f_path.dentry;
1050 + struct inode *parent_inode = parent->d_inode;
1052 + bstart = fbstart(file);
1053 + bend = fbend(file);
1055 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1057 + unionfs_check_file(file);
1058 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1059 + if (!d_deleted(dentry))
1060 + err = copyup_file(parent_inode, file, bstart,
1062 + i_size_read(dentry->d_inode));
1064 + err = copyup_deleted_file(file, dentry, parent,
1066 + /* if succeeded, set lower open-file flags and break */
1068 + struct file *lower_file;
1069 + lower_file = unionfs_lower_file_idx(file, bindex);
1070 + lower_file->f_flags = file->f_flags;
1074 + if (err || (bstart <= fbstart(file)))
1076 + bend = fbend(file);
1077 + for (bindex = bstart; bindex <= bend; bindex++) {
1078 + if (unionfs_lower_file_idx(file, bindex)) {
1079 + branchput(dentry->d_sb, bindex);
1080 + fput(unionfs_lower_file_idx(file, bindex));
1081 + unionfs_set_lower_file_idx(file, bindex, NULL);
1084 + path_put_lowers(dentry, bstart, bend, false);
1085 + iput_lowers(dentry->d_inode, bstart, bend, false);
1086 + /* for reg file, we only open it "once" */
1087 + fbend(file) = fbstart(file);
1088 + dbend(dentry) = dbstart(dentry);
1089 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1092 + unionfs_check_file(file);
1097 + * Helper function for unionfs_file_revalidate/locked.
1098 + * Expects dentry/parent to be locked already, and revalidated.
1100 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1101 + struct dentry *parent,
1102 + struct super_block *sb, int sbgen,
1103 + int dgen, bool willwrite)
1106 + int bstart, bend, orig_brid;
1110 + fgen = atomic_read(&UNIONFS_F(file)->generation);
1113 + * There are two cases we are interested in. The first is if the
1114 + * generation is lower than the super-block. The second is if
1115 + * someone has copied up this file from underneath us, we also need
1116 + * to refresh things.
1118 + if ((d_deleted(dentry) && dbstart(dentry) >= fbstart(file)) ||
1120 + dbstart(dentry) == fbstart(file) &&
1121 + unionfs_lower_file(file)))
1122 + goto out_may_copyup;
1124 + /* save orig branch ID */
1125 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1127 + /* First we throw out the existing files. */
1128 + cleanup_file(file);
1130 + /* Now we reopen the file(s) as in unionfs_open. */
1131 + bstart = fbstart(file) = dbstart(dentry);
1132 + bend = fbend(file) = dbend(dentry);
1134 + size = sizeof(struct file *) * sbmax(sb);
1135 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1136 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1140 + size = sizeof(int) * sbmax(sb);
1141 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1142 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1147 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1148 + /* We need to open all the files. */
1149 + err = open_all_files(file);
1154 + /* We only open the highest priority branch. */
1155 + err = open_highest_file(file, willwrite);
1158 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1159 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1161 + * If we re-opened the file on a different branch
1162 + * than the original one, and this was due to a new
1163 + * branch inserted, then update the mnt counts of
1164 + * the old and new branches accordingly.
1166 + unionfs_mntget(dentry, bstart);
1167 + unionfs_mntput(sb->s_root,
1168 + branch_id_to_idx(sb, orig_brid));
1170 + /* regular files have only one open lower file */
1171 + fbend(file) = fbstart(file);
1173 + atomic_set(&UNIONFS_F(file)->generation,
1174 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1177 + /* Copyup on the first write to a file on a readonly branch. */
1178 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1179 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1180 + is_robranch(dentry)) {
1181 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1182 + dentry->d_name.name);
1183 + err = do_delayed_copyup(file, parent);
1184 + /* regular files have only one open lower file */
1185 + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1186 + fbend(file) = fbstart(file);
1191 + kfree(UNIONFS_F(file)->lower_files);
1192 + kfree(UNIONFS_F(file)->saved_branch_ids);
1198 + * Revalidate the struct file
1199 + * @file: file to revalidate
1200 + * @parent: parent dentry (locked by caller)
1201 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1202 + * Caller must lock/unlock dentry's branch configuration.
1204 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1207 + struct super_block *sb;
1208 + struct dentry *dentry;
1212 + dentry = file->f_path.dentry;
1213 + sb = dentry->d_sb;
1214 + verify_locked(dentry);
1215 + verify_locked(parent);
1218 + * First revalidate the dentry inside struct file,
1219 + * but not unhashed dentries.
1221 + if (!d_deleted(dentry) &&
1222 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1227 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1228 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1230 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1231 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1232 + dentry->d_name.name);
1237 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1238 + sbgen, dgen, willwrite);
1243 +/* unionfs_open helper function: open a directory */
1244 +static int __open_dir(struct inode *inode, struct file *file)
1246 + struct dentry *lower_dentry;
1247 + struct file *lower_file;
1248 + int bindex, bstart, bend;
1249 + struct vfsmount *mnt;
1251 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1252 + bend = fbend(file) = dbend(file->f_path.dentry);
1254 + for (bindex = bstart; bindex <= bend; bindex++) {
1256 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1257 + if (!lower_dentry)
1260 + dget(lower_dentry);
1261 + unionfs_mntget(file->f_path.dentry, bindex);
1262 + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1263 + lower_file = dentry_open(lower_dentry, mnt, file->f_flags,
1265 + if (IS_ERR(lower_file))
1266 + return PTR_ERR(lower_file);
1268 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1271 + * The branchget goes after the open, because otherwise
1272 + * we would miss the reference on release.
1274 + branchget(inode->i_sb, bindex);
1280 +/* unionfs_open helper function: open a file */
1281 +static int __open_file(struct inode *inode, struct file *file,
1282 + struct dentry *parent)
1284 + struct dentry *lower_dentry;
1285 + struct file *lower_file;
1287 + int bindex, bstart, bend;
1289 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1290 + lower_flags = file->f_flags;
1292 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1293 + bend = fbend(file) = dbend(file->f_path.dentry);
1296 + * check for the permission for lower file. If the error is
1297 + * COPYUP_ERR, copyup the file.
1299 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1301 + * if the open will change the file, copy it up otherwise
1304 + if (lower_flags & O_TRUNC) {
1308 + /* copyup the file */
1309 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1310 + err = copyup_file(parent->d_inode, file,
1311 + bstart, bindex, size);
1313 + /* only one regular file open */
1314 + fbend(file) = fbstart(file);
1321 + * turn off writeable flags, to force delayed copyup
1324 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1328 + dget(lower_dentry);
1331 + * dentry_open will decrement mnt refcnt if err.
1332 + * otherwise fput() will do an mntput() for us upon file close.
1334 + unionfs_mntget(file->f_path.dentry, bstart);
1336 + dentry_open(lower_dentry,
1337 + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1338 + lower_flags, current_cred());
1339 + if (IS_ERR(lower_file))
1340 + return PTR_ERR(lower_file);
1342 + unionfs_set_lower_file(file, lower_file);
1343 + branchget(inode->i_sb, bstart);
1348 +int unionfs_open(struct inode *inode, struct file *file)
1351 + struct file *lower_file = NULL;
1352 + struct dentry *dentry = file->f_path.dentry;
1353 + struct dentry *parent;
1354 + int bindex = 0, bstart = 0, bend = 0;
1358 + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1359 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1360 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1362 + /* don't open unhashed/deleted files */
1363 + if (d_deleted(dentry)) {
1368 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1369 + valid = __unionfs_d_revalidate(dentry, parent, false);
1370 + if (unlikely(!valid)) {
1375 + file->private_data =
1376 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1377 + if (unlikely(!UNIONFS_F(file))) {
1381 + fbstart(file) = -1;
1383 + atomic_set(&UNIONFS_F(file)->generation,
1384 + atomic_read(&UNIONFS_I(inode)->generation));
1386 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1387 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1388 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1392 + size = sizeof(int) * sbmax(inode->i_sb);
1393 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1394 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1399 + bstart = fbstart(file) = dbstart(dentry);
1400 + bend = fbend(file) = dbend(dentry);
1403 + * open all directories and make the unionfs file struct point to
1404 + * these lower file structs
1406 + if (S_ISDIR(inode->i_mode))
1407 + err = __open_dir(inode, file); /* open a dir */
1409 + err = __open_file(inode, file, parent); /* open a file */
1411 + /* freeing the allocated resources, and fput the opened files */
1413 + for (bindex = bstart; bindex <= bend; bindex++) {
1414 + lower_file = unionfs_lower_file_idx(file, bindex);
1418 + branchput(dentry->d_sb, bindex);
1419 + /* fput calls dput for lower_dentry */
1426 + kfree(UNIONFS_F(file)->lower_files);
1427 + kfree(UNIONFS_F(file)->saved_branch_ids);
1428 + kfree(UNIONFS_F(file));
1432 + unionfs_postcopyup_setmnt(dentry);
1433 + unionfs_copy_attr_times(inode);
1434 + unionfs_check_file(file);
1435 + unionfs_check_inode(inode);
1437 + unionfs_unlock_dentry(dentry);
1438 + unionfs_unlock_parent(dentry, parent);
1439 + unionfs_read_unlock(inode->i_sb);
1444 + * release all lower object references & free the file info structure
1446 + * No need to grab sb info's rwsem.
1448 +int unionfs_file_release(struct inode *inode, struct file *file)
1450 + struct file *lower_file = NULL;
1451 + struct unionfs_file_info *fileinfo;
1452 + struct unionfs_inode_info *inodeinfo;
1453 + struct super_block *sb = inode->i_sb;
1454 + struct dentry *dentry = file->f_path.dentry;
1455 + struct dentry *parent;
1456 + int bindex, bstart, bend;
1460 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
1461 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
1462 + * has been causing false positives in file system stacking layers.
1463 + * In particular, our ->mmap is called after sys_mmap2 already holds
1464 + * mmap_sem, then we lock our own mutexes; but earlier, it's
1465 + * possible for lockdep to have locked our mutexes first, and then
1466 + * we call a lower ->readdir which could call might_fault. The
1467 + * different ordering of the locks is what lockdep complains about
1468 + * -- unnecessarily. Therefore, we have no choice but to tell
1469 + * lockdep to temporarily turn off lockdep here. Note: the comments
1470 + * inside might_sleep also suggest that it would have been
1471 + * nicer to only annotate paths that needs that might_lock_read.
1474 + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1475 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1476 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1479 + * We try to revalidate, but the VFS ignores return return values
1480 + * from file->release, so we must always try to succeed here,
1481 + * including to do the kfree and dput below. So if revalidation
1482 + * failed, all we can do is print some message and keep going.
1484 + err = unionfs_file_revalidate(file, parent,
1485 + UNIONFS_F(file)->wrote_to_file);
1487 + unionfs_check_file(file);
1488 + fileinfo = UNIONFS_F(file);
1489 + BUG_ON(file->f_path.dentry->d_inode != inode);
1490 + inodeinfo = UNIONFS_I(inode);
1492 + /* fput all the lower files */
1493 + bstart = fbstart(file);
1494 + bend = fbend(file);
1496 + for (bindex = bstart; bindex <= bend; bindex++) {
1497 + lower_file = unionfs_lower_file_idx(file, bindex);
1500 + unionfs_set_lower_file_idx(file, bindex, NULL);
1502 + branchput(sb, bindex);
1505 + /* if there are no more refs to the dentry, dput it */
1506 + if (d_deleted(dentry)) {
1507 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1508 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1512 + kfree(fileinfo->lower_files);
1513 + kfree(fileinfo->saved_branch_ids);
1515 + if (fileinfo->rdstate) {
1516 + fileinfo->rdstate->access = jiffies;
1517 + spin_lock(&inodeinfo->rdlock);
1518 + inodeinfo->rdcount++;
1519 + list_add_tail(&fileinfo->rdstate->cache,
1520 + &inodeinfo->readdircache);
1521 + mark_inode_dirty(inode);
1522 + spin_unlock(&inodeinfo->rdlock);
1523 + fileinfo->rdstate = NULL;
1527 + unionfs_unlock_dentry(dentry);
1528 + unionfs_unlock_parent(dentry, parent);
1529 + unionfs_read_unlock(sb);
1534 +/* pass the ioctl to the lower fs */
1535 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1537 + struct file *lower_file;
1540 + lower_file = unionfs_lower_file(file);
1543 + if (!lower_file || !lower_file->f_op)
1545 + if (lower_file->f_op->unlocked_ioctl) {
1546 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1547 +#ifdef CONFIG_COMPAT
1548 + } else if (lower_file->f_op->compat_ioctl) {
1549 + err = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
1558 + * return to user-space the branch indices containing the file in question
1560 + * We use fd_set and therefore we are limited to the number of the branches
1561 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1563 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1564 + unsigned int cmd, unsigned long arg)
1567 + fd_set branchlist;
1568 + int bstart = 0, bend = 0, bindex = 0;
1569 + int orig_bstart, orig_bend;
1570 + struct dentry *dentry, *lower_dentry;
1571 + struct vfsmount *mnt;
1573 + dentry = file->f_path.dentry;
1574 + orig_bstart = dbstart(dentry);
1575 + orig_bend = dbend(dentry);
1576 + err = unionfs_partial_lookup(dentry, parent);
1579 + bstart = dbstart(dentry);
1580 + bend = dbend(dentry);
1582 + FD_ZERO(&branchlist);
1584 + for (bindex = bstart; bindex <= bend; bindex++) {
1585 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1586 + if (!lower_dentry)
1588 + if (likely(lower_dentry->d_inode))
1589 + FD_SET(bindex, &branchlist);
1590 + /* purge any lower objects after partial_lookup */
1591 + if (bindex < orig_bstart || bindex > orig_bend) {
1592 + dput(lower_dentry);
1593 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1594 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1595 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1597 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1600 + unionfs_mntput(dentry, bindex);
1601 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1604 + /* restore original dentry's offsets */
1605 + dbstart(dentry) = orig_bstart;
1606 + dbend(dentry) = orig_bend;
1607 + ibstart(dentry->d_inode) = orig_bstart;
1608 + ibend(dentry->d_inode) = orig_bend;
1610 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1611 + if (unlikely(err))
1615 + return err < 0 ? err : bend;
1618 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1621 + struct dentry *dentry = file->f_path.dentry;
1622 + struct dentry *parent;
1624 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1625 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1626 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1628 + err = unionfs_file_revalidate(file, parent, true);
1629 + if (unlikely(err))
1632 + /* check if asked for local commands */
1634 + case UNIONFS_IOCTL_INCGEN:
1635 + /* Increment the superblock generation count */
1636 + pr_info("unionfs: incgen ioctl deprecated; "
1637 + "use \"-o remount,incgen\"\n");
1641 + case UNIONFS_IOCTL_QUERYFILE:
1642 + /* Return list of branches containing the given file */
1643 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1647 + /* pass the ioctl down */
1648 + err = do_ioctl(file, cmd, arg);
1653 + unionfs_check_file(file);
1654 + unionfs_unlock_dentry(dentry);
1655 + unionfs_unlock_parent(dentry, parent);
1656 + unionfs_read_unlock(dentry->d_sb);
1660 +int unionfs_flush(struct file *file, fl_owner_t id)
1663 + struct file *lower_file = NULL;
1664 + struct dentry *dentry = file->f_path.dentry;
1665 + struct dentry *parent;
1666 + int bindex, bstart, bend;
1668 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1669 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1670 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1672 + err = unionfs_file_revalidate(file, parent,
1673 + UNIONFS_F(file)->wrote_to_file);
1674 + if (unlikely(err))
1676 + unionfs_check_file(file);
1678 + bstart = fbstart(file);
1679 + bend = fbend(file);
1680 + for (bindex = bstart; bindex <= bend; bindex++) {
1681 + lower_file = unionfs_lower_file_idx(file, bindex);
1683 + if (lower_file && lower_file->f_op &&
1684 + lower_file->f_op->flush) {
1685 + err = lower_file->f_op->flush(lower_file, id);
1694 + unionfs_check_file(file);
1695 + unionfs_unlock_dentry(dentry);
1696 + unionfs_unlock_parent(dentry, parent);
1697 + unionfs_read_unlock(dentry->d_sb);
1700 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1701 new file mode 100644
1702 index 0000000..37c2654
1704 +++ b/fs/unionfs/copyup.c
1707 + * Copyright (c) 2003-2011 Erez Zadok
1708 + * Copyright (c) 2003-2006 Charles P. Wright
1709 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1710 + * Copyright (c) 2005-2006 Junjiro Okajima
1711 + * Copyright (c) 2005 Arun M. Krishnakumar
1712 + * Copyright (c) 2004-2006 David P. Quigley
1713 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1714 + * Copyright (c) 2003 Puja Gupta
1715 + * Copyright (c) 2003 Harikesavan Krishnan
1716 + * Copyright (c) 2003-2011 Stony Brook University
1717 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
1719 + * This program is free software; you can redistribute it and/or modify
1720 + * it under the terms of the GNU General Public License version 2 as
1721 + * published by the Free Software Foundation.
1727 + * For detailed explanation of copyup see:
1728 + * Documentation/filesystems/unionfs/concepts.txt
1731 +#ifdef CONFIG_UNION_FS_XATTR
1732 +/* copyup all extended attrs for a given dentry */
1733 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1734 + struct dentry *new_lower_dentry)
1737 + ssize_t list_size = -1;
1738 + char *name_list = NULL;
1739 + char *attr_value = NULL;
1740 + char *name_list_buf = NULL;
1742 + /* query the actual size of the xattr list */
1743 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1744 + if (list_size <= 0) {
1749 + /* allocate space for the actual list */
1750 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1751 + if (unlikely(!name_list || IS_ERR(name_list))) {
1752 + err = PTR_ERR(name_list);
1756 + name_list_buf = name_list; /* save for kfree at end */
1758 + /* now get the actual xattr list of the source file */
1759 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1760 + if (list_size <= 0) {
1765 + /* allocate space to hold each xattr's value */
1766 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1767 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1768 + err = PTR_ERR(name_list);
1772 + /* in a loop, get and set each xattr from src to dst file */
1773 + while (*name_list) {
1776 + /* Lock here since vfs_getxattr doesn't lock for us */
1777 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1778 + size = vfs_getxattr(old_lower_dentry, name_list,
1779 + attr_value, XATTR_SIZE_MAX);
1780 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1785 + if (size > XATTR_SIZE_MAX) {
1789 + /* Don't lock here since vfs_setxattr does it for us. */
1790 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1793 + * Selinux depends on "security.*" xattrs, so to maintain
1794 + * the security of copied-up files, if Selinux is active,
1795 + * then we must copy these xattrs as well. So we need to
1796 + * temporarily get FOWNER privileges.
1797 + * XXX: move entire copyup code to SIOQ.
1799 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1800 + const struct cred *old_creds;
1801 + struct cred *new_creds;
1803 + new_creds = prepare_creds();
1804 + if (unlikely(!new_creds)) {
1808 + cap_raise(new_creds->cap_effective, CAP_FOWNER);
1809 + old_creds = override_creds(new_creds);
1810 + err = vfs_setxattr(new_lower_dentry, name_list,
1811 + attr_value, size, 0);
1812 + revert_creds(old_creds);
1816 + name_list += strlen(name_list) + 1;
1819 + unionfs_xattr_kfree(name_list_buf);
1820 + unionfs_xattr_kfree(attr_value);
1821 + /* Ignore if xattr isn't supported */
1822 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1826 +#endif /* CONFIG_UNION_FS_XATTR */
1829 + * Determine the mode based on the copyup flags, and the existing dentry.
1831 + * Handle file systems which may not support certain options. For example
1832 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1833 + * errors, rather than propagating them up, which results in copyup errors
1834 + * and errors returned back to users.
1836 +static int copyup_permissions(struct super_block *sb,
1837 + struct dentry *old_lower_dentry,
1838 + struct dentry *new_lower_dentry)
1840 + struct inode *i = old_lower_dentry->d_inode;
1841 + struct iattr newattrs;
1844 + newattrs.ia_atime = i->i_atime;
1845 + newattrs.ia_mtime = i->i_mtime;
1846 + newattrs.ia_ctime = i->i_ctime;
1847 + newattrs.ia_gid = i->i_gid;
1848 + newattrs.ia_uid = i->i_uid;
1849 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1850 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1851 + ATTR_GID | ATTR_UID;
1852 + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1853 + err = notify_change(new_lower_dentry, &newattrs);
1857 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1858 + newattrs.ia_mode = i->i_mode;
1859 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1860 + err = notify_change(new_lower_dentry, &newattrs);
1861 + if (err == -EOPNOTSUPP &&
1862 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1863 + printk(KERN_WARNING
1864 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1865 + new_lower_dentry->d_name.name);
1870 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1875 + * create the new device/file/directory - use copyup_permission to copyup
1878 + * if the object being copied up is a regular file, the file is only created,
1879 + * the contents have to be copied up separately
1881 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1882 + struct dentry *new_lower_dentry,
1883 + struct dentry *new_lower_parent_dentry,
1887 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1888 + struct sioq_args args;
1890 + if (S_ISDIR(old_mode)) {
1891 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1892 + args.mkdir.dentry = new_lower_dentry;
1893 + args.mkdir.mode = old_mode;
1895 + run_sioq(__unionfs_mkdir, &args);
1897 + } else if (S_ISLNK(old_mode)) {
1898 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1899 + args.symlink.dentry = new_lower_dentry;
1900 + args.symlink.symbuf = symbuf;
1902 + run_sioq(__unionfs_symlink, &args);
1904 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1905 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1906 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1907 + args.mknod.dentry = new_lower_dentry;
1908 + args.mknod.mode = old_mode;
1909 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1911 + run_sioq(__unionfs_mknod, &args);
1913 + } else if (S_ISREG(old_mode)) {
1914 + struct nameidata nd;
1915 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1916 + if (unlikely(err < 0))
1918 + args.create.nd = &nd;
1919 + args.create.parent = new_lower_parent_dentry->d_inode;
1920 + args.create.dentry = new_lower_dentry;
1921 + args.create.mode = old_mode;
1923 + run_sioq(__unionfs_create, &args);
1925 + release_lower_nd(&nd, err);
1927 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1936 +static int __copyup_reg_data(struct dentry *dentry,
1937 + struct dentry *new_lower_dentry, int new_bindex,
1938 + struct dentry *old_lower_dentry, int old_bindex,
1939 + struct file **copyup_file, loff_t len)
1941 + struct super_block *sb = dentry->d_sb;
1942 + struct file *input_file;
1943 + struct file *output_file;
1944 + struct vfsmount *output_mnt;
1945 + mm_segment_t old_fs;
1947 + ssize_t read_bytes, write_bytes;
1951 + /* open old file */
1952 + unionfs_mntget(dentry, old_bindex);
1953 + branchget(sb, old_bindex);
1954 + /* dentry_open calls dput and mntput if it returns an error */
1955 + input_file = dentry_open(old_lower_dentry,
1956 + unionfs_lower_mnt_idx(dentry, old_bindex),
1957 + O_RDONLY | O_LARGEFILE, current_cred());
1958 + if (IS_ERR(input_file)) {
1959 + dput(old_lower_dentry);
1960 + err = PTR_ERR(input_file);
1963 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1965 + goto out_close_in;
1968 + /* open new file */
1969 + dget(new_lower_dentry);
1970 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
1971 + branchget(sb, new_bindex);
1972 + output_file = dentry_open(new_lower_dentry, output_mnt,
1973 + O_RDWR | O_LARGEFILE, current_cred());
1974 + if (IS_ERR(output_file)) {
1975 + err = PTR_ERR(output_file);
1976 + goto out_close_in2;
1978 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1980 + goto out_close_out;
1983 + /* allocating a buffer */
1984 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1985 + if (unlikely(!buf)) {
1987 + goto out_close_out;
1990 + input_file->f_pos = 0;
1991 + output_file->f_pos = 0;
1993 + old_fs = get_fs();
1994 + set_fs(KERNEL_DS);
1999 + if (len >= PAGE_SIZE)
2001 + else if ((len < PAGE_SIZE) && (len > 0))
2007 + input_file->f_op->read(input_file,
2008 + (char __user *)buf, size,
2009 + &input_file->f_pos);
2010 + if (read_bytes <= 0) {
2015 + /* see Documentation/filesystems/unionfs/issues.txt */
2018 + output_file->f_op->write(output_file,
2019 + (char __user *)buf,
2021 + &output_file->f_pos);
2023 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2024 + err = write_bytes;
2027 + } while ((read_bytes > 0) && (len > 0));
2034 + err = output_file->f_op->fsync(output_file, 0);
2037 + goto out_close_out;
2039 + if (copyup_file) {
2040 + *copyup_file = output_file;
2041 + goto out_close_in;
2045 + fput(output_file);
2048 + branchput(sb, new_bindex);
2054 + branchput(sb, old_bindex);
2060 + * dput the lower references for old and new dentry & clear a lower dentry
2063 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2064 + int old_bstart, int old_bend,
2065 + struct dentry *new_lower_dentry, int new_bindex)
2067 + /* get rid of the lower dentry and all its traces */
2068 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2069 + dbstart(dentry) = old_bstart;
2070 + dbend(dentry) = old_bend;
2072 + dput(new_lower_dentry);
2073 + dput(old_lower_dentry);
2077 + * Copy up a dentry to a file of specified name.
2079 + * @dir: used to pull the ->i_sb to access other branches
2080 + * @dentry: the non-negative dentry whose lower_inode we should copy
2081 + * @bstart: the branch of the lower_inode to copy from
2082 + * @new_bindex: the branch to create the new file in
2083 + * @name: the name of the file to create
2084 + * @namelen: length of @name
2085 + * @copyup_file: the "struct file" to return (optional)
2086 + * @len: how many bytes to copy-up?
2088 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2089 + int new_bindex, const char *name, int namelen,
2090 + struct file **copyup_file, loff_t len)
2092 + struct dentry *new_lower_dentry;
2093 + struct dentry *old_lower_dentry = NULL;
2094 + struct super_block *sb;
2099 + struct dentry *new_lower_parent_dentry = NULL;
2100 + mm_segment_t oldfs;
2101 + char *symbuf = NULL;
2103 + verify_locked(dentry);
2105 + old_bindex = bstart;
2106 + old_bstart = dbstart(dentry);
2107 + old_bend = dbend(dentry);
2109 + BUG_ON(new_bindex < 0);
2110 + BUG_ON(new_bindex >= old_bindex);
2114 + err = is_robranch_super(sb, new_bindex);
2118 + /* Create the directory structure above this dentry. */
2119 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2120 + if (IS_ERR(new_lower_dentry)) {
2121 + err = PTR_ERR(new_lower_dentry);
2125 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2126 + /* we conditionally dput this old_lower_dentry at end of function */
2127 + dget(old_lower_dentry);
2129 + /* For symlinks, we must read the link before we lock the directory. */
2130 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2132 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2133 + if (unlikely(!symbuf)) {
2134 + __clear(dentry, old_lower_dentry,
2135 + old_bstart, old_bend,
2136 + new_lower_dentry, new_bindex);
2142 + set_fs(KERNEL_DS);
2143 + err = old_lower_dentry->d_inode->i_op->readlink(
2145 + (char __user *)symbuf,
2149 + __clear(dentry, old_lower_dentry,
2150 + old_bstart, old_bend,
2151 + new_lower_dentry, new_bindex);
2154 + symbuf[err] = '\0';
2157 + /* Now we lock the parent, and create the object in the new branch. */
2158 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2160 + /* create the new inode */
2161 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2162 + new_lower_parent_dentry, symbuf);
2165 + __clear(dentry, old_lower_dentry,
2166 + old_bstart, old_bend,
2167 + new_lower_dentry, new_bindex);
2171 + /* We actually copyup the file here. */
2172 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2173 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2174 + old_lower_dentry, old_bindex,
2175 + copyup_file, len);
2179 + /* Set permissions. */
2180 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2184 +#ifdef CONFIG_UNION_FS_XATTR
2185 + /* Selinux uses extended attributes for permissions. */
2186 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2189 +#endif /* CONFIG_UNION_FS_XATTR */
2191 + /* do not allow files getting deleted to be re-interposed */
2192 + if (!d_deleted(dentry))
2193 + unionfs_reinterpose(dentry);
2199 + * copyup failed, because we possibly ran out of space or
2200 + * quota, or something else happened so let's unlink; we don't
2201 + * really care about the return value of vfs_unlink
2203 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2205 + if (copyup_file) {
2206 + /* need to close the file */
2208 + fput(*copyup_file);
2209 + branchput(sb, new_bindex);
2213 + * TODO: should we reset the error to something like -EIO?
2215 + * If we don't reset, the user may get some nonsensical errors, but
2216 + * on the other hand, if we reset to EIO, we guarantee that the user
2217 + * will get a "confusing" error message.
2221 + unlock_dir(new_lower_parent_dentry);
2225 + * If old_lower_dentry was not a file, then we need to dput it. If
2226 + * it was a file, then it was already dput indirectly by other
2227 + * functions we call above which operate on regular files.
2229 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2230 + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2231 + dput(old_lower_dentry);
2236 + * if directory creation succeeded, but inode copyup failed,
2237 + * then purge new dentries.
2239 + if (dbstart(dentry) < old_bstart &&
2240 + ibstart(dentry->d_inode) > dbstart(dentry))
2241 + __clear(dentry, NULL, old_bstart, old_bend,
2242 + unionfs_lower_dentry(dentry), dbstart(dentry));
2245 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2246 + unionfs_postcopyup_release(dentry);
2247 + if (!unionfs_lower_inode(dentry->d_inode)) {
2249 + * If we got here, then we copied up to an
2250 + * unlinked-open file, whose name is .unionfsXXXXX.
2252 + struct inode *inode = new_lower_dentry->d_inode;
2253 + atomic_inc(&inode->i_count);
2254 + unionfs_set_lower_inode_idx(dentry->d_inode,
2255 + ibstart(dentry->d_inode),
2259 + unionfs_postcopyup_setmnt(dentry);
2260 + /* sync inode times from copied-up inode to our inode */
2261 + unionfs_copy_attr_times(dentry->d_inode);
2262 + unionfs_check_inode(dir);
2263 + unionfs_check_dentry(dentry);
2269 + * This function creates a copy of a file represented by 'file' which
2270 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2271 + * will be named "name".
2273 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2274 + int bstart, int new_bindex, loff_t len)
2277 + struct file *output_file = NULL;
2279 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2280 + name, strlen(name), &output_file, len);
2282 + fbstart(file) = new_bindex;
2283 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2290 + * This function creates a copy of a file represented by 'file' which
2291 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2293 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2294 + int new_bindex, loff_t len)
2297 + struct file *output_file = NULL;
2298 + struct dentry *dentry = file->f_path.dentry;
2300 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2301 + dentry->d_name.name, dentry->d_name.len,
2302 + &output_file, len);
2304 + fbstart(file) = new_bindex;
2305 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2311 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2312 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2313 + int old_bstart, int old_bend)
2317 + int new_bstart = -1;
2318 + int new_bend = -1;
2321 + loop_start = min(old_bstart, bindex);
2322 + loop_end = max(old_bend, bindex);
2325 + * This loop sets the bstart and bend for the new dentry by
2326 + * traversing from left to right. It also dputs all negative
2327 + * dentries except bindex
2329 + for (i = loop_start; i <= loop_end; i++) {
2330 + if (!unionfs_lower_dentry_idx(dentry, i))
2333 + if (i == bindex) {
2335 + if (new_bstart < 0)
2340 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2341 + dput(unionfs_lower_dentry_idx(dentry, i));
2342 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2344 + unionfs_mntput(dentry, i);
2345 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2347 + if (new_bstart < 0)
2353 + if (new_bstart < 0)
2354 + new_bstart = bindex;
2356 + new_bend = bindex;
2357 + dbstart(dentry) = new_bstart;
2358 + dbend(dentry) = new_bend;
2362 +/* set lower inode ptr and update bstart & bend if necessary */
2363 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2366 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2367 + igrab(lower->d_inode));
2368 + if (likely(ibstart(upper->d_inode) > bindex))
2369 + ibstart(upper->d_inode) = bindex;
2370 + if (likely(ibend(upper->d_inode) < bindex))
2371 + ibend(upper->d_inode) = bindex;
2375 +/* set lower dentry ptr and update bstart & bend if necessary */
2376 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2379 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2380 + if (likely(dbstart(upper) > bindex))
2381 + dbstart(upper) = bindex;
2382 + if (likely(dbend(upper) < bindex))
2383 + dbend(upper) = bindex;
2387 + * This function replicates the directory structure up-to given dentry
2388 + * in the bindex branch.
2390 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2391 + const char *name, int bindex)
2394 + struct dentry *child_dentry;
2395 + struct dentry *parent_dentry;
2396 + struct dentry *lower_parent_dentry = NULL;
2397 + struct dentry *lower_dentry = NULL;
2398 + const char *childname;
2399 + unsigned int childnamelen;
2404 + struct dentry **path = NULL;
2405 + struct super_block *sb;
2407 + verify_locked(dentry);
2409 + err = is_robranch_super(dir->i_sb, bindex);
2411 + lower_dentry = ERR_PTR(err);
2415 + old_bstart = dbstart(dentry);
2416 + old_bend = dbend(dentry);
2418 + lower_dentry = ERR_PTR(-ENOMEM);
2420 + /* There is no sense allocating any less than the minimum. */
2422 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2423 + if (unlikely(!path))
2426 + /* assume the negative dentry of unionfs as the parent dentry */
2427 + parent_dentry = dentry;
2430 + * This loop finds the first parent that exists in the given branch.
2431 + * We start building the directory structure from there. At the end
2432 + * of the loop, the following should hold:
2433 + * - child_dentry is the first nonexistent child
2434 + * - parent_dentry is the first existent parent
2435 + * - path[0] is the = deepest child
2436 + * - path[count] is the first child to create
2439 + child_dentry = parent_dentry;
2441 + /* find the parent directory dentry in unionfs */
2442 + parent_dentry = dget_parent(child_dentry);
2444 + /* find out the lower_parent_dentry in the given branch */
2445 + lower_parent_dentry =
2446 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2448 + /* grow path table */
2449 + if (count == nr_dentry) {
2453 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2455 + if (unlikely(!p)) {
2456 + lower_dentry = ERR_PTR(-ENOMEM);
2462 + /* store the child dentry */
2463 + path[count++] = child_dentry;
2464 + } while (!lower_parent_dentry);
2467 + sb = dentry->d_sb;
2470 + * This code goes between the begin/end labels and basically
2471 + * emulates a while(child_dentry != dentry), only cleaner and
2472 + * shorter than what would be a much longer while loop.
2475 + /* get lower parent dir in the current branch */
2476 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2477 + dput(parent_dentry);
2479 + /* init the values to lookup */
2480 + childname = child_dentry->d_name.name;
2481 + childnamelen = child_dentry->d_name.len;
2483 + if (child_dentry != dentry) {
2484 + /* lookup child in the underlying file system */
2485 + lower_dentry = lookup_lck_len(childname, lower_parent_dentry,
2487 + if (IS_ERR(lower_dentry))
2491 + * Is the name a whiteout of the child name ? lookup the
2492 + * whiteout child in the underlying file system
2494 + lower_dentry = lookup_lck_len(name, lower_parent_dentry,
2496 + if (IS_ERR(lower_dentry))
2499 + /* Replace the current dentry (if any) with the new one */
2500 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2501 + unionfs_set_lower_dentry_idx(dentry, bindex,
2504 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2508 + if (lower_dentry->d_inode) {
2510 + * since this already exists we dput to avoid
2511 + * multiple references on the same dentry
2513 + dput(lower_dentry);
2515 + struct sioq_args args;
2517 + /* it's a negative dentry, create a new dir */
2518 + lower_parent_dentry = lock_parent(lower_dentry);
2520 + args.mkdir.parent = lower_parent_dentry->d_inode;
2521 + args.mkdir.dentry = lower_dentry;
2522 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2524 + run_sioq(__unionfs_mkdir, &args);
2528 + err = copyup_permissions(dir->i_sb, child_dentry,
2530 + unlock_dir(lower_parent_dentry);
2532 + dput(lower_dentry);
2533 + lower_dentry = ERR_PTR(err);
2539 + __set_inode(child_dentry, lower_dentry, bindex);
2540 + __set_dentry(child_dentry, lower_dentry, bindex);
2542 + * update times of this dentry, but also the parent, because if
2543 + * we changed, the parent may have changed too.
2545 + fsstack_copy_attr_times(parent_dentry->d_inode,
2546 + lower_parent_dentry->d_inode);
2547 + unionfs_copy_attr_times(child_dentry->d_inode);
2549 + parent_dentry = child_dentry;
2550 + child_dentry = path[--count];
2553 + /* cleanup any leftover locks from the do/while loop above */
2554 + if (IS_ERR(lower_dentry))
2556 + dput(path[count--]);
2558 + return lower_dentry;
2562 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2563 + * dentry+parents to the first parent node that has an mnt.
2565 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2567 + struct dentry *parent, *hasone;
2568 + int bindex = dbstart(dentry);
2570 + if (unionfs_lower_mnt_idx(dentry, bindex))
2572 + hasone = dentry->d_parent;
2573 + /* this loop should stop at root dentry */
2574 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2575 + hasone = hasone->d_parent;
2577 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2578 + unionfs_set_lower_mnt_idx(parent, bindex,
2579 + unionfs_mntget(hasone, bindex));
2580 + parent = parent->d_parent;
2585 + * Post-copyup helper to release all non-directory source objects of a
2586 + * copied-up file. Regular files should have only one lower object.
2588 +void unionfs_postcopyup_release(struct dentry *dentry)
2592 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2593 + bstart = dbstart(dentry);
2594 + bend = dbend(dentry);
2596 + path_put_lowers(dentry, bstart + 1, bend, false);
2597 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2599 + dbend(dentry) = bstart;
2600 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2602 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2603 new file mode 100644
2604 index 0000000..6092e69
2606 +++ b/fs/unionfs/debug.c
2609 + * Copyright (c) 2003-2011 Erez Zadok
2610 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2611 + * Copyright (c) 2003-2011 Stony Brook University
2612 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
2614 + * This program is free software; you can redistribute it and/or modify
2615 + * it under the terms of the GNU General Public License version 2 as
2616 + * published by the Free Software Foundation.
2622 + * Helper debugging functions for maintainers (and for users to report back
2623 + * useful information back to maintainers)
2626 +/* it's always useful to know what part of the code called us */
2627 +#define PRINT_CALLER(fname, fxn, line) \
2629 + if (!printed_caller) { \
2630 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2631 + printed_caller = 1; \
2636 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2637 + * the fan-out of various Unionfs objects. We check that no lower objects
2638 + * exist outside the start/end branch range; that all objects within are
2639 + * non-NULL (with some allowed exceptions); that for every lower file
2640 + * there's a lower dentry+inode; that the start/end ranges match for all
2641 + * corresponding lower objects; that open files/symlinks have only one lower
2642 + * objects, but directories can have several; and more.
2644 +void __unionfs_check_inode(const struct inode *inode,
2645 + const char *fname, const char *fxn, int line)
2649 + struct inode *lower_inode;
2650 + struct super_block *sb;
2651 + int printed_caller = 0;
2654 + /* for inodes now */
2657 + istart = ibstart(inode);
2658 + iend = ibend(inode);
2659 + /* don't check inode if no lower branches */
2660 + if (istart < 0 && iend < 0)
2662 + if (unlikely(istart > iend)) {
2663 + PRINT_CALLER(fname, fxn, line);
2664 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2665 + inode, istart, iend);
2667 + if (unlikely((istart == -1 && iend != -1) ||
2668 + (istart != -1 && iend == -1))) {
2669 + PRINT_CALLER(fname, fxn, line);
2670 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2671 + inode, istart, iend);
2673 + if (!S_ISDIR(inode->i_mode)) {
2674 + if (unlikely(iend != istart)) {
2675 + PRINT_CALLER(fname, fxn, line);
2676 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2677 + inode, istart, iend);
2681 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2682 + if (unlikely(!UNIONFS_I(inode))) {
2683 + PRINT_CALLER(fname, fxn, line);
2684 + pr_debug(" Ci3: no inode_info %p\n", inode);
2687 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2688 + PRINT_CALLER(fname, fxn, line);
2689 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2692 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2693 + if (lower_inode) {
2694 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2695 + if (unlikely(bindex < istart || bindex > iend)) {
2696 + PRINT_CALLER(fname, fxn, line);
2697 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2698 + "istart/end=%d:%d\n", inode,
2699 + lower_inode, bindex, istart, iend);
2700 + } else if (unlikely(lower_inode == poison_ptr)) {
2701 + /* freed inode! */
2702 + PRINT_CALLER(fname, fxn, line);
2703 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2704 + "istart/end=%d:%d\n", inode,
2705 + lower_inode, bindex, istart, iend);
2709 + /* if we get here, then lower_inode == NULL */
2710 + if (bindex < istart || bindex > iend)
2713 + * directories can have NULL lower inodes in b/t start/end,
2714 + * but NOT if at the start/end range.
2716 + if (unlikely(S_ISDIR(inode->i_mode) &&
2717 + bindex > istart && bindex < iend))
2719 + PRINT_CALLER(fname, fxn, line);
2720 + pr_debug(" Ci7: inode/linode=%p:%p "
2721 + "bindex=%d istart/end=%d:%d\n",
2722 + inode, lower_inode, bindex, istart, iend);
2726 +void __unionfs_check_dentry(const struct dentry *dentry,
2727 + const char *fname, const char *fxn, int line)
2730 + int dstart, dend, istart, iend;
2731 + struct dentry *lower_dentry;
2732 + struct inode *inode, *lower_inode;
2733 + struct super_block *sb;
2734 + struct vfsmount *lower_mnt;
2735 + int printed_caller = 0;
2739 + sb = dentry->d_sb;
2740 + inode = dentry->d_inode;
2741 + dstart = dbstart(dentry);
2742 + dend = dbend(dentry);
2743 + /* don't check dentry/mnt if no lower branches */
2744 + if (dstart < 0 && dend < 0)
2746 + BUG_ON(dstart > dend);
2748 + if (unlikely((dstart == -1 && dend != -1) ||
2749 + (dstart != -1 && dend == -1))) {
2750 + PRINT_CALLER(fname, fxn, line);
2751 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2752 + dentry, dstart, dend);
2755 + * check for NULL dentries inside the start/end range, or
2756 + * non-NULL dentries outside the start/end range.
2758 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2759 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2760 + if (lower_dentry) {
2761 + if (unlikely(bindex < dstart || bindex > dend)) {
2762 + PRINT_CALLER(fname, fxn, line);
2763 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2764 + "bindex=%d dstart/end=%d:%d\n",
2765 + dentry, lower_dentry,
2766 + (lower_dentry ? lower_dentry->d_inode :
2768 + bindex, dstart, dend);
2770 + } else { /* lower_dentry == NULL */
2771 + if (bindex < dstart || bindex > dend)
2774 + * Directories can have NULL lower inodes in b/t
2775 + * start/end, but NOT if at the start/end range.
2776 + * Ignore this rule, however, if this is a NULL
2777 + * dentry or a deleted dentry.
2779 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2781 + !(inode && S_ISDIR(inode->i_mode) &&
2782 + bindex > dstart && bindex < dend))) {
2783 + PRINT_CALLER(fname, fxn, line);
2784 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2785 + "bindex=%d dstart/end=%d:%d\n",
2786 + dentry, lower_dentry,
2788 + lower_dentry->d_inode :
2790 + bindex, dstart, dend);
2795 + /* check for vfsmounts same as for dentries */
2796 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2797 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2799 + if (unlikely(bindex < dstart || bindex > dend)) {
2800 + PRINT_CALLER(fname, fxn, line);
2801 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2802 + "dstart/end=%d:%d\n", dentry,
2803 + lower_mnt, bindex, dstart, dend);
2805 + } else { /* lower_mnt == NULL */
2806 + if (bindex < dstart || bindex > dend)
2809 + * Directories can have NULL lower inodes in b/t
2810 + * start/end, but NOT if at the start/end range.
2811 + * Ignore this rule, however, if this is a NULL
2814 + if (unlikely(inode &&
2815 + !(inode && S_ISDIR(inode->i_mode) &&
2816 + bindex > dstart && bindex < dend))) {
2817 + PRINT_CALLER(fname, fxn, line);
2818 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2819 + "bindex=%d dstart/end=%d:%d\n",
2820 + dentry, lower_mnt, bindex,
2827 + /* for inodes now */
2830 + istart = ibstart(inode);
2831 + iend = ibend(inode);
2832 + /* don't check inode if no lower branches */
2833 + if (istart < 0 && iend < 0)
2835 + BUG_ON(istart > iend);
2836 + if (unlikely((istart == -1 && iend != -1) ||
2837 + (istart != -1 && iend == -1))) {
2838 + PRINT_CALLER(fname, fxn, line);
2839 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2840 + dentry, inode, istart, iend);
2842 + if (unlikely(istart != dstart)) {
2843 + PRINT_CALLER(fname, fxn, line);
2844 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2845 + dentry, inode, istart, dstart);
2847 + if (unlikely(iend != dend)) {
2848 + PRINT_CALLER(fname, fxn, line);
2849 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2850 + dentry, inode, iend, dend);
2853 + if (!S_ISDIR(inode->i_mode)) {
2854 + if (unlikely(dend != dstart)) {
2855 + PRINT_CALLER(fname, fxn, line);
2856 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2857 + dentry, inode, dstart, dend);
2859 + if (unlikely(iend != istart)) {
2860 + PRINT_CALLER(fname, fxn, line);
2861 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2862 + dentry, inode, istart, iend);
2866 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2867 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2868 + if (lower_inode) {
2869 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2870 + if (unlikely(bindex < istart || bindex > iend)) {
2871 + PRINT_CALLER(fname, fxn, line);
2872 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2873 + "istart/end=%d:%d\n", dentry,
2874 + lower_inode, bindex, istart, iend);
2875 + } else if (unlikely(lower_inode == poison_ptr)) {
2876 + /* freed inode! */
2877 + PRINT_CALLER(fname, fxn, line);
2878 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2879 + "istart/end=%d:%d\n", dentry,
2880 + lower_inode, bindex, istart, iend);
2884 + /* if we get here, then lower_inode == NULL */
2885 + if (bindex < istart || bindex > iend)
2888 + * directories can have NULL lower inodes in b/t start/end,
2889 + * but NOT if at the start/end range.
2891 + if (unlikely(S_ISDIR(inode->i_mode) &&
2892 + bindex > istart && bindex < iend))
2894 + PRINT_CALLER(fname, fxn, line);
2895 + pr_debug(" CI7: dentry/linode=%p:%p "
2896 + "bindex=%d istart/end=%d:%d\n",
2897 + dentry, lower_inode, bindex, istart, iend);
2901 + * If it's a directory, then intermediate objects b/t start/end can
2902 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2905 + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2906 + for (bindex = dstart+1; bindex < dend; bindex++) {
2907 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2908 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2910 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2911 + if (unlikely(!((lower_inode && lower_dentry &&
2914 + !lower_dentry && !lower_mnt)))) {
2915 + PRINT_CALLER(fname, fxn, line);
2916 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2917 + "bindex=%d dstart/end=%d:%d\n",
2918 + lower_mnt, lower_dentry, lower_inode,
2919 + bindex, dstart, dend);
2922 + /* check if lower inode is newer than upper one (it shouldn't) */
2923 + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
2924 + PRINT_CALLER(fname, fxn, line);
2925 + for (bindex = ibstart(inode); bindex <= ibend(inode);
2927 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2928 + if (unlikely(!lower_inode))
2930 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2931 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
2933 + inode->i_mtime.tv_sec,
2934 + inode->i_mtime.tv_nsec,
2935 + lower_inode->i_mtime.tv_sec,
2936 + lower_inode->i_mtime.tv_nsec,
2937 + inode->i_ctime.tv_sec,
2938 + inode->i_ctime.tv_nsec,
2939 + lower_inode->i_ctime.tv_sec,
2940 + lower_inode->i_ctime.tv_nsec);
2945 +void __unionfs_check_file(const struct file *file,
2946 + const char *fname, const char *fxn, int line)
2949 + int dstart, dend, fstart, fend;
2950 + struct dentry *dentry;
2951 + struct file *lower_file;
2952 + struct inode *inode;
2953 + struct super_block *sb;
2954 + int printed_caller = 0;
2957 + dentry = file->f_path.dentry;
2958 + sb = dentry->d_sb;
2959 + dstart = dbstart(dentry);
2960 + dend = dbend(dentry);
2961 + BUG_ON(dstart > dend);
2962 + fstart = fbstart(file);
2963 + fend = fbend(file);
2964 + BUG_ON(fstart > fend);
2966 + if (unlikely((fstart == -1 && fend != -1) ||
2967 + (fstart != -1 && fend == -1))) {
2968 + PRINT_CALLER(fname, fxn, line);
2969 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
2970 + file, dentry, fstart, fend);
2972 + if (unlikely(fstart != dstart)) {
2973 + PRINT_CALLER(fname, fxn, line);
2974 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
2975 + file, dentry, fstart, dstart);
2977 + if (unlikely(fend != dend)) {
2978 + PRINT_CALLER(fname, fxn, line);
2979 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
2980 + file, dentry, fend, dend);
2982 + inode = dentry->d_inode;
2983 + if (!S_ISDIR(inode->i_mode)) {
2984 + if (unlikely(fend != fstart)) {
2985 + PRINT_CALLER(fname, fxn, line);
2986 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
2987 + file, inode, fstart, fend);
2989 + if (unlikely(dend != dstart)) {
2990 + PRINT_CALLER(fname, fxn, line);
2991 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
2992 + file, dentry, dstart, dend);
2997 + * check for NULL dentries inside the start/end range, or
2998 + * non-NULL dentries outside the start/end range.
3000 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
3001 + lower_file = unionfs_lower_file_idx(file, bindex);
3003 + if (unlikely(bindex < fstart || bindex > fend)) {
3004 + PRINT_CALLER(fname, fxn, line);
3005 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
3006 + "fstart/end=%d:%d\n", file,
3007 + lower_file, bindex, fstart, fend);
3009 + } else { /* lower_file == NULL */
3010 + if (bindex >= fstart && bindex <= fend) {
3012 + * directories can have NULL lower inodes in
3013 + * b/t start/end, but NOT if at the
3014 + * start/end range.
3016 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3017 + bindex > fstart &&
3018 + bindex < fend))) {
3019 + PRINT_CALLER(fname, fxn, line);
3020 + pr_debug(" CF6: file/lower=%p:%p "
3021 + "bindex=%d fstart/end=%d:%d\n",
3022 + file, lower_file, bindex,
3029 + __unionfs_check_dentry(dentry, fname, fxn, line);
3032 +void __unionfs_check_nd(const struct nameidata *nd,
3033 + const char *fname, const char *fxn, int line)
3035 + struct file *file;
3036 + int printed_caller = 0;
3038 + if (unlikely(!nd))
3040 + if (nd->flags & LOOKUP_OPEN) {
3041 + file = nd->intent.open.file;
3042 + if (unlikely(file->f_path.dentry &&
3043 + strcmp(file->f_path.dentry->d_sb->s_type->name,
3045 + PRINT_CALLER(fname, fxn, line);
3046 + pr_debug(" CND1: lower_file of type %s\n",
3047 + file->f_path.dentry->d_sb->s_type->name);
3052 +static unsigned int __mnt_get_count(struct vfsmount *mnt)
3055 + unsigned int count = 0;
3058 + for_each_possible_cpu(cpu) {
3059 + count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
3064 + return mnt->mnt_count;
3068 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3069 +void __show_branch_counts(const struct super_block *sb,
3070 + const char *file, const char *fxn, int line)
3073 + struct vfsmount *mnt;
3076 + for (i = 0; i < sbmax(sb); i++) {
3077 + if (likely(sb->s_root))
3078 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3081 + printk(KERN_CONT "%d:",
3082 + (mnt ? __mnt_get_count(mnt) : -99));
3084 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3087 +void __show_inode_times(const struct inode *inode,
3088 + const char *file, const char *fxn, int line)
3090 + struct inode *lower_inode;
3093 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3094 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3095 + if (unlikely(!lower_inode))
3097 + pr_debug("IT(%lu:%d): %s:%s:%d "
3098 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3099 + inode->i_ino, bindex,
3101 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3102 + lower_inode->i_mtime.tv_sec,
3103 + lower_inode->i_mtime.tv_nsec,
3104 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3105 + lower_inode->i_ctime.tv_sec,
3106 + lower_inode->i_ctime.tv_nsec);
3110 +void __show_dinode_times(const struct dentry *dentry,
3111 + const char *file, const char *fxn, int line)
3113 + struct inode *inode = dentry->d_inode;
3114 + struct inode *lower_inode;
3117 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3118 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3121 + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3122 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3123 + dentry->d_name.name, inode->i_ino, bindex,
3125 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3126 + lower_inode->i_mtime.tv_sec,
3127 + lower_inode->i_mtime.tv_nsec,
3128 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3129 + lower_inode->i_ctime.tv_sec,
3130 + lower_inode->i_ctime.tv_nsec);
3134 +void __show_inode_counts(const struct inode *inode,
3135 + const char *file, const char *fxn, int line)
3137 + struct inode *lower_inode;
3140 + if (unlikely(!inode)) {
3141 + pr_debug("SiC: Null inode\n");
3144 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3146 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3147 + if (unlikely(!lower_inode))
3149 + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3150 + inode->i_ino, bindex,
3151 + atomic_read(&(inode)->i_count),
3152 + atomic_read(&(lower_inode)->i_count),
3156 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
3157 new file mode 100644
3158 index 0000000..c0205a4
3160 +++ b/fs/unionfs/dentry.c
3163 + * Copyright (c) 2003-2011 Erez Zadok
3164 + * Copyright (c) 2003-2006 Charles P. Wright
3165 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3166 + * Copyright (c) 2005-2006 Junjiro Okajima
3167 + * Copyright (c) 2005 Arun M. Krishnakumar
3168 + * Copyright (c) 2004-2006 David P. Quigley
3169 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3170 + * Copyright (c) 2003 Puja Gupta
3171 + * Copyright (c) 2003 Harikesavan Krishnan
3172 + * Copyright (c) 2003-2011 Stony Brook University
3173 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
3175 + * This program is free software; you can redistribute it and/or modify
3176 + * it under the terms of the GNU General Public License version 2 as
3177 + * published by the Free Software Foundation.
3182 +bool is_negative_lower(const struct dentry *dentry)
3185 + struct dentry *lower_dentry;
3188 + /* cache coherency: check if file was deleted on lower branch */
3189 + if (dbstart(dentry) < 0)
3191 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3192 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3193 + /* unhashed (i.e., unlinked) lower dentries don't count */
3194 + if (lower_dentry && lower_dentry->d_inode &&
3195 + !d_deleted(lower_dentry) &&
3196 + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3202 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3204 + struct dentry *lower_dentry;
3209 + for (bindex = start; bindex <= end; bindex++) {
3210 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3211 + if (!lower_dentry)
3213 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3214 + dput(lower_dentry);
3219 + * Purge and invalidate as many data pages of a unionfs inode. This is
3220 + * called when the lower inode has changed, and we want to force processes
3221 + * to re-get the new data.
3223 +static inline void purge_inode_data(struct inode *inode)
3225 + /* remove all non-private mappings */
3226 + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3227 + /* invalidate as many pages as possible */
3228 + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3230 + * Don't try to truncate_inode_pages here, because this could lead
3231 + * to a deadlock between some of address_space ops and dentry
3232 + * revalidation: the address space op is invoked with a lock on our
3233 + * own page, and truncate_inode_pages will block on locked pages.
3238 + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3239 + * of the @dentry and its @parent are locked. Assume parent is valid,
3240 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3241 + * dentry). Returns true if valid, false otherwise.
3243 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3246 + bool valid = true; /* default is valid */
3247 + struct dentry *lower_dentry;
3248 + struct dentry *result;
3249 + int bindex, bstart, bend;
3250 + int sbgen, dgen, pdgen;
3252 + int interpose_flag;
3254 + verify_locked(dentry);
3255 + verify_locked(parent);
3257 + /* if the dentry is unhashed, do NOT revalidate */
3258 + if (d_deleted(dentry))
3261 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3263 + if (is_newer_lower(dentry)) {
3264 + /* root dentry is always valid */
3265 + if (IS_ROOT(dentry)) {
3266 + unionfs_copy_attr_times(dentry->d_inode);
3269 + * reset generation number to zero, guaranteed to be
3273 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3276 + purge_inode_data(dentry->d_inode);
3279 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3281 + BUG_ON(dbstart(dentry) == -1);
3282 + if (dentry->d_inode)
3285 + /* if our dentry is valid, then validate all lower ones */
3286 + if (sbgen == dgen)
3287 + goto validate_lowers;
3289 + /* The root entry should always be valid */
3290 + BUG_ON(IS_ROOT(dentry));
3292 + /* We can't work correctly if our parent isn't valid. */
3293 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3295 + /* Free the pointers for our inodes and this dentry. */
3296 + path_put_lowers_all(dentry, false);
3298 + interpose_flag = INTERPOSE_REVAL_NEG;
3300 + interpose_flag = INTERPOSE_REVAL;
3301 + iput_lowers_all(dentry->d_inode, true);
3304 + if (realloc_dentry_private_data(dentry) != 0) {
3309 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3311 + if (IS_ERR(result)) {
3316 + * current unionfs_lookup_backend() doesn't return
3323 + if (unlikely(positive && is_negative_lower(dentry))) {
3324 + /* call make_bad_inode here ? */
3331 + * if we got here then we have revalidated our dentry and all lower
3332 + * ones, so we can return safely.
3334 + if (!valid) /* lower dentry revalidation failed */
3338 + * If the parent's gen no. matches the superblock's gen no., then
3339 + * we can update our denty's gen no. If they didn't match, then it
3340 + * was OK to revalidate this dentry with a stale parent, but we'll
3341 + * purposely not update our dentry's gen no. (so it can be redone);
3342 + * and, we'll mark our parent dentry as invalid so it'll force it
3343 + * (and our dentry) to be revalidated.
3345 + if (pdgen == sbgen)
3346 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3351 + /* The revalidation must occur across all branches */
3352 + bstart = dbstart(dentry);
3353 + bend = dbend(dentry);
3354 + BUG_ON(bstart == -1);
3355 + for (bindex = bstart; bindex <= bend; bindex++) {
3357 + struct nameidata lower_nd;
3359 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3360 + if (!lower_dentry || !lower_dentry->d_op
3361 + || !lower_dentry->d_op->d_revalidate)
3364 + * Don't pass nameidata to lower file system, because we
3365 + * don't want an arbitrary lower file being opened or
3366 + * returned to us: it may be useless to us because of the
3367 + * fanout nature of unionfs (cf. file/directory open-file
3368 + * invariants). We will open lower files as and when needed
3371 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
3372 + if (unlikely(err < 0)) {
3376 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, &lower_nd))
3378 + release_lower_nd(&lower_nd, err);
3381 + if (!dentry->d_inode ||
3382 + ibstart(dentry->d_inode) < 0 ||
3383 + ibend(dentry->d_inode) < 0) {
3390 + * If we get here, and we copy the meta-data from the lower
3391 + * inode to our inode, then it is vital that we have already
3392 + * purged all unionfs-level file data. We do that in the
3393 + * caller (__unionfs_d_revalidate) by calling
3394 + * purge_inode_data.
3396 + unionfs_copy_attr_all(dentry->d_inode,
3397 + unionfs_lower_inode(dentry->d_inode));
3398 + fsstack_copy_inode_size(dentry->d_inode,
3399 + unionfs_lower_inode(dentry->d_inode));
3407 + * Determine if the lower inode objects have changed from below the unionfs
3408 + * inode. Return true if changed, false otherwise.
3410 + * We check if the mtime or ctime have changed. However, the inode times
3411 + * can be changed by anyone without much protection, including
3412 + * asynchronously. This can sometimes cause unionfs to find that the lower
3413 + * file system doesn't change its inode times quick enough, resulting in a
3414 + * false positive indication (which is harmless, it just makes unionfs do
3415 + * extra work in re-validating the objects). To minimize the chances of
3416 + * these situations, we still consider such small time changes valid, but we
3417 + * don't print debugging messages unless the time changes are greater than
3418 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3419 + * because significant changes are more likely due to users manually
3420 + * touching lower files.
3422 +bool is_newer_lower(const struct dentry *dentry)
3425 + struct inode *inode;
3426 + struct inode *lower_inode;
3428 + /* ignore if we're called on semi-initialized dentries/inodes */
3429 + if (!dentry || !UNIONFS_D(dentry))
3431 + inode = dentry->d_inode;
3432 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3433 + ibstart(inode) < 0 || ibend(inode) < 0)
3436 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3437 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3441 + /* check if mtime/ctime have changed */
3442 + if (unlikely(timespec_compare(&inode->i_mtime,
3443 + &lower_inode->i_mtime) < 0)) {
3444 + if ((lower_inode->i_mtime.tv_sec -
3445 + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3446 + pr_info("unionfs: new lower inode mtime "
3447 + "(bindex=%d, name=%s)\n", bindex,
3448 + dentry->d_name.name);
3449 + show_dinode_times(dentry);
3453 + if (unlikely(timespec_compare(&inode->i_ctime,
3454 + &lower_inode->i_ctime) < 0)) {
3455 + if ((lower_inode->i_ctime.tv_sec -
3456 + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3457 + pr_info("unionfs: new lower inode ctime "
3458 + "(bindex=%d, name=%s)\n", bindex,
3459 + dentry->d_name.name);
3460 + show_dinode_times(dentry);
3467 + * Last check: if this is a positive dentry, but somehow all lower
3468 + * dentries are negative or unhashed, then this dentry needs to be
3469 + * revalidated, because someone probably deleted the objects from
3470 + * the lower branches directly.
3472 + if (is_negative_lower(dentry))
3475 + return false; /* default: lower is not newer */
3478 +static int unionfs_d_revalidate(struct dentry *dentry,
3479 + struct nameidata *nd_unused)
3481 + bool valid = true;
3482 + int err = 1; /* 1 means valid for the VFS */
3483 + struct dentry *parent;
3485 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3486 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3487 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3489 + valid = __unionfs_d_revalidate(dentry, parent, false);
3491 + unionfs_postcopyup_setmnt(dentry);
3492 + unionfs_check_dentry(dentry);
3497 + unionfs_unlock_dentry(dentry);
3498 + unionfs_unlock_parent(dentry, parent);
3499 + unionfs_read_unlock(dentry->d_sb);
3504 +static void unionfs_d_release(struct dentry *dentry)
3506 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3507 + if (unlikely(!UNIONFS_D(dentry)))
3508 + goto out; /* skip if no lower branches */
3509 + /* must lock our branch configuration here */
3510 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3512 + unionfs_check_dentry(dentry);
3513 + /* this could be a negative dentry, so check first */
3514 + if (dbstart(dentry) < 0) {
3515 + unionfs_unlock_dentry(dentry);
3516 + goto out; /* due to a (normal) failed lookup */
3519 + /* Release all the lower dentries */
3520 + path_put_lowers_all(dentry, true);
3522 + unionfs_unlock_dentry(dentry);
3525 + free_dentry_private_data(dentry);
3526 + unionfs_read_unlock(dentry->d_sb);
3531 + * Called when we're removing the last reference to our dentry. So we
3532 + * should drop all lower references too.
3534 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3539 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3540 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3542 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3543 + goto drop_lower_inodes;
3544 + path_put_lowers_all(dentry, false);
3547 + rc = atomic_read(&inode->i_count);
3548 + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3549 + /* see Documentation/filesystems/unionfs/issues.txt */
3551 + iput(unionfs_lower_inode(inode));
3553 + unionfs_set_lower_inode(inode, NULL);
3554 + /* XXX: may need to set start/end to -1? */
3559 + unionfs_unlock_dentry(dentry);
3560 + unionfs_read_unlock(dentry->d_sb);
3563 +struct dentry_operations unionfs_dops = {
3564 + .d_revalidate = unionfs_d_revalidate,
3565 + .d_release = unionfs_d_release,
3566 + .d_iput = unionfs_d_iput,
3568 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3569 new file mode 100644
3570 index 0000000..72a9c1a
3572 +++ b/fs/unionfs/dirfops.c
3575 + * Copyright (c) 2003-2011 Erez Zadok
3576 + * Copyright (c) 2003-2006 Charles P. Wright
3577 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3578 + * Copyright (c) 2005-2006 Junjiro Okajima
3579 + * Copyright (c) 2005 Arun M. Krishnakumar
3580 + * Copyright (c) 2004-2006 David P. Quigley
3581 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3582 + * Copyright (c) 2003 Puja Gupta
3583 + * Copyright (c) 2003 Harikesavan Krishnan
3584 + * Copyright (c) 2003-2011 Stony Brook University
3585 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
3587 + * This program is free software; you can redistribute it and/or modify
3588 + * it under the terms of the GNU General Public License version 2 as
3589 + * published by the Free Software Foundation.
3594 +/* Make sure our rdstate is playing by the rules. */
3595 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3597 + BUG_ON(rdstate->offset >= DIREOF);
3598 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3601 +struct unionfs_getdents_callback {
3602 + struct unionfs_dir_state *rdstate;
3604 + int entries_written;
3605 + int filldir_called;
3606 + int filldir_error;
3607 + filldir_t filldir;
3608 + struct super_block *sb;
3611 +/* based on generic filldir in fs/readir.c */
3612 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3613 + loff_t offset, u64 ino, unsigned int d_type)
3615 + struct unionfs_getdents_callback *buf = dirent;
3616 + struct filldir_node *found = NULL;
3619 + char *name = (char *) oname;
3621 + buf->filldir_called++;
3623 + is_whiteout = is_whiteout_name(&name, &namelen);
3625 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3629 + * If we had non-whiteout entry in dir cache, then mark it
3630 + * as a whiteout and but leave it in the dir cache.
3632 + if (is_whiteout && !found->whiteout)
3633 + found->whiteout = is_whiteout;
3637 + /* if 'name' isn't a whiteout, filldir it. */
3638 + if (!is_whiteout) {
3639 + off_t pos = rdstate2offset(buf->rdstate);
3640 + u64 unionfs_ino = ino;
3642 + err = buf->filldir(buf->dirent, name, namelen, pos,
3643 + unionfs_ino, d_type);
3644 + buf->rdstate->offset++;
3645 + verify_rdstate_offset(buf->rdstate);
3648 + * If we did fill it, stuff it in our hash, otherwise return an
3652 + buf->filldir_error = err;
3655 + buf->entries_written++;
3656 + err = add_filldir_node(buf->rdstate, name, namelen,
3657 + buf->rdstate->bindex, is_whiteout);
3659 + buf->filldir_error = err;
3665 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3668 + struct file *lower_file = NULL;
3669 + struct dentry *dentry = file->f_path.dentry;
3670 + struct dentry *parent;
3671 + struct inode *inode = NULL;
3672 + struct unionfs_getdents_callback buf;
3673 + struct unionfs_dir_state *uds;
3677 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3678 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3679 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3681 + err = unionfs_file_revalidate(file, parent, false);
3682 + if (unlikely(err))
3685 + inode = dentry->d_inode;
3687 + uds = UNIONFS_F(file)->rdstate;
3689 + if (file->f_pos == DIREOF) {
3691 + } else if (file->f_pos > 0) {
3692 + uds = find_rdstate(inode, file->f_pos);
3693 + if (unlikely(!uds)) {
3697 + UNIONFS_F(file)->rdstate = uds;
3699 + init_rdstate(file);
3700 + uds = UNIONFS_F(file)->rdstate;
3703 + bend = fbend(file);
3705 + while (uds->bindex <= bend) {
3706 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3707 + if (!lower_file) {
3713 + /* prepare callback buffer */
3714 + buf.filldir_called = 0;
3715 + buf.filldir_error = 0;
3716 + buf.entries_written = 0;
3717 + buf.dirent = dirent;
3718 + buf.filldir = filldir;
3719 + buf.rdstate = uds;
3720 + buf.sb = inode->i_sb;
3722 + /* Read starting from where we last left off. */
3723 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3728 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3730 + /* Save the position for when we continue. */
3731 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3736 + uds->dirpos = offset;
3738 + /* Copy the atime. */
3739 + fsstack_copy_attr_atime(inode,
3740 + lower_file->f_path.dentry->d_inode);
3745 + if (buf.filldir_error)
3748 + if (!buf.entries_written) {
3754 + if (!buf.filldir_error && uds->bindex >= bend) {
3755 + /* Save the number of hash entries for next time. */
3756 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3757 + free_rdstate(uds);
3758 + UNIONFS_F(file)->rdstate = NULL;
3759 + file->f_pos = DIREOF;
3761 + file->f_pos = rdstate2offset(uds);
3766 + unionfs_check_file(file);
3767 + unionfs_unlock_dentry(dentry);
3768 + unionfs_unlock_parent(dentry, parent);
3769 + unionfs_read_unlock(dentry->d_sb);
3774 + * This is not meant to be a generic repositioning function. If you do
3775 + * things that aren't supported, then we return EINVAL.
3777 + * What is allowed:
3778 + * (1) seeking to the same position that you are currently at
3779 + * This really has no effect, but returns where you are.
3780 + * (2) seeking to the beginning of the file
3781 + * This throws out all state, and lets you begin again.
3783 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3785 + struct unionfs_dir_state *rdstate;
3786 + struct dentry *dentry = file->f_path.dentry;
3787 + struct dentry *parent;
3790 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3791 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3792 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3794 + err = unionfs_file_revalidate(file, parent, false);
3795 + if (unlikely(err))
3798 + rdstate = UNIONFS_F(file)->rdstate;
3801 + * we let users seek to their current position, but not anywhere
3808 + free_rdstate(rdstate);
3809 + UNIONFS_F(file)->rdstate = NULL;
3811 + init_rdstate(file);
3815 + err = file->f_pos;
3818 + /* Unsupported, because we would break everything. */
3826 + if (offset == rdstate2offset(rdstate))
3828 + else if (file->f_pos == DIREOF)
3833 + struct inode *inode;
3834 + inode = dentry->d_inode;
3835 + rdstate = find_rdstate(inode, offset);
3837 + UNIONFS_F(file)->rdstate = rdstate;
3838 + err = rdstate->offset;
3846 + /* Unsupported, because we would break everything. */
3854 + unionfs_check_file(file);
3855 + unionfs_unlock_dentry(dentry);
3856 + unionfs_unlock_parent(dentry, parent);
3857 + unionfs_read_unlock(dentry->d_sb);
3862 + * Trimmed directory options, we shouldn't pass everything down since
3863 + * we don't want to operate on partial directories.
3865 +struct file_operations unionfs_dir_fops = {
3866 + .llseek = unionfs_dir_llseek,
3867 + .read = generic_read_dir,
3868 + .readdir = unionfs_readdir,
3869 + .unlocked_ioctl = unionfs_ioctl,
3870 + .open = unionfs_open,
3871 + .release = unionfs_file_release,
3872 + .flush = unionfs_flush,
3873 + .fsync = unionfs_fsync,
3874 + .fasync = unionfs_fasync,
3876 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3877 new file mode 100644
3878 index 0000000..62ec9af
3880 +++ b/fs/unionfs/dirhelper.c
3883 + * Copyright (c) 2003-2011 Erez Zadok
3884 + * Copyright (c) 2003-2006 Charles P. Wright
3885 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3886 + * Copyright (c) 2005-2006 Junjiro Okajima
3887 + * Copyright (c) 2005 Arun M. Krishnakumar
3888 + * Copyright (c) 2004-2006 David P. Quigley
3889 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3890 + * Copyright (c) 2003 Puja Gupta
3891 + * Copyright (c) 2003 Harikesavan Krishnan
3892 + * Copyright (c) 2003-2011 Stony Brook University
3893 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
3895 + * This program is free software; you can redistribute it and/or modify
3896 + * it under the terms of the GNU General Public License version 2 as
3897 + * published by the Free Software Foundation.
3903 +#define RD_CHECK_EMPTY 1
3904 +/* The callback structure for check_empty. */
3905 +struct unionfs_rdutil_callback {
3907 + int filldir_called;
3908 + struct unionfs_dir_state *rdstate;
3912 +/* This filldir function makes sure only whiteouts exist within a directory. */
3913 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3914 + loff_t offset, u64 ino, unsigned int d_type)
3917 + struct unionfs_rdutil_callback *buf = dirent;
3919 + struct filldir_node *found;
3920 + char *name = (char *) oname;
3922 + buf->filldir_called = 1;
3924 + if (name[0] == '.' && (namelen == 1 ||
3925 + (name[1] == '.' && namelen == 2)))
3928 + is_whiteout = is_whiteout_name(&name, &namelen);
3930 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3931 + /* If it was found in the table there was a previous whiteout. */
3936 + * if it wasn't found and isn't a whiteout, the directory isn't
3940 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3943 + err = add_filldir_node(buf->rdstate, name, namelen,
3944 + buf->rdstate->bindex, is_whiteout);
3951 +/* Is a directory logically empty? */
3952 +int check_empty(struct dentry *dentry, struct dentry *parent,
3953 + struct unionfs_dir_state **namelist)
3956 + struct dentry *lower_dentry = NULL;
3957 + struct vfsmount *mnt;
3958 + struct super_block *sb;
3959 + struct file *lower_file;
3960 + struct unionfs_rdutil_callback *buf = NULL;
3961 + int bindex, bstart, bend, bopaque;
3963 + sb = dentry->d_sb;
3966 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3968 + err = unionfs_partial_lookup(dentry, parent);
3972 + bstart = dbstart(dentry);
3973 + bend = dbend(dentry);
3974 + bopaque = dbopaque(dentry);
3975 + if (0 <= bopaque && bopaque < bend)
3978 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3979 + if (unlikely(!buf)) {
3984 + buf->mode = RD_CHECK_EMPTY;
3985 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3986 + if (unlikely(!buf->rdstate)) {
3991 + /* Process the lower directories with rdutil_callback as a filldir. */
3992 + for (bindex = bstart; bindex <= bend; bindex++) {
3993 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3994 + if (!lower_dentry)
3996 + if (!lower_dentry->d_inode)
3998 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
4001 + dget(lower_dentry);
4002 + mnt = unionfs_mntget(dentry, bindex);
4003 + branchget(sb, bindex);
4004 + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
4005 + if (IS_ERR(lower_file)) {
4006 + err = PTR_ERR(lower_file);
4007 + branchput(sb, bindex);
4012 + buf->filldir_called = 0;
4013 + buf->rdstate->bindex = bindex;
4014 + err = vfs_readdir(lower_file,
4015 + readdir_util_callback, buf);
4018 + } while ((err >= 0) && buf->filldir_called);
4020 + /* fput calls dput for lower_dentry */
4022 + branchput(sb, bindex);
4030 + if (namelist && !err)
4031 + *namelist = buf->rdstate;
4032 + else if (buf->rdstate)
4033 + free_rdstate(buf->rdstate);
4040 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4041 new file mode 100644
4042 index 0000000..ae1b86a
4044 +++ b/fs/unionfs/fanout.h
4047 + * Copyright (c) 2003-2011 Erez Zadok
4048 + * Copyright (c) 2003-2006 Charles P. Wright
4049 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4050 + * Copyright (c) 2005 Arun M. Krishnakumar
4051 + * Copyright (c) 2004-2006 David P. Quigley
4052 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4053 + * Copyright (c) 2003 Puja Gupta
4054 + * Copyright (c) 2003 Harikesavan Krishnan
4055 + * Copyright (c) 2003-2011 Stony Brook University
4056 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
4058 + * This program is free software; you can redistribute it and/or modify
4059 + * it under the terms of the GNU General Public License version 2 as
4060 + * published by the Free Software Foundation.
4067 + * Inode to private data
4069 + * Since we use containers and the struct inode is _inside_ the
4070 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4071 + * inode pointer), return a valid non-NULL pointer.
4073 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4075 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4078 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4079 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4081 +/* Dentry to private data */
4082 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4083 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4084 +#define dbend(dent) (UNIONFS_D(dent)->bend)
4085 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4087 +/* Superblock to private data */
4088 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4089 +#define sbstart(sb) 0
4090 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4091 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4092 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4094 +/* File to private Data */
4095 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4096 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4097 +#define fbend(file) (UNIONFS_F(file)->bend)
4099 +/* macros to manipulate branch IDs in stored in our superblock */
4100 +static inline int branch_id(struct super_block *sb, int index)
4102 + BUG_ON(!sb || index < 0);
4103 + return UNIONFS_SB(sb)->data[index].branch_id;
4106 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4108 + BUG_ON(!sb || index < 0);
4109 + UNIONFS_SB(sb)->data[index].branch_id = val;
4112 +static inline void new_branch_id(struct super_block *sb, int index)
4114 + BUG_ON(!sb || index < 0);
4115 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4119 + * Find new index of matching branch with an existing superblock of a known
4120 + * (possibly old) id. This is needed because branches could have been
4121 + * added/deleted causing the branches of any open files to shift.
4123 + * @sb: the new superblock which may have new/different branch IDs
4124 + * @id: the old/existing id we're looking for
4125 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4127 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4130 + for (i = 0; i < sbmax(sb); i++) {
4131 + if (branch_id(sb, i) == id)
4134 + /* in the non-ODF code, this should really never happen */
4135 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4139 +/* File to lower file. */
4140 +static inline struct file *unionfs_lower_file(const struct file *f)
4143 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4146 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4149 + BUG_ON(!f || index < 0);
4150 + return UNIONFS_F(f)->lower_files[index];
4153 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4156 + BUG_ON(!f || index < 0);
4157 + UNIONFS_F(f)->lower_files[index] = val;
4158 + /* save branch ID (may be redundant?) */
4159 + UNIONFS_F(f)->saved_branch_ids[index] =
4160 + branch_id((f)->f_path.dentry->d_sb, index);
4163 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4166 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4169 +/* Inode to lower inode. */
4170 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4173 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4176 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4179 + BUG_ON(!i || index < 0);
4180 + return UNIONFS_I(i)->lower_inodes[index];
4183 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4184 + struct inode *val)
4186 + BUG_ON(!i || index < 0);
4187 + UNIONFS_I(i)->lower_inodes[index] = val;
4190 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4193 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4196 +/* Superblock to lower superblock. */
4197 +static inline struct super_block *unionfs_lower_super(
4198 + const struct super_block *sb)
4201 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4204 +static inline struct super_block *unionfs_lower_super_idx(
4205 + const struct super_block *sb,
4208 + BUG_ON(!sb || index < 0);
4209 + return UNIONFS_SB(sb)->data[index].sb;
4212 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4214 + struct super_block *val)
4216 + BUG_ON(!sb || index < 0);
4217 + UNIONFS_SB(sb)->data[index].sb = val;
4220 +static inline void unionfs_set_lower_super(struct super_block *sb,
4221 + struct super_block *val)
4224 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4227 +/* Branch count macros. */
4228 +static inline int branch_count(const struct super_block *sb, int index)
4230 + BUG_ON(!sb || index < 0);
4231 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4234 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4236 + BUG_ON(!sb || index < 0);
4237 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4240 +static inline void branchget(struct super_block *sb, int index)
4242 + BUG_ON(!sb || index < 0);
4243 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4246 +static inline void branchput(struct super_block *sb, int index)
4248 + BUG_ON(!sb || index < 0);
4249 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4252 +/* Dentry macros */
4253 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4254 + struct dentry *val)
4256 + BUG_ON(!dent || index < 0);
4257 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4260 +static inline struct dentry *unionfs_lower_dentry_idx(
4261 + const struct dentry *dent,
4264 + BUG_ON(!dent || index < 0);
4265 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4268 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4271 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4274 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4275 + struct vfsmount *mnt)
4277 + BUG_ON(!dent || index < 0);
4278 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4281 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4282 + const struct dentry *dent,
4285 + BUG_ON(!dent || index < 0);
4286 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4289 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4292 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4295 +/* Macros for locking a dentry. */
4296 +enum unionfs_dentry_lock_class {
4297 + UNIONFS_DMUTEX_NORMAL,
4298 + UNIONFS_DMUTEX_ROOT,
4299 + UNIONFS_DMUTEX_PARENT,
4300 + UNIONFS_DMUTEX_CHILD,
4301 + UNIONFS_DMUTEX_WHITEOUT,
4302 + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4303 + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4306 +static inline void unionfs_lock_dentry(struct dentry *d,
4307 + unsigned int subclass)
4310 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4313 +static inline void unionfs_unlock_dentry(struct dentry *d)
4316 + mutex_unlock(&UNIONFS_D(d)->lock);
4319 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4320 + unsigned int subclass)
4325 + p = dget_parent(d);
4327 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4331 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4336 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4337 + mutex_unlock(&UNIONFS_D(p)->lock);
4342 +static inline void verify_locked(struct dentry *d)
4345 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4348 +/* macros to put lower objects */
4351 + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4352 + * @free_lower is true, then also kfree the memory used to hold the lower
4353 + * object pointers.
4355 +static inline void iput_lowers(struct inode *inode,
4356 + int bstart, int bend, bool free_lower)
4358 + struct inode *lower_inode;
4362 + BUG_ON(!UNIONFS_I(inode));
4363 + BUG_ON(bstart < 0);
4365 + for (bindex = bstart; bindex <= bend; bindex++) {
4366 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4367 + if (lower_inode) {
4368 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4369 + /* see Documentation/filesystems/unionfs/issues.txt */
4371 + iput(lower_inode);
4377 + kfree(UNIONFS_I(inode)->lower_inodes);
4378 + UNIONFS_I(inode)->lower_inodes = NULL;
4382 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4383 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4388 + BUG_ON(!UNIONFS_I(inode));
4389 + bstart = ibstart(inode);
4390 + bend = ibend(inode);
4391 + BUG_ON(bstart < 0);
4393 + iput_lowers(inode, bstart, bend, free_lower);
4394 + ibstart(inode) = ibend(inode) = -1;
4398 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4399 + * bstart to bend. If @free_lower is true, then also kfree the memory used
4400 + * to hold the lower object pointers.
4402 + * XXX: implement using path_put VFS macros
4404 +static inline void path_put_lowers(struct dentry *dentry,
4405 + int bstart, int bend, bool free_lower)
4407 + struct dentry *lower_dentry;
4408 + struct vfsmount *lower_mnt;
4412 + BUG_ON(!UNIONFS_D(dentry));
4413 + BUG_ON(bstart < 0);
4415 + for (bindex = bstart; bindex <= bend; bindex++) {
4416 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4417 + if (lower_dentry) {
4418 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4419 + dput(lower_dentry);
4421 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4423 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4424 + mntput(lower_mnt);
4429 + kfree(UNIONFS_D(dentry)->lower_paths);
4430 + UNIONFS_D(dentry)->lower_paths = NULL;
4435 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4438 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4443 + BUG_ON(!UNIONFS_D(dentry));
4444 + bstart = dbstart(dentry);
4445 + bend = dbend(dentry);
4446 + BUG_ON(bstart < 0);
4448 + path_put_lowers(dentry, bstart, bend, free_lower);
4449 + dbstart(dentry) = dbend(dentry) = -1;
4452 +#endif /* not _FANOUT_H */
4453 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4454 new file mode 100644
4455 index 0000000..416c52f
4457 +++ b/fs/unionfs/file.c
4460 + * Copyright (c) 2003-2011 Erez Zadok
4461 + * Copyright (c) 2003-2006 Charles P. Wright
4462 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4463 + * Copyright (c) 2005-2006 Junjiro Okajima
4464 + * Copyright (c) 2005 Arun M. Krishnakumar
4465 + * Copyright (c) 2004-2006 David P. Quigley
4466 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4467 + * Copyright (c) 2003 Puja Gupta
4468 + * Copyright (c) 2003 Harikesavan Krishnan
4469 + * Copyright (c) 2003-2011 Stony Brook University
4470 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
4472 + * This program is free software; you can redistribute it and/or modify
4473 + * it under the terms of the GNU General Public License version 2 as
4474 + * published by the Free Software Foundation.
4479 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4480 + size_t count, loff_t *ppos)
4483 + struct file *lower_file;
4484 + struct dentry *dentry = file->f_path.dentry;
4485 + struct dentry *parent;
4487 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4488 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4489 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4491 + err = unionfs_file_revalidate(file, parent, false);
4492 + if (unlikely(err))
4495 + lower_file = unionfs_lower_file(file);
4496 + err = vfs_read(lower_file, buf, count, ppos);
4497 + /* update our inode atime upon a successful lower read */
4499 + fsstack_copy_attr_atime(dentry->d_inode,
4500 + lower_file->f_path.dentry->d_inode);
4501 + unionfs_check_file(file);
4505 + unionfs_unlock_dentry(dentry);
4506 + unionfs_unlock_parent(dentry, parent);
4507 + unionfs_read_unlock(dentry->d_sb);
4511 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4512 + size_t count, loff_t *ppos)
4515 + struct file *lower_file;
4516 + struct dentry *dentry = file->f_path.dentry;
4517 + struct dentry *parent;
4519 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4520 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4521 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4523 + err = unionfs_file_revalidate(file, parent, true);
4524 + if (unlikely(err))
4527 + lower_file = unionfs_lower_file(file);
4528 + err = vfs_write(lower_file, buf, count, ppos);
4529 + /* update our inode times+sizes upon a successful lower write */
4531 + fsstack_copy_inode_size(dentry->d_inode,
4532 + lower_file->f_path.dentry->d_inode);
4533 + fsstack_copy_attr_times(dentry->d_inode,
4534 + lower_file->f_path.dentry->d_inode);
4535 + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4536 + unionfs_check_file(file);
4540 + unionfs_unlock_dentry(dentry);
4541 + unionfs_unlock_parent(dentry, parent);
4542 + unionfs_read_unlock(dentry->d_sb);
4546 +static int unionfs_file_readdir(struct file *file, void *dirent,
4547 + filldir_t filldir)
4552 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4556 + struct file *lower_file;
4557 + struct dentry *dentry = file->f_path.dentry;
4558 + struct dentry *parent;
4559 + const struct vm_operations_struct *saved_vm_ops = NULL;
4562 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4563 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4564 + * has been causing false positives in file system stacking layers.
4565 + * In particular, our ->mmap is called after sys_mmap2 already holds
4566 + * mmap_sem, then we lock our own mutexes; but earlier, it's
4567 + * possible for lockdep to have locked our mutexes first, and then
4568 + * we call a lower ->readdir which could call might_fault. The
4569 + * different ordering of the locks is what lockdep complains about
4570 + * -- unnecessarily. Therefore, we have no choice but to tell
4571 + * lockdep to temporarily turn off lockdep here. Note: the comments
4572 + * inside might_sleep also suggest that it would have been
4573 + * nicer to only annotate paths that needs that might_lock_read.
4576 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4577 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4578 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4580 + /* This might be deferred to mmap's writepage */
4581 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4582 + err = unionfs_file_revalidate(file, parent, willwrite);
4583 + if (unlikely(err))
4585 + unionfs_check_file(file);
4588 + * File systems which do not implement ->writepage may use
4589 + * generic_file_readonly_mmap as their ->mmap op. If you call
4590 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4591 + * But we cannot call the lower ->mmap op, so we can't tell that
4592 + * writeable mappings won't work. Therefore, our only choice is to
4593 + * check if the lower file system supports the ->writepage, and if
4594 + * not, return EINVAL (the same error that
4595 + * generic_file_readonly_mmap returns in that case).
4597 + lower_file = unionfs_lower_file(file);
4598 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4600 + printk(KERN_ERR "unionfs: branch %d file system does not "
4601 + "support writeable mmap\n", fbstart(file));
4606 + * find and save lower vm_ops.
4608 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4610 + if (!UNIONFS_F(file)->lower_vm_ops) {
4611 + err = lower_file->f_op->mmap(lower_file, vma);
4613 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4616 + saved_vm_ops = vma->vm_ops;
4617 + err = do_munmap(current->mm, vma->vm_start,
4618 + vma->vm_end - vma->vm_start);
4620 + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4625 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4626 + err = generic_file_mmap(file, vma);
4627 + file->f_mapping->a_ops = &unionfs_aops;
4629 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4632 + vma->vm_ops = &unionfs_vm_ops;
4633 + if (!UNIONFS_F(file)->lower_vm_ops)
4634 + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4638 + /* copyup could cause parent dir times to change */
4639 + unionfs_copy_attr_times(parent->d_inode);
4640 + unionfs_check_file(file);
4642 + unionfs_unlock_dentry(dentry);
4643 + unionfs_unlock_parent(dentry, parent);
4644 + unionfs_read_unlock(dentry->d_sb);
4649 +int unionfs_fsync(struct file *file, int datasync)
4651 + int bindex, bstart, bend;
4652 + struct file *lower_file;
4653 + struct dentry *dentry = file->f_path.dentry;
4654 + struct dentry *lower_dentry;
4655 + struct dentry *parent;
4656 + struct inode *lower_inode, *inode;
4657 + int err = -EINVAL;
4659 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4660 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4661 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4663 + err = unionfs_file_revalidate(file, parent, true);
4664 + if (unlikely(err))
4666 + unionfs_check_file(file);
4668 + bstart = fbstart(file);
4669 + bend = fbend(file);
4670 + if (bstart < 0 || bend < 0)
4673 + inode = dentry->d_inode;
4674 + if (unlikely(!inode)) {
4676 + "unionfs: null lower inode in unionfs_fsync\n");
4679 + for (bindex = bstart; bindex <= bend; bindex++) {
4680 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4681 + if (!lower_inode || !lower_inode->i_fop->fsync)
4683 + lower_file = unionfs_lower_file_idx(file, bindex);
4684 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4685 + mutex_lock(&lower_inode->i_mutex);
4686 + err = lower_inode->i_fop->fsync(lower_file, datasync);
4687 + if (!err && bindex == bstart)
4688 + fsstack_copy_attr_times(inode, lower_inode);
4689 + mutex_unlock(&lower_inode->i_mutex);
4696 + unionfs_check_file(file);
4697 + unionfs_unlock_dentry(dentry);
4698 + unionfs_unlock_parent(dentry, parent);
4699 + unionfs_read_unlock(dentry->d_sb);
4703 +int unionfs_fasync(int fd, struct file *file, int flag)
4705 + int bindex, bstart, bend;
4706 + struct file *lower_file;
4707 + struct dentry *dentry = file->f_path.dentry;
4708 + struct dentry *parent;
4709 + struct inode *lower_inode, *inode;
4712 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4713 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4714 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4716 + err = unionfs_file_revalidate(file, parent, true);
4717 + if (unlikely(err))
4719 + unionfs_check_file(file);
4721 + bstart = fbstart(file);
4722 + bend = fbend(file);
4723 + if (bstart < 0 || bend < 0)
4726 + inode = dentry->d_inode;
4727 + if (unlikely(!inode)) {
4729 + "unionfs: null lower inode in unionfs_fasync\n");
4732 + for (bindex = bstart; bindex <= bend; bindex++) {
4733 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4734 + if (!lower_inode || !lower_inode->i_fop->fasync)
4736 + lower_file = unionfs_lower_file_idx(file, bindex);
4737 + mutex_lock(&lower_inode->i_mutex);
4738 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4739 + if (!err && bindex == bstart)
4740 + fsstack_copy_attr_times(inode, lower_inode);
4741 + mutex_unlock(&lower_inode->i_mutex);
4748 + unionfs_check_file(file);
4749 + unionfs_unlock_dentry(dentry);
4750 + unionfs_unlock_parent(dentry, parent);
4751 + unionfs_read_unlock(dentry->d_sb);
4755 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4756 + struct pipe_inode_info *pipe, size_t len,
4757 + unsigned int flags)
4760 + struct file *lower_file;
4761 + struct dentry *dentry = file->f_path.dentry;
4762 + struct dentry *parent;
4764 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4765 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4766 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4768 + err = unionfs_file_revalidate(file, parent, false);
4769 + if (unlikely(err))
4772 + lower_file = unionfs_lower_file(file);
4773 + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4774 + /* update our inode atime upon a successful lower splice-read */
4776 + fsstack_copy_attr_atime(dentry->d_inode,
4777 + lower_file->f_path.dentry->d_inode);
4778 + unionfs_check_file(file);
4782 + unionfs_unlock_dentry(dentry);
4783 + unionfs_unlock_parent(dentry, parent);
4784 + unionfs_read_unlock(dentry->d_sb);
4788 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4789 + struct file *file, loff_t *ppos,
4790 + size_t len, unsigned int flags)
4793 + struct file *lower_file;
4794 + struct dentry *dentry = file->f_path.dentry;
4795 + struct dentry *parent;
4797 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4798 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4799 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4801 + err = unionfs_file_revalidate(file, parent, true);
4802 + if (unlikely(err))
4805 + lower_file = unionfs_lower_file(file);
4806 + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4807 + /* update our inode times+sizes upon a successful lower write */
4809 + fsstack_copy_inode_size(dentry->d_inode,
4810 + lower_file->f_path.dentry->d_inode);
4811 + fsstack_copy_attr_times(dentry->d_inode,
4812 + lower_file->f_path.dentry->d_inode);
4813 + unionfs_check_file(file);
4817 + unionfs_unlock_dentry(dentry);
4818 + unionfs_unlock_parent(dentry, parent);
4819 + unionfs_read_unlock(dentry->d_sb);
4823 +struct file_operations unionfs_main_fops = {
4824 + .llseek = generic_file_llseek,
4825 + .read = unionfs_read,
4826 + .write = unionfs_write,
4827 + .readdir = unionfs_file_readdir,
4828 + .unlocked_ioctl = unionfs_ioctl,
4829 +#ifdef CONFIG_COMPAT
4830 + .compat_ioctl = unionfs_ioctl,
4832 + .mmap = unionfs_mmap,
4833 + .open = unionfs_open,
4834 + .flush = unionfs_flush,
4835 + .release = unionfs_file_release,
4836 + .fsync = unionfs_fsync,
4837 + .fasync = unionfs_fasync,
4838 + .splice_read = unionfs_splice_read,
4839 + .splice_write = unionfs_splice_write,
4841 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4842 new file mode 100644
4843 index 0000000..b207c13
4845 +++ b/fs/unionfs/inode.c
4848 + * Copyright (c) 2003-2011 Erez Zadok
4849 + * Copyright (c) 2003-2006 Charles P. Wright
4850 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4851 + * Copyright (c) 2005-2006 Junjiro Okajima
4852 + * Copyright (c) 2005 Arun M. Krishnakumar
4853 + * Copyright (c) 2004-2006 David P. Quigley
4854 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4855 + * Copyright (c) 2003 Puja Gupta
4856 + * Copyright (c) 2003 Harikesavan Krishnan
4857 + * Copyright (c) 2003-2011 Stony Brook University
4858 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
4860 + * This program is free software; you can redistribute it and/or modify
4861 + * it under the terms of the GNU General Public License version 2 as
4862 + * published by the Free Software Foundation.
4868 + * Find a writeable branch to create new object in. Checks all writeble
4869 + * branches of the parent inode, from istart to iend order; if none are
4870 + * suitable, also tries branch 0 (which may require a copyup).
4872 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4874 +static struct dentry *find_writeable_branch(struct inode *parent,
4875 + struct dentry *dentry)
4877 + int err = -EINVAL;
4878 + int bindex, istart, iend;
4879 + struct dentry *lower_dentry = NULL;
4881 + istart = ibstart(parent);
4882 + iend = ibend(parent);
4887 + for (bindex = istart; bindex <= iend; bindex++) {
4888 + /* skip non-writeable branches */
4889 + err = is_robranch_super(dentry->d_sb, bindex);
4894 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4895 + if (!lower_dentry)
4898 + * check for whiteouts in writeable branch, and remove them
4901 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4902 + if (err > 0) /* ignore if whiteout found and removed */
4906 + /* if get here, we can write to the branch */
4910 + * If istart wasn't already branch 0, and we got any error, then try
4911 + * branch 0 (which may require copyup)
4913 + if (err && istart > 0) {
4914 + istart = iend = 0;
4919 + * If we tried even branch 0, and still got an error, abort. But if
4920 + * the error was an EROFS, then we should try to copyup.
4922 + if (err && err != -EROFS)
4926 + * If we get here, then check if copyup needed. If lower_dentry is
4927 + * NULL, create the entire dentry directory structure in branch 0.
4929 + if (!lower_dentry) {
4931 + lower_dentry = create_parents(parent, dentry,
4932 + dentry->d_name.name, bindex);
4933 + if (IS_ERR(lower_dentry)) {
4934 + err = PTR_ERR(lower_dentry);
4938 + err = 0; /* all's well */
4941 + return ERR_PTR(err);
4942 + return lower_dentry;
4945 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4946 + int mode, struct nameidata *nd_unused)
4949 + struct dentry *lower_dentry = NULL;
4950 + struct dentry *lower_parent_dentry = NULL;
4951 + struct dentry *parent;
4953 + struct nameidata lower_nd;
4955 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4956 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4957 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4959 + valid = __unionfs_d_revalidate(dentry, parent, false);
4960 + if (unlikely(!valid)) {
4961 + err = -ESTALE; /* same as what real_lookup does */
4965 + lower_dentry = find_writeable_branch(dir, dentry);
4966 + if (IS_ERR(lower_dentry)) {
4967 + err = PTR_ERR(lower_dentry);
4971 + lower_parent_dentry = lock_parent(lower_dentry);
4972 + if (IS_ERR(lower_parent_dentry)) {
4973 + err = PTR_ERR(lower_parent_dentry);
4977 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4978 + if (unlikely(err < 0))
4980 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4982 + release_lower_nd(&lower_nd, err);
4985 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
4987 + unionfs_copy_attr_times(dir);
4988 + fsstack_copy_inode_size(dir,
4989 + lower_parent_dentry->d_inode);
4990 + /* update no. of links on parent directory */
4991 + dir->i_nlink = unionfs_get_nlinks(dir);
4996 + unlock_dir(lower_parent_dentry);
4999 + unionfs_postcopyup_setmnt(dentry);
5000 + unionfs_check_inode(dir);
5001 + unionfs_check_dentry(dentry);
5003 + unionfs_unlock_dentry(dentry);
5004 + unionfs_unlock_parent(dentry, parent);
5005 + unionfs_read_unlock(dentry->d_sb);
5010 + * unionfs_lookup is the only special function which takes a dentry, yet we
5011 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
5012 + * we don't have a valid dentry here yet.
5014 +static struct dentry *unionfs_lookup(struct inode *dir,
5015 + struct dentry *dentry,
5016 + struct nameidata *nd_unused)
5018 + struct dentry *ret, *parent;
5021 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5022 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5025 + * As long as we lock/dget the parent, then can skip validating the
5026 + * parent now; we may have to rebuild this dentry on the next
5027 + * ->d_revalidate, however.
5030 + /* allocate dentry private data. We free it in ->d_release */
5031 + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
5032 + if (unlikely(err)) {
5033 + ret = ERR_PTR(err);
5037 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
5039 + if (!IS_ERR(ret)) {
5042 + /* lookup_full can return multiple positive dentries */
5043 + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5044 + BUG_ON(dbstart(dentry) < 0);
5045 + unionfs_postcopyup_release(dentry);
5047 + unionfs_copy_attr_times(dentry->d_inode);
5050 + unionfs_check_inode(dir);
5052 + unionfs_check_dentry(dentry);
5053 + unionfs_check_dentry(parent);
5054 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5057 + unionfs_unlock_parent(dentry, parent);
5058 + unionfs_read_unlock(dentry->d_sb);
5063 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5064 + struct dentry *new_dentry)
5067 + struct dentry *lower_old_dentry = NULL;
5068 + struct dentry *lower_new_dentry = NULL;
5069 + struct dentry *lower_dir_dentry = NULL;
5070 + struct dentry *old_parent, *new_parent;
5071 + char *name = NULL;
5074 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5075 + old_parent = dget_parent(old_dentry);
5076 + new_parent = dget_parent(new_dentry);
5077 + unionfs_double_lock_parents(old_parent, new_parent);
5078 + unionfs_double_lock_dentry(old_dentry, new_dentry);
5080 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5081 + if (unlikely(!valid)) {
5085 + if (new_dentry->d_inode) {
5086 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5087 + if (unlikely(!valid)) {
5093 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5095 + /* check for a whiteout in new dentry branch, and delete it */
5096 + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5097 + dbstart(new_dentry));
5098 + if (err > 0) { /* whiteout found and removed successfully */
5099 + lower_dir_dentry = dget_parent(lower_new_dentry);
5100 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5101 + dput(lower_dir_dentry);
5102 + dir->i_nlink = unionfs_get_nlinks(dir);
5108 + /* check if parent hierachy is needed, then link in same branch */
5109 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5110 + lower_new_dentry = create_parents(dir, new_dentry,
5111 + new_dentry->d_name.name,
5112 + dbstart(old_dentry));
5113 + err = PTR_ERR(lower_new_dentry);
5114 + if (IS_COPYUP_ERR(err))
5116 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5119 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5120 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5122 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5123 + lower_dir_dentry = lock_parent(lower_new_dentry);
5124 + err = is_robranch(old_dentry);
5126 + /* see Documentation/filesystems/unionfs/issues.txt */
5128 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5129 + lower_new_dentry);
5132 + unlock_dir(lower_dir_dentry);
5135 + if (IS_COPYUP_ERR(err)) {
5136 + int old_bstart = dbstart(old_dentry);
5139 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5140 + err = copyup_dentry(old_parent->d_inode,
5141 + old_dentry, old_bstart,
5142 + bindex, old_dentry->d_name.name,
5143 + old_dentry->d_name.len, NULL,
5144 + i_size_read(old_dentry->d_inode));
5147 + lower_new_dentry =
5148 + create_parents(dir, new_dentry,
5149 + new_dentry->d_name.name,
5151 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5152 + lower_dir_dentry = lock_parent(lower_new_dentry);
5153 + /* see Documentation/filesystems/unionfs/issues.txt */
5156 + err = vfs_link(lower_old_dentry,
5157 + lower_dir_dentry->d_inode,
5158 + lower_new_dentry);
5160 + unlock_dir(lower_dir_dentry);
5167 + if (err || !lower_new_dentry->d_inode)
5170 + /* Its a hard link, so use the same inode */
5171 + new_dentry->d_inode = igrab(old_dentry->d_inode);
5172 + d_add(new_dentry, new_dentry->d_inode);
5173 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5174 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5176 + /* propagate number of hard-links */
5177 + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5178 + /* new dentry's ctime may have changed due to hard-link counts */
5179 + unionfs_copy_attr_times(new_dentry->d_inode);
5182 + if (!new_dentry->d_inode)
5183 + d_drop(new_dentry);
5187 + unionfs_postcopyup_setmnt(new_dentry);
5189 + unionfs_check_inode(dir);
5190 + unionfs_check_dentry(new_dentry);
5191 + unionfs_check_dentry(old_dentry);
5193 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5194 + unionfs_double_unlock_parents(old_parent, new_parent);
5197 + unionfs_read_unlock(old_dentry->d_sb);
5202 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5203 + const char *symname)
5206 + struct dentry *lower_dentry = NULL;
5207 + struct dentry *wh_dentry = NULL;
5208 + struct dentry *lower_parent_dentry = NULL;
5209 + struct dentry *parent;
5210 + char *name = NULL;
5214 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5215 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5216 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5218 + valid = __unionfs_d_revalidate(dentry, parent, false);
5219 + if (unlikely(!valid)) {
5225 + * It's only a bug if this dentry was not negative and couldn't be
5226 + * revalidated (shouldn't happen).
5228 + BUG_ON(!valid && dentry->d_inode);
5230 + lower_dentry = find_writeable_branch(dir, dentry);
5231 + if (IS_ERR(lower_dentry)) {
5232 + err = PTR_ERR(lower_dentry);
5236 + lower_parent_dentry = lock_parent(lower_dentry);
5237 + if (IS_ERR(lower_parent_dentry)) {
5238 + err = PTR_ERR(lower_parent_dentry);
5243 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5245 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5247 + unionfs_copy_attr_times(dir);
5248 + fsstack_copy_inode_size(dir,
5249 + lower_parent_dentry->d_inode);
5250 + /* update no. of links on parent directory */
5251 + dir->i_nlink = unionfs_get_nlinks(dir);
5256 + unlock_dir(lower_parent_dentry);
5262 + unionfs_postcopyup_setmnt(dentry);
5263 + unionfs_check_inode(dir);
5264 + unionfs_check_dentry(dentry);
5266 + unionfs_unlock_dentry(dentry);
5267 + unionfs_unlock_parent(dentry, parent);
5268 + unionfs_read_unlock(dentry->d_sb);
5272 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5275 + struct dentry *lower_dentry = NULL;
5276 + struct dentry *lower_parent_dentry = NULL;
5277 + struct dentry *parent;
5278 + int bindex = 0, bstart;
5279 + char *name = NULL;
5282 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5283 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5284 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5286 + valid = __unionfs_d_revalidate(dentry, parent, false);
5287 + if (unlikely(!valid)) {
5288 + err = -ESTALE; /* same as what real_lookup does */
5292 + bstart = dbstart(dentry);
5294 + lower_dentry = unionfs_lower_dentry(dentry);
5296 + /* check for a whiteout in new dentry branch, and delete it */
5297 + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5298 + if (err > 0) /* whiteout found and removed successfully */
5301 + /* exit if the error returned was NOT -EROFS */
5302 + if (!IS_COPYUP_ERR(err))
5307 + /* check if copyup's needed, and mkdir */
5308 + for (bindex = bstart; bindex >= 0; bindex--) {
5310 + int bend = dbend(dentry);
5312 + if (is_robranch_super(dentry->d_sb, bindex))
5315 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5316 + if (!lower_dentry) {
5317 + lower_dentry = create_parents(dir, dentry,
5318 + dentry->d_name.name,
5320 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5321 + printk(KERN_ERR "unionfs: lower dentry "
5322 + " NULL for bindex = %d\n", bindex);
5327 + lower_parent_dentry = lock_parent(lower_dentry);
5329 + if (IS_ERR(lower_parent_dentry)) {
5330 + err = PTR_ERR(lower_parent_dentry);
5334 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5337 + unlock_dir(lower_parent_dentry);
5339 + /* did the mkdir succeed? */
5343 + for (i = bindex + 1; i <= bend; i++) {
5344 + /* XXX: use path_put_lowers? */
5345 + if (unionfs_lower_dentry_idx(dentry, i)) {
5346 + dput(unionfs_lower_dentry_idx(dentry, i));
5347 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5350 + dbend(dentry) = bindex;
5353 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5356 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5358 + unionfs_copy_attr_times(dir);
5359 + fsstack_copy_inode_size(dir,
5360 + lower_parent_dentry->d_inode);
5362 + /* update number of links on parent directory */
5363 + dir->i_nlink = unionfs_get_nlinks(dir);
5366 + err = make_dir_opaque(dentry, dbstart(dentry));
5368 + printk(KERN_ERR "unionfs: mkdir: error creating "
5369 + ".wh.__dir_opaque: %d\n", err);
5373 + /* we are done! */
5378 + if (!dentry->d_inode)
5384 + unionfs_copy_attr_times(dentry->d_inode);
5385 + unionfs_postcopyup_setmnt(dentry);
5387 + unionfs_check_inode(dir);
5388 + unionfs_check_dentry(dentry);
5389 + unionfs_unlock_dentry(dentry);
5390 + unionfs_unlock_parent(dentry, parent);
5391 + unionfs_read_unlock(dentry->d_sb);
5396 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5400 + struct dentry *lower_dentry = NULL;
5401 + struct dentry *wh_dentry = NULL;
5402 + struct dentry *lower_parent_dentry = NULL;
5403 + struct dentry *parent;
5404 + char *name = NULL;
5407 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5408 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5409 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5411 + valid = __unionfs_d_revalidate(dentry, parent, false);
5412 + if (unlikely(!valid)) {
5418 + * It's only a bug if this dentry was not negative and couldn't be
5419 + * revalidated (shouldn't happen).
5421 + BUG_ON(!valid && dentry->d_inode);
5423 + lower_dentry = find_writeable_branch(dir, dentry);
5424 + if (IS_ERR(lower_dentry)) {
5425 + err = PTR_ERR(lower_dentry);
5429 + lower_parent_dentry = lock_parent(lower_dentry);
5430 + if (IS_ERR(lower_parent_dentry)) {
5431 + err = PTR_ERR(lower_parent_dentry);
5435 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5437 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5439 + unionfs_copy_attr_times(dir);
5440 + fsstack_copy_inode_size(dir,
5441 + lower_parent_dentry->d_inode);
5442 + /* update no. of links on parent directory */
5443 + dir->i_nlink = unionfs_get_nlinks(dir);
5448 + unlock_dir(lower_parent_dentry);
5454 + unionfs_postcopyup_setmnt(dentry);
5455 + unionfs_check_inode(dir);
5456 + unionfs_check_dentry(dentry);
5458 + unionfs_unlock_dentry(dentry);
5459 + unionfs_unlock_parent(dentry, parent);
5460 + unionfs_read_unlock(dentry->d_sb);
5464 +/* requires sb, dentry, and parent to already be locked */
5465 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5469 + struct dentry *lower_dentry;
5471 + lower_dentry = unionfs_lower_dentry(dentry);
5473 + if (!lower_dentry->d_inode->i_op ||
5474 + !lower_dentry->d_inode->i_op->readlink) {
5479 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5482 + fsstack_copy_attr_atime(dentry->d_inode,
5483 + lower_dentry->d_inode);
5489 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5493 + struct dentry *parent;
5495 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5496 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5497 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5499 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5504 + err = __unionfs_readlink(dentry, buf, bufsiz);
5507 + unionfs_check_dentry(dentry);
5508 + unionfs_unlock_dentry(dentry);
5509 + unionfs_unlock_parent(dentry, parent);
5510 + unionfs_read_unlock(dentry->d_sb);
5515 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5518 + int len = PAGE_SIZE, err;
5519 + mm_segment_t old_fs;
5520 + struct dentry *parent;
5522 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5523 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5524 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5526 + /* This is freed by the put_link method assuming a successful call. */
5527 + buf = kmalloc(len, GFP_KERNEL);
5528 + if (unlikely(!buf)) {
5533 + /* read the symlink, and then we will follow it */
5534 + old_fs = get_fs();
5535 + set_fs(KERNEL_DS);
5536 + err = __unionfs_readlink(dentry, buf, len);
5544 + nd_set_link(nd, buf);
5549 + unionfs_check_nd(nd);
5550 + unionfs_check_dentry(dentry);
5553 + unionfs_unlock_dentry(dentry);
5554 + unionfs_unlock_parent(dentry, parent);
5555 + unionfs_read_unlock(dentry->d_sb);
5557 + return ERR_PTR(err);
5560 +/* this @nd *IS* still used */
5561 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5564 + struct dentry *parent;
5567 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5568 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5569 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5571 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5573 + "unionfs: put_link failed to revalidate dentry\n");
5575 + unionfs_check_dentry(dentry);
5577 + /* XXX: can't run this check b/c this fxn can receive a poisoned 'nd' PTR */
5578 + unionfs_check_nd(nd);
5580 + buf = nd_get_link(nd);
5583 + unionfs_unlock_dentry(dentry);
5584 + unionfs_unlock_parent(dentry, parent);
5585 + unionfs_read_unlock(dentry->d_sb);
5589 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5590 + * skips over EROFS tests (because we perform copyup on EROFS).
5592 +static int __inode_permission(struct inode *inode, int mask, unsigned int flags)
5596 + /* nobody gets write access to an immutable file */
5597 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5600 + /* Ordinary permission routines do not understand MAY_APPEND. */
5601 + if (inode->i_op && inode->i_op->permission) {
5602 + retval = inode->i_op->permission(inode, mask, flags);
5605 + * Exec permission on a regular file is denied if none
5606 + * of the execute bits are set.
5608 + * This check should be done by the ->permission()
5611 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5612 + !(inode->i_mode & S_IXUGO))
5616 + retval = generic_permission(inode, mask, flags, NULL);
5621 + return security_inode_permission(inode,
5622 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5626 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5627 + * a deadlock with the branch-management "add branch" code (which grabbed
5628 + * the write lock). It is safe to not grab the read lock here, because even
5629 + * with branch management taking place, there is no chance that
5630 + * unionfs_permission, or anything it calls, will use stale branch
5633 +static int unionfs_permission(struct inode *inode, int mask, unsigned int flags)
5635 + struct inode *lower_inode = NULL;
5637 + int bindex, bstart, bend;
5639 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5640 + struct inode *inode_grabbed;
5641 + struct dentry *dentry;
5643 + if (flags & IPERM_FLAG_RCU) {
5648 + dentry = d_find_alias(inode);
5650 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5652 + inode_grabbed = igrab(inode);
5653 + is_file = !S_ISDIR(inode->i_mode);
5655 + if (!UNIONFS_I(inode)->lower_inodes) {
5656 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5657 + err = -ESTALE; /* force revalidate */
5660 + bstart = ibstart(inode);
5661 + bend = ibend(inode);
5662 + if (unlikely(bstart < 0 || bend < 0)) {
5664 + * With branch-management, we can get a stale inode here.
5665 + * If so, we return ESTALE back to link_path_walk, which
5666 + * would discard the dcache entry and re-lookup the
5667 + * dentry+inode. This should be equivalent to issuing
5668 + * __unionfs_d_revalidate_chain on nd.dentry here.
5670 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5671 + err = -ESTALE; /* force revalidate */
5675 + for (bindex = bstart; bindex <= bend; bindex++) {
5676 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5681 + * check the condition for D-F-D underlying files/directories,
5682 + * we don't have to check for files, if we are checking for
5685 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5689 + * We check basic permissions, but we ignore any conditions
5690 + * such as readonly file systems or branches marked as
5691 + * readonly, because those conditions should lead to a
5692 + * copyup taking place later on. However, if user never had
5693 + * access to the file, then no copyup could ever take place.
5695 + err = __inode_permission(lower_inode, mask, flags);
5696 + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5697 + umode_t mode = lower_inode->i_mode;
5698 + if ((is_robranch_super(inode->i_sb, bindex) ||
5699 + __is_rdonly(lower_inode)) &&
5700 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5702 + if (IS_COPYUP_ERR(err))
5707 + * NFS HACK: NFSv2/3 return EACCES on readonly-exported,
5708 + * locally readonly-mounted file systems, instead of EROFS
5709 + * like other file systems do. So we have no choice here
5710 + * but to intercept this and ignore it for NFS branches
5711 + * marked readonly. Specifically, we avoid using NFS's own
5712 + * "broken" ->permission method, and rely on
5713 + * generic_permission() to do basic checking for us.
5715 + if (err && err == -EACCES &&
5716 + is_robranch_super(inode->i_sb, bindex) &&
5717 + lower_inode->i_sb->s_magic == NFS_SUPER_MAGIC)
5718 + err = generic_permission(lower_inode, mask, flags, NULL);
5721 + * The permissions are an intersection of the overall directory
5722 + * permissions, so we fail if one fails.
5727 + /* only the leftmost file matters. */
5728 + if (is_file || write_mask) {
5729 + if (is_file && write_mask) {
5730 + err = get_write_access(lower_inode);
5732 + put_write_access(lower_inode);
5737 + /* sync times which may have changed (asynchronously) below */
5738 + unionfs_copy_attr_times(inode);
5741 + unionfs_check_inode(inode);
5743 + unionfs_unlock_dentry(dentry);
5746 + iput(inode_grabbed);
5751 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5754 + struct dentry *lower_dentry;
5755 + struct dentry *parent;
5756 + struct inode *inode;
5757 + struct inode *lower_inode;
5758 + int bstart, bend, bindex;
5760 + struct iattr lower_ia;
5762 + /* check if user has permission to change inode */
5763 + err = inode_change_ok(dentry->d_inode, ia);
5767 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5768 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5769 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5771 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5776 + bstart = dbstart(dentry);
5777 + bend = dbend(dentry);
5778 + inode = dentry->d_inode;
5781 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5782 + * to reinterpret it in its own way.
5784 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5785 + ia->ia_valid &= ~ATTR_MODE;
5787 + lower_dentry = unionfs_lower_dentry(dentry);
5788 + if (!lower_dentry) { /* should never happen after above revalidate */
5794 + * Get the lower inode directly from lower dentry, in case ibstart
5795 + * is -1 (which happens when the file is open but unlinked.
5797 + lower_inode = lower_dentry->d_inode;
5799 + /* check if user has permission to change lower inode */
5800 + err = inode_change_ok(lower_inode, ia);
5804 + /* copyup if the file is on a read only branch */
5805 + if (is_robranch_super(dentry->d_sb, bstart)
5806 + || __is_rdonly(lower_inode)) {
5807 + /* check if we have a branch to copy up to */
5808 + if (bstart <= 0) {
5813 + if (ia->ia_valid & ATTR_SIZE)
5814 + size = ia->ia_size;
5816 + size = i_size_read(inode);
5817 + /* copyup to next available branch */
5818 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5819 + err = copyup_dentry(parent->d_inode,
5820 + dentry, bstart, bindex,
5821 + dentry->d_name.name,
5822 + dentry->d_name.len,
5829 + /* get updated lower_dentry/inode after copyup */
5830 + lower_dentry = unionfs_lower_dentry(dentry);
5831 + lower_inode = unionfs_lower_inode(inode);
5833 + * check for whiteouts in writeable branch, and remove them
5836 + if (lower_dentry) {
5837 + err = check_unlink_whiteout(dentry, lower_dentry,
5839 + if (err > 0) /* ignore if whiteout found and removed */
5845 + * If shrinking, first truncate upper level to cancel writing dirty
5846 + * pages beyond the new eof; and also if its' maxbytes is more
5847 + * limiting (fail with -EFBIG before making any change to the lower
5848 + * level). There is no need to vmtruncate the upper level
5849 + * afterwards in the other cases: we fsstack_copy_inode_size from
5850 + * the lower level.
5852 + if (ia->ia_valid & ATTR_SIZE) {
5853 + size = i_size_read(inode);
5854 + if (ia->ia_size < size || (ia->ia_size > size &&
5855 + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5856 + err = vmtruncate(inode, ia->ia_size);
5862 + /* notify the (possibly copied-up) lower inode */
5864 + * Note: we use lower_dentry->d_inode, because lower_inode may be
5865 + * unlinked (no inode->i_sb and i_ino==0. This happens if someone
5866 + * tries to open(), unlink(), then ftruncate() a file.
5868 + /* prepare our own lower struct iattr (with our own lower file) */
5869 + memcpy(&lower_ia, ia, sizeof(lower_ia));
5870 + if (ia->ia_valid & ATTR_FILE) {
5871 + lower_ia.ia_file = unionfs_lower_file(ia->ia_file);
5872 + BUG_ON(!lower_ia.ia_file); // XXX?
5875 + mutex_lock(&lower_dentry->d_inode->i_mutex);
5876 + err = notify_change(lower_dentry, &lower_ia);
5877 + mutex_unlock(&lower_dentry->d_inode->i_mutex);
5881 + /* get attributes from the first lower inode */
5882 + if (ibstart(inode) >= 0)
5883 + unionfs_copy_attr_all(inode, lower_inode);
5885 + * unionfs_copy_attr_all will copy the lower times to our inode if
5886 + * the lower ones are newer (useful for cache coherency). However,
5887 + * ->setattr is the only place in which we may have to copy the
5888 + * lower inode times absolutely, to support utimes(2).
5890 + if (ia->ia_valid & ATTR_MTIME_SET)
5891 + inode->i_mtime = lower_inode->i_mtime;
5892 + if (ia->ia_valid & ATTR_CTIME)
5893 + inode->i_ctime = lower_inode->i_ctime;
5894 + if (ia->ia_valid & ATTR_ATIME_SET)
5895 + inode->i_atime = lower_inode->i_atime;
5896 + fsstack_copy_inode_size(inode, lower_inode);
5900 + unionfs_check_dentry(dentry);
5901 + unionfs_unlock_dentry(dentry);
5902 + unionfs_unlock_parent(dentry, parent);
5903 + unionfs_read_unlock(dentry->d_sb);
5908 +struct inode_operations unionfs_symlink_iops = {
5909 + .readlink = unionfs_readlink,
5910 + .permission = unionfs_permission,
5911 + .follow_link = unionfs_follow_link,
5912 + .setattr = unionfs_setattr,
5913 + .put_link = unionfs_put_link,
5916 +struct inode_operations unionfs_dir_iops = {
5917 + .create = unionfs_create,
5918 + .lookup = unionfs_lookup,
5919 + .link = unionfs_link,
5920 + .unlink = unionfs_unlink,
5921 + .symlink = unionfs_symlink,
5922 + .mkdir = unionfs_mkdir,
5923 + .rmdir = unionfs_rmdir,
5924 + .mknod = unionfs_mknod,
5925 + .rename = unionfs_rename,
5926 + .permission = unionfs_permission,
5927 + .setattr = unionfs_setattr,
5928 +#ifdef CONFIG_UNION_FS_XATTR
5929 + .setxattr = unionfs_setxattr,
5930 + .getxattr = unionfs_getxattr,
5931 + .removexattr = unionfs_removexattr,
5932 + .listxattr = unionfs_listxattr,
5933 +#endif /* CONFIG_UNION_FS_XATTR */
5936 +struct inode_operations unionfs_main_iops = {
5937 + .permission = unionfs_permission,
5938 + .setattr = unionfs_setattr,
5939 +#ifdef CONFIG_UNION_FS_XATTR
5940 + .setxattr = unionfs_setxattr,
5941 + .getxattr = unionfs_getxattr,
5942 + .removexattr = unionfs_removexattr,
5943 + .listxattr = unionfs_listxattr,
5944 +#endif /* CONFIG_UNION_FS_XATTR */
5946 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5947 new file mode 100644
5948 index 0000000..3cbde56
5950 +++ b/fs/unionfs/lookup.c
5953 + * Copyright (c) 2003-2011 Erez Zadok
5954 + * Copyright (c) 2003-2006 Charles P. Wright
5955 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5956 + * Copyright (c) 2005-2006 Junjiro Okajima
5957 + * Copyright (c) 2005 Arun M. Krishnakumar
5958 + * Copyright (c) 2004-2006 David P. Quigley
5959 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5960 + * Copyright (c) 2003 Puja Gupta
5961 + * Copyright (c) 2003 Harikesavan Krishnan
5962 + * Copyright (c) 2003-2011 Stony Brook University
5963 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
5965 + * This program is free software; you can redistribute it and/or modify
5966 + * it under the terms of the GNU General Public License version 2 as
5967 + * published by the Free Software Foundation.
5973 + * Lookup one path component @name relative to a <base,mnt> path pair.
5974 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5975 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5976 + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5977 + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5978 + * @dentry and @new_mnt.
5980 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5981 + const char *name, struct vfsmount **new_mnt)
5983 + struct dentry *dentry = NULL;
5984 + struct nameidata lower_nd;
5987 + /* we use flags=0 to get basic lookup */
5988 + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
5991 + case 0: /* no error */
5992 + dentry = lower_nd.path.dentry;
5994 + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
5998 + * We don't consider ENOENT an error, and we want to return
5999 + * a negative dentry (ala lookup_one_len). As we know
6000 + * there was no inode for this name before (-ENOENT), then
6001 + * it's safe to call lookup_one_len (which doesn't take a
6004 + dentry = lookup_lck_len(name, base, strlen(name));
6006 + *new_mnt = mntget(lower_nd.path.mnt);
6008 + default: /* all other real errors */
6009 + dentry = ERR_PTR(err);
6017 + * This is a utility function that fills in a unionfs dentry.
6018 + * Caller must lock this dentry with unionfs_lock_dentry.
6020 + * Returns: 0 (ok), or -ERRNO if an error occurred.
6021 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
6023 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
6025 + struct dentry *tmp;
6026 + int err = -ENOSYS;
6028 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
6034 + if (IS_ERR(tmp)) {
6035 + err = PTR_ERR(tmp);
6038 + /* XXX: need to change the interface */
6039 + BUG_ON(tmp != dentry);
6044 +/* The dentry cache is just so we have properly sized dentries. */
6045 +static struct kmem_cache *unionfs_dentry_cachep;
6046 +int unionfs_init_dentry_cache(void)
6048 + unionfs_dentry_cachep =
6049 + kmem_cache_create("unionfs_dentry",
6050 + sizeof(struct unionfs_dentry_info),
6051 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6053 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6056 +void unionfs_destroy_dentry_cache(void)
6058 + if (unionfs_dentry_cachep)
6059 + kmem_cache_destroy(unionfs_dentry_cachep);
6062 +void free_dentry_private_data(struct dentry *dentry)
6064 + if (!dentry || !dentry->d_fsdata)
6066 + kfree(UNIONFS_D(dentry)->lower_paths);
6067 + UNIONFS_D(dentry)->lower_paths = NULL;
6068 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6069 + dentry->d_fsdata = NULL;
6072 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6074 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6080 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6081 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6085 + info->lower_paths = p;
6087 + info->bstart = -1;
6089 + info->bopaque = -1;
6090 + info->bcount = sbmax(dentry->d_sb);
6091 + atomic_set(&info->generation,
6092 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6094 + memset(info->lower_paths, 0, size);
6099 +/* UNIONFS_D(dentry)->lock must be locked */
6100 +int realloc_dentry_private_data(struct dentry *dentry)
6102 + if (!__realloc_dentry_private_data(dentry))
6105 + kfree(UNIONFS_D(dentry)->lower_paths);
6106 + free_dentry_private_data(dentry);
6110 +/* allocate new dentry private data */
6111 +int new_dentry_private_data(struct dentry *dentry, int subclass)
6113 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6117 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6118 + if (unlikely(!info))
6121 + mutex_init(&info->lock);
6122 + mutex_lock_nested(&info->lock, subclass);
6124 + info->lower_paths = NULL;
6126 + dentry->d_fsdata = info;
6128 + if (!__realloc_dentry_private_data(dentry))
6131 + mutex_unlock(&info->lock);
6132 + free_dentry_private_data(dentry);
6137 + * scan through the lower dentry objects, and set bstart to reflect the
6140 +void update_bstart(struct dentry *dentry)
6143 + int bstart = dbstart(dentry);
6144 + int bend = dbend(dentry);
6145 + struct dentry *lower_dentry;
6147 + for (bindex = bstart; bindex <= bend; bindex++) {
6148 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6149 + if (!lower_dentry)
6151 + if (lower_dentry->d_inode) {
6152 + dbstart(dentry) = bindex;
6155 + dput(lower_dentry);
6156 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6162 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6163 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6164 + * Inside that nd structure, this function may also return an allocated
6165 + * struct file (for open intents). The caller, when done with this nd, must
6166 + * kfree the intent file (using release_lower_nd).
6168 + * XXX: this code, and the callers of this code, should be redone using
6169 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6170 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6171 + * function and a method that other file systems can call.
6173 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6176 +#ifdef ALLOC_LOWER_ND_FILE
6178 + * XXX: one day we may need to have the lower return an open file
6179 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6180 + * very well be needed for nfs4.
6182 + struct file *file;
6183 +#endif /* ALLOC_LOWER_ND_FILE */
6185 + memset(nd, 0, sizeof(struct nameidata));
6190 + case LOOKUP_CREATE:
6191 + nd->intent.open.flags |= O_CREAT;
6192 + /* fall through: shared code for create/open cases */
6194 + nd->flags = flags;
6195 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6196 +#ifdef ALLOC_LOWER_ND_FILE
6197 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6198 + if (unlikely(!file)) {
6200 + break; /* exit switch statement and thus return */
6202 + nd->intent.open.file = file;
6203 +#endif /* ALLOC_LOWER_ND_FILE */
6207 + * We should never get here, for now.
6208 + * We can add new cases here later on.
6210 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6218 +void release_lower_nd(struct nameidata *nd, int err)
6220 + if (!nd->intent.open.file)
6223 + release_open_intent(nd);
6224 +#ifdef ALLOC_LOWER_ND_FILE
6225 + kfree(nd->intent.open.file);
6226 +#endif /* ALLOC_LOWER_ND_FILE */
6230 + * Main (and complex) driver function for Unionfs's lookup
6232 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6233 + * PTR if d_splice returned a different dentry.
6235 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6236 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6237 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6238 + * dentry's info, which the caller must unlock.
6240 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6241 + struct dentry *parent, int lookupmode)
6244 + struct dentry *lower_dentry = NULL;
6245 + struct vfsmount *lower_mnt;
6246 + struct vfsmount *lower_dir_mnt;
6247 + struct dentry *wh_lower_dentry = NULL;
6248 + struct dentry *lower_dir_dentry = NULL;
6249 + struct dentry *d_interposed = NULL;
6250 + int bindex, bstart, bend, bopaque;
6251 + int opaque, num_positive = 0;
6254 + int pos_start, pos_end;
6257 + * We should already have a lock on this dentry in the case of a
6258 + * partial lookup, or a revalidation. Otherwise it is returned from
6259 + * new_dentry_private_data already locked.
6261 + verify_locked(dentry);
6262 + verify_locked(parent);
6264 + /* must initialize dentry operations */
6265 + dentry->d_op = &unionfs_dops;
6267 + /* We never partial lookup the root directory. */
6268 + if (IS_ROOT(dentry))
6271 + name = dentry->d_name.name;
6272 + namelen = dentry->d_name.len;
6274 + /* No dentries should get created for possible whiteout names. */
6275 + if (!is_validname(name)) {
6280 + /* Now start the actual lookup procedure. */
6281 + bstart = dbstart(parent);
6282 + bend = dbend(parent);
6283 + bopaque = dbopaque(parent);
6284 + BUG_ON(bstart < 0);
6286 + /* adjust bend to bopaque if needed */
6287 + if ((bopaque >= 0) && (bopaque < bend))
6290 + /* lookup all possible dentries */
6291 + for (bindex = bstart; bindex <= bend; bindex++) {
6293 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6294 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6296 + /* skip if we already have a positive lower dentry */
6297 + if (lower_dentry) {
6298 + if (dbstart(dentry) < 0)
6299 + dbstart(dentry) = bindex;
6300 + if (bindex > dbend(dentry))
6301 + dbend(dentry) = bindex;
6302 + if (lower_dentry->d_inode)
6307 + lower_dir_dentry =
6308 + unionfs_lower_dentry_idx(parent, bindex);
6309 + /* if the lower dentry's parent does not exist, skip this */
6310 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6313 + /* also skip it if the parent isn't a directory. */
6314 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6315 + continue; /* XXX: should be BUG_ON */
6317 + /* check for whiteouts: stop lookup if found */
6318 + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6319 + if (IS_ERR(wh_lower_dentry)) {
6320 + err = PTR_ERR(wh_lower_dentry);
6323 + if (wh_lower_dentry->d_inode) {
6324 + dbend(dentry) = dbopaque(dentry) = bindex;
6325 + if (dbstart(dentry) < 0)
6326 + dbstart(dentry) = bindex;
6327 + dput(wh_lower_dentry);
6330 + dput(wh_lower_dentry);
6332 + /* Now do regular lookup; lookup @name */
6333 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6334 + lower_mnt = NULL; /* XXX: needed? */
6336 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6337 + name, &lower_mnt);
6339 + if (IS_ERR(lower_dentry)) {
6340 + err = PTR_ERR(lower_dentry);
6343 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6345 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6347 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6349 + /* adjust dbstart/end */
6350 + if (dbstart(dentry) < 0)
6351 + dbstart(dentry) = bindex;
6352 + if (bindex > dbend(dentry))
6353 + dbend(dentry) = bindex;
6355 + * We always store the lower dentries above, and update
6356 + * dbstart/dbend, even if the whole unionfs dentry is
6357 + * negative (i.e., no lower inodes).
6359 + if (!lower_dentry->d_inode)
6364 + * check if we just found an opaque directory, if so, stop
6367 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6369 + opaque = is_opaque_dir(dentry, bindex);
6373 + } else if (opaque) {
6374 + dbend(dentry) = dbopaque(dentry) = bindex;
6377 + dbend(dentry) = bindex;
6379 + /* update parent directory's atime with the bindex */
6380 + fsstack_copy_attr_atime(parent->d_inode,
6381 + lower_dir_dentry->d_inode);
6384 + /* sanity checks, then decide if to process a negative dentry */
6385 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6386 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6388 + if (num_positive > 0)
6389 + goto out_positive;
6391 + /*** handle NEGATIVE dentries ***/
6394 + * If negative, keep only first lower negative dentry, to save on
6397 + if (dbstart(dentry) < dbend(dentry)) {
6398 + path_put_lowers(dentry, dbstart(dentry) + 1,
6399 + dbend(dentry), false);
6400 + dbend(dentry) = dbstart(dentry);
6402 + if (lookupmode == INTERPOSE_PARTIAL)
6404 + if (lookupmode == INTERPOSE_LOOKUP) {
6406 + * If all we found was a whiteout in the first available
6407 + * branch, then create a negative dentry for a possibly new
6408 + * file to be created.
6410 + if (dbopaque(dentry) < 0)
6412 + /* XXX: need to get mnt here */
6413 + bindex = dbstart(dentry);
6414 + if (unionfs_lower_dentry_idx(dentry, bindex))
6416 + lower_dir_dentry =
6417 + unionfs_lower_dentry_idx(parent, bindex);
6418 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6420 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6421 + goto out; /* XXX: should be BUG_ON */
6422 + /* XXX: do we need to cross bind mounts here? */
6423 + lower_dentry = lookup_lck_len(name, lower_dir_dentry, namelen);
6424 + if (IS_ERR(lower_dentry)) {
6425 + err = PTR_ERR(lower_dentry);
6428 + /* XXX: need to mntget/mntput as needed too! */
6429 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6430 + /* XXX: wrong mnt for crossing bind mounts! */
6431 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6432 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6437 + /* if we're revalidating a positive dentry, don't make it negative */
6438 + if (lookupmode != INTERPOSE_REVAL)
6439 + d_add(dentry, NULL);
6444 + /*** handle POSITIVE dentries ***/
6447 + * This unionfs dentry is positive (at least one lower inode
6448 + * exists), so scan entire dentry from beginning to end, and remove
6449 + * any negative lower dentries, if any. Then, update dbstart/dbend
6450 + * to reflect the start/end of positive dentries.
6452 + pos_start = pos_end = -1;
6453 + for (bindex = bstart; bindex <= bend; bindex++) {
6454 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6456 + if (lower_dentry && lower_dentry->d_inode) {
6457 + if (pos_start < 0)
6458 + pos_start = bindex;
6459 + if (bindex > pos_end)
6463 + path_put_lowers(dentry, bindex, bindex, false);
6465 + if (pos_start >= 0)
6466 + dbstart(dentry) = pos_start;
6468 + dbend(dentry) = pos_end;
6470 + /* Partial lookups need to re-interpose, or throw away older negs. */
6471 + if (lookupmode == INTERPOSE_PARTIAL) {
6472 + if (dentry->d_inode) {
6473 + unionfs_reinterpose(dentry);
6478 + * This dentry was positive, so it is as if we had a
6479 + * negative revalidation.
6481 + lookupmode = INTERPOSE_REVAL_NEG;
6482 + update_bstart(dentry);
6486 + * Interpose can return a dentry if d_splice returned a different
6489 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6490 + if (IS_ERR(d_interposed))
6491 + err = PTR_ERR(d_interposed);
6492 + else if (d_interposed)
6493 + dentry = d_interposed;
6500 + /* should dput/mntput all the underlying dentries on error condition */
6501 + if (dbstart(dentry) >= 0)
6502 + path_put_lowers_all(dentry, false);
6503 + /* free lower_paths unconditionally */
6504 + kfree(UNIONFS_D(dentry)->lower_paths);
6505 + UNIONFS_D(dentry)->lower_paths = NULL;
6508 + if (dentry && UNIONFS_D(dentry)) {
6509 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6510 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6512 + if (d_interposed && UNIONFS_D(d_interposed)) {
6513 + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6514 + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6517 + if (!err && d_interposed)
6518 + return d_interposed;
6519 + return ERR_PTR(err);
6521 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6522 new file mode 100644
6523 index 0000000..fa52f61
6525 +++ b/fs/unionfs/main.c
6528 + * Copyright (c) 2003-2011 Erez Zadok
6529 + * Copyright (c) 2003-2006 Charles P. Wright
6530 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6531 + * Copyright (c) 2005-2006 Junjiro Okajima
6532 + * Copyright (c) 2005 Arun M. Krishnakumar
6533 + * Copyright (c) 2004-2006 David P. Quigley
6534 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6535 + * Copyright (c) 2003 Puja Gupta
6536 + * Copyright (c) 2003 Harikesavan Krishnan
6537 + * Copyright (c) 2003-2011 Stony Brook University
6538 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
6540 + * This program is free software; you can redistribute it and/or modify
6541 + * it under the terms of the GNU General Public License version 2 as
6542 + * published by the Free Software Foundation.
6546 +#include <linux/module.h>
6547 +#include <linux/moduleparam.h>
6549 +static void unionfs_fill_inode(struct dentry *dentry,
6550 + struct inode *inode)
6552 + struct inode *lower_inode;
6553 + struct dentry *lower_dentry;
6554 + int bindex, bstart, bend;
6556 + bstart = dbstart(dentry);
6557 + bend = dbend(dentry);
6559 + for (bindex = bstart; bindex <= bend; bindex++) {
6560 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6561 + if (!lower_dentry) {
6562 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6566 + /* Initialize the lower inode to the new lower inode. */
6567 + if (!lower_dentry->d_inode)
6570 + unionfs_set_lower_inode_idx(inode, bindex,
6571 + igrab(lower_dentry->d_inode));
6574 + ibstart(inode) = dbstart(dentry);
6575 + ibend(inode) = dbend(dentry);
6577 + /* Use attributes from the first branch. */
6578 + lower_inode = unionfs_lower_inode(inode);
6580 + /* Use different set of inode ops for symlinks & directories */
6581 + if (S_ISLNK(lower_inode->i_mode))
6582 + inode->i_op = &unionfs_symlink_iops;
6583 + else if (S_ISDIR(lower_inode->i_mode))
6584 + inode->i_op = &unionfs_dir_iops;
6586 + /* Use different set of file ops for directories */
6587 + if (S_ISDIR(lower_inode->i_mode))
6588 + inode->i_fop = &unionfs_dir_fops;
6590 + /* properly initialize special inodes */
6591 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6592 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6593 + init_special_inode(inode, lower_inode->i_mode,
6594 + lower_inode->i_rdev);
6596 + /* all well, copy inode attributes */
6597 + unionfs_copy_attr_all(inode, lower_inode);
6598 + fsstack_copy_inode_size(inode, lower_inode);
6602 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6603 + * the classic stackable file system "vnode interposition" action.
6605 + * @sb: unionfs's super_block
6607 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6611 + struct inode *inode;
6612 + int need_fill_inode = 1;
6613 + struct dentry *spliced = NULL;
6615 + verify_locked(dentry);
6618 + * We allocate our new inode below by calling unionfs_iget,
6619 + * which will initialize some of the new inode's fields
6623 + * On revalidate we've already got our own inode and just need
6626 + if (flag == INTERPOSE_REVAL) {
6627 + inode = dentry->d_inode;
6628 + UNIONFS_I(inode)->bstart = -1;
6629 + UNIONFS_I(inode)->bend = -1;
6630 + atomic_set(&UNIONFS_I(inode)->generation,
6631 + atomic_read(&UNIONFS_SB(sb)->generation));
6633 + UNIONFS_I(inode)->lower_inodes =
6634 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6635 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6640 + /* get unique inode number for unionfs */
6641 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6642 + if (IS_ERR(inode)) {
6643 + err = PTR_ERR(inode);
6646 + if (atomic_read(&inode->i_count) > 1)
6650 + need_fill_inode = 0;
6651 + unionfs_fill_inode(dentry, inode);
6654 + /* only (our) lookup wants to do a d_add */
6656 + case INTERPOSE_DEFAULT:
6657 + /* for operations which create new inodes */
6658 + d_add(dentry, inode);
6660 + case INTERPOSE_REVAL_NEG:
6661 + d_instantiate(dentry, inode);
6663 + case INTERPOSE_LOOKUP:
6664 + spliced = d_splice_alias(inode, dentry);
6665 + if (spliced && spliced != dentry) {
6667 + * d_splice can return a dentry if it was
6668 + * disconnected and had to be moved. We must ensure
6669 + * that the private data of the new dentry is
6670 + * correct and that the inode info was filled
6671 + * properly. Finally we must return this new
6674 + spliced->d_op = &unionfs_dops;
6675 + spliced->d_fsdata = dentry->d_fsdata;
6676 + dentry->d_fsdata = NULL;
6678 + if (need_fill_inode) {
6679 + need_fill_inode = 0;
6680 + unionfs_fill_inode(dentry, inode);
6683 + } else if (!spliced) {
6684 + if (need_fill_inode) {
6685 + need_fill_inode = 0;
6686 + unionfs_fill_inode(dentry, inode);
6691 + case INTERPOSE_REVAL:
6695 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6704 + return ERR_PTR(err);
6707 +/* like interpose above, but for an already existing dentry */
6708 +void unionfs_reinterpose(struct dentry *dentry)
6710 + struct dentry *lower_dentry;
6711 + struct inode *inode;
6712 + int bindex, bstart, bend;
6714 + verify_locked(dentry);
6716 + /* This is pre-allocated inode */
6717 + inode = dentry->d_inode;
6719 + bstart = dbstart(dentry);
6720 + bend = dbend(dentry);
6721 + for (bindex = bstart; bindex <= bend; bindex++) {
6722 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6723 + if (!lower_dentry)
6726 + if (!lower_dentry->d_inode)
6728 + if (unionfs_lower_inode_idx(inode, bindex))
6730 + unionfs_set_lower_inode_idx(inode, bindex,
6731 + igrab(lower_dentry->d_inode));
6733 + ibstart(inode) = dbstart(dentry);
6734 + ibend(inode) = dbend(dentry);
6738 + * make sure the branch we just looked up (nd) makes sense:
6740 + * 1) we're not trying to stack unionfs on top of unionfs
6742 + * 3) is a directory
6744 +int check_branch(const struct path *path)
6746 + /* XXX: remove in ODF code -- stacking unions allowed there */
6747 + if (!strcmp(path->dentry->d_sb->s_type->name, UNIONFS_NAME))
6749 + if (!path->dentry->d_inode)
6751 + if (!S_ISDIR(path->dentry->d_inode->i_mode))
6756 +/* checks if two lower_dentries have overlapping branches */
6757 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6759 + struct dentry *dent = NULL;
6762 + while ((dent != dent2) && (dent->d_parent != dent))
6763 + dent = dent->d_parent;
6765 + if (dent == dent2)
6769 + while ((dent != dent1) && (dent->d_parent != dent))
6770 + dent = dent->d_parent;
6772 + return (dent == dent1);
6776 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6777 + * specified. Fill the mode bits in @perms. If encounter an unknown
6778 + * string, return -EINVAL. Otherwise return 0.
6780 +int parse_branch_mode(const char *name, int *perms)
6782 + if (!name || !strcmp(name, "rw")) {
6783 + *perms = MAY_READ | MAY_WRITE;
6786 + if (!strcmp(name, "ro")) {
6787 + *perms = MAY_READ;
6794 + * parse the dirs= mount argument
6796 + * We don't need to lock the superblock private data's rwsem, as we get
6797 + * called only by unionfs_read_super - it is still a long time before anyone
6798 + * can even get a reference to us.
6800 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6801 + *lower_root_info, char *options)
6810 + struct dentry *dent1;
6811 + struct dentry *dent2;
6813 + if (options[0] == '\0') {
6814 + printk(KERN_ERR "unionfs: no branches specified\n");
6820 + * Each colon means we have a separator, this is really just a rough
6821 + * guess, since strsep will handle empty fields for us.
6823 + for (i = 0; options[i]; i++)
6824 + if (options[i] == ':')
6827 + /* allocate space for underlying pointers to lower dentry */
6828 + UNIONFS_SB(sb)->data =
6829 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6830 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6835 + lower_root_info->lower_paths =
6836 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6837 + if (unlikely(!lower_root_info->lower_paths)) {
6839 + /* free the underlying pointer array */
6840 + kfree(UNIONFS_SB(sb)->data);
6841 + UNIONFS_SB(sb)->data = NULL;
6845 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6847 + while ((name = strsep(&options, ":")) != NULL) {
6849 + char *mode = strchr(name, '=');
6853 + if (!*name) { /* bad use of ':' (extra colons) */
6860 + /* strip off '=' if any */
6864 + err = parse_branch_mode(mode, &perms);
6866 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6867 + "branch %d\n", mode, bindex);
6870 + /* ensure that leftmost branch is writeable */
6871 + if (!bindex && !(perms & MAY_WRITE)) {
6872 + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6873 + "read-only (use \"-o ro\" to create a "
6874 + "read-only union)\n");
6879 + err = kern_path(name, LOOKUP_FOLLOW, &path);
6881 + printk(KERN_ERR "unionfs: error accessing "
6882 + "lower directory '%s' (error %d)\n",
6887 + err = check_branch(&path);
6889 + printk(KERN_ERR "unionfs: lower directory "
6890 + "'%s' is not a valid branch\n", name);
6895 + lower_root_info->lower_paths[bindex].dentry = path.dentry;
6896 + lower_root_info->lower_paths[bindex].mnt = path.mnt;
6898 + set_branchperms(sb, bindex, perms);
6899 + set_branch_count(sb, bindex, 0);
6900 + new_branch_id(sb, bindex);
6902 + if (lower_root_info->bstart < 0)
6903 + lower_root_info->bstart = bindex;
6904 + lower_root_info->bend = bindex;
6908 + if (branches == 0) {
6909 + printk(KERN_ERR "unionfs: no branches specified\n");
6914 + BUG_ON(branches != (lower_root_info->bend + 1));
6917 + * Ensure that no overlaps exist in the branches.
6919 + * This test is required because the Linux kernel has no support
6920 + * currently for ensuring coherency between stackable layers and
6921 + * branches. If we were to allow overlapping branches, it would be
6922 + * possible, for example, to delete a file via one branch, which
6923 + * would not be reflected in another branch. Such incoherency could
6924 + * lead to inconsistencies and even kernel oopses. Rather than
6925 + * implement hacks to work around some of these cache-coherency
6926 + * problems, we prevent branch overlapping, for now. A complete
6927 + * solution will involve proper kernel/VFS support for cache
6928 + * coherency, at which time we could safely remove this
6929 + * branch-overlapping test.
6931 + for (i = 0; i < branches; i++) {
6932 + dent1 = lower_root_info->lower_paths[i].dentry;
6933 + for (j = i + 1; j < branches; j++) {
6934 + dent2 = lower_root_info->lower_paths[j].dentry;
6935 + if (is_branch_overlap(dent1, dent2)) {
6936 + printk(KERN_ERR "unionfs: branches %d and "
6937 + "%d overlap\n", i, j);
6946 + for (i = 0; i < branches; i++)
6947 + path_put(&lower_root_info->lower_paths[i]);
6949 + kfree(lower_root_info->lower_paths);
6950 + kfree(UNIONFS_SB(sb)->data);
6953 + * MUST clear the pointers to prevent potential double free if
6954 + * the caller dies later on
6956 + lower_root_info->lower_paths = NULL;
6957 + UNIONFS_SB(sb)->data = NULL;
6964 + * Parse mount options. See the manual page for usage instructions.
6966 + * Returns the dentry object of the lower-level (lower) directory;
6967 + * We want to mount our stackable file system on top of that lower directory.
6969 +static struct unionfs_dentry_info *unionfs_parse_options(
6970 + struct super_block *sb,
6973 + struct unionfs_dentry_info *lower_root_info;
6977 + int dirsfound = 0;
6979 + /* allocate private data area */
6982 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6983 + if (unlikely(!lower_root_info))
6985 + lower_root_info->bstart = -1;
6986 + lower_root_info->bend = -1;
6987 + lower_root_info->bopaque = -1;
6989 + while ((optname = strsep(&options, ",")) != NULL) {
6992 + if (!optname || !*optname)
6995 + optarg = strchr(optname, '=');
7000 + * All of our options take an argument now. Insert ones that
7001 + * don't, above this check.
7004 + printk(KERN_ERR "unionfs: %s requires an argument\n",
7010 + if (!strcmp("dirs", optname)) {
7011 + if (++dirsfound > 1) {
7013 + "unionfs: multiple dirs specified\n");
7017 + err = parse_dirs_option(sb, lower_root_info, optarg);
7025 + "unionfs: unrecognized option '%s'\n", optname);
7028 + if (dirsfound != 1) {
7029 + printk(KERN_ERR "unionfs: dirs option required\n");
7036 + if (lower_root_info && lower_root_info->lower_paths) {
7037 + for (bindex = lower_root_info->bstart;
7038 + bindex >= 0 && bindex <= lower_root_info->bend;
7040 + path_put(&lower_root_info->lower_paths[bindex]);
7043 + kfree(lower_root_info->lower_paths);
7044 + kfree(lower_root_info);
7046 + kfree(UNIONFS_SB(sb)->data);
7047 + UNIONFS_SB(sb)->data = NULL;
7049 + lower_root_info = ERR_PTR(err);
7051 + return lower_root_info;
7055 + * our custom d_alloc_root work-alike
7057 + * we can't use d_alloc_root if we want to use our own interpose function
7058 + * unchanged, so we simply call our own "fake" d_alloc_root
7060 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7062 + struct dentry *ret = NULL;
7065 + static const struct qstr name = {
7070 + ret = d_alloc(NULL, &name);
7071 + if (likely(ret)) {
7072 + ret->d_op = &unionfs_dops;
7074 + ret->d_parent = ret;
7081 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7082 + * way anyone can have a reference to the superblock at this point in time.
7084 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7088 + struct unionfs_dentry_info *lower_root_info = NULL;
7089 + int bindex, bstart, bend;
7093 + "unionfs: read_super: missing data argument\n");
7098 + /* Allocate superblock private data */
7099 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7100 + if (unlikely(!UNIONFS_SB(sb))) {
7101 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7106 + UNIONFS_SB(sb)->bend = -1;
7107 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7108 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7109 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7111 + lower_root_info = unionfs_parse_options(sb, raw_data);
7112 + if (IS_ERR(lower_root_info)) {
7114 + "unionfs: read_super: error while parsing options "
7115 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7116 + err = PTR_ERR(lower_root_info);
7117 + lower_root_info = NULL;
7120 + if (lower_root_info->bstart == -1) {
7125 + /* set the lower superblock field of upper superblock */
7126 + bstart = lower_root_info->bstart;
7127 + BUG_ON(bstart != 0);
7128 + sbend(sb) = bend = lower_root_info->bend;
7129 + for (bindex = bstart; bindex <= bend; bindex++) {
7130 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7131 + atomic_inc(&d->d_sb->s_active);
7132 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7135 + /* max Bytes is the maximum bytes from highest priority branch */
7136 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7139 + * Our c/m/atime granularity is 1 ns because we may stack on file
7140 + * systems whose granularity is as good. This is important for our
7141 + * time-based cache coherency.
7143 + sb->s_time_gran = 1;
7145 + sb->s_op = &unionfs_sops;
7147 + /* See comment next to the definition of unionfs_d_alloc_root */
7148 + sb->s_root = unionfs_d_alloc_root(sb);
7149 + if (unlikely(!sb->s_root)) {
7154 + /* link the upper and lower dentries */
7155 + sb->s_root->d_fsdata = NULL;
7156 + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7157 + if (unlikely(err))
7160 + /* Set the lower dentries for s_root */
7161 + for (bindex = bstart; bindex <= bend; bindex++) {
7163 + struct vfsmount *m;
7165 + d = lower_root_info->lower_paths[bindex].dentry;
7166 + m = lower_root_info->lower_paths[bindex].mnt;
7168 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7169 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7171 + dbstart(sb->s_root) = bstart;
7172 + dbend(sb->s_root) = bend;
7174 + /* Set the generation number to one, since this is for the mount. */
7175 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7178 + * Call interpose to create the upper level inode. Only
7179 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7181 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7182 + unionfs_unlock_dentry(sb->s_root);
7185 + /* else fall through */
7188 + if (UNIONFS_D(sb->s_root)) {
7189 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7190 + free_dentry_private_data(sb->s_root);
7195 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7196 + for (bindex = lower_root_info->bstart;
7197 + bindex <= lower_root_info->bend; bindex++) {
7199 + d = lower_root_info->lower_paths[bindex].dentry;
7200 + /* drop refs we took earlier */
7201 + atomic_dec(&d->d_sb->s_active);
7202 + path_put(&lower_root_info->lower_paths[bindex]);
7204 + kfree(lower_root_info->lower_paths);
7205 + kfree(lower_root_info);
7206 + lower_root_info = NULL;
7210 + kfree(UNIONFS_SB(sb)->data);
7211 + kfree(UNIONFS_SB(sb));
7212 + sb->s_fs_info = NULL;
7215 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7216 + kfree(lower_root_info->lower_paths);
7217 + kfree(lower_root_info);
7222 +static struct dentry *unionfs_mount(struct file_system_type *fs_type,
7223 + int flags, const char *dev_name,
7226 + struct dentry *dentry;
7228 + dentry = mount_nodev(fs_type, flags, raw_data, unionfs_read_super);
7229 + if (!PTR_ERR(dentry))
7230 + UNIONFS_SB(dentry->d_sb)->dev_name =
7231 + kstrdup(dev_name, GFP_KERNEL);
7235 +static struct file_system_type unionfs_fs_type = {
7236 + .owner = THIS_MODULE,
7237 + .name = UNIONFS_NAME,
7238 + .mount = unionfs_mount,
7239 + .kill_sb = generic_shutdown_super,
7240 + .fs_flags = FS_REVAL_DOT,
7243 +static int __init init_unionfs_fs(void)
7247 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7249 + err = unionfs_init_filldir_cache();
7250 + if (unlikely(err))
7252 + err = unionfs_init_inode_cache();
7253 + if (unlikely(err))
7255 + err = unionfs_init_dentry_cache();
7256 + if (unlikely(err))
7258 + err = init_sioq();
7259 + if (unlikely(err))
7261 + err = register_filesystem(&unionfs_fs_type);
7263 + if (unlikely(err)) {
7265 + unionfs_destroy_filldir_cache();
7266 + unionfs_destroy_inode_cache();
7267 + unionfs_destroy_dentry_cache();
7272 +static void __exit exit_unionfs_fs(void)
7275 + unionfs_destroy_filldir_cache();
7276 + unionfs_destroy_inode_cache();
7277 + unionfs_destroy_dentry_cache();
7278 + unregister_filesystem(&unionfs_fs_type);
7279 + pr_info("Completed unionfs module unload\n");
7282 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7283 + " (http://www.fsl.cs.sunysb.edu)");
7284 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7285 + " (http://unionfs.filesystems.org)");
7286 +MODULE_LICENSE("GPL");
7288 +module_init(init_unionfs_fs);
7289 +module_exit(exit_unionfs_fs);
7290 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7291 new file mode 100644
7292 index 0000000..bcc5652
7294 +++ b/fs/unionfs/mmap.c
7297 + * Copyright (c) 2003-2011 Erez Zadok
7298 + * Copyright (c) 2003-2006 Charles P. Wright
7299 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7300 + * Copyright (c) 2005-2006 Junjiro Okajima
7301 + * Copyright (c) 2006 Shaya Potter
7302 + * Copyright (c) 2005 Arun M. Krishnakumar
7303 + * Copyright (c) 2004-2006 David P. Quigley
7304 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7305 + * Copyright (c) 2003 Puja Gupta
7306 + * Copyright (c) 2003 Harikesavan Krishnan
7307 + * Copyright (c) 2003-2011 Stony Brook University
7308 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
7310 + * This program is free software; you can redistribute it and/or modify
7311 + * it under the terms of the GNU General Public License version 2 as
7312 + * published by the Free Software Foundation.
7319 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7320 + * use in unionfs_mmap) checks for the existence of
7321 + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7322 + * be fixed to allow a file system to define vm_ops->fault without any
7323 + * address_space_ops whatsoever.
7325 + * Otherwise, we don't want to use our readpage method at all.
7327 +static int unionfs_readpage(struct file *file, struct page *page)
7333 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7336 + struct file *file, *lower_file;
7337 + const struct vm_operations_struct *lower_vm_ops;
7338 + struct vm_area_struct lower_vma;
7341 + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7342 + file = lower_vma.vm_file;
7343 + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7344 + BUG_ON(!lower_vm_ops);
7346 + lower_file = unionfs_lower_file(file);
7347 + BUG_ON(!lower_file);
7349 + * XXX: vm_ops->fault may be called in parallel. Because we have to
7350 + * resort to temporarily changing the vma->vm_file to point to the
7351 + * lower file, a concurrent invocation of unionfs_fault could see a
7352 + * different value. In this workaround, we keep a different copy of
7353 + * the vma structure in our stack, so we never expose a different
7354 + * value of the vma->vm_file called to us, even temporarily. A
7355 + * better fix would be to change the calling semantics of ->fault to
7356 + * take an explicit file pointer.
7358 + lower_vma.vm_file = lower_file;
7359 + err = lower_vm_ops->fault(&lower_vma, vmf);
7364 + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7365 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7366 + * the a_ops vector to be non-NULL.
7368 +struct address_space_operations unionfs_aops = {
7369 + /* empty on purpose */
7373 + * XXX: we need a second, dummy address_space_ops vector, to be used
7374 + * temporarily during unionfs_mmap, because the latter calls
7375 + * generic_file_mmap, which checks if ->readpage exists, else returns
7378 +struct address_space_operations unionfs_dummy_aops = {
7379 + .readpage = unionfs_readpage,
7382 +struct vm_operations_struct unionfs_vm_ops = {
7383 + .fault = unionfs_fault,
7385 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7386 new file mode 100644
7387 index 0000000..59b7333
7389 +++ b/fs/unionfs/rdstate.c
7392 + * Copyright (c) 2003-2011 Erez Zadok
7393 + * Copyright (c) 2003-2006 Charles P. Wright
7394 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7395 + * Copyright (c) 2005-2006 Junjiro Okajima
7396 + * Copyright (c) 2005 Arun M. Krishnakumar
7397 + * Copyright (c) 2004-2006 David P. Quigley
7398 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7399 + * Copyright (c) 2003 Puja Gupta
7400 + * Copyright (c) 2003 Harikesavan Krishnan
7401 + * Copyright (c) 2003-2011 Stony Brook University
7402 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
7404 + * This program is free software; you can redistribute it and/or modify
7405 + * it under the terms of the GNU General Public License version 2 as
7406 + * published by the Free Software Foundation.
7411 +/* This file contains the routines for maintaining readdir state. */
7414 + * There are two structures here, rdstate which is a hash table
7415 + * of the second structure which is a filldir_node.
7419 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7420 + * of them and they shouldn't waste memory. If the node has a small name
7421 + * (as defined by the dentry structure), then we use an inline name to
7422 + * preserve kmalloc space.
7424 +static struct kmem_cache *unionfs_filldir_cachep;
7426 +int unionfs_init_filldir_cache(void)
7428 + unionfs_filldir_cachep =
7429 + kmem_cache_create("unionfs_filldir",
7430 + sizeof(struct filldir_node), 0,
7431 + SLAB_RECLAIM_ACCOUNT, NULL);
7433 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7436 +void unionfs_destroy_filldir_cache(void)
7438 + if (unionfs_filldir_cachep)
7439 + kmem_cache_destroy(unionfs_filldir_cachep);
7443 + * This is a tuning parameter that tells us roughly how big to make the
7444 + * hash table in directory entries per page. This isn't perfect, but
7445 + * at least we get a hash table size that shouldn't be too overloaded.
7446 + * The following averages are based on my home directory.
7447 + * 14.44693 Overall
7448 + * 12.29 Single Page Directories
7449 + * 117.93 Multi-page directories
7451 +#define DENTPAGE 4096
7452 +#define DENTPERONEPAGE 12
7453 +#define DENTPERPAGE 118
7454 +#define MINHASHSIZE 1
7455 +static int guesstimate_hash_size(struct inode *inode)
7457 + struct inode *lower_inode;
7459 + int hashsize = MINHASHSIZE;
7461 + if (UNIONFS_I(inode)->hashsize > 0)
7462 + return UNIONFS_I(inode)->hashsize;
7464 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7465 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7469 + if (i_size_read(lower_inode) == DENTPAGE)
7470 + hashsize += DENTPERONEPAGE;
7472 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7479 +int init_rdstate(struct file *file)
7481 + BUG_ON(sizeof(loff_t) !=
7482 + (sizeof(unsigned int) + sizeof(unsigned int)));
7483 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7485 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7488 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7491 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7493 + struct unionfs_dir_state *rdstate = NULL;
7494 + struct list_head *pos;
7496 + spin_lock(&UNIONFS_I(inode)->rdlock);
7497 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7498 + struct unionfs_dir_state *r =
7499 + list_entry(pos, struct unionfs_dir_state, cache);
7500 + if (fpos == rdstate2offset(r)) {
7501 + UNIONFS_I(inode)->rdcount--;
7502 + list_del(&r->cache);
7507 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7511 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7515 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7516 + struct unionfs_dir_state *rdstate;
7518 + hashsize = guesstimate_hash_size(inode);
7519 + mallocsize += hashsize * sizeof(struct list_head);
7520 + mallocsize = __roundup_pow_of_two(mallocsize);
7522 + /* This should give us about 500 entries anyway. */
7523 + if (mallocsize > PAGE_SIZE)
7524 + mallocsize = PAGE_SIZE;
7526 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7527 + sizeof(struct list_head);
7529 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7530 + if (unlikely(!rdstate))
7533 + spin_lock(&UNIONFS_I(inode)->rdlock);
7534 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7535 + UNIONFS_I(inode)->cookie = 1;
7537 + UNIONFS_I(inode)->cookie++;
7539 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7540 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7541 + rdstate->offset = 1;
7542 + rdstate->access = jiffies;
7543 + rdstate->bindex = bindex;
7544 + rdstate->dirpos = 0;
7545 + rdstate->hashentries = 0;
7546 + rdstate->size = hashsize;
7547 + for (i = 0; i < rdstate->size; i++)
7548 + INIT_LIST_HEAD(&rdstate->list[i]);
7553 +static void free_filldir_node(struct filldir_node *node)
7555 + if (node->namelen >= DNAME_INLINE_LEN)
7556 + kfree(node->name);
7557 + kmem_cache_free(unionfs_filldir_cachep, node);
7560 +void free_rdstate(struct unionfs_dir_state *state)
7562 + struct filldir_node *tmp;
7565 + for (i = 0; i < state->size; i++) {
7566 + struct list_head *head = &(state->list[i]);
7567 + struct list_head *pos, *n;
7569 + /* traverse the list and deallocate space */
7570 + list_for_each_safe(pos, n, head) {
7571 + tmp = list_entry(pos, struct filldir_node, file_list);
7572 + list_del(&tmp->file_list);
7573 + free_filldir_node(tmp);
7580 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7581 + const char *name, int namelen,
7585 + unsigned int hash;
7586 + struct list_head *head;
7587 + struct list_head *pos;
7588 + struct filldir_node *cursor = NULL;
7591 + BUG_ON(namelen <= 0);
7593 + hash = full_name_hash(name, namelen);
7594 + index = hash % rdstate->size;
7596 + head = &(rdstate->list[index]);
7597 + list_for_each(pos, head) {
7598 + cursor = list_entry(pos, struct filldir_node, file_list);
7600 + if (cursor->namelen == namelen && cursor->hash == hash &&
7601 + !strncmp(cursor->name, name, namelen)) {
7603 + * a duplicate exists, and hence no need to create
7604 + * entry to the list
7609 + * if a duplicate is found in this branch, and is
7610 + * not due to the caller looking for an entry to
7611 + * whiteout, then the file system may be corrupted.
7613 + if (unlikely(!is_whiteout &&
7614 + cursor->bindex == rdstate->bindex))
7615 + printk(KERN_ERR "unionfs: filldir: possible "
7616 + "I/O error: a file is duplicated "
7617 + "in the same branch %d: %s\n",
7618 + rdstate->bindex, cursor->name);
7629 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7630 + int namelen, int bindex, int whiteout)
7632 + struct filldir_node *new;
7633 + unsigned int hash;
7636 + struct list_head *head;
7638 + BUG_ON(namelen <= 0);
7640 + hash = full_name_hash(name, namelen);
7641 + index = hash % rdstate->size;
7642 + head = &(rdstate->list[index]);
7644 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7645 + if (unlikely(!new)) {
7650 + INIT_LIST_HEAD(&new->file_list);
7651 + new->namelen = namelen;
7653 + new->bindex = bindex;
7654 + new->whiteout = whiteout;
7656 + if (namelen < DNAME_INLINE_LEN) {
7657 + new->name = new->iname;
7659 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7660 + if (unlikely(!new->name)) {
7661 + kmem_cache_free(unionfs_filldir_cachep, new);
7667 + memcpy(new->name, name, namelen);
7668 + new->name[namelen] = '\0';
7670 + rdstate->hashentries++;
7672 + list_add(&(new->file_list), head);
7676 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7677 new file mode 100644
7678 index 0000000..c8ab910
7680 +++ b/fs/unionfs/rename.c
7683 + * Copyright (c) 2003-2011 Erez Zadok
7684 + * Copyright (c) 2003-2006 Charles P. Wright
7685 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7686 + * Copyright (c) 2005-2006 Junjiro Okajima
7687 + * Copyright (c) 2005 Arun M. Krishnakumar
7688 + * Copyright (c) 2004-2006 David P. Quigley
7689 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7690 + * Copyright (c) 2003 Puja Gupta
7691 + * Copyright (c) 2003 Harikesavan Krishnan
7692 + * Copyright (c) 2003-2011 Stony Brook University
7693 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
7695 + * This program is free software; you can redistribute it and/or modify
7696 + * it under the terms of the GNU General Public License version 2 as
7697 + * published by the Free Software Foundation.
7703 + * This is a helper function for rename, used when rename ends up with hosed
7704 + * over dentries and we need to revert.
7706 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7707 + struct dentry *parent, int bindex)
7709 + struct dentry *lower_dentry;
7710 + struct dentry *lower_parent;
7712 + struct nameidata lower_nd;
7714 + verify_locked(dentry);
7716 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7718 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7720 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
7721 + if (unlikely(err < 0))
7723 + lower_dentry = lookup_one_len_nd(dentry->d_name.name, lower_parent,
7724 + dentry->d_name.len, &lower_nd);
7725 + release_lower_nd(&lower_nd, err);
7726 + if (IS_ERR(lower_dentry)) {
7727 + err = PTR_ERR(lower_dentry);
7731 + dput(unionfs_lower_dentry_idx(dentry, bindex));
7732 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7733 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7735 + if (!lower_dentry->d_inode) {
7736 + dput(lower_dentry);
7737 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7739 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7740 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7741 + igrab(lower_dentry->d_inode));
7748 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7749 + struct dentry *old_parent,
7750 + struct inode *new_dir, struct dentry *new_dentry,
7751 + struct dentry *new_parent,
7755 + struct dentry *lower_old_dentry;
7756 + struct dentry *lower_new_dentry;
7757 + struct dentry *lower_old_dir_dentry;
7758 + struct dentry *lower_new_dir_dentry;
7759 + struct dentry *trap;
7761 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7762 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7764 + if (!lower_new_dentry) {
7765 + lower_new_dentry =
7766 + create_parents(new_parent->d_inode,
7767 + new_dentry, new_dentry->d_name.name,
7769 + if (IS_ERR(lower_new_dentry)) {
7770 + err = PTR_ERR(lower_new_dentry);
7771 + if (IS_COPYUP_ERR(err))
7773 + printk(KERN_ERR "unionfs: error creating directory "
7774 + "tree for rename, bindex=%d err=%d\n",
7780 + /* check for and remove whiteout, if any */
7781 + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7782 + if (err > 0) /* ignore if whiteout found and successfully removed */
7787 + /* check of old_dentry branch is writable */
7788 + err = is_robranch_super(old_dentry->d_sb, bindex);
7792 + dget(lower_old_dentry);
7793 + dget(lower_new_dentry);
7794 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7795 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7797 + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7798 + /* source should not be ancenstor of target */
7799 + if (trap == lower_old_dentry) {
7801 + goto out_err_unlock;
7803 + /* target should not be ancenstor of source */
7804 + if (trap == lower_new_dentry) {
7806 + goto out_err_unlock;
7808 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7809 + lower_new_dir_dentry->d_inode, lower_new_dentry);
7812 + /* update parent dir times */
7813 + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7814 + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7816 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7818 + dput(lower_old_dir_dentry);
7819 + dput(lower_new_dir_dentry);
7820 + dput(lower_old_dentry);
7821 + dput(lower_new_dentry);
7825 + /* Fixup the new_dentry. */
7826 + if (bindex < dbstart(new_dentry))
7827 + dbstart(new_dentry) = bindex;
7828 + else if (bindex > dbend(new_dentry))
7829 + dbend(new_dentry) = bindex;
7836 + * Main rename code. This is sufficiently complex, that it's documented in
7837 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7838 + * __unionfs_rename() above to perform some of the work.
7840 +static int do_unionfs_rename(struct inode *old_dir,
7841 + struct dentry *old_dentry,
7842 + struct dentry *old_parent,
7843 + struct inode *new_dir,
7844 + struct dentry *new_dentry,
7845 + struct dentry *new_parent)
7849 + int old_bstart, old_bend;
7850 + int new_bstart, new_bend;
7851 + int do_copyup = -1;
7852 + int local_err = 0;
7856 + old_bstart = dbstart(old_dentry);
7857 + old_bend = dbend(old_dentry);
7859 + new_bstart = dbstart(new_dentry);
7860 + new_bend = dbend(new_dentry);
7862 + /* Rename source to destination. */
7863 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7864 + new_dir, new_dentry, new_parent,
7867 + if (!IS_COPYUP_ERR(err))
7869 + do_copyup = old_bstart - 1;
7875 + * Unlink all instances of destination that exist to the left of
7876 + * bstart of source. On error, revert back, goto out.
7878 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7879 + struct dentry *unlink_dentry;
7880 + struct dentry *unlink_dir_dentry;
7882 + BUG_ON(bindex < 0);
7883 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7884 + if (!unlink_dentry)
7887 + unlink_dir_dentry = lock_parent(unlink_dentry);
7888 + err = is_robranch_super(old_dir->i_sb, bindex);
7890 + err = vfs_unlink(unlink_dir_dentry->d_inode,
7893 + fsstack_copy_attr_times(new_parent->d_inode,
7894 + unlink_dir_dentry->d_inode);
7895 + /* propagate number of hard-links */
7896 + new_parent->d_inode->i_nlink =
7897 + unionfs_get_nlinks(new_parent->d_inode);
7899 + unlock_dir(unlink_dir_dentry);
7901 + if (bindex != new_bstart) {
7902 + dput(unlink_dentry);
7903 + unionfs_set_lower_dentry_idx(new_dentry,
7906 + } else if (IS_COPYUP_ERR(err)) {
7907 + do_copyup = bindex - 1;
7908 + } else if (revert) {
7913 + if (do_copyup != -1) {
7914 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7916 + * copyup the file into some left directory, so that
7917 + * you can rename it
7919 + err = copyup_dentry(old_parent->d_inode,
7920 + old_dentry, old_bstart, bindex,
7921 + old_dentry->d_name.name,
7922 + old_dentry->d_name.len, NULL,
7923 + i_size_read(old_dentry->d_inode));
7924 + /* if copyup failed, try next branch to the left */
7928 + * create whiteout before calling __unionfs_rename
7929 + * because the latter will change the old_dentry's
7930 + * lower name and parent dir, resulting in the
7931 + * whiteout getting created in the wrong dir.
7933 + err = create_whiteout(old_dentry, bindex);
7935 + printk(KERN_ERR "unionfs: can't create a "
7936 + "whiteout for %s in rename (err=%d)\n",
7937 + old_dentry->d_name.name, err);
7940 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7941 + new_dir, new_dentry, new_parent,
7947 + /* make it opaque */
7948 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7949 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7955 + * Create whiteout for source, only if:
7956 + * (1) There is more than one underlying instance of source.
7957 + * (We did a copy_up is taken care of above).
7959 + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7960 + err = create_whiteout(old_dentry, old_bstart);
7962 + /* can't fix anything now, so we exit with -EIO */
7963 + printk(KERN_ERR "unionfs: can't create a whiteout for "
7964 + "%s in rename!\n", old_dentry->d_name.name);
7973 + /* Do revert here. */
7974 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7977 + printk(KERN_ERR "unionfs: revert failed in rename: "
7978 + "the new refresh failed\n");
7982 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7985 + printk(KERN_ERR "unionfs: revert failed in rename: "
7986 + "the old refresh failed\n");
7991 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7992 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7993 + printk(KERN_ERR "unionfs: revert failed in rename: "
7994 + "the object disappeared from under us!\n");
7999 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
8000 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
8001 + printk(KERN_ERR "unionfs: revert failed in rename: "
8002 + "the object was created underneath us!\n");
8007 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
8008 + old_dir, old_dentry, old_parent,
8011 + /* If we can't fix it, then we cop-out with -EIO. */
8013 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
8017 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
8021 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
8033 + * We can't copyup a directory, because it may involve huge numbers of
8034 + * children, etc. Doing that in the kernel would be bad, so instead we
8035 + * return EXDEV to the user-space utility that caused this, and let the
8036 + * user-space recurse and ask us to copy up each file separately.
8038 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
8042 + err = check_empty(dentry, parent, NULL);
8043 + if (err == -ENOTEMPTY) {
8044 + if (is_robranch(dentry))
8050 + bstart = dbstart(dentry);
8051 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8054 + dbstart(dentry) = bstart + 1;
8055 + err = check_empty(dentry, parent, NULL);
8056 + dbstart(dentry) = bstart;
8057 + if (err == -ENOTEMPTY)
8063 + * The locking rules in unionfs_rename are complex. We could use a simpler
8064 + * superblock-level name-space lock for renames and copy-ups.
8066 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8067 + struct inode *new_dir, struct dentry *new_dentry)
8070 + struct dentry *wh_dentry;
8071 + struct dentry *old_parent, *new_parent;
8074 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
8075 + old_parent = dget_parent(old_dentry);
8076 + new_parent = dget_parent(new_dentry);
8077 + /* un/lock parent dentries only if they differ from old/new_dentry */
8078 + if (old_parent != old_dentry &&
8079 + old_parent != new_dentry)
8080 + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
8081 + if (new_parent != old_dentry &&
8082 + new_parent != new_dentry &&
8083 + new_parent != old_parent)
8084 + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
8085 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8087 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
8092 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
8093 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
8100 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8101 + err = unionfs_partial_lookup(old_dentry, old_parent);
8103 + err = may_rename_dir(old_dentry, old_parent);
8108 + err = unionfs_partial_lookup(new_dentry, new_parent);
8113 + * if new_dentry is already lower because of whiteout,
8114 + * simply override it even if the whited-out dir is not empty.
8116 + wh_dentry = find_first_whiteout(new_dentry);
8117 + if (!IS_ERR(wh_dentry)) {
8119 + } else if (new_dentry->d_inode) {
8120 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8121 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8122 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8123 + -ENOTDIR : -EISDIR;
8127 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8128 + struct unionfs_dir_state *namelist = NULL;
8129 + /* check if this unionfs directory is empty or not */
8130 + err = check_empty(new_dentry, new_parent, &namelist);
8134 + if (!is_robranch(new_dentry))
8135 + err = delete_whiteouts(new_dentry,
8136 + dbstart(new_dentry),
8139 + free_rdstate(namelist);
8146 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8147 + new_dir, new_dentry, new_parent);
8152 + * force re-lookup since the dir on ro branch is not renamed, and
8153 + * lower dentries still indicate the un-renamed ones.
8155 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8156 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8158 + unionfs_postcopyup_release(old_dentry);
8159 + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8160 + unionfs_postcopyup_release(new_dentry);
8161 + unionfs_postcopyup_setmnt(new_dentry);
8162 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8164 + * If we get here, it means that no copyup was
8165 + * needed, and that a file by the old name already
8166 + * existing on the destination branch; that file got
8167 + * renamed earlier in this function, so all we need
8168 + * to do here is set the lower inode.
8170 + struct inode *inode;
8171 + inode = unionfs_lower_inode(old_dentry->d_inode);
8173 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8174 + dbstart(new_dentry),
8178 + /* if all of this renaming succeeded, update our times */
8179 + unionfs_copy_attr_times(old_dentry->d_inode);
8180 + unionfs_copy_attr_times(new_dentry->d_inode);
8181 + unionfs_check_inode(old_dir);
8182 + unionfs_check_inode(new_dir);
8183 + unionfs_check_dentry(old_dentry);
8184 + unionfs_check_dentry(new_dentry);
8187 + if (err) /* clear the new_dentry stuff created */
8188 + d_drop(new_dentry);
8190 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8191 + if (new_parent != old_dentry &&
8192 + new_parent != new_dentry &&
8193 + new_parent != old_parent)
8194 + unionfs_unlock_dentry(new_parent);
8195 + if (old_parent != old_dentry &&
8196 + old_parent != new_dentry)
8197 + unionfs_unlock_dentry(old_parent);
8200 + unionfs_read_unlock(old_dentry->d_sb);
8204 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8205 new file mode 100644
8206 index 0000000..b923742
8208 +++ b/fs/unionfs/sioq.c
8211 + * Copyright (c) 2006-2011 Erez Zadok
8212 + * Copyright (c) 2006 Charles P. Wright
8213 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8214 + * Copyright (c) 2006 Junjiro Okajima
8215 + * Copyright (c) 2006 David P. Quigley
8216 + * Copyright (c) 2006-2011 Stony Brook University
8217 + * Copyright (c) 2006-2011 The Research Foundation of SUNY
8219 + * This program is free software; you can redistribute it and/or modify
8220 + * it under the terms of the GNU General Public License version 2 as
8221 + * published by the Free Software Foundation.
8227 + * Super-user IO work Queue - sometimes we need to perform actions which
8228 + * would fail due to the unix permissions on the parent directory (e.g.,
8229 + * rmdir a directory which appears empty, but in reality contains
8233 +static struct workqueue_struct *superio_workqueue;
8235 +int __init init_sioq(void)
8239 + superio_workqueue = create_workqueue("unionfs_siod");
8240 + if (!IS_ERR(superio_workqueue))
8243 + err = PTR_ERR(superio_workqueue);
8244 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8245 + superio_workqueue = NULL;
8249 +void stop_sioq(void)
8251 + if (superio_workqueue)
8252 + destroy_workqueue(superio_workqueue);
8255 +void run_sioq(work_func_t func, struct sioq_args *args)
8257 + INIT_WORK(&args->work, func);
8259 + init_completion(&args->comp);
8260 + while (!queue_work(superio_workqueue, &args->work)) {
8261 + /* TODO: do accounting if needed */
8264 + wait_for_completion(&args->comp);
8267 +void __unionfs_create(struct work_struct *work)
8269 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8270 + struct create_args *c = &args->create;
8272 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8273 + complete(&args->comp);
8276 +void __unionfs_mkdir(struct work_struct *work)
8278 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8279 + struct mkdir_args *m = &args->mkdir;
8281 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8282 + complete(&args->comp);
8285 +void __unionfs_mknod(struct work_struct *work)
8287 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8288 + struct mknod_args *m = &args->mknod;
8290 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8291 + complete(&args->comp);
8294 +void __unionfs_symlink(struct work_struct *work)
8296 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8297 + struct symlink_args *s = &args->symlink;
8299 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8300 + complete(&args->comp);
8303 +void __unionfs_unlink(struct work_struct *work)
8305 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8306 + struct unlink_args *u = &args->unlink;
8308 + args->err = vfs_unlink(u->parent, u->dentry);
8309 + complete(&args->comp);
8311 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8312 new file mode 100644
8313 index 0000000..c2dfb94
8315 +++ b/fs/unionfs/sioq.h
8318 + * Copyright (c) 2006-2011 Erez Zadok
8319 + * Copyright (c) 2006 Charles P. Wright
8320 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8321 + * Copyright (c) 2006 Junjiro Okajima
8322 + * Copyright (c) 2006 David P. Quigley
8323 + * Copyright (c) 2006-2011 Stony Brook University
8324 + * Copyright (c) 2006-2011 The Research Foundation of SUNY
8326 + * This program is free software; you can redistribute it and/or modify
8327 + * it under the terms of the GNU General Public License version 2 as
8328 + * published by the Free Software Foundation.
8334 +struct deletewh_args {
8335 + struct unionfs_dir_state *namelist;
8336 + struct dentry *dentry;
8340 +struct is_opaque_args {
8341 + struct dentry *dentry;
8344 +struct create_args {
8345 + struct inode *parent;
8346 + struct dentry *dentry;
8348 + struct nameidata *nd;
8351 +struct mkdir_args {
8352 + struct inode *parent;
8353 + struct dentry *dentry;
8357 +struct mknod_args {
8358 + struct inode *parent;
8359 + struct dentry *dentry;
8364 +struct symlink_args {
8365 + struct inode *parent;
8366 + struct dentry *dentry;
8370 +struct unlink_args {
8371 + struct inode *parent;
8372 + struct dentry *dentry;
8377 + struct completion comp;
8378 + struct work_struct work;
8383 + struct deletewh_args deletewh;
8384 + struct is_opaque_args is_opaque;
8385 + struct create_args create;
8386 + struct mkdir_args mkdir;
8387 + struct mknod_args mknod;
8388 + struct symlink_args symlink;
8389 + struct unlink_args unlink;
8393 +/* Extern definitions for SIOQ functions */
8394 +extern int __init init_sioq(void);
8395 +extern void stop_sioq(void);
8396 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8398 +/* Extern definitions for our privilege escalation helpers */
8399 +extern void __unionfs_create(struct work_struct *work);
8400 +extern void __unionfs_mkdir(struct work_struct *work);
8401 +extern void __unionfs_mknod(struct work_struct *work);
8402 +extern void __unionfs_symlink(struct work_struct *work);
8403 +extern void __unionfs_unlink(struct work_struct *work);
8404 +extern void __delete_whiteouts(struct work_struct *work);
8405 +extern void __is_opaque_dir(struct work_struct *work);
8407 +#endif /* not _SIOQ_H */
8408 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8409 new file mode 100644
8410 index 0000000..bdca2f7
8412 +++ b/fs/unionfs/subr.c
8415 + * Copyright (c) 2003-2011 Erez Zadok
8416 + * Copyright (c) 2003-2006 Charles P. Wright
8417 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8418 + * Copyright (c) 2005-2006 Junjiro Okajima
8419 + * Copyright (c) 2005 Arun M. Krishnakumar
8420 + * Copyright (c) 2004-2006 David P. Quigley
8421 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8422 + * Copyright (c) 2003 Puja Gupta
8423 + * Copyright (c) 2003 Harikesavan Krishnan
8424 + * Copyright (c) 2003-2011 Stony Brook University
8425 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
8427 + * This program is free software; you can redistribute it and/or modify
8428 + * it under the terms of the GNU General Public License version 2 as
8429 + * published by the Free Software Foundation.
8435 + * returns the right n_link value based on the inode type
8437 +int unionfs_get_nlinks(const struct inode *inode)
8439 + /* don't bother to do all the work since we're unlinked */
8440 + if (inode->i_nlink == 0)
8443 + if (!S_ISDIR(inode->i_mode))
8444 + return unionfs_lower_inode(inode)->i_nlink;
8447 + * For directories, we return 1. The only place that could cares
8448 + * about links is readdir, and there's d_type there so even that
8454 +/* copy a/m/ctime from the lower branch with the newest times */
8455 +void unionfs_copy_attr_times(struct inode *upper)
8458 + struct inode *lower;
8462 + if (ibstart(upper) < 0) {
8463 +#ifdef CONFIG_UNION_FS_DEBUG
8464 + WARN_ON(ibstart(upper) < 0);
8465 +#endif /* CONFIG_UNION_FS_DEBUG */
8468 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8469 + lower = unionfs_lower_inode_idx(upper, bindex);
8471 + continue; /* not all lower dir objects may exist */
8472 + if (unlikely(timespec_compare(&upper->i_mtime,
8473 + &lower->i_mtime) < 0))
8474 + upper->i_mtime = lower->i_mtime;
8475 + if (unlikely(timespec_compare(&upper->i_ctime,
8476 + &lower->i_ctime) < 0))
8477 + upper->i_ctime = lower->i_ctime;
8478 + if (unlikely(timespec_compare(&upper->i_atime,
8479 + &lower->i_atime) < 0))
8480 + upper->i_atime = lower->i_atime;
8485 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8486 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8487 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8488 + * important if the lower inode is a directory type)
8490 +void unionfs_copy_attr_all(struct inode *dest,
8491 + const struct inode *src)
8493 + dest->i_mode = src->i_mode;
8494 + dest->i_uid = src->i_uid;
8495 + dest->i_gid = src->i_gid;
8496 + dest->i_rdev = src->i_rdev;
8498 + unionfs_copy_attr_times(dest);
8500 + dest->i_blkbits = src->i_blkbits;
8501 + dest->i_flags = src->i_flags;
8504 + * Update the nlinks AFTER updating the above fields, because the
8505 + * get_links callback may depend on them.
8507 + dest->i_nlink = unionfs_get_nlinks(dest);
8509 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8510 new file mode 100644
8511 index 0000000..c3ac814
8513 +++ b/fs/unionfs/super.c
8516 + * Copyright (c) 2003-2011 Erez Zadok
8517 + * Copyright (c) 2003-2006 Charles P. Wright
8518 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8519 + * Copyright (c) 2005-2006 Junjiro Okajima
8520 + * Copyright (c) 2005 Arun M. Krishnakumar
8521 + * Copyright (c) 2004-2006 David P. Quigley
8522 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8523 + * Copyright (c) 2003 Puja Gupta
8524 + * Copyright (c) 2003 Harikesavan Krishnan
8525 + * Copyright (c) 2003-2011 Stony Brook University
8526 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
8528 + * This program is free software; you can redistribute it and/or modify
8529 + * it under the terms of the GNU General Public License version 2 as
8530 + * published by the Free Software Foundation.
8536 + * The inode cache is used with alloc_inode for both our inode info and the
8539 +static struct kmem_cache *unionfs_inode_cachep;
8541 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8544 + struct unionfs_inode_info *info;
8545 + struct inode *inode;
8547 + inode = iget_locked(sb, ino);
8549 + return ERR_PTR(-ENOMEM);
8550 + if (!(inode->i_state & I_NEW))
8553 + info = UNIONFS_I(inode);
8554 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8555 + info->bstart = -1;
8557 + atomic_set(&info->generation,
8558 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8559 + spin_lock_init(&info->rdlock);
8560 + info->rdcount = 1;
8561 + info->hashsize = -1;
8562 + INIT_LIST_HEAD(&info->readdircache);
8564 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8565 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8566 + if (unlikely(!info->lower_inodes)) {
8567 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8568 + "lower-pointer array!\n");
8569 + iget_failed(inode);
8570 + return ERR_PTR(-ENOMEM);
8573 + inode->i_version++;
8574 + inode->i_op = &unionfs_main_iops;
8575 + inode->i_fop = &unionfs_main_fops;
8577 + inode->i_mapping->a_ops = &unionfs_aops;
8580 + * reset times so unionfs_copy_attr_all can keep out time invariants
8581 + * right (upper inode time being the max of all lower ones).
8583 + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8584 + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8585 + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8586 + unlock_new_inode(inode);
8591 + * final actions when unmounting a file system
8593 + * No need to lock rwsem.
8595 +static void unionfs_put_super(struct super_block *sb)
8597 + int bindex, bstart, bend;
8598 + struct unionfs_sb_info *spd;
8601 + spd = UNIONFS_SB(sb);
8605 + bstart = sbstart(sb);
8608 + /* Make sure we have no leaks of branchget/branchput. */
8609 + for (bindex = bstart; bindex <= bend; bindex++)
8610 + if (unlikely(branch_count(sb, bindex) != 0)) {
8612 + "unionfs: branch %d has %d references left!\n",
8613 + bindex, branch_count(sb, bindex));
8616 + WARN_ON(leaks != 0);
8618 + /* decrement lower super references */
8619 + for (bindex = bstart; bindex <= bend; bindex++) {
8620 + struct super_block *s;
8621 + s = unionfs_lower_super_idx(sb, bindex);
8622 + unionfs_set_lower_super_idx(sb, bindex, NULL);
8623 + atomic_dec(&s->s_active);
8626 + kfree(spd->dev_name);
8629 + sb->s_fs_info = NULL;
8633 + * Since people use this to answer the "How big of a file can I write?"
8634 + * question, we report the size of the highest priority branch as the size of
8637 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8640 + struct super_block *sb;
8641 + struct dentry *lower_dentry;
8642 + struct dentry *parent;
8643 + struct path lower_path;
8646 + sb = dentry->d_sb;
8648 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8649 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8650 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8652 + valid = __unionfs_d_revalidate(dentry, parent, false);
8653 + if (unlikely(!valid)) {
8657 + unionfs_check_dentry(dentry);
8659 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8660 + lower_path.dentry = lower_dentry;
8661 + lower_path.mnt = unionfs_mntget(sb->s_root, 0);
8662 + err = vfs_statfs(&lower_path, buf);
8663 + mntput(lower_path.mnt);
8665 + /* set return buf to our f/s to avoid confusing user-level utils */
8666 + buf->f_type = UNIONFS_SUPER_MAGIC;
8668 + * Our maximum file name can is shorter by a few bytes because every
8669 + * file name could potentially be whited-out.
8671 + * XXX: this restriction goes away with ODF.
8673 + unionfs_set_max_namelen(&buf->f_namelen);
8676 + * reset two fields to avoid confusing user-land.
8677 + * XXX: is this still necessary?
8679 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8680 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8683 + unionfs_check_dentry(dentry);
8684 + unionfs_unlock_dentry(dentry);
8685 + unionfs_unlock_parent(dentry, parent);
8686 + unionfs_read_unlock(sb);
8690 +/* handle mode changing during remount */
8691 +static noinline_for_stack int do_remount_mode_option(
8694 + struct unionfs_data *new_data,
8695 + struct path *new_lower_paths)
8697 + int err = -EINVAL;
8699 + char *modename = strchr(optarg, '=');
8702 + /* by now, optarg contains the branch name */
8705 + "unionfs: no branch specified for mode change\n");
8709 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8713 + *modename++ = '\0';
8714 + err = parse_branch_mode(modename, &perms);
8716 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8717 + modename, optarg);
8722 + * Find matching branch index. For now, this assumes that nothing
8723 + * has been mounted on top of this Unionfs stack. Once we have /odf
8724 + * and cache-coherency resolved, we'll address the branch-path
8727 + err = kern_path(optarg, LOOKUP_FOLLOW, &path);
8729 + printk(KERN_ERR "unionfs: error accessing "
8730 + "lower directory \"%s\" (error %d)\n",
8734 + for (idx = 0; idx < cur_branches; idx++)
8735 + if (path.mnt == new_lower_paths[idx].mnt &&
8736 + path.dentry == new_lower_paths[idx].dentry)
8738 + path_put(&path); /* no longer needed */
8739 + if (idx == cur_branches) {
8740 + err = -ENOENT; /* err may have been reset above */
8741 + printk(KERN_ERR "unionfs: branch \"%s\" "
8742 + "not found\n", optarg);
8745 + /* check/change mode for existing branch */
8746 + /* we don't warn if perms==branchperms */
8747 + new_data[idx].branchperms = perms;
8753 +/* handle branch deletion during remount */
8754 +static noinline_for_stack int do_remount_del_option(
8755 + char *optarg, int cur_branches,
8756 + struct unionfs_data *new_data,
8757 + struct path *new_lower_paths)
8759 + int err = -EINVAL;
8763 + /* optarg contains the branch name to delete */
8766 + * Find matching branch index. For now, this assumes that nothing
8767 + * has been mounted on top of this Unionfs stack. Once we have /odf
8768 + * and cache-coherency resolved, we'll address the branch-path
8771 + err = kern_path(optarg, LOOKUP_FOLLOW, &path);
8773 + printk(KERN_ERR "unionfs: error accessing "
8774 + "lower directory \"%s\" (error %d)\n",
8778 + for (idx = 0; idx < cur_branches; idx++)
8779 + if (path.mnt == new_lower_paths[idx].mnt &&
8780 + path.dentry == new_lower_paths[idx].dentry)
8782 + path_put(&path); /* no longer needed */
8783 + if (idx == cur_branches) {
8784 + printk(KERN_ERR "unionfs: branch \"%s\" "
8785 + "not found\n", optarg);
8789 + /* check if there are any open files on the branch to be deleted */
8790 + if (atomic_read(&new_data[idx].open_files) > 0) {
8796 + * Now we have to delete the branch. First, release any handles it
8797 + * has. Then, move the remaining array indexes past "idx" in
8798 + * new_data and new_lower_paths one to the left. Finally, adjust
8801 + path_put(&new_lower_paths[idx]);
8803 + if (idx < cur_branches - 1) {
8804 + /* if idx==cur_branches-1, we delete last branch: easy */
8805 + memmove(&new_data[idx], &new_data[idx+1],
8806 + (cur_branches - 1 - idx) *
8807 + sizeof(struct unionfs_data));
8808 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8809 + (cur_branches - 1 - idx) * sizeof(struct path));
8817 +/* handle branch insertion during remount */
8818 +static noinline_for_stack int do_remount_add_option(
8819 + char *optarg, int cur_branches,
8820 + struct unionfs_data *new_data,
8821 + struct path *new_lower_paths,
8822 + int *high_branch_id)
8824 + int err = -EINVAL;
8826 + int idx = 0; /* default: insert at beginning */
8827 + char *new_branch , *modename = NULL;
8831 + * optarg can be of several forms:
8833 + * /bar:/foo insert /foo before /bar
8834 + * /bar:/foo=ro insert /foo in ro mode before /bar
8835 + * /foo insert /foo in the beginning (prepend)
8836 + * :/foo insert /foo at the end (append)
8838 + if (*optarg == ':') { /* append? */
8839 + new_branch = optarg + 1; /* skip ':' */
8840 + idx = cur_branches;
8841 + goto found_insertion_point;
8843 + new_branch = strchr(optarg, ':');
8844 + if (!new_branch) { /* prepend? */
8845 + new_branch = optarg;
8846 + goto found_insertion_point;
8848 + *new_branch++ = '\0'; /* holds path+mode of new branch */
8851 + * Find matching branch index. For now, this assumes that nothing
8852 + * has been mounted on top of this Unionfs stack. Once we have /odf
8853 + * and cache-coherency resolved, we'll address the branch-path
8856 + err = kern_path(optarg, LOOKUP_FOLLOW, &path);
8858 + printk(KERN_ERR "unionfs: error accessing "
8859 + "lower directory \"%s\" (error %d)\n",
8863 + for (idx = 0; idx < cur_branches; idx++)
8864 + if (path.mnt == new_lower_paths[idx].mnt &&
8865 + path.dentry == new_lower_paths[idx].dentry)
8867 + path_put(&path); /* no longer needed */
8868 + if (idx == cur_branches) {
8869 + printk(KERN_ERR "unionfs: branch \"%s\" "
8870 + "not found\n", optarg);
8876 + * At this point idx will hold the index where the new branch should
8877 + * be inserted before.
8879 +found_insertion_point:
8880 + /* find the mode for the new branch */
8882 + modename = strchr(new_branch, '=');
8884 + *modename++ = '\0';
8885 + if (!new_branch || !*new_branch) {
8886 + printk(KERN_ERR "unionfs: null new branch\n");
8890 + err = parse_branch_mode(modename, &perms);
8892 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8893 + "branch \"%s\"\n", modename, new_branch);
8896 + err = kern_path(new_branch, LOOKUP_FOLLOW, &path);
8898 + printk(KERN_ERR "unionfs: error accessing "
8899 + "lower directory \"%s\" (error %d)\n",
8904 + * It's probably safe to check_mode the new branch to insert. Note:
8905 + * we don't allow inserting branches which are unionfs's by
8906 + * themselves (check_branch returns EINVAL in that case). This is
8907 + * because this code base doesn't support stacking unionfs: the ODF
8908 + * code base supports that correctly.
8910 + err = check_branch(&path);
8912 + printk(KERN_ERR "unionfs: lower directory "
8913 + "\"%s\" is not a valid branch\n", optarg);
8919 + * Now we have to insert the new branch. But first, move the bits
8920 + * to make space for the new branch, if needed. Finally, adjust
8922 + * We don't release nd here; it's kept until umount/remount.
8924 + if (idx < cur_branches) {
8925 + /* if idx==cur_branches, we append: easy */
8926 + memmove(&new_data[idx+1], &new_data[idx],
8927 + (cur_branches - idx) * sizeof(struct unionfs_data));
8928 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8929 + (cur_branches - idx) * sizeof(struct path));
8931 + new_lower_paths[idx].dentry = path.dentry;
8932 + new_lower_paths[idx].mnt = path.mnt;
8934 + new_data[idx].sb = path.dentry->d_sb;
8935 + atomic_set(&new_data[idx].open_files, 0);
8936 + new_data[idx].branchperms = perms;
8937 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8946 + * Support branch management options on remount.
8948 + * See Documentation/filesystems/unionfs/ for details.
8950 + * @flags: numeric mount options
8951 + * @options: mount options string
8953 + * This function can rearrange a mounted union dynamically, adding and
8954 + * removing branches, including changing branch modes. Clearly this has to
8955 + * be done safely and atomically. Luckily, the VFS already calls this
8956 + * function with lock_super(sb) and lock_kernel() held, preventing
8957 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8958 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8959 + * to purge dentries/inodes from our superblock, and also called
8960 + * fsync_super(sb) to purge any dirty pages. So we're good.
8962 + * XXX: however, our remount code may also need to invalidate mapped pages
8963 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8964 + * branches. This has to wait for proper mmap and cache coherency support
8968 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8973 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
8975 + int cur_branches = 0; /* no. of current branches */
8976 + int new_branches = 0; /* no. of branches actually left in the end */
8977 + int add_branches; /* est. no. of branches to add */
8978 + int del_branches; /* est. no. of branches to del */
8979 + int max_branches; /* max possible no. of branches */
8980 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
8981 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
8982 + struct inode **new_lower_inodes = NULL;
8983 + int new_high_branch_id; /* new high branch ID */
8984 + int size; /* memory allocation size, temp var */
8985 + int old_ibstart, old_ibend;
8987 + unionfs_write_lock(sb);
8990 + * The VFS will take care of "ro" and "rw" flags, and we can safely
8991 + * ignore MS_SILENT, but anything else left over is an error. So we
8992 + * need to check if any other flags may have been passed (none are
8993 + * allowed/supported as of now).
8995 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8997 + "unionfs: remount flags 0x%x unsupported\n", *flags);
9003 + * If 'options' is NULL, it's probably because the user just changed
9004 + * the union to a "ro" or "rw" and the VFS took care of it. So
9005 + * nothing to do and we're done.
9007 + if (!options || options[0] == '\0')
9011 + * Find out how many branches we will have in the end, counting
9012 + * "add" and "del" commands. Copy the "options" string because
9013 + * strsep modifies the string and we need it later.
9015 + tmp_to_free = kstrdup(options, GFP_KERNEL);
9016 + optionstmp = tmp_to_free;
9017 + if (unlikely(!optionstmp)) {
9021 + cur_branches = sbmax(sb); /* current no. branches */
9022 + new_branches = sbmax(sb);
9025 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9026 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
9029 + if (!optname || !*optname)
9032 + optarg = strchr(optname, '=');
9036 + if (!strcmp("add", optname))
9038 + else if (!strcmp("del", optname))
9041 + kfree(tmp_to_free);
9042 + /* after all changes, will we have at least one branch left? */
9043 + if ((new_branches + add_branches - del_branches) < 1) {
9045 + "unionfs: no branches left after remount\n");
9051 + * Since we haven't actually parsed all the add/del options, nor
9052 + * have we checked them for errors, we don't know for sure how many
9053 + * branches we will have after all changes have taken place. In
9054 + * fact, the total number of branches left could be less than what
9055 + * we have now. So we need to allocate space for a temporary
9056 + * placeholder that is at least as large as the maximum number of
9057 + * branches we *could* have, which is the current number plus all
9058 + * the additions. Once we're done with these temp placeholders, we
9059 + * may have to re-allocate the final size, copy over from the temp,
9060 + * and then free the temps (done near the end of this function).
9062 + max_branches = cur_branches + add_branches;
9063 + /* allocate space for new pointers to lower dentry */
9064 + tmp_data = kcalloc(max_branches,
9065 + sizeof(struct unionfs_data), GFP_KERNEL);
9066 + if (unlikely(!tmp_data)) {
9070 + /* allocate space for new pointers to lower paths */
9071 + tmp_lower_paths = kcalloc(max_branches,
9072 + sizeof(struct path), GFP_KERNEL);
9073 + if (unlikely(!tmp_lower_paths)) {
9077 + /* copy current info into new placeholders, incrementing refcnts */
9078 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9079 + cur_branches * sizeof(struct unionfs_data));
9080 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9081 + cur_branches * sizeof(struct path));
9082 + for (i = 0; i < cur_branches; i++)
9083 + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9085 + /*******************************************************************
9086 + * For each branch command, do kern_path on the requested branch,
9087 + * and apply the change to a temp branch list. To handle errors, we
9088 + * already dup'ed the old arrays (above), and increased the refcnts
9089 + * on various f/s objects. So now we can do all the kern_path'ss
9090 + * and branch-management commands on the new arrays. If it fail mid
9091 + * way, we free the tmp arrays and *put all objects. If we succeed,
9092 + * then we free old arrays and *put its objects, and then replace
9093 + * the arrays with the new tmp list (we may have to re-allocate the
9094 + * memory because the temp lists could have been larger than what we
9095 + * actually needed).
9096 + *******************************************************************/
9098 + while ((optname = strsep(&options, ",")) != NULL) {
9101 + if (!optname || !*optname)
9104 + * At this stage optname holds a comma-delimited option, but
9105 + * without the commas. Next, we need to break the string on
9106 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9107 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9108 + * KEY is "/foo", and VAL is "rw".
9110 + optarg = strchr(optname, '=');
9113 + /* incgen remount option (instead of old ioctl) */
9114 + if (!strcmp("incgen", optname)) {
9116 + goto out_no_change;
9120 + * All of our options take an argument now. (Insert ones
9121 + * that don't above this check.) So at this stage optname
9122 + * contains the CMD part and optarg contains the ARG part.
9124 + if (!optarg || !*optarg) {
9125 + printk(KERN_ERR "unionfs: all remount options require "
9126 + "an argument (%s)\n", optname);
9131 + if (!strcmp("add", optname)) {
9132 + err = do_remount_add_option(optarg, new_branches,
9135 + &new_high_branch_id);
9139 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9140 + printk(KERN_ERR "unionfs: command exceeds "
9141 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9147 + if (!strcmp("del", optname)) {
9148 + err = do_remount_del_option(optarg, new_branches,
9156 + if (!strcmp("mode", optname)) {
9157 + err = do_remount_mode_option(optarg, new_branches,
9166 + * When you use "mount -o remount,ro", mount(8) will
9167 + * reportedly pass the original dirs= string from
9168 + * /proc/mounts. So for now, we have to ignore dirs= and
9169 + * not consider it an error, unless we want to allow users
9170 + * to pass dirs= in remount. Note that to allow the VFS to
9171 + * actually process the ro/rw remount options, we have to
9172 + * return 0 from this function.
9174 + if (!strcmp("dirs", optname)) {
9175 + printk(KERN_WARNING
9176 + "unionfs: remount ignoring option \"%s\"\n",
9183 + "unionfs: unrecognized option \"%s\"\n", optname);
9189 + /******************************************************************
9190 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9191 + * see if we need to allocate a small-sized new vector, copy the
9192 + * vectors to their correct place, release the refcnt of the older
9193 + * ones, and return. Also handle invalidating any pages that will
9194 + * have to be re-read.
9195 + *******************************************************************/
9197 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9198 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9199 + "(use \"remount,ro\" to create a read-only union)\n");
9204 + /* (re)allocate space for new pointers to lower dentry */
9205 + size = new_branches * sizeof(struct unionfs_data);
9206 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9207 + if (unlikely(!new_data)) {
9212 + /* allocate space for new pointers to lower paths */
9213 + size = new_branches * sizeof(struct path);
9214 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9215 + if (unlikely(!new_lower_paths)) {
9220 + /* allocate space for new pointers to lower inodes */
9221 + new_lower_inodes = kcalloc(new_branches,
9222 + sizeof(struct inode *), GFP_KERNEL);
9223 + if (unlikely(!new_lower_inodes)) {
9229 + * OK, just before we actually put the new set of branches in place,
9230 + * we need to ensure that our own f/s has no dirty objects left.
9231 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9232 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9233 + * pages. So all that's left is for us to invalidate any leftover
9234 + * (non-dirty) pages to ensure that they will be re-read from the
9235 + * new lower branches (and to support mmap).
9239 + * Once we finish the remounting successfully, our superblock
9240 + * generation number will have increased. This will be detected by
9241 + * our dentry-revalidation code upon subsequent f/s operations
9242 + * through unionfs. The revalidation code will rebuild the union of
9243 + * lower inodes for a given unionfs inode and invalidate any pages
9244 + * of such "stale" inodes (by calling our purge_inode_data
9245 + * function). This revalidation will happen lazily and
9246 + * incrementally, as users perform operations on cached inodes. We
9247 + * would like to encourage this revalidation to happen sooner if
9248 + * possible, so we like to try to invalidate as many other pages in
9249 + * our superblock as we can. We used to call drop_pagecache_sb() or
9250 + * a variant thereof, but either method was racy (drop_caches alone
9251 + * is known to be racy). So now we let the revalidation happen on a
9252 + * per file basis in ->d_revalidate.
9255 + /* grab new lower super references; release old ones */
9256 + for (i = 0; i < new_branches; i++)
9257 + atomic_inc(&new_data[i].sb->s_active);
9258 + for (i = 0; i < sbmax(sb); i++)
9259 + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9261 + /* copy new vectors into their correct place */
9262 + tmp_data = UNIONFS_SB(sb)->data;
9263 + UNIONFS_SB(sb)->data = new_data;
9264 + new_data = NULL; /* so don't free good pointers below */
9265 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9266 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9267 + new_lower_paths = NULL; /* so don't free good pointers below */
9269 + /* update our unionfs_sb_info and root dentry index of last branch */
9270 + i = sbmax(sb); /* save no. of branches to release at end */
9271 + sbend(sb) = new_branches - 1;
9272 + dbend(sb->s_root) = new_branches - 1;
9273 + old_ibstart = ibstart(sb->s_root->d_inode);
9274 + old_ibend = ibend(sb->s_root->d_inode);
9275 + ibend(sb->s_root->d_inode) = new_branches - 1;
9276 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9277 + new_branches = i; /* no. of branches to release below */
9280 + * Update lower inodes: 3 steps
9281 + * 1. grab ref on all new lower inodes
9283 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9284 + struct dentry *lower_dentry =
9285 + unionfs_lower_dentry_idx(sb->s_root, i);
9286 + igrab(lower_dentry->d_inode);
9287 + new_lower_inodes[i] = lower_dentry->d_inode;
9289 + /* 2. release reference on all older lower inodes */
9290 + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9291 + /* 3. update root dentry's inode to new lower_inodes array */
9292 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9293 + new_lower_inodes = NULL;
9295 + /* maxbytes may have changed */
9296 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9297 + /* update high branch ID */
9298 + sbhbid(sb) = new_high_branch_id;
9300 + /* update our sb->generation for revalidating objects */
9301 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9302 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9303 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9304 + if (!(*flags & MS_SILENT))
9305 + pr_info("unionfs: %s: new generation number %d\n",
9306 + UNIONFS_SB(sb)->dev_name, i);
9307 + /* finally, update the root dentry's times */
9308 + unionfs_copy_attr_times(sb->s_root->d_inode);
9309 + err = 0; /* reset to success */
9312 + * The code above falls through to the next label, and releases the
9313 + * refcnts of the older ones (stored in tmp_*): if we fell through
9314 + * here, it means success. However, if we jump directly to this
9315 + * label from any error above, then an error occurred after we
9316 + * grabbed various refcnts, and so we have to release the
9317 + * temporarily constructed structures.
9320 + /* no need to cleanup/release anything in tmp_data */
9321 + if (tmp_lower_paths)
9322 + for (i = 0; i < new_branches; i++)
9323 + path_put(&tmp_lower_paths[i]);
9325 + kfree(tmp_lower_paths);
9327 + kfree(new_lower_paths);
9329 + kfree(new_lower_inodes);
9331 + unionfs_check_dentry(sb->s_root);
9332 + unionfs_write_unlock(sb);
9337 + * Called by iput() when the inode reference count reached zero
9338 + * and the inode is not hashed anywhere. Used to clear anything
9339 + * that needs to be, before the inode is completely destroyed and put
9340 + * on the inode free list.
9342 + * No need to lock sb info's rwsem.
9344 +static void unionfs_evict_inode(struct inode *inode)
9346 + int bindex, bstart, bend;
9347 + struct inode *lower_inode;
9348 + struct list_head *pos, *n;
9349 + struct unionfs_dir_state *rdstate;
9351 + truncate_inode_pages(&inode->i_data, 0);
9352 + end_writeback(inode);
9354 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9355 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9356 + list_del(&rdstate->cache);
9357 + free_rdstate(rdstate);
9361 + * Decrement a reference to a lower_inode, which was incremented
9362 + * by our read_inode when it was created initially.
9364 + bstart = ibstart(inode);
9365 + bend = ibend(inode);
9366 + if (bstart >= 0) {
9367 + for (bindex = bstart; bindex <= bend; bindex++) {
9368 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9371 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9372 + /* see Documentation/filesystems/unionfs/issues.txt */
9374 + iput(lower_inode);
9379 + kfree(UNIONFS_I(inode)->lower_inodes);
9380 + UNIONFS_I(inode)->lower_inodes = NULL;
9383 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9385 + struct unionfs_inode_info *i;
9387 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9391 + /* memset everything up to the inode to 0 */
9392 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9394 + i->vfs_inode.i_version = 1;
9395 + return &i->vfs_inode;
9398 +static void unionfs_destroy_inode(struct inode *inode)
9400 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9403 +/* unionfs inode cache constructor */
9404 +static void init_once(void *obj)
9406 + struct unionfs_inode_info *i = obj;
9408 + inode_init_once(&i->vfs_inode);
9411 +int unionfs_init_inode_cache(void)
9415 + unionfs_inode_cachep =
9416 + kmem_cache_create("unionfs_inode_cache",
9417 + sizeof(struct unionfs_inode_info), 0,
9418 + SLAB_RECLAIM_ACCOUNT, init_once);
9419 + if (unlikely(!unionfs_inode_cachep))
9424 +/* unionfs inode cache destructor */
9425 +void unionfs_destroy_inode_cache(void)
9427 + if (unionfs_inode_cachep)
9428 + kmem_cache_destroy(unionfs_inode_cachep);
9432 + * Called when we have a dirty inode, right here we only throw out
9433 + * parts of our readdir list that are too old.
9435 + * No need to grab sb info's rwsem.
9437 +static int unionfs_write_inode(struct inode *inode,
9438 + struct writeback_control *wbc)
9440 + struct list_head *pos, *n;
9441 + struct unionfs_dir_state *rdstate;
9443 + spin_lock(&UNIONFS_I(inode)->rdlock);
9444 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9445 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9446 + /* We keep this list in LRU order. */
9447 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9449 + UNIONFS_I(inode)->rdcount--;
9450 + list_del(&rdstate->cache);
9451 + free_rdstate(rdstate);
9453 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9459 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9460 + * code can actually succeed and won't leave tasks that need handling.
9462 +static void unionfs_umount_begin(struct super_block *sb)
9464 + struct super_block *lower_sb;
9465 + int bindex, bstart, bend;
9467 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9469 + bstart = sbstart(sb);
9471 + for (bindex = bstart; bindex <= bend; bindex++) {
9472 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9474 + if (lower_sb && lower_sb->s_op &&
9475 + lower_sb->s_op->umount_begin)
9476 + lower_sb->s_op->umount_begin(lower_sb);
9479 + unionfs_read_unlock(sb);
9482 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9484 + struct super_block *sb = mnt->mnt_sb;
9488 + int bindex, bstart, bend;
9491 + /* to prevent a silly lockdep warning with namespace_sem */
9493 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9494 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9496 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9497 + if (unlikely(!tmp_page)) {
9502 + bstart = sbstart(sb);
9505 + seq_printf(m, ",dirs=");
9506 + for (bindex = bstart; bindex <= bend; bindex++) {
9508 + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9509 + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9510 + path = d_path(&p, tmp_page, PAGE_SIZE);
9511 + if (IS_ERR(path)) {
9512 + ret = PTR_ERR(path);
9516 + perms = branchperms(sb, bindex);
9518 + seq_printf(m, "%s=%s", path,
9519 + perms & MAY_WRITE ? "rw" : "ro");
9520 + if (bindex != bend)
9521 + seq_printf(m, ":");
9525 + free_page((unsigned long) tmp_page);
9527 + unionfs_unlock_dentry(sb->s_root);
9528 + unionfs_read_unlock(sb);
9534 +struct super_operations unionfs_sops = {
9535 + .put_super = unionfs_put_super,
9536 + .statfs = unionfs_statfs,
9537 + .remount_fs = unionfs_remount_fs,
9538 + .evict_inode = unionfs_evict_inode,
9539 + .umount_begin = unionfs_umount_begin,
9540 + .show_options = unionfs_show_options,
9541 + .write_inode = unionfs_write_inode,
9542 + .alloc_inode = unionfs_alloc_inode,
9543 + .destroy_inode = unionfs_destroy_inode,
9545 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9546 new file mode 100644
9547 index 0000000..1821705
9549 +++ b/fs/unionfs/union.h
9552 + * Copyright (c) 2003-2011 Erez Zadok
9553 + * Copyright (c) 2003-2006 Charles P. Wright
9554 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9555 + * Copyright (c) 2005 Arun M. Krishnakumar
9556 + * Copyright (c) 2004-2006 David P. Quigley
9557 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9558 + * Copyright (c) 2003 Puja Gupta
9559 + * Copyright (c) 2003 Harikesavan Krishnan
9560 + * Copyright (c) 2003-2011 Stony Brook University
9561 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
9563 + * This program is free software; you can redistribute it and/or modify
9564 + * it under the terms of the GNU General Public License version 2 as
9565 + * published by the Free Software Foundation.
9571 +#include <linux/dcache.h>
9572 +#include <linux/file.h>
9573 +#include <linux/list.h>
9574 +#include <linux/fs.h>
9575 +#include <linux/mm.h>
9576 +#include <linux/module.h>
9577 +#include <linux/mount.h>
9578 +#include <linux/namei.h>
9579 +#include <linux/page-flags.h>
9580 +#include <linux/pagemap.h>
9581 +#include <linux/poll.h>
9582 +#include <linux/security.h>
9583 +#include <linux/seq_file.h>
9584 +#include <linux/slab.h>
9585 +#include <linux/spinlock.h>
9586 +#include <linux/statfs.h>
9587 +#include <linux/string.h>
9588 +#include <linux/vmalloc.h>
9589 +#include <linux/writeback.h>
9590 +#include <linux/buffer_head.h>
9591 +#include <linux/xattr.h>
9592 +#include <linux/fs_stack.h>
9593 +#include <linux/magic.h>
9594 +#include <linux/log2.h>
9595 +#include <linux/poison.h>
9596 +#include <linux/mman.h>
9597 +#include <linux/backing-dev.h>
9598 +#include <linux/splice.h>
9599 +#include <linux/sched.h>
9601 +#include <asm/system.h>
9603 +#include <linux/union_fs.h>
9605 +/* the file system name */
9606 +#define UNIONFS_NAME "unionfs"
9608 +/* unionfs root inode number */
9609 +#define UNIONFS_ROOT_INO 1
9611 +/* number of times we try to get a unique temporary file name */
9612 +#define GET_TMPNAM_MAX_RETRY 5
9614 +/* maximum number of branches we support, to avoid memory blowup */
9615 +#define UNIONFS_MAX_BRANCHES 128
9617 +/* minimum time (seconds) required for time-based cache-coherency */
9618 +#define UNIONFS_MIN_CC_TIME 3
9620 +/* Operations vectors defined in specific files. */
9621 +extern struct file_operations unionfs_main_fops;
9622 +extern struct file_operations unionfs_dir_fops;
9623 +extern struct inode_operations unionfs_main_iops;
9624 +extern struct inode_operations unionfs_dir_iops;
9625 +extern struct inode_operations unionfs_symlink_iops;
9626 +extern struct super_operations unionfs_sops;
9627 +extern struct dentry_operations unionfs_dops;
9628 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9629 +extern struct vm_operations_struct unionfs_vm_ops;
9631 +/* How long should an entry be allowed to persist */
9632 +#define RDCACHE_JIFFIES (5*HZ)
9634 +/* compatibility with Real-Time patches */
9635 +#ifdef CONFIG_PREEMPT_RT
9636 +# define unionfs_rw_semaphore compat_rw_semaphore
9637 +#else /* not CONFIG_PREEMPT_RT */
9638 +# define unionfs_rw_semaphore rw_semaphore
9639 +#endif /* not CONFIG_PREEMPT_RT */
9641 +/* file private data. */
9642 +struct unionfs_file_info {
9645 + atomic_t generation;
9647 + struct unionfs_dir_state *rdstate;
9648 + struct file **lower_files;
9649 + int *saved_branch_ids; /* IDs of branches when file was opened */
9650 + const struct vm_operations_struct *lower_vm_ops;
9651 + bool wrote_to_file; /* for delayed copyup */
9654 +/* unionfs inode data in memory */
9655 +struct unionfs_inode_info {
9658 + atomic_t generation;
9659 + /* Stuff for readdir over NFS. */
9660 + spinlock_t rdlock;
9661 + struct list_head readdircache;
9666 + /* The lower inodes */
9667 + struct inode **lower_inodes;
9669 + struct inode vfs_inode;
9672 +/* unionfs dentry data in memory */
9673 +struct unionfs_dentry_info {
9675 + * The semaphore is used to lock the dentry as soon as we get into a
9676 + * unionfs function from the VFS. Our lock ordering is that children
9677 + * go before their parents.
9679 + struct mutex lock;
9684 + atomic_t generation;
9685 + struct path *lower_paths;
9688 +/* These are the pointers to our various objects. */
9689 +struct unionfs_data {
9690 + struct super_block *sb; /* lower super_block */
9691 + atomic_t open_files; /* number of open files on branch */
9693 + int branch_id; /* unique branch ID at re/mount time */
9696 +/* unionfs super-block data in memory */
9697 +struct unionfs_sb_info {
9700 + atomic_t generation;
9703 + * This rwsem is used to make sure that a branch management
9705 + * 1) will not begin before all currently in-flight operations
9707 + * 2) any new operations do not execute until the currently
9708 + * running branch management operation completes.
9710 + * The write_lock_owner records the PID of the task which grabbed
9711 + * the rw_sem for writing. If the same task also tries to grab the
9712 + * read lock, we allow it. This prevents a self-deadlock when
9713 + * branch-management is used on a pivot_root'ed union, because we
9714 + * have to ->lookup paths which belong to the same union.
9716 + struct unionfs_rw_semaphore rwsem;
9717 + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9718 + int high_branch_id; /* last unique branch ID given */
9719 + char *dev_name; /* to identify different unions in pr_debug */
9720 + struct unionfs_data *data;
9724 + * structure for making the linked list of entries by readdir on left branch
9725 + * to compare with entries on right branch
9727 +struct filldir_node {
9728 + struct list_head file_list; /* list for directory entries */
9729 + char *name; /* name entry */
9730 + int hash; /* name hash */
9731 + int namelen; /* name len since name is not 0 terminated */
9734 + * we can check for duplicate whiteouts and files in the same branch
9735 + * in order to return -EIO.
9739 + /* is this a whiteout entry? */
9742 + /* Inline name, so we don't need to separately kmalloc small ones */
9743 + char iname[DNAME_INLINE_LEN];
9746 +/* Directory hash table. */
9747 +struct unionfs_dir_state {
9748 + unsigned int cookie; /* the cookie, based off of rdversion */
9749 + unsigned int offset; /* The entry we have returned. */
9751 + loff_t dirpos; /* offset within the lower level directory */
9752 + int size; /* How big is the hash table? */
9753 + int hashentries; /* How many entries have been inserted? */
9754 + unsigned long access;
9756 + /* This cache list is used when the inode keeps us around. */
9757 + struct list_head cache;
9758 + struct list_head list[0];
9761 +/* externs needed for fanout.h or sioq.h */
9762 +extern int unionfs_get_nlinks(const struct inode *inode);
9763 +extern void unionfs_copy_attr_times(struct inode *upper);
9764 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9766 +/* include miscellaneous macros */
9767 +#include "fanout.h"
9770 +/* externs for cache creation/deletion routines */
9771 +extern void unionfs_destroy_filldir_cache(void);
9772 +extern int unionfs_init_filldir_cache(void);
9773 +extern int unionfs_init_inode_cache(void);
9774 +extern void unionfs_destroy_inode_cache(void);
9775 +extern int unionfs_init_dentry_cache(void);
9776 +extern void unionfs_destroy_dentry_cache(void);
9778 +/* Initialize and free readdir-specific state. */
9779 +extern int init_rdstate(struct file *file);
9780 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9782 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9784 +extern void free_rdstate(struct unionfs_dir_state *state);
9785 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9786 + const char *name, int namelen, int bindex,
9788 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9789 + const char *name, int namelen,
9792 +extern struct dentry **alloc_new_dentries(int objs);
9793 +extern struct unionfs_data *alloc_new_data(int objs);
9795 +/* We can only use 32-bits of offset for rdstate --- blech! */
9796 +#define DIREOF (0xfffff)
9797 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9798 +#define MAXRDCOOKIE (0xfff)
9799 +/* Turn an rdstate into an offset. */
9800 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9804 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9805 + | (buf->offset & DIREOF);
9809 +/* Macros for locking a super_block. */
9810 +enum unionfs_super_lock_class {
9811 + UNIONFS_SMUTEX_NORMAL,
9812 + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9813 + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9815 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9817 + if (UNIONFS_SB(sb)->write_lock_owner &&
9818 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9820 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9822 +static inline void unionfs_read_unlock(struct super_block *sb)
9824 + if (UNIONFS_SB(sb)->write_lock_owner &&
9825 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9827 + up_read(&UNIONFS_SB(sb)->rwsem);
9829 +static inline void unionfs_write_lock(struct super_block *sb)
9831 + down_write(&UNIONFS_SB(sb)->rwsem);
9832 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9834 +static inline void unionfs_write_unlock(struct super_block *sb)
9836 + up_write(&UNIONFS_SB(sb)->rwsem);
9837 + UNIONFS_SB(sb)->write_lock_owner = 0;
9840 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9841 + struct dentry *d2)
9845 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9846 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9848 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9849 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9853 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9854 + struct dentry *d2)
9857 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9858 + unionfs_unlock_dentry(d1);
9859 + unionfs_unlock_dentry(d2);
9861 + unionfs_unlock_dentry(d2);
9862 + unionfs_unlock_dentry(d1);
9866 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9867 + struct dentry *p2)
9870 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9874 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9875 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9877 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9878 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9882 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9883 + struct dentry *p2)
9886 + unionfs_unlock_dentry(p1);
9889 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9890 + unionfs_unlock_dentry(p1);
9891 + unionfs_unlock_dentry(p2);
9893 + unionfs_unlock_dentry(p2);
9894 + unionfs_unlock_dentry(p1);
9898 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9899 +extern int realloc_dentry_private_data(struct dentry *dentry);
9900 +extern void free_dentry_private_data(struct dentry *dentry);
9901 +extern void update_bstart(struct dentry *dentry);
9902 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9903 +extern void release_lower_nd(struct nameidata *nd, int err);
9909 +/* replicates the directory structure up to given dentry in given branch */
9910 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9911 + const char *name, int bindex);
9913 +/* partial lookup */
9914 +extern int unionfs_partial_lookup(struct dentry *dentry,
9915 + struct dentry *parent);
9916 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9917 + struct dentry *parent,
9920 +/* copies a file from dbstart to newbindex branch */
9921 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9922 + int newbindex, loff_t size);
9923 +extern int copyup_named_file(struct inode *dir, struct file *file,
9924 + char *name, int bstart, int new_bindex,
9926 +/* copies a dentry from dbstart to newbindex branch */
9927 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9928 + int bstart, int new_bindex, const char *name,
9929 + int namelen, struct file **copyup_file, loff_t len);
9930 +/* helper functions for post-copyup actions */
9931 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9932 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9934 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9935 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9936 + struct unionfs_dir_state **namelist);
9937 +/* whiteout and opaque directory helpers */
9938 +extern char *alloc_whname(const char *name, int len);
9939 +extern bool is_whiteout_name(char **namep, int *namelenp);
9940 +extern bool is_validname(const char *name);
9941 +extern struct dentry *lookup_whiteout(const char *name,
9942 + struct dentry *lower_parent);
9943 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9944 +extern int unlink_whiteout(struct dentry *wh_dentry);
9945 +extern int check_unlink_whiteout(struct dentry *dentry,
9946 + struct dentry *lower_dentry, int bindex);
9947 +extern int create_whiteout(struct dentry *dentry, int start);
9948 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9949 + struct unionfs_dir_state *namelist);
9950 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9951 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9952 +extern void unionfs_set_max_namelen(long *namelen);
9954 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9955 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9957 +/* Locking functions. */
9958 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9959 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9961 +/* Common file operations. */
9962 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9964 +extern int unionfs_open(struct inode *inode, struct file *file);
9965 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9966 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9967 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9968 + unsigned long arg);
9969 +extern int unionfs_fsync(struct file *file, int datasync);
9970 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9972 +/* Inode operations */
9973 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9974 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9975 + struct inode *new_dir, struct dentry *new_dentry);
9976 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
9977 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
9979 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
9980 + struct dentry *parent, bool willwrite);
9981 +extern bool is_negative_lower(const struct dentry *dentry);
9982 +extern bool is_newer_lower(const struct dentry *dentry);
9983 +extern void purge_sb_data(struct super_block *sb);
9985 +/* The values for unionfs_interpose's flag. */
9986 +#define INTERPOSE_DEFAULT 0
9987 +#define INTERPOSE_LOOKUP 1
9988 +#define INTERPOSE_REVAL 2
9989 +#define INTERPOSE_REVAL_NEG 3
9990 +#define INTERPOSE_PARTIAL 4
9992 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9993 + struct super_block *sb, int flag);
9995 +#ifdef CONFIG_UNION_FS_XATTR
9996 +/* Extended attribute functions. */
9997 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
9998 +static inline void unionfs_xattr_kfree(const void *p)
10002 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
10003 + void *value, size_t size);
10004 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
10005 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
10007 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
10008 + const void *value, size_t size, int flags);
10009 +#endif /* CONFIG_UNION_FS_XATTR */
10011 +/* The root directory is unhashed, but isn't deleted. */
10012 +static inline int d_deleted(struct dentry *d)
10014 + return d_unhashed(d) && (d != d->d_sb->s_root);
10017 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10018 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10020 +/* unionfs_open, check if we need to copyup the file */
10021 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10022 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10024 +static inline int branchperms(const struct super_block *sb, int index)
10026 + BUG_ON(index < 0);
10027 + return UNIONFS_SB(sb)->data[index].branchperms;
10030 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10032 + BUG_ON(index < 0);
10033 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10037 +/* check if readonly lower inode, but possibly unlinked (no inode->i_sb) */
10038 +static inline int __is_rdonly(const struct inode *inode)
10040 + /* if unlinked, can't be readonly (?) */
10041 + if (!inode->i_sb)
10043 + return IS_RDONLY(inode);
10046 +/* Is this file on a read-only branch? */
10047 +static inline int is_robranch_super(const struct super_block *sb, int index)
10051 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10055 +/* Is this file on a read-only branch? */
10056 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10058 + struct super_block *lower_sb;
10060 + BUG_ON(index < 0);
10062 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10065 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10066 + BUG_ON(lower_sb == NULL);
10068 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10069 + * lower_dentry could be a negative.
10071 + if (lower_sb->s_flags & MS_RDONLY)
10077 +static inline int is_robranch(const struct dentry *dentry)
10081 + index = UNIONFS_D(dentry)->bstart;
10082 + BUG_ON(index < 0);
10084 + return is_robranch_idx(dentry, index);
10090 +extern int check_branch(const struct path *path);
10091 +extern int parse_branch_mode(const char *name, int *perms);
10093 +/* locking helpers */
10094 +static inline struct dentry *lock_parent(struct dentry *dentry)
10096 + struct dentry *dir = dget_parent(dentry);
10097 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
10100 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10102 + struct dentry *dir = dget_parent(dentry);
10104 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10108 +static inline void unlock_dir(struct dentry *dir)
10110 + mutex_unlock(&dir->d_inode->i_mutex);
10114 +/* lock base inode mutex before calling lookup_one_len */
10115 +static inline struct dentry *lookup_lck_len(const char *name,
10116 + struct dentry *base, int len)
10118 + struct dentry *d;
10119 + struct nameidata lower_nd;
10122 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
10123 + if (unlikely(err < 0)) {
10124 + d = ERR_PTR(err);
10127 + mutex_lock(&base->d_inode->i_mutex);
10128 + d = lookup_one_len_nd(name, base, len, &lower_nd);
10129 + release_lower_nd(&lower_nd, err);
10130 + mutex_unlock(&base->d_inode->i_mutex);
10135 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10138 + struct vfsmount *mnt;
10140 + BUG_ON(!dentry || bindex < 0);
10142 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10143 +#ifdef CONFIG_UNION_FS_DEBUG
10145 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10147 +#endif /* CONFIG_UNION_FS_DEBUG */
10152 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10154 + struct vfsmount *mnt;
10156 + if (!dentry && bindex < 0)
10158 + BUG_ON(!dentry || bindex < 0);
10160 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10161 +#ifdef CONFIG_UNION_FS_DEBUG
10163 + * Directories can have NULL lower objects in between start/end, but
10164 + * NOT if at the start/end range. We cannot verify that this dentry
10165 + * is a type=DIR, because it may already be a negative dentry. But
10166 + * if dbstart is greater than dbend, we know that this couldn't have
10167 + * been a regular file: it had to have been a directory.
10169 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10170 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10171 +#endif /* CONFIG_UNION_FS_DEBUG */
10175 +#ifdef CONFIG_UNION_FS_DEBUG
10177 +/* useful for tracking code reachability */
10178 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10180 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10181 + __FILE__, __func__, __LINE__)
10182 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10183 + __FILE__, __func__, __LINE__)
10184 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10185 + __FILE__, __func__, __LINE__)
10186 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10187 + __FILE__, __func__, __LINE__)
10188 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10189 + __FILE__, __func__, __LINE__)
10190 +#define show_inode_times(i) __show_inode_times((i), \
10191 + __FILE__, __func__, __LINE__)
10192 +#define show_dinode_times(d) __show_dinode_times((d), \
10193 + __FILE__, __func__, __LINE__)
10194 +#define show_inode_counts(i) __show_inode_counts((i), \
10195 + __FILE__, __func__, __LINE__)
10197 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10198 + const char *fxn, int line);
10199 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10200 + const char *fname, const char *fxn,
10202 +extern void __unionfs_check_file(const struct file *file,
10203 + const char *fname, const char *fxn, int line);
10204 +extern void __unionfs_check_nd(const struct nameidata *nd,
10205 + const char *fname, const char *fxn, int line);
10206 +extern void __show_branch_counts(const struct super_block *sb,
10207 + const char *file, const char *fxn, int line);
10208 +extern void __show_inode_times(const struct inode *inode,
10209 + const char *file, const char *fxn, int line);
10210 +extern void __show_dinode_times(const struct dentry *dentry,
10211 + const char *file, const char *fxn, int line);
10212 +extern void __show_inode_counts(const struct inode *inode,
10213 + const char *file, const char *fxn, int line);
10215 +#else /* not CONFIG_UNION_FS_DEBUG */
10217 +/* we leave useful hooks for these check functions throughout the code */
10218 +#define unionfs_check_inode(i) do { } while (0)
10219 +#define unionfs_check_dentry(d) do { } while (0)
10220 +#define unionfs_check_file(f) do { } while (0)
10221 +#define unionfs_check_nd(n) do { } while (0)
10222 +#define show_branch_counts(sb) do { } while (0)
10223 +#define show_inode_times(i) do { } while (0)
10224 +#define show_dinode_times(d) do { } while (0)
10225 +#define show_inode_counts(i) do { } while (0)
10227 +#endif /* not CONFIG_UNION_FS_DEBUG */
10229 +#endif /* not _UNION_H_ */
10230 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10231 new file mode 100644
10232 index 0000000..bf447bb
10234 +++ b/fs/unionfs/unlink.c
10237 + * Copyright (c) 2003-2011 Erez Zadok
10238 + * Copyright (c) 2003-2006 Charles P. Wright
10239 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10240 + * Copyright (c) 2005-2006 Junjiro Okajima
10241 + * Copyright (c) 2005 Arun M. Krishnakumar
10242 + * Copyright (c) 2004-2006 David P. Quigley
10243 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10244 + * Copyright (c) 2003 Puja Gupta
10245 + * Copyright (c) 2003 Harikesavan Krishnan
10246 + * Copyright (c) 2003-2011 Stony Brook University
10247 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
10249 + * This program is free software; you can redistribute it and/or modify
10250 + * it under the terms of the GNU General Public License version 2 as
10251 + * published by the Free Software Foundation.
10254 +#include "union.h"
10257 + * Helper function for Unionfs's unlink operation.
10259 + * The main goal of this function is to optimize the unlinking of non-dir
10260 + * objects in unionfs by deleting all possible lower inode objects from the
10261 + * underlying branches having same dentry name as the non-dir dentry on
10262 + * which this unlink operation is called. This way we delete as many lower
10263 + * inodes as possible, and save space. Whiteouts need to be created in
10264 + * branch0 only if unlinking fails on any of the lower branch other than
10265 + * branch0, or if a lower branch is marked read-only.
10267 + * Also, while unlinking a file, if we encounter any dir type entry in any
10268 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10269 + * The following special cases are also handled:
10271 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10272 + * appropriate error.
10274 + * (2) If we get an error during unlink in any of other lower branch other
10275 + * than branch0, then we create a whiteout in branch0.
10277 + * (3) If a whiteout already exists in any intermediate branch, we delete
10278 + * all possible inodes only up to that branch (this is an "opaqueness"
10279 + * as as per Documentation/filesystems/unionfs/concepts.txt).
10282 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10283 + struct dentry *parent)
10285 + struct dentry *lower_dentry;
10286 + struct dentry *lower_dir_dentry;
10290 + err = unionfs_partial_lookup(dentry, parent);
10294 + /* trying to unlink all possible valid instances */
10295 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10296 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10297 + if (!lower_dentry || !lower_dentry->d_inode)
10300 + lower_dir_dentry = lock_parent(lower_dentry);
10302 + /* avoid destroying the lower inode if the object is in use */
10303 + dget(lower_dentry);
10304 + err = is_robranch_super(dentry->d_sb, bindex);
10306 + /* see Documentation/filesystems/unionfs/issues.txt */
10308 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10309 + err = vfs_unlink(lower_dir_dentry->d_inode,
10312 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10317 + /* if lower object deletion succeeds, update inode's times */
10319 + unionfs_copy_attr_times(dentry->d_inode);
10320 + dput(lower_dentry);
10321 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10322 + unlock_dir(lower_dir_dentry);
10329 + * Create the whiteout in branch 0 (highest priority) only if (a)
10330 + * there was an error in any intermediate branch other than branch 0
10331 + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10332 + * mounted read-only.
10335 + if ((bindex == 0) ||
10336 + ((bindex == dbstart(dentry)) &&
10337 + (!IS_COPYUP_ERR(err))))
10340 + if (!IS_COPYUP_ERR(err))
10341 + pr_debug("unionfs: lower object deletion "
10342 + "failed in branch:%d\n", bindex);
10343 + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10349 + inode_dec_link_count(dentry->d_inode);
10351 + /* We don't want to leave negative leftover dentries for revalidate. */
10352 + if (!err && (dbopaque(dentry) != -1))
10353 + update_bstart(dentry);
10358 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10361 + struct inode *inode = dentry->d_inode;
10362 + struct dentry *parent;
10365 + BUG_ON(S_ISDIR(inode->i_mode));
10366 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10367 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10368 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10370 + valid = __unionfs_d_revalidate(dentry, parent, false);
10371 + if (unlikely(!valid)) {
10375 + unionfs_check_dentry(dentry);
10377 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10378 + /* call d_drop so the system "forgets" about us */
10380 + unionfs_postcopyup_release(dentry);
10381 + unionfs_postcopyup_setmnt(parent);
10382 + if (inode->i_nlink == 0) /* drop lower inodes */
10383 + iput_lowers_all(inode, false);
10386 + * if unlink/whiteout succeeded, parent dir mtime has
10389 + unionfs_copy_attr_times(dir);
10394 + unionfs_check_dentry(dentry);
10395 + unionfs_check_inode(dir);
10397 + unionfs_unlock_dentry(dentry);
10398 + unionfs_unlock_parent(dentry, parent);
10399 + unionfs_read_unlock(dentry->d_sb);
10403 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10404 + struct unionfs_dir_state *namelist)
10407 + struct dentry *lower_dentry;
10408 + struct dentry *lower_dir_dentry = NULL;
10410 + /* Here we need to remove whiteout entries. */
10411 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10415 + lower_dentry = unionfs_lower_dentry(dentry);
10417 + lower_dir_dentry = lock_parent(lower_dentry);
10419 + /* avoid destroying the lower inode if the file is in use */
10420 + dget(lower_dentry);
10421 + err = is_robranch(dentry);
10423 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10424 + dput(lower_dentry);
10426 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10427 + /* propagate number of hard-links */
10428 + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10431 + if (lower_dir_dentry)
10432 + unlock_dir(lower_dir_dentry);
10436 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10439 + struct unionfs_dir_state *namelist = NULL;
10440 + struct dentry *parent;
10441 + int dstart, dend;
10444 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10445 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10446 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10448 + valid = __unionfs_d_revalidate(dentry, parent, false);
10449 + if (unlikely(!valid)) {
10453 + unionfs_check_dentry(dentry);
10455 + /* check if this unionfs directory is empty or not */
10456 + err = check_empty(dentry, parent, &namelist);
10460 + err = unionfs_rmdir_first(dir, dentry, namelist);
10461 + dstart = dbstart(dentry);
10462 + dend = dbend(dentry);
10464 + * We create a whiteout for the directory if there was an error to
10465 + * rmdir the first directory entry in the union. Otherwise, we
10466 + * create a whiteout only if there is no chance that a lower
10467 + * priority branch might also have the same named directory. IOW,
10468 + * if there is not another same-named directory at a lower priority
10469 + * branch, then we don't need to create a whiteout for it.
10472 + if (dstart < dend)
10473 + err = create_whiteout(dentry, dstart);
10480 + /* exit if the error returned was NOT -EROFS */
10481 + if (!IS_COPYUP_ERR(err))
10484 + new_err = create_whiteout(dentry, dstart - 1);
10485 + if (new_err != -EEXIST)
10491 + * Drop references to lower dentry/inode so storage space for them
10492 + * can be reclaimed. Then, call d_drop so the system "forgets"
10496 + iput_lowers_all(dentry->d_inode, false);
10497 + dput(unionfs_lower_dentry_idx(dentry, dstart));
10498 + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10500 + /* update our lower vfsmnts, in case a copyup took place */
10501 + unionfs_postcopyup_setmnt(dentry);
10502 + unionfs_check_dentry(dentry);
10503 + unionfs_check_inode(dir);
10507 + free_rdstate(namelist);
10509 + unionfs_unlock_dentry(dentry);
10510 + unionfs_unlock_parent(dentry, parent);
10511 + unionfs_read_unlock(dentry->d_sb);
10514 diff --git a/fs/unionfs/whiteout.c b/fs/unionfs/whiteout.c
10515 new file mode 100644
10516 index 0000000..582cef2
10518 +++ b/fs/unionfs/whiteout.c
10521 + * Copyright (c) 2003-2011 Erez Zadok
10522 + * Copyright (c) 2003-2006 Charles P. Wright
10523 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10524 + * Copyright (c) 2005-2006 Junjiro Okajima
10525 + * Copyright (c) 2005 Arun M. Krishnakumar
10526 + * Copyright (c) 2004-2006 David P. Quigley
10527 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10528 + * Copyright (c) 2003 Puja Gupta
10529 + * Copyright (c) 2003 Harikesavan Krishnan
10530 + * Copyright (c) 2003-2011 Stony Brook University
10531 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
10533 + * This program is free software; you can redistribute it and/or modify
10534 + * it under the terms of the GNU General Public License version 2 as
10535 + * published by the Free Software Foundation.
10538 +#include "union.h"
10541 + * whiteout and opaque directory helpers
10544 +/* What do we use for whiteouts. */
10545 +#define UNIONFS_WHPFX ".wh."
10546 +#define UNIONFS_WHLEN 4
10548 + * If a directory contains this file, then it is opaque. We start with the
10549 + * .wh. flag so that it is blocked by lookup.
10551 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10552 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10554 +/* construct whiteout filename */
10555 +char *alloc_whname(const char *name, int len)
10559 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10560 + if (unlikely(!buf))
10561 + return ERR_PTR(-ENOMEM);
10563 + strcpy(buf, UNIONFS_WHPFX);
10564 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10570 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10571 + * code in one place.
10573 +void unionfs_set_max_namelen(long *namelen)
10575 + *namelen -= UNIONFS_WHLEN;
10578 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10579 +bool is_whiteout_name(char **namep, int *namelenp)
10581 + if (*namelenp > UNIONFS_WHLEN &&
10582 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10583 + *namep += UNIONFS_WHLEN;
10584 + *namelenp -= UNIONFS_WHLEN;
10590 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10591 +bool is_validname(const char *name)
10593 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10595 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10596 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10602 + * Look for a whiteout @name in @lower_parent directory. If error, return
10603 + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10605 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10606 + * free then re-malloc calls. Need to provide a different API for those
10609 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10611 + char *whname = NULL;
10612 + int err = 0, namelen;
10613 + struct dentry *wh_dentry = NULL;
10615 + namelen = strlen(name);
10616 + whname = alloc_whname(name, namelen);
10617 + if (unlikely(IS_ERR(whname))) {
10618 + err = PTR_ERR(whname);
10622 + /* check if whiteout exists in this branch: lookup .wh.foo */
10623 + wh_dentry = lookup_lck_len(whname, lower_parent, strlen(whname));
10624 + if (IS_ERR(wh_dentry)) {
10625 + err = PTR_ERR(wh_dentry);
10629 + /* check if negative dentry (ENOENT) */
10630 + if (!wh_dentry->d_inode)
10633 + /* whiteout found: check if valid type */
10634 + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10635 + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10636 + whname, wh_dentry->d_inode->i_mode);
10645 + wh_dentry = ERR_PTR(err);
10646 + return wh_dentry;
10649 +/* find and return first whiteout in parent directory, else ENOENT */
10650 +struct dentry *find_first_whiteout(struct dentry *dentry)
10652 + int bindex, bstart, bend;
10653 + struct dentry *parent, *lower_parent, *wh_dentry;
10655 + parent = dget_parent(dentry);
10657 + bstart = dbstart(parent);
10658 + bend = dbend(parent);
10659 + wh_dentry = ERR_PTR(-ENOENT);
10661 + for (bindex = bstart; bindex <= bend; bindex++) {
10662 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10663 + if (!lower_parent)
10665 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10666 + if (IS_ERR(wh_dentry))
10668 + if (wh_dentry->d_inode)
10671 + wh_dentry = ERR_PTR(-ENOENT);
10676 + return wh_dentry;
10680 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10681 + * release dentry reference.
10683 +int unlink_whiteout(struct dentry *wh_dentry)
10686 + struct dentry *lower_dir_dentry;
10688 + /* dget and lock parent dentry */
10689 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10691 + /* see Documentation/filesystems/unionfs/issues.txt */
10693 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10695 + unlock_dir(lower_dir_dentry);
10698 + * Whiteouts are special files and should be deleted no matter what
10699 + * (as if they never existed), in order to allow this create
10700 + * operation to succeed. This is especially important in sticky
10701 + * directories: a whiteout may have been created by one user, but
10702 + * the newly created file may be created by another user.
10703 + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10704 + * above failed, then we have to try to directly unlink the
10705 + * whiteout. Note: in the ODF version of unionfs, whiteout are
10706 + * handled much more cleanly.
10708 + if (err == -EPERM) {
10709 + struct inode *inode = lower_dir_dentry->d_inode;
10710 + err = inode->i_op->unlink(inode, wh_dentry);
10713 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10714 + "err = %d\n", wh_dentry->d_name.name, err);
10721 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10722 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10723 + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10724 + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10725 + * If it finds both a regular file and a whiteout, delete whiteout (this
10726 + * should never happen).
10728 + * Return 0 if no whiteout was found. Return 1 if one was found and
10729 + * successfully removed. Therefore a value >= 0 tells the caller that
10730 + * @lower_dentry belongs to a good branch to create the new object in).
10731 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10732 + * unlink the whiteout.
10734 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10738 + struct dentry *wh_dentry = NULL;
10739 + struct dentry *lower_dir_dentry = NULL;
10741 + /* look for whiteout dentry first */
10742 + lower_dir_dentry = dget_parent(lower_dentry);
10743 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10744 + dput(lower_dir_dentry);
10745 + if (IS_ERR(wh_dentry)) {
10746 + err = PTR_ERR(wh_dentry);
10750 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10755 + /* check if regular file and whiteout were both found */
10756 + if (unlikely(lower_dentry->d_inode))
10757 + printk(KERN_WARNING "unionfs: removing whiteout; regular "
10758 + "file exists in directory %s (branch %d)\n",
10759 + lower_dir_dentry->d_name.name, bindex);
10761 + /* check if branch is writeable */
10762 + err = is_robranch_super(dentry->d_sb, bindex);
10766 + /* .wh.foo has been found, so let's unlink it */
10767 + err = unlink_whiteout(wh_dentry);
10769 + err = 1; /* a whiteout was found and successfully removed */
10777 + * Pass an unionfs dentry and an index. It will try to create a whiteout
10778 + * for the filename in dentry, and will try in branch 'index'. On error,
10779 + * it will proceed to a branch to the left.
10781 +int create_whiteout(struct dentry *dentry, int start)
10783 + int bstart, bend, bindex;
10784 + struct dentry *lower_dir_dentry;
10785 + struct dentry *lower_dentry;
10786 + struct dentry *lower_wh_dentry;
10787 + struct nameidata nd;
10788 + char *name = NULL;
10789 + int err = -EINVAL;
10791 + verify_locked(dentry);
10793 + bstart = dbstart(dentry);
10794 + bend = dbend(dentry);
10796 + /* create dentry's whiteout equivalent */
10797 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10798 + if (unlikely(IS_ERR(name))) {
10799 + err = PTR_ERR(name);
10803 + for (bindex = start; bindex >= 0; bindex--) {
10804 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10806 + if (!lower_dentry) {
10808 + * if lower dentry is not present, create the
10809 + * entire lower dentry directory structure and go
10810 + * ahead. Since we want to just create whiteout, we
10811 + * only want the parent dentry, and hence get rid of
10814 + lower_dentry = create_parents(dentry->d_inode,
10816 + dentry->d_name.name,
10818 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10819 + int ret = PTR_ERR(lower_dentry);
10820 + if (!IS_COPYUP_ERR(ret))
10822 + "unionfs: create_parents for "
10823 + "whiteout failed: bindex=%d "
10824 + "err=%d\n", bindex, ret);
10829 + lower_wh_dentry =
10830 + lookup_lck_len(name, lower_dentry->d_parent,
10831 + dentry->d_name.len + UNIONFS_WHLEN);
10832 + if (IS_ERR(lower_wh_dentry))
10836 + * The whiteout already exists. This used to be impossible,
10837 + * but now is possible because of opaqueness.
10839 + if (lower_wh_dentry->d_inode) {
10840 + dput(lower_wh_dentry);
10845 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10846 + if (unlikely(err < 0))
10848 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10849 + err = is_robranch_super(dentry->d_sb, bindex);
10851 + err = vfs_create(lower_dir_dentry->d_inode,
10853 + current_umask() & S_IRUGO,
10855 + unlock_dir(lower_dir_dentry);
10856 + dput(lower_wh_dentry);
10857 + release_lower_nd(&nd, err);
10859 + if (!err || !IS_COPYUP_ERR(err))
10863 + /* set dbopaque so that lookup will not proceed after this branch */
10865 + dbopaque(dentry) = bindex;
10873 + * Delete all of the whiteouts in a given directory for rmdir.
10875 + * lower directory inode should be locked
10877 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10878 + struct unionfs_dir_state *namelist)
10881 + struct dentry *lower_dir_dentry = NULL;
10882 + struct dentry *lower_dentry;
10883 + char *name = NULL, *p;
10884 + struct inode *lower_dir;
10886 + struct list_head *pos;
10887 + struct filldir_node *cursor;
10889 + /* Find out lower parent dentry */
10890 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10891 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10892 + lower_dir = lower_dir_dentry->d_inode;
10893 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10896 + name = __getname();
10897 + if (unlikely(!name))
10899 + strcpy(name, UNIONFS_WHPFX);
10900 + p = name + UNIONFS_WHLEN;
10903 + for (i = 0; !err && i < namelist->size; i++) {
10904 + list_for_each(pos, &namelist->list[i]) {
10906 + list_entry(pos, struct filldir_node,
10908 + /* Only operate on whiteouts in this branch. */
10909 + if (cursor->bindex != bindex)
10911 + if (!cursor->whiteout)
10914 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10916 + lookup_lck_len(name, lower_dir_dentry,
10917 + cursor->namelen +
10919 + if (IS_ERR(lower_dentry)) {
10920 + err = PTR_ERR(lower_dentry);
10923 + if (lower_dentry->d_inode)
10924 + err = vfs_unlink(lower_dir, lower_dentry);
10925 + dput(lower_dentry);
10933 + /* After all of the removals, we should copy the attributes once. */
10934 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10941 +void __delete_whiteouts(struct work_struct *work)
10943 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10944 + struct deletewh_args *d = &args->deletewh;
10946 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10947 + complete(&args->comp);
10950 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10951 +int delete_whiteouts(struct dentry *dentry, int bindex,
10952 + struct unionfs_dir_state *namelist)
10955 + struct super_block *sb;
10956 + struct dentry *lower_dir_dentry;
10957 + struct inode *lower_dir;
10958 + struct sioq_args args;
10960 + sb = dentry->d_sb;
10962 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10963 + BUG_ON(bindex < dbstart(dentry));
10964 + BUG_ON(bindex > dbend(dentry));
10965 + err = is_robranch_super(sb, bindex);
10969 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10970 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10971 + lower_dir = lower_dir_dentry->d_inode;
10972 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10974 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10975 + err = do_delete_whiteouts(dentry, bindex, namelist);
10977 + args.deletewh.namelist = namelist;
10978 + args.deletewh.dentry = dentry;
10979 + args.deletewh.bindex = bindex;
10980 + run_sioq(__delete_whiteouts, &args);
10988 +/****************************************************************************
10989 + * Opaque directory helpers *
10990 + ****************************************************************************/
10993 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
10994 + * -errno if an error occurred trying to figure this out.
10996 +int is_opaque_dir(struct dentry *dentry, int bindex)
10999 + struct dentry *lower_dentry;
11000 + struct dentry *wh_lower_dentry;
11001 + struct inode *lower_inode;
11002 + struct sioq_args args;
11003 + struct nameidata lower_nd;
11005 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11006 + lower_inode = lower_dentry->d_inode;
11008 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
11010 + mutex_lock(&lower_inode->i_mutex);
11012 + if (!inode_permission(lower_inode, MAY_EXEC)) {
11013 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
11014 + if (unlikely(err < 0)) {
11015 + mutex_unlock(&lower_inode->i_mutex);
11018 + wh_lower_dentry =
11019 + lookup_one_len_nd(UNIONFS_DIR_OPAQUE, lower_dentry,
11020 + sizeof(UNIONFS_DIR_OPAQUE) - 1,
11022 + release_lower_nd(&lower_nd, err);
11024 + args.is_opaque.dentry = lower_dentry;
11025 + run_sioq(__is_opaque_dir, &args);
11026 + wh_lower_dentry = args.ret;
11029 + mutex_unlock(&lower_inode->i_mutex);
11031 + if (IS_ERR(wh_lower_dentry)) {
11032 + err = PTR_ERR(wh_lower_dentry);
11036 + /* This is an opaque dir iff wh_lower_dentry is positive */
11037 + err = !!wh_lower_dentry->d_inode;
11039 + dput(wh_lower_dentry);
11044 +void __is_opaque_dir(struct work_struct *work)
11046 + struct sioq_args *args = container_of(work, struct sioq_args, work);
11047 + struct nameidata lower_nd;
11050 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
11051 + if (unlikely(err < 0))
11053 + args->ret = lookup_one_len_nd(UNIONFS_DIR_OPAQUE,
11054 + args->is_opaque.dentry,
11055 + sizeof(UNIONFS_DIR_OPAQUE) - 1,
11057 + release_lower_nd(&lower_nd, err);
11058 + complete(&args->comp);
11061 +int make_dir_opaque(struct dentry *dentry, int bindex)
11064 + struct dentry *lower_dentry, *diropq;
11065 + struct inode *lower_dir;
11066 + struct nameidata nd;
11067 + const struct cred *old_creds;
11068 + struct cred *new_creds;
11071 + * Opaque directory whiteout markers are special files (like regular
11072 + * whiteouts), and should appear to the users as if they don't
11073 + * exist. They should be created/deleted regardless of directory
11074 + * search/create permissions, but only for the duration of this
11075 + * creation of the .wh.__dir_opaque: file. Note, this does not
11076 + * circumvent normal ->permission).
11078 + new_creds = prepare_creds();
11079 + if (unlikely(!new_creds)) {
11083 + cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
11084 + cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
11085 + old_creds = override_creds(new_creds);
11087 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11088 + lower_dir = lower_dentry->d_inode;
11089 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
11090 + !S_ISDIR(lower_dir->i_mode));
11092 + mutex_lock(&lower_dir->i_mutex);
11093 + err = init_lower_nd(&nd, LOOKUP_OPEN);
11094 + if (unlikely(err < 0))
11096 + diropq = lookup_one_len_nd(UNIONFS_DIR_OPAQUE, lower_dentry,
11097 + sizeof(UNIONFS_DIR_OPAQUE) - 1, &nd);
11098 + release_lower_nd(&nd, err);
11099 + if (IS_ERR(diropq)) {
11100 + err = PTR_ERR(diropq);
11104 + err = init_lower_nd(&nd, LOOKUP_CREATE);
11105 + if (unlikely(err < 0))
11107 + if (!diropq->d_inode)
11108 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
11110 + dbopaque(dentry) = bindex;
11111 + release_lower_nd(&nd, err);
11116 + mutex_unlock(&lower_dir->i_mutex);
11117 + revert_creds(old_creds);
11121 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
11122 new file mode 100644
11123 index 0000000..a93d803
11125 +++ b/fs/unionfs/xattr.c
11128 + * Copyright (c) 2003-2011 Erez Zadok
11129 + * Copyright (c) 2003-2006 Charles P. Wright
11130 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11131 + * Copyright (c) 2005-2006 Junjiro Okajima
11132 + * Copyright (c) 2005 Arun M. Krishnakumar
11133 + * Copyright (c) 2004-2006 David P. Quigley
11134 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
11135 + * Copyright (c) 2003 Puja Gupta
11136 + * Copyright (c) 2003 Harikesavan Krishnan
11137 + * Copyright (c) 2003-2011 Stony Brook University
11138 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
11140 + * This program is free software; you can redistribute it and/or modify
11141 + * it under the terms of the GNU General Public License version 2 as
11142 + * published by the Free Software Foundation.
11145 +#include "union.h"
11147 +/* This is lifted from fs/xattr.c */
11148 +void *unionfs_xattr_alloc(size_t size, size_t limit)
11152 + if (size > limit)
11153 + return ERR_PTR(-E2BIG);
11155 + if (!size) /* size request, no buffer is needed */
11158 + ptr = kmalloc(size, GFP_KERNEL);
11159 + if (unlikely(!ptr))
11160 + return ERR_PTR(-ENOMEM);
11165 + * BKL held by caller.
11166 + * dentry->d_inode->i_mutex locked
11168 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11171 + struct dentry *lower_dentry = NULL;
11172 + struct dentry *parent;
11173 + int err = -EOPNOTSUPP;
11176 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11177 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11178 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11180 + valid = __unionfs_d_revalidate(dentry, parent, false);
11181 + if (unlikely(!valid)) {
11186 + lower_dentry = unionfs_lower_dentry(dentry);
11188 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11191 + unionfs_check_dentry(dentry);
11192 + unionfs_unlock_dentry(dentry);
11193 + unionfs_unlock_parent(dentry, parent);
11194 + unionfs_read_unlock(dentry->d_sb);
11199 + * BKL held by caller.
11200 + * dentry->d_inode->i_mutex locked
11202 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11203 + const void *value, size_t size, int flags)
11205 + struct dentry *lower_dentry = NULL;
11206 + struct dentry *parent;
11207 + int err = -EOPNOTSUPP;
11210 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11211 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11212 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11214 + valid = __unionfs_d_revalidate(dentry, parent, false);
11215 + if (unlikely(!valid)) {
11220 + lower_dentry = unionfs_lower_dentry(dentry);
11222 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11226 + unionfs_check_dentry(dentry);
11227 + unionfs_unlock_dentry(dentry);
11228 + unionfs_unlock_parent(dentry, parent);
11229 + unionfs_read_unlock(dentry->d_sb);
11234 + * BKL held by caller.
11235 + * dentry->d_inode->i_mutex locked
11237 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11239 + struct dentry *lower_dentry = NULL;
11240 + struct dentry *parent;
11241 + int err = -EOPNOTSUPP;
11244 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11245 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11246 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11248 + valid = __unionfs_d_revalidate(dentry, parent, false);
11249 + if (unlikely(!valid)) {
11254 + lower_dentry = unionfs_lower_dentry(dentry);
11256 + err = vfs_removexattr(lower_dentry, (char *) name);
11259 + unionfs_check_dentry(dentry);
11260 + unionfs_unlock_dentry(dentry);
11261 + unionfs_unlock_parent(dentry, parent);
11262 + unionfs_read_unlock(dentry->d_sb);
11267 + * BKL held by caller.
11268 + * dentry->d_inode->i_mutex locked
11270 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11272 + struct dentry *lower_dentry = NULL;
11273 + struct dentry *parent;
11274 + int err = -EOPNOTSUPP;
11275 + char *encoded_list = NULL;
11278 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11279 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11280 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11282 + valid = __unionfs_d_revalidate(dentry, parent, false);
11283 + if (unlikely(!valid)) {
11288 + lower_dentry = unionfs_lower_dentry(dentry);
11290 + encoded_list = list;
11291 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11294 + unionfs_check_dentry(dentry);
11295 + unionfs_unlock_dentry(dentry);
11296 + unionfs_unlock_parent(dentry, parent);
11297 + unionfs_read_unlock(dentry->d_sb);
11300 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
11301 index da317c7..64f1ced 100644
11302 --- a/include/linux/fs_stack.h
11303 +++ b/include/linux/fs_stack.h
11306 + * Copyright (c) 2006-2009 Erez Zadok
11307 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11308 + * Copyright (c) 2006-2009 Stony Brook University
11309 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11311 + * This program is free software; you can redistribute it and/or modify
11312 + * it under the terms of the GNU General Public License version 2 as
11313 + * published by the Free Software Foundation.
11316 #ifndef _LINUX_FS_STACK_H
11317 #define _LINUX_FS_STACK_H
11319 -/* This file defines generic functions used primarily by stackable
11321 + * This file defines generic functions used primarily by stackable
11322 * filesystems; none of these functions require i_mutex to be held.
11325 diff --git a/include/linux/magic.h b/include/linux/magic.h
11326 index 1e5df2a..01ee54d 100644
11327 --- a/include/linux/magic.h
11328 +++ b/include/linux/magic.h
11330 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11331 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11333 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11335 #define SMB_SUPER_MAGIC 0x517B
11336 #define USBDEVICE_SUPER_MAGIC 0x9fa2
11337 #define CGROUP_SUPER_MAGIC 0x27e0eb
11338 diff --git a/include/linux/namei.h b/include/linux/namei.h
11339 index eba45ea..8e19e9c 100644
11340 --- a/include/linux/namei.h
11341 +++ b/include/linux/namei.h
11342 @@ -81,8 +81,11 @@ extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
11344 extern struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
11345 int (*open)(struct inode *, struct file *));
11346 +extern void release_open_intent(struct nameidata *);
11348 extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
11349 +extern struct dentry *lookup_one_len_nd(const char *, struct dentry *, int,
11350 + struct nameidata *nd);
11352 extern int follow_down_one(struct path *);
11353 extern int follow_down(struct path *);
11354 diff --git a/include/linux/splice.h b/include/linux/splice.h
11355 index 997c3b4..54f5501 100644
11356 --- a/include/linux/splice.h
11357 +++ b/include/linux/splice.h
11358 @@ -81,6 +81,11 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
11359 struct splice_pipe_desc *);
11360 extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11361 splice_direct_actor *);
11362 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11363 + loff_t *ppos, size_t len, unsigned int flags);
11364 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11365 + struct pipe_inode_info *pipe, size_t len,
11366 + unsigned int flags);
11369 * for dynamic pipe sizing
11370 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
11371 new file mode 100644
11372 index 0000000..c84d97e
11374 +++ b/include/linux/union_fs.h
11377 + * Copyright (c) 2003-2009 Erez Zadok
11378 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11379 + * Copyright (c) 2003-2009 Stony Brook University
11380 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11382 + * This program is free software; you can redistribute it and/or modify
11383 + * it under the terms of the GNU General Public License version 2 as
11384 + * published by the Free Software Foundation.
11387 +#ifndef _LINUX_UNION_FS_H
11388 +#define _LINUX_UNION_FS_H
11391 + * DEFINITIONS FOR USER AND KERNEL CODE:
11393 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11394 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11396 +#endif /* _LINUX_UNIONFS_H */
11398 diff --git a/security/security.c b/security/security.c
11399 index 4ba6d4c..093d8b4 100644
11400 --- a/security/security.c
11401 +++ b/security/security.c
11402 @@ -520,6 +520,7 @@ int security_inode_permission(struct inode *inode, int mask)
11404 return security_ops->inode_permission(inode, mask, 0);
11406 +EXPORT_SYMBOL(security_inode_permission);
11408 int security_inode_exec_permission(struct inode *inode, unsigned int flags)