1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index 59db1bc..40816d4 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -86,6 +86,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..37a62d8
34 +++ b/Documentation/filesystems/unionfs/concepts.txt
36 +Unionfs 2.1 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.)
90 +Duplicate Elimination:
91 +======================
93 +It is possible for files on different branches to have the same name.
94 +Unionfs then has to select which instance of the file to show to the user.
95 +Given the fact that each branch has a priority associated with it, the
96 +simplest solution is to take the instance from the highest priority
97 +(numerically lowest value) and "hide" the others.
103 +When a change is made to the contents of a file's data or meta-data, they
104 +have to be stored somewhere. The best way is to create a copy of the
105 +original file on a branch that is writable, and then redirect the write
106 +though to this copy. The copy must be made on a higher priority branch so
107 +that lookup and readdir return this newer "version" of the file rather than
108 +the original (see duplicate elimination).
114 +Unionfs users often want to be able to modify files and directories directly
115 +on the lower branches, and have those changes be visible at the Unionfs
116 +level. This means that data (e.g., pages) and meta-data (dentries, inodes,
117 +open files, etc.) have to be synchronized between the upper and lower
118 +layers. In other words, the newest changes from a layer below have to be
119 +propagated to the Unionfs layer above. If the two layers are not in sync, a
120 +cache incoherency ensues, which could lead to application failures and even
121 +oopses. The Linux kernel, however, has a rather limited set of mechanisms
122 +to ensure this inter-layer cache coherency---so Unionfs has to do most of
123 +the hard work on its own.
125 +Maintaining Invariants:
127 +The way Unionfs ensures cache coherency is as follows. At each entry point
128 +to a Unionfs file system method, we call a utility function to validate the
129 +primary objects of this method. Generally, we call unionfs_file_revalidate
130 +on open files, and __unionfs_d_revalidate_chain on dentries (which also
131 +validates inodes). These utility functions check to see whether the upper
132 +Unionfs object is in sync with any of the lower objects that it represents.
133 +The checks we perform include whether the Unionfs superblock has a newer
134 +generation number, or if any of the lower objects mtime's or ctime's are
135 +newer. (Note: generation numbers change when branch-management commands are
136 +issued, so in a way, maintaining cache coherency is also very important for
137 +branch-management.) If indeed we determine that any Unionfs object is no
138 +longer in sync with its lower counterparts, then we rebuild that object
139 +similarly to how we do so for branch-management.
141 +While rebuilding Unionfs's objects, we also purge any page mappings and
142 +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data). This is to
143 +ensure that Unionfs will re-get the newer data from the lower branches. We
144 +perform this purging only if the Unionfs operation in question is a reading
145 +operation; if Unionfs is performing a data writing operation (e.g., ->write,
146 +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
147 +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
148 +considered more authoritative anyway, as they are newer and will overwrite
151 +Unionfs maintains the following important invariant regarding mtime's,
152 +ctime's, and atime's: the upper inode object's times are the max() of all of
153 +the lower ones. For non-directory objects, there's only one object below,
154 +so the mapping is simple; for directory objects, there could me multiple
155 +lower objects and we have to sync up with the newest one of all the lower
156 +ones. This invariant is important to maintain, especially for directories
157 +(besides, we need this to be POSIX compliant). A union could comprise
158 +multiple writable branches, each of which could change. If we don't reflect
159 +the newest possible mtime/ctime, some applications could fail. For example,
160 +NFSv2/v3 exports check for newer directory mtimes on the server to determine
161 +if the client-side attribute cache should be purged.
163 +To maintain these important invariants, of course, Unionfs carefully
164 +synchronizes upper and lower times in various places. For example, if we
165 +copy-up a file to a top-level branch, the parent directory where the file
166 +was copied up to will now have a new mtime: so after a successful copy-up,
167 +we sync up with the new top-level branch's parent directory mtime.
171 +This cache-coherency implementation is efficient because it defers any
172 +synchronizing between the upper and lower layers until absolutely needed.
173 +Consider the example a common situation where users perform a lot of lower
174 +changes, such as untarring a whole package. While these take place,
175 +typically the user doesn't access the files via Unionfs; only after the
176 +lower changes are done, does the user try to access the lower files. With
177 +our cache-coherency implementation, the entirety of the changes to the lower
178 +branches will not result in a single CPU cycle spent at the Unionfs level
179 +until the user invokes a system call that goes through Unionfs.
181 +We have considered two alternate cache-coherency designs. (1) Using the
182 +dentry/inode notify functionality to register interest in finding out about
183 +any lower changes. This is a somewhat limited and also a heavy-handed
184 +approach which could result in many notifications to the Unionfs layer upon
185 +each small change at the lower layer (imagine a file being modified multiple
186 +times in rapid succession). (2) Rewriting the VFS to support explicit
187 +callbacks from lower objects to upper objects. We began exploring such an
188 +implementation, but found it to be very complicated--it would have resulted
189 +in massive VFS/MM changes which are unlikely to be accepted by the LKML
190 +community. We therefore believe that our current cache-coherency design and
191 +implementation represent the best approach at this time.
195 +Our implementation works in that as long as a user process will have caused
196 +Unionfs to be called, directly or indirectly, even to just do
197 +->d_revalidate; then we will have purged the current Unionfs data and the
198 +process will see the new data. For example, a process that continually
199 +re-reads the same file's data will see the NEW data as soon as the lower
200 +file had changed, upon the next read(2) syscall (even if the file is still
201 +open!) However, this doesn't work when the process re-reads the open file's
202 +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
203 +it). Once we respond to ->readpage(s), then the kernel maps the page into
204 +the process's address space and there doesn't appear to be a way to force
205 +the kernel to invalidate those pages/mappings, and force the process to
206 +re-issue ->readpage. If there's a way to invalidate active mappings and
207 +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
210 +Our current Unionfs code has to perform many file-revalidation calls. It
211 +would be really nice if the VFS would export an optional file system hook
212 +->file_revalidate (similarly to dentry->d_revalidate) that will be called
213 +before each VFS op that has a "struct file" in it.
216 +For more information, see <http://unionfs.filesystems.org/>.
217 diff --git a/Documentation/filesystems/unionfs/issues.txt b/Documentation/filesystems/unionfs/issues.txt
219 index 0000000..9db1d70
221 +++ b/Documentation/filesystems/unionfs/issues.txt
223 +KNOWN Unionfs 2.1 ISSUES:
224 +=========================
226 +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
227 + confuses NFSv4. Currently, unionfs_lookup() passes lookup intents to the
228 + lower file-system, this eliminates part of the problem. The remaining
229 + calls to lookup_one_len may need to be changed to pass an intent. We are
230 + currently introducing VFS changes to fs/namei.c's do_path_lookup() to
231 + allow proper file lookup and opening in stackable file systems.
233 +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
234 + incorrectly complains about locking problems. The problem boils down to
235 + this: Lockdep considers all objects of a certain type to be in the same
236 + class, for example, all inodes. Lockdep doesn't like to see a lock held
237 + on two inodes within the same task, and warns that it could lead to a
238 + deadlock. However, stackable file systems do precisely that: they lock
239 + an upper object, and then a lower object, in a strict order to avoid
240 + locking problems; in addition, Unionfs, as a fan-out file system, may
241 + have to lock several lower inodes. We are currently looking into Lockdep
242 + to see how to make it aware of stackable file systems. In the meantime,
243 + if you get any warnings from Lockdep, you can safely ignore them (or feel
244 + free to report them to the Unionfs maintainers, just to be sure).
246 +For more information, see <http://unionfs.filesystems.org/>.
247 diff --git a/Documentation/filesystems/unionfs/rename.txt b/Documentation/filesystems/unionfs/rename.txt
249 index 0000000..e20bb82
251 +++ b/Documentation/filesystems/unionfs/rename.txt
253 +Rename is a complex beast. The following table shows which rename(2) operations
254 +should succeed and which should fail.
257 +E: error (either unionfs or vfs)
260 +none = file does not exist
261 +file = file is a file
262 +dir = file is a empty directory
263 +child= file is a non-empty directory
264 +wh = file is a directory containing only whiteouts; this makes it logically
267 + none file dir child wh
274 +Renaming directories:
275 +=====================
277 +Whenever a empty (either physically or logically) directory is being renamed,
278 +the following sequence of events should take place:
280 +1) Remove whiteouts from both source and destination directory
281 +2) Rename source to destination
282 +3) Make destination opaque to prevent anything under it from showing up
284 diff --git a/Documentation/filesystems/unionfs/usage.txt b/Documentation/filesystems/unionfs/usage.txt
286 index 0000000..d8c15de
288 +++ b/Documentation/filesystems/unionfs/usage.txt
290 +Unionfs is a stackable unification file system, which can appear to merge
291 +the contents of several directories (branches), while keeping their physical
292 +content separate. Unionfs is useful for unified source tree management,
293 +merged contents of split CD-ROM, merged separate software package
294 +directories, data grids, and more. Unionfs allows any mix of read-only and
295 +read-write branches, as well as insertion and deletion of branches anywhere
296 +in the fan-out. To maintain Unix semantics, Unionfs handles elimination of
297 +duplicates, partial-error conditions, and more.
299 +# mount -t unionfs -o branch-option[,union-options[,...]] none MOUNTPOINT
301 +The available branch-option for the mount command is:
303 + dirs=branch[=ro|=rw][:...]
305 +specifies a separated list of which directories compose the union.
306 +Directories that come earlier in the list have a higher precedence than
307 +those which come later. Additionally, read-only or read-write permissions of
308 +the branch can be specified by appending =ro or =rw (default) to each
313 + dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
317 + dirs=/writable_branch=rw:/read-only_branch=ro
320 +DYNAMIC BRANCH MANAGEMENT AND REMOUNTS
321 +======================================
323 +You can remount a union and change its overall mode, or reconfigure the
324 +branches, as follows.
326 +To downgrade a union from read-write to read-only:
328 +# mount -t unionfs -o remount,ro none MOUNTPOINT
330 +To upgrade a union from read-only to read-write:
332 +# mount -t unionfs -o remount,rw none MOUNTPOINT
334 +To delete a branch /foo, regardless where it is in the current union:
336 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
338 +To insert (add) a branch /foo before /bar:
340 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
342 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
344 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
346 +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
347 +new highest-priority branch), you can use the above syntax, or use a short
348 +hand version as follows:
350 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
352 +To append a branch to the very end (new lowest-priority branch):
354 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
356 +To append a branch to the very end (new lowest-priority branch), in
359 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
361 +Finally, to change the mode of one existing branch, say /foo, from read-only
362 +to read-write, and change /bar from read-write to read-only:
364 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
370 +If you modify any file on any of the lower branches directly, while there is
371 +a Unionfs 2.1 mounted above any of those branches, you should tell Unionfs
372 +to purge its caches and re-get the objects. To do that, you have to
373 +increment the generation number of the superblock using the following
376 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
378 +Note that the older way of incrementing the generation number using an
379 +ioctl, is no longer supported in Unionfs 2.0 and newer. Ioctls in general
380 +are not encouraged. Plus, an ioctl is per-file concept, whereas the
381 +generation number is a per-file-system concept. Worse, such an ioctl
382 +requires an open file, which then has to be invalidated by the very nature
383 +of the generation number increase (read: the old generation increase ioctl
387 +For more information, see <http://unionfs.filesystems.org/>.
388 diff --git a/MAINTAINERS b/MAINTAINERS
389 index 9a91d9e..7e45e87 100644
392 @@ -3740,6 +3740,15 @@ L: linux-kernel@vger.kernel.org
393 W: http://www.kernel.dk
398 +M: ezk@cs.sunysb.edu
399 +P: Josef "Jeff" Sipek
400 +M: jsipek@cs.sunysb.edu
401 +L: unionfs@filesystems.org
402 +W: http://unionfs.filesystems.org
407 M: oliver@neukum.name
408 diff --git a/fs/Kconfig b/fs/Kconfig
409 index f9eed6d..9afb8df 100644
412 @@ -1027,6 +1027,47 @@ config CONFIGFS_FS
416 +menu "Layered filesystems"
419 + tristate "eCrypt filesystem layer support (EXPERIMENTAL)"
420 + depends on EXPERIMENTAL && KEYS && CRYPTO && NET
422 + Encrypted filesystem that operates on the VFS layer. See
423 + <file:Documentation/ecryptfs.txt> to learn more about
424 + eCryptfs. Userspace components are required and can be
425 + obtained from <http://ecryptfs.sf.net>.
427 + To compile this file system support as a module, choose M here: the
428 + module will be called ecryptfs.
431 + tristate "Union file system (EXPERIMENTAL)"
432 + depends on EXPERIMENTAL
434 + Unionfs is a stackable unification file system, which appears to
435 + merge the contents of several directories (branches), while keeping
436 + their physical content separate.
438 + See <http://unionfs.filesystems.org> for details
440 +config UNION_FS_XATTR
441 + bool "Unionfs extended attributes"
442 + depends on UNION_FS
444 + Extended attributes are name:value pairs associated with inodes by
445 + the kernel or by users (see the attr(5) manual page).
449 +config UNION_FS_DEBUG
450 + bool "Debug Unionfs"
451 + depends on UNION_FS
453 + If you say Y here, you can turn on debugging output from Unionfs.
457 menu "Miscellaneous filesystems"
460 @@ -1134,18 +1134,6 @@
461 To compile this file system support as a module, choose M here: the
462 module will be called affs. If unsure, say N.
465 - tristate "eCrypt filesystem layer support (EXPERIMENTAL)"
466 - depends on EXPERIMENTAL && KEYS && CRYPTO && NET
468 - Encrypted filesystem that operates on the VFS layer. See
469 - <file:Documentation/filesystems/ecryptfs.txt> to learn more about
470 - eCryptfs. Userspace components are required and can be
471 - obtained from <http://ecryptfs.sf.net>.
473 - To compile this file system support as a module, choose M here: the
474 - module will be called ecryptfs.
477 tristate "Apple Macintosh file system support (EXPERIMENTAL)"
478 depends on BLOCK && EXPERIMENTAL
479 diff --git a/fs/Makefile b/fs/Makefile
480 index 720c29d..951f411 100644
483 @@ -118,3 +118,4 @@ obj-$(CONFIG_HPPFS) += hppfs/
484 obj-$(CONFIG_DEBUG_FS) += debugfs/
485 obj-$(CONFIG_OCFS2_FS) += ocfs2/
486 obj-$(CONFIG_GFS2_FS) += gfs2/
487 +obj-$(CONFIG_UNION_FS) += unionfs/
488 diff --git a/fs/drop_caches.c b/fs/drop_caches.c
489 index 59375ef..90410ac 100644
490 --- a/fs/drop_caches.c
491 +++ b/fs/drop_caches.c
495 #include <linux/kernel.h>
496 +#include <linux/module.h>
497 #include <linux/mm.h>
498 #include <linux/fs.h>
499 #include <linux/writeback.h>
501 /* A global variable is a bit ugly, but it keeps the code simple */
502 int sysctl_drop_caches;
504 -static void drop_pagecache_sb(struct super_block *sb)
505 +void drop_pagecache_sb(struct super_block *sb)
509 @@ -24,6 +25,7 @@ static void drop_pagecache_sb(struct super_block *sb)
511 spin_unlock(&inode_lock);
513 +EXPORT_SYMBOL(drop_pagecache_sb);
515 void drop_pagecache(void)
517 diff --git a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
518 index cb20b96..a8c1686 100644
519 --- a/fs/ecryptfs/dentry.c
520 +++ b/fs/ecryptfs/dentry.c
521 @@ -62,7 +62,7 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
522 struct inode *lower_inode =
523 ecryptfs_inode_to_lower(dentry->d_inode);
525 - fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
526 + fsstack_copy_attr_all(dentry->d_inode, lower_inode);
530 diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
531 index 131954b..fc4c6cb 100644
532 --- a/fs/ecryptfs/inode.c
533 +++ b/fs/ecryptfs/inode.c
534 @@ -601,9 +601,9 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
535 lower_new_dir_dentry->d_inode, lower_new_dentry);
538 - fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
539 + fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
540 if (new_dir != old_dir)
541 - fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
542 + fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
544 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
545 dput(lower_new_dentry->d_parent);
546 @@ -961,7 +961,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
548 rc = notify_change(lower_dentry, ia);
550 - fsstack_copy_attr_all(inode, lower_inode, NULL);
551 + fsstack_copy_attr_all(inode, lower_inode);
555 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
556 index a984972..cb349a4 100644
557 --- a/fs/ecryptfs/main.c
558 +++ b/fs/ecryptfs/main.c
559 @@ -151,7 +151,7 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
560 d_add(dentry, inode);
562 d_instantiate(dentry, inode);
563 - fsstack_copy_attr_all(inode, lower_inode, NULL);
564 + fsstack_copy_attr_all(inode, lower_inode);
565 /* This size will be overwritten for real files w/ headers and
567 fsstack_copy_inode_size(inode, lower_inode);
568 diff --git a/fs/namei.c b/fs/namei.c
569 index a83160a..b2b7c8e 100644
572 @@ -374,6 +374,7 @@ void release_open_intent(struct nameidata *nd)
574 fput(nd->intent.open.file);
576 +EXPORT_SYMBOL(release_open_intent);
578 static inline struct dentry *
579 do_revalidate(struct dentry *dentry, struct nameidata *nd)
580 diff --git a/fs/stack.c b/fs/stack.c
581 index 67716f6..a548aac 100644
586 + * Copyright (c) 2006-2007 Erez Zadok
587 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
588 + * Copyright (c) 2006-2007 Stony Brook University
589 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
591 + * This program is free software; you can redistribute it and/or modify
592 + * it under the terms of the GNU General Public License version 2 as
593 + * published by the Free Software Foundation.
596 #include <linux/module.h>
597 #include <linux/fs.h>
598 #include <linux/fs_stack.h>
600 -/* does _NOT_ require i_mutex to be held.
602 + * does _NOT_ require i_mutex to be held.
604 * This function cannot be inlined since i_size_{read,write} is rather
605 * heavy-weight on 32-bit systems
606 @@ -14,11 +26,11 @@ void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
608 EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
610 -/* copy all attributes; get_nlinks is optional way to override the i_nlink
612 + * copy all attributes; get_nlinks is optional way to override the i_nlink
615 -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
616 - int (*get_nlinks)(struct inode *))
617 +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
619 dest->i_mode = src->i_mode;
620 dest->i_uid = src->i_uid;
621 @@ -29,14 +41,6 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
622 dest->i_ctime = src->i_ctime;
623 dest->i_blkbits = src->i_blkbits;
624 dest->i_flags = src->i_flags;
627 - * Update the nlinks AFTER updating the above fields, because the
628 - * get_links callback may depend on them.
631 - dest->i_nlink = src->i_nlink;
633 - dest->i_nlink = (*get_nlinks)(dest);
634 + dest->i_nlink = src->i_nlink;
636 EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
637 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
639 index 0000000..6c55790
641 +++ b/fs/unionfs/Makefile
643 +UNIONFS_VERSION="2.1.6 (for 2.6.23-rc8)"
645 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
647 +obj-$(CONFIG_UNION_FS) += unionfs.o
649 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
650 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
651 + lookup.o commonfops.o dirfops.o sioq.o mmap.o
653 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
655 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
657 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
658 +EXTRA_CFLAGS += -DDEBUG
660 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
662 index 0000000..7654bcb
664 +++ b/fs/unionfs/commonfops.c
667 + * Copyright (c) 2003-2007 Erez Zadok
668 + * Copyright (c) 2003-2006 Charles P. Wright
669 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
670 + * Copyright (c) 2005-2006 Junjiro Okajima
671 + * Copyright (c) 2005 Arun M. Krishnakumar
672 + * Copyright (c) 2004-2006 David P. Quigley
673 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
674 + * Copyright (c) 2003 Puja Gupta
675 + * Copyright (c) 2003 Harikesavan Krishnan
676 + * Copyright (c) 2003-2007 Stony Brook University
677 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
679 + * This program is free software; you can redistribute it and/or modify
680 + * it under the terms of the GNU General Public License version 2 as
681 + * published by the Free Software Foundation.
687 + * 1) Copyup the file
688 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
689 + * stolen from NFS's silly rename
691 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
692 + int bstart, int bindex)
694 + static unsigned int counter;
695 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
696 + const int countersize = sizeof(counter) * 2;
697 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
698 + char name[nlen + 1];
700 + struct dentry *tmp_dentry = NULL;
701 + struct dentry *lower_dentry;
702 + struct dentry *lower_dir_dentry = NULL;
704 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
706 + sprintf(name, ".unionfs%*.*lx",
707 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
710 + * Loop, looking for an unused temp name to copyup to.
712 + * It's somewhat silly that we look for a free temp tmp name in the
713 + * source branch (bstart) instead of the dest branch (bindex), where
714 + * the final name will be created. We _will_ catch it if somehow
715 + * the name exists in the dest branch, but it'd be nice to catch it
716 + * sooner than later.
721 + char *suffix = name + nlen - countersize;
725 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
727 + pr_debug("unionfs: trying to rename %s to %s\n",
728 + dentry->d_name.name, name);
730 + tmp_dentry = lookup_one_len(name, lower_dentry->d_parent,
732 + if (IS_ERR(tmp_dentry)) {
733 + err = PTR_ERR(tmp_dentry);
736 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
739 + err = copyup_named_file(dentry->d_parent->d_inode, file, name, bstart,
740 + bindex, file->f_path.dentry->d_inode->i_size);
742 + if (unlikely(err == -EEXIST))
747 + /* bring it to the same state as an unlinked file */
748 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
749 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
750 + atomic_inc(&lower_dentry->d_inode->i_count);
751 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
752 + lower_dentry->d_inode);
754 + lower_dir_dentry = lock_parent(lower_dentry);
755 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
756 + unlock_dir(lower_dir_dentry);
760 + unionfs_check_dentry(dentry);
765 + * put all references held by upper struct file and free lower file pointer
768 +static void cleanup_file(struct file *file)
770 + int bindex, bstart, bend;
771 + struct file **lower_files;
772 + struct file *lower_file;
773 + struct super_block *sb = file->f_path.dentry->d_sb;
775 + lower_files = UNIONFS_F(file)->lower_files;
776 + bstart = fbstart(file);
777 + bend = fbend(file);
779 + for (bindex = bstart; bindex <= bend; bindex++) {
780 + int i; /* holds (possibly) updated branch index */
783 + lower_file = unionfs_lower_file_idx(file, bindex);
788 + * Find new index of matching branch with an open
789 + * file, since branches could have been added or
790 + * deleted causing the one with open files to shift.
792 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
793 + i = branch_id_to_idx(sb, old_bid);
794 + if (unlikely(i < 0)) {
795 + printk(KERN_ERR "unionfs: no superblock for "
796 + "file %p\n", file);
800 + /* decrement count of open files */
803 + * fput will perform an mntput for us on the correct branch.
804 + * Although we're using the file's old branch configuration,
805 + * bindex, which is the old index, correctly points to the
806 + * right branch in the file's branch list. In other words,
807 + * we're going to mntput the correct branch even if branches
808 + * have been added/removed.
811 + UNIONFS_F(file)->lower_files[bindex] = NULL;
812 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
815 + UNIONFS_F(file)->lower_files = NULL;
816 + kfree(lower_files);
817 + kfree(UNIONFS_F(file)->saved_branch_ids);
818 + /* set to NULL because caller needs to know if to kfree on error */
819 + UNIONFS_F(file)->saved_branch_ids = NULL;
822 +/* open all lower files for a given file */
823 +static int open_all_files(struct file *file)
825 + int bindex, bstart, bend, err = 0;
826 + struct file *lower_file;
827 + struct dentry *lower_dentry;
828 + struct dentry *dentry = file->f_path.dentry;
829 + struct super_block *sb = dentry->d_sb;
831 + bstart = dbstart(dentry);
832 + bend = dbend(dentry);
834 + for (bindex = bstart; bindex <= bend; bindex++) {
835 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
839 + dget(lower_dentry);
840 + unionfs_mntget(dentry, bindex);
841 + branchget(sb, bindex);
844 + dentry_open(lower_dentry,
845 + unionfs_lower_mnt_idx(dentry, bindex),
847 + if (IS_ERR(lower_file)) {
848 + err = PTR_ERR(lower_file);
851 + unionfs_set_lower_file_idx(file, bindex, lower_file);
858 +/* open the highest priority file for a given upper file */
859 +static int open_highest_file(struct file *file, bool willwrite)
861 + int bindex, bstart, bend, err = 0;
862 + struct file *lower_file;
863 + struct dentry *lower_dentry;
864 + struct dentry *dentry = file->f_path.dentry;
865 + struct inode *parent_inode = dentry->d_parent->d_inode;
866 + struct super_block *sb = dentry->d_sb;
867 + size_t inode_size = dentry->d_inode->i_size;
869 + bstart = dbstart(dentry);
870 + bend = dbend(dentry);
872 + lower_dentry = unionfs_lower_dentry(dentry);
873 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
874 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
875 + err = copyup_file(parent_inode, file, bstart, bindex,
880 + atomic_set(&UNIONFS_F(file)->generation,
881 + atomic_read(&UNIONFS_I(dentry->d_inode)->
886 + dget(lower_dentry);
887 + unionfs_mntget(dentry, bstart);
888 + lower_file = dentry_open(lower_dentry,
889 + unionfs_lower_mnt_idx(dentry, bstart),
891 + if (IS_ERR(lower_file)) {
892 + err = PTR_ERR(lower_file);
895 + branchget(sb, bstart);
896 + unionfs_set_lower_file(file, lower_file);
897 + /* Fix up the position. */
898 + lower_file->f_pos = file->f_pos;
900 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
905 +/* perform a delayed copyup of a read-write file on a read-only branch */
906 +static int do_delayed_copyup(struct file *file)
908 + int bindex, bstart, bend, err = 0;
909 + struct dentry *dentry = file->f_path.dentry;
910 + struct inode *parent_inode = dentry->d_parent->d_inode;
911 + loff_t inode_size = dentry->d_inode->i_size;
913 + bstart = fbstart(file);
914 + bend = fbend(file);
916 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
918 + unionfs_check_file(file);
919 + unionfs_check_dentry(dentry);
920 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
921 + if (!d_deleted(dentry))
922 + err = copyup_file(parent_inode, file, bstart,
923 + bindex, inode_size);
925 + err = copyup_deleted_file(file, dentry, bstart,
931 + if (err || (bstart <= fbstart(file)))
933 + bend = fbend(file);
934 + for (bindex = bstart; bindex <= bend; bindex++) {
935 + if (unionfs_lower_file_idx(file, bindex)) {
936 + branchput(dentry->d_sb, bindex);
937 + fput(unionfs_lower_file_idx(file, bindex));
938 + unionfs_set_lower_file_idx(file, bindex, NULL);
940 + if (unionfs_lower_mnt_idx(dentry, bindex)) {
941 + unionfs_mntput(dentry, bindex);
942 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
944 + if (unionfs_lower_dentry_idx(dentry, bindex)) {
945 + BUG_ON(!dentry->d_inode);
946 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
947 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
949 + dput(unionfs_lower_dentry_idx(dentry, bindex));
950 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
953 + /* for reg file, we only open it "once" */
954 + fbend(file) = fbstart(file);
955 + set_dbend(dentry, dbstart(dentry));
956 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
959 + unionfs_check_file(file);
960 + unionfs_check_dentry(dentry);
965 + * Revalidate the struct file
966 + * @file: file to revalidate
967 + * @willwrite: true if caller may cause changes to the file; false otherwise.
969 +int unionfs_file_revalidate(struct file *file, bool willwrite)
971 + struct super_block *sb;
972 + struct dentry *dentry;
973 + int sbgen, fgen, dgen;
978 + dentry = file->f_path.dentry;
979 + unionfs_lock_dentry(dentry);
983 + * First revalidate the dentry inside struct file,
984 + * but not unhashed dentries.
986 + if (unlikely(!d_deleted(dentry) &&
987 + !__unionfs_d_revalidate_chain(dentry, NULL, willwrite))) {
992 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
993 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
994 + fgen = atomic_read(&UNIONFS_F(file)->generation);
996 + BUG_ON(sbgen > dgen);
999 + * There are two cases we are interested in. The first is if the
1000 + * generation is lower than the super-block. The second is if
1001 + * someone has copied up this file from underneath us, we also need
1002 + * to refresh things.
1004 + if (unlikely(!d_deleted(dentry) &&
1005 + (sbgen > fgen || dbstart(dentry) != fbstart(file)))) {
1006 + /* save orig branch ID */
1008 + UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1010 + /* First we throw out the existing files. */
1011 + cleanup_file(file);
1013 + /* Now we reopen the file(s) as in unionfs_open. */
1014 + bstart = fbstart(file) = dbstart(dentry);
1015 + bend = fbend(file) = dbend(dentry);
1017 + size = sizeof(struct file *) * sbmax(sb);
1018 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1019 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1023 + size = sizeof(int) * sbmax(sb);
1024 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1025 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1030 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1031 + /* We need to open all the files. */
1032 + err = open_all_files(file);
1037 + /* We only open the highest priority branch. */
1038 + err = open_highest_file(file, willwrite);
1041 + new_brid = UNIONFS_F(file)->
1042 + saved_branch_ids[fbstart(file)];
1043 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1045 + * If we re-opened the file on a different
1046 + * branch than the original one, and this
1047 + * was due to a new branch inserted, then
1048 + * update the mnt counts of the old and new
1049 + * branches accordingly.
1051 + unionfs_mntget(dentry, bstart);
1052 + unionfs_mntput(sb->s_root,
1053 + branch_id_to_idx(sb, orig_brid));
1056 + atomic_set(&UNIONFS_F(file)->generation,
1058 + &UNIONFS_I(dentry->d_inode)->generation));
1061 + /* Copyup on the first write to a file on a readonly branch. */
1062 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1063 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1064 + is_robranch(dentry)) {
1065 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1066 + dentry->d_name.name);
1067 + err = do_delayed_copyup(file);
1072 + kfree(UNIONFS_F(file)->lower_files);
1073 + kfree(UNIONFS_F(file)->saved_branch_ids);
1077 + unionfs_check_file(file);
1078 + unionfs_unlock_dentry(dentry);
1082 +/* unionfs_open helper function: open a directory */
1083 +static int __open_dir(struct inode *inode, struct file *file)
1085 + struct dentry *lower_dentry;
1086 + struct file *lower_file;
1087 + int bindex, bstart, bend;
1088 + struct vfsmount *mnt;
1090 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1091 + bend = fbend(file) = dbend(file->f_path.dentry);
1093 + for (bindex = bstart; bindex <= bend; bindex++) {
1095 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1096 + if (!lower_dentry)
1099 + dget(lower_dentry);
1100 + unionfs_mntget(file->f_path.dentry, bindex);
1101 + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1102 + lower_file = dentry_open(lower_dentry, mnt, file->f_flags);
1103 + if (IS_ERR(lower_file))
1104 + return PTR_ERR(lower_file);
1106 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1109 + * The branchget goes after the open, because otherwise
1110 + * we would miss the reference on release.
1112 + branchget(inode->i_sb, bindex);
1118 +/* unionfs_open helper function: open a file */
1119 +static int __open_file(struct inode *inode, struct file *file)
1121 + struct dentry *lower_dentry;
1122 + struct file *lower_file;
1124 + int bindex, bstart, bend;
1126 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1127 + lower_flags = file->f_flags;
1129 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1130 + bend = fbend(file) = dbend(file->f_path.dentry);
1133 + * check for the permission for lower file. If the error is
1134 + * COPYUP_ERR, copyup the file.
1136 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1138 + * if the open will change the file, copy it up otherwise
1141 + if (lower_flags & O_TRUNC) {
1145 + /* copyup the file */
1146 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1147 + err = copyup_file(
1148 + file->f_path.dentry->d_parent->d_inode,
1149 + file, bstart, bindex, size);
1155 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1159 + dget(lower_dentry);
1162 + * dentry_open will decrement mnt refcnt if err.
1163 + * otherwise fput() will do an mntput() for us upon file close.
1165 + unionfs_mntget(file->f_path.dentry, bstart);
1167 + dentry_open(lower_dentry,
1168 + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1170 + if (IS_ERR(lower_file))
1171 + return PTR_ERR(lower_file);
1173 + unionfs_set_lower_file(file, lower_file);
1174 + branchget(inode->i_sb, bstart);
1179 +int unionfs_open(struct inode *inode, struct file *file)
1182 + struct file *lower_file = NULL;
1183 + struct dentry *dentry = NULL;
1184 + int bindex = 0, bstart = 0, bend = 0;
1187 + unionfs_read_lock(inode->i_sb);
1189 + file->private_data =
1190 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1191 + if (unlikely(!UNIONFS_F(file))) {
1195 + fbstart(file) = -1;
1197 + atomic_set(&UNIONFS_F(file)->generation,
1198 + atomic_read(&UNIONFS_I(inode)->generation));
1200 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1201 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1202 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1206 + size = sizeof(int) * sbmax(inode->i_sb);
1207 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1208 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1213 + dentry = file->f_path.dentry;
1214 + unionfs_lock_dentry(dentry);
1216 + bstart = fbstart(file) = dbstart(dentry);
1217 + bend = fbend(file) = dbend(dentry);
1219 + /* increment, so that we can flush appropriately */
1220 + atomic_inc(&UNIONFS_I(dentry->d_inode)->totalopens);
1223 + * open all directories and make the unionfs file struct point to
1224 + * these lower file structs
1226 + if (S_ISDIR(inode->i_mode))
1227 + err = __open_dir(inode, file); /* open a dir */
1229 + err = __open_file(inode, file); /* open a file */
1231 + /* freeing the allocated resources, and fput the opened files */
1233 + atomic_dec(&UNIONFS_I(dentry->d_inode)->totalopens);
1234 + for (bindex = bstart; bindex <= bend; bindex++) {
1235 + lower_file = unionfs_lower_file_idx(file, bindex);
1239 + branchput(file->f_path.dentry->d_sb, bindex);
1240 + /* fput calls dput for lower_dentry */
1245 + unionfs_unlock_dentry(dentry);
1249 + kfree(UNIONFS_F(file)->lower_files);
1250 + kfree(UNIONFS_F(file)->saved_branch_ids);
1251 + kfree(UNIONFS_F(file));
1254 + unionfs_read_unlock(inode->i_sb);
1255 + unionfs_check_inode(inode);
1257 + unionfs_check_file(file);
1258 + unionfs_check_dentry(file->f_path.dentry->d_parent);
1264 + * release all lower object references & free the file info structure
1266 + * No need to grab sb info's rwsem.
1268 +int unionfs_file_release(struct inode *inode, struct file *file)
1270 + struct file *lower_file = NULL;
1271 + struct unionfs_file_info *fileinfo;
1272 + struct unionfs_inode_info *inodeinfo;
1273 + struct super_block *sb = inode->i_sb;
1274 + int bindex, bstart, bend;
1275 + int fgen, err = 0;
1277 + unionfs_read_lock(sb);
1279 + * Yes, we have to revalidate this file even if it's being released.
1280 + * This is important for open-but-unlinked files, as well as mmap
1283 + err = unionfs_file_revalidate(file, true);
1284 + if (unlikely(err))
1286 + unionfs_check_file(file);
1287 + fileinfo = UNIONFS_F(file);
1288 + BUG_ON(file->f_path.dentry->d_inode != inode);
1289 + inodeinfo = UNIONFS_I(inode);
1291 + /* fput all the lower files */
1292 + fgen = atomic_read(&fileinfo->generation);
1293 + bstart = fbstart(file);
1294 + bend = fbend(file);
1296 + for (bindex = bstart; bindex <= bend; bindex++) {
1297 + lower_file = unionfs_lower_file_idx(file, bindex);
1301 + branchput(sb, bindex);
1304 + kfree(fileinfo->lower_files);
1305 + kfree(fileinfo->saved_branch_ids);
1307 + if (fileinfo->rdstate) {
1308 + fileinfo->rdstate->access = jiffies;
1309 + pr_debug("unionfs: saving rdstate with cookie "
1311 + fileinfo->rdstate->cookie,
1312 + fileinfo->rdstate->bindex,
1313 + (long long)fileinfo->rdstate->dirpos);
1314 + spin_lock(&inodeinfo->rdlock);
1315 + inodeinfo->rdcount++;
1316 + list_add_tail(&fileinfo->rdstate->cache,
1317 + &inodeinfo->readdircache);
1318 + mark_inode_dirty(inode);
1319 + spin_unlock(&inodeinfo->rdlock);
1320 + fileinfo->rdstate = NULL;
1325 + unionfs_read_unlock(sb);
1329 +/* pass the ioctl to the lower fs */
1330 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1332 + struct file *lower_file;
1335 + lower_file = unionfs_lower_file(file);
1337 + err = security_file_ioctl(lower_file, cmd, arg);
1342 + if (!lower_file || !lower_file->f_op)
1344 + if (lower_file->f_op->unlocked_ioctl) {
1345 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1346 + } else if (lower_file->f_op->ioctl) {
1348 + err = lower_file->f_op->ioctl(
1349 + lower_file->f_path.dentry->d_inode,
1350 + lower_file, cmd, arg);
1359 + * return to user-space the branch indices containing the file in question
1361 + * We use fd_set and therefore we are limited to the number of the branches
1362 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1364 +static int unionfs_ioctl_queryfile(struct file *file, unsigned int cmd,
1365 + unsigned long arg)
1368 + fd_set branchlist;
1369 + int bstart = 0, bend = 0, bindex = 0;
1370 + int orig_bstart, orig_bend;
1371 + struct dentry *dentry, *lower_dentry;
1372 + struct vfsmount *mnt;
1374 + dentry = file->f_path.dentry;
1375 + unionfs_lock_dentry(dentry);
1376 + orig_bstart = dbstart(dentry);
1377 + orig_bend = dbend(dentry);
1378 + err = unionfs_partial_lookup(dentry);
1381 + bstart = dbstart(dentry);
1382 + bend = dbend(dentry);
1384 + FD_ZERO(&branchlist);
1386 + for (bindex = bstart; bindex <= bend; bindex++) {
1387 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1388 + if (!lower_dentry)
1390 + if (likely(lower_dentry->d_inode))
1391 + FD_SET(bindex, &branchlist);
1392 + /* purge any lower objects after partial_lookup */
1393 + if (bindex < orig_bstart || bindex > orig_bend) {
1394 + dput(lower_dentry);
1395 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1396 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1397 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1399 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1402 + unionfs_mntput(dentry, bindex);
1403 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1406 + /* restore original dentry's offsets */
1407 + set_dbstart(dentry, orig_bstart);
1408 + set_dbend(dentry, orig_bend);
1409 + ibstart(dentry->d_inode) = orig_bstart;
1410 + ibend(dentry->d_inode) = orig_bend;
1412 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1413 + if (unlikely(err))
1417 + unionfs_unlock_dentry(dentry);
1418 + return err < 0 ? err : bend;
1421 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1425 + unionfs_read_lock(file->f_path.dentry->d_sb);
1427 + err = unionfs_file_revalidate(file, true);
1428 + if (unlikely(err))
1431 + /* check if asked for local commands */
1433 + case UNIONFS_IOCTL_INCGEN:
1434 + /* Increment the superblock generation count */
1435 + pr_info("unionfs: incgen ioctl deprecated; "
1436 + "use \"-o remount,incgen\"\n");
1440 + case UNIONFS_IOCTL_QUERYFILE:
1441 + /* Return list of branches containing the given file */
1442 + err = unionfs_ioctl_queryfile(file, cmd, arg);
1446 + /* pass the ioctl down */
1447 + err = do_ioctl(file, cmd, arg);
1452 + unionfs_read_unlock(file->f_path.dentry->d_sb);
1453 + unionfs_check_file(file);
1457 +int unionfs_flush(struct file *file, fl_owner_t id)
1460 + struct file *lower_file = NULL;
1461 + struct dentry *dentry = file->f_path.dentry;
1462 + int bindex, bstart, bend;
1464 + unionfs_read_lock(dentry->d_sb);
1466 + err = unionfs_file_revalidate(file, true);
1467 + if (unlikely(err))
1469 + unionfs_check_file(file);
1471 + if (!atomic_dec_and_test(&UNIONFS_I(dentry->d_inode)->totalopens))
1474 + unionfs_lock_dentry(dentry);
1476 + bstart = fbstart(file);
1477 + bend = fbend(file);
1478 + for (bindex = bstart; bindex <= bend; bindex++) {
1479 + lower_file = unionfs_lower_file_idx(file, bindex);
1481 + if (lower_file && lower_file->f_op &&
1482 + lower_file->f_op->flush) {
1483 + err = lower_file->f_op->flush(lower_file, id);
1487 + /* if there are no more refs to the dentry, dput it */
1488 + if (d_deleted(dentry)) {
1489 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1490 + unionfs_set_lower_dentry_idx(dentry, bindex,
1497 + /* on success, update our times */
1498 + unionfs_copy_attr_times(dentry->d_inode);
1499 + /* parent time could have changed too (async) */
1500 + unionfs_copy_attr_times(dentry->d_parent->d_inode);
1503 + unionfs_unlock_dentry(dentry);
1505 + unionfs_read_unlock(dentry->d_sb);
1506 + unionfs_check_file(file);
1509 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1510 new file mode 100644
1511 index 0000000..98bed0b
1513 +++ b/fs/unionfs/copyup.c
1516 + * Copyright (c) 2003-2007 Erez Zadok
1517 + * Copyright (c) 2003-2006 Charles P. Wright
1518 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1519 + * Copyright (c) 2005-2006 Junjiro Okajima
1520 + * Copyright (c) 2005 Arun M. Krishnakumar
1521 + * Copyright (c) 2004-2006 David P. Quigley
1522 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1523 + * Copyright (c) 2003 Puja Gupta
1524 + * Copyright (c) 2003 Harikesavan Krishnan
1525 + * Copyright (c) 2003-2007 Stony Brook University
1526 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
1528 + * This program is free software; you can redistribute it and/or modify
1529 + * it under the terms of the GNU General Public License version 2 as
1530 + * published by the Free Software Foundation.
1536 + * For detailed explanation of copyup see:
1537 + * Documentation/filesystems/unionfs/concepts.txt
1540 +#ifdef CONFIG_UNION_FS_XATTR
1541 +/* copyup all extended attrs for a given dentry */
1542 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1543 + struct dentry *new_lower_dentry)
1546 + ssize_t list_size = -1;
1547 + char *name_list = NULL;
1548 + char *attr_value = NULL;
1549 + char *name_list_buf = NULL;
1551 + /* query the actual size of the xattr list */
1552 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1553 + if (list_size <= 0) {
1558 + /* allocate space for the actual list */
1559 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1560 + if (unlikely(!name_list || IS_ERR(name_list))) {
1561 + err = PTR_ERR(name_list);
1565 + name_list_buf = name_list; /* save for kfree at end */
1567 + /* now get the actual xattr list of the source file */
1568 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1569 + if (list_size <= 0) {
1574 + /* allocate space to hold each xattr's value */
1575 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1576 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1577 + err = PTR_ERR(name_list);
1581 + /* in a loop, get and set each xattr from src to dst file */
1582 + while (*name_list) {
1585 + /* Lock here since vfs_getxattr doesn't lock for us */
1586 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1587 + size = vfs_getxattr(old_lower_dentry, name_list,
1588 + attr_value, XATTR_SIZE_MAX);
1589 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1594 + if (size > XATTR_SIZE_MAX) {
1598 + /* Don't lock here since vfs_setxattr does it for us. */
1599 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1602 + * Selinux depends on "security.*" xattrs, so to maintain
1603 + * the security of copied-up files, if Selinux is active,
1604 + * then we must copy these xattrs as well. So we need to
1605 + * temporarily get FOWNER privileges.
1606 + * XXX: move entire copyup code to SIOQ.
1608 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1609 + cap_raise(current->cap_effective, CAP_FOWNER);
1610 + err = vfs_setxattr(new_lower_dentry, name_list,
1611 + attr_value, size, 0);
1612 + cap_lower(current->cap_effective, CAP_FOWNER);
1616 + name_list += strlen(name_list) + 1;
1619 + unionfs_xattr_kfree(name_list_buf);
1620 + unionfs_xattr_kfree(attr_value);
1621 + /* Ignore if xattr isn't supported */
1622 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1626 +#endif /* CONFIG_UNION_FS_XATTR */
1629 + * Determine the mode based on the copyup flags, and the existing dentry.
1631 + * Handle file systems which may not support certain options. For example
1632 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1633 + * errors, rather than propagating them up, which results in copyup errors
1634 + * and errors returned back to users.
1636 +static int copyup_permissions(struct super_block *sb,
1637 + struct dentry *old_lower_dentry,
1638 + struct dentry *new_lower_dentry)
1640 + struct inode *i = old_lower_dentry->d_inode;
1641 + struct iattr newattrs;
1644 + newattrs.ia_atime = i->i_atime;
1645 + newattrs.ia_mtime = i->i_mtime;
1646 + newattrs.ia_ctime = i->i_ctime;
1647 + newattrs.ia_gid = i->i_gid;
1648 + newattrs.ia_uid = i->i_uid;
1649 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1650 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1651 + ATTR_GID | ATTR_UID;
1652 + err = notify_change(new_lower_dentry, &newattrs);
1656 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1657 + newattrs.ia_mode = i->i_mode;
1658 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1659 + err = notify_change(new_lower_dentry, &newattrs);
1660 + if (err == -EOPNOTSUPP &&
1661 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1662 + printk(KERN_WARNING
1663 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1664 + new_lower_dentry->d_name.name);
1673 + * create the new device/file/directory - use copyup_permission to copyup
1676 + * if the object being copied up is a regular file, the file is only created,
1677 + * the contents have to be copied up separately
1679 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1680 + struct dentry *new_lower_dentry,
1681 + struct dentry *new_lower_parent_dentry,
1685 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1686 + struct sioq_args args;
1688 + if (S_ISDIR(old_mode)) {
1689 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1690 + args.mkdir.dentry = new_lower_dentry;
1691 + args.mkdir.mode = old_mode;
1693 + run_sioq(__unionfs_mkdir, &args);
1695 + } else if (S_ISLNK(old_mode)) {
1696 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1697 + args.symlink.dentry = new_lower_dentry;
1698 + args.symlink.symbuf = symbuf;
1699 + args.symlink.mode = old_mode;
1701 + run_sioq(__unionfs_symlink, &args);
1703 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1704 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1705 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1706 + args.mknod.dentry = new_lower_dentry;
1707 + args.mknod.mode = old_mode;
1708 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1710 + run_sioq(__unionfs_mknod, &args);
1712 + } else if (S_ISREG(old_mode)) {
1713 + struct nameidata nd;
1714 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1715 + if (unlikely(err < 0))
1717 + args.create.nd = &nd;
1718 + args.create.parent = new_lower_parent_dentry->d_inode;
1719 + args.create.dentry = new_lower_dentry;
1720 + args.create.mode = old_mode;
1722 + run_sioq(__unionfs_create, &args);
1724 + release_lower_nd(&nd, err);
1726 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1735 +static int __copyup_reg_data(struct dentry *dentry,
1736 + struct dentry *new_lower_dentry, int new_bindex,
1737 + struct dentry *old_lower_dentry, int old_bindex,
1738 + struct file **copyup_file, loff_t len)
1740 + struct super_block *sb = dentry->d_sb;
1741 + struct file *input_file;
1742 + struct file *output_file;
1743 + struct vfsmount *output_mnt;
1744 + mm_segment_t old_fs;
1746 + ssize_t read_bytes, write_bytes;
1750 + /* open old file */
1751 + unionfs_mntget(dentry, old_bindex);
1752 + branchget(sb, old_bindex);
1753 + /* dentry_open calls dput and mntput if it returns an error */
1754 + input_file = dentry_open(old_lower_dentry,
1755 + unionfs_lower_mnt_idx(dentry, old_bindex),
1756 + O_RDONLY | O_LARGEFILE);
1757 + if (IS_ERR(input_file)) {
1758 + dput(old_lower_dentry);
1759 + err = PTR_ERR(input_file);
1762 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1764 + goto out_close_in;
1767 + /* open new file */
1768 + dget(new_lower_dentry);
1769 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
1770 + branchget(sb, new_bindex);
1771 + output_file = dentry_open(new_lower_dentry, output_mnt,
1772 + O_RDWR | O_LARGEFILE);
1773 + if (IS_ERR(output_file)) {
1774 + err = PTR_ERR(output_file);
1775 + goto out_close_in2;
1777 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1779 + goto out_close_out;
1782 + /* allocating a buffer */
1783 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1784 + if (unlikely(!buf)) {
1786 + goto out_close_out;
1789 + input_file->f_pos = 0;
1790 + output_file->f_pos = 0;
1792 + old_fs = get_fs();
1793 + set_fs(KERNEL_DS);
1798 + if (len >= PAGE_SIZE)
1800 + else if ((len < PAGE_SIZE) && (len > 0))
1806 + input_file->f_op->read(input_file,
1807 + (char __user *)buf, size,
1808 + &input_file->f_pos);
1809 + if (read_bytes <= 0) {
1815 + output_file->f_op->write(output_file,
1816 + (char __user *)buf,
1818 + &output_file->f_pos);
1819 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
1820 + err = write_bytes;
1823 + } while ((read_bytes > 0) && (len > 0));
1830 + err = output_file->f_op->fsync(output_file,
1831 + new_lower_dentry, 0);
1834 + goto out_close_out;
1836 + if (copyup_file) {
1837 + *copyup_file = output_file;
1838 + goto out_close_in;
1842 + fput(output_file);
1845 + branchput(sb, new_bindex);
1851 + branchput(sb, old_bindex);
1857 + * dput the lower references for old and new dentry & clear a lower dentry
1860 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
1861 + int old_bstart, int old_bend,
1862 + struct dentry *new_lower_dentry, int new_bindex)
1864 + /* get rid of the lower dentry and all its traces */
1865 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
1866 + set_dbstart(dentry, old_bstart);
1867 + set_dbend(dentry, old_bend);
1869 + dput(new_lower_dentry);
1870 + dput(old_lower_dentry);
1874 + * Copy up a dentry to a file of specified name.
1876 + * @dir: used to pull the ->i_sb to access other branches
1877 + * @dentry: the non-negative dentry whose lower_inode we should copy
1878 + * @bstart: the branch of the lower_inode to copy from
1879 + * @new_bindex: the branch to create the new file in
1880 + * @name: the name of the file to create
1881 + * @namelen: length of @name
1882 + * @copyup_file: the "struct file" to return (optional)
1883 + * @len: how many bytes to copy-up?
1885 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
1886 + int new_bindex, const char *name, int namelen,
1887 + struct file **copyup_file, loff_t len)
1889 + struct dentry *new_lower_dentry;
1890 + struct dentry *old_lower_dentry = NULL;
1891 + struct super_block *sb;
1896 + struct dentry *new_lower_parent_dentry = NULL;
1897 + mm_segment_t oldfs;
1898 + char *symbuf = NULL;
1900 + verify_locked(dentry);
1902 + old_bindex = bstart;
1903 + old_bstart = dbstart(dentry);
1904 + old_bend = dbend(dentry);
1906 + BUG_ON(new_bindex < 0);
1907 + BUG_ON(new_bindex >= old_bindex);
1911 + err = is_robranch_super(sb, new_bindex);
1915 + /* Create the directory structure above this dentry. */
1916 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
1917 + if (IS_ERR(new_lower_dentry)) {
1918 + err = PTR_ERR(new_lower_dentry);
1922 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
1923 + /* we conditionally dput this old_lower_dentry at end of function */
1924 + dget(old_lower_dentry);
1926 + /* For symlinks, we must read the link before we lock the directory. */
1927 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
1929 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
1930 + if (unlikely(!symbuf)) {
1931 + __clear(dentry, old_lower_dentry,
1932 + old_bstart, old_bend,
1933 + new_lower_dentry, new_bindex);
1939 + set_fs(KERNEL_DS);
1940 + err = old_lower_dentry->d_inode->i_op->readlink(
1942 + (char __user *)symbuf,
1946 + __clear(dentry, old_lower_dentry,
1947 + old_bstart, old_bend,
1948 + new_lower_dentry, new_bindex);
1951 + symbuf[err] = '\0';
1954 + /* Now we lock the parent, and create the object in the new branch. */
1955 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
1957 + /* create the new inode */
1958 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
1959 + new_lower_parent_dentry, symbuf);
1962 + __clear(dentry, old_lower_dentry,
1963 + old_bstart, old_bend,
1964 + new_lower_dentry, new_bindex);
1968 + /* We actually copyup the file here. */
1969 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
1970 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
1971 + old_lower_dentry, old_bindex,
1972 + copyup_file, len);
1976 + /* Set permissions. */
1977 + if ((err = copyup_permissions(sb, old_lower_dentry,
1978 + new_lower_dentry)))
1981 +#ifdef CONFIG_UNION_FS_XATTR
1982 + /* Selinux uses extended attributes for permissions. */
1983 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
1986 +#endif /* CONFIG_UNION_FS_XATTR */
1988 + /* do not allow files getting deleted to be re-interposed */
1989 + if (!d_deleted(dentry))
1990 + unionfs_reinterpose(dentry);
1996 + * copyup failed, because we possibly ran out of space or
1997 + * quota, or something else happened so let's unlink; we don't
1998 + * really care about the return value of vfs_unlink
2000 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2002 + if (copyup_file) {
2003 + /* need to close the file */
2005 + fput(*copyup_file);
2006 + branchput(sb, new_bindex);
2010 + * TODO: should we reset the error to something like -EIO?
2012 + * If we don't reset, the user may get some nonsensical errors, but
2013 + * on the other hand, if we reset to EIO, we guarantee that the user
2014 + * will get a "confusing" error message.
2018 + unlock_dir(new_lower_parent_dentry);
2022 + * If old_lower_dentry was a directory, we need to dput it. If it
2023 + * was a file, then it was already dput indirectly by other
2024 + * functions we call above which operate on regular files.
2026 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2027 + (S_ISDIR(old_lower_dentry->d_inode->i_mode) ||
2028 + S_ISLNK(old_lower_dentry->d_inode->i_mode)))
2029 + dput(old_lower_dentry);
2034 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2035 + unionfs_postcopyup_release(dentry);
2036 + if (!unionfs_lower_inode(dentry->d_inode)) {
2038 + * If we got here, then we copied up to an
2039 + * unlinked-open file, whose name is .unionfsXXXXX.
2041 + struct inode *inode = new_lower_dentry->d_inode;
2042 + atomic_inc(&inode->i_count);
2043 + unionfs_set_lower_inode_idx(dentry->d_inode,
2044 + ibstart(dentry->d_inode),
2048 + unionfs_postcopyup_setmnt(dentry);
2049 + /* sync inode times from copied-up inode to our inode */
2050 + unionfs_copy_attr_times(dentry->d_inode);
2051 + unionfs_check_inode(dir);
2052 + unionfs_check_dentry(dentry);
2058 + * This function creates a copy of a file represented by 'file' which
2059 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2060 + * will be named "name".
2062 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2063 + int bstart, int new_bindex, loff_t len)
2066 + struct file *output_file = NULL;
2068 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2069 + name, strlen(name), &output_file, len);
2071 + fbstart(file) = new_bindex;
2072 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2079 + * This function creates a copy of a file represented by 'file' which
2080 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2082 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2083 + int new_bindex, loff_t len)
2086 + struct file *output_file = NULL;
2087 + struct dentry *dentry = file->f_path.dentry;
2089 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2090 + dentry->d_name.name, dentry->d_name.len,
2091 + &output_file, len);
2093 + fbstart(file) = new_bindex;
2094 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2100 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2101 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2102 + int old_bstart, int old_bend)
2106 + int new_bstart = -1;
2107 + int new_bend = -1;
2110 + loop_start = min(old_bstart, bindex);
2111 + loop_end = max(old_bend, bindex);
2114 + * This loop sets the bstart and bend for the new dentry by
2115 + * traversing from left to right. It also dputs all negative
2116 + * dentries except bindex
2118 + for (i = loop_start; i <= loop_end; i++) {
2119 + if (!unionfs_lower_dentry_idx(dentry, i))
2122 + if (i == bindex) {
2124 + if (new_bstart < 0)
2129 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2130 + dput(unionfs_lower_dentry_idx(dentry, i));
2131 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2133 + unionfs_mntput(dentry, i);
2134 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2136 + if (new_bstart < 0)
2142 + if (new_bstart < 0)
2143 + new_bstart = bindex;
2145 + new_bend = bindex;
2146 + set_dbstart(dentry, new_bstart);
2147 + set_dbend(dentry, new_bend);
2151 +/* set lower inode ptr and update bstart & bend if necessary */
2152 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2155 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2156 + igrab(lower->d_inode));
2157 + if (likely(ibstart(upper->d_inode) > bindex))
2158 + ibstart(upper->d_inode) = bindex;
2159 + if (likely(ibend(upper->d_inode) < bindex))
2160 + ibend(upper->d_inode) = bindex;
2164 +/* set lower dentry ptr and update bstart & bend if necessary */
2165 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2168 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2169 + if (likely(dbstart(upper) > bindex))
2170 + set_dbstart(upper, bindex);
2171 + if (likely(dbend(upper) < bindex))
2172 + set_dbend(upper, bindex);
2176 + * This function replicates the directory structure up-to given dentry
2177 + * in the bindex branch.
2179 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2180 + const char *name, int bindex)
2183 + struct dentry *child_dentry;
2184 + struct dentry *parent_dentry;
2185 + struct dentry *lower_parent_dentry = NULL;
2186 + struct dentry *lower_dentry = NULL;
2187 + const char *childname;
2188 + unsigned int childnamelen;
2193 + struct dentry **path = NULL;
2194 + struct super_block *sb;
2196 + verify_locked(dentry);
2198 + err = is_robranch_super(dir->i_sb, bindex);
2200 + lower_dentry = ERR_PTR(err);
2204 + old_bstart = dbstart(dentry);
2205 + old_bend = dbend(dentry);
2207 + lower_dentry = ERR_PTR(-ENOMEM);
2209 + /* There is no sense allocating any less than the minimum. */
2211 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2212 + if (unlikely(!path))
2215 + /* assume the negative dentry of unionfs as the parent dentry */
2216 + parent_dentry = dentry;
2219 + * This loop finds the first parent that exists in the given branch.
2220 + * We start building the directory structure from there. At the end
2221 + * of the loop, the following should hold:
2222 + * - child_dentry is the first nonexistent child
2223 + * - parent_dentry is the first existent parent
2224 + * - path[0] is the = deepest child
2225 + * - path[count] is the first child to create
2228 + child_dentry = parent_dentry;
2230 + /* find the parent directory dentry in unionfs */
2231 + parent_dentry = child_dentry->d_parent;
2232 + unionfs_lock_dentry(parent_dentry);
2234 + /* find out the lower_parent_dentry in the given branch */
2235 + lower_parent_dentry =
2236 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2238 + /* grow path table */
2239 + if (count == nr_dentry) {
2243 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2245 + if (unlikely(!p)) {
2246 + lower_dentry = ERR_PTR(-ENOMEM);
2252 + /* store the child dentry */
2253 + path[count++] = child_dentry;
2254 + } while (!lower_parent_dentry);
2257 + sb = dentry->d_sb;
2260 + * This code goes between the begin/end labels and basically
2261 + * emulates a while(child_dentry != dentry), only cleaner and
2262 + * shorter than what would be a much longer while loop.
2265 + /* get lower parent dir in the current branch */
2266 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2267 + unionfs_unlock_dentry(parent_dentry);
2269 + /* init the values to lookup */
2270 + childname = child_dentry->d_name.name;
2271 + childnamelen = child_dentry->d_name.len;
2273 + if (child_dentry != dentry) {
2274 + /* lookup child in the underlying file system */
2275 + lower_dentry = lookup_one_len(childname, lower_parent_dentry,
2277 + if (IS_ERR(lower_dentry))
2281 + * Is the name a whiteout of the child name ? lookup the
2282 + * whiteout child in the underlying file system
2284 + lower_dentry = lookup_one_len(name, lower_parent_dentry,
2286 + if (IS_ERR(lower_dentry))
2289 + /* Replace the current dentry (if any) with the new one */
2290 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2291 + unionfs_set_lower_dentry_idx(dentry, bindex,
2294 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2298 + if (lower_dentry->d_inode) {
2300 + * since this already exists we dput to avoid
2301 + * multiple references on the same dentry
2303 + dput(lower_dentry);
2305 + struct sioq_args args;
2307 + /* it's a negative dentry, create a new dir */
2308 + lower_parent_dentry = lock_parent(lower_dentry);
2310 + args.mkdir.parent = lower_parent_dentry->d_inode;
2311 + args.mkdir.dentry = lower_dentry;
2312 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2314 + run_sioq(__unionfs_mkdir, &args);
2318 + err = copyup_permissions(dir->i_sb, child_dentry,
2320 + unlock_dir(lower_parent_dentry);
2322 + struct inode *inode = lower_dentry->d_inode;
2324 + * If we get here, it means that we created a new
2325 + * dentry+inode, but copying permissions failed.
2326 + * Therefore, we should delete this inode and dput
2327 + * the dentry so as not to leave cruft behind.
2329 + if (lower_dentry->d_op && lower_dentry->d_op->d_iput)
2330 + lower_dentry->d_op->d_iput(lower_dentry,
2334 + lower_dentry->d_inode = NULL;
2335 + dput(lower_dentry);
2336 + lower_dentry = ERR_PTR(err);
2342 + __set_inode(child_dentry, lower_dentry, bindex);
2343 + __set_dentry(child_dentry, lower_dentry, bindex);
2345 + * update times of this dentry, but also the parent, because if
2346 + * we changed, the parent may have changed too.
2348 + unionfs_copy_attr_times(parent_dentry->d_inode);
2349 + unionfs_copy_attr_times(child_dentry->d_inode);
2351 + parent_dentry = child_dentry;
2352 + child_dentry = path[--count];
2355 + /* cleanup any leftover locks from the do/while loop above */
2356 + if (IS_ERR(lower_dentry))
2358 + unionfs_unlock_dentry(path[count--]);
2360 + return lower_dentry;
2364 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2365 + * dentry+parents to the first parent node that has an mnt.
2367 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2369 + struct dentry *parent, *hasone;
2370 + int bindex = dbstart(dentry);
2372 + if (unionfs_lower_mnt_idx(dentry, bindex))
2374 + hasone = dentry->d_parent;
2375 + /* this loop should stop at root dentry */
2376 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2377 + hasone = hasone->d_parent;
2379 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2380 + unionfs_set_lower_mnt_idx(parent, bindex,
2381 + unionfs_mntget(hasone, bindex));
2382 + parent = parent->d_parent;
2387 + * Post-copyup helper to release all non-directory source objects of a
2388 + * copied-up file. Regular files should have only one lower object.
2390 +void unionfs_postcopyup_release(struct dentry *dentry)
2394 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2395 + for (bindex = dbstart(dentry)+1; bindex <= dbend(dentry); bindex++) {
2396 + if (unionfs_lower_mnt_idx(dentry, bindex)) {
2397 + unionfs_mntput(dentry, bindex);
2398 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
2400 + if (unionfs_lower_dentry_idx(dentry, bindex)) {
2401 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2402 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
2403 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
2404 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
2408 + bindex = dbstart(dentry);
2409 + set_dbend(dentry, bindex);
2410 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bindex;
2412 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2413 new file mode 100644
2414 index 0000000..68692d7
2416 +++ b/fs/unionfs/debug.c
2419 + * Copyright (c) 2003-2007 Erez Zadok
2420 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2421 + * Copyright (c) 2003-2007 Stony Brook University
2422 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
2424 + * This program is free software; you can redistribute it and/or modify
2425 + * it under the terms of the GNU General Public License version 2 as
2426 + * published by the Free Software Foundation.
2432 + * Helper debugging functions for maintainers (and for users to report back
2433 + * useful information back to maintainers)
2436 +/* it's always useful to know what part of the code called us */
2437 +#define PRINT_CALLER(fname, fxn, line) \
2439 + if (!printed_caller) { \
2440 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2441 + printed_caller = 1; \
2446 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2447 + * the fan-out of various Unionfs objects. We check that no lower objects
2448 + * exist outside the start/end branch range; that all objects within are
2449 + * non-NULL (with some allowed exceptions); that for every lower file
2450 + * there's a lower dentry+inode; that the start/end ranges match for all
2451 + * corresponding lower objects; that open files/symlinks have only one lower
2452 + * objects, but directories can have several; and more.
2454 +void __unionfs_check_inode(const struct inode *inode,
2455 + const char *fname, const char *fxn, int line)
2459 + struct inode *lower_inode;
2460 + struct super_block *sb;
2461 + int printed_caller = 0;
2464 + /* for inodes now */
2467 + istart = ibstart(inode);
2468 + iend = ibend(inode);
2469 + if (unlikely(istart > iend)) {
2470 + PRINT_CALLER(fname, fxn, line);
2471 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2472 + inode, istart, iend);
2474 + if (unlikely((istart == -1 && iend != -1) ||
2475 + (istart != -1 && iend == -1))) {
2476 + PRINT_CALLER(fname, fxn, line);
2477 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2478 + inode, istart, iend);
2480 + if (!S_ISDIR(inode->i_mode)) {
2481 + if (unlikely(iend != istart)) {
2482 + PRINT_CALLER(fname, fxn, line);
2483 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2484 + inode, istart, iend);
2488 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2489 + if (unlikely(!UNIONFS_I(inode))) {
2490 + PRINT_CALLER(fname, fxn, line);
2491 + pr_debug(" Ci3: no inode_info %p\n", inode);
2494 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2495 + PRINT_CALLER(fname, fxn, line);
2496 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2499 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2500 + if (lower_inode) {
2501 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2502 + if (unlikely(bindex < istart || bindex > iend)) {
2503 + PRINT_CALLER(fname, fxn, line);
2504 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2505 + "istart/end=%d:%d\n", inode,
2506 + lower_inode, bindex, istart, iend);
2507 + } else if (unlikely(lower_inode == poison_ptr)) {
2508 + /* freed inode! */
2509 + PRINT_CALLER(fname, fxn, line);
2510 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2511 + "istart/end=%d:%d\n", inode,
2512 + lower_inode, bindex, istart, iend);
2514 + } else { /* lower_inode == NULL */
2515 + if (bindex >= istart && bindex <= iend) {
2517 + * directories can have NULL lower inodes in
2518 + * b/t start/end, but NOT if at the
2519 + * start/end range.
2521 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
2522 + bindex > istart &&
2523 + bindex < iend))) {
2524 + PRINT_CALLER(fname, fxn, line);
2525 + pr_debug(" Ci7: inode/linode=%p:%p "
2526 + "bindex=%d istart/end=%d:%d\n",
2527 + inode, lower_inode, bindex,
2535 +void __unionfs_check_dentry(const struct dentry *dentry,
2536 + const char *fname, const char *fxn, int line)
2539 + int dstart, dend, istart, iend;
2540 + struct dentry *lower_dentry;
2541 + struct inode *inode, *lower_inode;
2542 + struct super_block *sb;
2543 + struct vfsmount *lower_mnt;
2544 + int printed_caller = 0;
2548 + sb = dentry->d_sb;
2549 + inode = dentry->d_inode;
2550 + dstart = dbstart(dentry);
2551 + dend = dbend(dentry);
2552 + BUG_ON(dstart > dend);
2554 + if (unlikely((dstart == -1 && dend != -1) ||
2555 + (dstart != -1 && dend == -1))) {
2556 + PRINT_CALLER(fname, fxn, line);
2557 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2558 + dentry, dstart, dend);
2561 + * check for NULL dentries inside the start/end range, or
2562 + * non-NULL dentries outside the start/end range.
2564 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2565 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2566 + if (lower_dentry) {
2567 + if (unlikely(bindex < dstart || bindex > dend)) {
2568 + PRINT_CALLER(fname, fxn, line);
2569 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2570 + "bindex=%d dstart/end=%d:%d\n",
2571 + dentry, lower_dentry,
2572 + (lower_dentry ? lower_dentry->d_inode :
2574 + bindex, dstart, dend);
2576 + } else { /* lower_dentry == NULL */
2577 + if (bindex < dstart || bindex > dend)
2580 + * Directories can have NULL lower inodes in b/t
2581 + * start/end, but NOT if at the start/end range.
2582 + * Ignore this rule, however, if this is a NULL
2583 + * dentry or a deleted dentry.
2585 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2587 + !(inode && S_ISDIR(inode->i_mode) &&
2588 + bindex > dstart && bindex < dend))) {
2589 + PRINT_CALLER(fname, fxn, line);
2590 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2591 + "bindex=%d dstart/end=%d:%d\n",
2592 + dentry, lower_dentry,
2594 + lower_dentry->d_inode :
2596 + bindex, dstart, dend);
2601 + /* check for vfsmounts same as for dentries */
2602 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2603 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2605 + if (unlikely(bindex < dstart || bindex > dend)) {
2606 + PRINT_CALLER(fname, fxn, line);
2607 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2608 + "dstart/end=%d:%d\n", dentry,
2609 + lower_mnt, bindex, dstart, dend);
2611 + } else { /* lower_mnt == NULL */
2612 + if (bindex < dstart || bindex > dend)
2615 + * Directories can have NULL lower inodes in b/t
2616 + * start/end, but NOT if at the start/end range.
2617 + * Ignore this rule, however, if this is a NULL
2620 + if (unlikely(inode &&
2621 + !(inode && S_ISDIR(inode->i_mode) &&
2622 + bindex > dstart && bindex < dend))) {
2623 + PRINT_CALLER(fname, fxn, line);
2624 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2625 + "bindex=%d dstart/end=%d:%d\n",
2626 + dentry, lower_mnt, bindex,
2632 + /* for inodes now */
2635 + istart = ibstart(inode);
2636 + iend = ibend(inode);
2637 + BUG_ON(istart > iend);
2638 + if (unlikely((istart == -1 && iend != -1) ||
2639 + (istart != -1 && iend == -1))) {
2640 + PRINT_CALLER(fname, fxn, line);
2641 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2642 + dentry, inode, istart, iend);
2644 + if (unlikely(istart != dstart)) {
2645 + PRINT_CALLER(fname, fxn, line);
2646 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2647 + dentry, inode, istart, dstart);
2649 + if (unlikely(iend != dend)) {
2650 + PRINT_CALLER(fname, fxn, line);
2651 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2652 + dentry, inode, iend, dend);
2655 + if (!S_ISDIR(inode->i_mode)) {
2656 + if (unlikely(dend != dstart)) {
2657 + PRINT_CALLER(fname, fxn, line);
2658 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2659 + dentry, inode, dstart, dend);
2661 + if (unlikely(iend != istart)) {
2662 + PRINT_CALLER(fname, fxn, line);
2663 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2664 + dentry, inode, istart, iend);
2668 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2669 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2670 + if (lower_inode) {
2671 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2672 + if (unlikely(bindex < istart || bindex > iend)) {
2673 + PRINT_CALLER(fname, fxn, line);
2674 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2675 + "istart/end=%d:%d\n", dentry,
2676 + lower_inode, bindex, istart, iend);
2677 + } else if (unlikely(lower_inode == poison_ptr)) {
2678 + /* freed inode! */
2679 + PRINT_CALLER(fname, fxn, line);
2680 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2681 + "istart/end=%d:%d\n", dentry,
2682 + lower_inode, bindex, istart, iend);
2684 + } else { /* lower_inode == NULL */
2685 + if (bindex >= istart && bindex <= iend) {
2687 + * directories can have NULL lower inodes in
2688 + * b/t start/end, but NOT if at the
2689 + * start/end range.
2691 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
2692 + bindex > istart &&
2693 + bindex < iend))) {
2694 + PRINT_CALLER(fname, fxn, line);
2695 + pr_debug(" CI7: dentry/linode=%p:%p "
2696 + "bindex=%d istart/end=%d:%d\n",
2697 + dentry, lower_inode, bindex,
2705 + * If it's a directory, then intermediate objects b/t start/end can
2706 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2709 + if (S_ISDIR(inode->i_mode))
2710 + for (bindex = dstart+1; bindex < dend; bindex++) {
2711 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2712 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2714 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2715 + if (unlikely(!((lower_inode && lower_dentry &&
2718 + !lower_dentry && !lower_mnt)))) {
2719 + PRINT_CALLER(fname, fxn, line);
2720 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2721 + "bindex=%d dstart/end=%d:%d\n",
2722 + lower_mnt, lower_dentry, lower_inode,
2723 + bindex, dstart, dend);
2726 + /* check if lower inode is newer than upper one (it shouldn't) */
2727 + if (unlikely(is_newer_lower(dentry))) {
2728 + PRINT_CALLER(fname, fxn, line);
2729 + for (bindex = ibstart(inode); bindex <= ibend(inode);
2731 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2732 + if (unlikely(!lower_inode))
2734 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2735 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
2737 + inode->i_mtime.tv_sec,
2738 + inode->i_mtime.tv_nsec,
2739 + lower_inode->i_mtime.tv_sec,
2740 + lower_inode->i_mtime.tv_nsec,
2741 + inode->i_ctime.tv_sec,
2742 + inode->i_ctime.tv_nsec,
2743 + lower_inode->i_ctime.tv_sec,
2744 + lower_inode->i_ctime.tv_nsec);
2749 +void __unionfs_check_file(const struct file *file,
2750 + const char *fname, const char *fxn, int line)
2753 + int dstart, dend, fstart, fend;
2754 + struct dentry *dentry;
2755 + struct file *lower_file;
2756 + struct inode *inode;
2757 + struct super_block *sb;
2758 + int printed_caller = 0;
2761 + dentry = file->f_path.dentry;
2762 + sb = dentry->d_sb;
2763 + dstart = dbstart(dentry);
2764 + dend = dbend(dentry);
2765 + BUG_ON(dstart > dend);
2766 + fstart = fbstart(file);
2767 + fend = fbend(file);
2768 + BUG_ON(fstart > fend);
2770 + if (unlikely((fstart == -1 && fend != -1) ||
2771 + (fstart != -1 && fend == -1))) {
2772 + PRINT_CALLER(fname, fxn, line);
2773 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
2774 + file, dentry, fstart, fend);
2776 + if (unlikely(fstart != dstart)) {
2777 + PRINT_CALLER(fname, fxn, line);
2778 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
2779 + file, dentry, fstart, dstart);
2781 + if (unlikely(fend != dend)) {
2782 + PRINT_CALLER(fname, fxn, line);
2783 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
2784 + file, dentry, fend, dend);
2786 + inode = dentry->d_inode;
2787 + if (!S_ISDIR(inode->i_mode)) {
2788 + if (unlikely(fend != fstart)) {
2789 + PRINT_CALLER(fname, fxn, line);
2790 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
2791 + file, inode, fstart, fend);
2793 + if (unlikely(dend != dstart)) {
2794 + PRINT_CALLER(fname, fxn, line);
2795 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
2796 + file, dentry, dstart, dend);
2801 + * check for NULL dentries inside the start/end range, or
2802 + * non-NULL dentries outside the start/end range.
2804 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2805 + lower_file = unionfs_lower_file_idx(file, bindex);
2807 + if (unlikely(bindex < fstart || bindex > fend)) {
2808 + PRINT_CALLER(fname, fxn, line);
2809 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
2810 + "fstart/end=%d:%d\n", file,
2811 + lower_file, bindex, fstart, fend);
2813 + } else { /* lower_file == NULL */
2814 + if (bindex >= fstart && bindex <= fend) {
2816 + * directories can have NULL lower inodes in
2817 + * b/t start/end, but NOT if at the
2818 + * start/end range.
2820 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
2821 + bindex > fstart &&
2822 + bindex < fend))) {
2823 + PRINT_CALLER(fname, fxn, line);
2824 + pr_debug(" CF6: file/lower=%p:%p "
2825 + "bindex=%d fstart/end=%d:%d\n",
2826 + file, lower_file, bindex,
2833 + __unionfs_check_dentry(dentry, fname, fxn, line);
2836 +void __unionfs_check_nd(const struct nameidata *nd,
2837 + const char *fname, const char *fxn, int line)
2839 + struct file *file;
2840 + int printed_caller = 0;
2842 + if (unlikely(!nd))
2844 + if (nd->flags & LOOKUP_OPEN) {
2845 + file = nd->intent.open.file;
2846 + if (unlikely(file->f_path.dentry &&
2847 + strcmp(file->f_dentry->d_sb->s_type->name,
2849 + PRINT_CALLER(fname, fxn, line);
2850 + pr_debug(" CND1: lower_file of type %s\n",
2851 + file->f_path.dentry->d_sb->s_type->name);
2857 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
2858 +void __show_branch_counts(const struct super_block *sb,
2859 + const char *file, const char *fxn, int line)
2862 + struct vfsmount *mnt;
2865 + for (i = 0; i < sbmax(sb); i++) {
2866 + if (likely(sb->s_root))
2867 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
2870 + pr_debug("%d:", (mnt ? atomic_read(&mnt->mnt_count) : -99));
2872 + pr_debug("%s:%s:%d\n", file, fxn, line);
2875 +void __show_inode_times(const struct inode *inode,
2876 + const char *file, const char *fxn, int line)
2878 + struct inode *lower_inode;
2881 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
2882 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2883 + if (unlikely(!lower_inode))
2885 + pr_debug("IT(%lu:%d): ", inode->i_ino, bindex);
2886 + pr_debug("%s:%s:%d ", file, fxn, line);
2887 + pr_debug("um=%lu/%lu lm=%lu/%lu ",
2888 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2889 + lower_inode->i_mtime.tv_sec,
2890 + lower_inode->i_mtime.tv_nsec);
2891 + pr_debug("uc=%lu/%lu lc=%lu/%lu\n",
2892 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2893 + lower_inode->i_ctime.tv_sec,
2894 + lower_inode->i_ctime.tv_nsec);
2898 +void __show_dinode_times(const struct dentry *dentry,
2899 + const char *file, const char *fxn, int line)
2901 + struct inode *inode = dentry->d_inode;
2902 + struct inode *lower_inode;
2905 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
2906 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2909 + pr_debug("DT(%s:%lu:%d): ", dentry->d_name.name, inode->i_ino,
2911 + pr_debug("%s:%s:%d ", file, fxn, line);
2912 + pr_debug("um=%lu/%lu lm=%lu/%lu ",
2913 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2914 + lower_inode->i_mtime.tv_sec,
2915 + lower_inode->i_mtime.tv_nsec);
2916 + pr_debug("uc=%lu/%lu lc=%lu/%lu\n",
2917 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2918 + lower_inode->i_ctime.tv_sec,
2919 + lower_inode->i_ctime.tv_nsec);
2923 +void __show_inode_counts(const struct inode *inode,
2924 + const char *file, const char *fxn, int line)
2926 + struct inode *lower_inode;
2929 + if (unlikely(!inode)) {
2930 + pr_debug("SiC: Null inode\n");
2933 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
2935 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2936 + if (unlikely(!lower_inode))
2938 + pr_debug("SIC(%lu:%d:%d): ", inode->i_ino, bindex,
2939 + atomic_read(&(inode)->i_count));
2940 + pr_debug("lc=%d ", atomic_read(&(lower_inode)->i_count));
2941 + pr_debug("%s:%s:%d\n", file, fxn, line);
2944 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
2945 new file mode 100644
2946 index 0000000..6bab9d6
2948 +++ b/fs/unionfs/dentry.c
2951 + * Copyright (c) 2003-2007 Erez Zadok
2952 + * Copyright (c) 2003-2006 Charles P. Wright
2953 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2954 + * Copyright (c) 2005-2006 Junjiro Okajima
2955 + * Copyright (c) 2005 Arun M. Krishnakumar
2956 + * Copyright (c) 2004-2006 David P. Quigley
2957 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
2958 + * Copyright (c) 2003 Puja Gupta
2959 + * Copyright (c) 2003 Harikesavan Krishnan
2960 + * Copyright (c) 2003-2007 Stony Brook University
2961 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
2963 + * This program is free software; you can redistribute it and/or modify
2964 + * it under the terms of the GNU General Public License version 2 as
2965 + * published by the Free Software Foundation.
2971 + * Revalidate a single dentry.
2972 + * Assume that dentry's info node is locked.
2973 + * Assume that parent(s) are all valid already, but
2974 + * the child may not yet be valid.
2975 + * Returns true if valid, false otherwise.
2977 +static bool __unionfs_d_revalidate_one(struct dentry *dentry,
2978 + struct nameidata *nd)
2980 + bool valid = true; /* default is valid */
2981 + struct dentry *lower_dentry;
2982 + int bindex, bstart, bend;
2986 + int interpose_flag;
2987 + struct nameidata lowernd; /* TODO: be gentler to the stack */
2990 + memcpy(&lowernd, nd, sizeof(struct nameidata));
2992 + memset(&lowernd, 0, sizeof(struct nameidata));
2994 + verify_locked(dentry);
2996 + /* if the dentry is unhashed, do NOT revalidate */
2997 + if (d_deleted(dentry)) {
2998 + pr_debug("unionfs: unhashed dentry being "
2999 + "revalidated: %*s\n",
3000 + dentry->d_name.len, dentry->d_name.name);
3004 + BUG_ON(dbstart(dentry) == -1);
3005 + if (dentry->d_inode)
3007 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3008 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3010 + * If we are working on an unconnected dentry, then there is no
3011 + * revalidation to be done, because this file does not exist within
3012 + * the namespace, and Unionfs operates on the namespace, not data.
3014 + if (unlikely(sbgen != dgen)) {
3015 + struct dentry *result;
3018 + /* The root entry should always be valid */
3019 + BUG_ON(IS_ROOT(dentry));
3021 + /* We can't work correctly if our parent isn't valid. */
3022 + pdgen = atomic_read(&UNIONFS_D(dentry->d_parent)->generation);
3023 + BUG_ON(pdgen != sbgen); /* should never happen here */
3025 + /* Free the pointers for our inodes and this dentry. */
3026 + bstart = dbstart(dentry);
3027 + bend = dbend(dentry);
3028 + if (bstart >= 0) {
3029 + struct dentry *lower_dentry;
3030 + for (bindex = bstart; bindex <= bend; bindex++) {
3032 + unionfs_lower_dentry_idx(dentry,
3034 + dput(lower_dentry);
3037 + set_dbstart(dentry, -1);
3038 + set_dbend(dentry, -1);
3040 + interpose_flag = INTERPOSE_REVAL_NEG;
3042 + interpose_flag = INTERPOSE_REVAL;
3044 + * During BRM, the VFS could already hold a lock on
3045 + * a file being read, so don't lock it again
3046 + * (deadlock), but if you lock it in this function,
3047 + * then release it here too.
3049 + if (!mutex_is_locked(&dentry->d_inode->i_mutex)) {
3050 + mutex_lock(&dentry->d_inode->i_mutex);
3054 + bstart = ibstart(dentry->d_inode);
3055 + bend = ibend(dentry->d_inode);
3056 + if (bstart >= 0) {
3057 + struct inode *lower_inode;
3058 + for (bindex = bstart; bindex <= bend;
3061 + unionfs_lower_inode_idx(
3064 + iput(lower_inode);
3067 + kfree(UNIONFS_I(dentry->d_inode)->lower_inodes);
3068 + UNIONFS_I(dentry->d_inode)->lower_inodes = NULL;
3069 + ibstart(dentry->d_inode) = -1;
3070 + ibend(dentry->d_inode) = -1;
3072 + mutex_unlock(&dentry->d_inode->i_mutex);
3075 + result = unionfs_lookup_backend(dentry, &lowernd,
3078 + if (IS_ERR(result)) {
3083 + * current unionfs_lookup_backend() doesn't return
3090 + if (unlikely(positive && UNIONFS_I(dentry->d_inode)->stale)) {
3091 + make_bad_inode(dentry->d_inode);
3099 + /* The revalidation must occur across all branches */
3100 + bstart = dbstart(dentry);
3101 + bend = dbend(dentry);
3102 + BUG_ON(bstart == -1);
3103 + for (bindex = bstart; bindex <= bend; bindex++) {
3104 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3105 + if (!lower_dentry || !lower_dentry->d_op
3106 + || !lower_dentry->d_op->d_revalidate)
3109 + * Don't pass nameidata to lower file system, because we
3110 + * don't want an arbitrary lower file being opened or
3111 + * returned to us: it may be useless to us because of the
3112 + * fanout nature of unionfs (cf. file/directory open-file
3113 + * invariants). We will open lower files as and when needed
3116 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3120 + if (!dentry->d_inode)
3125 + * If we get here, and we copy the meta-data from the lower
3126 + * inode to our inode, then it is vital that we have already
3127 + * purged all unionfs-level file data. We do that in the
3128 + * caller (__unionfs_d_revalidate_chain) by calling
3129 + * purge_inode_data.
3131 + unionfs_copy_attr_all(dentry->d_inode,
3132 + unionfs_lower_inode(dentry->d_inode));
3133 + fsstack_copy_inode_size(dentry->d_inode,
3134 + unionfs_lower_inode(dentry->d_inode));
3142 + * Determine if the lower inode objects have changed from below the unionfs
3143 + * inode. Return true if changed, false otherwise.
3145 +bool is_newer_lower(const struct dentry *dentry)
3148 + struct inode *inode;
3149 + struct inode *lower_inode;
3151 + /* ignore if we're called on semi-initialized dentries/inodes */
3152 + if (!dentry || !UNIONFS_D(dentry))
3154 + inode = dentry->d_inode;
3155 + if (!inode || !UNIONFS_I(inode) ||
3156 + ibstart(inode) < 0 || ibend(inode) < 0)
3159 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3160 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3164 + * We may want to apply other tests to determine if the
3165 + * lower inode's data has changed, but checking for changed
3166 + * ctime and mtime on the lower inode should be enough.
3168 + if (unlikely(timespec_compare(&inode->i_mtime,
3169 + &lower_inode->i_mtime) < 0)) {
3170 + pr_info("unionfs: new lower inode mtime "
3171 + "(bindex=%d, name=%s)\n", bindex,
3172 + dentry->d_name.name);
3173 + show_dinode_times(dentry);
3174 + return true; /* mtime changed! */
3176 + if (unlikely(timespec_compare(&inode->i_ctime,
3177 + &lower_inode->i_ctime) < 0)) {
3178 + pr_info("unionfs: new lower inode ctime "
3179 + "(bindex=%d, name=%s)\n", bindex,
3180 + dentry->d_name.name);
3181 + show_dinode_times(dentry);
3182 + return true; /* ctime changed! */
3185 + return false; /* default: lower is not newer */
3189 + * Purge/remove/unmap all date pages of a unionfs inode. This is called
3190 + * when the lower inode has changed, and we have to force processes to get
3193 + * XXX: Our implementation works in that as long as a user process will have
3194 + * caused Unionfs to be called, directly or indirectly, even to just do
3195 + * ->d_revalidate; then we will have purged the current Unionfs data and the
3196 + * process will see the new data. For example, a process that continually
3197 + * re-reads the same file's data will see the NEW data as soon as the lower
3198 + * file had changed, upon the next read(2) syscall (even if the file is
3199 + * still open!) However, this doesn't work when the process re-reads the
3200 + * open file's data via mmap(2) (unless the user unmaps/closes the file and
3201 + * remaps/reopens it). Once we respond to ->readpage(s), then the kernel
3202 + * maps the page into the process's address space and there doesn't appear
3203 + * to be a way to force the kernel to invalidate those pages/mappings, and
3204 + * force the process to re-issue ->readpage. If there's a way to invalidate
3205 + * active mappings and force a ->readpage, let us know please
3206 + * (invalidate_inode_pages2 doesn't do the trick).
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);
3213 + if (inode->i_data.nrpages)
3214 + truncate_inode_pages(&inode->i_data, 0);
3218 + * Revalidate a parent chain of dentries, then the actual node.
3219 + * Assumes that dentry is locked, but will lock all parents if/when needed.
3221 + * If 'willwrite' is true, and the lower inode times are not in sync, then
3222 + * *don't* purge_inode_data, as it could deadlock if ->write calls us and we
3223 + * try to truncate a locked page. Besides, if unionfs is about to write
3224 + * data to a file, then there's the data unionfs is about to write is more
3225 + * authoritative than what's below, therefore we can safely overwrite the
3226 + * lower inode times and data.
3228 +bool __unionfs_d_revalidate_chain(struct dentry *dentry, struct nameidata *nd,
3231 + bool valid = false; /* default is invalid */
3232 + struct dentry **chain = NULL; /* chain of dentries to reval */
3233 + int chain_len = 0;
3234 + struct dentry *dtmp;
3235 + int sbgen, dgen, i;
3236 + int saved_bstart, saved_bend, bindex;
3238 + /* find length of chain needed to revalidate */
3239 + /* XXX: should I grab some global (dcache?) lock? */
3241 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3242 + dtmp = dentry->d_parent;
3243 + dgen = atomic_read(&UNIONFS_D(dtmp)->generation);
3244 + /* XXX: should we check if is_newer_lower all the way up? */
3245 + if (unlikely(is_newer_lower(dtmp))) {
3247 + * Special case: the root dentry's generation number must
3248 + * always be valid, but its lower inode times don't have to
3249 + * be, so sync up the times only.
3251 + if (IS_ROOT(dtmp)) {
3252 + unionfs_copy_attr_times(dtmp->d_inode);
3255 + * reset generation number to zero, guaranteed to be
3259 + atomic_set(&UNIONFS_D(dtmp)->generation, dgen);
3261 + purge_inode_data(dtmp->d_inode);
3263 + while (sbgen != dgen) {
3264 + /* The root entry should always be valid */
3265 + BUG_ON(IS_ROOT(dtmp));
3267 + dtmp = dtmp->d_parent;
3268 + dgen = atomic_read(&UNIONFS_D(dtmp)->generation);
3270 + if (chain_len == 0)
3271 + goto out_this; /* shortcut if parents are OK */
3274 + * Allocate array of dentries to reval. We could use linked lists,
3275 + * but the number of entries we need to alloc here is often small,
3276 + * and short lived, so locality will be better.
3278 + chain = kzalloc(chain_len * sizeof(struct dentry *), GFP_KERNEL);
3279 + if (unlikely(!chain)) {
3280 + printk(KERN_CRIT "unionfs: no more memory in %s\n",
3286 + * lock all dentries in chain, in child to parent order.
3287 + * if failed, then sleep for a little, then retry.
3289 + dtmp = dentry->d_parent;
3290 + for (i = chain_len-1; i >= 0; i--) {
3291 + chain[i] = dget(dtmp);
3292 + dtmp = dtmp->d_parent;
3296 + * call __unionfs_d_revalidate_one() on each dentry, but in parent
3299 + for (i = 0; i < chain_len; i++) {
3300 + unionfs_lock_dentry(chain[i]);
3301 + saved_bstart = dbstart(chain[i]);
3302 + saved_bend = dbend(chain[i]);
3303 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3304 + dgen = atomic_read(&UNIONFS_D(chain[i])->generation);
3306 + valid = __unionfs_d_revalidate_one(chain[i], nd);
3307 + /* XXX: is this the correct mntput condition?! */
3308 + if (valid && chain_len > 0 &&
3309 + sbgen != dgen && chain[i]->d_inode &&
3310 + S_ISDIR(chain[i]->d_inode->i_mode)) {
3311 + for (bindex = saved_bstart; bindex <= saved_bend;
3313 + unionfs_mntput(chain[i], bindex);
3315 + unionfs_unlock_dentry(chain[i]);
3317 + if (unlikely(!valid))
3323 + /* finally, lock this dentry and revalidate it */
3324 + verify_locked(dentry);
3325 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3327 + if (unlikely(is_newer_lower(dentry))) {
3328 + /* root dentry special case as aforementioned */
3329 + if (IS_ROOT(dentry)) {
3330 + unionfs_copy_attr_times(dentry->d_inode);
3333 + * reset generation number to zero, guaranteed to be
3337 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3340 + purge_inode_data(dentry->d_inode);
3342 + valid = __unionfs_d_revalidate_one(dentry, nd);
3345 + * If __unionfs_d_revalidate_one() succeeded above, then it will
3346 + * have incremented the refcnt of the mnt's, but also the branch
3347 + * indices of the dentry will have been updated (to take into
3348 + * account any branch insertions/deletion. So the current
3349 + * dbstart/dbend match the current, and new, indices of the mnts
3350 + * which __unionfs_d_revalidate_one has incremented. Note: the "if"
3351 + * test below does not depend on whether chain_len was 0 or greater.
3353 + if (valid && sbgen != dgen)
3354 + for (bindex = dbstart(dentry);
3355 + bindex <= dbend(dentry);
3357 + unionfs_mntput(dentry, bindex);
3360 + /* unlock/dput all dentries in chain and return status */
3361 + if (chain_len > 0) {
3362 + for (i = 0; i < chain_len; i++)
3370 +static int unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
3374 + unionfs_read_lock(dentry->d_sb);
3376 + unionfs_lock_dentry(dentry);
3377 + err = __unionfs_d_revalidate_chain(dentry, nd, false);
3378 + unionfs_unlock_dentry(dentry);
3379 + if (likely(err > 0)) { /* true==1: dentry is valid */
3380 + unionfs_check_dentry(dentry);
3381 + unionfs_check_nd(nd);
3384 + unionfs_read_unlock(dentry->d_sb);
3390 + * At this point no one can reference this dentry, so we don't have to be
3391 + * careful about concurrent access.
3393 +static void unionfs_d_release(struct dentry *dentry)
3395 + int bindex, bstart, bend;
3397 + unionfs_read_lock(dentry->d_sb);
3399 + unionfs_check_dentry(dentry);
3400 + /* this could be a negative dentry, so check first */
3401 + if (unlikely(!UNIONFS_D(dentry))) {
3402 + printk(KERN_ERR "unionfs: dentry without private data: %.*s\n",
3403 + dentry->d_name.len, dentry->d_name.name);
3405 + } else if (dbstart(dentry) < 0) {
3406 + /* this is due to a failed lookup */
3407 + printk(KERN_ERR "unionfs: dentry without lower "
3408 + "dentries: %.*s\n",
3409 + dentry->d_name.len, dentry->d_name.name);
3413 + /* Release all the lower dentries */
3414 + bstart = dbstart(dentry);
3415 + bend = dbend(dentry);
3416 + for (bindex = bstart; bindex <= bend; bindex++) {
3417 + dput(unionfs_lower_dentry_idx(dentry, bindex));
3418 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3419 + /* NULL lower mnt is ok if this is a negative dentry */
3420 + if (!dentry->d_inode && !unionfs_lower_mnt_idx(dentry, bindex))
3422 + unionfs_mntput(dentry, bindex);
3423 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
3425 + /* free private data (unionfs_dentry_info) here */
3426 + kfree(UNIONFS_D(dentry)->lower_paths);
3427 + UNIONFS_D(dentry)->lower_paths = NULL;
3430 + /* No need to unlock it, because it is disappeared. */
3431 + free_dentry_private_data(dentry);
3434 + unionfs_read_unlock(dentry->d_sb);
3438 +struct dentry_operations unionfs_dops = {
3439 + .d_revalidate = unionfs_d_revalidate,
3440 + .d_release = unionfs_d_release,
3442 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3443 new file mode 100644
3444 index 0000000..c644c13
3446 +++ b/fs/unionfs/dirfops.c
3449 + * Copyright (c) 2003-2007 Erez Zadok
3450 + * Copyright (c) 2003-2006 Charles P. Wright
3451 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3452 + * Copyright (c) 2005-2006 Junjiro Okajima
3453 + * Copyright (c) 2005 Arun M. Krishnakumar
3454 + * Copyright (c) 2004-2006 David P. Quigley
3455 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3456 + * Copyright (c) 2003 Puja Gupta
3457 + * Copyright (c) 2003 Harikesavan Krishnan
3458 + * Copyright (c) 2003-2007 Stony Brook University
3459 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
3461 + * This program is free software; you can redistribute it and/or modify
3462 + * it under the terms of the GNU General Public License version 2 as
3463 + * published by the Free Software Foundation.
3468 +/* Make sure our rdstate is playing by the rules. */
3469 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3471 + BUG_ON(rdstate->offset >= DIREOF);
3472 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3475 +struct unionfs_getdents_callback {
3476 + struct unionfs_dir_state *rdstate;
3478 + int entries_written;
3479 + int filldir_called;
3480 + int filldir_error;
3481 + filldir_t filldir;
3482 + struct super_block *sb;
3485 +/* based on generic filldir in fs/readir.c */
3486 +static int unionfs_filldir(void *dirent, const char *name, int namelen,
3487 + loff_t offset, u64 ino, unsigned int d_type)
3489 + struct unionfs_getdents_callback *buf = dirent;
3490 + struct filldir_node *found = NULL;
3492 + int is_wh_entry = 0;
3494 + buf->filldir_called++;
3496 + if ((namelen > UNIONFS_WHLEN) &&
3497 + !strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
3498 + name += UNIONFS_WHLEN;
3499 + namelen -= UNIONFS_WHLEN;
3503 + found = find_filldir_node(buf->rdstate, name, namelen);
3508 + /* if 'name' isn't a whiteout, filldir it. */
3509 + if (!is_wh_entry) {
3510 + off_t pos = rdstate2offset(buf->rdstate);
3511 + u64 unionfs_ino = ino;
3513 + err = buf->filldir(buf->dirent, name, namelen, pos,
3514 + unionfs_ino, d_type);
3515 + buf->rdstate->offset++;
3516 + verify_rdstate_offset(buf->rdstate);
3519 + * If we did fill it, stuff it in our hash, otherwise return an
3523 + buf->filldir_error = err;
3526 + buf->entries_written++;
3527 + if ((err = add_filldir_node(buf->rdstate, name, namelen,
3528 + buf->rdstate->bindex, is_wh_entry)))
3529 + buf->filldir_error = err;
3535 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3538 + struct file *lower_file = NULL;
3539 + struct inode *inode = NULL;
3540 + struct unionfs_getdents_callback buf;
3541 + struct unionfs_dir_state *uds;
3545 + unionfs_read_lock(file->f_path.dentry->d_sb);
3547 + err = unionfs_file_revalidate(file, false);
3548 + if (unlikely(err))
3551 + inode = file->f_path.dentry->d_inode;
3553 + uds = UNIONFS_F(file)->rdstate;
3555 + if (file->f_pos == DIREOF) {
3557 + } else if (file->f_pos > 0) {
3558 + uds = find_rdstate(inode, file->f_pos);
3559 + if (unlikely(!uds)) {
3563 + UNIONFS_F(file)->rdstate = uds;
3565 + init_rdstate(file);
3566 + uds = UNIONFS_F(file)->rdstate;
3569 + bend = fbend(file);
3571 + while (uds->bindex <= bend) {
3572 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3573 + if (!lower_file) {
3579 + /* prepare callback buffer */
3580 + buf.filldir_called = 0;
3581 + buf.filldir_error = 0;
3582 + buf.entries_written = 0;
3583 + buf.dirent = dirent;
3584 + buf.filldir = filldir;
3585 + buf.rdstate = uds;
3586 + buf.sb = inode->i_sb;
3588 + /* Read starting from where we last left off. */
3589 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3594 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3596 + /* Save the position for when we continue. */
3597 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3602 + uds->dirpos = offset;
3604 + /* Copy the atime. */
3605 + fsstack_copy_attr_atime(inode,
3606 + lower_file->f_path.dentry->d_inode);
3611 + if (buf.filldir_error)
3614 + if (!buf.entries_written) {
3620 + if (!buf.filldir_error && uds->bindex >= bend) {
3621 + /* Save the number of hash entries for next time. */
3622 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3623 + free_rdstate(uds);
3624 + UNIONFS_F(file)->rdstate = NULL;
3625 + file->f_pos = DIREOF;
3627 + file->f_pos = rdstate2offset(uds);
3631 + unionfs_read_unlock(file->f_path.dentry->d_sb);
3636 + * This is not meant to be a generic repositioning function. If you do
3637 + * things that aren't supported, then we return EINVAL.
3639 + * What is allowed:
3640 + * (1) seeking to the same position that you are currently at
3641 + * This really has no effect, but returns where you are.
3642 + * (2) seeking to the beginning of the file
3643 + * This throws out all state, and lets you begin again.
3645 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3647 + struct unionfs_dir_state *rdstate;
3650 + unionfs_read_lock(file->f_path.dentry->d_sb);
3652 + err = unionfs_file_revalidate(file, false);
3653 + if (unlikely(err))
3656 + rdstate = UNIONFS_F(file)->rdstate;
3659 + * we let users seek to their current position, but not anywhere
3666 + free_rdstate(rdstate);
3667 + UNIONFS_F(file)->rdstate = NULL;
3669 + init_rdstate(file);
3673 + err = file->f_pos;
3676 + /* Unsupported, because we would break everything. */
3684 + if (offset == rdstate2offset(rdstate))
3686 + else if (file->f_pos == DIREOF)
3691 + struct inode *inode;
3692 + inode = file->f_path.dentry->d_inode;
3693 + rdstate = find_rdstate(inode, offset);
3695 + UNIONFS_F(file)->rdstate = rdstate;
3696 + err = rdstate->offset;
3704 + /* Unsupported, because we would break everything. */
3711 + unionfs_read_unlock(file->f_path.dentry->d_sb);
3716 + * Trimmed directory options, we shouldn't pass everything down since
3717 + * we don't want to operate on partial directories.
3719 +struct file_operations unionfs_dir_fops = {
3720 + .llseek = unionfs_dir_llseek,
3721 + .read = generic_read_dir,
3722 + .readdir = unionfs_readdir,
3723 + .unlocked_ioctl = unionfs_ioctl,
3724 + .open = unionfs_open,
3725 + .release = unionfs_file_release,
3726 + .flush = unionfs_flush,
3727 + .fsync = unionfs_fsync,
3728 + .fasync = unionfs_fasync,
3730 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3731 new file mode 100644
3732 index 0000000..7a28444
3734 +++ b/fs/unionfs/dirhelper.c
3737 + * Copyright (c) 2003-2007 Erez Zadok
3738 + * Copyright (c) 2003-2006 Charles P. Wright
3739 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3740 + * Copyright (c) 2005-2006 Junjiro Okajima
3741 + * Copyright (c) 2005 Arun M. Krishnakumar
3742 + * Copyright (c) 2004-2006 David P. Quigley
3743 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3744 + * Copyright (c) 2003 Puja Gupta
3745 + * Copyright (c) 2003 Harikesavan Krishnan
3746 + * Copyright (c) 2003-2007 Stony Brook University
3747 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
3749 + * This program is free software; you can redistribute it and/or modify
3750 + * it under the terms of the GNU General Public License version 2 as
3751 + * published by the Free Software Foundation.
3757 + * Delete all of the whiteouts in a given directory for rmdir.
3759 + * lower directory inode should be locked
3761 +int do_delete_whiteouts(struct dentry *dentry, int bindex,
3762 + struct unionfs_dir_state *namelist)
3765 + struct dentry *lower_dir_dentry = NULL;
3766 + struct dentry *lower_dentry;
3767 + char *name = NULL, *p;
3768 + struct inode *lower_dir;
3770 + struct list_head *pos;
3771 + struct filldir_node *cursor;
3773 + /* Find out lower parent dentry */
3774 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3775 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
3776 + lower_dir = lower_dir_dentry->d_inode;
3777 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
3780 + name = __getname();
3781 + if (unlikely(!name))
3783 + strcpy(name, UNIONFS_WHPFX);
3784 + p = name + UNIONFS_WHLEN;
3787 + for (i = 0; !err && i < namelist->size; i++) {
3788 + list_for_each(pos, &namelist->list[i]) {
3790 + list_entry(pos, struct filldir_node,
3792 + /* Only operate on whiteouts in this branch. */
3793 + if (cursor->bindex != bindex)
3795 + if (!cursor->whiteout)
3798 + strcpy(p, cursor->name);
3800 + lookup_one_len(name, lower_dir_dentry,
3803 + if (IS_ERR(lower_dentry)) {
3804 + err = PTR_ERR(lower_dentry);
3807 + if (lower_dentry->d_inode)
3808 + err = vfs_unlink(lower_dir, lower_dentry);
3809 + dput(lower_dentry);
3817 + /* After all of the removals, we should copy the attributes once. */
3818 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
3824 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
3825 +int delete_whiteouts(struct dentry *dentry, int bindex,
3826 + struct unionfs_dir_state *namelist)
3829 + struct super_block *sb;
3830 + struct dentry *lower_dir_dentry;
3831 + struct inode *lower_dir;
3832 + struct sioq_args args;
3834 + sb = dentry->d_sb;
3836 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3837 + BUG_ON(bindex < dbstart(dentry));
3838 + BUG_ON(bindex > dbend(dentry));
3839 + err = is_robranch_super(sb, bindex);
3843 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3844 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
3845 + lower_dir = lower_dir_dentry->d_inode;
3846 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
3848 + mutex_lock(&lower_dir->i_mutex);
3849 + if (!permission(lower_dir, MAY_WRITE | MAY_EXEC, NULL)) {
3850 + err = do_delete_whiteouts(dentry, bindex, namelist);
3852 + args.deletewh.namelist = namelist;
3853 + args.deletewh.dentry = dentry;
3854 + args.deletewh.bindex = bindex;
3855 + run_sioq(__delete_whiteouts, &args);
3858 + mutex_unlock(&lower_dir->i_mutex);
3865 +#define RD_CHECK_EMPTY 1
3866 +/* The callback structure for check_empty. */
3867 +struct unionfs_rdutil_callback {
3869 + int filldir_called;
3870 + struct unionfs_dir_state *rdstate;
3874 +/* This filldir function makes sure only whiteouts exist within a directory. */
3875 +static int readdir_util_callback(void *dirent, const char *name, int namelen,
3876 + loff_t offset, u64 ino, unsigned int d_type)
3879 + struct unionfs_rdutil_callback *buf = dirent;
3881 + struct filldir_node *found;
3883 + buf->filldir_called = 1;
3885 + if (name[0] == '.' && (namelen == 1 ||
3886 + (name[1] == '.' && namelen == 2)))
3889 + if (namelen > UNIONFS_WHLEN &&
3890 + !strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
3891 + namelen -= UNIONFS_WHLEN;
3892 + name += UNIONFS_WHLEN;
3896 + found = find_filldir_node(buf->rdstate, name, namelen);
3897 + /* If it was found in the table there was a previous whiteout. */
3902 + * if it wasn't found and isn't a whiteout, the directory isn't
3906 + if ((buf->mode == RD_CHECK_EMPTY) && !whiteout)
3909 + err = add_filldir_node(buf->rdstate, name, namelen,
3910 + buf->rdstate->bindex, whiteout);
3917 +/* Is a directory logically empty? */
3918 +int check_empty(struct dentry *dentry, struct unionfs_dir_state **namelist)
3921 + struct dentry *lower_dentry = NULL;
3922 + struct super_block *sb;
3923 + struct file *lower_file;
3924 + struct unionfs_rdutil_callback *buf = NULL;
3925 + int bindex, bstart, bend, bopaque;
3927 + sb = dentry->d_sb;
3930 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3932 + err = unionfs_partial_lookup(dentry);
3936 + bstart = dbstart(dentry);
3937 + bend = dbend(dentry);
3938 + bopaque = dbopaque(dentry);
3939 + if (0 <= bopaque && bopaque < bend)
3942 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3943 + if (unlikely(!buf)) {
3948 + buf->mode = RD_CHECK_EMPTY;
3949 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3950 + if (unlikely(!buf->rdstate)) {
3955 + /* Process the lower directories with rdutil_callback as a filldir. */
3956 + for (bindex = bstart; bindex <= bend; bindex++) {
3957 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3958 + if (!lower_dentry)
3960 + if (!lower_dentry->d_inode)
3962 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
3965 + dget(lower_dentry);
3966 + unionfs_mntget(dentry, bindex);
3967 + branchget(sb, bindex);
3969 + dentry_open(lower_dentry,
3970 + unionfs_lower_mnt_idx(dentry, bindex),
3972 + if (IS_ERR(lower_file)) {
3973 + err = PTR_ERR(lower_file);
3974 + dput(lower_dentry);
3975 + branchput(sb, bindex);
3980 + buf->filldir_called = 0;
3981 + buf->rdstate->bindex = bindex;
3982 + err = vfs_readdir(lower_file,
3983 + readdir_util_callback, buf);
3986 + } while ((err >= 0) && buf->filldir_called);
3988 + /* fput calls dput for lower_dentry */
3990 + branchput(sb, bindex);
3998 + if (namelist && !err)
3999 + *namelist = buf->rdstate;
4000 + else if (buf->rdstate)
4001 + free_rdstate(buf->rdstate);
4008 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4009 new file mode 100644
4010 index 0000000..ec18013
4012 +++ b/fs/unionfs/fanout.h
4015 + * Copyright (c) 2003-2007 Erez Zadok
4016 + * Copyright (c) 2003-2006 Charles P. Wright
4017 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4018 + * Copyright (c) 2005 Arun M. Krishnakumar
4019 + * Copyright (c) 2004-2006 David P. Quigley
4020 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4021 + * Copyright (c) 2003 Puja Gupta
4022 + * Copyright (c) 2003 Harikesavan Krishnan
4023 + * Copyright (c) 2003-2007 Stony Brook University
4024 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
4026 + * This program is free software; you can redistribute it and/or modify
4027 + * it under the terms of the GNU General Public License version 2 as
4028 + * published by the Free Software Foundation.
4035 + * Inode to private data
4037 + * Since we use containers and the struct inode is _inside_ the
4038 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4039 + * inode pointer), return a valid non-NULL pointer.
4041 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4043 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4046 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4047 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4049 +/* Superblock to private data */
4050 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4051 +#define sbstart(sb) 0
4052 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4053 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4054 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4056 +/* File to private Data */
4057 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4058 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4059 +#define fbend(file) (UNIONFS_F(file)->bend)
4061 +/* macros to manipulate branch IDs in stored in our superblock */
4062 +static inline int branch_id(struct super_block *sb, int index)
4064 + BUG_ON(!sb || index < 0);
4065 + return UNIONFS_SB(sb)->data[index].branch_id;
4068 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4070 + BUG_ON(!sb || index < 0);
4071 + UNIONFS_SB(sb)->data[index].branch_id = val;
4074 +static inline void new_branch_id(struct super_block *sb, int index)
4076 + BUG_ON(!sb || index < 0);
4077 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4081 + * Find new index of matching branch with an existing superblock of a known
4082 + * (possibly old) id. This is needed because branches could have been
4083 + * added/deleted causing the branches of any open files to shift.
4085 + * @sb: the new superblock which may have new/different branch IDs
4086 + * @id: the old/existing id we're looking for
4087 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4089 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4092 + for (i = 0; i < sbmax(sb); i++) {
4093 + if (branch_id(sb, i) == id)
4096 + /* in the non-ODF code, this should really never happen */
4097 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4101 +/* File to lower file. */
4102 +static inline struct file *unionfs_lower_file(const struct file *f)
4105 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4108 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4111 + BUG_ON(!f || index < 0);
4112 + return UNIONFS_F(f)->lower_files[index];
4115 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4118 + BUG_ON(!f || index < 0);
4119 + UNIONFS_F(f)->lower_files[index] = val;
4120 + /* save branch ID (may be redundant?) */
4121 + UNIONFS_F(f)->saved_branch_ids[index] =
4122 + branch_id((f)->f_dentry->d_sb, index);
4125 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4128 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4131 +/* Inode to lower inode. */
4132 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4135 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4138 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4141 + BUG_ON(!i || index < 0);
4142 + return UNIONFS_I(i)->lower_inodes[index];
4145 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4146 + struct inode *val)
4148 + BUG_ON(!i || index < 0);
4149 + UNIONFS_I(i)->lower_inodes[index] = val;
4152 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4155 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4158 +/* Superblock to lower superblock. */
4159 +static inline struct super_block *unionfs_lower_super(
4160 + const struct super_block *sb)
4163 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4166 +static inline struct super_block *unionfs_lower_super_idx(
4167 + const struct super_block *sb,
4170 + BUG_ON(!sb || index < 0);
4171 + return UNIONFS_SB(sb)->data[index].sb;
4174 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4176 + struct super_block *val)
4178 + BUG_ON(!sb || index < 0);
4179 + UNIONFS_SB(sb)->data[index].sb = val;
4182 +static inline void unionfs_set_lower_super(struct super_block *sb,
4183 + struct super_block *val)
4186 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4189 +/* Branch count macros. */
4190 +static inline int branch_count(const struct super_block *sb, int index)
4192 + BUG_ON(!sb || index < 0);
4193 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4196 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4198 + BUG_ON(!sb || index < 0);
4199 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4202 +static inline void branchget(struct super_block *sb, int index)
4204 + BUG_ON(!sb || index < 0);
4205 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4208 +static inline void branchput(struct super_block *sb, int index)
4210 + BUG_ON(!sb || index < 0);
4211 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4214 +/* Dentry macros */
4215 +static inline struct unionfs_dentry_info *UNIONFS_D(const struct dentry *dent)
4218 + return dent->d_fsdata;
4221 +static inline int dbstart(const struct dentry *dent)
4224 + return UNIONFS_D(dent)->bstart;
4227 +static inline void set_dbstart(struct dentry *dent, int val)
4230 + UNIONFS_D(dent)->bstart = val;
4233 +static inline int dbend(const struct dentry *dent)
4236 + return UNIONFS_D(dent)->bend;
4239 +static inline void set_dbend(struct dentry *dent, int val)
4242 + UNIONFS_D(dent)->bend = val;
4245 +static inline int dbopaque(const struct dentry *dent)
4248 + return UNIONFS_D(dent)->bopaque;
4251 +static inline void set_dbopaque(struct dentry *dent, int val)
4254 + UNIONFS_D(dent)->bopaque = val;
4257 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4258 + struct dentry *val)
4260 + BUG_ON(!dent || index < 0);
4261 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4264 +static inline struct dentry *unionfs_lower_dentry_idx(
4265 + const struct dentry *dent,
4268 + BUG_ON(!dent || index < 0);
4269 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4272 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4275 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4278 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4279 + struct vfsmount *mnt)
4281 + BUG_ON(!dent || index < 0);
4282 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4285 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4286 + const struct dentry *dent,
4289 + BUG_ON(!dent || index < 0);
4290 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4293 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4296 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4299 +/* Macros for locking a dentry. */
4300 +static inline void unionfs_lock_dentry(struct dentry *d)
4303 + mutex_lock(&UNIONFS_D(d)->lock);
4306 +static inline void unionfs_unlock_dentry(struct dentry *d)
4309 + mutex_unlock(&UNIONFS_D(d)->lock);
4312 +static inline void verify_locked(struct dentry *d)
4315 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4318 +/* copy a/m/ctime from the lower branch with the newest times */
4319 +static inline void unionfs_copy_attr_times(struct inode *upper)
4322 + struct inode *lower;
4324 + if (!upper || ibstart(upper) < 0)
4326 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
4327 + lower = unionfs_lower_inode_idx(upper, bindex);
4329 + continue; /* not all lower dir objects may exist */
4330 + if (unlikely(timespec_compare(&upper->i_mtime,
4331 + &lower->i_mtime) < 0))
4332 + upper->i_mtime = lower->i_mtime;
4333 + if (unlikely(timespec_compare(&upper->i_ctime,
4334 + &lower->i_ctime) < 0))
4335 + upper->i_ctime = lower->i_ctime;
4336 + if (unlikely(timespec_compare(&upper->i_atime,
4337 + &lower->i_atime) < 0))
4338 + upper->i_atime = lower->i_atime;
4343 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
4344 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
4345 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
4346 + * important if the lower inode is a directory type)
4348 +static inline void unionfs_copy_attr_all(struct inode *dest,
4349 + const struct inode *src)
4351 + dest->i_mode = src->i_mode;
4352 + dest->i_uid = src->i_uid;
4353 + dest->i_gid = src->i_gid;
4354 + dest->i_rdev = src->i_rdev;
4356 + unionfs_copy_attr_times(dest);
4358 + dest->i_blkbits = src->i_blkbits;
4359 + dest->i_flags = src->i_flags;
4362 + * Update the nlinks AFTER updating the above fields, because the
4363 + * get_links callback may depend on them.
4365 + dest->i_nlink = unionfs_get_nlinks(dest);
4368 +#endif /* not _FANOUT_H */
4369 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4370 new file mode 100644
4371 index 0000000..126df5e
4373 +++ b/fs/unionfs/file.c
4376 + * Copyright (c) 2003-2007 Erez Zadok
4377 + * Copyright (c) 2003-2006 Charles P. Wright
4378 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4379 + * Copyright (c) 2005-2006 Junjiro Okajima
4380 + * Copyright (c) 2005 Arun M. Krishnakumar
4381 + * Copyright (c) 2004-2006 David P. Quigley
4382 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4383 + * Copyright (c) 2003 Puja Gupta
4384 + * Copyright (c) 2003 Harikesavan Krishnan
4385 + * Copyright (c) 2003-2007 Stony Brook University
4386 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
4388 + * This program is free software; you can redistribute it and/or modify
4389 + * it under the terms of the GNU General Public License version 2 as
4390 + * published by the Free Software Foundation.
4395 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4396 + size_t count, loff_t *ppos)
4400 + unionfs_read_lock(file->f_path.dentry->d_sb);
4401 + err = unionfs_file_revalidate(file, false);
4402 + if (unlikely(err))
4404 + unionfs_check_file(file);
4406 + err = do_sync_read(file, buf, count, ppos);
4409 + touch_atime(unionfs_lower_mnt(file->f_path.dentry),
4410 + unionfs_lower_dentry(file->f_path.dentry));
4413 + unionfs_read_unlock(file->f_path.dentry->d_sb);
4414 + unionfs_check_file(file);
4418 +static ssize_t unionfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
4419 + unsigned long nr_segs, loff_t pos)
4422 + struct file *file = iocb->ki_filp;
4424 + unionfs_read_lock(file->f_path.dentry->d_sb);
4425 + err = unionfs_file_revalidate(file, false);
4426 + if (unlikely(err))
4428 + unionfs_check_file(file);
4430 + err = generic_file_aio_read(iocb, iov, nr_segs, pos);
4432 + if (err == -EIOCBQUEUED)
4433 + err = wait_on_sync_kiocb(iocb);
4436 + touch_atime(unionfs_lower_mnt(file->f_path.dentry),
4437 + unionfs_lower_dentry(file->f_path.dentry));
4440 + unionfs_read_unlock(file->f_path.dentry->d_sb);
4441 + unionfs_check_file(file);
4445 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4446 + size_t count, loff_t *ppos)
4450 + unionfs_read_lock(file->f_path.dentry->d_sb);
4451 + err = unionfs_file_revalidate(file, true);
4452 + if (unlikely(err))
4454 + unionfs_check_file(file);
4456 + err = do_sync_write(file, buf, count, ppos);
4457 + /* update our inode times upon a successful lower write */
4459 + unionfs_copy_attr_times(file->f_path.dentry->d_inode);
4460 + unionfs_check_file(file);
4464 + unionfs_read_unlock(file->f_path.dentry->d_sb);
4468 +static int unionfs_file_readdir(struct file *file, void *dirent,
4469 + filldir_t filldir)
4474 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4478 + struct file *lower_file;
4480 + unionfs_read_lock(file->f_path.dentry->d_sb);
4482 + /* This might be deferred to mmap's writepage */
4483 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4484 + err = unionfs_file_revalidate(file, willwrite);
4485 + if (unlikely(err))
4487 + unionfs_check_file(file);
4490 + * File systems which do not implement ->writepage may use
4491 + * generic_file_readonly_mmap as their ->mmap op. If you call
4492 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4493 + * But we cannot call the lower ->mmap op, so we can't tell that
4494 + * writeable mappings won't work. Therefore, our only choice is to
4495 + * check if the lower file system supports the ->writepage, and if
4496 + * not, return EINVAL (the same error that
4497 + * generic_file_readonly_mmap returns in that case).
4499 + lower_file = unionfs_lower_file(file);
4500 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4502 + printk(KERN_ERR "unionfs: branch %d file system does not "
4503 + "support writeable mmap\n", fbstart(file));
4505 + err = generic_file_mmap(file, vma);
4508 + "unionfs: generic_file_mmap failed %d\n", err);
4512 + unionfs_read_unlock(file->f_path.dentry->d_sb);
4514 + /* copyup could cause parent dir times to change */
4515 + unionfs_copy_attr_times(file->f_path.dentry->d_parent->d_inode);
4516 + unionfs_check_file(file);
4517 + unionfs_check_dentry(file->f_path.dentry->d_parent);
4522 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4524 + int bindex, bstart, bend;
4525 + struct file *lower_file;
4526 + struct dentry *lower_dentry;
4527 + struct inode *lower_inode, *inode;
4528 + int err = -EINVAL;
4530 + unionfs_read_lock(file->f_path.dentry->d_sb);
4531 + err = unionfs_file_revalidate(file, true);
4532 + if (unlikely(err))
4534 + unionfs_check_file(file);
4536 + bstart = fbstart(file);
4537 + bend = fbend(file);
4538 + if (bstart < 0 || bend < 0)
4541 + inode = dentry->d_inode;
4542 + if (unlikely(!inode)) {
4544 + "unionfs: null lower inode in unionfs_fsync\n");
4547 + for (bindex = bstart; bindex <= bend; bindex++) {
4548 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4549 + if (!lower_inode || !lower_inode->i_fop->fsync)
4551 + lower_file = unionfs_lower_file_idx(file, bindex);
4552 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4553 + mutex_lock(&lower_inode->i_mutex);
4554 + err = lower_inode->i_fop->fsync(lower_file,
4557 + mutex_unlock(&lower_inode->i_mutex);
4562 + unionfs_copy_attr_times(inode);
4565 + unionfs_read_unlock(file->f_path.dentry->d_sb);
4566 + unionfs_check_file(file);
4570 +int unionfs_fasync(int fd, struct file *file, int flag)
4572 + int bindex, bstart, bend;
4573 + struct file *lower_file;
4574 + struct dentry *dentry;
4575 + struct inode *lower_inode, *inode;
4578 + unionfs_read_lock(file->f_path.dentry->d_sb);
4579 + err = unionfs_file_revalidate(file, true);
4580 + if (unlikely(err))
4582 + unionfs_check_file(file);
4584 + bstart = fbstart(file);
4585 + bend = fbend(file);
4586 + if (bstart < 0 || bend < 0)
4589 + dentry = file->f_path.dentry;
4590 + inode = dentry->d_inode;
4591 + if (unlikely(!inode)) {
4593 + "unionfs: null lower inode in unionfs_fasync\n");
4596 + for (bindex = bstart; bindex <= bend; bindex++) {
4597 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4598 + if (!lower_inode || !lower_inode->i_fop->fasync)
4600 + lower_file = unionfs_lower_file_idx(file, bindex);
4601 + mutex_lock(&lower_inode->i_mutex);
4602 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4603 + mutex_unlock(&lower_inode->i_mutex);
4608 + unionfs_copy_attr_times(inode);
4611 + unionfs_read_unlock(file->f_path.dentry->d_sb);
4612 + unionfs_check_file(file);
4616 +struct file_operations unionfs_main_fops = {
4617 + .llseek = generic_file_llseek,
4618 + .read = unionfs_read,
4619 + .aio_read = unionfs_aio_read,
4620 + .write = unionfs_write,
4621 + .aio_write = generic_file_aio_write,
4622 + .readdir = unionfs_file_readdir,
4623 + .unlocked_ioctl = unionfs_ioctl,
4624 + .mmap = unionfs_mmap,
4625 + .open = unionfs_open,
4626 + .flush = unionfs_flush,
4627 + .release = unionfs_file_release,
4628 + .fsync = unionfs_fsync,
4629 + .fasync = unionfs_fasync,
4630 + .splice_read = generic_file_splice_read,
4632 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4633 new file mode 100644
4634 index 0000000..4e59ace
4636 +++ b/fs/unionfs/inode.c
4639 + * Copyright (c) 2003-2007 Erez Zadok
4640 + * Copyright (c) 2003-2006 Charles P. Wright
4641 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4642 + * Copyright (c) 2005-2006 Junjiro Okajima
4643 + * Copyright (c) 2005 Arun M. Krishnakumar
4644 + * Copyright (c) 2004-2006 David P. Quigley
4645 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4646 + * Copyright (c) 2003 Puja Gupta
4647 + * Copyright (c) 2003 Harikesavan Krishnan
4648 + * Copyright (c) 2003-2007 Stony Brook University
4649 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
4651 + * This program is free software; you can redistribute it and/or modify
4652 + * it under the terms of the GNU General Public License version 2 as
4653 + * published by the Free Software Foundation.
4658 +static int unionfs_create(struct inode *parent, struct dentry *dentry,
4659 + int mode, struct nameidata *nd)
4662 + struct dentry *lower_dentry = NULL;
4663 + struct dentry *wh_dentry = NULL;
4664 + struct dentry *lower_parent_dentry = NULL;
4665 + char *name = NULL;
4667 + struct nameidata lower_nd;
4669 + unionfs_read_lock(dentry->d_sb);
4670 + unionfs_lock_dentry(dentry);
4672 + unionfs_lock_dentry(dentry->d_parent);
4673 + valid = __unionfs_d_revalidate_chain(dentry->d_parent, nd, false);
4674 + unionfs_unlock_dentry(dentry->d_parent);
4675 + if (unlikely(!valid)) {
4676 + err = -ESTALE; /* same as what real_lookup does */
4679 + valid = __unionfs_d_revalidate_chain(dentry, nd, false);
4681 + * It's only a bug if this dentry was not negative and couldn't be
4682 + * revalidated (shouldn't happen).
4684 + BUG_ON(!valid && dentry->d_inode);
4687 + * We shouldn't create things in a read-only branch; this check is a
4688 + * bit redundant as we don't allow branch 0 to be read-only at the
4691 + err = is_robranch_super(dentry->d_sb, 0);
4698 + * We _always_ create on branch 0
4700 + lower_dentry = unionfs_lower_dentry_idx(dentry, 0);
4701 + if (lower_dentry) {
4703 + * check if whiteout exists in this branch, i.e. lookup .wh.foo
4706 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
4707 + if (unlikely(IS_ERR(name))) {
4708 + err = PTR_ERR(name);
4712 + wh_dentry = lookup_one_len(name, lower_dentry->d_parent,
4713 + dentry->d_name.len + UNIONFS_WHLEN);
4714 + if (IS_ERR(wh_dentry)) {
4715 + err = PTR_ERR(wh_dentry);
4720 + if (wh_dentry->d_inode) {
4722 + * .wh.foo has been found, so let's unlink it
4724 + struct dentry *lower_dir_dentry;
4726 + lower_dir_dentry = lock_parent(wh_dentry);
4727 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
4728 + unlock_dir(lower_dir_dentry);
4731 + printk(KERN_ERR "unionfs: create: could not "
4732 + "unlink whiteout, err = %d\n", err);
4738 + * if lower_dentry is NULL, create the entire
4739 + * dentry directory structure in branch 0.
4741 + lower_dentry = create_parents(parent, dentry,
4742 + dentry->d_name.name, 0);
4743 + if (IS_ERR(lower_dentry)) {
4744 + err = PTR_ERR(lower_dentry);
4749 + lower_parent_dentry = lock_parent(lower_dentry);
4750 + if (IS_ERR(lower_parent_dentry)) {
4751 + err = PTR_ERR(lower_parent_dentry);
4755 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4756 + if (unlikely(err < 0))
4758 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4760 + release_lower_nd(&lower_nd, err);
4763 + err = PTR_ERR(unionfs_interpose(dentry, parent->i_sb, 0));
4765 + unionfs_copy_attr_times(parent);
4766 + fsstack_copy_inode_size(parent,
4767 + lower_parent_dentry->d_inode);
4768 + /* update no. of links on parent directory */
4769 + parent->i_nlink = unionfs_get_nlinks(parent);
4773 + unlock_dir(lower_parent_dentry);
4780 + unionfs_postcopyup_setmnt(dentry);
4781 + unionfs_unlock_dentry(dentry);
4782 + unionfs_read_unlock(dentry->d_sb);
4784 + unionfs_check_inode(parent);
4786 + unionfs_check_dentry(dentry->d_parent);
4787 + unionfs_check_nd(nd);
4789 + unionfs_check_dentry(dentry);
4794 + * unionfs_lookup is the only special function which takes a dentry, yet we
4795 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
4796 + * we don't have a valid dentry here yet.
4798 +static struct dentry *unionfs_lookup(struct inode *parent,
4799 + struct dentry *dentry,
4800 + struct nameidata *nd)
4802 + struct path path_save;
4803 + struct dentry *ret;
4805 + unionfs_read_lock(dentry->d_sb);
4807 + /* save the dentry & vfsmnt from namei */
4809 + path_save.dentry = nd->dentry;
4810 + path_save.mnt = nd->mnt;
4814 + * unionfs_lookup_backend returns a locked dentry upon success,
4815 + * so we'll have to unlock it below.
4817 + ret = unionfs_lookup_backend(dentry, nd, INTERPOSE_LOOKUP);
4819 + /* restore the dentry & vfsmnt in namei */
4821 + nd->dentry = path_save.dentry;
4822 + nd->mnt = path_save.mnt;
4824 + if (!IS_ERR(ret)) {
4827 + /* parent times may have changed */
4828 + unionfs_copy_attr_times(dentry->d_parent->d_inode);
4829 + unionfs_unlock_dentry(dentry);
4832 + unionfs_check_inode(parent);
4833 + unionfs_check_dentry(dentry);
4834 + unionfs_check_dentry(dentry->d_parent);
4835 + unionfs_check_nd(nd);
4836 + unionfs_read_unlock(dentry->d_sb);
4841 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
4842 + struct dentry *new_dentry)
4845 + struct dentry *lower_old_dentry = NULL;
4846 + struct dentry *lower_new_dentry = NULL;
4847 + struct dentry *lower_dir_dentry = NULL;
4848 + struct dentry *whiteout_dentry;
4849 + char *name = NULL;
4851 + unionfs_read_lock(old_dentry->d_sb);
4852 + unionfs_double_lock_dentry(new_dentry, old_dentry);
4854 + if (unlikely(!__unionfs_d_revalidate_chain(old_dentry, NULL, false))) {
4858 + if (unlikely(new_dentry->d_inode &&
4859 + !__unionfs_d_revalidate_chain(new_dentry, NULL, false))) {
4864 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
4867 + * check if whiteout exists in the branch of new dentry, i.e. lookup
4868 + * .wh.foo first. If present, delete it
4870 + name = alloc_whname(new_dentry->d_name.name, new_dentry->d_name.len);
4871 + if (unlikely(IS_ERR(name))) {
4872 + err = PTR_ERR(name);
4876 + whiteout_dentry = lookup_one_len(name, lower_new_dentry->d_parent,
4877 + new_dentry->d_name.len +
4879 + if (IS_ERR(whiteout_dentry)) {
4880 + err = PTR_ERR(whiteout_dentry);
4884 + if (!whiteout_dentry->d_inode) {
4885 + dput(whiteout_dentry);
4886 + whiteout_dentry = NULL;
4888 + /* found a .wh.foo entry, unlink it and then call vfs_link() */
4889 + lower_dir_dentry = lock_parent(whiteout_dentry);
4890 + err = is_robranch_super(new_dentry->d_sb, dbstart(new_dentry));
4892 + err = vfs_unlink(lower_dir_dentry->d_inode,
4895 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
4896 + dir->i_nlink = unionfs_get_nlinks(dir);
4897 + unlock_dir(lower_dir_dentry);
4898 + lower_dir_dentry = NULL;
4899 + dput(whiteout_dentry);
4904 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
4905 + lower_new_dentry = create_parents(dir, new_dentry,
4906 + new_dentry->d_name.name,
4907 + dbstart(old_dentry));
4908 + err = PTR_ERR(lower_new_dentry);
4909 + if (IS_COPYUP_ERR(err))
4911 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
4914 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
4915 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
4917 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
4918 + lower_dir_dentry = lock_parent(lower_new_dentry);
4919 + err = is_robranch(old_dentry);
4921 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
4922 + lower_new_dentry);
4923 + unlock_dir(lower_dir_dentry);
4926 + if (IS_COPYUP_ERR(err)) {
4927 + int old_bstart = dbstart(old_dentry);
4930 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
4931 + err = copyup_dentry(old_dentry->d_parent->d_inode,
4932 + old_dentry, old_bstart,
4933 + bindex, old_dentry->d_name.name,
4934 + old_dentry->d_name.len, NULL,
4935 + old_dentry->d_inode->i_size);
4937 + lower_new_dentry =
4938 + create_parents(dir, new_dentry,
4939 + new_dentry->d_name.name,
4941 + lower_old_dentry =
4942 + unionfs_lower_dentry(old_dentry);
4943 + lower_dir_dentry =
4944 + lock_parent(lower_new_dentry);
4946 + err = vfs_link(lower_old_dentry,
4947 + lower_dir_dentry->d_inode,
4948 + lower_new_dentry);
4949 + unlock_dir(lower_dir_dentry);
4957 + if (err || !lower_new_dentry->d_inode)
4960 + /* Its a hard link, so use the same inode */
4961 + new_dentry->d_inode = igrab(old_dentry->d_inode);
4962 + d_instantiate(new_dentry, new_dentry->d_inode);
4963 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
4964 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
4966 + /* propagate number of hard-links */
4967 + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
4968 + /* new dentry's ctime may have changed due to hard-link counts */
4969 + unionfs_copy_attr_times(new_dentry->d_inode);
4972 + if (!new_dentry->d_inode)
4973 + d_drop(new_dentry);
4977 + unionfs_postcopyup_setmnt(new_dentry);
4979 + unionfs_unlock_dentry(new_dentry);
4980 + unionfs_unlock_dentry(old_dentry);
4982 + unionfs_check_inode(dir);
4983 + unionfs_check_dentry(new_dentry);
4984 + unionfs_check_dentry(old_dentry);
4985 + unionfs_read_unlock(old_dentry->d_sb);
4990 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
4991 + const char *symname)
4994 + struct dentry *lower_dentry = NULL;
4995 + struct dentry *whiteout_dentry = NULL;
4996 + struct dentry *lower_dir_dentry = NULL;
4998 + int bindex = 0, bstart;
4999 + char *name = NULL;
5001 + unionfs_read_lock(dentry->d_sb);
5002 + unionfs_lock_dentry(dentry);
5004 + if (unlikely(dentry->d_inode &&
5005 + !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5010 + /* We start out in the leftmost branch. */
5011 + bstart = dbstart(dentry);
5013 + lower_dentry = unionfs_lower_dentry(dentry);
5016 + * check if whiteout exists in this branch, i.e. lookup .wh.foo
5017 + * first. If present, delete it
5019 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
5020 + if (unlikely(IS_ERR(name))) {
5021 + err = PTR_ERR(name);
5026 + lookup_one_len(name, lower_dentry->d_parent,
5027 + dentry->d_name.len + UNIONFS_WHLEN);
5028 + if (IS_ERR(whiteout_dentry)) {
5029 + err = PTR_ERR(whiteout_dentry);
5033 + if (!whiteout_dentry->d_inode) {
5034 + dput(whiteout_dentry);
5035 + whiteout_dentry = NULL;
5038 + * found a .wh.foo entry, unlink it and then call
5041 + lower_dir_dentry = lock_parent(whiteout_dentry);
5043 + err = is_robranch_super(dentry->d_sb, bstart);
5045 + err = vfs_unlink(lower_dir_dentry->d_inode,
5047 + dput(whiteout_dentry);
5049 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5050 + /* propagate number of hard-links */
5051 + dir->i_nlink = unionfs_get_nlinks(dir);
5053 + unlock_dir(lower_dir_dentry);
5056 + /* exit if the error returned was NOT -EROFS */
5057 + if (!IS_COPYUP_ERR(err))
5060 + * should now try to create symlink in the another
5068 + * deleted whiteout if it was present, now do a normal vfs_symlink()
5069 + * with possible recursive directory creation
5071 + for (bindex = bstart; bindex >= 0; bindex--) {
5072 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5073 + if (!lower_dentry) {
5075 + * if lower_dentry is NULL, create the entire
5076 + * dentry directory structure in branch 'bindex'.
5077 + * lower_dentry will NOT be null when bindex ==
5078 + * bstart because lookup passed as a negative
5079 + * unionfs dentry pointing to a lone negative
5080 + * underlying dentry
5082 + lower_dentry = create_parents(dir, dentry,
5083 + dentry->d_name.name,
5085 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5086 + if (IS_ERR(lower_dentry))
5087 + err = PTR_ERR(lower_dentry);
5089 + printk(KERN_ERR "unionfs: lower dentry "
5090 + "NULL (or error) for bindex = %d\n",
5096 + lower_dir_dentry = lock_parent(lower_dentry);
5098 + err = is_robranch_super(dentry->d_sb, bindex);
5101 + err = vfs_symlink(lower_dir_dentry->d_inode,
5102 + lower_dentry, symname, mode);
5104 + unlock_dir(lower_dir_dentry);
5106 + if (err || !lower_dentry->d_inode) {
5108 + * break out of for loop if error returned was NOT
5111 + if (!IS_COPYUP_ERR(err))
5115 + * Only INTERPOSE_LOOKUP can return a value other
5118 + err = PTR_ERR(unionfs_interpose(dentry,
5121 + fsstack_copy_attr_times(dir,
5122 + lower_dir_dentry->
5124 + fsstack_copy_inode_size(dir,
5125 + lower_dir_dentry->
5128 + * update number of links on parent
5131 + dir->i_nlink = unionfs_get_nlinks(dir);
5138 + if (!dentry->d_inode)
5143 + unionfs_postcopyup_setmnt(dentry);
5144 + unionfs_unlock_dentry(dentry);
5146 + unionfs_check_inode(dir);
5147 + unionfs_check_dentry(dentry);
5148 + unionfs_read_unlock(dentry->d_sb);
5153 +static int unionfs_mkdir(struct inode *parent, struct dentry *dentry, int mode)
5156 + struct dentry *lower_dentry = NULL, *whiteout_dentry = NULL;
5157 + struct dentry *lower_parent_dentry = NULL;
5158 + int bindex = 0, bstart;
5159 + char *name = NULL;
5160 + int whiteout_unlinked = 0;
5161 + struct sioq_args args;
5163 + unionfs_read_lock(dentry->d_sb);
5164 + unionfs_lock_dentry(dentry);
5166 + if (unlikely(dentry->d_inode &&
5167 + !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5172 + bstart = dbstart(dentry);
5174 + lower_dentry = unionfs_lower_dentry(dentry);
5177 + * check if whiteout exists in this branch, i.e. lookup .wh.foo
5180 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
5181 + if (unlikely(IS_ERR(name))) {
5182 + err = PTR_ERR(name);
5186 + whiteout_dentry = lookup_one_len(name, lower_dentry->d_parent,
5187 + dentry->d_name.len + UNIONFS_WHLEN);
5188 + if (IS_ERR(whiteout_dentry)) {
5189 + err = PTR_ERR(whiteout_dentry);
5193 + if (!whiteout_dentry->d_inode) {
5194 + dput(whiteout_dentry);
5195 + whiteout_dentry = NULL;
5197 + lower_parent_dentry = lock_parent(whiteout_dentry);
5199 + /* found a.wh.foo entry, remove it then do vfs_mkdir */
5200 + err = is_robranch_super(dentry->d_sb, bstart);
5202 + args.unlink.parent = lower_parent_dentry->d_inode;
5203 + args.unlink.dentry = whiteout_dentry;
5204 + run_sioq(__unionfs_unlink, &args);
5207 + dput(whiteout_dentry);
5209 + unlock_dir(lower_parent_dentry);
5212 + /* exit if the error returned was NOT -EROFS */
5213 + if (!IS_COPYUP_ERR(err))
5217 + whiteout_unlinked = 1;
5221 + for (bindex = bstart; bindex >= 0; bindex--) {
5223 + int bend = dbend(dentry);
5225 + if (is_robranch_super(dentry->d_sb, bindex))
5228 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5229 + if (!lower_dentry) {
5230 + lower_dentry = create_parents(parent, dentry,
5231 + dentry->d_name.name,
5233 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5234 + printk(KERN_ERR "unionfs: lower dentry "
5235 + " NULL for bindex = %d\n", bindex);
5240 + lower_parent_dentry = lock_parent(lower_dentry);
5242 + if (IS_ERR(lower_parent_dentry)) {
5243 + err = PTR_ERR(lower_parent_dentry);
5247 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5250 + unlock_dir(lower_parent_dentry);
5252 + /* did the mkdir succeed? */
5256 + for (i = bindex + 1; i < bend; i++) {
5257 + if (unionfs_lower_dentry_idx(dentry, i)) {
5258 + dput(unionfs_lower_dentry_idx(dentry, i));
5259 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5262 + set_dbend(dentry, bindex);
5265 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5268 + err = PTR_ERR(unionfs_interpose(dentry, parent->i_sb, 0));
5270 + unionfs_copy_attr_times(parent);
5271 + fsstack_copy_inode_size(parent,
5272 + lower_parent_dentry->d_inode);
5274 + /* update number of links on parent directory */
5275 + parent->i_nlink = unionfs_get_nlinks(parent);
5278 + err = make_dir_opaque(dentry, dbstart(dentry));
5280 + printk(KERN_ERR "unionfs: mkdir: error creating "
5281 + ".wh.__dir_opaque: %d\n", err);
5285 + /* we are done! */
5290 + if (!dentry->d_inode)
5296 + unionfs_copy_attr_times(dentry->d_inode);
5297 + unionfs_unlock_dentry(dentry);
5298 + unionfs_check_inode(parent);
5299 + unionfs_check_dentry(dentry);
5300 + unionfs_read_unlock(dentry->d_sb);
5305 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5309 + struct dentry *lower_dentry = NULL, *whiteout_dentry = NULL;
5310 + struct dentry *lower_parent_dentry = NULL;
5311 + int bindex = 0, bstart;
5312 + char *name = NULL;
5313 + int whiteout_unlinked = 0;
5315 + unionfs_read_lock(dentry->d_sb);
5316 + unionfs_lock_dentry(dentry);
5318 + if (unlikely(dentry->d_inode &&
5319 + !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5324 + bstart = dbstart(dentry);
5326 + lower_dentry = unionfs_lower_dentry(dentry);
5329 + * check if whiteout exists in this branch, i.e. lookup .wh.foo
5332 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
5333 + if (unlikely(IS_ERR(name))) {
5334 + err = PTR_ERR(name);
5338 + whiteout_dentry = lookup_one_len(name, lower_dentry->d_parent,
5339 + dentry->d_name.len + UNIONFS_WHLEN);
5340 + if (IS_ERR(whiteout_dentry)) {
5341 + err = PTR_ERR(whiteout_dentry);
5345 + if (!whiteout_dentry->d_inode) {
5346 + dput(whiteout_dentry);
5347 + whiteout_dentry = NULL;
5349 + /* found .wh.foo, unlink it */
5350 + lower_parent_dentry = lock_parent(whiteout_dentry);
5352 + /* found a.wh.foo entry, remove it then do vfs_mkdir */
5353 + err = is_robranch_super(dentry->d_sb, bstart);
5355 + err = vfs_unlink(lower_parent_dentry->d_inode,
5357 + dput(whiteout_dentry);
5359 + unlock_dir(lower_parent_dentry);
5362 + if (!IS_COPYUP_ERR(err))
5366 + whiteout_unlinked = 1;
5370 + for (bindex = bstart; bindex >= 0; bindex--) {
5371 + if (is_robranch_super(dentry->d_sb, bindex))
5374 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5375 + if (!lower_dentry) {
5376 + lower_dentry = create_parents(dir, dentry,
5377 + dentry->d_name.name,
5379 + if (IS_ERR(lower_dentry)) {
5380 + printk(KERN_ERR "unionfs: failed to create "
5381 + "parents on %d, err = %ld\n",
5382 + bindex, PTR_ERR(lower_dentry));
5387 + lower_parent_dentry = lock_parent(lower_dentry);
5388 + if (IS_ERR(lower_parent_dentry)) {
5389 + err = PTR_ERR(lower_parent_dentry);
5393 + err = vfs_mknod(lower_parent_dentry->d_inode,
5394 + lower_dentry, mode, dev);
5397 + unlock_dir(lower_parent_dentry);
5402 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5405 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5407 + fsstack_copy_attr_times(dir,
5408 + lower_parent_dentry->d_inode);
5409 + fsstack_copy_inode_size(dir,
5410 + lower_parent_dentry->d_inode);
5411 + /* update number of links on parent directory */
5412 + dir->i_nlink = unionfs_get_nlinks(dir);
5414 + unlock_dir(lower_parent_dentry);
5420 + if (!dentry->d_inode)
5426 + unionfs_postcopyup_setmnt(dentry);
5427 + unionfs_unlock_dentry(dentry);
5429 + unionfs_check_inode(dir);
5430 + unionfs_check_dentry(dentry);
5431 + unionfs_read_unlock(dentry->d_sb);
5436 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5440 + struct dentry *lower_dentry;
5442 + unionfs_read_lock(dentry->d_sb);
5443 + unionfs_lock_dentry(dentry);
5445 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5450 + lower_dentry = unionfs_lower_dentry(dentry);
5452 + if (!lower_dentry->d_inode->i_op ||
5453 + !lower_dentry->d_inode->i_op->readlink) {
5458 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5461 + fsstack_copy_attr_atime(dentry->d_inode,
5462 + lower_dentry->d_inode);
5465 + unionfs_unlock_dentry(dentry);
5466 + unionfs_check_dentry(dentry);
5467 + unionfs_read_unlock(dentry->d_sb);
5473 + * unionfs_follow_link takes a dentry, but it is simple. It only needs to
5474 + * allocate some memory and then call our ->readlink method. Our
5475 + * unionfs_readlink *does* lock our dentry and revalidate the dentry.
5476 + * Therefore, we do not have to lock our dentry here, to prevent a deadlock;
5477 + * nor do we need to revalidate it either. It is safe to not lock our
5478 + * dentry here, nor revalidate it, because unionfs_follow_link does not do
5479 + * anything (prior to calling ->readlink) which could become inconsistent
5480 + * due to branch management.
5482 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5485 + int len = PAGE_SIZE, err;
5486 + mm_segment_t old_fs;
5488 + unionfs_read_lock(dentry->d_sb);
5490 + /* This is freed by the put_link method assuming a successful call. */
5491 + buf = kmalloc(len, GFP_KERNEL);
5492 + if (unlikely(!buf)) {
5497 + /* read the symlink, and then we will follow it */
5498 + old_fs = get_fs();
5499 + set_fs(KERNEL_DS);
5500 + err = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
5508 + nd_set_link(nd, buf);
5512 + unionfs_check_dentry(dentry);
5513 + unionfs_check_nd(nd);
5514 + unionfs_read_unlock(dentry->d_sb);
5515 + return ERR_PTR(err);
5518 +/* FIXME: We may not have to lock here */
5519 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5522 + unionfs_read_lock(dentry->d_sb);
5524 + unionfs_lock_dentry(dentry);
5525 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, nd, false)))
5527 + "unionfs: put_link failed to revalidate dentry\n");
5528 + unionfs_unlock_dentry(dentry);
5530 + unionfs_check_dentry(dentry);
5531 + unionfs_check_nd(nd);
5532 + kfree(nd_get_link(nd));
5533 + unionfs_read_unlock(dentry->d_sb);
5537 + * Basically copied from the kernel vfs permission(), but we've changed
5539 + * (1) the IS_RDONLY check is skipped, and
5540 + * (2) We return 0 (success) if the non-leftmost branch is mounted
5541 + * readonly, to allow copyup to work.
5542 + * (3) we do call security_inode_permission, and therefore security inside
5543 + * SELinux, etc. are performed.
5545 + * @inode: the lower inode we're checking permission on
5547 +static int inode_permission(struct super_block *sb, struct inode *inode,
5548 + int mask, struct nameidata *nd, int bindex)
5550 + int retval, submask;
5552 + if (mask & MAY_WRITE) {
5553 + umode_t mode = inode->i_mode;
5554 + /* The first branch is allowed to be really readonly. */
5555 + if (bindex == 0 &&
5556 + IS_RDONLY(inode) &&
5557 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5560 + * Nobody gets write access to an immutable file.
5562 + if (IS_IMMUTABLE(inode))
5565 + * For all other branches than the first one, we ignore
5566 + * EROFS or if the branch is mounted as readonly, to let
5567 + * copyup take place.
5570 + is_robranch_super(sb, bindex) &&
5571 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5575 + /* Ordinary permission routines do not understand MAY_APPEND. */
5576 + submask = mask & ~MAY_APPEND;
5577 + if (inode->i_op && inode->i_op->permission)
5578 + retval = inode->i_op->permission(inode, submask, nd);
5580 + retval = generic_permission(inode, submask, NULL);
5582 + if (retval && retval != -EROFS) /* ignore EROFS */
5585 + retval = security_inode_permission(inode, mask, nd);
5586 + return ((retval == -EROFS) ? 0 : retval); /* ignore EROFS */
5590 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5591 + * a deadlock with the branch-management "add branch" code (which grabbed
5592 + * the write lock). It is safe to not grab the read lock here, because even
5593 + * with branch management taking place, there is no chance that
5594 + * unionfs_permission, or anything it calls, will use stale branch
5597 +static int unionfs_permission(struct inode *inode, int mask,
5598 + struct nameidata *nd)
5600 + struct inode *lower_inode = NULL;
5602 + int bindex, bstart, bend;
5603 + const int is_file = !S_ISDIR(inode->i_mode);
5604 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5606 + bstart = ibstart(inode);
5607 + bend = ibend(inode);
5608 + if (unlikely(bstart < 0 || bend < 0)) {
5610 + * With branch-management, we can get a stale inode here.
5611 + * If so, we return ESTALE back to link_path_walk, which
5612 + * would discard the dcache entry and re-lookup the
5613 + * dentry+inode. This should be equivalent to issuing
5614 + * __unionfs_d_revalidate_chain on nd.dentry here.
5616 + err = -ESTALE; /* force revalidate */
5620 + for (bindex = bstart; bindex <= bend; bindex++) {
5621 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5626 + * check the condition for D-F-D underlying files/directories,
5627 + * we don't have to check for files, if we are checking for
5630 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5634 + * We use our own special version of permission, such that
5635 + * only the first branch returns -EROFS.
5637 + err = inode_permission(inode->i_sb, lower_inode, mask, nd,
5641 + * The permissions are an intersection of the overall directory
5642 + * permissions, so we fail if one fails.
5647 + /* only the leftmost file matters. */
5648 + if (is_file || write_mask) {
5649 + if (is_file && write_mask) {
5650 + err = get_write_access(lower_inode);
5652 + put_write_access(lower_inode);
5657 + /* sync times which may have changed (asynchronously) below */
5658 + unionfs_copy_attr_times(inode);
5661 + unionfs_check_inode(inode);
5662 + unionfs_check_nd(nd);
5666 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5669 + struct dentry *lower_dentry;
5670 + struct inode *inode = NULL;
5671 + struct inode *lower_inode = NULL;
5672 + int bstart, bend, bindex;
5676 + unionfs_read_lock(dentry->d_sb);
5677 + unionfs_lock_dentry(dentry);
5679 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5684 + bstart = dbstart(dentry);
5685 + bend = dbend(dentry);
5686 + inode = dentry->d_inode;
5688 + for (bindex = bstart; (bindex <= bend) || (bindex == bstart);
5690 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5691 + if (!lower_dentry)
5693 + BUG_ON(lower_dentry->d_inode == NULL);
5695 + /* If the file is on a read only branch */
5696 + if (is_robranch_super(dentry->d_sb, bindex)
5697 + || IS_RDONLY(lower_dentry->d_inode)) {
5698 + if (copyup || (bindex != bstart))
5700 + /* Only if its the leftmost file, copyup the file */
5701 + for (i = bstart - 1; i >= 0; i--) {
5702 + loff_t size = dentry->d_inode->i_size;
5703 + if (ia->ia_valid & ATTR_SIZE)
5704 + size = ia->ia_size;
5705 + err = copyup_dentry(dentry->d_parent->d_inode,
5706 + dentry, bstart, i,
5707 + dentry->d_name.name,
5708 + dentry->d_name.len,
5714 + unionfs_lower_dentry(dentry);
5718 + * if error is in the leftmost branch, pass
5726 + err = notify_change(lower_dentry, ia);
5733 + if (ia->ia_valid & ATTR_SIZE) {
5734 + if (ia->ia_size != i_size_read(inode)) {
5735 + err = vmtruncate(inode, ia->ia_size);
5738 + "unionfs: setattr: vmtruncate failed\n");
5742 + /* get the size from the first lower inode */
5743 + lower_inode = unionfs_lower_inode(inode);
5744 + unionfs_copy_attr_all(inode, lower_inode);
5745 + fsstack_copy_inode_size(inode, lower_inode);
5746 + /* if setattr succeeded, then parent dir may have changed */
5747 + unionfs_copy_attr_times(dentry->d_parent->d_inode);
5749 + unionfs_unlock_dentry(dentry);
5750 + unionfs_check_dentry(dentry);
5751 + unionfs_check_dentry(dentry->d_parent);
5752 + unionfs_read_unlock(dentry->d_sb);
5757 +struct inode_operations unionfs_symlink_iops = {
5758 + .readlink = unionfs_readlink,
5759 + .permission = unionfs_permission,
5760 + .follow_link = unionfs_follow_link,
5761 + .setattr = unionfs_setattr,
5762 + .put_link = unionfs_put_link,
5765 +struct inode_operations unionfs_dir_iops = {
5766 + .create = unionfs_create,
5767 + .lookup = unionfs_lookup,
5768 + .link = unionfs_link,
5769 + .unlink = unionfs_unlink,
5770 + .symlink = unionfs_symlink,
5771 + .mkdir = unionfs_mkdir,
5772 + .rmdir = unionfs_rmdir,
5773 + .mknod = unionfs_mknod,
5774 + .rename = unionfs_rename,
5775 + .permission = unionfs_permission,
5776 + .setattr = unionfs_setattr,
5777 +#ifdef CONFIG_UNION_FS_XATTR
5778 + .setxattr = unionfs_setxattr,
5779 + .getxattr = unionfs_getxattr,
5780 + .removexattr = unionfs_removexattr,
5781 + .listxattr = unionfs_listxattr,
5782 +#endif /* CONFIG_UNION_FS_XATTR */
5785 +struct inode_operations unionfs_main_iops = {
5786 + .permission = unionfs_permission,
5787 + .setattr = unionfs_setattr,
5788 +#ifdef CONFIG_UNION_FS_XATTR
5789 + .setxattr = unionfs_setxattr,
5790 + .getxattr = unionfs_getxattr,
5791 + .removexattr = unionfs_removexattr,
5792 + .listxattr = unionfs_listxattr,
5793 +#endif /* CONFIG_UNION_FS_XATTR */
5795 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5796 new file mode 100644
5797 index 0000000..a1904c9
5799 +++ b/fs/unionfs/lookup.c
5802 + * Copyright (c) 2003-2007 Erez Zadok
5803 + * Copyright (c) 2003-2006 Charles P. Wright
5804 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5805 + * Copyright (c) 2005-2006 Junjiro Okajima
5806 + * Copyright (c) 2005 Arun M. Krishnakumar
5807 + * Copyright (c) 2004-2006 David P. Quigley
5808 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5809 + * Copyright (c) 2003 Puja Gupta
5810 + * Copyright (c) 2003 Harikesavan Krishnan
5811 + * Copyright (c) 2003-2007 Stony Brook University
5812 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
5814 + * This program is free software; you can redistribute it and/or modify
5815 + * it under the terms of the GNU General Public License version 2 as
5816 + * published by the Free Software Foundation.
5821 +static int realloc_dentry_private_data(struct dentry *dentry);
5823 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
5824 +static int is_validname(const char *name)
5826 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
5828 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
5829 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
5834 +/* The rest of these are utility functions for lookup. */
5835 +static noinline int is_opaque_dir(struct dentry *dentry, int bindex)
5838 + struct dentry *lower_dentry;
5839 + struct dentry *wh_lower_dentry;
5840 + struct inode *lower_inode;
5841 + struct sioq_args args;
5843 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5844 + lower_inode = lower_dentry->d_inode;
5846 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
5848 + mutex_lock(&lower_inode->i_mutex);
5850 + if (!permission(lower_inode, MAY_EXEC, NULL)) {
5852 + lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
5853 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
5855 + args.is_opaque.dentry = lower_dentry;
5856 + run_sioq(__is_opaque_dir, &args);
5857 + wh_lower_dentry = args.ret;
5860 + mutex_unlock(&lower_inode->i_mutex);
5862 + if (IS_ERR(wh_lower_dentry)) {
5863 + err = PTR_ERR(wh_lower_dentry);
5867 + /* This is an opaque dir iff wh_lower_dentry is positive */
5868 + err = !!wh_lower_dentry->d_inode;
5870 + dput(wh_lower_dentry);
5876 + * Main (and complex) driver function for Unionfs's lookup
5878 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
5879 + * PTR if d_splice returned a different dentry.
5881 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
5882 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
5883 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
5884 + * dentry's info, which the caller must unlock.
5886 +struct dentry *unionfs_lookup_backend(struct dentry *dentry,
5887 + struct nameidata *nd, int lookupmode)
5890 + struct dentry *lower_dentry = NULL;
5891 + struct dentry *wh_lower_dentry = NULL;
5892 + struct dentry *lower_dir_dentry = NULL;
5893 + struct dentry *parent_dentry = NULL;
5894 + struct dentry *d_interposed = NULL;
5895 + int bindex, bstart = -1, bend, bopaque;
5896 + int dentry_count = 0; /* Number of positive dentries. */
5897 + int first_dentry_offset = -1; /* -1 is uninitialized */
5898 + struct dentry *first_dentry = NULL;
5899 + struct dentry *first_lower_dentry = NULL;
5900 + struct vfsmount *first_lower_mnt = NULL;
5901 + int locked_parent = 0;
5903 + char *whname = NULL;
5908 + * We should already have a lock on this dentry in the case of a
5909 + * partial lookup, or a revalidation. Otherwise it is returned from
5910 + * new_dentry_private_data already locked.
5912 + if (lookupmode == INTERPOSE_PARTIAL || lookupmode == INTERPOSE_REVAL ||
5913 + lookupmode == INTERPOSE_REVAL_NEG)
5914 + verify_locked(dentry);
5915 + else /* this could only be INTERPOSE_LOOKUP */
5916 + BUG_ON(UNIONFS_D(dentry) != NULL);
5918 + switch (lookupmode) {
5919 + case INTERPOSE_PARTIAL:
5921 + case INTERPOSE_LOOKUP:
5922 + err = new_dentry_private_data(dentry);
5923 + if (unlikely(err))
5927 + /* default: can only be INTERPOSE_REVAL/REVAL_NEG */
5928 + err = realloc_dentry_private_data(dentry);
5929 + if (unlikely(err))
5934 + /* must initialize dentry operations */
5935 + dentry->d_op = &unionfs_dops;
5937 + parent_dentry = dget_parent(dentry);
5938 + /* We never partial lookup the root directory. */
5939 + if (parent_dentry != dentry) {
5940 + unionfs_lock_dentry(parent_dentry);
5941 + locked_parent = 1;
5943 + dput(parent_dentry);
5944 + parent_dentry = NULL;
5948 + name = dentry->d_name.name;
5949 + namelen = dentry->d_name.len;
5951 + /* No dentries should get created for possible whiteout names. */
5952 + if (!is_validname(name)) {
5957 + /* Now start the actual lookup procedure. */
5958 + bstart = dbstart(parent_dentry);
5959 + bend = dbend(parent_dentry);
5960 + bopaque = dbopaque(parent_dentry);
5961 + BUG_ON(bstart < 0);
5964 + * It would be ideal if we could convert partial lookups to only have
5965 + * to do this work when they really need to. It could probably improve
5966 + * performance quite a bit, and maybe simplify the rest of the code.
5968 + if (lookupmode == INTERPOSE_PARTIAL) {
5970 + if ((bopaque != -1) && (bopaque < bend))
5974 + for (bindex = bstart; bindex <= bend; bindex++) {
5975 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5976 + if (lookupmode == INTERPOSE_PARTIAL && lower_dentry)
5978 + BUG_ON(lower_dentry != NULL);
5980 + lower_dir_dentry =
5981 + unionfs_lower_dentry_idx(parent_dentry, bindex);
5983 + /* if the parent lower dentry does not exist skip this */
5984 + if (!(lower_dir_dentry && lower_dir_dentry->d_inode))
5987 + /* also skip it if the parent isn't a directory. */
5988 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
5991 + /* Reuse the whiteout name because its value doesn't change. */
5993 + whname = alloc_whname(name, namelen);
5994 + if (unlikely(IS_ERR(whname))) {
5995 + err = PTR_ERR(whname);
6000 + /* check if whiteout exists in this branch: lookup .wh.foo */
6001 + wh_lower_dentry = lookup_one_len(whname, lower_dir_dentry,
6002 + namelen + UNIONFS_WHLEN);
6003 + if (IS_ERR(wh_lower_dentry)) {
6004 + dput(first_lower_dentry);
6005 + unionfs_mntput(first_dentry, first_dentry_offset);
6006 + err = PTR_ERR(wh_lower_dentry);
6010 + if (wh_lower_dentry->d_inode) {
6011 + /* We found a whiteout so let's give up. */
6012 + if (S_ISREG(wh_lower_dentry->d_inode->i_mode)) {
6013 + set_dbend(dentry, bindex);
6014 + set_dbopaque(dentry, bindex);
6015 + dput(wh_lower_dentry);
6019 + printk(KERN_ERR "unionfs: EIO: invalid whiteout "
6020 + "entry type %d\n",
6021 + wh_lower_dentry->d_inode->i_mode);
6022 + dput(wh_lower_dentry);
6023 + dput(first_lower_dentry);
6024 + unionfs_mntput(first_dentry, first_dentry_offset);
6028 + dput(wh_lower_dentry);
6029 + wh_lower_dentry = NULL;
6031 + /* Now do regular lookup; lookup foo */
6032 + lower_dentry = lookup_one_len(name, lower_dir_dentry, namelen);
6033 + if (IS_ERR(lower_dentry)) {
6034 + dput(first_lower_dentry);
6035 + unionfs_mntput(first_dentry, first_dentry_offset);
6036 + err = PTR_ERR(lower_dentry);
6041 + * Store the first negative dentry specially, because if they
6042 + * are all negative we need this for future creates.
6044 + if (!lower_dentry->d_inode) {
6045 + if (!first_lower_dentry && (dbstart(dentry) == -1)) {
6046 + first_lower_dentry = lower_dentry;
6048 + * FIXME: following line needs to be changed
6049 + * to allow mount-point crossing
6051 + first_dentry = parent_dentry;
6053 + unionfs_mntget(parent_dentry, bindex);
6054 + first_dentry_offset = bindex;
6056 + dput(lower_dentry);
6062 + /* number of positive dentries */
6065 + /* store underlying dentry */
6066 + if (dbstart(dentry) == -1)
6067 + set_dbstart(dentry, bindex);
6068 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6070 + * FIXME: the following line needs to get fixed to allow
6071 + * mount-point crossing
6073 + unionfs_set_lower_mnt_idx(dentry, bindex,
6074 + unionfs_mntget(parent_dentry,
6076 + set_dbend(dentry, bindex);
6078 + /* update parent directory's atime with the bindex */
6079 + fsstack_copy_attr_atime(parent_dentry->d_inode,
6080 + lower_dir_dentry->d_inode);
6082 + /* We terminate file lookups here. */
6083 + if (!S_ISDIR(lower_dentry->d_inode->i_mode)) {
6084 + if (lookupmode == INTERPOSE_PARTIAL)
6086 + if (dentry_count == 1)
6087 + goto out_positive;
6088 + /* This can only happen with mixed D-*-F-* */
6089 + BUG_ON(!S_ISDIR(unionfs_lower_dentry(dentry)->
6090 + d_inode->i_mode));
6094 + opaque = is_opaque_dir(dentry, bindex);
6096 + dput(first_lower_dentry);
6097 + unionfs_mntput(first_dentry, first_dentry_offset);
6100 + } else if (opaque) {
6101 + set_dbend(dentry, bindex);
6102 + set_dbopaque(dentry, bindex);
6108 + goto out_positive;
6110 + goto out_negative;
6113 + if (lookupmode == INTERPOSE_PARTIAL)
6116 + /* If we've only got negative dentries, then use the leftmost one. */
6117 + if (lookupmode == INTERPOSE_REVAL) {
6118 + if (dentry->d_inode)
6119 + UNIONFS_I(dentry->d_inode)->stale = 1;
6122 + /* This should only happen if we found a whiteout. */
6123 + if (first_dentry_offset == -1) {
6124 + first_lower_dentry = lookup_one_len(name, lower_dir_dentry,
6126 + first_dentry_offset = bindex;
6127 + if (IS_ERR(first_lower_dentry)) {
6128 + err = PTR_ERR(first_lower_dentry);
6133 + * FIXME: the following line needs to be changed to allow
6134 + * mount-point crossing
6136 + first_dentry = dentry;
6137 + first_lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6140 + unionfs_set_lower_dentry_idx(dentry, first_dentry_offset,
6141 + first_lower_dentry);
6142 + unionfs_set_lower_mnt_idx(dentry, first_dentry_offset,
6144 + set_dbstart(dentry, first_dentry_offset);
6145 + set_dbend(dentry, first_dentry_offset);
6147 + if (lookupmode == INTERPOSE_REVAL_NEG)
6148 + BUG_ON(dentry->d_inode != NULL);
6150 + d_add(dentry, NULL);
6153 +/* This part of the code is for positive dentries. */
6155 + BUG_ON(dentry_count <= 0);
6158 + * If we're holding onto the first negative dentry & corresponding
6159 + * vfsmount - throw it out.
6161 + dput(first_lower_dentry);
6162 + unionfs_mntput(first_dentry, first_dentry_offset);
6164 + /* Partial lookups need to re-interpose, or throw away older negs. */
6165 + if (lookupmode == INTERPOSE_PARTIAL) {
6166 + if (dentry->d_inode) {
6167 + unionfs_reinterpose(dentry);
6172 + * This somehow turned positive, so it is as if we had a
6173 + * negative revalidation.
6175 + lookupmode = INTERPOSE_REVAL_NEG;
6177 + update_bstart(dentry);
6178 + bstart = dbstart(dentry);
6179 + bend = dbend(dentry);
6183 + * Interpose can return a dentry if d_splice returned a different
6186 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6187 + if (IS_ERR(d_interposed))
6188 + err = PTR_ERR(d_interposed);
6189 + else if (d_interposed)
6190 + dentry = d_interposed;
6201 + /* should dput all the underlying dentries on error condition */
6202 + bstart = dbstart(dentry);
6203 + if (bstart >= 0) {
6204 + bend = dbend(dentry);
6205 + for (bindex = bstart; bindex <= bend; bindex++) {
6206 + dput(unionfs_lower_dentry_idx(dentry, bindex));
6207 + unionfs_mntput(dentry, bindex);
6210 + kfree(UNIONFS_D(dentry)->lower_paths);
6211 + UNIONFS_D(dentry)->lower_paths = NULL;
6212 + set_dbstart(dentry, -1);
6213 + set_dbend(dentry, -1);
6216 + if (!err && UNIONFS_D(dentry)) {
6217 + BUG_ON(dbend(dentry) > UNIONFS_D(dentry)->bcount);
6218 + BUG_ON(dbend(dentry) > sbmax(dentry->d_sb));
6219 + if (dbstart(dentry) < 0 &&
6220 + dentry->d_inode && bstart >= 0 &&
6221 + (!UNIONFS_I(dentry->d_inode) ||
6222 + !UNIONFS_I(dentry->d_inode)->lower_inodes)) {
6223 + unionfs_mntput(dentry->d_sb->s_root, bstart);
6224 + dput(first_lower_dentry);
6225 + UNIONFS_I(dentry->d_inode)->stale = 1;
6229 + if (locked_parent)
6230 + unionfs_unlock_dentry(parent_dentry);
6231 + dput(parent_dentry);
6232 + if (err && (lookupmode == INTERPOSE_LOOKUP))
6233 + unionfs_unlock_dentry(dentry);
6234 + if (!err && d_interposed)
6235 + return d_interposed;
6236 + if (dentry->d_inode && UNIONFS_I(dentry->d_inode)->stale &&
6237 + first_dentry_offset >= 0)
6238 + unionfs_mntput(dentry->d_sb->s_root, first_dentry_offset);
6239 + return ERR_PTR(err);
6243 + * This is a utility function that fills in a unionfs dentry.
6245 + * Returns: 0 (ok), or -ERRNO if an error occurred.
6247 +int unionfs_partial_lookup(struct dentry *dentry)
6249 + struct dentry *tmp;
6250 + struct nameidata nd = { .flags = 0 };
6251 + int err = -ENOSYS;
6253 + tmp = unionfs_lookup_backend(dentry, &nd, INTERPOSE_PARTIAL);
6258 + if (IS_ERR(tmp)) {
6259 + err = PTR_ERR(tmp);
6262 + /* need to change the interface */
6263 + BUG_ON(tmp != dentry);
6268 +/* The dentry cache is just so we have properly sized dentries. */
6269 +static struct kmem_cache *unionfs_dentry_cachep;
6270 +int unionfs_init_dentry_cache(void)
6272 + unionfs_dentry_cachep =
6273 + kmem_cache_create("unionfs_dentry",
6274 + sizeof(struct unionfs_dentry_info),
6275 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6277 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6280 +void unionfs_destroy_dentry_cache(void)
6282 + if (unionfs_dentry_cachep)
6283 + kmem_cache_destroy(unionfs_dentry_cachep);
6286 +void free_dentry_private_data(struct dentry *dentry)
6288 + if (!dentry || !dentry->d_fsdata)
6290 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6291 + dentry->d_fsdata = NULL;
6294 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6296 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6302 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6303 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6307 + info->lower_paths = p;
6309 + info->bstart = -1;
6311 + info->bopaque = -1;
6312 + info->bcount = sbmax(dentry->d_sb);
6313 + atomic_set(&info->generation,
6314 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6316 + memset(info->lower_paths, 0, size);
6321 +/* UNIONFS_D(dentry)->lock must be locked */
6322 +static int realloc_dentry_private_data(struct dentry *dentry)
6324 + if (!__realloc_dentry_private_data(dentry))
6327 + kfree(UNIONFS_D(dentry)->lower_paths);
6328 + free_dentry_private_data(dentry);
6332 +/* allocate new dentry private data */
6333 +int new_dentry_private_data(struct dentry *dentry)
6335 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6339 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6340 + if (unlikely(!info))
6343 + mutex_init(&info->lock);
6344 + mutex_lock(&info->lock);
6346 + info->lower_paths = NULL;
6348 + dentry->d_fsdata = info;
6350 + if (!__realloc_dentry_private_data(dentry))
6353 + mutex_unlock(&info->lock);
6354 + free_dentry_private_data(dentry);
6359 + * scan through the lower dentry objects, and set bstart to reflect the
6362 +void update_bstart(struct dentry *dentry)
6365 + int bstart = dbstart(dentry);
6366 + int bend = dbend(dentry);
6367 + struct dentry *lower_dentry;
6369 + for (bindex = bstart; bindex <= bend; bindex++) {
6370 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6371 + if (!lower_dentry)
6373 + if (lower_dentry->d_inode) {
6374 + set_dbstart(dentry, bindex);
6377 + dput(lower_dentry);
6378 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6384 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6385 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6386 + * Inside that nd structure, this function may also return an allocated
6387 + * struct file (for open intents). The caller, when done with this nd, must
6388 + * kfree the intent file (using release_lower_nd).
6390 + * XXX: this code, and the callers of this code, should be redone using
6391 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6392 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6393 + * function and a method that other file systems can call.
6395 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6398 +#ifdef ALLOC_LOWER_ND_FILE
6400 + * XXX: one day we may need to have the lower return an open file
6401 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6402 + * very well be needed for nfs4.
6404 + struct file *file;
6405 +#endif /* ALLOC_LOWER_ND_FILE */
6407 + memset(nd, 0, sizeof(struct nameidata));
6412 + case LOOKUP_CREATE:
6413 + nd->intent.open.flags |= O_CREAT;
6414 + /* fall through: shared code for create/open cases */
6416 + nd->flags = flags;
6417 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6418 +#ifdef ALLOC_LOWER_ND_FILE
6419 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6420 + if (unlikely(!file)) {
6422 + break; /* exit switch statement and thus return */
6424 + nd->intent.open.file = file;
6425 +#endif /* ALLOC_LOWER_ND_FILE */
6427 + case LOOKUP_ACCESS:
6428 + nd->flags = flags;
6432 + * We should never get here, for now.
6433 + * We can add new cases here later on.
6435 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6443 +void release_lower_nd(struct nameidata *nd, int err)
6445 + if (!nd->intent.open.file)
6448 + release_open_intent(nd);
6449 +#ifdef ALLOC_LOWER_ND_FILE
6450 + kfree(nd->intent.open.file);
6451 +#endif /* ALLOC_LOWER_ND_FILE */
6453 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6454 new file mode 100644
6455 index 0000000..ffb0da1
6457 +++ b/fs/unionfs/main.c
6460 + * Copyright (c) 2003-2007 Erez Zadok
6461 + * Copyright (c) 2003-2006 Charles P. Wright
6462 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6463 + * Copyright (c) 2005-2006 Junjiro Okajima
6464 + * Copyright (c) 2005 Arun M. Krishnakumar
6465 + * Copyright (c) 2004-2006 David P. Quigley
6466 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6467 + * Copyright (c) 2003 Puja Gupta
6468 + * Copyright (c) 2003 Harikesavan Krishnan
6469 + * Copyright (c) 2003-2007 Stony Brook University
6470 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
6472 + * This program is free software; you can redistribute it and/or modify
6473 + * it under the terms of the GNU General Public License version 2 as
6474 + * published by the Free Software Foundation.
6478 +#include <linux/module.h>
6479 +#include <linux/moduleparam.h>
6481 +static void unionfs_fill_inode(struct dentry *dentry,
6482 + struct inode *inode)
6484 + struct inode *lower_inode;
6485 + struct dentry *lower_dentry;
6486 + int bindex, bstart, bend;
6488 + bstart = dbstart(dentry);
6489 + bend = dbend(dentry);
6491 + for (bindex = bstart; bindex <= bend; bindex++) {
6492 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6493 + if (!lower_dentry) {
6494 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6498 + /* Initialize the lower inode to the new lower inode. */
6499 + if (!lower_dentry->d_inode)
6502 + unionfs_set_lower_inode_idx(inode, bindex,
6503 + igrab(lower_dentry->d_inode));
6506 + ibstart(inode) = dbstart(dentry);
6507 + ibend(inode) = dbend(dentry);
6509 + /* Use attributes from the first branch. */
6510 + lower_inode = unionfs_lower_inode(inode);
6512 + /* Use different set of inode ops for symlinks & directories */
6513 + if (S_ISLNK(lower_inode->i_mode))
6514 + inode->i_op = &unionfs_symlink_iops;
6515 + else if (S_ISDIR(lower_inode->i_mode))
6516 + inode->i_op = &unionfs_dir_iops;
6518 + /* Use different set of file ops for directories */
6519 + if (S_ISDIR(lower_inode->i_mode))
6520 + inode->i_fop = &unionfs_dir_fops;
6522 + /* properly initialize special inodes */
6523 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6524 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6525 + init_special_inode(inode, lower_inode->i_mode,
6526 + lower_inode->i_rdev);
6528 + /* all well, copy inode attributes */
6529 + unionfs_copy_attr_all(inode, lower_inode);
6530 + fsstack_copy_inode_size(inode, lower_inode);
6534 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6535 + * the classic stackable file system "vnode interposition" action.
6537 + * @sb: unionfs's super_block
6539 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6543 + struct inode *inode;
6544 + int is_negative_dentry = 1;
6545 + int bindex, bstart, bend;
6546 + int need_fill_inode = 1;
6547 + struct dentry *spliced = NULL;
6549 + verify_locked(dentry);
6551 + bstart = dbstart(dentry);
6552 + bend = dbend(dentry);
6554 + /* Make sure that we didn't get a negative dentry. */
6555 + for (bindex = bstart; bindex <= bend; bindex++) {
6556 + if (unionfs_lower_dentry_idx(dentry, bindex) &&
6557 + unionfs_lower_dentry_idx(dentry, bindex)->d_inode) {
6558 + is_negative_dentry = 0;
6562 + BUG_ON(is_negative_dentry);
6565 + * We allocate our new inode below, by calling iget.
6566 + * iget will call our read_inode which will initialize some
6567 + * of the new inode's fields
6571 + * On revalidate we've already got our own inode and just need
6574 + if (flag == INTERPOSE_REVAL) {
6575 + inode = dentry->d_inode;
6576 + UNIONFS_I(inode)->bstart = -1;
6577 + UNIONFS_I(inode)->bend = -1;
6578 + atomic_set(&UNIONFS_I(inode)->generation,
6579 + atomic_read(&UNIONFS_SB(sb)->generation));
6581 + UNIONFS_I(inode)->lower_inodes =
6582 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6583 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6588 + /* get unique inode number for unionfs */
6589 + inode = iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6594 + if (atomic_read(&inode->i_count) > 1)
6598 + need_fill_inode = 0;
6599 + unionfs_fill_inode(dentry, inode);
6602 + /* only (our) lookup wants to do a d_add */
6604 + case INTERPOSE_DEFAULT:
6605 + case INTERPOSE_REVAL_NEG:
6606 + d_instantiate(dentry, inode);
6608 + case INTERPOSE_LOOKUP:
6609 + spliced = d_splice_alias(inode, dentry);
6610 + if (IS_ERR(spliced)) {
6611 + err = PTR_ERR(spliced);
6612 + } else if (spliced && spliced != dentry) {
6614 + * d_splice can return a dentry if it was
6615 + * disconnected and had to be moved. We must ensure
6616 + * that the private data of the new dentry is
6617 + * correct and that the inode info was filled
6618 + * properly. Finally we must return this new
6621 + spliced->d_op = &unionfs_dops;
6622 + spliced->d_fsdata = dentry->d_fsdata;
6623 + dentry->d_fsdata = NULL;
6625 + if (need_fill_inode) {
6626 + need_fill_inode = 0;
6627 + unionfs_fill_inode(dentry, inode);
6632 + case INTERPOSE_REVAL:
6636 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6645 + return ERR_PTR(err);
6648 +/* like interpose above, but for an already existing dentry */
6649 +void unionfs_reinterpose(struct dentry *dentry)
6651 + struct dentry *lower_dentry;
6652 + struct inode *inode;
6653 + int bindex, bstart, bend;
6655 + verify_locked(dentry);
6657 + /* This is pre-allocated inode */
6658 + inode = dentry->d_inode;
6660 + bstart = dbstart(dentry);
6661 + bend = dbend(dentry);
6662 + for (bindex = bstart; bindex <= bend; bindex++) {
6663 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6664 + if (!lower_dentry)
6667 + if (!lower_dentry->d_inode)
6669 + if (unionfs_lower_inode_idx(inode, bindex))
6671 + unionfs_set_lower_inode_idx(inode, bindex,
6672 + igrab(lower_dentry->d_inode));
6674 + ibstart(inode) = dbstart(dentry);
6675 + ibend(inode) = dbend(dentry);
6679 + * make sure the branch we just looked up (nd) makes sense:
6681 + * 1) we're not trying to stack unionfs on top of unionfs
6683 + * 3) is a directory
6685 +int check_branch(struct nameidata *nd)
6687 + /* XXX: remove in ODF code -- stacking unions allowed there */
6688 + if (!strcmp(nd->dentry->d_sb->s_type->name, UNIONFS_NAME))
6690 + if (!nd->dentry->d_inode)
6692 + if (!S_ISDIR(nd->dentry->d_inode->i_mode))
6697 +/* checks if two lower_dentries have overlapping branches */
6698 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6700 + struct dentry *dent = NULL;
6703 + while ((dent != dent2) && (dent->d_parent != dent))
6704 + dent = dent->d_parent;
6706 + if (dent == dent2)
6710 + while ((dent != dent1) && (dent->d_parent != dent))
6711 + dent = dent->d_parent;
6713 + return (dent == dent1);
6717 + * Parse branch mode helper function
6719 +int __parse_branch_mode(const char *name)
6723 + if (!strcmp(name, "ro"))
6725 + if (!strcmp(name, "rw"))
6726 + return (MAY_READ | MAY_WRITE);
6731 + * Parse "ro" or "rw" options, but default to "rw" of no mode options
6734 +int parse_branch_mode(const char *name)
6736 + int perms = __parse_branch_mode(name);
6739 + perms = MAY_READ | MAY_WRITE;
6744 + * parse the dirs= mount argument
6746 + * We don't need to lock the superblock private data's rwsem, as we get
6747 + * called only by unionfs_read_super - it is still a long time before anyone
6748 + * can even get a reference to us.
6750 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6751 + *lower_root_info, char *options)
6753 + struct nameidata nd;
6760 + struct dentry *dent1;
6761 + struct dentry *dent2;
6763 + if (options[0] == '\0') {
6764 + printk(KERN_ERR "unionfs: no branches specified\n");
6770 + * Each colon means we have a separator, this is really just a rough
6771 + * guess, since strsep will handle empty fields for us.
6773 + for (i = 0; options[i]; i++)
6774 + if (options[i] == ':')
6777 + /* allocate space for underlying pointers to lower dentry */
6778 + UNIONFS_SB(sb)->data =
6779 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6780 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6785 + lower_root_info->lower_paths =
6786 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6787 + if (unlikely(!lower_root_info->lower_paths)) {
6792 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6794 + while ((name = strsep(&options, ":")) != NULL) {
6796 + char *mode = strchr(name, '=');
6800 + if (!*name) { /* bad use of ':' (extra colons) */
6807 + /* strip off '=' if any */
6811 + perms = parse_branch_mode(mode);
6812 + if (!bindex && !(perms & MAY_WRITE)) {
6817 + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6819 + printk(KERN_ERR "unionfs: error accessing "
6820 + "lower directory '%s' (error %d)\n",
6825 + err = check_branch(&nd);
6827 + printk(KERN_ERR "unionfs: lower directory "
6828 + "'%s' is not a valid branch\n", name);
6829 + path_release(&nd);
6833 + lower_root_info->lower_paths[bindex].dentry = nd.dentry;
6834 + lower_root_info->lower_paths[bindex].mnt = nd.mnt;
6836 + set_branchperms(sb, bindex, perms);
6837 + set_branch_count(sb, bindex, 0);
6838 + new_branch_id(sb, bindex);
6840 + if (lower_root_info->bstart < 0)
6841 + lower_root_info->bstart = bindex;
6842 + lower_root_info->bend = bindex;
6846 + if (branches == 0) {
6847 + printk(KERN_ERR "unionfs: no branches specified\n");
6852 + BUG_ON(branches != (lower_root_info->bend + 1));
6855 + * Ensure that no overlaps exist in the branches.
6857 + * This test is required because the Linux kernel has no support
6858 + * currently for ensuring coherency between stackable layers and
6859 + * branches. If we were to allow overlapping branches, it would be
6860 + * possible, for example, to delete a file via one branch, which
6861 + * would not be reflected in another branch. Such incoherency could
6862 + * lead to inconsistencies and even kernel oopses. Rather than
6863 + * implement hacks to work around some of these cache-coherency
6864 + * problems, we prevent branch overlapping, for now. A complete
6865 + * solution will involve proper kernel/VFS support for cache
6866 + * coherency, at which time we could safely remove this
6867 + * branch-overlapping test.
6869 + for (i = 0; i < branches; i++) {
6870 + dent1 = lower_root_info->lower_paths[i].dentry;
6871 + for (j = i + 1; j < branches; j++) {
6872 + dent2 = lower_root_info->lower_paths[j].dentry;
6873 + if (is_branch_overlap(dent1, dent2)) {
6874 + printk(KERN_ERR "unionfs: branches %d and "
6875 + "%d overlap\n", i, j);
6884 + for (i = 0; i < branches; i++)
6885 + if (lower_root_info->lower_paths[i].dentry) {
6886 + dput(lower_root_info->lower_paths[i].dentry);
6887 + /* initialize: can't use unionfs_mntput here */
6888 + mntput(lower_root_info->lower_paths[i].mnt);
6891 + kfree(lower_root_info->lower_paths);
6892 + kfree(UNIONFS_SB(sb)->data);
6895 + * MUST clear the pointers to prevent potential double free if
6896 + * the caller dies later on
6898 + lower_root_info->lower_paths = NULL;
6899 + UNIONFS_SB(sb)->data = NULL;
6905 + * Parse mount options. See the manual page for usage instructions.
6907 + * Returns the dentry object of the lower-level (lower) directory;
6908 + * We want to mount our stackable file system on top of that lower directory.
6910 +static struct unionfs_dentry_info *unionfs_parse_options(
6911 + struct super_block *sb,
6914 + struct unionfs_dentry_info *lower_root_info;
6918 + int dirsfound = 0;
6920 + /* allocate private data area */
6923 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6924 + if (unlikely(!lower_root_info))
6926 + lower_root_info->bstart = -1;
6927 + lower_root_info->bend = -1;
6928 + lower_root_info->bopaque = -1;
6930 + while ((optname = strsep(&options, ",")) != NULL) {
6935 + if (!optname || !*optname)
6938 + optarg = strchr(optname, '=');
6943 + * All of our options take an argument now. Insert ones that
6944 + * don't, above this check.
6947 + printk(KERN_ERR "unionfs: %s requires an argument\n",
6953 + if (!strcmp("dirs", optname)) {
6954 + if (++dirsfound > 1) {
6956 + "unionfs: multiple dirs specified\n");
6960 + err = parse_dirs_option(sb, lower_root_info, optarg);
6966 + /* All of these options require an integer argument. */
6967 + intval = simple_strtoul(optarg, &endptr, 0);
6970 + "unionfs: invalid %s option '%s'\n",
6978 + "unionfs: unrecognized option '%s'\n", optname);
6981 + if (dirsfound != 1) {
6982 + printk(KERN_ERR "unionfs: dirs option required\n");
6989 + if (lower_root_info && lower_root_info->lower_paths) {
6990 + for (bindex = lower_root_info->bstart;
6991 + bindex >= 0 && bindex <= lower_root_info->bend;
6994 + struct vfsmount *m;
6996 + d = lower_root_info->lower_paths[bindex].dentry;
6997 + m = lower_root_info->lower_paths[bindex].mnt;
7000 + /* initializing: can't use unionfs_mntput here */
7005 + kfree(lower_root_info->lower_paths);
7006 + kfree(lower_root_info);
7008 + kfree(UNIONFS_SB(sb)->data);
7009 + UNIONFS_SB(sb)->data = NULL;
7011 + lower_root_info = ERR_PTR(err);
7013 + return lower_root_info;
7017 + * our custom d_alloc_root work-alike
7019 + * we can't use d_alloc_root if we want to use our own interpose function
7020 + * unchanged, so we simply call our own "fake" d_alloc_root
7022 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7024 + struct dentry *ret = NULL;
7027 + static const struct qstr name = {
7032 + ret = d_alloc(NULL, &name);
7033 + if (likely(ret)) {
7034 + ret->d_op = &unionfs_dops;
7036 + ret->d_parent = ret;
7043 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7044 + * way anyone can have a reference to the superblock at this point in time.
7046 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7050 + struct unionfs_dentry_info *lower_root_info = NULL;
7051 + int bindex, bstart, bend;
7055 + "unionfs: read_super: missing data argument\n");
7060 + /* Allocate superblock private data */
7061 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7062 + if (unlikely(!UNIONFS_SB(sb))) {
7063 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7068 + UNIONFS_SB(sb)->bend = -1;
7069 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7070 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7071 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7073 + lower_root_info = unionfs_parse_options(sb, raw_data);
7074 + if (IS_ERR(lower_root_info)) {
7076 + "unionfs: read_super: error while parsing options "
7077 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7078 + err = PTR_ERR(lower_root_info);
7079 + lower_root_info = NULL;
7082 + if (lower_root_info->bstart == -1) {
7087 + /* set the lower superblock field of upper superblock */
7088 + bstart = lower_root_info->bstart;
7089 + BUG_ON(bstart != 0);
7090 + sbend(sb) = bend = lower_root_info->bend;
7091 + for (bindex = bstart; bindex <= bend; bindex++) {
7092 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7093 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7096 + /* max Bytes is the maximum bytes from highest priority branch */
7097 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7099 + sb->s_op = &unionfs_sops;
7101 + /* See comment next to the definition of unionfs_d_alloc_root */
7102 + sb->s_root = unionfs_d_alloc_root(sb);
7103 + if (unlikely(!sb->s_root)) {
7108 + /* link the upper and lower dentries */
7109 + sb->s_root->d_fsdata = NULL;
7110 + err = new_dentry_private_data(sb->s_root);
7111 + if (unlikely(err))
7114 + /* Set the lower dentries for s_root */
7115 + for (bindex = bstart; bindex <= bend; bindex++) {
7117 + struct vfsmount *m;
7119 + d = lower_root_info->lower_paths[bindex].dentry;
7120 + m = lower_root_info->lower_paths[bindex].mnt;
7122 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7123 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7125 + set_dbstart(sb->s_root, bstart);
7126 + set_dbend(sb->s_root, bend);
7128 + /* Set the generation number to one, since this is for the mount. */
7129 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7132 + * Call interpose to create the upper level inode. Only
7133 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7135 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7136 + unionfs_unlock_dentry(sb->s_root);
7139 + /* else fall through */
7142 + if (UNIONFS_D(sb->s_root)) {
7143 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7144 + free_dentry_private_data(sb->s_root);
7149 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7150 + for (bindex = lower_root_info->bstart;
7151 + bindex <= lower_root_info->bend; bindex++) {
7153 + struct vfsmount *m;
7155 + d = lower_root_info->lower_paths[bindex].dentry;
7156 + m = lower_root_info->lower_paths[bindex].mnt;
7159 + /* initializing: can't use unionfs_mntput here */
7162 + kfree(lower_root_info->lower_paths);
7163 + kfree(lower_root_info);
7164 + lower_root_info = NULL;
7168 + kfree(UNIONFS_SB(sb)->data);
7169 + kfree(UNIONFS_SB(sb));
7170 + sb->s_fs_info = NULL;
7173 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7174 + kfree(lower_root_info->lower_paths);
7175 + kfree(lower_root_info);
7180 +static int unionfs_get_sb(struct file_system_type *fs_type,
7181 + int flags, const char *dev_name,
7182 + void *raw_data, struct vfsmount *mnt)
7184 + return get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7187 +static struct file_system_type unionfs_fs_type = {
7188 + .owner = THIS_MODULE,
7189 + .name = UNIONFS_NAME,
7190 + .get_sb = unionfs_get_sb,
7191 + .kill_sb = generic_shutdown_super,
7192 + .fs_flags = FS_REVAL_DOT,
7195 +static int __init init_unionfs_fs(void)
7199 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7201 + err = unionfs_init_filldir_cache();
7202 + if (unlikely(err))
7204 + err = unionfs_init_inode_cache();
7205 + if (unlikely(err))
7207 + err = unionfs_init_dentry_cache();
7208 + if (unlikely(err))
7210 + err = init_sioq();
7211 + if (unlikely(err))
7213 + err = register_filesystem(&unionfs_fs_type);
7215 + if (unlikely(err)) {
7217 + unionfs_destroy_filldir_cache();
7218 + unionfs_destroy_inode_cache();
7219 + unionfs_destroy_dentry_cache();
7224 +static void __exit exit_unionfs_fs(void)
7227 + unionfs_destroy_filldir_cache();
7228 + unionfs_destroy_inode_cache();
7229 + unionfs_destroy_dentry_cache();
7230 + unregister_filesystem(&unionfs_fs_type);
7231 + pr_info("Completed unionfs module unload\n");
7234 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7235 + " (http://www.fsl.cs.sunysb.edu)");
7236 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7237 + " (http://unionfs.filesystems.org)");
7238 +MODULE_LICENSE("GPL");
7240 +module_init(init_unionfs_fs);
7241 +module_exit(exit_unionfs_fs);
7242 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7243 new file mode 100644
7244 index 0000000..6440282
7246 +++ b/fs/unionfs/mmap.c
7249 + * Copyright (c) 2003-2007 Erez Zadok
7250 + * Copyright (c) 2003-2006 Charles P. Wright
7251 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7252 + * Copyright (c) 2005-2006 Junjiro Okajima
7253 + * Copyright (c) 2006 Shaya Potter
7254 + * Copyright (c) 2005 Arun M. Krishnakumar
7255 + * Copyright (c) 2004-2006 David P. Quigley
7256 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7257 + * Copyright (c) 2003 Puja Gupta
7258 + * Copyright (c) 2003 Harikesavan Krishnan
7259 + * Copyright (c) 2003-2007 Stony Brook University
7260 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
7262 + * This program is free software; you can redistribute it and/or modify
7263 + * it under the terms of the GNU General Public License version 2 as
7264 + * published by the Free Software Foundation.
7270 + * Unionfs doesn't implement ->writepages, which is OK with the VFS and
7271 + * keeps our code simpler and smaller. Nevertheless, somehow, our own
7272 + * ->writepage must be called so we can sync the upper pages with the lower
7273 + * pages: otherwise data changed at the upper layer won't get written to the
7276 + * Some lower file systems (e.g., NFS) expect the VFS to call its writepages
7277 + * only, which in turn will call generic_writepages and invoke each of the
7278 + * lower file system's ->writepage. NFS in particular uses the
7279 + * wbc->fs_private field in its nfs_writepage, which is set in its
7280 + * nfs_writepages. So if we don't call the lower nfs_writepages first, then
7281 + * NFS's nfs_writepage will dereference a NULL wbc->fs_private and cause an
7282 + * OOPS. If, however, we implement a unionfs_writepages and then we do call
7283 + * the lower nfs_writepages, then we "lose control" over the pages we're
7284 + * trying to write to the lower file system: we won't be writing our own
7285 + * new/modified data from the upper pages to the lower pages, and any
7286 + * mmap-based changes are lost.
7288 + * This is a fundamental cache-coherency problem in Linux. The kernel isn't
7289 + * able to support such stacking abstractions cleanly. One possible clean
7290 + * way would be that a lower file system's ->writepage method have some sort
7291 + * of a callback to validate if any upper pages for the same file+offset
7292 + * exist and have newer content in them.
7294 + * This whole NULL ptr dereference is triggered at the lower file system
7295 + * (NFS) because the wbc->for_writepages is set to 1. Therefore, to avoid
7296 + * this NULL pointer dereference, we set this flag to 0 and restore it upon
7297 + * exit. This probably means that we're slightly less efficient in writing
7298 + * pages out, doing them one at a time, but at least we avoid the oops until
7299 + * such day as Linux can better support address_space_ops in a stackable
7302 +static int unionfs_writepage(struct page *page, struct writeback_control *wbc)
7305 + struct inode *inode;
7306 + struct inode *lower_inode;
7307 + struct page *lower_page;
7308 + char *kaddr, *lower_kaddr;
7309 + int saved_for_writepages = wbc->for_writepages;
7311 + inode = page->mapping->host;
7312 + lower_inode = unionfs_lower_inode(inode);
7315 + * find lower page (returns a locked page)
7317 + * NOTE: we used to call grab_cache_page(), but that was unnecessary
7318 + * as it would have tried to create a new lower page if it didn't
7319 + * exist, leading to deadlocks (esp. under memory-pressure
7320 + * conditions, when it is really a bad idea to *consume* more
7321 + * memory). Instead, we assume the lower page exists, and if we can
7322 + * find it, then we ->writepage on it; if we can't find it, then it
7323 + * couldn't have disappeared unless the kernel already flushed it,
7324 + * in which case we're still OK. This is especially correct if
7325 + * wbc->sync_mode is WB_SYNC_NONE (as per
7326 + * Documentation/filesystems/vfs.txt). If we can't flush our page
7327 + * because we can't find a lower page, then at least we re-mark our
7328 + * page as dirty, and return AOP_WRITEPAGE_ACTIVATE as the VFS
7329 + * expects us to. (Note, if in the future it'd turn out that we
7330 + * have to find a lower page no matter what, then we'd have to
7331 + * resort to RAIF's page pointer flipping trick.)
7333 + lower_page = find_lock_page(lower_inode->i_mapping, page->index);
7334 + if (!lower_page) {
7335 + err = AOP_WRITEPAGE_ACTIVATE;
7336 + set_page_dirty(page);
7340 + /* get page address, and encode it */
7341 + kaddr = kmap(page);
7342 + lower_kaddr = kmap(lower_page);
7344 + memcpy(lower_kaddr, kaddr, PAGE_CACHE_SIZE);
7347 + kunmap(lower_page);
7349 + BUG_ON(!lower_inode->i_mapping->a_ops->writepage);
7351 + /* workaround for some lower file systems: see big comment on top */
7352 + if (wbc->for_writepages && !wbc->fs_private)
7353 + wbc->for_writepages = 0;
7355 + /* call lower writepage (expects locked page) */
7356 + clear_page_dirty_for_io(lower_page); /* emulate VFS behavior */
7357 + err = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
7358 + wbc->for_writepages = saved_for_writepages; /* restore value */
7360 + /* b/c find_lock_page locked it and ->writepage unlocks on success */
7362 + unlock_page(lower_page);
7363 + /* b/c grab_cache_page increased refcnt */
7364 + page_cache_release(lower_page);
7367 + ClearPageUptodate(page);
7370 + if (err == AOP_WRITEPAGE_ACTIVATE) {
7372 + * Lower file systems such as ramfs and tmpfs, may return
7373 + * AOP_WRITEPAGE_ACTIVATE so that the VM won't try to
7374 + * (pointlessly) write the page again for a while. But
7375 + * those lower file systems also set the page dirty bit back
7376 + * again. So we mimic that behaviour here.
7378 + if (PageDirty(lower_page))
7379 + set_page_dirty(page);
7384 + SetPageUptodate(page);
7385 + /* lower mtimes has changed: update ours */
7386 + unionfs_copy_attr_times(inode);
7388 + unlock_page(page);
7395 + * readpage is called from generic_page_read and the fault handler.
7396 + * If your file system uses generic_page_read for the read op, it
7397 + * must implement readpage.
7399 + * Readpage expects a locked page, and must unlock it.
7401 +static int unionfs_do_readpage(struct file *file, struct page *page)
7404 + struct file *lower_file;
7405 + struct inode *inode;
7406 + mm_segment_t old_fs;
7407 + char *page_data = NULL;
7410 + if (!UNIONFS_F(file)) {
7415 + lower_file = unionfs_lower_file(file);
7416 + /* FIXME: is this assertion right here? */
7417 + BUG_ON(lower_file == NULL);
7419 + inode = file->f_path.dentry->d_inode;
7421 + page_data = (char *) kmap(page);
7423 + * Use vfs_read because some lower file systems don't have a
7424 + * readpage method, and some file systems (esp. distributed ones)
7425 + * don't like their pages to be accessed directly. Using vfs_read
7426 + * may be a little slower, but a lot safer, as the VFS does a lot of
7427 + * the necessary magic for us.
7429 + lower_file->f_pos = page_offset(page);
7430 + offset = page_offset(page);
7431 + old_fs = get_fs();
7432 + set_fs(KERNEL_DS);
7433 + err = vfs_read(lower_file, page_data, PAGE_CACHE_SIZE,
7434 + &lower_file->f_pos);
7443 + /* if vfs_read succeeded above, sync up our times */
7444 + unionfs_copy_attr_times(inode);
7446 + flush_dcache_page(page);
7450 + SetPageUptodate(page);
7452 + ClearPageUptodate(page);
7457 +static int unionfs_readpage(struct file *file, struct page *page)
7461 + unionfs_read_lock(file->f_path.dentry->d_sb);
7462 + err = unionfs_file_revalidate(file, false);
7463 + if (unlikely(err))
7465 + unionfs_check_file(file);
7467 + err = unionfs_do_readpage(file, page);
7470 + touch_atime(unionfs_lower_mnt(file->f_path.dentry),
7471 + unionfs_lower_dentry(file->f_path.dentry));
7472 + unionfs_copy_attr_times(file->f_path.dentry->d_inode);
7476 + * we have to unlock our page, b/c we _might_ have gotten a locked
7477 + * page. but we no longer have to wakeup on our page here, b/c
7478 + * UnlockPage does it
7481 + unlock_page(page);
7482 + unionfs_check_file(file);
7483 + unionfs_read_unlock(file->f_path.dentry->d_sb);
7488 +static int unionfs_prepare_write(struct file *file, struct page *page,
7489 + unsigned from, unsigned to)
7493 + unionfs_read_lock(file->f_path.dentry->d_sb);
7495 + * This is the only place where we unconditionally copy the lower
7496 + * attribute times before calling unionfs_file_revalidate. The
7497 + * reason is that our ->write calls do_sync_write which in turn will
7498 + * call our ->prepare_write and then ->commit_write. Before our
7499 + * ->write is called, the lower mtimes are in sync, but by the time
7500 + * the VFS calls our ->commit_write, the lower mtimes have changed.
7501 + * Therefore, the only reasonable time for us to sync up from the
7502 + * changed lower mtimes, and avoid an invariant violation warning,
7503 + * is here, in ->prepare_write.
7505 + unionfs_copy_attr_times(file->f_path.dentry->d_inode);
7506 + err = unionfs_file_revalidate(file, true);
7507 + unionfs_check_file(file);
7508 + unionfs_read_unlock(file->f_path.dentry->d_sb);
7513 +static int unionfs_commit_write(struct file *file, struct page *page,
7514 + unsigned from, unsigned to)
7516 + int err = -ENOMEM;
7517 + struct inode *inode, *lower_inode;
7518 + struct file *lower_file = NULL;
7520 + unsigned bytes = to - from;
7521 + char *page_data = NULL;
7522 + mm_segment_t old_fs;
7524 + BUG_ON(file == NULL);
7526 + unionfs_read_lock(file->f_path.dentry->d_sb);
7527 + err = unionfs_file_revalidate(file, true);
7528 + if (unlikely(err))
7530 + unionfs_check_file(file);
7532 + inode = page->mapping->host;
7533 + lower_inode = unionfs_lower_inode(inode);
7535 + if (UNIONFS_F(file) != NULL)
7536 + lower_file = unionfs_lower_file(file);
7538 + /* FIXME: is this assertion right here? */
7539 + BUG_ON(lower_file == NULL);
7541 + page_data = (char *)kmap(page);
7542 + lower_file->f_pos = page_offset(page) + from;
7545 + * We use vfs_write instead of copying page data and the
7546 + * prepare_write/commit_write combo because file system's like
7547 + * GFS/OCFS2 don't like things touching those directly,
7548 + * calling the underlying write op, while a little bit slower, will
7549 + * call all the FS specific code as well
7551 + old_fs = get_fs();
7552 + set_fs(KERNEL_DS);
7553 + err = vfs_write(lower_file, page_data + from, bytes,
7554 + &lower_file->f_pos);
7562 + inode->i_blocks = lower_inode->i_blocks;
7563 + /* we may have to update i_size */
7564 + pos = page_offset(page) + to;
7565 + if (pos > i_size_read(inode))
7566 + i_size_write(inode, pos);
7567 + /* if vfs_write succeeded above, sync up our times */
7568 + unionfs_copy_attr_times(inode);
7569 + mark_inode_dirty_sync(inode);
7573 + ClearPageUptodate(page);
7575 + unionfs_read_unlock(file->f_path.dentry->d_sb);
7576 + unionfs_check_file(file);
7577 + return err; /* assume all is ok */
7580 +static void unionfs_sync_page(struct page *page)
7582 + struct inode *inode;
7583 + struct inode *lower_inode;
7584 + struct page *lower_page;
7585 + struct address_space *mapping;
7587 + inode = page->mapping->host;
7588 + lower_inode = unionfs_lower_inode(inode);
7591 + * Find lower page (returns a locked page).
7593 + * NOTE: we used to call grab_cache_page(), but that was unnecessary
7594 + * as it would have tried to create a new lower page if it didn't
7595 + * exist, leading to deadlocks. All our sync_page method needs to
7596 + * do is ensure that pending I/O gets done.
7598 + lower_page = find_lock_page(lower_inode->i_mapping, page->index);
7599 + if (!lower_page) {
7600 + printk(KERN_ERR "unionfs: find_lock_page failed\n");
7604 + /* do the actual sync */
7605 + mapping = lower_page->mapping;
7607 + * XXX: can we optimize ala RAIF and set the lower page to be
7608 + * discarded after a successful sync_page?
7610 + if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
7611 + mapping->a_ops->sync_page(lower_page);
7613 + /* b/c find_lock_page locked it */
7614 + unlock_page(lower_page);
7615 + /* b/c find_lock_page increased refcnt */
7616 + page_cache_release(lower_page);
7622 +struct address_space_operations unionfs_aops = {
7623 + .writepage = unionfs_writepage,
7624 + .readpage = unionfs_readpage,
7625 + .prepare_write = unionfs_prepare_write,
7626 + .commit_write = unionfs_commit_write,
7627 + .sync_page = unionfs_sync_page,
7629 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7630 new file mode 100644
7631 index 0000000..0df5f52
7633 +++ b/fs/unionfs/rdstate.c
7636 + * Copyright (c) 2003-2007 Erez Zadok
7637 + * Copyright (c) 2003-2006 Charles P. Wright
7638 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7639 + * Copyright (c) 2005-2006 Junjiro Okajima
7640 + * Copyright (c) 2005 Arun M. Krishnakumar
7641 + * Copyright (c) 2004-2006 David P. Quigley
7642 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7643 + * Copyright (c) 2003 Puja Gupta
7644 + * Copyright (c) 2003 Harikesavan Krishnan
7645 + * Copyright (c) 2003-2007 Stony Brook University
7646 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
7648 + * This program is free software; you can redistribute it and/or modify
7649 + * it under the terms of the GNU General Public License version 2 as
7650 + * published by the Free Software Foundation.
7655 +/* This file contains the routines for maintaining readdir state. */
7658 + * There are two structures here, rdstate which is a hash table
7659 + * of the second structure which is a filldir_node.
7663 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7664 + * of them and they shouldn't waste memory. If the node has a small name
7665 + * (as defined by the dentry structure), then we use an inline name to
7666 + * preserve kmalloc space.
7668 +static struct kmem_cache *unionfs_filldir_cachep;
7670 +int unionfs_init_filldir_cache(void)
7672 + unionfs_filldir_cachep =
7673 + kmem_cache_create("unionfs_filldir",
7674 + sizeof(struct filldir_node), 0,
7675 + SLAB_RECLAIM_ACCOUNT, NULL);
7677 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7680 +void unionfs_destroy_filldir_cache(void)
7682 + if (unionfs_filldir_cachep)
7683 + kmem_cache_destroy(unionfs_filldir_cachep);
7687 + * This is a tuning parameter that tells us roughly how big to make the
7688 + * hash table in directory entries per page. This isn't perfect, but
7689 + * at least we get a hash table size that shouldn't be too overloaded.
7690 + * The following averages are based on my home directory.
7691 + * 14.44693 Overall
7692 + * 12.29 Single Page Directories
7693 + * 117.93 Multi-page directories
7695 +#define DENTPAGE 4096
7696 +#define DENTPERONEPAGE 12
7697 +#define DENTPERPAGE 118
7698 +#define MINHASHSIZE 1
7699 +static int guesstimate_hash_size(struct inode *inode)
7701 + struct inode *lower_inode;
7703 + int hashsize = MINHASHSIZE;
7705 + if (UNIONFS_I(inode)->hashsize > 0)
7706 + return UNIONFS_I(inode)->hashsize;
7708 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7709 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7713 + if (lower_inode->i_size == DENTPAGE)
7714 + hashsize += DENTPERONEPAGE;
7716 + hashsize += (lower_inode->i_size / DENTPAGE) *
7723 +int init_rdstate(struct file *file)
7725 + BUG_ON(sizeof(loff_t) !=
7726 + (sizeof(unsigned int) + sizeof(unsigned int)));
7727 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7729 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7732 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7735 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7737 + struct unionfs_dir_state *rdstate = NULL;
7738 + struct list_head *pos;
7740 + spin_lock(&UNIONFS_I(inode)->rdlock);
7741 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7742 + struct unionfs_dir_state *r =
7743 + list_entry(pos, struct unionfs_dir_state, cache);
7744 + if (fpos == rdstate2offset(r)) {
7745 + UNIONFS_I(inode)->rdcount--;
7746 + list_del(&r->cache);
7751 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7755 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7759 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7760 + struct unionfs_dir_state *rdstate;
7762 + hashsize = guesstimate_hash_size(inode);
7763 + mallocsize += hashsize * sizeof(struct list_head);
7764 + mallocsize = __roundup_pow_of_two(mallocsize);
7766 + /* This should give us about 500 entries anyway. */
7767 + if (mallocsize > PAGE_SIZE)
7768 + mallocsize = PAGE_SIZE;
7770 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7771 + sizeof(struct list_head);
7773 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7774 + if (unlikely(!rdstate))
7777 + spin_lock(&UNIONFS_I(inode)->rdlock);
7778 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7779 + UNIONFS_I(inode)->cookie = 1;
7781 + UNIONFS_I(inode)->cookie++;
7783 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7784 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7785 + rdstate->offset = 1;
7786 + rdstate->access = jiffies;
7787 + rdstate->bindex = bindex;
7788 + rdstate->dirpos = 0;
7789 + rdstate->hashentries = 0;
7790 + rdstate->size = hashsize;
7791 + for (i = 0; i < rdstate->size; i++)
7792 + INIT_LIST_HEAD(&rdstate->list[i]);
7797 +static void free_filldir_node(struct filldir_node *node)
7799 + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7800 + kfree(node->name);
7801 + kmem_cache_free(unionfs_filldir_cachep, node);
7804 +void free_rdstate(struct unionfs_dir_state *state)
7806 + struct filldir_node *tmp;
7809 + for (i = 0; i < state->size; i++) {
7810 + struct list_head *head = &(state->list[i]);
7811 + struct list_head *pos, *n;
7813 + /* traverse the list and deallocate space */
7814 + list_for_each_safe(pos, n, head) {
7815 + tmp = list_entry(pos, struct filldir_node, file_list);
7816 + list_del(&tmp->file_list);
7817 + free_filldir_node(tmp);
7824 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7825 + const char *name, int namelen)
7828 + unsigned int hash;
7829 + struct list_head *head;
7830 + struct list_head *pos;
7831 + struct filldir_node *cursor = NULL;
7834 + BUG_ON(namelen <= 0);
7836 + hash = full_name_hash(name, namelen);
7837 + index = hash % rdstate->size;
7839 + head = &(rdstate->list[index]);
7840 + list_for_each(pos, head) {
7841 + cursor = list_entry(pos, struct filldir_node, file_list);
7843 + if (cursor->namelen == namelen && cursor->hash == hash &&
7844 + !strncmp(cursor->name, name, namelen)) {
7846 + * a duplicate exists, and hence no need to create
7847 + * entry to the list
7852 + * if the duplicate is in this branch, then the file
7853 + * system is corrupted.
7855 + if (unlikely(cursor->bindex == rdstate->bindex))
7856 + printk(KERN_ERR "unionfs: filldir: possible "
7857 + "I/O error: a file is duplicated "
7858 + "in the same branch %d: %s\n",
7859 + rdstate->bindex, cursor->name);
7870 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7871 + int namelen, int bindex, int whiteout)
7873 + struct filldir_node *new;
7874 + unsigned int hash;
7877 + struct list_head *head;
7879 + BUG_ON(namelen <= 0);
7881 + hash = full_name_hash(name, namelen);
7882 + index = hash % rdstate->size;
7883 + head = &(rdstate->list[index]);
7885 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7886 + if (unlikely(!new)) {
7891 + INIT_LIST_HEAD(&new->file_list);
7892 + new->namelen = namelen;
7894 + new->bindex = bindex;
7895 + new->whiteout = whiteout;
7897 + if (namelen < DNAME_INLINE_LEN_MIN) {
7898 + new->name = new->iname;
7900 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7901 + if (unlikely(!new->name)) {
7902 + kmem_cache_free(unionfs_filldir_cachep, new);
7908 + memcpy(new->name, name, namelen);
7909 + new->name[namelen] = '\0';
7911 + rdstate->hashentries++;
7913 + list_add(&(new->file_list), head);
7917 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7918 new file mode 100644
7919 index 0000000..91d41d4
7921 +++ b/fs/unionfs/rename.c
7924 + * Copyright (c) 2003-2007 Erez Zadok
7925 + * Copyright (c) 2003-2006 Charles P. Wright
7926 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7927 + * Copyright (c) 2005-2006 Junjiro Okajima
7928 + * Copyright (c) 2005 Arun M. Krishnakumar
7929 + * Copyright (c) 2004-2006 David P. Quigley
7930 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7931 + * Copyright (c) 2003 Puja Gupta
7932 + * Copyright (c) 2003 Harikesavan Krishnan
7933 + * Copyright (c) 2003-2007 Stony Brook University
7934 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
7936 + * This program is free software; you can redistribute it and/or modify
7937 + * it under the terms of the GNU General Public License version 2 as
7938 + * published by the Free Software Foundation.
7943 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7944 + struct inode *new_dir, struct dentry *new_dentry,
7945 + int bindex, struct dentry **wh_old)
7948 + struct dentry *lower_old_dentry;
7949 + struct dentry *lower_new_dentry;
7950 + struct dentry *lower_old_dir_dentry;
7951 + struct dentry *lower_new_dir_dentry;
7952 + struct dentry *lower_wh_dentry;
7953 + struct dentry *lower_wh_dir_dentry;
7954 + char *wh_name = NULL;
7956 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7957 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7959 + if (!lower_new_dentry) {
7960 + lower_new_dentry =
7961 + create_parents(new_dentry->d_parent->d_inode,
7962 + new_dentry, new_dentry->d_name.name,
7964 + if (IS_ERR(lower_new_dentry)) {
7965 + printk(KERN_ERR "unionfs: error creating directory "
7966 + "tree for rename, bindex = %d, err = %ld\n",
7967 + bindex, PTR_ERR(lower_new_dentry));
7968 + err = PTR_ERR(lower_new_dentry);
7973 + wh_name = alloc_whname(new_dentry->d_name.name,
7974 + new_dentry->d_name.len);
7975 + if (unlikely(IS_ERR(wh_name))) {
7976 + err = PTR_ERR(wh_name);
7980 + lower_wh_dentry = lookup_one_len(wh_name, lower_new_dentry->d_parent,
7981 + new_dentry->d_name.len +
7983 + if (IS_ERR(lower_wh_dentry)) {
7984 + err = PTR_ERR(lower_wh_dentry);
7988 + if (lower_wh_dentry->d_inode) {
7989 + /* get rid of the whiteout that is existing */
7990 + if (lower_new_dentry->d_inode) {
7991 + printk(KERN_ERR "unionfs: both a whiteout and a "
7992 + "dentry exist when doing a rename!\n");
7995 + dput(lower_wh_dentry);
7999 + lower_wh_dir_dentry = lock_parent(lower_wh_dentry);
8000 + err = is_robranch_super(old_dentry->d_sb, bindex);
8002 + err = vfs_unlink(lower_wh_dir_dentry->d_inode,
8005 + dput(lower_wh_dentry);
8006 + unlock_dir(lower_wh_dir_dentry);
8010 + dput(lower_wh_dentry);
8013 + dget(lower_old_dentry);
8014 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
8015 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
8017 + lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
8019 + err = is_robranch_super(old_dentry->d_sb, bindex);
8024 + * ready to whiteout for old_dentry. caller will create the actual
8025 + * whiteout, and must dput(*wh_old)
8029 + whname = alloc_whname(old_dentry->d_name.name,
8030 + old_dentry->d_name.len);
8031 + err = PTR_ERR(whname);
8032 + if (unlikely(IS_ERR(whname)))
8034 + *wh_old = lookup_one_len(whname, lower_old_dir_dentry,
8035 + old_dentry->d_name.len +
8038 + err = PTR_ERR(*wh_old);
8039 + if (IS_ERR(*wh_old)) {
8045 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
8046 + lower_new_dir_dentry->d_inode, lower_new_dentry);
8049 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
8051 + dput(lower_old_dir_dentry);
8052 + dput(lower_new_dir_dentry);
8053 + dput(lower_old_dentry);
8057 + /* Fixup the new_dentry. */
8058 + if (bindex < dbstart(new_dentry))
8059 + set_dbstart(new_dentry, bindex);
8060 + else if (bindex > dbend(new_dentry))
8061 + set_dbend(new_dentry, bindex);
8070 + * Main rename code. This is sufficiently complex, that it's documented in
8071 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
8072 + * __unionfs_rename() above to perform some of the work.
8074 +static int do_unionfs_rename(struct inode *old_dir,
8075 + struct dentry *old_dentry,
8076 + struct inode *new_dir,
8077 + struct dentry *new_dentry)
8080 + int bindex, bwh_old;
8081 + int old_bstart, old_bend;
8082 + int new_bstart, new_bend;
8083 + int do_copyup = -1;
8084 + struct dentry *parent_dentry;
8085 + int local_err = 0;
8088 + struct dentry *wh_old = NULL;
8090 + old_bstart = dbstart(old_dentry);
8091 + bwh_old = old_bstart;
8092 + old_bend = dbend(old_dentry);
8093 + parent_dentry = old_dentry->d_parent;
8095 + new_bstart = dbstart(new_dentry);
8096 + new_bend = dbend(new_dentry);
8098 + /* Rename source to destination. */
8099 + err = __unionfs_rename(old_dir, old_dentry, new_dir, new_dentry,
8100 + old_bstart, &wh_old);
8102 + if (!IS_COPYUP_ERR(err))
8104 + do_copyup = old_bstart - 1;
8110 + * Unlink all instances of destination that exist to the left of
8111 + * bstart of source. On error, revert back, goto out.
8113 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
8114 + struct dentry *unlink_dentry;
8115 + struct dentry *unlink_dir_dentry;
8117 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
8118 + if (!unlink_dentry)
8121 + unlink_dir_dentry = lock_parent(unlink_dentry);
8122 + err = is_robranch_super(old_dir->i_sb, bindex);
8124 + err = vfs_unlink(unlink_dir_dentry->d_inode,
8127 + fsstack_copy_attr_times(new_dentry->d_parent->d_inode,
8128 + unlink_dir_dentry->d_inode);
8129 + /* propagate number of hard-links */
8130 + new_dentry->d_parent->d_inode->i_nlink =
8131 + unionfs_get_nlinks(new_dentry->d_parent->d_inode);
8133 + unlock_dir(unlink_dir_dentry);
8135 + if (bindex != new_bstart) {
8136 + dput(unlink_dentry);
8137 + unionfs_set_lower_dentry_idx(new_dentry,
8140 + } else if (IS_COPYUP_ERR(err)) {
8141 + do_copyup = bindex - 1;
8142 + } else if (revert) {
8148 + if (do_copyup != -1) {
8149 + for (bindex = do_copyup; bindex >= 0; bindex--) {
8151 + * copyup the file into some left directory, so that
8152 + * you can rename it
8154 + err = copyup_dentry(old_dentry->d_parent->d_inode,
8155 + old_dentry, old_bstart, bindex,
8156 + old_dentry->d_name.name,
8157 + old_dentry->d_name.len,
8158 + NULL, old_dentry->d_inode->i_size);
8159 + /* if copyup failed, try next branch to the left */
8164 + err = __unionfs_rename(old_dir, old_dentry,
8165 + new_dir, new_dentry,
8171 + /* make it opaque */
8172 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
8173 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
8179 + * Create whiteout for source, only if:
8180 + * (1) There is more than one underlying instance of source.
8181 + * (2) We did a copy_up
8183 + if ((old_bstart != old_bend) || (do_copyup != -1)) {
8184 + struct dentry *lower_parent;
8185 + struct nameidata nd;
8186 + if (!wh_old || wh_old->d_inode || bwh_old < 0) {
8187 + printk(KERN_ERR "unionfs: rename error "
8188 + "(wh_old=%p/%p bwh_old=%d)\n", wh_old,
8189 + (wh_old ? wh_old->d_inode : NULL), bwh_old);
8193 + err = init_lower_nd(&nd, LOOKUP_CREATE);
8194 + if (unlikely(err < 0))
8196 + lower_parent = lock_parent(wh_old);
8197 + local_err = vfs_create(lower_parent->d_inode, wh_old, S_IRUGO,
8199 + unlock_dir(lower_parent);
8201 + set_dbopaque(old_dentry, bwh_old);
8204 + * we can't fix anything now, so we cop-out and use
8207 + printk(KERN_ERR "unionfs: can't create a whiteout for "
8208 + "the source in rename!\n");
8211 + release_lower_nd(&nd, local_err);
8219 + /* Do revert here. */
8220 + local_err = unionfs_refresh_lower_dentry(new_dentry, old_bstart);
8222 + printk(KERN_ERR "unionfs: revert failed in rename: "
8223 + "the new refresh failed\n");
8227 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_bstart);
8229 + printk(KERN_ERR "unionfs: revert failed in rename: "
8230 + "the old refresh failed\n");
8235 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
8236 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
8237 + printk(KERN_ERR "unionfs: revert failed in rename: "
8238 + "the object disappeared from under us!\n");
8243 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
8244 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
8245 + printk(KERN_ERR "unionfs: revert failed in rename: "
8246 + "the object was created underneath us!\n");
8251 + local_err = __unionfs_rename(new_dir, new_dentry,
8252 + old_dir, old_dentry, old_bstart, NULL);
8254 + /* If we can't fix it, then we cop-out with -EIO. */
8256 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
8260 + local_err = unionfs_refresh_lower_dentry(new_dentry, bindex);
8263 + local_err = unionfs_refresh_lower_dentry(old_dentry, bindex);
8273 +static struct dentry *lookup_whiteout(struct dentry *dentry)
8276 + int bindex = -1, bstart = -1, bend = -1;
8277 + struct dentry *parent, *lower_parent, *wh_dentry;
8279 + whname = alloc_whname(dentry->d_name.name, dentry->d_name.len);
8280 + if (unlikely(IS_ERR(whname)))
8281 + return (void *)whname;
8283 + parent = dget_parent(dentry);
8284 + unionfs_lock_dentry(parent);
8285 + bstart = dbstart(parent);
8286 + bend = dbend(parent);
8287 + wh_dentry = ERR_PTR(-ENOENT);
8288 + for (bindex = bstart; bindex <= bend; bindex++) {
8289 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
8290 + if (!lower_parent)
8292 + wh_dentry = lookup_one_len(whname, lower_parent,
8293 + dentry->d_name.len + UNIONFS_WHLEN);
8294 + if (IS_ERR(wh_dentry))
8296 + if (wh_dentry->d_inode)
8299 + wh_dentry = ERR_PTR(-ENOENT);
8301 + unionfs_unlock_dentry(parent);
8308 + * We can't copyup a directory, because it may involve huge numbers of
8309 + * children, etc. Doing that in the kernel would be bad, so instead we
8310 + * return EXDEV to the user-space utility that caused this, and let the
8311 + * user-space recurse and ask us to copy up each file separately.
8313 +static int may_rename_dir(struct dentry *dentry)
8317 + err = check_empty(dentry, NULL);
8318 + if (err == -ENOTEMPTY) {
8319 + if (is_robranch(dentry))
8325 + bstart = dbstart(dentry);
8326 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8329 + set_dbstart(dentry, bstart + 1);
8330 + err = check_empty(dentry, NULL);
8331 + set_dbstart(dentry, bstart);
8332 + if (err == -ENOTEMPTY)
8337 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8338 + struct inode *new_dir, struct dentry *new_dentry)
8341 + struct dentry *wh_dentry;
8343 + unionfs_read_lock(old_dentry->d_sb);
8344 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8346 + if (unlikely(!__unionfs_d_revalidate_chain(old_dentry, NULL, false))) {
8350 + if (unlikely(!d_deleted(new_dentry) && new_dentry->d_inode &&
8351 + !__unionfs_d_revalidate_chain(new_dentry, NULL, false))) {
8356 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8357 + err = unionfs_partial_lookup(old_dentry);
8359 + err = may_rename_dir(old_dentry);
8364 + err = unionfs_partial_lookup(new_dentry);
8369 + * if new_dentry is already lower because of whiteout,
8370 + * simply override it even if the whited-out dir is not empty.
8372 + wh_dentry = lookup_whiteout(new_dentry);
8373 + if (!IS_ERR(wh_dentry)) {
8375 + } else if (new_dentry->d_inode) {
8376 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8377 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8378 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8379 + -ENOTDIR : -EISDIR;
8383 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8384 + struct unionfs_dir_state *namelist;
8385 + /* check if this unionfs directory is empty or not */
8386 + err = check_empty(new_dentry, &namelist);
8390 + if (!is_robranch(new_dentry))
8391 + err = delete_whiteouts(new_dentry,
8392 + dbstart(new_dentry),
8395 + free_rdstate(namelist);
8401 + err = do_unionfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8404 + /* clear the new_dentry stuff created */
8405 + d_drop(new_dentry);
8408 + * force re-lookup since the dir on ro branch is not renamed,
8409 + * and lower dentries still indicate the un-renamed ones.
8411 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8412 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8414 + unionfs_postcopyup_release(old_dentry);
8415 + if (new_dentry->d_inode &&
8416 + !S_ISDIR(new_dentry->d_inode->i_mode)) {
8417 + unionfs_postcopyup_release(new_dentry);
8418 + unionfs_postcopyup_setmnt(new_dentry);
8419 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8421 + * If we get here, it means that no copyup
8422 + * was needed, and that a file by the old
8423 + * name already existing on the destination
8424 + * branch; that file got renamed earlier in
8425 + * this function, so all we need to do here
8426 + * is set the lower inode.
8428 + struct inode *inode;
8429 + inode = unionfs_lower_inode(
8430 + old_dentry->d_inode);
8432 + unionfs_set_lower_inode_idx(
8433 + new_dentry->d_inode,
8434 + dbstart(new_dentry), inode);
8438 + /* if all of this renaming succeeded, update our times */
8439 + unionfs_copy_attr_times(old_dir);
8440 + unionfs_copy_attr_times(new_dir);
8441 + unionfs_copy_attr_times(old_dentry->d_inode);
8442 + unionfs_copy_attr_times(new_dentry->d_inode);
8443 + unionfs_check_inode(old_dir);
8444 + unionfs_check_inode(new_dir);
8445 + unionfs_check_dentry(old_dentry);
8446 + unionfs_check_dentry(new_dentry);
8449 + unionfs_unlock_dentry(new_dentry);
8450 + unionfs_unlock_dentry(old_dentry);
8451 + unionfs_read_unlock(old_dentry->d_sb);
8454 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8455 new file mode 100644
8456 index 0000000..2a8c88e
8458 +++ b/fs/unionfs/sioq.c
8461 + * Copyright (c) 2006-2007 Erez Zadok
8462 + * Copyright (c) 2006 Charles P. Wright
8463 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8464 + * Copyright (c) 2006 Junjiro Okajima
8465 + * Copyright (c) 2006 David P. Quigley
8466 + * Copyright (c) 2006-2007 Stony Brook University
8467 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
8469 + * This program is free software; you can redistribute it and/or modify
8470 + * it under the terms of the GNU General Public License version 2 as
8471 + * published by the Free Software Foundation.
8477 + * Super-user IO work Queue - sometimes we need to perform actions which
8478 + * would fail due to the unix permissions on the parent directory (e.g.,
8479 + * rmdir a directory which appears empty, but in reality contains
8483 +static struct workqueue_struct *superio_workqueue;
8485 +int __init init_sioq(void)
8489 + superio_workqueue = create_workqueue("unionfs_siod");
8490 + if (!IS_ERR(superio_workqueue))
8493 + err = PTR_ERR(superio_workqueue);
8494 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8495 + superio_workqueue = NULL;
8499 +void stop_sioq(void)
8501 + if (superio_workqueue)
8502 + destroy_workqueue(superio_workqueue);
8505 +void run_sioq(work_func_t func, struct sioq_args *args)
8507 + INIT_WORK(&args->work, func);
8509 + init_completion(&args->comp);
8510 + while (!queue_work(superio_workqueue, &args->work)) {
8511 + /* TODO: do accounting if needed */
8514 + wait_for_completion(&args->comp);
8517 +void __unionfs_create(struct work_struct *work)
8519 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8520 + struct create_args *c = &args->create;
8522 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8523 + complete(&args->comp);
8526 +void __unionfs_mkdir(struct work_struct *work)
8528 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8529 + struct mkdir_args *m = &args->mkdir;
8531 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8532 + complete(&args->comp);
8535 +void __unionfs_mknod(struct work_struct *work)
8537 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8538 + struct mknod_args *m = &args->mknod;
8540 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8541 + complete(&args->comp);
8544 +void __unionfs_symlink(struct work_struct *work)
8546 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8547 + struct symlink_args *s = &args->symlink;
8549 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf, s->mode);
8550 + complete(&args->comp);
8553 +void __unionfs_unlink(struct work_struct *work)
8555 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8556 + struct unlink_args *u = &args->unlink;
8558 + args->err = vfs_unlink(u->parent, u->dentry);
8559 + complete(&args->comp);
8562 +void __delete_whiteouts(struct work_struct *work)
8564 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8565 + struct deletewh_args *d = &args->deletewh;
8567 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
8568 + complete(&args->comp);
8571 +void __is_opaque_dir(struct work_struct *work)
8573 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8575 + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
8576 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
8577 + complete(&args->comp);
8579 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8580 new file mode 100644
8581 index 0000000..afb71ee
8583 +++ b/fs/unionfs/sioq.h
8586 + * Copyright (c) 2006-2007 Erez Zadok
8587 + * Copyright (c) 2006 Charles P. Wright
8588 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8589 + * Copyright (c) 2006 Junjiro Okajima
8590 + * Copyright (c) 2006 David P. Quigley
8591 + * Copyright (c) 2006-2007 Stony Brook University
8592 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
8594 + * This program is free software; you can redistribute it and/or modify
8595 + * it under the terms of the GNU General Public License version 2 as
8596 + * published by the Free Software Foundation.
8602 +struct deletewh_args {
8603 + struct unionfs_dir_state *namelist;
8604 + struct dentry *dentry;
8608 +struct is_opaque_args {
8609 + struct dentry *dentry;
8612 +struct create_args {
8613 + struct inode *parent;
8614 + struct dentry *dentry;
8616 + struct nameidata *nd;
8619 +struct mkdir_args {
8620 + struct inode *parent;
8621 + struct dentry *dentry;
8625 +struct mknod_args {
8626 + struct inode *parent;
8627 + struct dentry *dentry;
8632 +struct symlink_args {
8633 + struct inode *parent;
8634 + struct dentry *dentry;
8639 +struct unlink_args {
8640 + struct inode *parent;
8641 + struct dentry *dentry;
8646 + struct completion comp;
8647 + struct work_struct work;
8652 + struct deletewh_args deletewh;
8653 + struct is_opaque_args is_opaque;
8654 + struct create_args create;
8655 + struct mkdir_args mkdir;
8656 + struct mknod_args mknod;
8657 + struct symlink_args symlink;
8658 + struct unlink_args unlink;
8662 +/* Extern definitions for SIOQ functions */
8663 +extern int __init init_sioq(void);
8664 +extern void stop_sioq(void);
8665 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8667 +/* Extern definitions for our privilege escalation helpers */
8668 +extern void __unionfs_create(struct work_struct *work);
8669 +extern void __unionfs_mkdir(struct work_struct *work);
8670 +extern void __unionfs_mknod(struct work_struct *work);
8671 +extern void __unionfs_symlink(struct work_struct *work);
8672 +extern void __unionfs_unlink(struct work_struct *work);
8673 +extern void __delete_whiteouts(struct work_struct *work);
8674 +extern void __is_opaque_dir(struct work_struct *work);
8676 +#endif /* not _SIOQ_H */
8677 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8678 new file mode 100644
8679 index 0000000..d97086a
8681 +++ b/fs/unionfs/subr.c
8684 + * Copyright (c) 2003-2007 Erez Zadok
8685 + * Copyright (c) 2003-2006 Charles P. Wright
8686 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8687 + * Copyright (c) 2005-2006 Junjiro Okajima
8688 + * Copyright (c) 2005 Arun M. Krishnakumar
8689 + * Copyright (c) 2004-2006 David P. Quigley
8690 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8691 + * Copyright (c) 2003 Puja Gupta
8692 + * Copyright (c) 2003 Harikesavan Krishnan
8693 + * Copyright (c) 2003-2007 Stony Brook University
8694 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
8696 + * This program is free software; you can redistribute it and/or modify
8697 + * it under the terms of the GNU General Public License version 2 as
8698 + * published by the Free Software Foundation.
8704 + * Pass an unionfs dentry and an index. It will try to create a whiteout
8705 + * for the filename in dentry, and will try in branch 'index'. On error,
8706 + * it will proceed to a branch to the left.
8708 +int create_whiteout(struct dentry *dentry, int start)
8710 + int bstart, bend, bindex;
8711 + struct dentry *lower_dir_dentry;
8712 + struct dentry *lower_dentry;
8713 + struct dentry *lower_wh_dentry;
8714 + struct nameidata nd;
8715 + char *name = NULL;
8716 + int err = -EINVAL;
8718 + verify_locked(dentry);
8720 + bstart = dbstart(dentry);
8721 + bend = dbend(dentry);
8723 + /* create dentry's whiteout equivalent */
8724 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
8725 + if (unlikely(IS_ERR(name))) {
8726 + err = PTR_ERR(name);
8730 + for (bindex = start; bindex >= 0; bindex--) {
8731 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
8733 + if (!lower_dentry) {
8735 + * if lower dentry is not present, create the
8736 + * entire lower dentry directory structure and go
8737 + * ahead. Since we want to just create whiteout, we
8738 + * only want the parent dentry, and hence get rid of
8741 + lower_dentry = create_parents(dentry->d_inode,
8743 + dentry->d_name.name,
8745 + if (!lower_dentry || IS_ERR(lower_dentry)) {
8746 + printk(KERN_ERR "unionfs: create_parents "
8747 + "failed for bindex = %d\n", bindex);
8753 + lookup_one_len(name, lower_dentry->d_parent,
8754 + dentry->d_name.len + UNIONFS_WHLEN);
8755 + if (IS_ERR(lower_wh_dentry))
8759 + * The whiteout already exists. This used to be impossible,
8760 + * but now is possible because of opaqueness.
8762 + if (lower_wh_dentry->d_inode) {
8763 + dput(lower_wh_dentry);
8768 + err = init_lower_nd(&nd, LOOKUP_CREATE);
8769 + if (unlikely(err < 0))
8771 + lower_dir_dentry = lock_parent(lower_wh_dentry);
8772 + err = is_robranch_super(dentry->d_sb, bindex);
8774 + err = vfs_create(lower_dir_dentry->d_inode,
8776 + ~current->fs->umask & S_IRWXUGO,
8778 + unlock_dir(lower_dir_dentry);
8779 + dput(lower_wh_dentry);
8780 + release_lower_nd(&nd, err);
8782 + if (!err || !IS_COPYUP_ERR(err))
8786 + /* set dbopaque so that lookup will not proceed after this branch */
8788 + set_dbopaque(dentry, bindex);
8796 + * This is a helper function for rename, which ends up with hosed over
8797 + * dentries when it needs to revert.
8799 +int unionfs_refresh_lower_dentry(struct dentry *dentry, int bindex)
8801 + struct dentry *lower_dentry;
8802 + struct dentry *lower_parent;
8805 + verify_locked(dentry);
8807 + unionfs_lock_dentry(dentry->d_parent);
8808 + lower_parent = unionfs_lower_dentry_idx(dentry->d_parent, bindex);
8809 + unionfs_unlock_dentry(dentry->d_parent);
8811 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
8813 + lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
8814 + dentry->d_name.len);
8815 + if (IS_ERR(lower_dentry)) {
8816 + err = PTR_ERR(lower_dentry);
8820 + dput(unionfs_lower_dentry_idx(dentry, bindex));
8821 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
8822 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
8824 + if (!lower_dentry->d_inode) {
8825 + dput(lower_dentry);
8826 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
8828 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
8829 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
8830 + igrab(lower_dentry->d_inode));
8837 +int make_dir_opaque(struct dentry *dentry, int bindex)
8840 + struct dentry *lower_dentry, *diropq;
8841 + struct inode *lower_dir;
8842 + struct nameidata nd;
8844 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
8845 + lower_dir = lower_dentry->d_inode;
8846 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
8847 + !S_ISDIR(lower_dir->i_mode));
8849 + mutex_lock(&lower_dir->i_mutex);
8850 + diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
8851 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
8852 + if (IS_ERR(diropq)) {
8853 + err = PTR_ERR(diropq);
8857 + err = init_lower_nd(&nd, LOOKUP_CREATE);
8858 + if (unlikely(err < 0))
8860 + if (!diropq->d_inode)
8861 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
8863 + set_dbopaque(dentry, bindex);
8864 + release_lower_nd(&nd, err);
8869 + mutex_unlock(&lower_dir->i_mutex);
8874 + * returns the right n_link value based on the inode type
8876 +int unionfs_get_nlinks(const struct inode *inode)
8878 + /* don't bother to do all the work since we're unlinked */
8879 + if (inode->i_nlink == 0)
8882 + if (!S_ISDIR(inode->i_mode))
8883 + return unionfs_lower_inode(inode)->i_nlink;
8886 + * For directories, we return 1. The only place that could cares
8887 + * about links is readdir, and there's d_type there so even that
8893 +/* construct whiteout filename */
8894 +char *alloc_whname(const char *name, int len)
8898 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
8899 + if (unlikely(!buf))
8900 + return ERR_PTR(-ENOMEM);
8902 + strcpy(buf, UNIONFS_WHPFX);
8903 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
8907 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8908 new file mode 100644
8909 index 0000000..515689d
8911 +++ b/fs/unionfs/super.c
8914 + * Copyright (c) 2003-2007 Erez Zadok
8915 + * Copyright (c) 2003-2006 Charles P. Wright
8916 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8917 + * Copyright (c) 2005-2006 Junjiro Okajima
8918 + * Copyright (c) 2005 Arun M. Krishnakumar
8919 + * Copyright (c) 2004-2006 David P. Quigley
8920 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8921 + * Copyright (c) 2003 Puja Gupta
8922 + * Copyright (c) 2003 Harikesavan Krishnan
8923 + * Copyright (c) 2003-2007 Stony Brook University
8924 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
8926 + * This program is free software; you can redistribute it and/or modify
8927 + * it under the terms of the GNU General Public License version 2 as
8928 + * published by the Free Software Foundation.
8934 + * The inode cache is used with alloc_inode for both our inode info and the
8937 +static struct kmem_cache *unionfs_inode_cachep;
8939 +static void unionfs_read_inode(struct inode *inode)
8942 + struct unionfs_inode_info *info = UNIONFS_I(inode);
8944 + unionfs_read_lock(inode->i_sb);
8946 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8947 + info->bstart = -1;
8949 + atomic_set(&info->generation,
8950 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8951 + spin_lock_init(&info->rdlock);
8952 + info->rdcount = 1;
8953 + info->hashsize = -1;
8954 + INIT_LIST_HEAD(&info->readdircache);
8956 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8957 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8958 + if (unlikely(!info->lower_inodes)) {
8959 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8960 + "lower-pointer array!\n");
8964 + inode->i_version++;
8965 + inode->i_op = &unionfs_main_iops;
8966 + inode->i_fop = &unionfs_main_fops;
8968 + inode->i_mapping->a_ops = &unionfs_aops;
8970 + unionfs_read_unlock(inode->i_sb);
8974 + * we now define delete_inode, because there are two VFS paths that may
8975 + * destroy an inode: one of them calls clear inode before doing everything
8976 + * else that's needed, and the other is fine. This way we truncate the inode
8977 + * size (and its pages) and then clear our own inode, which will do an iput
8978 + * on our and the lower inode.
8980 + * No need to lock sb info's rwsem.
8982 +static void unionfs_delete_inode(struct inode *inode)
8984 + inode->i_size = 0; /* every f/s seems to do that */
8986 + if (inode->i_data.nrpages)
8987 + truncate_inode_pages(&inode->i_data, 0);
8989 + clear_inode(inode);
8993 + * final actions when unmounting a file system
8995 + * No need to lock rwsem.
8997 +static void unionfs_put_super(struct super_block *sb)
8999 + int bindex, bstart, bend;
9000 + struct unionfs_sb_info *spd;
9003 + spd = UNIONFS_SB(sb);
9007 + bstart = sbstart(sb);
9010 + /* Make sure we have no leaks of branchget/branchput. */
9011 + for (bindex = bstart; bindex <= bend; bindex++)
9012 + if (unlikely(branch_count(sb, bindex) != 0)) {
9014 + "unionfs: branch %d has %d references left!\n",
9015 + bindex, branch_count(sb, bindex));
9018 + BUG_ON(leaks != 0);
9022 + sb->s_fs_info = NULL;
9026 + * Since people use this to answer the "How big of a file can I write?"
9027 + * question, we report the size of the highest priority branch as the size of
9030 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
9033 + struct super_block *sb;
9034 + struct dentry *lower_dentry;
9036 + sb = dentry->d_sb;
9038 + unionfs_read_lock(sb);
9039 + unionfs_lock_dentry(dentry);
9041 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
9045 + unionfs_check_dentry(dentry);
9047 + lower_dentry = unionfs_lower_dentry(sb->s_root);
9048 + err = vfs_statfs(lower_dentry, buf);
9050 + /* set return buf to our f/s to avoid confusing user-level utils */
9051 + buf->f_type = UNIONFS_SUPER_MAGIC;
9053 + * Our maximum file name can is shorter by a few bytes because every
9054 + * file name could potentially be whited-out.
9056 + * XXX: this restriction goes away with ODF.
9058 + buf->f_namelen -= UNIONFS_WHLEN;
9061 + * reset two fields to avoid confusing user-land.
9062 + * XXX: is this still necessary?
9064 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
9065 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
9068 + unionfs_unlock_dentry(dentry);
9069 + unionfs_check_dentry(dentry);
9070 + unionfs_read_unlock(sb);
9074 +/* handle mode changing during remount */
9075 +static noinline int do_remount_mode_option(char *optarg, int cur_branches,
9076 + struct unionfs_data *new_data,
9077 + struct path *new_lower_paths)
9079 + int err = -EINVAL;
9081 + char *modename = strchr(optarg, '=');
9082 + struct nameidata nd;
9084 + /* by now, optarg contains the branch name */
9087 + "unionfs: no branch specified for mode change\n");
9091 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
9095 + *modename++ = '\0';
9096 + perms = __parse_branch_mode(modename);
9098 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
9099 + modename, optarg);
9104 + * Find matching branch index. For now, this assumes that nothing
9105 + * has been mounted on top of this Unionfs stack. Once we have /odf
9106 + * and cache-coherency resolved, we'll address the branch-path
9109 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
9111 + printk(KERN_ERR "unionfs: error accessing "
9112 + "lower directory \"%s\" (error %d)\n",
9116 + for (idx = 0; idx < cur_branches; idx++)
9117 + if (nd.mnt == new_lower_paths[idx].mnt &&
9118 + nd.dentry == new_lower_paths[idx].dentry)
9120 + path_release(&nd); /* no longer needed */
9121 + if (idx == cur_branches) {
9122 + err = -ENOENT; /* err may have been reset above */
9123 + printk(KERN_ERR "unionfs: branch \"%s\" "
9124 + "not found\n", optarg);
9127 + /* check/change mode for existing branch */
9128 + /* we don't warn if perms==branchperms */
9129 + new_data[idx].branchperms = perms;
9135 +/* handle branch deletion during remount */
9136 +static noinline int do_remount_del_option(char *optarg, int cur_branches,
9137 + struct unionfs_data *new_data,
9138 + struct path *new_lower_paths)
9140 + int err = -EINVAL;
9142 + struct nameidata nd;
9144 + /* optarg contains the branch name to delete */
9147 + * Find matching branch index. For now, this assumes that nothing
9148 + * has been mounted on top of this Unionfs stack. Once we have /odf
9149 + * and cache-coherency resolved, we'll address the branch-path
9152 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
9154 + printk(KERN_ERR "unionfs: error accessing "
9155 + "lower directory \"%s\" (error %d)\n",
9159 + for (idx = 0; idx < cur_branches; idx++)
9160 + if (nd.mnt == new_lower_paths[idx].mnt &&
9161 + nd.dentry == new_lower_paths[idx].dentry)
9163 + path_release(&nd); /* no longer needed */
9164 + if (idx == cur_branches) {
9165 + printk(KERN_ERR "unionfs: branch \"%s\" "
9166 + "not found\n", optarg);
9170 + /* check if there are any open files on the branch to be deleted */
9171 + if (atomic_read(&new_data[idx].open_files) > 0) {
9177 + * Now we have to delete the branch. First, release any handles it
9178 + * has. Then, move the remaining array indexes past "idx" in
9179 + * new_data and new_lower_paths one to the left. Finally, adjust
9182 + pathput(&new_lower_paths[idx]);
9184 + if (idx < cur_branches - 1) {
9185 + /* if idx==cur_branches-1, we delete last branch: easy */
9186 + memmove(&new_data[idx], &new_data[idx+1],
9187 + (cur_branches - 1 - idx) *
9188 + sizeof(struct unionfs_data));
9189 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
9190 + (cur_branches - 1 - idx) * sizeof(struct path));
9198 +/* handle branch insertion during remount */
9199 +static noinline int do_remount_add_option(char *optarg, int cur_branches,
9200 + struct unionfs_data *new_data,
9201 + struct path *new_lower_paths,
9202 + int *high_branch_id)
9204 + int err = -EINVAL;
9206 + int idx = 0; /* default: insert at beginning */
9207 + char *new_branch , *modename = NULL;
9208 + struct nameidata nd;
9211 + * optarg can be of several forms:
9213 + * /bar:/foo insert /foo before /bar
9214 + * /bar:/foo=ro insert /foo in ro mode before /bar
9215 + * /foo insert /foo in the beginning (prepend)
9216 + * :/foo insert /foo at the end (append)
9218 + if (*optarg == ':') { /* append? */
9219 + new_branch = optarg + 1; /* skip ':' */
9220 + idx = cur_branches;
9221 + goto found_insertion_point;
9223 + new_branch = strchr(optarg, ':');
9224 + if (!new_branch) { /* prepend? */
9225 + new_branch = optarg;
9226 + goto found_insertion_point;
9228 + *new_branch++ = '\0'; /* holds path+mode of new branch */
9231 + * Find matching branch index. For now, this assumes that nothing
9232 + * has been mounted on top of this Unionfs stack. Once we have /odf
9233 + * and cache-coherency resolved, we'll address the branch-path
9236 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
9238 + printk(KERN_ERR "unionfs: error accessing "
9239 + "lower directory \"%s\" (error %d)\n",
9243 + for (idx = 0; idx < cur_branches; idx++)
9244 + if (nd.mnt == new_lower_paths[idx].mnt &&
9245 + nd.dentry == new_lower_paths[idx].dentry)
9247 + path_release(&nd); /* no longer needed */
9248 + if (idx == cur_branches) {
9249 + printk(KERN_ERR "unionfs: branch \"%s\" "
9250 + "not found\n", optarg);
9256 + * At this point idx will hold the index where the new branch should
9257 + * be inserted before.
9259 +found_insertion_point:
9260 + /* find the mode for the new branch */
9262 + modename = strchr(new_branch, '=');
9264 + *modename++ = '\0';
9265 + perms = parse_branch_mode(modename);
9267 + if (!new_branch || !*new_branch) {
9268 + printk(KERN_ERR "unionfs: null new branch\n");
9272 + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
9274 + printk(KERN_ERR "unionfs: error accessing "
9275 + "lower directory \"%s\" (error %d)\n",
9280 + * It's probably safe to check_mode the new branch to insert. Note:
9281 + * we don't allow inserting branches which are unionfs's by
9282 + * themselves (check_branch returns EINVAL in that case). This is
9283 + * because this code base doesn't support stacking unionfs: the ODF
9284 + * code base supports that correctly.
9286 + err = check_branch(&nd);
9288 + printk(KERN_ERR "unionfs: lower directory "
9289 + "\"%s\" is not a valid branch\n", optarg);
9290 + path_release(&nd);
9295 + * Now we have to insert the new branch. But first, move the bits
9296 + * to make space for the new branch, if needed. Finally, adjust
9298 + * We don't release nd here; it's kept until umount/remount.
9300 + if (idx < cur_branches) {
9301 + /* if idx==cur_branches, we append: easy */
9302 + memmove(&new_data[idx+1], &new_data[idx],
9303 + (cur_branches - idx) * sizeof(struct unionfs_data));
9304 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
9305 + (cur_branches - idx) * sizeof(struct path));
9307 + new_lower_paths[idx].dentry = nd.dentry;
9308 + new_lower_paths[idx].mnt = nd.mnt;
9310 + new_data[idx].sb = nd.dentry->d_sb;
9311 + atomic_set(&new_data[idx].open_files, 0);
9312 + new_data[idx].branchperms = perms;
9313 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
9322 + * Support branch management options on remount.
9324 + * See Documentation/filesystems/unionfs/ for details.
9326 + * @flags: numeric mount options
9327 + * @options: mount options string
9329 + * This function can rearrange a mounted union dynamically, adding and
9330 + * removing branches, including changing branch modes. Clearly this has to
9331 + * be done safely and atomically. Luckily, the VFS already calls this
9332 + * function with lock_super(sb) and lock_kernel() held, preventing
9333 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
9334 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
9335 + * to purge dentries/inodes from our superblock, and also called
9336 + * fsync_super(sb) to purge any dirty pages. So we're good.
9338 + * XXX: however, our remount code may also need to invalidate mapped pages
9339 + * so as to force them to be re-gotten from the (newly reconfigured) lower
9340 + * branches. This has to wait for proper mmap and cache coherency support
9344 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
9349 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
9351 + int cur_branches = 0; /* no. of current branches */
9352 + int new_branches = 0; /* no. of branches actually left in the end */
9353 + int add_branches; /* est. no. of branches to add */
9354 + int del_branches; /* est. no. of branches to del */
9355 + int max_branches; /* max possible no. of branches */
9356 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
9357 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
9358 + struct inode **new_lower_inodes = NULL;
9359 + int new_high_branch_id; /* new high branch ID */
9360 + int size; /* memory allocation size, temp var */
9361 + int old_ibstart, old_ibend;
9363 + unionfs_write_lock(sb);
9366 + * The VFS will take care of "ro" and "rw" flags, and we can safely
9367 + * ignore MS_SILENT, but anything else left over is an error. So we
9368 + * need to check if any other flags may have been passed (none are
9369 + * allowed/supported as of now).
9371 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
9373 + "unionfs: remount flags 0x%x unsupported\n", *flags);
9379 + * If 'options' is NULL, it's probably because the user just changed
9380 + * the union to a "ro" or "rw" and the VFS took care of it. So
9381 + * nothing to do and we're done.
9383 + if (!options || options[0] == '\0')
9387 + * Find out how many branches we will have in the end, counting
9388 + * "add" and "del" commands. Copy the "options" string because
9389 + * strsep modifies the string and we need it later.
9391 + tmp_to_free = kstrdup(options, GFP_KERNEL);
9392 + optionstmp = tmp_to_free;
9393 + if (unlikely(!optionstmp)) {
9397 + cur_branches = sbmax(sb); /* current no. branches */
9398 + new_branches = sbmax(sb);
9401 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9402 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
9405 + if (!optname || !*optname)
9408 + optarg = strchr(optname, '=');
9412 + if (!strcmp("add", optname))
9414 + else if (!strcmp("del", optname))
9417 + kfree(tmp_to_free);
9418 + /* after all changes, will we have at least one branch left? */
9419 + if ((new_branches + add_branches - del_branches) < 1) {
9421 + "unionfs: no branches left after remount\n");
9427 + * Since we haven't actually parsed all the add/del options, nor
9428 + * have we checked them for errors, we don't know for sure how many
9429 + * branches we will have after all changes have taken place. In
9430 + * fact, the total number of branches left could be less than what
9431 + * we have now. So we need to allocate space for a temporary
9432 + * placeholder that is at least as large as the maximum number of
9433 + * branches we *could* have, which is the current number plus all
9434 + * the additions. Once we're done with these temp placeholders, we
9435 + * may have to re-allocate the final size, copy over from the temp,
9436 + * and then free the temps (done near the end of this function).
9438 + max_branches = cur_branches + add_branches;
9439 + /* allocate space for new pointers to lower dentry */
9440 + tmp_data = kcalloc(max_branches,
9441 + sizeof(struct unionfs_data), GFP_KERNEL);
9442 + if (unlikely(!tmp_data)) {
9446 + /* allocate space for new pointers to lower paths */
9447 + tmp_lower_paths = kcalloc(max_branches,
9448 + sizeof(struct path), GFP_KERNEL);
9449 + if (unlikely(!tmp_lower_paths)) {
9453 + /* copy current info into new placeholders, incrementing refcnts */
9454 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9455 + cur_branches * sizeof(struct unionfs_data));
9456 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9457 + cur_branches * sizeof(struct path));
9458 + for (i = 0; i < cur_branches; i++)
9459 + pathget(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9461 + /*******************************************************************
9462 + * For each branch command, do path_lookup on the requested branch,
9463 + * and apply the change to a temp branch list. To handle errors, we
9464 + * already dup'ed the old arrays (above), and increased the refcnts
9465 + * on various f/s objects. So now we can do all the path_lookups
9466 + * and branch-management commands on the new arrays. If it fail mid
9467 + * way, we free the tmp arrays and *put all objects. If we succeed,
9468 + * then we free old arrays and *put its objects, and then replace
9469 + * the arrays with the new tmp list (we may have to re-allocate the
9470 + * memory because the temp lists could have been larger than what we
9471 + * actually needed).
9472 + *******************************************************************/
9474 + while ((optname = strsep(&options, ",")) != NULL) {
9477 + if (!optname || !*optname)
9480 + * At this stage optname holds a comma-delimited option, but
9481 + * without the commas. Next, we need to break the string on
9482 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9483 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9484 + * KEY is "/foo", and VAL is "rw".
9486 + optarg = strchr(optname, '=');
9489 + /* incgen remount option (instead of old ioctl) */
9490 + if (!strcmp("incgen", optname)) {
9492 + goto out_no_change;
9496 + * All of our options take an argument now. (Insert ones
9497 + * that don't above this check.) So at this stage optname
9498 + * contains the CMD part and optarg contains the ARG part.
9500 + if (!optarg || !*optarg) {
9501 + printk(KERN_ERR "unionfs: all remount options require "
9502 + "an argument (%s)\n", optname);
9507 + if (!strcmp("add", optname)) {
9508 + err = do_remount_add_option(optarg, new_branches,
9511 + &new_high_branch_id);
9515 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9516 + printk(KERN_ERR "unionfs: command exceeds "
9517 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9523 + if (!strcmp("del", optname)) {
9524 + err = do_remount_del_option(optarg, new_branches,
9532 + if (!strcmp("mode", optname)) {
9533 + err = do_remount_mode_option(optarg, new_branches,
9542 + * When you use "mount -o remount,ro", mount(8) will
9543 + * reportedly pass the original dirs= string from
9544 + * /proc/mounts. So for now, we have to ignore dirs= and
9545 + * not consider it an error, unless we want to allow users
9546 + * to pass dirs= in remount. Note that to allow the VFS to
9547 + * actually process the ro/rw remount options, we have to
9548 + * return 0 from this function.
9550 + if (!strcmp("dirs", optname)) {
9551 + printk(KERN_WARNING
9552 + "unionfs: remount ignoring option \"%s\"\n",
9559 + "unionfs: unrecognized option \"%s\"\n", optname);
9565 + /******************************************************************
9566 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9567 + * see if we need to allocate a small-sized new vector, copy the
9568 + * vectors to their correct place, release the refcnt of the older
9569 + * ones, and return. Also handle invalidating any pages that will
9570 + * have to be re-read.
9571 + *******************************************************************/
9573 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9574 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9575 + "(use \"remount,ro\" to create a read-only union)\n");
9580 + /* (re)allocate space for new pointers to lower dentry */
9581 + size = new_branches * sizeof(struct unionfs_data);
9582 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9583 + if (unlikely(!new_data)) {
9588 + /* allocate space for new pointers to lower paths */
9589 + size = new_branches * sizeof(struct path);
9590 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9591 + if (unlikely(!new_lower_paths)) {
9596 + /* allocate space for new pointers to lower inodes */
9597 + new_lower_inodes = kcalloc(new_branches,
9598 + sizeof(struct inode *), GFP_KERNEL);
9599 + if (unlikely(!new_lower_inodes)) {
9605 + * OK, just before we actually put the new set of branches in place,
9606 + * we need to ensure that our own f/s has no dirty objects left.
9607 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9608 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9609 + * pages. So all that's left is for us to invalidate any leftover
9610 + * (non-dirty) pages to ensure that they will be re-read from the
9611 + * new lower branches (and to support mmap).
9615 + * Now we call drop_pagecache_sb() to invalidate all pages in this
9616 + * super. This function calls invalidate_inode_pages(mapping),
9617 + * which calls invalidate_mapping_pages(): the latter, however, will
9618 + * not invalidate pages which are dirty, locked, under writeback, or
9619 + * mapped into page tables. We shouldn't have to worry about dirty
9620 + * or under-writeback pages, because do_remount_sb() called
9621 + * fsync_super() which would not have returned until all dirty pages
9624 + * But do we have to worry about locked pages? Is there any chance
9625 + * that in here we'll get locked pages?
9627 + * XXX: what about pages mapped into pagetables? Are these pages
9628 + * which user processes may have mmap(2)'ed? If so, then we need to
9629 + * invalidate those too, no? Maybe we'll have to write our own
9630 + * version of invalidate_mapping_pages() which also handled mapped
9633 + * XXX: Alternatively, maybe we should call truncate_inode_pages(),
9634 + * which use two passes over the pages list, and will truncate all
9637 + drop_pagecache_sb(sb);
9639 + /* copy new vectors into their correct place */
9640 + tmp_data = UNIONFS_SB(sb)->data;
9641 + UNIONFS_SB(sb)->data = new_data;
9642 + new_data = NULL; /* so don't free good pointers below */
9643 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9644 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9645 + new_lower_paths = NULL; /* so don't free good pointers below */
9647 + /* update our unionfs_sb_info and root dentry index of last branch */
9648 + i = sbmax(sb); /* save no. of branches to release at end */
9649 + sbend(sb) = new_branches - 1;
9650 + set_dbend(sb->s_root, new_branches - 1);
9651 + old_ibstart = ibstart(sb->s_root->d_inode);
9652 + old_ibend = ibend(sb->s_root->d_inode);
9653 + ibend(sb->s_root->d_inode) = new_branches - 1;
9654 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9655 + new_branches = i; /* no. of branches to release below */
9658 + * Update lower inodes: 3 steps
9659 + * 1. grab ref on all new lower inodes
9661 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9662 + struct dentry *lower_dentry =
9663 + unionfs_lower_dentry_idx(sb->s_root, i);
9664 + igrab(lower_dentry->d_inode);
9665 + new_lower_inodes[i] = lower_dentry->d_inode;
9667 + /* 2. release reference on all older lower inodes */
9668 + for (i = old_ibstart; i <= old_ibend; i++) {
9669 + iput(unionfs_lower_inode_idx(sb->s_root->d_inode, i));
9670 + unionfs_set_lower_inode_idx(sb->s_root->d_inode, i, NULL);
9672 + kfree(UNIONFS_I(sb->s_root->d_inode)->lower_inodes);
9673 + /* 3. update root dentry's inode to new lower_inodes array */
9674 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9675 + new_lower_inodes = NULL;
9677 + /* maxbytes may have changed */
9678 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9679 + /* update high branch ID */
9680 + sbhbid(sb) = new_high_branch_id;
9682 + /* update our sb->generation for revalidating objects */
9683 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9684 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9685 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9686 + if (!(*flags & MS_SILENT))
9687 + pr_info("unionfs: new generation number %d\n", i);
9688 + /* finally, update the root dentry's times */
9689 + unionfs_copy_attr_times(sb->s_root->d_inode);
9690 + err = 0; /* reset to success */
9693 + * The code above falls through to the next label, and releases the
9694 + * refcnts of the older ones (stored in tmp_*): if we fell through
9695 + * here, it means success. However, if we jump directly to this
9696 + * label from any error above, then an error occurred after we
9697 + * grabbed various refcnts, and so we have to release the
9698 + * temporarily constructed structures.
9701 + /* no need to cleanup/release anything in tmp_data */
9702 + if (tmp_lower_paths)
9703 + for (i = 0; i < new_branches; i++)
9704 + pathput(&tmp_lower_paths[i]);
9706 + kfree(tmp_lower_paths);
9708 + kfree(new_lower_paths);
9710 + kfree(new_lower_inodes);
9712 + unionfs_write_unlock(sb);
9713 + unionfs_check_dentry(sb->s_root);
9718 + * Called by iput() when the inode reference count reached zero
9719 + * and the inode is not hashed anywhere. Used to clear anything
9720 + * that needs to be, before the inode is completely destroyed and put
9721 + * on the inode free list.
9723 + * No need to lock sb info's rwsem.
9725 +static void unionfs_clear_inode(struct inode *inode)
9727 + int bindex, bstart, bend;
9728 + struct inode *lower_inode;
9729 + struct list_head *pos, *n;
9730 + struct unionfs_dir_state *rdstate;
9732 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9733 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9734 + list_del(&rdstate->cache);
9735 + free_rdstate(rdstate);
9739 + * Decrement a reference to a lower_inode, which was incremented
9740 + * by our read_inode when it was created initially.
9742 + bstart = ibstart(inode);
9743 + bend = ibend(inode);
9744 + if (bstart >= 0) {
9745 + for (bindex = bstart; bindex <= bend; bindex++) {
9746 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9749 + iput(lower_inode);
9753 + kfree(UNIONFS_I(inode)->lower_inodes);
9754 + UNIONFS_I(inode)->lower_inodes = NULL;
9757 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9759 + struct unionfs_inode_info *i;
9761 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9765 + /* memset everything up to the inode to 0 */
9766 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9768 + i->vfs_inode.i_version = 1;
9769 + return &i->vfs_inode;
9772 +static void unionfs_destroy_inode(struct inode *inode)
9774 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9777 +/* unionfs inode cache constructor */
9778 +static void init_once(void *v, struct kmem_cache *cachep, unsigned long flags)
9780 + struct unionfs_inode_info *i = v;
9782 + inode_init_once(&i->vfs_inode);
9785 +int unionfs_init_inode_cache(void)
9789 + unionfs_inode_cachep =
9790 + kmem_cache_create("unionfs_inode_cache",
9791 + sizeof(struct unionfs_inode_info), 0,
9792 + SLAB_RECLAIM_ACCOUNT, init_once);
9793 + if (unlikely(!unionfs_inode_cachep))
9798 +/* unionfs inode cache destructor */
9799 +void unionfs_destroy_inode_cache(void)
9801 + if (unionfs_inode_cachep)
9802 + kmem_cache_destroy(unionfs_inode_cachep);
9806 + * Called when we have a dirty inode, right here we only throw out
9807 + * parts of our readdir list that are too old.
9809 + * No need to grab sb info's rwsem.
9811 +static int unionfs_write_inode(struct inode *inode, int sync)
9813 + struct list_head *pos, *n;
9814 + struct unionfs_dir_state *rdstate;
9816 + spin_lock(&UNIONFS_I(inode)->rdlock);
9817 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9818 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9819 + /* We keep this list in LRU order. */
9820 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9822 + UNIONFS_I(inode)->rdcount--;
9823 + list_del(&rdstate->cache);
9824 + free_rdstate(rdstate);
9826 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9832 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9833 + * code can actually succeed and won't leave tasks that need handling.
9835 +static void unionfs_umount_begin(struct vfsmount *mnt, int flags)
9837 + struct super_block *sb, *lower_sb;
9838 + struct vfsmount *lower_mnt;
9839 + int bindex, bstart, bend;
9841 + if (!(flags & MNT_FORCE))
9843 + * we are not being MNT_FORCE'd, therefore we should emulate
9850 + unionfs_read_lock(sb);
9852 + bstart = sbstart(sb);
9854 + for (bindex = bstart; bindex <= bend; bindex++) {
9855 + lower_mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9856 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9858 + if (lower_mnt && lower_sb && lower_sb->s_op &&
9859 + lower_sb->s_op->umount_begin)
9860 + lower_sb->s_op->umount_begin(lower_mnt, flags);
9863 + unionfs_read_unlock(sb);
9866 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9868 + struct super_block *sb = mnt->mnt_sb;
9872 + int bindex, bstart, bend;
9875 + unionfs_read_lock(sb);
9877 + unionfs_lock_dentry(sb->s_root);
9879 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9880 + if (unlikely(!tmp_page)) {
9885 + bstart = sbstart(sb);
9888 + seq_printf(m, ",dirs=");
9889 + for (bindex = bstart; bindex <= bend; bindex++) {
9890 + path = d_path(unionfs_lower_dentry_idx(sb->s_root, bindex),
9891 + unionfs_lower_mnt_idx(sb->s_root, bindex),
9892 + tmp_page, PAGE_SIZE);
9893 + if (IS_ERR(path)) {
9894 + ret = PTR_ERR(path);
9898 + perms = branchperms(sb, bindex);
9900 + seq_printf(m, "%s=%s", path,
9901 + perms & MAY_WRITE ? "rw" : "ro");
9902 + if (bindex != bend)
9903 + seq_printf(m, ":");
9907 + free_page((unsigned long) tmp_page);
9909 + unionfs_unlock_dentry(sb->s_root);
9911 + unionfs_read_unlock(sb);
9916 +struct super_operations unionfs_sops = {
9917 + .read_inode = unionfs_read_inode,
9918 + .delete_inode = unionfs_delete_inode,
9919 + .put_super = unionfs_put_super,
9920 + .statfs = unionfs_statfs,
9921 + .remount_fs = unionfs_remount_fs,
9922 + .clear_inode = unionfs_clear_inode,
9923 + .umount_begin = unionfs_umount_begin,
9924 + .show_options = unionfs_show_options,
9925 + .write_inode = unionfs_write_inode,
9926 + .alloc_inode = unionfs_alloc_inode,
9927 + .destroy_inode = unionfs_destroy_inode,
9929 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9930 new file mode 100644
9931 index 0000000..22a4b91
9933 +++ b/fs/unionfs/union.h
9936 + * Copyright (c) 2003-2007 Erez Zadok
9937 + * Copyright (c) 2003-2006 Charles P. Wright
9938 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9939 + * Copyright (c) 2005 Arun M. Krishnakumar
9940 + * Copyright (c) 2004-2006 David P. Quigley
9941 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9942 + * Copyright (c) 2003 Puja Gupta
9943 + * Copyright (c) 2003 Harikesavan Krishnan
9944 + * Copyright (c) 2003-2007 Stony Brook University
9945 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
9947 + * This program is free software; you can redistribute it and/or modify
9948 + * it under the terms of the GNU General Public License version 2 as
9949 + * published by the Free Software Foundation.
9955 +#include <linux/dcache.h>
9956 +#include <linux/file.h>
9957 +#include <linux/list.h>
9958 +#include <linux/fs.h>
9959 +#include <linux/mm.h>
9960 +#include <linux/module.h>
9961 +#include <linux/mount.h>
9962 +#include <linux/namei.h>
9963 +#include <linux/page-flags.h>
9964 +#include <linux/pagemap.h>
9965 +#include <linux/poll.h>
9966 +#include <linux/security.h>
9967 +#include <linux/seq_file.h>
9968 +#include <linux/slab.h>
9969 +#include <linux/spinlock.h>
9970 +#include <linux/smp_lock.h>
9971 +#include <linux/statfs.h>
9972 +#include <linux/string.h>
9973 +#include <linux/vmalloc.h>
9974 +#include <linux/writeback.h>
9975 +#include <linux/buffer_head.h>
9976 +#include <linux/xattr.h>
9977 +#include <linux/fs_stack.h>
9978 +#include <linux/magic.h>
9979 +#include <linux/log2.h>
9980 +#include <linux/poison.h>
9981 +#include <linux/mman.h>
9983 +#include <asm/system.h>
9985 +#include <linux/union_fs.h>
9987 +/* the file system name */
9988 +#define UNIONFS_NAME "unionfs"
9990 +/* unionfs root inode number */
9991 +#define UNIONFS_ROOT_INO 1
9993 +/* number of times we try to get a unique temporary file name */
9994 +#define GET_TMPNAM_MAX_RETRY 5
9996 +/* maximum number of branches we support, to avoid memory blowup */
9997 +#define UNIONFS_MAX_BRANCHES 128
9999 +/* Operations vectors defined in specific files. */
10000 +extern struct file_operations unionfs_main_fops;
10001 +extern struct file_operations unionfs_dir_fops;
10002 +extern struct inode_operations unionfs_main_iops;
10003 +extern struct inode_operations unionfs_dir_iops;
10004 +extern struct inode_operations unionfs_symlink_iops;
10005 +extern struct super_operations unionfs_sops;
10006 +extern struct dentry_operations unionfs_dops;
10007 +extern struct address_space_operations unionfs_aops;
10009 +/* How long should an entry be allowed to persist */
10010 +#define RDCACHE_JIFFIES (5*HZ)
10012 +/* file private data. */
10013 +struct unionfs_file_info {
10016 + atomic_t generation;
10018 + struct unionfs_dir_state *rdstate;
10019 + struct file **lower_files;
10020 + int *saved_branch_ids; /* IDs of branches when file was opened */
10023 +/* unionfs inode data in memory */
10024 +struct unionfs_inode_info {
10027 + atomic_t generation;
10029 + /* Stuff for readdir over NFS. */
10030 + spinlock_t rdlock;
10031 + struct list_head readdircache;
10036 + /* The lower inodes */
10037 + struct inode **lower_inodes;
10038 + /* to keep track of reads/writes for unlinks before closes */
10039 + atomic_t totalopens;
10041 + struct inode vfs_inode;
10044 +/* unionfs dentry data in memory */
10045 +struct unionfs_dentry_info {
10047 + * The semaphore is used to lock the dentry as soon as we get into a
10048 + * unionfs function from the VFS. Our lock ordering is that children
10049 + * go before their parents.
10051 + struct mutex lock;
10056 + atomic_t generation;
10057 + struct path *lower_paths;
10060 +/* These are the pointers to our various objects. */
10061 +struct unionfs_data {
10062 + struct super_block *sb;
10063 + atomic_t open_files; /* number of open files on branch */
10065 + int branch_id; /* unique branch ID at re/mount time */
10068 +/* unionfs super-block data in memory */
10069 +struct unionfs_sb_info {
10072 + atomic_t generation;
10075 + * This rwsem is used to make sure that a branch management
10077 + * 1) will not begin before all currently in-flight operations
10079 + * 2) any new operations do not execute until the currently
10080 + * running branch management operation completes
10082 +#ifdef CONFIG_PREEMPT_RT
10083 + struct compat_rw_semaphore rwsem;
10084 +#else /* not CONFIG_PREEMPT_RT */
10085 + struct rw_semaphore rwsem;
10086 +#endif /* not CONFIG_PREEMPT_RT */
10087 + int high_branch_id; /* last unique branch ID given */
10088 + struct unionfs_data *data;
10092 + * structure for making the linked list of entries by readdir on left branch
10093 + * to compare with entries on right branch
10095 +struct filldir_node {
10096 + struct list_head file_list; /* list for directory entries */
10097 + char *name; /* name entry */
10098 + int hash; /* name hash */
10099 + int namelen; /* name len since name is not 0 terminated */
10102 + * we can check for duplicate whiteouts and files in the same branch
10103 + * in order to return -EIO.
10107 + /* is this a whiteout entry? */
10110 + /* Inline name, so we don't need to separately kmalloc small ones */
10111 + char iname[DNAME_INLINE_LEN_MIN];
10114 +/* Directory hash table. */
10115 +struct unionfs_dir_state {
10116 + unsigned int cookie; /* the cookie, based off of rdversion */
10117 + unsigned int offset; /* The entry we have returned. */
10119 + loff_t dirpos; /* offset within the lower level directory */
10120 + int size; /* How big is the hash table? */
10121 + int hashentries; /* How many entries have been inserted? */
10122 + unsigned long access;
10124 + /* This cache list is used when the inode keeps us around. */
10125 + struct list_head cache;
10126 + struct list_head list[0];
10129 +/* externs needed for fanout.h or sioq.h */
10130 +extern int unionfs_get_nlinks(const struct inode *inode);
10132 +/* include miscellaneous macros */
10133 +#include "fanout.h"
10136 +/* externs for cache creation/deletion routines */
10137 +extern void unionfs_destroy_filldir_cache(void);
10138 +extern int unionfs_init_filldir_cache(void);
10139 +extern int unionfs_init_inode_cache(void);
10140 +extern void unionfs_destroy_inode_cache(void);
10141 +extern int unionfs_init_dentry_cache(void);
10142 +extern void unionfs_destroy_dentry_cache(void);
10144 +/* Initialize and free readdir-specific state. */
10145 +extern int init_rdstate(struct file *file);
10146 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
10148 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
10150 +extern void free_rdstate(struct unionfs_dir_state *state);
10151 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
10152 + const char *name, int namelen, int bindex,
10154 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
10155 + const char *name, int namelen);
10157 +extern struct dentry **alloc_new_dentries(int objs);
10158 +extern struct unionfs_data *alloc_new_data(int objs);
10160 +/* We can only use 32-bits of offset for rdstate --- blech! */
10161 +#define DIREOF (0xfffff)
10162 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
10163 +#define MAXRDCOOKIE (0xfff)
10164 +/* Turn an rdstate into an offset. */
10165 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
10169 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
10170 + | (buf->offset & DIREOF);
10174 +#define unionfs_read_lock(sb) down_read(&UNIONFS_SB(sb)->rwsem)
10175 +#define unionfs_read_unlock(sb) up_read(&UNIONFS_SB(sb)->rwsem)
10176 +#define unionfs_write_lock(sb) down_write(&UNIONFS_SB(sb)->rwsem)
10177 +#define unionfs_write_unlock(sb) up_write(&UNIONFS_SB(sb)->rwsem)
10179 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
10180 + struct dentry *d2)
10183 + struct dentry *tmp = d1;
10187 + unionfs_lock_dentry(d1);
10188 + unionfs_lock_dentry(d2);
10191 +extern int new_dentry_private_data(struct dentry *dentry);
10192 +extern void free_dentry_private_data(struct dentry *dentry);
10193 +extern void update_bstart(struct dentry *dentry);
10194 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
10195 +extern void release_lower_nd(struct nameidata *nd, int err);
10201 +/* replicates the directory structure up to given dentry in given branch */
10202 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
10203 + const char *name, int bindex);
10204 +extern int make_dir_opaque(struct dentry *dir, int bindex);
10206 +/* partial lookup */
10207 +extern int unionfs_partial_lookup(struct dentry *dentry);
10210 + * Pass an unionfs dentry and an index and it will try to create a whiteout
10211 + * in branch 'index'.
10213 + * On error, it will proceed to a branch to the left
10215 +extern int create_whiteout(struct dentry *dentry, int start);
10216 +/* copies a file from dbstart to newbindex branch */
10217 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
10218 + int newbindex, loff_t size);
10219 +extern int copyup_named_file(struct inode *dir, struct file *file,
10220 + char *name, int bstart, int new_bindex,
10222 +/* copies a dentry from dbstart to newbindex branch */
10223 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
10224 + int bstart, int new_bindex, const char *name,
10225 + int namelen, struct file **copyup_file, loff_t len);
10226 +/* helper functions for post-copyup actions */
10227 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
10228 +extern void unionfs_postcopyup_release(struct dentry *dentry);
10230 +extern int remove_whiteouts(struct dentry *dentry,
10231 + struct dentry *lower_dentry, int bindex);
10233 +extern int do_delete_whiteouts(struct dentry *dentry, int bindex,
10234 + struct unionfs_dir_state *namelist);
10236 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
10237 +extern int check_empty(struct dentry *dentry,
10238 + struct unionfs_dir_state **namelist);
10239 +/* Delete whiteouts from this directory in branch bindex. */
10240 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
10241 + struct unionfs_dir_state *namelist);
10243 +/* Re-lookup a lower dentry. */
10244 +extern int unionfs_refresh_lower_dentry(struct dentry *dentry, int bindex);
10246 +extern void unionfs_reinterpose(struct dentry *this_dentry);
10247 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
10249 +/* Locking functions. */
10250 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
10251 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
10253 +/* Common file operations. */
10254 +extern int unionfs_file_revalidate(struct file *file, bool willwrite);
10255 +extern int unionfs_open(struct inode *inode, struct file *file);
10256 +extern int unionfs_file_release(struct inode *inode, struct file *file);
10257 +extern int unionfs_flush(struct file *file, fl_owner_t id);
10258 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
10259 + unsigned long arg);
10260 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
10262 +extern int unionfs_fasync(int fd, struct file *file, int flag);
10264 +/* Inode operations */
10265 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
10266 + struct inode *new_dir, struct dentry *new_dentry);
10267 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
10268 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
10270 +extern bool __unionfs_d_revalidate_chain(struct dentry *dentry,
10271 + struct nameidata *nd, bool willwrite);
10272 +extern bool is_newer_lower(const struct dentry *dentry);
10274 +/* The values for unionfs_interpose's flag. */
10275 +#define INTERPOSE_DEFAULT 0
10276 +#define INTERPOSE_LOOKUP 1
10277 +#define INTERPOSE_REVAL 2
10278 +#define INTERPOSE_REVAL_NEG 3
10279 +#define INTERPOSE_PARTIAL 4
10281 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
10282 + struct super_block *sb, int flag);
10284 +#ifdef CONFIG_UNION_FS_XATTR
10285 +/* Extended attribute functions. */
10286 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
10287 +static inline void unionfs_xattr_kfree(const void *p)
10291 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
10292 + void *value, size_t size);
10293 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
10294 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
10296 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
10297 + const void *value, size_t size, int flags);
10298 +#endif /* CONFIG_UNION_FS_XATTR */
10300 +/* The root directory is unhashed, but isn't deleted. */
10301 +static inline int d_deleted(struct dentry *d)
10303 + return d_unhashed(d) && (d != d->d_sb->s_root);
10306 +struct dentry *unionfs_lookup_backend(struct dentry *dentry,
10307 + struct nameidata *nd, int lookupmode);
10309 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10310 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10312 +/* unionfs_open, check if we need to copyup the file */
10313 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10314 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10316 +static inline int branchperms(const struct super_block *sb, int index)
10318 + BUG_ON(index < 0);
10319 + return UNIONFS_SB(sb)->data[index].branchperms;
10322 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10324 + BUG_ON(index < 0);
10325 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10329 +/* Is this file on a read-only branch? */
10330 +static inline int is_robranch_super(const struct super_block *sb, int index)
10334 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10338 +/* Is this file on a read-only branch? */
10339 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10341 + struct super_block *lower_sb;
10343 + BUG_ON(index < 0);
10345 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10348 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10349 + BUG_ON(lower_sb == NULL);
10351 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10352 + * lower_dentry could be a negative.
10354 + if (lower_sb->s_flags & MS_RDONLY)
10360 +static inline int is_robranch(const struct dentry *dentry)
10364 + index = UNIONFS_D(dentry)->bstart;
10365 + BUG_ON(index < 0);
10367 + return is_robranch_idx(dentry, index);
10370 +/* What do we use for whiteouts. */
10371 +#define UNIONFS_WHPFX ".wh."
10372 +#define UNIONFS_WHLEN 4
10374 + * If a directory contains this file, then it is opaque. We start with the
10375 + * .wh. flag so that it is blocked by lookup.
10377 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10378 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10383 +extern char *alloc_whname(const char *name, int len);
10384 +extern int check_branch(struct nameidata *nd);
10385 +extern int __parse_branch_mode(const char *name);
10386 +extern int parse_branch_mode(const char *name);
10389 + * These two functions are here because it is kind of daft to copy and paste
10390 + * the contents of the two functions to 32+ places in unionfs
10392 +static inline struct dentry *lock_parent(struct dentry *dentry)
10394 + struct dentry *dir = dget(dentry->d_parent);
10396 + mutex_lock(&dir->d_inode->i_mutex);
10400 +static inline void unlock_dir(struct dentry *dir)
10402 + mutex_unlock(&dir->d_inode->i_mutex);
10406 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10409 + struct vfsmount *mnt;
10411 + BUG_ON(!dentry || bindex < 0);
10413 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10414 +#ifdef CONFIG_UNION_FS_DEBUG
10416 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10418 +#endif /* CONFIG_UNION_FS_DEBUG */
10423 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10425 + struct vfsmount *mnt;
10427 + if (!dentry && bindex < 0)
10429 + BUG_ON(!dentry || bindex < 0);
10431 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10432 +#ifdef CONFIG_UNION_FS_DEBUG
10434 + * Directories can have NULL lower objects in between start/end, but
10435 + * NOT if at the start/end range. We cannot verify that this dentry
10436 + * is a type=DIR, because it may already be a negative dentry. But
10437 + * if dbstart is greater than dbend, we know that this couldn't have
10438 + * been a regular file: it had to have been a directory.
10440 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10441 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10442 +#endif /* CONFIG_UNION_FS_DEBUG */
10446 +#ifdef CONFIG_UNION_FS_DEBUG
10448 +/* useful for tracking code reachability */
10449 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__)
10451 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10452 + __FILE__, __FUNCTION__, __LINE__)
10453 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10454 + __FILE__, __FUNCTION__, __LINE__)
10455 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10456 + __FILE__, __FUNCTION__, __LINE__)
10457 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10458 + __FILE__, __FUNCTION__, __LINE__)
10459 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10460 + __FILE__, __FUNCTION__, __LINE__)
10461 +#define show_inode_times(i) __show_inode_times((i), \
10462 + __FILE__, __FUNCTION__, __LINE__)
10463 +#define show_dinode_times(d) __show_dinode_times((d), \
10464 + __FILE__, __FUNCTION__, __LINE__)
10465 +#define show_inode_counts(i) __show_inode_counts((i), \
10466 + __FILE__, __FUNCTION__, __LINE__)
10468 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10469 + const char *fxn, int line);
10470 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10471 + const char *fname, const char *fxn,
10473 +extern void __unionfs_check_file(const struct file *file,
10474 + const char *fname, const char *fxn, int line);
10475 +extern void __unionfs_check_nd(const struct nameidata *nd,
10476 + const char *fname, const char *fxn, int line);
10477 +extern void __show_branch_counts(const struct super_block *sb,
10478 + const char *file, const char *fxn, int line);
10479 +extern void __show_inode_times(const struct inode *inode,
10480 + const char *file, const char *fxn, int line);
10481 +extern void __show_dinode_times(const struct dentry *dentry,
10482 + const char *file, const char *fxn, int line);
10483 +extern void __show_inode_counts(const struct inode *inode,
10484 + const char *file, const char *fxn, int line);
10486 +#else /* not CONFIG_UNION_FS_DEBUG */
10488 +/* we leave useful hooks for these check functions throughout the code */
10489 +#define unionfs_check_inode(i) do { } while (0)
10490 +#define unionfs_check_dentry(d) do { } while (0)
10491 +#define unionfs_check_file(f) do { } while (0)
10492 +#define unionfs_check_nd(n) do { } while (0)
10493 +#define show_branch_counts(sb) do { } while (0)
10494 +#define show_inode_times(i) do { } while (0)
10495 +#define show_dinode_times(d) do { } while (0)
10496 +#define show_inode_counts(i) do { } while (0)
10498 +#endif /* not CONFIG_UNION_FS_DEBUG */
10500 +#endif /* not _UNION_H_ */
10501 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10502 new file mode 100644
10503 index 0000000..a8de672
10505 +++ b/fs/unionfs/unlink.c
10508 + * Copyright (c) 2003-2007 Erez Zadok
10509 + * Copyright (c) 2003-2006 Charles P. Wright
10510 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10511 + * Copyright (c) 2005-2006 Junjiro Okajima
10512 + * Copyright (c) 2005 Arun M. Krishnakumar
10513 + * Copyright (c) 2004-2006 David P. Quigley
10514 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10515 + * Copyright (c) 2003 Puja Gupta
10516 + * Copyright (c) 2003 Harikesavan Krishnan
10517 + * Copyright (c) 2003-2007 Stony Brook University
10518 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
10520 + * This program is free software; you can redistribute it and/or modify
10521 + * it under the terms of the GNU General Public License version 2 as
10522 + * published by the Free Software Foundation.
10525 +#include "union.h"
10527 +/* unlink a file by creating a whiteout */
10528 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry)
10530 + struct dentry *lower_dentry;
10531 + struct dentry *lower_dir_dentry;
10535 + err = unionfs_partial_lookup(dentry);
10539 + bindex = dbstart(dentry);
10541 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10542 + if (!lower_dentry)
10545 + lower_dir_dentry = lock_parent(lower_dentry);
10547 + /* avoid destroying the lower inode if the file is in use */
10548 + dget(lower_dentry);
10549 + err = is_robranch_super(dentry->d_sb, bindex);
10551 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
10552 + /* if vfs_unlink succeeded, update our inode's times */
10554 + unionfs_copy_attr_times(dentry->d_inode);
10555 + dput(lower_dentry);
10556 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10557 + unlock_dir(lower_dir_dentry);
10559 + if (err && !IS_COPYUP_ERR(err))
10563 + if (dbstart(dentry) == 0)
10565 + err = create_whiteout(dentry, dbstart(dentry) - 1);
10566 + } else if (dbopaque(dentry) != -1) {
10567 + /* There is a lower lower-priority file with the same name. */
10568 + err = create_whiteout(dentry, dbopaque(dentry));
10570 + err = create_whiteout(dentry, dbstart(dentry));
10575 + dentry->d_inode->i_nlink--;
10577 + /* We don't want to leave negative leftover dentries for revalidate. */
10578 + if (!err && (dbopaque(dentry) != -1))
10579 + update_bstart(dentry);
10584 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10588 + unionfs_read_lock(dentry->d_sb);
10589 + unionfs_lock_dentry(dentry);
10591 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10595 + unionfs_check_dentry(dentry);
10597 + err = unionfs_unlink_whiteout(dir, dentry);
10598 + /* call d_drop so the system "forgets" about us */
10600 + if (!S_ISDIR(dentry->d_inode->i_mode))
10601 + unionfs_postcopyup_release(dentry);
10604 + * if unlink/whiteout succeeded, parent dir mtime has
10607 + unionfs_copy_attr_times(dir);
10612 + unionfs_check_dentry(dentry);
10613 + unionfs_check_inode(dir);
10615 + unionfs_unlock_dentry(dentry);
10616 + unionfs_read_unlock(dentry->d_sb);
10620 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10621 + struct unionfs_dir_state *namelist)
10624 + struct dentry *lower_dentry;
10625 + struct dentry *lower_dir_dentry = NULL;
10627 + /* Here we need to remove whiteout entries. */
10628 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10632 + lower_dentry = unionfs_lower_dentry(dentry);
10634 + lower_dir_dentry = lock_parent(lower_dentry);
10636 + /* avoid destroying the lower inode if the file is in use */
10637 + dget(lower_dentry);
10638 + err = is_robranch(dentry);
10640 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10641 + dput(lower_dentry);
10643 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10644 + /* propagate number of hard-links */
10645 + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10648 + if (lower_dir_dentry)
10649 + unlock_dir(lower_dir_dentry);
10653 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10656 + struct unionfs_dir_state *namelist = NULL;
10658 + unionfs_read_lock(dentry->d_sb);
10659 + unionfs_lock_dentry(dentry);
10661 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10665 + unionfs_check_dentry(dentry);
10667 + /* check if this unionfs directory is empty or not */
10668 + err = check_empty(dentry, &namelist);
10672 + err = unionfs_rmdir_first(dir, dentry, namelist);
10673 + /* create whiteout */
10675 + err = create_whiteout(dentry, dbstart(dentry));
10679 + if (dbstart(dentry) == 0)
10682 + /* exit if the error returned was NOT -EROFS */
10683 + if (!IS_COPYUP_ERR(err))
10686 + new_err = create_whiteout(dentry, dbstart(dentry) - 1);
10687 + if (new_err != -EEXIST)
10692 + /* call d_drop so the system "forgets" about us */
10697 + free_rdstate(namelist);
10699 + unionfs_unlock_dentry(dentry);
10700 + unionfs_read_unlock(dentry->d_sb);
10703 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
10704 new file mode 100644
10705 index 0000000..602cedf
10707 +++ b/fs/unionfs/xattr.c
10710 + * Copyright (c) 2003-2007 Erez Zadok
10711 + * Copyright (c) 2003-2006 Charles P. Wright
10712 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10713 + * Copyright (c) 2005-2006 Junjiro Okajima
10714 + * Copyright (c) 2005 Arun M. Krishnakumar
10715 + * Copyright (c) 2004-2006 David P. Quigley
10716 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10717 + * Copyright (c) 2003 Puja Gupta
10718 + * Copyright (c) 2003 Harikesavan Krishnan
10719 + * Copyright (c) 2003-2007 Stony Brook University
10720 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
10722 + * This program is free software; you can redistribute it and/or modify
10723 + * it under the terms of the GNU General Public License version 2 as
10724 + * published by the Free Software Foundation.
10727 +#include "union.h"
10729 +/* This is lifted from fs/xattr.c */
10730 +void *unionfs_xattr_alloc(size_t size, size_t limit)
10734 + if (size > limit)
10735 + return ERR_PTR(-E2BIG);
10737 + if (!size) /* size request, no buffer is needed */
10740 + ptr = kmalloc(size, GFP_KERNEL);
10741 + if (unlikely(!ptr))
10742 + return ERR_PTR(-ENOMEM);
10747 + * BKL held by caller.
10748 + * dentry->d_inode->i_mutex locked
10750 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
10753 + struct dentry *lower_dentry = NULL;
10754 + int err = -EOPNOTSUPP;
10756 + unionfs_read_lock(dentry->d_sb);
10757 + unionfs_lock_dentry(dentry);
10759 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10764 + lower_dentry = unionfs_lower_dentry(dentry);
10766 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
10769 + unionfs_unlock_dentry(dentry);
10770 + unionfs_check_dentry(dentry);
10771 + unionfs_read_unlock(dentry->d_sb);
10776 + * BKL held by caller.
10777 + * dentry->d_inode->i_mutex locked
10779 +int unionfs_setxattr(struct dentry *dentry, const char *name,
10780 + const void *value, size_t size, int flags)
10782 + struct dentry *lower_dentry = NULL;
10783 + int err = -EOPNOTSUPP;
10785 + unionfs_read_lock(dentry->d_sb);
10786 + unionfs_lock_dentry(dentry);
10788 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10793 + lower_dentry = unionfs_lower_dentry(dentry);
10795 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
10799 + unionfs_unlock_dentry(dentry);
10800 + unionfs_check_dentry(dentry);
10801 + unionfs_read_unlock(dentry->d_sb);
10806 + * BKL held by caller.
10807 + * dentry->d_inode->i_mutex locked
10809 +int unionfs_removexattr(struct dentry *dentry, const char *name)
10811 + struct dentry *lower_dentry = NULL;
10812 + int err = -EOPNOTSUPP;
10814 + unionfs_read_lock(dentry->d_sb);
10815 + unionfs_lock_dentry(dentry);
10817 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10822 + lower_dentry = unionfs_lower_dentry(dentry);
10824 + err = vfs_removexattr(lower_dentry, (char *) name);
10827 + unionfs_unlock_dentry(dentry);
10828 + unionfs_check_dentry(dentry);
10829 + unionfs_read_unlock(dentry->d_sb);
10834 + * BKL held by caller.
10835 + * dentry->d_inode->i_mutex locked
10837 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
10839 + struct dentry *lower_dentry = NULL;
10840 + int err = -EOPNOTSUPP;
10841 + char *encoded_list = NULL;
10843 + unionfs_read_lock(dentry->d_sb);
10844 + unionfs_lock_dentry(dentry);
10846 + if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10851 + lower_dentry = unionfs_lower_dentry(dentry);
10853 + encoded_list = list;
10854 + err = vfs_listxattr(lower_dentry, encoded_list, size);
10857 + unionfs_unlock_dentry(dentry);
10858 + unionfs_check_dentry(dentry);
10859 + unionfs_read_unlock(dentry->d_sb);
10862 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
10863 index bb516ce..6b52faf 100644
10864 --- a/include/linux/fs_stack.h
10865 +++ b/include/linux/fs_stack.h
10868 + * Copyright (c) 2006-2007 Erez Zadok
10869 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
10870 + * Copyright (c) 2006-2007 Stony Brook University
10871 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
10873 + * This program is free software; you can redistribute it and/or modify
10874 + * it under the terms of the GNU General Public License version 2 as
10875 + * published by the Free Software Foundation.
10878 #ifndef _LINUX_FS_STACK_H
10879 #define _LINUX_FS_STACK_H
10881 -/* This file defines generic functions used primarily by stackable
10883 + * This file defines generic functions used primarily by stackable
10884 * filesystems; none of these functions require i_mutex to be held.
10887 #include <linux/fs.h>
10889 /* externs for fs/stack.c */
10890 -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
10891 - int (*get_nlinks)(struct inode *));
10893 -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
10894 +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
10895 +extern void fsstack_copy_inode_size(struct inode *dst,
10896 + const struct inode *src);
10899 static inline void fsstack_copy_attr_atime(struct inode *dest,
10900 diff --git a/include/linux/magic.h b/include/linux/magic.h
10901 index 36cc20d..8dac50b 100644
10902 --- a/include/linux/magic.h
10903 +++ b/include/linux/magic.h
10905 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
10906 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
10908 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
10910 #define SMB_SUPER_MAGIC 0x517B
10911 #define USBDEVICE_SUPER_MAGIC 0x9fa2
10913 diff --git a/include/linux/mm.h b/include/linux/mm.h
10914 index 1692dd6..aee99b6 100644
10915 --- a/include/linux/mm.h
10916 +++ b/include/linux/mm.h
10917 @@ -20,6 +20,7 @@ struct anon_vma;
10918 struct file_ra_state;
10919 struct user_struct;
10920 struct writeback_control;
10921 +struct super_block;
10923 #ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */
10924 extern unsigned long max_mapnr;
10925 @@ -1207,6 +1208,7 @@ int drop_caches_sysctl_handler(struct ctl_table *, int, struct file *,
10926 void __user *, size_t *, loff_t *);
10927 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
10928 unsigned long lru_pages);
10929 +extern void drop_pagecache_sb(struct super_block *);
10930 void drop_pagecache(void);
10931 void drop_slab(void);
10933 diff --git a/include/linux/namei.h b/include/linux/namei.h
10934 index 6c38efb..e9477b5 100644
10935 --- a/include/linux/namei.h
10936 +++ b/include/linux/namei.h
10939 #include <linux/dcache.h>
10940 #include <linux/linkage.h>
10941 +#include <linux/mount.h>
10945 @@ -100,4 +101,16 @@ static inline char *nd_get_link(struct nameidata *nd)
10946 return nd->saved_names[nd->depth];
10949 +static inline void pathget(struct path *path)
10951 + mntget(path->mnt);
10952 + dget(path->dentry);
10955 +static inline void pathput(struct path *path)
10957 + dput(path->dentry);
10958 + mntput(path->mnt);
10961 #endif /* _LINUX_NAMEI_H */
10962 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
10963 new file mode 100644
10964 index 0000000..d13eb48
10966 +++ b/include/linux/union_fs.h
10969 + * Copyright (c) 2003-2007 Erez Zadok
10970 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10971 + * Copyright (c) 2003-2007 Stony Brook University
10972 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
10974 + * This program is free software; you can redistribute it and/or modify
10975 + * it under the terms of the GNU General Public License version 2 as
10976 + * published by the Free Software Foundation.
10979 +#ifndef _LINUX_UNION_FS_H
10980 +#define _LINUX_UNION_FS_H
10983 + * DEFINITIONS FOR USER AND KERNEL CODE:
10985 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
10986 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
10988 +/* We don't support normal remount, but unionctl uses it. */
10989 +# define UNIONFS_REMOUNT_MAGIC 0x4a5a4380
10991 +#endif /* _LINUX_UNIONFS_H */