1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index 52cd611..bc6b437 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -106,6 +106,8 @@ udf.txt
6 - info and mount options for the UDF filesystem.
8 - info on the ufs filesystem.
10 + - info on the unionfs filesystem
12 - info on using the VFAT filesystem used in Windows NT and Windows 95
14 diff --git a/Documentation/filesystems/unionfs/00-INDEX b/Documentation/filesystems/unionfs/00-INDEX
16 index 0000000..96fdf67
18 +++ b/Documentation/filesystems/unionfs/00-INDEX
23 + - A brief introduction of concepts.
25 + - A summary of known issues with unionfs.
27 + - Information regarding rename operations.
29 + - Usage information and examples.
30 diff --git a/Documentation/filesystems/unionfs/concepts.txt b/Documentation/filesystems/unionfs/concepts.txt
32 index 0000000..b853788
34 +++ b/Documentation/filesystems/unionfs/concepts.txt
36 +Unionfs 2.x CONCEPTS:
37 +=====================
39 +This file describes the concepts needed by a namespace unification file
46 +Each branch is assigned a unique priority - starting from 0 (highest
47 +priority). No two branches can have the same priority.
53 +Each branch is assigned a mode - read-write or read-only. This allows
54 +directories on media mounted read-write to be used in a read-only manner.
60 +A whiteout removes a file name from the namespace. Whiteouts are needed when
61 +one attempts to remove a file on a read-only branch.
63 +Suppose we have a two-branch union, where branch 0 is read-write and branch
64 +1 is read-only. And a file 'foo' on branch 1:
70 +The unified view would simply be:
75 +Since 'foo' is stored on a read-only branch, it cannot be removed. A
76 +whiteout is used to remove the name 'foo' from the unified namespace. Again,
77 +since branch 1 is read-only, the whiteout cannot be created there. So, we
78 +try on a higher priority (lower numerically) branch and create the whiteout
86 +Later, when Unionfs traverses branches (due to lookup or readdir), it
87 +eliminate 'foo' from the namespace (as well as the whiteout itself.)
93 +Assume we have a unionfs mount comprising of two branches. Branch 0 is
94 +empty; branch 1 has the directory /a and file /a/f. Let's say we mount a
95 +union of branch 0 as read-write and branch 1 as read-only. Now, let's say
96 +we try to perform the following operation in the union:
100 +Because branch 1 is not writable, we cannot physically remove the file /a/f
101 +or the directory /a. So instead, we will create a whiteout in branch 0
102 +named /.wh.a, masking out the name "a" from branch 1. Next, let's say we
103 +try to create a directory named "a" as follows:
107 +Because we have a whiteout for "a" already, Unionfs behaves as if "a"
108 +doesn't exist, and thus will delete the whiteout and replace it with an
109 +actual directory named "a".
111 +The problem now is that if you try to "ls" in the union, Unionfs will
112 +perform is normal directory name unification, for *all* directories named
113 +"a" in all branches. This will cause the file /a/f from branch 1 to
114 +re-appear in the union's namespace, which violates Unix semantics.
116 +To avoid this problem, we have a different form of whiteouts for
117 +directories, called "opaque directories" (same as BSD Union Mount does).
118 +Whenever we replace a whiteout with a directory, that directory is marked as
119 +opaque. In Unionfs 2.x, it means that we create a file named
120 +/a/.wh.__dir_opaque in branch 0, after having created directory /a there.
121 +When unionfs notices that a directory is opaque, it stops all namespace
122 +operations (including merging readdir contents) at that opaque directory.
123 +This prevents re-exposing names from masked out directories.
126 +Duplicate Elimination:
127 +======================
129 +It is possible for files on different branches to have the same name.
130 +Unionfs then has to select which instance of the file to show to the user.
131 +Given the fact that each branch has a priority associated with it, the
132 +simplest solution is to take the instance from the highest priority
133 +(numerically lowest value) and "hide" the others.
139 +Unlink operation on non-directory instances is optimized to remove the
140 +maximum possible objects in case multiple underlying branches have the same
141 +file name. The unlink operation will first try to delete file instances
142 +from highest priority branch and then move further to delete from remaining
143 +branches in order of their decreasing priority. Consider a case (F..D..F),
144 +where F is a file and D is a directory of the same name; here, some
145 +intermediate branch could have an empty directory instance with the same
146 +name, so this operation also tries to delete this directory instance and
147 +proceed further to delete from next possible lower priority branch. The
148 +unionfs unlink operation will smoothly delete the files with same name from
149 +all possible underlying branches. In case if some error occurs, it creates
150 +whiteout in highest priority branch that will hide file instance in rest of
151 +the branches. An error could occur either if an unlink operations in any of
152 +the underlying branch failed or if a branch has no write permission.
154 +This unlinking policy is known as "delete all" and it has the benefit of
155 +overall reducing the number of inodes used by duplicate files, and further
156 +reducing the total number of inodes consumed by whiteouts. The cost is of
157 +extra processing, but testing shows this extra processing is well worth the
164 +When a change is made to the contents of a file's data or meta-data, they
165 +have to be stored somewhere. The best way is to create a copy of the
166 +original file on a branch that is writable, and then redirect the write
167 +though to this copy. The copy must be made on a higher priority branch so
168 +that lookup and readdir return this newer "version" of the file rather than
169 +the original (see duplicate elimination).
171 +An entire unionfs mount can be read-only or read-write. If it's read-only,
172 +then none of the branches will be written to, even if some of the branches
173 +are physically writeable. If the unionfs mount is read-write, then the
174 +leftmost (highest priority) branch must be writeable (for copyup to take
175 +place); the remaining branches can be any mix of read-write and read-only.
177 +In a writeable mount, unionfs will create new files/dir in the leftmost
178 +branch. If one tries to modify a file in a read-only branch/media, unionfs
179 +will copyup the file to the leftmost branch and modify it there. If you try
180 +to modify a file from a writeable branch which is not the leftmost branch,
181 +then unionfs will modify it in that branch; this is useful if you, say,
182 +unify differnet packages (e.g., apache, sendmail, ftpd, etc.) and you want
183 +changes to specific package files to remain logically in the directory where
189 +Unionfs users often want to be able to modify files and directories directly
190 +on the lower branches, and have those changes be visible at the Unionfs
191 +level. This means that data (e.g., pages) and meta-data (dentries, inodes,
192 +open files, etc.) have to be synchronized between the upper and lower
193 +layers. In other words, the newest changes from a layer below have to be
194 +propagated to the Unionfs layer above. If the two layers are not in sync, a
195 +cache incoherency ensues, which could lead to application failures and even
196 +oopses. The Linux kernel, however, has a rather limited set of mechanisms
197 +to ensure this inter-layer cache coherency---so Unionfs has to do most of
198 +the hard work on its own.
200 +Maintaining Invariants:
202 +The way Unionfs ensures cache coherency is as follows. At each entry point
203 +to a Unionfs file system method, we call a utility function to validate the
204 +primary objects of this method. Generally, we call unionfs_file_revalidate
205 +on open files, and __unionfs_d_revalidate_chain on dentries (which also
206 +validates inodes). These utility functions check to see whether the upper
207 +Unionfs object is in sync with any of the lower objects that it represents.
208 +The checks we perform include whether the Unionfs superblock has a newer
209 +generation number, or if any of the lower objects mtime's or ctime's are
210 +newer. (Note: generation numbers change when branch-management commands are
211 +issued, so in a way, maintaining cache coherency is also very important for
212 +branch-management.) If indeed we determine that any Unionfs object is no
213 +longer in sync with its lower counterparts, then we rebuild that object
214 +similarly to how we do so for branch-management.
216 +While rebuilding Unionfs's objects, we also purge any page mappings and
217 +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data). This is to
218 +ensure that Unionfs will re-get the newer data from the lower branches. We
219 +perform this purging only if the Unionfs operation in question is a reading
220 +operation; if Unionfs is performing a data writing operation (e.g., ->write,
221 +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
222 +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
223 +considered more authoritative anyway, as they are newer and will overwrite
226 +Unionfs maintains the following important invariant regarding mtime's,
227 +ctime's, and atime's: the upper inode object's times are the max() of all of
228 +the lower ones. For non-directory objects, there's only one object below,
229 +so the mapping is simple; for directory objects, there could me multiple
230 +lower objects and we have to sync up with the newest one of all the lower
231 +ones. This invariant is important to maintain, especially for directories
232 +(besides, we need this to be POSIX compliant). A union could comprise
233 +multiple writable branches, each of which could change. If we don't reflect
234 +the newest possible mtime/ctime, some applications could fail. For example,
235 +NFSv2/v3 exports check for newer directory mtimes on the server to determine
236 +if the client-side attribute cache should be purged.
238 +To maintain these important invariants, of course, Unionfs carefully
239 +synchronizes upper and lower times in various places. For example, if we
240 +copy-up a file to a top-level branch, the parent directory where the file
241 +was copied up to will now have a new mtime: so after a successful copy-up,
242 +we sync up with the new top-level branch's parent directory mtime.
246 +This cache-coherency implementation is efficient because it defers any
247 +synchronizing between the upper and lower layers until absolutely needed.
248 +Consider the example a common situation where users perform a lot of lower
249 +changes, such as untarring a whole package. While these take place,
250 +typically the user doesn't access the files via Unionfs; only after the
251 +lower changes are done, does the user try to access the lower files. With
252 +our cache-coherency implementation, the entirety of the changes to the lower
253 +branches will not result in a single CPU cycle spent at the Unionfs level
254 +until the user invokes a system call that goes through Unionfs.
256 +We have considered two alternate cache-coherency designs. (1) Using the
257 +dentry/inode notify functionality to register interest in finding out about
258 +any lower changes. This is a somewhat limited and also a heavy-handed
259 +approach which could result in many notifications to the Unionfs layer upon
260 +each small change at the lower layer (imagine a file being modified multiple
261 +times in rapid succession). (2) Rewriting the VFS to support explicit
262 +callbacks from lower objects to upper objects. We began exploring such an
263 +implementation, but found it to be very complicated--it would have resulted
264 +in massive VFS/MM changes which are unlikely to be accepted by the LKML
265 +community. We therefore believe that our current cache-coherency design and
266 +implementation represent the best approach at this time.
270 +Our implementation works in that as long as a user process will have caused
271 +Unionfs to be called, directly or indirectly, even to just do
272 +->d_revalidate; then we will have purged the current Unionfs data and the
273 +process will see the new data. For example, a process that continually
274 +re-reads the same file's data will see the NEW data as soon as the lower
275 +file had changed, upon the next read(2) syscall (even if the file is still
276 +open!) However, this doesn't work when the process re-reads the open file's
277 +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
278 +it). Once we respond to ->readpage(s), then the kernel maps the page into
279 +the process's address space and there doesn't appear to be a way to force
280 +the kernel to invalidate those pages/mappings, and force the process to
281 +re-issue ->readpage. If there's a way to invalidate active mappings and
282 +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
285 +Our current Unionfs code has to perform many file-revalidation calls. It
286 +would be really nice if the VFS would export an optional file system hook
287 +->file_revalidate (similarly to dentry->d_revalidate) that will be called
288 +before each VFS op that has a "struct file" in it.
290 +Certain file systems have micro-second granularity (or better) for inode
291 +times, and asynchronous actions could cause those times to change with some
292 +small delay. In such cases, Unionfs may see a changed inode time that only
293 +differs by a tiny fraction of a second: such a change may be a false
294 +positive indication that the lower object has changed, whereas if unionfs
295 +waits a little longer, that false indication will not be seen. (These false
296 +positives are harmless, because they would at most cause unionfs to
297 +re-validate an object that may need no revalidation, and print a debugging
298 +message that clutters the console/logs.) Therefore, to minimize the chances
299 +of these situations, we delay the detection of changed times by a small
300 +factor of a few seconds, called UNIONFS_MIN_CC_TIME (which defaults to 3
301 +seconds, as does NFS). This means that we will detect the change, only a
302 +couple of seconds later, if indeed the time change persists in the lower
303 +file object. This delayed detection has an added performance benefit: we
304 +reduce the number of times that unionfs has to revalidate objects, in case
305 +there's a lot of concurrent activity on both the upper and lower objects,
306 +for the same file(s). Lastly, this delayed time attribute detection is
307 +similar to how NFS clients operate (e.g., acregmin).
309 +Finally, there is no way currently in Linux to prevent lower directories
310 +from being moved around (i.e., topology changes); there's no way to prevent
311 +modifications to directory sub-trees of whole file systems which are mounted
312 +read-write. It is therefore possible for in-flight operations in unionfs to
313 +take place, while a lower directory is being moved around. Therefore, if
314 +you try to, say, create a new file in a directory through unionfs, while the
315 +directory is being moved around directly, then the new file may get created
316 +in the new location where that directory was moved to. This is a somewhat
317 +similar behaviour in NFS: an NFS client could be creating a new file while
318 +th NFS server is moving th directory around; the file will get successfully
319 +created in the new location. (The one exception in unionfs is that if the
320 +branch is marked read-only by unionfs, then a copyup will take place.)
322 +For more information, see <http://unionfs.filesystems.org/>.
323 diff --git a/Documentation/filesystems/unionfs/issues.txt b/Documentation/filesystems/unionfs/issues.txt
325 index 0000000..f4b7e7e
327 +++ b/Documentation/filesystems/unionfs/issues.txt
329 +KNOWN Unionfs 2.x ISSUES:
330 +=========================
332 +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
333 + confuses NFSv4. Currently, unionfs_lookup() passes lookup intents to the
334 + lower file-system, this eliminates part of the problem. The remaining
335 + calls to lookup_one_len may need to be changed to pass an intent. We are
336 + currently introducing VFS changes to fs/namei.c's do_path_lookup() to
337 + allow proper file lookup and opening in stackable file systems.
339 +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
340 + incorrectly complains about locking problems. The problem boils down to
341 + this: Lockdep considers all objects of a certain type to be in the same
342 + class, for example, all inodes. Lockdep doesn't like to see a lock held
343 + on two inodes within the same task, and warns that it could lead to a
344 + deadlock. However, stackable file systems do precisely that: they lock
345 + an upper object, and then a lower object, in a strict order to avoid
346 + locking problems; in addition, Unionfs, as a fan-out file system, may
347 + have to lock several lower inodes. We are currently looking into Lockdep
348 + to see how to make it aware of stackable file systems. For now, we
349 + temporarily disable lockdep when calling vfs methods on lower objects,
350 + but only for those places where lockdep complained. While this solution
351 + may seem unclean, it is not without precedent: other places in the kernel
352 + also do similar temporary disabling, of course after carefully having
353 + checked that it is the right thing to do. Anyway, you get any warnings
354 + from Lockdep, please report them to the Unionfs maintainers.
356 +For more information, see <http://unionfs.filesystems.org/>.
357 diff --git a/Documentation/filesystems/unionfs/rename.txt b/Documentation/filesystems/unionfs/rename.txt
359 index 0000000..e20bb82
361 +++ b/Documentation/filesystems/unionfs/rename.txt
363 +Rename is a complex beast. The following table shows which rename(2) operations
364 +should succeed and which should fail.
367 +E: error (either unionfs or vfs)
370 +none = file does not exist
371 +file = file is a file
372 +dir = file is a empty directory
373 +child= file is a non-empty directory
374 +wh = file is a directory containing only whiteouts; this makes it logically
377 + none file dir child wh
384 +Renaming directories:
385 +=====================
387 +Whenever a empty (either physically or logically) directory is being renamed,
388 +the following sequence of events should take place:
390 +1) Remove whiteouts from both source and destination directory
391 +2) Rename source to destination
392 +3) Make destination opaque to prevent anything under it from showing up
394 diff --git a/Documentation/filesystems/unionfs/usage.txt b/Documentation/filesystems/unionfs/usage.txt
396 index 0000000..1adde69
398 +++ b/Documentation/filesystems/unionfs/usage.txt
400 +Unionfs is a stackable unification file system, which can appear to merge
401 +the contents of several directories (branches), while keeping their physical
402 +content separate. Unionfs is useful for unified source tree management,
403 +merged contents of split CD-ROM, merged separate software package
404 +directories, data grids, and more. Unionfs allows any mix of read-only and
405 +read-write branches, as well as insertion and deletion of branches anywhere
406 +in the fan-out. To maintain Unix semantics, Unionfs handles elimination of
407 +duplicates, partial-error conditions, and more.
412 +# mount -t unionfs -o <OPTIONS>,<BRANCH-OPTIONS> none MOUNTPOINT
414 +OPTIONS can be any legal combination of:
416 +- ro # mount file system read-only
417 +- rw # mount file system read-write
418 +- remount # remount the file system (see Branch Management below)
419 +- incgen # increment generation no. (see Cache Consistency below)
421 +BRANCH-OPTIONS can be either (1) a list of branches given to the "dirs="
422 +option, or (2) a list of individual branch manipulation commands, combined
423 +with the "remount" option, and is further described in the "Branch
424 +Management" section below.
426 +The syntax for the "dirs=" mount option is:
428 + dirs=branch[=ro|=rw][:...]
430 +The "dirs=" option takes a colon-delimited list of directories to compose
431 +the union, with an optional branch mode for each of those directories.
432 +Directories that come earlier (specified first, on the left) in the list
433 +have a higher precedence than those which come later. Additionally,
434 +read-only or read-write permissions of the branch can be specified by
435 +appending =ro or =rw (default) to each directory. See the Copyup section in
436 +concepts.txt, for a description of Unionfs's behavior when mixing read-only
437 +and read-write branches and mounts.
441 + dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
445 + dirs=/writable_branch=rw:/read-only_branch=ro
451 +Once you mount your union for the first time, using the "dirs=" option, you
452 +can then change the union's overall mode or reconfigure the branches, using
453 +the remount option, as follows.
455 +To downgrade a union from read-write to read-only:
457 +# mount -t unionfs -o remount,ro none MOUNTPOINT
459 +To upgrade a union from read-only to read-write:
461 +# mount -t unionfs -o remount,rw none MOUNTPOINT
463 +To delete a branch /foo, regardless where it is in the current union:
465 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
467 +To insert (add) a branch /foo before /bar:
469 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
471 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
473 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
475 +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
476 +new highest-priority branch), you can use the above syntax, or use a short
477 +hand version as follows:
479 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
481 +To append a branch to the very end (new lowest-priority branch):
483 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
485 +To append a branch to the very end (new lowest-priority branch), in
488 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
490 +Finally, to change the mode of one existing branch, say /foo, from read-only
491 +to read-write, and change /bar from read-write to read-only:
493 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
495 +Note: in Unionfs 2.x, you cannot set the leftmost branch to readonly because
496 +then Unionfs won't have any writable place for copyups to take place.
497 +Moreover, the VFS can get confused when it tries to modify something in a
498 +file system mounted read-write, but isn't permitted to write to it.
499 +Instead, you should set the whole union as readonly, as described above.
500 +If, however, you must set the leftmost branch as readonly, perhaps so you
501 +can get a snapshot of it at a point in time, then you should insert a new
502 +writable top-level branch, and mark the one you want as readonly. This can
503 +be accomplished as follows, assuming that /foo is your current leftmost
506 +# mount -t tmpfs -o size=NNN /new
507 +# mount -t unionfs -o remount,add=/new,mode=/foo=ro none MOUNTPOINT
508 +<do what you want safely in /foo>
509 +# mount -t unionfs -o remount,del=/new,mode=/foo=rw none MOUNTPOINT
510 +<check if there's anything in /new you want to preserve>
516 +If you modify any file on any of the lower branches directly, while there is
517 +a Unionfs 2.x mounted above any of those branches, you should tell Unionfs
518 +to purge its caches and re-get the objects. To do that, you have to
519 +increment the generation number of the superblock using the following
522 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
524 +Note that the older way of incrementing the generation number using an
525 +ioctl, is no longer supported in Unionfs 2.0 and newer. Ioctls in general
526 +are not encouraged. Plus, an ioctl is per-file concept, whereas the
527 +generation number is a per-file-system concept. Worse, such an ioctl
528 +requires an open file, which then has to be invalidated by the very nature
529 +of the generation number increase (read: the old generation increase ioctl
533 +For more information, see <http://unionfs.filesystems.org/>.
534 diff --git a/MAINTAINERS b/MAINTAINERS
535 index 0a613cb..d811b00 100644
538 @@ -4144,6 +4144,14 @@ L: linux-kernel@vger.kernel.org
539 W: http://www.kernel.dk
544 +M: ezk@cs.sunysb.edu
545 +L: unionfs@filesystems.org
546 +W: http://unionfs.filesystems.org
547 +T: git git.kernel.org/pub/scm/linux/kernel/git/ezk/unionfs.git
552 M: oliver@neukum.name
553 diff --git a/fs/Kconfig b/fs/Kconfig
554 index abccb5d..3d9310a 100644
557 @@ -981,6 +981,47 @@ config CONFIGFS_FS
561 +menu "Layered filesystems"
564 + tristate "eCrypt filesystem layer support (EXPERIMENTAL)"
565 + depends on EXPERIMENTAL && KEYS && CRYPTO && NET
567 + Encrypted filesystem that operates on the VFS layer. See
568 + <file:Documentation/filesystems/ecryptfs.txt> to learn more about
569 + eCryptfs. Userspace components are required and can be
570 + obtained from <http://ecryptfs.sf.net>.
572 + To compile this file system support as a module, choose M here: the
573 + module will be called ecryptfs.
576 + tristate "Union file system (EXPERIMENTAL)"
577 + depends on EXPERIMENTAL
579 + Unionfs is a stackable unification file system, which appears to
580 + merge the contents of several directories (branches), while keeping
581 + their physical content separate.
583 + See <http://unionfs.filesystems.org> for details
585 +config UNION_FS_XATTR
586 + bool "Unionfs extended attributes"
587 + depends on UNION_FS
589 + Extended attributes are name:value pairs associated with inodes by
590 + the kernel or by users (see the attr(5) manual page).
594 +config UNION_FS_DEBUG
595 + bool "Debug Unionfs"
596 + depends on UNION_FS
598 + If you say Y here, you can turn on debugging output from Unionfs.
602 menu "Miscellaneous filesystems"
605 @@ -1033,18 +1074,6 @@ config AFFS_FS
606 To compile this file system support as a module, choose M here: the
607 module will be called affs. If unsure, say N.
610 - tristate "eCrypt filesystem layer support (EXPERIMENTAL)"
611 - depends on EXPERIMENTAL && KEYS && CRYPTO && NET
613 - Encrypted filesystem that operates on the VFS layer. See
614 - <file:Documentation/filesystems/ecryptfs.txt> to learn more about
615 - eCryptfs. Userspace components are required and can be
616 - obtained from <http://ecryptfs.sf.net>.
618 - To compile this file system support as a module, choose M here: the
619 - module will be called ecryptfs.
622 tristate "Apple Macintosh file system support (EXPERIMENTAL)"
623 depends on BLOCK && EXPERIMENTAL
624 diff --git a/fs/Makefile b/fs/Makefile
625 index a1482a5..9bf3915 100644
628 @@ -86,6 +86,7 @@ obj-$(CONFIG_ISO9660_FS) += isofs/
629 obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
630 obj-$(CONFIG_HFS_FS) += hfs/
631 obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
632 +obj-$(CONFIG_UNION_FS) += unionfs/
633 obj-$(CONFIG_VXFS_FS) += freevxfs/
634 obj-$(CONFIG_NFS_FS) += nfs/
635 obj-$(CONFIG_EXPORTFS) += exportfs/
636 diff --git a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
637 index 5e59658..4621f89 100644
638 --- a/fs/ecryptfs/dentry.c
639 +++ b/fs/ecryptfs/dentry.c
640 @@ -62,7 +62,7 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
641 struct inode *lower_inode =
642 ecryptfs_inode_to_lower(dentry->d_inode);
644 - fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
645 + fsstack_copy_attr_all(dentry->d_inode, lower_inode);
649 diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
650 index 89209f0..d99a83e 100644
651 --- a/fs/ecryptfs/inode.c
652 +++ b/fs/ecryptfs/inode.c
653 @@ -589,9 +589,9 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
654 lower_new_dir_dentry->d_inode, lower_new_dentry);
657 - fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
658 + fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
659 if (new_dir != old_dir)
660 - fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
661 + fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
663 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
664 dput(lower_new_dentry->d_parent);
665 @@ -913,7 +913,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
666 rc = notify_change(lower_dentry, ia);
667 mutex_unlock(&lower_dentry->d_inode->i_mutex);
669 - fsstack_copy_attr_all(inode, lower_inode, NULL);
670 + fsstack_copy_attr_all(inode, lower_inode);
674 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
675 index 448dfd5..db2db5d 100644
676 --- a/fs/ecryptfs/main.c
677 +++ b/fs/ecryptfs/main.c
678 @@ -197,7 +197,7 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
679 d_add(dentry, inode);
681 d_instantiate(dentry, inode);
682 - fsstack_copy_attr_all(inode, lower_inode, NULL);
683 + fsstack_copy_attr_all(inode, lower_inode);
684 /* This size will be overwritten for real files w/ headers and
686 fsstack_copy_inode_size(inode, lower_inode);
687 diff --git a/fs/namei.c b/fs/namei.c
688 index 4ea63ed..3c8e0d6 100644
691 @@ -392,6 +392,7 @@ void release_open_intent(struct nameidata *nd)
693 fput(nd->intent.open.file);
695 +EXPORT_SYMBOL_GPL(release_open_intent);
697 static inline struct dentry *
698 do_revalidate(struct dentry *dentry, struct nameidata *nd)
699 diff --git a/fs/splice.c b/fs/splice.c
700 index 1bbc6f4..7de91ce 100644
703 @@ -887,8 +887,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
705 * Attempt to initiate a splice from pipe to file.
707 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
708 - loff_t *ppos, size_t len, unsigned int flags)
709 +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
710 + loff_t *ppos, size_t len, unsigned int flags)
714 @@ -904,13 +904,14 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
716 return out->f_op->splice_write(pipe, out, ppos, len, flags);
718 +EXPORT_SYMBOL_GPL(vfs_splice_from);
721 * Attempt to initiate a splice from a file to a pipe.
723 -static long do_splice_to(struct file *in, loff_t *ppos,
724 - struct pipe_inode_info *pipe, size_t len,
725 - unsigned int flags)
726 +long vfs_splice_to(struct file *in, loff_t *ppos,
727 + struct pipe_inode_info *pipe, size_t len,
728 + unsigned int flags)
732 @@ -926,6 +927,7 @@ static long do_splice_to(struct file *in, loff_t *ppos,
734 return in->f_op->splice_read(in, ppos, pipe, len, flags);
736 +EXPORT_SYMBOL_GPL(vfs_splice_to);
739 * splice_direct_to_actor - splices data directly between two non-pipes
740 @@ -995,7 +997,7 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
742 loff_t pos = sd->pos, prev_pos = pos;
744 - ret = do_splice_to(in, &pos, pipe, len, flags);
745 + ret = vfs_splice_to(in, &pos, pipe, len, flags);
746 if (unlikely(ret <= 0))
749 @@ -1054,7 +1056,7 @@ static int direct_splice_actor(struct pipe_inode_info *pipe,
751 struct file *file = sd->u.file;
753 - return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
754 + return vfs_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
758 @@ -1128,7 +1130,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
762 - ret = do_splice_from(pipe, out, off, len, flags);
763 + ret = vfs_splice_from(pipe, out, off, len, flags);
765 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
767 @@ -1149,7 +1151,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
771 - ret = do_splice_to(in, off, pipe, len, flags);
772 + ret = vfs_splice_to(in, off, pipe, len, flags);
774 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
776 diff --git a/fs/stack.c b/fs/stack.c
777 index 67716f6..a66ff6c 100644
782 + * Copyright (c) 2006-2007 Erez Zadok
783 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
784 + * Copyright (c) 2006-2007 Stony Brook University
785 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
787 + * This program is free software; you can redistribute it and/or modify
788 + * it under the terms of the GNU General Public License version 2 as
789 + * published by the Free Software Foundation.
792 #include <linux/module.h>
793 #include <linux/fs.h>
794 #include <linux/fs_stack.h>
796 -/* does _NOT_ require i_mutex to be held.
798 + * does _NOT_ require i_mutex to be held.
800 * This function cannot be inlined since i_size_{read,write} is rather
801 * heavy-weight on 32-bit systems
803 -void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
804 +void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
806 - i_size_write(dst, i_size_read((struct inode *)src));
807 - dst->i_blocks = src->i_blocks;
812 + * i_size_read() includes its own seqlocking and protection from
813 + * preemption (see include/linux/fs.h): we need nothing extra for
814 + * that here, and prefer to avoid nesting locks than attempt to
815 + * keep i_size and i_blocks in synch together.
817 + i_size = i_size_read(src);
820 + * But if CONFIG_LSF (on 32-bit), we ought to make an effort to keep
821 + * the two halves of i_blocks in synch despite SMP or PREEMPT - though
822 + * stat's generic_fillattr() doesn't bother, and we won't be applying
823 + * quotas (where i_blocks does become important) at the upper level.
825 + * We don't actually know what locking is used at the lower level; but
826 + * if it's a filesystem that supports quotas, it will be using i_lock
827 + * as in inode_add_bytes(). tmpfs uses other locking, and its 32-bit
828 + * is (just) able to exceed 2TB i_size with the aid of holes; but its
829 + * i_blocks cannot carry into the upper long without almost 2TB swap -
830 + * let's ignore that case.
832 + if (sizeof(i_blocks) > sizeof(long))
833 + spin_lock(&src->i_lock);
834 + i_blocks = src->i_blocks;
835 + if (sizeof(i_blocks) > sizeof(long))
836 + spin_unlock(&src->i_lock);
839 + * If CONFIG_SMP on 32-bit, it's vital for fsstack_copy_inode_size()
840 + * to hold some lock around i_size_write(), otherwise i_size_read()
841 + * may spin forever (see include/linux/fs.h). We don't necessarily
842 + * hold i_mutex when this is called, so take i_lock for that case.
844 + * And if CONFIG_LSF (on 32-bit), continue our effort to keep the
845 + * two halves of i_blocks in synch despite SMP or PREEMPT: use i_lock
846 + * for that case too, and do both at once by combining the tests.
848 + * There is none of this locking overhead in the 64-bit case.
850 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
851 + spin_lock(&dst->i_lock);
852 + i_size_write(dst, i_size);
853 + dst->i_blocks = i_blocks;
854 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
855 + spin_unlock(&dst->i_lock);
857 EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
859 -/* copy all attributes; get_nlinks is optional way to override the i_nlink
861 + * copy all attributes; get_nlinks is optional way to override the i_nlink
864 -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
865 - int (*get_nlinks)(struct inode *))
866 +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
868 dest->i_mode = src->i_mode;
869 dest->i_uid = src->i_uid;
870 @@ -29,14 +87,6 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
871 dest->i_ctime = src->i_ctime;
872 dest->i_blkbits = src->i_blkbits;
873 dest->i_flags = src->i_flags;
876 - * Update the nlinks AFTER updating the above fields, because the
877 - * get_links callback may depend on them.
880 - dest->i_nlink = src->i_nlink;
882 - dest->i_nlink = (*get_nlinks)(dest);
883 + dest->i_nlink = src->i_nlink;
885 EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
886 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
888 index 0000000..fc98b38
890 +++ b/fs/unionfs/Makefile
892 +UNIONFS_VERSION="2.5 (for 2.6.27-rc6)"
894 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
896 +obj-$(CONFIG_UNION_FS) += unionfs.o
898 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
899 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
900 + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
902 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
904 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
906 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
907 +EXTRA_CFLAGS += -DDEBUG
909 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
911 index 0000000..ed3604e
913 +++ b/fs/unionfs/commonfops.c
916 + * Copyright (c) 2003-2008 Erez Zadok
917 + * Copyright (c) 2003-2006 Charles P. Wright
918 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
919 + * Copyright (c) 2005-2006 Junjiro Okajima
920 + * Copyright (c) 2005 Arun M. Krishnakumar
921 + * Copyright (c) 2004-2006 David P. Quigley
922 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
923 + * Copyright (c) 2003 Puja Gupta
924 + * Copyright (c) 2003 Harikesavan Krishnan
925 + * Copyright (c) 2003-2008 Stony Brook University
926 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
928 + * This program is free software; you can redistribute it and/or modify
929 + * it under the terms of the GNU General Public License version 2 as
930 + * published by the Free Software Foundation.
936 + * 1) Copyup the file
937 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
938 + * stolen from NFS's silly rename
940 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
941 + struct dentry *parent, int bstart, int bindex)
943 + static unsigned int counter;
944 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
945 + const int countersize = sizeof(counter) * 2;
946 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
947 + char name[nlen + 1];
949 + struct dentry *tmp_dentry = NULL;
950 + struct dentry *lower_dentry;
951 + struct dentry *lower_dir_dentry = NULL;
953 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
955 + sprintf(name, ".unionfs%*.*lx",
956 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
959 + * Loop, looking for an unused temp name to copyup to.
961 + * It's somewhat silly that we look for a free temp tmp name in the
962 + * source branch (bstart) instead of the dest branch (bindex), where
963 + * the final name will be created. We _will_ catch it if somehow
964 + * the name exists in the dest branch, but it'd be nice to catch it
965 + * sooner than later.
970 + char *suffix = name + nlen - countersize;
974 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
976 + pr_debug("unionfs: trying to rename %s to %s\n",
977 + dentry->d_name.name, name);
979 + tmp_dentry = lookup_one_len(name, lower_dentry->d_parent,
981 + if (IS_ERR(tmp_dentry)) {
982 + err = PTR_ERR(tmp_dentry);
985 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
988 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
989 + i_size_read(file->f_path.dentry->d_inode));
991 + if (unlikely(err == -EEXIST))
996 + /* bring it to the same state as an unlinked file */
997 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
998 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
999 + atomic_inc(&lower_dentry->d_inode->i_count);
1000 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1001 + lower_dentry->d_inode);
1003 + lower_dir_dentry = lock_parent(lower_dentry);
1004 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
1005 + unlock_dir(lower_dir_dentry);
1009 + unionfs_check_dentry(dentry);
1014 + * put all references held by upper struct file and free lower file pointer
1017 +static void cleanup_file(struct file *file)
1019 + int bindex, bstart, bend;
1020 + struct file **lower_files;
1021 + struct file *lower_file;
1022 + struct super_block *sb = file->f_path.dentry->d_sb;
1024 + lower_files = UNIONFS_F(file)->lower_files;
1025 + bstart = fbstart(file);
1026 + bend = fbend(file);
1028 + for (bindex = bstart; bindex <= bend; bindex++) {
1029 + int i; /* holds (possibly) updated branch index */
1032 + lower_file = unionfs_lower_file_idx(file, bindex);
1037 + * Find new index of matching branch with an open
1038 + * file, since branches could have been added or
1039 + * deleted causing the one with open files to shift.
1041 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
1042 + i = branch_id_to_idx(sb, old_bid);
1043 + if (unlikely(i < 0)) {
1044 + printk(KERN_ERR "unionfs: no superblock for "
1045 + "file %p\n", file);
1049 + /* decrement count of open files */
1052 + * fput will perform an mntput for us on the correct branch.
1053 + * Although we're using the file's old branch configuration,
1054 + * bindex, which is the old index, correctly points to the
1055 + * right branch in the file's branch list. In other words,
1056 + * we're going to mntput the correct branch even if branches
1057 + * have been added/removed.
1060 + UNIONFS_F(file)->lower_files[bindex] = NULL;
1061 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
1064 + UNIONFS_F(file)->lower_files = NULL;
1065 + kfree(lower_files);
1066 + kfree(UNIONFS_F(file)->saved_branch_ids);
1067 + /* set to NULL because caller needs to know if to kfree on error */
1068 + UNIONFS_F(file)->saved_branch_ids = NULL;
1071 +/* open all lower files for a given file */
1072 +static int open_all_files(struct file *file)
1074 + int bindex, bstart, bend, err = 0;
1075 + struct file *lower_file;
1076 + struct dentry *lower_dentry;
1077 + struct dentry *dentry = file->f_path.dentry;
1078 + struct super_block *sb = dentry->d_sb;
1080 + bstart = dbstart(dentry);
1081 + bend = dbend(dentry);
1083 + for (bindex = bstart; bindex <= bend; bindex++) {
1084 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1085 + if (!lower_dentry)
1088 + dget(lower_dentry);
1089 + unionfs_mntget(dentry, bindex);
1090 + branchget(sb, bindex);
1093 + dentry_open(lower_dentry,
1094 + unionfs_lower_mnt_idx(dentry, bindex),
1096 + if (IS_ERR(lower_file)) {
1097 + branchput(sb, bindex);
1098 + err = PTR_ERR(lower_file);
1101 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1108 +/* open the highest priority file for a given upper file */
1109 +static int open_highest_file(struct file *file, bool willwrite)
1111 + int bindex, bstart, bend, err = 0;
1112 + struct file *lower_file;
1113 + struct dentry *lower_dentry;
1114 + struct dentry *dentry = file->f_path.dentry;
1115 + struct dentry *parent = dget_parent(dentry);
1116 + struct inode *parent_inode = parent->d_inode;
1117 + struct super_block *sb = dentry->d_sb;
1119 + bstart = dbstart(dentry);
1120 + bend = dbend(dentry);
1122 + lower_dentry = unionfs_lower_dentry(dentry);
1123 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
1124 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1125 + err = copyup_file(parent_inode, file, bstart, bindex,
1126 + i_size_read(dentry->d_inode));
1130 + atomic_set(&UNIONFS_F(file)->generation,
1131 + atomic_read(&UNIONFS_I(dentry->d_inode)->
1136 + dget(lower_dentry);
1137 + unionfs_mntget(dentry, bstart);
1138 + lower_file = dentry_open(lower_dentry,
1139 + unionfs_lower_mnt_idx(dentry, bstart),
1141 + if (IS_ERR(lower_file)) {
1142 + err = PTR_ERR(lower_file);
1145 + branchget(sb, bstart);
1146 + unionfs_set_lower_file(file, lower_file);
1147 + /* Fix up the position. */
1148 + lower_file->f_pos = file->f_pos;
1150 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1156 +/* perform a delayed copyup of a read-write file on a read-only branch */
1157 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1159 + int bindex, bstart, bend, err = 0;
1160 + struct dentry *dentry = file->f_path.dentry;
1161 + struct inode *parent_inode = parent->d_inode;
1163 + bstart = fbstart(file);
1164 + bend = fbend(file);
1166 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1168 + unionfs_check_file(file);
1169 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1170 + if (!d_deleted(dentry))
1171 + err = copyup_file(parent_inode, file, bstart,
1173 + i_size_read(dentry->d_inode));
1175 + err = copyup_deleted_file(file, dentry, parent,
1177 + /* if succeeded, set lower open-file flags and break */
1179 + struct file *lower_file;
1180 + lower_file = unionfs_lower_file_idx(file, bindex);
1181 + lower_file->f_flags = file->f_flags;
1185 + if (err || (bstart <= fbstart(file)))
1187 + bend = fbend(file);
1188 + for (bindex = bstart; bindex <= bend; bindex++) {
1189 + if (unionfs_lower_file_idx(file, bindex)) {
1190 + branchput(dentry->d_sb, bindex);
1191 + fput(unionfs_lower_file_idx(file, bindex));
1192 + unionfs_set_lower_file_idx(file, bindex, NULL);
1195 + path_put_lowers(dentry, bstart, bend, false);
1196 + iput_lowers(dentry->d_inode, bstart, bend, false);
1197 + /* for reg file, we only open it "once" */
1198 + fbend(file) = fbstart(file);
1199 + dbend(dentry) = dbstart(dentry);
1200 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1203 + unionfs_check_file(file);
1208 + * Helper function for unionfs_file_revalidate/locked.
1209 + * Expects dentry/parent to be locked already, and revalidated.
1211 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1212 + struct dentry *parent,
1213 + struct super_block *sb, int sbgen,
1214 + int dgen, bool willwrite)
1217 + int bstart, bend, orig_brid;
1221 + fgen = atomic_read(&UNIONFS_F(file)->generation);
1224 + * There are two cases we are interested in. The first is if the
1225 + * generation is lower than the super-block. The second is if
1226 + * someone has copied up this file from underneath us, we also need
1227 + * to refresh things.
1229 + if (d_deleted(dentry) ||
1231 + dbstart(dentry) == fbstart(file) &&
1232 + unionfs_lower_file(file)))
1233 + goto out_may_copyup;
1235 + /* save orig branch ID */
1236 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1238 + /* First we throw out the existing files. */
1239 + cleanup_file(file);
1241 + /* Now we reopen the file(s) as in unionfs_open. */
1242 + bstart = fbstart(file) = dbstart(dentry);
1243 + bend = fbend(file) = dbend(dentry);
1245 + size = sizeof(struct file *) * sbmax(sb);
1246 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1247 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1251 + size = sizeof(int) * sbmax(sb);
1252 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1253 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1258 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1259 + /* We need to open all the files. */
1260 + err = open_all_files(file);
1265 + /* We only open the highest priority branch. */
1266 + err = open_highest_file(file, willwrite);
1269 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1270 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1272 + * If we re-opened the file on a different branch
1273 + * than the original one, and this was due to a new
1274 + * branch inserted, then update the mnt counts of
1275 + * the old and new branches accordingly.
1277 + unionfs_mntget(dentry, bstart);
1278 + unionfs_mntput(sb->s_root,
1279 + branch_id_to_idx(sb, orig_brid));
1281 + /* regular files have only one open lower file */
1282 + fbend(file) = fbstart(file);
1284 + atomic_set(&UNIONFS_F(file)->generation,
1285 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1288 + /* Copyup on the first write to a file on a readonly branch. */
1289 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1290 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1291 + is_robranch(dentry)) {
1292 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1293 + dentry->d_name.name);
1294 + err = do_delayed_copyup(file, parent);
1295 + /* regular files have only one open lower file */
1296 + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1297 + fbend(file) = fbstart(file);
1302 + kfree(UNIONFS_F(file)->lower_files);
1303 + kfree(UNIONFS_F(file)->saved_branch_ids);
1309 + * Revalidate the struct file
1310 + * @file: file to revalidate
1311 + * @parent: parent dentry (locked by caller)
1312 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1313 + * Caller must lock/unlock dentry's branch configuration.
1315 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1318 + struct super_block *sb;
1319 + struct dentry *dentry;
1323 + dentry = file->f_path.dentry;
1324 + sb = dentry->d_sb;
1325 + verify_locked(dentry);
1326 + verify_locked(parent);
1329 + * First revalidate the dentry inside struct file,
1330 + * but not unhashed dentries.
1332 + if (!d_deleted(dentry) &&
1333 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1338 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1339 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1341 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1342 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1343 + dentry->d_name.name);
1348 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1349 + sbgen, dgen, willwrite);
1354 +/* unionfs_open helper function: open a directory */
1355 +static int __open_dir(struct inode *inode, struct file *file)
1357 + struct dentry *lower_dentry;
1358 + struct file *lower_file;
1359 + int bindex, bstart, bend;
1360 + struct vfsmount *mnt;
1362 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1363 + bend = fbend(file) = dbend(file->f_path.dentry);
1365 + for (bindex = bstart; bindex <= bend; bindex++) {
1367 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1368 + if (!lower_dentry)
1371 + dget(lower_dentry);
1372 + unionfs_mntget(file->f_path.dentry, bindex);
1373 + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1374 + lower_file = dentry_open(lower_dentry, mnt, file->f_flags);
1375 + if (IS_ERR(lower_file))
1376 + return PTR_ERR(lower_file);
1378 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1381 + * The branchget goes after the open, because otherwise
1382 + * we would miss the reference on release.
1384 + branchget(inode->i_sb, bindex);
1390 +/* unionfs_open helper function: open a file */
1391 +static int __open_file(struct inode *inode, struct file *file,
1392 + struct dentry *parent)
1394 + struct dentry *lower_dentry;
1395 + struct file *lower_file;
1397 + int bindex, bstart, bend;
1399 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1400 + lower_flags = file->f_flags;
1402 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1403 + bend = fbend(file) = dbend(file->f_path.dentry);
1406 + * check for the permission for lower file. If the error is
1407 + * COPYUP_ERR, copyup the file.
1409 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1411 + * if the open will change the file, copy it up otherwise
1414 + if (lower_flags & O_TRUNC) {
1418 + /* copyup the file */
1419 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1420 + err = copyup_file(parent->d_inode, file,
1421 + bstart, bindex, size);
1428 + * turn off writeable flags, to force delayed copyup
1431 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1435 + dget(lower_dentry);
1438 + * dentry_open will decrement mnt refcnt if err.
1439 + * otherwise fput() will do an mntput() for us upon file close.
1441 + unionfs_mntget(file->f_path.dentry, bstart);
1443 + dentry_open(lower_dentry,
1444 + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1446 + if (IS_ERR(lower_file))
1447 + return PTR_ERR(lower_file);
1449 + unionfs_set_lower_file(file, lower_file);
1450 + branchget(inode->i_sb, bstart);
1455 +int unionfs_open(struct inode *inode, struct file *file)
1458 + struct file *lower_file = NULL;
1459 + struct dentry *dentry = file->f_path.dentry;
1460 + struct dentry *parent;
1461 + int bindex = 0, bstart = 0, bend = 0;
1465 + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1466 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1467 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1469 + /* don't open unhashed/deleted files */
1470 + if (d_deleted(dentry)) {
1475 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1476 + valid = __unionfs_d_revalidate(dentry, parent, false);
1477 + if (unlikely(!valid)) {
1482 + file->private_data =
1483 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1484 + if (unlikely(!UNIONFS_F(file))) {
1488 + fbstart(file) = -1;
1490 + atomic_set(&UNIONFS_F(file)->generation,
1491 + atomic_read(&UNIONFS_I(inode)->generation));
1493 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1494 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1495 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1499 + size = sizeof(int) * sbmax(inode->i_sb);
1500 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1501 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1506 + bstart = fbstart(file) = dbstart(dentry);
1507 + bend = fbend(file) = dbend(dentry);
1510 + * open all directories and make the unionfs file struct point to
1511 + * these lower file structs
1513 + if (S_ISDIR(inode->i_mode))
1514 + err = __open_dir(inode, file); /* open a dir */
1516 + err = __open_file(inode, file, parent); /* open a file */
1518 + /* freeing the allocated resources, and fput the opened files */
1520 + for (bindex = bstart; bindex <= bend; bindex++) {
1521 + lower_file = unionfs_lower_file_idx(file, bindex);
1525 + branchput(dentry->d_sb, bindex);
1526 + /* fput calls dput for lower_dentry */
1533 + kfree(UNIONFS_F(file)->lower_files);
1534 + kfree(UNIONFS_F(file)->saved_branch_ids);
1535 + kfree(UNIONFS_F(file));
1539 + unionfs_postcopyup_setmnt(dentry);
1540 + unionfs_copy_attr_times(inode);
1541 + unionfs_check_file(file);
1542 + unionfs_check_inode(inode);
1544 + unionfs_unlock_dentry(dentry);
1545 + unionfs_unlock_parent(dentry, parent);
1546 + unionfs_read_unlock(inode->i_sb);
1551 + * release all lower object references & free the file info structure
1553 + * No need to grab sb info's rwsem.
1555 +int unionfs_file_release(struct inode *inode, struct file *file)
1557 + struct file *lower_file = NULL;
1558 + struct unionfs_file_info *fileinfo;
1559 + struct unionfs_inode_info *inodeinfo;
1560 + struct super_block *sb = inode->i_sb;
1561 + struct dentry *dentry = file->f_path.dentry;
1562 + struct dentry *parent;
1563 + int bindex, bstart, bend;
1564 + int fgen, err = 0;
1566 + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1567 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1568 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1571 + * We try to revalidate, but the VFS ignores return return values
1572 + * from file->release, so we must always try to succeed here,
1573 + * including to do the kfree and dput below. So if revalidation
1574 + * failed, all we can do is print some message and keep going.
1576 + err = unionfs_file_revalidate(file, parent,
1577 + UNIONFS_F(file)->wrote_to_file);
1579 + unionfs_check_file(file);
1580 + fileinfo = UNIONFS_F(file);
1581 + BUG_ON(file->f_path.dentry->d_inode != inode);
1582 + inodeinfo = UNIONFS_I(inode);
1584 + /* fput all the lower files */
1585 + fgen = atomic_read(&fileinfo->generation);
1586 + bstart = fbstart(file);
1587 + bend = fbend(file);
1589 + for (bindex = bstart; bindex <= bend; bindex++) {
1590 + lower_file = unionfs_lower_file_idx(file, bindex);
1593 + unionfs_set_lower_file_idx(file, bindex, NULL);
1595 + branchput(sb, bindex);
1598 + /* if there are no more refs to the dentry, dput it */
1599 + if (d_deleted(dentry)) {
1600 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1601 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1605 + kfree(fileinfo->lower_files);
1606 + kfree(fileinfo->saved_branch_ids);
1608 + if (fileinfo->rdstate) {
1609 + fileinfo->rdstate->access = jiffies;
1610 + spin_lock(&inodeinfo->rdlock);
1611 + inodeinfo->rdcount++;
1612 + list_add_tail(&fileinfo->rdstate->cache,
1613 + &inodeinfo->readdircache);
1614 + mark_inode_dirty(inode);
1615 + spin_unlock(&inodeinfo->rdlock);
1616 + fileinfo->rdstate = NULL;
1620 + unionfs_unlock_dentry(dentry);
1621 + unionfs_unlock_parent(dentry, parent);
1622 + unionfs_read_unlock(sb);
1626 +/* pass the ioctl to the lower fs */
1627 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1629 + struct file *lower_file;
1632 + lower_file = unionfs_lower_file(file);
1635 + if (!lower_file || !lower_file->f_op)
1637 + if (lower_file->f_op->unlocked_ioctl) {
1638 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1639 + } else if (lower_file->f_op->ioctl) {
1641 + err = lower_file->f_op->ioctl(
1642 + lower_file->f_path.dentry->d_inode,
1643 + lower_file, cmd, arg);
1652 + * return to user-space the branch indices containing the file in question
1654 + * We use fd_set and therefore we are limited to the number of the branches
1655 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1657 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1658 + unsigned int cmd, unsigned long arg)
1661 + fd_set branchlist;
1662 + int bstart = 0, bend = 0, bindex = 0;
1663 + int orig_bstart, orig_bend;
1664 + struct dentry *dentry, *lower_dentry;
1665 + struct vfsmount *mnt;
1667 + dentry = file->f_path.dentry;
1668 + orig_bstart = dbstart(dentry);
1669 + orig_bend = dbend(dentry);
1670 + err = unionfs_partial_lookup(dentry, parent);
1673 + bstart = dbstart(dentry);
1674 + bend = dbend(dentry);
1676 + FD_ZERO(&branchlist);
1678 + for (bindex = bstart; bindex <= bend; bindex++) {
1679 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1680 + if (!lower_dentry)
1682 + if (likely(lower_dentry->d_inode))
1683 + FD_SET(bindex, &branchlist);
1684 + /* purge any lower objects after partial_lookup */
1685 + if (bindex < orig_bstart || bindex > orig_bend) {
1686 + dput(lower_dentry);
1687 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1688 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1689 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1691 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1694 + unionfs_mntput(dentry, bindex);
1695 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1698 + /* restore original dentry's offsets */
1699 + dbstart(dentry) = orig_bstart;
1700 + dbend(dentry) = orig_bend;
1701 + ibstart(dentry->d_inode) = orig_bstart;
1702 + ibend(dentry->d_inode) = orig_bend;
1704 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1705 + if (unlikely(err))
1709 + return err < 0 ? err : bend;
1712 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1715 + struct dentry *dentry = file->f_path.dentry;
1716 + struct dentry *parent;
1718 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1719 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1720 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1722 + err = unionfs_file_revalidate(file, parent, true);
1723 + if (unlikely(err))
1726 + /* check if asked for local commands */
1728 + case UNIONFS_IOCTL_INCGEN:
1729 + /* Increment the superblock generation count */
1730 + pr_info("unionfs: incgen ioctl deprecated; "
1731 + "use \"-o remount,incgen\"\n");
1735 + case UNIONFS_IOCTL_QUERYFILE:
1736 + /* Return list of branches containing the given file */
1737 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1741 + /* pass the ioctl down */
1742 + err = do_ioctl(file, cmd, arg);
1747 + unionfs_check_file(file);
1748 + unionfs_unlock_dentry(dentry);
1749 + unionfs_unlock_parent(dentry, parent);
1750 + unionfs_read_unlock(dentry->d_sb);
1754 +int unionfs_flush(struct file *file, fl_owner_t id)
1757 + struct file *lower_file = NULL;
1758 + struct dentry *dentry = file->f_path.dentry;
1759 + struct dentry *parent;
1760 + int bindex, bstart, bend;
1762 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1763 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1764 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1766 + err = unionfs_file_revalidate(file, parent,
1767 + UNIONFS_F(file)->wrote_to_file);
1768 + if (unlikely(err))
1770 + unionfs_check_file(file);
1772 + bstart = fbstart(file);
1773 + bend = fbend(file);
1774 + for (bindex = bstart; bindex <= bend; bindex++) {
1775 + lower_file = unionfs_lower_file_idx(file, bindex);
1777 + if (lower_file && lower_file->f_op &&
1778 + lower_file->f_op->flush) {
1779 + err = lower_file->f_op->flush(lower_file, id);
1788 + unionfs_check_file(file);
1789 + unionfs_unlock_dentry(dentry);
1790 + unionfs_unlock_parent(dentry, parent);
1791 + unionfs_read_unlock(dentry->d_sb);
1794 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1795 new file mode 100644
1796 index 0000000..ae6ea2b
1798 +++ b/fs/unionfs/copyup.c
1801 + * Copyright (c) 2003-2008 Erez Zadok
1802 + * Copyright (c) 2003-2006 Charles P. Wright
1803 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1804 + * Copyright (c) 2005-2006 Junjiro Okajima
1805 + * Copyright (c) 2005 Arun M. Krishnakumar
1806 + * Copyright (c) 2004-2006 David P. Quigley
1807 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1808 + * Copyright (c) 2003 Puja Gupta
1809 + * Copyright (c) 2003 Harikesavan Krishnan
1810 + * Copyright (c) 2003-2008 Stony Brook University
1811 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
1813 + * This program is free software; you can redistribute it and/or modify
1814 + * it under the terms of the GNU General Public License version 2 as
1815 + * published by the Free Software Foundation.
1821 + * For detailed explanation of copyup see:
1822 + * Documentation/filesystems/unionfs/concepts.txt
1825 +#ifdef CONFIG_UNION_FS_XATTR
1826 +/* copyup all extended attrs for a given dentry */
1827 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1828 + struct dentry *new_lower_dentry)
1831 + ssize_t list_size = -1;
1832 + char *name_list = NULL;
1833 + char *attr_value = NULL;
1834 + char *name_list_buf = NULL;
1836 + /* query the actual size of the xattr list */
1837 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1838 + if (list_size <= 0) {
1843 + /* allocate space for the actual list */
1844 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1845 + if (unlikely(!name_list || IS_ERR(name_list))) {
1846 + err = PTR_ERR(name_list);
1850 + name_list_buf = name_list; /* save for kfree at end */
1852 + /* now get the actual xattr list of the source file */
1853 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1854 + if (list_size <= 0) {
1859 + /* allocate space to hold each xattr's value */
1860 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1861 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1862 + err = PTR_ERR(name_list);
1866 + /* in a loop, get and set each xattr from src to dst file */
1867 + while (*name_list) {
1870 + /* Lock here since vfs_getxattr doesn't lock for us */
1871 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1872 + size = vfs_getxattr(old_lower_dentry, name_list,
1873 + attr_value, XATTR_SIZE_MAX);
1874 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1879 + if (size > XATTR_SIZE_MAX) {
1883 + /* Don't lock here since vfs_setxattr does it for us. */
1884 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1887 + * Selinux depends on "security.*" xattrs, so to maintain
1888 + * the security of copied-up files, if Selinux is active,
1889 + * then we must copy these xattrs as well. So we need to
1890 + * temporarily get FOWNER privileges.
1891 + * XXX: move entire copyup code to SIOQ.
1893 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1894 + cap_raise(current->cap_effective, CAP_FOWNER);
1895 + err = vfs_setxattr(new_lower_dentry, name_list,
1896 + attr_value, size, 0);
1897 + cap_lower(current->cap_effective, CAP_FOWNER);
1901 + name_list += strlen(name_list) + 1;
1904 + unionfs_xattr_kfree(name_list_buf);
1905 + unionfs_xattr_kfree(attr_value);
1906 + /* Ignore if xattr isn't supported */
1907 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1911 +#endif /* CONFIG_UNION_FS_XATTR */
1914 + * Determine the mode based on the copyup flags, and the existing dentry.
1916 + * Handle file systems which may not support certain options. For example
1917 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1918 + * errors, rather than propagating them up, which results in copyup errors
1919 + * and errors returned back to users.
1921 +static int copyup_permissions(struct super_block *sb,
1922 + struct dentry *old_lower_dentry,
1923 + struct dentry *new_lower_dentry)
1925 + struct inode *i = old_lower_dentry->d_inode;
1926 + struct iattr newattrs;
1929 + newattrs.ia_atime = i->i_atime;
1930 + newattrs.ia_mtime = i->i_mtime;
1931 + newattrs.ia_ctime = i->i_ctime;
1932 + newattrs.ia_gid = i->i_gid;
1933 + newattrs.ia_uid = i->i_uid;
1934 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1935 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1936 + ATTR_GID | ATTR_UID;
1937 + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1938 + err = notify_change(new_lower_dentry, &newattrs);
1942 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1943 + newattrs.ia_mode = i->i_mode;
1944 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1945 + err = notify_change(new_lower_dentry, &newattrs);
1946 + if (err == -EOPNOTSUPP &&
1947 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1948 + printk(KERN_WARNING
1949 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1950 + new_lower_dentry->d_name.name);
1955 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1960 + * create the new device/file/directory - use copyup_permission to copyup
1963 + * if the object being copied up is a regular file, the file is only created,
1964 + * the contents have to be copied up separately
1966 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1967 + struct dentry *new_lower_dentry,
1968 + struct dentry *new_lower_parent_dentry,
1972 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1973 + struct sioq_args args;
1975 + if (S_ISDIR(old_mode)) {
1976 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1977 + args.mkdir.dentry = new_lower_dentry;
1978 + args.mkdir.mode = old_mode;
1980 + run_sioq(__unionfs_mkdir, &args);
1982 + } else if (S_ISLNK(old_mode)) {
1983 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1984 + args.symlink.dentry = new_lower_dentry;
1985 + args.symlink.symbuf = symbuf;
1987 + run_sioq(__unionfs_symlink, &args);
1989 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1990 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1991 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1992 + args.mknod.dentry = new_lower_dentry;
1993 + args.mknod.mode = old_mode;
1994 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1996 + run_sioq(__unionfs_mknod, &args);
1998 + } else if (S_ISREG(old_mode)) {
1999 + struct nameidata nd;
2000 + err = init_lower_nd(&nd, LOOKUP_CREATE);
2001 + if (unlikely(err < 0))
2003 + args.create.nd = &nd;
2004 + args.create.parent = new_lower_parent_dentry->d_inode;
2005 + args.create.dentry = new_lower_dentry;
2006 + args.create.mode = old_mode;
2008 + run_sioq(__unionfs_create, &args);
2010 + release_lower_nd(&nd, err);
2012 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
2021 +static int __copyup_reg_data(struct dentry *dentry,
2022 + struct dentry *new_lower_dentry, int new_bindex,
2023 + struct dentry *old_lower_dentry, int old_bindex,
2024 + struct file **copyup_file, loff_t len)
2026 + struct super_block *sb = dentry->d_sb;
2027 + struct file *input_file;
2028 + struct file *output_file;
2029 + struct vfsmount *output_mnt;
2030 + mm_segment_t old_fs;
2032 + ssize_t read_bytes, write_bytes;
2036 + /* open old file */
2037 + unionfs_mntget(dentry, old_bindex);
2038 + branchget(sb, old_bindex);
2039 + /* dentry_open calls dput and mntput if it returns an error */
2040 + input_file = dentry_open(old_lower_dentry,
2041 + unionfs_lower_mnt_idx(dentry, old_bindex),
2042 + O_RDONLY | O_LARGEFILE);
2043 + if (IS_ERR(input_file)) {
2044 + dput(old_lower_dentry);
2045 + err = PTR_ERR(input_file);
2048 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
2050 + goto out_close_in;
2053 + /* open new file */
2054 + dget(new_lower_dentry);
2055 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
2056 + branchget(sb, new_bindex);
2057 + output_file = dentry_open(new_lower_dentry, output_mnt,
2058 + O_RDWR | O_LARGEFILE);
2059 + if (IS_ERR(output_file)) {
2060 + err = PTR_ERR(output_file);
2061 + goto out_close_in2;
2063 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
2065 + goto out_close_out;
2068 + /* allocating a buffer */
2069 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2070 + if (unlikely(!buf)) {
2072 + goto out_close_out;
2075 + input_file->f_pos = 0;
2076 + output_file->f_pos = 0;
2078 + old_fs = get_fs();
2079 + set_fs(KERNEL_DS);
2084 + if (len >= PAGE_SIZE)
2086 + else if ((len < PAGE_SIZE) && (len > 0))
2092 + input_file->f_op->read(input_file,
2093 + (char __user *)buf, size,
2094 + &input_file->f_pos);
2095 + if (read_bytes <= 0) {
2100 + /* see Documentation/filesystems/unionfs/issues.txt */
2103 + output_file->f_op->write(output_file,
2104 + (char __user *)buf,
2106 + &output_file->f_pos);
2108 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2109 + err = write_bytes;
2112 + } while ((read_bytes > 0) && (len > 0));
2119 + err = output_file->f_op->fsync(output_file,
2120 + new_lower_dentry, 0);
2123 + goto out_close_out;
2125 + if (copyup_file) {
2126 + *copyup_file = output_file;
2127 + goto out_close_in;
2131 + fput(output_file);
2134 + branchput(sb, new_bindex);
2140 + branchput(sb, old_bindex);
2146 + * dput the lower references for old and new dentry & clear a lower dentry
2149 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2150 + int old_bstart, int old_bend,
2151 + struct dentry *new_lower_dentry, int new_bindex)
2153 + /* get rid of the lower dentry and all its traces */
2154 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2155 + dbstart(dentry) = old_bstart;
2156 + dbend(dentry) = old_bend;
2158 + dput(new_lower_dentry);
2159 + dput(old_lower_dentry);
2163 + * Copy up a dentry to a file of specified name.
2165 + * @dir: used to pull the ->i_sb to access other branches
2166 + * @dentry: the non-negative dentry whose lower_inode we should copy
2167 + * @bstart: the branch of the lower_inode to copy from
2168 + * @new_bindex: the branch to create the new file in
2169 + * @name: the name of the file to create
2170 + * @namelen: length of @name
2171 + * @copyup_file: the "struct file" to return (optional)
2172 + * @len: how many bytes to copy-up?
2174 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2175 + int new_bindex, const char *name, int namelen,
2176 + struct file **copyup_file, loff_t len)
2178 + struct dentry *new_lower_dentry;
2179 + struct dentry *old_lower_dentry = NULL;
2180 + struct super_block *sb;
2185 + struct dentry *new_lower_parent_dentry = NULL;
2186 + mm_segment_t oldfs;
2187 + char *symbuf = NULL;
2189 + verify_locked(dentry);
2191 + old_bindex = bstart;
2192 + old_bstart = dbstart(dentry);
2193 + old_bend = dbend(dentry);
2195 + BUG_ON(new_bindex < 0);
2196 + BUG_ON(new_bindex >= old_bindex);
2200 + err = is_robranch_super(sb, new_bindex);
2204 + /* Create the directory structure above this dentry. */
2205 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2206 + if (IS_ERR(new_lower_dentry)) {
2207 + err = PTR_ERR(new_lower_dentry);
2211 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2212 + /* we conditionally dput this old_lower_dentry at end of function */
2213 + dget(old_lower_dentry);
2215 + /* For symlinks, we must read the link before we lock the directory. */
2216 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2218 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2219 + if (unlikely(!symbuf)) {
2220 + __clear(dentry, old_lower_dentry,
2221 + old_bstart, old_bend,
2222 + new_lower_dentry, new_bindex);
2228 + set_fs(KERNEL_DS);
2229 + err = old_lower_dentry->d_inode->i_op->readlink(
2231 + (char __user *)symbuf,
2235 + __clear(dentry, old_lower_dentry,
2236 + old_bstart, old_bend,
2237 + new_lower_dentry, new_bindex);
2240 + symbuf[err] = '\0';
2243 + /* Now we lock the parent, and create the object in the new branch. */
2244 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2246 + /* create the new inode */
2247 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2248 + new_lower_parent_dentry, symbuf);
2251 + __clear(dentry, old_lower_dentry,
2252 + old_bstart, old_bend,
2253 + new_lower_dentry, new_bindex);
2257 + /* We actually copyup the file here. */
2258 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2259 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2260 + old_lower_dentry, old_bindex,
2261 + copyup_file, len);
2265 + /* Set permissions. */
2266 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2270 +#ifdef CONFIG_UNION_FS_XATTR
2271 + /* Selinux uses extended attributes for permissions. */
2272 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2275 +#endif /* CONFIG_UNION_FS_XATTR */
2277 + /* do not allow files getting deleted to be re-interposed */
2278 + if (!d_deleted(dentry))
2279 + unionfs_reinterpose(dentry);
2285 + * copyup failed, because we possibly ran out of space or
2286 + * quota, or something else happened so let's unlink; we don't
2287 + * really care about the return value of vfs_unlink
2289 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2291 + if (copyup_file) {
2292 + /* need to close the file */
2294 + fput(*copyup_file);
2295 + branchput(sb, new_bindex);
2299 + * TODO: should we reset the error to something like -EIO?
2301 + * If we don't reset, the user may get some nonsensical errors, but
2302 + * on the other hand, if we reset to EIO, we guarantee that the user
2303 + * will get a "confusing" error message.
2307 + unlock_dir(new_lower_parent_dentry);
2311 + * If old_lower_dentry was not a file, then we need to dput it. If
2312 + * it was a file, then it was already dput indirectly by other
2313 + * functions we call above which operate on regular files.
2315 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2316 + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2317 + dput(old_lower_dentry);
2322 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2323 + unionfs_postcopyup_release(dentry);
2324 + if (!unionfs_lower_inode(dentry->d_inode)) {
2326 + * If we got here, then we copied up to an
2327 + * unlinked-open file, whose name is .unionfsXXXXX.
2329 + struct inode *inode = new_lower_dentry->d_inode;
2330 + atomic_inc(&inode->i_count);
2331 + unionfs_set_lower_inode_idx(dentry->d_inode,
2332 + ibstart(dentry->d_inode),
2336 + unionfs_postcopyup_setmnt(dentry);
2337 + /* sync inode times from copied-up inode to our inode */
2338 + unionfs_copy_attr_times(dentry->d_inode);
2339 + unionfs_check_inode(dir);
2340 + unionfs_check_dentry(dentry);
2346 + * This function creates a copy of a file represented by 'file' which
2347 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2348 + * will be named "name".
2350 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2351 + int bstart, int new_bindex, loff_t len)
2354 + struct file *output_file = NULL;
2356 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2357 + name, strlen(name), &output_file, len);
2359 + fbstart(file) = new_bindex;
2360 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2367 + * This function creates a copy of a file represented by 'file' which
2368 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2370 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2371 + int new_bindex, loff_t len)
2374 + struct file *output_file = NULL;
2375 + struct dentry *dentry = file->f_path.dentry;
2377 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2378 + dentry->d_name.name, dentry->d_name.len,
2379 + &output_file, len);
2381 + fbstart(file) = new_bindex;
2382 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2388 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2389 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2390 + int old_bstart, int old_bend)
2394 + int new_bstart = -1;
2395 + int new_bend = -1;
2398 + loop_start = min(old_bstart, bindex);
2399 + loop_end = max(old_bend, bindex);
2402 + * This loop sets the bstart and bend for the new dentry by
2403 + * traversing from left to right. It also dputs all negative
2404 + * dentries except bindex
2406 + for (i = loop_start; i <= loop_end; i++) {
2407 + if (!unionfs_lower_dentry_idx(dentry, i))
2410 + if (i == bindex) {
2412 + if (new_bstart < 0)
2417 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2418 + dput(unionfs_lower_dentry_idx(dentry, i));
2419 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2421 + unionfs_mntput(dentry, i);
2422 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2424 + if (new_bstart < 0)
2430 + if (new_bstart < 0)
2431 + new_bstart = bindex;
2433 + new_bend = bindex;
2434 + dbstart(dentry) = new_bstart;
2435 + dbend(dentry) = new_bend;
2439 +/* set lower inode ptr and update bstart & bend if necessary */
2440 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2443 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2444 + igrab(lower->d_inode));
2445 + if (likely(ibstart(upper->d_inode) > bindex))
2446 + ibstart(upper->d_inode) = bindex;
2447 + if (likely(ibend(upper->d_inode) < bindex))
2448 + ibend(upper->d_inode) = bindex;
2452 +/* set lower dentry ptr and update bstart & bend if necessary */
2453 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2456 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2457 + if (likely(dbstart(upper) > bindex))
2458 + dbstart(upper) = bindex;
2459 + if (likely(dbend(upper) < bindex))
2460 + dbend(upper) = bindex;
2464 + * This function replicates the directory structure up-to given dentry
2465 + * in the bindex branch.
2467 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2468 + const char *name, int bindex)
2471 + struct dentry *child_dentry;
2472 + struct dentry *parent_dentry;
2473 + struct dentry *lower_parent_dentry = NULL;
2474 + struct dentry *lower_dentry = NULL;
2475 + const char *childname;
2476 + unsigned int childnamelen;
2481 + struct dentry **path = NULL;
2482 + struct super_block *sb;
2484 + verify_locked(dentry);
2486 + err = is_robranch_super(dir->i_sb, bindex);
2488 + lower_dentry = ERR_PTR(err);
2492 + old_bstart = dbstart(dentry);
2493 + old_bend = dbend(dentry);
2495 + lower_dentry = ERR_PTR(-ENOMEM);
2497 + /* There is no sense allocating any less than the minimum. */
2499 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2500 + if (unlikely(!path))
2503 + /* assume the negative dentry of unionfs as the parent dentry */
2504 + parent_dentry = dentry;
2507 + * This loop finds the first parent that exists in the given branch.
2508 + * We start building the directory structure from there. At the end
2509 + * of the loop, the following should hold:
2510 + * - child_dentry is the first nonexistent child
2511 + * - parent_dentry is the first existent parent
2512 + * - path[0] is the = deepest child
2513 + * - path[count] is the first child to create
2516 + child_dentry = parent_dentry;
2518 + /* find the parent directory dentry in unionfs */
2519 + parent_dentry = dget_parent(child_dentry);
2521 + /* find out the lower_parent_dentry in the given branch */
2522 + lower_parent_dentry =
2523 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2525 + /* grow path table */
2526 + if (count == nr_dentry) {
2530 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2532 + if (unlikely(!p)) {
2533 + lower_dentry = ERR_PTR(-ENOMEM);
2539 + /* store the child dentry */
2540 + path[count++] = child_dentry;
2541 + } while (!lower_parent_dentry);
2544 + sb = dentry->d_sb;
2547 + * This code goes between the begin/end labels and basically
2548 + * emulates a while(child_dentry != dentry), only cleaner and
2549 + * shorter than what would be a much longer while loop.
2552 + /* get lower parent dir in the current branch */
2553 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2554 + dput(parent_dentry);
2556 + /* init the values to lookup */
2557 + childname = child_dentry->d_name.name;
2558 + childnamelen = child_dentry->d_name.len;
2560 + if (child_dentry != dentry) {
2561 + /* lookup child in the underlying file system */
2562 + lower_dentry = lookup_one_len(childname, lower_parent_dentry,
2564 + if (IS_ERR(lower_dentry))
2568 + * Is the name a whiteout of the child name ? lookup the
2569 + * whiteout child in the underlying file system
2571 + lower_dentry = lookup_one_len(name, lower_parent_dentry,
2573 + if (IS_ERR(lower_dentry))
2576 + /* Replace the current dentry (if any) with the new one */
2577 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2578 + unionfs_set_lower_dentry_idx(dentry, bindex,
2581 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2585 + if (lower_dentry->d_inode) {
2587 + * since this already exists we dput to avoid
2588 + * multiple references on the same dentry
2590 + dput(lower_dentry);
2592 + struct sioq_args args;
2594 + /* it's a negative dentry, create a new dir */
2595 + lower_parent_dentry = lock_parent(lower_dentry);
2597 + args.mkdir.parent = lower_parent_dentry->d_inode;
2598 + args.mkdir.dentry = lower_dentry;
2599 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2601 + run_sioq(__unionfs_mkdir, &args);
2605 + err = copyup_permissions(dir->i_sb, child_dentry,
2607 + unlock_dir(lower_parent_dentry);
2609 + dput(lower_dentry);
2610 + lower_dentry = ERR_PTR(err);
2616 + __set_inode(child_dentry, lower_dentry, bindex);
2617 + __set_dentry(child_dentry, lower_dentry, bindex);
2619 + * update times of this dentry, but also the parent, because if
2620 + * we changed, the parent may have changed too.
2622 + fsstack_copy_attr_times(parent_dentry->d_inode,
2623 + lower_parent_dentry->d_inode);
2624 + unionfs_copy_attr_times(child_dentry->d_inode);
2626 + parent_dentry = child_dentry;
2627 + child_dentry = path[--count];
2630 + /* cleanup any leftover locks from the do/while loop above */
2631 + if (IS_ERR(lower_dentry))
2633 + dput(path[count--]);
2635 + return lower_dentry;
2639 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2640 + * dentry+parents to the first parent node that has an mnt.
2642 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2644 + struct dentry *parent, *hasone;
2645 + int bindex = dbstart(dentry);
2647 + if (unionfs_lower_mnt_idx(dentry, bindex))
2649 + hasone = dentry->d_parent;
2650 + /* this loop should stop at root dentry */
2651 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2652 + hasone = hasone->d_parent;
2654 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2655 + unionfs_set_lower_mnt_idx(parent, bindex,
2656 + unionfs_mntget(hasone, bindex));
2657 + parent = parent->d_parent;
2662 + * Post-copyup helper to release all non-directory source objects of a
2663 + * copied-up file. Regular files should have only one lower object.
2665 +void unionfs_postcopyup_release(struct dentry *dentry)
2669 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2670 + bstart = dbstart(dentry);
2671 + bend = dbend(dentry);
2673 + path_put_lowers(dentry, bstart + 1, bend, false);
2674 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2676 + dbend(dentry) = bstart;
2677 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2679 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2680 new file mode 100644
2681 index 0000000..db62d22
2683 +++ b/fs/unionfs/debug.c
2686 + * Copyright (c) 2003-2008 Erez Zadok
2687 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2688 + * Copyright (c) 2003-2008 Stony Brook University
2689 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
2691 + * This program is free software; you can redistribute it and/or modify
2692 + * it under the terms of the GNU General Public License version 2 as
2693 + * published by the Free Software Foundation.
2699 + * Helper debugging functions for maintainers (and for users to report back
2700 + * useful information back to maintainers)
2703 +/* it's always useful to know what part of the code called us */
2704 +#define PRINT_CALLER(fname, fxn, line) \
2706 + if (!printed_caller) { \
2707 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2708 + printed_caller = 1; \
2713 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2714 + * the fan-out of various Unionfs objects. We check that no lower objects
2715 + * exist outside the start/end branch range; that all objects within are
2716 + * non-NULL (with some allowed exceptions); that for every lower file
2717 + * there's a lower dentry+inode; that the start/end ranges match for all
2718 + * corresponding lower objects; that open files/symlinks have only one lower
2719 + * objects, but directories can have several; and more.
2721 +void __unionfs_check_inode(const struct inode *inode,
2722 + const char *fname, const char *fxn, int line)
2726 + struct inode *lower_inode;
2727 + struct super_block *sb;
2728 + int printed_caller = 0;
2731 + /* for inodes now */
2734 + istart = ibstart(inode);
2735 + iend = ibend(inode);
2736 + /* don't check inode if no lower branches */
2737 + if (istart < 0 && iend < 0)
2739 + if (unlikely(istart > iend)) {
2740 + PRINT_CALLER(fname, fxn, line);
2741 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2742 + inode, istart, iend);
2744 + if (unlikely((istart == -1 && iend != -1) ||
2745 + (istart != -1 && iend == -1))) {
2746 + PRINT_CALLER(fname, fxn, line);
2747 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2748 + inode, istart, iend);
2750 + if (!S_ISDIR(inode->i_mode)) {
2751 + if (unlikely(iend != istart)) {
2752 + PRINT_CALLER(fname, fxn, line);
2753 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2754 + inode, istart, iend);
2758 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2759 + if (unlikely(!UNIONFS_I(inode))) {
2760 + PRINT_CALLER(fname, fxn, line);
2761 + pr_debug(" Ci3: no inode_info %p\n", inode);
2764 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2765 + PRINT_CALLER(fname, fxn, line);
2766 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2769 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2770 + if (lower_inode) {
2771 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2772 + if (unlikely(bindex < istart || bindex > iend)) {
2773 + PRINT_CALLER(fname, fxn, line);
2774 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2775 + "istart/end=%d:%d\n", inode,
2776 + lower_inode, bindex, istart, iend);
2777 + } else if (unlikely(lower_inode == poison_ptr)) {
2778 + /* freed inode! */
2779 + PRINT_CALLER(fname, fxn, line);
2780 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2781 + "istart/end=%d:%d\n", inode,
2782 + lower_inode, bindex, istart, iend);
2786 + /* if we get here, then lower_inode == NULL */
2787 + if (bindex < istart || bindex > iend)
2790 + * directories can have NULL lower inodes in b/t start/end,
2791 + * but NOT if at the start/end range.
2793 + if (unlikely(S_ISDIR(inode->i_mode) &&
2794 + bindex > istart && bindex < iend))
2796 + PRINT_CALLER(fname, fxn, line);
2797 + pr_debug(" Ci7: inode/linode=%p:%p "
2798 + "bindex=%d istart/end=%d:%d\n",
2799 + inode, lower_inode, bindex, istart, iend);
2803 +void __unionfs_check_dentry(const struct dentry *dentry,
2804 + const char *fname, const char *fxn, int line)
2807 + int dstart, dend, istart, iend;
2808 + struct dentry *lower_dentry;
2809 + struct inode *inode, *lower_inode;
2810 + struct super_block *sb;
2811 + struct vfsmount *lower_mnt;
2812 + int printed_caller = 0;
2816 + sb = dentry->d_sb;
2817 + inode = dentry->d_inode;
2818 + dstart = dbstart(dentry);
2819 + dend = dbend(dentry);
2820 + /* don't check dentry/mnt if no lower branches */
2821 + if (dstart < 0 && dend < 0)
2823 + BUG_ON(dstart > dend);
2825 + if (unlikely((dstart == -1 && dend != -1) ||
2826 + (dstart != -1 && dend == -1))) {
2827 + PRINT_CALLER(fname, fxn, line);
2828 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2829 + dentry, dstart, dend);
2832 + * check for NULL dentries inside the start/end range, or
2833 + * non-NULL dentries outside the start/end range.
2835 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2836 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2837 + if (lower_dentry) {
2838 + if (unlikely(bindex < dstart || bindex > dend)) {
2839 + PRINT_CALLER(fname, fxn, line);
2840 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2841 + "bindex=%d dstart/end=%d:%d\n",
2842 + dentry, lower_dentry,
2843 + (lower_dentry ? lower_dentry->d_inode :
2845 + bindex, dstart, dend);
2847 + } else { /* lower_dentry == NULL */
2848 + if (bindex < dstart || bindex > dend)
2851 + * Directories can have NULL lower inodes in b/t
2852 + * start/end, but NOT if at the start/end range.
2853 + * Ignore this rule, however, if this is a NULL
2854 + * dentry or a deleted dentry.
2856 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2858 + !(inode && S_ISDIR(inode->i_mode) &&
2859 + bindex > dstart && bindex < dend))) {
2860 + PRINT_CALLER(fname, fxn, line);
2861 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2862 + "bindex=%d dstart/end=%d:%d\n",
2863 + dentry, lower_dentry,
2865 + lower_dentry->d_inode :
2867 + bindex, dstart, dend);
2872 + /* check for vfsmounts same as for dentries */
2873 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2874 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2876 + if (unlikely(bindex < dstart || bindex > dend)) {
2877 + PRINT_CALLER(fname, fxn, line);
2878 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2879 + "dstart/end=%d:%d\n", dentry,
2880 + lower_mnt, bindex, dstart, dend);
2882 + } else { /* lower_mnt == NULL */
2883 + if (bindex < dstart || bindex > dend)
2886 + * Directories can have NULL lower inodes in b/t
2887 + * start/end, but NOT if at the start/end range.
2888 + * Ignore this rule, however, if this is a NULL
2891 + if (unlikely(inode &&
2892 + !(inode && S_ISDIR(inode->i_mode) &&
2893 + bindex > dstart && bindex < dend))) {
2894 + PRINT_CALLER(fname, fxn, line);
2895 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2896 + "bindex=%d dstart/end=%d:%d\n",
2897 + dentry, lower_mnt, bindex,
2904 + /* for inodes now */
2907 + istart = ibstart(inode);
2908 + iend = ibend(inode);
2909 + /* don't check inode if no lower branches */
2910 + if (istart < 0 && iend < 0)
2912 + BUG_ON(istart > iend);
2913 + if (unlikely((istart == -1 && iend != -1) ||
2914 + (istart != -1 && iend == -1))) {
2915 + PRINT_CALLER(fname, fxn, line);
2916 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2917 + dentry, inode, istart, iend);
2919 + if (unlikely(istart != dstart)) {
2920 + PRINT_CALLER(fname, fxn, line);
2921 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2922 + dentry, inode, istart, dstart);
2924 + if (unlikely(iend != dend)) {
2925 + PRINT_CALLER(fname, fxn, line);
2926 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2927 + dentry, inode, iend, dend);
2930 + if (!S_ISDIR(inode->i_mode)) {
2931 + if (unlikely(dend != dstart)) {
2932 + PRINT_CALLER(fname, fxn, line);
2933 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2934 + dentry, inode, dstart, dend);
2936 + if (unlikely(iend != istart)) {
2937 + PRINT_CALLER(fname, fxn, line);
2938 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2939 + dentry, inode, istart, iend);
2943 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2944 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2945 + if (lower_inode) {
2946 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2947 + if (unlikely(bindex < istart || bindex > iend)) {
2948 + PRINT_CALLER(fname, fxn, line);
2949 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2950 + "istart/end=%d:%d\n", dentry,
2951 + lower_inode, bindex, istart, iend);
2952 + } else if (unlikely(lower_inode == poison_ptr)) {
2953 + /* freed inode! */
2954 + PRINT_CALLER(fname, fxn, line);
2955 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2956 + "istart/end=%d:%d\n", dentry,
2957 + lower_inode, bindex, istart, iend);
2961 + /* if we get here, then lower_inode == NULL */
2962 + if (bindex < istart || bindex > iend)
2965 + * directories can have NULL lower inodes in b/t start/end,
2966 + * but NOT if at the start/end range.
2968 + if (unlikely(S_ISDIR(inode->i_mode) &&
2969 + bindex > istart && bindex < iend))
2971 + PRINT_CALLER(fname, fxn, line);
2972 + pr_debug(" CI7: dentry/linode=%p:%p "
2973 + "bindex=%d istart/end=%d:%d\n",
2974 + dentry, lower_inode, bindex, istart, iend);
2978 + * If it's a directory, then intermediate objects b/t start/end can
2979 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2982 + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2983 + for (bindex = dstart+1; bindex < dend; bindex++) {
2984 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2985 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2987 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2988 + if (unlikely(!((lower_inode && lower_dentry &&
2991 + !lower_dentry && !lower_mnt)))) {
2992 + PRINT_CALLER(fname, fxn, line);
2993 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2994 + "bindex=%d dstart/end=%d:%d\n",
2995 + lower_mnt, lower_dentry, lower_inode,
2996 + bindex, dstart, dend);
2999 + /* check if lower inode is newer than upper one (it shouldn't) */
3000 + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
3001 + PRINT_CALLER(fname, fxn, line);
3002 + for (bindex = ibstart(inode); bindex <= ibend(inode);
3004 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3005 + if (unlikely(!lower_inode))
3007 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
3008 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
3010 + inode->i_mtime.tv_sec,
3011 + inode->i_mtime.tv_nsec,
3012 + lower_inode->i_mtime.tv_sec,
3013 + lower_inode->i_mtime.tv_nsec,
3014 + inode->i_ctime.tv_sec,
3015 + inode->i_ctime.tv_nsec,
3016 + lower_inode->i_ctime.tv_sec,
3017 + lower_inode->i_ctime.tv_nsec);
3022 +void __unionfs_check_file(const struct file *file,
3023 + const char *fname, const char *fxn, int line)
3026 + int dstart, dend, fstart, fend;
3027 + struct dentry *dentry;
3028 + struct file *lower_file;
3029 + struct inode *inode;
3030 + struct super_block *sb;
3031 + int printed_caller = 0;
3034 + dentry = file->f_path.dentry;
3035 + sb = dentry->d_sb;
3036 + dstart = dbstart(dentry);
3037 + dend = dbend(dentry);
3038 + BUG_ON(dstart > dend);
3039 + fstart = fbstart(file);
3040 + fend = fbend(file);
3041 + BUG_ON(fstart > fend);
3043 + if (unlikely((fstart == -1 && fend != -1) ||
3044 + (fstart != -1 && fend == -1))) {
3045 + PRINT_CALLER(fname, fxn, line);
3046 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
3047 + file, dentry, fstart, fend);
3049 + if (unlikely(fstart != dstart)) {
3050 + PRINT_CALLER(fname, fxn, line);
3051 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
3052 + file, dentry, fstart, dstart);
3054 + if (unlikely(fend != dend)) {
3055 + PRINT_CALLER(fname, fxn, line);
3056 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
3057 + file, dentry, fend, dend);
3059 + inode = dentry->d_inode;
3060 + if (!S_ISDIR(inode->i_mode)) {
3061 + if (unlikely(fend != fstart)) {
3062 + PRINT_CALLER(fname, fxn, line);
3063 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
3064 + file, inode, fstart, fend);
3066 + if (unlikely(dend != dstart)) {
3067 + PRINT_CALLER(fname, fxn, line);
3068 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
3069 + file, dentry, dstart, dend);
3074 + * check for NULL dentries inside the start/end range, or
3075 + * non-NULL dentries outside the start/end range.
3077 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
3078 + lower_file = unionfs_lower_file_idx(file, bindex);
3080 + if (unlikely(bindex < fstart || bindex > fend)) {
3081 + PRINT_CALLER(fname, fxn, line);
3082 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
3083 + "fstart/end=%d:%d\n", file,
3084 + lower_file, bindex, fstart, fend);
3086 + } else { /* lower_file == NULL */
3087 + if (bindex >= fstart && bindex <= fend) {
3089 + * directories can have NULL lower inodes in
3090 + * b/t start/end, but NOT if at the
3091 + * start/end range.
3093 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3094 + bindex > fstart &&
3095 + bindex < fend))) {
3096 + PRINT_CALLER(fname, fxn, line);
3097 + pr_debug(" CF6: file/lower=%p:%p "
3098 + "bindex=%d fstart/end=%d:%d\n",
3099 + file, lower_file, bindex,
3106 + __unionfs_check_dentry(dentry, fname, fxn, line);
3109 +void __unionfs_check_nd(const struct nameidata *nd,
3110 + const char *fname, const char *fxn, int line)
3112 + struct file *file;
3113 + int printed_caller = 0;
3115 + if (unlikely(!nd))
3117 + if (nd->flags & LOOKUP_OPEN) {
3118 + file = nd->intent.open.file;
3119 + if (unlikely(file->f_path.dentry &&
3120 + strcmp(file->f_path.dentry->d_sb->s_type->name,
3122 + PRINT_CALLER(fname, fxn, line);
3123 + pr_debug(" CND1: lower_file of type %s\n",
3124 + file->f_path.dentry->d_sb->s_type->name);
3130 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3131 +void __show_branch_counts(const struct super_block *sb,
3132 + const char *file, const char *fxn, int line)
3135 + struct vfsmount *mnt;
3138 + for (i = 0; i < sbmax(sb); i++) {
3139 + if (likely(sb->s_root))
3140 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3143 + printk(KERN_CONT "%d:",
3144 + (mnt ? atomic_read(&mnt->mnt_count) : -99));
3146 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3149 +void __show_inode_times(const struct inode *inode,
3150 + const char *file, const char *fxn, int line)
3152 + struct inode *lower_inode;
3155 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3156 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3157 + if (unlikely(!lower_inode))
3159 + pr_debug("IT(%lu:%d): %s:%s:%d "
3160 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3161 + inode->i_ino, bindex,
3163 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3164 + lower_inode->i_mtime.tv_sec,
3165 + lower_inode->i_mtime.tv_nsec,
3166 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3167 + lower_inode->i_ctime.tv_sec,
3168 + lower_inode->i_ctime.tv_nsec);
3172 +void __show_dinode_times(const struct dentry *dentry,
3173 + const char *file, const char *fxn, int line)
3175 + struct inode *inode = dentry->d_inode;
3176 + struct inode *lower_inode;
3179 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3180 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3183 + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3184 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3185 + dentry->d_name.name, inode->i_ino, bindex,
3187 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3188 + lower_inode->i_mtime.tv_sec,
3189 + lower_inode->i_mtime.tv_nsec,
3190 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3191 + lower_inode->i_ctime.tv_sec,
3192 + lower_inode->i_ctime.tv_nsec);
3196 +void __show_inode_counts(const struct inode *inode,
3197 + const char *file, const char *fxn, int line)
3199 + struct inode *lower_inode;
3202 + if (unlikely(!inode)) {
3203 + pr_debug("SiC: Null inode\n");
3206 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3208 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3209 + if (unlikely(!lower_inode))
3211 + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3212 + inode->i_ino, bindex,
3213 + atomic_read(&(inode)->i_count),
3214 + atomic_read(&(lower_inode)->i_count),
3218 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
3219 new file mode 100644
3220 index 0000000..583f4a4
3222 +++ b/fs/unionfs/dentry.c
3225 + * Copyright (c) 2003-2008 Erez Zadok
3226 + * Copyright (c) 2003-2006 Charles P. Wright
3227 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3228 + * Copyright (c) 2005-2006 Junjiro Okajima
3229 + * Copyright (c) 2005 Arun M. Krishnakumar
3230 + * Copyright (c) 2004-2006 David P. Quigley
3231 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3232 + * Copyright (c) 2003 Puja Gupta
3233 + * Copyright (c) 2003 Harikesavan Krishnan
3234 + * Copyright (c) 2003-2008 Stony Brook University
3235 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
3237 + * This program is free software; you can redistribute it and/or modify
3238 + * it under the terms of the GNU General Public License version 2 as
3239 + * published by the Free Software Foundation.
3244 +bool is_negative_lower(const struct dentry *dentry)
3247 + struct dentry *lower_dentry;
3250 + /* cache coherency: check if file was deleted on lower branch */
3251 + if (dbstart(dentry) < 0)
3253 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3254 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3255 + /* unhashed (i.e., unlinked) lower dentries don't count */
3256 + if (lower_dentry && lower_dentry->d_inode &&
3257 + !d_deleted(lower_dentry) &&
3258 + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3264 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3266 + struct dentry *lower_dentry;
3271 + for (bindex = start; bindex <= end; bindex++) {
3272 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3273 + if (!lower_dentry)
3275 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3276 + dput(lower_dentry);
3281 + * Purge and invalidate as many data pages of a unionfs inode. This is
3282 + * called when the lower inode has changed, and we want to force processes
3283 + * to re-get the new data.
3285 +static inline void purge_inode_data(struct inode *inode)
3287 + /* remove all non-private mappings */
3288 + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3289 + /* invalidate as many pages as possible */
3290 + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3292 + * Don't try to truncate_inode_pages here, because this could lead
3293 + * to a deadlock between some of address_space ops and dentry
3294 + * revalidation: the address space op is invoked with a lock on our
3295 + * own page, and truncate_inode_pages will block on locked pages.
3300 + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3301 + * of the @dentry and its @parent are locked. Assume parent is valid,
3302 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3303 + * dentry). Returns true if valid, false otherwise.
3305 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3308 + bool valid = true; /* default is valid */
3309 + struct dentry *lower_dentry;
3310 + struct dentry *result;
3311 + int bindex, bstart, bend;
3312 + int sbgen, dgen, pdgen;
3314 + int interpose_flag;
3316 + verify_locked(dentry);
3317 + verify_locked(parent);
3319 + /* if the dentry is unhashed, do NOT revalidate */
3320 + if (d_deleted(dentry))
3323 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3325 + if (is_newer_lower(dentry)) {
3326 + /* root dentry is always valid */
3327 + if (IS_ROOT(dentry)) {
3328 + unionfs_copy_attr_times(dentry->d_inode);
3331 + * reset generation number to zero, guaranteed to be
3335 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3338 + purge_inode_data(dentry->d_inode);
3341 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3343 + BUG_ON(dbstart(dentry) == -1);
3344 + if (dentry->d_inode)
3347 + /* if our dentry is valid, then validate all lower ones */
3348 + if (sbgen == dgen)
3349 + goto validate_lowers;
3351 + /* The root entry should always be valid */
3352 + BUG_ON(IS_ROOT(dentry));
3354 + /* We can't work correctly if our parent isn't valid. */
3355 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3357 + /* Free the pointers for our inodes and this dentry. */
3358 + path_put_lowers_all(dentry, false);
3360 + interpose_flag = INTERPOSE_REVAL_NEG;
3362 + interpose_flag = INTERPOSE_REVAL;
3363 + iput_lowers_all(dentry->d_inode, true);
3366 + if (realloc_dentry_private_data(dentry) != 0) {
3371 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3373 + if (IS_ERR(result)) {
3378 + * current unionfs_lookup_backend() doesn't return
3385 + if (unlikely(positive && is_negative_lower(dentry))) {
3386 + /* call make_bad_inode here ? */
3393 + * if we got here then we have revalidated our dentry and all lower
3394 + * ones, so we can return safely.
3396 + if (!valid) /* lower dentry revalidation failed */
3400 + * If the parent's gen no. matches the superblock's gen no., then
3401 + * we can update our denty's gen no. If they didn't match, then it
3402 + * was OK to revalidate this dentry with a stale parent, but we'll
3403 + * purposely not update our dentry's gen no. (so it can be redone);
3404 + * and, we'll mark our parent dentry as invalid so it'll force it
3405 + * (and our dentry) to be revalidated.
3407 + if (pdgen == sbgen)
3408 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3413 + /* The revalidation must occur across all branches */
3414 + bstart = dbstart(dentry);
3415 + bend = dbend(dentry);
3416 + BUG_ON(bstart == -1);
3417 + for (bindex = bstart; bindex <= bend; bindex++) {
3418 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3419 + if (!lower_dentry || !lower_dentry->d_op
3420 + || !lower_dentry->d_op->d_revalidate)
3423 + * Don't pass nameidata to lower file system, because we
3424 + * don't want an arbitrary lower file being opened or
3425 + * returned to us: it may be useless to us because of the
3426 + * fanout nature of unionfs (cf. file/directory open-file
3427 + * invariants). We will open lower files as and when needed
3430 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3434 + if (!dentry->d_inode ||
3435 + ibstart(dentry->d_inode) < 0 ||
3436 + ibend(dentry->d_inode) < 0) {
3443 + * If we get here, and we copy the meta-data from the lower
3444 + * inode to our inode, then it is vital that we have already
3445 + * purged all unionfs-level file data. We do that in the
3446 + * caller (__unionfs_d_revalidate) by calling
3447 + * purge_inode_data.
3449 + unionfs_copy_attr_all(dentry->d_inode,
3450 + unionfs_lower_inode(dentry->d_inode));
3451 + fsstack_copy_inode_size(dentry->d_inode,
3452 + unionfs_lower_inode(dentry->d_inode));
3460 + * Determine if the lower inode objects have changed from below the unionfs
3461 + * inode. Return true if changed, false otherwise.
3463 + * We check if the mtime or ctime have changed. However, the inode times
3464 + * can be changed by anyone without much protection, including
3465 + * asynchronously. This can sometimes cause unionfs to find that the lower
3466 + * file system doesn't change its inode times quick enough, resulting in a
3467 + * false positive indication (which is harmless, it just makes unionfs do
3468 + * extra work in re-validating the objects). To minimize the chances of
3469 + * these situations, we still consider such small time changes valid, but we
3470 + * don't print debugging messages unless the time changes are greater than
3471 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3472 + * because significant changes are more likely due to users manually
3473 + * touching lower files.
3475 +bool is_newer_lower(const struct dentry *dentry)
3478 + struct inode *inode;
3479 + struct inode *lower_inode;
3481 + /* ignore if we're called on semi-initialized dentries/inodes */
3482 + if (!dentry || !UNIONFS_D(dentry))
3484 + inode = dentry->d_inode;
3485 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3486 + ibstart(inode) < 0 || ibend(inode) < 0)
3489 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3490 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3494 + /* check if mtime/ctime have changed */
3495 + if (unlikely(timespec_compare(&inode->i_mtime,
3496 + &lower_inode->i_mtime) < 0)) {
3497 + if ((lower_inode->i_mtime.tv_sec -
3498 + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3499 + pr_info("unionfs: new lower inode mtime "
3500 + "(bindex=%d, name=%s)\n", bindex,
3501 + dentry->d_name.name);
3502 + show_dinode_times(dentry);
3506 + if (unlikely(timespec_compare(&inode->i_ctime,
3507 + &lower_inode->i_ctime) < 0)) {
3508 + if ((lower_inode->i_ctime.tv_sec -
3509 + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3510 + pr_info("unionfs: new lower inode ctime "
3511 + "(bindex=%d, name=%s)\n", bindex,
3512 + dentry->d_name.name);
3513 + show_dinode_times(dentry);
3520 + * Last check: if this is a positive dentry, but somehow all lower
3521 + * dentries are negative or unhashed, then this dentry needs to be
3522 + * revalidated, because someone probably deleted the objects from
3523 + * the lower branches directly.
3525 + if (is_negative_lower(dentry))
3528 + return false; /* default: lower is not newer */
3531 +static int unionfs_d_revalidate(struct dentry *dentry,
3532 + struct nameidata *nd_unused)
3534 + bool valid = true;
3535 + int err = 1; /* 1 means valid for the VFS */
3536 + struct dentry *parent;
3538 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3539 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3540 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3542 + valid = __unionfs_d_revalidate(dentry, parent, false);
3544 + unionfs_postcopyup_setmnt(dentry);
3545 + unionfs_check_dentry(dentry);
3550 + unionfs_unlock_dentry(dentry);
3551 + unionfs_unlock_parent(dentry, parent);
3552 + unionfs_read_unlock(dentry->d_sb);
3557 +static void unionfs_d_release(struct dentry *dentry)
3559 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3560 + if (unlikely(!UNIONFS_D(dentry)))
3561 + goto out; /* skip if no lower branches */
3562 + /* must lock our branch configuration here */
3563 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3565 + unionfs_check_dentry(dentry);
3566 + /* this could be a negative dentry, so check first */
3567 + if (dbstart(dentry) < 0) {
3568 + unionfs_unlock_dentry(dentry);
3569 + goto out; /* due to a (normal) failed lookup */
3572 + /* Release all the lower dentries */
3573 + path_put_lowers_all(dentry, true);
3575 + unionfs_unlock_dentry(dentry);
3578 + free_dentry_private_data(dentry);
3579 + unionfs_read_unlock(dentry->d_sb);
3584 + * Called when we're removing the last reference to our dentry. So we
3585 + * should drop all lower references too.
3587 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3592 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3593 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3595 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3596 + goto drop_lower_inodes;
3597 + path_put_lowers_all(dentry, false);
3600 + rc = atomic_read(&inode->i_count);
3601 + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3602 + /* see Documentation/filesystems/unionfs/issues.txt */
3604 + iput(unionfs_lower_inode(inode));
3606 + unionfs_set_lower_inode(inode, NULL);
3607 + /* XXX: may need to set start/end to -1? */
3612 + unionfs_unlock_dentry(dentry);
3613 + unionfs_read_unlock(dentry->d_sb);
3616 +struct dentry_operations unionfs_dops = {
3617 + .d_revalidate = unionfs_d_revalidate,
3618 + .d_release = unionfs_d_release,
3619 + .d_iput = unionfs_d_iput,
3621 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3622 new file mode 100644
3623 index 0000000..63fb419
3625 +++ b/fs/unionfs/dirfops.c
3628 + * Copyright (c) 2003-2008 Erez Zadok
3629 + * Copyright (c) 2003-2006 Charles P. Wright
3630 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3631 + * Copyright (c) 2005-2006 Junjiro Okajima
3632 + * Copyright (c) 2005 Arun M. Krishnakumar
3633 + * Copyright (c) 2004-2006 David P. Quigley
3634 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3635 + * Copyright (c) 2003 Puja Gupta
3636 + * Copyright (c) 2003 Harikesavan Krishnan
3637 + * Copyright (c) 2003-2008 Stony Brook University
3638 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
3640 + * This program is free software; you can redistribute it and/or modify
3641 + * it under the terms of the GNU General Public License version 2 as
3642 + * published by the Free Software Foundation.
3647 +/* Make sure our rdstate is playing by the rules. */
3648 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3650 + BUG_ON(rdstate->offset >= DIREOF);
3651 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3654 +struct unionfs_getdents_callback {
3655 + struct unionfs_dir_state *rdstate;
3657 + int entries_written;
3658 + int filldir_called;
3659 + int filldir_error;
3660 + filldir_t filldir;
3661 + struct super_block *sb;
3664 +/* based on generic filldir in fs/readir.c */
3665 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3666 + loff_t offset, u64 ino, unsigned int d_type)
3668 + struct unionfs_getdents_callback *buf = dirent;
3669 + struct filldir_node *found = NULL;
3672 + char *name = (char *) oname;
3674 + buf->filldir_called++;
3676 + is_whiteout = is_whiteout_name(&name, &namelen);
3678 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3682 + * If we had non-whiteout entry in dir cache, then mark it
3683 + * as a whiteout and but leave it in the dir cache.
3685 + if (is_whiteout && !found->whiteout)
3686 + found->whiteout = is_whiteout;
3690 + /* if 'name' isn't a whiteout, filldir it. */
3691 + if (!is_whiteout) {
3692 + off_t pos = rdstate2offset(buf->rdstate);
3693 + u64 unionfs_ino = ino;
3695 + err = buf->filldir(buf->dirent, name, namelen, pos,
3696 + unionfs_ino, d_type);
3697 + buf->rdstate->offset++;
3698 + verify_rdstate_offset(buf->rdstate);
3701 + * If we did fill it, stuff it in our hash, otherwise return an
3705 + buf->filldir_error = err;
3708 + buf->entries_written++;
3709 + err = add_filldir_node(buf->rdstate, name, namelen,
3710 + buf->rdstate->bindex, is_whiteout);
3712 + buf->filldir_error = err;
3718 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3721 + struct file *lower_file = NULL;
3722 + struct dentry *dentry = file->f_path.dentry;
3723 + struct dentry *parent;
3724 + struct inode *inode = NULL;
3725 + struct unionfs_getdents_callback buf;
3726 + struct unionfs_dir_state *uds;
3730 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3731 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3732 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3734 + err = unionfs_file_revalidate(file, parent, false);
3735 + if (unlikely(err))
3738 + inode = dentry->d_inode;
3740 + uds = UNIONFS_F(file)->rdstate;
3742 + if (file->f_pos == DIREOF) {
3744 + } else if (file->f_pos > 0) {
3745 + uds = find_rdstate(inode, file->f_pos);
3746 + if (unlikely(!uds)) {
3750 + UNIONFS_F(file)->rdstate = uds;
3752 + init_rdstate(file);
3753 + uds = UNIONFS_F(file)->rdstate;
3756 + bend = fbend(file);
3758 + while (uds->bindex <= bend) {
3759 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3760 + if (!lower_file) {
3766 + /* prepare callback buffer */
3767 + buf.filldir_called = 0;
3768 + buf.filldir_error = 0;
3769 + buf.entries_written = 0;
3770 + buf.dirent = dirent;
3771 + buf.filldir = filldir;
3772 + buf.rdstate = uds;
3773 + buf.sb = inode->i_sb;
3775 + /* Read starting from where we last left off. */
3776 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3781 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3783 + /* Save the position for when we continue. */
3784 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3789 + uds->dirpos = offset;
3791 + /* Copy the atime. */
3792 + fsstack_copy_attr_atime(inode,
3793 + lower_file->f_path.dentry->d_inode);
3798 + if (buf.filldir_error)
3801 + if (!buf.entries_written) {
3807 + if (!buf.filldir_error && uds->bindex >= bend) {
3808 + /* Save the number of hash entries for next time. */
3809 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3810 + free_rdstate(uds);
3811 + UNIONFS_F(file)->rdstate = NULL;
3812 + file->f_pos = DIREOF;
3814 + file->f_pos = rdstate2offset(uds);
3819 + unionfs_check_file(file);
3820 + unionfs_unlock_dentry(dentry);
3821 + unionfs_unlock_parent(dentry, parent);
3822 + unionfs_read_unlock(dentry->d_sb);
3827 + * This is not meant to be a generic repositioning function. If you do
3828 + * things that aren't supported, then we return EINVAL.
3830 + * What is allowed:
3831 + * (1) seeking to the same position that you are currently at
3832 + * This really has no effect, but returns where you are.
3833 + * (2) seeking to the beginning of the file
3834 + * This throws out all state, and lets you begin again.
3836 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3838 + struct unionfs_dir_state *rdstate;
3839 + struct dentry *dentry = file->f_path.dentry;
3840 + struct dentry *parent;
3843 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3844 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3845 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3847 + err = unionfs_file_revalidate(file, parent, false);
3848 + if (unlikely(err))
3851 + rdstate = UNIONFS_F(file)->rdstate;
3854 + * we let users seek to their current position, but not anywhere
3861 + free_rdstate(rdstate);
3862 + UNIONFS_F(file)->rdstate = NULL;
3864 + init_rdstate(file);
3868 + err = file->f_pos;
3871 + /* Unsupported, because we would break everything. */
3879 + if (offset == rdstate2offset(rdstate))
3881 + else if (file->f_pos == DIREOF)
3886 + struct inode *inode;
3887 + inode = dentry->d_inode;
3888 + rdstate = find_rdstate(inode, offset);
3890 + UNIONFS_F(file)->rdstate = rdstate;
3891 + err = rdstate->offset;
3899 + /* Unsupported, because we would break everything. */
3907 + unionfs_check_file(file);
3908 + unionfs_unlock_dentry(dentry);
3909 + unionfs_unlock_parent(dentry, parent);
3910 + unionfs_read_unlock(dentry->d_sb);
3915 + * Trimmed directory options, we shouldn't pass everything down since
3916 + * we don't want to operate on partial directories.
3918 +struct file_operations unionfs_dir_fops = {
3919 + .llseek = unionfs_dir_llseek,
3920 + .read = generic_read_dir,
3921 + .readdir = unionfs_readdir,
3922 + .unlocked_ioctl = unionfs_ioctl,
3923 + .open = unionfs_open,
3924 + .release = unionfs_file_release,
3925 + .flush = unionfs_flush,
3926 + .fsync = unionfs_fsync,
3927 + .fasync = unionfs_fasync,
3929 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3930 new file mode 100644
3931 index 0000000..aa31e91
3933 +++ b/fs/unionfs/dirhelper.c
3936 + * Copyright (c) 2003-2008 Erez Zadok
3937 + * Copyright (c) 2003-2006 Charles P. Wright
3938 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3939 + * Copyright (c) 2005-2006 Junjiro Okajima
3940 + * Copyright (c) 2005 Arun M. Krishnakumar
3941 + * Copyright (c) 2004-2006 David P. Quigley
3942 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3943 + * Copyright (c) 2003 Puja Gupta
3944 + * Copyright (c) 2003 Harikesavan Krishnan
3945 + * Copyright (c) 2003-2008 Stony Brook University
3946 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
3948 + * This program is free software; you can redistribute it and/or modify
3949 + * it under the terms of the GNU General Public License version 2 as
3950 + * published by the Free Software Foundation.
3956 +#define RD_CHECK_EMPTY 1
3957 +/* The callback structure for check_empty. */
3958 +struct unionfs_rdutil_callback {
3960 + int filldir_called;
3961 + struct unionfs_dir_state *rdstate;
3965 +/* This filldir function makes sure only whiteouts exist within a directory. */
3966 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3967 + loff_t offset, u64 ino, unsigned int d_type)
3970 + struct unionfs_rdutil_callback *buf = dirent;
3972 + struct filldir_node *found;
3973 + char *name = (char *) oname;
3975 + buf->filldir_called = 1;
3977 + if (name[0] == '.' && (namelen == 1 ||
3978 + (name[1] == '.' && namelen == 2)))
3981 + is_whiteout = is_whiteout_name(&name, &namelen);
3983 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3984 + /* If it was found in the table there was a previous whiteout. */
3989 + * if it wasn't found and isn't a whiteout, the directory isn't
3993 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3996 + err = add_filldir_node(buf->rdstate, name, namelen,
3997 + buf->rdstate->bindex, is_whiteout);
4004 +/* Is a directory logically empty? */
4005 +int check_empty(struct dentry *dentry, struct dentry *parent,
4006 + struct unionfs_dir_state **namelist)
4009 + struct dentry *lower_dentry = NULL;
4010 + struct vfsmount *mnt;
4011 + struct super_block *sb;
4012 + struct file *lower_file;
4013 + struct unionfs_rdutil_callback *buf = NULL;
4014 + int bindex, bstart, bend, bopaque;
4016 + sb = dentry->d_sb;
4019 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
4021 + err = unionfs_partial_lookup(dentry, parent);
4025 + bstart = dbstart(dentry);
4026 + bend = dbend(dentry);
4027 + bopaque = dbopaque(dentry);
4028 + if (0 <= bopaque && bopaque < bend)
4031 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
4032 + if (unlikely(!buf)) {
4037 + buf->mode = RD_CHECK_EMPTY;
4038 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
4039 + if (unlikely(!buf->rdstate)) {
4044 + /* Process the lower directories with rdutil_callback as a filldir. */
4045 + for (bindex = bstart; bindex <= bend; bindex++) {
4046 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4047 + if (!lower_dentry)
4049 + if (!lower_dentry->d_inode)
4051 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
4054 + dget(lower_dentry);
4055 + mnt = unionfs_mntget(dentry, bindex);
4056 + branchget(sb, bindex);
4057 + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY);
4058 + if (IS_ERR(lower_file)) {
4059 + err = PTR_ERR(lower_file);
4060 + branchput(sb, bindex);
4065 + buf->filldir_called = 0;
4066 + buf->rdstate->bindex = bindex;
4067 + err = vfs_readdir(lower_file,
4068 + readdir_util_callback, buf);
4071 + } while ((err >= 0) && buf->filldir_called);
4073 + /* fput calls dput for lower_dentry */
4075 + branchput(sb, bindex);
4083 + if (namelist && !err)
4084 + *namelist = buf->rdstate;
4085 + else if (buf->rdstate)
4086 + free_rdstate(buf->rdstate);
4093 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4094 new file mode 100644
4095 index 0000000..69c0921
4097 +++ b/fs/unionfs/fanout.h
4100 + * Copyright (c) 2003-2008 Erez Zadok
4101 + * Copyright (c) 2003-2006 Charles P. Wright
4102 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4103 + * Copyright (c) 2005 Arun M. Krishnakumar
4104 + * Copyright (c) 2004-2006 David P. Quigley
4105 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4106 + * Copyright (c) 2003 Puja Gupta
4107 + * Copyright (c) 2003 Harikesavan Krishnan
4108 + * Copyright (c) 2003-2008 Stony Brook University
4109 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
4111 + * This program is free software; you can redistribute it and/or modify
4112 + * it under the terms of the GNU General Public License version 2 as
4113 + * published by the Free Software Foundation.
4120 + * Inode to private data
4122 + * Since we use containers and the struct inode is _inside_ the
4123 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4124 + * inode pointer), return a valid non-NULL pointer.
4126 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4128 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4131 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4132 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4134 +/* Dentry to private data */
4135 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4136 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4137 +#define dbend(dent) (UNIONFS_D(dent)->bend)
4138 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4140 +/* Superblock to private data */
4141 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4142 +#define sbstart(sb) 0
4143 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4144 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4145 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4147 +/* File to private Data */
4148 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4149 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4150 +#define fbend(file) (UNIONFS_F(file)->bend)
4152 +/* macros to manipulate branch IDs in stored in our superblock */
4153 +static inline int branch_id(struct super_block *sb, int index)
4155 + BUG_ON(!sb || index < 0);
4156 + return UNIONFS_SB(sb)->data[index].branch_id;
4159 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4161 + BUG_ON(!sb || index < 0);
4162 + UNIONFS_SB(sb)->data[index].branch_id = val;
4165 +static inline void new_branch_id(struct super_block *sb, int index)
4167 + BUG_ON(!sb || index < 0);
4168 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4172 + * Find new index of matching branch with an existing superblock of a known
4173 + * (possibly old) id. This is needed because branches could have been
4174 + * added/deleted causing the branches of any open files to shift.
4176 + * @sb: the new superblock which may have new/different branch IDs
4177 + * @id: the old/existing id we're looking for
4178 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4180 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4183 + for (i = 0; i < sbmax(sb); i++) {
4184 + if (branch_id(sb, i) == id)
4187 + /* in the non-ODF code, this should really never happen */
4188 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4192 +/* File to lower file. */
4193 +static inline struct file *unionfs_lower_file(const struct file *f)
4196 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4199 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4202 + BUG_ON(!f || index < 0);
4203 + return UNIONFS_F(f)->lower_files[index];
4206 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4209 + BUG_ON(!f || index < 0);
4210 + UNIONFS_F(f)->lower_files[index] = val;
4211 + /* save branch ID (may be redundant?) */
4212 + UNIONFS_F(f)->saved_branch_ids[index] =
4213 + branch_id((f)->f_path.dentry->d_sb, index);
4216 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4219 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4222 +/* Inode to lower inode. */
4223 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4226 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4229 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4232 + BUG_ON(!i || index < 0);
4233 + return UNIONFS_I(i)->lower_inodes[index];
4236 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4237 + struct inode *val)
4239 + BUG_ON(!i || index < 0);
4240 + UNIONFS_I(i)->lower_inodes[index] = val;
4243 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4246 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4249 +/* Superblock to lower superblock. */
4250 +static inline struct super_block *unionfs_lower_super(
4251 + const struct super_block *sb)
4254 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4257 +static inline struct super_block *unionfs_lower_super_idx(
4258 + const struct super_block *sb,
4261 + BUG_ON(!sb || index < 0);
4262 + return UNIONFS_SB(sb)->data[index].sb;
4265 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4267 + struct super_block *val)
4269 + BUG_ON(!sb || index < 0);
4270 + UNIONFS_SB(sb)->data[index].sb = val;
4273 +static inline void unionfs_set_lower_super(struct super_block *sb,
4274 + struct super_block *val)
4277 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4280 +/* Branch count macros. */
4281 +static inline int branch_count(const struct super_block *sb, int index)
4283 + BUG_ON(!sb || index < 0);
4284 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4287 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4289 + BUG_ON(!sb || index < 0);
4290 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4293 +static inline void branchget(struct super_block *sb, int index)
4295 + BUG_ON(!sb || index < 0);
4296 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4299 +static inline void branchput(struct super_block *sb, int index)
4301 + BUG_ON(!sb || index < 0);
4302 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4305 +/* Dentry macros */
4306 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4307 + struct dentry *val)
4309 + BUG_ON(!dent || index < 0);
4310 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4313 +static inline struct dentry *unionfs_lower_dentry_idx(
4314 + const struct dentry *dent,
4317 + BUG_ON(!dent || index < 0);
4318 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4321 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4324 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4327 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4328 + struct vfsmount *mnt)
4330 + BUG_ON(!dent || index < 0);
4331 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4334 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4335 + const struct dentry *dent,
4338 + BUG_ON(!dent || index < 0);
4339 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4342 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4345 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4348 +/* Macros for locking a dentry. */
4349 +enum unionfs_dentry_lock_class {
4350 + UNIONFS_DMUTEX_NORMAL,
4351 + UNIONFS_DMUTEX_ROOT,
4352 + UNIONFS_DMUTEX_PARENT,
4353 + UNIONFS_DMUTEX_CHILD,
4354 + UNIONFS_DMUTEX_WHITEOUT,
4355 + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4356 + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4359 +static inline void unionfs_lock_dentry(struct dentry *d,
4360 + unsigned int subclass)
4363 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4366 +static inline void unionfs_unlock_dentry(struct dentry *d)
4369 + mutex_unlock(&UNIONFS_D(d)->lock);
4372 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4373 + unsigned int subclass)
4378 + p = dget_parent(d);
4380 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4384 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4389 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4390 + mutex_unlock(&UNIONFS_D(p)->lock);
4395 +static inline void verify_locked(struct dentry *d)
4398 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4401 +/* macros to put lower objects */
4404 + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4405 + * @free_lower is true, then also kfree the memory used to hold the lower
4406 + * object pointers.
4408 +static inline void iput_lowers(struct inode *inode,
4409 + int bstart, int bend, bool free_lower)
4411 + struct inode *lower_inode;
4415 + BUG_ON(!UNIONFS_I(inode));
4416 + BUG_ON(bstart < 0);
4418 + for (bindex = bstart; bindex <= bend; bindex++) {
4419 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4420 + if (lower_inode) {
4421 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4422 + /* see Documentation/filesystems/unionfs/issues.txt */
4424 + iput(lower_inode);
4430 + kfree(UNIONFS_I(inode)->lower_inodes);
4431 + UNIONFS_I(inode)->lower_inodes = NULL;
4435 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4436 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4441 + BUG_ON(!UNIONFS_I(inode));
4442 + bstart = ibstart(inode);
4443 + bend = ibend(inode);
4444 + BUG_ON(bstart < 0);
4446 + iput_lowers(inode, bstart, bend, free_lower);
4447 + ibstart(inode) = ibend(inode) = -1;
4451 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4452 + * bstart to bend. If @free_lower is true, then also kfree the memory used
4453 + * to hold the lower object pointers.
4455 + * XXX: implement using path_put VFS macros
4457 +static inline void path_put_lowers(struct dentry *dentry,
4458 + int bstart, int bend, bool free_lower)
4460 + struct dentry *lower_dentry;
4461 + struct vfsmount *lower_mnt;
4465 + BUG_ON(!UNIONFS_D(dentry));
4466 + BUG_ON(bstart < 0);
4468 + for (bindex = bstart; bindex <= bend; bindex++) {
4469 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4470 + if (lower_dentry) {
4471 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4472 + dput(lower_dentry);
4474 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4476 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4477 + mntput(lower_mnt);
4482 + kfree(UNIONFS_D(dentry)->lower_paths);
4483 + UNIONFS_D(dentry)->lower_paths = NULL;
4488 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4491 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4496 + BUG_ON(!UNIONFS_D(dentry));
4497 + bstart = dbstart(dentry);
4498 + bend = dbend(dentry);
4499 + BUG_ON(bstart < 0);
4501 + path_put_lowers(dentry, bstart, bend, free_lower);
4502 + dbstart(dentry) = dbend(dentry) = -1;
4505 +#endif /* not _FANOUT_H */
4506 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4507 new file mode 100644
4508 index 0000000..3cc6a76
4510 +++ b/fs/unionfs/file.c
4513 + * Copyright (c) 2003-2008 Erez Zadok
4514 + * Copyright (c) 2003-2006 Charles P. Wright
4515 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4516 + * Copyright (c) 2005-2006 Junjiro Okajima
4517 + * Copyright (c) 2005 Arun M. Krishnakumar
4518 + * Copyright (c) 2004-2006 David P. Quigley
4519 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4520 + * Copyright (c) 2003 Puja Gupta
4521 + * Copyright (c) 2003 Harikesavan Krishnan
4522 + * Copyright (c) 2003-2008 Stony Brook University
4523 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
4525 + * This program is free software; you can redistribute it and/or modify
4526 + * it under the terms of the GNU General Public License version 2 as
4527 + * published by the Free Software Foundation.
4532 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4533 + size_t count, loff_t *ppos)
4536 + struct file *lower_file;
4537 + struct dentry *dentry = file->f_path.dentry;
4538 + struct dentry *parent;
4540 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4541 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4542 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4544 + err = unionfs_file_revalidate(file, parent, false);
4545 + if (unlikely(err))
4548 + lower_file = unionfs_lower_file(file);
4549 + err = vfs_read(lower_file, buf, count, ppos);
4550 + /* update our inode atime upon a successful lower read */
4552 + fsstack_copy_attr_atime(dentry->d_inode,
4553 + lower_file->f_path.dentry->d_inode);
4554 + unionfs_check_file(file);
4558 + unionfs_unlock_dentry(dentry);
4559 + unionfs_unlock_parent(dentry, parent);
4560 + unionfs_read_unlock(dentry->d_sb);
4564 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4565 + size_t count, loff_t *ppos)
4568 + struct file *lower_file;
4569 + struct dentry *dentry = file->f_path.dentry;
4570 + struct dentry *parent;
4572 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4573 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4574 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4576 + err = unionfs_file_revalidate(file, parent, true);
4577 + if (unlikely(err))
4580 + lower_file = unionfs_lower_file(file);
4581 + err = vfs_write(lower_file, buf, count, ppos);
4582 + /* update our inode times+sizes upon a successful lower write */
4584 + fsstack_copy_inode_size(dentry->d_inode,
4585 + lower_file->f_path.dentry->d_inode);
4586 + fsstack_copy_attr_times(dentry->d_inode,
4587 + lower_file->f_path.dentry->d_inode);
4588 + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4589 + unionfs_check_file(file);
4593 + unionfs_unlock_dentry(dentry);
4594 + unionfs_unlock_parent(dentry, parent);
4595 + unionfs_read_unlock(dentry->d_sb);
4599 +static int unionfs_file_readdir(struct file *file, void *dirent,
4600 + filldir_t filldir)
4605 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4609 + struct file *lower_file;
4610 + struct dentry *dentry = file->f_path.dentry;
4611 + struct dentry *parent;
4612 + struct vm_operations_struct *saved_vm_ops = NULL;
4614 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4615 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4616 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4618 + /* This might be deferred to mmap's writepage */
4619 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4620 + err = unionfs_file_revalidate(file, parent, willwrite);
4621 + if (unlikely(err))
4623 + unionfs_check_file(file);
4626 + * File systems which do not implement ->writepage may use
4627 + * generic_file_readonly_mmap as their ->mmap op. If you call
4628 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4629 + * But we cannot call the lower ->mmap op, so we can't tell that
4630 + * writeable mappings won't work. Therefore, our only choice is to
4631 + * check if the lower file system supports the ->writepage, and if
4632 + * not, return EINVAL (the same error that
4633 + * generic_file_readonly_mmap returns in that case).
4635 + lower_file = unionfs_lower_file(file);
4636 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4638 + printk(KERN_ERR "unionfs: branch %d file system does not "
4639 + "support writeable mmap\n", fbstart(file));
4644 + * find and save lower vm_ops.
4646 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4648 + if (!UNIONFS_F(file)->lower_vm_ops) {
4649 + err = lower_file->f_op->mmap(lower_file, vma);
4651 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4654 + saved_vm_ops = vma->vm_ops;
4655 + err = do_munmap(current->mm, vma->vm_start,
4656 + vma->vm_end - vma->vm_start);
4658 + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4663 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4664 + err = generic_file_mmap(file, vma);
4665 + file->f_mapping->a_ops = &unionfs_aops;
4667 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4670 + vma->vm_ops = &unionfs_vm_ops;
4671 + if (!UNIONFS_F(file)->lower_vm_ops)
4672 + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4676 + /* copyup could cause parent dir times to change */
4677 + unionfs_copy_attr_times(parent->d_inode);
4678 + unionfs_check_file(file);
4680 + unionfs_unlock_dentry(dentry);
4681 + unionfs_unlock_parent(dentry, parent);
4682 + unionfs_read_unlock(dentry->d_sb);
4686 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4688 + int bindex, bstart, bend;
4689 + struct file *lower_file;
4690 + struct dentry *lower_dentry;
4691 + struct dentry *parent;
4692 + struct inode *lower_inode, *inode;
4693 + int err = -EINVAL;
4695 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4696 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4697 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4699 + err = unionfs_file_revalidate(file, parent, true);
4700 + if (unlikely(err))
4702 + unionfs_check_file(file);
4704 + bstart = fbstart(file);
4705 + bend = fbend(file);
4706 + if (bstart < 0 || bend < 0)
4709 + inode = dentry->d_inode;
4710 + if (unlikely(!inode)) {
4712 + "unionfs: null lower inode in unionfs_fsync\n");
4715 + for (bindex = bstart; bindex <= bend; bindex++) {
4716 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4717 + if (!lower_inode || !lower_inode->i_fop->fsync)
4719 + lower_file = unionfs_lower_file_idx(file, bindex);
4720 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4721 + mutex_lock(&lower_inode->i_mutex);
4722 + err = lower_inode->i_fop->fsync(lower_file,
4725 + if (!err && bindex == bstart)
4726 + fsstack_copy_attr_times(inode, lower_inode);
4727 + mutex_unlock(&lower_inode->i_mutex);
4734 + unionfs_check_file(file);
4735 + unionfs_unlock_dentry(dentry);
4736 + unionfs_unlock_parent(dentry, parent);
4737 + unionfs_read_unlock(dentry->d_sb);
4741 +int unionfs_fasync(int fd, struct file *file, int flag)
4743 + int bindex, bstart, bend;
4744 + struct file *lower_file;
4745 + struct dentry *dentry = file->f_path.dentry;
4746 + struct dentry *parent;
4747 + struct inode *lower_inode, *inode;
4750 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4751 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4752 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4754 + err = unionfs_file_revalidate(file, parent, true);
4755 + if (unlikely(err))
4757 + unionfs_check_file(file);
4759 + bstart = fbstart(file);
4760 + bend = fbend(file);
4761 + if (bstart < 0 || bend < 0)
4764 + inode = dentry->d_inode;
4765 + if (unlikely(!inode)) {
4767 + "unionfs: null lower inode in unionfs_fasync\n");
4770 + for (bindex = bstart; bindex <= bend; bindex++) {
4771 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4772 + if (!lower_inode || !lower_inode->i_fop->fasync)
4774 + lower_file = unionfs_lower_file_idx(file, bindex);
4775 + mutex_lock(&lower_inode->i_mutex);
4776 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4777 + if (!err && bindex == bstart)
4778 + fsstack_copy_attr_times(inode, lower_inode);
4779 + mutex_unlock(&lower_inode->i_mutex);
4786 + unionfs_check_file(file);
4787 + unionfs_unlock_dentry(dentry);
4788 + unionfs_unlock_parent(dentry, parent);
4789 + unionfs_read_unlock(dentry->d_sb);
4793 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4794 + struct pipe_inode_info *pipe, size_t len,
4795 + unsigned int flags)
4798 + struct file *lower_file;
4799 + struct dentry *dentry = file->f_path.dentry;
4800 + struct dentry *parent;
4802 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4803 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4804 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4806 + err = unionfs_file_revalidate(file, parent, false);
4807 + if (unlikely(err))
4810 + lower_file = unionfs_lower_file(file);
4811 + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4812 + /* update our inode atime upon a successful lower splice-read */
4814 + fsstack_copy_attr_atime(dentry->d_inode,
4815 + lower_file->f_path.dentry->d_inode);
4816 + unionfs_check_file(file);
4820 + unionfs_unlock_dentry(dentry);
4821 + unionfs_unlock_parent(dentry, parent);
4822 + unionfs_read_unlock(dentry->d_sb);
4826 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4827 + struct file *file, loff_t *ppos,
4828 + size_t len, unsigned int flags)
4831 + struct file *lower_file;
4832 + struct dentry *dentry = file->f_path.dentry;
4833 + struct dentry *parent;
4835 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4836 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4837 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4839 + err = unionfs_file_revalidate(file, parent, true);
4840 + if (unlikely(err))
4843 + lower_file = unionfs_lower_file(file);
4844 + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4845 + /* update our inode times+sizes upon a successful lower write */
4847 + fsstack_copy_inode_size(dentry->d_inode,
4848 + lower_file->f_path.dentry->d_inode);
4849 + fsstack_copy_attr_times(dentry->d_inode,
4850 + lower_file->f_path.dentry->d_inode);
4851 + unionfs_check_file(file);
4855 + unionfs_unlock_dentry(dentry);
4856 + unionfs_unlock_parent(dentry, parent);
4857 + unionfs_read_unlock(dentry->d_sb);
4861 +struct file_operations unionfs_main_fops = {
4862 + .llseek = generic_file_llseek,
4863 + .read = unionfs_read,
4864 + .write = unionfs_write,
4865 + .readdir = unionfs_file_readdir,
4866 + .unlocked_ioctl = unionfs_ioctl,
4867 + .mmap = unionfs_mmap,
4868 + .open = unionfs_open,
4869 + .flush = unionfs_flush,
4870 + .release = unionfs_file_release,
4871 + .fsync = unionfs_fsync,
4872 + .fasync = unionfs_fasync,
4873 + .splice_read = unionfs_splice_read,
4874 + .splice_write = unionfs_splice_write,
4876 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4877 new file mode 100644
4878 index 0000000..800648e
4880 +++ b/fs/unionfs/inode.c
4883 + * Copyright (c) 2003-2008 Erez Zadok
4884 + * Copyright (c) 2003-2006 Charles P. Wright
4885 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4886 + * Copyright (c) 2005-2006 Junjiro Okajima
4887 + * Copyright (c) 2005 Arun M. Krishnakumar
4888 + * Copyright (c) 2004-2006 David P. Quigley
4889 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4890 + * Copyright (c) 2003 Puja Gupta
4891 + * Copyright (c) 2003 Harikesavan Krishnan
4892 + * Copyright (c) 2003-2008 Stony Brook University
4893 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
4895 + * This program is free software; you can redistribute it and/or modify
4896 + * it under the terms of the GNU General Public License version 2 as
4897 + * published by the Free Software Foundation.
4903 + * Find a writeable branch to create new object in. Checks all writeble
4904 + * branches of the parent inode, from istart to iend order; if none are
4905 + * suitable, also tries branch 0 (which may require a copyup).
4907 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4909 +static struct dentry *find_writeable_branch(struct inode *parent,
4910 + struct dentry *dentry)
4912 + int err = -EINVAL;
4913 + int bindex, istart, iend;
4914 + struct dentry *lower_dentry = NULL;
4916 + istart = ibstart(parent);
4917 + iend = ibend(parent);
4922 + for (bindex = istart; bindex <= iend; bindex++) {
4923 + /* skip non-writeable branches */
4924 + err = is_robranch_super(dentry->d_sb, bindex);
4929 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4930 + if (!lower_dentry)
4933 + * check for whiteouts in writeable branch, and remove them
4936 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4937 + if (err > 0) /* ignore if whiteout found and removed */
4941 + /* if get here, we can write to the branch */
4945 + * If istart wasn't already branch 0, and we got any error, then try
4946 + * branch 0 (which may require copyup)
4948 + if (err && istart > 0) {
4949 + istart = iend = 0;
4954 + * If we tried even branch 0, and still got an error, abort. But if
4955 + * the error was an EROFS, then we should try to copyup.
4957 + if (err && err != -EROFS)
4961 + * If we get here, then check if copyup needed. If lower_dentry is
4962 + * NULL, create the entire dentry directory structure in branch 0.
4964 + if (!lower_dentry) {
4966 + lower_dentry = create_parents(parent, dentry,
4967 + dentry->d_name.name, bindex);
4968 + if (IS_ERR(lower_dentry)) {
4969 + err = PTR_ERR(lower_dentry);
4973 + err = 0; /* all's well */
4976 + return ERR_PTR(err);
4977 + return lower_dentry;
4980 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4981 + int mode, struct nameidata *nd_unused)
4984 + struct dentry *lower_dentry = NULL;
4985 + struct dentry *lower_parent_dentry = NULL;
4986 + struct dentry *parent;
4988 + struct nameidata lower_nd;
4990 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4991 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4992 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4994 + valid = __unionfs_d_revalidate(dentry, parent, false);
4995 + if (unlikely(!valid)) {
4996 + err = -ESTALE; /* same as what real_lookup does */
5000 + lower_dentry = find_writeable_branch(dir, dentry);
5001 + if (IS_ERR(lower_dentry)) {
5002 + err = PTR_ERR(lower_dentry);
5006 + lower_parent_dentry = lock_parent(lower_dentry);
5007 + if (IS_ERR(lower_parent_dentry)) {
5008 + err = PTR_ERR(lower_parent_dentry);
5012 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
5013 + if (unlikely(err < 0))
5015 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
5017 + release_lower_nd(&lower_nd, err);
5020 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5022 + unionfs_copy_attr_times(dir);
5023 + fsstack_copy_inode_size(dir,
5024 + lower_parent_dentry->d_inode);
5025 + /* update no. of links on parent directory */
5026 + dir->i_nlink = unionfs_get_nlinks(dir);
5030 + unlock_dir(lower_parent_dentry);
5034 + unionfs_postcopyup_setmnt(dentry);
5035 + unionfs_check_inode(dir);
5036 + unionfs_check_dentry(dentry);
5038 + unionfs_unlock_dentry(dentry);
5039 + unionfs_unlock_parent(dentry, parent);
5040 + unionfs_read_unlock(dentry->d_sb);
5045 + * unionfs_lookup is the only special function which takes a dentry, yet we
5046 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
5047 + * we don't have a valid dentry here yet.
5049 +static struct dentry *unionfs_lookup(struct inode *dir,
5050 + struct dentry *dentry,
5051 + struct nameidata *nd_unused)
5053 + struct dentry *ret, *parent;
5056 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5057 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5060 + * As long as we lock/dget the parent, then can skip validating the
5061 + * parent now; we may have to rebuild this dentry on the next
5062 + * ->d_revalidate, however.
5065 + /* allocate dentry private data. We free it in ->d_release */
5066 + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
5067 + if (unlikely(err)) {
5068 + ret = ERR_PTR(err);
5072 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
5074 + if (!IS_ERR(ret)) {
5077 + /* lookup_full can return multiple positive dentries */
5078 + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5079 + BUG_ON(dbstart(dentry) < 0);
5080 + unionfs_postcopyup_release(dentry);
5082 + unionfs_copy_attr_times(dentry->d_inode);
5085 + unionfs_check_inode(dir);
5087 + unionfs_check_dentry(dentry);
5088 + unionfs_check_dentry(parent);
5089 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5092 + unionfs_unlock_parent(dentry, parent);
5093 + unionfs_read_unlock(dentry->d_sb);
5098 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5099 + struct dentry *new_dentry)
5102 + struct dentry *lower_old_dentry = NULL;
5103 + struct dentry *lower_new_dentry = NULL;
5104 + struct dentry *lower_dir_dentry = NULL;
5105 + struct dentry *old_parent, *new_parent;
5106 + char *name = NULL;
5109 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5110 + old_parent = dget_parent(old_dentry);
5111 + new_parent = dget_parent(new_dentry);
5112 + unionfs_double_lock_parents(old_parent, new_parent);
5113 + unionfs_double_lock_dentry(old_dentry, new_dentry);
5115 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5116 + if (unlikely(!valid)) {
5120 + if (new_dentry->d_inode) {
5121 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5122 + if (unlikely(!valid)) {
5128 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5130 + /* check for a whiteout in new dentry branch, and delete it */
5131 + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5132 + dbstart(new_dentry));
5133 + if (err > 0) { /* whiteout found and removed successfully */
5134 + lower_dir_dentry = dget_parent(lower_new_dentry);
5135 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5136 + dput(lower_dir_dentry);
5137 + dir->i_nlink = unionfs_get_nlinks(dir);
5143 + /* check if parent hierachy is needed, then link in same branch */
5144 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5145 + lower_new_dentry = create_parents(dir, new_dentry,
5146 + new_dentry->d_name.name,
5147 + dbstart(old_dentry));
5148 + err = PTR_ERR(lower_new_dentry);
5149 + if (IS_COPYUP_ERR(err))
5151 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5154 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5155 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5157 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5158 + lower_dir_dentry = lock_parent(lower_new_dentry);
5159 + err = is_robranch(old_dentry);
5161 + /* see Documentation/filesystems/unionfs/issues.txt */
5163 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5164 + lower_new_dentry);
5167 + unlock_dir(lower_dir_dentry);
5170 + if (IS_COPYUP_ERR(err)) {
5171 + int old_bstart = dbstart(old_dentry);
5174 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5175 + err = copyup_dentry(old_parent->d_inode,
5176 + old_dentry, old_bstart,
5177 + bindex, old_dentry->d_name.name,
5178 + old_dentry->d_name.len, NULL,
5179 + i_size_read(old_dentry->d_inode));
5182 + lower_new_dentry =
5183 + create_parents(dir, new_dentry,
5184 + new_dentry->d_name.name,
5186 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5187 + lower_dir_dentry = lock_parent(lower_new_dentry);
5188 + /* see Documentation/filesystems/unionfs/issues.txt */
5191 + err = vfs_link(lower_old_dentry,
5192 + lower_dir_dentry->d_inode,
5193 + lower_new_dentry);
5195 + unlock_dir(lower_dir_dentry);
5202 + if (err || !lower_new_dentry->d_inode)
5205 + /* Its a hard link, so use the same inode */
5206 + new_dentry->d_inode = igrab(old_dentry->d_inode);
5207 + d_add(new_dentry, new_dentry->d_inode);
5208 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5209 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5211 + /* propagate number of hard-links */
5212 + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5213 + /* new dentry's ctime may have changed due to hard-link counts */
5214 + unionfs_copy_attr_times(new_dentry->d_inode);
5217 + if (!new_dentry->d_inode)
5218 + d_drop(new_dentry);
5222 + unionfs_postcopyup_setmnt(new_dentry);
5224 + unionfs_check_inode(dir);
5225 + unionfs_check_dentry(new_dentry);
5226 + unionfs_check_dentry(old_dentry);
5228 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5229 + unionfs_double_unlock_parents(old_parent, new_parent);
5232 + unionfs_read_unlock(old_dentry->d_sb);
5237 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5238 + const char *symname)
5241 + struct dentry *lower_dentry = NULL;
5242 + struct dentry *wh_dentry = NULL;
5243 + struct dentry *lower_parent_dentry = NULL;
5244 + struct dentry *parent;
5245 + char *name = NULL;
5249 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5250 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5251 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5253 + valid = __unionfs_d_revalidate(dentry, parent, false);
5254 + if (unlikely(!valid)) {
5260 + * It's only a bug if this dentry was not negative and couldn't be
5261 + * revalidated (shouldn't happen).
5263 + BUG_ON(!valid && dentry->d_inode);
5265 + lower_dentry = find_writeable_branch(dir, dentry);
5266 + if (IS_ERR(lower_dentry)) {
5267 + err = PTR_ERR(lower_dentry);
5271 + lower_parent_dentry = lock_parent(lower_dentry);
5272 + if (IS_ERR(lower_parent_dentry)) {
5273 + err = PTR_ERR(lower_parent_dentry);
5278 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5280 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5282 + unionfs_copy_attr_times(dir);
5283 + fsstack_copy_inode_size(dir,
5284 + lower_parent_dentry->d_inode);
5285 + /* update no. of links on parent directory */
5286 + dir->i_nlink = unionfs_get_nlinks(dir);
5290 + unlock_dir(lower_parent_dentry);
5297 + unionfs_postcopyup_setmnt(dentry);
5298 + unionfs_check_inode(dir);
5299 + unionfs_check_dentry(dentry);
5301 + unionfs_unlock_dentry(dentry);
5302 + unionfs_unlock_parent(dentry, parent);
5303 + unionfs_read_unlock(dentry->d_sb);
5307 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5310 + struct dentry *lower_dentry = NULL;
5311 + struct dentry *lower_parent_dentry = NULL;
5312 + struct dentry *parent;
5313 + int bindex = 0, bstart;
5314 + char *name = NULL;
5317 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5318 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5319 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5321 + valid = __unionfs_d_revalidate(dentry, parent, false);
5322 + if (unlikely(!valid)) {
5323 + err = -ESTALE; /* same as what real_lookup does */
5327 + bstart = dbstart(dentry);
5329 + lower_dentry = unionfs_lower_dentry(dentry);
5331 + /* check for a whiteout in new dentry branch, and delete it */
5332 + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5333 + if (err > 0) /* whiteout found and removed successfully */
5336 + /* exit if the error returned was NOT -EROFS */
5337 + if (!IS_COPYUP_ERR(err))
5342 + /* check if copyup's needed, and mkdir */
5343 + for (bindex = bstart; bindex >= 0; bindex--) {
5345 + int bend = dbend(dentry);
5347 + if (is_robranch_super(dentry->d_sb, bindex))
5350 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5351 + if (!lower_dentry) {
5352 + lower_dentry = create_parents(dir, dentry,
5353 + dentry->d_name.name,
5355 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5356 + printk(KERN_ERR "unionfs: lower dentry "
5357 + " NULL for bindex = %d\n", bindex);
5362 + lower_parent_dentry = lock_parent(lower_dentry);
5364 + if (IS_ERR(lower_parent_dentry)) {
5365 + err = PTR_ERR(lower_parent_dentry);
5369 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5372 + unlock_dir(lower_parent_dentry);
5374 + /* did the mkdir succeed? */
5378 + for (i = bindex + 1; i <= bend; i++) {
5379 + /* XXX: use path_put_lowers? */
5380 + if (unionfs_lower_dentry_idx(dentry, i)) {
5381 + dput(unionfs_lower_dentry_idx(dentry, i));
5382 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5385 + dbend(dentry) = bindex;
5388 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5391 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5393 + unionfs_copy_attr_times(dir);
5394 + fsstack_copy_inode_size(dir,
5395 + lower_parent_dentry->d_inode);
5397 + /* update number of links on parent directory */
5398 + dir->i_nlink = unionfs_get_nlinks(dir);
5401 + err = make_dir_opaque(dentry, dbstart(dentry));
5403 + printk(KERN_ERR "unionfs: mkdir: error creating "
5404 + ".wh.__dir_opaque: %d\n", err);
5408 + /* we are done! */
5413 + if (!dentry->d_inode)
5419 + unionfs_copy_attr_times(dentry->d_inode);
5420 + unionfs_postcopyup_setmnt(dentry);
5422 + unionfs_check_inode(dir);
5423 + unionfs_check_dentry(dentry);
5424 + unionfs_unlock_dentry(dentry);
5425 + unionfs_unlock_parent(dentry, parent);
5426 + unionfs_read_unlock(dentry->d_sb);
5431 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5435 + struct dentry *lower_dentry = NULL;
5436 + struct dentry *wh_dentry = NULL;
5437 + struct dentry *lower_parent_dentry = NULL;
5438 + struct dentry *parent;
5439 + char *name = NULL;
5442 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5443 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5444 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5446 + valid = __unionfs_d_revalidate(dentry, parent, false);
5447 + if (unlikely(!valid)) {
5453 + * It's only a bug if this dentry was not negative and couldn't be
5454 + * revalidated (shouldn't happen).
5456 + BUG_ON(!valid && dentry->d_inode);
5458 + lower_dentry = find_writeable_branch(dir, dentry);
5459 + if (IS_ERR(lower_dentry)) {
5460 + err = PTR_ERR(lower_dentry);
5464 + lower_parent_dentry = lock_parent(lower_dentry);
5465 + if (IS_ERR(lower_parent_dentry)) {
5466 + err = PTR_ERR(lower_parent_dentry);
5470 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5472 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5474 + unionfs_copy_attr_times(dir);
5475 + fsstack_copy_inode_size(dir,
5476 + lower_parent_dentry->d_inode);
5477 + /* update no. of links on parent directory */
5478 + dir->i_nlink = unionfs_get_nlinks(dir);
5482 + unlock_dir(lower_parent_dentry);
5489 + unionfs_postcopyup_setmnt(dentry);
5490 + unionfs_check_inode(dir);
5491 + unionfs_check_dentry(dentry);
5493 + unionfs_unlock_dentry(dentry);
5494 + unionfs_unlock_parent(dentry, parent);
5495 + unionfs_read_unlock(dentry->d_sb);
5499 +/* requires sb, dentry, and parent to already be locked */
5500 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5504 + struct dentry *lower_dentry;
5506 + lower_dentry = unionfs_lower_dentry(dentry);
5508 + if (!lower_dentry->d_inode->i_op ||
5509 + !lower_dentry->d_inode->i_op->readlink) {
5514 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5517 + fsstack_copy_attr_atime(dentry->d_inode,
5518 + lower_dentry->d_inode);
5524 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5528 + struct dentry *parent;
5530 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5531 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5532 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5534 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5539 + err = __unionfs_readlink(dentry, buf, bufsiz);
5542 + unionfs_check_dentry(dentry);
5543 + unionfs_unlock_dentry(dentry);
5544 + unionfs_unlock_parent(dentry, parent);
5545 + unionfs_read_unlock(dentry->d_sb);
5550 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5553 + int len = PAGE_SIZE, err;
5554 + mm_segment_t old_fs;
5555 + struct dentry *parent;
5557 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5558 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5559 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5561 + /* This is freed by the put_link method assuming a successful call. */
5562 + buf = kmalloc(len, GFP_KERNEL);
5563 + if (unlikely(!buf)) {
5568 + /* read the symlink, and then we will follow it */
5569 + old_fs = get_fs();
5570 + set_fs(KERNEL_DS);
5571 + err = __unionfs_readlink(dentry, buf, len);
5579 + nd_set_link(nd, buf);
5584 + unionfs_check_nd(nd);
5585 + unionfs_check_dentry(dentry);
5588 + unionfs_unlock_dentry(dentry);
5589 + unionfs_unlock_parent(dentry, parent);
5590 + unionfs_read_unlock(dentry->d_sb);
5592 + return ERR_PTR(err);
5595 +/* this @nd *IS* still used */
5596 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5599 + struct dentry *parent;
5601 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5602 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5603 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5605 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5607 + "unionfs: put_link failed to revalidate dentry\n");
5609 + unionfs_check_dentry(dentry);
5610 + unionfs_check_nd(nd);
5611 + kfree(nd_get_link(nd));
5612 + unionfs_unlock_dentry(dentry);
5613 + unionfs_unlock_parent(dentry, parent);
5614 + unionfs_read_unlock(dentry->d_sb);
5618 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5619 + * skips over EROFS tests (because we perform copyup on EROFS).
5621 +static int __inode_permission(struct inode *inode, int mask)
5625 + /* nobody gets write access to an immutable file */
5626 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5629 + /* Ordinary permission routines do not understand MAY_APPEND. */
5630 + if (inode->i_op && inode->i_op->permission) {
5631 + retval = inode->i_op->permission(inode, mask);
5634 + * Exec permission on a regular file is denied if none
5635 + * of the execute bits are set.
5637 + * This check should be done by the ->permission()
5640 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5641 + !(inode->i_mode & S_IXUGO))
5645 + retval = generic_permission(inode, mask, NULL);
5650 + return security_inode_permission(inode,
5651 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5655 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5656 + * a deadlock with the branch-management "add branch" code (which grabbed
5657 + * the write lock). It is safe to not grab the read lock here, because even
5658 + * with branch management taking place, there is no chance that
5659 + * unionfs_permission, or anything it calls, will use stale branch
5662 +static int unionfs_permission(struct inode *inode, int mask)
5664 + struct inode *lower_inode = NULL;
5666 + int bindex, bstart, bend;
5667 + const int is_file = !S_ISDIR(inode->i_mode);
5668 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5669 + struct inode *inode_grabbed = igrab(inode);
5670 + struct dentry *dentry = d_find_alias(inode);
5673 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5675 + if (!UNIONFS_I(inode)->lower_inodes) {
5676 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5677 + err = -ESTALE; /* force revalidate */
5680 + bstart = ibstart(inode);
5681 + bend = ibend(inode);
5682 + if (unlikely(bstart < 0 || bend < 0)) {
5684 + * With branch-management, we can get a stale inode here.
5685 + * If so, we return ESTALE back to link_path_walk, which
5686 + * would discard the dcache entry and re-lookup the
5687 + * dentry+inode. This should be equivalent to issuing
5688 + * __unionfs_d_revalidate_chain on nd.dentry here.
5690 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5691 + err = -ESTALE; /* force revalidate */
5695 + for (bindex = bstart; bindex <= bend; bindex++) {
5696 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5701 + * check the condition for D-F-D underlying files/directories,
5702 + * we don't have to check for files, if we are checking for
5705 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5709 + * We check basic permissions, but we ignore any conditions
5710 + * such as readonly file systems or branches marked as
5711 + * readonly, because those conditions should lead to a
5712 + * copyup taking place later on. However, if user never had
5713 + * access to the file, then no copyup could ever take place.
5715 + err = __inode_permission(lower_inode, mask);
5716 + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5717 + umode_t mode = lower_inode->i_mode;
5718 + if ((is_robranch_super(inode->i_sb, bindex) ||
5719 + IS_RDONLY(lower_inode)) &&
5720 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5722 + if (IS_COPYUP_ERR(err))
5727 + * The permissions are an intersection of the overall directory
5728 + * permissions, so we fail if one fails.
5733 + /* only the leftmost file matters. */
5734 + if (is_file || write_mask) {
5735 + if (is_file && write_mask) {
5736 + err = get_write_access(lower_inode);
5738 + put_write_access(lower_inode);
5743 + /* sync times which may have changed (asynchronously) below */
5744 + unionfs_copy_attr_times(inode);
5747 + unionfs_check_inode(inode);
5749 + unionfs_unlock_dentry(dentry);
5752 + iput(inode_grabbed);
5756 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5759 + struct dentry *lower_dentry;
5760 + struct dentry *parent;
5761 + struct inode *inode;
5762 + struct inode *lower_inode;
5763 + int bstart, bend, bindex;
5766 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5767 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5768 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5770 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5775 + bstart = dbstart(dentry);
5776 + bend = dbend(dentry);
5777 + inode = dentry->d_inode;
5780 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5781 + * to reinterpret it in its own way.
5783 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5784 + ia->ia_valid &= ~ATTR_MODE;
5786 + lower_dentry = unionfs_lower_dentry(dentry);
5787 + if (!lower_dentry) { /* should never happen after above revalidate */
5791 + lower_inode = unionfs_lower_inode(inode);
5793 + /* check if user has permission to change lower inode */
5794 + err = inode_change_ok(lower_inode, ia);
5798 + /* copyup if the file is on a read only branch */
5799 + if (is_robranch_super(dentry->d_sb, bstart)
5800 + || IS_RDONLY(lower_inode)) {
5801 + /* check if we have a branch to copy up to */
5802 + if (bstart <= 0) {
5807 + if (ia->ia_valid & ATTR_SIZE)
5808 + size = ia->ia_size;
5810 + size = i_size_read(inode);
5811 + /* copyup to next available branch */
5812 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5813 + err = copyup_dentry(parent->d_inode,
5814 + dentry, bstart, bindex,
5815 + dentry->d_name.name,
5816 + dentry->d_name.len,
5823 + /* get updated lower_dentry/inode after copyup */
5824 + lower_dentry = unionfs_lower_dentry(dentry);
5825 + lower_inode = unionfs_lower_inode(inode);
5829 + * If shrinking, first truncate upper level to cancel writing dirty
5830 + * pages beyond the new eof; and also if its' maxbytes is more
5831 + * limiting (fail with -EFBIG before making any change to the lower
5832 + * level). There is no need to vmtruncate the upper level
5833 + * afterwards in the other cases: we fsstack_copy_inode_size from
5834 + * the lower level.
5836 + if (ia->ia_valid & ATTR_SIZE) {
5837 + size = i_size_read(inode);
5838 + if (ia->ia_size < size || (ia->ia_size > size &&
5839 + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5840 + err = vmtruncate(inode, ia->ia_size);
5846 + /* notify the (possibly copied-up) lower inode */
5847 + mutex_lock(&lower_inode->i_mutex);
5848 + err = notify_change(lower_dentry, ia);
5849 + mutex_unlock(&lower_inode->i_mutex);
5853 + /* get attributes from the first lower inode */
5854 + unionfs_copy_attr_all(inode, lower_inode);
5856 + * unionfs_copy_attr_all will copy the lower times to our inode if
5857 + * the lower ones are newer (useful for cache coherency). However,
5858 + * ->setattr is the only place in which we may have to copy the
5859 + * lower inode times absolutely, to support utimes(2).
5861 + if (ia->ia_valid & ATTR_MTIME_SET)
5862 + inode->i_mtime = lower_inode->i_mtime;
5863 + if (ia->ia_valid & ATTR_CTIME)
5864 + inode->i_ctime = lower_inode->i_ctime;
5865 + if (ia->ia_valid & ATTR_ATIME_SET)
5866 + inode->i_atime = lower_inode->i_atime;
5867 + fsstack_copy_inode_size(inode, lower_inode);
5871 + unionfs_check_dentry(dentry);
5872 + unionfs_unlock_dentry(dentry);
5873 + unionfs_unlock_parent(dentry, parent);
5874 + unionfs_read_unlock(dentry->d_sb);
5879 +struct inode_operations unionfs_symlink_iops = {
5880 + .readlink = unionfs_readlink,
5881 + .permission = unionfs_permission,
5882 + .follow_link = unionfs_follow_link,
5883 + .setattr = unionfs_setattr,
5884 + .put_link = unionfs_put_link,
5887 +struct inode_operations unionfs_dir_iops = {
5888 + .create = unionfs_create,
5889 + .lookup = unionfs_lookup,
5890 + .link = unionfs_link,
5891 + .unlink = unionfs_unlink,
5892 + .symlink = unionfs_symlink,
5893 + .mkdir = unionfs_mkdir,
5894 + .rmdir = unionfs_rmdir,
5895 + .mknod = unionfs_mknod,
5896 + .rename = unionfs_rename,
5897 + .permission = unionfs_permission,
5898 + .setattr = unionfs_setattr,
5899 +#ifdef CONFIG_UNION_FS_XATTR
5900 + .setxattr = unionfs_setxattr,
5901 + .getxattr = unionfs_getxattr,
5902 + .removexattr = unionfs_removexattr,
5903 + .listxattr = unionfs_listxattr,
5904 +#endif /* CONFIG_UNION_FS_XATTR */
5907 +struct inode_operations unionfs_main_iops = {
5908 + .permission = unionfs_permission,
5909 + .setattr = unionfs_setattr,
5910 +#ifdef CONFIG_UNION_FS_XATTR
5911 + .setxattr = unionfs_setxattr,
5912 + .getxattr = unionfs_getxattr,
5913 + .removexattr = unionfs_removexattr,
5914 + .listxattr = unionfs_listxattr,
5915 +#endif /* CONFIG_UNION_FS_XATTR */
5917 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5918 new file mode 100644
5919 index 0000000..6877b8c
5921 +++ b/fs/unionfs/lookup.c
5924 + * Copyright (c) 2003-2008 Erez Zadok
5925 + * Copyright (c) 2003-2006 Charles P. Wright
5926 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5927 + * Copyright (c) 2005-2006 Junjiro Okajima
5928 + * Copyright (c) 2005 Arun M. Krishnakumar
5929 + * Copyright (c) 2004-2006 David P. Quigley
5930 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5931 + * Copyright (c) 2003 Puja Gupta
5932 + * Copyright (c) 2003 Harikesavan Krishnan
5933 + * Copyright (c) 2003-2008 Stony Brook University
5934 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
5936 + * This program is free software; you can redistribute it and/or modify
5937 + * it under the terms of the GNU General Public License version 2 as
5938 + * published by the Free Software Foundation.
5944 + * Lookup one path component @name relative to a <base,mnt> path pair.
5945 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5946 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5947 + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5948 + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5949 + * @dentry and @new_mnt.
5951 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5952 + const char *name, struct vfsmount **new_mnt)
5954 + struct dentry *dentry = NULL;
5955 + struct nameidata lower_nd;
5958 + /* we use flags=0 to get basic lookup */
5959 + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
5962 + case 0: /* no error */
5963 + dentry = lower_nd.path.dentry;
5965 + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
5969 + * We don't consider ENOENT an error, and we want to return
5970 + * a negative dentry (ala lookup_one_len). As we know
5971 + * there was no inode for this name before (-ENOENT), then
5972 + * it's safe to call lookup_one_len (which doesn't take a
5975 + dentry = lookup_one_len(name, base, strlen(name));
5977 + *new_mnt = mntget(lower_nd.path.mnt);
5979 + default: /* all other real errors */
5980 + dentry = ERR_PTR(err);
5988 + * This is a utility function that fills in a unionfs dentry.
5989 + * Caller must lock this dentry with unionfs_lock_dentry.
5991 + * Returns: 0 (ok), or -ERRNO if an error occurred.
5992 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
5994 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
5996 + struct dentry *tmp;
5997 + int err = -ENOSYS;
5999 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
6005 + if (IS_ERR(tmp)) {
6006 + err = PTR_ERR(tmp);
6009 + /* XXX: need to change the interface */
6010 + BUG_ON(tmp != dentry);
6015 +/* The dentry cache is just so we have properly sized dentries. */
6016 +static struct kmem_cache *unionfs_dentry_cachep;
6017 +int unionfs_init_dentry_cache(void)
6019 + unionfs_dentry_cachep =
6020 + kmem_cache_create("unionfs_dentry",
6021 + sizeof(struct unionfs_dentry_info),
6022 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6024 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6027 +void unionfs_destroy_dentry_cache(void)
6029 + if (unionfs_dentry_cachep)
6030 + kmem_cache_destroy(unionfs_dentry_cachep);
6033 +void free_dentry_private_data(struct dentry *dentry)
6035 + if (!dentry || !dentry->d_fsdata)
6037 + kfree(UNIONFS_D(dentry)->lower_paths);
6038 + UNIONFS_D(dentry)->lower_paths = NULL;
6039 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6040 + dentry->d_fsdata = NULL;
6043 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6045 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6051 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6052 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6056 + info->lower_paths = p;
6058 + info->bstart = -1;
6060 + info->bopaque = -1;
6061 + info->bcount = sbmax(dentry->d_sb);
6062 + atomic_set(&info->generation,
6063 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6065 + memset(info->lower_paths, 0, size);
6070 +/* UNIONFS_D(dentry)->lock must be locked */
6071 +int realloc_dentry_private_data(struct dentry *dentry)
6073 + if (!__realloc_dentry_private_data(dentry))
6076 + kfree(UNIONFS_D(dentry)->lower_paths);
6077 + free_dentry_private_data(dentry);
6081 +/* allocate new dentry private data */
6082 +int new_dentry_private_data(struct dentry *dentry, int subclass)
6084 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6088 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6089 + if (unlikely(!info))
6092 + mutex_init(&info->lock);
6093 + mutex_lock_nested(&info->lock, subclass);
6095 + info->lower_paths = NULL;
6097 + dentry->d_fsdata = info;
6099 + if (!__realloc_dentry_private_data(dentry))
6102 + mutex_unlock(&info->lock);
6103 + free_dentry_private_data(dentry);
6108 + * scan through the lower dentry objects, and set bstart to reflect the
6111 +void update_bstart(struct dentry *dentry)
6114 + int bstart = dbstart(dentry);
6115 + int bend = dbend(dentry);
6116 + struct dentry *lower_dentry;
6118 + for (bindex = bstart; bindex <= bend; bindex++) {
6119 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6120 + if (!lower_dentry)
6122 + if (lower_dentry->d_inode) {
6123 + dbstart(dentry) = bindex;
6126 + dput(lower_dentry);
6127 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6133 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6134 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6135 + * Inside that nd structure, this function may also return an allocated
6136 + * struct file (for open intents). The caller, when done with this nd, must
6137 + * kfree the intent file (using release_lower_nd).
6139 + * XXX: this code, and the callers of this code, should be redone using
6140 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6141 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6142 + * function and a method that other file systems can call.
6144 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6147 +#ifdef ALLOC_LOWER_ND_FILE
6149 + * XXX: one day we may need to have the lower return an open file
6150 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6151 + * very well be needed for nfs4.
6153 + struct file *file;
6154 +#endif /* ALLOC_LOWER_ND_FILE */
6156 + memset(nd, 0, sizeof(struct nameidata));
6161 + case LOOKUP_CREATE:
6162 + nd->intent.open.flags |= O_CREAT;
6163 + /* fall through: shared code for create/open cases */
6165 + nd->flags = flags;
6166 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6167 +#ifdef ALLOC_LOWER_ND_FILE
6168 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6169 + if (unlikely(!file)) {
6171 + break; /* exit switch statement and thus return */
6173 + nd->intent.open.file = file;
6174 +#endif /* ALLOC_LOWER_ND_FILE */
6178 + * We should never get here, for now.
6179 + * We can add new cases here later on.
6181 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6189 +void release_lower_nd(struct nameidata *nd, int err)
6191 + if (!nd->intent.open.file)
6194 + release_open_intent(nd);
6195 +#ifdef ALLOC_LOWER_ND_FILE
6196 + kfree(nd->intent.open.file);
6197 +#endif /* ALLOC_LOWER_ND_FILE */
6201 + * Main (and complex) driver function for Unionfs's lookup
6203 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6204 + * PTR if d_splice returned a different dentry.
6206 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6207 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6208 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6209 + * dentry's info, which the caller must unlock.
6211 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6212 + struct dentry *parent, int lookupmode)
6215 + struct dentry *lower_dentry = NULL;
6216 + struct vfsmount *lower_mnt;
6217 + struct vfsmount *lower_dir_mnt;
6218 + struct dentry *wh_lower_dentry = NULL;
6219 + struct dentry *lower_dir_dentry = NULL;
6220 + struct dentry *d_interposed = NULL;
6221 + int bindex, bstart, bend, bopaque;
6222 + int opaque, num_positive = 0;
6225 + int pos_start, pos_end;
6228 + * We should already have a lock on this dentry in the case of a
6229 + * partial lookup, or a revalidation. Otherwise it is returned from
6230 + * new_dentry_private_data already locked.
6232 + verify_locked(dentry);
6233 + verify_locked(parent);
6235 + /* must initialize dentry operations */
6236 + dentry->d_op = &unionfs_dops;
6238 + /* We never partial lookup the root directory. */
6239 + if (IS_ROOT(dentry))
6242 + name = dentry->d_name.name;
6243 + namelen = dentry->d_name.len;
6245 + /* No dentries should get created for possible whiteout names. */
6246 + if (!is_validname(name)) {
6251 + /* Now start the actual lookup procedure. */
6252 + bstart = dbstart(parent);
6253 + bend = dbend(parent);
6254 + bopaque = dbopaque(parent);
6255 + BUG_ON(bstart < 0);
6257 + /* adjust bend to bopaque if needed */
6258 + if ((bopaque >= 0) && (bopaque < bend))
6261 + /* lookup all possible dentries */
6262 + for (bindex = bstart; bindex <= bend; bindex++) {
6264 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6265 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6267 + /* skip if we already have a positive lower dentry */
6268 + if (lower_dentry) {
6269 + if (dbstart(dentry) < 0)
6270 + dbstart(dentry) = bindex;
6271 + if (bindex > dbend(dentry))
6272 + dbend(dentry) = bindex;
6273 + if (lower_dentry->d_inode)
6278 + lower_dir_dentry =
6279 + unionfs_lower_dentry_idx(parent, bindex);
6280 + /* if the lower dentry's parent does not exist, skip this */
6281 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6284 + /* also skip it if the parent isn't a directory. */
6285 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6286 + continue; /* XXX: should be BUG_ON */
6288 + /* check for whiteouts: stop lookup if found */
6289 + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6290 + if (IS_ERR(wh_lower_dentry)) {
6291 + err = PTR_ERR(wh_lower_dentry);
6294 + if (wh_lower_dentry->d_inode) {
6295 + dbend(dentry) = dbopaque(dentry) = bindex;
6296 + if (dbstart(dentry) < 0)
6297 + dbstart(dentry) = bindex;
6298 + dput(wh_lower_dentry);
6301 + dput(wh_lower_dentry);
6303 + /* Now do regular lookup; lookup @name */
6304 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6305 + lower_mnt = NULL; /* XXX: needed? */
6307 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6308 + name, &lower_mnt);
6310 + if (IS_ERR(lower_dentry)) {
6311 + err = PTR_ERR(lower_dentry);
6314 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6316 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6318 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6320 + /* adjust dbstart/end */
6321 + if (dbstart(dentry) < 0)
6322 + dbstart(dentry) = bindex;
6323 + if (bindex > dbend(dentry))
6324 + dbend(dentry) = bindex;
6326 + * We always store the lower dentries above, and update
6327 + * dbstart/dbend, even if the whole unionfs dentry is
6328 + * negative (i.e., no lower inodes).
6330 + if (!lower_dentry->d_inode)
6335 + * check if we just found an opaque directory, if so, stop
6338 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6340 + opaque = is_opaque_dir(dentry, bindex);
6344 + } else if (opaque) {
6345 + dbend(dentry) = dbopaque(dentry) = bindex;
6348 + dbend(dentry) = bindex;
6350 + /* update parent directory's atime with the bindex */
6351 + fsstack_copy_attr_atime(parent->d_inode,
6352 + lower_dir_dentry->d_inode);
6355 + /* sanity checks, then decide if to process a negative dentry */
6356 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6357 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6359 + if (num_positive > 0)
6360 + goto out_positive;
6362 + /*** handle NEGATIVE dentries ***/
6365 + * If negative, keep only first lower negative dentry, to save on
6368 + if (dbstart(dentry) < dbend(dentry)) {
6369 + path_put_lowers(dentry, dbstart(dentry) + 1,
6370 + dbend(dentry), false);
6371 + dbend(dentry) = dbstart(dentry);
6373 + if (lookupmode == INTERPOSE_PARTIAL)
6375 + if (lookupmode == INTERPOSE_LOOKUP) {
6377 + * If all we found was a whiteout in the first available
6378 + * branch, then create a negative dentry for a possibly new
6379 + * file to be created.
6381 + if (dbopaque(dentry) < 0)
6383 + /* XXX: need to get mnt here */
6384 + bindex = dbstart(dentry);
6385 + if (unionfs_lower_dentry_idx(dentry, bindex))
6387 + lower_dir_dentry =
6388 + unionfs_lower_dentry_idx(parent, bindex);
6389 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6391 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6392 + goto out; /* XXX: should be BUG_ON */
6393 + /* XXX: do we need to cross bind mounts here? */
6394 + lower_dentry = lookup_one_len(name, lower_dir_dentry, namelen);
6395 + if (IS_ERR(lower_dentry)) {
6396 + err = PTR_ERR(lower_dentry);
6399 + /* XXX: need to mntget/mntput as needed too! */
6400 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6401 + /* XXX: wrong mnt for crossing bind mounts! */
6402 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6403 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6408 + /* if we're revalidating a positive dentry, don't make it negative */
6409 + if (lookupmode != INTERPOSE_REVAL)
6410 + d_add(dentry, NULL);
6415 + /*** handle POSITIVE dentries ***/
6418 + * This unionfs dentry is positive (at least one lower inode
6419 + * exists), so scan entire dentry from beginning to end, and remove
6420 + * any negative lower dentries, if any. Then, update dbstart/dbend
6421 + * to reflect the start/end of positive dentries.
6423 + pos_start = pos_end = -1;
6424 + for (bindex = bstart; bindex <= bend; bindex++) {
6425 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6427 + if (lower_dentry && lower_dentry->d_inode) {
6428 + if (pos_start < 0)
6429 + pos_start = bindex;
6430 + if (bindex > pos_end)
6434 + path_put_lowers(dentry, bindex, bindex, false);
6436 + if (pos_start >= 0)
6437 + dbstart(dentry) = pos_start;
6439 + dbend(dentry) = pos_end;
6441 + /* Partial lookups need to re-interpose, or throw away older negs. */
6442 + if (lookupmode == INTERPOSE_PARTIAL) {
6443 + if (dentry->d_inode) {
6444 + unionfs_reinterpose(dentry);
6449 + * This dentry was positive, so it is as if we had a
6450 + * negative revalidation.
6452 + lookupmode = INTERPOSE_REVAL_NEG;
6453 + update_bstart(dentry);
6457 + * Interpose can return a dentry if d_splice returned a different
6460 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6461 + if (IS_ERR(d_interposed))
6462 + err = PTR_ERR(d_interposed);
6463 + else if (d_interposed)
6464 + dentry = d_interposed;
6471 + /* should dput/mntput all the underlying dentries on error condition */
6472 + if (dbstart(dentry) >= 0)
6473 + path_put_lowers_all(dentry, false);
6474 + /* free lower_paths unconditionally */
6475 + kfree(UNIONFS_D(dentry)->lower_paths);
6476 + UNIONFS_D(dentry)->lower_paths = NULL;
6479 + if (dentry && UNIONFS_D(dentry)) {
6480 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6481 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6483 + if (d_interposed && UNIONFS_D(d_interposed)) {
6484 + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6485 + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6488 + if (!err && d_interposed)
6489 + return d_interposed;
6490 + return ERR_PTR(err);
6492 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6493 new file mode 100644
6494 index 0000000..fea670b
6496 +++ b/fs/unionfs/main.c
6499 + * Copyright (c) 2003-2008 Erez Zadok
6500 + * Copyright (c) 2003-2006 Charles P. Wright
6501 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6502 + * Copyright (c) 2005-2006 Junjiro Okajima
6503 + * Copyright (c) 2005 Arun M. Krishnakumar
6504 + * Copyright (c) 2004-2006 David P. Quigley
6505 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6506 + * Copyright (c) 2003 Puja Gupta
6507 + * Copyright (c) 2003 Harikesavan Krishnan
6508 + * Copyright (c) 2003-2008 Stony Brook University
6509 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
6511 + * This program is free software; you can redistribute it and/or modify
6512 + * it under the terms of the GNU General Public License version 2 as
6513 + * published by the Free Software Foundation.
6517 +#include <linux/module.h>
6518 +#include <linux/moduleparam.h>
6520 +static void unionfs_fill_inode(struct dentry *dentry,
6521 + struct inode *inode)
6523 + struct inode *lower_inode;
6524 + struct dentry *lower_dentry;
6525 + int bindex, bstart, bend;
6527 + bstart = dbstart(dentry);
6528 + bend = dbend(dentry);
6530 + for (bindex = bstart; bindex <= bend; bindex++) {
6531 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6532 + if (!lower_dentry) {
6533 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6537 + /* Initialize the lower inode to the new lower inode. */
6538 + if (!lower_dentry->d_inode)
6541 + unionfs_set_lower_inode_idx(inode, bindex,
6542 + igrab(lower_dentry->d_inode));
6545 + ibstart(inode) = dbstart(dentry);
6546 + ibend(inode) = dbend(dentry);
6548 + /* Use attributes from the first branch. */
6549 + lower_inode = unionfs_lower_inode(inode);
6551 + /* Use different set of inode ops for symlinks & directories */
6552 + if (S_ISLNK(lower_inode->i_mode))
6553 + inode->i_op = &unionfs_symlink_iops;
6554 + else if (S_ISDIR(lower_inode->i_mode))
6555 + inode->i_op = &unionfs_dir_iops;
6557 + /* Use different set of file ops for directories */
6558 + if (S_ISDIR(lower_inode->i_mode))
6559 + inode->i_fop = &unionfs_dir_fops;
6561 + /* properly initialize special inodes */
6562 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6563 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6564 + init_special_inode(inode, lower_inode->i_mode,
6565 + lower_inode->i_rdev);
6567 + /* all well, copy inode attributes */
6568 + unionfs_copy_attr_all(inode, lower_inode);
6569 + fsstack_copy_inode_size(inode, lower_inode);
6573 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6574 + * the classic stackable file system "vnode interposition" action.
6576 + * @sb: unionfs's super_block
6578 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6582 + struct inode *inode;
6583 + int need_fill_inode = 1;
6584 + struct dentry *spliced = NULL;
6586 + verify_locked(dentry);
6589 + * We allocate our new inode below by calling unionfs_iget,
6590 + * which will initialize some of the new inode's fields
6594 + * On revalidate we've already got our own inode and just need
6597 + if (flag == INTERPOSE_REVAL) {
6598 + inode = dentry->d_inode;
6599 + UNIONFS_I(inode)->bstart = -1;
6600 + UNIONFS_I(inode)->bend = -1;
6601 + atomic_set(&UNIONFS_I(inode)->generation,
6602 + atomic_read(&UNIONFS_SB(sb)->generation));
6604 + UNIONFS_I(inode)->lower_inodes =
6605 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6606 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6611 + /* get unique inode number for unionfs */
6612 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6613 + if (IS_ERR(inode)) {
6614 + err = PTR_ERR(inode);
6617 + if (atomic_read(&inode->i_count) > 1)
6621 + need_fill_inode = 0;
6622 + unionfs_fill_inode(dentry, inode);
6625 + /* only (our) lookup wants to do a d_add */
6627 + case INTERPOSE_DEFAULT:
6628 + /* for operations which create new inodes */
6629 + d_add(dentry, inode);
6631 + case INTERPOSE_REVAL_NEG:
6632 + d_instantiate(dentry, inode);
6634 + case INTERPOSE_LOOKUP:
6635 + spliced = d_splice_alias(inode, dentry);
6636 + if (spliced && spliced != dentry) {
6638 + * d_splice can return a dentry if it was
6639 + * disconnected and had to be moved. We must ensure
6640 + * that the private data of the new dentry is
6641 + * correct and that the inode info was filled
6642 + * properly. Finally we must return this new
6645 + spliced->d_op = &unionfs_dops;
6646 + spliced->d_fsdata = dentry->d_fsdata;
6647 + dentry->d_fsdata = NULL;
6649 + if (need_fill_inode) {
6650 + need_fill_inode = 0;
6651 + unionfs_fill_inode(dentry, inode);
6654 + } else if (!spliced) {
6655 + if (need_fill_inode) {
6656 + need_fill_inode = 0;
6657 + unionfs_fill_inode(dentry, inode);
6662 + case INTERPOSE_REVAL:
6666 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6675 + return ERR_PTR(err);
6678 +/* like interpose above, but for an already existing dentry */
6679 +void unionfs_reinterpose(struct dentry *dentry)
6681 + struct dentry *lower_dentry;
6682 + struct inode *inode;
6683 + int bindex, bstart, bend;
6685 + verify_locked(dentry);
6687 + /* This is pre-allocated inode */
6688 + inode = dentry->d_inode;
6690 + bstart = dbstart(dentry);
6691 + bend = dbend(dentry);
6692 + for (bindex = bstart; bindex <= bend; bindex++) {
6693 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6694 + if (!lower_dentry)
6697 + if (!lower_dentry->d_inode)
6699 + if (unionfs_lower_inode_idx(inode, bindex))
6701 + unionfs_set_lower_inode_idx(inode, bindex,
6702 + igrab(lower_dentry->d_inode));
6704 + ibstart(inode) = dbstart(dentry);
6705 + ibend(inode) = dbend(dentry);
6709 + * make sure the branch we just looked up (nd) makes sense:
6711 + * 1) we're not trying to stack unionfs on top of unionfs
6713 + * 3) is a directory
6715 +int check_branch(struct nameidata *nd)
6717 + /* XXX: remove in ODF code -- stacking unions allowed there */
6718 + if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6720 + if (!nd->path.dentry->d_inode)
6722 + if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6727 +/* checks if two lower_dentries have overlapping branches */
6728 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6730 + struct dentry *dent = NULL;
6733 + while ((dent != dent2) && (dent->d_parent != dent))
6734 + dent = dent->d_parent;
6736 + if (dent == dent2)
6740 + while ((dent != dent1) && (dent->d_parent != dent))
6741 + dent = dent->d_parent;
6743 + return (dent == dent1);
6747 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6748 + * specified. Fill the mode bits in @perms. If encounter an unknown
6749 + * string, return -EINVAL. Otherwise return 0.
6751 +int parse_branch_mode(const char *name, int *perms)
6753 + if (!name || !strcmp(name, "rw")) {
6754 + *perms = MAY_READ | MAY_WRITE;
6757 + if (!strcmp(name, "ro")) {
6758 + *perms = MAY_READ;
6765 + * parse the dirs= mount argument
6767 + * We don't need to lock the superblock private data's rwsem, as we get
6768 + * called only by unionfs_read_super - it is still a long time before anyone
6769 + * can even get a reference to us.
6771 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6772 + *lower_root_info, char *options)
6774 + struct nameidata nd;
6781 + struct dentry *dent1;
6782 + struct dentry *dent2;
6784 + if (options[0] == '\0') {
6785 + printk(KERN_ERR "unionfs: no branches specified\n");
6791 + * Each colon means we have a separator, this is really just a rough
6792 + * guess, since strsep will handle empty fields for us.
6794 + for (i = 0; options[i]; i++)
6795 + if (options[i] == ':')
6798 + /* allocate space for underlying pointers to lower dentry */
6799 + UNIONFS_SB(sb)->data =
6800 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6801 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6806 + lower_root_info->lower_paths =
6807 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6808 + if (unlikely(!lower_root_info->lower_paths)) {
6813 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6815 + while ((name = strsep(&options, ":")) != NULL) {
6817 + char *mode = strchr(name, '=');
6821 + if (!*name) { /* bad use of ':' (extra colons) */
6828 + /* strip off '=' if any */
6832 + err = parse_branch_mode(mode, &perms);
6834 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6835 + "branch %d\n", mode, bindex);
6838 + /* ensure that leftmost branch is writeable */
6839 + if (!bindex && !(perms & MAY_WRITE)) {
6840 + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6841 + "read-only (use \"-o ro\" to create a "
6842 + "read-only union)\n");
6847 + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6849 + printk(KERN_ERR "unionfs: error accessing "
6850 + "lower directory '%s' (error %d)\n",
6855 + err = check_branch(&nd);
6857 + printk(KERN_ERR "unionfs: lower directory "
6858 + "'%s' is not a valid branch\n", name);
6859 + path_put(&nd.path);
6863 + lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6864 + lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6866 + set_branchperms(sb, bindex, perms);
6867 + set_branch_count(sb, bindex, 0);
6868 + new_branch_id(sb, bindex);
6870 + if (lower_root_info->bstart < 0)
6871 + lower_root_info->bstart = bindex;
6872 + lower_root_info->bend = bindex;
6876 + if (branches == 0) {
6877 + printk(KERN_ERR "unionfs: no branches specified\n");
6882 + BUG_ON(branches != (lower_root_info->bend + 1));
6885 + * Ensure that no overlaps exist in the branches.
6887 + * This test is required because the Linux kernel has no support
6888 + * currently for ensuring coherency between stackable layers and
6889 + * branches. If we were to allow overlapping branches, it would be
6890 + * possible, for example, to delete a file via one branch, which
6891 + * would not be reflected in another branch. Such incoherency could
6892 + * lead to inconsistencies and even kernel oopses. Rather than
6893 + * implement hacks to work around some of these cache-coherency
6894 + * problems, we prevent branch overlapping, for now. A complete
6895 + * solution will involve proper kernel/VFS support for cache
6896 + * coherency, at which time we could safely remove this
6897 + * branch-overlapping test.
6899 + for (i = 0; i < branches; i++) {
6900 + dent1 = lower_root_info->lower_paths[i].dentry;
6901 + for (j = i + 1; j < branches; j++) {
6902 + dent2 = lower_root_info->lower_paths[j].dentry;
6903 + if (is_branch_overlap(dent1, dent2)) {
6904 + printk(KERN_ERR "unionfs: branches %d and "
6905 + "%d overlap\n", i, j);
6914 + for (i = 0; i < branches; i++)
6915 + if (lower_root_info->lower_paths[i].dentry) {
6916 + dput(lower_root_info->lower_paths[i].dentry);
6917 + /* initialize: can't use unionfs_mntput here */
6918 + mntput(lower_root_info->lower_paths[i].mnt);
6921 + kfree(lower_root_info->lower_paths);
6922 + kfree(UNIONFS_SB(sb)->data);
6925 + * MUST clear the pointers to prevent potential double free if
6926 + * the caller dies later on
6928 + lower_root_info->lower_paths = NULL;
6929 + UNIONFS_SB(sb)->data = NULL;
6935 + * Parse mount options. See the manual page for usage instructions.
6937 + * Returns the dentry object of the lower-level (lower) directory;
6938 + * We want to mount our stackable file system on top of that lower directory.
6940 +static struct unionfs_dentry_info *unionfs_parse_options(
6941 + struct super_block *sb,
6944 + struct unionfs_dentry_info *lower_root_info;
6948 + int dirsfound = 0;
6950 + /* allocate private data area */
6953 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6954 + if (unlikely(!lower_root_info))
6956 + lower_root_info->bstart = -1;
6957 + lower_root_info->bend = -1;
6958 + lower_root_info->bopaque = -1;
6960 + while ((optname = strsep(&options, ",")) != NULL) {
6963 + if (!optname || !*optname)
6966 + optarg = strchr(optname, '=');
6971 + * All of our options take an argument now. Insert ones that
6972 + * don't, above this check.
6975 + printk(KERN_ERR "unionfs: %s requires an argument\n",
6981 + if (!strcmp("dirs", optname)) {
6982 + if (++dirsfound > 1) {
6984 + "unionfs: multiple dirs specified\n");
6988 + err = parse_dirs_option(sb, lower_root_info, optarg);
6996 + "unionfs: unrecognized option '%s'\n", optname);
6999 + if (dirsfound != 1) {
7000 + printk(KERN_ERR "unionfs: dirs option required\n");
7007 + if (lower_root_info && lower_root_info->lower_paths) {
7008 + for (bindex = lower_root_info->bstart;
7009 + bindex >= 0 && bindex <= lower_root_info->bend;
7012 + struct vfsmount *m;
7014 + d = lower_root_info->lower_paths[bindex].dentry;
7015 + m = lower_root_info->lower_paths[bindex].mnt;
7018 + /* initializing: can't use unionfs_mntput here */
7023 + kfree(lower_root_info->lower_paths);
7024 + kfree(lower_root_info);
7026 + kfree(UNIONFS_SB(sb)->data);
7027 + UNIONFS_SB(sb)->data = NULL;
7029 + lower_root_info = ERR_PTR(err);
7031 + return lower_root_info;
7035 + * our custom d_alloc_root work-alike
7037 + * we can't use d_alloc_root if we want to use our own interpose function
7038 + * unchanged, so we simply call our own "fake" d_alloc_root
7040 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7042 + struct dentry *ret = NULL;
7045 + static const struct qstr name = {
7050 + ret = d_alloc(NULL, &name);
7051 + if (likely(ret)) {
7052 + ret->d_op = &unionfs_dops;
7054 + ret->d_parent = ret;
7061 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7062 + * way anyone can have a reference to the superblock at this point in time.
7064 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7068 + struct unionfs_dentry_info *lower_root_info = NULL;
7069 + int bindex, bstart, bend;
7073 + "unionfs: read_super: missing data argument\n");
7078 + /* Allocate superblock private data */
7079 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7080 + if (unlikely(!UNIONFS_SB(sb))) {
7081 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7086 + UNIONFS_SB(sb)->bend = -1;
7087 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7088 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7089 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7091 + lower_root_info = unionfs_parse_options(sb, raw_data);
7092 + if (IS_ERR(lower_root_info)) {
7094 + "unionfs: read_super: error while parsing options "
7095 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7096 + err = PTR_ERR(lower_root_info);
7097 + lower_root_info = NULL;
7100 + if (lower_root_info->bstart == -1) {
7105 + /* set the lower superblock field of upper superblock */
7106 + bstart = lower_root_info->bstart;
7107 + BUG_ON(bstart != 0);
7108 + sbend(sb) = bend = lower_root_info->bend;
7109 + for (bindex = bstart; bindex <= bend; bindex++) {
7110 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7111 + atomic_inc(&d->d_sb->s_active);
7112 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7115 + /* max Bytes is the maximum bytes from highest priority branch */
7116 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7119 + * Our c/m/atime granularity is 1 ns because we may stack on file
7120 + * systems whose granularity is as good. This is important for our
7121 + * time-based cache coherency.
7123 + sb->s_time_gran = 1;
7125 + sb->s_op = &unionfs_sops;
7127 + /* See comment next to the definition of unionfs_d_alloc_root */
7128 + sb->s_root = unionfs_d_alloc_root(sb);
7129 + if (unlikely(!sb->s_root)) {
7134 + /* link the upper and lower dentries */
7135 + sb->s_root->d_fsdata = NULL;
7136 + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7137 + if (unlikely(err))
7140 + /* Set the lower dentries for s_root */
7141 + for (bindex = bstart; bindex <= bend; bindex++) {
7143 + struct vfsmount *m;
7145 + d = lower_root_info->lower_paths[bindex].dentry;
7146 + m = lower_root_info->lower_paths[bindex].mnt;
7148 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7149 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7151 + dbstart(sb->s_root) = bstart;
7152 + dbend(sb->s_root) = bend;
7154 + /* Set the generation number to one, since this is for the mount. */
7155 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7158 + * Call interpose to create the upper level inode. Only
7159 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7161 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7162 + unionfs_unlock_dentry(sb->s_root);
7165 + /* else fall through */
7168 + if (UNIONFS_D(sb->s_root)) {
7169 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7170 + free_dentry_private_data(sb->s_root);
7175 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7176 + for (bindex = lower_root_info->bstart;
7177 + bindex <= lower_root_info->bend; bindex++) {
7179 + struct vfsmount *m;
7181 + d = lower_root_info->lower_paths[bindex].dentry;
7182 + m = lower_root_info->lower_paths[bindex].mnt;
7185 + /* initializing: can't use unionfs_mntput here */
7187 + /* drop refs we took earlier */
7188 + atomic_dec(&d->d_sb->s_active);
7190 + kfree(lower_root_info->lower_paths);
7191 + kfree(lower_root_info);
7192 + lower_root_info = NULL;
7196 + kfree(UNIONFS_SB(sb)->data);
7197 + kfree(UNIONFS_SB(sb));
7198 + sb->s_fs_info = NULL;
7201 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7202 + kfree(lower_root_info->lower_paths);
7203 + kfree(lower_root_info);
7208 +static int unionfs_get_sb(struct file_system_type *fs_type,
7209 + int flags, const char *dev_name,
7210 + void *raw_data, struct vfsmount *mnt)
7213 + err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7215 + UNIONFS_SB(mnt->mnt_sb)->dev_name =
7216 + kstrdup(dev_name, GFP_KERNEL);
7220 +static struct file_system_type unionfs_fs_type = {
7221 + .owner = THIS_MODULE,
7222 + .name = UNIONFS_NAME,
7223 + .get_sb = unionfs_get_sb,
7224 + .kill_sb = generic_shutdown_super,
7225 + .fs_flags = FS_REVAL_DOT,
7228 +static int __init init_unionfs_fs(void)
7232 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7234 + err = unionfs_init_filldir_cache();
7235 + if (unlikely(err))
7237 + err = unionfs_init_inode_cache();
7238 + if (unlikely(err))
7240 + err = unionfs_init_dentry_cache();
7241 + if (unlikely(err))
7243 + err = init_sioq();
7244 + if (unlikely(err))
7246 + err = register_filesystem(&unionfs_fs_type);
7248 + if (unlikely(err)) {
7250 + unionfs_destroy_filldir_cache();
7251 + unionfs_destroy_inode_cache();
7252 + unionfs_destroy_dentry_cache();
7257 +static void __exit exit_unionfs_fs(void)
7260 + unionfs_destroy_filldir_cache();
7261 + unionfs_destroy_inode_cache();
7262 + unionfs_destroy_dentry_cache();
7263 + unregister_filesystem(&unionfs_fs_type);
7264 + pr_info("Completed unionfs module unload\n");
7267 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7268 + " (http://www.fsl.cs.sunysb.edu)");
7269 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7270 + " (http://unionfs.filesystems.org)");
7271 +MODULE_LICENSE("GPL");
7273 +module_init(init_unionfs_fs);
7274 +module_exit(exit_unionfs_fs);
7275 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7276 new file mode 100644
7277 index 0000000..b7d4713
7279 +++ b/fs/unionfs/mmap.c
7282 + * Copyright (c) 2003-2008 Erez Zadok
7283 + * Copyright (c) 2003-2006 Charles P. Wright
7284 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7285 + * Copyright (c) 2005-2006 Junjiro Okajima
7286 + * Copyright (c) 2006 Shaya Potter
7287 + * Copyright (c) 2005 Arun M. Krishnakumar
7288 + * Copyright (c) 2004-2006 David P. Quigley
7289 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7290 + * Copyright (c) 2003 Puja Gupta
7291 + * Copyright (c) 2003 Harikesavan Krishnan
7292 + * Copyright (c) 2003-2008 Stony Brook University
7293 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
7295 + * This program is free software; you can redistribute it and/or modify
7296 + * it under the terms of the GNU General Public License version 2 as
7297 + * published by the Free Software Foundation.
7304 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7305 + * use in unionfs_mmap) checks for the existence of
7306 + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7307 + * be fixed to allow a file system to define vm_ops->fault without any
7308 + * address_space_ops whatsoever.
7310 + * Otherwise, we don't want to use our readpage method at all.
7312 +static int unionfs_readpage(struct file *file, struct page *page)
7318 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7321 + struct file *file, *lower_file;
7322 + struct vm_operations_struct *lower_vm_ops;
7323 + struct vm_area_struct lower_vma;
7326 + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7327 + file = lower_vma.vm_file;
7328 + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7329 + BUG_ON(!lower_vm_ops);
7331 + lower_file = unionfs_lower_file(file);
7332 + BUG_ON(!lower_file);
7334 + * XXX: vm_ops->fault may be called in parallel. Because we have to
7335 + * resort to temporarily changing the vma->vm_file to point to the
7336 + * lower file, a concurrent invocation of unionfs_fault could see a
7337 + * different value. In this workaround, we keep a different copy of
7338 + * the vma structure in our stack, so we never expose a different
7339 + * value of the vma->vm_file called to us, even temporarily. A
7340 + * better fix would be to change the calling semantics of ->fault to
7341 + * take an explicit file pointer.
7343 + lower_vma.vm_file = lower_file;
7344 + err = lower_vm_ops->fault(&lower_vma, vmf);
7349 + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7350 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7351 + * the a_ops vector to be non-NULL.
7353 +struct address_space_operations unionfs_aops = {
7354 + /* empty on purpose */
7358 + * XXX: we need a second, dummy address_space_ops vector, to be used
7359 + * temporarily during unionfs_mmap, because the latter calls
7360 + * generic_file_mmap, which checks if ->readpage exists, else returns
7363 +struct address_space_operations unionfs_dummy_aops = {
7364 + .readpage = unionfs_readpage,
7367 +struct vm_operations_struct unionfs_vm_ops = {
7368 + .fault = unionfs_fault,
7370 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7371 new file mode 100644
7372 index 0000000..06d5374
7374 +++ b/fs/unionfs/rdstate.c
7377 + * Copyright (c) 2003-2008 Erez Zadok
7378 + * Copyright (c) 2003-2006 Charles P. Wright
7379 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7380 + * Copyright (c) 2005-2006 Junjiro Okajima
7381 + * Copyright (c) 2005 Arun M. Krishnakumar
7382 + * Copyright (c) 2004-2006 David P. Quigley
7383 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7384 + * Copyright (c) 2003 Puja Gupta
7385 + * Copyright (c) 2003 Harikesavan Krishnan
7386 + * Copyright (c) 2003-2008 Stony Brook University
7387 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
7389 + * This program is free software; you can redistribute it and/or modify
7390 + * it under the terms of the GNU General Public License version 2 as
7391 + * published by the Free Software Foundation.
7396 +/* This file contains the routines for maintaining readdir state. */
7399 + * There are two structures here, rdstate which is a hash table
7400 + * of the second structure which is a filldir_node.
7404 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7405 + * of them and they shouldn't waste memory. If the node has a small name
7406 + * (as defined by the dentry structure), then we use an inline name to
7407 + * preserve kmalloc space.
7409 +static struct kmem_cache *unionfs_filldir_cachep;
7411 +int unionfs_init_filldir_cache(void)
7413 + unionfs_filldir_cachep =
7414 + kmem_cache_create("unionfs_filldir",
7415 + sizeof(struct filldir_node), 0,
7416 + SLAB_RECLAIM_ACCOUNT, NULL);
7418 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7421 +void unionfs_destroy_filldir_cache(void)
7423 + if (unionfs_filldir_cachep)
7424 + kmem_cache_destroy(unionfs_filldir_cachep);
7428 + * This is a tuning parameter that tells us roughly how big to make the
7429 + * hash table in directory entries per page. This isn't perfect, but
7430 + * at least we get a hash table size that shouldn't be too overloaded.
7431 + * The following averages are based on my home directory.
7432 + * 14.44693 Overall
7433 + * 12.29 Single Page Directories
7434 + * 117.93 Multi-page directories
7436 +#define DENTPAGE 4096
7437 +#define DENTPERONEPAGE 12
7438 +#define DENTPERPAGE 118
7439 +#define MINHASHSIZE 1
7440 +static int guesstimate_hash_size(struct inode *inode)
7442 + struct inode *lower_inode;
7444 + int hashsize = MINHASHSIZE;
7446 + if (UNIONFS_I(inode)->hashsize > 0)
7447 + return UNIONFS_I(inode)->hashsize;
7449 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7450 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7454 + if (i_size_read(lower_inode) == DENTPAGE)
7455 + hashsize += DENTPERONEPAGE;
7457 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7464 +int init_rdstate(struct file *file)
7466 + BUG_ON(sizeof(loff_t) !=
7467 + (sizeof(unsigned int) + sizeof(unsigned int)));
7468 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7470 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7473 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7476 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7478 + struct unionfs_dir_state *rdstate = NULL;
7479 + struct list_head *pos;
7481 + spin_lock(&UNIONFS_I(inode)->rdlock);
7482 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7483 + struct unionfs_dir_state *r =
7484 + list_entry(pos, struct unionfs_dir_state, cache);
7485 + if (fpos == rdstate2offset(r)) {
7486 + UNIONFS_I(inode)->rdcount--;
7487 + list_del(&r->cache);
7492 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7496 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7500 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7501 + struct unionfs_dir_state *rdstate;
7503 + hashsize = guesstimate_hash_size(inode);
7504 + mallocsize += hashsize * sizeof(struct list_head);
7505 + mallocsize = __roundup_pow_of_two(mallocsize);
7507 + /* This should give us about 500 entries anyway. */
7508 + if (mallocsize > PAGE_SIZE)
7509 + mallocsize = PAGE_SIZE;
7511 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7512 + sizeof(struct list_head);
7514 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7515 + if (unlikely(!rdstate))
7518 + spin_lock(&UNIONFS_I(inode)->rdlock);
7519 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7520 + UNIONFS_I(inode)->cookie = 1;
7522 + UNIONFS_I(inode)->cookie++;
7524 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7525 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7526 + rdstate->offset = 1;
7527 + rdstate->access = jiffies;
7528 + rdstate->bindex = bindex;
7529 + rdstate->dirpos = 0;
7530 + rdstate->hashentries = 0;
7531 + rdstate->size = hashsize;
7532 + for (i = 0; i < rdstate->size; i++)
7533 + INIT_LIST_HEAD(&rdstate->list[i]);
7538 +static void free_filldir_node(struct filldir_node *node)
7540 + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7541 + kfree(node->name);
7542 + kmem_cache_free(unionfs_filldir_cachep, node);
7545 +void free_rdstate(struct unionfs_dir_state *state)
7547 + struct filldir_node *tmp;
7550 + for (i = 0; i < state->size; i++) {
7551 + struct list_head *head = &(state->list[i]);
7552 + struct list_head *pos, *n;
7554 + /* traverse the list and deallocate space */
7555 + list_for_each_safe(pos, n, head) {
7556 + tmp = list_entry(pos, struct filldir_node, file_list);
7557 + list_del(&tmp->file_list);
7558 + free_filldir_node(tmp);
7565 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7566 + const char *name, int namelen,
7570 + unsigned int hash;
7571 + struct list_head *head;
7572 + struct list_head *pos;
7573 + struct filldir_node *cursor = NULL;
7576 + BUG_ON(namelen <= 0);
7578 + hash = full_name_hash(name, namelen);
7579 + index = hash % rdstate->size;
7581 + head = &(rdstate->list[index]);
7582 + list_for_each(pos, head) {
7583 + cursor = list_entry(pos, struct filldir_node, file_list);
7585 + if (cursor->namelen == namelen && cursor->hash == hash &&
7586 + !strncmp(cursor->name, name, namelen)) {
7588 + * a duplicate exists, and hence no need to create
7589 + * entry to the list
7594 + * if a duplicate is found in this branch, and is
7595 + * not due to the caller looking for an entry to
7596 + * whiteout, then the file system may be corrupted.
7598 + if (unlikely(!is_whiteout &&
7599 + cursor->bindex == rdstate->bindex))
7600 + printk(KERN_ERR "unionfs: filldir: possible "
7601 + "I/O error: a file is duplicated "
7602 + "in the same branch %d: %s\n",
7603 + rdstate->bindex, cursor->name);
7614 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7615 + int namelen, int bindex, int whiteout)
7617 + struct filldir_node *new;
7618 + unsigned int hash;
7621 + struct list_head *head;
7623 + BUG_ON(namelen <= 0);
7625 + hash = full_name_hash(name, namelen);
7626 + index = hash % rdstate->size;
7627 + head = &(rdstate->list[index]);
7629 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7630 + if (unlikely(!new)) {
7635 + INIT_LIST_HEAD(&new->file_list);
7636 + new->namelen = namelen;
7638 + new->bindex = bindex;
7639 + new->whiteout = whiteout;
7641 + if (namelen < DNAME_INLINE_LEN_MIN) {
7642 + new->name = new->iname;
7644 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7645 + if (unlikely(!new->name)) {
7646 + kmem_cache_free(unionfs_filldir_cachep, new);
7652 + memcpy(new->name, name, namelen);
7653 + new->name[namelen] = '\0';
7655 + rdstate->hashentries++;
7657 + list_add(&(new->file_list), head);
7661 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7662 new file mode 100644
7663 index 0000000..800d9ee
7665 +++ b/fs/unionfs/rename.c
7668 + * Copyright (c) 2003-2008 Erez Zadok
7669 + * Copyright (c) 2003-2006 Charles P. Wright
7670 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7671 + * Copyright (c) 2005-2006 Junjiro Okajima
7672 + * Copyright (c) 2005 Arun M. Krishnakumar
7673 + * Copyright (c) 2004-2006 David P. Quigley
7674 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7675 + * Copyright (c) 2003 Puja Gupta
7676 + * Copyright (c) 2003 Harikesavan Krishnan
7677 + * Copyright (c) 2003-2008 Stony Brook University
7678 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
7680 + * This program is free software; you can redistribute it and/or modify
7681 + * it under the terms of the GNU General Public License version 2 as
7682 + * published by the Free Software Foundation.
7688 + * This is a helper function for rename, used when rename ends up with hosed
7689 + * over dentries and we need to revert.
7691 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7692 + struct dentry *parent, int bindex)
7694 + struct dentry *lower_dentry;
7695 + struct dentry *lower_parent;
7698 + verify_locked(dentry);
7700 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7702 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7704 + lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
7705 + dentry->d_name.len);
7706 + if (IS_ERR(lower_dentry)) {
7707 + err = PTR_ERR(lower_dentry);
7711 + dput(unionfs_lower_dentry_idx(dentry, bindex));
7712 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7713 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7715 + if (!lower_dentry->d_inode) {
7716 + dput(lower_dentry);
7717 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7719 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7720 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7721 + igrab(lower_dentry->d_inode));
7728 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7729 + struct dentry *old_parent,
7730 + struct inode *new_dir, struct dentry *new_dentry,
7731 + struct dentry *new_parent,
7735 + struct dentry *lower_old_dentry;
7736 + struct dentry *lower_new_dentry;
7737 + struct dentry *lower_old_dir_dentry;
7738 + struct dentry *lower_new_dir_dentry;
7739 + struct dentry *trap;
7741 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7742 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7744 + if (!lower_new_dentry) {
7745 + lower_new_dentry =
7746 + create_parents(new_parent->d_inode,
7747 + new_dentry, new_dentry->d_name.name,
7749 + if (IS_ERR(lower_new_dentry)) {
7750 + err = PTR_ERR(lower_new_dentry);
7751 + if (IS_COPYUP_ERR(err))
7753 + printk(KERN_ERR "unionfs: error creating directory "
7754 + "tree for rename, bindex=%d err=%d\n",
7760 + /* check for and remove whiteout, if any */
7761 + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7762 + if (err > 0) /* ignore if whiteout found and successfully removed */
7767 + /* check of old_dentry branch is writable */
7768 + err = is_robranch_super(old_dentry->d_sb, bindex);
7772 + dget(lower_old_dentry);
7773 + dget(lower_new_dentry);
7774 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7775 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7777 + /* see Documentation/filesystems/unionfs/issues.txt */
7779 + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7780 + /* source should not be ancenstor of target */
7781 + if (trap == lower_old_dentry) {
7783 + goto out_err_unlock;
7785 + /* target should not be ancenstor of source */
7786 + if (trap == lower_new_dentry) {
7788 + goto out_err_unlock;
7790 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7791 + lower_new_dir_dentry->d_inode, lower_new_dentry);
7794 + /* update parent dir times */
7795 + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7796 + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7798 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7801 + dput(lower_old_dir_dentry);
7802 + dput(lower_new_dir_dentry);
7803 + dput(lower_old_dentry);
7804 + dput(lower_new_dentry);
7808 + /* Fixup the new_dentry. */
7809 + if (bindex < dbstart(new_dentry))
7810 + dbstart(new_dentry) = bindex;
7811 + else if (bindex > dbend(new_dentry))
7812 + dbend(new_dentry) = bindex;
7819 + * Main rename code. This is sufficiently complex, that it's documented in
7820 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7821 + * __unionfs_rename() above to perform some of the work.
7823 +static int do_unionfs_rename(struct inode *old_dir,
7824 + struct dentry *old_dentry,
7825 + struct dentry *old_parent,
7826 + struct inode *new_dir,
7827 + struct dentry *new_dentry,
7828 + struct dentry *new_parent)
7831 + int bindex, bwh_old;
7832 + int old_bstart, old_bend;
7833 + int new_bstart, new_bend;
7834 + int do_copyup = -1;
7835 + int local_err = 0;
7839 + old_bstart = dbstart(old_dentry);
7840 + bwh_old = old_bstart;
7841 + old_bend = dbend(old_dentry);
7843 + new_bstart = dbstart(new_dentry);
7844 + new_bend = dbend(new_dentry);
7846 + /* Rename source to destination. */
7847 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7848 + new_dir, new_dentry, new_parent,
7851 + if (!IS_COPYUP_ERR(err))
7853 + do_copyup = old_bstart - 1;
7859 + * Unlink all instances of destination that exist to the left of
7860 + * bstart of source. On error, revert back, goto out.
7862 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7863 + struct dentry *unlink_dentry;
7864 + struct dentry *unlink_dir_dentry;
7866 + BUG_ON(bindex < 0);
7867 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7868 + if (!unlink_dentry)
7871 + unlink_dir_dentry = lock_parent(unlink_dentry);
7872 + err = is_robranch_super(old_dir->i_sb, bindex);
7874 + err = vfs_unlink(unlink_dir_dentry->d_inode,
7877 + fsstack_copy_attr_times(new_parent->d_inode,
7878 + unlink_dir_dentry->d_inode);
7879 + /* propagate number of hard-links */
7880 + new_parent->d_inode->i_nlink =
7881 + unionfs_get_nlinks(new_parent->d_inode);
7883 + unlock_dir(unlink_dir_dentry);
7885 + if (bindex != new_bstart) {
7886 + dput(unlink_dentry);
7887 + unionfs_set_lower_dentry_idx(new_dentry,
7890 + } else if (IS_COPYUP_ERR(err)) {
7891 + do_copyup = bindex - 1;
7892 + } else if (revert) {
7897 + if (do_copyup != -1) {
7898 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7900 + * copyup the file into some left directory, so that
7901 + * you can rename it
7903 + err = copyup_dentry(old_parent->d_inode,
7904 + old_dentry, old_bstart, bindex,
7905 + old_dentry->d_name.name,
7906 + old_dentry->d_name.len, NULL,
7907 + i_size_read(old_dentry->d_inode));
7908 + /* if copyup failed, try next branch to the left */
7912 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7913 + new_dir, new_dentry, new_parent,
7919 + /* make it opaque */
7920 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7921 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7927 + * Create whiteout for source, only if:
7928 + * (1) There is more than one underlying instance of source.
7929 + * (2) We did a copy_up
7931 + if ((old_bstart != old_bend) || (do_copyup != -1)) {
7932 + if (bwh_old < 0) {
7933 + printk(KERN_ERR "unionfs: rename error (bwh_old=%d)\n",
7938 + err = create_whiteout(old_dentry, bwh_old);
7940 + /* can't fix anything now, so we exit with -EIO */
7941 + printk(KERN_ERR "unionfs: can't create a whiteout for "
7942 + "%s in rename!\n", old_dentry->d_name.name);
7951 + /* Do revert here. */
7952 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7955 + printk(KERN_ERR "unionfs: revert failed in rename: "
7956 + "the new refresh failed\n");
7960 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7963 + printk(KERN_ERR "unionfs: revert failed in rename: "
7964 + "the old refresh failed\n");
7969 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7970 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7971 + printk(KERN_ERR "unionfs: revert failed in rename: "
7972 + "the object disappeared from under us!\n");
7977 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
7978 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
7979 + printk(KERN_ERR "unionfs: revert failed in rename: "
7980 + "the object was created underneath us!\n");
7985 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
7986 + old_dir, old_dentry, old_parent,
7989 + /* If we can't fix it, then we cop-out with -EIO. */
7991 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
7995 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7999 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
8011 + * We can't copyup a directory, because it may involve huge numbers of
8012 + * children, etc. Doing that in the kernel would be bad, so instead we
8013 + * return EXDEV to the user-space utility that caused this, and let the
8014 + * user-space recurse and ask us to copy up each file separately.
8016 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
8020 + err = check_empty(dentry, parent, NULL);
8021 + if (err == -ENOTEMPTY) {
8022 + if (is_robranch(dentry))
8028 + bstart = dbstart(dentry);
8029 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8032 + dbstart(dentry) = bstart + 1;
8033 + err = check_empty(dentry, parent, NULL);
8034 + dbstart(dentry) = bstart;
8035 + if (err == -ENOTEMPTY)
8041 + * The locking rules in unionfs_rename are complex. We could use a simpler
8042 + * superblock-level name-space lock for renames and copy-ups.
8044 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8045 + struct inode *new_dir, struct dentry *new_dentry)
8048 + struct dentry *wh_dentry;
8049 + struct dentry *old_parent, *new_parent;
8052 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
8053 + old_parent = dget_parent(old_dentry);
8054 + new_parent = dget_parent(new_dentry);
8055 + /* un/lock parent dentries only if they differ from old/new_dentry */
8056 + if (old_parent != old_dentry &&
8057 + old_parent != new_dentry)
8058 + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
8059 + if (new_parent != old_dentry &&
8060 + new_parent != new_dentry &&
8061 + new_parent != old_parent)
8062 + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
8063 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8065 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
8070 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
8071 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
8078 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8079 + err = unionfs_partial_lookup(old_dentry, old_parent);
8081 + err = may_rename_dir(old_dentry, old_parent);
8086 + err = unionfs_partial_lookup(new_dentry, new_parent);
8091 + * if new_dentry is already lower because of whiteout,
8092 + * simply override it even if the whited-out dir is not empty.
8094 + wh_dentry = find_first_whiteout(new_dentry);
8095 + if (!IS_ERR(wh_dentry)) {
8097 + } else if (new_dentry->d_inode) {
8098 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8099 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8100 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8101 + -ENOTDIR : -EISDIR;
8105 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8106 + struct unionfs_dir_state *namelist = NULL;
8107 + /* check if this unionfs directory is empty or not */
8108 + err = check_empty(new_dentry, new_parent, &namelist);
8112 + if (!is_robranch(new_dentry))
8113 + err = delete_whiteouts(new_dentry,
8114 + dbstart(new_dentry),
8117 + free_rdstate(namelist);
8124 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8125 + new_dir, new_dentry, new_parent);
8130 + * force re-lookup since the dir on ro branch is not renamed, and
8131 + * lower dentries still indicate the un-renamed ones.
8133 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8134 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8136 + unionfs_postcopyup_release(old_dentry);
8137 + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8138 + unionfs_postcopyup_release(new_dentry);
8139 + unionfs_postcopyup_setmnt(new_dentry);
8140 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8142 + * If we get here, it means that no copyup was
8143 + * needed, and that a file by the old name already
8144 + * existing on the destination branch; that file got
8145 + * renamed earlier in this function, so all we need
8146 + * to do here is set the lower inode.
8148 + struct inode *inode;
8149 + inode = unionfs_lower_inode(old_dentry->d_inode);
8151 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8152 + dbstart(new_dentry),
8156 + /* if all of this renaming succeeded, update our times */
8157 + unionfs_copy_attr_times(old_dentry->d_inode);
8158 + unionfs_copy_attr_times(new_dentry->d_inode);
8159 + unionfs_check_inode(old_dir);
8160 + unionfs_check_inode(new_dir);
8161 + unionfs_check_dentry(old_dentry);
8162 + unionfs_check_dentry(new_dentry);
8165 + if (err) /* clear the new_dentry stuff created */
8166 + d_drop(new_dentry);
8168 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8169 + if (new_parent != old_dentry &&
8170 + new_parent != new_dentry &&
8171 + new_parent != old_parent)
8172 + unionfs_unlock_dentry(new_parent);
8173 + if (old_parent != old_dentry &&
8174 + old_parent != new_dentry)
8175 + unionfs_unlock_dentry(old_parent);
8178 + unionfs_read_unlock(old_dentry->d_sb);
8182 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8183 new file mode 100644
8184 index 0000000..dd45e39
8186 +++ b/fs/unionfs/sioq.c
8189 + * Copyright (c) 2006-2008 Erez Zadok
8190 + * Copyright (c) 2006 Charles P. Wright
8191 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8192 + * Copyright (c) 2006 Junjiro Okajima
8193 + * Copyright (c) 2006 David P. Quigley
8194 + * Copyright (c) 2006-2008 Stony Brook University
8195 + * Copyright (c) 2006-2008 The Research Foundation of SUNY
8197 + * This program is free software; you can redistribute it and/or modify
8198 + * it under the terms of the GNU General Public License version 2 as
8199 + * published by the Free Software Foundation.
8205 + * Super-user IO work Queue - sometimes we need to perform actions which
8206 + * would fail due to the unix permissions on the parent directory (e.g.,
8207 + * rmdir a directory which appears empty, but in reality contains
8211 +static struct workqueue_struct *superio_workqueue;
8213 +int __init init_sioq(void)
8217 + superio_workqueue = create_workqueue("unionfs_siod");
8218 + if (!IS_ERR(superio_workqueue))
8221 + err = PTR_ERR(superio_workqueue);
8222 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8223 + superio_workqueue = NULL;
8227 +void stop_sioq(void)
8229 + if (superio_workqueue)
8230 + destroy_workqueue(superio_workqueue);
8233 +void run_sioq(work_func_t func, struct sioq_args *args)
8235 + INIT_WORK(&args->work, func);
8237 + init_completion(&args->comp);
8238 + while (!queue_work(superio_workqueue, &args->work)) {
8239 + /* TODO: do accounting if needed */
8242 + wait_for_completion(&args->comp);
8245 +void __unionfs_create(struct work_struct *work)
8247 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8248 + struct create_args *c = &args->create;
8250 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8251 + complete(&args->comp);
8254 +void __unionfs_mkdir(struct work_struct *work)
8256 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8257 + struct mkdir_args *m = &args->mkdir;
8259 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8260 + complete(&args->comp);
8263 +void __unionfs_mknod(struct work_struct *work)
8265 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8266 + struct mknod_args *m = &args->mknod;
8268 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8269 + complete(&args->comp);
8272 +void __unionfs_symlink(struct work_struct *work)
8274 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8275 + struct symlink_args *s = &args->symlink;
8277 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8278 + complete(&args->comp);
8281 +void __unionfs_unlink(struct work_struct *work)
8283 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8284 + struct unlink_args *u = &args->unlink;
8286 + args->err = vfs_unlink(u->parent, u->dentry);
8287 + complete(&args->comp);
8289 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8290 new file mode 100644
8291 index 0000000..679a0df
8293 +++ b/fs/unionfs/sioq.h
8296 + * Copyright (c) 2006-2008 Erez Zadok
8297 + * Copyright (c) 2006 Charles P. Wright
8298 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8299 + * Copyright (c) 2006 Junjiro Okajima
8300 + * Copyright (c) 2006 David P. Quigley
8301 + * Copyright (c) 2006-2008 Stony Brook University
8302 + * Copyright (c) 2006-2008 The Research Foundation of SUNY
8304 + * This program is free software; you can redistribute it and/or modify
8305 + * it under the terms of the GNU General Public License version 2 as
8306 + * published by the Free Software Foundation.
8312 +struct deletewh_args {
8313 + struct unionfs_dir_state *namelist;
8314 + struct dentry *dentry;
8318 +struct is_opaque_args {
8319 + struct dentry *dentry;
8322 +struct create_args {
8323 + struct inode *parent;
8324 + struct dentry *dentry;
8326 + struct nameidata *nd;
8329 +struct mkdir_args {
8330 + struct inode *parent;
8331 + struct dentry *dentry;
8335 +struct mknod_args {
8336 + struct inode *parent;
8337 + struct dentry *dentry;
8342 +struct symlink_args {
8343 + struct inode *parent;
8344 + struct dentry *dentry;
8348 +struct unlink_args {
8349 + struct inode *parent;
8350 + struct dentry *dentry;
8355 + struct completion comp;
8356 + struct work_struct work;
8361 + struct deletewh_args deletewh;
8362 + struct is_opaque_args is_opaque;
8363 + struct create_args create;
8364 + struct mkdir_args mkdir;
8365 + struct mknod_args mknod;
8366 + struct symlink_args symlink;
8367 + struct unlink_args unlink;
8371 +/* Extern definitions for SIOQ functions */
8372 +extern int __init init_sioq(void);
8373 +extern void stop_sioq(void);
8374 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8376 +/* Extern definitions for our privilege escalation helpers */
8377 +extern void __unionfs_create(struct work_struct *work);
8378 +extern void __unionfs_mkdir(struct work_struct *work);
8379 +extern void __unionfs_mknod(struct work_struct *work);
8380 +extern void __unionfs_symlink(struct work_struct *work);
8381 +extern void __unionfs_unlink(struct work_struct *work);
8382 +extern void __delete_whiteouts(struct work_struct *work);
8383 +extern void __is_opaque_dir(struct work_struct *work);
8385 +#endif /* not _SIOQ_H */
8386 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8387 new file mode 100644
8388 index 0000000..8747d20
8390 +++ b/fs/unionfs/subr.c
8393 + * Copyright (c) 2003-2008 Erez Zadok
8394 + * Copyright (c) 2003-2006 Charles P. Wright
8395 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8396 + * Copyright (c) 2005-2006 Junjiro Okajima
8397 + * Copyright (c) 2005 Arun M. Krishnakumar
8398 + * Copyright (c) 2004-2006 David P. Quigley
8399 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8400 + * Copyright (c) 2003 Puja Gupta
8401 + * Copyright (c) 2003 Harikesavan Krishnan
8402 + * Copyright (c) 2003-2008 Stony Brook University
8403 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
8405 + * This program is free software; you can redistribute it and/or modify
8406 + * it under the terms of the GNU General Public License version 2 as
8407 + * published by the Free Software Foundation.
8413 + * returns the right n_link value based on the inode type
8415 +int unionfs_get_nlinks(const struct inode *inode)
8417 + /* don't bother to do all the work since we're unlinked */
8418 + if (inode->i_nlink == 0)
8421 + if (!S_ISDIR(inode->i_mode))
8422 + return unionfs_lower_inode(inode)->i_nlink;
8425 + * For directories, we return 1. The only place that could cares
8426 + * about links is readdir, and there's d_type there so even that
8432 +/* copy a/m/ctime from the lower branch with the newest times */
8433 +void unionfs_copy_attr_times(struct inode *upper)
8436 + struct inode *lower;
8440 + if (ibstart(upper) < 0) {
8441 +#ifdef CONFIG_UNION_FS_DEBUG
8442 + WARN_ON(ibstart(upper) < 0);
8443 +#endif /* CONFIG_UNION_FS_DEBUG */
8446 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8447 + lower = unionfs_lower_inode_idx(upper, bindex);
8449 + continue; /* not all lower dir objects may exist */
8450 + if (unlikely(timespec_compare(&upper->i_mtime,
8451 + &lower->i_mtime) < 0))
8452 + upper->i_mtime = lower->i_mtime;
8453 + if (unlikely(timespec_compare(&upper->i_ctime,
8454 + &lower->i_ctime) < 0))
8455 + upper->i_ctime = lower->i_ctime;
8456 + if (unlikely(timespec_compare(&upper->i_atime,
8457 + &lower->i_atime) < 0))
8458 + upper->i_atime = lower->i_atime;
8463 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8464 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8465 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8466 + * important if the lower inode is a directory type)
8468 +void unionfs_copy_attr_all(struct inode *dest,
8469 + const struct inode *src)
8471 + dest->i_mode = src->i_mode;
8472 + dest->i_uid = src->i_uid;
8473 + dest->i_gid = src->i_gid;
8474 + dest->i_rdev = src->i_rdev;
8476 + unionfs_copy_attr_times(dest);
8478 + dest->i_blkbits = src->i_blkbits;
8479 + dest->i_flags = src->i_flags;
8482 + * Update the nlinks AFTER updating the above fields, because the
8483 + * get_links callback may depend on them.
8485 + dest->i_nlink = unionfs_get_nlinks(dest);
8487 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8488 new file mode 100644
8489 index 0000000..8115079
8491 +++ b/fs/unionfs/super.c
8494 + * Copyright (c) 2003-2008 Erez Zadok
8495 + * Copyright (c) 2003-2006 Charles P. Wright
8496 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8497 + * Copyright (c) 2005-2006 Junjiro Okajima
8498 + * Copyright (c) 2005 Arun M. Krishnakumar
8499 + * Copyright (c) 2004-2006 David P. Quigley
8500 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8501 + * Copyright (c) 2003 Puja Gupta
8502 + * Copyright (c) 2003 Harikesavan Krishnan
8503 + * Copyright (c) 2003-2008 Stony Brook University
8504 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
8506 + * This program is free software; you can redistribute it and/or modify
8507 + * it under the terms of the GNU General Public License version 2 as
8508 + * published by the Free Software Foundation.
8514 + * The inode cache is used with alloc_inode for both our inode info and the
8517 +static struct kmem_cache *unionfs_inode_cachep;
8519 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8522 + struct unionfs_inode_info *info;
8523 + struct inode *inode;
8525 + inode = iget_locked(sb, ino);
8527 + return ERR_PTR(-ENOMEM);
8528 + if (!(inode->i_state & I_NEW))
8531 + info = UNIONFS_I(inode);
8532 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8533 + info->bstart = -1;
8535 + atomic_set(&info->generation,
8536 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8537 + spin_lock_init(&info->rdlock);
8538 + info->rdcount = 1;
8539 + info->hashsize = -1;
8540 + INIT_LIST_HEAD(&info->readdircache);
8542 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8543 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8544 + if (unlikely(!info->lower_inodes)) {
8545 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8546 + "lower-pointer array!\n");
8547 + iget_failed(inode);
8548 + return ERR_PTR(-ENOMEM);
8551 + inode->i_version++;
8552 + inode->i_op = &unionfs_main_iops;
8553 + inode->i_fop = &unionfs_main_fops;
8555 + inode->i_mapping->a_ops = &unionfs_aops;
8558 + * reset times so unionfs_copy_attr_all can keep out time invariants
8559 + * right (upper inode time being the max of all lower ones).
8561 + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8562 + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8563 + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8564 + unlock_new_inode(inode);
8569 + * we now define delete_inode, because there are two VFS paths that may
8570 + * destroy an inode: one of them calls clear inode before doing everything
8571 + * else that's needed, and the other is fine. This way we truncate the inode
8572 + * size (and its pages) and then clear our own inode, which will do an iput
8573 + * on our and the lower inode.
8575 + * No need to lock sb info's rwsem.
8577 +static void unionfs_delete_inode(struct inode *inode)
8579 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8580 + spin_lock(&inode->i_lock);
8582 + i_size_write(inode, 0); /* every f/s seems to do that */
8583 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8584 + spin_unlock(&inode->i_lock);
8587 + if (inode->i_data.nrpages)
8588 + truncate_inode_pages(&inode->i_data, 0);
8590 + clear_inode(inode);
8594 + * final actions when unmounting a file system
8596 + * No need to lock rwsem.
8598 +static void unionfs_put_super(struct super_block *sb)
8600 + int bindex, bstart, bend;
8601 + struct unionfs_sb_info *spd;
8604 + spd = UNIONFS_SB(sb);
8608 + bstart = sbstart(sb);
8611 + /* Make sure we have no leaks of branchget/branchput. */
8612 + for (bindex = bstart; bindex <= bend; bindex++)
8613 + if (unlikely(branch_count(sb, bindex) != 0)) {
8615 + "unionfs: branch %d has %d references left!\n",
8616 + bindex, branch_count(sb, bindex));
8619 + WARN_ON(leaks != 0);
8621 + /* decrement lower super references */
8622 + for (bindex = bstart; bindex <= bend; bindex++) {
8623 + struct super_block *s;
8624 + s = unionfs_lower_super_idx(sb, bindex);
8625 + unionfs_set_lower_super_idx(sb, bindex, NULL);
8626 + atomic_dec(&s->s_active);
8629 + kfree(spd->dev_name);
8632 + sb->s_fs_info = NULL;
8636 + * Since people use this to answer the "How big of a file can I write?"
8637 + * question, we report the size of the highest priority branch as the size of
8640 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8643 + struct super_block *sb;
8644 + struct dentry *lower_dentry;
8645 + struct dentry *parent;
8648 + sb = dentry->d_sb;
8650 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8651 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8652 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8654 + valid = __unionfs_d_revalidate(dentry, parent, false);
8655 + if (unlikely(!valid)) {
8659 + unionfs_check_dentry(dentry);
8661 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8662 + err = vfs_statfs(lower_dentry, buf);
8664 + /* set return buf to our f/s to avoid confusing user-level utils */
8665 + buf->f_type = UNIONFS_SUPER_MAGIC;
8667 + * Our maximum file name can is shorter by a few bytes because every
8668 + * file name could potentially be whited-out.
8670 + * XXX: this restriction goes away with ODF.
8672 + unionfs_set_max_namelen(&buf->f_namelen);
8675 + * reset two fields to avoid confusing user-land.
8676 + * XXX: is this still necessary?
8678 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8679 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8682 + unionfs_check_dentry(dentry);
8683 + unionfs_unlock_dentry(dentry);
8684 + unionfs_unlock_parent(dentry, parent);
8685 + unionfs_read_unlock(sb);
8689 +/* handle mode changing during remount */
8690 +static noinline_for_stack int do_remount_mode_option(
8693 + struct unionfs_data *new_data,
8694 + struct path *new_lower_paths)
8696 + int err = -EINVAL;
8698 + char *modename = strchr(optarg, '=');
8699 + struct nameidata nd;
8701 + /* by now, optarg contains the branch name */
8704 + "unionfs: no branch specified for mode change\n");
8708 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8712 + *modename++ = '\0';
8713 + err = parse_branch_mode(modename, &perms);
8715 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8716 + modename, optarg);
8721 + * Find matching branch index. For now, this assumes that nothing
8722 + * has been mounted on top of this Unionfs stack. Once we have /odf
8723 + * and cache-coherency resolved, we'll address the branch-path
8726 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8728 + printk(KERN_ERR "unionfs: error accessing "
8729 + "lower directory \"%s\" (error %d)\n",
8733 + for (idx = 0; idx < cur_branches; idx++)
8734 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8735 + nd.path.dentry == new_lower_paths[idx].dentry)
8737 + path_put(&nd.path); /* no longer needed */
8738 + if (idx == cur_branches) {
8739 + err = -ENOENT; /* err may have been reset above */
8740 + printk(KERN_ERR "unionfs: branch \"%s\" "
8741 + "not found\n", optarg);
8744 + /* check/change mode for existing branch */
8745 + /* we don't warn if perms==branchperms */
8746 + new_data[idx].branchperms = perms;
8752 +/* handle branch deletion during remount */
8753 +static noinline_for_stack int do_remount_del_option(
8754 + char *optarg, int cur_branches,
8755 + struct unionfs_data *new_data,
8756 + struct path *new_lower_paths)
8758 + int err = -EINVAL;
8760 + struct nameidata nd;
8762 + /* optarg contains the branch name to delete */
8765 + * Find matching branch index. For now, this assumes that nothing
8766 + * has been mounted on top of this Unionfs stack. Once we have /odf
8767 + * and cache-coherency resolved, we'll address the branch-path
8770 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8772 + printk(KERN_ERR "unionfs: error accessing "
8773 + "lower directory \"%s\" (error %d)\n",
8777 + for (idx = 0; idx < cur_branches; idx++)
8778 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8779 + nd.path.dentry == new_lower_paths[idx].dentry)
8781 + path_put(&nd.path); /* no longer needed */
8782 + if (idx == cur_branches) {
8783 + printk(KERN_ERR "unionfs: branch \"%s\" "
8784 + "not found\n", optarg);
8788 + /* check if there are any open files on the branch to be deleted */
8789 + if (atomic_read(&new_data[idx].open_files) > 0) {
8795 + * Now we have to delete the branch. First, release any handles it
8796 + * has. Then, move the remaining array indexes past "idx" in
8797 + * new_data and new_lower_paths one to the left. Finally, adjust
8800 + path_put(&new_lower_paths[idx]);
8802 + if (idx < cur_branches - 1) {
8803 + /* if idx==cur_branches-1, we delete last branch: easy */
8804 + memmove(&new_data[idx], &new_data[idx+1],
8805 + (cur_branches - 1 - idx) *
8806 + sizeof(struct unionfs_data));
8807 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8808 + (cur_branches - 1 - idx) * sizeof(struct path));
8816 +/* handle branch insertion during remount */
8817 +static noinline_for_stack int do_remount_add_option(
8818 + char *optarg, int cur_branches,
8819 + struct unionfs_data *new_data,
8820 + struct path *new_lower_paths,
8821 + int *high_branch_id)
8823 + int err = -EINVAL;
8825 + int idx = 0; /* default: insert at beginning */
8826 + char *new_branch , *modename = NULL;
8827 + struct nameidata nd;
8830 + * optarg can be of several forms:
8832 + * /bar:/foo insert /foo before /bar
8833 + * /bar:/foo=ro insert /foo in ro mode before /bar
8834 + * /foo insert /foo in the beginning (prepend)
8835 + * :/foo insert /foo at the end (append)
8837 + if (*optarg == ':') { /* append? */
8838 + new_branch = optarg + 1; /* skip ':' */
8839 + idx = cur_branches;
8840 + goto found_insertion_point;
8842 + new_branch = strchr(optarg, ':');
8843 + if (!new_branch) { /* prepend? */
8844 + new_branch = optarg;
8845 + goto found_insertion_point;
8847 + *new_branch++ = '\0'; /* holds path+mode of new branch */
8850 + * Find matching branch index. For now, this assumes that nothing
8851 + * has been mounted on top of this Unionfs stack. Once we have /odf
8852 + * and cache-coherency resolved, we'll address the branch-path
8855 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8857 + printk(KERN_ERR "unionfs: error accessing "
8858 + "lower directory \"%s\" (error %d)\n",
8862 + for (idx = 0; idx < cur_branches; idx++)
8863 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8864 + nd.path.dentry == new_lower_paths[idx].dentry)
8866 + path_put(&nd.path); /* no longer needed */
8867 + if (idx == cur_branches) {
8868 + printk(KERN_ERR "unionfs: branch \"%s\" "
8869 + "not found\n", optarg);
8875 + * At this point idx will hold the index where the new branch should
8876 + * be inserted before.
8878 +found_insertion_point:
8879 + /* find the mode for the new branch */
8881 + modename = strchr(new_branch, '=');
8883 + *modename++ = '\0';
8884 + if (!new_branch || !*new_branch) {
8885 + printk(KERN_ERR "unionfs: null new branch\n");
8889 + err = parse_branch_mode(modename, &perms);
8891 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8892 + "branch \"%s\"\n", modename, new_branch);
8895 + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8897 + printk(KERN_ERR "unionfs: error accessing "
8898 + "lower directory \"%s\" (error %d)\n",
8903 + * It's probably safe to check_mode the new branch to insert. Note:
8904 + * we don't allow inserting branches which are unionfs's by
8905 + * themselves (check_branch returns EINVAL in that case). This is
8906 + * because this code base doesn't support stacking unionfs: the ODF
8907 + * code base supports that correctly.
8909 + err = check_branch(&nd);
8911 + printk(KERN_ERR "unionfs: lower directory "
8912 + "\"%s\" is not a valid branch\n", optarg);
8913 + path_put(&nd.path);
8918 + * Now we have to insert the new branch. But first, move the bits
8919 + * to make space for the new branch, if needed. Finally, adjust
8921 + * We don't release nd here; it's kept until umount/remount.
8923 + if (idx < cur_branches) {
8924 + /* if idx==cur_branches, we append: easy */
8925 + memmove(&new_data[idx+1], &new_data[idx],
8926 + (cur_branches - idx) * sizeof(struct unionfs_data));
8927 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8928 + (cur_branches - idx) * sizeof(struct path));
8930 + new_lower_paths[idx].dentry = nd.path.dentry;
8931 + new_lower_paths[idx].mnt = nd.path.mnt;
8933 + new_data[idx].sb = nd.path.dentry->d_sb;
8934 + atomic_set(&new_data[idx].open_files, 0);
8935 + new_data[idx].branchperms = perms;
8936 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8945 + * Support branch management options on remount.
8947 + * See Documentation/filesystems/unionfs/ for details.
8949 + * @flags: numeric mount options
8950 + * @options: mount options string
8952 + * This function can rearrange a mounted union dynamically, adding and
8953 + * removing branches, including changing branch modes. Clearly this has to
8954 + * be done safely and atomically. Luckily, the VFS already calls this
8955 + * function with lock_super(sb) and lock_kernel() held, preventing
8956 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8957 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8958 + * to purge dentries/inodes from our superblock, and also called
8959 + * fsync_super(sb) to purge any dirty pages. So we're good.
8961 + * XXX: however, our remount code may also need to invalidate mapped pages
8962 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8963 + * branches. This has to wait for proper mmap and cache coherency support
8967 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8972 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
8974 + int cur_branches = 0; /* no. of current branches */
8975 + int new_branches = 0; /* no. of branches actually left in the end */
8976 + int add_branches; /* est. no. of branches to add */
8977 + int del_branches; /* est. no. of branches to del */
8978 + int max_branches; /* max possible no. of branches */
8979 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
8980 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
8981 + struct inode **new_lower_inodes = NULL;
8982 + int new_high_branch_id; /* new high branch ID */
8983 + int size; /* memory allocation size, temp var */
8984 + int old_ibstart, old_ibend;
8986 + unionfs_write_lock(sb);
8989 + * The VFS will take care of "ro" and "rw" flags, and we can safely
8990 + * ignore MS_SILENT, but anything else left over is an error. So we
8991 + * need to check if any other flags may have been passed (none are
8992 + * allowed/supported as of now).
8994 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8996 + "unionfs: remount flags 0x%x unsupported\n", *flags);
9002 + * If 'options' is NULL, it's probably because the user just changed
9003 + * the union to a "ro" or "rw" and the VFS took care of it. So
9004 + * nothing to do and we're done.
9006 + if (!options || options[0] == '\0')
9010 + * Find out how many branches we will have in the end, counting
9011 + * "add" and "del" commands. Copy the "options" string because
9012 + * strsep modifies the string and we need it later.
9014 + tmp_to_free = kstrdup(options, GFP_KERNEL);
9015 + optionstmp = tmp_to_free;
9016 + if (unlikely(!optionstmp)) {
9020 + cur_branches = sbmax(sb); /* current no. branches */
9021 + new_branches = sbmax(sb);
9024 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9025 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
9028 + if (!optname || !*optname)
9031 + optarg = strchr(optname, '=');
9035 + if (!strcmp("add", optname))
9037 + else if (!strcmp("del", optname))
9040 + kfree(tmp_to_free);
9041 + /* after all changes, will we have at least one branch left? */
9042 + if ((new_branches + add_branches - del_branches) < 1) {
9044 + "unionfs: no branches left after remount\n");
9050 + * Since we haven't actually parsed all the add/del options, nor
9051 + * have we checked them for errors, we don't know for sure how many
9052 + * branches we will have after all changes have taken place. In
9053 + * fact, the total number of branches left could be less than what
9054 + * we have now. So we need to allocate space for a temporary
9055 + * placeholder that is at least as large as the maximum number of
9056 + * branches we *could* have, which is the current number plus all
9057 + * the additions. Once we're done with these temp placeholders, we
9058 + * may have to re-allocate the final size, copy over from the temp,
9059 + * and then free the temps (done near the end of this function).
9061 + max_branches = cur_branches + add_branches;
9062 + /* allocate space for new pointers to lower dentry */
9063 + tmp_data = kcalloc(max_branches,
9064 + sizeof(struct unionfs_data), GFP_KERNEL);
9065 + if (unlikely(!tmp_data)) {
9069 + /* allocate space for new pointers to lower paths */
9070 + tmp_lower_paths = kcalloc(max_branches,
9071 + sizeof(struct path), GFP_KERNEL);
9072 + if (unlikely(!tmp_lower_paths)) {
9076 + /* copy current info into new placeholders, incrementing refcnts */
9077 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9078 + cur_branches * sizeof(struct unionfs_data));
9079 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9080 + cur_branches * sizeof(struct path));
9081 + for (i = 0; i < cur_branches; i++)
9082 + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9084 + /*******************************************************************
9085 + * For each branch command, do path_lookup on the requested branch,
9086 + * and apply the change to a temp branch list. To handle errors, we
9087 + * already dup'ed the old arrays (above), and increased the refcnts
9088 + * on various f/s objects. So now we can do all the path_lookups
9089 + * and branch-management commands on the new arrays. If it fail mid
9090 + * way, we free the tmp arrays and *put all objects. If we succeed,
9091 + * then we free old arrays and *put its objects, and then replace
9092 + * the arrays with the new tmp list (we may have to re-allocate the
9093 + * memory because the temp lists could have been larger than what we
9094 + * actually needed).
9095 + *******************************************************************/
9097 + while ((optname = strsep(&options, ",")) != NULL) {
9100 + if (!optname || !*optname)
9103 + * At this stage optname holds a comma-delimited option, but
9104 + * without the commas. Next, we need to break the string on
9105 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9106 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9107 + * KEY is "/foo", and VAL is "rw".
9109 + optarg = strchr(optname, '=');
9112 + /* incgen remount option (instead of old ioctl) */
9113 + if (!strcmp("incgen", optname)) {
9115 + goto out_no_change;
9119 + * All of our options take an argument now. (Insert ones
9120 + * that don't above this check.) So at this stage optname
9121 + * contains the CMD part and optarg contains the ARG part.
9123 + if (!optarg || !*optarg) {
9124 + printk(KERN_ERR "unionfs: all remount options require "
9125 + "an argument (%s)\n", optname);
9130 + if (!strcmp("add", optname)) {
9131 + err = do_remount_add_option(optarg, new_branches,
9134 + &new_high_branch_id);
9138 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9139 + printk(KERN_ERR "unionfs: command exceeds "
9140 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9146 + if (!strcmp("del", optname)) {
9147 + err = do_remount_del_option(optarg, new_branches,
9155 + if (!strcmp("mode", optname)) {
9156 + err = do_remount_mode_option(optarg, new_branches,
9165 + * When you use "mount -o remount,ro", mount(8) will
9166 + * reportedly pass the original dirs= string from
9167 + * /proc/mounts. So for now, we have to ignore dirs= and
9168 + * not consider it an error, unless we want to allow users
9169 + * to pass dirs= in remount. Note that to allow the VFS to
9170 + * actually process the ro/rw remount options, we have to
9171 + * return 0 from this function.
9173 + if (!strcmp("dirs", optname)) {
9174 + printk(KERN_WARNING
9175 + "unionfs: remount ignoring option \"%s\"\n",
9182 + "unionfs: unrecognized option \"%s\"\n", optname);
9188 + /******************************************************************
9189 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9190 + * see if we need to allocate a small-sized new vector, copy the
9191 + * vectors to their correct place, release the refcnt of the older
9192 + * ones, and return. Also handle invalidating any pages that will
9193 + * have to be re-read.
9194 + *******************************************************************/
9196 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9197 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9198 + "(use \"remount,ro\" to create a read-only union)\n");
9203 + /* (re)allocate space for new pointers to lower dentry */
9204 + size = new_branches * sizeof(struct unionfs_data);
9205 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9206 + if (unlikely(!new_data)) {
9211 + /* allocate space for new pointers to lower paths */
9212 + size = new_branches * sizeof(struct path);
9213 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9214 + if (unlikely(!new_lower_paths)) {
9219 + /* allocate space for new pointers to lower inodes */
9220 + new_lower_inodes = kcalloc(new_branches,
9221 + sizeof(struct inode *), GFP_KERNEL);
9222 + if (unlikely(!new_lower_inodes)) {
9228 + * OK, just before we actually put the new set of branches in place,
9229 + * we need to ensure that our own f/s has no dirty objects left.
9230 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9231 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9232 + * pages. So all that's left is for us to invalidate any leftover
9233 + * (non-dirty) pages to ensure that they will be re-read from the
9234 + * new lower branches (and to support mmap).
9238 + * Once we finish the remounting successfully, our superblock
9239 + * generation number will have increased. This will be detected by
9240 + * our dentry-revalidation code upon subsequent f/s operations
9241 + * through unionfs. The revalidation code will rebuild the union of
9242 + * lower inodes for a given unionfs inode and invalidate any pages
9243 + * of such "stale" inodes (by calling our purge_inode_data
9244 + * function). This revalidation will happen lazily and
9245 + * incrementally, as users perform operations on cached inodes. We
9246 + * would like to encourage this revalidation to happen sooner if
9247 + * possible, so we like to try to invalidate as many other pages in
9248 + * our superblock as we can. We used to call drop_pagecache_sb() or
9249 + * a variant thereof, but either method was racy (drop_caches alone
9250 + * is known to be racy). So now we let the revalidation happen on a
9251 + * per file basis in ->d_revalidate.
9254 + /* grab new lower super references; release old ones */
9255 + for (i = 0; i < new_branches; i++)
9256 + atomic_inc(&new_data[i].sb->s_active);
9257 + for (i = 0; i < sbmax(sb); i++)
9258 + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9260 + /* copy new vectors into their correct place */
9261 + tmp_data = UNIONFS_SB(sb)->data;
9262 + UNIONFS_SB(sb)->data = new_data;
9263 + new_data = NULL; /* so don't free good pointers below */
9264 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9265 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9266 + new_lower_paths = NULL; /* so don't free good pointers below */
9268 + /* update our unionfs_sb_info and root dentry index of last branch */
9269 + i = sbmax(sb); /* save no. of branches to release at end */
9270 + sbend(sb) = new_branches - 1;
9271 + dbend(sb->s_root) = new_branches - 1;
9272 + old_ibstart = ibstart(sb->s_root->d_inode);
9273 + old_ibend = ibend(sb->s_root->d_inode);
9274 + ibend(sb->s_root->d_inode) = new_branches - 1;
9275 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9276 + new_branches = i; /* no. of branches to release below */
9279 + * Update lower inodes: 3 steps
9280 + * 1. grab ref on all new lower inodes
9282 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9283 + struct dentry *lower_dentry =
9284 + unionfs_lower_dentry_idx(sb->s_root, i);
9285 + igrab(lower_dentry->d_inode);
9286 + new_lower_inodes[i] = lower_dentry->d_inode;
9288 + /* 2. release reference on all older lower inodes */
9289 + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9290 + /* 3. update root dentry's inode to new lower_inodes array */
9291 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9292 + new_lower_inodes = NULL;
9294 + /* maxbytes may have changed */
9295 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9296 + /* update high branch ID */
9297 + sbhbid(sb) = new_high_branch_id;
9299 + /* update our sb->generation for revalidating objects */
9300 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9301 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9302 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9303 + if (!(*flags & MS_SILENT))
9304 + pr_info("unionfs: %s: new generation number %d\n",
9305 + UNIONFS_SB(sb)->dev_name, i);
9306 + /* finally, update the root dentry's times */
9307 + unionfs_copy_attr_times(sb->s_root->d_inode);
9308 + err = 0; /* reset to success */
9311 + * The code above falls through to the next label, and releases the
9312 + * refcnts of the older ones (stored in tmp_*): if we fell through
9313 + * here, it means success. However, if we jump directly to this
9314 + * label from any error above, then an error occurred after we
9315 + * grabbed various refcnts, and so we have to release the
9316 + * temporarily constructed structures.
9319 + /* no need to cleanup/release anything in tmp_data */
9320 + if (tmp_lower_paths)
9321 + for (i = 0; i < new_branches; i++)
9322 + path_put(&tmp_lower_paths[i]);
9324 + kfree(tmp_lower_paths);
9326 + kfree(new_lower_paths);
9328 + kfree(new_lower_inodes);
9330 + unionfs_check_dentry(sb->s_root);
9331 + unionfs_write_unlock(sb);
9336 + * Called by iput() when the inode reference count reached zero
9337 + * and the inode is not hashed anywhere. Used to clear anything
9338 + * that needs to be, before the inode is completely destroyed and put
9339 + * on the inode free list.
9341 + * No need to lock sb info's rwsem.
9343 +static void unionfs_clear_inode(struct inode *inode)
9345 + int bindex, bstart, bend;
9346 + struct inode *lower_inode;
9347 + struct list_head *pos, *n;
9348 + struct unionfs_dir_state *rdstate;
9350 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9351 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9352 + list_del(&rdstate->cache);
9353 + free_rdstate(rdstate);
9357 + * Decrement a reference to a lower_inode, which was incremented
9358 + * by our read_inode when it was created initially.
9360 + bstart = ibstart(inode);
9361 + bend = ibend(inode);
9362 + if (bstart >= 0) {
9363 + for (bindex = bstart; bindex <= bend; bindex++) {
9364 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9367 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9368 + /* see Documentation/filesystems/unionfs/issues.txt */
9370 + iput(lower_inode);
9375 + kfree(UNIONFS_I(inode)->lower_inodes);
9376 + UNIONFS_I(inode)->lower_inodes = NULL;
9379 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9381 + struct unionfs_inode_info *i;
9383 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9387 + /* memset everything up to the inode to 0 */
9388 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9390 + i->vfs_inode.i_version = 1;
9391 + return &i->vfs_inode;
9394 +static void unionfs_destroy_inode(struct inode *inode)
9396 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9399 +/* unionfs inode cache constructor */
9400 +static void init_once(void *obj)
9402 + struct unionfs_inode_info *i = obj;
9404 + inode_init_once(&i->vfs_inode);
9407 +int unionfs_init_inode_cache(void)
9411 + unionfs_inode_cachep =
9412 + kmem_cache_create("unionfs_inode_cache",
9413 + sizeof(struct unionfs_inode_info), 0,
9414 + SLAB_RECLAIM_ACCOUNT, init_once);
9415 + if (unlikely(!unionfs_inode_cachep))
9420 +/* unionfs inode cache destructor */
9421 +void unionfs_destroy_inode_cache(void)
9423 + if (unionfs_inode_cachep)
9424 + kmem_cache_destroy(unionfs_inode_cachep);
9428 + * Called when we have a dirty inode, right here we only throw out
9429 + * parts of our readdir list that are too old.
9431 + * No need to grab sb info's rwsem.
9433 +static int unionfs_write_inode(struct inode *inode, int sync)
9435 + struct list_head *pos, *n;
9436 + struct unionfs_dir_state *rdstate;
9438 + spin_lock(&UNIONFS_I(inode)->rdlock);
9439 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9440 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9441 + /* We keep this list in LRU order. */
9442 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9444 + UNIONFS_I(inode)->rdcount--;
9445 + list_del(&rdstate->cache);
9446 + free_rdstate(rdstate);
9448 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9454 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9455 + * code can actually succeed and won't leave tasks that need handling.
9457 +static void unionfs_umount_begin(struct super_block *sb)
9459 + struct super_block *lower_sb;
9460 + int bindex, bstart, bend;
9462 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9464 + bstart = sbstart(sb);
9466 + for (bindex = bstart; bindex <= bend; bindex++) {
9467 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9469 + if (lower_sb && lower_sb->s_op &&
9470 + lower_sb->s_op->umount_begin)
9471 + lower_sb->s_op->umount_begin(lower_sb);
9474 + unionfs_read_unlock(sb);
9477 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9479 + struct super_block *sb = mnt->mnt_sb;
9483 + int bindex, bstart, bend;
9486 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9488 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9490 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9491 + if (unlikely(!tmp_page)) {
9496 + bstart = sbstart(sb);
9499 + seq_printf(m, ",dirs=");
9500 + for (bindex = bstart; bindex <= bend; bindex++) {
9502 + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9503 + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9504 + path = d_path(&p, tmp_page, PAGE_SIZE);
9505 + if (IS_ERR(path)) {
9506 + ret = PTR_ERR(path);
9510 + perms = branchperms(sb, bindex);
9512 + seq_printf(m, "%s=%s", path,
9513 + perms & MAY_WRITE ? "rw" : "ro");
9514 + if (bindex != bend)
9515 + seq_printf(m, ":");
9519 + free_page((unsigned long) tmp_page);
9521 + unionfs_unlock_dentry(sb->s_root);
9523 + unionfs_read_unlock(sb);
9528 +struct super_operations unionfs_sops = {
9529 + .delete_inode = unionfs_delete_inode,
9530 + .put_super = unionfs_put_super,
9531 + .statfs = unionfs_statfs,
9532 + .remount_fs = unionfs_remount_fs,
9533 + .clear_inode = unionfs_clear_inode,
9534 + .umount_begin = unionfs_umount_begin,
9535 + .show_options = unionfs_show_options,
9536 + .write_inode = unionfs_write_inode,
9537 + .alloc_inode = unionfs_alloc_inode,
9538 + .destroy_inode = unionfs_destroy_inode,
9540 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9541 new file mode 100644
9542 index 0000000..00e6dec
9544 +++ b/fs/unionfs/union.h
9547 + * Copyright (c) 2003-2008 Erez Zadok
9548 + * Copyright (c) 2003-2006 Charles P. Wright
9549 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9550 + * Copyright (c) 2005 Arun M. Krishnakumar
9551 + * Copyright (c) 2004-2006 David P. Quigley
9552 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9553 + * Copyright (c) 2003 Puja Gupta
9554 + * Copyright (c) 2003 Harikesavan Krishnan
9555 + * Copyright (c) 2003-2008 Stony Brook University
9556 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
9558 + * This program is free software; you can redistribute it and/or modify
9559 + * it under the terms of the GNU General Public License version 2 as
9560 + * published by the Free Software Foundation.
9566 +#include <linux/dcache.h>
9567 +#include <linux/file.h>
9568 +#include <linux/list.h>
9569 +#include <linux/fs.h>
9570 +#include <linux/mm.h>
9571 +#include <linux/module.h>
9572 +#include <linux/mount.h>
9573 +#include <linux/namei.h>
9574 +#include <linux/page-flags.h>
9575 +#include <linux/pagemap.h>
9576 +#include <linux/poll.h>
9577 +#include <linux/security.h>
9578 +#include <linux/seq_file.h>
9579 +#include <linux/slab.h>
9580 +#include <linux/spinlock.h>
9581 +#include <linux/smp_lock.h>
9582 +#include <linux/statfs.h>
9583 +#include <linux/string.h>
9584 +#include <linux/vmalloc.h>
9585 +#include <linux/writeback.h>
9586 +#include <linux/buffer_head.h>
9587 +#include <linux/xattr.h>
9588 +#include <linux/fs_stack.h>
9589 +#include <linux/magic.h>
9590 +#include <linux/log2.h>
9591 +#include <linux/poison.h>
9592 +#include <linux/mman.h>
9593 +#include <linux/backing-dev.h>
9594 +#include <linux/splice.h>
9596 +#include <asm/system.h>
9598 +#include <linux/union_fs.h>
9600 +/* the file system name */
9601 +#define UNIONFS_NAME "unionfs"
9603 +/* unionfs root inode number */
9604 +#define UNIONFS_ROOT_INO 1
9606 +/* number of times we try to get a unique temporary file name */
9607 +#define GET_TMPNAM_MAX_RETRY 5
9609 +/* maximum number of branches we support, to avoid memory blowup */
9610 +#define UNIONFS_MAX_BRANCHES 128
9612 +/* minimum time (seconds) required for time-based cache-coherency */
9613 +#define UNIONFS_MIN_CC_TIME 3
9615 +/* Operations vectors defined in specific files. */
9616 +extern struct file_operations unionfs_main_fops;
9617 +extern struct file_operations unionfs_dir_fops;
9618 +extern struct inode_operations unionfs_main_iops;
9619 +extern struct inode_operations unionfs_dir_iops;
9620 +extern struct inode_operations unionfs_symlink_iops;
9621 +extern struct super_operations unionfs_sops;
9622 +extern struct dentry_operations unionfs_dops;
9623 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9624 +extern struct vm_operations_struct unionfs_vm_ops;
9626 +/* How long should an entry be allowed to persist */
9627 +#define RDCACHE_JIFFIES (5*HZ)
9629 +/* compatibility with Real-Time patches */
9630 +#ifdef CONFIG_PREEMPT_RT
9631 +# define unionfs_rw_semaphore compat_rw_semaphore
9632 +#else /* not CONFIG_PREEMPT_RT */
9633 +# define unionfs_rw_semaphore rw_semaphore
9634 +#endif /* not CONFIG_PREEMPT_RT */
9636 +/* file private data. */
9637 +struct unionfs_file_info {
9640 + atomic_t generation;
9642 + struct unionfs_dir_state *rdstate;
9643 + struct file **lower_files;
9644 + int *saved_branch_ids; /* IDs of branches when file was opened */
9645 + struct vm_operations_struct *lower_vm_ops;
9646 + bool wrote_to_file; /* for delayed copyup */
9649 +/* unionfs inode data in memory */
9650 +struct unionfs_inode_info {
9653 + atomic_t generation;
9654 + /* Stuff for readdir over NFS. */
9655 + spinlock_t rdlock;
9656 + struct list_head readdircache;
9661 + /* The lower inodes */
9662 + struct inode **lower_inodes;
9664 + struct inode vfs_inode;
9667 +/* unionfs dentry data in memory */
9668 +struct unionfs_dentry_info {
9670 + * The semaphore is used to lock the dentry as soon as we get into a
9671 + * unionfs function from the VFS. Our lock ordering is that children
9672 + * go before their parents.
9674 + struct mutex lock;
9679 + atomic_t generation;
9680 + struct path *lower_paths;
9683 +/* These are the pointers to our various objects. */
9684 +struct unionfs_data {
9685 + struct super_block *sb; /* lower super_block */
9686 + atomic_t open_files; /* number of open files on branch */
9688 + int branch_id; /* unique branch ID at re/mount time */
9691 +/* unionfs super-block data in memory */
9692 +struct unionfs_sb_info {
9695 + atomic_t generation;
9698 + * This rwsem is used to make sure that a branch management
9700 + * 1) will not begin before all currently in-flight operations
9702 + * 2) any new operations do not execute until the currently
9703 + * running branch management operation completes.
9705 + * The write_lock_owner records the PID of the task which grabbed
9706 + * the rw_sem for writing. If the same task also tries to grab the
9707 + * read lock, we allow it. This prevents a self-deadlock when
9708 + * branch-management is used on a pivot_root'ed union, because we
9709 + * have to ->lookup paths which belong to the same union.
9711 + struct unionfs_rw_semaphore rwsem;
9712 + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9713 + int high_branch_id; /* last unique branch ID given */
9714 + char *dev_name; /* to identify different unions in pr_debug */
9715 + struct unionfs_data *data;
9719 + * structure for making the linked list of entries by readdir on left branch
9720 + * to compare with entries on right branch
9722 +struct filldir_node {
9723 + struct list_head file_list; /* list for directory entries */
9724 + char *name; /* name entry */
9725 + int hash; /* name hash */
9726 + int namelen; /* name len since name is not 0 terminated */
9729 + * we can check for duplicate whiteouts and files in the same branch
9730 + * in order to return -EIO.
9734 + /* is this a whiteout entry? */
9737 + /* Inline name, so we don't need to separately kmalloc small ones */
9738 + char iname[DNAME_INLINE_LEN_MIN];
9741 +/* Directory hash table. */
9742 +struct unionfs_dir_state {
9743 + unsigned int cookie; /* the cookie, based off of rdversion */
9744 + unsigned int offset; /* The entry we have returned. */
9746 + loff_t dirpos; /* offset within the lower level directory */
9747 + int size; /* How big is the hash table? */
9748 + int hashentries; /* How many entries have been inserted? */
9749 + unsigned long access;
9751 + /* This cache list is used when the inode keeps us around. */
9752 + struct list_head cache;
9753 + struct list_head list[0];
9756 +/* externs needed for fanout.h or sioq.h */
9757 +extern int unionfs_get_nlinks(const struct inode *inode);
9758 +extern void unionfs_copy_attr_times(struct inode *upper);
9759 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9761 +/* include miscellaneous macros */
9762 +#include "fanout.h"
9765 +/* externs for cache creation/deletion routines */
9766 +extern void unionfs_destroy_filldir_cache(void);
9767 +extern int unionfs_init_filldir_cache(void);
9768 +extern int unionfs_init_inode_cache(void);
9769 +extern void unionfs_destroy_inode_cache(void);
9770 +extern int unionfs_init_dentry_cache(void);
9771 +extern void unionfs_destroy_dentry_cache(void);
9773 +/* Initialize and free readdir-specific state. */
9774 +extern int init_rdstate(struct file *file);
9775 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9777 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9779 +extern void free_rdstate(struct unionfs_dir_state *state);
9780 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9781 + const char *name, int namelen, int bindex,
9783 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9784 + const char *name, int namelen,
9787 +extern struct dentry **alloc_new_dentries(int objs);
9788 +extern struct unionfs_data *alloc_new_data(int objs);
9790 +/* We can only use 32-bits of offset for rdstate --- blech! */
9791 +#define DIREOF (0xfffff)
9792 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9793 +#define MAXRDCOOKIE (0xfff)
9794 +/* Turn an rdstate into an offset. */
9795 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9799 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9800 + | (buf->offset & DIREOF);
9804 +/* Macros for locking a super_block. */
9805 +enum unionfs_super_lock_class {
9806 + UNIONFS_SMUTEX_NORMAL,
9807 + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9808 + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9810 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9812 + if (UNIONFS_SB(sb)->write_lock_owner &&
9813 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9815 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9817 +static inline void unionfs_read_unlock(struct super_block *sb)
9819 + if (UNIONFS_SB(sb)->write_lock_owner &&
9820 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9822 + up_read(&UNIONFS_SB(sb)->rwsem);
9824 +static inline void unionfs_write_lock(struct super_block *sb)
9826 + down_write(&UNIONFS_SB(sb)->rwsem);
9827 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9829 +static inline void unionfs_write_unlock(struct super_block *sb)
9831 + up_write(&UNIONFS_SB(sb)->rwsem);
9832 + UNIONFS_SB(sb)->write_lock_owner = 0;
9835 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9836 + struct dentry *d2)
9840 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9841 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9843 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9844 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9848 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9849 + struct dentry *d2)
9852 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9853 + unionfs_unlock_dentry(d1);
9854 + unionfs_unlock_dentry(d2);
9856 + unionfs_unlock_dentry(d2);
9857 + unionfs_unlock_dentry(d1);
9861 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9862 + struct dentry *p2)
9865 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9869 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9870 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9872 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9873 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9877 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9878 + struct dentry *p2)
9881 + unionfs_unlock_dentry(p1);
9884 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9885 + unionfs_unlock_dentry(p1);
9886 + unionfs_unlock_dentry(p2);
9888 + unionfs_unlock_dentry(p2);
9889 + unionfs_unlock_dentry(p1);
9893 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9894 +extern int realloc_dentry_private_data(struct dentry *dentry);
9895 +extern void free_dentry_private_data(struct dentry *dentry);
9896 +extern void update_bstart(struct dentry *dentry);
9897 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9898 +extern void release_lower_nd(struct nameidata *nd, int err);
9904 +/* replicates the directory structure up to given dentry in given branch */
9905 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9906 + const char *name, int bindex);
9908 +/* partial lookup */
9909 +extern int unionfs_partial_lookup(struct dentry *dentry,
9910 + struct dentry *parent);
9911 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9912 + struct dentry *parent,
9915 +/* copies a file from dbstart to newbindex branch */
9916 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9917 + int newbindex, loff_t size);
9918 +extern int copyup_named_file(struct inode *dir, struct file *file,
9919 + char *name, int bstart, int new_bindex,
9921 +/* copies a dentry from dbstart to newbindex branch */
9922 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9923 + int bstart, int new_bindex, const char *name,
9924 + int namelen, struct file **copyup_file, loff_t len);
9925 +/* helper functions for post-copyup actions */
9926 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9927 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9929 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9930 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9931 + struct unionfs_dir_state **namelist);
9932 +/* whiteout and opaque directory helpers */
9933 +extern char *alloc_whname(const char *name, int len);
9934 +extern bool is_whiteout_name(char **namep, int *namelenp);
9935 +extern bool is_validname(const char *name);
9936 +extern struct dentry *lookup_whiteout(const char *name,
9937 + struct dentry *lower_parent);
9938 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9939 +extern int unlink_whiteout(struct dentry *wh_dentry);
9940 +extern int check_unlink_whiteout(struct dentry *dentry,
9941 + struct dentry *lower_dentry, int bindex);
9942 +extern int create_whiteout(struct dentry *dentry, int start);
9943 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9944 + struct unionfs_dir_state *namelist);
9945 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9946 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9947 +extern void unionfs_set_max_namelen(long *namelen);
9949 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9950 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9952 +/* Locking functions. */
9953 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9954 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9956 +/* Common file operations. */
9957 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9959 +extern int unionfs_open(struct inode *inode, struct file *file);
9960 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9961 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9962 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9963 + unsigned long arg);
9964 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
9966 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9968 +/* Inode operations */
9969 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9970 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9971 + struct inode *new_dir, struct dentry *new_dentry);
9972 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
9973 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
9975 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
9976 + struct dentry *parent, bool willwrite);
9977 +extern bool is_negative_lower(const struct dentry *dentry);
9978 +extern bool is_newer_lower(const struct dentry *dentry);
9979 +extern void purge_sb_data(struct super_block *sb);
9981 +/* The values for unionfs_interpose's flag. */
9982 +#define INTERPOSE_DEFAULT 0
9983 +#define INTERPOSE_LOOKUP 1
9984 +#define INTERPOSE_REVAL 2
9985 +#define INTERPOSE_REVAL_NEG 3
9986 +#define INTERPOSE_PARTIAL 4
9988 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9989 + struct super_block *sb, int flag);
9991 +#ifdef CONFIG_UNION_FS_XATTR
9992 +/* Extended attribute functions. */
9993 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
9994 +static inline void unionfs_xattr_kfree(const void *p)
9998 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
9999 + void *value, size_t size);
10000 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
10001 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
10003 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
10004 + const void *value, size_t size, int flags);
10005 +#endif /* CONFIG_UNION_FS_XATTR */
10007 +/* The root directory is unhashed, but isn't deleted. */
10008 +static inline int d_deleted(struct dentry *d)
10010 + return d_unhashed(d) && (d != d->d_sb->s_root);
10013 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10014 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10016 +/* unionfs_open, check if we need to copyup the file */
10017 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10018 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10020 +static inline int branchperms(const struct super_block *sb, int index)
10022 + BUG_ON(index < 0);
10023 + return UNIONFS_SB(sb)->data[index].branchperms;
10026 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10028 + BUG_ON(index < 0);
10029 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10033 +/* Is this file on a read-only branch? */
10034 +static inline int is_robranch_super(const struct super_block *sb, int index)
10038 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10042 +/* Is this file on a read-only branch? */
10043 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10045 + struct super_block *lower_sb;
10047 + BUG_ON(index < 0);
10049 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10052 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10053 + BUG_ON(lower_sb == NULL);
10055 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10056 + * lower_dentry could be a negative.
10058 + if (lower_sb->s_flags & MS_RDONLY)
10064 +static inline int is_robranch(const struct dentry *dentry)
10068 + index = UNIONFS_D(dentry)->bstart;
10069 + BUG_ON(index < 0);
10071 + return is_robranch_idx(dentry, index);
10077 +extern int check_branch(struct nameidata *nd);
10078 +extern int parse_branch_mode(const char *name, int *perms);
10080 +/* locking helpers */
10081 +static inline struct dentry *lock_parent(struct dentry *dentry)
10083 + struct dentry *dir = dget_parent(dentry);
10084 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
10087 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10089 + struct dentry *dir = dget_parent(dentry);
10091 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10095 +static inline void unlock_dir(struct dentry *dir)
10097 + mutex_unlock(&dir->d_inode->i_mutex);
10101 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10104 + struct vfsmount *mnt;
10106 + BUG_ON(!dentry || bindex < 0);
10108 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10109 +#ifdef CONFIG_UNION_FS_DEBUG
10111 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10113 +#endif /* CONFIG_UNION_FS_DEBUG */
10118 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10120 + struct vfsmount *mnt;
10122 + if (!dentry && bindex < 0)
10124 + BUG_ON(!dentry || bindex < 0);
10126 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10127 +#ifdef CONFIG_UNION_FS_DEBUG
10129 + * Directories can have NULL lower objects in between start/end, but
10130 + * NOT if at the start/end range. We cannot verify that this dentry
10131 + * is a type=DIR, because it may already be a negative dentry. But
10132 + * if dbstart is greater than dbend, we know that this couldn't have
10133 + * been a regular file: it had to have been a directory.
10135 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10136 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10137 +#endif /* CONFIG_UNION_FS_DEBUG */
10141 +#ifdef CONFIG_UNION_FS_DEBUG
10143 +/* useful for tracking code reachability */
10144 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10146 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10147 + __FILE__, __func__, __LINE__)
10148 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10149 + __FILE__, __func__, __LINE__)
10150 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10151 + __FILE__, __func__, __LINE__)
10152 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10153 + __FILE__, __func__, __LINE__)
10154 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10155 + __FILE__, __func__, __LINE__)
10156 +#define show_inode_times(i) __show_inode_times((i), \
10157 + __FILE__, __func__, __LINE__)
10158 +#define show_dinode_times(d) __show_dinode_times((d), \
10159 + __FILE__, __func__, __LINE__)
10160 +#define show_inode_counts(i) __show_inode_counts((i), \
10161 + __FILE__, __func__, __LINE__)
10163 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10164 + const char *fxn, int line);
10165 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10166 + const char *fname, const char *fxn,
10168 +extern void __unionfs_check_file(const struct file *file,
10169 + const char *fname, const char *fxn, int line);
10170 +extern void __unionfs_check_nd(const struct nameidata *nd,
10171 + const char *fname, const char *fxn, int line);
10172 +extern void __show_branch_counts(const struct super_block *sb,
10173 + const char *file, const char *fxn, int line);
10174 +extern void __show_inode_times(const struct inode *inode,
10175 + const char *file, const char *fxn, int line);
10176 +extern void __show_dinode_times(const struct dentry *dentry,
10177 + const char *file, const char *fxn, int line);
10178 +extern void __show_inode_counts(const struct inode *inode,
10179 + const char *file, const char *fxn, int line);
10181 +#else /* not CONFIG_UNION_FS_DEBUG */
10183 +/* we leave useful hooks for these check functions throughout the code */
10184 +#define unionfs_check_inode(i) do { } while (0)
10185 +#define unionfs_check_dentry(d) do { } while (0)
10186 +#define unionfs_check_file(f) do { } while (0)
10187 +#define unionfs_check_nd(n) do { } while (0)
10188 +#define show_branch_counts(sb) do { } while (0)
10189 +#define show_inode_times(i) do { } while (0)
10190 +#define show_dinode_times(d) do { } while (0)
10191 +#define show_inode_counts(i) do { } while (0)
10193 +#endif /* not CONFIG_UNION_FS_DEBUG */
10195 +#endif /* not _UNION_H_ */
10196 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10197 new file mode 100644
10198 index 0000000..6634c4b
10200 +++ b/fs/unionfs/unlink.c
10203 + * Copyright (c) 2003-2008 Erez Zadok
10204 + * Copyright (c) 2003-2006 Charles P. Wright
10205 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10206 + * Copyright (c) 2005-2006 Junjiro Okajima
10207 + * Copyright (c) 2005 Arun M. Krishnakumar
10208 + * Copyright (c) 2004-2006 David P. Quigley
10209 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10210 + * Copyright (c) 2003 Puja Gupta
10211 + * Copyright (c) 2003 Harikesavan Krishnan
10212 + * Copyright (c) 2003-2008 Stony Brook University
10213 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
10215 + * This program is free software; you can redistribute it and/or modify
10216 + * it under the terms of the GNU General Public License version 2 as
10217 + * published by the Free Software Foundation.
10220 +#include "union.h"
10223 + * Helper function for Unionfs's unlink operation.
10225 + * The main goal of this function is to optimize the unlinking of non-dir
10226 + * objects in unionfs by deleting all possible lower inode objects from the
10227 + * underlying branches having same dentry name as the non-dir dentry on
10228 + * which this unlink operation is called. This way we delete as many lower
10229 + * inodes as possible, and save space. Whiteouts need to be created in
10230 + * branch0 only if unlinking fails on any of the lower branch other than
10231 + * branch0, or if a lower branch is marked read-only.
10233 + * Also, while unlinking a file, if we encounter any dir type entry in any
10234 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10235 + * The following special cases are also handled:
10237 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10238 + * appropriate error.
10240 + * (2) If we get an error during unlink in any of other lower branch other
10241 + * than branch0, then we create a whiteout in branch0.
10243 + * (3) If a whiteout already exists in any intermediate branch, we delete
10244 + * all possible inodes only up to that branch (this is an "opaqueness"
10245 + * as as per Documentation/filesystems/unionfs/concepts.txt).
10248 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10249 + struct dentry *parent)
10251 + struct dentry *lower_dentry;
10252 + struct dentry *lower_dir_dentry;
10256 + err = unionfs_partial_lookup(dentry, parent);
10260 + /* trying to unlink all possible valid instances */
10261 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10262 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10263 + if (!lower_dentry || !lower_dentry->d_inode)
10266 + lower_dir_dentry = lock_parent(lower_dentry);
10268 + /* avoid destroying the lower inode if the object is in use */
10269 + dget(lower_dentry);
10270 + err = is_robranch_super(dentry->d_sb, bindex);
10272 + /* see Documentation/filesystems/unionfs/issues.txt */
10274 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10275 + err = vfs_unlink(lower_dir_dentry->d_inode,
10278 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10283 + /* if lower object deletion succeeds, update inode's times */
10285 + unionfs_copy_attr_times(dentry->d_inode);
10286 + dput(lower_dentry);
10287 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10288 + unlock_dir(lower_dir_dentry);
10295 + * Create the whiteout in branch 0 (highest priority) only if (a)
10296 + * there was an error in any intermediate branch other than branch 0
10297 + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10298 + * mounted read-only.
10301 + if ((bindex == 0) ||
10302 + ((bindex == dbstart(dentry)) &&
10303 + (!IS_COPYUP_ERR(err))))
10306 + if (!IS_COPYUP_ERR(err))
10307 + pr_debug("unionfs: lower object deletion "
10308 + "failed in branch:%d\n", bindex);
10309 + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10315 + inode_dec_link_count(dentry->d_inode);
10317 + /* We don't want to leave negative leftover dentries for revalidate. */
10318 + if (!err && (dbopaque(dentry) != -1))
10319 + update_bstart(dentry);
10324 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10327 + struct inode *inode = dentry->d_inode;
10328 + struct dentry *parent;
10331 + BUG_ON(S_ISDIR(inode->i_mode));
10332 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10333 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10334 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10336 + valid = __unionfs_d_revalidate(dentry, parent, false);
10337 + if (unlikely(!valid)) {
10341 + unionfs_check_dentry(dentry);
10343 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10344 + /* call d_drop so the system "forgets" about us */
10346 + unionfs_postcopyup_release(dentry);
10347 + unionfs_postcopyup_setmnt(parent);
10348 + if (inode->i_nlink == 0) /* drop lower inodes */
10349 + iput_lowers_all(inode, false);
10352 + * if unlink/whiteout succeeded, parent dir mtime has
10355 + unionfs_copy_attr_times(dir);
10360 + unionfs_check_dentry(dentry);
10361 + unionfs_check_inode(dir);
10363 + unionfs_unlock_dentry(dentry);
10364 + unionfs_unlock_parent(dentry, parent);
10365 + unionfs_read_unlock(dentry->d_sb);
10369 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10370 + struct unionfs_dir_state *namelist)
10373 + struct dentry *lower_dentry;
10374 + struct dentry *lower_dir_dentry = NULL;
10376 + /* Here we need to remove whiteout entries. */
10377 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10381 + lower_dentry = unionfs_lower_dentry(dentry);
10383 + lower_dir_dentry = lock_parent(lower_dentry);
10385 + /* avoid destroying the lower inode if the file is in use */
10386 + dget(lower_dentry);
10387 + err = is_robranch(dentry);
10389 + /* see Documentation/filesystems/unionfs/issues.txt */
10391 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10394 + dput(lower_dentry);
10396 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10397 + /* propagate number of hard-links */
10398 + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10401 + if (lower_dir_dentry)
10402 + unlock_dir(lower_dir_dentry);
10406 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10409 + struct unionfs_dir_state *namelist = NULL;
10410 + struct dentry *parent;
10411 + int dstart, dend;
10414 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10415 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10416 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10418 + valid = __unionfs_d_revalidate(dentry, parent, false);
10419 + if (unlikely(!valid)) {
10423 + unionfs_check_dentry(dentry);
10425 + /* check if this unionfs directory is empty or not */
10426 + err = check_empty(dentry, parent, &namelist);
10430 + err = unionfs_rmdir_first(dir, dentry, namelist);
10431 + dstart = dbstart(dentry);
10432 + dend = dbend(dentry);
10434 + * We create a whiteout for the directory if there was an error to
10435 + * rmdir the first directory entry in the union. Otherwise, we
10436 + * create a whiteout only if there is no chance that a lower
10437 + * priority branch might also have the same named directory. IOW,
10438 + * if there is not another same-named directory at a lower priority
10439 + * branch, then we don't need to create a whiteout for it.
10442 + if (dstart < dend)
10443 + err = create_whiteout(dentry, dstart);
10450 + /* exit if the error returned was NOT -EROFS */
10451 + if (!IS_COPYUP_ERR(err))
10454 + new_err = create_whiteout(dentry, dstart - 1);
10455 + if (new_err != -EEXIST)
10461 + * Drop references to lower dentry/inode so storage space for them
10462 + * can be reclaimed. Then, call d_drop so the system "forgets"
10466 + iput_lowers_all(dentry->d_inode, false);
10467 + dput(unionfs_lower_dentry_idx(dentry, dstart));
10468 + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10470 + /* update our lower vfsmnts, in case a copyup took place */
10471 + unionfs_postcopyup_setmnt(dentry);
10472 + unionfs_check_dentry(dentry);
10473 + unionfs_check_inode(dir);
10477 + free_rdstate(namelist);
10479 + unionfs_unlock_dentry(dentry);
10480 + unionfs_unlock_parent(dentry, parent);
10481 + unionfs_read_unlock(dentry->d_sb);
10484 diff --git a/fs/unionfs/whiteout.c b/fs/unionfs/whiteout.c
10485 new file mode 100644
10486 index 0000000..0934ac8
10488 +++ b/fs/unionfs/whiteout.c
10491 + * Copyright (c) 2003-2008 Erez Zadok
10492 + * Copyright (c) 2003-2006 Charles P. Wright
10493 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10494 + * Copyright (c) 2005-2006 Junjiro Okajima
10495 + * Copyright (c) 2005 Arun M. Krishnakumar
10496 + * Copyright (c) 2004-2006 David P. Quigley
10497 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10498 + * Copyright (c) 2003 Puja Gupta
10499 + * Copyright (c) 2003 Harikesavan Krishnan
10500 + * Copyright (c) 2003-2008 Stony Brook University
10501 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
10503 + * This program is free software; you can redistribute it and/or modify
10504 + * it under the terms of the GNU General Public License version 2 as
10505 + * published by the Free Software Foundation.
10508 +#include "union.h"
10511 + * whiteout and opaque directory helpers
10514 +/* What do we use for whiteouts. */
10515 +#define UNIONFS_WHPFX ".wh."
10516 +#define UNIONFS_WHLEN 4
10518 + * If a directory contains this file, then it is opaque. We start with the
10519 + * .wh. flag so that it is blocked by lookup.
10521 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10522 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10524 +/* construct whiteout filename */
10525 +char *alloc_whname(const char *name, int len)
10529 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10530 + if (unlikely(!buf))
10531 + return ERR_PTR(-ENOMEM);
10533 + strcpy(buf, UNIONFS_WHPFX);
10534 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10540 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10541 + * code in one place.
10543 +void unionfs_set_max_namelen(long *namelen)
10545 + *namelen -= UNIONFS_WHLEN;
10548 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10549 +bool is_whiteout_name(char **namep, int *namelenp)
10551 + if (*namelenp > UNIONFS_WHLEN &&
10552 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10553 + *namep += UNIONFS_WHLEN;
10554 + *namelenp -= UNIONFS_WHLEN;
10560 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10561 +bool is_validname(const char *name)
10563 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10565 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10566 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10572 + * Look for a whiteout @name in @lower_parent directory. If error, return
10573 + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10575 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10576 + * free then re-malloc calls. Need to provide a different API for those
10579 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10581 + char *whname = NULL;
10582 + int err = 0, namelen;
10583 + struct dentry *wh_dentry = NULL;
10585 + namelen = strlen(name);
10586 + whname = alloc_whname(name, namelen);
10587 + if (unlikely(IS_ERR(whname))) {
10588 + err = PTR_ERR(whname);
10592 + /* check if whiteout exists in this branch: lookup .wh.foo */
10593 + wh_dentry = lookup_one_len(whname, lower_parent, strlen(whname));
10594 + if (IS_ERR(wh_dentry)) {
10595 + err = PTR_ERR(wh_dentry);
10599 + /* check if negative dentry (ENOENT) */
10600 + if (!wh_dentry->d_inode)
10603 + /* whiteout found: check if valid type */
10604 + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10605 + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10606 + whname, wh_dentry->d_inode->i_mode);
10615 + wh_dentry = ERR_PTR(err);
10616 + return wh_dentry;
10619 +/* find and return first whiteout in parent directory, else ENOENT */
10620 +struct dentry *find_first_whiteout(struct dentry *dentry)
10622 + int bindex, bstart, bend;
10623 + struct dentry *parent, *lower_parent, *wh_dentry;
10625 + parent = dget_parent(dentry);
10627 + bstart = dbstart(parent);
10628 + bend = dbend(parent);
10629 + wh_dentry = ERR_PTR(-ENOENT);
10631 + for (bindex = bstart; bindex <= bend; bindex++) {
10632 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10633 + if (!lower_parent)
10635 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10636 + if (IS_ERR(wh_dentry))
10638 + if (wh_dentry->d_inode)
10641 + wh_dentry = ERR_PTR(-ENOENT);
10646 + return wh_dentry;
10650 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10651 + * release dentry reference.
10653 +int unlink_whiteout(struct dentry *wh_dentry)
10656 + struct dentry *lower_dir_dentry;
10658 + /* dget and lock parent dentry */
10659 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10661 + /* see Documentation/filesystems/unionfs/issues.txt */
10663 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10665 + unlock_dir(lower_dir_dentry);
10668 + * Whiteouts are special files and should be deleted no matter what
10669 + * (as if they never existed), in order to allow this create
10670 + * operation to succeed. This is especially important in sticky
10671 + * directories: a whiteout may have been created by one user, but
10672 + * the newly created file may be created by another user.
10673 + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10674 + * above failed, then we have to try to directly unlink the
10675 + * whiteout. Note: in the ODF version of unionfs, whiteout are
10676 + * handled much more cleanly.
10678 + if (err == -EPERM) {
10679 + struct inode *inode = lower_dir_dentry->d_inode;
10680 + err = inode->i_op->unlink(inode, wh_dentry);
10683 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10684 + "err = %d\n", wh_dentry->d_name.name, err);
10691 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10692 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10693 + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10694 + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10695 + * If it finds both a regular file and a whiteout, return -EIO (this should
10698 + * Return 0 if no whiteout was found. Return 1 if one was found and
10699 + * successfully removed. Therefore a value >= 0 tells the caller that
10700 + * @lower_dentry belongs to a good branch to create the new object in).
10701 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10702 + * unlink the whiteout.
10704 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10708 + struct dentry *wh_dentry = NULL;
10709 + struct dentry *lower_dir_dentry = NULL;
10711 + /* look for whiteout dentry first */
10712 + lower_dir_dentry = dget_parent(lower_dentry);
10713 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10714 + dput(lower_dir_dentry);
10715 + if (IS_ERR(wh_dentry)) {
10716 + err = PTR_ERR(wh_dentry);
10720 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10725 + /* check if regular file and whiteout were both found */
10726 + if (unlikely(lower_dentry->d_inode)) {
10728 + printk(KERN_ERR "unionfs: found both whiteout and regular "
10729 + "file in directory %s (branch %d)\n",
10730 + lower_dir_dentry->d_name.name, bindex);
10734 + /* check if branch is writeable */
10735 + err = is_robranch_super(dentry->d_sb, bindex);
10739 + /* .wh.foo has been found, so let's unlink it */
10740 + err = unlink_whiteout(wh_dentry);
10742 + err = 1; /* a whiteout was found and successfully removed */
10750 + * Pass an unionfs dentry and an index. It will try to create a whiteout
10751 + * for the filename in dentry, and will try in branch 'index'. On error,
10752 + * it will proceed to a branch to the left.
10754 +int create_whiteout(struct dentry *dentry, int start)
10756 + int bstart, bend, bindex;
10757 + struct dentry *lower_dir_dentry;
10758 + struct dentry *lower_dentry;
10759 + struct dentry *lower_wh_dentry;
10760 + struct nameidata nd;
10761 + char *name = NULL;
10762 + int err = -EINVAL;
10764 + verify_locked(dentry);
10766 + bstart = dbstart(dentry);
10767 + bend = dbend(dentry);
10769 + /* create dentry's whiteout equivalent */
10770 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10771 + if (unlikely(IS_ERR(name))) {
10772 + err = PTR_ERR(name);
10776 + for (bindex = start; bindex >= 0; bindex--) {
10777 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10779 + if (!lower_dentry) {
10781 + * if lower dentry is not present, create the
10782 + * entire lower dentry directory structure and go
10783 + * ahead. Since we want to just create whiteout, we
10784 + * only want the parent dentry, and hence get rid of
10787 + lower_dentry = create_parents(dentry->d_inode,
10789 + dentry->d_name.name,
10791 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10792 + int ret = PTR_ERR(lower_dentry);
10793 + if (!IS_COPYUP_ERR(ret))
10795 + "unionfs: create_parents for "
10796 + "whiteout failed: bindex=%d "
10797 + "err=%d\n", bindex, ret);
10802 + lower_wh_dentry =
10803 + lookup_one_len(name, lower_dentry->d_parent,
10804 + dentry->d_name.len + UNIONFS_WHLEN);
10805 + if (IS_ERR(lower_wh_dentry))
10809 + * The whiteout already exists. This used to be impossible,
10810 + * but now is possible because of opaqueness.
10812 + if (lower_wh_dentry->d_inode) {
10813 + dput(lower_wh_dentry);
10818 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10819 + if (unlikely(err < 0))
10821 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10822 + err = is_robranch_super(dentry->d_sb, bindex);
10824 + err = vfs_create(lower_dir_dentry->d_inode,
10826 + ~current->fs->umask & S_IRUGO,
10828 + unlock_dir(lower_dir_dentry);
10829 + dput(lower_wh_dentry);
10830 + release_lower_nd(&nd, err);
10832 + if (!err || !IS_COPYUP_ERR(err))
10836 + /* set dbopaque so that lookup will not proceed after this branch */
10838 + dbopaque(dentry) = bindex;
10846 + * Delete all of the whiteouts in a given directory for rmdir.
10848 + * lower directory inode should be locked
10850 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10851 + struct unionfs_dir_state *namelist)
10854 + struct dentry *lower_dir_dentry = NULL;
10855 + struct dentry *lower_dentry;
10856 + char *name = NULL, *p;
10857 + struct inode *lower_dir;
10859 + struct list_head *pos;
10860 + struct filldir_node *cursor;
10862 + /* Find out lower parent dentry */
10863 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10864 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10865 + lower_dir = lower_dir_dentry->d_inode;
10866 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10869 + name = __getname();
10870 + if (unlikely(!name))
10872 + strcpy(name, UNIONFS_WHPFX);
10873 + p = name + UNIONFS_WHLEN;
10876 + for (i = 0; !err && i < namelist->size; i++) {
10877 + list_for_each(pos, &namelist->list[i]) {
10879 + list_entry(pos, struct filldir_node,
10881 + /* Only operate on whiteouts in this branch. */
10882 + if (cursor->bindex != bindex)
10884 + if (!cursor->whiteout)
10887 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10889 + lookup_one_len(name, lower_dir_dentry,
10890 + cursor->namelen +
10892 + if (IS_ERR(lower_dentry)) {
10893 + err = PTR_ERR(lower_dentry);
10896 + if (lower_dentry->d_inode)
10897 + err = vfs_unlink(lower_dir, lower_dentry);
10898 + dput(lower_dentry);
10906 + /* After all of the removals, we should copy the attributes once. */
10907 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10914 +void __delete_whiteouts(struct work_struct *work)
10916 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10917 + struct deletewh_args *d = &args->deletewh;
10919 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10920 + complete(&args->comp);
10923 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10924 +int delete_whiteouts(struct dentry *dentry, int bindex,
10925 + struct unionfs_dir_state *namelist)
10928 + struct super_block *sb;
10929 + struct dentry *lower_dir_dentry;
10930 + struct inode *lower_dir;
10931 + struct sioq_args args;
10933 + sb = dentry->d_sb;
10935 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10936 + BUG_ON(bindex < dbstart(dentry));
10937 + BUG_ON(bindex > dbend(dentry));
10938 + err = is_robranch_super(sb, bindex);
10942 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10943 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10944 + lower_dir = lower_dir_dentry->d_inode;
10945 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10947 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10948 + err = do_delete_whiteouts(dentry, bindex, namelist);
10950 + args.deletewh.namelist = namelist;
10951 + args.deletewh.dentry = dentry;
10952 + args.deletewh.bindex = bindex;
10953 + run_sioq(__delete_whiteouts, &args);
10961 +/****************************************************************************
10962 + * Opaque directory helpers *
10963 + ****************************************************************************/
10966 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
10967 + * -errno if an error occurred trying to figure this out.
10969 +int is_opaque_dir(struct dentry *dentry, int bindex)
10972 + struct dentry *lower_dentry;
10973 + struct dentry *wh_lower_dentry;
10974 + struct inode *lower_inode;
10975 + struct sioq_args args;
10977 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10978 + lower_inode = lower_dentry->d_inode;
10980 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
10982 + mutex_lock(&lower_inode->i_mutex);
10984 + if (!inode_permission(lower_inode, MAY_EXEC)) {
10985 + wh_lower_dentry =
10986 + lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
10987 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
10989 + args.is_opaque.dentry = lower_dentry;
10990 + run_sioq(__is_opaque_dir, &args);
10991 + wh_lower_dentry = args.ret;
10994 + mutex_unlock(&lower_inode->i_mutex);
10996 + if (IS_ERR(wh_lower_dentry)) {
10997 + err = PTR_ERR(wh_lower_dentry);
11001 + /* This is an opaque dir iff wh_lower_dentry is positive */
11002 + err = !!wh_lower_dentry->d_inode;
11004 + dput(wh_lower_dentry);
11009 +void __is_opaque_dir(struct work_struct *work)
11011 + struct sioq_args *args = container_of(work, struct sioq_args, work);
11013 + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
11014 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11015 + complete(&args->comp);
11018 +int make_dir_opaque(struct dentry *dentry, int bindex)
11021 + struct dentry *lower_dentry, *diropq;
11022 + struct inode *lower_dir;
11023 + struct nameidata nd;
11024 + kernel_cap_t orig_cap;
11027 + * Opaque directory whiteout markers are special files (like regular
11028 + * whiteouts), and should appear to the users as if they don't
11029 + * exist. They should be created/deleted regardless of directory
11030 + * search/create permissions, but only for the duration of this
11031 + * creation of the .wh.__dir_opaque: file. Note, this does not
11032 + * circumvent normal ->permission).
11034 + orig_cap = current->cap_effective;
11035 + cap_raise(current->cap_effective, CAP_DAC_READ_SEARCH);
11036 + cap_raise(current->cap_effective, CAP_DAC_OVERRIDE);
11038 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11039 + lower_dir = lower_dentry->d_inode;
11040 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
11041 + !S_ISDIR(lower_dir->i_mode));
11043 + mutex_lock(&lower_dir->i_mutex);
11044 + diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
11045 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11046 + if (IS_ERR(diropq)) {
11047 + err = PTR_ERR(diropq);
11051 + err = init_lower_nd(&nd, LOOKUP_CREATE);
11052 + if (unlikely(err < 0))
11054 + if (!diropq->d_inode)
11055 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
11057 + dbopaque(dentry) = bindex;
11058 + release_lower_nd(&nd, err);
11063 + mutex_unlock(&lower_dir->i_mutex);
11064 + current->cap_effective = orig_cap;
11067 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
11068 new file mode 100644
11069 index 0000000..e2215c1
11071 +++ b/fs/unionfs/xattr.c
11074 + * Copyright (c) 2003-2008 Erez Zadok
11075 + * Copyright (c) 2003-2006 Charles P. Wright
11076 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11077 + * Copyright (c) 2005-2006 Junjiro Okajima
11078 + * Copyright (c) 2005 Arun M. Krishnakumar
11079 + * Copyright (c) 2004-2006 David P. Quigley
11080 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
11081 + * Copyright (c) 2003 Puja Gupta
11082 + * Copyright (c) 2003 Harikesavan Krishnan
11083 + * Copyright (c) 2003-2008 Stony Brook University
11084 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
11086 + * This program is free software; you can redistribute it and/or modify
11087 + * it under the terms of the GNU General Public License version 2 as
11088 + * published by the Free Software Foundation.
11091 +#include "union.h"
11093 +/* This is lifted from fs/xattr.c */
11094 +void *unionfs_xattr_alloc(size_t size, size_t limit)
11098 + if (size > limit)
11099 + return ERR_PTR(-E2BIG);
11101 + if (!size) /* size request, no buffer is needed */
11104 + ptr = kmalloc(size, GFP_KERNEL);
11105 + if (unlikely(!ptr))
11106 + return ERR_PTR(-ENOMEM);
11111 + * BKL held by caller.
11112 + * dentry->d_inode->i_mutex locked
11114 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11117 + struct dentry *lower_dentry = NULL;
11118 + struct dentry *parent;
11119 + int err = -EOPNOTSUPP;
11122 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11123 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11124 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11126 + valid = __unionfs_d_revalidate(dentry, parent, false);
11127 + if (unlikely(!valid)) {
11132 + lower_dentry = unionfs_lower_dentry(dentry);
11134 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11137 + unionfs_check_dentry(dentry);
11138 + unionfs_unlock_dentry(dentry);
11139 + unionfs_unlock_parent(dentry, parent);
11140 + unionfs_read_unlock(dentry->d_sb);
11145 + * BKL held by caller.
11146 + * dentry->d_inode->i_mutex locked
11148 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11149 + const void *value, size_t size, int flags)
11151 + struct dentry *lower_dentry = NULL;
11152 + struct dentry *parent;
11153 + int err = -EOPNOTSUPP;
11156 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11157 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11158 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11160 + valid = __unionfs_d_revalidate(dentry, parent, false);
11161 + if (unlikely(!valid)) {
11166 + lower_dentry = unionfs_lower_dentry(dentry);
11168 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11172 + unionfs_check_dentry(dentry);
11173 + unionfs_unlock_dentry(dentry);
11174 + unionfs_unlock_parent(dentry, parent);
11175 + unionfs_read_unlock(dentry->d_sb);
11180 + * BKL held by caller.
11181 + * dentry->d_inode->i_mutex locked
11183 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11185 + struct dentry *lower_dentry = NULL;
11186 + struct dentry *parent;
11187 + int err = -EOPNOTSUPP;
11190 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11191 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11192 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11194 + valid = __unionfs_d_revalidate(dentry, parent, false);
11195 + if (unlikely(!valid)) {
11200 + lower_dentry = unionfs_lower_dentry(dentry);
11202 + err = vfs_removexattr(lower_dentry, (char *) name);
11205 + unionfs_check_dentry(dentry);
11206 + unionfs_unlock_dentry(dentry);
11207 + unionfs_unlock_parent(dentry, parent);
11208 + unionfs_read_unlock(dentry->d_sb);
11213 + * BKL held by caller.
11214 + * dentry->d_inode->i_mutex locked
11216 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11218 + struct dentry *lower_dentry = NULL;
11219 + struct dentry *parent;
11220 + int err = -EOPNOTSUPP;
11221 + char *encoded_list = NULL;
11224 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11225 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11226 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11228 + valid = __unionfs_d_revalidate(dentry, parent, false);
11229 + if (unlikely(!valid)) {
11234 + lower_dentry = unionfs_lower_dentry(dentry);
11236 + encoded_list = list;
11237 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11240 + unionfs_check_dentry(dentry);
11241 + unionfs_unlock_dentry(dentry);
11242 + unionfs_unlock_parent(dentry, parent);
11243 + unionfs_read_unlock(dentry->d_sb);
11246 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
11247 index bb516ce..6615a52 100644
11248 --- a/include/linux/fs_stack.h
11249 +++ b/include/linux/fs_stack.h
11252 + * Copyright (c) 2006-2007 Erez Zadok
11253 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11254 + * Copyright (c) 2006-2007 Stony Brook University
11255 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
11257 + * This program is free software; you can redistribute it and/or modify
11258 + * it under the terms of the GNU General Public License version 2 as
11259 + * published by the Free Software Foundation.
11262 #ifndef _LINUX_FS_STACK_H
11263 #define _LINUX_FS_STACK_H
11265 -/* This file defines generic functions used primarily by stackable
11267 + * This file defines generic functions used primarily by stackable
11268 * filesystems; none of these functions require i_mutex to be held.
11271 #include <linux/fs.h>
11273 /* externs for fs/stack.c */
11274 -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
11275 - int (*get_nlinks)(struct inode *));
11277 -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
11278 +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
11279 +extern void fsstack_copy_inode_size(struct inode *dst, struct inode *src);
11282 static inline void fsstack_copy_attr_atime(struct inode *dest,
11283 diff --git a/include/linux/magic.h b/include/linux/magic.h
11284 index 1fa0c2c..67043ed 100644
11285 --- a/include/linux/magic.h
11286 +++ b/include/linux/magic.h
11288 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11289 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11291 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11293 #define SMB_SUPER_MAGIC 0x517B
11294 #define USBDEVICE_SUPER_MAGIC 0x9fa2
11295 #define CGROUP_SUPER_MAGIC 0x27e0eb
11296 diff --git a/include/linux/splice.h b/include/linux/splice.h
11297 index 528dcb9..4b5727c 100644
11298 --- a/include/linux/splice.h
11299 +++ b/include/linux/splice.h
11300 @@ -70,5 +70,10 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
11301 struct splice_pipe_desc *);
11302 extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11303 splice_direct_actor *);
11304 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11305 + loff_t *ppos, size_t len, unsigned int flags);
11306 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11307 + struct pipe_inode_info *pipe, size_t len,
11308 + unsigned int flags);
11311 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
11312 new file mode 100644
11313 index 0000000..bc15a16
11315 +++ b/include/linux/union_fs.h
11318 + * Copyright (c) 2003-2008 Erez Zadok
11319 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11320 + * Copyright (c) 2003-2008 Stony Brook University
11321 + * Copyright (c) 2003-2008 The Research Foundation of SUNY
11323 + * This program is free software; you can redistribute it and/or modify
11324 + * it under the terms of the GNU General Public License version 2 as
11325 + * published by the Free Software Foundation.
11328 +#ifndef _LINUX_UNION_FS_H
11329 +#define _LINUX_UNION_FS_H
11332 + * DEFINITIONS FOR USER AND KERNEL CODE:
11334 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11335 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11337 +#endif /* _LINUX_UNIONFS_H */