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 9a648eb..81b24a8 100644
538 @@ -6765,6 +6765,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 d621f02..c12677d 100644
557 @@ -194,6 +194,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 93804d4..3ff10bb 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 208c6aa..050eded 100644
581 @@ -491,6 +491,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 @@ -1804,6 +1805,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_empty(int dfd, const char __user *name, unsigned flags,
630 struct path *path, int *empty)
632 @@ -3384,6 +3421,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 1ec0493..3215728 100644
644 @@ -1084,8 +1084,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 @@ -1108,13 +1108,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 @@ -1134,6 +1135,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 @@ -1203,7 +1205,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 @@ -1262,8 +1264,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 @@ -1348,7 +1350,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 @@ -1368,7 +1370,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/unionfs/Kconfig b/fs/unionfs/Kconfig
721 index 0000000..f3c1ac4
723 +++ b/fs/unionfs/Kconfig
726 + tristate "Union file system (EXPERIMENTAL)"
727 + depends on EXPERIMENTAL
729 + Unionfs is a stackable unification file system, which appears to
730 + merge the contents of several directories (branches), while keeping
731 + their physical content separate.
733 + See <http://unionfs.filesystems.org> for details
735 +config UNION_FS_XATTR
736 + bool "Unionfs extended attributes"
737 + depends on UNION_FS
739 + Extended attributes are name:value pairs associated with inodes by
740 + the kernel or by users (see the attr(5) manual page).
744 +config UNION_FS_DEBUG
745 + bool "Debug Unionfs"
746 + depends on UNION_FS
748 + If you say Y here, you can turn on debugging output from Unionfs.
749 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
751 index 0000000..60b6060
753 +++ b/fs/unionfs/Makefile
755 +UNIONFS_VERSION="2.5.11 (for 3.3.0-rc3)"
757 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
759 +obj-$(CONFIG_UNION_FS) += unionfs.o
761 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
762 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
763 + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
765 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
767 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
769 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
770 +EXTRA_CFLAGS += -DDEBUG
772 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
774 index 0000000..71cacfe
776 +++ b/fs/unionfs/commonfops.c
779 + * Copyright (c) 2003-2011 Erez Zadok
780 + * Copyright (c) 2003-2006 Charles P. Wright
781 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
782 + * Copyright (c) 2005-2006 Junjiro Okajima
783 + * Copyright (c) 2005 Arun M. Krishnakumar
784 + * Copyright (c) 2004-2006 David P. Quigley
785 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
786 + * Copyright (c) 2003 Puja Gupta
787 + * Copyright (c) 2003 Harikesavan Krishnan
788 + * Copyright (c) 2003-2011 Stony Brook University
789 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
791 + * This program is free software; you can redistribute it and/or modify
792 + * it under the terms of the GNU General Public License version 2 as
793 + * published by the Free Software Foundation.
799 + * 1) Copyup the file
800 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
801 + * stolen from NFS's silly rename
803 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
804 + struct dentry *parent, int bstart, int bindex)
806 + static unsigned int counter;
807 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
808 + const int countersize = sizeof(counter) * 2;
809 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
810 + char name[nlen + 1];
812 + struct dentry *tmp_dentry = NULL;
813 + struct dentry *lower_dentry;
814 + struct dentry *lower_dir_dentry = NULL;
816 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
818 + sprintf(name, ".unionfs%*.*lx",
819 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
822 + * Loop, looking for an unused temp name to copyup to.
824 + * It's somewhat silly that we look for a free temp tmp name in the
825 + * source branch (bstart) instead of the dest branch (bindex), where
826 + * the final name will be created. We _will_ catch it if somehow
827 + * the name exists in the dest branch, but it'd be nice to catch it
828 + * sooner than later.
833 + char *suffix = name + nlen - countersize;
837 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
839 + pr_debug("unionfs: trying to rename %s to %s\n",
840 + dentry->d_name.name, name);
842 + tmp_dentry = lookup_lck_len(name, lower_dentry->d_parent,
844 + if (IS_ERR(tmp_dentry)) {
845 + err = PTR_ERR(tmp_dentry);
848 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
851 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
852 + i_size_read(file->f_path.dentry->d_inode));
854 + if (unlikely(err == -EEXIST))
859 + /* bring it to the same state as an unlinked file */
860 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
861 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
862 + atomic_inc(&lower_dentry->d_inode->i_count);
863 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
864 + lower_dentry->d_inode);
866 + lower_dir_dentry = lock_parent(lower_dentry);
867 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
868 + unlock_dir(lower_dir_dentry);
872 + unionfs_check_dentry(dentry);
877 + * put all references held by upper struct file and free lower file pointer
880 +static void cleanup_file(struct file *file)
882 + int bindex, bstart, bend;
883 + struct file **lower_files;
884 + struct file *lower_file;
885 + struct super_block *sb = file->f_path.dentry->d_sb;
887 + lower_files = UNIONFS_F(file)->lower_files;
888 + bstart = fbstart(file);
889 + bend = fbend(file);
891 + for (bindex = bstart; bindex <= bend; bindex++) {
892 + int i; /* holds (possibly) updated branch index */
895 + lower_file = unionfs_lower_file_idx(file, bindex);
900 + * Find new index of matching branch with an open
901 + * file, since branches could have been added or
902 + * deleted causing the one with open files to shift.
904 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
905 + i = branch_id_to_idx(sb, old_bid);
906 + if (unlikely(i < 0)) {
907 + printk(KERN_ERR "unionfs: no superblock for "
908 + "file %p\n", file);
912 + /* decrement count of open files */
915 + * fput will perform an mntput for us on the correct branch.
916 + * Although we're using the file's old branch configuration,
917 + * bindex, which is the old index, correctly points to the
918 + * right branch in the file's branch list. In other words,
919 + * we're going to mntput the correct branch even if branches
920 + * have been added/removed.
923 + UNIONFS_F(file)->lower_files[bindex] = NULL;
924 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
927 + UNIONFS_F(file)->lower_files = NULL;
928 + kfree(lower_files);
929 + kfree(UNIONFS_F(file)->saved_branch_ids);
930 + /* set to NULL because caller needs to know if to kfree on error */
931 + UNIONFS_F(file)->saved_branch_ids = NULL;
934 +/* open all lower files for a given file */
935 +static int open_all_files(struct file *file)
937 + int bindex, bstart, bend, err = 0;
938 + struct file *lower_file;
939 + struct dentry *lower_dentry;
940 + struct dentry *dentry = file->f_path.dentry;
941 + struct super_block *sb = dentry->d_sb;
943 + bstart = dbstart(dentry);
944 + bend = dbend(dentry);
946 + for (bindex = bstart; bindex <= bend; bindex++) {
947 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
951 + dget(lower_dentry);
952 + unionfs_mntget(dentry, bindex);
953 + branchget(sb, bindex);
956 + dentry_open(lower_dentry,
957 + unionfs_lower_mnt_idx(dentry, bindex),
958 + file->f_flags, current_cred());
959 + if (IS_ERR(lower_file)) {
960 + branchput(sb, bindex);
961 + err = PTR_ERR(lower_file);
964 + unionfs_set_lower_file_idx(file, bindex, lower_file);
971 +/* open the highest priority file for a given upper file */
972 +static int open_highest_file(struct file *file, bool willwrite)
974 + int bindex, bstart, bend, err = 0;
975 + struct file *lower_file;
976 + struct dentry *lower_dentry;
977 + struct dentry *dentry = file->f_path.dentry;
978 + struct dentry *parent = dget_parent(dentry);
979 + struct inode *parent_inode = parent->d_inode;
980 + struct super_block *sb = dentry->d_sb;
982 + bstart = dbstart(dentry);
983 + bend = dbend(dentry);
985 + lower_dentry = unionfs_lower_dentry(dentry);
986 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
987 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
988 + err = copyup_file(parent_inode, file, bstart, bindex,
989 + i_size_read(dentry->d_inode));
993 + atomic_set(&UNIONFS_F(file)->generation,
994 + atomic_read(&UNIONFS_I(dentry->d_inode)->
999 + dget(lower_dentry);
1000 + unionfs_mntget(dentry, bstart);
1001 + lower_file = dentry_open(lower_dentry,
1002 + unionfs_lower_mnt_idx(dentry, bstart),
1003 + file->f_flags, current_cred());
1004 + if (IS_ERR(lower_file)) {
1005 + err = PTR_ERR(lower_file);
1008 + branchget(sb, bstart);
1009 + unionfs_set_lower_file(file, lower_file);
1010 + /* Fix up the position. */
1011 + lower_file->f_pos = file->f_pos;
1013 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1019 +/* perform a delayed copyup of a read-write file on a read-only branch */
1020 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1022 + int bindex, bstart, bend, err = 0;
1023 + struct dentry *dentry = file->f_path.dentry;
1024 + struct inode *parent_inode = parent->d_inode;
1026 + bstart = fbstart(file);
1027 + bend = fbend(file);
1029 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1031 + unionfs_check_file(file);
1032 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1033 + if (!d_deleted(dentry))
1034 + err = copyup_file(parent_inode, file, bstart,
1036 + i_size_read(dentry->d_inode));
1038 + err = copyup_deleted_file(file, dentry, parent,
1040 + /* if succeeded, set lower open-file flags and break */
1042 + struct file *lower_file;
1043 + lower_file = unionfs_lower_file_idx(file, bindex);
1044 + lower_file->f_flags = file->f_flags;
1048 + if (err || (bstart <= fbstart(file)))
1050 + bend = fbend(file);
1051 + for (bindex = bstart; bindex <= bend; bindex++) {
1052 + if (unionfs_lower_file_idx(file, bindex)) {
1053 + branchput(dentry->d_sb, bindex);
1054 + fput(unionfs_lower_file_idx(file, bindex));
1055 + unionfs_set_lower_file_idx(file, bindex, NULL);
1058 + path_put_lowers(dentry, bstart, bend, false);
1059 + iput_lowers(dentry->d_inode, bstart, bend, false);
1060 + /* for reg file, we only open it "once" */
1061 + fbend(file) = fbstart(file);
1062 + dbend(dentry) = dbstart(dentry);
1063 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1066 + unionfs_check_file(file);
1071 + * Helper function for unionfs_file_revalidate/locked.
1072 + * Expects dentry/parent to be locked already, and revalidated.
1074 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1075 + struct dentry *parent,
1076 + struct super_block *sb, int sbgen,
1077 + int dgen, bool willwrite)
1080 + int bstart, bend, orig_brid;
1084 + fgen = atomic_read(&UNIONFS_F(file)->generation);
1087 + * There are two cases we are interested in. The first is if the
1088 + * generation is lower than the super-block. The second is if
1089 + * someone has copied up this file from underneath us, we also need
1090 + * to refresh things.
1092 + if (d_deleted(dentry) ||
1094 + dbstart(dentry) == fbstart(file) &&
1095 + unionfs_lower_file(file)))
1096 + goto out_may_copyup;
1098 + /* save orig branch ID */
1099 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1101 + /* First we throw out the existing files. */
1102 + cleanup_file(file);
1104 + /* Now we reopen the file(s) as in unionfs_open. */
1105 + bstart = fbstart(file) = dbstart(dentry);
1106 + bend = fbend(file) = dbend(dentry);
1108 + size = sizeof(struct file *) * sbmax(sb);
1109 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1110 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1114 + size = sizeof(int) * sbmax(sb);
1115 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1116 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1121 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1122 + /* We need to open all the files. */
1123 + err = open_all_files(file);
1128 + /* We only open the highest priority branch. */
1129 + err = open_highest_file(file, willwrite);
1132 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1133 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1135 + * If we re-opened the file on a different branch
1136 + * than the original one, and this was due to a new
1137 + * branch inserted, then update the mnt counts of
1138 + * the old and new branches accordingly.
1140 + unionfs_mntget(dentry, bstart);
1141 + unionfs_mntput(sb->s_root,
1142 + branch_id_to_idx(sb, orig_brid));
1144 + /* regular files have only one open lower file */
1145 + fbend(file) = fbstart(file);
1147 + atomic_set(&UNIONFS_F(file)->generation,
1148 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1151 + /* Copyup on the first write to a file on a readonly branch. */
1152 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1153 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1154 + is_robranch(dentry)) {
1155 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1156 + dentry->d_name.name);
1157 + err = do_delayed_copyup(file, parent);
1158 + /* regular files have only one open lower file */
1159 + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1160 + fbend(file) = fbstart(file);
1165 + kfree(UNIONFS_F(file)->lower_files);
1166 + kfree(UNIONFS_F(file)->saved_branch_ids);
1172 + * Revalidate the struct file
1173 + * @file: file to revalidate
1174 + * @parent: parent dentry (locked by caller)
1175 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1176 + * Caller must lock/unlock dentry's branch configuration.
1178 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1181 + struct super_block *sb;
1182 + struct dentry *dentry;
1186 + dentry = file->f_path.dentry;
1187 + sb = dentry->d_sb;
1188 + verify_locked(dentry);
1189 + verify_locked(parent);
1192 + * First revalidate the dentry inside struct file,
1193 + * but not unhashed dentries.
1195 + if (!d_deleted(dentry) &&
1196 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1201 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1202 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1204 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1205 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1206 + dentry->d_name.name);
1211 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1212 + sbgen, dgen, willwrite);
1217 +/* unionfs_open helper function: open a directory */
1218 +static int __open_dir(struct inode *inode, struct file *file,
1219 + struct dentry *parent)
1221 + struct dentry *lower_dentry;
1222 + struct file *lower_file;
1223 + int bindex, bstart, bend;
1224 + struct vfsmount *lower_mnt;
1225 + struct dentry *dentry = file->f_path.dentry;
1227 + bstart = fbstart(file) = dbstart(dentry);
1228 + bend = fbend(file) = dbend(dentry);
1230 + for (bindex = bstart; bindex <= bend; bindex++) {
1232 + unionfs_lower_dentry_idx(dentry, bindex);
1233 + if (!lower_dentry)
1236 + dget(lower_dentry);
1237 + lower_mnt = unionfs_mntget(dentry, bindex);
1239 + lower_mnt = unionfs_mntget(parent, bindex);
1240 + lower_file = dentry_open(lower_dentry, lower_mnt, file->f_flags,
1242 + if (IS_ERR(lower_file))
1243 + return PTR_ERR(lower_file);
1245 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1246 + if (!unionfs_lower_mnt_idx(dentry, bindex))
1247 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
1250 + * The branchget goes after the open, because otherwise
1251 + * we would miss the reference on release.
1253 + branchget(inode->i_sb, bindex);
1259 +/* unionfs_open helper function: open a file */
1260 +static int __open_file(struct inode *inode, struct file *file,
1261 + struct dentry *parent)
1263 + struct dentry *lower_dentry;
1264 + struct file *lower_file;
1266 + int bindex, bstart, bend;
1267 + struct dentry *dentry = file->f_path.dentry;
1268 + struct vfsmount *lower_mnt;
1270 + lower_dentry = unionfs_lower_dentry(dentry);
1271 + lower_flags = file->f_flags;
1273 + bstart = fbstart(file) = dbstart(dentry);
1274 + bend = fbend(file) = dbend(dentry);
1277 + * check for the permission for lower file. If the error is
1278 + * COPYUP_ERR, copyup the file.
1280 + if (lower_dentry->d_inode && is_robranch(dentry)) {
1282 + * if the open will change the file, copy it up otherwise
1285 + if (lower_flags & O_TRUNC) {
1289 + /* copyup the file */
1290 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1291 + err = copyup_file(parent->d_inode, file,
1292 + bstart, bindex, size);
1294 + /* only one regular file open */
1295 + fbend(file) = fbstart(file);
1302 + * turn off writeable flags, to force delayed copyup
1305 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1309 + dget(lower_dentry);
1312 + * dentry_open will decrement mnt refcnt if err.
1313 + * otherwise fput() will do an mntput() for us upon file close.
1315 + lower_mnt = unionfs_mntget(dentry, bstart);
1316 + lower_file = dentry_open(lower_dentry, lower_mnt, lower_flags,
1318 + if (IS_ERR(lower_file))
1319 + return PTR_ERR(lower_file);
1321 + unionfs_set_lower_file(file, lower_file);
1322 + branchget(inode->i_sb, bstart);
1327 +int unionfs_open(struct inode *inode, struct file *file)
1330 + struct file *lower_file = NULL;
1331 + struct dentry *dentry = file->f_path.dentry;
1332 + struct dentry *parent;
1333 + int bindex = 0, bstart = 0, bend = 0;
1337 + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1338 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1339 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1341 + /* don't open unhashed/deleted files */
1342 + if (d_deleted(dentry)) {
1347 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1348 + valid = __unionfs_d_revalidate(dentry, parent, false);
1349 + if (unlikely(!valid)) {
1354 + file->private_data =
1355 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1356 + if (unlikely(!UNIONFS_F(file))) {
1360 + fbstart(file) = -1;
1362 + atomic_set(&UNIONFS_F(file)->generation,
1363 + atomic_read(&UNIONFS_I(inode)->generation));
1365 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1366 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1367 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1371 + size = sizeof(int) * sbmax(inode->i_sb);
1372 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1373 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1378 + bstart = fbstart(file) = dbstart(dentry);
1379 + bend = fbend(file) = dbend(dentry);
1382 + * open all directories and make the unionfs file struct point to
1383 + * these lower file structs
1385 + if (S_ISDIR(inode->i_mode))
1386 + err = __open_dir(inode, file, parent); /* open a dir */
1388 + err = __open_file(inode, file, parent); /* open a file */
1390 + /* freeing the allocated resources, and fput the opened files */
1392 + for (bindex = bstart; bindex <= bend; bindex++) {
1393 + lower_file = unionfs_lower_file_idx(file, bindex);
1397 + branchput(dentry->d_sb, bindex);
1398 + /* fput calls dput for lower_dentry */
1405 + kfree(UNIONFS_F(file)->lower_files);
1406 + kfree(UNIONFS_F(file)->saved_branch_ids);
1407 + kfree(UNIONFS_F(file));
1411 + unionfs_postcopyup_setmnt(dentry);
1412 + unionfs_copy_attr_times(inode);
1413 + unionfs_check_file(file);
1414 + unionfs_check_inode(inode);
1416 + unionfs_unlock_dentry(dentry);
1417 + unionfs_unlock_parent(dentry, parent);
1418 + unionfs_read_unlock(inode->i_sb);
1423 + * release all lower object references & free the file info structure
1425 + * No need to grab sb info's rwsem.
1427 +int unionfs_file_release(struct inode *inode, struct file *file)
1429 + struct file *lower_file = NULL;
1430 + struct unionfs_file_info *fileinfo;
1431 + struct unionfs_inode_info *inodeinfo;
1432 + struct super_block *sb = inode->i_sb;
1433 + struct dentry *dentry = file->f_path.dentry;
1434 + struct dentry *parent;
1435 + int bindex, bstart, bend;
1439 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
1440 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
1441 + * has been causing false positives in file system stacking layers.
1442 + * In particular, our ->mmap is called after sys_mmap2 already holds
1443 + * mmap_sem, then we lock our own mutexes; but earlier, it's
1444 + * possible for lockdep to have locked our mutexes first, and then
1445 + * we call a lower ->readdir which could call might_fault. The
1446 + * different ordering of the locks is what lockdep complains about
1447 + * -- unnecessarily. Therefore, we have no choice but to tell
1448 + * lockdep to temporarily turn off lockdep here. Note: the comments
1449 + * inside might_sleep also suggest that it would have been
1450 + * nicer to only annotate paths that needs that might_lock_read.
1453 + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1454 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1455 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1458 + * We try to revalidate, but the VFS ignores return return values
1459 + * from file->release, so we must always try to succeed here,
1460 + * including to do the kfree and dput below. So if revalidation
1461 + * failed, all we can do is print some message and keep going.
1463 + err = unionfs_file_revalidate(file, parent,
1464 + UNIONFS_F(file)->wrote_to_file);
1466 + unionfs_check_file(file);
1467 + fileinfo = UNIONFS_F(file);
1468 + BUG_ON(file->f_path.dentry->d_inode != inode);
1469 + inodeinfo = UNIONFS_I(inode);
1471 + /* fput all the lower files */
1472 + bstart = fbstart(file);
1473 + bend = fbend(file);
1475 + for (bindex = bstart; bindex <= bend; bindex++) {
1476 + lower_file = unionfs_lower_file_idx(file, bindex);
1479 + unionfs_set_lower_file_idx(file, bindex, NULL);
1481 + branchput(sb, bindex);
1484 + /* if there are no more refs to the dentry, dput it */
1485 + if (d_deleted(dentry)) {
1486 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1487 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1491 + kfree(fileinfo->lower_files);
1492 + kfree(fileinfo->saved_branch_ids);
1494 + if (fileinfo->rdstate) {
1495 + fileinfo->rdstate->access = jiffies;
1496 + spin_lock(&inodeinfo->rdlock);
1497 + inodeinfo->rdcount++;
1498 + list_add_tail(&fileinfo->rdstate->cache,
1499 + &inodeinfo->readdircache);
1500 + mark_inode_dirty(inode);
1501 + spin_unlock(&inodeinfo->rdlock);
1502 + fileinfo->rdstate = NULL;
1506 + unionfs_unlock_dentry(dentry);
1507 + unionfs_unlock_parent(dentry, parent);
1508 + unionfs_read_unlock(sb);
1513 +/* pass the ioctl to the lower fs */
1514 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1516 + struct file *lower_file;
1519 + lower_file = unionfs_lower_file(file);
1522 + if (!lower_file || !lower_file->f_op)
1524 + if (lower_file->f_op->unlocked_ioctl) {
1525 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1526 +#ifdef CONFIG_COMPAT
1527 + } else if (lower_file->f_op->compat_ioctl) {
1528 + err = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
1537 + * return to user-space the branch indices containing the file in question
1539 + * We use fd_set and therefore we are limited to the number of the branches
1540 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1542 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1543 + unsigned int cmd, unsigned long arg)
1546 + fd_set branchlist;
1547 + int bstart = 0, bend = 0, bindex = 0;
1548 + int orig_bstart, orig_bend;
1549 + struct dentry *dentry, *lower_dentry;
1550 + struct vfsmount *mnt;
1552 + dentry = file->f_path.dentry;
1553 + orig_bstart = dbstart(dentry);
1554 + orig_bend = dbend(dentry);
1555 + err = unionfs_partial_lookup(dentry, parent);
1558 + bstart = dbstart(dentry);
1559 + bend = dbend(dentry);
1561 + FD_ZERO(&branchlist);
1563 + for (bindex = bstart; bindex <= bend; bindex++) {
1564 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1565 + if (!lower_dentry)
1567 + if (likely(lower_dentry->d_inode))
1568 + FD_SET(bindex, &branchlist);
1569 + /* purge any lower objects after partial_lookup */
1570 + if (bindex < orig_bstart || bindex > orig_bend) {
1571 + dput(lower_dentry);
1572 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1573 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1574 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1576 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1579 + unionfs_mntput(dentry, bindex);
1580 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1583 + /* restore original dentry's offsets */
1584 + dbstart(dentry) = orig_bstart;
1585 + dbend(dentry) = orig_bend;
1586 + ibstart(dentry->d_inode) = orig_bstart;
1587 + ibend(dentry->d_inode) = orig_bend;
1589 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1590 + if (unlikely(err))
1594 + return err < 0 ? err : bend;
1597 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1600 + struct dentry *dentry = file->f_path.dentry;
1601 + struct dentry *parent;
1603 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1604 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1605 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1607 + err = unionfs_file_revalidate(file, parent, true);
1608 + if (unlikely(err))
1611 + /* check if asked for local commands */
1613 + case UNIONFS_IOCTL_INCGEN:
1614 + /* Increment the superblock generation count */
1615 + pr_info("unionfs: incgen ioctl deprecated; "
1616 + "use \"-o remount,incgen\"\n");
1620 + case UNIONFS_IOCTL_QUERYFILE:
1621 + /* Return list of branches containing the given file */
1622 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1626 + /* pass the ioctl down */
1627 + err = do_ioctl(file, cmd, arg);
1632 + unionfs_check_file(file);
1633 + unionfs_unlock_dentry(dentry);
1634 + unionfs_unlock_parent(dentry, parent);
1635 + unionfs_read_unlock(dentry->d_sb);
1639 +int unionfs_flush(struct file *file, fl_owner_t id)
1642 + struct file *lower_file = NULL;
1643 + struct dentry *dentry = file->f_path.dentry;
1644 + struct dentry *parent;
1645 + int bindex, bstart, bend;
1647 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1648 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1649 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1651 + err = unionfs_file_revalidate(file, parent,
1652 + UNIONFS_F(file)->wrote_to_file);
1653 + if (unlikely(err))
1655 + unionfs_check_file(file);
1657 + bstart = fbstart(file);
1658 + bend = fbend(file);
1659 + for (bindex = bstart; bindex <= bend; bindex++) {
1660 + lower_file = unionfs_lower_file_idx(file, bindex);
1662 + if (lower_file && lower_file->f_op &&
1663 + lower_file->f_op->flush) {
1664 + err = lower_file->f_op->flush(lower_file, id);
1673 + unionfs_check_file(file);
1674 + unionfs_unlock_dentry(dentry);
1675 + unionfs_unlock_parent(dentry, parent);
1676 + unionfs_read_unlock(dentry->d_sb);
1679 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1680 new file mode 100644
1681 index 0000000..078ca27
1683 +++ b/fs/unionfs/copyup.c
1686 + * Copyright (c) 2003-2011 Erez Zadok
1687 + * Copyright (c) 2003-2006 Charles P. Wright
1688 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1689 + * Copyright (c) 2005-2006 Junjiro Okajima
1690 + * Copyright (c) 2005 Arun M. Krishnakumar
1691 + * Copyright (c) 2004-2006 David P. Quigley
1692 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1693 + * Copyright (c) 2003 Puja Gupta
1694 + * Copyright (c) 2003 Harikesavan Krishnan
1695 + * Copyright (c) 2003-2011 Stony Brook University
1696 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
1698 + * This program is free software; you can redistribute it and/or modify
1699 + * it under the terms of the GNU General Public License version 2 as
1700 + * published by the Free Software Foundation.
1706 + * For detailed explanation of copyup see:
1707 + * Documentation/filesystems/unionfs/concepts.txt
1710 +#ifdef CONFIG_UNION_FS_XATTR
1711 +/* copyup all extended attrs for a given dentry */
1712 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1713 + struct dentry *new_lower_dentry)
1716 + ssize_t list_size = -1;
1717 + char *name_list = NULL;
1718 + char *attr_value = NULL;
1719 + char *name_list_buf = NULL;
1721 + /* query the actual size of the xattr list */
1722 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1723 + if (list_size <= 0) {
1728 + /* allocate space for the actual list */
1729 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1730 + if (unlikely(!name_list || IS_ERR(name_list))) {
1731 + err = PTR_ERR(name_list);
1735 + name_list_buf = name_list; /* save for kfree at end */
1737 + /* now get the actual xattr list of the source file */
1738 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1739 + if (list_size <= 0) {
1744 + /* allocate space to hold each xattr's value */
1745 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1746 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1747 + err = PTR_ERR(name_list);
1751 + /* in a loop, get and set each xattr from src to dst file */
1752 + while (*name_list) {
1755 + /* Lock here since vfs_getxattr doesn't lock for us */
1756 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1757 + size = vfs_getxattr(old_lower_dentry, name_list,
1758 + attr_value, XATTR_SIZE_MAX);
1759 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1764 + if (size > XATTR_SIZE_MAX) {
1768 + /* Don't lock here since vfs_setxattr does it for us. */
1769 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1772 + * Selinux depends on "security.*" xattrs, so to maintain
1773 + * the security of copied-up files, if Selinux is active,
1774 + * then we must copy these xattrs as well. So we need to
1775 + * temporarily get FOWNER privileges.
1776 + * XXX: move entire copyup code to SIOQ.
1778 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1779 + const struct cred *old_creds;
1780 + struct cred *new_creds;
1782 + new_creds = prepare_creds();
1783 + if (unlikely(!new_creds)) {
1787 + cap_raise(new_creds->cap_effective, CAP_FOWNER);
1788 + old_creds = override_creds(new_creds);
1789 + err = vfs_setxattr(new_lower_dentry, name_list,
1790 + attr_value, size, 0);
1791 + revert_creds(old_creds);
1795 + name_list += strlen(name_list) + 1;
1798 + unionfs_xattr_kfree(name_list_buf);
1799 + unionfs_xattr_kfree(attr_value);
1800 + /* Ignore if xattr isn't supported */
1801 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1805 +#endif /* CONFIG_UNION_FS_XATTR */
1808 + * Determine the mode based on the copyup flags, and the existing dentry.
1810 + * Handle file systems which may not support certain options. For example
1811 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1812 + * errors, rather than propagating them up, which results in copyup errors
1813 + * and errors returned back to users.
1815 +static int copyup_permissions(struct super_block *sb,
1816 + struct dentry *old_lower_dentry,
1817 + struct dentry *new_lower_dentry)
1819 + struct inode *i = old_lower_dentry->d_inode;
1820 + struct iattr newattrs;
1823 + newattrs.ia_atime = i->i_atime;
1824 + newattrs.ia_mtime = i->i_mtime;
1825 + newattrs.ia_ctime = i->i_ctime;
1826 + newattrs.ia_gid = i->i_gid;
1827 + newattrs.ia_uid = i->i_uid;
1828 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1829 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1830 + ATTR_GID | ATTR_UID;
1831 + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1832 + err = notify_change(new_lower_dentry, &newattrs);
1836 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1837 + newattrs.ia_mode = i->i_mode;
1838 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1839 + err = notify_change(new_lower_dentry, &newattrs);
1840 + if (err == -EOPNOTSUPP &&
1841 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1842 + printk(KERN_WARNING
1843 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1844 + new_lower_dentry->d_name.name);
1849 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1854 + * create the new device/file/directory - use copyup_permission to copyup
1857 + * if the object being copied up is a regular file, the file is only created,
1858 + * the contents have to be copied up separately
1860 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1861 + struct dentry *new_lower_dentry,
1862 + struct dentry *new_lower_parent_dentry,
1866 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1867 + struct sioq_args args;
1869 + if (S_ISDIR(old_mode)) {
1870 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1871 + args.mkdir.dentry = new_lower_dentry;
1872 + args.mkdir.mode = old_mode;
1874 + run_sioq(__unionfs_mkdir, &args);
1876 + } else if (S_ISLNK(old_mode)) {
1877 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1878 + args.symlink.dentry = new_lower_dentry;
1879 + args.symlink.symbuf = symbuf;
1881 + run_sioq(__unionfs_symlink, &args);
1883 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1884 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1885 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1886 + args.mknod.dentry = new_lower_dentry;
1887 + args.mknod.mode = old_mode;
1888 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1890 + run_sioq(__unionfs_mknod, &args);
1892 + } else if (S_ISREG(old_mode)) {
1893 + struct nameidata nd;
1894 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1895 + if (unlikely(err < 0))
1897 + args.create.nd = &nd;
1898 + args.create.parent = new_lower_parent_dentry->d_inode;
1899 + args.create.dentry = new_lower_dentry;
1900 + args.create.mode = old_mode;
1902 + run_sioq(__unionfs_create, &args);
1904 + release_lower_nd(&nd, err);
1906 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1915 +static int __copyup_reg_data(struct dentry *dentry,
1916 + struct dentry *new_lower_dentry, int new_bindex,
1917 + struct dentry *old_lower_dentry, int old_bindex,
1918 + struct file **copyup_file, loff_t len)
1920 + struct super_block *sb = dentry->d_sb;
1921 + struct file *input_file;
1922 + struct file *output_file;
1923 + struct vfsmount *output_mnt;
1924 + mm_segment_t old_fs;
1926 + ssize_t read_bytes, write_bytes;
1930 + /* open old file */
1931 + unionfs_mntget(dentry, old_bindex);
1932 + branchget(sb, old_bindex);
1933 + /* dentry_open calls dput and mntput if it returns an error */
1934 + input_file = dentry_open(old_lower_dentry,
1935 + unionfs_lower_mnt_idx(dentry, old_bindex),
1936 + O_RDONLY | O_LARGEFILE, current_cred());
1937 + if (IS_ERR(input_file)) {
1938 + dput(old_lower_dentry);
1939 + err = PTR_ERR(input_file);
1942 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1944 + goto out_close_in;
1947 + /* open new file */
1948 + dget(new_lower_dentry);
1949 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
1950 + branchget(sb, new_bindex);
1951 + output_file = dentry_open(new_lower_dentry, output_mnt,
1952 + O_RDWR | O_LARGEFILE, current_cred());
1953 + if (IS_ERR(output_file)) {
1954 + err = PTR_ERR(output_file);
1955 + goto out_close_in2;
1957 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1959 + goto out_close_out;
1962 + /* allocating a buffer */
1963 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1964 + if (unlikely(!buf)) {
1966 + goto out_close_out;
1969 + input_file->f_pos = 0;
1970 + output_file->f_pos = 0;
1972 + old_fs = get_fs();
1973 + set_fs(KERNEL_DS);
1978 + if (len >= PAGE_SIZE)
1980 + else if ((len < PAGE_SIZE) && (len > 0))
1986 + input_file->f_op->read(input_file,
1987 + (char __user *)buf, size,
1988 + &input_file->f_pos);
1989 + if (read_bytes <= 0) {
1994 + /* see Documentation/filesystems/unionfs/issues.txt */
1997 + output_file->f_op->write(output_file,
1998 + (char __user *)buf,
2000 + &output_file->f_pos);
2002 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2003 + err = write_bytes;
2006 + } while ((read_bytes > 0) && (len > 0));
2013 + /* XXX: code no longer needed? */
2015 + err = output_file->f_op->fsync(output_file, 0);
2019 + goto out_close_out;
2021 + if (copyup_file) {
2022 + *copyup_file = output_file;
2023 + goto out_close_in;
2027 + fput(output_file);
2030 + branchput(sb, new_bindex);
2036 + branchput(sb, old_bindex);
2042 + * dput the lower references for old and new dentry & clear a lower dentry
2045 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2046 + int old_bstart, int old_bend,
2047 + struct dentry *new_lower_dentry, int new_bindex)
2049 + /* get rid of the lower dentry and all its traces */
2050 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2051 + dbstart(dentry) = old_bstart;
2052 + dbend(dentry) = old_bend;
2054 + dput(new_lower_dentry);
2055 + dput(old_lower_dentry);
2059 + * Copy up a dentry to a file of specified name.
2061 + * @dir: used to pull the ->i_sb to access other branches
2062 + * @dentry: the non-negative dentry whose lower_inode we should copy
2063 + * @bstart: the branch of the lower_inode to copy from
2064 + * @new_bindex: the branch to create the new file in
2065 + * @name: the name of the file to create
2066 + * @namelen: length of @name
2067 + * @copyup_file: the "struct file" to return (optional)
2068 + * @len: how many bytes to copy-up?
2070 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2071 + int new_bindex, const char *name, int namelen,
2072 + struct file **copyup_file, loff_t len)
2074 + struct dentry *new_lower_dentry;
2075 + struct dentry *old_lower_dentry = NULL;
2076 + struct super_block *sb;
2081 + struct dentry *new_lower_parent_dentry = NULL;
2082 + mm_segment_t oldfs;
2083 + char *symbuf = NULL;
2085 + verify_locked(dentry);
2087 + old_bindex = bstart;
2088 + old_bstart = dbstart(dentry);
2089 + old_bend = dbend(dentry);
2091 + BUG_ON(new_bindex < 0);
2092 + BUG_ON(new_bindex >= old_bindex);
2096 + err = is_robranch_super(sb, new_bindex);
2100 + /* Create the directory structure above this dentry. */
2101 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2102 + if (IS_ERR(new_lower_dentry)) {
2103 + err = PTR_ERR(new_lower_dentry);
2107 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2108 + /* we conditionally dput this old_lower_dentry at end of function */
2109 + dget(old_lower_dentry);
2111 + /* For symlinks, we must read the link before we lock the directory. */
2112 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2114 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2115 + if (unlikely(!symbuf)) {
2116 + __clear(dentry, old_lower_dentry,
2117 + old_bstart, old_bend,
2118 + new_lower_dentry, new_bindex);
2124 + set_fs(KERNEL_DS);
2125 + err = old_lower_dentry->d_inode->i_op->readlink(
2127 + (char __user *)symbuf,
2131 + __clear(dentry, old_lower_dentry,
2132 + old_bstart, old_bend,
2133 + new_lower_dentry, new_bindex);
2136 + symbuf[err] = '\0';
2139 + /* Now we lock the parent, and create the object in the new branch. */
2140 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2142 + /* create the new inode */
2143 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2144 + new_lower_parent_dentry, symbuf);
2147 + __clear(dentry, old_lower_dentry,
2148 + old_bstart, old_bend,
2149 + new_lower_dentry, new_bindex);
2153 + /* We actually copyup the file here. */
2154 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2155 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2156 + old_lower_dentry, old_bindex,
2157 + copyup_file, len);
2161 + /* Set permissions. */
2162 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2166 +#ifdef CONFIG_UNION_FS_XATTR
2167 + /* Selinux uses extended attributes for permissions. */
2168 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2171 +#endif /* CONFIG_UNION_FS_XATTR */
2173 + /* do not allow files getting deleted to be re-interposed */
2174 + if (!d_deleted(dentry))
2175 + unionfs_reinterpose(dentry);
2181 + * copyup failed, because we possibly ran out of space or
2182 + * quota, or something else happened so let's unlink; we don't
2183 + * really care about the return value of vfs_unlink
2185 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2187 + if (copyup_file) {
2188 + /* need to close the file */
2190 + fput(*copyup_file);
2191 + branchput(sb, new_bindex);
2195 + * TODO: should we reset the error to something like -EIO?
2197 + * If we don't reset, the user may get some nonsensical errors, but
2198 + * on the other hand, if we reset to EIO, we guarantee that the user
2199 + * will get a "confusing" error message.
2203 + unlock_dir(new_lower_parent_dentry);
2207 + * If old_lower_dentry was not a file, then we need to dput it. If
2208 + * it was a file, then it was already dput indirectly by other
2209 + * functions we call above which operate on regular files.
2211 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2212 + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2213 + dput(old_lower_dentry);
2218 + * if directory creation succeeded, but inode copyup failed,
2219 + * then purge new dentries.
2221 + if (dbstart(dentry) < old_bstart &&
2222 + ibstart(dentry->d_inode) > dbstart(dentry))
2223 + __clear(dentry, NULL, old_bstart, old_bend,
2224 + unionfs_lower_dentry(dentry), dbstart(dentry));
2227 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2228 + unionfs_postcopyup_release(dentry);
2229 + if (!unionfs_lower_inode(dentry->d_inode)) {
2231 + * If we got here, then we copied up to an
2232 + * unlinked-open file, whose name is .unionfsXXXXX.
2234 + struct inode *inode = new_lower_dentry->d_inode;
2235 + atomic_inc(&inode->i_count);
2236 + unionfs_set_lower_inode_idx(dentry->d_inode,
2237 + ibstart(dentry->d_inode),
2241 + unionfs_postcopyup_setmnt(dentry);
2242 + /* sync inode times from copied-up inode to our inode */
2243 + unionfs_copy_attr_times(dentry->d_inode);
2244 + unionfs_check_inode(dir);
2245 + unionfs_check_dentry(dentry);
2251 + * This function creates a copy of a file represented by 'file' which
2252 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2253 + * will be named "name".
2255 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2256 + int bstart, int new_bindex, loff_t len)
2259 + struct file *output_file = NULL;
2261 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2262 + name, strlen(name), &output_file, len);
2264 + fbstart(file) = new_bindex;
2265 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2272 + * This function creates a copy of a file represented by 'file' which
2273 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2275 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2276 + int new_bindex, loff_t len)
2279 + struct file *output_file = NULL;
2280 + struct dentry *dentry = file->f_path.dentry;
2282 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2283 + dentry->d_name.name, dentry->d_name.len,
2284 + &output_file, len);
2286 + fbstart(file) = new_bindex;
2287 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2293 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2294 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2295 + int old_bstart, int old_bend)
2299 + int new_bstart = -1;
2300 + int new_bend = -1;
2303 + loop_start = min(old_bstart, bindex);
2304 + loop_end = max(old_bend, bindex);
2307 + * This loop sets the bstart and bend for the new dentry by
2308 + * traversing from left to right. It also dputs all negative
2309 + * dentries except bindex
2311 + for (i = loop_start; i <= loop_end; i++) {
2312 + if (!unionfs_lower_dentry_idx(dentry, i))
2315 + if (i == bindex) {
2317 + if (new_bstart < 0)
2322 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2323 + dput(unionfs_lower_dentry_idx(dentry, i));
2324 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2326 + unionfs_mntput(dentry, i);
2327 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2329 + if (new_bstart < 0)
2335 + if (new_bstart < 0)
2336 + new_bstart = bindex;
2338 + new_bend = bindex;
2339 + dbstart(dentry) = new_bstart;
2340 + dbend(dentry) = new_bend;
2344 +/* set lower inode ptr and update bstart & bend if necessary */
2345 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2348 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2349 + igrab(lower->d_inode));
2350 + if (likely(ibstart(upper->d_inode) > bindex))
2351 + ibstart(upper->d_inode) = bindex;
2352 + if (likely(ibend(upper->d_inode) < bindex))
2353 + ibend(upper->d_inode) = bindex;
2357 +/* set lower dentry ptr and update bstart & bend if necessary */
2358 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2361 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2362 + if (likely(dbstart(upper) > bindex))
2363 + dbstart(upper) = bindex;
2364 + if (likely(dbend(upper) < bindex))
2365 + dbend(upper) = bindex;
2369 + * This function replicates the directory structure up-to given dentry
2370 + * in the bindex branch.
2372 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2373 + const char *name, int bindex)
2376 + struct dentry *child_dentry;
2377 + struct dentry *parent_dentry;
2378 + struct dentry *lower_parent_dentry = NULL;
2379 + struct dentry *lower_dentry = NULL;
2380 + const char *childname;
2381 + unsigned int childnamelen;
2386 + struct dentry **path = NULL;
2387 + struct super_block *sb;
2389 + verify_locked(dentry);
2391 + err = is_robranch_super(dir->i_sb, bindex);
2393 + lower_dentry = ERR_PTR(err);
2397 + old_bstart = dbstart(dentry);
2398 + old_bend = dbend(dentry);
2400 + lower_dentry = ERR_PTR(-ENOMEM);
2402 + /* There is no sense allocating any less than the minimum. */
2404 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2405 + if (unlikely(!path))
2408 + /* assume the negative dentry of unionfs as the parent dentry */
2409 + parent_dentry = dentry;
2412 + * This loop finds the first parent that exists in the given branch.
2413 + * We start building the directory structure from there. At the end
2414 + * of the loop, the following should hold:
2415 + * - child_dentry is the first nonexistent child
2416 + * - parent_dentry is the first existent parent
2417 + * - path[0] is the = deepest child
2418 + * - path[count] is the first child to create
2421 + child_dentry = parent_dentry;
2423 + /* find the parent directory dentry in unionfs */
2424 + parent_dentry = dget_parent(child_dentry);
2426 + /* find out the lower_parent_dentry in the given branch */
2427 + lower_parent_dentry =
2428 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2430 + /* grow path table */
2431 + if (count == nr_dentry) {
2435 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2437 + if (unlikely(!p)) {
2438 + lower_dentry = ERR_PTR(-ENOMEM);
2444 + /* store the child dentry */
2445 + path[count++] = child_dentry;
2446 + } while (!lower_parent_dentry);
2449 + sb = dentry->d_sb;
2452 + * This code goes between the begin/end labels and basically
2453 + * emulates a while(child_dentry != dentry), only cleaner and
2454 + * shorter than what would be a much longer while loop.
2457 + /* get lower parent dir in the current branch */
2458 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2459 + dput(parent_dentry);
2461 + /* init the values to lookup */
2462 + childname = child_dentry->d_name.name;
2463 + childnamelen = child_dentry->d_name.len;
2465 + if (child_dentry != dentry) {
2466 + /* lookup child in the underlying file system */
2467 + lower_dentry = lookup_lck_len(childname, lower_parent_dentry,
2469 + if (IS_ERR(lower_dentry))
2473 + * Is the name a whiteout of the child name ? lookup the
2474 + * whiteout child in the underlying file system
2476 + lower_dentry = lookup_lck_len(name, lower_parent_dentry,
2478 + if (IS_ERR(lower_dentry))
2481 + /* Replace the current dentry (if any) with the new one */
2482 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2483 + unionfs_set_lower_dentry_idx(dentry, bindex,
2486 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2490 + if (lower_dentry->d_inode) {
2492 + * since this already exists we dput to avoid
2493 + * multiple references on the same dentry
2495 + dput(lower_dentry);
2497 + struct sioq_args args;
2499 + /* it's a negative dentry, create a new dir */
2500 + lower_parent_dentry = lock_parent(lower_dentry);
2502 + args.mkdir.parent = lower_parent_dentry->d_inode;
2503 + args.mkdir.dentry = lower_dentry;
2504 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2506 + run_sioq(__unionfs_mkdir, &args);
2510 + err = copyup_permissions(dir->i_sb, child_dentry,
2512 + unlock_dir(lower_parent_dentry);
2514 + dput(lower_dentry);
2515 + lower_dentry = ERR_PTR(err);
2521 + __set_inode(child_dentry, lower_dentry, bindex);
2522 + __set_dentry(child_dentry, lower_dentry, bindex);
2524 + * update times of this dentry, but also the parent, because if
2525 + * we changed, the parent may have changed too.
2527 + fsstack_copy_attr_times(parent_dentry->d_inode,
2528 + lower_parent_dentry->d_inode);
2529 + unionfs_copy_attr_times(child_dentry->d_inode);
2531 + parent_dentry = child_dentry;
2532 + child_dentry = path[--count];
2535 + /* cleanup any leftover locks from the do/while loop above */
2536 + if (IS_ERR(lower_dentry))
2538 + dput(path[count--]);
2540 + return lower_dentry;
2544 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2545 + * dentry+parents to the first parent node that has an mnt.
2547 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2549 + struct dentry *parent, *hasone;
2550 + int bindex = dbstart(dentry);
2552 + if (unionfs_lower_mnt_idx(dentry, bindex))
2554 + hasone = dentry->d_parent;
2555 + /* this loop should stop at root dentry */
2556 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2557 + hasone = hasone->d_parent;
2559 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2560 + unionfs_set_lower_mnt_idx(parent, bindex,
2561 + unionfs_mntget(hasone, bindex));
2562 + parent = parent->d_parent;
2567 + * Post-copyup helper to release all non-directory source objects of a
2568 + * copied-up file. Regular files should have only one lower object.
2570 +void unionfs_postcopyup_release(struct dentry *dentry)
2574 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2575 + bstart = dbstart(dentry);
2576 + bend = dbend(dentry);
2578 + path_put_lowers(dentry, bstart + 1, bend, false);
2579 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2581 + dbend(dentry) = bstart;
2582 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2584 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2585 new file mode 100644
2586 index 0000000..21ce90c
2588 +++ b/fs/unionfs/debug.c
2591 + * Copyright (c) 2003-2011 Erez Zadok
2592 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2593 + * Copyright (c) 2003-2011 Stony Brook University
2594 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
2596 + * This program is free software; you can redistribute it and/or modify
2597 + * it under the terms of the GNU General Public License version 2 as
2598 + * published by the Free Software Foundation.
2602 +#include "../mount.h"
2605 + * Helper debugging functions for maintainers (and for users to report back
2606 + * useful information back to maintainers)
2609 +/* it's always useful to know what part of the code called us */
2610 +#define PRINT_CALLER(fname, fxn, line) \
2612 + if (!printed_caller) { \
2613 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2614 + printed_caller = 1; \
2619 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2620 + * the fan-out of various Unionfs objects. We check that no lower objects
2621 + * exist outside the start/end branch range; that all objects within are
2622 + * non-NULL (with some allowed exceptions); that for every lower file
2623 + * there's a lower dentry+inode; that the start/end ranges match for all
2624 + * corresponding lower objects; that open files/symlinks have only one lower
2625 + * objects, but directories can have several; and more.
2627 +void __unionfs_check_inode(const struct inode *inode,
2628 + const char *fname, const char *fxn, int line)
2632 + struct inode *lower_inode;
2633 + struct super_block *sb;
2634 + int printed_caller = 0;
2637 + /* for inodes now */
2640 + istart = ibstart(inode);
2641 + iend = ibend(inode);
2642 + /* don't check inode if no lower branches */
2643 + if (istart < 0 && iend < 0)
2645 + if (unlikely(istart > iend)) {
2646 + PRINT_CALLER(fname, fxn, line);
2647 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2648 + inode, istart, iend);
2650 + if (unlikely((istart == -1 && iend != -1) ||
2651 + (istart != -1 && iend == -1))) {
2652 + PRINT_CALLER(fname, fxn, line);
2653 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2654 + inode, istart, iend);
2656 + if (!S_ISDIR(inode->i_mode)) {
2657 + if (unlikely(iend != istart)) {
2658 + PRINT_CALLER(fname, fxn, line);
2659 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2660 + inode, istart, iend);
2664 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2665 + if (unlikely(!UNIONFS_I(inode))) {
2666 + PRINT_CALLER(fname, fxn, line);
2667 + pr_debug(" Ci3: no inode_info %p\n", inode);
2670 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2671 + PRINT_CALLER(fname, fxn, line);
2672 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2675 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2676 + if (lower_inode) {
2677 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2678 + if (unlikely(bindex < istart || bindex > iend)) {
2679 + PRINT_CALLER(fname, fxn, line);
2680 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2681 + "istart/end=%d:%d\n", inode,
2682 + lower_inode, bindex, istart, iend);
2683 + } else if (unlikely(lower_inode == poison_ptr)) {
2684 + /* freed inode! */
2685 + PRINT_CALLER(fname, fxn, line);
2686 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2687 + "istart/end=%d:%d\n", inode,
2688 + lower_inode, bindex, istart, iend);
2692 + /* if we get here, then lower_inode == NULL */
2693 + if (bindex < istart || bindex > iend)
2696 + * directories can have NULL lower inodes in b/t start/end,
2697 + * but NOT if at the start/end range.
2699 + if (unlikely(S_ISDIR(inode->i_mode) &&
2700 + bindex > istart && bindex < iend))
2702 + PRINT_CALLER(fname, fxn, line);
2703 + pr_debug(" Ci7: inode/linode=%p:%p "
2704 + "bindex=%d istart/end=%d:%d\n",
2705 + inode, lower_inode, bindex, istart, iend);
2709 +void __unionfs_check_dentry(const struct dentry *dentry,
2710 + const char *fname, const char *fxn, int line)
2713 + int dstart, dend, istart, iend;
2714 + struct dentry *lower_dentry;
2715 + struct inode *inode, *lower_inode;
2716 + struct super_block *sb;
2717 + struct vfsmount *lower_mnt;
2718 + int printed_caller = 0;
2722 + sb = dentry->d_sb;
2723 + inode = dentry->d_inode;
2724 + dstart = dbstart(dentry);
2725 + dend = dbend(dentry);
2726 + /* don't check dentry/mnt if no lower branches */
2727 + if (dstart < 0 && dend < 0)
2729 + BUG_ON(dstart > dend);
2731 + if (unlikely((dstart == -1 && dend != -1) ||
2732 + (dstart != -1 && dend == -1))) {
2733 + PRINT_CALLER(fname, fxn, line);
2734 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2735 + dentry, dstart, dend);
2738 + * check for NULL dentries inside the start/end range, or
2739 + * non-NULL dentries outside the start/end range.
2741 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2742 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2743 + if (lower_dentry) {
2744 + if (unlikely(bindex < dstart || bindex > dend)) {
2745 + PRINT_CALLER(fname, fxn, line);
2746 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2747 + "bindex=%d dstart/end=%d:%d\n",
2748 + dentry, lower_dentry,
2749 + (lower_dentry ? lower_dentry->d_inode :
2751 + bindex, dstart, dend);
2753 + } else { /* lower_dentry == NULL */
2754 + if (bindex < dstart || bindex > dend)
2757 + * Directories can have NULL lower inodes in b/t
2758 + * start/end, but NOT if at the start/end range.
2759 + * Ignore this rule, however, if this is a NULL
2760 + * dentry or a deleted dentry.
2762 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2764 + !(inode && S_ISDIR(inode->i_mode) &&
2765 + bindex > dstart && bindex < dend))) {
2766 + PRINT_CALLER(fname, fxn, line);
2767 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2768 + "bindex=%d dstart/end=%d:%d\n",
2769 + dentry, lower_dentry,
2771 + lower_dentry->d_inode :
2773 + bindex, dstart, dend);
2778 + /* check for vfsmounts same as for dentries */
2779 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2780 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2782 + if (unlikely(bindex < dstart || bindex > dend)) {
2783 + PRINT_CALLER(fname, fxn, line);
2784 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2785 + "dstart/end=%d:%d\n", dentry,
2786 + lower_mnt, bindex, dstart, dend);
2788 + } else { /* lower_mnt == NULL */
2789 + if (bindex < dstart || bindex > dend)
2792 + * Directories can have NULL lower inodes in b/t
2793 + * start/end, but NOT if at the start/end range.
2794 + * Ignore this rule, however, if this is a NULL
2797 + if (unlikely(inode &&
2798 + !(inode && S_ISDIR(inode->i_mode) &&
2799 + bindex > dstart && bindex < dend))) {
2800 + PRINT_CALLER(fname, fxn, line);
2801 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2802 + "bindex=%d dstart/end=%d:%d\n",
2803 + dentry, lower_mnt, bindex,
2810 + /* for inodes now */
2813 + istart = ibstart(inode);
2814 + iend = ibend(inode);
2815 + /* don't check inode if no lower branches */
2816 + if (istart < 0 && iend < 0)
2818 + BUG_ON(istart > iend);
2819 + if (unlikely((istart == -1 && iend != -1) ||
2820 + (istart != -1 && iend == -1))) {
2821 + PRINT_CALLER(fname, fxn, line);
2822 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2823 + dentry, inode, istart, iend);
2825 + if (unlikely(istart != dstart)) {
2826 + PRINT_CALLER(fname, fxn, line);
2827 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2828 + dentry, inode, istart, dstart);
2830 + if (unlikely(iend != dend)) {
2831 + PRINT_CALLER(fname, fxn, line);
2832 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2833 + dentry, inode, iend, dend);
2836 + if (!S_ISDIR(inode->i_mode)) {
2837 + if (unlikely(dend != dstart)) {
2838 + PRINT_CALLER(fname, fxn, line);
2839 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2840 + dentry, inode, dstart, dend);
2842 + if (unlikely(iend != istart)) {
2843 + PRINT_CALLER(fname, fxn, line);
2844 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2845 + dentry, inode, istart, iend);
2849 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2850 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2851 + if (lower_inode) {
2852 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2853 + if (unlikely(bindex < istart || bindex > iend)) {
2854 + PRINT_CALLER(fname, fxn, line);
2855 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2856 + "istart/end=%d:%d\n", dentry,
2857 + lower_inode, bindex, istart, iend);
2858 + } else if (unlikely(lower_inode == poison_ptr)) {
2859 + /* freed inode! */
2860 + PRINT_CALLER(fname, fxn, line);
2861 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2862 + "istart/end=%d:%d\n", dentry,
2863 + lower_inode, bindex, istart, iend);
2867 + /* if we get here, then lower_inode == NULL */
2868 + if (bindex < istart || bindex > iend)
2871 + * directories can have NULL lower inodes in b/t start/end,
2872 + * but NOT if at the start/end range.
2874 + if (unlikely(S_ISDIR(inode->i_mode) &&
2875 + bindex > istart && bindex < iend))
2877 + PRINT_CALLER(fname, fxn, line);
2878 + pr_debug(" CI7: dentry/linode=%p:%p "
2879 + "bindex=%d istart/end=%d:%d\n",
2880 + dentry, lower_inode, bindex, istart, iend);
2884 + * If it's a directory, then intermediate objects b/t start/end can
2885 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2888 + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2889 + for (bindex = dstart+1; bindex < dend; bindex++) {
2890 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2891 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2893 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2894 + if (unlikely(!((lower_inode && lower_dentry &&
2897 + !lower_dentry && !lower_mnt)))) {
2898 + PRINT_CALLER(fname, fxn, line);
2899 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2900 + "bindex=%d dstart/end=%d:%d\n",
2901 + lower_mnt, lower_dentry, lower_inode,
2902 + bindex, dstart, dend);
2905 + /* check if lower inode is newer than upper one (it shouldn't) */
2906 + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
2907 + PRINT_CALLER(fname, fxn, line);
2908 + for (bindex = ibstart(inode); bindex <= ibend(inode);
2910 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2911 + if (unlikely(!lower_inode))
2913 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2914 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
2916 + inode->i_mtime.tv_sec,
2917 + inode->i_mtime.tv_nsec,
2918 + lower_inode->i_mtime.tv_sec,
2919 + lower_inode->i_mtime.tv_nsec,
2920 + inode->i_ctime.tv_sec,
2921 + inode->i_ctime.tv_nsec,
2922 + lower_inode->i_ctime.tv_sec,
2923 + lower_inode->i_ctime.tv_nsec);
2928 +void __unionfs_check_file(const struct file *file,
2929 + const char *fname, const char *fxn, int line)
2932 + int dstart, dend, fstart, fend;
2933 + struct dentry *dentry;
2934 + struct file *lower_file;
2935 + struct inode *inode;
2936 + struct super_block *sb;
2937 + int printed_caller = 0;
2940 + dentry = file->f_path.dentry;
2941 + sb = dentry->d_sb;
2942 + dstart = dbstart(dentry);
2943 + dend = dbend(dentry);
2944 + BUG_ON(dstart > dend);
2945 + fstart = fbstart(file);
2946 + fend = fbend(file);
2947 + BUG_ON(fstart > fend);
2949 + if (unlikely((fstart == -1 && fend != -1) ||
2950 + (fstart != -1 && fend == -1))) {
2951 + PRINT_CALLER(fname, fxn, line);
2952 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
2953 + file, dentry, fstart, fend);
2955 + /* d_deleted dentries can be ignored for this test */
2956 + if (unlikely(fstart != dstart) && !d_deleted(dentry)) {
2957 + PRINT_CALLER(fname, fxn, line);
2958 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
2959 + file, dentry, fstart, dstart);
2961 + if (unlikely(fend != dend) && !d_deleted(dentry)) {
2962 + PRINT_CALLER(fname, fxn, line);
2963 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
2964 + file, dentry, fend, dend);
2966 + inode = dentry->d_inode;
2967 + if (!S_ISDIR(inode->i_mode)) {
2968 + if (unlikely(fend != fstart)) {
2969 + PRINT_CALLER(fname, fxn, line);
2970 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
2971 + file, inode, fstart, fend);
2973 + if (unlikely(dend != dstart)) {
2974 + PRINT_CALLER(fname, fxn, line);
2975 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
2976 + file, dentry, dstart, dend);
2981 + * check for NULL dentries inside the start/end range, or
2982 + * non-NULL dentries outside the start/end range.
2984 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2985 + lower_file = unionfs_lower_file_idx(file, bindex);
2987 + if (unlikely(bindex < fstart || bindex > fend)) {
2988 + PRINT_CALLER(fname, fxn, line);
2989 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
2990 + "fstart/end=%d:%d\n", file,
2991 + lower_file, bindex, fstart, fend);
2993 + } else { /* lower_file == NULL */
2994 + if (bindex >= fstart && bindex <= fend) {
2996 + * directories can have NULL lower inodes in
2997 + * b/t start/end, but NOT if at the
2998 + * start/end range.
3000 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3001 + bindex > fstart &&
3002 + bindex < fend))) {
3003 + PRINT_CALLER(fname, fxn, line);
3004 + pr_debug(" CF6: file/lower=%p:%p "
3005 + "bindex=%d fstart/end=%d:%d\n",
3006 + file, lower_file, bindex,
3013 + __unionfs_check_dentry(dentry, fname, fxn, line);
3016 +void __unionfs_check_nd(const struct nameidata *nd,
3017 + const char *fname, const char *fxn, int line)
3019 + struct file *file;
3020 + int printed_caller = 0;
3022 + if (unlikely(!nd))
3024 + if (nd->flags & LOOKUP_OPEN) {
3025 + file = nd->intent.open.file;
3026 + if (unlikely(file->f_path.dentry &&
3027 + strcmp(file->f_path.dentry->d_sb->s_type->name,
3029 + PRINT_CALLER(fname, fxn, line);
3030 + pr_debug(" CND1: lower_file of type %s\n",
3031 + file->f_path.dentry->d_sb->s_type->name);
3036 +static unsigned int __mnt_get_count(struct vfsmount *mnt)
3038 + struct mount *m = real_mount(mnt);
3040 + unsigned int count = 0;
3043 + for_each_possible_cpu(cpu) {
3044 + count += per_cpu_ptr(m->mnt_pcp, cpu)->mnt_count;
3049 + return m->mnt_count;
3053 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3054 +void __show_branch_counts(const struct super_block *sb,
3055 + const char *file, const char *fxn, int line)
3058 + struct vfsmount *mnt;
3061 + for (i = 0; i < sbmax(sb); i++) {
3062 + if (likely(sb->s_root))
3063 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3066 + printk(KERN_CONT "%d:",
3067 + (mnt ? __mnt_get_count(mnt) : -99));
3069 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3072 +void __show_inode_times(const struct inode *inode,
3073 + const char *file, const char *fxn, int line)
3075 + struct inode *lower_inode;
3078 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3079 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3080 + if (unlikely(!lower_inode))
3082 + pr_debug("IT(%lu:%d): %s:%s:%d "
3083 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3084 + inode->i_ino, bindex,
3086 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3087 + lower_inode->i_mtime.tv_sec,
3088 + lower_inode->i_mtime.tv_nsec,
3089 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3090 + lower_inode->i_ctime.tv_sec,
3091 + lower_inode->i_ctime.tv_nsec);
3095 +void __show_dinode_times(const struct dentry *dentry,
3096 + const char *file, const char *fxn, int line)
3098 + struct inode *inode = dentry->d_inode;
3099 + struct inode *lower_inode;
3102 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3103 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3106 + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3107 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3108 + dentry->d_name.name, inode->i_ino, bindex,
3110 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3111 + lower_inode->i_mtime.tv_sec,
3112 + lower_inode->i_mtime.tv_nsec,
3113 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3114 + lower_inode->i_ctime.tv_sec,
3115 + lower_inode->i_ctime.tv_nsec);
3119 +void __show_inode_counts(const struct inode *inode,
3120 + const char *file, const char *fxn, int line)
3122 + struct inode *lower_inode;
3125 + if (unlikely(!inode)) {
3126 + pr_debug("SiC: Null inode\n");
3129 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3131 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3132 + if (unlikely(!lower_inode))
3134 + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3135 + inode->i_ino, bindex,
3136 + atomic_read(&(inode)->i_count),
3137 + atomic_read(&(lower_inode)->i_count),
3141 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
3142 new file mode 100644
3143 index 0000000..1628dad
3145 +++ b/fs/unionfs/dentry.c
3148 + * Copyright (c) 2003-2011 Erez Zadok
3149 + * Copyright (c) 2003-2006 Charles P. Wright
3150 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3151 + * Copyright (c) 2005-2006 Junjiro Okajima
3152 + * Copyright (c) 2005 Arun M. Krishnakumar
3153 + * Copyright (c) 2004-2006 David P. Quigley
3154 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3155 + * Copyright (c) 2003 Puja Gupta
3156 + * Copyright (c) 2003 Harikesavan Krishnan
3157 + * Copyright (c) 2003-2011 Stony Brook University
3158 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
3160 + * This program is free software; you can redistribute it and/or modify
3161 + * it under the terms of the GNU General Public License version 2 as
3162 + * published by the Free Software Foundation.
3167 +bool is_negative_lower(const struct dentry *dentry)
3170 + struct dentry *lower_dentry;
3173 + /* cache coherency: check if file was deleted on lower branch */
3174 + if (dbstart(dentry) < 0)
3176 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3177 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3178 + /* unhashed (i.e., unlinked) lower dentries don't count */
3179 + if (lower_dentry && lower_dentry->d_inode &&
3180 + !d_deleted(lower_dentry) &&
3181 + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3187 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3189 + struct dentry *lower_dentry;
3194 + for (bindex = start; bindex <= end; bindex++) {
3195 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3196 + if (!lower_dentry)
3198 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3199 + dput(lower_dentry);
3204 + * Purge and invalidate as many data pages of a unionfs inode. This is
3205 + * called when the lower inode has changed, and we want to force processes
3206 + * to re-get the new data.
3208 +static inline void purge_inode_data(struct inode *inode)
3210 + /* remove all non-private mappings */
3211 + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3212 + /* invalidate as many pages as possible */
3213 + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3215 + * Don't try to truncate_inode_pages here, because this could lead
3216 + * to a deadlock between some of address_space ops and dentry
3217 + * revalidation: the address space op is invoked with a lock on our
3218 + * own page, and truncate_inode_pages will block on locked pages.
3223 + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3224 + * of the @dentry and its @parent are locked. Assume parent is valid,
3225 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3226 + * dentry). Returns true if valid, false otherwise.
3228 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3231 + bool valid = true; /* default is valid */
3232 + struct dentry *lower_dentry;
3233 + struct dentry *result;
3234 + int bindex, bstart, bend;
3235 + int sbgen, dgen, pdgen;
3237 + int interpose_flag;
3239 + verify_locked(dentry);
3240 + verify_locked(parent);
3242 + /* if the dentry is unhashed, do NOT revalidate */
3243 + if (d_deleted(dentry))
3246 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3248 + if (is_newer_lower(dentry)) {
3249 + /* root dentry is always valid */
3250 + if (IS_ROOT(dentry)) {
3251 + unionfs_copy_attr_times(dentry->d_inode);
3254 + * reset generation number to zero, guaranteed to be
3258 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3261 + purge_inode_data(dentry->d_inode);
3264 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3266 + BUG_ON(dbstart(dentry) == -1);
3267 + if (dentry->d_inode)
3270 + /* if our dentry is valid, then validate all lower ones */
3271 + if (sbgen == dgen)
3272 + goto validate_lowers;
3274 + /* The root entry should always be valid */
3275 + BUG_ON(IS_ROOT(dentry));
3277 + /* We can't work correctly if our parent isn't valid. */
3278 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3280 + /* Free the pointers for our inodes and this dentry. */
3281 + path_put_lowers_all(dentry, false);
3283 + interpose_flag = INTERPOSE_REVAL_NEG;
3285 + interpose_flag = INTERPOSE_REVAL;
3286 + iput_lowers_all(dentry->d_inode, true);
3289 + if (realloc_dentry_private_data(dentry) != 0) {
3294 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3296 + if (IS_ERR(result)) {
3301 + * current unionfs_lookup_backend() doesn't return
3308 + if (unlikely(positive && is_negative_lower(dentry))) {
3309 + /* call make_bad_inode here ? */
3316 + * if we got here then we have revalidated our dentry and all lower
3317 + * ones, so we can return safely.
3319 + if (!valid) /* lower dentry revalidation failed */
3323 + * If the parent's gen no. matches the superblock's gen no., then
3324 + * we can update our denty's gen no. If they didn't match, then it
3325 + * was OK to revalidate this dentry with a stale parent, but we'll
3326 + * purposely not update our dentry's gen no. (so it can be redone);
3327 + * and, we'll mark our parent dentry as invalid so it'll force it
3328 + * (and our dentry) to be revalidated.
3330 + if (pdgen == sbgen)
3331 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3336 + /* The revalidation must occur across all branches */
3337 + bstart = dbstart(dentry);
3338 + bend = dbend(dentry);
3339 + BUG_ON(bstart == -1);
3340 + for (bindex = bstart; bindex <= bend; bindex++) {
3342 + struct nameidata lower_nd;
3344 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3345 + if (!lower_dentry || !lower_dentry->d_op
3346 + || !lower_dentry->d_op->d_revalidate)
3349 + * Don't pass nameidata to lower file system, because we
3350 + * don't want an arbitrary lower file being opened or
3351 + * returned to us: it may be useless to us because of the
3352 + * fanout nature of unionfs (cf. file/directory open-file
3353 + * invariants). We will open lower files as and when needed
3356 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
3357 + if (unlikely(err < 0)) {
3361 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, &lower_nd))
3363 + release_lower_nd(&lower_nd, err);
3366 + if (!dentry->d_inode ||
3367 + ibstart(dentry->d_inode) < 0 ||
3368 + ibend(dentry->d_inode) < 0) {
3375 + * If we get here, and we copy the meta-data from the lower
3376 + * inode to our inode, then it is vital that we have already
3377 + * purged all unionfs-level file data. We do that in the
3378 + * caller (__unionfs_d_revalidate) by calling
3379 + * purge_inode_data.
3381 + unionfs_copy_attr_all(dentry->d_inode,
3382 + unionfs_lower_inode(dentry->d_inode));
3383 + fsstack_copy_inode_size(dentry->d_inode,
3384 + unionfs_lower_inode(dentry->d_inode));
3392 + * Determine if the lower inode objects have changed from below the unionfs
3393 + * inode. Return true if changed, false otherwise.
3395 + * We check if the mtime or ctime have changed. However, the inode times
3396 + * can be changed by anyone without much protection, including
3397 + * asynchronously. This can sometimes cause unionfs to find that the lower
3398 + * file system doesn't change its inode times quick enough, resulting in a
3399 + * false positive indication (which is harmless, it just makes unionfs do
3400 + * extra work in re-validating the objects). To minimize the chances of
3401 + * these situations, we still consider such small time changes valid, but we
3402 + * don't print debugging messages unless the time changes are greater than
3403 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3404 + * because significant changes are more likely due to users manually
3405 + * touching lower files.
3407 +bool is_newer_lower(const struct dentry *dentry)
3410 + struct inode *inode;
3411 + struct inode *lower_inode;
3413 + /* ignore if we're called on semi-initialized dentries/inodes */
3414 + if (!dentry || !UNIONFS_D(dentry))
3416 + inode = dentry->d_inode;
3417 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3418 + ibstart(inode) < 0 || ibend(inode) < 0)
3421 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3422 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3426 + /* check if mtime/ctime have changed */
3427 + if (unlikely(timespec_compare(&inode->i_mtime,
3428 + &lower_inode->i_mtime) < 0)) {
3429 + if ((lower_inode->i_mtime.tv_sec -
3430 + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3431 + pr_info("unionfs: new lower inode mtime "
3432 + "(bindex=%d, name=%s)\n", bindex,
3433 + dentry->d_name.name);
3434 + show_dinode_times(dentry);
3438 + if (unlikely(timespec_compare(&inode->i_ctime,
3439 + &lower_inode->i_ctime) < 0)) {
3440 + if ((lower_inode->i_ctime.tv_sec -
3441 + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3442 + pr_info("unionfs: new lower inode ctime "
3443 + "(bindex=%d, name=%s)\n", bindex,
3444 + dentry->d_name.name);
3445 + show_dinode_times(dentry);
3452 + * Last check: if this is a positive dentry, but somehow all lower
3453 + * dentries are negative or unhashed, then this dentry needs to be
3454 + * revalidated, because someone probably deleted the objects from
3455 + * the lower branches directly.
3457 + if (is_negative_lower(dentry))
3460 + return false; /* default: lower is not newer */
3463 +static int unionfs_d_revalidate(struct dentry *dentry,
3464 + struct nameidata *nd)
3466 + bool valid = true;
3467 + int err = 1; /* 1 means valid for the VFS */
3468 + struct dentry *parent;
3470 + if (nd && nd->flags & LOOKUP_RCU)
3473 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3474 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3475 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3477 + valid = __unionfs_d_revalidate(dentry, parent, false);
3479 + unionfs_postcopyup_setmnt(dentry);
3480 + unionfs_check_dentry(dentry);
3485 + unionfs_unlock_dentry(dentry);
3486 + unionfs_unlock_parent(dentry, parent);
3487 + unionfs_read_unlock(dentry->d_sb);
3492 +static void unionfs_d_release(struct dentry *dentry)
3494 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3495 + if (unlikely(!UNIONFS_D(dentry)))
3496 + goto out; /* skip if no lower branches */
3497 + /* must lock our branch configuration here */
3498 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3500 + unionfs_check_dentry(dentry);
3501 + /* this could be a negative dentry, so check first */
3502 + if (dbstart(dentry) < 0) {
3503 + unionfs_unlock_dentry(dentry);
3504 + goto out; /* due to a (normal) failed lookup */
3507 + /* Release all the lower dentries */
3508 + path_put_lowers_all(dentry, true);
3510 + unionfs_unlock_dentry(dentry);
3513 + free_dentry_private_data(dentry);
3514 + unionfs_read_unlock(dentry->d_sb);
3519 + * Called when we're removing the last reference to our dentry. So we
3520 + * should drop all lower references too.
3522 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3527 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3528 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3530 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3531 + goto drop_lower_inodes;
3532 + path_put_lowers_all(dentry, false);
3535 + rc = atomic_read(&inode->i_count);
3536 + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3537 + /* see Documentation/filesystems/unionfs/issues.txt */
3539 + iput(unionfs_lower_inode(inode));
3541 + unionfs_set_lower_inode(inode, NULL);
3542 + /* XXX: may need to set start/end to -1? */
3547 + unionfs_unlock_dentry(dentry);
3548 + unionfs_read_unlock(dentry->d_sb);
3551 +struct dentry_operations unionfs_dops = {
3552 + .d_revalidate = unionfs_d_revalidate,
3553 + .d_release = unionfs_d_release,
3554 + .d_iput = unionfs_d_iput,
3556 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3557 new file mode 100644
3558 index 0000000..72a9c1a
3560 +++ b/fs/unionfs/dirfops.c
3563 + * Copyright (c) 2003-2011 Erez Zadok
3564 + * Copyright (c) 2003-2006 Charles P. Wright
3565 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3566 + * Copyright (c) 2005-2006 Junjiro Okajima
3567 + * Copyright (c) 2005 Arun M. Krishnakumar
3568 + * Copyright (c) 2004-2006 David P. Quigley
3569 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3570 + * Copyright (c) 2003 Puja Gupta
3571 + * Copyright (c) 2003 Harikesavan Krishnan
3572 + * Copyright (c) 2003-2011 Stony Brook University
3573 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
3575 + * This program is free software; you can redistribute it and/or modify
3576 + * it under the terms of the GNU General Public License version 2 as
3577 + * published by the Free Software Foundation.
3582 +/* Make sure our rdstate is playing by the rules. */
3583 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3585 + BUG_ON(rdstate->offset >= DIREOF);
3586 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3589 +struct unionfs_getdents_callback {
3590 + struct unionfs_dir_state *rdstate;
3592 + int entries_written;
3593 + int filldir_called;
3594 + int filldir_error;
3595 + filldir_t filldir;
3596 + struct super_block *sb;
3599 +/* based on generic filldir in fs/readir.c */
3600 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3601 + loff_t offset, u64 ino, unsigned int d_type)
3603 + struct unionfs_getdents_callback *buf = dirent;
3604 + struct filldir_node *found = NULL;
3607 + char *name = (char *) oname;
3609 + buf->filldir_called++;
3611 + is_whiteout = is_whiteout_name(&name, &namelen);
3613 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3617 + * If we had non-whiteout entry in dir cache, then mark it
3618 + * as a whiteout and but leave it in the dir cache.
3620 + if (is_whiteout && !found->whiteout)
3621 + found->whiteout = is_whiteout;
3625 + /* if 'name' isn't a whiteout, filldir it. */
3626 + if (!is_whiteout) {
3627 + off_t pos = rdstate2offset(buf->rdstate);
3628 + u64 unionfs_ino = ino;
3630 + err = buf->filldir(buf->dirent, name, namelen, pos,
3631 + unionfs_ino, d_type);
3632 + buf->rdstate->offset++;
3633 + verify_rdstate_offset(buf->rdstate);
3636 + * If we did fill it, stuff it in our hash, otherwise return an
3640 + buf->filldir_error = err;
3643 + buf->entries_written++;
3644 + err = add_filldir_node(buf->rdstate, name, namelen,
3645 + buf->rdstate->bindex, is_whiteout);
3647 + buf->filldir_error = err;
3653 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3656 + struct file *lower_file = NULL;
3657 + struct dentry *dentry = file->f_path.dentry;
3658 + struct dentry *parent;
3659 + struct inode *inode = NULL;
3660 + struct unionfs_getdents_callback buf;
3661 + struct unionfs_dir_state *uds;
3665 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3666 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3667 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3669 + err = unionfs_file_revalidate(file, parent, false);
3670 + if (unlikely(err))
3673 + inode = dentry->d_inode;
3675 + uds = UNIONFS_F(file)->rdstate;
3677 + if (file->f_pos == DIREOF) {
3679 + } else if (file->f_pos > 0) {
3680 + uds = find_rdstate(inode, file->f_pos);
3681 + if (unlikely(!uds)) {
3685 + UNIONFS_F(file)->rdstate = uds;
3687 + init_rdstate(file);
3688 + uds = UNIONFS_F(file)->rdstate;
3691 + bend = fbend(file);
3693 + while (uds->bindex <= bend) {
3694 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3695 + if (!lower_file) {
3701 + /* prepare callback buffer */
3702 + buf.filldir_called = 0;
3703 + buf.filldir_error = 0;
3704 + buf.entries_written = 0;
3705 + buf.dirent = dirent;
3706 + buf.filldir = filldir;
3707 + buf.rdstate = uds;
3708 + buf.sb = inode->i_sb;
3710 + /* Read starting from where we last left off. */
3711 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3716 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3718 + /* Save the position for when we continue. */
3719 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3724 + uds->dirpos = offset;
3726 + /* Copy the atime. */
3727 + fsstack_copy_attr_atime(inode,
3728 + lower_file->f_path.dentry->d_inode);
3733 + if (buf.filldir_error)
3736 + if (!buf.entries_written) {
3742 + if (!buf.filldir_error && uds->bindex >= bend) {
3743 + /* Save the number of hash entries for next time. */
3744 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3745 + free_rdstate(uds);
3746 + UNIONFS_F(file)->rdstate = NULL;
3747 + file->f_pos = DIREOF;
3749 + file->f_pos = rdstate2offset(uds);
3754 + unionfs_check_file(file);
3755 + unionfs_unlock_dentry(dentry);
3756 + unionfs_unlock_parent(dentry, parent);
3757 + unionfs_read_unlock(dentry->d_sb);
3762 + * This is not meant to be a generic repositioning function. If you do
3763 + * things that aren't supported, then we return EINVAL.
3765 + * What is allowed:
3766 + * (1) seeking to the same position that you are currently at
3767 + * This really has no effect, but returns where you are.
3768 + * (2) seeking to the beginning of the file
3769 + * This throws out all state, and lets you begin again.
3771 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3773 + struct unionfs_dir_state *rdstate;
3774 + struct dentry *dentry = file->f_path.dentry;
3775 + struct dentry *parent;
3778 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3779 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3780 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3782 + err = unionfs_file_revalidate(file, parent, false);
3783 + if (unlikely(err))
3786 + rdstate = UNIONFS_F(file)->rdstate;
3789 + * we let users seek to their current position, but not anywhere
3796 + free_rdstate(rdstate);
3797 + UNIONFS_F(file)->rdstate = NULL;
3799 + init_rdstate(file);
3803 + err = file->f_pos;
3806 + /* Unsupported, because we would break everything. */
3814 + if (offset == rdstate2offset(rdstate))
3816 + else if (file->f_pos == DIREOF)
3821 + struct inode *inode;
3822 + inode = dentry->d_inode;
3823 + rdstate = find_rdstate(inode, offset);
3825 + UNIONFS_F(file)->rdstate = rdstate;
3826 + err = rdstate->offset;
3834 + /* Unsupported, because we would break everything. */
3842 + unionfs_check_file(file);
3843 + unionfs_unlock_dentry(dentry);
3844 + unionfs_unlock_parent(dentry, parent);
3845 + unionfs_read_unlock(dentry->d_sb);
3850 + * Trimmed directory options, we shouldn't pass everything down since
3851 + * we don't want to operate on partial directories.
3853 +struct file_operations unionfs_dir_fops = {
3854 + .llseek = unionfs_dir_llseek,
3855 + .read = generic_read_dir,
3856 + .readdir = unionfs_readdir,
3857 + .unlocked_ioctl = unionfs_ioctl,
3858 + .open = unionfs_open,
3859 + .release = unionfs_file_release,
3860 + .flush = unionfs_flush,
3861 + .fsync = unionfs_fsync,
3862 + .fasync = unionfs_fasync,
3864 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3865 new file mode 100644
3866 index 0000000..62ec9af
3868 +++ b/fs/unionfs/dirhelper.c
3871 + * Copyright (c) 2003-2011 Erez Zadok
3872 + * Copyright (c) 2003-2006 Charles P. Wright
3873 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3874 + * Copyright (c) 2005-2006 Junjiro Okajima
3875 + * Copyright (c) 2005 Arun M. Krishnakumar
3876 + * Copyright (c) 2004-2006 David P. Quigley
3877 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3878 + * Copyright (c) 2003 Puja Gupta
3879 + * Copyright (c) 2003 Harikesavan Krishnan
3880 + * Copyright (c) 2003-2011 Stony Brook University
3881 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
3883 + * This program is free software; you can redistribute it and/or modify
3884 + * it under the terms of the GNU General Public License version 2 as
3885 + * published by the Free Software Foundation.
3891 +#define RD_CHECK_EMPTY 1
3892 +/* The callback structure for check_empty. */
3893 +struct unionfs_rdutil_callback {
3895 + int filldir_called;
3896 + struct unionfs_dir_state *rdstate;
3900 +/* This filldir function makes sure only whiteouts exist within a directory. */
3901 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3902 + loff_t offset, u64 ino, unsigned int d_type)
3905 + struct unionfs_rdutil_callback *buf = dirent;
3907 + struct filldir_node *found;
3908 + char *name = (char *) oname;
3910 + buf->filldir_called = 1;
3912 + if (name[0] == '.' && (namelen == 1 ||
3913 + (name[1] == '.' && namelen == 2)))
3916 + is_whiteout = is_whiteout_name(&name, &namelen);
3918 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3919 + /* If it was found in the table there was a previous whiteout. */
3924 + * if it wasn't found and isn't a whiteout, the directory isn't
3928 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3931 + err = add_filldir_node(buf->rdstate, name, namelen,
3932 + buf->rdstate->bindex, is_whiteout);
3939 +/* Is a directory logically empty? */
3940 +int check_empty(struct dentry *dentry, struct dentry *parent,
3941 + struct unionfs_dir_state **namelist)
3944 + struct dentry *lower_dentry = NULL;
3945 + struct vfsmount *mnt;
3946 + struct super_block *sb;
3947 + struct file *lower_file;
3948 + struct unionfs_rdutil_callback *buf = NULL;
3949 + int bindex, bstart, bend, bopaque;
3951 + sb = dentry->d_sb;
3954 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3956 + err = unionfs_partial_lookup(dentry, parent);
3960 + bstart = dbstart(dentry);
3961 + bend = dbend(dentry);
3962 + bopaque = dbopaque(dentry);
3963 + if (0 <= bopaque && bopaque < bend)
3966 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3967 + if (unlikely(!buf)) {
3972 + buf->mode = RD_CHECK_EMPTY;
3973 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3974 + if (unlikely(!buf->rdstate)) {
3979 + /* Process the lower directories with rdutil_callback as a filldir. */
3980 + for (bindex = bstart; bindex <= bend; bindex++) {
3981 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3982 + if (!lower_dentry)
3984 + if (!lower_dentry->d_inode)
3986 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
3989 + dget(lower_dentry);
3990 + mnt = unionfs_mntget(dentry, bindex);
3991 + branchget(sb, bindex);
3992 + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
3993 + if (IS_ERR(lower_file)) {
3994 + err = PTR_ERR(lower_file);
3995 + branchput(sb, bindex);
4000 + buf->filldir_called = 0;
4001 + buf->rdstate->bindex = bindex;
4002 + err = vfs_readdir(lower_file,
4003 + readdir_util_callback, buf);
4006 + } while ((err >= 0) && buf->filldir_called);
4008 + /* fput calls dput for lower_dentry */
4010 + branchput(sb, bindex);
4018 + if (namelist && !err)
4019 + *namelist = buf->rdstate;
4020 + else if (buf->rdstate)
4021 + free_rdstate(buf->rdstate);
4028 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4029 new file mode 100644
4030 index 0000000..ae1b86a
4032 +++ b/fs/unionfs/fanout.h
4035 + * Copyright (c) 2003-2011 Erez Zadok
4036 + * Copyright (c) 2003-2006 Charles P. Wright
4037 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4038 + * Copyright (c) 2005 Arun M. Krishnakumar
4039 + * Copyright (c) 2004-2006 David P. Quigley
4040 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4041 + * Copyright (c) 2003 Puja Gupta
4042 + * Copyright (c) 2003 Harikesavan Krishnan
4043 + * Copyright (c) 2003-2011 Stony Brook University
4044 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
4046 + * This program is free software; you can redistribute it and/or modify
4047 + * it under the terms of the GNU General Public License version 2 as
4048 + * published by the Free Software Foundation.
4055 + * Inode to private data
4057 + * Since we use containers and the struct inode is _inside_ the
4058 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4059 + * inode pointer), return a valid non-NULL pointer.
4061 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4063 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4066 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4067 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4069 +/* Dentry to private data */
4070 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4071 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4072 +#define dbend(dent) (UNIONFS_D(dent)->bend)
4073 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4075 +/* Superblock to private data */
4076 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4077 +#define sbstart(sb) 0
4078 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4079 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4080 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4082 +/* File to private Data */
4083 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4084 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4085 +#define fbend(file) (UNIONFS_F(file)->bend)
4087 +/* macros to manipulate branch IDs in stored in our superblock */
4088 +static inline int branch_id(struct super_block *sb, int index)
4090 + BUG_ON(!sb || index < 0);
4091 + return UNIONFS_SB(sb)->data[index].branch_id;
4094 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4096 + BUG_ON(!sb || index < 0);
4097 + UNIONFS_SB(sb)->data[index].branch_id = val;
4100 +static inline void new_branch_id(struct super_block *sb, int index)
4102 + BUG_ON(!sb || index < 0);
4103 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4107 + * Find new index of matching branch with an existing superblock of a known
4108 + * (possibly old) id. This is needed because branches could have been
4109 + * added/deleted causing the branches of any open files to shift.
4111 + * @sb: the new superblock which may have new/different branch IDs
4112 + * @id: the old/existing id we're looking for
4113 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4115 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4118 + for (i = 0; i < sbmax(sb); i++) {
4119 + if (branch_id(sb, i) == id)
4122 + /* in the non-ODF code, this should really never happen */
4123 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4127 +/* File to lower file. */
4128 +static inline struct file *unionfs_lower_file(const struct file *f)
4131 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4134 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4137 + BUG_ON(!f || index < 0);
4138 + return UNIONFS_F(f)->lower_files[index];
4141 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4144 + BUG_ON(!f || index < 0);
4145 + UNIONFS_F(f)->lower_files[index] = val;
4146 + /* save branch ID (may be redundant?) */
4147 + UNIONFS_F(f)->saved_branch_ids[index] =
4148 + branch_id((f)->f_path.dentry->d_sb, index);
4151 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4154 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4157 +/* Inode to lower inode. */
4158 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4161 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4164 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4167 + BUG_ON(!i || index < 0);
4168 + return UNIONFS_I(i)->lower_inodes[index];
4171 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4172 + struct inode *val)
4174 + BUG_ON(!i || index < 0);
4175 + UNIONFS_I(i)->lower_inodes[index] = val;
4178 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4181 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4184 +/* Superblock to lower superblock. */
4185 +static inline struct super_block *unionfs_lower_super(
4186 + const struct super_block *sb)
4189 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4192 +static inline struct super_block *unionfs_lower_super_idx(
4193 + const struct super_block *sb,
4196 + BUG_ON(!sb || index < 0);
4197 + return UNIONFS_SB(sb)->data[index].sb;
4200 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4202 + struct super_block *val)
4204 + BUG_ON(!sb || index < 0);
4205 + UNIONFS_SB(sb)->data[index].sb = val;
4208 +static inline void unionfs_set_lower_super(struct super_block *sb,
4209 + struct super_block *val)
4212 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4215 +/* Branch count macros. */
4216 +static inline int branch_count(const struct super_block *sb, int index)
4218 + BUG_ON(!sb || index < 0);
4219 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4222 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4224 + BUG_ON(!sb || index < 0);
4225 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4228 +static inline void branchget(struct super_block *sb, int index)
4230 + BUG_ON(!sb || index < 0);
4231 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4234 +static inline void branchput(struct super_block *sb, int index)
4236 + BUG_ON(!sb || index < 0);
4237 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4240 +/* Dentry macros */
4241 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4242 + struct dentry *val)
4244 + BUG_ON(!dent || index < 0);
4245 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4248 +static inline struct dentry *unionfs_lower_dentry_idx(
4249 + const struct dentry *dent,
4252 + BUG_ON(!dent || index < 0);
4253 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4256 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4259 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4262 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4263 + struct vfsmount *mnt)
4265 + BUG_ON(!dent || index < 0);
4266 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4269 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4270 + const struct dentry *dent,
4273 + BUG_ON(!dent || index < 0);
4274 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4277 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4280 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4283 +/* Macros for locking a dentry. */
4284 +enum unionfs_dentry_lock_class {
4285 + UNIONFS_DMUTEX_NORMAL,
4286 + UNIONFS_DMUTEX_ROOT,
4287 + UNIONFS_DMUTEX_PARENT,
4288 + UNIONFS_DMUTEX_CHILD,
4289 + UNIONFS_DMUTEX_WHITEOUT,
4290 + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4291 + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4294 +static inline void unionfs_lock_dentry(struct dentry *d,
4295 + unsigned int subclass)
4298 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4301 +static inline void unionfs_unlock_dentry(struct dentry *d)
4304 + mutex_unlock(&UNIONFS_D(d)->lock);
4307 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4308 + unsigned int subclass)
4313 + p = dget_parent(d);
4315 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4319 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4324 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4325 + mutex_unlock(&UNIONFS_D(p)->lock);
4330 +static inline void verify_locked(struct dentry *d)
4333 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4336 +/* macros to put lower objects */
4339 + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4340 + * @free_lower is true, then also kfree the memory used to hold the lower
4341 + * object pointers.
4343 +static inline void iput_lowers(struct inode *inode,
4344 + int bstart, int bend, bool free_lower)
4346 + struct inode *lower_inode;
4350 + BUG_ON(!UNIONFS_I(inode));
4351 + BUG_ON(bstart < 0);
4353 + for (bindex = bstart; bindex <= bend; bindex++) {
4354 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4355 + if (lower_inode) {
4356 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4357 + /* see Documentation/filesystems/unionfs/issues.txt */
4359 + iput(lower_inode);
4365 + kfree(UNIONFS_I(inode)->lower_inodes);
4366 + UNIONFS_I(inode)->lower_inodes = NULL;
4370 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4371 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4376 + BUG_ON(!UNIONFS_I(inode));
4377 + bstart = ibstart(inode);
4378 + bend = ibend(inode);
4379 + BUG_ON(bstart < 0);
4381 + iput_lowers(inode, bstart, bend, free_lower);
4382 + ibstart(inode) = ibend(inode) = -1;
4386 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4387 + * bstart to bend. If @free_lower is true, then also kfree the memory used
4388 + * to hold the lower object pointers.
4390 + * XXX: implement using path_put VFS macros
4392 +static inline void path_put_lowers(struct dentry *dentry,
4393 + int bstart, int bend, bool free_lower)
4395 + struct dentry *lower_dentry;
4396 + struct vfsmount *lower_mnt;
4400 + BUG_ON(!UNIONFS_D(dentry));
4401 + BUG_ON(bstart < 0);
4403 + for (bindex = bstart; bindex <= bend; bindex++) {
4404 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4405 + if (lower_dentry) {
4406 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4407 + dput(lower_dentry);
4409 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4411 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4412 + mntput(lower_mnt);
4417 + kfree(UNIONFS_D(dentry)->lower_paths);
4418 + UNIONFS_D(dentry)->lower_paths = NULL;
4423 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4426 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4431 + BUG_ON(!UNIONFS_D(dentry));
4432 + bstart = dbstart(dentry);
4433 + bend = dbend(dentry);
4434 + BUG_ON(bstart < 0);
4436 + path_put_lowers(dentry, bstart, bend, free_lower);
4437 + dbstart(dentry) = dbend(dentry) = -1;
4440 +#endif /* not _FANOUT_H */
4441 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4442 new file mode 100644
4443 index 0000000..f583c8f
4445 +++ b/fs/unionfs/file.c
4448 + * Copyright (c) 2003-2011 Erez Zadok
4449 + * Copyright (c) 2003-2006 Charles P. Wright
4450 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4451 + * Copyright (c) 2005-2006 Junjiro Okajima
4452 + * Copyright (c) 2005 Arun M. Krishnakumar
4453 + * Copyright (c) 2004-2006 David P. Quigley
4454 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4455 + * Copyright (c) 2003 Puja Gupta
4456 + * Copyright (c) 2003 Harikesavan Krishnan
4457 + * Copyright (c) 2003-2011 Stony Brook University
4458 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
4460 + * This program is free software; you can redistribute it and/or modify
4461 + * it under the terms of the GNU General Public License version 2 as
4462 + * published by the Free Software Foundation.
4467 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4468 + size_t count, loff_t *ppos)
4471 + struct file *lower_file;
4472 + struct dentry *dentry = file->f_path.dentry;
4473 + struct dentry *parent;
4475 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4476 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4477 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4479 + err = unionfs_file_revalidate(file, parent, false);
4480 + if (unlikely(err))
4483 + lower_file = unionfs_lower_file(file);
4484 + err = vfs_read(lower_file, buf, count, ppos);
4485 + /* update our inode atime upon a successful lower read */
4487 + fsstack_copy_attr_atime(dentry->d_inode,
4488 + lower_file->f_path.dentry->d_inode);
4489 + unionfs_check_file(file);
4493 + unionfs_unlock_dentry(dentry);
4494 + unionfs_unlock_parent(dentry, parent);
4495 + unionfs_read_unlock(dentry->d_sb);
4499 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4500 + size_t count, loff_t *ppos)
4503 + struct file *lower_file;
4504 + struct dentry *dentry = file->f_path.dentry;
4505 + struct dentry *parent;
4507 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4508 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4509 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4511 + err = unionfs_file_revalidate(file, parent, true);
4512 + if (unlikely(err))
4515 + lower_file = unionfs_lower_file(file);
4516 + err = vfs_write(lower_file, buf, count, ppos);
4517 + /* update our inode times+sizes upon a successful lower write */
4519 + fsstack_copy_inode_size(dentry->d_inode,
4520 + lower_file->f_path.dentry->d_inode);
4521 + fsstack_copy_attr_times(dentry->d_inode,
4522 + lower_file->f_path.dentry->d_inode);
4523 + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4524 + unionfs_check_file(file);
4528 + unionfs_unlock_dentry(dentry);
4529 + unionfs_unlock_parent(dentry, parent);
4530 + unionfs_read_unlock(dentry->d_sb);
4534 +static int unionfs_file_readdir(struct file *file, void *dirent,
4535 + filldir_t filldir)
4540 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4544 + struct file *lower_file;
4545 + struct dentry *dentry = file->f_path.dentry;
4546 + struct dentry *parent;
4547 + const struct vm_operations_struct *saved_vm_ops = NULL;
4550 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4551 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4552 + * has been causing false positives in file system stacking layers.
4553 + * In particular, our ->mmap is called after sys_mmap2 already holds
4554 + * mmap_sem, then we lock our own mutexes; but earlier, it's
4555 + * possible for lockdep to have locked our mutexes first, and then
4556 + * we call a lower ->readdir which could call might_fault. The
4557 + * different ordering of the locks is what lockdep complains about
4558 + * -- unnecessarily. Therefore, we have no choice but to tell
4559 + * lockdep to temporarily turn off lockdep here. Note: the comments
4560 + * inside might_sleep also suggest that it would have been
4561 + * nicer to only annotate paths that needs that might_lock_read.
4564 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4565 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4566 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4568 + /* This might be deferred to mmap's writepage */
4569 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4570 + err = unionfs_file_revalidate(file, parent, willwrite);
4571 + if (unlikely(err))
4573 + unionfs_check_file(file);
4576 + * File systems which do not implement ->writepage may use
4577 + * generic_file_readonly_mmap as their ->mmap op. If you call
4578 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4579 + * But we cannot call the lower ->mmap op, so we can't tell that
4580 + * writeable mappings won't work. Therefore, our only choice is to
4581 + * check if the lower file system supports the ->writepage, and if
4582 + * not, return EINVAL (the same error that
4583 + * generic_file_readonly_mmap returns in that case).
4585 + lower_file = unionfs_lower_file(file);
4586 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4588 + printk(KERN_ERR "unionfs: branch %d file system does not "
4589 + "support writeable mmap\n", fbstart(file));
4594 + * find and save lower vm_ops.
4596 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4598 + if (!UNIONFS_F(file)->lower_vm_ops) {
4599 + err = lower_file->f_op->mmap(lower_file, vma);
4601 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4604 + saved_vm_ops = vma->vm_ops;
4605 + err = vm_munmap(vma->vm_start,
4606 + vma->vm_end - vma->vm_start);
4608 + printk(KERN_ERR "unionfs: vm_munmap failed %d\n", err);
4613 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4614 + err = generic_file_mmap(file, vma);
4615 + file->f_mapping->a_ops = &unionfs_aops;
4617 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4620 + vma->vm_ops = &unionfs_vm_ops;
4621 + if (!UNIONFS_F(file)->lower_vm_ops)
4622 + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4626 + /* copyup could cause parent dir times to change */
4627 + unionfs_copy_attr_times(parent->d_inode);
4628 + unionfs_check_file(file);
4630 + unionfs_unlock_dentry(dentry);
4631 + unionfs_unlock_parent(dentry, parent);
4632 + unionfs_read_unlock(dentry->d_sb);
4637 +int unionfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4639 + int bindex, bstart, bend;
4640 + struct file *lower_file;
4641 + struct dentry *dentry = file->f_path.dentry;
4642 + struct dentry *lower_dentry;
4643 + struct dentry *parent;
4644 + struct inode *lower_inode, *inode;
4645 + int err = -EINVAL;
4648 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4649 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4650 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4652 + err = unionfs_file_revalidate(file, parent, true);
4653 + if (unlikely(err))
4655 + unionfs_check_file(file);
4657 + err = generic_file_fsync(file, start, end, datasync);
4661 + bstart = fbstart(file);
4662 + bend = fbend(file);
4663 + if (bstart < 0 || bend < 0)
4666 + inode = dentry->d_inode;
4667 + if (unlikely(!inode)) {
4669 + "unionfs: null lower inode in unionfs_fsync\n");
4672 + for (bindex = bstart; bindex <= bend; bindex++) {
4673 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4674 + if (!lower_inode || !lower_inode->i_fop->fsync)
4676 + lower_file = unionfs_lower_file_idx(file, bindex);
4677 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4678 + err = vfs_fsync_range(lower_file, start, end, datasync);
4679 + if (!err && bindex == bstart)
4680 + fsstack_copy_attr_times(inode, lower_inode);
4687 + unionfs_check_file(file);
4688 + unionfs_unlock_dentry(dentry);
4689 + unionfs_unlock_parent(dentry, parent);
4690 + unionfs_read_unlock(dentry->d_sb);
4695 +int unionfs_fasync(int fd, struct file *file, int flag)
4697 + int bindex, bstart, bend;
4698 + struct file *lower_file;
4699 + struct dentry *dentry = file->f_path.dentry;
4700 + struct dentry *parent;
4701 + struct inode *lower_inode, *inode;
4704 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4705 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4706 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4708 + err = unionfs_file_revalidate(file, parent, true);
4709 + if (unlikely(err))
4711 + unionfs_check_file(file);
4713 + bstart = fbstart(file);
4714 + bend = fbend(file);
4715 + if (bstart < 0 || bend < 0)
4718 + inode = dentry->d_inode;
4719 + if (unlikely(!inode)) {
4721 + "unionfs: null lower inode in unionfs_fasync\n");
4724 + for (bindex = bstart; bindex <= bend; bindex++) {
4725 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4726 + if (!lower_inode || !lower_inode->i_fop->fasync)
4728 + lower_file = unionfs_lower_file_idx(file, bindex);
4729 + mutex_lock(&lower_inode->i_mutex);
4730 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4731 + if (!err && bindex == bstart)
4732 + fsstack_copy_attr_times(inode, lower_inode);
4733 + mutex_unlock(&lower_inode->i_mutex);
4740 + unionfs_check_file(file);
4741 + unionfs_unlock_dentry(dentry);
4742 + unionfs_unlock_parent(dentry, parent);
4743 + unionfs_read_unlock(dentry->d_sb);
4747 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4748 + struct pipe_inode_info *pipe, size_t len,
4749 + unsigned int flags)
4752 + struct file *lower_file;
4753 + struct dentry *dentry = file->f_path.dentry;
4754 + struct dentry *parent;
4756 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4757 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4758 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4760 + err = unionfs_file_revalidate(file, parent, false);
4761 + if (unlikely(err))
4764 + lower_file = unionfs_lower_file(file);
4765 + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4766 + /* update our inode atime upon a successful lower splice-read */
4768 + fsstack_copy_attr_atime(dentry->d_inode,
4769 + lower_file->f_path.dentry->d_inode);
4770 + unionfs_check_file(file);
4774 + unionfs_unlock_dentry(dentry);
4775 + unionfs_unlock_parent(dentry, parent);
4776 + unionfs_read_unlock(dentry->d_sb);
4780 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4781 + struct file *file, loff_t *ppos,
4782 + size_t len, unsigned int flags)
4785 + struct file *lower_file;
4786 + struct dentry *dentry = file->f_path.dentry;
4787 + struct dentry *parent;
4789 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4790 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4791 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4793 + err = unionfs_file_revalidate(file, parent, true);
4794 + if (unlikely(err))
4797 + lower_file = unionfs_lower_file(file);
4798 + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4799 + /* update our inode times+sizes upon a successful lower write */
4801 + fsstack_copy_inode_size(dentry->d_inode,
4802 + lower_file->f_path.dentry->d_inode);
4803 + fsstack_copy_attr_times(dentry->d_inode,
4804 + lower_file->f_path.dentry->d_inode);
4805 + unionfs_check_file(file);
4809 + unionfs_unlock_dentry(dentry);
4810 + unionfs_unlock_parent(dentry, parent);
4811 + unionfs_read_unlock(dentry->d_sb);
4815 +struct file_operations unionfs_main_fops = {
4816 + .llseek = generic_file_llseek,
4817 + .read = unionfs_read,
4818 + .write = unionfs_write,
4819 + .readdir = unionfs_file_readdir,
4820 + .unlocked_ioctl = unionfs_ioctl,
4821 +#ifdef CONFIG_COMPAT
4822 + .compat_ioctl = unionfs_ioctl,
4824 + .mmap = unionfs_mmap,
4825 + .open = unionfs_open,
4826 + .flush = unionfs_flush,
4827 + .release = unionfs_file_release,
4828 + .fsync = unionfs_fsync,
4829 + .fasync = unionfs_fasync,
4830 + .splice_read = unionfs_splice_read,
4831 + .splice_write = unionfs_splice_write,
4833 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4834 new file mode 100644
4835 index 0000000..dd522c2
4837 +++ b/fs/unionfs/inode.c
4840 + * Copyright (c) 2003-2011 Erez Zadok
4841 + * Copyright (c) 2003-2006 Charles P. Wright
4842 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4843 + * Copyright (c) 2005-2006 Junjiro Okajima
4844 + * Copyright (c) 2005 Arun M. Krishnakumar
4845 + * Copyright (c) 2004-2006 David P. Quigley
4846 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4847 + * Copyright (c) 2003 Puja Gupta
4848 + * Copyright (c) 2003 Harikesavan Krishnan
4849 + * Copyright (c) 2003-2011 Stony Brook University
4850 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
4852 + * This program is free software; you can redistribute it and/or modify
4853 + * it under the terms of the GNU General Public License version 2 as
4854 + * published by the Free Software Foundation.
4860 + * Find a writeable branch to create new object in. Checks all writeble
4861 + * branches of the parent inode, from istart to iend order; if none are
4862 + * suitable, also tries branch 0 (which may require a copyup).
4864 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4866 +static struct dentry *find_writeable_branch(struct inode *parent,
4867 + struct dentry *dentry)
4869 + int err = -EINVAL;
4870 + int bindex, istart, iend;
4871 + struct dentry *lower_dentry = NULL;
4873 + istart = ibstart(parent);
4874 + iend = ibend(parent);
4879 + for (bindex = istart; bindex <= iend; bindex++) {
4880 + /* skip non-writeable branches */
4881 + err = is_robranch_super(dentry->d_sb, bindex);
4886 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4887 + if (!lower_dentry)
4890 + * check for whiteouts in writeable branch, and remove them
4893 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4894 + if (err > 0) /* ignore if whiteout found and removed */
4898 + /* if get here, we can write to the branch */
4902 + * If istart wasn't already branch 0, and we got any error, then try
4903 + * branch 0 (which may require copyup)
4905 + if (err && istart > 0) {
4906 + istart = iend = 0;
4911 + * If we tried even branch 0, and still got an error, abort. But if
4912 + * the error was an EROFS, then we should try to copyup.
4914 + if (err && err != -EROFS)
4918 + * If we get here, then check if copyup needed. If lower_dentry is
4919 + * NULL, create the entire dentry directory structure in branch 0.
4921 + if (!lower_dentry) {
4923 + lower_dentry = create_parents(parent, dentry,
4924 + dentry->d_name.name, bindex);
4925 + if (IS_ERR(lower_dentry)) {
4926 + err = PTR_ERR(lower_dentry);
4930 + err = 0; /* all's well */
4933 + return ERR_PTR(err);
4934 + return lower_dentry;
4937 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4938 + umode_t mode, struct nameidata *nd_unused)
4941 + struct dentry *lower_dentry = NULL;
4942 + struct dentry *lower_parent_dentry = NULL;
4943 + struct dentry *parent;
4945 + struct nameidata lower_nd;
4947 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4948 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4949 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4951 + valid = __unionfs_d_revalidate(dentry, parent, false);
4952 + if (unlikely(!valid)) {
4953 + err = -ESTALE; /* same as what real_lookup does */
4957 + lower_dentry = find_writeable_branch(dir, dentry);
4958 + if (IS_ERR(lower_dentry)) {
4959 + err = PTR_ERR(lower_dentry);
4963 + lower_parent_dentry = lock_parent(lower_dentry);
4964 + if (IS_ERR(lower_parent_dentry)) {
4965 + err = PTR_ERR(lower_parent_dentry);
4969 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4970 + if (unlikely(err < 0))
4972 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4974 + release_lower_nd(&lower_nd, err);
4977 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
4979 + unionfs_copy_attr_times(dir);
4980 + fsstack_copy_inode_size(dir,
4981 + lower_parent_dentry->d_inode);
4982 + /* update no. of links on parent directory */
4983 + set_nlink(dir, unionfs_get_nlinks(dir));
4988 + unlock_dir(lower_parent_dentry);
4991 + unionfs_postcopyup_setmnt(dentry);
4992 + unionfs_check_inode(dir);
4993 + unionfs_check_dentry(dentry);
4995 + unionfs_unlock_dentry(dentry);
4996 + unionfs_unlock_parent(dentry, parent);
4997 + unionfs_read_unlock(dentry->d_sb);
5002 + * unionfs_lookup is the only special function which takes a dentry, yet we
5003 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
5004 + * we don't have a valid dentry here yet.
5006 +static struct dentry *unionfs_lookup(struct inode *dir,
5007 + struct dentry *dentry,
5008 + struct nameidata *nd_unused)
5010 + struct dentry *ret, *parent;
5013 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5014 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5017 + * As long as we lock/dget the parent, then can skip validating the
5018 + * parent now; we may have to rebuild this dentry on the next
5019 + * ->d_revalidate, however.
5022 + /* allocate dentry private data. We free it in ->d_release */
5023 + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
5024 + if (unlikely(err)) {
5025 + ret = ERR_PTR(err);
5029 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
5031 + if (!IS_ERR(ret)) {
5034 + /* lookup_full can return multiple positive dentries */
5035 + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5036 + BUG_ON(dbstart(dentry) < 0);
5037 + unionfs_postcopyup_release(dentry);
5039 + unionfs_copy_attr_times(dentry->d_inode);
5042 + unionfs_check_inode(dir);
5044 + unionfs_check_dentry(dentry);
5045 + unionfs_check_dentry(parent);
5046 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5049 + unionfs_unlock_parent(dentry, parent);
5050 + unionfs_read_unlock(dentry->d_sb);
5055 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5056 + struct dentry *new_dentry)
5059 + struct dentry *lower_old_dentry = NULL;
5060 + struct dentry *lower_new_dentry = NULL;
5061 + struct dentry *lower_dir_dentry = NULL;
5062 + struct dentry *old_parent, *new_parent;
5063 + char *name = NULL;
5066 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5067 + old_parent = dget_parent(old_dentry);
5068 + new_parent = dget_parent(new_dentry);
5069 + unionfs_double_lock_parents(old_parent, new_parent);
5070 + unionfs_double_lock_dentry(old_dentry, new_dentry);
5072 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5073 + if (unlikely(!valid)) {
5077 + if (new_dentry->d_inode) {
5078 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5079 + if (unlikely(!valid)) {
5085 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5087 + /* check for a whiteout in new dentry branch, and delete it */
5088 + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5089 + dbstart(new_dentry));
5090 + if (err > 0) { /* whiteout found and removed successfully */
5091 + lower_dir_dentry = dget_parent(lower_new_dentry);
5092 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5093 + dput(lower_dir_dentry);
5094 + set_nlink(dir, unionfs_get_nlinks(dir));
5100 + /* check if parent hierachy is needed, then link in same branch */
5101 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5102 + lower_new_dentry = create_parents(dir, new_dentry,
5103 + new_dentry->d_name.name,
5104 + dbstart(old_dentry));
5105 + err = PTR_ERR(lower_new_dentry);
5106 + if (IS_COPYUP_ERR(err))
5108 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5111 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5112 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5114 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5115 + lower_dir_dentry = lock_parent(lower_new_dentry);
5116 + err = is_robranch(old_dentry);
5118 + /* see Documentation/filesystems/unionfs/issues.txt */
5120 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5121 + lower_new_dentry);
5124 + unlock_dir(lower_dir_dentry);
5127 + if (IS_COPYUP_ERR(err)) {
5128 + int old_bstart = dbstart(old_dentry);
5131 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5132 + err = copyup_dentry(old_parent->d_inode,
5133 + old_dentry, old_bstart,
5134 + bindex, old_dentry->d_name.name,
5135 + old_dentry->d_name.len, NULL,
5136 + i_size_read(old_dentry->d_inode));
5139 + lower_new_dentry =
5140 + create_parents(dir, new_dentry,
5141 + new_dentry->d_name.name,
5143 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5144 + lower_dir_dentry = lock_parent(lower_new_dentry);
5145 + /* see Documentation/filesystems/unionfs/issues.txt */
5148 + err = vfs_link(lower_old_dentry,
5149 + lower_dir_dentry->d_inode,
5150 + lower_new_dentry);
5152 + unlock_dir(lower_dir_dentry);
5159 + if (err || !lower_new_dentry->d_inode)
5162 + /* Its a hard link, so use the same inode */
5163 + new_dentry->d_inode = igrab(old_dentry->d_inode);
5164 + d_add(new_dentry, new_dentry->d_inode);
5165 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5166 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5168 + /* propagate number of hard-links */
5169 + set_nlink(old_dentry->d_inode,
5170 + unionfs_get_nlinks(old_dentry->d_inode));
5171 + /* new dentry's ctime may have changed due to hard-link counts */
5172 + unionfs_copy_attr_times(new_dentry->d_inode);
5175 + if (!new_dentry->d_inode)
5176 + d_drop(new_dentry);
5180 + unionfs_postcopyup_setmnt(new_dentry);
5182 + unionfs_check_inode(dir);
5183 + unionfs_check_dentry(new_dentry);
5184 + unionfs_check_dentry(old_dentry);
5186 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5187 + unionfs_double_unlock_parents(old_parent, new_parent);
5190 + unionfs_read_unlock(old_dentry->d_sb);
5195 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5196 + const char *symname)
5199 + struct dentry *lower_dentry = NULL;
5200 + struct dentry *wh_dentry = NULL;
5201 + struct dentry *lower_parent_dentry = NULL;
5202 + struct dentry *parent;
5203 + char *name = NULL;
5207 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5208 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5209 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5211 + valid = __unionfs_d_revalidate(dentry, parent, false);
5212 + if (unlikely(!valid)) {
5218 + * It's only a bug if this dentry was not negative and couldn't be
5219 + * revalidated (shouldn't happen).
5221 + BUG_ON(!valid && dentry->d_inode);
5223 + lower_dentry = find_writeable_branch(dir, dentry);
5224 + if (IS_ERR(lower_dentry)) {
5225 + err = PTR_ERR(lower_dentry);
5229 + lower_parent_dentry = lock_parent(lower_dentry);
5230 + if (IS_ERR(lower_parent_dentry)) {
5231 + err = PTR_ERR(lower_parent_dentry);
5236 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5238 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5240 + unionfs_copy_attr_times(dir);
5241 + fsstack_copy_inode_size(dir,
5242 + lower_parent_dentry->d_inode);
5243 + /* update no. of links on parent directory */
5244 + set_nlink(dir, unionfs_get_nlinks(dir));
5249 + unlock_dir(lower_parent_dentry);
5255 + unionfs_postcopyup_setmnt(dentry);
5256 + unionfs_check_inode(dir);
5257 + unionfs_check_dentry(dentry);
5259 + unionfs_unlock_dentry(dentry);
5260 + unionfs_unlock_parent(dentry, parent);
5261 + unionfs_read_unlock(dentry->d_sb);
5265 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
5268 + struct dentry *lower_dentry = NULL;
5269 + struct dentry *lower_parent_dentry = NULL;
5270 + struct dentry *parent;
5271 + int bindex = 0, bstart;
5272 + char *name = NULL;
5275 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5276 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5277 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5279 + valid = __unionfs_d_revalidate(dentry, parent, false);
5280 + if (unlikely(!valid)) {
5281 + err = -ESTALE; /* same as what real_lookup does */
5285 + bstart = dbstart(dentry);
5287 + lower_dentry = unionfs_lower_dentry(dentry);
5289 + /* check for a whiteout in new dentry branch, and delete it */
5290 + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5291 + if (err > 0) /* whiteout found and removed successfully */
5294 + /* exit if the error returned was NOT -EROFS */
5295 + if (!IS_COPYUP_ERR(err))
5300 + /* check if copyup's needed, and mkdir */
5301 + for (bindex = bstart; bindex >= 0; bindex--) {
5303 + int bend = dbend(dentry);
5305 + if (is_robranch_super(dentry->d_sb, bindex))
5308 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5309 + if (!lower_dentry) {
5310 + lower_dentry = create_parents(dir, dentry,
5311 + dentry->d_name.name,
5313 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5314 + printk(KERN_ERR "unionfs: lower dentry "
5315 + " NULL for bindex = %d\n", bindex);
5320 + lower_parent_dentry = lock_parent(lower_dentry);
5322 + if (IS_ERR(lower_parent_dentry)) {
5323 + err = PTR_ERR(lower_parent_dentry);
5327 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5330 + unlock_dir(lower_parent_dentry);
5332 + /* did the mkdir succeed? */
5336 + for (i = bindex + 1; i <= bend; i++) {
5337 + /* XXX: use path_put_lowers? */
5338 + if (unionfs_lower_dentry_idx(dentry, i)) {
5339 + dput(unionfs_lower_dentry_idx(dentry, i));
5340 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5343 + dbend(dentry) = bindex;
5346 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5349 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5351 + unionfs_copy_attr_times(dir);
5352 + fsstack_copy_inode_size(dir,
5353 + lower_parent_dentry->d_inode);
5355 + /* update number of links on parent directory */
5356 + set_nlink(dir, unionfs_get_nlinks(dir));
5359 + err = make_dir_opaque(dentry, dbstart(dentry));
5361 + printk(KERN_ERR "unionfs: mkdir: error creating "
5362 + ".wh.__dir_opaque: %d\n", err);
5366 + /* we are done! */
5371 + if (!dentry->d_inode)
5377 + unionfs_copy_attr_times(dentry->d_inode);
5378 + unionfs_postcopyup_setmnt(dentry);
5380 + unionfs_check_inode(dir);
5381 + unionfs_check_dentry(dentry);
5382 + unionfs_unlock_dentry(dentry);
5383 + unionfs_unlock_parent(dentry, parent);
5384 + unionfs_read_unlock(dentry->d_sb);
5389 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
5393 + struct dentry *lower_dentry = NULL;
5394 + struct dentry *wh_dentry = NULL;
5395 + struct dentry *lower_parent_dentry = NULL;
5396 + struct dentry *parent;
5397 + char *name = NULL;
5400 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5401 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5402 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5404 + valid = __unionfs_d_revalidate(dentry, parent, false);
5405 + if (unlikely(!valid)) {
5411 + * It's only a bug if this dentry was not negative and couldn't be
5412 + * revalidated (shouldn't happen).
5414 + BUG_ON(!valid && dentry->d_inode);
5416 + lower_dentry = find_writeable_branch(dir, dentry);
5417 + if (IS_ERR(lower_dentry)) {
5418 + err = PTR_ERR(lower_dentry);
5422 + lower_parent_dentry = lock_parent(lower_dentry);
5423 + if (IS_ERR(lower_parent_dentry)) {
5424 + err = PTR_ERR(lower_parent_dentry);
5428 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5430 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5432 + unionfs_copy_attr_times(dir);
5433 + fsstack_copy_inode_size(dir,
5434 + lower_parent_dentry->d_inode);
5435 + /* update no. of links on parent directory */
5436 + set_nlink(dir, unionfs_get_nlinks(dir));
5441 + unlock_dir(lower_parent_dentry);
5447 + unionfs_postcopyup_setmnt(dentry);
5448 + unionfs_check_inode(dir);
5449 + unionfs_check_dentry(dentry);
5451 + unionfs_unlock_dentry(dentry);
5452 + unionfs_unlock_parent(dentry, parent);
5453 + unionfs_read_unlock(dentry->d_sb);
5457 +/* requires sb, dentry, and parent to already be locked */
5458 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5462 + struct dentry *lower_dentry;
5464 + lower_dentry = unionfs_lower_dentry(dentry);
5466 + if (!lower_dentry->d_inode->i_op ||
5467 + !lower_dentry->d_inode->i_op->readlink) {
5472 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5475 + fsstack_copy_attr_atime(dentry->d_inode,
5476 + lower_dentry->d_inode);
5482 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5486 + struct dentry *parent;
5488 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5489 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5490 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5492 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5497 + err = __unionfs_readlink(dentry, buf, bufsiz);
5500 + unionfs_check_dentry(dentry);
5501 + unionfs_unlock_dentry(dentry);
5502 + unionfs_unlock_parent(dentry, parent);
5503 + unionfs_read_unlock(dentry->d_sb);
5508 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5511 + int len = PAGE_SIZE, err;
5512 + mm_segment_t old_fs;
5513 + struct dentry *parent;
5515 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5516 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5517 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5519 + /* This is freed by the put_link method assuming a successful call. */
5520 + buf = kmalloc(len, GFP_KERNEL);
5521 + if (unlikely(!buf)) {
5526 + /* read the symlink, and then we will follow it */
5527 + old_fs = get_fs();
5528 + set_fs(KERNEL_DS);
5529 + err = __unionfs_readlink(dentry, buf, len);
5537 + nd_set_link(nd, buf);
5542 + unionfs_check_nd(nd);
5543 + unionfs_check_dentry(dentry);
5546 + unionfs_unlock_dentry(dentry);
5547 + unionfs_unlock_parent(dentry, parent);
5548 + unionfs_read_unlock(dentry->d_sb);
5550 + return ERR_PTR(err);
5553 +/* this @nd *IS* still used */
5554 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5557 + struct dentry *parent;
5560 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5561 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5562 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5564 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5566 + "unionfs: put_link failed to revalidate dentry\n");
5568 + unionfs_check_dentry(dentry);
5570 + /* XXX: can't run this check b/c this fxn can receive a poisoned 'nd' PTR */
5571 + unionfs_check_nd(nd);
5573 + buf = nd_get_link(nd);
5576 + unionfs_unlock_dentry(dentry);
5577 + unionfs_unlock_parent(dentry, parent);
5578 + unionfs_read_unlock(dentry->d_sb);
5582 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5583 + * skips over EROFS tests (because we perform copyup on EROFS).
5585 +static int __inode_permission(struct inode *inode, int mask)
5589 + /* nobody gets write access to an immutable file */
5590 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5593 + /* Ordinary permission routines do not understand MAY_APPEND. */
5594 + if (inode->i_op && inode->i_op->permission) {
5595 + retval = inode->i_op->permission(inode, mask);
5598 + * Exec permission on a regular file is denied if none
5599 + * of the execute bits are set.
5601 + * This check should be done by the ->permission()
5604 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5605 + !(inode->i_mode & S_IXUGO))
5609 + retval = generic_permission(inode, mask);
5614 + return security_inode_permission(inode,
5615 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5619 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5620 + * a deadlock with the branch-management "add branch" code (which grabbed
5621 + * the write lock). It is safe to not grab the read lock here, because even
5622 + * with branch management taking place, there is no chance that
5623 + * unionfs_permission, or anything it calls, will use stale branch
5626 +static int unionfs_permission(struct inode *inode, int mask)
5628 + struct inode *lower_inode = NULL;
5630 + int bindex, bstart, bend;
5632 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5633 + struct inode *inode_grabbed;
5635 + inode_grabbed = igrab(inode);
5636 + is_file = !S_ISDIR(inode->i_mode);
5638 + if (!UNIONFS_I(inode)->lower_inodes) {
5639 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5640 + err = -ESTALE; /* force revalidate */
5643 + bstart = ibstart(inode);
5644 + bend = ibend(inode);
5645 + if (unlikely(bstart < 0 || bend < 0)) {
5647 + * With branch-management, we can get a stale inode here.
5648 + * If so, we return ESTALE back to link_path_walk, which
5649 + * would discard the dcache entry and re-lookup the
5650 + * dentry+inode. This should be equivalent to issuing
5651 + * __unionfs_d_revalidate_chain on nd.dentry here.
5653 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5654 + err = -ESTALE; /* force revalidate */
5658 + for (bindex = bstart; bindex <= bend; bindex++) {
5659 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5664 + * check the condition for D-F-D underlying files/directories,
5665 + * we don't have to check for files, if we are checking for
5668 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5672 + * We check basic permissions, but we ignore any conditions
5673 + * such as readonly file systems or branches marked as
5674 + * readonly, because those conditions should lead to a
5675 + * copyup taking place later on. However, if user never had
5676 + * access to the file, then no copyup could ever take place.
5678 + err = __inode_permission(lower_inode, mask);
5679 + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5680 + umode_t mode = lower_inode->i_mode;
5681 + if ((is_robranch_super(inode->i_sb, bindex) ||
5682 + __is_rdonly(lower_inode)) &&
5683 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5685 + if (IS_COPYUP_ERR(err))
5690 + * NFS HACK: NFSv2/3 return EACCES on readonly-exported,
5691 + * locally readonly-mounted file systems, instead of EROFS
5692 + * like other file systems do. So we have no choice here
5693 + * but to intercept this and ignore it for NFS branches
5694 + * marked readonly. Specifically, we avoid using NFS's own
5695 + * "broken" ->permission method, and rely on
5696 + * generic_permission() to do basic checking for us.
5698 + if (err && err == -EACCES &&
5699 + is_robranch_super(inode->i_sb, bindex) &&
5700 + lower_inode->i_sb->s_magic == NFS_SUPER_MAGIC)
5701 + err = generic_permission(lower_inode, mask);
5704 + * The permissions are an intersection of the overall directory
5705 + * permissions, so we fail if one fails.
5710 + /* only the leftmost file matters. */
5711 + if (is_file || write_mask) {
5712 + if (is_file && write_mask) {
5713 + err = get_write_access(lower_inode);
5715 + put_write_access(lower_inode);
5720 + /* sync times which may have changed (asynchronously) below */
5721 + unionfs_copy_attr_times(inode);
5724 + unionfs_check_inode(inode);
5725 + iput(inode_grabbed);
5729 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5732 + struct dentry *lower_dentry;
5733 + struct dentry *parent;
5734 + struct inode *inode;
5735 + struct inode *lower_inode;
5736 + int bstart, bend, bindex;
5738 + struct iattr lower_ia;
5740 + /* check if user has permission to change inode */
5741 + err = inode_change_ok(dentry->d_inode, ia);
5745 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5746 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5747 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5749 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5754 + bstart = dbstart(dentry);
5755 + bend = dbend(dentry);
5756 + inode = dentry->d_inode;
5759 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5760 + * to reinterpret it in its own way.
5762 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5763 + ia->ia_valid &= ~ATTR_MODE;
5765 + lower_dentry = unionfs_lower_dentry(dentry);
5766 + if (!lower_dentry) { /* should never happen after above revalidate */
5772 + * Get the lower inode directly from lower dentry, in case ibstart
5773 + * is -1 (which happens when the file is open but unlinked.
5775 + lower_inode = lower_dentry->d_inode;
5777 + /* check if user has permission to change lower inode */
5778 + err = inode_change_ok(lower_inode, ia);
5782 + /* copyup if the file is on a read only branch */
5783 + if (is_robranch_super(dentry->d_sb, bstart)
5784 + || __is_rdonly(lower_inode)) {
5785 + /* check if we have a branch to copy up to */
5786 + if (bstart <= 0) {
5791 + if (ia->ia_valid & ATTR_SIZE)
5792 + size = ia->ia_size;
5794 + size = i_size_read(inode);
5795 + /* copyup to next available branch */
5796 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5797 + err = copyup_dentry(parent->d_inode,
5798 + dentry, bstart, bindex,
5799 + dentry->d_name.name,
5800 + dentry->d_name.len,
5807 + /* get updated lower_dentry/inode after copyup */
5808 + lower_dentry = unionfs_lower_dentry(dentry);
5809 + lower_inode = unionfs_lower_inode(inode);
5811 + * check for whiteouts in writeable branch, and remove them
5814 + if (lower_dentry) {
5815 + err = check_unlink_whiteout(dentry, lower_dentry,
5817 + if (err > 0) /* ignore if whiteout found and removed */
5823 + * If shrinking, first truncate upper level to cancel writing dirty
5824 + * pages beyond the new eof; and also if its' maxbytes is more
5825 + * limiting (fail with -EFBIG before making any change to the lower
5826 + * level). There is no need to vmtruncate the upper level
5827 + * afterwards in the other cases: we fsstack_copy_inode_size from
5828 + * the lower level.
5830 + if (ia->ia_valid & ATTR_SIZE) {
5831 + size = i_size_read(inode);
5832 + if (ia->ia_size < size || (ia->ia_size > size &&
5833 + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5834 + err = vmtruncate(inode, ia->ia_size);
5840 + /* notify the (possibly copied-up) lower inode */
5842 + * Note: we use lower_dentry->d_inode, because lower_inode may be
5843 + * unlinked (no inode->i_sb and i_ino==0. This happens if someone
5844 + * tries to open(), unlink(), then ftruncate() a file.
5846 + /* prepare our own lower struct iattr (with our own lower file) */
5847 + memcpy(&lower_ia, ia, sizeof(lower_ia));
5848 + if (ia->ia_valid & ATTR_FILE) {
5849 + lower_ia.ia_file = unionfs_lower_file(ia->ia_file);
5850 + BUG_ON(!lower_ia.ia_file); // XXX?
5853 + mutex_lock(&lower_dentry->d_inode->i_mutex);
5854 + err = notify_change(lower_dentry, &lower_ia);
5855 + mutex_unlock(&lower_dentry->d_inode->i_mutex);
5859 + /* get attributes from the first lower inode */
5860 + if (ibstart(inode) >= 0)
5861 + unionfs_copy_attr_all(inode, lower_inode);
5863 + * unionfs_copy_attr_all will copy the lower times to our inode if
5864 + * the lower ones are newer (useful for cache coherency). However,
5865 + * ->setattr is the only place in which we may have to copy the
5866 + * lower inode times absolutely, to support utimes(2).
5868 + if (ia->ia_valid & ATTR_MTIME_SET)
5869 + inode->i_mtime = lower_inode->i_mtime;
5870 + if (ia->ia_valid & ATTR_CTIME)
5871 + inode->i_ctime = lower_inode->i_ctime;
5872 + if (ia->ia_valid & ATTR_ATIME_SET)
5873 + inode->i_atime = lower_inode->i_atime;
5874 + fsstack_copy_inode_size(inode, lower_inode);
5878 + unionfs_check_dentry(dentry);
5879 + unionfs_unlock_dentry(dentry);
5880 + unionfs_unlock_parent(dentry, parent);
5881 + unionfs_read_unlock(dentry->d_sb);
5886 +struct inode_operations unionfs_symlink_iops = {
5887 + .readlink = unionfs_readlink,
5888 + .permission = unionfs_permission,
5889 + .follow_link = unionfs_follow_link,
5890 + .setattr = unionfs_setattr,
5891 + .put_link = unionfs_put_link,
5894 +struct inode_operations unionfs_dir_iops = {
5895 + .create = unionfs_create,
5896 + .lookup = unionfs_lookup,
5897 + .link = unionfs_link,
5898 + .unlink = unionfs_unlink,
5899 + .symlink = unionfs_symlink,
5900 + .mkdir = unionfs_mkdir,
5901 + .rmdir = unionfs_rmdir,
5902 + .mknod = unionfs_mknod,
5903 + .rename = unionfs_rename,
5904 + .permission = unionfs_permission,
5905 + .setattr = unionfs_setattr,
5906 +#ifdef CONFIG_UNION_FS_XATTR
5907 + .setxattr = unionfs_setxattr,
5908 + .getxattr = unionfs_getxattr,
5909 + .removexattr = unionfs_removexattr,
5910 + .listxattr = unionfs_listxattr,
5911 +#endif /* CONFIG_UNION_FS_XATTR */
5914 +struct inode_operations unionfs_main_iops = {
5915 + .permission = unionfs_permission,
5916 + .setattr = unionfs_setattr,
5917 +#ifdef CONFIG_UNION_FS_XATTR
5918 + .setxattr = unionfs_setxattr,
5919 + .getxattr = unionfs_getxattr,
5920 + .removexattr = unionfs_removexattr,
5921 + .listxattr = unionfs_listxattr,
5922 +#endif /* CONFIG_UNION_FS_XATTR */
5924 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5925 new file mode 100644
5926 index 0000000..041d674
5928 +++ b/fs/unionfs/lookup.c
5931 + * Copyright (c) 2003-2011 Erez Zadok
5932 + * Copyright (c) 2003-2006 Charles P. Wright
5933 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5934 + * Copyright (c) 2005-2006 Junjiro Okajima
5935 + * Copyright (c) 2005 Arun M. Krishnakumar
5936 + * Copyright (c) 2004-2006 David P. Quigley
5937 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5938 + * Copyright (c) 2003 Puja Gupta
5939 + * Copyright (c) 2003 Harikesavan Krishnan
5940 + * Copyright (c) 2003-2011 Stony Brook University
5941 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
5943 + * This program is free software; you can redistribute it and/or modify
5944 + * it under the terms of the GNU General Public License version 2 as
5945 + * published by the Free Software Foundation.
5951 + * Lookup one path component @name relative to a <base,mnt> path pair.
5952 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5953 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5954 + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5955 + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5956 + * @dentry and @new_mnt.
5958 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5959 + const char *name, struct vfsmount **new_mnt)
5961 + struct dentry *dentry = NULL;
5962 + struct path lower_path = {NULL, NULL};
5965 + /* we use flags=0 to get basic lookup */
5966 + err = vfs_path_lookup(base, mnt, name, 0, &lower_path);
5969 + case 0: /* no error */
5970 + dentry = lower_path.dentry;
5972 + *new_mnt = lower_path.mnt; /* rc already inc'ed */
5976 + * We don't consider ENOENT an error, and we want to return
5977 + * a negative dentry (ala lookup_one_len). As we know
5978 + * there was no inode for this name before (-ENOENT), then
5979 + * it's safe to call lookup_one_len (which doesn't take a
5982 + dentry = lookup_lck_len(name, base, strlen(name));
5984 + *new_mnt = mntget(lower_path.mnt);
5986 + default: /* all other real errors */
5987 + dentry = ERR_PTR(err);
5995 + * This is a utility function that fills in a unionfs dentry.
5996 + * Caller must lock this dentry with unionfs_lock_dentry.
5998 + * Returns: 0 (ok), or -ERRNO if an error occurred.
5999 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
6001 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
6003 + struct dentry *tmp;
6004 + int err = -ENOSYS;
6006 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
6012 + if (IS_ERR(tmp)) {
6013 + err = PTR_ERR(tmp);
6016 + /* XXX: need to change the interface */
6017 + BUG_ON(tmp != dentry);
6022 +/* The dentry cache is just so we have properly sized dentries. */
6023 +static struct kmem_cache *unionfs_dentry_cachep;
6024 +int unionfs_init_dentry_cache(void)
6026 + unionfs_dentry_cachep =
6027 + kmem_cache_create("unionfs_dentry",
6028 + sizeof(struct unionfs_dentry_info),
6029 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6031 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6034 +void unionfs_destroy_dentry_cache(void)
6036 + if (unionfs_dentry_cachep)
6037 + kmem_cache_destroy(unionfs_dentry_cachep);
6040 +void free_dentry_private_data(struct dentry *dentry)
6042 + if (!dentry || !dentry->d_fsdata)
6044 + kfree(UNIONFS_D(dentry)->lower_paths);
6045 + UNIONFS_D(dentry)->lower_paths = NULL;
6046 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6047 + dentry->d_fsdata = NULL;
6050 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6052 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6058 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6059 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6063 + info->lower_paths = p;
6065 + info->bstart = -1;
6067 + info->bopaque = -1;
6068 + info->bcount = sbmax(dentry->d_sb);
6069 + atomic_set(&info->generation,
6070 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6072 + memset(info->lower_paths, 0, size);
6077 +/* UNIONFS_D(dentry)->lock must be locked */
6078 +int realloc_dentry_private_data(struct dentry *dentry)
6080 + if (!__realloc_dentry_private_data(dentry))
6083 + kfree(UNIONFS_D(dentry)->lower_paths);
6084 + free_dentry_private_data(dentry);
6088 +/* allocate new dentry private data */
6089 +int new_dentry_private_data(struct dentry *dentry, int subclass)
6091 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6095 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6096 + if (unlikely(!info))
6099 + mutex_init(&info->lock);
6100 + mutex_lock_nested(&info->lock, subclass);
6102 + info->lower_paths = NULL;
6104 + dentry->d_fsdata = info;
6106 + if (!__realloc_dentry_private_data(dentry))
6109 + mutex_unlock(&info->lock);
6110 + free_dentry_private_data(dentry);
6115 + * scan through the lower dentry objects, and set bstart to reflect the
6118 +void update_bstart(struct dentry *dentry)
6121 + int bstart = dbstart(dentry);
6122 + int bend = dbend(dentry);
6123 + struct dentry *lower_dentry;
6125 + for (bindex = bstart; bindex <= bend; bindex++) {
6126 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6127 + if (!lower_dentry)
6129 + if (lower_dentry->d_inode) {
6130 + dbstart(dentry) = bindex;
6133 + dput(lower_dentry);
6134 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6140 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6141 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6142 + * Inside that nd structure, this function may also return an allocated
6143 + * struct file (for open intents). The caller, when done with this nd, must
6144 + * kfree the intent file (using release_lower_nd).
6146 + * XXX: this code, and the callers of this code, should be redone using
6147 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6148 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6149 + * function and a method that other file systems can call.
6151 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6154 +#ifdef ALLOC_LOWER_ND_FILE
6156 + * XXX: one day we may need to have the lower return an open file
6157 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6158 + * very well be needed for nfs4.
6160 + struct file *file;
6161 +#endif /* ALLOC_LOWER_ND_FILE */
6163 + memset(nd, 0, sizeof(struct nameidata));
6168 + case LOOKUP_CREATE:
6169 + nd->intent.open.flags |= O_CREAT;
6170 + /* fall through: shared code for create/open cases */
6172 + nd->flags = flags;
6173 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6174 +#ifdef ALLOC_LOWER_ND_FILE
6175 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6176 + if (unlikely(!file)) {
6178 + break; /* exit switch statement and thus return */
6180 + nd->intent.open.file = file;
6181 +#endif /* ALLOC_LOWER_ND_FILE */
6185 + * We should never get here, for now.
6186 + * We can add new cases here later on.
6188 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6196 +void release_lower_nd(struct nameidata *nd, int err)
6198 + if (!nd->intent.open.file)
6201 + release_open_intent(nd);
6202 +#ifdef ALLOC_LOWER_ND_FILE
6203 + kfree(nd->intent.open.file);
6204 +#endif /* ALLOC_LOWER_ND_FILE */
6208 + * Main (and complex) driver function for Unionfs's lookup
6210 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6211 + * PTR if d_splice returned a different dentry.
6213 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6214 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6215 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6216 + * dentry's info, which the caller must unlock.
6218 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6219 + struct dentry *parent, int lookupmode)
6222 + struct dentry *lower_dentry = NULL;
6223 + struct vfsmount *lower_mnt;
6224 + struct vfsmount *lower_dir_mnt;
6225 + struct dentry *wh_lower_dentry = NULL;
6226 + struct dentry *lower_dir_dentry = NULL;
6227 + struct dentry *d_interposed = NULL;
6228 + int bindex, bstart, bend, bopaque;
6229 + int opaque, num_positive = 0;
6232 + int pos_start, pos_end;
6235 + * We should already have a lock on this dentry in the case of a
6236 + * partial lookup, or a revalidation. Otherwise it is returned from
6237 + * new_dentry_private_data already locked.
6239 + verify_locked(dentry);
6240 + verify_locked(parent);
6242 + /* must initialize dentry operations */
6243 + if (lookupmode == INTERPOSE_LOOKUP)
6244 + d_set_d_op(dentry, &unionfs_dops);
6246 + /* We never partial lookup the root directory. */
6247 + if (IS_ROOT(dentry))
6250 + name = dentry->d_name.name;
6251 + namelen = dentry->d_name.len;
6253 + /* No dentries should get created for possible whiteout names. */
6254 + if (!is_validname(name)) {
6259 + /* Now start the actual lookup procedure. */
6260 + bstart = dbstart(parent);
6261 + bend = dbend(parent);
6262 + bopaque = dbopaque(parent);
6263 + BUG_ON(bstart < 0);
6265 + /* adjust bend to bopaque if needed */
6266 + if ((bopaque >= 0) && (bopaque < bend))
6269 + /* lookup all possible dentries */
6270 + for (bindex = bstart; bindex <= bend; bindex++) {
6272 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6273 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6275 + /* skip if we already have a positive lower dentry */
6276 + if (lower_dentry) {
6277 + if (dbstart(dentry) < 0)
6278 + dbstart(dentry) = bindex;
6279 + if (bindex > dbend(dentry))
6280 + dbend(dentry) = bindex;
6281 + if (lower_dentry->d_inode)
6286 + lower_dir_dentry =
6287 + unionfs_lower_dentry_idx(parent, bindex);
6288 + /* if the lower dentry's parent does not exist, skip this */
6289 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6292 + /* also skip it if the parent isn't a directory. */
6293 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6294 + continue; /* XXX: should be BUG_ON */
6296 + /* check for whiteouts: stop lookup if found */
6297 + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6298 + if (IS_ERR(wh_lower_dentry)) {
6299 + err = PTR_ERR(wh_lower_dentry);
6302 + if (wh_lower_dentry->d_inode) {
6303 + dbend(dentry) = dbopaque(dentry) = bindex;
6304 + if (dbstart(dentry) < 0)
6305 + dbstart(dentry) = bindex;
6306 + dput(wh_lower_dentry);
6309 + dput(wh_lower_dentry);
6311 + /* Now do regular lookup; lookup @name */
6312 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6313 + lower_mnt = NULL; /* XXX: needed? */
6315 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6316 + name, &lower_mnt);
6318 + if (IS_ERR(lower_dentry)) {
6319 + err = PTR_ERR(lower_dentry);
6322 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6324 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6326 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6328 + /* adjust dbstart/end */
6329 + if (dbstart(dentry) < 0)
6330 + dbstart(dentry) = bindex;
6331 + if (bindex > dbend(dentry))
6332 + dbend(dentry) = bindex;
6334 + * We always store the lower dentries above, and update
6335 + * dbstart/dbend, even if the whole unionfs dentry is
6336 + * negative (i.e., no lower inodes).
6338 + if (!lower_dentry->d_inode)
6343 + * check if we just found an opaque directory, if so, stop
6346 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6348 + opaque = is_opaque_dir(dentry, bindex);
6352 + } else if (opaque) {
6353 + dbend(dentry) = dbopaque(dentry) = bindex;
6356 + dbend(dentry) = bindex;
6358 + /* update parent directory's atime with the bindex */
6359 + fsstack_copy_attr_atime(parent->d_inode,
6360 + lower_dir_dentry->d_inode);
6363 + /* sanity checks, then decide if to process a negative dentry */
6364 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6365 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6367 + if (num_positive > 0)
6368 + goto out_positive;
6370 + /*** handle NEGATIVE dentries ***/
6373 + * If negative, keep only first lower negative dentry, to save on
6376 + if (dbstart(dentry) < dbend(dentry)) {
6377 + path_put_lowers(dentry, dbstart(dentry) + 1,
6378 + dbend(dentry), false);
6379 + dbend(dentry) = dbstart(dentry);
6381 + if (lookupmode == INTERPOSE_PARTIAL)
6383 + if (lookupmode == INTERPOSE_LOOKUP) {
6385 + * If all we found was a whiteout in the first available
6386 + * branch, then create a negative dentry for a possibly new
6387 + * file to be created.
6389 + if (dbopaque(dentry) < 0)
6391 + /* XXX: need to get mnt here */
6392 + bindex = dbstart(dentry);
6393 + if (unionfs_lower_dentry_idx(dentry, bindex))
6395 + lower_dir_dentry =
6396 + unionfs_lower_dentry_idx(parent, bindex);
6397 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6399 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6400 + goto out; /* XXX: should be BUG_ON */
6401 + /* XXX: do we need to cross bind mounts here? */
6402 + lower_dentry = lookup_lck_len(name, lower_dir_dentry, namelen);
6403 + if (IS_ERR(lower_dentry)) {
6404 + err = PTR_ERR(lower_dentry);
6407 + /* XXX: need to mntget/mntput as needed too! */
6408 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6409 + /* XXX: wrong mnt for crossing bind mounts! */
6410 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6411 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6416 + /* if we're revalidating a positive dentry, don't make it negative */
6417 + if (lookupmode != INTERPOSE_REVAL)
6418 + d_add(dentry, NULL);
6423 + /*** handle POSITIVE dentries ***/
6426 + * This unionfs dentry is positive (at least one lower inode
6427 + * exists), so scan entire dentry from beginning to end, and remove
6428 + * any negative lower dentries, if any. Then, update dbstart/dbend
6429 + * to reflect the start/end of positive dentries.
6431 + pos_start = pos_end = -1;
6432 + for (bindex = bstart; bindex <= bend; bindex++) {
6433 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6435 + if (lower_dentry && lower_dentry->d_inode) {
6436 + if (pos_start < 0)
6437 + pos_start = bindex;
6438 + if (bindex > pos_end)
6442 + path_put_lowers(dentry, bindex, bindex, false);
6444 + if (pos_start >= 0)
6445 + dbstart(dentry) = pos_start;
6447 + dbend(dentry) = pos_end;
6449 + /* Partial lookups need to re-interpose, or throw away older negs. */
6450 + if (lookupmode == INTERPOSE_PARTIAL) {
6451 + if (dentry->d_inode) {
6452 + unionfs_reinterpose(dentry);
6457 + * This dentry was positive, so it is as if we had a
6458 + * negative revalidation.
6460 + lookupmode = INTERPOSE_REVAL_NEG;
6461 + update_bstart(dentry);
6465 + * Interpose can return a dentry if d_splice returned a different
6468 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6469 + if (IS_ERR(d_interposed))
6470 + err = PTR_ERR(d_interposed);
6471 + else if (d_interposed)
6472 + dentry = d_interposed;
6479 + /* should dput/mntput all the underlying dentries on error condition */
6480 + if (dbstart(dentry) >= 0)
6481 + path_put_lowers_all(dentry, false);
6482 + /* free lower_paths unconditionally */
6483 + kfree(UNIONFS_D(dentry)->lower_paths);
6484 + UNIONFS_D(dentry)->lower_paths = NULL;
6487 + if (dentry && UNIONFS_D(dentry)) {
6488 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6489 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6491 + if (d_interposed && UNIONFS_D(d_interposed)) {
6492 + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6493 + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6496 + if (!err && d_interposed)
6497 + return d_interposed;
6498 + return ERR_PTR(err);
6500 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6501 new file mode 100644
6502 index 0000000..ee78f1d
6504 +++ b/fs/unionfs/main.c
6507 + * Copyright (c) 2003-2011 Erez Zadok
6508 + * Copyright (c) 2003-2006 Charles P. Wright
6509 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6510 + * Copyright (c) 2005-2006 Junjiro Okajima
6511 + * Copyright (c) 2005 Arun M. Krishnakumar
6512 + * Copyright (c) 2004-2006 David P. Quigley
6513 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6514 + * Copyright (c) 2003 Puja Gupta
6515 + * Copyright (c) 2003 Harikesavan Krishnan
6516 + * Copyright (c) 2003-2011 Stony Brook University
6517 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
6519 + * This program is free software; you can redistribute it and/or modify
6520 + * it under the terms of the GNU General Public License version 2 as
6521 + * published by the Free Software Foundation.
6525 +#include <linux/module.h>
6526 +#include <linux/moduleparam.h>
6528 +static void unionfs_fill_inode(struct dentry *dentry,
6529 + struct inode *inode)
6531 + struct inode *lower_inode;
6532 + struct dentry *lower_dentry;
6533 + int bindex, bstart, bend;
6535 + bstart = dbstart(dentry);
6536 + bend = dbend(dentry);
6538 + for (bindex = bstart; bindex <= bend; bindex++) {
6539 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6540 + if (!lower_dentry) {
6541 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6545 + /* Initialize the lower inode to the new lower inode. */
6546 + if (!lower_dentry->d_inode)
6549 + unionfs_set_lower_inode_idx(inode, bindex,
6550 + igrab(lower_dentry->d_inode));
6553 + ibstart(inode) = dbstart(dentry);
6554 + ibend(inode) = dbend(dentry);
6556 + /* Use attributes from the first branch. */
6557 + lower_inode = unionfs_lower_inode(inode);
6559 + /* Use different set of inode ops for symlinks & directories */
6560 + if (S_ISLNK(lower_inode->i_mode))
6561 + inode->i_op = &unionfs_symlink_iops;
6562 + else if (S_ISDIR(lower_inode->i_mode))
6563 + inode->i_op = &unionfs_dir_iops;
6565 + /* Use different set of file ops for directories */
6566 + if (S_ISDIR(lower_inode->i_mode))
6567 + inode->i_fop = &unionfs_dir_fops;
6569 + /* properly initialize special inodes */
6570 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6571 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6572 + init_special_inode(inode, lower_inode->i_mode,
6573 + lower_inode->i_rdev);
6575 + /* all well, copy inode attributes */
6576 + unionfs_copy_attr_all(inode, lower_inode);
6577 + fsstack_copy_inode_size(inode, lower_inode);
6581 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6582 + * the classic stackable file system "vnode interposition" action.
6584 + * @sb: unionfs's super_block
6586 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6590 + struct inode *inode;
6591 + int need_fill_inode = 1;
6592 + struct dentry *spliced = NULL;
6594 + verify_locked(dentry);
6597 + * We allocate our new inode below by calling unionfs_iget,
6598 + * which will initialize some of the new inode's fields
6602 + * On revalidate we've already got our own inode and just need
6605 + if (flag == INTERPOSE_REVAL) {
6606 + inode = dentry->d_inode;
6607 + UNIONFS_I(inode)->bstart = -1;
6608 + UNIONFS_I(inode)->bend = -1;
6609 + atomic_set(&UNIONFS_I(inode)->generation,
6610 + atomic_read(&UNIONFS_SB(sb)->generation));
6612 + UNIONFS_I(inode)->lower_inodes =
6613 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6614 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6619 + /* get unique inode number for unionfs */
6620 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6621 + if (IS_ERR(inode)) {
6622 + err = PTR_ERR(inode);
6625 + if (atomic_read(&inode->i_count) > 1)
6629 + need_fill_inode = 0;
6630 + unionfs_fill_inode(dentry, inode);
6633 + /* only (our) lookup wants to do a d_add */
6635 + case INTERPOSE_DEFAULT:
6636 + /* for operations which create new inodes */
6637 + d_add(dentry, inode);
6639 + case INTERPOSE_REVAL_NEG:
6640 + d_instantiate(dentry, inode);
6642 + case INTERPOSE_LOOKUP:
6643 + spliced = d_splice_alias(inode, dentry);
6644 + if (spliced && spliced != dentry) {
6646 + * d_splice can return a dentry if it was
6647 + * disconnected and had to be moved. We must ensure
6648 + * that the private data of the new dentry is
6649 + * correct and that the inode info was filled
6650 + * properly. Finally we must return this new
6653 + d_set_d_op(spliced, &unionfs_dops);
6654 + spliced->d_fsdata = dentry->d_fsdata;
6655 + dentry->d_fsdata = NULL;
6657 + if (need_fill_inode) {
6658 + need_fill_inode = 0;
6659 + unionfs_fill_inode(dentry, inode);
6662 + } else if (!spliced) {
6663 + if (need_fill_inode) {
6664 + need_fill_inode = 0;
6665 + unionfs_fill_inode(dentry, inode);
6670 + case INTERPOSE_REVAL:
6674 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6683 + return ERR_PTR(err);
6686 +/* like interpose above, but for an already existing dentry */
6687 +void unionfs_reinterpose(struct dentry *dentry)
6689 + struct dentry *lower_dentry;
6690 + struct inode *inode;
6691 + int bindex, bstart, bend;
6693 + verify_locked(dentry);
6695 + /* This is pre-allocated inode */
6696 + inode = dentry->d_inode;
6698 + bstart = dbstart(dentry);
6699 + bend = dbend(dentry);
6700 + for (bindex = bstart; bindex <= bend; bindex++) {
6701 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6702 + if (!lower_dentry)
6705 + if (!lower_dentry->d_inode)
6707 + if (unionfs_lower_inode_idx(inode, bindex))
6709 + unionfs_set_lower_inode_idx(inode, bindex,
6710 + igrab(lower_dentry->d_inode));
6712 + ibstart(inode) = dbstart(dentry);
6713 + ibend(inode) = dbend(dentry);
6717 + * make sure the branch we just looked up (nd) makes sense:
6719 + * 1) we're not trying to stack unionfs on top of unionfs
6721 + * 3) is a directory
6723 +int check_branch(const struct path *path)
6725 + /* XXX: remove in ODF code -- stacking unions allowed there */
6726 + if (!strcmp(path->dentry->d_sb->s_type->name, UNIONFS_NAME))
6728 + if (!path->dentry->d_inode)
6730 + if (!S_ISDIR(path->dentry->d_inode->i_mode))
6735 +/* checks if two lower_dentries have overlapping branches */
6736 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6738 + struct dentry *dent = NULL;
6741 + while ((dent != dent2) && (dent->d_parent != dent))
6742 + dent = dent->d_parent;
6744 + if (dent == dent2)
6748 + while ((dent != dent1) && (dent->d_parent != dent))
6749 + dent = dent->d_parent;
6751 + return (dent == dent1);
6755 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6756 + * specified. Fill the mode bits in @perms. If encounter an unknown
6757 + * string, return -EINVAL. Otherwise return 0.
6759 +int parse_branch_mode(const char *name, int *perms)
6761 + if (!name || !strcmp(name, "rw")) {
6762 + *perms = MAY_READ | MAY_WRITE;
6765 + if (!strcmp(name, "ro")) {
6766 + *perms = MAY_READ;
6773 + * parse the dirs= mount argument
6775 + * We don't need to lock the superblock private data's rwsem, as we get
6776 + * called only by unionfs_read_super - it is still a long time before anyone
6777 + * can even get a reference to us.
6779 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6780 + *lower_root_info, char *options)
6789 + struct dentry *dent1;
6790 + struct dentry *dent2;
6792 + if (options[0] == '\0') {
6793 + printk(KERN_ERR "unionfs: no branches specified\n");
6799 + * Each colon means we have a separator, this is really just a rough
6800 + * guess, since strsep will handle empty fields for us.
6802 + for (i = 0; options[i]; i++)
6803 + if (options[i] == ':')
6806 + /* allocate space for underlying pointers to lower dentry */
6807 + UNIONFS_SB(sb)->data =
6808 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6809 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6814 + lower_root_info->lower_paths =
6815 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6816 + if (unlikely(!lower_root_info->lower_paths)) {
6818 + /* free the underlying pointer array */
6819 + kfree(UNIONFS_SB(sb)->data);
6820 + UNIONFS_SB(sb)->data = NULL;
6824 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6826 + while ((name = strsep(&options, ":")) != NULL) {
6828 + char *mode = strchr(name, '=');
6832 + if (!*name) { /* bad use of ':' (extra colons) */
6839 + /* strip off '=' if any */
6843 + err = parse_branch_mode(mode, &perms);
6845 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6846 + "branch %d\n", mode, bindex);
6849 + /* ensure that leftmost branch is writeable */
6850 + if (!bindex && !(perms & MAY_WRITE)) {
6851 + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6852 + "read-only (use \"-o ro\" to create a "
6853 + "read-only union)\n");
6858 + err = kern_path(name, LOOKUP_FOLLOW, &path);
6860 + printk(KERN_ERR "unionfs: error accessing "
6861 + "lower directory '%s' (error %d)\n",
6866 + err = check_branch(&path);
6868 + printk(KERN_ERR "unionfs: lower directory "
6869 + "'%s' is not a valid branch\n", name);
6874 + lower_root_info->lower_paths[bindex].dentry = path.dentry;
6875 + lower_root_info->lower_paths[bindex].mnt = path.mnt;
6877 + set_branchperms(sb, bindex, perms);
6878 + set_branch_count(sb, bindex, 0);
6879 + new_branch_id(sb, bindex);
6881 + if (lower_root_info->bstart < 0)
6882 + lower_root_info->bstart = bindex;
6883 + lower_root_info->bend = bindex;
6887 + if (branches == 0) {
6888 + printk(KERN_ERR "unionfs: no branches specified\n");
6893 + BUG_ON(branches != (lower_root_info->bend + 1));
6896 + * Ensure that no overlaps exist in the branches.
6898 + * This test is required because the Linux kernel has no support
6899 + * currently for ensuring coherency between stackable layers and
6900 + * branches. If we were to allow overlapping branches, it would be
6901 + * possible, for example, to delete a file via one branch, which
6902 + * would not be reflected in another branch. Such incoherency could
6903 + * lead to inconsistencies and even kernel oopses. Rather than
6904 + * implement hacks to work around some of these cache-coherency
6905 + * problems, we prevent branch overlapping, for now. A complete
6906 + * solution will involve proper kernel/VFS support for cache
6907 + * coherency, at which time we could safely remove this
6908 + * branch-overlapping test.
6910 + for (i = 0; i < branches; i++) {
6911 + dent1 = lower_root_info->lower_paths[i].dentry;
6912 + for (j = i + 1; j < branches; j++) {
6913 + dent2 = lower_root_info->lower_paths[j].dentry;
6914 + if (is_branch_overlap(dent1, dent2)) {
6915 + printk(KERN_ERR "unionfs: branches %d and "
6916 + "%d overlap\n", i, j);
6925 + for (i = 0; i < branches; i++)
6926 + path_put(&lower_root_info->lower_paths[i]);
6928 + kfree(lower_root_info->lower_paths);
6929 + kfree(UNIONFS_SB(sb)->data);
6932 + * MUST clear the pointers to prevent potential double free if
6933 + * the caller dies later on
6935 + lower_root_info->lower_paths = NULL;
6936 + UNIONFS_SB(sb)->data = NULL;
6943 + * Parse mount options. See the manual page for usage instructions.
6945 + * Returns the dentry object of the lower-level (lower) directory;
6946 + * We want to mount our stackable file system on top of that lower directory.
6948 +static struct unionfs_dentry_info *unionfs_parse_options(
6949 + struct super_block *sb,
6952 + struct unionfs_dentry_info *lower_root_info;
6956 + int dirsfound = 0;
6958 + /* allocate private data area */
6961 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6962 + if (unlikely(!lower_root_info))
6964 + lower_root_info->bstart = -1;
6965 + lower_root_info->bend = -1;
6966 + lower_root_info->bopaque = -1;
6968 + while ((optname = strsep(&options, ",")) != NULL) {
6971 + if (!optname || !*optname)
6974 + optarg = strchr(optname, '=');
6979 + * All of our options take an argument now. Insert ones that
6980 + * don't, above this check.
6983 + printk(KERN_ERR "unionfs: %s requires an argument\n",
6989 + if (!strcmp("dirs", optname)) {
6990 + if (++dirsfound > 1) {
6992 + "unionfs: multiple dirs specified\n");
6996 + err = parse_dirs_option(sb, lower_root_info, optarg);
7004 + "unionfs: unrecognized option '%s'\n", optname);
7007 + if (dirsfound != 1) {
7008 + printk(KERN_ERR "unionfs: dirs option required\n");
7015 + if (lower_root_info && lower_root_info->lower_paths) {
7016 + for (bindex = lower_root_info->bstart;
7017 + bindex >= 0 && bindex <= lower_root_info->bend;
7019 + path_put(&lower_root_info->lower_paths[bindex]);
7022 + kfree(lower_root_info->lower_paths);
7023 + kfree(lower_root_info);
7025 + kfree(UNIONFS_SB(sb)->data);
7026 + UNIONFS_SB(sb)->data = NULL;
7028 + lower_root_info = ERR_PTR(err);
7030 + return lower_root_info;
7034 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7035 + * way anyone can have a reference to the superblock at this point in time.
7037 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7041 + struct unionfs_dentry_info *lower_root_info = NULL;
7042 + int bindex, bstart, bend;
7043 + struct inode *inode = NULL;
7047 + "unionfs: read_super: missing data argument\n");
7052 + /* Allocate superblock private data */
7053 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7054 + if (unlikely(!UNIONFS_SB(sb))) {
7055 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7060 + UNIONFS_SB(sb)->bend = -1;
7061 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7062 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7063 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7065 + lower_root_info = unionfs_parse_options(sb, raw_data);
7066 + if (IS_ERR(lower_root_info)) {
7068 + "unionfs: read_super: error while parsing options "
7069 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7070 + err = PTR_ERR(lower_root_info);
7071 + lower_root_info = NULL;
7074 + if (lower_root_info->bstart == -1) {
7079 + /* set the lower superblock field of upper superblock */
7080 + bstart = lower_root_info->bstart;
7081 + BUG_ON(bstart != 0);
7082 + sbend(sb) = bend = lower_root_info->bend;
7083 + for (bindex = bstart; bindex <= bend; bindex++) {
7084 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7085 + atomic_inc(&d->d_sb->s_active);
7086 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7089 + /* max Bytes is the maximum bytes from highest priority branch */
7090 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7093 + * Our c/m/atime granularity is 1 ns because we may stack on file
7094 + * systems whose granularity is as good. This is important for our
7095 + * time-based cache coherency.
7097 + sb->s_time_gran = 1;
7099 + sb->s_op = &unionfs_sops;
7101 + /* get a new inode and allocate our root dentry */
7102 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
7103 + if (IS_ERR(inode)) {
7104 + err = PTR_ERR(inode);
7107 + sb->s_root = d_make_root(inode);
7108 + if (unlikely(!sb->s_root)) {
7112 + d_set_d_op(sb->s_root, &unionfs_dops);
7114 + /* link the upper and lower dentries */
7115 + sb->s_root->d_fsdata = NULL;
7116 + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7117 + if (unlikely(err))
7120 + /* if get here: cannot have error */
7122 + /* Set the lower dentries for s_root */
7123 + for (bindex = bstart; bindex <= bend; bindex++) {
7125 + struct vfsmount *m;
7127 + d = lower_root_info->lower_paths[bindex].dentry;
7128 + m = lower_root_info->lower_paths[bindex].mnt;
7130 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7131 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7133 + dbstart(sb->s_root) = bstart;
7134 + dbend(sb->s_root) = bend;
7136 + /* Set the generation number to one, since this is for the mount. */
7137 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7139 + if (atomic_read(&inode->i_count) <= 1)
7140 + unionfs_fill_inode(sb->s_root, inode);
7143 + * No need to call interpose because we already have a positive
7144 + * dentry, which was instantiated by d_alloc_root. Just need to
7147 + d_rehash(sb->s_root);
7149 + unionfs_unlock_dentry(sb->s_root);
7150 + goto out; /* all is well */
7153 + if (UNIONFS_D(sb->s_root)) {
7154 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7155 + free_dentry_private_data(sb->s_root);
7163 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7164 + for (bindex = lower_root_info->bstart;
7165 + bindex <= lower_root_info->bend; bindex++) {
7167 + d = lower_root_info->lower_paths[bindex].dentry;
7168 + /* drop refs we took earlier */
7169 + atomic_dec(&d->d_sb->s_active);
7170 + path_put(&lower_root_info->lower_paths[bindex]);
7172 + kfree(lower_root_info->lower_paths);
7173 + kfree(lower_root_info);
7174 + lower_root_info = NULL;
7178 + kfree(UNIONFS_SB(sb)->data);
7179 + kfree(UNIONFS_SB(sb));
7180 + sb->s_fs_info = NULL;
7183 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7184 + kfree(lower_root_info->lower_paths);
7185 + kfree(lower_root_info);
7190 +static struct dentry *unionfs_mount(struct file_system_type *fs_type,
7191 + int flags, const char *dev_name,
7194 + struct dentry *dentry;
7196 + dentry = mount_nodev(fs_type, flags, raw_data, unionfs_read_super);
7197 + if (!IS_ERR(dentry))
7198 + UNIONFS_SB(dentry->d_sb)->dev_name =
7199 + kstrdup(dev_name, GFP_KERNEL);
7203 +static struct file_system_type unionfs_fs_type = {
7204 + .owner = THIS_MODULE,
7205 + .name = UNIONFS_NAME,
7206 + .mount = unionfs_mount,
7207 + .kill_sb = generic_shutdown_super,
7208 + .fs_flags = FS_REVAL_DOT,
7211 +static int __init init_unionfs_fs(void)
7215 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7217 + err = unionfs_init_filldir_cache();
7218 + if (unlikely(err))
7220 + err = unionfs_init_inode_cache();
7221 + if (unlikely(err))
7223 + err = unionfs_init_dentry_cache();
7224 + if (unlikely(err))
7226 + err = init_sioq();
7227 + if (unlikely(err))
7229 + err = register_filesystem(&unionfs_fs_type);
7231 + if (unlikely(err)) {
7233 + unionfs_destroy_filldir_cache();
7234 + unionfs_destroy_inode_cache();
7235 + unionfs_destroy_dentry_cache();
7240 +static void __exit exit_unionfs_fs(void)
7243 + unionfs_destroy_filldir_cache();
7244 + unionfs_destroy_inode_cache();
7245 + unionfs_destroy_dentry_cache();
7246 + unregister_filesystem(&unionfs_fs_type);
7247 + pr_info("Completed unionfs module unload\n");
7250 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7251 + " (http://www.fsl.cs.sunysb.edu)");
7252 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7253 + " (http://unionfs.filesystems.org)");
7254 +MODULE_LICENSE("GPL");
7256 +module_init(init_unionfs_fs);
7257 +module_exit(exit_unionfs_fs);
7258 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7259 new file mode 100644
7260 index 0000000..bcc5652
7262 +++ b/fs/unionfs/mmap.c
7265 + * Copyright (c) 2003-2011 Erez Zadok
7266 + * Copyright (c) 2003-2006 Charles P. Wright
7267 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7268 + * Copyright (c) 2005-2006 Junjiro Okajima
7269 + * Copyright (c) 2006 Shaya Potter
7270 + * Copyright (c) 2005 Arun M. Krishnakumar
7271 + * Copyright (c) 2004-2006 David P. Quigley
7272 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7273 + * Copyright (c) 2003 Puja Gupta
7274 + * Copyright (c) 2003 Harikesavan Krishnan
7275 + * Copyright (c) 2003-2011 Stony Brook University
7276 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
7278 + * This program is free software; you can redistribute it and/or modify
7279 + * it under the terms of the GNU General Public License version 2 as
7280 + * published by the Free Software Foundation.
7287 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7288 + * use in unionfs_mmap) checks for the existence of
7289 + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7290 + * be fixed to allow a file system to define vm_ops->fault without any
7291 + * address_space_ops whatsoever.
7293 + * Otherwise, we don't want to use our readpage method at all.
7295 +static int unionfs_readpage(struct file *file, struct page *page)
7301 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7304 + struct file *file, *lower_file;
7305 + const struct vm_operations_struct *lower_vm_ops;
7306 + struct vm_area_struct lower_vma;
7309 + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7310 + file = lower_vma.vm_file;
7311 + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7312 + BUG_ON(!lower_vm_ops);
7314 + lower_file = unionfs_lower_file(file);
7315 + BUG_ON(!lower_file);
7317 + * XXX: vm_ops->fault may be called in parallel. Because we have to
7318 + * resort to temporarily changing the vma->vm_file to point to the
7319 + * lower file, a concurrent invocation of unionfs_fault could see a
7320 + * different value. In this workaround, we keep a different copy of
7321 + * the vma structure in our stack, so we never expose a different
7322 + * value of the vma->vm_file called to us, even temporarily. A
7323 + * better fix would be to change the calling semantics of ->fault to
7324 + * take an explicit file pointer.
7326 + lower_vma.vm_file = lower_file;
7327 + err = lower_vm_ops->fault(&lower_vma, vmf);
7332 + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7333 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7334 + * the a_ops vector to be non-NULL.
7336 +struct address_space_operations unionfs_aops = {
7337 + /* empty on purpose */
7341 + * XXX: we need a second, dummy address_space_ops vector, to be used
7342 + * temporarily during unionfs_mmap, because the latter calls
7343 + * generic_file_mmap, which checks if ->readpage exists, else returns
7346 +struct address_space_operations unionfs_dummy_aops = {
7347 + .readpage = unionfs_readpage,
7350 +struct vm_operations_struct unionfs_vm_ops = {
7351 + .fault = unionfs_fault,
7353 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7354 new file mode 100644
7355 index 0000000..59b7333
7357 +++ b/fs/unionfs/rdstate.c
7360 + * Copyright (c) 2003-2011 Erez Zadok
7361 + * Copyright (c) 2003-2006 Charles P. Wright
7362 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7363 + * Copyright (c) 2005-2006 Junjiro Okajima
7364 + * Copyright (c) 2005 Arun M. Krishnakumar
7365 + * Copyright (c) 2004-2006 David P. Quigley
7366 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7367 + * Copyright (c) 2003 Puja Gupta
7368 + * Copyright (c) 2003 Harikesavan Krishnan
7369 + * Copyright (c) 2003-2011 Stony Brook University
7370 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
7372 + * This program is free software; you can redistribute it and/or modify
7373 + * it under the terms of the GNU General Public License version 2 as
7374 + * published by the Free Software Foundation.
7379 +/* This file contains the routines for maintaining readdir state. */
7382 + * There are two structures here, rdstate which is a hash table
7383 + * of the second structure which is a filldir_node.
7387 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7388 + * of them and they shouldn't waste memory. If the node has a small name
7389 + * (as defined by the dentry structure), then we use an inline name to
7390 + * preserve kmalloc space.
7392 +static struct kmem_cache *unionfs_filldir_cachep;
7394 +int unionfs_init_filldir_cache(void)
7396 + unionfs_filldir_cachep =
7397 + kmem_cache_create("unionfs_filldir",
7398 + sizeof(struct filldir_node), 0,
7399 + SLAB_RECLAIM_ACCOUNT, NULL);
7401 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7404 +void unionfs_destroy_filldir_cache(void)
7406 + if (unionfs_filldir_cachep)
7407 + kmem_cache_destroy(unionfs_filldir_cachep);
7411 + * This is a tuning parameter that tells us roughly how big to make the
7412 + * hash table in directory entries per page. This isn't perfect, but
7413 + * at least we get a hash table size that shouldn't be too overloaded.
7414 + * The following averages are based on my home directory.
7415 + * 14.44693 Overall
7416 + * 12.29 Single Page Directories
7417 + * 117.93 Multi-page directories
7419 +#define DENTPAGE 4096
7420 +#define DENTPERONEPAGE 12
7421 +#define DENTPERPAGE 118
7422 +#define MINHASHSIZE 1
7423 +static int guesstimate_hash_size(struct inode *inode)
7425 + struct inode *lower_inode;
7427 + int hashsize = MINHASHSIZE;
7429 + if (UNIONFS_I(inode)->hashsize > 0)
7430 + return UNIONFS_I(inode)->hashsize;
7432 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7433 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7437 + if (i_size_read(lower_inode) == DENTPAGE)
7438 + hashsize += DENTPERONEPAGE;
7440 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7447 +int init_rdstate(struct file *file)
7449 + BUG_ON(sizeof(loff_t) !=
7450 + (sizeof(unsigned int) + sizeof(unsigned int)));
7451 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7453 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7456 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7459 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7461 + struct unionfs_dir_state *rdstate = NULL;
7462 + struct list_head *pos;
7464 + spin_lock(&UNIONFS_I(inode)->rdlock);
7465 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7466 + struct unionfs_dir_state *r =
7467 + list_entry(pos, struct unionfs_dir_state, cache);
7468 + if (fpos == rdstate2offset(r)) {
7469 + UNIONFS_I(inode)->rdcount--;
7470 + list_del(&r->cache);
7475 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7479 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7483 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7484 + struct unionfs_dir_state *rdstate;
7486 + hashsize = guesstimate_hash_size(inode);
7487 + mallocsize += hashsize * sizeof(struct list_head);
7488 + mallocsize = __roundup_pow_of_two(mallocsize);
7490 + /* This should give us about 500 entries anyway. */
7491 + if (mallocsize > PAGE_SIZE)
7492 + mallocsize = PAGE_SIZE;
7494 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7495 + sizeof(struct list_head);
7497 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7498 + if (unlikely(!rdstate))
7501 + spin_lock(&UNIONFS_I(inode)->rdlock);
7502 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7503 + UNIONFS_I(inode)->cookie = 1;
7505 + UNIONFS_I(inode)->cookie++;
7507 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7508 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7509 + rdstate->offset = 1;
7510 + rdstate->access = jiffies;
7511 + rdstate->bindex = bindex;
7512 + rdstate->dirpos = 0;
7513 + rdstate->hashentries = 0;
7514 + rdstate->size = hashsize;
7515 + for (i = 0; i < rdstate->size; i++)
7516 + INIT_LIST_HEAD(&rdstate->list[i]);
7521 +static void free_filldir_node(struct filldir_node *node)
7523 + if (node->namelen >= DNAME_INLINE_LEN)
7524 + kfree(node->name);
7525 + kmem_cache_free(unionfs_filldir_cachep, node);
7528 +void free_rdstate(struct unionfs_dir_state *state)
7530 + struct filldir_node *tmp;
7533 + for (i = 0; i < state->size; i++) {
7534 + struct list_head *head = &(state->list[i]);
7535 + struct list_head *pos, *n;
7537 + /* traverse the list and deallocate space */
7538 + list_for_each_safe(pos, n, head) {
7539 + tmp = list_entry(pos, struct filldir_node, file_list);
7540 + list_del(&tmp->file_list);
7541 + free_filldir_node(tmp);
7548 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7549 + const char *name, int namelen,
7553 + unsigned int hash;
7554 + struct list_head *head;
7555 + struct list_head *pos;
7556 + struct filldir_node *cursor = NULL;
7559 + BUG_ON(namelen <= 0);
7561 + hash = full_name_hash(name, namelen);
7562 + index = hash % rdstate->size;
7564 + head = &(rdstate->list[index]);
7565 + list_for_each(pos, head) {
7566 + cursor = list_entry(pos, struct filldir_node, file_list);
7568 + if (cursor->namelen == namelen && cursor->hash == hash &&
7569 + !strncmp(cursor->name, name, namelen)) {
7571 + * a duplicate exists, and hence no need to create
7572 + * entry to the list
7577 + * if a duplicate is found in this branch, and is
7578 + * not due to the caller looking for an entry to
7579 + * whiteout, then the file system may be corrupted.
7581 + if (unlikely(!is_whiteout &&
7582 + cursor->bindex == rdstate->bindex))
7583 + printk(KERN_ERR "unionfs: filldir: possible "
7584 + "I/O error: a file is duplicated "
7585 + "in the same branch %d: %s\n",
7586 + rdstate->bindex, cursor->name);
7597 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7598 + int namelen, int bindex, int whiteout)
7600 + struct filldir_node *new;
7601 + unsigned int hash;
7604 + struct list_head *head;
7606 + BUG_ON(namelen <= 0);
7608 + hash = full_name_hash(name, namelen);
7609 + index = hash % rdstate->size;
7610 + head = &(rdstate->list[index]);
7612 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7613 + if (unlikely(!new)) {
7618 + INIT_LIST_HEAD(&new->file_list);
7619 + new->namelen = namelen;
7621 + new->bindex = bindex;
7622 + new->whiteout = whiteout;
7624 + if (namelen < DNAME_INLINE_LEN) {
7625 + new->name = new->iname;
7627 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7628 + if (unlikely(!new->name)) {
7629 + kmem_cache_free(unionfs_filldir_cachep, new);
7635 + memcpy(new->name, name, namelen);
7636 + new->name[namelen] = '\0';
7638 + rdstate->hashentries++;
7640 + list_add(&(new->file_list), head);
7644 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7645 new file mode 100644
7646 index 0000000..ce85b84
7648 +++ b/fs/unionfs/rename.c
7651 + * Copyright (c) 2003-2011 Erez Zadok
7652 + * Copyright (c) 2003-2006 Charles P. Wright
7653 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7654 + * Copyright (c) 2005-2006 Junjiro Okajima
7655 + * Copyright (c) 2005 Arun M. Krishnakumar
7656 + * Copyright (c) 2004-2006 David P. Quigley
7657 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7658 + * Copyright (c) 2003 Puja Gupta
7659 + * Copyright (c) 2003 Harikesavan Krishnan
7660 + * Copyright (c) 2003-2011 Stony Brook University
7661 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
7663 + * This program is free software; you can redistribute it and/or modify
7664 + * it under the terms of the GNU General Public License version 2 as
7665 + * published by the Free Software Foundation.
7671 + * This is a helper function for rename, used when rename ends up with hosed
7672 + * over dentries and we need to revert.
7674 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7675 + struct dentry *parent, int bindex)
7677 + struct dentry *lower_dentry;
7678 + struct dentry *lower_parent;
7680 + struct nameidata lower_nd;
7682 + verify_locked(dentry);
7684 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7686 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7688 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
7689 + if (unlikely(err < 0))
7691 + lower_dentry = lookup_one_len_nd(dentry->d_name.name, lower_parent,
7692 + dentry->d_name.len, &lower_nd);
7693 + release_lower_nd(&lower_nd, err);
7694 + if (IS_ERR(lower_dentry)) {
7695 + err = PTR_ERR(lower_dentry);
7699 + dput(unionfs_lower_dentry_idx(dentry, bindex));
7700 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7701 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7703 + if (!lower_dentry->d_inode) {
7704 + dput(lower_dentry);
7705 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7707 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7708 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7709 + igrab(lower_dentry->d_inode));
7716 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7717 + struct dentry *old_parent,
7718 + struct inode *new_dir, struct dentry *new_dentry,
7719 + struct dentry *new_parent,
7723 + struct dentry *lower_old_dentry;
7724 + struct dentry *lower_new_dentry;
7725 + struct dentry *lower_old_dir_dentry;
7726 + struct dentry *lower_new_dir_dentry;
7727 + struct dentry *trap;
7729 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7730 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7732 + if (!lower_new_dentry) {
7733 + lower_new_dentry =
7734 + create_parents(new_parent->d_inode,
7735 + new_dentry, new_dentry->d_name.name,
7737 + if (IS_ERR(lower_new_dentry)) {
7738 + err = PTR_ERR(lower_new_dentry);
7739 + if (IS_COPYUP_ERR(err))
7741 + printk(KERN_ERR "unionfs: error creating directory "
7742 + "tree for rename, bindex=%d err=%d\n",
7748 + /* check for and remove whiteout, if any */
7749 + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7750 + if (err > 0) /* ignore if whiteout found and successfully removed */
7755 + /* check of old_dentry branch is writable */
7756 + err = is_robranch_super(old_dentry->d_sb, bindex);
7760 + dget(lower_old_dentry);
7761 + dget(lower_new_dentry);
7762 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7763 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7765 + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7766 + /* source should not be ancenstor of target */
7767 + if (trap == lower_old_dentry) {
7769 + goto out_err_unlock;
7771 + /* target should not be ancenstor of source */
7772 + if (trap == lower_new_dentry) {
7774 + goto out_err_unlock;
7776 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7777 + lower_new_dir_dentry->d_inode, lower_new_dentry);
7780 + /* update parent dir times */
7781 + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7782 + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7784 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7786 + dput(lower_old_dir_dentry);
7787 + dput(lower_new_dir_dentry);
7788 + dput(lower_old_dentry);
7789 + dput(lower_new_dentry);
7793 + /* Fixup the new_dentry. */
7794 + if (bindex < dbstart(new_dentry))
7795 + dbstart(new_dentry) = bindex;
7796 + else if (bindex > dbend(new_dentry))
7797 + dbend(new_dentry) = bindex;
7804 + * Main rename code. This is sufficiently complex, that it's documented in
7805 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7806 + * __unionfs_rename() above to perform some of the work.
7808 +static int do_unionfs_rename(struct inode *old_dir,
7809 + struct dentry *old_dentry,
7810 + struct dentry *old_parent,
7811 + struct inode *new_dir,
7812 + struct dentry *new_dentry,
7813 + struct dentry *new_parent)
7817 + int old_bstart, old_bend;
7818 + int new_bstart, new_bend;
7819 + int do_copyup = -1;
7820 + int local_err = 0;
7824 + old_bstart = dbstart(old_dentry);
7825 + old_bend = dbend(old_dentry);
7827 + new_bstart = dbstart(new_dentry);
7828 + new_bend = dbend(new_dentry);
7830 + /* Rename source to destination. */
7831 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7832 + new_dir, new_dentry, new_parent,
7835 + if (!IS_COPYUP_ERR(err))
7837 + do_copyup = old_bstart - 1;
7843 + * Unlink all instances of destination that exist to the left of
7844 + * bstart of source. On error, revert back, goto out.
7846 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7847 + struct dentry *unlink_dentry;
7848 + struct dentry *unlink_dir_dentry;
7850 + BUG_ON(bindex < 0);
7851 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7852 + if (!unlink_dentry)
7855 + unlink_dir_dentry = lock_parent(unlink_dentry);
7856 + err = is_robranch_super(old_dir->i_sb, bindex);
7858 + err = vfs_unlink(unlink_dir_dentry->d_inode,
7861 + fsstack_copy_attr_times(new_parent->d_inode,
7862 + unlink_dir_dentry->d_inode);
7863 + /* propagate number of hard-links */
7864 + set_nlink(new_parent->d_inode,
7865 + unionfs_get_nlinks(new_parent->d_inode));
7867 + unlock_dir(unlink_dir_dentry);
7869 + if (bindex != new_bstart) {
7870 + dput(unlink_dentry);
7871 + unionfs_set_lower_dentry_idx(new_dentry,
7874 + } else if (IS_COPYUP_ERR(err)) {
7875 + do_copyup = bindex - 1;
7876 + } else if (revert) {
7881 + if (do_copyup != -1) {
7882 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7884 + * copyup the file into some left directory, so that
7885 + * you can rename it
7887 + err = copyup_dentry(old_parent->d_inode,
7888 + old_dentry, old_bstart, bindex,
7889 + old_dentry->d_name.name,
7890 + old_dentry->d_name.len, NULL,
7891 + i_size_read(old_dentry->d_inode));
7892 + /* if copyup failed, try next branch to the left */
7896 + * create whiteout before calling __unionfs_rename
7897 + * because the latter will change the old_dentry's
7898 + * lower name and parent dir, resulting in the
7899 + * whiteout getting created in the wrong dir.
7901 + err = create_whiteout(old_dentry, bindex);
7903 + printk(KERN_ERR "unionfs: can't create a "
7904 + "whiteout for %s in rename (err=%d)\n",
7905 + old_dentry->d_name.name, err);
7908 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7909 + new_dir, new_dentry, new_parent,
7915 + /* make it opaque */
7916 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7917 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7923 + * Create whiteout for source, only if:
7924 + * (1) There is more than one underlying instance of source.
7925 + * (We did a copy_up is taken care of above).
7927 + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7928 + err = create_whiteout(old_dentry, old_bstart);
7930 + /* can't fix anything now, so we exit with -EIO */
7931 + printk(KERN_ERR "unionfs: can't create a whiteout for "
7932 + "%s in rename!\n", old_dentry->d_name.name);
7941 + /* Do revert here. */
7942 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7945 + printk(KERN_ERR "unionfs: revert failed in rename: "
7946 + "the new refresh failed\n");
7950 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7953 + printk(KERN_ERR "unionfs: revert failed in rename: "
7954 + "the old refresh failed\n");
7959 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7960 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7961 + printk(KERN_ERR "unionfs: revert failed in rename: "
7962 + "the object disappeared from under us!\n");
7967 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
7968 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
7969 + printk(KERN_ERR "unionfs: revert failed in rename: "
7970 + "the object was created underneath us!\n");
7975 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
7976 + old_dir, old_dentry, old_parent,
7979 + /* If we can't fix it, then we cop-out with -EIO. */
7981 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
7985 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7989 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
8001 + * We can't copyup a directory, because it may involve huge numbers of
8002 + * children, etc. Doing that in the kernel would be bad, so instead we
8003 + * return EXDEV to the user-space utility that caused this, and let the
8004 + * user-space recurse and ask us to copy up each file separately.
8006 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
8010 + err = check_empty(dentry, parent, NULL);
8011 + if (err == -ENOTEMPTY) {
8012 + if (is_robranch(dentry))
8018 + bstart = dbstart(dentry);
8019 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8022 + dbstart(dentry) = bstart + 1;
8023 + err = check_empty(dentry, parent, NULL);
8024 + dbstart(dentry) = bstart;
8025 + if (err == -ENOTEMPTY)
8031 + * The locking rules in unionfs_rename are complex. We could use a simpler
8032 + * superblock-level name-space lock for renames and copy-ups.
8034 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8035 + struct inode *new_dir, struct dentry *new_dentry)
8038 + struct dentry *wh_dentry;
8039 + struct dentry *old_parent, *new_parent;
8042 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
8043 + old_parent = dget_parent(old_dentry);
8044 + new_parent = dget_parent(new_dentry);
8045 + /* un/lock parent dentries only if they differ from old/new_dentry */
8046 + if (old_parent != old_dentry &&
8047 + old_parent != new_dentry)
8048 + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
8049 + if (new_parent != old_dentry &&
8050 + new_parent != new_dentry &&
8051 + new_parent != old_parent)
8052 + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
8053 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8055 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
8060 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
8061 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
8068 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8069 + err = unionfs_partial_lookup(old_dentry, old_parent);
8071 + err = may_rename_dir(old_dentry, old_parent);
8076 + err = unionfs_partial_lookup(new_dentry, new_parent);
8081 + * if new_dentry is already lower because of whiteout,
8082 + * simply override it even if the whited-out dir is not empty.
8084 + wh_dentry = find_first_whiteout(new_dentry);
8085 + if (!IS_ERR(wh_dentry)) {
8087 + } else if (new_dentry->d_inode) {
8088 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8089 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8090 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8091 + -ENOTDIR : -EISDIR;
8095 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8096 + struct unionfs_dir_state *namelist = NULL;
8097 + /* check if this unionfs directory is empty or not */
8098 + err = check_empty(new_dentry, new_parent, &namelist);
8102 + if (!is_robranch(new_dentry))
8103 + err = delete_whiteouts(new_dentry,
8104 + dbstart(new_dentry),
8107 + free_rdstate(namelist);
8114 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8115 + new_dir, new_dentry, new_parent);
8120 + * force re-lookup since the dir on ro branch is not renamed, and
8121 + * lower dentries still indicate the un-renamed ones.
8123 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8124 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8126 + unionfs_postcopyup_release(old_dentry);
8127 + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8128 + unionfs_postcopyup_release(new_dentry);
8129 + unionfs_postcopyup_setmnt(new_dentry);
8130 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8132 + * If we get here, it means that no copyup was
8133 + * needed, and that a file by the old name already
8134 + * existing on the destination branch; that file got
8135 + * renamed earlier in this function, so all we need
8136 + * to do here is set the lower inode.
8138 + struct inode *inode;
8139 + inode = unionfs_lower_inode(old_dentry->d_inode);
8141 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8142 + dbstart(new_dentry),
8146 + /* if all of this renaming succeeded, update our times */
8147 + unionfs_copy_attr_times(old_dentry->d_inode);
8148 + unionfs_copy_attr_times(new_dentry->d_inode);
8149 + unionfs_check_inode(old_dir);
8150 + unionfs_check_inode(new_dir);
8151 + unionfs_check_dentry(old_dentry);
8152 + unionfs_check_dentry(new_dentry);
8155 + if (err) /* clear the new_dentry stuff created */
8156 + d_drop(new_dentry);
8158 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8159 + if (new_parent != old_dentry &&
8160 + new_parent != new_dentry &&
8161 + new_parent != old_parent)
8162 + unionfs_unlock_dentry(new_parent);
8163 + if (old_parent != old_dentry &&
8164 + old_parent != new_dentry)
8165 + unionfs_unlock_dentry(old_parent);
8168 + unionfs_read_unlock(old_dentry->d_sb);
8172 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8173 new file mode 100644
8174 index 0000000..b923742
8176 +++ b/fs/unionfs/sioq.c
8179 + * Copyright (c) 2006-2011 Erez Zadok
8180 + * Copyright (c) 2006 Charles P. Wright
8181 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8182 + * Copyright (c) 2006 Junjiro Okajima
8183 + * Copyright (c) 2006 David P. Quigley
8184 + * Copyright (c) 2006-2011 Stony Brook University
8185 + * Copyright (c) 2006-2011 The Research Foundation of SUNY
8187 + * This program is free software; you can redistribute it and/or modify
8188 + * it under the terms of the GNU General Public License version 2 as
8189 + * published by the Free Software Foundation.
8195 + * Super-user IO work Queue - sometimes we need to perform actions which
8196 + * would fail due to the unix permissions on the parent directory (e.g.,
8197 + * rmdir a directory which appears empty, but in reality contains
8201 +static struct workqueue_struct *superio_workqueue;
8203 +int __init init_sioq(void)
8207 + superio_workqueue = create_workqueue("unionfs_siod");
8208 + if (!IS_ERR(superio_workqueue))
8211 + err = PTR_ERR(superio_workqueue);
8212 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8213 + superio_workqueue = NULL;
8217 +void stop_sioq(void)
8219 + if (superio_workqueue)
8220 + destroy_workqueue(superio_workqueue);
8223 +void run_sioq(work_func_t func, struct sioq_args *args)
8225 + INIT_WORK(&args->work, func);
8227 + init_completion(&args->comp);
8228 + while (!queue_work(superio_workqueue, &args->work)) {
8229 + /* TODO: do accounting if needed */
8232 + wait_for_completion(&args->comp);
8235 +void __unionfs_create(struct work_struct *work)
8237 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8238 + struct create_args *c = &args->create;
8240 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8241 + complete(&args->comp);
8244 +void __unionfs_mkdir(struct work_struct *work)
8246 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8247 + struct mkdir_args *m = &args->mkdir;
8249 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8250 + complete(&args->comp);
8253 +void __unionfs_mknod(struct work_struct *work)
8255 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8256 + struct mknod_args *m = &args->mknod;
8258 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8259 + complete(&args->comp);
8262 +void __unionfs_symlink(struct work_struct *work)
8264 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8265 + struct symlink_args *s = &args->symlink;
8267 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8268 + complete(&args->comp);
8271 +void __unionfs_unlink(struct work_struct *work)
8273 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8274 + struct unlink_args *u = &args->unlink;
8276 + args->err = vfs_unlink(u->parent, u->dentry);
8277 + complete(&args->comp);
8279 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8280 new file mode 100644
8281 index 0000000..c2dfb94
8283 +++ b/fs/unionfs/sioq.h
8286 + * Copyright (c) 2006-2011 Erez Zadok
8287 + * Copyright (c) 2006 Charles P. Wright
8288 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8289 + * Copyright (c) 2006 Junjiro Okajima
8290 + * Copyright (c) 2006 David P. Quigley
8291 + * Copyright (c) 2006-2011 Stony Brook University
8292 + * Copyright (c) 2006-2011 The Research Foundation of SUNY
8294 + * This program is free software; you can redistribute it and/or modify
8295 + * it under the terms of the GNU General Public License version 2 as
8296 + * published by the Free Software Foundation.
8302 +struct deletewh_args {
8303 + struct unionfs_dir_state *namelist;
8304 + struct dentry *dentry;
8308 +struct is_opaque_args {
8309 + struct dentry *dentry;
8312 +struct create_args {
8313 + struct inode *parent;
8314 + struct dentry *dentry;
8316 + struct nameidata *nd;
8319 +struct mkdir_args {
8320 + struct inode *parent;
8321 + struct dentry *dentry;
8325 +struct mknod_args {
8326 + struct inode *parent;
8327 + struct dentry *dentry;
8332 +struct symlink_args {
8333 + struct inode *parent;
8334 + struct dentry *dentry;
8338 +struct unlink_args {
8339 + struct inode *parent;
8340 + struct dentry *dentry;
8345 + struct completion comp;
8346 + struct work_struct work;
8351 + struct deletewh_args deletewh;
8352 + struct is_opaque_args is_opaque;
8353 + struct create_args create;
8354 + struct mkdir_args mkdir;
8355 + struct mknod_args mknod;
8356 + struct symlink_args symlink;
8357 + struct unlink_args unlink;
8361 +/* Extern definitions for SIOQ functions */
8362 +extern int __init init_sioq(void);
8363 +extern void stop_sioq(void);
8364 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8366 +/* Extern definitions for our privilege escalation helpers */
8367 +extern void __unionfs_create(struct work_struct *work);
8368 +extern void __unionfs_mkdir(struct work_struct *work);
8369 +extern void __unionfs_mknod(struct work_struct *work);
8370 +extern void __unionfs_symlink(struct work_struct *work);
8371 +extern void __unionfs_unlink(struct work_struct *work);
8372 +extern void __delete_whiteouts(struct work_struct *work);
8373 +extern void __is_opaque_dir(struct work_struct *work);
8375 +#endif /* not _SIOQ_H */
8376 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8377 new file mode 100644
8378 index 0000000..e7fc5a5
8380 +++ b/fs/unionfs/subr.c
8383 + * Copyright (c) 2003-2011 Erez Zadok
8384 + * Copyright (c) 2003-2006 Charles P. Wright
8385 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8386 + * Copyright (c) 2005-2006 Junjiro Okajima
8387 + * Copyright (c) 2005 Arun M. Krishnakumar
8388 + * Copyright (c) 2004-2006 David P. Quigley
8389 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8390 + * Copyright (c) 2003 Puja Gupta
8391 + * Copyright (c) 2003 Harikesavan Krishnan
8392 + * Copyright (c) 2003-2011 Stony Brook University
8393 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
8395 + * This program is free software; you can redistribute it and/or modify
8396 + * it under the terms of the GNU General Public License version 2 as
8397 + * published by the Free Software Foundation.
8403 + * returns the right n_link value based on the inode type
8405 +int unionfs_get_nlinks(const struct inode *inode)
8407 + /* don't bother to do all the work since we're unlinked */
8408 + if (inode->i_nlink == 0)
8411 + if (!S_ISDIR(inode->i_mode))
8412 + return unionfs_lower_inode(inode)->i_nlink;
8415 + * For directories, we return 1. The only place that could cares
8416 + * about links is readdir, and there's d_type there so even that
8422 +/* copy a/m/ctime from the lower branch with the newest times */
8423 +void unionfs_copy_attr_times(struct inode *upper)
8426 + struct inode *lower;
8430 + if (ibstart(upper) < 0) {
8431 +#ifdef CONFIG_UNION_FS_DEBUG
8432 + WARN_ON(ibstart(upper) < 0);
8433 +#endif /* CONFIG_UNION_FS_DEBUG */
8436 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8437 + lower = unionfs_lower_inode_idx(upper, bindex);
8439 + continue; /* not all lower dir objects may exist */
8440 + if (unlikely(timespec_compare(&upper->i_mtime,
8441 + &lower->i_mtime) < 0))
8442 + upper->i_mtime = lower->i_mtime;
8443 + if (unlikely(timespec_compare(&upper->i_ctime,
8444 + &lower->i_ctime) < 0))
8445 + upper->i_ctime = lower->i_ctime;
8446 + if (unlikely(timespec_compare(&upper->i_atime,
8447 + &lower->i_atime) < 0))
8448 + upper->i_atime = lower->i_atime;
8453 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8454 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8455 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8456 + * important if the lower inode is a directory type)
8458 +void unionfs_copy_attr_all(struct inode *dest,
8459 + const struct inode *src)
8461 + dest->i_mode = src->i_mode;
8462 + dest->i_uid = src->i_uid;
8463 + dest->i_gid = src->i_gid;
8464 + dest->i_rdev = src->i_rdev;
8466 + unionfs_copy_attr_times(dest);
8468 + dest->i_blkbits = src->i_blkbits;
8469 + dest->i_flags = src->i_flags;
8472 + * Update the nlinks AFTER updating the above fields, because the
8473 + * get_links callback may depend on them.
8475 + set_nlink(dest, unionfs_get_nlinks(dest));
8477 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8478 new file mode 100644
8479 index 0000000..b99f14d
8481 +++ b/fs/unionfs/super.c
8484 + * Copyright (c) 2003-2011 Erez Zadok
8485 + * Copyright (c) 2003-2006 Charles P. Wright
8486 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8487 + * Copyright (c) 2005-2006 Junjiro Okajima
8488 + * Copyright (c) 2005 Arun M. Krishnakumar
8489 + * Copyright (c) 2004-2006 David P. Quigley
8490 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8491 + * Copyright (c) 2003 Puja Gupta
8492 + * Copyright (c) 2003 Harikesavan Krishnan
8493 + * Copyright (c) 2003-2011 Stony Brook University
8494 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
8496 + * This program is free software; you can redistribute it and/or modify
8497 + * it under the terms of the GNU General Public License version 2 as
8498 + * published by the Free Software Foundation.
8504 + * The inode cache is used with alloc_inode for both our inode info and the
8507 +static struct kmem_cache *unionfs_inode_cachep;
8509 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8512 + struct unionfs_inode_info *info;
8513 + struct inode *inode;
8515 + inode = iget_locked(sb, ino);
8517 + return ERR_PTR(-ENOMEM);
8518 + if (!(inode->i_state & I_NEW))
8521 + info = UNIONFS_I(inode);
8522 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8523 + info->bstart = -1;
8525 + atomic_set(&info->generation,
8526 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8527 + spin_lock_init(&info->rdlock);
8528 + info->rdcount = 1;
8529 + info->hashsize = -1;
8530 + INIT_LIST_HEAD(&info->readdircache);
8532 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8533 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8534 + if (unlikely(!info->lower_inodes)) {
8535 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8536 + "lower-pointer array!\n");
8537 + iget_failed(inode);
8538 + return ERR_PTR(-ENOMEM);
8541 + inode->i_version++;
8542 + inode->i_op = &unionfs_main_iops;
8543 + inode->i_fop = &unionfs_main_fops;
8545 + inode->i_mapping->a_ops = &unionfs_aops;
8548 + * reset times so unionfs_copy_attr_all can keep out time invariants
8549 + * right (upper inode time being the max of all lower ones).
8551 + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8552 + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8553 + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8554 + unlock_new_inode(inode);
8559 + * final actions when unmounting a file system
8561 + * No need to lock rwsem.
8563 +static void unionfs_put_super(struct super_block *sb)
8565 + int bindex, bstart, bend;
8566 + struct unionfs_sb_info *spd;
8569 + spd = UNIONFS_SB(sb);
8573 + bstart = sbstart(sb);
8576 + /* Make sure we have no leaks of branchget/branchput. */
8577 + for (bindex = bstart; bindex <= bend; bindex++)
8578 + if (unlikely(branch_count(sb, bindex) != 0)) {
8580 + "unionfs: branch %d has %d references left!\n",
8581 + bindex, branch_count(sb, bindex));
8584 + WARN_ON(leaks != 0);
8586 + /* decrement lower super references */
8587 + for (bindex = bstart; bindex <= bend; bindex++) {
8588 + struct super_block *s;
8589 + s = unionfs_lower_super_idx(sb, bindex);
8590 + unionfs_set_lower_super_idx(sb, bindex, NULL);
8591 + atomic_dec(&s->s_active);
8594 + kfree(spd->dev_name);
8597 + sb->s_fs_info = NULL;
8601 + * Since people use this to answer the "How big of a file can I write?"
8602 + * question, we report the size of the highest priority branch as the size of
8605 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8608 + struct super_block *sb;
8609 + struct dentry *lower_dentry;
8610 + struct dentry *parent;
8611 + struct path lower_path;
8614 + sb = dentry->d_sb;
8616 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8617 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8618 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8620 + valid = __unionfs_d_revalidate(dentry, parent, false);
8621 + if (unlikely(!valid)) {
8625 + unionfs_check_dentry(dentry);
8627 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8628 + lower_path.dentry = lower_dentry;
8629 + lower_path.mnt = unionfs_mntget(sb->s_root, 0);
8630 + err = vfs_statfs(&lower_path, buf);
8631 + mntput(lower_path.mnt);
8633 + /* set return buf to our f/s to avoid confusing user-level utils */
8634 + buf->f_type = UNIONFS_SUPER_MAGIC;
8636 + * Our maximum file name can is shorter by a few bytes because every
8637 + * file name could potentially be whited-out.
8639 + * XXX: this restriction goes away with ODF.
8641 + unionfs_set_max_namelen(&buf->f_namelen);
8644 + * reset two fields to avoid confusing user-land.
8645 + * XXX: is this still necessary?
8647 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8648 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8651 + unionfs_check_dentry(dentry);
8652 + unionfs_unlock_dentry(dentry);
8653 + unionfs_unlock_parent(dentry, parent);
8654 + unionfs_read_unlock(sb);
8658 +/* handle mode changing during remount */
8659 +static noinline_for_stack int do_remount_mode_option(
8662 + struct unionfs_data *new_data,
8663 + struct path *new_lower_paths)
8665 + int err = -EINVAL;
8667 + char *modename = strchr(optarg, '=');
8670 + /* by now, optarg contains the branch name */
8673 + "unionfs: no branch specified for mode change\n");
8677 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8681 + *modename++ = '\0';
8682 + err = parse_branch_mode(modename, &perms);
8684 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8685 + modename, optarg);
8690 + * Find matching branch index. For now, this assumes that nothing
8691 + * has been mounted on top of this Unionfs stack. Once we have /odf
8692 + * and cache-coherency resolved, we'll address the branch-path
8695 + err = kern_path(optarg, LOOKUP_FOLLOW, &path);
8697 + printk(KERN_ERR "unionfs: error accessing "
8698 + "lower directory \"%s\" (error %d)\n",
8702 + for (idx = 0; idx < cur_branches; idx++)
8703 + if (path.mnt == new_lower_paths[idx].mnt &&
8704 + path.dentry == new_lower_paths[idx].dentry)
8706 + path_put(&path); /* no longer needed */
8707 + if (idx == cur_branches) {
8708 + err = -ENOENT; /* err may have been reset above */
8709 + printk(KERN_ERR "unionfs: branch \"%s\" "
8710 + "not found\n", optarg);
8713 + /* check/change mode for existing branch */
8714 + /* we don't warn if perms==branchperms */
8715 + new_data[idx].branchperms = perms;
8721 +/* handle branch deletion during remount */
8722 +static noinline_for_stack int do_remount_del_option(
8723 + char *optarg, int cur_branches,
8724 + struct unionfs_data *new_data,
8725 + struct path *new_lower_paths)
8727 + int err = -EINVAL;
8731 + /* optarg contains the branch name to delete */
8734 + * Find matching branch index. For now, this assumes that nothing
8735 + * has been mounted on top of this Unionfs stack. Once we have /odf
8736 + * and cache-coherency resolved, we'll address the branch-path
8739 + err = kern_path(optarg, LOOKUP_FOLLOW, &path);
8741 + printk(KERN_ERR "unionfs: error accessing "
8742 + "lower directory \"%s\" (error %d)\n",
8746 + for (idx = 0; idx < cur_branches; idx++)
8747 + if (path.mnt == new_lower_paths[idx].mnt &&
8748 + path.dentry == new_lower_paths[idx].dentry)
8750 + path_put(&path); /* no longer needed */
8751 + if (idx == cur_branches) {
8752 + printk(KERN_ERR "unionfs: branch \"%s\" "
8753 + "not found\n", optarg);
8757 + /* check if there are any open files on the branch to be deleted */
8758 + if (atomic_read(&new_data[idx].open_files) > 0) {
8764 + * Now we have to delete the branch. First, release any handles it
8765 + * has. Then, move the remaining array indexes past "idx" in
8766 + * new_data and new_lower_paths one to the left. Finally, adjust
8769 + path_put(&new_lower_paths[idx]);
8771 + if (idx < cur_branches - 1) {
8772 + /* if idx==cur_branches-1, we delete last branch: easy */
8773 + memmove(&new_data[idx], &new_data[idx+1],
8774 + (cur_branches - 1 - idx) *
8775 + sizeof(struct unionfs_data));
8776 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8777 + (cur_branches - 1 - idx) * sizeof(struct path));
8785 +/* handle branch insertion during remount */
8786 +static noinline_for_stack int do_remount_add_option(
8787 + char *optarg, int cur_branches,
8788 + struct unionfs_data *new_data,
8789 + struct path *new_lower_paths,
8790 + int *high_branch_id)
8792 + int err = -EINVAL;
8794 + int idx = 0; /* default: insert at beginning */
8795 + char *new_branch , *modename = NULL;
8799 + * optarg can be of several forms:
8801 + * /bar:/foo insert /foo before /bar
8802 + * /bar:/foo=ro insert /foo in ro mode before /bar
8803 + * /foo insert /foo in the beginning (prepend)
8804 + * :/foo insert /foo at the end (append)
8806 + if (*optarg == ':') { /* append? */
8807 + new_branch = optarg + 1; /* skip ':' */
8808 + idx = cur_branches;
8809 + goto found_insertion_point;
8811 + new_branch = strchr(optarg, ':');
8812 + if (!new_branch) { /* prepend? */
8813 + new_branch = optarg;
8814 + goto found_insertion_point;
8816 + *new_branch++ = '\0'; /* holds path+mode of new branch */
8819 + * Find matching branch index. For now, this assumes that nothing
8820 + * has been mounted on top of this Unionfs stack. Once we have /odf
8821 + * and cache-coherency resolved, we'll address the branch-path
8824 + err = kern_path(optarg, LOOKUP_FOLLOW, &path);
8826 + printk(KERN_ERR "unionfs: error accessing "
8827 + "lower directory \"%s\" (error %d)\n",
8831 + for (idx = 0; idx < cur_branches; idx++)
8832 + if (path.mnt == new_lower_paths[idx].mnt &&
8833 + path.dentry == new_lower_paths[idx].dentry)
8835 + path_put(&path); /* no longer needed */
8836 + if (idx == cur_branches) {
8837 + printk(KERN_ERR "unionfs: branch \"%s\" "
8838 + "not found\n", optarg);
8844 + * At this point idx will hold the index where the new branch should
8845 + * be inserted before.
8847 +found_insertion_point:
8848 + /* find the mode for the new branch */
8850 + modename = strchr(new_branch, '=');
8852 + *modename++ = '\0';
8853 + if (!new_branch || !*new_branch) {
8854 + printk(KERN_ERR "unionfs: null new branch\n");
8858 + err = parse_branch_mode(modename, &perms);
8860 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8861 + "branch \"%s\"\n", modename, new_branch);
8864 + err = kern_path(new_branch, LOOKUP_FOLLOW, &path);
8866 + printk(KERN_ERR "unionfs: error accessing "
8867 + "lower directory \"%s\" (error %d)\n",
8872 + * It's probably safe to check_mode the new branch to insert. Note:
8873 + * we don't allow inserting branches which are unionfs's by
8874 + * themselves (check_branch returns EINVAL in that case). This is
8875 + * because this code base doesn't support stacking unionfs: the ODF
8876 + * code base supports that correctly.
8878 + err = check_branch(&path);
8880 + printk(KERN_ERR "unionfs: lower directory "
8881 + "\"%s\" is not a valid branch\n", optarg);
8887 + * Now we have to insert the new branch. But first, move the bits
8888 + * to make space for the new branch, if needed. Finally, adjust
8890 + * We don't release nd here; it's kept until umount/remount.
8892 + if (idx < cur_branches) {
8893 + /* if idx==cur_branches, we append: easy */
8894 + memmove(&new_data[idx+1], &new_data[idx],
8895 + (cur_branches - idx) * sizeof(struct unionfs_data));
8896 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8897 + (cur_branches - idx) * sizeof(struct path));
8899 + new_lower_paths[idx].dentry = path.dentry;
8900 + new_lower_paths[idx].mnt = path.mnt;
8902 + new_data[idx].sb = path.dentry->d_sb;
8903 + atomic_set(&new_data[idx].open_files, 0);
8904 + new_data[idx].branchperms = perms;
8905 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8914 + * Support branch management options on remount.
8916 + * See Documentation/filesystems/unionfs/ for details.
8918 + * @flags: numeric mount options
8919 + * @options: mount options string
8921 + * This function can rearrange a mounted union dynamically, adding and
8922 + * removing branches, including changing branch modes. Clearly this has to
8923 + * be done safely and atomically. Luckily, the VFS already calls this
8924 + * function with lock_super(sb) and lock_kernel() held, preventing
8925 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8926 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8927 + * to purge dentries/inodes from our superblock, and also called
8928 + * fsync_super(sb) to purge any dirty pages. So we're good.
8930 + * XXX: however, our remount code may also need to invalidate mapped pages
8931 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8932 + * branches. This has to wait for proper mmap and cache coherency support
8936 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8941 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
8943 + int cur_branches = 0; /* no. of current branches */
8944 + int new_branches = 0; /* no. of branches actually left in the end */
8945 + int add_branches; /* est. no. of branches to add */
8946 + int del_branches; /* est. no. of branches to del */
8947 + int max_branches; /* max possible no. of branches */
8948 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
8949 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
8950 + struct inode **new_lower_inodes = NULL;
8951 + int new_high_branch_id; /* new high branch ID */
8952 + int size; /* memory allocation size, temp var */
8953 + int old_ibstart, old_ibend;
8955 + unionfs_write_lock(sb);
8958 + * The VFS will take care of "ro" and "rw" flags, and we can safely
8959 + * ignore MS_SILENT, but anything else left over is an error. So we
8960 + * need to check if any other flags may have been passed (none are
8961 + * allowed/supported as of now).
8963 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8965 + "unionfs: remount flags 0x%x unsupported\n", *flags);
8971 + * If 'options' is NULL, it's probably because the user just changed
8972 + * the union to a "ro" or "rw" and the VFS took care of it. So
8973 + * nothing to do and we're done.
8975 + if (!options || options[0] == '\0')
8979 + * Find out how many branches we will have in the end, counting
8980 + * "add" and "del" commands. Copy the "options" string because
8981 + * strsep modifies the string and we need it later.
8983 + tmp_to_free = kstrdup(options, GFP_KERNEL);
8984 + optionstmp = tmp_to_free;
8985 + if (unlikely(!optionstmp)) {
8989 + cur_branches = sbmax(sb); /* current no. branches */
8990 + new_branches = sbmax(sb);
8993 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
8994 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
8997 + if (!optname || !*optname)
9000 + optarg = strchr(optname, '=');
9004 + if (!strcmp("add", optname))
9006 + else if (!strcmp("del", optname))
9009 + kfree(tmp_to_free);
9010 + /* after all changes, will we have at least one branch left? */
9011 + if ((new_branches + add_branches - del_branches) < 1) {
9013 + "unionfs: no branches left after remount\n");
9019 + * Since we haven't actually parsed all the add/del options, nor
9020 + * have we checked them for errors, we don't know for sure how many
9021 + * branches we will have after all changes have taken place. In
9022 + * fact, the total number of branches left could be less than what
9023 + * we have now. So we need to allocate space for a temporary
9024 + * placeholder that is at least as large as the maximum number of
9025 + * branches we *could* have, which is the current number plus all
9026 + * the additions. Once we're done with these temp placeholders, we
9027 + * may have to re-allocate the final size, copy over from the temp,
9028 + * and then free the temps (done near the end of this function).
9030 + max_branches = cur_branches + add_branches;
9031 + /* allocate space for new pointers to lower dentry */
9032 + tmp_data = kcalloc(max_branches,
9033 + sizeof(struct unionfs_data), GFP_KERNEL);
9034 + if (unlikely(!tmp_data)) {
9038 + /* allocate space for new pointers to lower paths */
9039 + tmp_lower_paths = kcalloc(max_branches,
9040 + sizeof(struct path), GFP_KERNEL);
9041 + if (unlikely(!tmp_lower_paths)) {
9045 + /* copy current info into new placeholders, incrementing refcnts */
9046 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9047 + cur_branches * sizeof(struct unionfs_data));
9048 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9049 + cur_branches * sizeof(struct path));
9050 + for (i = 0; i < cur_branches; i++)
9051 + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9053 + /*******************************************************************
9054 + * For each branch command, do kern_path on the requested branch,
9055 + * and apply the change to a temp branch list. To handle errors, we
9056 + * already dup'ed the old arrays (above), and increased the refcnts
9057 + * on various f/s objects. So now we can do all the kern_path'ss
9058 + * and branch-management commands on the new arrays. If it fail mid
9059 + * way, we free the tmp arrays and *put all objects. If we succeed,
9060 + * then we free old arrays and *put its objects, and then replace
9061 + * the arrays with the new tmp list (we may have to re-allocate the
9062 + * memory because the temp lists could have been larger than what we
9063 + * actually needed).
9064 + *******************************************************************/
9066 + while ((optname = strsep(&options, ",")) != NULL) {
9069 + if (!optname || !*optname)
9072 + * At this stage optname holds a comma-delimited option, but
9073 + * without the commas. Next, we need to break the string on
9074 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9075 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9076 + * KEY is "/foo", and VAL is "rw".
9078 + optarg = strchr(optname, '=');
9081 + /* incgen remount option (instead of old ioctl) */
9082 + if (!strcmp("incgen", optname)) {
9084 + goto out_no_change;
9088 + * All of our options take an argument now. (Insert ones
9089 + * that don't above this check.) So at this stage optname
9090 + * contains the CMD part and optarg contains the ARG part.
9092 + if (!optarg || !*optarg) {
9093 + printk(KERN_ERR "unionfs: all remount options require "
9094 + "an argument (%s)\n", optname);
9099 + if (!strcmp("add", optname)) {
9100 + err = do_remount_add_option(optarg, new_branches,
9103 + &new_high_branch_id);
9107 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9108 + printk(KERN_ERR "unionfs: command exceeds "
9109 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9115 + if (!strcmp("del", optname)) {
9116 + err = do_remount_del_option(optarg, new_branches,
9124 + if (!strcmp("mode", optname)) {
9125 + err = do_remount_mode_option(optarg, new_branches,
9134 + * When you use "mount -o remount,ro", mount(8) will
9135 + * reportedly pass the original dirs= string from
9136 + * /proc/mounts. So for now, we have to ignore dirs= and
9137 + * not consider it an error, unless we want to allow users
9138 + * to pass dirs= in remount. Note that to allow the VFS to
9139 + * actually process the ro/rw remount options, we have to
9140 + * return 0 from this function.
9142 + if (!strcmp("dirs", optname)) {
9143 + printk(KERN_WARNING
9144 + "unionfs: remount ignoring option \"%s\"\n",
9151 + "unionfs: unrecognized option \"%s\"\n", optname);
9157 + /******************************************************************
9158 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9159 + * see if we need to allocate a small-sized new vector, copy the
9160 + * vectors to their correct place, release the refcnt of the older
9161 + * ones, and return. Also handle invalidating any pages that will
9162 + * have to be re-read.
9163 + *******************************************************************/
9165 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9166 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9167 + "(use \"remount,ro\" to create a read-only union)\n");
9172 + /* (re)allocate space for new pointers to lower dentry */
9173 + size = new_branches * sizeof(struct unionfs_data);
9174 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9175 + if (unlikely(!new_data)) {
9180 + /* allocate space for new pointers to lower paths */
9181 + size = new_branches * sizeof(struct path);
9182 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9183 + if (unlikely(!new_lower_paths)) {
9188 + /* allocate space for new pointers to lower inodes */
9189 + new_lower_inodes = kcalloc(new_branches,
9190 + sizeof(struct inode *), GFP_KERNEL);
9191 + if (unlikely(!new_lower_inodes)) {
9197 + * OK, just before we actually put the new set of branches in place,
9198 + * we need to ensure that our own f/s has no dirty objects left.
9199 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9200 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9201 + * pages. So all that's left is for us to invalidate any leftover
9202 + * (non-dirty) pages to ensure that they will be re-read from the
9203 + * new lower branches (and to support mmap).
9207 + * Once we finish the remounting successfully, our superblock
9208 + * generation number will have increased. This will be detected by
9209 + * our dentry-revalidation code upon subsequent f/s operations
9210 + * through unionfs. The revalidation code will rebuild the union of
9211 + * lower inodes for a given unionfs inode and invalidate any pages
9212 + * of such "stale" inodes (by calling our purge_inode_data
9213 + * function). This revalidation will happen lazily and
9214 + * incrementally, as users perform operations on cached inodes. We
9215 + * would like to encourage this revalidation to happen sooner if
9216 + * possible, so we like to try to invalidate as many other pages in
9217 + * our superblock as we can. We used to call drop_pagecache_sb() or
9218 + * a variant thereof, but either method was racy (drop_caches alone
9219 + * is known to be racy). So now we let the revalidation happen on a
9220 + * per file basis in ->d_revalidate.
9223 + /* grab new lower super references; release old ones */
9224 + for (i = 0; i < new_branches; i++)
9225 + atomic_inc(&new_data[i].sb->s_active);
9226 + for (i = 0; i < sbmax(sb); i++)
9227 + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9229 + /* copy new vectors into their correct place */
9230 + tmp_data = UNIONFS_SB(sb)->data;
9231 + UNIONFS_SB(sb)->data = new_data;
9232 + new_data = NULL; /* so don't free good pointers below */
9233 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9234 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9235 + new_lower_paths = NULL; /* so don't free good pointers below */
9237 + /* update our unionfs_sb_info and root dentry index of last branch */
9238 + i = sbmax(sb); /* save no. of branches to release at end */
9239 + sbend(sb) = new_branches - 1;
9240 + dbend(sb->s_root) = new_branches - 1;
9241 + old_ibstart = ibstart(sb->s_root->d_inode);
9242 + old_ibend = ibend(sb->s_root->d_inode);
9243 + ibend(sb->s_root->d_inode) = new_branches - 1;
9244 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9245 + new_branches = i; /* no. of branches to release below */
9248 + * Update lower inodes: 3 steps
9249 + * 1. grab ref on all new lower inodes
9251 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9252 + struct dentry *lower_dentry =
9253 + unionfs_lower_dentry_idx(sb->s_root, i);
9254 + igrab(lower_dentry->d_inode);
9255 + new_lower_inodes[i] = lower_dentry->d_inode;
9257 + /* 2. release reference on all older lower inodes */
9258 + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9259 + /* 3. update root dentry's inode to new lower_inodes array */
9260 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9261 + new_lower_inodes = NULL;
9263 + /* maxbytes may have changed */
9264 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9265 + /* update high branch ID */
9266 + sbhbid(sb) = new_high_branch_id;
9268 + /* update our sb->generation for revalidating objects */
9269 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9270 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9271 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9272 + if (!(*flags & MS_SILENT))
9273 + pr_info("unionfs: %s: new generation number %d\n",
9274 + UNIONFS_SB(sb)->dev_name, i);
9275 + /* finally, update the root dentry's times */
9276 + unionfs_copy_attr_times(sb->s_root->d_inode);
9277 + err = 0; /* reset to success */
9280 + * The code above falls through to the next label, and releases the
9281 + * refcnts of the older ones (stored in tmp_*): if we fell through
9282 + * here, it means success. However, if we jump directly to this
9283 + * label from any error above, then an error occurred after we
9284 + * grabbed various refcnts, and so we have to release the
9285 + * temporarily constructed structures.
9288 + /* no need to cleanup/release anything in tmp_data */
9289 + if (tmp_lower_paths)
9290 + for (i = 0; i < new_branches; i++)
9291 + path_put(&tmp_lower_paths[i]);
9293 + kfree(tmp_lower_paths);
9295 + kfree(new_lower_paths);
9297 + kfree(new_lower_inodes);
9299 + unionfs_check_dentry(sb->s_root);
9300 + unionfs_write_unlock(sb);
9305 + * Called by iput() when the inode reference count reached zero
9306 + * and the inode is not hashed anywhere. Used to clear anything
9307 + * that needs to be, before the inode is completely destroyed and put
9308 + * on the inode free list.
9310 + * No need to lock sb info's rwsem.
9312 +static void unionfs_evict_inode(struct inode *inode)
9314 + int bindex, bstart, bend;
9315 + struct inode *lower_inode;
9316 + struct list_head *pos, *n;
9317 + struct unionfs_dir_state *rdstate;
9319 + truncate_inode_pages(&inode->i_data, 0);
9320 + clear_inode(inode);
9322 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9323 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9324 + list_del(&rdstate->cache);
9325 + free_rdstate(rdstate);
9329 + * Decrement a reference to a lower_inode, which was incremented
9330 + * by our read_inode when it was created initially.
9332 + bstart = ibstart(inode);
9333 + bend = ibend(inode);
9334 + if (bstart >= 0) {
9335 + for (bindex = bstart; bindex <= bend; bindex++) {
9336 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9339 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9340 + /* see Documentation/filesystems/unionfs/issues.txt */
9342 + iput(lower_inode);
9347 + kfree(UNIONFS_I(inode)->lower_inodes);
9348 + UNIONFS_I(inode)->lower_inodes = NULL;
9351 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9353 + struct unionfs_inode_info *i;
9355 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9359 + /* memset everything up to the inode to 0 */
9360 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9362 + i->vfs_inode.i_version = 1;
9363 + return &i->vfs_inode;
9366 +static void unionfs_destroy_inode(struct inode *inode)
9368 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9371 +/* unionfs inode cache constructor */
9372 +static void init_once(void *obj)
9374 + struct unionfs_inode_info *i = obj;
9376 + inode_init_once(&i->vfs_inode);
9379 +int unionfs_init_inode_cache(void)
9383 + unionfs_inode_cachep =
9384 + kmem_cache_create("unionfs_inode_cache",
9385 + sizeof(struct unionfs_inode_info), 0,
9386 + SLAB_RECLAIM_ACCOUNT, init_once);
9387 + if (unlikely(!unionfs_inode_cachep))
9392 +/* unionfs inode cache destructor */
9393 +void unionfs_destroy_inode_cache(void)
9395 + if (unionfs_inode_cachep)
9396 + kmem_cache_destroy(unionfs_inode_cachep);
9400 + * Called when we have a dirty inode, right here we only throw out
9401 + * parts of our readdir list that are too old.
9403 + * No need to grab sb info's rwsem.
9405 +static int unionfs_write_inode(struct inode *inode,
9406 + struct writeback_control *wbc)
9408 + struct list_head *pos, *n;
9409 + struct unionfs_dir_state *rdstate;
9411 + spin_lock(&UNIONFS_I(inode)->rdlock);
9412 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9413 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9414 + /* We keep this list in LRU order. */
9415 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9417 + UNIONFS_I(inode)->rdcount--;
9418 + list_del(&rdstate->cache);
9419 + free_rdstate(rdstate);
9421 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9427 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9428 + * code can actually succeed and won't leave tasks that need handling.
9430 +static void unionfs_umount_begin(struct super_block *sb)
9432 + struct super_block *lower_sb;
9433 + int bindex, bstart, bend;
9435 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9437 + bstart = sbstart(sb);
9439 + for (bindex = bstart; bindex <= bend; bindex++) {
9440 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9442 + if (lower_sb && lower_sb->s_op &&
9443 + lower_sb->s_op->umount_begin)
9444 + lower_sb->s_op->umount_begin(lower_sb);
9447 + unionfs_read_unlock(sb);
9450 +static int unionfs_show_options(struct seq_file *m, struct dentry *root)
9452 + struct super_block *sb = root->d_sb;
9456 + int bindex, bstart, bend;
9459 + /* to prevent a silly lockdep warning with namespace_sem */
9461 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9462 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9464 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9465 + if (unlikely(!tmp_page)) {
9470 + bstart = sbstart(sb);
9473 + seq_printf(m, ",dirs=");
9474 + for (bindex = bstart; bindex <= bend; bindex++) {
9476 + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9477 + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9478 + path = d_path(&p, tmp_page, PAGE_SIZE);
9479 + if (IS_ERR(path)) {
9480 + ret = PTR_ERR(path);
9484 + perms = branchperms(sb, bindex);
9486 + seq_printf(m, "%s=%s", path,
9487 + perms & MAY_WRITE ? "rw" : "ro");
9488 + if (bindex != bend)
9489 + seq_printf(m, ":");
9493 + free_page((unsigned long) tmp_page);
9495 + unionfs_unlock_dentry(sb->s_root);
9496 + unionfs_read_unlock(sb);
9502 +struct super_operations unionfs_sops = {
9503 + .put_super = unionfs_put_super,
9504 + .statfs = unionfs_statfs,
9505 + .remount_fs = unionfs_remount_fs,
9506 + .evict_inode = unionfs_evict_inode,
9507 + .umount_begin = unionfs_umount_begin,
9508 + .show_options = unionfs_show_options,
9509 + .write_inode = unionfs_write_inode,
9510 + .alloc_inode = unionfs_alloc_inode,
9511 + .destroy_inode = unionfs_destroy_inode,
9513 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9514 new file mode 100644
9515 index 0000000..8e7fcfb
9517 +++ b/fs/unionfs/union.h
9520 + * Copyright (c) 2003-2011 Erez Zadok
9521 + * Copyright (c) 2003-2006 Charles P. Wright
9522 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9523 + * Copyright (c) 2005 Arun M. Krishnakumar
9524 + * Copyright (c) 2004-2006 David P. Quigley
9525 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9526 + * Copyright (c) 2003 Puja Gupta
9527 + * Copyright (c) 2003 Harikesavan Krishnan
9528 + * Copyright (c) 2003-2011 Stony Brook University
9529 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
9531 + * This program is free software; you can redistribute it and/or modify
9532 + * it under the terms of the GNU General Public License version 2 as
9533 + * published by the Free Software Foundation.
9539 +#include <linux/dcache.h>
9540 +#include <linux/file.h>
9541 +#include <linux/list.h>
9542 +#include <linux/fs.h>
9543 +#include <linux/mm.h>
9544 +#include <linux/module.h>
9545 +#include <linux/mount.h>
9546 +#include <linux/namei.h>
9547 +#include <linux/page-flags.h>
9548 +#include <linux/pagemap.h>
9549 +#include <linux/poll.h>
9550 +#include <linux/security.h>
9551 +#include <linux/seq_file.h>
9552 +#include <linux/slab.h>
9553 +#include <linux/spinlock.h>
9554 +#include <linux/statfs.h>
9555 +#include <linux/string.h>
9556 +#include <linux/vmalloc.h>
9557 +#include <linux/writeback.h>
9558 +#include <linux/buffer_head.h>
9559 +#include <linux/xattr.h>
9560 +#include <linux/fs_stack.h>
9561 +#include <linux/magic.h>
9562 +#include <linux/log2.h>
9563 +#include <linux/poison.h>
9564 +#include <linux/mman.h>
9565 +#include <linux/backing-dev.h>
9566 +#include <linux/splice.h>
9567 +#include <linux/sched.h>
9571 +#include <linux/union_fs.h>
9573 +/* the file system name */
9574 +#define UNIONFS_NAME "unionfs"
9576 +/* unionfs root inode number */
9577 +#define UNIONFS_ROOT_INO 1
9579 +/* number of times we try to get a unique temporary file name */
9580 +#define GET_TMPNAM_MAX_RETRY 5
9582 +/* maximum number of branches we support, to avoid memory blowup */
9583 +#define UNIONFS_MAX_BRANCHES 128
9585 +/* minimum time (seconds) required for time-based cache-coherency */
9586 +#define UNIONFS_MIN_CC_TIME 3
9588 +/* Operations vectors defined in specific files. */
9589 +extern struct file_operations unionfs_main_fops;
9590 +extern struct file_operations unionfs_dir_fops;
9591 +extern struct inode_operations unionfs_main_iops;
9592 +extern struct inode_operations unionfs_dir_iops;
9593 +extern struct inode_operations unionfs_symlink_iops;
9594 +extern struct super_operations unionfs_sops;
9595 +extern struct dentry_operations unionfs_dops;
9596 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9597 +extern struct vm_operations_struct unionfs_vm_ops;
9599 +/* How long should an entry be allowed to persist */
9600 +#define RDCACHE_JIFFIES (5*HZ)
9602 +/* compatibility with Real-Time patches */
9603 +#ifdef CONFIG_PREEMPT_RT
9604 +# define unionfs_rw_semaphore compat_rw_semaphore
9605 +#else /* not CONFIG_PREEMPT_RT */
9606 +# define unionfs_rw_semaphore rw_semaphore
9607 +#endif /* not CONFIG_PREEMPT_RT */
9609 +/* file private data. */
9610 +struct unionfs_file_info {
9613 + atomic_t generation;
9615 + struct unionfs_dir_state *rdstate;
9616 + struct file **lower_files;
9617 + int *saved_branch_ids; /* IDs of branches when file was opened */
9618 + const struct vm_operations_struct *lower_vm_ops;
9619 + bool wrote_to_file; /* for delayed copyup */
9622 +/* unionfs inode data in memory */
9623 +struct unionfs_inode_info {
9626 + atomic_t generation;
9627 + /* Stuff for readdir over NFS. */
9628 + spinlock_t rdlock;
9629 + struct list_head readdircache;
9634 + /* The lower inodes */
9635 + struct inode **lower_inodes;
9637 + struct inode vfs_inode;
9640 +/* unionfs dentry data in memory */
9641 +struct unionfs_dentry_info {
9643 + * The semaphore is used to lock the dentry as soon as we get into a
9644 + * unionfs function from the VFS. Our lock ordering is that children
9645 + * go before their parents.
9647 + struct mutex lock;
9652 + atomic_t generation;
9653 + struct path *lower_paths;
9656 +/* These are the pointers to our various objects. */
9657 +struct unionfs_data {
9658 + struct super_block *sb; /* lower super_block */
9659 + atomic_t open_files; /* number of open files on branch */
9661 + int branch_id; /* unique branch ID at re/mount time */
9664 +/* unionfs super-block data in memory */
9665 +struct unionfs_sb_info {
9668 + atomic_t generation;
9671 + * This rwsem is used to make sure that a branch management
9673 + * 1) will not begin before all currently in-flight operations
9675 + * 2) any new operations do not execute until the currently
9676 + * running branch management operation completes.
9678 + * The write_lock_owner records the PID of the task which grabbed
9679 + * the rw_sem for writing. If the same task also tries to grab the
9680 + * read lock, we allow it. This prevents a self-deadlock when
9681 + * branch-management is used on a pivot_root'ed union, because we
9682 + * have to ->lookup paths which belong to the same union.
9684 + struct unionfs_rw_semaphore rwsem;
9685 + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9686 + int high_branch_id; /* last unique branch ID given */
9687 + char *dev_name; /* to identify different unions in pr_debug */
9688 + struct unionfs_data *data;
9692 + * structure for making the linked list of entries by readdir on left branch
9693 + * to compare with entries on right branch
9695 +struct filldir_node {
9696 + struct list_head file_list; /* list for directory entries */
9697 + char *name; /* name entry */
9698 + int hash; /* name hash */
9699 + int namelen; /* name len since name is not 0 terminated */
9702 + * we can check for duplicate whiteouts and files in the same branch
9703 + * in order to return -EIO.
9707 + /* is this a whiteout entry? */
9710 + /* Inline name, so we don't need to separately kmalloc small ones */
9711 + char iname[DNAME_INLINE_LEN];
9714 +/* Directory hash table. */
9715 +struct unionfs_dir_state {
9716 + unsigned int cookie; /* the cookie, based off of rdversion */
9717 + unsigned int offset; /* The entry we have returned. */
9719 + loff_t dirpos; /* offset within the lower level directory */
9720 + int size; /* How big is the hash table? */
9721 + int hashentries; /* How many entries have been inserted? */
9722 + unsigned long access;
9724 + /* This cache list is used when the inode keeps us around. */
9725 + struct list_head cache;
9726 + struct list_head list[0];
9729 +/* externs needed for fanout.h or sioq.h */
9730 +extern int unionfs_get_nlinks(const struct inode *inode);
9731 +extern void unionfs_copy_attr_times(struct inode *upper);
9732 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9734 +/* include miscellaneous macros */
9735 +#include "fanout.h"
9738 +/* externs for cache creation/deletion routines */
9739 +extern void unionfs_destroy_filldir_cache(void);
9740 +extern int unionfs_init_filldir_cache(void);
9741 +extern int unionfs_init_inode_cache(void);
9742 +extern void unionfs_destroy_inode_cache(void);
9743 +extern int unionfs_init_dentry_cache(void);
9744 +extern void unionfs_destroy_dentry_cache(void);
9746 +/* Initialize and free readdir-specific state. */
9747 +extern int init_rdstate(struct file *file);
9748 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9750 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9752 +extern void free_rdstate(struct unionfs_dir_state *state);
9753 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9754 + const char *name, int namelen, int bindex,
9756 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9757 + const char *name, int namelen,
9760 +extern struct dentry **alloc_new_dentries(int objs);
9761 +extern struct unionfs_data *alloc_new_data(int objs);
9763 +/* We can only use 32-bits of offset for rdstate --- blech! */
9764 +#define DIREOF (0xfffff)
9765 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9766 +#define MAXRDCOOKIE (0xfff)
9767 +/* Turn an rdstate into an offset. */
9768 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9772 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9773 + | (buf->offset & DIREOF);
9777 +/* Macros for locking a super_block. */
9778 +enum unionfs_super_lock_class {
9779 + UNIONFS_SMUTEX_NORMAL,
9780 + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9781 + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9783 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9785 + if (UNIONFS_SB(sb)->write_lock_owner &&
9786 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9788 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9790 +static inline void unionfs_read_unlock(struct super_block *sb)
9792 + if (UNIONFS_SB(sb)->write_lock_owner &&
9793 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9795 + up_read(&UNIONFS_SB(sb)->rwsem);
9797 +static inline void unionfs_write_lock(struct super_block *sb)
9799 + down_write(&UNIONFS_SB(sb)->rwsem);
9800 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9802 +static inline void unionfs_write_unlock(struct super_block *sb)
9804 + up_write(&UNIONFS_SB(sb)->rwsem);
9805 + UNIONFS_SB(sb)->write_lock_owner = 0;
9808 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9809 + struct dentry *d2)
9813 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9814 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9816 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9817 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9821 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9822 + struct dentry *d2)
9825 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9826 + unionfs_unlock_dentry(d1);
9827 + unionfs_unlock_dentry(d2);
9829 + unionfs_unlock_dentry(d2);
9830 + unionfs_unlock_dentry(d1);
9834 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9835 + struct dentry *p2)
9838 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9842 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9843 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9845 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9846 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9850 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9851 + struct dentry *p2)
9854 + unionfs_unlock_dentry(p1);
9857 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9858 + unionfs_unlock_dentry(p1);
9859 + unionfs_unlock_dentry(p2);
9861 + unionfs_unlock_dentry(p2);
9862 + unionfs_unlock_dentry(p1);
9866 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9867 +extern int realloc_dentry_private_data(struct dentry *dentry);
9868 +extern void free_dentry_private_data(struct dentry *dentry);
9869 +extern void update_bstart(struct dentry *dentry);
9870 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9871 +extern void release_lower_nd(struct nameidata *nd, int err);
9877 +/* replicates the directory structure up to given dentry in given branch */
9878 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9879 + const char *name, int bindex);
9881 +/* partial lookup */
9882 +extern int unionfs_partial_lookup(struct dentry *dentry,
9883 + struct dentry *parent);
9884 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9885 + struct dentry *parent,
9888 +/* copies a file from dbstart to newbindex branch */
9889 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9890 + int newbindex, loff_t size);
9891 +extern int copyup_named_file(struct inode *dir, struct file *file,
9892 + char *name, int bstart, int new_bindex,
9894 +/* copies a dentry from dbstart to newbindex branch */
9895 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9896 + int bstart, int new_bindex, const char *name,
9897 + int namelen, struct file **copyup_file, loff_t len);
9898 +/* helper functions for post-copyup actions */
9899 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9900 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9902 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9903 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9904 + struct unionfs_dir_state **namelist);
9905 +/* whiteout and opaque directory helpers */
9906 +extern char *alloc_whname(const char *name, int len);
9907 +extern bool is_whiteout_name(char **namep, int *namelenp);
9908 +extern bool is_validname(const char *name);
9909 +extern struct dentry *lookup_whiteout(const char *name,
9910 + struct dentry *lower_parent);
9911 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9912 +extern int unlink_whiteout(struct dentry *wh_dentry);
9913 +extern int check_unlink_whiteout(struct dentry *dentry,
9914 + struct dentry *lower_dentry, int bindex);
9915 +extern int create_whiteout(struct dentry *dentry, int start);
9916 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9917 + struct unionfs_dir_state *namelist);
9918 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9919 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9920 +extern void unionfs_set_max_namelen(long *namelen);
9922 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9923 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9925 +/* Locking functions. */
9926 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9927 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9929 +/* Common file operations. */
9930 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9932 +extern int unionfs_open(struct inode *inode, struct file *file);
9933 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9934 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9935 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9936 + unsigned long arg);
9937 +extern int unionfs_fsync(struct file *file, loff_t start, loff_t end,
9939 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9941 +/* Inode operations */
9942 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9943 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9944 + struct inode *new_dir, struct dentry *new_dentry);
9945 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
9946 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
9948 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
9949 + struct dentry *parent, bool willwrite);
9950 +extern bool is_negative_lower(const struct dentry *dentry);
9951 +extern bool is_newer_lower(const struct dentry *dentry);
9952 +extern void purge_sb_data(struct super_block *sb);
9954 +/* The values for unionfs_interpose's flag. */
9955 +#define INTERPOSE_DEFAULT 0
9956 +#define INTERPOSE_LOOKUP 1
9957 +#define INTERPOSE_REVAL 2
9958 +#define INTERPOSE_REVAL_NEG 3
9959 +#define INTERPOSE_PARTIAL 4
9961 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9962 + struct super_block *sb, int flag);
9964 +#ifdef CONFIG_UNION_FS_XATTR
9965 +/* Extended attribute functions. */
9966 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
9967 +static inline void unionfs_xattr_kfree(const void *p)
9971 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
9972 + void *value, size_t size);
9973 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
9974 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
9976 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
9977 + const void *value, size_t size, int flags);
9978 +#endif /* CONFIG_UNION_FS_XATTR */
9980 +/* The root directory is unhashed, but isn't deleted. */
9981 +static inline int d_deleted(struct dentry *d)
9983 + return d_unhashed(d) && (d != d->d_sb->s_root);
9986 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
9987 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
9989 +/* unionfs_open, check if we need to copyup the file */
9990 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
9991 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
9993 +static inline int branchperms(const struct super_block *sb, int index)
9995 + BUG_ON(index < 0);
9996 + return UNIONFS_SB(sb)->data[index].branchperms;
9999 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10001 + BUG_ON(index < 0);
10002 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10006 +/* check if readonly lower inode, but possibly unlinked (no inode->i_sb) */
10007 +static inline int __is_rdonly(const struct inode *inode)
10009 + /* if unlinked, can't be readonly (?) */
10010 + if (!inode->i_sb)
10012 + return IS_RDONLY(inode);
10015 +/* Is this file on a read-only branch? */
10016 +static inline int is_robranch_super(const struct super_block *sb, int index)
10020 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10024 +/* Is this file on a read-only branch? */
10025 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10027 + struct super_block *lower_sb;
10029 + BUG_ON(index < 0);
10031 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10034 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10035 + BUG_ON(lower_sb == NULL);
10037 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10038 + * lower_dentry could be a negative.
10040 + if (lower_sb->s_flags & MS_RDONLY)
10046 +static inline int is_robranch(const struct dentry *dentry)
10050 + index = UNIONFS_D(dentry)->bstart;
10051 + BUG_ON(index < 0);
10053 + return is_robranch_idx(dentry, index);
10059 +extern int check_branch(const struct path *path);
10060 +extern int parse_branch_mode(const char *name, int *perms);
10062 +/* locking helpers */
10063 +static inline struct dentry *lock_parent(struct dentry *dentry)
10065 + struct dentry *dir = dget_parent(dentry);
10066 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
10069 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10071 + struct dentry *dir = dget_parent(dentry);
10073 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10077 +static inline void unlock_dir(struct dentry *dir)
10079 + mutex_unlock(&dir->d_inode->i_mutex);
10083 +/* lock base inode mutex before calling lookup_one_len */
10084 +static inline struct dentry *lookup_lck_len(const char *name,
10085 + struct dentry *base, int len)
10087 + struct dentry *d;
10088 + struct nameidata lower_nd;
10091 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
10092 + if (unlikely(err < 0)) {
10093 + d = ERR_PTR(err);
10096 + mutex_lock(&base->d_inode->i_mutex);
10097 + d = lookup_one_len_nd(name, base, len, &lower_nd);
10098 + release_lower_nd(&lower_nd, err);
10099 + mutex_unlock(&base->d_inode->i_mutex);
10104 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10107 + struct vfsmount *mnt;
10109 + BUG_ON(!dentry || bindex < 0);
10111 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10112 +#ifdef CONFIG_UNION_FS_DEBUG
10114 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10116 +#endif /* CONFIG_UNION_FS_DEBUG */
10121 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10123 + struct vfsmount *mnt;
10125 + if (!dentry && bindex < 0)
10127 + BUG_ON(!dentry || bindex < 0);
10129 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10130 +#ifdef CONFIG_UNION_FS_DEBUG
10132 + * Directories can have NULL lower objects in between start/end, but
10133 + * NOT if at the start/end range. We cannot verify that this dentry
10134 + * is a type=DIR, because it may already be a negative dentry. But
10135 + * if dbstart is greater than dbend, we know that this couldn't have
10136 + * been a regular file: it had to have been a directory.
10138 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10139 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10140 +#endif /* CONFIG_UNION_FS_DEBUG */
10144 +#ifdef CONFIG_UNION_FS_DEBUG
10146 +/* useful for tracking code reachability */
10147 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10149 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10150 + __FILE__, __func__, __LINE__)
10151 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10152 + __FILE__, __func__, __LINE__)
10153 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10154 + __FILE__, __func__, __LINE__)
10155 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10156 + __FILE__, __func__, __LINE__)
10157 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10158 + __FILE__, __func__, __LINE__)
10159 +#define show_inode_times(i) __show_inode_times((i), \
10160 + __FILE__, __func__, __LINE__)
10161 +#define show_dinode_times(d) __show_dinode_times((d), \
10162 + __FILE__, __func__, __LINE__)
10163 +#define show_inode_counts(i) __show_inode_counts((i), \
10164 + __FILE__, __func__, __LINE__)
10166 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10167 + const char *fxn, int line);
10168 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10169 + const char *fname, const char *fxn,
10171 +extern void __unionfs_check_file(const struct file *file,
10172 + const char *fname, const char *fxn, int line);
10173 +extern void __unionfs_check_nd(const struct nameidata *nd,
10174 + const char *fname, const char *fxn, int line);
10175 +extern void __show_branch_counts(const struct super_block *sb,
10176 + const char *file, const char *fxn, int line);
10177 +extern void __show_inode_times(const struct inode *inode,
10178 + const char *file, const char *fxn, int line);
10179 +extern void __show_dinode_times(const struct dentry *dentry,
10180 + const char *file, const char *fxn, int line);
10181 +extern void __show_inode_counts(const struct inode *inode,
10182 + const char *file, const char *fxn, int line);
10184 +#else /* not CONFIG_UNION_FS_DEBUG */
10186 +/* we leave useful hooks for these check functions throughout the code */
10187 +#define unionfs_check_inode(i) do { } while (0)
10188 +#define unionfs_check_dentry(d) do { } while (0)
10189 +#define unionfs_check_file(f) do { } while (0)
10190 +#define unionfs_check_nd(n) do { } while (0)
10191 +#define show_branch_counts(sb) do { } while (0)
10192 +#define show_inode_times(i) do { } while (0)
10193 +#define show_dinode_times(d) do { } while (0)
10194 +#define show_inode_counts(i) do { } while (0)
10195 +#define UDBG do { } while (0)
10197 +#endif /* not CONFIG_UNION_FS_DEBUG */
10199 +#endif /* not _UNION_H_ */
10200 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10201 new file mode 100644
10202 index 0000000..25943a5
10204 +++ b/fs/unionfs/unlink.c
10207 + * Copyright (c) 2003-2011 Erez Zadok
10208 + * Copyright (c) 2003-2006 Charles P. Wright
10209 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10210 + * Copyright (c) 2005-2006 Junjiro Okajima
10211 + * Copyright (c) 2005 Arun M. Krishnakumar
10212 + * Copyright (c) 2004-2006 David P. Quigley
10213 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10214 + * Copyright (c) 2003 Puja Gupta
10215 + * Copyright (c) 2003 Harikesavan Krishnan
10216 + * Copyright (c) 2003-2011 Stony Brook University
10217 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
10219 + * This program is free software; you can redistribute it and/or modify
10220 + * it under the terms of the GNU General Public License version 2 as
10221 + * published by the Free Software Foundation.
10224 +#include "union.h"
10227 + * Helper function for Unionfs's unlink operation.
10229 + * The main goal of this function is to optimize the unlinking of non-dir
10230 + * objects in unionfs by deleting all possible lower inode objects from the
10231 + * underlying branches having same dentry name as the non-dir dentry on
10232 + * which this unlink operation is called. This way we delete as many lower
10233 + * inodes as possible, and save space. Whiteouts need to be created in
10234 + * branch0 only if unlinking fails on any of the lower branch other than
10235 + * branch0, or if a lower branch is marked read-only.
10237 + * Also, while unlinking a file, if we encounter any dir type entry in any
10238 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10239 + * The following special cases are also handled:
10241 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10242 + * appropriate error.
10244 + * (2) If we get an error during unlink in any of other lower branch other
10245 + * than branch0, then we create a whiteout in branch0.
10247 + * (3) If a whiteout already exists in any intermediate branch, we delete
10248 + * all possible inodes only up to that branch (this is an "opaqueness"
10249 + * as as per Documentation/filesystems/unionfs/concepts.txt).
10252 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10253 + struct dentry *parent)
10255 + struct dentry *lower_dentry;
10256 + struct dentry *lower_dir_dentry;
10260 + err = unionfs_partial_lookup(dentry, parent);
10264 + /* trying to unlink all possible valid instances */
10265 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10266 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10267 + if (!lower_dentry || !lower_dentry->d_inode)
10270 + lower_dir_dentry = lock_parent(lower_dentry);
10272 + /* avoid destroying the lower inode if the object is in use */
10273 + dget(lower_dentry);
10274 + err = is_robranch_super(dentry->d_sb, bindex);
10276 + /* see Documentation/filesystems/unionfs/issues.txt */
10278 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10279 + err = vfs_unlink(lower_dir_dentry->d_inode,
10282 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10287 + /* if lower object deletion succeeds, update inode's times */
10289 + unionfs_copy_attr_times(dentry->d_inode);
10290 + dput(lower_dentry);
10291 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10292 + unlock_dir(lower_dir_dentry);
10299 + * Create the whiteout in branch 0 (highest priority) only if (a)
10300 + * there was an error in any intermediate branch other than branch 0
10301 + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10302 + * mounted read-only.
10305 + if ((bindex == 0) ||
10306 + ((bindex == dbstart(dentry)) &&
10307 + (!IS_COPYUP_ERR(err))))
10310 + if (!IS_COPYUP_ERR(err))
10311 + pr_debug("unionfs: lower object deletion "
10312 + "failed in branch:%d\n", bindex);
10313 + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10319 + inode_dec_link_count(dentry->d_inode);
10321 + /* We don't want to leave negative leftover dentries for revalidate. */
10322 + if (!err && (dbopaque(dentry) != -1))
10323 + update_bstart(dentry);
10328 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10331 + struct inode *inode = dentry->d_inode;
10332 + struct dentry *parent;
10335 + BUG_ON(S_ISDIR(inode->i_mode));
10336 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10337 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10338 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10340 + valid = __unionfs_d_revalidate(dentry, parent, false);
10341 + if (unlikely(!valid)) {
10345 + unionfs_check_dentry(dentry);
10347 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10348 + /* call d_drop so the system "forgets" about us */
10350 + unionfs_postcopyup_release(dentry);
10351 + unionfs_postcopyup_setmnt(parent);
10352 + if (inode->i_nlink == 0) /* drop lower inodes */
10353 + iput_lowers_all(inode, false);
10356 + * if unlink/whiteout succeeded, parent dir mtime has
10359 + unionfs_copy_attr_times(dir);
10364 + unionfs_check_dentry(dentry);
10365 + unionfs_check_inode(dir);
10367 + unionfs_unlock_dentry(dentry);
10368 + unionfs_unlock_parent(dentry, parent);
10369 + unionfs_read_unlock(dentry->d_sb);
10373 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10374 + struct unionfs_dir_state *namelist)
10377 + struct dentry *lower_dentry;
10378 + struct dentry *lower_dir_dentry = NULL;
10380 + /* Here we need to remove whiteout entries. */
10381 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10385 + lower_dentry = unionfs_lower_dentry(dentry);
10387 + lower_dir_dentry = lock_parent(lower_dentry);
10389 + /* avoid destroying the lower inode if the file is in use */
10390 + dget(lower_dentry);
10391 + err = is_robranch(dentry);
10393 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10394 + dput(lower_dentry);
10396 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10397 + /* propagate number of hard-links */
10398 + set_nlink(dentry->d_inode, unionfs_get_nlinks(dentry->d_inode));
10401 + if (lower_dir_dentry)
10402 + unlock_dir(lower_dir_dentry);
10406 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10409 + struct unionfs_dir_state *namelist = NULL;
10410 + struct dentry *parent;
10411 + int dstart, dend;
10414 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10415 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10416 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10418 + valid = __unionfs_d_revalidate(dentry, parent, false);
10419 + if (unlikely(!valid)) {
10423 + unionfs_check_dentry(dentry);
10425 + /* check if this unionfs directory is empty or not */
10426 + err = check_empty(dentry, parent, &namelist);
10430 + err = unionfs_rmdir_first(dir, dentry, namelist);
10431 + dstart = dbstart(dentry);
10432 + dend = dbend(dentry);
10434 + * We create a whiteout for the directory if there was an error to
10435 + * rmdir the first directory entry in the union. Otherwise, we
10436 + * create a whiteout only if there is no chance that a lower
10437 + * priority branch might also have the same named directory. IOW,
10438 + * if there is not another same-named directory at a lower priority
10439 + * branch, then we don't need to create a whiteout for it.
10442 + if (dstart < dend)
10443 + err = create_whiteout(dentry, dstart);
10450 + /* exit if the error returned was NOT -EROFS */
10451 + if (!IS_COPYUP_ERR(err))
10454 + new_err = create_whiteout(dentry, dstart - 1);
10455 + if (new_err != -EEXIST)
10461 + * Drop references to lower dentry/inode so storage space for them
10462 + * can be reclaimed. Then, call d_drop so the system "forgets"
10466 + iput_lowers_all(dentry->d_inode, false);
10467 + dput(unionfs_lower_dentry_idx(dentry, dstart));
10468 + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10470 + /* update our lower vfsmnts, in case a copyup took place */
10471 + unionfs_postcopyup_setmnt(dentry);
10472 + unionfs_check_dentry(dentry);
10473 + unionfs_check_inode(dir);
10477 + free_rdstate(namelist);
10479 + unionfs_unlock_dentry(dentry);
10480 + unionfs_unlock_parent(dentry, parent);
10481 + unionfs_read_unlock(dentry->d_sb);
10484 diff --git a/fs/unionfs/whiteout.c b/fs/unionfs/whiteout.c
10485 new file mode 100644
10486 index 0000000..582cef2
10488 +++ b/fs/unionfs/whiteout.c
10491 + * Copyright (c) 2003-2011 Erez Zadok
10492 + * Copyright (c) 2003-2006 Charles P. Wright
10493 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10494 + * Copyright (c) 2005-2006 Junjiro Okajima
10495 + * Copyright (c) 2005 Arun M. Krishnakumar
10496 + * Copyright (c) 2004-2006 David P. Quigley
10497 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10498 + * Copyright (c) 2003 Puja Gupta
10499 + * Copyright (c) 2003 Harikesavan Krishnan
10500 + * Copyright (c) 2003-2011 Stony Brook University
10501 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
10503 + * This program is free software; you can redistribute it and/or modify
10504 + * it under the terms of the GNU General Public License version 2 as
10505 + * published by the Free Software Foundation.
10508 +#include "union.h"
10511 + * whiteout and opaque directory helpers
10514 +/* What do we use for whiteouts. */
10515 +#define UNIONFS_WHPFX ".wh."
10516 +#define UNIONFS_WHLEN 4
10518 + * If a directory contains this file, then it is opaque. We start with the
10519 + * .wh. flag so that it is blocked by lookup.
10521 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10522 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10524 +/* construct whiteout filename */
10525 +char *alloc_whname(const char *name, int len)
10529 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10530 + if (unlikely(!buf))
10531 + return ERR_PTR(-ENOMEM);
10533 + strcpy(buf, UNIONFS_WHPFX);
10534 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10540 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10541 + * code in one place.
10543 +void unionfs_set_max_namelen(long *namelen)
10545 + *namelen -= UNIONFS_WHLEN;
10548 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10549 +bool is_whiteout_name(char **namep, int *namelenp)
10551 + if (*namelenp > UNIONFS_WHLEN &&
10552 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10553 + *namep += UNIONFS_WHLEN;
10554 + *namelenp -= UNIONFS_WHLEN;
10560 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10561 +bool is_validname(const char *name)
10563 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10565 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10566 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10572 + * Look for a whiteout @name in @lower_parent directory. If error, return
10573 + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10575 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10576 + * free then re-malloc calls. Need to provide a different API for those
10579 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10581 + char *whname = NULL;
10582 + int err = 0, namelen;
10583 + struct dentry *wh_dentry = NULL;
10585 + namelen = strlen(name);
10586 + whname = alloc_whname(name, namelen);
10587 + if (unlikely(IS_ERR(whname))) {
10588 + err = PTR_ERR(whname);
10592 + /* check if whiteout exists in this branch: lookup .wh.foo */
10593 + wh_dentry = lookup_lck_len(whname, lower_parent, strlen(whname));
10594 + if (IS_ERR(wh_dentry)) {
10595 + err = PTR_ERR(wh_dentry);
10599 + /* check if negative dentry (ENOENT) */
10600 + if (!wh_dentry->d_inode)
10603 + /* whiteout found: check if valid type */
10604 + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10605 + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10606 + whname, wh_dentry->d_inode->i_mode);
10615 + wh_dentry = ERR_PTR(err);
10616 + return wh_dentry;
10619 +/* find and return first whiteout in parent directory, else ENOENT */
10620 +struct dentry *find_first_whiteout(struct dentry *dentry)
10622 + int bindex, bstart, bend;
10623 + struct dentry *parent, *lower_parent, *wh_dentry;
10625 + parent = dget_parent(dentry);
10627 + bstart = dbstart(parent);
10628 + bend = dbend(parent);
10629 + wh_dentry = ERR_PTR(-ENOENT);
10631 + for (bindex = bstart; bindex <= bend; bindex++) {
10632 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10633 + if (!lower_parent)
10635 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10636 + if (IS_ERR(wh_dentry))
10638 + if (wh_dentry->d_inode)
10641 + wh_dentry = ERR_PTR(-ENOENT);
10646 + return wh_dentry;
10650 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10651 + * release dentry reference.
10653 +int unlink_whiteout(struct dentry *wh_dentry)
10656 + struct dentry *lower_dir_dentry;
10658 + /* dget and lock parent dentry */
10659 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10661 + /* see Documentation/filesystems/unionfs/issues.txt */
10663 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10665 + unlock_dir(lower_dir_dentry);
10668 + * Whiteouts are special files and should be deleted no matter what
10669 + * (as if they never existed), in order to allow this create
10670 + * operation to succeed. This is especially important in sticky
10671 + * directories: a whiteout may have been created by one user, but
10672 + * the newly created file may be created by another user.
10673 + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10674 + * above failed, then we have to try to directly unlink the
10675 + * whiteout. Note: in the ODF version of unionfs, whiteout are
10676 + * handled much more cleanly.
10678 + if (err == -EPERM) {
10679 + struct inode *inode = lower_dir_dentry->d_inode;
10680 + err = inode->i_op->unlink(inode, wh_dentry);
10683 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10684 + "err = %d\n", wh_dentry->d_name.name, err);
10691 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10692 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10693 + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10694 + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10695 + * If it finds both a regular file and a whiteout, delete whiteout (this
10696 + * should never happen).
10698 + * Return 0 if no whiteout was found. Return 1 if one was found and
10699 + * successfully removed. Therefore a value >= 0 tells the caller that
10700 + * @lower_dentry belongs to a good branch to create the new object in).
10701 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10702 + * unlink the whiteout.
10704 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10708 + struct dentry *wh_dentry = NULL;
10709 + struct dentry *lower_dir_dentry = NULL;
10711 + /* look for whiteout dentry first */
10712 + lower_dir_dentry = dget_parent(lower_dentry);
10713 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10714 + dput(lower_dir_dentry);
10715 + if (IS_ERR(wh_dentry)) {
10716 + err = PTR_ERR(wh_dentry);
10720 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10725 + /* check if regular file and whiteout were both found */
10726 + if (unlikely(lower_dentry->d_inode))
10727 + printk(KERN_WARNING "unionfs: removing whiteout; regular "
10728 + "file exists in directory %s (branch %d)\n",
10729 + lower_dir_dentry->d_name.name, bindex);
10731 + /* check if branch is writeable */
10732 + err = is_robranch_super(dentry->d_sb, bindex);
10736 + /* .wh.foo has been found, so let's unlink it */
10737 + err = unlink_whiteout(wh_dentry);
10739 + err = 1; /* a whiteout was found and successfully removed */
10747 + * Pass an unionfs dentry and an index. It will try to create a whiteout
10748 + * for the filename in dentry, and will try in branch 'index'. On error,
10749 + * it will proceed to a branch to the left.
10751 +int create_whiteout(struct dentry *dentry, int start)
10753 + int bstart, bend, bindex;
10754 + struct dentry *lower_dir_dentry;
10755 + struct dentry *lower_dentry;
10756 + struct dentry *lower_wh_dentry;
10757 + struct nameidata nd;
10758 + char *name = NULL;
10759 + int err = -EINVAL;
10761 + verify_locked(dentry);
10763 + bstart = dbstart(dentry);
10764 + bend = dbend(dentry);
10766 + /* create dentry's whiteout equivalent */
10767 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10768 + if (unlikely(IS_ERR(name))) {
10769 + err = PTR_ERR(name);
10773 + for (bindex = start; bindex >= 0; bindex--) {
10774 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10776 + if (!lower_dentry) {
10778 + * if lower dentry is not present, create the
10779 + * entire lower dentry directory structure and go
10780 + * ahead. Since we want to just create whiteout, we
10781 + * only want the parent dentry, and hence get rid of
10784 + lower_dentry = create_parents(dentry->d_inode,
10786 + dentry->d_name.name,
10788 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10789 + int ret = PTR_ERR(lower_dentry);
10790 + if (!IS_COPYUP_ERR(ret))
10792 + "unionfs: create_parents for "
10793 + "whiteout failed: bindex=%d "
10794 + "err=%d\n", bindex, ret);
10799 + lower_wh_dentry =
10800 + lookup_lck_len(name, lower_dentry->d_parent,
10801 + dentry->d_name.len + UNIONFS_WHLEN);
10802 + if (IS_ERR(lower_wh_dentry))
10806 + * The whiteout already exists. This used to be impossible,
10807 + * but now is possible because of opaqueness.
10809 + if (lower_wh_dentry->d_inode) {
10810 + dput(lower_wh_dentry);
10815 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10816 + if (unlikely(err < 0))
10818 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10819 + err = is_robranch_super(dentry->d_sb, bindex);
10821 + err = vfs_create(lower_dir_dentry->d_inode,
10823 + current_umask() & S_IRUGO,
10825 + unlock_dir(lower_dir_dentry);
10826 + dput(lower_wh_dentry);
10827 + release_lower_nd(&nd, err);
10829 + if (!err || !IS_COPYUP_ERR(err))
10833 + /* set dbopaque so that lookup will not proceed after this branch */
10835 + dbopaque(dentry) = bindex;
10843 + * Delete all of the whiteouts in a given directory for rmdir.
10845 + * lower directory inode should be locked
10847 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10848 + struct unionfs_dir_state *namelist)
10851 + struct dentry *lower_dir_dentry = NULL;
10852 + struct dentry *lower_dentry;
10853 + char *name = NULL, *p;
10854 + struct inode *lower_dir;
10856 + struct list_head *pos;
10857 + struct filldir_node *cursor;
10859 + /* Find out lower parent dentry */
10860 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10861 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10862 + lower_dir = lower_dir_dentry->d_inode;
10863 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10866 + name = __getname();
10867 + if (unlikely(!name))
10869 + strcpy(name, UNIONFS_WHPFX);
10870 + p = name + UNIONFS_WHLEN;
10873 + for (i = 0; !err && i < namelist->size; i++) {
10874 + list_for_each(pos, &namelist->list[i]) {
10876 + list_entry(pos, struct filldir_node,
10878 + /* Only operate on whiteouts in this branch. */
10879 + if (cursor->bindex != bindex)
10881 + if (!cursor->whiteout)
10884 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10886 + lookup_lck_len(name, lower_dir_dentry,
10887 + cursor->namelen +
10889 + if (IS_ERR(lower_dentry)) {
10890 + err = PTR_ERR(lower_dentry);
10893 + if (lower_dentry->d_inode)
10894 + err = vfs_unlink(lower_dir, lower_dentry);
10895 + dput(lower_dentry);
10903 + /* After all of the removals, we should copy the attributes once. */
10904 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10911 +void __delete_whiteouts(struct work_struct *work)
10913 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10914 + struct deletewh_args *d = &args->deletewh;
10916 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10917 + complete(&args->comp);
10920 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10921 +int delete_whiteouts(struct dentry *dentry, int bindex,
10922 + struct unionfs_dir_state *namelist)
10925 + struct super_block *sb;
10926 + struct dentry *lower_dir_dentry;
10927 + struct inode *lower_dir;
10928 + struct sioq_args args;
10930 + sb = dentry->d_sb;
10932 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10933 + BUG_ON(bindex < dbstart(dentry));
10934 + BUG_ON(bindex > dbend(dentry));
10935 + err = is_robranch_super(sb, bindex);
10939 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10940 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10941 + lower_dir = lower_dir_dentry->d_inode;
10942 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10944 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10945 + err = do_delete_whiteouts(dentry, bindex, namelist);
10947 + args.deletewh.namelist = namelist;
10948 + args.deletewh.dentry = dentry;
10949 + args.deletewh.bindex = bindex;
10950 + run_sioq(__delete_whiteouts, &args);
10958 +/****************************************************************************
10959 + * Opaque directory helpers *
10960 + ****************************************************************************/
10963 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
10964 + * -errno if an error occurred trying to figure this out.
10966 +int is_opaque_dir(struct dentry *dentry, int bindex)
10969 + struct dentry *lower_dentry;
10970 + struct dentry *wh_lower_dentry;
10971 + struct inode *lower_inode;
10972 + struct sioq_args args;
10973 + struct nameidata lower_nd;
10975 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10976 + lower_inode = lower_dentry->d_inode;
10978 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
10980 + mutex_lock(&lower_inode->i_mutex);
10982 + if (!inode_permission(lower_inode, MAY_EXEC)) {
10983 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
10984 + if (unlikely(err < 0)) {
10985 + mutex_unlock(&lower_inode->i_mutex);
10988 + wh_lower_dentry =
10989 + lookup_one_len_nd(UNIONFS_DIR_OPAQUE, lower_dentry,
10990 + sizeof(UNIONFS_DIR_OPAQUE) - 1,
10992 + release_lower_nd(&lower_nd, err);
10994 + args.is_opaque.dentry = lower_dentry;
10995 + run_sioq(__is_opaque_dir, &args);
10996 + wh_lower_dentry = args.ret;
10999 + mutex_unlock(&lower_inode->i_mutex);
11001 + if (IS_ERR(wh_lower_dentry)) {
11002 + err = PTR_ERR(wh_lower_dentry);
11006 + /* This is an opaque dir iff wh_lower_dentry is positive */
11007 + err = !!wh_lower_dentry->d_inode;
11009 + dput(wh_lower_dentry);
11014 +void __is_opaque_dir(struct work_struct *work)
11016 + struct sioq_args *args = container_of(work, struct sioq_args, work);
11017 + struct nameidata lower_nd;
11020 + err = init_lower_nd(&lower_nd, LOOKUP_OPEN);
11021 + if (unlikely(err < 0))
11023 + args->ret = lookup_one_len_nd(UNIONFS_DIR_OPAQUE,
11024 + args->is_opaque.dentry,
11025 + sizeof(UNIONFS_DIR_OPAQUE) - 1,
11027 + release_lower_nd(&lower_nd, err);
11028 + complete(&args->comp);
11031 +int make_dir_opaque(struct dentry *dentry, int bindex)
11034 + struct dentry *lower_dentry, *diropq;
11035 + struct inode *lower_dir;
11036 + struct nameidata nd;
11037 + const struct cred *old_creds;
11038 + struct cred *new_creds;
11041 + * Opaque directory whiteout markers are special files (like regular
11042 + * whiteouts), and should appear to the users as if they don't
11043 + * exist. They should be created/deleted regardless of directory
11044 + * search/create permissions, but only for the duration of this
11045 + * creation of the .wh.__dir_opaque: file. Note, this does not
11046 + * circumvent normal ->permission).
11048 + new_creds = prepare_creds();
11049 + if (unlikely(!new_creds)) {
11053 + cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
11054 + cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
11055 + old_creds = override_creds(new_creds);
11057 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11058 + lower_dir = lower_dentry->d_inode;
11059 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
11060 + !S_ISDIR(lower_dir->i_mode));
11062 + mutex_lock(&lower_dir->i_mutex);
11063 + err = init_lower_nd(&nd, LOOKUP_OPEN);
11064 + if (unlikely(err < 0))
11066 + diropq = lookup_one_len_nd(UNIONFS_DIR_OPAQUE, lower_dentry,
11067 + sizeof(UNIONFS_DIR_OPAQUE) - 1, &nd);
11068 + release_lower_nd(&nd, err);
11069 + if (IS_ERR(diropq)) {
11070 + err = PTR_ERR(diropq);
11074 + err = init_lower_nd(&nd, LOOKUP_CREATE);
11075 + if (unlikely(err < 0))
11077 + if (!diropq->d_inode)
11078 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
11080 + dbopaque(dentry) = bindex;
11081 + release_lower_nd(&nd, err);
11086 + mutex_unlock(&lower_dir->i_mutex);
11087 + revert_creds(old_creds);
11091 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
11092 new file mode 100644
11093 index 0000000..a93d803
11095 +++ b/fs/unionfs/xattr.c
11098 + * Copyright (c) 2003-2011 Erez Zadok
11099 + * Copyright (c) 2003-2006 Charles P. Wright
11100 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11101 + * Copyright (c) 2005-2006 Junjiro Okajima
11102 + * Copyright (c) 2005 Arun M. Krishnakumar
11103 + * Copyright (c) 2004-2006 David P. Quigley
11104 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
11105 + * Copyright (c) 2003 Puja Gupta
11106 + * Copyright (c) 2003 Harikesavan Krishnan
11107 + * Copyright (c) 2003-2011 Stony Brook University
11108 + * Copyright (c) 2003-2011 The Research Foundation of SUNY
11110 + * This program is free software; you can redistribute it and/or modify
11111 + * it under the terms of the GNU General Public License version 2 as
11112 + * published by the Free Software Foundation.
11115 +#include "union.h"
11117 +/* This is lifted from fs/xattr.c */
11118 +void *unionfs_xattr_alloc(size_t size, size_t limit)
11122 + if (size > limit)
11123 + return ERR_PTR(-E2BIG);
11125 + if (!size) /* size request, no buffer is needed */
11128 + ptr = kmalloc(size, GFP_KERNEL);
11129 + if (unlikely(!ptr))
11130 + return ERR_PTR(-ENOMEM);
11135 + * BKL held by caller.
11136 + * dentry->d_inode->i_mutex locked
11138 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11141 + struct dentry *lower_dentry = NULL;
11142 + struct dentry *parent;
11143 + int err = -EOPNOTSUPP;
11146 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11147 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11148 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11150 + valid = __unionfs_d_revalidate(dentry, parent, false);
11151 + if (unlikely(!valid)) {
11156 + lower_dentry = unionfs_lower_dentry(dentry);
11158 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11161 + unionfs_check_dentry(dentry);
11162 + unionfs_unlock_dentry(dentry);
11163 + unionfs_unlock_parent(dentry, parent);
11164 + unionfs_read_unlock(dentry->d_sb);
11169 + * BKL held by caller.
11170 + * dentry->d_inode->i_mutex locked
11172 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11173 + const void *value, size_t size, int flags)
11175 + struct dentry *lower_dentry = NULL;
11176 + struct dentry *parent;
11177 + int err = -EOPNOTSUPP;
11180 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11181 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11182 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11184 + valid = __unionfs_d_revalidate(dentry, parent, false);
11185 + if (unlikely(!valid)) {
11190 + lower_dentry = unionfs_lower_dentry(dentry);
11192 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11196 + unionfs_check_dentry(dentry);
11197 + unionfs_unlock_dentry(dentry);
11198 + unionfs_unlock_parent(dentry, parent);
11199 + unionfs_read_unlock(dentry->d_sb);
11204 + * BKL held by caller.
11205 + * dentry->d_inode->i_mutex locked
11207 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11209 + struct dentry *lower_dentry = NULL;
11210 + struct dentry *parent;
11211 + int err = -EOPNOTSUPP;
11214 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11215 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11216 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11218 + valid = __unionfs_d_revalidate(dentry, parent, false);
11219 + if (unlikely(!valid)) {
11224 + lower_dentry = unionfs_lower_dentry(dentry);
11226 + err = vfs_removexattr(lower_dentry, (char *) name);
11229 + unionfs_check_dentry(dentry);
11230 + unionfs_unlock_dentry(dentry);
11231 + unionfs_unlock_parent(dentry, parent);
11232 + unionfs_read_unlock(dentry->d_sb);
11237 + * BKL held by caller.
11238 + * dentry->d_inode->i_mutex locked
11240 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11242 + struct dentry *lower_dentry = NULL;
11243 + struct dentry *parent;
11244 + int err = -EOPNOTSUPP;
11245 + char *encoded_list = NULL;
11248 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11249 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11250 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11252 + valid = __unionfs_d_revalidate(dentry, parent, false);
11253 + if (unlikely(!valid)) {
11258 + lower_dentry = unionfs_lower_dentry(dentry);
11260 + encoded_list = list;
11261 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11264 + unionfs_check_dentry(dentry);
11265 + unionfs_unlock_dentry(dentry);
11266 + unionfs_unlock_parent(dentry, parent);
11267 + unionfs_read_unlock(dentry->d_sb);
11270 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
11271 index da317c7..64f1ced 100644
11272 --- a/include/linux/fs_stack.h
11273 +++ b/include/linux/fs_stack.h
11276 + * Copyright (c) 2006-2009 Erez Zadok
11277 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11278 + * Copyright (c) 2006-2009 Stony Brook University
11279 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11281 + * This program is free software; you can redistribute it and/or modify
11282 + * it under the terms of the GNU General Public License version 2 as
11283 + * published by the Free Software Foundation.
11286 #ifndef _LINUX_FS_STACK_H
11287 #define _LINUX_FS_STACK_H
11289 -/* This file defines generic functions used primarily by stackable
11291 + * This file defines generic functions used primarily by stackable
11292 * filesystems; none of these functions require i_mutex to be held.
11295 diff --git a/include/linux/magic.h b/include/linux/magic.h
11296 index 2d4beab..39f5798 100644
11297 --- a/include/linux/magic.h
11298 +++ b/include/linux/magic.h
11300 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11301 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11303 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11305 #define SMB_SUPER_MAGIC 0x517B
11306 #define USBDEVICE_SUPER_MAGIC 0x9fa2
11307 #define CGROUP_SUPER_MAGIC 0x27e0eb
11308 diff --git a/include/linux/namei.h b/include/linux/namei.h
11309 index ffc0213..99802c3 100644
11310 --- a/include/linux/namei.h
11311 +++ b/include/linux/namei.h
11312 @@ -84,8 +84,11 @@ extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
11314 extern struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
11315 int (*open)(struct inode *, struct file *));
11316 +extern void release_open_intent(struct nameidata *);
11318 extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
11319 +extern struct dentry *lookup_one_len_nd(const char *, struct dentry *, int,
11320 + struct nameidata *nd);
11322 extern int follow_down_one(struct path *);
11323 extern int follow_down(struct path *);
11324 diff --git a/include/linux/splice.h b/include/linux/splice.h
11325 index 26e5b61..28213e6 100644
11326 --- a/include/linux/splice.h
11327 +++ b/include/linux/splice.h
11328 @@ -81,6 +81,11 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
11329 struct splice_pipe_desc *);
11330 extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11331 splice_direct_actor *);
11332 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11333 + loff_t *ppos, size_t len, unsigned int flags);
11334 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11335 + struct pipe_inode_info *pipe, size_t len,
11336 + unsigned int flags);
11339 * for dynamic pipe sizing
11340 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
11341 new file mode 100644
11342 index 0000000..c84d97e
11344 +++ b/include/linux/union_fs.h
11347 + * Copyright (c) 2003-2009 Erez Zadok
11348 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11349 + * Copyright (c) 2003-2009 Stony Brook University
11350 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11352 + * This program is free software; you can redistribute it and/or modify
11353 + * it under the terms of the GNU General Public License version 2 as
11354 + * published by the Free Software Foundation.
11357 +#ifndef _LINUX_UNION_FS_H
11358 +#define _LINUX_UNION_FS_H
11361 + * DEFINITIONS FOR USER AND KERNEL CODE:
11363 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11364 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11366 +#endif /* _LINUX_UNIONFS_H */
11368 diff --git a/security/security.c b/security/security.c
11369 index d754249..f7e8373 100644
11370 --- a/security/security.c
11371 +++ b/security/security.c
11372 @@ -538,6 +538,7 @@ int security_inode_permission(struct inode *inode, int mask)
11374 return security_ops->inode_permission(inode, mask);
11376 +EXPORT_SYMBOL(security_inode_permission);
11378 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)