]> git.pld-linux.org Git - packages/kernel.git/blob - linux-2.6-unionfs-2.1.1.patch
- applies
[packages/kernel.git] / linux-2.6-unionfs-2.1.1.patch
1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index 59db1bc..40816d4 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -86,6 +86,8 @@ udf.txt
6         - info and mount options for the UDF filesystem.
7  ufs.txt
8         - info on the ufs filesystem.
9 +unionfs/
10 +       - info on the unionfs filesystem
11  vfat.txt
12         - info on using the VFAT filesystem used in Windows NT and Windows 95
13  vfs.txt
14 diff --git a/Documentation/filesystems/unionfs/00-INDEX b/Documentation/filesystems/unionfs/00-INDEX
15 new file mode 100644
16 index 0000000..96fdf67
17 --- /dev/null
18 +++ b/Documentation/filesystems/unionfs/00-INDEX
19 @@ -0,0 +1,10 @@
20 +00-INDEX
21 +       - this file.
22 +concepts.txt
23 +       - A brief introduction of concepts.
24 +issues.txt
25 +       - A summary of known issues with unionfs.
26 +rename.txt
27 +       - Information regarding rename operations.
28 +usage.txt
29 +       - Usage information and examples.
30 diff --git a/Documentation/filesystems/unionfs/concepts.txt b/Documentation/filesystems/unionfs/concepts.txt
31 new file mode 100644
32 index 0000000..37a62d8
33 --- /dev/null
34 +++ b/Documentation/filesystems/unionfs/concepts.txt
35 @@ -0,0 +1,181 @@
36 +Unionfs 2.1 CONCEPTS:
37 +=====================
38 +
39 +This file describes the concepts needed by a namespace unification file
40 +system.
41 +
42 +
43 +Branch Priority:
44 +================
45 +
46 +Each branch is assigned a unique priority - starting from 0 (highest
47 +priority).  No two branches can have the same priority.
48 +
49 +
50 +Branch Mode:
51 +============
52 +
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.
55 +
56 +
57 +Whiteouts:
58 +==========
59 +
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.
62 +
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:
65 +
66 +./b0/
67 +./b1/
68 +./b1/foo
69 +
70 +The unified view would simply be:
71 +
72 +./union/
73 +./union/foo
74 +
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
79 +there.
80 +
81 +./b0/
82 +./b0/.wh.foo
83 +./b1/
84 +./b1/foo
85 +
86 +Later, when Unionfs traverses branches (due to lookup or readdir), it
87 +eliminate 'foo' from the namespace (as well as the whiteout itself.)
88 +
89 +
90 +Duplicate Elimination:
91 +======================
92 +
93 +It is possible for files on different branches to have the same name.
94 +Unionfs then has to select which instance of the file to show to the user.
95 +Given the fact that each branch has a priority associated with it, the
96 +simplest solution is to take the instance from the highest priority
97 +(numerically lowest value) and "hide" the others.
98 +
99 +
100 +Copyup:
101 +=======
102 +
103 +When a change is made to the contents of a file's data or meta-data, they
104 +have to be stored somewhere. The best way is to create a copy of the
105 +original file on a branch that is writable, and then redirect the write
106 +though to this copy. The copy must be made on a higher priority branch so
107 +that lookup and readdir return this newer "version" of the file rather than
108 +the original (see duplicate elimination).
109 +
110 +
111 +Cache Coherency:
112 +================
113 +
114 +Unionfs users often want to be able to modify files and directories directly
115 +on the lower branches, and have those changes be visible at the Unionfs
116 +level.  This means that data (e.g., pages) and meta-data (dentries, inodes,
117 +open files, etc.) have to be synchronized between the upper and lower
118 +layers.  In other words, the newest changes from a layer below have to be
119 +propagated to the Unionfs layer above.  If the two layers are not in sync, a
120 +cache incoherency ensues, which could lead to application failures and even
121 +oopses.  The Linux kernel, however, has a rather limited set of mechanisms
122 +to ensure this inter-layer cache coherency---so Unionfs has to do most of
123 +the hard work on its own.
124 +
125 +Maintaining Invariants:
126 +
127 +The way Unionfs ensures cache coherency is as follows.  At each entry point
128 +to a Unionfs file system method, we call a utility function to validate the
129 +primary objects of this method.  Generally, we call unionfs_file_revalidate
130 +on open files, and __unionfs_d_revalidate_chain on dentries (which also
131 +validates inodes).  These utility functions check to see whether the upper
132 +Unionfs object is in sync with any of the lower objects that it represents.
133 +The checks we perform include whether the Unionfs superblock has a newer
134 +generation number, or if any of the lower objects mtime's or ctime's are
135 +newer.  (Note: generation numbers change when branch-management commands are
136 +issued, so in a way, maintaining cache coherency is also very important for
137 +branch-management.)  If indeed we determine that any Unionfs object is no
138 +longer in sync with its lower counterparts, then we rebuild that object
139 +similarly to how we do so for branch-management.
140 +
141 +While rebuilding Unionfs's objects, we also purge any page mappings and
142 +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data).  This is to
143 +ensure that Unionfs will re-get the newer data from the lower branches.  We
144 +perform this purging only if the Unionfs operation in question is a reading
145 +operation; if Unionfs is performing a data writing operation (e.g., ->write,
146 +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
147 +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
148 +considered more authoritative anyway, as they are newer and will overwrite
149 +any lower pages.
150 +
151 +Unionfs maintains the following important invariant regarding mtime's,
152 +ctime's, and atime's: the upper inode object's times are the max() of all of
153 +the lower ones.  For non-directory objects, there's only one object below,
154 +so the mapping is simple; for directory objects, there could me multiple
155 +lower objects and we have to sync up with the newest one of all the lower
156 +ones.  This invariant is important to maintain, especially for directories
157 +(besides, we need this to be POSIX compliant).  A union could comprise
158 +multiple writable branches, each of which could change.  If we don't reflect
159 +the newest possible mtime/ctime, some applications could fail.  For example,
160 +NFSv2/v3 exports check for newer directory mtimes on the server to determine
161 +if the client-side attribute cache should be purged.
162 +
163 +To maintain these important invariants, of course, Unionfs carefully
164 +synchronizes upper and lower times in various places.  For example, if we
165 +copy-up a file to a top-level branch, the parent directory where the file
166 +was copied up to will now have a new mtime: so after a successful copy-up,
167 +we sync up with the new top-level branch's parent directory mtime.
168 +
169 +Implementation:
170 +
171 +This cache-coherency implementation is efficient because it defers any
172 +synchronizing between the upper and lower layers until absolutely needed.
173 +Consider the example a common situation where users perform a lot of lower
174 +changes, such as untarring a whole package.  While these take place,
175 +typically the user doesn't access the files via Unionfs; only after the
176 +lower changes are done, does the user try to access the lower files.  With
177 +our cache-coherency implementation, the entirety of the changes to the lower
178 +branches will not result in a single CPU cycle spent at the Unionfs level
179 +until the user invokes a system call that goes through Unionfs.
180 +
181 +We have considered two alternate cache-coherency designs.  (1) Using the
182 +dentry/inode notify functionality to register interest in finding out about
183 +any lower changes.  This is a somewhat limited and also a heavy-handed
184 +approach which could result in many notifications to the Unionfs layer upon
185 +each small change at the lower layer (imagine a file being modified multiple
186 +times in rapid succession).  (2) Rewriting the VFS to support explicit
187 +callbacks from lower objects to upper objects.  We began exploring such an
188 +implementation, but found it to be very complicated--it would have resulted
189 +in massive VFS/MM changes which are unlikely to be accepted by the LKML
190 +community.  We therefore believe that our current cache-coherency design and
191 +implementation represent the best approach at this time.
192 +
193 +Limitations:
194 +
195 +Our implementation works in that as long as a user process will have caused
196 +Unionfs to be called, directly or indirectly, even to just do
197 +->d_revalidate; then we will have purged the current Unionfs data and the
198 +process will see the new data.  For example, a process that continually
199 +re-reads the same file's data will see the NEW data as soon as the lower
200 +file had changed, upon the next read(2) syscall (even if the file is still
201 +open!)  However, this doesn't work when the process re-reads the open file's
202 +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
203 +it).  Once we respond to ->readpage(s), then the kernel maps the page into
204 +the process's address space and there doesn't appear to be a way to force
205 +the kernel to invalidate those pages/mappings, and force the process to
206 +re-issue ->readpage.  If there's a way to invalidate active mappings and
207 +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
208 +the trick).
209 +
210 +Our current Unionfs code has to perform many file-revalidation calls.  It
211 +would be really nice if the VFS would export an optional file system hook
212 +->file_revalidate (similarly to dentry->d_revalidate) that will be called
213 +before each VFS op that has a "struct file" in it.
214 +
215 +
216 +For more information, see <http://unionfs.filesystems.org/>.
217 diff --git a/Documentation/filesystems/unionfs/issues.txt b/Documentation/filesystems/unionfs/issues.txt
218 new file mode 100644
219 index 0000000..9db1d70
220 --- /dev/null
221 +++ b/Documentation/filesystems/unionfs/issues.txt
222 @@ -0,0 +1,24 @@
223 +KNOWN Unionfs 2.1 ISSUES:
224 +=========================
225 +
226 +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
227 +   confuses NFSv4.  Currently, unionfs_lookup() passes lookup intents to the
228 +   lower file-system, this eliminates part of the problem.  The remaining
229 +   calls to lookup_one_len may need to be changed to pass an intent.  We are
230 +   currently introducing VFS changes to fs/namei.c's do_path_lookup() to
231 +   allow proper file lookup and opening in stackable file systems.
232 +
233 +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
234 +   incorrectly complains about locking problems.  The problem boils down to
235 +   this: Lockdep considers all objects of a certain type to be in the same
236 +   class, for example, all inodes.  Lockdep doesn't like to see a lock held
237 +   on two inodes within the same task, and warns that it could lead to a
238 +   deadlock.  However, stackable file systems do precisely that: they lock
239 +   an upper object, and then a lower object, in a strict order to avoid
240 +   locking problems; in addition, Unionfs, as a fan-out file system, may
241 +   have to lock several lower inodes.  We are currently looking into Lockdep
242 +   to see how to make it aware of stackable file systems.  In the meantime,
243 +   if you get any warnings from Lockdep, you can safely ignore them (or feel
244 +   free to report them to the Unionfs maintainers, just to be sure).
245 +
246 +For more information, see <http://unionfs.filesystems.org/>.
247 diff --git a/Documentation/filesystems/unionfs/rename.txt b/Documentation/filesystems/unionfs/rename.txt
248 new file mode 100644
249 index 0000000..e20bb82
250 --- /dev/null
251 +++ b/Documentation/filesystems/unionfs/rename.txt
252 @@ -0,0 +1,31 @@
253 +Rename is a complex beast. The following table shows which rename(2) operations
254 +should succeed and which should fail.
255 +
256 +o: success
257 +E: error (either unionfs or vfs)
258 +X: EXDEV
259 +
260 +none = file does not exist
261 +file = file is a file
262 +dir  = file is a empty directory
263 +child= file is a non-empty directory
264 +wh   = file is a directory containing only whiteouts; this makes it logically
265 +               empty
266 +
267 +                      none    file    dir     child   wh
268 +file                  o       o       E       E       E
269 +dir                   o       E       o       E       o
270 +child                 X       E       X       E       X
271 +wh                    o       E       o       E       o
272 +
273 +
274 +Renaming directories:
275 +=====================
276 +
277 +Whenever a empty (either physically or logically) directory is being renamed,
278 +the following sequence of events should take place:
279 +
280 +1) Remove whiteouts from both source and destination directory
281 +2) Rename source to destination
282 +3) Make destination opaque to prevent anything under it from showing up
283 +
284 diff --git a/Documentation/filesystems/unionfs/usage.txt b/Documentation/filesystems/unionfs/usage.txt
285 new file mode 100644
286 index 0000000..d8c15de
287 --- /dev/null
288 +++ b/Documentation/filesystems/unionfs/usage.txt
289 @@ -0,0 +1,98 @@
290 +Unionfs is a stackable unification file system, which can appear to merge
291 +the contents of several directories (branches), while keeping their physical
292 +content separate.  Unionfs is useful for unified source tree management,
293 +merged contents of split CD-ROM, merged separate software package
294 +directories, data grids, and more.  Unionfs allows any mix of read-only and
295 +read-write branches, as well as insertion and deletion of branches anywhere
296 +in the fan-out.  To maintain Unix semantics, Unionfs handles elimination of
297 +duplicates, partial-error conditions, and more.
298 +
299 +# mount -t unionfs -o branch-option[,union-options[,...]] none MOUNTPOINT
300 +
301 +The available branch-option for the mount command is:
302 +
303 +       dirs=branch[=ro|=rw][:...]
304 +
305 +specifies a separated list of which directories compose the union.
306 +Directories that come earlier in the list have a higher precedence than
307 +those which come later. Additionally, read-only or read-write permissions of
308 +the branch can be specified by appending =ro or =rw (default) to each
309 +directory.
310 +
311 +Syntax:
312 +
313 +       dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
314 +
315 +Example:
316 +
317 +       dirs=/writable_branch=rw:/read-only_branch=ro
318 +
319 +
320 +DYNAMIC BRANCH MANAGEMENT AND REMOUNTS
321 +======================================
322 +
323 +You can remount a union and change its overall mode, or reconfigure the
324 +branches, as follows.
325 +
326 +To downgrade a union from read-write to read-only:
327 +
328 +# mount -t unionfs -o remount,ro none MOUNTPOINT
329 +
330 +To upgrade a union from read-only to read-write:
331 +
332 +# mount -t unionfs -o remount,rw none MOUNTPOINT
333 +
334 +To delete a branch /foo, regardless where it is in the current union:
335 +
336 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
337 +
338 +To insert (add) a branch /foo before /bar:
339 +
340 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
341 +
342 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
343 +
344 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
345 +
346 +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
347 +new highest-priority branch), you can use the above syntax, or use a short
348 +hand version as follows:
349 +
350 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
351 +
352 +To append a branch to the very end (new lowest-priority branch):
353 +
354 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
355 +
356 +To append a branch to the very end (new lowest-priority branch), in
357 +read-only mode:
358 +
359 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
360 +
361 +Finally, to change the mode of one existing branch, say /foo, from read-only
362 +to read-write, and change /bar from read-write to read-only:
363 +
364 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
365 +
366 +
367 +CACHE CONSISTENCY
368 +=================
369 +
370 +If you modify any file on any of the lower branches directly, while there is
371 +a Unionfs 2.1 mounted above any of those branches, you should tell Unionfs
372 +to purge its caches and re-get the objects.  To do that, you have to
373 +increment the generation number of the superblock using the following
374 +command:
375 +
376 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
377 +
378 +Note that the older way of incrementing the generation number using an
379 +ioctl, is no longer supported in Unionfs 2.0 and newer.  Ioctls in general
380 +are not encouraged.  Plus, an ioctl is per-file concept, whereas the
381 +generation number is a per-file-system concept.  Worse, such an ioctl
382 +requires an open file, which then has to be invalidated by the very nature
383 +of the generation number increase (read: the old generation increase ioctl
384 +was pretty racy).
385 +
386 +
387 +For more information, see <http://unionfs.filesystems.org/>.
388 diff --git a/MAINTAINERS b/MAINTAINERS
389 index 9a91d9e..7e45e87 100644
390 --- a/MAINTAINERS
391 +++ b/MAINTAINERS
392 @@ -3740,6 +3740,15 @@ L:       linux-kernel@vger.kernel.org
393  W:     http://www.kernel.dk
394  S:     Maintained
395  
396 +UNIONFS
397 +P:     Erez Zadok
398 +M:     ezk@cs.sunysb.edu
399 +P:     Josef "Jeff" Sipek
400 +M:     jsipek@cs.sunysb.edu
401 +L:     unionfs@filesystems.org
402 +W:     http://unionfs.filesystems.org
403 +S:     Maintained
404 +
405  USB ACM DRIVER
406  P:     Oliver Neukum
407  M:     oliver@neukum.name
408 diff --git a/fs/Kconfig b/fs/Kconfig
409 index f9eed6d..9afb8df 100644
410 --- a/fs/Kconfig
411 +++ b/fs/Kconfig
412 @@ -1027,6 +1027,47 @@ config CONFIGFS_FS
413  
414  endmenu
415  
416 +menu "Layered filesystems"
417 +
418 +config ECRYPT_FS
419 +       tristate "eCrypt filesystem layer support (EXPERIMENTAL)"
420 +       depends on EXPERIMENTAL && KEYS && CRYPTO && NET
421 +       help
422 +         Encrypted filesystem that operates on the VFS layer.  See
423 +         <file:Documentation/ecryptfs.txt> to learn more about
424 +         eCryptfs.  Userspace components are required and can be
425 +         obtained from <http://ecryptfs.sf.net>.
426 +
427 +         To compile this file system support as a module, choose M here: the
428 +         module will be called ecryptfs.
429 +
430 +config UNION_FS
431 +       tristate "Union file system (EXPERIMENTAL)"
432 +       depends on EXPERIMENTAL
433 +       help
434 +         Unionfs is a stackable unification file system, which appears to
435 +         merge the contents of several directories (branches), while keeping
436 +         their physical content separate.
437 +
438 +         See <http://unionfs.filesystems.org> for details
439 +
440 +config UNION_FS_XATTR
441 +       bool "Unionfs extended attributes"
442 +       depends on UNION_FS
443 +       help
444 +         Extended attributes are name:value pairs associated with inodes by
445 +         the kernel or by users (see the attr(5) manual page).
446 +
447 +         If unsure, say N.
448 +
449 +config UNION_FS_DEBUG
450 +       bool "Debug Unionfs"
451 +       depends on UNION_FS
452 +       help
453 +         If you say Y here, you can turn on debugging output from Unionfs.
454 +
455 +endmenu
456 +
457  menu "Miscellaneous filesystems"
458  
459  config ADFS_FS
460 @@ -1134,18 +1134,6 @@
461           To compile this file system support as a module, choose M here: the
462           module will be called affs.  If unsure, say N.
463  
464 -config ECRYPT_FS
465 -       tristate "eCrypt filesystem layer support (EXPERIMENTAL)"
466 -       depends on EXPERIMENTAL && KEYS && CRYPTO && NET
467 -       help
468 -         Encrypted filesystem that operates on the VFS layer.  See
469 -         <file:Documentation/filesystems/ecryptfs.txt> to learn more about
470 -         eCryptfs.  Userspace components are required and can be
471 -         obtained from <http://ecryptfs.sf.net>.
472 -
473 -         To compile this file system support as a module, choose M here: the
474 -         module will be called ecryptfs.
475 -
476  config HFS_FS
477         tristate "Apple Macintosh file system support (EXPERIMENTAL)"
478         depends on BLOCK && EXPERIMENTAL
479 diff --git a/fs/Makefile b/fs/Makefile
480 index 720c29d..951f411 100644
481 --- a/fs/Makefile
482 +++ b/fs/Makefile
483 @@ -118,3 +118,4 @@ obj-$(CONFIG_HPPFS)         += hppfs/
484  obj-$(CONFIG_DEBUG_FS)         += debugfs/
485  obj-$(CONFIG_OCFS2_FS)         += ocfs2/
486  obj-$(CONFIG_GFS2_FS)           += gfs2/
487 +obj-$(CONFIG_UNION_FS)         += unionfs/
488 diff --git a/fs/drop_caches.c b/fs/drop_caches.c
489 index 59375ef..90410ac 100644
490 --- a/fs/drop_caches.c
491 +++ b/fs/drop_caches.c
492 @@ -3,6 +3,7 @@
493   */
494  
495  #include <linux/kernel.h>
496 +#include <linux/module.h>
497  #include <linux/mm.h>
498  #include <linux/fs.h>
499  #include <linux/writeback.h>
500 @@ -12,7 +13,7 @@
501  /* A global variable is a bit ugly, but it keeps the code simple */
502  int sysctl_drop_caches;
503  
504 -static void drop_pagecache_sb(struct super_block *sb)
505 +void drop_pagecache_sb(struct super_block *sb)
506  {
507         struct inode *inode;
508  
509 @@ -24,6 +25,7 @@ static void drop_pagecache_sb(struct super_block *sb)
510         }
511         spin_unlock(&inode_lock);
512  }
513 +EXPORT_SYMBOL(drop_pagecache_sb);
514  
515  void drop_pagecache(void)
516  {
517 diff --git a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
518 index cb20b96..a8c1686 100644
519 --- a/fs/ecryptfs/dentry.c
520 +++ b/fs/ecryptfs/dentry.c
521 @@ -62,7 +62,7 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
522                 struct inode *lower_inode =
523                         ecryptfs_inode_to_lower(dentry->d_inode);
524  
525 -               fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
526 +               fsstack_copy_attr_all(dentry->d_inode, lower_inode);
527         }
528  out:
529         return rc;
530 diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
531 index 131954b..fc4c6cb 100644
532 --- a/fs/ecryptfs/inode.c
533 +++ b/fs/ecryptfs/inode.c
534 @@ -601,9 +601,9 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
535                         lower_new_dir_dentry->d_inode, lower_new_dentry);
536         if (rc)
537                 goto out_lock;
538 -       fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
539 +       fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
540         if (new_dir != old_dir)
541 -               fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
542 +               fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
543  out_lock:
544         unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
545         dput(lower_new_dentry->d_parent);
546 @@ -961,7 +961,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
547         }
548         rc = notify_change(lower_dentry, ia);
549  out:
550 -       fsstack_copy_attr_all(inode, lower_inode, NULL);
551 +       fsstack_copy_attr_all(inode, lower_inode);
552         return rc;
553  }
554  
555 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
556 index a984972..cb349a4 100644
557 --- a/fs/ecryptfs/main.c
558 +++ b/fs/ecryptfs/main.c
559 @@ -151,7 +151,7 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
560                 d_add(dentry, inode);
561         else
562                 d_instantiate(dentry, inode);
563 -       fsstack_copy_attr_all(inode, lower_inode, NULL);
564 +       fsstack_copy_attr_all(inode, lower_inode);
565         /* This size will be overwritten for real files w/ headers and
566          * other metadata */
567         fsstack_copy_inode_size(inode, lower_inode);
568 diff --git a/fs/namei.c b/fs/namei.c
569 index a83160a..b2b7c8e 100644
570 --- a/fs/namei.c
571 +++ b/fs/namei.c
572 @@ -374,6 +374,7 @@ void release_open_intent(struct nameidata *nd)
573         else
574                 fput(nd->intent.open.file);
575  }
576 +EXPORT_SYMBOL(release_open_intent);
577  
578  static inline struct dentry *
579  do_revalidate(struct dentry *dentry, struct nameidata *nd)
580 diff --git a/fs/stack.c b/fs/stack.c
581 index 67716f6..a548aac 100644
582 --- a/fs/stack.c
583 +++ b/fs/stack.c
584 @@ -1,8 +1,20 @@
585 +/*
586 + * Copyright (c) 2006-2007 Erez Zadok
587 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
588 + * Copyright (c) 2006-2007 Stony Brook University
589 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
590 + *
591 + * This program is free software; you can redistribute it and/or modify
592 + * it under the terms of the GNU General Public License version 2 as
593 + * published by the Free Software Foundation.
594 + */
595 +
596  #include <linux/module.h>
597  #include <linux/fs.h>
598  #include <linux/fs_stack.h>
599  
600 -/* does _NOT_ require i_mutex to be held.
601 +/*
602 + * does _NOT_ require i_mutex to be held.
603   *
604   * This function cannot be inlined since i_size_{read,write} is rather
605   * heavy-weight on 32-bit systems
606 @@ -14,11 +26,11 @@ void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
607  }
608  EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
609  
610 -/* copy all attributes; get_nlinks is optional way to override the i_nlink
611 +/*
612 + * copy all attributes; get_nlinks is optional way to override the i_nlink
613   * copying
614   */
615 -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
616 -                               int (*get_nlinks)(struct inode *))
617 +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
618  {
619         dest->i_mode = src->i_mode;
620         dest->i_uid = src->i_uid;
621 @@ -29,14 +41,6 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
622         dest->i_ctime = src->i_ctime;
623         dest->i_blkbits = src->i_blkbits;
624         dest->i_flags = src->i_flags;
625 -
626 -       /*
627 -        * Update the nlinks AFTER updating the above fields, because the
628 -        * get_links callback may depend on them.
629 -        */
630 -       if (!get_nlinks)
631 -               dest->i_nlink = src->i_nlink;
632 -       else
633 -               dest->i_nlink = (*get_nlinks)(dest);
634 +       dest->i_nlink = src->i_nlink;
635  }
636  EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
637 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
638 new file mode 100644
639 index 0000000..6c55790
640 --- /dev/null
641 +++ b/fs/unionfs/Makefile
642 @@ -0,0 +1,17 @@
643 +UNIONFS_VERSION="2.1.6 (for 2.6.23-rc8)"
644 +
645 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
646 +
647 +obj-$(CONFIG_UNION_FS) += unionfs.o
648 +
649 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
650 +       rdstate.o copyup.o dirhelper.o rename.o unlink.o \
651 +       lookup.o commonfops.o dirfops.o sioq.o mmap.o
652 +
653 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
654 +
655 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
656 +
657 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
658 +EXTRA_CFLAGS += -DDEBUG
659 +endif
660 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
661 new file mode 100644
662 index 0000000..7654bcb
663 --- /dev/null
664 +++ b/fs/unionfs/commonfops.c
665 @@ -0,0 +1,843 @@
666 +/*
667 + * Copyright (c) 2003-2007 Erez Zadok
668 + * Copyright (c) 2003-2006 Charles P. Wright
669 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
670 + * Copyright (c) 2005-2006 Junjiro Okajima
671 + * Copyright (c) 2005      Arun M. Krishnakumar
672 + * Copyright (c) 2004-2006 David P. Quigley
673 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
674 + * Copyright (c) 2003      Puja Gupta
675 + * Copyright (c) 2003      Harikesavan Krishnan
676 + * Copyright (c) 2003-2007 Stony Brook University
677 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
678 + *
679 + * This program is free software; you can redistribute it and/or modify
680 + * it under the terms of the GNU General Public License version 2 as
681 + * published by the Free Software Foundation.
682 + */
683 +
684 +#include "union.h"
685 +
686 +/*
687 + * 1) Copyup the file
688 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
689 + * stolen from NFS's silly rename
690 + */
691 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
692 +                              int bstart, int bindex)
693 +{
694 +       static unsigned int counter;
695 +       const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
696 +       const int countersize = sizeof(counter) * 2;
697 +       const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
698 +       char name[nlen + 1];
699 +       int err;
700 +       struct dentry *tmp_dentry = NULL;
701 +       struct dentry *lower_dentry;
702 +       struct dentry *lower_dir_dentry = NULL;
703 +
704 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
705 +
706 +       sprintf(name, ".unionfs%*.*lx",
707 +               i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
708 +
709 +       /*
710 +        * Loop, looking for an unused temp name to copyup to.
711 +        *
712 +        * It's somewhat silly that we look for a free temp tmp name in the
713 +        * source branch (bstart) instead of the dest branch (bindex), where
714 +        * the final name will be created.  We _will_ catch it if somehow
715 +        * the name exists in the dest branch, but it'd be nice to catch it
716 +        * sooner than later.
717 +        */
718 +retry:
719 +       tmp_dentry = NULL;
720 +       do {
721 +               char *suffix = name + nlen - countersize;
722 +
723 +               dput(tmp_dentry);
724 +               counter++;
725 +               sprintf(suffix, "%*.*x", countersize, countersize, counter);
726 +
727 +               pr_debug("unionfs: trying to rename %s to %s\n",
728 +                        dentry->d_name.name, name);
729 +
730 +               tmp_dentry = lookup_one_len(name, lower_dentry->d_parent,
731 +                                           nlen);
732 +               if (IS_ERR(tmp_dentry)) {
733 +                       err = PTR_ERR(tmp_dentry);
734 +                       goto out;
735 +               }
736 +       } while (tmp_dentry->d_inode != NULL);  /* need negative dentry */
737 +       dput(tmp_dentry);
738 +
739 +       err = copyup_named_file(dentry->d_parent->d_inode, file, name, bstart,
740 +                               bindex, file->f_path.dentry->d_inode->i_size);
741 +       if (err) {
742 +               if (unlikely(err == -EEXIST))
743 +                       goto retry;
744 +               goto out;
745 +       }
746 +
747 +       /* bring it to the same state as an unlinked file */
748 +       lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
749 +       if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
750 +               atomic_inc(&lower_dentry->d_inode->i_count);
751 +               unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
752 +                                           lower_dentry->d_inode);
753 +       }
754 +       lower_dir_dentry = lock_parent(lower_dentry);
755 +       err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
756 +       unlock_dir(lower_dir_dentry);
757 +
758 +out:
759 +       if (!err)
760 +               unionfs_check_dentry(dentry);
761 +       return err;
762 +}
763 +
764 +/*
765 + * put all references held by upper struct file and free lower file pointer
766 + * array
767 + */
768 +static void cleanup_file(struct file *file)
769 +{
770 +       int bindex, bstart, bend;
771 +       struct file **lower_files;
772 +       struct file *lower_file;
773 +       struct super_block *sb = file->f_path.dentry->d_sb;
774 +
775 +       lower_files = UNIONFS_F(file)->lower_files;
776 +       bstart = fbstart(file);
777 +       bend = fbend(file);
778 +
779 +       for (bindex = bstart; bindex <= bend; bindex++) {
780 +               int i;  /* holds (possibly) updated branch index */
781 +               int old_bid;
782 +
783 +               lower_file = unionfs_lower_file_idx(file, bindex);
784 +               if (!lower_file)
785 +                       continue;
786 +
787 +               /*
788 +                * Find new index of matching branch with an open
789 +                * file, since branches could have been added or
790 +                * deleted causing the one with open files to shift.
791 +                */
792 +               old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
793 +               i = branch_id_to_idx(sb, old_bid);
794 +               if (unlikely(i < 0)) {
795 +                       printk(KERN_ERR "unionfs: no superblock for "
796 +                              "file %p\n", file);
797 +                       continue;
798 +               }
799 +
800 +               /* decrement count of open files */
801 +               branchput(sb, i);
802 +               /*
803 +                * fput will perform an mntput for us on the correct branch.
804 +                * Although we're using the file's old branch configuration,
805 +                * bindex, which is the old index, correctly points to the
806 +                * right branch in the file's branch list.  In other words,
807 +                * we're going to mntput the correct branch even if branches
808 +                * have been added/removed.
809 +                */
810 +               fput(lower_file);
811 +               UNIONFS_F(file)->lower_files[bindex] = NULL;
812 +               UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
813 +       }
814 +
815 +       UNIONFS_F(file)->lower_files = NULL;
816 +       kfree(lower_files);
817 +       kfree(UNIONFS_F(file)->saved_branch_ids);
818 +       /* set to NULL because caller needs to know if to kfree on error */
819 +       UNIONFS_F(file)->saved_branch_ids = NULL;
820 +}
821 +
822 +/* open all lower files for a given file */
823 +static int open_all_files(struct file *file)
824 +{
825 +       int bindex, bstart, bend, err = 0;
826 +       struct file *lower_file;
827 +       struct dentry *lower_dentry;
828 +       struct dentry *dentry = file->f_path.dentry;
829 +       struct super_block *sb = dentry->d_sb;
830 +
831 +       bstart = dbstart(dentry);
832 +       bend = dbend(dentry);
833 +
834 +       for (bindex = bstart; bindex <= bend; bindex++) {
835 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
836 +               if (!lower_dentry)
837 +                       continue;
838 +
839 +               dget(lower_dentry);
840 +               unionfs_mntget(dentry, bindex);
841 +               branchget(sb, bindex);
842 +
843 +               lower_file =
844 +                       dentry_open(lower_dentry,
845 +                                   unionfs_lower_mnt_idx(dentry, bindex),
846 +                                   file->f_flags);
847 +               if (IS_ERR(lower_file)) {
848 +                       err = PTR_ERR(lower_file);
849 +                       goto out;
850 +               } else {
851 +                       unionfs_set_lower_file_idx(file, bindex, lower_file);
852 +               }
853 +       }
854 +out:
855 +       return err;
856 +}
857 +
858 +/* open the highest priority file for a given upper file */
859 +static int open_highest_file(struct file *file, bool willwrite)
860 +{
861 +       int bindex, bstart, bend, err = 0;
862 +       struct file *lower_file;
863 +       struct dentry *lower_dentry;
864 +       struct dentry *dentry = file->f_path.dentry;
865 +       struct inode *parent_inode = dentry->d_parent->d_inode;
866 +       struct super_block *sb = dentry->d_sb;
867 +       size_t inode_size = dentry->d_inode->i_size;
868 +
869 +       bstart = dbstart(dentry);
870 +       bend = dbend(dentry);
871 +
872 +       lower_dentry = unionfs_lower_dentry(dentry);
873 +       if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
874 +               for (bindex = bstart - 1; bindex >= 0; bindex--) {
875 +                       err = copyup_file(parent_inode, file, bstart, bindex,
876 +                                         inode_size);
877 +                       if (!err)
878 +                               break;
879 +               }
880 +               atomic_set(&UNIONFS_F(file)->generation,
881 +                          atomic_read(&UNIONFS_I(dentry->d_inode)->
882 +                                      generation));
883 +               goto out;
884 +       }
885 +
886 +       dget(lower_dentry);
887 +       unionfs_mntget(dentry, bstart);
888 +       lower_file = dentry_open(lower_dentry,
889 +                                unionfs_lower_mnt_idx(dentry, bstart),
890 +                                file->f_flags);
891 +       if (IS_ERR(lower_file)) {
892 +               err = PTR_ERR(lower_file);
893 +               goto out;
894 +       }
895 +       branchget(sb, bstart);
896 +       unionfs_set_lower_file(file, lower_file);
897 +       /* Fix up the position. */
898 +       lower_file->f_pos = file->f_pos;
899 +
900 +       memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
901 +out:
902 +       return err;
903 +}
904 +
905 +/* perform a delayed copyup of a read-write file on a read-only branch */
906 +static int do_delayed_copyup(struct file *file)
907 +{
908 +       int bindex, bstart, bend, err = 0;
909 +       struct dentry *dentry = file->f_path.dentry;
910 +       struct inode *parent_inode = dentry->d_parent->d_inode;
911 +       loff_t inode_size = dentry->d_inode->i_size;
912 +
913 +       bstart = fbstart(file);
914 +       bend = fbend(file);
915 +
916 +       BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
917 +
918 +       unionfs_check_file(file);
919 +       unionfs_check_dentry(dentry);
920 +       for (bindex = bstart - 1; bindex >= 0; bindex--) {
921 +               if (!d_deleted(dentry))
922 +                       err = copyup_file(parent_inode, file, bstart,
923 +                                         bindex, inode_size);
924 +               else
925 +                       err = copyup_deleted_file(file, dentry, bstart,
926 +                                                 bindex);
927 +
928 +               if (!err)
929 +                       break;
930 +       }
931 +       if (err || (bstart <= fbstart(file)))
932 +               goto out;
933 +       bend = fbend(file);
934 +       for (bindex = bstart; bindex <= bend; bindex++) {
935 +               if (unionfs_lower_file_idx(file, bindex)) {
936 +                       branchput(dentry->d_sb, bindex);
937 +                       fput(unionfs_lower_file_idx(file, bindex));
938 +                       unionfs_set_lower_file_idx(file, bindex, NULL);
939 +               }
940 +               if (unionfs_lower_mnt_idx(dentry, bindex)) {
941 +                       unionfs_mntput(dentry, bindex);
942 +                       unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
943 +               }
944 +               if (unionfs_lower_dentry_idx(dentry, bindex)) {
945 +                       BUG_ON(!dentry->d_inode);
946 +                       iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
947 +                       unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
948 +                                                   NULL);
949 +                       dput(unionfs_lower_dentry_idx(dentry, bindex));
950 +                       unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
951 +               }
952 +       }
953 +       /* for reg file, we only open it "once" */
954 +       fbend(file) = fbstart(file);
955 +       set_dbend(dentry, dbstart(dentry));
956 +       ibend(dentry->d_inode) = ibstart(dentry->d_inode);
957 +
958 +out:
959 +       unionfs_check_file(file);
960 +       unionfs_check_dentry(dentry);
961 +       return err;
962 +}
963 +
964 +/*
965 + * Revalidate the struct file
966 + * @file: file to revalidate
967 + * @willwrite: true if caller may cause changes to the file; false otherwise.
968 + */
969 +int unionfs_file_revalidate(struct file *file, bool willwrite)
970 +{
971 +       struct super_block *sb;
972 +       struct dentry *dentry;
973 +       int sbgen, fgen, dgen;
974 +       int bstart, bend;
975 +       int size;
976 +       int err = 0;
977 +
978 +       dentry = file->f_path.dentry;
979 +       unionfs_lock_dentry(dentry);
980 +       sb = dentry->d_sb;
981 +
982 +       /*
983 +        * First revalidate the dentry inside struct file,
984 +        * but not unhashed dentries.
985 +        */
986 +       if (unlikely(!d_deleted(dentry) &&
987 +                    !__unionfs_d_revalidate_chain(dentry, NULL, willwrite))) {
988 +               err = -ESTALE;
989 +               goto out_nofree;
990 +       }
991 +
992 +       sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
993 +       dgen = atomic_read(&UNIONFS_D(dentry)->generation);
994 +       fgen = atomic_read(&UNIONFS_F(file)->generation);
995 +
996 +       BUG_ON(sbgen > dgen);
997 +
998 +       /*
999 +        * There are two cases we are interested in.  The first is if the
1000 +        * generation is lower than the super-block.  The second is if
1001 +        * someone has copied up this file from underneath us, we also need
1002 +        * to refresh things.
1003 +        */
1004 +       if (unlikely(!d_deleted(dentry) &&
1005 +                    (sbgen > fgen || dbstart(dentry) != fbstart(file)))) {
1006 +               /* save orig branch ID */
1007 +               int orig_brid =
1008 +                       UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1009 +
1010 +               /* First we throw out the existing files. */
1011 +               cleanup_file(file);
1012 +
1013 +               /* Now we reopen the file(s) as in unionfs_open. */
1014 +               bstart = fbstart(file) = dbstart(dentry);
1015 +               bend = fbend(file) = dbend(dentry);
1016 +
1017 +               size = sizeof(struct file *) * sbmax(sb);
1018 +               UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1019 +               if (unlikely(!UNIONFS_F(file)->lower_files)) {
1020 +                       err = -ENOMEM;
1021 +                       goto out;
1022 +               }
1023 +               size = sizeof(int) * sbmax(sb);
1024 +               UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1025 +               if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1026 +                       err = -ENOMEM;
1027 +                       goto out;
1028 +               }
1029 +
1030 +               if (S_ISDIR(dentry->d_inode->i_mode)) {
1031 +                       /* We need to open all the files. */
1032 +                       err = open_all_files(file);
1033 +                       if (err)
1034 +                               goto out;
1035 +               } else {
1036 +                       int new_brid;
1037 +                       /* We only open the highest priority branch. */
1038 +                       err = open_highest_file(file, willwrite);
1039 +                       if (err)
1040 +                               goto out;
1041 +                       new_brid = UNIONFS_F(file)->
1042 +                         saved_branch_ids[fbstart(file)];
1043 +                       if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1044 +                               /*
1045 +                                * If we re-opened the file on a different
1046 +                                * branch than the original one, and this
1047 +                                * was due to a new branch inserted, then
1048 +                                * update the mnt counts of the old and new
1049 +                                * branches accordingly.
1050 +                                */
1051 +                               unionfs_mntget(dentry, bstart);
1052 +                               unionfs_mntput(sb->s_root,
1053 +                                              branch_id_to_idx(sb, orig_brid));
1054 +                       }
1055 +               }
1056 +               atomic_set(&UNIONFS_F(file)->generation,
1057 +                          atomic_read(
1058 +                                  &UNIONFS_I(dentry->d_inode)->generation));
1059 +       }
1060 +
1061 +       /* Copyup on the first write to a file on a readonly branch. */
1062 +       if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1063 +           !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1064 +           is_robranch(dentry)) {
1065 +               pr_debug("unionfs: do delay copyup of \"%s\"\n",
1066 +                        dentry->d_name.name);
1067 +               err = do_delayed_copyup(file);
1068 +       }
1069 +
1070 +out:
1071 +       if (err) {
1072 +               kfree(UNIONFS_F(file)->lower_files);
1073 +               kfree(UNIONFS_F(file)->saved_branch_ids);
1074 +       }
1075 +out_nofree:
1076 +       if (!err)
1077 +               unionfs_check_file(file);
1078 +       unionfs_unlock_dentry(dentry);
1079 +       return err;
1080 +}
1081 +
1082 +/* unionfs_open helper function: open a directory */
1083 +static int __open_dir(struct inode *inode, struct file *file)
1084 +{
1085 +       struct dentry *lower_dentry;
1086 +       struct file *lower_file;
1087 +       int bindex, bstart, bend;
1088 +       struct vfsmount *mnt;
1089 +
1090 +       bstart = fbstart(file) = dbstart(file->f_path.dentry);
1091 +       bend = fbend(file) = dbend(file->f_path.dentry);
1092 +
1093 +       for (bindex = bstart; bindex <= bend; bindex++) {
1094 +               lower_dentry =
1095 +                       unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1096 +               if (!lower_dentry)
1097 +                       continue;
1098 +
1099 +               dget(lower_dentry);
1100 +               unionfs_mntget(file->f_path.dentry, bindex);
1101 +               mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1102 +               lower_file = dentry_open(lower_dentry, mnt, file->f_flags);
1103 +               if (IS_ERR(lower_file))
1104 +                       return PTR_ERR(lower_file);
1105 +
1106 +               unionfs_set_lower_file_idx(file, bindex, lower_file);
1107 +
1108 +               /*
1109 +                * The branchget goes after the open, because otherwise
1110 +                * we would miss the reference on release.
1111 +                */
1112 +               branchget(inode->i_sb, bindex);
1113 +       }
1114 +
1115 +       return 0;
1116 +}
1117 +
1118 +/* unionfs_open helper function: open a file */
1119 +static int __open_file(struct inode *inode, struct file *file)
1120 +{
1121 +       struct dentry *lower_dentry;
1122 +       struct file *lower_file;
1123 +       int lower_flags;
1124 +       int bindex, bstart, bend;
1125 +
1126 +       lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1127 +       lower_flags = file->f_flags;
1128 +
1129 +       bstart = fbstart(file) = dbstart(file->f_path.dentry);
1130 +       bend = fbend(file) = dbend(file->f_path.dentry);
1131 +
1132 +       /*
1133 +        * check for the permission for lower file.  If the error is
1134 +        * COPYUP_ERR, copyup the file.
1135 +        */
1136 +       if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1137 +               /*
1138 +                * if the open will change the file, copy it up otherwise
1139 +                * defer it.
1140 +                */
1141 +               if (lower_flags & O_TRUNC) {
1142 +                       int size = 0;
1143 +                       int err = -EROFS;
1144 +
1145 +                       /* copyup the file */
1146 +                       for (bindex = bstart - 1; bindex >= 0; bindex--) {
1147 +                               err = copyup_file(
1148 +                                       file->f_path.dentry->d_parent->d_inode,
1149 +                                       file, bstart, bindex, size);
1150 +                               if (!err)
1151 +                                       break;
1152 +                       }
1153 +                       return err;
1154 +               } else {
1155 +                       lower_flags &= ~(OPEN_WRITE_FLAGS);
1156 +               }
1157 +       }
1158 +
1159 +       dget(lower_dentry);
1160 +
1161 +       /*
1162 +        * dentry_open will decrement mnt refcnt if err.
1163 +        * otherwise fput() will do an mntput() for us upon file close.
1164 +        */
1165 +       unionfs_mntget(file->f_path.dentry, bstart);
1166 +       lower_file =
1167 +               dentry_open(lower_dentry,
1168 +                           unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1169 +                           lower_flags);
1170 +       if (IS_ERR(lower_file))
1171 +               return PTR_ERR(lower_file);
1172 +
1173 +       unionfs_set_lower_file(file, lower_file);
1174 +       branchget(inode->i_sb, bstart);
1175 +
1176 +       return 0;
1177 +}
1178 +
1179 +int unionfs_open(struct inode *inode, struct file *file)
1180 +{
1181 +       int err = 0;
1182 +       struct file *lower_file = NULL;
1183 +       struct dentry *dentry = NULL;
1184 +       int bindex = 0, bstart = 0, bend = 0;
1185 +       int size;
1186 +
1187 +       unionfs_read_lock(inode->i_sb);
1188 +
1189 +       file->private_data =
1190 +               kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1191 +       if (unlikely(!UNIONFS_F(file))) {
1192 +               err = -ENOMEM;
1193 +               goto out_nofree;
1194 +       }
1195 +       fbstart(file) = -1;
1196 +       fbend(file) = -1;
1197 +       atomic_set(&UNIONFS_F(file)->generation,
1198 +                  atomic_read(&UNIONFS_I(inode)->generation));
1199 +
1200 +       size = sizeof(struct file *) * sbmax(inode->i_sb);
1201 +       UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1202 +       if (unlikely(!UNIONFS_F(file)->lower_files)) {
1203 +               err = -ENOMEM;
1204 +               goto out;
1205 +       }
1206 +       size = sizeof(int) * sbmax(inode->i_sb);
1207 +       UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1208 +       if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1209 +               err = -ENOMEM;
1210 +               goto out;
1211 +       }
1212 +
1213 +       dentry = file->f_path.dentry;
1214 +       unionfs_lock_dentry(dentry);
1215 +
1216 +       bstart = fbstart(file) = dbstart(dentry);
1217 +       bend = fbend(file) = dbend(dentry);
1218 +
1219 +       /* increment, so that we can flush appropriately */
1220 +       atomic_inc(&UNIONFS_I(dentry->d_inode)->totalopens);
1221 +
1222 +       /*
1223 +        * open all directories and make the unionfs file struct point to
1224 +        * these lower file structs
1225 +        */
1226 +       if (S_ISDIR(inode->i_mode))
1227 +               err = __open_dir(inode, file);  /* open a dir */
1228 +       else
1229 +               err = __open_file(inode, file); /* open a file */
1230 +
1231 +       /* freeing the allocated resources, and fput the opened files */
1232 +       if (err) {
1233 +               atomic_dec(&UNIONFS_I(dentry->d_inode)->totalopens);
1234 +               for (bindex = bstart; bindex <= bend; bindex++) {
1235 +                       lower_file = unionfs_lower_file_idx(file, bindex);
1236 +                       if (!lower_file)
1237 +                               continue;
1238 +
1239 +                       branchput(file->f_path.dentry->d_sb, bindex);
1240 +                       /* fput calls dput for lower_dentry */
1241 +                       fput(lower_file);
1242 +               }
1243 +       }
1244 +
1245 +       unionfs_unlock_dentry(dentry);
1246 +
1247 +out:
1248 +       if (err) {
1249 +               kfree(UNIONFS_F(file)->lower_files);
1250 +               kfree(UNIONFS_F(file)->saved_branch_ids);
1251 +               kfree(UNIONFS_F(file));
1252 +       }
1253 +out_nofree:
1254 +       unionfs_read_unlock(inode->i_sb);
1255 +       unionfs_check_inode(inode);
1256 +       if (!err) {
1257 +               unionfs_check_file(file);
1258 +               unionfs_check_dentry(file->f_path.dentry->d_parent);
1259 +       }
1260 +       return err;
1261 +}
1262 +
1263 +/*
1264 + * release all lower object references & free the file info structure
1265 + *
1266 + * No need to grab sb info's rwsem.
1267 + */
1268 +int unionfs_file_release(struct inode *inode, struct file *file)
1269 +{
1270 +       struct file *lower_file = NULL;
1271 +       struct unionfs_file_info *fileinfo;
1272 +       struct unionfs_inode_info *inodeinfo;
1273 +       struct super_block *sb = inode->i_sb;
1274 +       int bindex, bstart, bend;
1275 +       int fgen, err = 0;
1276 +
1277 +       unionfs_read_lock(sb);
1278 +       /*
1279 +        * Yes, we have to revalidate this file even if it's being released.
1280 +        * This is important for open-but-unlinked files, as well as mmap
1281 +        * support.
1282 +        */
1283 +       err = unionfs_file_revalidate(file, true);
1284 +       if (unlikely(err))
1285 +               goto out;
1286 +       unionfs_check_file(file);
1287 +       fileinfo = UNIONFS_F(file);
1288 +       BUG_ON(file->f_path.dentry->d_inode != inode);
1289 +       inodeinfo = UNIONFS_I(inode);
1290 +
1291 +       /* fput all the lower files */
1292 +       fgen = atomic_read(&fileinfo->generation);
1293 +       bstart = fbstart(file);
1294 +       bend = fbend(file);
1295 +
1296 +       for (bindex = bstart; bindex <= bend; bindex++) {
1297 +               lower_file = unionfs_lower_file_idx(file, bindex);
1298 +
1299 +               if (lower_file) {
1300 +                       fput(lower_file);
1301 +                       branchput(sb, bindex);
1302 +               }
1303 +       }
1304 +       kfree(fileinfo->lower_files);
1305 +       kfree(fileinfo->saved_branch_ids);
1306 +
1307 +       if (fileinfo->rdstate) {
1308 +               fileinfo->rdstate->access = jiffies;
1309 +               pr_debug("unionfs: saving rdstate with cookie "
1310 +                        "%u [%d.%lld]\n",
1311 +                        fileinfo->rdstate->cookie,
1312 +                        fileinfo->rdstate->bindex,
1313 +                        (long long)fileinfo->rdstate->dirpos);
1314 +               spin_lock(&inodeinfo->rdlock);
1315 +               inodeinfo->rdcount++;
1316 +               list_add_tail(&fileinfo->rdstate->cache,
1317 +                             &inodeinfo->readdircache);
1318 +               mark_inode_dirty(inode);
1319 +               spin_unlock(&inodeinfo->rdlock);
1320 +               fileinfo->rdstate = NULL;
1321 +       }
1322 +       kfree(fileinfo);
1323 +
1324 +out:
1325 +       unionfs_read_unlock(sb);
1326 +       return err;
1327 +}
1328 +
1329 +/* pass the ioctl to the lower fs */
1330 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1331 +{
1332 +       struct file *lower_file;
1333 +       int err;
1334 +
1335 +       lower_file = unionfs_lower_file(file);
1336 +
1337 +       err = security_file_ioctl(lower_file, cmd, arg);
1338 +       if (err)
1339 +               goto out;
1340 +
1341 +       err = -ENOTTY;
1342 +       if (!lower_file || !lower_file->f_op)
1343 +               goto out;
1344 +       if (lower_file->f_op->unlocked_ioctl) {
1345 +               err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1346 +       } else if (lower_file->f_op->ioctl) {
1347 +               lock_kernel();
1348 +               err = lower_file->f_op->ioctl(
1349 +                       lower_file->f_path.dentry->d_inode,
1350 +                       lower_file, cmd, arg);
1351 +               unlock_kernel();
1352 +       }
1353 +
1354 +out:
1355 +       return err;
1356 +}
1357 +
1358 +/*
1359 + * return to user-space the branch indices containing the file in question
1360 + *
1361 + * We use fd_set and therefore we are limited to the number of the branches
1362 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1363 + */
1364 +static int unionfs_ioctl_queryfile(struct file *file, unsigned int cmd,
1365 +                                  unsigned long arg)
1366 +{
1367 +       int err = 0;
1368 +       fd_set branchlist;
1369 +       int bstart = 0, bend = 0, bindex = 0;
1370 +       int orig_bstart, orig_bend;
1371 +       struct dentry *dentry, *lower_dentry;
1372 +       struct vfsmount *mnt;
1373 +
1374 +       dentry = file->f_path.dentry;
1375 +       unionfs_lock_dentry(dentry);
1376 +       orig_bstart = dbstart(dentry);
1377 +       orig_bend = dbend(dentry);
1378 +       err = unionfs_partial_lookup(dentry);
1379 +       if (err)
1380 +               goto out;
1381 +       bstart = dbstart(dentry);
1382 +       bend = dbend(dentry);
1383 +
1384 +       FD_ZERO(&branchlist);
1385 +
1386 +       for (bindex = bstart; bindex <= bend; bindex++) {
1387 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1388 +               if (!lower_dentry)
1389 +                       continue;
1390 +               if (likely(lower_dentry->d_inode))
1391 +                       FD_SET(bindex, &branchlist);
1392 +               /* purge any lower objects after partial_lookup */
1393 +               if (bindex < orig_bstart || bindex > orig_bend) {
1394 +                       dput(lower_dentry);
1395 +                       unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1396 +                       iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1397 +                       unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1398 +                                                   NULL);
1399 +                       mnt = unionfs_lower_mnt_idx(dentry, bindex);
1400 +                       if (!mnt)
1401 +                               continue;
1402 +                       unionfs_mntput(dentry, bindex);
1403 +                       unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1404 +               }
1405 +       }
1406 +       /* restore original dentry's offsets */
1407 +       set_dbstart(dentry, orig_bstart);
1408 +       set_dbend(dentry, orig_bend);
1409 +       ibstart(dentry->d_inode) = orig_bstart;
1410 +       ibend(dentry->d_inode) = orig_bend;
1411 +
1412 +       err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1413 +       if (unlikely(err))
1414 +               err = -EFAULT;
1415 +
1416 +out:
1417 +       unionfs_unlock_dentry(dentry);
1418 +       return err < 0 ? err : bend;
1419 +}
1420 +
1421 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1422 +{
1423 +       long err;
1424 +
1425 +       unionfs_read_lock(file->f_path.dentry->d_sb);
1426 +
1427 +       err = unionfs_file_revalidate(file, true);
1428 +       if (unlikely(err))
1429 +               goto out;
1430 +
1431 +       /* check if asked for local commands */
1432 +       switch (cmd) {
1433 +       case UNIONFS_IOCTL_INCGEN:
1434 +               /* Increment the superblock generation count */
1435 +               pr_info("unionfs: incgen ioctl deprecated; "
1436 +                       "use \"-o remount,incgen\"\n");
1437 +               err = -ENOSYS;
1438 +               break;
1439 +
1440 +       case UNIONFS_IOCTL_QUERYFILE:
1441 +               /* Return list of branches containing the given file */
1442 +               err = unionfs_ioctl_queryfile(file, cmd, arg);
1443 +               break;
1444 +
1445 +       default:
1446 +               /* pass the ioctl down */
1447 +               err = do_ioctl(file, cmd, arg);
1448 +               break;
1449 +       }
1450 +
1451 +out:
1452 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
1453 +       unionfs_check_file(file);
1454 +       return err;
1455 +}
1456 +
1457 +int unionfs_flush(struct file *file, fl_owner_t id)
1458 +{
1459 +       int err = 0;
1460 +       struct file *lower_file = NULL;
1461 +       struct dentry *dentry = file->f_path.dentry;
1462 +       int bindex, bstart, bend;
1463 +
1464 +       unionfs_read_lock(dentry->d_sb);
1465 +
1466 +       err = unionfs_file_revalidate(file, true);
1467 +       if (unlikely(err))
1468 +               goto out;
1469 +       unionfs_check_file(file);
1470 +
1471 +       if (!atomic_dec_and_test(&UNIONFS_I(dentry->d_inode)->totalopens))
1472 +               goto out;
1473 +
1474 +       unionfs_lock_dentry(dentry);
1475 +
1476 +       bstart = fbstart(file);
1477 +       bend = fbend(file);
1478 +       for (bindex = bstart; bindex <= bend; bindex++) {
1479 +               lower_file = unionfs_lower_file_idx(file, bindex);
1480 +
1481 +               if (lower_file && lower_file->f_op &&
1482 +                   lower_file->f_op->flush) {
1483 +                       err = lower_file->f_op->flush(lower_file, id);
1484 +                       if (err)
1485 +                               goto out_lock;
1486 +
1487 +                       /* if there are no more refs to the dentry, dput it */
1488 +                       if (d_deleted(dentry)) {
1489 +                               dput(unionfs_lower_dentry_idx(dentry, bindex));
1490 +                               unionfs_set_lower_dentry_idx(dentry, bindex,
1491 +                                                            NULL);
1492 +                       }
1493 +               }
1494 +
1495 +       }
1496 +
1497 +       /* on success, update our times */
1498 +       unionfs_copy_attr_times(dentry->d_inode);
1499 +       /* parent time could have changed too (async) */
1500 +       unionfs_copy_attr_times(dentry->d_parent->d_inode);
1501 +
1502 +out_lock:
1503 +       unionfs_unlock_dentry(dentry);
1504 +out:
1505 +       unionfs_read_unlock(dentry->d_sb);
1506 +       unionfs_check_file(file);
1507 +       return err;
1508 +}
1509 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1510 new file mode 100644
1511 index 0000000..98bed0b
1512 --- /dev/null
1513 +++ b/fs/unionfs/copyup.c
1514 @@ -0,0 +1,897 @@
1515 +/*
1516 + * Copyright (c) 2003-2007 Erez Zadok
1517 + * Copyright (c) 2003-2006 Charles P. Wright
1518 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1519 + * Copyright (c) 2005-2006 Junjiro Okajima
1520 + * Copyright (c) 2005      Arun M. Krishnakumar
1521 + * Copyright (c) 2004-2006 David P. Quigley
1522 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1523 + * Copyright (c) 2003      Puja Gupta
1524 + * Copyright (c) 2003      Harikesavan Krishnan
1525 + * Copyright (c) 2003-2007 Stony Brook University
1526 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
1527 + *
1528 + * This program is free software; you can redistribute it and/or modify
1529 + * it under the terms of the GNU General Public License version 2 as
1530 + * published by the Free Software Foundation.
1531 + */
1532 +
1533 +#include "union.h"
1534 +
1535 +/*
1536 + * For detailed explanation of copyup see:
1537 + * Documentation/filesystems/unionfs/concepts.txt
1538 + */
1539 +
1540 +#ifdef CONFIG_UNION_FS_XATTR
1541 +/* copyup all extended attrs for a given dentry */
1542 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1543 +                        struct dentry *new_lower_dentry)
1544 +{
1545 +       int err = 0;
1546 +       ssize_t list_size = -1;
1547 +       char *name_list = NULL;
1548 +       char *attr_value = NULL;
1549 +       char *name_list_buf = NULL;
1550 +
1551 +       /* query the actual size of the xattr list */
1552 +       list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1553 +       if (list_size <= 0) {
1554 +               err = list_size;
1555 +               goto out;
1556 +       }
1557 +
1558 +       /* allocate space for the actual list */
1559 +       name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1560 +       if (unlikely(!name_list || IS_ERR(name_list))) {
1561 +               err = PTR_ERR(name_list);
1562 +               goto out;
1563 +       }
1564 +
1565 +       name_list_buf = name_list; /* save for kfree at end */
1566 +
1567 +       /* now get the actual xattr list of the source file */
1568 +       list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1569 +       if (list_size <= 0) {
1570 +               err = list_size;
1571 +               goto out;
1572 +       }
1573 +
1574 +       /* allocate space to hold each xattr's value */
1575 +       attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1576 +       if (unlikely(!attr_value || IS_ERR(attr_value))) {
1577 +               err = PTR_ERR(name_list);
1578 +               goto out;
1579 +       }
1580 +
1581 +       /* in a loop, get and set each xattr from src to dst file */
1582 +       while (*name_list) {
1583 +               ssize_t size;
1584 +
1585 +               /* Lock here since vfs_getxattr doesn't lock for us */
1586 +               mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1587 +               size = vfs_getxattr(old_lower_dentry, name_list,
1588 +                                   attr_value, XATTR_SIZE_MAX);
1589 +               mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1590 +               if (size < 0) {
1591 +                       err = size;
1592 +                       goto out;
1593 +               }
1594 +               if (size > XATTR_SIZE_MAX) {
1595 +                       err = -E2BIG;
1596 +                       goto out;
1597 +               }
1598 +               /* Don't lock here since vfs_setxattr does it for us. */
1599 +               err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1600 +                                  size, 0);
1601 +               /*
1602 +                * Selinux depends on "security.*" xattrs, so to maintain
1603 +                * the security of copied-up files, if Selinux is active,
1604 +                * then we must copy these xattrs as well.  So we need to
1605 +                * temporarily get FOWNER privileges.
1606 +                * XXX: move entire copyup code to SIOQ.
1607 +                */
1608 +               if (err == -EPERM && !capable(CAP_FOWNER)) {
1609 +                       cap_raise(current->cap_effective, CAP_FOWNER);
1610 +                       err = vfs_setxattr(new_lower_dentry, name_list,
1611 +                                          attr_value, size, 0);
1612 +                       cap_lower(current->cap_effective, CAP_FOWNER);
1613 +               }
1614 +               if (err < 0)
1615 +                       goto out;
1616 +               name_list += strlen(name_list) + 1;
1617 +       }
1618 +out:
1619 +       unionfs_xattr_kfree(name_list_buf);
1620 +       unionfs_xattr_kfree(attr_value);
1621 +       /* Ignore if xattr isn't supported */
1622 +       if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1623 +               err = 0;
1624 +       return err;
1625 +}
1626 +#endif /* CONFIG_UNION_FS_XATTR */
1627 +
1628 +/*
1629 + * Determine the mode based on the copyup flags, and the existing dentry.
1630 + *
1631 + * Handle file systems which may not support certain options.  For example
1632 + * jffs2 doesn't allow one to chmod a symlink.  So we ignore such harmless
1633 + * errors, rather than propagating them up, which results in copyup errors
1634 + * and errors returned back to users.
1635 + */
1636 +static int copyup_permissions(struct super_block *sb,
1637 +                             struct dentry *old_lower_dentry,
1638 +                             struct dentry *new_lower_dentry)
1639 +{
1640 +       struct inode *i = old_lower_dentry->d_inode;
1641 +       struct iattr newattrs;
1642 +       int err;
1643 +
1644 +       newattrs.ia_atime = i->i_atime;
1645 +       newattrs.ia_mtime = i->i_mtime;
1646 +       newattrs.ia_ctime = i->i_ctime;
1647 +       newattrs.ia_gid = i->i_gid;
1648 +       newattrs.ia_uid = i->i_uid;
1649 +       newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1650 +               ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1651 +               ATTR_GID | ATTR_UID;
1652 +       err = notify_change(new_lower_dentry, &newattrs);
1653 +       if (err)
1654 +               goto out;
1655 +
1656 +       /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1657 +       newattrs.ia_mode = i->i_mode;
1658 +       newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1659 +       err = notify_change(new_lower_dentry, &newattrs);
1660 +       if (err == -EOPNOTSUPP &&
1661 +           S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1662 +               printk(KERN_WARNING
1663 +                      "unionfs: changing \"%s\" symlink mode unsupported\n",
1664 +                      new_lower_dentry->d_name.name);
1665 +               err = 0;
1666 +       }
1667 +
1668 +out:
1669 +       return err;
1670 +}
1671 +
1672 +/*
1673 + * create the new device/file/directory - use copyup_permission to copyup
1674 + * times, and mode
1675 + *
1676 + * if the object being copied up is a regular file, the file is only created,
1677 + * the contents have to be copied up separately
1678 + */
1679 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1680 +                           struct dentry *new_lower_dentry,
1681 +                           struct dentry *new_lower_parent_dentry,
1682 +                           char *symbuf)
1683 +{
1684 +       int err = 0;
1685 +       umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1686 +       struct sioq_args args;
1687 +
1688 +       if (S_ISDIR(old_mode)) {
1689 +               args.mkdir.parent = new_lower_parent_dentry->d_inode;
1690 +               args.mkdir.dentry = new_lower_dentry;
1691 +               args.mkdir.mode = old_mode;
1692 +
1693 +               run_sioq(__unionfs_mkdir, &args);
1694 +               err = args.err;
1695 +       } else if (S_ISLNK(old_mode)) {
1696 +               args.symlink.parent = new_lower_parent_dentry->d_inode;
1697 +               args.symlink.dentry = new_lower_dentry;
1698 +               args.symlink.symbuf = symbuf;
1699 +               args.symlink.mode = old_mode;
1700 +
1701 +               run_sioq(__unionfs_symlink, &args);
1702 +               err = args.err;
1703 +       } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1704 +                  S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1705 +               args.mknod.parent = new_lower_parent_dentry->d_inode;
1706 +               args.mknod.dentry = new_lower_dentry;
1707 +               args.mknod.mode = old_mode;
1708 +               args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1709 +
1710 +               run_sioq(__unionfs_mknod, &args);
1711 +               err = args.err;
1712 +       } else if (S_ISREG(old_mode)) {
1713 +               struct nameidata nd;
1714 +               err = init_lower_nd(&nd, LOOKUP_CREATE);
1715 +               if (unlikely(err < 0))
1716 +                       goto out;
1717 +               args.create.nd = &nd;
1718 +               args.create.parent = new_lower_parent_dentry->d_inode;
1719 +               args.create.dentry = new_lower_dentry;
1720 +               args.create.mode = old_mode;
1721 +
1722 +               run_sioq(__unionfs_create, &args);
1723 +               err = args.err;
1724 +               release_lower_nd(&nd, err);
1725 +       } else {
1726 +               printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1727 +                      old_mode);
1728 +               BUG();
1729 +       }
1730 +
1731 +out:
1732 +       return err;
1733 +}
1734 +
1735 +static int __copyup_reg_data(struct dentry *dentry,
1736 +                            struct dentry *new_lower_dentry, int new_bindex,
1737 +                            struct dentry *old_lower_dentry, int old_bindex,
1738 +                            struct file **copyup_file, loff_t len)
1739 +{
1740 +       struct super_block *sb = dentry->d_sb;
1741 +       struct file *input_file;
1742 +       struct file *output_file;
1743 +       struct vfsmount *output_mnt;
1744 +       mm_segment_t old_fs;
1745 +       char *buf = NULL;
1746 +       ssize_t read_bytes, write_bytes;
1747 +       loff_t size;
1748 +       int err = 0;
1749 +
1750 +       /* open old file */
1751 +       unionfs_mntget(dentry, old_bindex);
1752 +       branchget(sb, old_bindex);
1753 +       /* dentry_open calls dput and mntput if it returns an error */
1754 +       input_file = dentry_open(old_lower_dentry,
1755 +                                unionfs_lower_mnt_idx(dentry, old_bindex),
1756 +                                O_RDONLY | O_LARGEFILE);
1757 +       if (IS_ERR(input_file)) {
1758 +               dput(old_lower_dentry);
1759 +               err = PTR_ERR(input_file);
1760 +               goto out;
1761 +       }
1762 +       if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1763 +               err = -EINVAL;
1764 +               goto out_close_in;
1765 +       }
1766 +
1767 +       /* open new file */
1768 +       dget(new_lower_dentry);
1769 +       output_mnt = unionfs_mntget(sb->s_root, new_bindex);
1770 +       branchget(sb, new_bindex);
1771 +       output_file = dentry_open(new_lower_dentry, output_mnt,
1772 +                                 O_RDWR | O_LARGEFILE);
1773 +       if (IS_ERR(output_file)) {
1774 +               err = PTR_ERR(output_file);
1775 +               goto out_close_in2;
1776 +       }
1777 +       if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1778 +               err = -EINVAL;
1779 +               goto out_close_out;
1780 +       }
1781 +
1782 +       /* allocating a buffer */
1783 +       buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1784 +       if (unlikely(!buf)) {
1785 +               err = -ENOMEM;
1786 +               goto out_close_out;
1787 +       }
1788 +
1789 +       input_file->f_pos = 0;
1790 +       output_file->f_pos = 0;
1791 +
1792 +       old_fs = get_fs();
1793 +       set_fs(KERNEL_DS);
1794 +
1795 +       size = len;
1796 +       err = 0;
1797 +       do {
1798 +               if (len >= PAGE_SIZE)
1799 +                       size = PAGE_SIZE;
1800 +               else if ((len < PAGE_SIZE) && (len > 0))
1801 +                       size = len;
1802 +
1803 +               len -= PAGE_SIZE;
1804 +
1805 +               read_bytes =
1806 +                       input_file->f_op->read(input_file,
1807 +                                              (char __user *)buf, size,
1808 +                                              &input_file->f_pos);
1809 +               if (read_bytes <= 0) {
1810 +                       err = read_bytes;
1811 +                       break;
1812 +               }
1813 +
1814 +               write_bytes =
1815 +                       output_file->f_op->write(output_file,
1816 +                                                (char __user *)buf,
1817 +                                                read_bytes,
1818 +                                                &output_file->f_pos);
1819 +               if ((write_bytes < 0) || (write_bytes < read_bytes)) {
1820 +                       err = write_bytes;
1821 +                       break;
1822 +               }
1823 +       } while ((read_bytes > 0) && (len > 0));
1824 +
1825 +       set_fs(old_fs);
1826 +
1827 +       kfree(buf);
1828 +
1829 +       if (!err)
1830 +               err = output_file->f_op->fsync(output_file,
1831 +                                              new_lower_dentry, 0);
1832 +
1833 +       if (err)
1834 +               goto out_close_out;
1835 +
1836 +       if (copyup_file) {
1837 +               *copyup_file = output_file;
1838 +               goto out_close_in;
1839 +       }
1840 +
1841 +out_close_out:
1842 +       fput(output_file);
1843 +
1844 +out_close_in2:
1845 +       branchput(sb, new_bindex);
1846 +
1847 +out_close_in:
1848 +       fput(input_file);
1849 +
1850 +out:
1851 +       branchput(sb, old_bindex);
1852 +
1853 +       return err;
1854 +}
1855 +
1856 +/*
1857 + * dput the lower references for old and new dentry & clear a lower dentry
1858 + * pointer
1859 + */
1860 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
1861 +                   int old_bstart, int old_bend,
1862 +                   struct dentry *new_lower_dentry, int new_bindex)
1863 +{
1864 +       /* get rid of the lower dentry and all its traces */
1865 +       unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
1866 +       set_dbstart(dentry, old_bstart);
1867 +       set_dbend(dentry, old_bend);
1868 +
1869 +       dput(new_lower_dentry);
1870 +       dput(old_lower_dentry);
1871 +}
1872 +
1873 +/*
1874 + * Copy up a dentry to a file of specified name.
1875 + *
1876 + * @dir: used to pull the ->i_sb to access other branches
1877 + * @dentry: the non-negative dentry whose lower_inode we should copy
1878 + * @bstart: the branch of the lower_inode to copy from
1879 + * @new_bindex: the branch to create the new file in
1880 + * @name: the name of the file to create
1881 + * @namelen: length of @name
1882 + * @copyup_file: the "struct file" to return (optional)
1883 + * @len: how many bytes to copy-up?
1884 + */
1885 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
1886 +                 int new_bindex, const char *name, int namelen,
1887 +                 struct file **copyup_file, loff_t len)
1888 +{
1889 +       struct dentry *new_lower_dentry;
1890 +       struct dentry *old_lower_dentry = NULL;
1891 +       struct super_block *sb;
1892 +       int err = 0;
1893 +       int old_bindex;
1894 +       int old_bstart;
1895 +       int old_bend;
1896 +       struct dentry *new_lower_parent_dentry = NULL;
1897 +       mm_segment_t oldfs;
1898 +       char *symbuf = NULL;
1899 +
1900 +       verify_locked(dentry);
1901 +
1902 +       old_bindex = bstart;
1903 +       old_bstart = dbstart(dentry);
1904 +       old_bend = dbend(dentry);
1905 +
1906 +       BUG_ON(new_bindex < 0);
1907 +       BUG_ON(new_bindex >= old_bindex);
1908 +
1909 +       sb = dir->i_sb;
1910 +
1911 +       err = is_robranch_super(sb, new_bindex);
1912 +       if (err)
1913 +               goto out;
1914 +
1915 +       /* Create the directory structure above this dentry. */
1916 +       new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
1917 +       if (IS_ERR(new_lower_dentry)) {
1918 +               err = PTR_ERR(new_lower_dentry);
1919 +               goto out;
1920 +       }
1921 +
1922 +       old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
1923 +       /* we conditionally dput this old_lower_dentry at end of function */
1924 +       dget(old_lower_dentry);
1925 +
1926 +       /* For symlinks, we must read the link before we lock the directory. */
1927 +       if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
1928 +
1929 +               symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
1930 +               if (unlikely(!symbuf)) {
1931 +                       __clear(dentry, old_lower_dentry,
1932 +                               old_bstart, old_bend,
1933 +                               new_lower_dentry, new_bindex);
1934 +                       err = -ENOMEM;
1935 +                       goto out_free;
1936 +               }
1937 +
1938 +               oldfs = get_fs();
1939 +               set_fs(KERNEL_DS);
1940 +               err = old_lower_dentry->d_inode->i_op->readlink(
1941 +                       old_lower_dentry,
1942 +                       (char __user *)symbuf,
1943 +                       PATH_MAX);
1944 +               set_fs(oldfs);
1945 +               if (err < 0) {
1946 +                       __clear(dentry, old_lower_dentry,
1947 +                               old_bstart, old_bend,
1948 +                               new_lower_dentry, new_bindex);
1949 +                       goto out_free;
1950 +               }
1951 +               symbuf[err] = '\0';
1952 +       }
1953 +
1954 +       /* Now we lock the parent, and create the object in the new branch. */
1955 +       new_lower_parent_dentry = lock_parent(new_lower_dentry);
1956 +
1957 +       /* create the new inode */
1958 +       err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
1959 +                              new_lower_parent_dentry, symbuf);
1960 +
1961 +       if (err) {
1962 +               __clear(dentry, old_lower_dentry,
1963 +                       old_bstart, old_bend,
1964 +                       new_lower_dentry, new_bindex);
1965 +               goto out_unlock;
1966 +       }
1967 +
1968 +       /* We actually copyup the file here. */
1969 +       if (S_ISREG(old_lower_dentry->d_inode->i_mode))
1970 +               err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
1971 +                                       old_lower_dentry, old_bindex,
1972 +                                       copyup_file, len);
1973 +       if (err)
1974 +               goto out_unlink;
1975 +
1976 +       /* Set permissions. */
1977 +       if ((err = copyup_permissions(sb, old_lower_dentry,
1978 +                                     new_lower_dentry)))
1979 +               goto out_unlink;
1980 +
1981 +#ifdef CONFIG_UNION_FS_XATTR
1982 +       /* Selinux uses extended attributes for permissions. */
1983 +       err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
1984 +       if (err)
1985 +               goto out_unlink;
1986 +#endif /* CONFIG_UNION_FS_XATTR */
1987 +
1988 +       /* do not allow files getting deleted to be re-interposed */
1989 +       if (!d_deleted(dentry))
1990 +               unionfs_reinterpose(dentry);
1991 +
1992 +       goto out_unlock;
1993 +
1994 +out_unlink:
1995 +       /*
1996 +        * copyup failed, because we possibly ran out of space or
1997 +        * quota, or something else happened so let's unlink; we don't
1998 +        * really care about the return value of vfs_unlink
1999 +        */
2000 +       vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2001 +
2002 +       if (copyup_file) {
2003 +               /* need to close the file */
2004 +
2005 +               fput(*copyup_file);
2006 +               branchput(sb, new_bindex);
2007 +       }
2008 +
2009 +       /*
2010 +        * TODO: should we reset the error to something like -EIO?
2011 +        *
2012 +        * If we don't reset, the user may get some nonsensical errors, but
2013 +        * on the other hand, if we reset to EIO, we guarantee that the user
2014 +        * will get a "confusing" error message.
2015 +        */
2016 +
2017 +out_unlock:
2018 +       unlock_dir(new_lower_parent_dentry);
2019 +
2020 +out_free:
2021 +       /*
2022 +        * If old_lower_dentry was a directory, we need to dput it.  If it
2023 +        * was a file, then it was already dput indirectly by other
2024 +        * functions we call above which operate on regular files.
2025 +        */
2026 +       if (old_lower_dentry && old_lower_dentry->d_inode &&
2027 +           (S_ISDIR(old_lower_dentry->d_inode->i_mode) ||
2028 +            S_ISLNK(old_lower_dentry->d_inode->i_mode)))
2029 +               dput(old_lower_dentry);
2030 +       kfree(symbuf);
2031 +
2032 +       if (err)
2033 +               goto out;
2034 +       if (!S_ISDIR(dentry->d_inode->i_mode)) {
2035 +               unionfs_postcopyup_release(dentry);
2036 +               if (!unionfs_lower_inode(dentry->d_inode)) {
2037 +                       /*
2038 +                        * If we got here, then we copied up to an
2039 +                        * unlinked-open file, whose name is .unionfsXXXXX.
2040 +                        */
2041 +                       struct inode *inode = new_lower_dentry->d_inode;
2042 +                       atomic_inc(&inode->i_count);
2043 +                       unionfs_set_lower_inode_idx(dentry->d_inode,
2044 +                                                   ibstart(dentry->d_inode),
2045 +                                                   inode);
2046 +               }
2047 +       }
2048 +       unionfs_postcopyup_setmnt(dentry);
2049 +       /* sync inode times from copied-up inode to our inode */
2050 +       unionfs_copy_attr_times(dentry->d_inode);
2051 +       unionfs_check_inode(dir);
2052 +       unionfs_check_dentry(dentry);
2053 +out:
2054 +       return err;
2055 +}
2056 +
2057 +/*
2058 + * This function creates a copy of a file represented by 'file' which
2059 + * currently resides in branch 'bstart' to branch 'new_bindex.'  The copy
2060 + * will be named "name".
2061 + */
2062 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2063 +                     int bstart, int new_bindex, loff_t len)
2064 +{
2065 +       int err = 0;
2066 +       struct file *output_file = NULL;
2067 +
2068 +       err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2069 +                           name, strlen(name), &output_file, len);
2070 +       if (!err) {
2071 +               fbstart(file) = new_bindex;
2072 +               unionfs_set_lower_file_idx(file, new_bindex, output_file);
2073 +       }
2074 +
2075 +       return err;
2076 +}
2077 +
2078 +/*
2079 + * This function creates a copy of a file represented by 'file' which
2080 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2081 + */
2082 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2083 +               int new_bindex, loff_t len)
2084 +{
2085 +       int err = 0;
2086 +       struct file *output_file = NULL;
2087 +       struct dentry *dentry = file->f_path.dentry;
2088 +
2089 +       err = copyup_dentry(dir, dentry, bstart, new_bindex,
2090 +                           dentry->d_name.name, dentry->d_name.len,
2091 +                           &output_file, len);
2092 +       if (!err) {
2093 +               fbstart(file) = new_bindex;
2094 +               unionfs_set_lower_file_idx(file, new_bindex, output_file);
2095 +       }
2096 +
2097 +       return err;
2098 +}
2099 +
2100 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2101 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2102 +                            int old_bstart, int old_bend)
2103 +{
2104 +       int loop_start;
2105 +       int loop_end;
2106 +       int new_bstart = -1;
2107 +       int new_bend = -1;
2108 +       int i;
2109 +
2110 +       loop_start = min(old_bstart, bindex);
2111 +       loop_end = max(old_bend, bindex);
2112 +
2113 +       /*
2114 +        * This loop sets the bstart and bend for the new dentry by
2115 +        * traversing from left to right.  It also dputs all negative
2116 +        * dentries except bindex
2117 +        */
2118 +       for (i = loop_start; i <= loop_end; i++) {
2119 +               if (!unionfs_lower_dentry_idx(dentry, i))
2120 +                       continue;
2121 +
2122 +               if (i == bindex) {
2123 +                       new_bend = i;
2124 +                       if (new_bstart < 0)
2125 +                               new_bstart = i;
2126 +                       continue;
2127 +               }
2128 +
2129 +               if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2130 +                       dput(unionfs_lower_dentry_idx(dentry, i));
2131 +                       unionfs_set_lower_dentry_idx(dentry, i, NULL);
2132 +
2133 +                       unionfs_mntput(dentry, i);
2134 +                       unionfs_set_lower_mnt_idx(dentry, i, NULL);
2135 +               } else {
2136 +                       if (new_bstart < 0)
2137 +                               new_bstart = i;
2138 +                       new_bend = i;
2139 +               }
2140 +       }
2141 +
2142 +       if (new_bstart < 0)
2143 +               new_bstart = bindex;
2144 +       if (new_bend < 0)
2145 +               new_bend = bindex;
2146 +       set_dbstart(dentry, new_bstart);
2147 +       set_dbend(dentry, new_bend);
2148 +
2149 +}
2150 +
2151 +/* set lower inode ptr and update bstart & bend if necessary */
2152 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2153 +                       int bindex)
2154 +{
2155 +       unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2156 +                                   igrab(lower->d_inode));
2157 +       if (likely(ibstart(upper->d_inode) > bindex))
2158 +               ibstart(upper->d_inode) = bindex;
2159 +       if (likely(ibend(upper->d_inode) < bindex))
2160 +               ibend(upper->d_inode) = bindex;
2161 +
2162 +}
2163 +
2164 +/* set lower dentry ptr and update bstart & bend if necessary */
2165 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2166 +                        int bindex)
2167 +{
2168 +       unionfs_set_lower_dentry_idx(upper, bindex, lower);
2169 +       if (likely(dbstart(upper) > bindex))
2170 +               set_dbstart(upper, bindex);
2171 +       if (likely(dbend(upper) < bindex))
2172 +               set_dbend(upper, bindex);
2173 +}
2174 +
2175 +/*
2176 + * This function replicates the directory structure up-to given dentry
2177 + * in the bindex branch.
2178 + */
2179 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2180 +                             const char *name, int bindex)
2181 +{
2182 +       int err;
2183 +       struct dentry *child_dentry;
2184 +       struct dentry *parent_dentry;
2185 +       struct dentry *lower_parent_dentry = NULL;
2186 +       struct dentry *lower_dentry = NULL;
2187 +       const char *childname;
2188 +       unsigned int childnamelen;
2189 +       int nr_dentry;
2190 +       int count = 0;
2191 +       int old_bstart;
2192 +       int old_bend;
2193 +       struct dentry **path = NULL;
2194 +       struct super_block *sb;
2195 +
2196 +       verify_locked(dentry);
2197 +
2198 +       err = is_robranch_super(dir->i_sb, bindex);
2199 +       if (err) {
2200 +               lower_dentry = ERR_PTR(err);
2201 +               goto out;
2202 +       }
2203 +
2204 +       old_bstart = dbstart(dentry);
2205 +       old_bend = dbend(dentry);
2206 +
2207 +       lower_dentry = ERR_PTR(-ENOMEM);
2208 +
2209 +       /* There is no sense allocating any less than the minimum. */
2210 +       nr_dentry = 1;
2211 +       path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2212 +       if (unlikely(!path))
2213 +               goto out;
2214 +
2215 +       /* assume the negative dentry of unionfs as the parent dentry */
2216 +       parent_dentry = dentry;
2217 +
2218 +       /*
2219 +        * This loop finds the first parent that exists in the given branch.
2220 +        * We start building the directory structure from there.  At the end
2221 +        * of the loop, the following should hold:
2222 +        *  - child_dentry is the first nonexistent child
2223 +        *  - parent_dentry is the first existent parent
2224 +        *  - path[0] is the = deepest child
2225 +        *  - path[count] is the first child to create
2226 +        */
2227 +       do {
2228 +               child_dentry = parent_dentry;
2229 +
2230 +               /* find the parent directory dentry in unionfs */
2231 +               parent_dentry = child_dentry->d_parent;
2232 +               unionfs_lock_dentry(parent_dentry);
2233 +
2234 +               /* find out the lower_parent_dentry in the given branch */
2235 +               lower_parent_dentry =
2236 +                       unionfs_lower_dentry_idx(parent_dentry, bindex);
2237 +
2238 +               /* grow path table */
2239 +               if (count == nr_dentry) {
2240 +                       void *p;
2241 +
2242 +                       nr_dentry *= 2;
2243 +                       p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2244 +                                    GFP_KERNEL);
2245 +                       if (unlikely(!p)) {
2246 +                               lower_dentry = ERR_PTR(-ENOMEM);
2247 +                               goto out;
2248 +                       }
2249 +                       path = p;
2250 +               }
2251 +
2252 +               /* store the child dentry */
2253 +               path[count++] = child_dentry;
2254 +       } while (!lower_parent_dentry);
2255 +       count--;
2256 +
2257 +       sb = dentry->d_sb;
2258 +
2259 +       /*
2260 +        * This code goes between the begin/end labels and basically
2261 +        * emulates a while(child_dentry != dentry), only cleaner and
2262 +        * shorter than what would be a much longer while loop.
2263 +        */
2264 +begin:
2265 +       /* get lower parent dir in the current branch */
2266 +       lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2267 +       unionfs_unlock_dentry(parent_dentry);
2268 +
2269 +       /* init the values to lookup */
2270 +       childname = child_dentry->d_name.name;
2271 +       childnamelen = child_dentry->d_name.len;
2272 +
2273 +       if (child_dentry != dentry) {
2274 +               /* lookup child in the underlying file system */
2275 +               lower_dentry = lookup_one_len(childname, lower_parent_dentry,
2276 +                                             childnamelen);
2277 +               if (IS_ERR(lower_dentry))
2278 +                       goto out;
2279 +       } else {
2280 +               /*
2281 +                * Is the name a whiteout of the child name ?  lookup the
2282 +                * whiteout child in the underlying file system
2283 +                */
2284 +               lower_dentry = lookup_one_len(name, lower_parent_dentry,
2285 +                                             strlen(name));
2286 +               if (IS_ERR(lower_dentry))
2287 +                       goto out;
2288 +
2289 +               /* Replace the current dentry (if any) with the new one */
2290 +               dput(unionfs_lower_dentry_idx(dentry, bindex));
2291 +               unionfs_set_lower_dentry_idx(dentry, bindex,
2292 +                                            lower_dentry);
2293 +
2294 +               __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2295 +               goto out;
2296 +       }
2297 +
2298 +       if (lower_dentry->d_inode) {
2299 +               /*
2300 +                * since this already exists we dput to avoid
2301 +                * multiple references on the same dentry
2302 +                */
2303 +               dput(lower_dentry);
2304 +       } else {
2305 +               struct sioq_args args;
2306 +
2307 +               /* it's a negative dentry, create a new dir */
2308 +               lower_parent_dentry = lock_parent(lower_dentry);
2309 +
2310 +               args.mkdir.parent = lower_parent_dentry->d_inode;
2311 +               args.mkdir.dentry = lower_dentry;
2312 +               args.mkdir.mode = child_dentry->d_inode->i_mode;
2313 +
2314 +               run_sioq(__unionfs_mkdir, &args);
2315 +               err = args.err;
2316 +
2317 +               if (!err)
2318 +                       err = copyup_permissions(dir->i_sb, child_dentry,
2319 +                                                lower_dentry);
2320 +               unlock_dir(lower_parent_dentry);
2321 +               if (err) {
2322 +                       struct inode *inode = lower_dentry->d_inode;
2323 +                       /*
2324 +                        * If we get here, it means that we created a new
2325 +                        * dentry+inode, but copying permissions failed.
2326 +                        * Therefore, we should delete this inode and dput
2327 +                        * the dentry so as not to leave cruft behind.
2328 +                        */
2329 +                       if (lower_dentry->d_op && lower_dentry->d_op->d_iput)
2330 +                               lower_dentry->d_op->d_iput(lower_dentry,
2331 +                                                          inode);
2332 +                       else
2333 +                               iput(inode);
2334 +                       lower_dentry->d_inode = NULL;
2335 +                       dput(lower_dentry);
2336 +                       lower_dentry = ERR_PTR(err);
2337 +                       goto out;
2338 +               }
2339 +
2340 +       }
2341 +
2342 +       __set_inode(child_dentry, lower_dentry, bindex);
2343 +       __set_dentry(child_dentry, lower_dentry, bindex);
2344 +       /*
2345 +        * update times of this dentry, but also the parent, because if
2346 +        * we changed, the parent may have changed too.
2347 +        */
2348 +       unionfs_copy_attr_times(parent_dentry->d_inode);
2349 +       unionfs_copy_attr_times(child_dentry->d_inode);
2350 +
2351 +       parent_dentry = child_dentry;
2352 +       child_dentry = path[--count];
2353 +       goto begin;
2354 +out:
2355 +       /* cleanup any leftover locks from the do/while loop above */
2356 +       if (IS_ERR(lower_dentry))
2357 +               while (count)
2358 +                       unionfs_unlock_dentry(path[count--]);
2359 +       kfree(path);
2360 +       return lower_dentry;
2361 +}
2362 +
2363 +/*
2364 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2365 + * dentry+parents to the first parent node that has an mnt.
2366 + */
2367 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2368 +{
2369 +       struct dentry *parent, *hasone;
2370 +       int bindex = dbstart(dentry);
2371 +
2372 +       if (unionfs_lower_mnt_idx(dentry, bindex))
2373 +               return;
2374 +       hasone = dentry->d_parent;
2375 +       /* this loop should stop at root dentry */
2376 +       while (!unionfs_lower_mnt_idx(hasone, bindex))
2377 +               hasone = hasone->d_parent;
2378 +       parent = dentry;
2379 +       while (!unionfs_lower_mnt_idx(parent, bindex)) {
2380 +               unionfs_set_lower_mnt_idx(parent, bindex,
2381 +                                         unionfs_mntget(hasone, bindex));
2382 +               parent = parent->d_parent;
2383 +       }
2384 +}
2385 +
2386 +/*
2387 + * Post-copyup helper to release all non-directory source objects of a
2388 + * copied-up file.  Regular files should have only one lower object.
2389 + */
2390 +void unionfs_postcopyup_release(struct dentry *dentry)
2391 +{
2392 +       int bindex;
2393 +
2394 +       BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2395 +       for (bindex = dbstart(dentry)+1; bindex <= dbend(dentry); bindex++) {
2396 +               if (unionfs_lower_mnt_idx(dentry, bindex)) {
2397 +                       unionfs_mntput(dentry, bindex);
2398 +                       unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
2399 +               }
2400 +               if (unionfs_lower_dentry_idx(dentry, bindex)) {
2401 +                       dput(unionfs_lower_dentry_idx(dentry, bindex));
2402 +                       unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
2403 +                       iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
2404 +                       unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
2405 +                                                   NULL);
2406 +               }
2407 +       }
2408 +       bindex = dbstart(dentry);
2409 +       set_dbend(dentry, bindex);
2410 +       ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bindex;
2411 +}
2412 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2413 new file mode 100644
2414 index 0000000..68692d7
2415 --- /dev/null
2416 +++ b/fs/unionfs/debug.c
2417 @@ -0,0 +1,526 @@
2418 +/*
2419 + * Copyright (c) 2003-2007 Erez Zadok
2420 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2421 + * Copyright (c) 2003-2007 Stony Brook University
2422 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
2423 + *
2424 + * This program is free software; you can redistribute it and/or modify
2425 + * it under the terms of the GNU General Public License version 2 as
2426 + * published by the Free Software Foundation.
2427 + */
2428 +
2429 +#include "union.h"
2430 +
2431 +/*
2432 + * Helper debugging functions for maintainers (and for users to report back
2433 + * useful information back to maintainers)
2434 + */
2435 +
2436 +/* it's always useful to know what part of the code called us */
2437 +#define PRINT_CALLER(fname, fxn, line)                                 \
2438 +       do {                                                            \
2439 +               if (!printed_caller) {                                  \
2440 +                       pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2441 +                       printed_caller = 1;                             \
2442 +               }                                                       \
2443 +       } while (0)
2444 +
2445 +/*
2446 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2447 + * the fan-out of various Unionfs objects.  We check that no lower objects
2448 + * exist  outside the start/end branch range; that all objects within are
2449 + * non-NULL (with some allowed exceptions); that for every lower file
2450 + * there's a lower dentry+inode; that the start/end ranges match for all
2451 + * corresponding lower objects; that open files/symlinks have only one lower
2452 + * objects, but directories can have several; and more.
2453 + */
2454 +void __unionfs_check_inode(const struct inode *inode,
2455 +                          const char *fname, const char *fxn, int line)
2456 +{
2457 +       int bindex;
2458 +       int istart, iend;
2459 +       struct inode *lower_inode;
2460 +       struct super_block *sb;
2461 +       int printed_caller = 0;
2462 +       void *poison_ptr;
2463 +
2464 +       /* for inodes now */
2465 +       BUG_ON(!inode);
2466 +       sb = inode->i_sb;
2467 +       istart = ibstart(inode);
2468 +       iend = ibend(inode);
2469 +       if (unlikely(istart > iend)) {
2470 +               PRINT_CALLER(fname, fxn, line);
2471 +               pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2472 +                        inode, istart, iend);
2473 +       }
2474 +       if (unlikely((istart == -1 && iend != -1) ||
2475 +                    (istart != -1 && iend == -1))) {
2476 +               PRINT_CALLER(fname, fxn, line);
2477 +               pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2478 +                        inode, istart, iend);
2479 +       }
2480 +       if (!S_ISDIR(inode->i_mode)) {
2481 +               if (unlikely(iend != istart)) {
2482 +                       PRINT_CALLER(fname, fxn, line);
2483 +                       pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2484 +                                inode, istart, iend);
2485 +               }
2486 +       }
2487 +
2488 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2489 +               if (unlikely(!UNIONFS_I(inode))) {
2490 +                       PRINT_CALLER(fname, fxn, line);
2491 +                       pr_debug(" Ci3: no inode_info %p\n", inode);
2492 +                       return;
2493 +               }
2494 +               if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2495 +                       PRINT_CALLER(fname, fxn, line);
2496 +                       pr_debug(" Ci4: no lower_inodes %p\n", inode);
2497 +                       return;
2498 +               }
2499 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2500 +               if (lower_inode) {
2501 +                       memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2502 +                       if (unlikely(bindex < istart || bindex > iend)) {
2503 +                               PRINT_CALLER(fname, fxn, line);
2504 +                               pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2505 +                                        "istart/end=%d:%d\n", inode,
2506 +                                        lower_inode, bindex, istart, iend);
2507 +                       } else if (unlikely(lower_inode == poison_ptr)) {
2508 +                               /* freed inode! */
2509 +                               PRINT_CALLER(fname, fxn, line);
2510 +                               pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2511 +                                        "istart/end=%d:%d\n", inode,
2512 +                                        lower_inode, bindex, istart, iend);
2513 +                       }
2514 +               } else {        /* lower_inode == NULL */
2515 +                       if (bindex >= istart && bindex <= iend) {
2516 +                               /*
2517 +                                * directories can have NULL lower inodes in
2518 +                                * b/t start/end, but NOT if at the
2519 +                                * start/end range.
2520 +                                */
2521 +                               if (unlikely(!(S_ISDIR(inode->i_mode) &&
2522 +                                              bindex > istart &&
2523 +                                              bindex < iend))) {
2524 +                                       PRINT_CALLER(fname, fxn, line);
2525 +                                       pr_debug(" Ci7: inode/linode=%p:%p "
2526 +                                                "bindex=%d istart/end=%d:%d\n",
2527 +                                                inode, lower_inode, bindex,
2528 +                                                istart, iend);
2529 +                               }
2530 +                       }
2531 +               }
2532 +       }
2533 +}
2534 +
2535 +void __unionfs_check_dentry(const struct dentry *dentry,
2536 +                           const char *fname, const char *fxn, int line)
2537 +{
2538 +       int bindex;
2539 +       int dstart, dend, istart, iend;
2540 +       struct dentry *lower_dentry;
2541 +       struct inode *inode, *lower_inode;
2542 +       struct super_block *sb;
2543 +       struct vfsmount *lower_mnt;
2544 +       int printed_caller = 0;
2545 +       void *poison_ptr;
2546 +
2547 +       BUG_ON(!dentry);
2548 +       sb = dentry->d_sb;
2549 +       inode = dentry->d_inode;
2550 +       dstart = dbstart(dentry);
2551 +       dend = dbend(dentry);
2552 +       BUG_ON(dstart > dend);
2553 +
2554 +       if (unlikely((dstart == -1 && dend != -1) ||
2555 +                    (dstart != -1 && dend == -1))) {
2556 +               PRINT_CALLER(fname, fxn, line);
2557 +               pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2558 +                        dentry, dstart, dend);
2559 +       }
2560 +       /*
2561 +        * check for NULL dentries inside the start/end range, or
2562 +        * non-NULL dentries outside the start/end range.
2563 +        */
2564 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2565 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2566 +               if (lower_dentry) {
2567 +                       if (unlikely(bindex < dstart || bindex > dend)) {
2568 +                               PRINT_CALLER(fname, fxn, line);
2569 +                               pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2570 +                                        "bindex=%d dstart/end=%d:%d\n",
2571 +                                        dentry, lower_dentry,
2572 +                                        (lower_dentry ? lower_dentry->d_inode :
2573 +                                         (void *) -1L),
2574 +                                        bindex, dstart, dend);
2575 +                       }
2576 +               } else {        /* lower_dentry == NULL */
2577 +                       if (bindex < dstart || bindex > dend)
2578 +                               continue;
2579 +                       /*
2580 +                        * Directories can have NULL lower inodes in b/t
2581 +                        * start/end, but NOT if at the start/end range.
2582 +                        * Ignore this rule, however, if this is a NULL
2583 +                        * dentry or a deleted dentry.
2584 +                        */
2585 +                       if (unlikely(!d_deleted((struct dentry *) dentry) &&
2586 +                                    inode &&
2587 +                                    !(inode && S_ISDIR(inode->i_mode) &&
2588 +                                      bindex > dstart && bindex < dend))) {
2589 +                               PRINT_CALLER(fname, fxn, line);
2590 +                               pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2591 +                                        "bindex=%d dstart/end=%d:%d\n",
2592 +                                        dentry, lower_dentry,
2593 +                                        (lower_dentry ?
2594 +                                         lower_dentry->d_inode :
2595 +                                         (void *) -1L),
2596 +                                        bindex, dstart, dend);
2597 +                       }
2598 +               }
2599 +       }
2600 +
2601 +       /* check for vfsmounts same as for dentries */
2602 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2603 +               lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2604 +               if (lower_mnt) {
2605 +                       if (unlikely(bindex < dstart || bindex > dend)) {
2606 +                               PRINT_CALLER(fname, fxn, line);
2607 +                               pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2608 +                                        "dstart/end=%d:%d\n", dentry,
2609 +                                        lower_mnt, bindex, dstart, dend);
2610 +                       }
2611 +               } else {        /* lower_mnt == NULL */
2612 +                       if (bindex < dstart || bindex > dend)
2613 +                               continue;
2614 +                       /*
2615 +                        * Directories can have NULL lower inodes in b/t
2616 +                        * start/end, but NOT if at the start/end range.
2617 +                        * Ignore this rule, however, if this is a NULL
2618 +                        * dentry.
2619 +                        */
2620 +                       if (unlikely(inode &&
2621 +                                    !(inode && S_ISDIR(inode->i_mode) &&
2622 +                                      bindex > dstart && bindex < dend))) {
2623 +                               PRINT_CALLER(fname, fxn, line);
2624 +                               pr_debug(" CM1: dentry/lmnt=%p:%p "
2625 +                                        "bindex=%d dstart/end=%d:%d\n",
2626 +                                        dentry, lower_mnt, bindex,
2627 +                                        dstart, dend);
2628 +                       }
2629 +               }
2630 +       }
2631 +
2632 +       /* for inodes now */
2633 +       if (!inode)
2634 +               return;
2635 +       istart = ibstart(inode);
2636 +       iend = ibend(inode);
2637 +       BUG_ON(istart > iend);
2638 +       if (unlikely((istart == -1 && iend != -1) ||
2639 +                    (istart != -1 && iend == -1))) {
2640 +               PRINT_CALLER(fname, fxn, line);
2641 +               pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2642 +                        dentry, inode, istart, iend);
2643 +       }
2644 +       if (unlikely(istart != dstart)) {
2645 +               PRINT_CALLER(fname, fxn, line);
2646 +               pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2647 +                        dentry, inode, istart, dstart);
2648 +       }
2649 +       if (unlikely(iend != dend)) {
2650 +               PRINT_CALLER(fname, fxn, line);
2651 +               pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2652 +                        dentry, inode, iend, dend);
2653 +       }
2654 +
2655 +       if (!S_ISDIR(inode->i_mode)) {
2656 +               if (unlikely(dend != dstart)) {
2657 +                       PRINT_CALLER(fname, fxn, line);
2658 +                       pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2659 +                                dentry, inode, dstart, dend);
2660 +               }
2661 +               if (unlikely(iend != istart)) {
2662 +                       PRINT_CALLER(fname, fxn, line);
2663 +                       pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2664 +                                dentry, inode, istart, iend);
2665 +               }
2666 +       }
2667 +
2668 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2669 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2670 +               if (lower_inode) {
2671 +                       memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2672 +                       if (unlikely(bindex < istart || bindex > iend)) {
2673 +                               PRINT_CALLER(fname, fxn, line);
2674 +                               pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2675 +                                        "istart/end=%d:%d\n", dentry,
2676 +                                        lower_inode, bindex, istart, iend);
2677 +                       } else if (unlikely(lower_inode == poison_ptr)) {
2678 +                               /* freed inode! */
2679 +                               PRINT_CALLER(fname, fxn, line);
2680 +                               pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2681 +                                        "istart/end=%d:%d\n", dentry,
2682 +                                        lower_inode, bindex, istart, iend);
2683 +                       }
2684 +               } else {        /* lower_inode == NULL */
2685 +                       if (bindex >= istart && bindex <= iend) {
2686 +                               /*
2687 +                                * directories can have NULL lower inodes in
2688 +                                * b/t start/end, but NOT if at the
2689 +                                * start/end range.
2690 +                                */
2691 +                               if (unlikely(!(S_ISDIR(inode->i_mode) &&
2692 +                                              bindex > istart &&
2693 +                                              bindex < iend))) {
2694 +                                       PRINT_CALLER(fname, fxn, line);
2695 +                                       pr_debug(" CI7: dentry/linode=%p:%p "
2696 +                                                "bindex=%d istart/end=%d:%d\n",
2697 +                                                dentry, lower_inode, bindex,
2698 +                                                istart, iend);
2699 +                               }
2700 +                       }
2701 +               }
2702 +       }
2703 +
2704 +       /*
2705 +        * If it's a directory, then intermediate objects b/t start/end can
2706 +        * be NULL.  But, check that all three are NULL: lower dentry, mnt,
2707 +        * and inode.
2708 +        */
2709 +       if (S_ISDIR(inode->i_mode))
2710 +               for (bindex = dstart+1; bindex < dend; bindex++) {
2711 +                       lower_inode = unionfs_lower_inode_idx(inode, bindex);
2712 +                       lower_dentry = unionfs_lower_dentry_idx(dentry,
2713 +                                                               bindex);
2714 +                       lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2715 +                       if (unlikely(!((lower_inode && lower_dentry &&
2716 +                                       lower_mnt) ||
2717 +                                      (!lower_inode &&
2718 +                                       !lower_dentry && !lower_mnt)))) {
2719 +                               PRINT_CALLER(fname, fxn, line);
2720 +                               pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2721 +                                        "bindex=%d dstart/end=%d:%d\n",
2722 +                                        lower_mnt, lower_dentry, lower_inode,
2723 +                                        bindex, dstart, dend);
2724 +                       }
2725 +               }
2726 +       /* check if lower inode is newer than upper one (it shouldn't) */
2727 +       if (unlikely(is_newer_lower(dentry))) {
2728 +               PRINT_CALLER(fname, fxn, line);
2729 +               for (bindex = ibstart(inode); bindex <= ibend(inode);
2730 +                    bindex++) {
2731 +                       lower_inode = unionfs_lower_inode_idx(inode, bindex);
2732 +                       if (unlikely(!lower_inode))
2733 +                               continue;
2734 +                       pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2735 +                                "ctime/lctime=%lu.%lu/%lu.%lu\n",
2736 +                                bindex,
2737 +                                inode->i_mtime.tv_sec,
2738 +                                inode->i_mtime.tv_nsec,
2739 +                                lower_inode->i_mtime.tv_sec,
2740 +                                lower_inode->i_mtime.tv_nsec,
2741 +                                inode->i_ctime.tv_sec,
2742 +                                inode->i_ctime.tv_nsec,
2743 +                                lower_inode->i_ctime.tv_sec,
2744 +                                lower_inode->i_ctime.tv_nsec);
2745 +               }
2746 +       }
2747 +}
2748 +
2749 +void __unionfs_check_file(const struct file *file,
2750 +                         const char *fname, const char *fxn, int line)
2751 +{
2752 +       int bindex;
2753 +       int dstart, dend, fstart, fend;
2754 +       struct dentry *dentry;
2755 +       struct file *lower_file;
2756 +       struct inode *inode;
2757 +       struct super_block *sb;
2758 +       int printed_caller = 0;
2759 +
2760 +       BUG_ON(!file);
2761 +       dentry = file->f_path.dentry;
2762 +       sb = dentry->d_sb;
2763 +       dstart = dbstart(dentry);
2764 +       dend = dbend(dentry);
2765 +       BUG_ON(dstart > dend);
2766 +       fstart = fbstart(file);
2767 +       fend = fbend(file);
2768 +       BUG_ON(fstart > fend);
2769 +
2770 +       if (unlikely((fstart == -1 && fend != -1) ||
2771 +                    (fstart != -1 && fend == -1))) {
2772 +               PRINT_CALLER(fname, fxn, line);
2773 +               pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
2774 +                        file, dentry, fstart, fend);
2775 +       }
2776 +       if (unlikely(fstart != dstart)) {
2777 +               PRINT_CALLER(fname, fxn, line);
2778 +               pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
2779 +                        file, dentry, fstart, dstart);
2780 +       }
2781 +       if (unlikely(fend != dend)) {
2782 +               PRINT_CALLER(fname, fxn, line);
2783 +               pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
2784 +                        file, dentry, fend, dend);
2785 +       }
2786 +       inode = dentry->d_inode;
2787 +       if (!S_ISDIR(inode->i_mode)) {
2788 +               if (unlikely(fend != fstart)) {
2789 +                       PRINT_CALLER(fname, fxn, line);
2790 +                       pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
2791 +                                file, inode, fstart, fend);
2792 +               }
2793 +               if (unlikely(dend != dstart)) {
2794 +                       PRINT_CALLER(fname, fxn, line);
2795 +                       pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
2796 +                                file, dentry, dstart, dend);
2797 +               }
2798 +       }
2799 +
2800 +       /*
2801 +        * check for NULL dentries inside the start/end range, or
2802 +        * non-NULL dentries outside the start/end range.
2803 +        */
2804 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2805 +               lower_file = unionfs_lower_file_idx(file, bindex);
2806 +               if (lower_file) {
2807 +                       if (unlikely(bindex < fstart || bindex > fend)) {
2808 +                               PRINT_CALLER(fname, fxn, line);
2809 +                               pr_debug(" CF5: file/lower=%p:%p bindex=%d "
2810 +                                        "fstart/end=%d:%d\n", file,
2811 +                                        lower_file, bindex, fstart, fend);
2812 +                       }
2813 +               } else {        /* lower_file == NULL */
2814 +                       if (bindex >= fstart && bindex <= fend) {
2815 +                               /*
2816 +                                * directories can have NULL lower inodes in
2817 +                                * b/t start/end, but NOT if at the
2818 +                                * start/end range.
2819 +                                */
2820 +                               if (unlikely(!(S_ISDIR(inode->i_mode) &&
2821 +                                              bindex > fstart &&
2822 +                                              bindex < fend))) {
2823 +                                       PRINT_CALLER(fname, fxn, line);
2824 +                                       pr_debug(" CF6: file/lower=%p:%p "
2825 +                                                "bindex=%d fstart/end=%d:%d\n",
2826 +                                                file, lower_file, bindex,
2827 +                                                fstart, fend);
2828 +                               }
2829 +                       }
2830 +               }
2831 +       }
2832 +
2833 +       __unionfs_check_dentry(dentry, fname, fxn, line);
2834 +}
2835 +
2836 +void __unionfs_check_nd(const struct nameidata *nd,
2837 +                       const char *fname, const char *fxn, int line)
2838 +{
2839 +       struct file *file;
2840 +       int printed_caller = 0;
2841 +
2842 +       if (unlikely(!nd))
2843 +               return;
2844 +       if (nd->flags & LOOKUP_OPEN) {
2845 +               file = nd->intent.open.file;
2846 +               if (unlikely(file->f_path.dentry &&
2847 +                            strcmp(file->f_dentry->d_sb->s_type->name,
2848 +                                   UNIONFS_NAME))) {
2849 +                       PRINT_CALLER(fname, fxn, line);
2850 +                       pr_debug(" CND1: lower_file of type %s\n",
2851 +                                file->f_path.dentry->d_sb->s_type->name);
2852 +                       BUG();
2853 +               }
2854 +       }
2855 +}
2856 +
2857 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
2858 +void __show_branch_counts(const struct super_block *sb,
2859 +                         const char *file, const char *fxn, int line)
2860 +{
2861 +       int i;
2862 +       struct vfsmount *mnt;
2863 +
2864 +       pr_debug("BC:");
2865 +       for (i = 0; i < sbmax(sb); i++) {
2866 +               if (likely(sb->s_root))
2867 +                       mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
2868 +               else
2869 +                       mnt = NULL;
2870 +               pr_debug("%d:", (mnt ? atomic_read(&mnt->mnt_count) : -99));
2871 +       }
2872 +       pr_debug("%s:%s:%d\n", file, fxn, line);
2873 +}
2874 +
2875 +void __show_inode_times(const struct inode *inode,
2876 +                       const char *file, const char *fxn, int line)
2877 +{
2878 +       struct inode *lower_inode;
2879 +       int bindex;
2880 +
2881 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
2882 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2883 +               if (unlikely(!lower_inode))
2884 +                       continue;
2885 +               pr_debug("IT(%lu:%d): ", inode->i_ino, bindex);
2886 +               pr_debug("%s:%s:%d ", file, fxn, line);
2887 +               pr_debug("um=%lu/%lu lm=%lu/%lu ",
2888 +                        inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2889 +                        lower_inode->i_mtime.tv_sec,
2890 +                        lower_inode->i_mtime.tv_nsec);
2891 +               pr_debug("uc=%lu/%lu lc=%lu/%lu\n",
2892 +                        inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2893 +                        lower_inode->i_ctime.tv_sec,
2894 +                        lower_inode->i_ctime.tv_nsec);
2895 +       }
2896 +}
2897 +
2898 +void __show_dinode_times(const struct dentry *dentry,
2899 +                       const char *file, const char *fxn, int line)
2900 +{
2901 +       struct inode *inode = dentry->d_inode;
2902 +       struct inode *lower_inode;
2903 +       int bindex;
2904 +
2905 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
2906 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2907 +               if (!lower_inode)
2908 +                       continue;
2909 +               pr_debug("DT(%s:%lu:%d): ", dentry->d_name.name, inode->i_ino,
2910 +                        bindex);
2911 +               pr_debug("%s:%s:%d ", file, fxn, line);
2912 +               pr_debug("um=%lu/%lu lm=%lu/%lu ",
2913 +                        inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2914 +                        lower_inode->i_mtime.tv_sec,
2915 +                        lower_inode->i_mtime.tv_nsec);
2916 +               pr_debug("uc=%lu/%lu lc=%lu/%lu\n",
2917 +                        inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2918 +                        lower_inode->i_ctime.tv_sec,
2919 +                        lower_inode->i_ctime.tv_nsec);
2920 +       }
2921 +}
2922 +
2923 +void __show_inode_counts(const struct inode *inode,
2924 +                       const char *file, const char *fxn, int line)
2925 +{
2926 +       struct inode *lower_inode;
2927 +       int bindex;
2928 +
2929 +       if (unlikely(!inode)) {
2930 +               pr_debug("SiC: Null inode\n");
2931 +               return;
2932 +       }
2933 +       for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
2934 +            bindex++) {
2935 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2936 +               if (unlikely(!lower_inode))
2937 +                       continue;
2938 +               pr_debug("SIC(%lu:%d:%d): ", inode->i_ino, bindex,
2939 +                        atomic_read(&(inode)->i_count));
2940 +               pr_debug("lc=%d ", atomic_read(&(lower_inode)->i_count));
2941 +               pr_debug("%s:%s:%d\n", file, fxn, line);
2942 +       }
2943 +}
2944 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
2945 new file mode 100644
2946 index 0000000..6bab9d6
2947 --- /dev/null
2948 +++ b/fs/unionfs/dentry.c
2949 @@ -0,0 +1,492 @@
2950 +/*
2951 + * Copyright (c) 2003-2007 Erez Zadok
2952 + * Copyright (c) 2003-2006 Charles P. Wright
2953 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2954 + * Copyright (c) 2005-2006 Junjiro Okajima
2955 + * Copyright (c) 2005      Arun M. Krishnakumar
2956 + * Copyright (c) 2004-2006 David P. Quigley
2957 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
2958 + * Copyright (c) 2003      Puja Gupta
2959 + * Copyright (c) 2003      Harikesavan Krishnan
2960 + * Copyright (c) 2003-2007 Stony Brook University
2961 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
2962 + *
2963 + * This program is free software; you can redistribute it and/or modify
2964 + * it under the terms of the GNU General Public License version 2 as
2965 + * published by the Free Software Foundation.
2966 + */
2967 +
2968 +#include "union.h"
2969 +
2970 +/*
2971 + * Revalidate a single dentry.
2972 + * Assume that dentry's info node is locked.
2973 + * Assume that parent(s) are all valid already, but
2974 + * the child may not yet be valid.
2975 + * Returns true if valid, false otherwise.
2976 + */
2977 +static bool __unionfs_d_revalidate_one(struct dentry *dentry,
2978 +                                      struct nameidata *nd)
2979 +{
2980 +       bool valid = true;      /* default is valid */
2981 +       struct dentry *lower_dentry;
2982 +       int bindex, bstart, bend;
2983 +       int sbgen, dgen;
2984 +       int positive = 0;
2985 +       int locked = 0;
2986 +       int interpose_flag;
2987 +       struct nameidata lowernd; /* TODO: be gentler to the stack */
2988 +
2989 +       if (nd)
2990 +               memcpy(&lowernd, nd, sizeof(struct nameidata));
2991 +       else
2992 +               memset(&lowernd, 0, sizeof(struct nameidata));
2993 +
2994 +       verify_locked(dentry);
2995 +
2996 +       /* if the dentry is unhashed, do NOT revalidate */
2997 +       if (d_deleted(dentry)) {
2998 +               pr_debug("unionfs: unhashed dentry being "
2999 +                        "revalidated: %*s\n",
3000 +                        dentry->d_name.len, dentry->d_name.name);
3001 +               goto out;
3002 +       }
3003 +
3004 +       BUG_ON(dbstart(dentry) == -1);
3005 +       if (dentry->d_inode)
3006 +               positive = 1;
3007 +       dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3008 +       sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3009 +       /*
3010 +        * If we are working on an unconnected dentry, then there is no
3011 +        * revalidation to be done, because this file does not exist within
3012 +        * the namespace, and Unionfs operates on the namespace, not data.
3013 +        */
3014 +       if (unlikely(sbgen != dgen)) {
3015 +               struct dentry *result;
3016 +               int pdgen;
3017 +
3018 +               /* The root entry should always be valid */
3019 +               BUG_ON(IS_ROOT(dentry));
3020 +
3021 +               /* We can't work correctly if our parent isn't valid. */
3022 +               pdgen = atomic_read(&UNIONFS_D(dentry->d_parent)->generation);
3023 +               BUG_ON(pdgen != sbgen); /* should never happen here */
3024 +
3025 +               /* Free the pointers for our inodes and this dentry. */
3026 +               bstart = dbstart(dentry);
3027 +               bend = dbend(dentry);
3028 +               if (bstart >= 0) {
3029 +                       struct dentry *lower_dentry;
3030 +                       for (bindex = bstart; bindex <= bend; bindex++) {
3031 +                               lower_dentry =
3032 +                                       unionfs_lower_dentry_idx(dentry,
3033 +                                                                bindex);
3034 +                               dput(lower_dentry);
3035 +                       }
3036 +               }
3037 +               set_dbstart(dentry, -1);
3038 +               set_dbend(dentry, -1);
3039 +
3040 +               interpose_flag = INTERPOSE_REVAL_NEG;
3041 +               if (positive) {
3042 +                       interpose_flag = INTERPOSE_REVAL;
3043 +                       /*
3044 +                        * During BRM, the VFS could already hold a lock on
3045 +                        * a file being read, so don't lock it again
3046 +                        * (deadlock), but if you lock it in this function,
3047 +                        * then release it here too.
3048 +                        */
3049 +                       if (!mutex_is_locked(&dentry->d_inode->i_mutex)) {
3050 +                               mutex_lock(&dentry->d_inode->i_mutex);
3051 +                               locked = 1;
3052 +                       }
3053 +
3054 +                       bstart = ibstart(dentry->d_inode);
3055 +                       bend = ibend(dentry->d_inode);
3056 +                       if (bstart >= 0) {
3057 +                               struct inode *lower_inode;
3058 +                               for (bindex = bstart; bindex <= bend;
3059 +                                    bindex++) {
3060 +                                       lower_inode =
3061 +                                               unionfs_lower_inode_idx(
3062 +                                                       dentry->d_inode,
3063 +                                                       bindex);
3064 +                                       iput(lower_inode);
3065 +                               }
3066 +                       }
3067 +                       kfree(UNIONFS_I(dentry->d_inode)->lower_inodes);
3068 +                       UNIONFS_I(dentry->d_inode)->lower_inodes = NULL;
3069 +                       ibstart(dentry->d_inode) = -1;
3070 +                       ibend(dentry->d_inode) = -1;
3071 +                       if (locked)
3072 +                               mutex_unlock(&dentry->d_inode->i_mutex);
3073 +               }
3074 +
3075 +               result = unionfs_lookup_backend(dentry, &lowernd,
3076 +                                               interpose_flag);
3077 +               if (result) {
3078 +                       if (IS_ERR(result)) {
3079 +                               valid = false;
3080 +                               goto out;
3081 +                       }
3082 +                       /*
3083 +                        * current unionfs_lookup_backend() doesn't return
3084 +                        * a valid dentry
3085 +                        */
3086 +                       dput(dentry);
3087 +                       dentry = result;
3088 +               }
3089 +
3090 +               if (unlikely(positive && UNIONFS_I(dentry->d_inode)->stale)) {
3091 +                       make_bad_inode(dentry->d_inode);
3092 +                       d_drop(dentry);
3093 +                       valid = false;
3094 +                       goto out;
3095 +               }
3096 +               goto out;
3097 +       }
3098 +
3099 +       /* The revalidation must occur across all branches */
3100 +       bstart = dbstart(dentry);
3101 +       bend = dbend(dentry);
3102 +       BUG_ON(bstart == -1);
3103 +       for (bindex = bstart; bindex <= bend; bindex++) {
3104 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3105 +               if (!lower_dentry || !lower_dentry->d_op
3106 +                   || !lower_dentry->d_op->d_revalidate)
3107 +                       continue;
3108 +               /*
3109 +                * Don't pass nameidata to lower file system, because we
3110 +                * don't want an arbitrary lower file being opened or
3111 +                * returned to us: it may be useless to us because of the
3112 +                * fanout nature of unionfs (cf. file/directory open-file
3113 +                * invariants).  We will open lower files as and when needed
3114 +                * later on.
3115 +                */
3116 +               if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3117 +                       valid = false;
3118 +       }
3119 +
3120 +       if (!dentry->d_inode)
3121 +               valid = false;
3122 +
3123 +       if (valid) {
3124 +               /*
3125 +                * If we get here, and we copy the meta-data from the lower
3126 +                * inode to our inode, then it is vital that we have already
3127 +                * purged all unionfs-level file data.  We do that in the
3128 +                * caller (__unionfs_d_revalidate_chain) by calling
3129 +                * purge_inode_data.
3130 +                */
3131 +               unionfs_copy_attr_all(dentry->d_inode,
3132 +                                     unionfs_lower_inode(dentry->d_inode));
3133 +               fsstack_copy_inode_size(dentry->d_inode,
3134 +                                       unionfs_lower_inode(dentry->d_inode));
3135 +       }
3136 +
3137 +out:
3138 +       return valid;
3139 +}
3140 +
3141 +/*
3142 + * Determine if the lower inode objects have changed from below the unionfs
3143 + * inode.  Return true if changed, false otherwise.
3144 + */
3145 +bool is_newer_lower(const struct dentry *dentry)
3146 +{
3147 +       int bindex;
3148 +       struct inode *inode;
3149 +       struct inode *lower_inode;
3150 +
3151 +       /* ignore if we're called on semi-initialized dentries/inodes */
3152 +       if (!dentry || !UNIONFS_D(dentry))
3153 +               return false;
3154 +       inode = dentry->d_inode;
3155 +       if (!inode || !UNIONFS_I(inode) ||
3156 +           ibstart(inode) < 0 || ibend(inode) < 0)
3157 +               return false;
3158 +
3159 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3160 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
3161 +               if (!lower_inode)
3162 +                       continue;
3163 +               /*
3164 +                * We may want to apply other tests to determine if the
3165 +                * lower inode's data has changed, but checking for changed
3166 +                * ctime and mtime on the lower inode should be enough.
3167 +                */
3168 +               if (unlikely(timespec_compare(&inode->i_mtime,
3169 +                                             &lower_inode->i_mtime) < 0)) {
3170 +                       pr_info("unionfs: new lower inode mtime "
3171 +                               "(bindex=%d, name=%s)\n", bindex,
3172 +                               dentry->d_name.name);
3173 +                       show_dinode_times(dentry);
3174 +                       return true; /* mtime changed! */
3175 +               }
3176 +               if (unlikely(timespec_compare(&inode->i_ctime,
3177 +                                             &lower_inode->i_ctime) < 0)) {
3178 +                       pr_info("unionfs: new lower inode ctime "
3179 +                               "(bindex=%d, name=%s)\n", bindex,
3180 +                               dentry->d_name.name);
3181 +                       show_dinode_times(dentry);
3182 +                       return true; /* ctime changed! */
3183 +               }
3184 +       }
3185 +       return false;           /* default: lower is not newer */
3186 +}
3187 +
3188 +/*
3189 + * Purge/remove/unmap all date pages of a unionfs inode.  This is called
3190 + * when the lower inode has changed, and we have to force processes to get
3191 + * the new data.
3192 + *
3193 + * XXX: Our implementation works in that as long as a user process will have
3194 + * caused Unionfs to be called, directly or indirectly, even to just do
3195 + * ->d_revalidate; then we will have purged the current Unionfs data and the
3196 + * process will see the new data.  For example, a process that continually
3197 + * re-reads the same file's data will see the NEW data as soon as the lower
3198 + * file had changed, upon the next read(2) syscall (even if the file is
3199 + * still open!)  However, this doesn't work when the process re-reads the
3200 + * open file's data via mmap(2) (unless the user unmaps/closes the file and
3201 + * remaps/reopens it).  Once we respond to ->readpage(s), then the kernel
3202 + * maps the page into the process's address space and there doesn't appear
3203 + * to be a way to force the kernel to invalidate those pages/mappings, and
3204 + * force the process to re-issue ->readpage.  If there's a way to invalidate
3205 + * active mappings and force a ->readpage, let us know please
3206 + * (invalidate_inode_pages2 doesn't do the trick).
3207 + */
3208 +static inline void purge_inode_data(struct inode *inode)
3209 +{
3210 +       /* remove all non-private mappings */
3211 +       unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3212 +
3213 +       if (inode->i_data.nrpages)
3214 +               truncate_inode_pages(&inode->i_data, 0);
3215 +}
3216 +
3217 +/*
3218 + * Revalidate a parent chain of dentries, then the actual node.
3219 + * Assumes that dentry is locked, but will lock all parents if/when needed.
3220 + *
3221 + * If 'willwrite' is true, and the lower inode times are not in sync, then
3222 + * *don't* purge_inode_data, as it could deadlock if ->write calls us and we
3223 + * try to truncate a locked page.  Besides, if unionfs is about to write
3224 + * data to a file, then there's the data unionfs is about to write is more
3225 + * authoritative than what's below, therefore we can safely overwrite the
3226 + * lower inode times and data.
3227 + */
3228 +bool __unionfs_d_revalidate_chain(struct dentry *dentry, struct nameidata *nd,
3229 +                                 bool willwrite)
3230 +{
3231 +       bool valid = false;     /* default is invalid */
3232 +       struct dentry **chain = NULL; /* chain of dentries to reval */
3233 +       int chain_len = 0;
3234 +       struct dentry *dtmp;
3235 +       int sbgen, dgen, i;
3236 +       int saved_bstart, saved_bend, bindex;
3237 +
3238 +       /* find length of chain needed to revalidate */
3239 +       /* XXX: should I grab some global (dcache?) lock? */
3240 +       chain_len = 0;
3241 +       sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3242 +       dtmp = dentry->d_parent;
3243 +       dgen = atomic_read(&UNIONFS_D(dtmp)->generation);
3244 +       /* XXX: should we check if is_newer_lower all the way up? */
3245 +       if (unlikely(is_newer_lower(dtmp))) {
3246 +               /*
3247 +                * Special case: the root dentry's generation number must
3248 +                * always be valid, but its lower inode times don't have to
3249 +                * be, so sync up the times only.
3250 +                */
3251 +               if (IS_ROOT(dtmp)) {
3252 +                       unionfs_copy_attr_times(dtmp->d_inode);
3253 +               } else {
3254 +                       /*
3255 +                        * reset generation number to zero, guaranteed to be
3256 +                        * "old"
3257 +                        */
3258 +                       dgen = 0;
3259 +                       atomic_set(&UNIONFS_D(dtmp)->generation, dgen);
3260 +               }
3261 +               purge_inode_data(dtmp->d_inode);
3262 +       }
3263 +       while (sbgen != dgen) {
3264 +               /* The root entry should always be valid */
3265 +               BUG_ON(IS_ROOT(dtmp));
3266 +               chain_len++;
3267 +               dtmp = dtmp->d_parent;
3268 +               dgen = atomic_read(&UNIONFS_D(dtmp)->generation);
3269 +       }
3270 +       if (chain_len == 0)
3271 +               goto out_this;  /* shortcut if parents are OK */
3272 +
3273 +       /*
3274 +        * Allocate array of dentries to reval.  We could use linked lists,
3275 +        * but the number of entries we need to alloc here is often small,
3276 +        * and short lived, so locality will be better.
3277 +        */
3278 +       chain = kzalloc(chain_len * sizeof(struct dentry *), GFP_KERNEL);
3279 +       if (unlikely(!chain)) {
3280 +               printk(KERN_CRIT "unionfs: no more memory in %s\n",
3281 +                      __FUNCTION__);
3282 +               goto out;
3283 +       }
3284 +
3285 +       /*
3286 +        * lock all dentries in chain, in child to parent order.
3287 +        * if failed, then sleep for a little, then retry.
3288 +        */
3289 +       dtmp = dentry->d_parent;
3290 +       for (i = chain_len-1; i >= 0; i--) {
3291 +               chain[i] = dget(dtmp);
3292 +               dtmp = dtmp->d_parent;
3293 +       }
3294 +
3295 +       /*
3296 +        * call __unionfs_d_revalidate_one() on each dentry, but in parent
3297 +        * to child order.
3298 +        */
3299 +       for (i = 0; i < chain_len; i++) {
3300 +               unionfs_lock_dentry(chain[i]);
3301 +               saved_bstart = dbstart(chain[i]);
3302 +               saved_bend = dbend(chain[i]);
3303 +               sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3304 +               dgen = atomic_read(&UNIONFS_D(chain[i])->generation);
3305 +
3306 +               valid = __unionfs_d_revalidate_one(chain[i], nd);
3307 +               /* XXX: is this the correct mntput condition?! */
3308 +               if (valid && chain_len > 0 &&
3309 +                   sbgen != dgen && chain[i]->d_inode &&
3310 +                   S_ISDIR(chain[i]->d_inode->i_mode)) {
3311 +                       for (bindex = saved_bstart; bindex <= saved_bend;
3312 +                            bindex++)
3313 +                               unionfs_mntput(chain[i], bindex);
3314 +               }
3315 +               unionfs_unlock_dentry(chain[i]);
3316 +
3317 +               if (unlikely(!valid))
3318 +                       goto out_free;
3319 +       }
3320 +
3321 +
3322 +out_this:
3323 +       /* finally, lock this dentry and revalidate it */
3324 +       verify_locked(dentry);
3325 +       dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3326 +
3327 +       if (unlikely(is_newer_lower(dentry))) {
3328 +               /* root dentry special case as aforementioned */
3329 +               if (IS_ROOT(dentry)) {
3330 +                       unionfs_copy_attr_times(dentry->d_inode);
3331 +               } else {
3332 +                       /*
3333 +                        * reset generation number to zero, guaranteed to be
3334 +                        * "old"
3335 +                        */
3336 +                       dgen = 0;
3337 +                       atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3338 +               }
3339 +               if (!willwrite)
3340 +                       purge_inode_data(dentry->d_inode);
3341 +       }
3342 +       valid = __unionfs_d_revalidate_one(dentry, nd);
3343 +
3344 +       /*
3345 +        * If __unionfs_d_revalidate_one() succeeded above, then it will
3346 +        * have incremented the refcnt of the mnt's, but also the branch
3347 +        * indices of the dentry will have been updated (to take into
3348 +        * account any branch insertions/deletion.  So the current
3349 +        * dbstart/dbend match the current, and new, indices of the mnts
3350 +        * which __unionfs_d_revalidate_one has incremented.  Note: the "if"
3351 +        * test below does not depend on whether chain_len was 0 or greater.
3352 +        */
3353 +       if (valid && sbgen != dgen)
3354 +               for (bindex = dbstart(dentry);
3355 +                    bindex <= dbend(dentry);
3356 +                    bindex++)
3357 +                       unionfs_mntput(dentry, bindex);
3358 +
3359 +out_free:
3360 +       /* unlock/dput all dentries in chain and return status */
3361 +       if (chain_len > 0) {
3362 +               for (i = 0; i < chain_len; i++)
3363 +                       dput(chain[i]);
3364 +               kfree(chain);
3365 +       }
3366 +out:
3367 +       return valid;
3368 +}
3369 +
3370 +static int unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
3371 +{
3372 +       int err;
3373 +
3374 +       unionfs_read_lock(dentry->d_sb);
3375 +
3376 +       unionfs_lock_dentry(dentry);
3377 +       err = __unionfs_d_revalidate_chain(dentry, nd, false);
3378 +       unionfs_unlock_dentry(dentry);
3379 +       if (likely(err > 0)) { /* true==1: dentry is valid */
3380 +               unionfs_check_dentry(dentry);
3381 +               unionfs_check_nd(nd);
3382 +       }
3383 +
3384 +       unionfs_read_unlock(dentry->d_sb);
3385 +
3386 +       return err;
3387 +}
3388 +
3389 +/*
3390 + * At this point no one can reference this dentry, so we don't have to be
3391 + * careful about concurrent access.
3392 + */
3393 +static void unionfs_d_release(struct dentry *dentry)
3394 +{
3395 +       int bindex, bstart, bend;
3396 +
3397 +       unionfs_read_lock(dentry->d_sb);
3398 +
3399 +       unionfs_check_dentry(dentry);
3400 +       /* this could be a negative dentry, so check first */
3401 +       if (unlikely(!UNIONFS_D(dentry))) {
3402 +               printk(KERN_ERR "unionfs: dentry without private data: %.*s\n",
3403 +                      dentry->d_name.len, dentry->d_name.name);
3404 +               goto out;
3405 +       } else if (dbstart(dentry) < 0) {
3406 +               /* this is due to a failed lookup */
3407 +               printk(KERN_ERR "unionfs: dentry without lower "
3408 +                      "dentries: %.*s\n",
3409 +                      dentry->d_name.len, dentry->d_name.name);
3410 +               goto out_free;
3411 +       }
3412 +
3413 +       /* Release all the lower dentries */
3414 +       bstart = dbstart(dentry);
3415 +       bend = dbend(dentry);
3416 +       for (bindex = bstart; bindex <= bend; bindex++) {
3417 +               dput(unionfs_lower_dentry_idx(dentry, bindex));
3418 +               unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3419 +               /* NULL lower mnt is ok if this is a negative dentry */
3420 +               if (!dentry->d_inode && !unionfs_lower_mnt_idx(dentry, bindex))
3421 +                       continue;
3422 +               unionfs_mntput(dentry, bindex);
3423 +               unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
3424 +       }
3425 +       /* free private data (unionfs_dentry_info) here */
3426 +       kfree(UNIONFS_D(dentry)->lower_paths);
3427 +       UNIONFS_D(dentry)->lower_paths = NULL;
3428 +
3429 +out_free:
3430 +       /* No need to unlock it, because it is disappeared. */
3431 +       free_dentry_private_data(dentry);
3432 +
3433 +out:
3434 +       unionfs_read_unlock(dentry->d_sb);
3435 +       return;
3436 +}
3437 +
3438 +struct dentry_operations unionfs_dops = {
3439 +       .d_revalidate   = unionfs_d_revalidate,
3440 +       .d_release      = unionfs_d_release,
3441 +};
3442 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3443 new file mode 100644
3444 index 0000000..c644c13
3445 --- /dev/null
3446 +++ b/fs/unionfs/dirfops.c
3447 @@ -0,0 +1,282 @@
3448 +/*
3449 + * Copyright (c) 2003-2007 Erez Zadok
3450 + * Copyright (c) 2003-2006 Charles P. Wright
3451 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3452 + * Copyright (c) 2005-2006 Junjiro Okajima
3453 + * Copyright (c) 2005      Arun M. Krishnakumar
3454 + * Copyright (c) 2004-2006 David P. Quigley
3455 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3456 + * Copyright (c) 2003      Puja Gupta
3457 + * Copyright (c) 2003      Harikesavan Krishnan
3458 + * Copyright (c) 2003-2007 Stony Brook University
3459 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
3460 + *
3461 + * This program is free software; you can redistribute it and/or modify
3462 + * it under the terms of the GNU General Public License version 2 as
3463 + * published by the Free Software Foundation.
3464 + */
3465 +
3466 +#include "union.h"
3467 +
3468 +/* Make sure our rdstate is playing by the rules. */
3469 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3470 +{
3471 +       BUG_ON(rdstate->offset >= DIREOF);
3472 +       BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3473 +}
3474 +
3475 +struct unionfs_getdents_callback {
3476 +       struct unionfs_dir_state *rdstate;
3477 +       void *dirent;
3478 +       int entries_written;
3479 +       int filldir_called;
3480 +       int filldir_error;
3481 +       filldir_t filldir;
3482 +       struct super_block *sb;
3483 +};
3484 +
3485 +/* based on generic filldir in fs/readir.c */
3486 +static int unionfs_filldir(void *dirent, const char *name, int namelen,
3487 +                          loff_t offset, u64 ino, unsigned int d_type)
3488 +{
3489 +       struct unionfs_getdents_callback *buf = dirent;
3490 +       struct filldir_node *found = NULL;
3491 +       int err = 0;
3492 +       int is_wh_entry = 0;
3493 +
3494 +       buf->filldir_called++;
3495 +
3496 +       if ((namelen > UNIONFS_WHLEN) &&
3497 +           !strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
3498 +               name += UNIONFS_WHLEN;
3499 +               namelen -= UNIONFS_WHLEN;
3500 +               is_wh_entry = 1;
3501 +       }
3502 +
3503 +       found = find_filldir_node(buf->rdstate, name, namelen);
3504 +
3505 +       if (found)
3506 +               goto out;
3507 +
3508 +       /* if 'name' isn't a whiteout, filldir it. */
3509 +       if (!is_wh_entry) {
3510 +               off_t pos = rdstate2offset(buf->rdstate);
3511 +               u64 unionfs_ino = ino;
3512 +
3513 +               err = buf->filldir(buf->dirent, name, namelen, pos,
3514 +                                  unionfs_ino, d_type);
3515 +               buf->rdstate->offset++;
3516 +               verify_rdstate_offset(buf->rdstate);
3517 +       }
3518 +       /*
3519 +        * If we did fill it, stuff it in our hash, otherwise return an
3520 +        * error.
3521 +        */
3522 +       if (err) {
3523 +               buf->filldir_error = err;
3524 +               goto out;
3525 +       }
3526 +       buf->entries_written++;
3527 +       if ((err = add_filldir_node(buf->rdstate, name, namelen,
3528 +                                   buf->rdstate->bindex, is_wh_entry)))
3529 +               buf->filldir_error = err;
3530 +
3531 +out:
3532 +       return err;
3533 +}
3534 +
3535 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3536 +{
3537 +       int err = 0;
3538 +       struct file *lower_file = NULL;
3539 +       struct inode *inode = NULL;
3540 +       struct unionfs_getdents_callback buf;
3541 +       struct unionfs_dir_state *uds;
3542 +       int bend;
3543 +       loff_t offset;
3544 +
3545 +       unionfs_read_lock(file->f_path.dentry->d_sb);
3546 +
3547 +       err = unionfs_file_revalidate(file, false);
3548 +       if (unlikely(err))
3549 +               goto out;
3550 +
3551 +       inode = file->f_path.dentry->d_inode;
3552 +
3553 +       uds = UNIONFS_F(file)->rdstate;
3554 +       if (!uds) {
3555 +               if (file->f_pos == DIREOF) {
3556 +                       goto out;
3557 +               } else if (file->f_pos > 0) {
3558 +                       uds = find_rdstate(inode, file->f_pos);
3559 +                       if (unlikely(!uds)) {
3560 +                               err = -ESTALE;
3561 +                               goto out;
3562 +                       }
3563 +                       UNIONFS_F(file)->rdstate = uds;
3564 +               } else {
3565 +                       init_rdstate(file);
3566 +                       uds = UNIONFS_F(file)->rdstate;
3567 +               }
3568 +       }
3569 +       bend = fbend(file);
3570 +
3571 +       while (uds->bindex <= bend) {
3572 +               lower_file = unionfs_lower_file_idx(file, uds->bindex);
3573 +               if (!lower_file) {
3574 +                       uds->bindex++;
3575 +                       uds->dirpos = 0;
3576 +                       continue;
3577 +               }
3578 +
3579 +               /* prepare callback buffer */
3580 +               buf.filldir_called = 0;
3581 +               buf.filldir_error = 0;
3582 +               buf.entries_written = 0;
3583 +               buf.dirent = dirent;
3584 +               buf.filldir = filldir;
3585 +               buf.rdstate = uds;
3586 +               buf.sb = inode->i_sb;
3587 +
3588 +               /* Read starting from where we last left off. */
3589 +               offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3590 +               if (offset < 0) {
3591 +                       err = offset;
3592 +                       goto out;
3593 +               }
3594 +               err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3595 +
3596 +               /* Save the position for when we continue. */
3597 +               offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3598 +               if (offset < 0) {
3599 +                       err = offset;
3600 +                       goto out;
3601 +               }
3602 +               uds->dirpos = offset;
3603 +
3604 +               /* Copy the atime. */
3605 +               fsstack_copy_attr_atime(inode,
3606 +                                       lower_file->f_path.dentry->d_inode);
3607 +
3608 +               if (err < 0)
3609 +                       goto out;
3610 +
3611 +               if (buf.filldir_error)
3612 +                       break;
3613 +
3614 +               if (!buf.entries_written) {
3615 +                       uds->bindex++;
3616 +                       uds->dirpos = 0;
3617 +               }
3618 +       }
3619 +
3620 +       if (!buf.filldir_error && uds->bindex >= bend) {
3621 +               /* Save the number of hash entries for next time. */
3622 +               UNIONFS_I(inode)->hashsize = uds->hashentries;
3623 +               free_rdstate(uds);
3624 +               UNIONFS_F(file)->rdstate = NULL;
3625 +               file->f_pos = DIREOF;
3626 +       } else {
3627 +               file->f_pos = rdstate2offset(uds);
3628 +       }
3629 +
3630 +out:
3631 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
3632 +       return err;
3633 +}
3634 +
3635 +/*
3636 + * This is not meant to be a generic repositioning function.  If you do
3637 + * things that aren't supported, then we return EINVAL.
3638 + *
3639 + * What is allowed:
3640 + *  (1) seeking to the same position that you are currently at
3641 + *     This really has no effect, but returns where you are.
3642 + *  (2) seeking to the beginning of the file
3643 + *     This throws out all state, and lets you begin again.
3644 + */
3645 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3646 +{
3647 +       struct unionfs_dir_state *rdstate;
3648 +       loff_t err;
3649 +
3650 +       unionfs_read_lock(file->f_path.dentry->d_sb);
3651 +
3652 +       err = unionfs_file_revalidate(file, false);
3653 +       if (unlikely(err))
3654 +               goto out;
3655 +
3656 +       rdstate = UNIONFS_F(file)->rdstate;
3657 +
3658 +       /*
3659 +        * we let users seek to their current position, but not anywhere
3660 +        * else.
3661 +        */
3662 +       if (!offset) {
3663 +               switch (origin) {
3664 +               case SEEK_SET:
3665 +                       if (rdstate) {
3666 +                               free_rdstate(rdstate);
3667 +                               UNIONFS_F(file)->rdstate = NULL;
3668 +                       }
3669 +                       init_rdstate(file);
3670 +                       err = 0;
3671 +                       break;
3672 +               case SEEK_CUR:
3673 +                       err = file->f_pos;
3674 +                       break;
3675 +               case SEEK_END:
3676 +                       /* Unsupported, because we would break everything.  */
3677 +                       err = -EINVAL;
3678 +                       break;
3679 +               }
3680 +       } else {
3681 +               switch (origin) {
3682 +               case SEEK_SET:
3683 +                       if (rdstate) {
3684 +                               if (offset == rdstate2offset(rdstate))
3685 +                                       err = offset;
3686 +                               else if (file->f_pos == DIREOF)
3687 +                                       err = DIREOF;
3688 +                               else
3689 +                                       err = -EINVAL;
3690 +                       } else {
3691 +                               struct inode *inode;
3692 +                               inode = file->f_path.dentry->d_inode;
3693 +                               rdstate = find_rdstate(inode, offset);
3694 +                               if (rdstate) {
3695 +                                       UNIONFS_F(file)->rdstate = rdstate;
3696 +                                       err = rdstate->offset;
3697 +                               } else {
3698 +                                       err = -EINVAL;
3699 +                               }
3700 +                       }
3701 +                       break;
3702 +               case SEEK_CUR:
3703 +               case SEEK_END:
3704 +                       /* Unsupported, because we would break everything.  */
3705 +                       err = -EINVAL;
3706 +                       break;
3707 +               }
3708 +       }
3709 +
3710 +out:
3711 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
3712 +       return err;
3713 +}
3714 +
3715 +/*
3716 + * Trimmed directory options, we shouldn't pass everything down since
3717 + * we don't want to operate on partial directories.
3718 + */
3719 +struct file_operations unionfs_dir_fops = {
3720 +       .llseek         = unionfs_dir_llseek,
3721 +       .read           = generic_read_dir,
3722 +       .readdir        = unionfs_readdir,
3723 +       .unlocked_ioctl = unionfs_ioctl,
3724 +       .open           = unionfs_open,
3725 +       .release        = unionfs_file_release,
3726 +       .flush          = unionfs_flush,
3727 +       .fsync          = unionfs_fsync,
3728 +       .fasync         = unionfs_fasync,
3729 +};
3730 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3731 new file mode 100644
3732 index 0000000..7a28444
3733 --- /dev/null
3734 +++ b/fs/unionfs/dirhelper.c
3735 @@ -0,0 +1,272 @@
3736 +/*
3737 + * Copyright (c) 2003-2007 Erez Zadok
3738 + * Copyright (c) 2003-2006 Charles P. Wright
3739 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3740 + * Copyright (c) 2005-2006 Junjiro Okajima
3741 + * Copyright (c) 2005      Arun M. Krishnakumar
3742 + * Copyright (c) 2004-2006 David P. Quigley
3743 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3744 + * Copyright (c) 2003      Puja Gupta
3745 + * Copyright (c) 2003      Harikesavan Krishnan
3746 + * Copyright (c) 2003-2007 Stony Brook University
3747 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
3748 + *
3749 + * This program is free software; you can redistribute it and/or modify
3750 + * it under the terms of the GNU General Public License version 2 as
3751 + * published by the Free Software Foundation.
3752 + */
3753 +
3754 +#include "union.h"
3755 +
3756 +/*
3757 + * Delete all of the whiteouts in a given directory for rmdir.
3758 + *
3759 + * lower directory inode should be locked
3760 + */
3761 +int do_delete_whiteouts(struct dentry *dentry, int bindex,
3762 +                       struct unionfs_dir_state *namelist)
3763 +{
3764 +       int err = 0;
3765 +       struct dentry *lower_dir_dentry = NULL;
3766 +       struct dentry *lower_dentry;
3767 +       char *name = NULL, *p;
3768 +       struct inode *lower_dir;
3769 +       int i;
3770 +       struct list_head *pos;
3771 +       struct filldir_node *cursor;
3772 +
3773 +       /* Find out lower parent dentry */
3774 +       lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3775 +       BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
3776 +       lower_dir = lower_dir_dentry->d_inode;
3777 +       BUG_ON(!S_ISDIR(lower_dir->i_mode));
3778 +
3779 +       err = -ENOMEM;
3780 +       name = __getname();
3781 +       if (unlikely(!name))
3782 +               goto out;
3783 +       strcpy(name, UNIONFS_WHPFX);
3784 +       p = name + UNIONFS_WHLEN;
3785 +
3786 +       err = 0;
3787 +       for (i = 0; !err && i < namelist->size; i++) {
3788 +               list_for_each(pos, &namelist->list[i]) {
3789 +                       cursor =
3790 +                               list_entry(pos, struct filldir_node,
3791 +                                          file_list);
3792 +                       /* Only operate on whiteouts in this branch. */
3793 +                       if (cursor->bindex != bindex)
3794 +                               continue;
3795 +                       if (!cursor->whiteout)
3796 +                               continue;
3797 +
3798 +                       strcpy(p, cursor->name);
3799 +                       lower_dentry =
3800 +                               lookup_one_len(name, lower_dir_dentry,
3801 +                                              cursor->namelen +
3802 +                                              UNIONFS_WHLEN);
3803 +                       if (IS_ERR(lower_dentry)) {
3804 +                               err = PTR_ERR(lower_dentry);
3805 +                               break;
3806 +                       }
3807 +                       if (lower_dentry->d_inode)
3808 +                               err = vfs_unlink(lower_dir, lower_dentry);
3809 +                       dput(lower_dentry);
3810 +                       if (err)
3811 +                               break;
3812 +               }
3813 +       }
3814 +
3815 +       __putname(name);
3816 +
3817 +       /* After all of the removals, we should copy the attributes once. */
3818 +       fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
3819 +
3820 +out:
3821 +       return err;
3822 +}
3823 +
3824 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
3825 +int delete_whiteouts(struct dentry *dentry, int bindex,
3826 +                    struct unionfs_dir_state *namelist)
3827 +{
3828 +       int err;
3829 +       struct super_block *sb;
3830 +       struct dentry *lower_dir_dentry;
3831 +       struct inode *lower_dir;
3832 +       struct sioq_args args;
3833 +
3834 +       sb = dentry->d_sb;
3835 +
3836 +       BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3837 +       BUG_ON(bindex < dbstart(dentry));
3838 +       BUG_ON(bindex > dbend(dentry));
3839 +       err = is_robranch_super(sb, bindex);
3840 +       if (err)
3841 +               goto out;
3842 +
3843 +       lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3844 +       BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
3845 +       lower_dir = lower_dir_dentry->d_inode;
3846 +       BUG_ON(!S_ISDIR(lower_dir->i_mode));
3847 +
3848 +       mutex_lock(&lower_dir->i_mutex);
3849 +       if (!permission(lower_dir, MAY_WRITE | MAY_EXEC, NULL)) {
3850 +               err = do_delete_whiteouts(dentry, bindex, namelist);
3851 +       } else {
3852 +               args.deletewh.namelist = namelist;
3853 +               args.deletewh.dentry = dentry;
3854 +               args.deletewh.bindex = bindex;
3855 +               run_sioq(__delete_whiteouts, &args);
3856 +               err = args.err;
3857 +       }
3858 +       mutex_unlock(&lower_dir->i_mutex);
3859 +
3860 +out:
3861 +       return err;
3862 +}
3863 +
3864 +#define RD_NONE 0
3865 +#define RD_CHECK_EMPTY 1
3866 +/* The callback structure for check_empty. */
3867 +struct unionfs_rdutil_callback {
3868 +       int err;
3869 +       int filldir_called;
3870 +       struct unionfs_dir_state *rdstate;
3871 +       int mode;
3872 +};
3873 +
3874 +/* This filldir function makes sure only whiteouts exist within a directory. */
3875 +static int readdir_util_callback(void *dirent, const char *name, int namelen,
3876 +                                loff_t offset, u64 ino, unsigned int d_type)
3877 +{
3878 +       int err = 0;
3879 +       struct unionfs_rdutil_callback *buf = dirent;
3880 +       int whiteout = 0;
3881 +       struct filldir_node *found;
3882 +
3883 +       buf->filldir_called = 1;
3884 +
3885 +       if (name[0] == '.' && (namelen == 1 ||
3886 +                              (name[1] == '.' && namelen == 2)))
3887 +               goto out;
3888 +
3889 +       if (namelen > UNIONFS_WHLEN &&
3890 +           !strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
3891 +               namelen -= UNIONFS_WHLEN;
3892 +               name += UNIONFS_WHLEN;
3893 +               whiteout = 1;
3894 +       }
3895 +
3896 +       found = find_filldir_node(buf->rdstate, name, namelen);
3897 +       /* If it was found in the table there was a previous whiteout. */
3898 +       if (found)
3899 +               goto out;
3900 +
3901 +       /*
3902 +        * if it wasn't found and isn't a whiteout, the directory isn't
3903 +        * empty.
3904 +        */
3905 +       err = -ENOTEMPTY;
3906 +       if ((buf->mode == RD_CHECK_EMPTY) && !whiteout)
3907 +               goto out;
3908 +
3909 +       err = add_filldir_node(buf->rdstate, name, namelen,
3910 +                              buf->rdstate->bindex, whiteout);
3911 +
3912 +out:
3913 +       buf->err = err;
3914 +       return err;
3915 +}
3916 +
3917 +/* Is a directory logically empty? */
3918 +int check_empty(struct dentry *dentry, struct unionfs_dir_state **namelist)
3919 +{
3920 +       int err = 0;
3921 +       struct dentry *lower_dentry = NULL;
3922 +       struct super_block *sb;
3923 +       struct file *lower_file;
3924 +       struct unionfs_rdutil_callback *buf = NULL;
3925 +       int bindex, bstart, bend, bopaque;
3926 +
3927 +       sb = dentry->d_sb;
3928 +
3929 +
3930 +       BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3931 +
3932 +       err = unionfs_partial_lookup(dentry);
3933 +       if (err)
3934 +               goto out;
3935 +
3936 +       bstart = dbstart(dentry);
3937 +       bend = dbend(dentry);
3938 +       bopaque = dbopaque(dentry);
3939 +       if (0 <= bopaque && bopaque < bend)
3940 +               bend = bopaque;
3941 +
3942 +       buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3943 +       if (unlikely(!buf)) {
3944 +               err = -ENOMEM;
3945 +               goto out;
3946 +       }
3947 +       buf->err = 0;
3948 +       buf->mode = RD_CHECK_EMPTY;
3949 +       buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3950 +       if (unlikely(!buf->rdstate)) {
3951 +               err = -ENOMEM;
3952 +               goto out;
3953 +       }
3954 +
3955 +       /* Process the lower directories with rdutil_callback as a filldir. */
3956 +       for (bindex = bstart; bindex <= bend; bindex++) {
3957 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3958 +               if (!lower_dentry)
3959 +                       continue;
3960 +               if (!lower_dentry->d_inode)
3961 +                       continue;
3962 +               if (!S_ISDIR(lower_dentry->d_inode->i_mode))
3963 +                       continue;
3964 +
3965 +               dget(lower_dentry);
3966 +               unionfs_mntget(dentry, bindex);
3967 +               branchget(sb, bindex);
3968 +               lower_file =
3969 +                       dentry_open(lower_dentry,
3970 +                                   unionfs_lower_mnt_idx(dentry, bindex),
3971 +                                   O_RDONLY);
3972 +               if (IS_ERR(lower_file)) {
3973 +                       err = PTR_ERR(lower_file);
3974 +                       dput(lower_dentry);
3975 +                       branchput(sb, bindex);
3976 +                       goto out;
3977 +               }
3978 +
3979 +               do {
3980 +                       buf->filldir_called = 0;
3981 +                       buf->rdstate->bindex = bindex;
3982 +                       err = vfs_readdir(lower_file,
3983 +                                         readdir_util_callback, buf);
3984 +                       if (buf->err)
3985 +                               err = buf->err;
3986 +               } while ((err >= 0) && buf->filldir_called);
3987 +
3988 +               /* fput calls dput for lower_dentry */
3989 +               fput(lower_file);
3990 +               branchput(sb, bindex);
3991 +
3992 +               if (err < 0)
3993 +                       goto out;
3994 +       }
3995 +
3996 +out:
3997 +       if (buf) {
3998 +               if (namelist && !err)
3999 +                       *namelist = buf->rdstate;
4000 +               else if (buf->rdstate)
4001 +                       free_rdstate(buf->rdstate);
4002 +               kfree(buf);
4003 +       }
4004 +
4005 +
4006 +       return err;
4007 +}
4008 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4009 new file mode 100644
4010 index 0000000..ec18013
4011 --- /dev/null
4012 +++ b/fs/unionfs/fanout.h
4013 @@ -0,0 +1,355 @@
4014 +/*
4015 + * Copyright (c) 2003-2007 Erez Zadok
4016 + * Copyright (c) 2003-2006 Charles P. Wright
4017 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4018 + * Copyright (c) 2005      Arun M. Krishnakumar
4019 + * Copyright (c) 2004-2006 David P. Quigley
4020 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4021 + * Copyright (c) 2003      Puja Gupta
4022 + * Copyright (c) 2003      Harikesavan Krishnan
4023 + * Copyright (c) 2003-2007 Stony Brook University
4024 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
4025 + *
4026 + * This program is free software; you can redistribute it and/or modify
4027 + * it under the terms of the GNU General Public License version 2 as
4028 + * published by the Free Software Foundation.
4029 + */
4030 +
4031 +#ifndef _FANOUT_H_
4032 +#define _FANOUT_H_
4033 +
4034 +/*
4035 + * Inode to private data
4036 + *
4037 + * Since we use containers and the struct inode is _inside_ the
4038 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4039 + * inode pointer), return a valid non-NULL pointer.
4040 + */
4041 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4042 +{
4043 +       return container_of(inode, struct unionfs_inode_info, vfs_inode);
4044 +}
4045 +
4046 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4047 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4048 +
4049 +/* Superblock to private data */
4050 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4051 +#define sbstart(sb) 0
4052 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4053 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4054 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4055 +
4056 +/* File to private Data */
4057 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4058 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4059 +#define fbend(file) (UNIONFS_F(file)->bend)
4060 +
4061 +/* macros to manipulate branch IDs in stored in our superblock */
4062 +static inline int branch_id(struct super_block *sb, int index)
4063 +{
4064 +       BUG_ON(!sb || index < 0);
4065 +       return UNIONFS_SB(sb)->data[index].branch_id;
4066 +}
4067 +
4068 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4069 +{
4070 +       BUG_ON(!sb || index < 0);
4071 +       UNIONFS_SB(sb)->data[index].branch_id = val;
4072 +}
4073 +
4074 +static inline void new_branch_id(struct super_block *sb, int index)
4075 +{
4076 +       BUG_ON(!sb || index < 0);
4077 +       set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4078 +}
4079 +
4080 +/*
4081 + * Find new index of matching branch with an existing superblock of a known
4082 + * (possibly old) id.  This is needed because branches could have been
4083 + * added/deleted causing the branches of any open files to shift.
4084 + *
4085 + * @sb: the new superblock which may have new/different branch IDs
4086 + * @id: the old/existing id we're looking for
4087 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4088 + */
4089 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4090 +{
4091 +       int i;
4092 +       for (i = 0; i < sbmax(sb); i++) {
4093 +               if (branch_id(sb, i) == id)
4094 +                       return i;
4095 +       }
4096 +       /* in the non-ODF code, this should really never happen */
4097 +       printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4098 +       return -1;
4099 +}
4100 +
4101 +/* File to lower file. */
4102 +static inline struct file *unionfs_lower_file(const struct file *f)
4103 +{
4104 +       BUG_ON(!f);
4105 +       return UNIONFS_F(f)->lower_files[fbstart(f)];
4106 +}
4107 +
4108 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4109 +                                                 int index)
4110 +{
4111 +       BUG_ON(!f || index < 0);
4112 +       return UNIONFS_F(f)->lower_files[index];
4113 +}
4114 +
4115 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4116 +                                             struct file *val)
4117 +{
4118 +       BUG_ON(!f || index < 0);
4119 +       UNIONFS_F(f)->lower_files[index] = val;
4120 +       /* save branch ID (may be redundant?) */
4121 +       UNIONFS_F(f)->saved_branch_ids[index] =
4122 +               branch_id((f)->f_dentry->d_sb, index);
4123 +}
4124 +
4125 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4126 +{
4127 +       BUG_ON(!f);
4128 +       unionfs_set_lower_file_idx((f), fbstart(f), (val));
4129 +}
4130 +
4131 +/* Inode to lower inode. */
4132 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4133 +{
4134 +       BUG_ON(!i);
4135 +       return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4136 +}
4137 +
4138 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4139 +                                                   int index)
4140 +{
4141 +       BUG_ON(!i || index < 0);
4142 +       return UNIONFS_I(i)->lower_inodes[index];
4143 +}
4144 +
4145 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4146 +                                              struct inode *val)
4147 +{
4148 +       BUG_ON(!i || index < 0);
4149 +       UNIONFS_I(i)->lower_inodes[index] = val;
4150 +}
4151 +
4152 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4153 +{
4154 +       BUG_ON(!i);
4155 +       UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4156 +}
4157 +
4158 +/* Superblock to lower superblock. */
4159 +static inline struct super_block *unionfs_lower_super(
4160 +                                       const struct super_block *sb)
4161 +{
4162 +       BUG_ON(!sb);
4163 +       return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4164 +}
4165 +
4166 +static inline struct super_block *unionfs_lower_super_idx(
4167 +                                       const struct super_block *sb,
4168 +                                       int index)
4169 +{
4170 +       BUG_ON(!sb || index < 0);
4171 +       return UNIONFS_SB(sb)->data[index].sb;
4172 +}
4173 +
4174 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4175 +                                              int index,
4176 +                                              struct super_block *val)
4177 +{
4178 +       BUG_ON(!sb || index < 0);
4179 +       UNIONFS_SB(sb)->data[index].sb = val;
4180 +}
4181 +
4182 +static inline void unionfs_set_lower_super(struct super_block *sb,
4183 +                                          struct super_block *val)
4184 +{
4185 +       BUG_ON(!sb);
4186 +       UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4187 +}
4188 +
4189 +/* Branch count macros. */
4190 +static inline int branch_count(const struct super_block *sb, int index)
4191 +{
4192 +       BUG_ON(!sb || index < 0);
4193 +       return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4194 +}
4195 +
4196 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4197 +{
4198 +       BUG_ON(!sb || index < 0);
4199 +       atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4200 +}
4201 +
4202 +static inline void branchget(struct super_block *sb, int index)
4203 +{
4204 +       BUG_ON(!sb || index < 0);
4205 +       atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4206 +}
4207 +
4208 +static inline void branchput(struct super_block *sb, int index)
4209 +{
4210 +       BUG_ON(!sb || index < 0);
4211 +       atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4212 +}
4213 +
4214 +/* Dentry macros */
4215 +static inline struct unionfs_dentry_info *UNIONFS_D(const struct dentry *dent)
4216 +{
4217 +       BUG_ON(!dent);
4218 +       return dent->d_fsdata;
4219 +}
4220 +
4221 +static inline int dbstart(const struct dentry *dent)
4222 +{
4223 +       BUG_ON(!dent);
4224 +       return UNIONFS_D(dent)->bstart;
4225 +}
4226 +
4227 +static inline void set_dbstart(struct dentry *dent, int val)
4228 +{
4229 +       BUG_ON(!dent);
4230 +       UNIONFS_D(dent)->bstart = val;
4231 +}
4232 +
4233 +static inline int dbend(const struct dentry *dent)
4234 +{
4235 +       BUG_ON(!dent);
4236 +       return UNIONFS_D(dent)->bend;
4237 +}
4238 +
4239 +static inline void set_dbend(struct dentry *dent, int val)
4240 +{
4241 +       BUG_ON(!dent);
4242 +       UNIONFS_D(dent)->bend = val;
4243 +}
4244 +
4245 +static inline int dbopaque(const struct dentry *dent)
4246 +{
4247 +       BUG_ON(!dent);
4248 +       return UNIONFS_D(dent)->bopaque;
4249 +}
4250 +
4251 +static inline void set_dbopaque(struct dentry *dent, int val)
4252 +{
4253 +       BUG_ON(!dent);
4254 +       UNIONFS_D(dent)->bopaque = val;
4255 +}
4256 +
4257 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4258 +                                               struct dentry *val)
4259 +{
4260 +       BUG_ON(!dent || index < 0);
4261 +       UNIONFS_D(dent)->lower_paths[index].dentry = val;
4262 +}
4263 +
4264 +static inline struct dentry *unionfs_lower_dentry_idx(
4265 +                               const struct dentry *dent,
4266 +                               int index)
4267 +{
4268 +       BUG_ON(!dent || index < 0);
4269 +       return UNIONFS_D(dent)->lower_paths[index].dentry;
4270 +}
4271 +
4272 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4273 +{
4274 +       BUG_ON(!dent);
4275 +       return unionfs_lower_dentry_idx(dent, dbstart(dent));
4276 +}
4277 +
4278 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4279 +                                            struct vfsmount *mnt)
4280 +{
4281 +       BUG_ON(!dent || index < 0);
4282 +       UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4283 +}
4284 +
4285 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4286 +                                       const struct dentry *dent,
4287 +                                       int index)
4288 +{
4289 +       BUG_ON(!dent || index < 0);
4290 +       return UNIONFS_D(dent)->lower_paths[index].mnt;
4291 +}
4292 +
4293 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4294 +{
4295 +       BUG_ON(!dent);
4296 +       return unionfs_lower_mnt_idx(dent, dbstart(dent));
4297 +}
4298 +
4299 +/* Macros for locking a dentry. */
4300 +static inline void unionfs_lock_dentry(struct dentry *d)
4301 +{
4302 +       BUG_ON(!d);
4303 +       mutex_lock(&UNIONFS_D(d)->lock);
4304 +}
4305 +
4306 +static inline void unionfs_unlock_dentry(struct dentry *d)
4307 +{
4308 +       BUG_ON(!d);
4309 +       mutex_unlock(&UNIONFS_D(d)->lock);
4310 +}
4311 +
4312 +static inline void verify_locked(struct dentry *d)
4313 +{
4314 +       BUG_ON(!d);
4315 +       BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4316 +}
4317 +
4318 +/* copy a/m/ctime from the lower branch with the newest times */
4319 +static inline void unionfs_copy_attr_times(struct inode *upper)
4320 +{
4321 +       int bindex;
4322 +       struct inode *lower;
4323 +
4324 +       if (!upper || ibstart(upper) < 0)
4325 +               return;
4326 +       for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
4327 +               lower = unionfs_lower_inode_idx(upper, bindex);
4328 +               if (!lower)
4329 +                       continue; /* not all lower dir objects may exist */
4330 +               if (unlikely(timespec_compare(&upper->i_mtime,
4331 +                                             &lower->i_mtime) < 0))
4332 +                       upper->i_mtime = lower->i_mtime;
4333 +               if (unlikely(timespec_compare(&upper->i_ctime,
4334 +                                             &lower->i_ctime) < 0))
4335 +                       upper->i_ctime = lower->i_ctime;
4336 +               if (unlikely(timespec_compare(&upper->i_atime,
4337 +                                             &lower->i_atime) < 0))
4338 +                       upper->i_atime = lower->i_atime;
4339 +       }
4340 +}
4341 +
4342 +/*
4343 + * A unionfs/fanout version of fsstack_copy_attr_all.  Uses a
4344 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
4345 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
4346 + * important if the lower inode is a directory type)
4347 + */
4348 +static inline void unionfs_copy_attr_all(struct inode *dest,
4349 +                                        const struct inode *src)
4350 +{
4351 +       dest->i_mode = src->i_mode;
4352 +       dest->i_uid = src->i_uid;
4353 +       dest->i_gid = src->i_gid;
4354 +       dest->i_rdev = src->i_rdev;
4355 +
4356 +       unionfs_copy_attr_times(dest);
4357 +
4358 +       dest->i_blkbits = src->i_blkbits;
4359 +       dest->i_flags = src->i_flags;
4360 +
4361 +       /*
4362 +        * Update the nlinks AFTER updating the above fields, because the
4363 +        * get_links callback may depend on them.
4364 +        */
4365 +       dest->i_nlink = unionfs_get_nlinks(dest);
4366 +}
4367 +
4368 +#endif /* not _FANOUT_H */
4369 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4370 new file mode 100644
4371 index 0000000..126df5e
4372 --- /dev/null
4373 +++ b/fs/unionfs/file.c
4374 @@ -0,0 +1,257 @@
4375 +/*
4376 + * Copyright (c) 2003-2007 Erez Zadok
4377 + * Copyright (c) 2003-2006 Charles P. Wright
4378 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4379 + * Copyright (c) 2005-2006 Junjiro Okajima
4380 + * Copyright (c) 2005      Arun M. Krishnakumar
4381 + * Copyright (c) 2004-2006 David P. Quigley
4382 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4383 + * Copyright (c) 2003      Puja Gupta
4384 + * Copyright (c) 2003      Harikesavan Krishnan
4385 + * Copyright (c) 2003-2007 Stony Brook University
4386 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
4387 + *
4388 + * This program is free software; you can redistribute it and/or modify
4389 + * it under the terms of the GNU General Public License version 2 as
4390 + * published by the Free Software Foundation.
4391 + */
4392 +
4393 +#include "union.h"
4394 +
4395 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4396 +                           size_t count, loff_t *ppos)
4397 +{
4398 +       int err;
4399 +
4400 +       unionfs_read_lock(file->f_path.dentry->d_sb);
4401 +       err = unionfs_file_revalidate(file, false);
4402 +       if (unlikely(err))
4403 +               goto out;
4404 +       unionfs_check_file(file);
4405 +
4406 +       err = do_sync_read(file, buf, count, ppos);
4407 +
4408 +       if (err >= 0)
4409 +               touch_atime(unionfs_lower_mnt(file->f_path.dentry),
4410 +                           unionfs_lower_dentry(file->f_path.dentry));
4411 +
4412 +out:
4413 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
4414 +       unionfs_check_file(file);
4415 +       return err;
4416 +}
4417 +
4418 +static ssize_t unionfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
4419 +                               unsigned long nr_segs, loff_t pos)
4420 +{
4421 +       int err = 0;
4422 +       struct file *file = iocb->ki_filp;
4423 +
4424 +       unionfs_read_lock(file->f_path.dentry->d_sb);
4425 +       err = unionfs_file_revalidate(file, false);
4426 +       if (unlikely(err))
4427 +               goto out;
4428 +       unionfs_check_file(file);
4429 +
4430 +       err = generic_file_aio_read(iocb, iov, nr_segs, pos);
4431 +
4432 +       if (err == -EIOCBQUEUED)
4433 +               err = wait_on_sync_kiocb(iocb);
4434 +
4435 +       if (err >= 0)
4436 +               touch_atime(unionfs_lower_mnt(file->f_path.dentry),
4437 +                           unionfs_lower_dentry(file->f_path.dentry));
4438 +
4439 +out:
4440 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
4441 +       unionfs_check_file(file);
4442 +       return err;
4443 +}
4444 +
4445 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4446 +                            size_t count, loff_t *ppos)
4447 +{
4448 +       int err = 0;
4449 +
4450 +       unionfs_read_lock(file->f_path.dentry->d_sb);
4451 +       err = unionfs_file_revalidate(file, true);
4452 +       if (unlikely(err))
4453 +               goto out;
4454 +       unionfs_check_file(file);
4455 +
4456 +       err = do_sync_write(file, buf, count, ppos);
4457 +       /* update our inode times upon a successful lower write */
4458 +       if (err >= 0) {
4459 +               unionfs_copy_attr_times(file->f_path.dentry->d_inode);
4460 +               unionfs_check_file(file);
4461 +       }
4462 +
4463 +out:
4464 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
4465 +       return err;
4466 +}
4467 +
4468 +static int unionfs_file_readdir(struct file *file, void *dirent,
4469 +                               filldir_t filldir)
4470 +{
4471 +       return -ENOTDIR;
4472 +}
4473 +
4474 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4475 +{
4476 +       int err = 0;
4477 +       bool willwrite;
4478 +       struct file *lower_file;
4479 +
4480 +       unionfs_read_lock(file->f_path.dentry->d_sb);
4481 +
4482 +       /* This might be deferred to mmap's writepage */
4483 +       willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4484 +       err = unionfs_file_revalidate(file, willwrite);
4485 +       if (unlikely(err))
4486 +               goto out;
4487 +       unionfs_check_file(file);
4488 +
4489 +       /*
4490 +        * File systems which do not implement ->writepage may use
4491 +        * generic_file_readonly_mmap as their ->mmap op.  If you call
4492 +        * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4493 +        * But we cannot call the lower ->mmap op, so we can't tell that
4494 +        * writeable mappings won't work.  Therefore, our only choice is to
4495 +        * check if the lower file system supports the ->writepage, and if
4496 +        * not, return EINVAL (the same error that
4497 +        * generic_file_readonly_mmap returns in that case).
4498 +        */
4499 +       lower_file = unionfs_lower_file(file);
4500 +       if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4501 +               err = -EINVAL;
4502 +               printk(KERN_ERR "unionfs: branch %d file system does not "
4503 +                      "support writeable mmap\n", fbstart(file));
4504 +       } else {
4505 +               err = generic_file_mmap(file, vma);
4506 +               if (err)
4507 +                       printk(KERN_ERR
4508 +                              "unionfs: generic_file_mmap failed %d\n", err);
4509 +       }
4510 +
4511 +out:
4512 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
4513 +       if (!err) {
4514 +               /* copyup could cause parent dir times to change */
4515 +               unionfs_copy_attr_times(file->f_path.dentry->d_parent->d_inode);
4516 +               unionfs_check_file(file);
4517 +               unionfs_check_dentry(file->f_path.dentry->d_parent);
4518 +       }
4519 +       return err;
4520 +}
4521 +
4522 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4523 +{
4524 +       int bindex, bstart, bend;
4525 +       struct file *lower_file;
4526 +       struct dentry *lower_dentry;
4527 +       struct inode *lower_inode, *inode;
4528 +       int err = -EINVAL;
4529 +
4530 +       unionfs_read_lock(file->f_path.dentry->d_sb);
4531 +       err = unionfs_file_revalidate(file, true);
4532 +       if (unlikely(err))
4533 +               goto out;
4534 +       unionfs_check_file(file);
4535 +
4536 +       bstart = fbstart(file);
4537 +       bend = fbend(file);
4538 +       if (bstart < 0 || bend < 0)
4539 +               goto out;
4540 +
4541 +       inode = dentry->d_inode;
4542 +       if (unlikely(!inode)) {
4543 +               printk(KERN_ERR
4544 +                      "unionfs: null lower inode in unionfs_fsync\n");
4545 +               goto out;
4546 +       }
4547 +       for (bindex = bstart; bindex <= bend; bindex++) {
4548 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
4549 +               if (!lower_inode || !lower_inode->i_fop->fsync)
4550 +                       continue;
4551 +               lower_file = unionfs_lower_file_idx(file, bindex);
4552 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4553 +               mutex_lock(&lower_inode->i_mutex);
4554 +               err = lower_inode->i_fop->fsync(lower_file,
4555 +                                               lower_dentry,
4556 +                                               datasync);
4557 +               mutex_unlock(&lower_inode->i_mutex);
4558 +               if (err)
4559 +                       goto out;
4560 +       }
4561 +
4562 +       unionfs_copy_attr_times(inode);
4563 +
4564 +out:
4565 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
4566 +       unionfs_check_file(file);
4567 +       return err;
4568 +}
4569 +
4570 +int unionfs_fasync(int fd, struct file *file, int flag)
4571 +{
4572 +       int bindex, bstart, bend;
4573 +       struct file *lower_file;
4574 +       struct dentry *dentry;
4575 +       struct inode *lower_inode, *inode;
4576 +       int err = 0;
4577 +
4578 +       unionfs_read_lock(file->f_path.dentry->d_sb);
4579 +       err = unionfs_file_revalidate(file, true);
4580 +       if (unlikely(err))
4581 +               goto out;
4582 +       unionfs_check_file(file);
4583 +
4584 +       bstart = fbstart(file);
4585 +       bend = fbend(file);
4586 +       if (bstart < 0 || bend < 0)
4587 +               goto out;
4588 +
4589 +       dentry = file->f_path.dentry;
4590 +       inode = dentry->d_inode;
4591 +       if (unlikely(!inode)) {
4592 +               printk(KERN_ERR
4593 +                      "unionfs: null lower inode in unionfs_fasync\n");
4594 +               goto out;
4595 +       }
4596 +       for (bindex = bstart; bindex <= bend; bindex++) {
4597 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
4598 +               if (!lower_inode || !lower_inode->i_fop->fasync)
4599 +                       continue;
4600 +               lower_file = unionfs_lower_file_idx(file, bindex);
4601 +               mutex_lock(&lower_inode->i_mutex);
4602 +               err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4603 +               mutex_unlock(&lower_inode->i_mutex);
4604 +               if (err)
4605 +                       goto out;
4606 +       }
4607 +
4608 +       unionfs_copy_attr_times(inode);
4609 +
4610 +out:
4611 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
4612 +       unionfs_check_file(file);
4613 +       return err;
4614 +}
4615 +
4616 +struct file_operations unionfs_main_fops = {
4617 +       .llseek         = generic_file_llseek,
4618 +       .read           = unionfs_read,
4619 +       .aio_read       = unionfs_aio_read,
4620 +       .write          = unionfs_write,
4621 +       .aio_write      = generic_file_aio_write,
4622 +       .readdir        = unionfs_file_readdir,
4623 +       .unlocked_ioctl = unionfs_ioctl,
4624 +       .mmap           = unionfs_mmap,
4625 +       .open           = unionfs_open,
4626 +       .flush          = unionfs_flush,
4627 +       .release        = unionfs_file_release,
4628 +       .fsync          = unionfs_fsync,
4629 +       .fasync         = unionfs_fasync,
4630 +       .splice_read    = generic_file_splice_read,
4631 +};
4632 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4633 new file mode 100644
4634 index 0000000..4e59ace
4635 --- /dev/null
4636 +++ b/fs/unionfs/inode.c
4637 @@ -0,0 +1,1157 @@
4638 +/*
4639 + * Copyright (c) 2003-2007 Erez Zadok
4640 + * Copyright (c) 2003-2006 Charles P. Wright
4641 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4642 + * Copyright (c) 2005-2006 Junjiro Okajima
4643 + * Copyright (c) 2005      Arun M. Krishnakumar
4644 + * Copyright (c) 2004-2006 David P. Quigley
4645 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4646 + * Copyright (c) 2003      Puja Gupta
4647 + * Copyright (c) 2003      Harikesavan Krishnan
4648 + * Copyright (c) 2003-2007 Stony Brook University
4649 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
4650 + *
4651 + * This program is free software; you can redistribute it and/or modify
4652 + * it under the terms of the GNU General Public License version 2 as
4653 + * published by the Free Software Foundation.
4654 + */
4655 +
4656 +#include "union.h"
4657 +
4658 +static int unionfs_create(struct inode *parent, struct dentry *dentry,
4659 +                         int mode, struct nameidata *nd)
4660 +{
4661 +       int err = 0;
4662 +       struct dentry *lower_dentry = NULL;
4663 +       struct dentry *wh_dentry = NULL;
4664 +       struct dentry *lower_parent_dentry = NULL;
4665 +       char *name = NULL;
4666 +       int valid = 0;
4667 +       struct nameidata lower_nd;
4668 +
4669 +       unionfs_read_lock(dentry->d_sb);
4670 +       unionfs_lock_dentry(dentry);
4671 +
4672 +       unionfs_lock_dentry(dentry->d_parent);
4673 +       valid = __unionfs_d_revalidate_chain(dentry->d_parent, nd, false);
4674 +       unionfs_unlock_dentry(dentry->d_parent);
4675 +       if (unlikely(!valid)) {
4676 +               err = -ESTALE;  /* same as what real_lookup does */
4677 +               goto out;
4678 +       }
4679 +       valid = __unionfs_d_revalidate_chain(dentry, nd, false);
4680 +       /*
4681 +        * It's only a bug if this dentry was not negative and couldn't be
4682 +        * revalidated (shouldn't happen).
4683 +        */
4684 +       BUG_ON(!valid && dentry->d_inode);
4685 +
4686 +       /*
4687 +        * We shouldn't create things in a read-only branch; this check is a
4688 +        * bit redundant as we don't allow branch 0 to be read-only at the
4689 +        * moment
4690 +        */
4691 +       err = is_robranch_super(dentry->d_sb, 0);
4692 +       if (err) {
4693 +               err = -EROFS;
4694 +               goto out;
4695 +       }
4696 +
4697 +       /*
4698 +        * We _always_ create on branch 0
4699 +        */
4700 +       lower_dentry = unionfs_lower_dentry_idx(dentry, 0);
4701 +       if (lower_dentry) {
4702 +               /*
4703 +                * check if whiteout exists in this branch, i.e. lookup .wh.foo
4704 +                * first.
4705 +                */
4706 +               name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
4707 +               if (unlikely(IS_ERR(name))) {
4708 +                       err = PTR_ERR(name);
4709 +                       goto out;
4710 +               }
4711 +
4712 +               wh_dentry = lookup_one_len(name, lower_dentry->d_parent,
4713 +                                          dentry->d_name.len + UNIONFS_WHLEN);
4714 +               if (IS_ERR(wh_dentry)) {
4715 +                       err = PTR_ERR(wh_dentry);
4716 +                       wh_dentry = NULL;
4717 +                       goto out;
4718 +               }
4719 +
4720 +               if (wh_dentry->d_inode) {
4721 +                       /*
4722 +                        * .wh.foo has been found, so let's unlink it
4723 +                        */
4724 +                       struct dentry *lower_dir_dentry;
4725 +
4726 +                       lower_dir_dentry = lock_parent(wh_dentry);
4727 +                       err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
4728 +                       unlock_dir(lower_dir_dentry);
4729 +
4730 +                       if (err) {
4731 +                               printk(KERN_ERR "unionfs: create: could not "
4732 +                                      "unlink whiteout, err = %d\n", err);
4733 +                               goto out;
4734 +                       }
4735 +               }
4736 +       } else {
4737 +               /*
4738 +                * if lower_dentry is NULL, create the entire
4739 +                * dentry directory structure in branch 0.
4740 +                */
4741 +               lower_dentry = create_parents(parent, dentry,
4742 +                                             dentry->d_name.name, 0);
4743 +               if (IS_ERR(lower_dentry)) {
4744 +                       err = PTR_ERR(lower_dentry);
4745 +                       goto out;
4746 +               }
4747 +       }
4748 +
4749 +       lower_parent_dentry = lock_parent(lower_dentry);
4750 +       if (IS_ERR(lower_parent_dentry)) {
4751 +               err = PTR_ERR(lower_parent_dentry);
4752 +               goto out;
4753 +       }
4754 +
4755 +       err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4756 +       if (unlikely(err < 0))
4757 +               goto out;
4758 +       err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4759 +                        &lower_nd);
4760 +       release_lower_nd(&lower_nd, err);
4761 +
4762 +       if (!err) {
4763 +               err = PTR_ERR(unionfs_interpose(dentry, parent->i_sb, 0));
4764 +               if (!err) {
4765 +                       unionfs_copy_attr_times(parent);
4766 +                       fsstack_copy_inode_size(parent,
4767 +                                               lower_parent_dentry->d_inode);
4768 +                       /* update no. of links on parent directory */
4769 +                       parent->i_nlink = unionfs_get_nlinks(parent);
4770 +               }
4771 +       }
4772 +
4773 +       unlock_dir(lower_parent_dentry);
4774 +
4775 +out:
4776 +       dput(wh_dentry);
4777 +       kfree(name);
4778 +
4779 +       if (!err)
4780 +               unionfs_postcopyup_setmnt(dentry);
4781 +       unionfs_unlock_dentry(dentry);
4782 +       unionfs_read_unlock(dentry->d_sb);
4783 +
4784 +       unionfs_check_inode(parent);
4785 +       if (!err) {
4786 +               unionfs_check_dentry(dentry->d_parent);
4787 +               unionfs_check_nd(nd);
4788 +       }
4789 +       unionfs_check_dentry(dentry);
4790 +       return err;
4791 +}
4792 +
4793 +/*
4794 + * unionfs_lookup is the only special function which takes a dentry, yet we
4795 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
4796 + * we don't have a valid dentry here yet.
4797 + */
4798 +static struct dentry *unionfs_lookup(struct inode *parent,
4799 +                                    struct dentry *dentry,
4800 +                                    struct nameidata *nd)
4801 +{
4802 +       struct path path_save;
4803 +       struct dentry *ret;
4804 +
4805 +       unionfs_read_lock(dentry->d_sb);
4806 +
4807 +       /* save the dentry & vfsmnt from namei */
4808 +       if (nd) {
4809 +               path_save.dentry = nd->dentry;
4810 +               path_save.mnt = nd->mnt;
4811 +       }
4812 +
4813 +       /*
4814 +        * unionfs_lookup_backend returns a locked dentry upon success,
4815 +        * so we'll have to unlock it below.
4816 +        */
4817 +       ret = unionfs_lookup_backend(dentry, nd, INTERPOSE_LOOKUP);
4818 +
4819 +       /* restore the dentry & vfsmnt in namei */
4820 +       if (nd) {
4821 +               nd->dentry = path_save.dentry;
4822 +               nd->mnt = path_save.mnt;
4823 +       }
4824 +       if (!IS_ERR(ret)) {
4825 +               if (ret)
4826 +                       dentry = ret;
4827 +               /* parent times may have changed */
4828 +               unionfs_copy_attr_times(dentry->d_parent->d_inode);
4829 +               unionfs_unlock_dentry(dentry);
4830 +       }
4831 +
4832 +       unionfs_check_inode(parent);
4833 +       unionfs_check_dentry(dentry);
4834 +       unionfs_check_dentry(dentry->d_parent);
4835 +       unionfs_check_nd(nd);
4836 +       unionfs_read_unlock(dentry->d_sb);
4837 +
4838 +       return ret;
4839 +}
4840 +
4841 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
4842 +                       struct dentry *new_dentry)
4843 +{
4844 +       int err = 0;
4845 +       struct dentry *lower_old_dentry = NULL;
4846 +       struct dentry *lower_new_dentry = NULL;
4847 +       struct dentry *lower_dir_dentry = NULL;
4848 +       struct dentry *whiteout_dentry;
4849 +       char *name = NULL;
4850 +
4851 +       unionfs_read_lock(old_dentry->d_sb);
4852 +       unionfs_double_lock_dentry(new_dentry, old_dentry);
4853 +
4854 +       if (unlikely(!__unionfs_d_revalidate_chain(old_dentry, NULL, false))) {
4855 +               err = -ESTALE;
4856 +               goto out;
4857 +       }
4858 +       if (unlikely(new_dentry->d_inode &&
4859 +                    !__unionfs_d_revalidate_chain(new_dentry, NULL, false))) {
4860 +               err = -ESTALE;
4861 +               goto out;
4862 +       }
4863 +
4864 +       lower_new_dentry = unionfs_lower_dentry(new_dentry);
4865 +
4866 +       /*
4867 +        * check if whiteout exists in the branch of new dentry, i.e. lookup
4868 +        * .wh.foo first. If present, delete it
4869 +        */
4870 +       name = alloc_whname(new_dentry->d_name.name, new_dentry->d_name.len);
4871 +       if (unlikely(IS_ERR(name))) {
4872 +               err = PTR_ERR(name);
4873 +               goto out;
4874 +       }
4875 +
4876 +       whiteout_dentry = lookup_one_len(name, lower_new_dentry->d_parent,
4877 +                                        new_dentry->d_name.len +
4878 +                                        UNIONFS_WHLEN);
4879 +       if (IS_ERR(whiteout_dentry)) {
4880 +               err = PTR_ERR(whiteout_dentry);
4881 +               goto out;
4882 +       }
4883 +
4884 +       if (!whiteout_dentry->d_inode) {
4885 +               dput(whiteout_dentry);
4886 +               whiteout_dentry = NULL;
4887 +       } else {
4888 +               /* found a .wh.foo entry, unlink it and then call vfs_link() */
4889 +               lower_dir_dentry = lock_parent(whiteout_dentry);
4890 +               err = is_robranch_super(new_dentry->d_sb, dbstart(new_dentry));
4891 +               if (!err)
4892 +                       err = vfs_unlink(lower_dir_dentry->d_inode,
4893 +                                        whiteout_dentry);
4894 +
4895 +               fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
4896 +               dir->i_nlink = unionfs_get_nlinks(dir);
4897 +               unlock_dir(lower_dir_dentry);
4898 +               lower_dir_dentry = NULL;
4899 +               dput(whiteout_dentry);
4900 +               if (err)
4901 +                       goto out;
4902 +       }
4903 +
4904 +       if (dbstart(old_dentry) != dbstart(new_dentry)) {
4905 +               lower_new_dentry = create_parents(dir, new_dentry,
4906 +                                                 new_dentry->d_name.name,
4907 +                                                 dbstart(old_dentry));
4908 +               err = PTR_ERR(lower_new_dentry);
4909 +               if (IS_COPYUP_ERR(err))
4910 +                       goto docopyup;
4911 +               if (!lower_new_dentry || IS_ERR(lower_new_dentry))
4912 +                       goto out;
4913 +       }
4914 +       lower_new_dentry = unionfs_lower_dentry(new_dentry);
4915 +       lower_old_dentry = unionfs_lower_dentry(old_dentry);
4916 +
4917 +       BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
4918 +       lower_dir_dentry = lock_parent(lower_new_dentry);
4919 +       err = is_robranch(old_dentry);
4920 +       if (!err)
4921 +               err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
4922 +                              lower_new_dentry);
4923 +       unlock_dir(lower_dir_dentry);
4924 +
4925 +docopyup:
4926 +       if (IS_COPYUP_ERR(err)) {
4927 +               int old_bstart = dbstart(old_dentry);
4928 +               int bindex;
4929 +
4930 +               for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
4931 +                       err = copyup_dentry(old_dentry->d_parent->d_inode,
4932 +                                           old_dentry, old_bstart,
4933 +                                           bindex, old_dentry->d_name.name,
4934 +                                           old_dentry->d_name.len, NULL,
4935 +                                           old_dentry->d_inode->i_size);
4936 +                       if (!err) {
4937 +                               lower_new_dentry =
4938 +                                       create_parents(dir, new_dentry,
4939 +                                                      new_dentry->d_name.name,
4940 +                                                      bindex);
4941 +                               lower_old_dentry =
4942 +                                       unionfs_lower_dentry(old_dentry);
4943 +                               lower_dir_dentry =
4944 +                                       lock_parent(lower_new_dentry);
4945 +                               /* do vfs_link */
4946 +                               err = vfs_link(lower_old_dentry,
4947 +                                              lower_dir_dentry->d_inode,
4948 +                                              lower_new_dentry);
4949 +                               unlock_dir(lower_dir_dentry);
4950 +                               goto check_link;
4951 +                       }
4952 +               }
4953 +               goto out;
4954 +       }
4955 +
4956 +check_link:
4957 +       if (err || !lower_new_dentry->d_inode)
4958 +               goto out;
4959 +
4960 +       /* Its a hard link, so use the same inode */
4961 +       new_dentry->d_inode = igrab(old_dentry->d_inode);
4962 +       d_instantiate(new_dentry, new_dentry->d_inode);
4963 +       unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
4964 +       fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
4965 +
4966 +       /* propagate number of hard-links */
4967 +       old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
4968 +       /* new dentry's ctime may have changed due to hard-link counts */
4969 +       unionfs_copy_attr_times(new_dentry->d_inode);
4970 +
4971 +out:
4972 +       if (!new_dentry->d_inode)
4973 +               d_drop(new_dentry);
4974 +
4975 +       kfree(name);
4976 +       if (!err)
4977 +               unionfs_postcopyup_setmnt(new_dentry);
4978 +
4979 +       unionfs_unlock_dentry(new_dentry);
4980 +       unionfs_unlock_dentry(old_dentry);
4981 +
4982 +       unionfs_check_inode(dir);
4983 +       unionfs_check_dentry(new_dentry);
4984 +       unionfs_check_dentry(old_dentry);
4985 +       unionfs_read_unlock(old_dentry->d_sb);
4986 +
4987 +       return err;
4988 +}
4989 +
4990 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
4991 +                          const char *symname)
4992 +{
4993 +       int err = 0;
4994 +       struct dentry *lower_dentry = NULL;
4995 +       struct dentry *whiteout_dentry = NULL;
4996 +       struct dentry *lower_dir_dentry = NULL;
4997 +       umode_t mode;
4998 +       int bindex = 0, bstart;
4999 +       char *name = NULL;
5000 +
5001 +       unionfs_read_lock(dentry->d_sb);
5002 +       unionfs_lock_dentry(dentry);
5003 +
5004 +       if (unlikely(dentry->d_inode &&
5005 +                    !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5006 +               err = -ESTALE;
5007 +               goto out;
5008 +       }
5009 +
5010 +       /* We start out in the leftmost branch. */
5011 +       bstart = dbstart(dentry);
5012 +
5013 +       lower_dentry = unionfs_lower_dentry(dentry);
5014 +
5015 +       /*
5016 +        * check if whiteout exists in this branch, i.e. lookup .wh.foo
5017 +        * first. If present, delete it
5018 +        */
5019 +       name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
5020 +       if (unlikely(IS_ERR(name))) {
5021 +               err = PTR_ERR(name);
5022 +               goto out;
5023 +       }
5024 +
5025 +       whiteout_dentry =
5026 +               lookup_one_len(name, lower_dentry->d_parent,
5027 +                              dentry->d_name.len + UNIONFS_WHLEN);
5028 +       if (IS_ERR(whiteout_dentry)) {
5029 +               err = PTR_ERR(whiteout_dentry);
5030 +               goto out;
5031 +       }
5032 +
5033 +       if (!whiteout_dentry->d_inode) {
5034 +               dput(whiteout_dentry);
5035 +               whiteout_dentry = NULL;
5036 +       } else {
5037 +               /*
5038 +                * found a .wh.foo entry, unlink it and then call
5039 +                * vfs_symlink().
5040 +                */
5041 +               lower_dir_dentry = lock_parent(whiteout_dentry);
5042 +
5043 +               err = is_robranch_super(dentry->d_sb, bstart);
5044 +               if (!err)
5045 +                       err = vfs_unlink(lower_dir_dentry->d_inode,
5046 +                                        whiteout_dentry);
5047 +               dput(whiteout_dentry);
5048 +
5049 +               fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5050 +               /* propagate number of hard-links */
5051 +               dir->i_nlink = unionfs_get_nlinks(dir);
5052 +
5053 +               unlock_dir(lower_dir_dentry);
5054 +
5055 +               if (err) {
5056 +                       /* exit if the error returned was NOT -EROFS */
5057 +                       if (!IS_COPYUP_ERR(err))
5058 +                               goto out;
5059 +                       /*
5060 +                        * should now try to create symlink in the another
5061 +                        * branch.
5062 +                        */
5063 +                       bstart--;
5064 +               }
5065 +       }
5066 +
5067 +       /*
5068 +        * deleted whiteout if it was present, now do a normal vfs_symlink()
5069 +        * with possible recursive directory creation
5070 +        */
5071 +       for (bindex = bstart; bindex >= 0; bindex--) {
5072 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5073 +               if (!lower_dentry) {
5074 +                       /*
5075 +                        * if lower_dentry is NULL, create the entire
5076 +                        * dentry directory structure in branch 'bindex'.
5077 +                        * lower_dentry will NOT be null when bindex ==
5078 +                        * bstart because lookup passed as a negative
5079 +                        * unionfs dentry pointing to a lone negative
5080 +                        * underlying dentry
5081 +                        */
5082 +                       lower_dentry = create_parents(dir, dentry,
5083 +                                                     dentry->d_name.name,
5084 +                                                     bindex);
5085 +                       if (!lower_dentry || IS_ERR(lower_dentry)) {
5086 +                               if (IS_ERR(lower_dentry))
5087 +                                       err = PTR_ERR(lower_dentry);
5088 +
5089 +                               printk(KERN_ERR "unionfs: lower dentry "
5090 +                                      "NULL (or error) for bindex = %d\n",
5091 +                                      bindex);
5092 +                               continue;
5093 +                       }
5094 +               }
5095 +
5096 +               lower_dir_dentry = lock_parent(lower_dentry);
5097 +
5098 +               err = is_robranch_super(dentry->d_sb, bindex);
5099 +               if (!err) {
5100 +                       mode = S_IALLUGO;
5101 +                       err = vfs_symlink(lower_dir_dentry->d_inode,
5102 +                                         lower_dentry, symname, mode);
5103 +               }
5104 +               unlock_dir(lower_dir_dentry);
5105 +
5106 +               if (err || !lower_dentry->d_inode) {
5107 +                       /*
5108 +                        * break out of for loop if error returned was NOT
5109 +                        * -EROFS.
5110 +                        */
5111 +                       if (!IS_COPYUP_ERR(err))
5112 +                               break;
5113 +               } else {
5114 +                       /*
5115 +                        * Only INTERPOSE_LOOKUP can return a value other
5116 +                        * than 0 on err.
5117 +                        */
5118 +                       err = PTR_ERR(unionfs_interpose(dentry,
5119 +                                                       dir->i_sb, 0));
5120 +                       if (!err) {
5121 +                               fsstack_copy_attr_times(dir,
5122 +                                                       lower_dir_dentry->
5123 +                                                       d_inode);
5124 +                               fsstack_copy_inode_size(dir,
5125 +                                                       lower_dir_dentry->
5126 +                                                       d_inode);
5127 +                               /*
5128 +                                * update number of links on parent
5129 +                                * directory.
5130 +                                */
5131 +                               dir->i_nlink = unionfs_get_nlinks(dir);
5132 +                       }
5133 +                       break;
5134 +               }
5135 +       }
5136 +
5137 +out:
5138 +       if (!dentry->d_inode)
5139 +               d_drop(dentry);
5140 +
5141 +       kfree(name);
5142 +       if (!err)
5143 +               unionfs_postcopyup_setmnt(dentry);
5144 +       unionfs_unlock_dentry(dentry);
5145 +
5146 +       unionfs_check_inode(dir);
5147 +       unionfs_check_dentry(dentry);
5148 +       unionfs_read_unlock(dentry->d_sb);
5149 +
5150 +       return err;
5151 +}
5152 +
5153 +static int unionfs_mkdir(struct inode *parent, struct dentry *dentry, int mode)
5154 +{
5155 +       int err = 0;
5156 +       struct dentry *lower_dentry = NULL, *whiteout_dentry = NULL;
5157 +       struct dentry *lower_parent_dentry = NULL;
5158 +       int bindex = 0, bstart;
5159 +       char *name = NULL;
5160 +       int whiteout_unlinked = 0;
5161 +       struct sioq_args args;
5162 +
5163 +       unionfs_read_lock(dentry->d_sb);
5164 +       unionfs_lock_dentry(dentry);
5165 +
5166 +       if (unlikely(dentry->d_inode &&
5167 +                    !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5168 +               err = -ESTALE;
5169 +               goto out;
5170 +       }
5171 +
5172 +       bstart = dbstart(dentry);
5173 +
5174 +       lower_dentry = unionfs_lower_dentry(dentry);
5175 +
5176 +       /*
5177 +        * check if whiteout exists in this branch, i.e. lookup .wh.foo
5178 +        * first.
5179 +        */
5180 +       name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
5181 +       if (unlikely(IS_ERR(name))) {
5182 +               err = PTR_ERR(name);
5183 +               goto out;
5184 +       }
5185 +
5186 +       whiteout_dentry = lookup_one_len(name, lower_dentry->d_parent,
5187 +                                        dentry->d_name.len + UNIONFS_WHLEN);
5188 +       if (IS_ERR(whiteout_dentry)) {
5189 +               err = PTR_ERR(whiteout_dentry);
5190 +               goto out;
5191 +       }
5192 +
5193 +       if (!whiteout_dentry->d_inode) {
5194 +               dput(whiteout_dentry);
5195 +               whiteout_dentry = NULL;
5196 +       } else {
5197 +               lower_parent_dentry = lock_parent(whiteout_dentry);
5198 +
5199 +               /* found a.wh.foo entry, remove it then do vfs_mkdir */
5200 +               err = is_robranch_super(dentry->d_sb, bstart);
5201 +               if (!err) {
5202 +                       args.unlink.parent = lower_parent_dentry->d_inode;
5203 +                       args.unlink.dentry = whiteout_dentry;
5204 +                       run_sioq(__unionfs_unlink, &args);
5205 +                       err = args.err;
5206 +               }
5207 +               dput(whiteout_dentry);
5208 +
5209 +               unlock_dir(lower_parent_dentry);
5210 +
5211 +               if (err) {
5212 +                       /* exit if the error returned was NOT -EROFS */
5213 +                       if (!IS_COPYUP_ERR(err))
5214 +                               goto out;
5215 +                       bstart--;
5216 +               } else {
5217 +                       whiteout_unlinked = 1;
5218 +               }
5219 +       }
5220 +
5221 +       for (bindex = bstart; bindex >= 0; bindex--) {
5222 +               int i;
5223 +               int bend = dbend(dentry);
5224 +
5225 +               if (is_robranch_super(dentry->d_sb, bindex))
5226 +                       continue;
5227 +
5228 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5229 +               if (!lower_dentry) {
5230 +                       lower_dentry = create_parents(parent, dentry,
5231 +                                                     dentry->d_name.name,
5232 +                                                     bindex);
5233 +                       if (!lower_dentry || IS_ERR(lower_dentry)) {
5234 +                               printk(KERN_ERR "unionfs: lower dentry "
5235 +                                      " NULL for bindex = %d\n", bindex);
5236 +                               continue;
5237 +                       }
5238 +               }
5239 +
5240 +               lower_parent_dentry = lock_parent(lower_dentry);
5241 +
5242 +               if (IS_ERR(lower_parent_dentry)) {
5243 +                       err = PTR_ERR(lower_parent_dentry);
5244 +                       goto out;
5245 +               }
5246 +
5247 +               err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5248 +                               mode);
5249 +
5250 +               unlock_dir(lower_parent_dentry);
5251 +
5252 +               /* did the mkdir succeed? */
5253 +               if (err)
5254 +                       break;
5255 +
5256 +               for (i = bindex + 1; i < bend; i++) {
5257 +                       if (unionfs_lower_dentry_idx(dentry, i)) {
5258 +                               dput(unionfs_lower_dentry_idx(dentry, i));
5259 +                               unionfs_set_lower_dentry_idx(dentry, i, NULL);
5260 +                       }
5261 +               }
5262 +               set_dbend(dentry, bindex);
5263 +
5264 +               /*
5265 +                * Only INTERPOSE_LOOKUP can return a value other than 0 on
5266 +                * err.
5267 +                */
5268 +               err = PTR_ERR(unionfs_interpose(dentry, parent->i_sb, 0));
5269 +               if (!err) {
5270 +                       unionfs_copy_attr_times(parent);
5271 +                       fsstack_copy_inode_size(parent,
5272 +                                               lower_parent_dentry->d_inode);
5273 +
5274 +                       /* update number of links on parent directory */
5275 +                       parent->i_nlink = unionfs_get_nlinks(parent);
5276 +               }
5277 +
5278 +               err = make_dir_opaque(dentry, dbstart(dentry));
5279 +               if (err) {
5280 +                       printk(KERN_ERR "unionfs: mkdir: error creating "
5281 +                              ".wh.__dir_opaque: %d\n", err);
5282 +                       goto out;
5283 +               }
5284 +
5285 +               /* we are done! */
5286 +               break;
5287 +       }
5288 +
5289 +out:
5290 +       if (!dentry->d_inode)
5291 +               d_drop(dentry);
5292 +
5293 +       kfree(name);
5294 +
5295 +       if (!err)
5296 +               unionfs_copy_attr_times(dentry->d_inode);
5297 +       unionfs_unlock_dentry(dentry);
5298 +       unionfs_check_inode(parent);
5299 +       unionfs_check_dentry(dentry);
5300 +       unionfs_read_unlock(dentry->d_sb);
5301 +
5302 +       return err;
5303 +}
5304 +
5305 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5306 +                        dev_t dev)
5307 +{
5308 +       int err = 0;
5309 +       struct dentry *lower_dentry = NULL, *whiteout_dentry = NULL;
5310 +       struct dentry *lower_parent_dentry = NULL;
5311 +       int bindex = 0, bstart;
5312 +       char *name = NULL;
5313 +       int whiteout_unlinked = 0;
5314 +
5315 +       unionfs_read_lock(dentry->d_sb);
5316 +       unionfs_lock_dentry(dentry);
5317 +
5318 +       if (unlikely(dentry->d_inode &&
5319 +                    !__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5320 +               err = -ESTALE;
5321 +               goto out;
5322 +       }
5323 +
5324 +       bstart = dbstart(dentry);
5325 +
5326 +       lower_dentry = unionfs_lower_dentry(dentry);
5327 +
5328 +       /*
5329 +        * check if whiteout exists in this branch, i.e. lookup .wh.foo
5330 +        * first.
5331 +        */
5332 +       name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
5333 +       if (unlikely(IS_ERR(name))) {
5334 +               err = PTR_ERR(name);
5335 +               goto out;
5336 +       }
5337 +
5338 +       whiteout_dentry = lookup_one_len(name, lower_dentry->d_parent,
5339 +                                        dentry->d_name.len + UNIONFS_WHLEN);
5340 +       if (IS_ERR(whiteout_dentry)) {
5341 +               err = PTR_ERR(whiteout_dentry);
5342 +               goto out;
5343 +       }
5344 +
5345 +       if (!whiteout_dentry->d_inode) {
5346 +               dput(whiteout_dentry);
5347 +               whiteout_dentry = NULL;
5348 +       } else {
5349 +               /* found .wh.foo, unlink it */
5350 +               lower_parent_dentry = lock_parent(whiteout_dentry);
5351 +
5352 +               /* found a.wh.foo entry, remove it then do vfs_mkdir */
5353 +               err = is_robranch_super(dentry->d_sb, bstart);
5354 +               if (!err)
5355 +                       err = vfs_unlink(lower_parent_dentry->d_inode,
5356 +                                        whiteout_dentry);
5357 +               dput(whiteout_dentry);
5358 +
5359 +               unlock_dir(lower_parent_dentry);
5360 +
5361 +               if (err) {
5362 +                       if (!IS_COPYUP_ERR(err))
5363 +                               goto out;
5364 +                       bstart--;
5365 +               } else {
5366 +                       whiteout_unlinked = 1;
5367 +               }
5368 +       }
5369 +
5370 +       for (bindex = bstart; bindex >= 0; bindex--) {
5371 +               if (is_robranch_super(dentry->d_sb, bindex))
5372 +                       continue;
5373 +
5374 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5375 +               if (!lower_dentry) {
5376 +                       lower_dentry = create_parents(dir, dentry,
5377 +                                                     dentry->d_name.name,
5378 +                                                     bindex);
5379 +                       if (IS_ERR(lower_dentry)) {
5380 +                               printk(KERN_ERR "unionfs: failed to create "
5381 +                                      "parents on %d, err = %ld\n",
5382 +                                      bindex, PTR_ERR(lower_dentry));
5383 +                               continue;
5384 +                       }
5385 +               }
5386 +
5387 +               lower_parent_dentry = lock_parent(lower_dentry);
5388 +               if (IS_ERR(lower_parent_dentry)) {
5389 +                       err = PTR_ERR(lower_parent_dentry);
5390 +                       goto out;
5391 +               }
5392 +
5393 +               err = vfs_mknod(lower_parent_dentry->d_inode,
5394 +                               lower_dentry, mode, dev);
5395 +
5396 +               if (err) {
5397 +                       unlock_dir(lower_parent_dentry);
5398 +                       break;
5399 +               }
5400 +
5401 +               /*
5402 +                * Only INTERPOSE_LOOKUP can return a value other than 0 on
5403 +                * err.
5404 +                */
5405 +               err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5406 +               if (!err) {
5407 +                       fsstack_copy_attr_times(dir,
5408 +                                               lower_parent_dentry->d_inode);
5409 +                       fsstack_copy_inode_size(dir,
5410 +                                               lower_parent_dentry->d_inode);
5411 +                       /* update number of links on parent directory */
5412 +                       dir->i_nlink = unionfs_get_nlinks(dir);
5413 +               }
5414 +               unlock_dir(lower_parent_dentry);
5415 +
5416 +               break;
5417 +       }
5418 +
5419 +out:
5420 +       if (!dentry->d_inode)
5421 +               d_drop(dentry);
5422 +
5423 +       kfree(name);
5424 +
5425 +       if (!err)
5426 +               unionfs_postcopyup_setmnt(dentry);
5427 +       unionfs_unlock_dentry(dentry);
5428 +
5429 +       unionfs_check_inode(dir);
5430 +       unionfs_check_dentry(dentry);
5431 +       unionfs_read_unlock(dentry->d_sb);
5432 +
5433 +       return err;
5434 +}
5435 +
5436 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5437 +                           int bufsiz)
5438 +{
5439 +       int err;
5440 +       struct dentry *lower_dentry;
5441 +
5442 +       unionfs_read_lock(dentry->d_sb);
5443 +       unionfs_lock_dentry(dentry);
5444 +
5445 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5446 +               err = -ESTALE;
5447 +               goto out;
5448 +       }
5449 +
5450 +       lower_dentry = unionfs_lower_dentry(dentry);
5451 +
5452 +       if (!lower_dentry->d_inode->i_op ||
5453 +           !lower_dentry->d_inode->i_op->readlink) {
5454 +               err = -EINVAL;
5455 +               goto out;
5456 +       }
5457 +
5458 +       err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5459 +                                                   buf, bufsiz);
5460 +       if (err > 0)
5461 +               fsstack_copy_attr_atime(dentry->d_inode,
5462 +                                       lower_dentry->d_inode);
5463 +
5464 +out:
5465 +       unionfs_unlock_dentry(dentry);
5466 +       unionfs_check_dentry(dentry);
5467 +       unionfs_read_unlock(dentry->d_sb);
5468 +
5469 +       return err;
5470 +}
5471 +
5472 +/*
5473 + * unionfs_follow_link takes a dentry, but it is simple.  It only needs to
5474 + * allocate some memory and then call our ->readlink method.  Our
5475 + * unionfs_readlink *does* lock our dentry and revalidate the dentry.
5476 + * Therefore, we do not have to lock our dentry here, to prevent a deadlock;
5477 + * nor do we need to revalidate it either.  It is safe to not lock our
5478 + * dentry here, nor revalidate it, because unionfs_follow_link does not do
5479 + * anything (prior to calling ->readlink) which could become inconsistent
5480 + * due to branch management.
5481 + */
5482 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5483 +{
5484 +       char *buf;
5485 +       int len = PAGE_SIZE, err;
5486 +       mm_segment_t old_fs;
5487 +
5488 +       unionfs_read_lock(dentry->d_sb);
5489 +
5490 +       /* This is freed by the put_link method assuming a successful call. */
5491 +       buf = kmalloc(len, GFP_KERNEL);
5492 +       if (unlikely(!buf)) {
5493 +               err = -ENOMEM;
5494 +               goto out;
5495 +       }
5496 +
5497 +       /* read the symlink, and then we will follow it */
5498 +       old_fs = get_fs();
5499 +       set_fs(KERNEL_DS);
5500 +       err = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
5501 +       set_fs(old_fs);
5502 +       if (err < 0) {
5503 +               kfree(buf);
5504 +               buf = NULL;
5505 +               goto out;
5506 +       }
5507 +       buf[err] = 0;
5508 +       nd_set_link(nd, buf);
5509 +       err = 0;
5510 +
5511 +out:
5512 +       unionfs_check_dentry(dentry);
5513 +       unionfs_check_nd(nd);
5514 +       unionfs_read_unlock(dentry->d_sb);
5515 +       return ERR_PTR(err);
5516 +}
5517 +
5518 +/* FIXME: We may not have to lock here */
5519 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5520 +                            void *cookie)
5521 +{
5522 +       unionfs_read_lock(dentry->d_sb);
5523 +
5524 +       unionfs_lock_dentry(dentry);
5525 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, nd, false)))
5526 +               printk(KERN_ERR
5527 +                      "unionfs: put_link failed to revalidate dentry\n");
5528 +       unionfs_unlock_dentry(dentry);
5529 +
5530 +       unionfs_check_dentry(dentry);
5531 +       unionfs_check_nd(nd);
5532 +       kfree(nd_get_link(nd));
5533 +       unionfs_read_unlock(dentry->d_sb);
5534 +}
5535 +
5536 +/*
5537 + * Basically copied from the kernel vfs permission(), but we've changed
5538 + * the following:
5539 + *   (1) the IS_RDONLY check is skipped, and
5540 + *   (2) We return 0 (success) if the non-leftmost branch is mounted
5541 + *       readonly, to allow copyup to work.
5542 + *   (3) we do call security_inode_permission, and therefore security inside
5543 + *       SELinux, etc. are performed.
5544 + *
5545 + * @inode: the lower inode we're checking permission on
5546 + */
5547 +static int inode_permission(struct super_block *sb, struct inode *inode,
5548 +                           int mask, struct nameidata *nd, int bindex)
5549 +{
5550 +       int retval, submask;
5551 +
5552 +       if (mask & MAY_WRITE) {
5553 +               umode_t mode = inode->i_mode;
5554 +               /* The first branch is allowed to be really readonly. */
5555 +               if (bindex == 0 &&
5556 +                   IS_RDONLY(inode) &&
5557 +                   (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5558 +                       return -EROFS;
5559 +               /*
5560 +                * Nobody gets write access to an immutable file.
5561 +                */
5562 +               if (IS_IMMUTABLE(inode))
5563 +                       return -EACCES;
5564 +               /*
5565 +                * For all other branches than the first one, we ignore
5566 +                * EROFS or if the branch is mounted as readonly, to let
5567 +                * copyup take place.
5568 +                */
5569 +               if (bindex > 0 &&
5570 +                   is_robranch_super(sb, bindex) &&
5571 +                   (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5572 +                       return 0;
5573 +       }
5574 +
5575 +       /* Ordinary permission routines do not understand MAY_APPEND. */
5576 +       submask = mask & ~MAY_APPEND;
5577 +       if (inode->i_op && inode->i_op->permission)
5578 +               retval = inode->i_op->permission(inode, submask, nd);
5579 +       else
5580 +               retval = generic_permission(inode, submask, NULL);
5581 +
5582 +       if (retval && retval != -EROFS) /* ignore EROFS */
5583 +               return retval;
5584 +
5585 +       retval = security_inode_permission(inode, mask, nd);
5586 +       return ((retval == -EROFS) ? 0 : retval);       /* ignore EROFS */
5587 +}
5588 +
5589 +/*
5590 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5591 + * a deadlock with the branch-management "add branch" code (which grabbed
5592 + * the write lock).  It is safe to not grab the read lock here, because even
5593 + * with branch management taking place, there is no chance that
5594 + * unionfs_permission, or anything it calls, will use stale branch
5595 + * information.
5596 + */
5597 +static int unionfs_permission(struct inode *inode, int mask,
5598 +                             struct nameidata *nd)
5599 +{
5600 +       struct inode *lower_inode = NULL;
5601 +       int err = 0;
5602 +       int bindex, bstart, bend;
5603 +       const int is_file = !S_ISDIR(inode->i_mode);
5604 +       const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5605 +
5606 +       bstart = ibstart(inode);
5607 +       bend = ibend(inode);
5608 +       if (unlikely(bstart < 0 || bend < 0)) {
5609 +               /*
5610 +                * With branch-management, we can get a stale inode here.
5611 +                * If so, we return ESTALE back to link_path_walk, which
5612 +                * would discard the dcache entry and re-lookup the
5613 +                * dentry+inode.  This should be equivalent to issuing
5614 +                * __unionfs_d_revalidate_chain on nd.dentry here.
5615 +                */
5616 +               err = -ESTALE;  /* force revalidate */
5617 +               goto out;
5618 +       }
5619 +
5620 +       for (bindex = bstart; bindex <= bend; bindex++) {
5621 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
5622 +               if (!lower_inode)
5623 +                       continue;
5624 +
5625 +               /*
5626 +                * check the condition for D-F-D underlying files/directories,
5627 +                * we don't have to check for files, if we are checking for
5628 +                * directories.
5629 +                */
5630 +               if (!is_file && !S_ISDIR(lower_inode->i_mode))
5631 +                       continue;
5632 +
5633 +               /*
5634 +                * We use our own special version of permission, such that
5635 +                * only the first branch returns -EROFS.
5636 +                */
5637 +               err = inode_permission(inode->i_sb, lower_inode, mask, nd,
5638 +                                      bindex);
5639 +
5640 +               /*
5641 +                * The permissions are an intersection of the overall directory
5642 +                * permissions, so we fail if one fails.
5643 +                */
5644 +               if (err)
5645 +                       goto out;
5646 +
5647 +               /* only the leftmost file matters. */
5648 +               if (is_file || write_mask) {
5649 +                       if (is_file && write_mask) {
5650 +                               err = get_write_access(lower_inode);
5651 +                               if (!err)
5652 +                                       put_write_access(lower_inode);
5653 +                       }
5654 +                       break;
5655 +               }
5656 +       }
5657 +       /* sync times which may have changed (asynchronously) below */
5658 +       unionfs_copy_attr_times(inode);
5659 +
5660 +out:
5661 +       unionfs_check_inode(inode);
5662 +       unionfs_check_nd(nd);
5663 +       return err;
5664 +}
5665 +
5666 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5667 +{
5668 +       int err = 0;
5669 +       struct dentry *lower_dentry;
5670 +       struct inode *inode = NULL;
5671 +       struct inode *lower_inode = NULL;
5672 +       int bstart, bend, bindex;
5673 +       int i;
5674 +       int copyup = 0;
5675 +
5676 +       unionfs_read_lock(dentry->d_sb);
5677 +       unionfs_lock_dentry(dentry);
5678 +
5679 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
5680 +               err = -ESTALE;
5681 +               goto out;
5682 +       }
5683 +
5684 +       bstart = dbstart(dentry);
5685 +       bend = dbend(dentry);
5686 +       inode = dentry->d_inode;
5687 +
5688 +       for (bindex = bstart; (bindex <= bend) || (bindex == bstart);
5689 +            bindex++) {
5690 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5691 +               if (!lower_dentry)
5692 +                       continue;
5693 +               BUG_ON(lower_dentry->d_inode == NULL);
5694 +
5695 +               /* If the file is on a read only branch */
5696 +               if (is_robranch_super(dentry->d_sb, bindex)
5697 +                   || IS_RDONLY(lower_dentry->d_inode)) {
5698 +                       if (copyup || (bindex != bstart))
5699 +                               continue;
5700 +                       /* Only if its the leftmost file, copyup the file */
5701 +                       for (i = bstart - 1; i >= 0; i--) {
5702 +                               loff_t size = dentry->d_inode->i_size;
5703 +                               if (ia->ia_valid & ATTR_SIZE)
5704 +                                       size = ia->ia_size;
5705 +                               err = copyup_dentry(dentry->d_parent->d_inode,
5706 +                                                   dentry, bstart, i,
5707 +                                                   dentry->d_name.name,
5708 +                                                   dentry->d_name.len,
5709 +                                                   NULL, size);
5710 +
5711 +                               if (!err) {
5712 +                                       copyup = 1;
5713 +                                       lower_dentry =
5714 +                                               unionfs_lower_dentry(dentry);
5715 +                                       break;
5716 +                               }
5717 +                               /*
5718 +                                * if error is in the leftmost branch, pass
5719 +                                * it up.
5720 +                                */
5721 +                               if (i == 0)
5722 +                                       goto out;
5723 +                       }
5724 +
5725 +               }
5726 +               err = notify_change(lower_dentry, ia);
5727 +               if (err)
5728 +                       goto out;
5729 +               break;
5730 +       }
5731 +
5732 +       /* for mmap */
5733 +       if (ia->ia_valid & ATTR_SIZE) {
5734 +               if (ia->ia_size != i_size_read(inode)) {
5735 +                       err = vmtruncate(inode, ia->ia_size);
5736 +                       if (err)
5737 +                               printk(KERN_ERR
5738 +                                      "unionfs: setattr: vmtruncate failed\n");
5739 +               }
5740 +       }
5741 +
5742 +       /* get the size from the first lower inode */
5743 +       lower_inode = unionfs_lower_inode(inode);
5744 +       unionfs_copy_attr_all(inode, lower_inode);
5745 +       fsstack_copy_inode_size(inode, lower_inode);
5746 +       /* if setattr succeeded, then parent dir may have changed */
5747 +       unionfs_copy_attr_times(dentry->d_parent->d_inode);
5748 +out:
5749 +       unionfs_unlock_dentry(dentry);
5750 +       unionfs_check_dentry(dentry);
5751 +       unionfs_check_dentry(dentry->d_parent);
5752 +       unionfs_read_unlock(dentry->d_sb);
5753 +
5754 +       return err;
5755 +}
5756 +
5757 +struct inode_operations unionfs_symlink_iops = {
5758 +       .readlink       = unionfs_readlink,
5759 +       .permission     = unionfs_permission,
5760 +       .follow_link    = unionfs_follow_link,
5761 +       .setattr        = unionfs_setattr,
5762 +       .put_link       = unionfs_put_link,
5763 +};
5764 +
5765 +struct inode_operations unionfs_dir_iops = {
5766 +       .create         = unionfs_create,
5767 +       .lookup         = unionfs_lookup,
5768 +       .link           = unionfs_link,
5769 +       .unlink         = unionfs_unlink,
5770 +       .symlink        = unionfs_symlink,
5771 +       .mkdir          = unionfs_mkdir,
5772 +       .rmdir          = unionfs_rmdir,
5773 +       .mknod          = unionfs_mknod,
5774 +       .rename         = unionfs_rename,
5775 +       .permission     = unionfs_permission,
5776 +       .setattr        = unionfs_setattr,
5777 +#ifdef CONFIG_UNION_FS_XATTR
5778 +       .setxattr       = unionfs_setxattr,
5779 +       .getxattr       = unionfs_getxattr,
5780 +       .removexattr    = unionfs_removexattr,
5781 +       .listxattr      = unionfs_listxattr,
5782 +#endif /* CONFIG_UNION_FS_XATTR */
5783 +};
5784 +
5785 +struct inode_operations unionfs_main_iops = {
5786 +       .permission     = unionfs_permission,
5787 +       .setattr        = unionfs_setattr,
5788 +#ifdef CONFIG_UNION_FS_XATTR
5789 +       .setxattr       = unionfs_setxattr,
5790 +       .getxattr       = unionfs_getxattr,
5791 +       .removexattr    = unionfs_removexattr,
5792 +       .listxattr      = unionfs_listxattr,
5793 +#endif /* CONFIG_UNION_FS_XATTR */
5794 +};
5795 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5796 new file mode 100644
5797 index 0000000..a1904c9
5798 --- /dev/null
5799 +++ b/fs/unionfs/lookup.c
5800 @@ -0,0 +1,652 @@
5801 +/*
5802 + * Copyright (c) 2003-2007 Erez Zadok
5803 + * Copyright (c) 2003-2006 Charles P. Wright
5804 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5805 + * Copyright (c) 2005-2006 Junjiro Okajima
5806 + * Copyright (c) 2005      Arun M. Krishnakumar
5807 + * Copyright (c) 2004-2006 David P. Quigley
5808 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5809 + * Copyright (c) 2003      Puja Gupta
5810 + * Copyright (c) 2003      Harikesavan Krishnan
5811 + * Copyright (c) 2003-2007 Stony Brook University
5812 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
5813 + *
5814 + * This program is free software; you can redistribute it and/or modify
5815 + * it under the terms of the GNU General Public License version 2 as
5816 + * published by the Free Software Foundation.
5817 + */
5818 +
5819 +#include "union.h"
5820 +
5821 +static int realloc_dentry_private_data(struct dentry *dentry);
5822 +
5823 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
5824 +static int is_validname(const char *name)
5825 +{
5826 +       if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
5827 +               return 0;
5828 +       if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
5829 +                    sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
5830 +               return 0;
5831 +       return 1;
5832 +}
5833 +
5834 +/* The rest of these are utility functions for lookup. */
5835 +static noinline int is_opaque_dir(struct dentry *dentry, int bindex)
5836 +{
5837 +       int err = 0;
5838 +       struct dentry *lower_dentry;
5839 +       struct dentry *wh_lower_dentry;
5840 +       struct inode *lower_inode;
5841 +       struct sioq_args args;
5842 +
5843 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5844 +       lower_inode = lower_dentry->d_inode;
5845 +
5846 +       BUG_ON(!S_ISDIR(lower_inode->i_mode));
5847 +
5848 +       mutex_lock(&lower_inode->i_mutex);
5849 +
5850 +       if (!permission(lower_inode, MAY_EXEC, NULL)) {
5851 +               wh_lower_dentry =
5852 +                       lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
5853 +                                      sizeof(UNIONFS_DIR_OPAQUE) - 1);
5854 +       } else {
5855 +               args.is_opaque.dentry = lower_dentry;
5856 +               run_sioq(__is_opaque_dir, &args);
5857 +               wh_lower_dentry = args.ret;
5858 +       }
5859 +
5860 +       mutex_unlock(&lower_inode->i_mutex);
5861 +
5862 +       if (IS_ERR(wh_lower_dentry)) {
5863 +               err = PTR_ERR(wh_lower_dentry);
5864 +               goto out;
5865 +       }
5866 +
5867 +       /* This is an opaque dir iff wh_lower_dentry is positive */
5868 +       err = !!wh_lower_dentry->d_inode;
5869 +
5870 +       dput(wh_lower_dentry);
5871 +out:
5872 +       return err;
5873 +}
5874 +
5875 +/*
5876 + * Main (and complex) driver function for Unionfs's lookup
5877 + *
5878 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
5879 + * PTR if d_splice returned a different dentry.
5880 + *
5881 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
5882 + * inode info must be locked.  If lookupmode is INTERPOSE_LOOKUP (i.e., a
5883 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
5884 + * dentry's info, which the caller must unlock.
5885 + */
5886 +struct dentry *unionfs_lookup_backend(struct dentry *dentry,
5887 +                                     struct nameidata *nd, int lookupmode)
5888 +{
5889 +       int err = 0;
5890 +       struct dentry *lower_dentry = NULL;
5891 +       struct dentry *wh_lower_dentry = NULL;
5892 +       struct dentry *lower_dir_dentry = NULL;
5893 +       struct dentry *parent_dentry = NULL;
5894 +       struct dentry *d_interposed = NULL;
5895 +       int bindex, bstart = -1, bend, bopaque;
5896 +       int dentry_count = 0;   /* Number of positive dentries. */
5897 +       int first_dentry_offset = -1; /* -1 is uninitialized */
5898 +       struct dentry *first_dentry = NULL;
5899 +       struct dentry *first_lower_dentry = NULL;
5900 +       struct vfsmount *first_lower_mnt = NULL;
5901 +       int locked_parent = 0;
5902 +       int opaque;
5903 +       char *whname = NULL;
5904 +       const char *name;
5905 +       int namelen;
5906 +
5907 +       /*
5908 +        * We should already have a lock on this dentry in the case of a
5909 +        * partial lookup, or a revalidation. Otherwise it is returned from
5910 +        * new_dentry_private_data already locked.
5911 +        */
5912 +       if (lookupmode == INTERPOSE_PARTIAL || lookupmode == INTERPOSE_REVAL ||
5913 +           lookupmode == INTERPOSE_REVAL_NEG)
5914 +               verify_locked(dentry);
5915 +       else                    /* this could only be INTERPOSE_LOOKUP */
5916 +               BUG_ON(UNIONFS_D(dentry) != NULL);
5917 +
5918 +       switch (lookupmode) {
5919 +       case INTERPOSE_PARTIAL:
5920 +               break;
5921 +       case INTERPOSE_LOOKUP:
5922 +               err = new_dentry_private_data(dentry);
5923 +               if (unlikely(err))
5924 +                       goto out;
5925 +               break;
5926 +       default:
5927 +               /* default: can only be INTERPOSE_REVAL/REVAL_NEG */
5928 +               err = realloc_dentry_private_data(dentry);
5929 +               if (unlikely(err))
5930 +                       goto out;
5931 +               break;
5932 +       }
5933 +
5934 +       /* must initialize dentry operations */
5935 +       dentry->d_op = &unionfs_dops;
5936 +
5937 +       parent_dentry = dget_parent(dentry);
5938 +       /* We never partial lookup the root directory. */
5939 +       if (parent_dentry != dentry) {
5940 +               unionfs_lock_dentry(parent_dentry);
5941 +               locked_parent = 1;
5942 +       } else {
5943 +               dput(parent_dentry);
5944 +               parent_dentry = NULL;
5945 +               goto out;
5946 +       }
5947 +
5948 +       name = dentry->d_name.name;
5949 +       namelen = dentry->d_name.len;
5950 +
5951 +       /* No dentries should get created for possible whiteout names. */
5952 +       if (!is_validname(name)) {
5953 +               err = -EPERM;
5954 +               goto out_free;
5955 +       }
5956 +
5957 +       /* Now start the actual lookup procedure. */
5958 +       bstart = dbstart(parent_dentry);
5959 +       bend = dbend(parent_dentry);
5960 +       bopaque = dbopaque(parent_dentry);
5961 +       BUG_ON(bstart < 0);
5962 +
5963 +       /*
5964 +        * It would be ideal if we could convert partial lookups to only have
5965 +        * to do this work when they really need to.  It could probably improve
5966 +        * performance quite a bit, and maybe simplify the rest of the code.
5967 +        */
5968 +       if (lookupmode == INTERPOSE_PARTIAL) {
5969 +               bstart++;
5970 +               if ((bopaque != -1) && (bopaque < bend))
5971 +                       bend = bopaque;
5972 +       }
5973 +
5974 +       for (bindex = bstart; bindex <= bend; bindex++) {
5975 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5976 +               if (lookupmode == INTERPOSE_PARTIAL && lower_dentry)
5977 +                       continue;
5978 +               BUG_ON(lower_dentry != NULL);
5979 +
5980 +               lower_dir_dentry =
5981 +                       unionfs_lower_dentry_idx(parent_dentry, bindex);
5982 +
5983 +               /* if the parent lower dentry does not exist skip this */
5984 +               if (!(lower_dir_dentry && lower_dir_dentry->d_inode))
5985 +                       continue;
5986 +
5987 +               /* also skip it if the parent isn't a directory. */
5988 +               if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
5989 +                       continue;
5990 +
5991 +               /* Reuse the whiteout name because its value doesn't change. */
5992 +               if (!whname) {
5993 +                       whname = alloc_whname(name, namelen);
5994 +                       if (unlikely(IS_ERR(whname))) {
5995 +                               err = PTR_ERR(whname);
5996 +                               goto out_free;
5997 +                       }
5998 +               }
5999 +
6000 +               /* check if whiteout exists in this branch: lookup .wh.foo */
6001 +               wh_lower_dentry = lookup_one_len(whname, lower_dir_dentry,
6002 +                                                namelen + UNIONFS_WHLEN);
6003 +               if (IS_ERR(wh_lower_dentry)) {
6004 +                       dput(first_lower_dentry);
6005 +                       unionfs_mntput(first_dentry, first_dentry_offset);
6006 +                       err = PTR_ERR(wh_lower_dentry);
6007 +                       goto out_free;
6008 +               }
6009 +
6010 +               if (wh_lower_dentry->d_inode) {
6011 +                       /* We found a whiteout so let's give up. */
6012 +                       if (S_ISREG(wh_lower_dentry->d_inode->i_mode)) {
6013 +                               set_dbend(dentry, bindex);
6014 +                               set_dbopaque(dentry, bindex);
6015 +                               dput(wh_lower_dentry);
6016 +                               break;
6017 +                       }
6018 +                       err = -EIO;
6019 +                       printk(KERN_ERR "unionfs: EIO: invalid whiteout "
6020 +                              "entry type %d\n",
6021 +                              wh_lower_dentry->d_inode->i_mode);
6022 +                       dput(wh_lower_dentry);
6023 +                       dput(first_lower_dentry);
6024 +                       unionfs_mntput(first_dentry, first_dentry_offset);
6025 +                       goto out_free;
6026 +               }
6027 +
6028 +               dput(wh_lower_dentry);
6029 +               wh_lower_dentry = NULL;
6030 +
6031 +               /* Now do regular lookup; lookup foo */
6032 +               lower_dentry = lookup_one_len(name, lower_dir_dentry, namelen);
6033 +               if (IS_ERR(lower_dentry)) {
6034 +                       dput(first_lower_dentry);
6035 +                       unionfs_mntput(first_dentry, first_dentry_offset);
6036 +                       err = PTR_ERR(lower_dentry);
6037 +                       goto out_free;
6038 +               }
6039 +
6040 +               /*
6041 +                * Store the first negative dentry specially, because if they
6042 +                * are all negative we need this for future creates.
6043 +                */
6044 +               if (!lower_dentry->d_inode) {
6045 +                       if (!first_lower_dentry && (dbstart(dentry) == -1)) {
6046 +                               first_lower_dentry = lower_dentry;
6047 +                               /*
6048 +                                * FIXME: following line needs to be changed
6049 +                                * to allow mount-point crossing
6050 +                                */
6051 +                               first_dentry = parent_dentry;
6052 +                               first_lower_mnt =
6053 +                                       unionfs_mntget(parent_dentry, bindex);
6054 +                               first_dentry_offset = bindex;
6055 +                       } else {
6056 +                               dput(lower_dentry);
6057 +                       }
6058 +
6059 +                       continue;
6060 +               }
6061 +
6062 +               /* number of positive dentries */
6063 +               dentry_count++;
6064 +
6065 +               /* store underlying dentry */
6066 +               if (dbstart(dentry) == -1)
6067 +                       set_dbstart(dentry, bindex);
6068 +               unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6069 +               /*
6070 +                * FIXME: the following line needs to get fixed to allow
6071 +                * mount-point crossing
6072 +                */
6073 +               unionfs_set_lower_mnt_idx(dentry, bindex,
6074 +                                         unionfs_mntget(parent_dentry,
6075 +                                                        bindex));
6076 +               set_dbend(dentry, bindex);
6077 +
6078 +               /* update parent directory's atime with the bindex */
6079 +               fsstack_copy_attr_atime(parent_dentry->d_inode,
6080 +                                       lower_dir_dentry->d_inode);
6081 +
6082 +               /* We terminate file lookups here. */
6083 +               if (!S_ISDIR(lower_dentry->d_inode->i_mode)) {
6084 +                       if (lookupmode == INTERPOSE_PARTIAL)
6085 +                               continue;
6086 +                       if (dentry_count == 1)
6087 +                               goto out_positive;
6088 +                       /* This can only happen with mixed D-*-F-* */
6089 +                       BUG_ON(!S_ISDIR(unionfs_lower_dentry(dentry)->
6090 +                                       d_inode->i_mode));
6091 +                       continue;
6092 +               }
6093 +
6094 +               opaque = is_opaque_dir(dentry, bindex);
6095 +               if (opaque < 0) {
6096 +                       dput(first_lower_dentry);
6097 +                       unionfs_mntput(first_dentry, first_dentry_offset);
6098 +                       err = opaque;
6099 +                       goto out_free;
6100 +               } else if (opaque) {
6101 +                       set_dbend(dentry, bindex);
6102 +                       set_dbopaque(dentry, bindex);
6103 +                       break;
6104 +               }
6105 +       }
6106 +
6107 +       if (dentry_count)
6108 +               goto out_positive;
6109 +       else
6110 +               goto out_negative;
6111 +
6112 +out_negative:
6113 +       if (lookupmode == INTERPOSE_PARTIAL)
6114 +               goto out;
6115 +
6116 +       /* If we've only got negative dentries, then use the leftmost one. */
6117 +       if (lookupmode == INTERPOSE_REVAL) {
6118 +               if (dentry->d_inode)
6119 +                       UNIONFS_I(dentry->d_inode)->stale = 1;
6120 +               goto out;
6121 +       }
6122 +       /* This should only happen if we found a whiteout. */
6123 +       if (first_dentry_offset == -1) {
6124 +               first_lower_dentry = lookup_one_len(name, lower_dir_dentry,
6125 +                                                   namelen);
6126 +               first_dentry_offset = bindex;
6127 +               if (IS_ERR(first_lower_dentry)) {
6128 +                       err = PTR_ERR(first_lower_dentry);
6129 +                       goto out;
6130 +               }
6131 +
6132 +               /*
6133 +                * FIXME: the following line needs to be changed to allow
6134 +                * mount-point crossing
6135 +                */
6136 +               first_dentry = dentry;
6137 +               first_lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6138 +                                                bindex);
6139 +       }
6140 +       unionfs_set_lower_dentry_idx(dentry, first_dentry_offset,
6141 +                                    first_lower_dentry);
6142 +       unionfs_set_lower_mnt_idx(dentry, first_dentry_offset,
6143 +                                 first_lower_mnt);
6144 +       set_dbstart(dentry, first_dentry_offset);
6145 +       set_dbend(dentry, first_dentry_offset);
6146 +
6147 +       if (lookupmode == INTERPOSE_REVAL_NEG)
6148 +               BUG_ON(dentry->d_inode != NULL);
6149 +       else
6150 +               d_add(dentry, NULL);
6151 +       goto out;
6152 +
6153 +/* This part of the code is for positive dentries. */
6154 +out_positive:
6155 +       BUG_ON(dentry_count <= 0);
6156 +
6157 +       /*
6158 +        * If we're holding onto the first negative dentry & corresponding
6159 +        * vfsmount - throw it out.
6160 +        */
6161 +       dput(first_lower_dentry);
6162 +       unionfs_mntput(first_dentry, first_dentry_offset);
6163 +
6164 +       /* Partial lookups need to re-interpose, or throw away older negs. */
6165 +       if (lookupmode == INTERPOSE_PARTIAL) {
6166 +               if (dentry->d_inode) {
6167 +                       unionfs_reinterpose(dentry);
6168 +                       goto out;
6169 +               }
6170 +
6171 +               /*
6172 +                * This somehow turned positive, so it is as if we had a
6173 +                * negative revalidation.
6174 +                */
6175 +               lookupmode = INTERPOSE_REVAL_NEG;
6176 +
6177 +               update_bstart(dentry);
6178 +               bstart = dbstart(dentry);
6179 +               bend = dbend(dentry);
6180 +       }
6181 +
6182 +       /*
6183 +        * Interpose can return a dentry if d_splice returned a different
6184 +        * dentry.
6185 +        */
6186 +       d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6187 +       if (IS_ERR(d_interposed))
6188 +               err = PTR_ERR(d_interposed);
6189 +       else if (d_interposed)
6190 +               dentry = d_interposed;
6191 +
6192 +       if (err)
6193 +               goto out_drop;
6194 +
6195 +       goto out;
6196 +
6197 +out_drop:
6198 +       d_drop(dentry);
6199 +
6200 +out_free:
6201 +       /* should dput all the underlying dentries on error condition */
6202 +       bstart = dbstart(dentry);
6203 +       if (bstart >= 0) {
6204 +               bend = dbend(dentry);
6205 +               for (bindex = bstart; bindex <= bend; bindex++) {
6206 +                       dput(unionfs_lower_dentry_idx(dentry, bindex));
6207 +                       unionfs_mntput(dentry, bindex);
6208 +               }
6209 +       }
6210 +       kfree(UNIONFS_D(dentry)->lower_paths);
6211 +       UNIONFS_D(dentry)->lower_paths = NULL;
6212 +       set_dbstart(dentry, -1);
6213 +       set_dbend(dentry, -1);
6214 +
6215 +out:
6216 +       if (!err && UNIONFS_D(dentry)) {
6217 +               BUG_ON(dbend(dentry) > UNIONFS_D(dentry)->bcount);
6218 +               BUG_ON(dbend(dentry) > sbmax(dentry->d_sb));
6219 +               if (dbstart(dentry) < 0 &&
6220 +                   dentry->d_inode && bstart >= 0 &&
6221 +                   (!UNIONFS_I(dentry->d_inode) ||
6222 +                    !UNIONFS_I(dentry->d_inode)->lower_inodes)) {
6223 +                       unionfs_mntput(dentry->d_sb->s_root, bstart);
6224 +                       dput(first_lower_dentry);
6225 +                       UNIONFS_I(dentry->d_inode)->stale = 1;
6226 +               }
6227 +       }
6228 +       kfree(whname);
6229 +       if (locked_parent)
6230 +               unionfs_unlock_dentry(parent_dentry);
6231 +       dput(parent_dentry);
6232 +       if (err && (lookupmode == INTERPOSE_LOOKUP))
6233 +               unionfs_unlock_dentry(dentry);
6234 +       if (!err && d_interposed)
6235 +               return d_interposed;
6236 +       if (dentry->d_inode && UNIONFS_I(dentry->d_inode)->stale &&
6237 +           first_dentry_offset >= 0)
6238 +               unionfs_mntput(dentry->d_sb->s_root, first_dentry_offset);
6239 +       return ERR_PTR(err);
6240 +}
6241 +
6242 +/*
6243 + * This is a utility function that fills in a unionfs dentry.
6244 + *
6245 + * Returns: 0 (ok), or -ERRNO if an error occurred.
6246 + */
6247 +int unionfs_partial_lookup(struct dentry *dentry)
6248 +{
6249 +       struct dentry *tmp;
6250 +       struct nameidata nd = { .flags = 0 };
6251 +       int err = -ENOSYS;
6252 +
6253 +       tmp = unionfs_lookup_backend(dentry, &nd, INTERPOSE_PARTIAL);
6254 +       if (!tmp) {
6255 +               err = 0;
6256 +               goto out;
6257 +       }
6258 +       if (IS_ERR(tmp)) {
6259 +               err = PTR_ERR(tmp);
6260 +               goto out;
6261 +       }
6262 +       /* need to change the interface */
6263 +       BUG_ON(tmp != dentry);
6264 +out:
6265 +       return err;
6266 +}
6267 +
6268 +/* The dentry cache is just so we have properly sized dentries. */
6269 +static struct kmem_cache *unionfs_dentry_cachep;
6270 +int unionfs_init_dentry_cache(void)
6271 +{
6272 +       unionfs_dentry_cachep =
6273 +               kmem_cache_create("unionfs_dentry",
6274 +                                 sizeof(struct unionfs_dentry_info),
6275 +                                 0, SLAB_RECLAIM_ACCOUNT, NULL);
6276 +
6277 +       return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6278 +}
6279 +
6280 +void unionfs_destroy_dentry_cache(void)
6281 +{
6282 +       if (unionfs_dentry_cachep)
6283 +               kmem_cache_destroy(unionfs_dentry_cachep);
6284 +}
6285 +
6286 +void free_dentry_private_data(struct dentry *dentry)
6287 +{
6288 +       if (!dentry || !dentry->d_fsdata)
6289 +               return;
6290 +       kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6291 +       dentry->d_fsdata = NULL;
6292 +}
6293 +
6294 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6295 +{
6296 +       struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6297 +       void *p;
6298 +       int size;
6299 +
6300 +       BUG_ON(!info);
6301 +
6302 +       size = sizeof(struct path) * sbmax(dentry->d_sb);
6303 +       p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6304 +       if (unlikely(!p))
6305 +               return -ENOMEM;
6306 +
6307 +       info->lower_paths = p;
6308 +
6309 +       info->bstart = -1;
6310 +       info->bend = -1;
6311 +       info->bopaque = -1;
6312 +       info->bcount = sbmax(dentry->d_sb);
6313 +       atomic_set(&info->generation,
6314 +                       atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6315 +
6316 +       memset(info->lower_paths, 0, size);
6317 +
6318 +       return 0;
6319 +}
6320 +
6321 +/* UNIONFS_D(dentry)->lock must be locked */
6322 +static int realloc_dentry_private_data(struct dentry *dentry)
6323 +{
6324 +       if (!__realloc_dentry_private_data(dentry))
6325 +               return 0;
6326 +
6327 +       kfree(UNIONFS_D(dentry)->lower_paths);
6328 +       free_dentry_private_data(dentry);
6329 +       return -ENOMEM;
6330 +}
6331 +
6332 +/* allocate new dentry private data */
6333 +int new_dentry_private_data(struct dentry *dentry)
6334 +{
6335 +       struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6336 +
6337 +       BUG_ON(info);
6338 +
6339 +       info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6340 +       if (unlikely(!info))
6341 +               return -ENOMEM;
6342 +
6343 +       mutex_init(&info->lock);
6344 +       mutex_lock(&info->lock);
6345 +
6346 +       info->lower_paths = NULL;
6347 +
6348 +       dentry->d_fsdata = info;
6349 +
6350 +       if (!__realloc_dentry_private_data(dentry))
6351 +               return 0;
6352 +
6353 +       mutex_unlock(&info->lock);
6354 +       free_dentry_private_data(dentry);
6355 +       return -ENOMEM;
6356 +}
6357 +
6358 +/*
6359 + * scan through the lower dentry objects, and set bstart to reflect the
6360 + * starting branch
6361 + */
6362 +void update_bstart(struct dentry *dentry)
6363 +{
6364 +       int bindex;
6365 +       int bstart = dbstart(dentry);
6366 +       int bend = dbend(dentry);
6367 +       struct dentry *lower_dentry;
6368 +
6369 +       for (bindex = bstart; bindex <= bend; bindex++) {
6370 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6371 +               if (!lower_dentry)
6372 +                       continue;
6373 +               if (lower_dentry->d_inode) {
6374 +                       set_dbstart(dentry, bindex);
6375 +                       break;
6376 +               }
6377 +               dput(lower_dentry);
6378 +               unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6379 +       }
6380 +}
6381 +
6382 +
6383 +/*
6384 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6385 + * file system.  Returns 0 on success or -error (only -ENOMEM possible).
6386 + * Inside that nd structure, this function may also return an allocated
6387 + * struct file (for open intents).  The caller, when done with this nd, must
6388 + * kfree the intent file (using release_lower_nd).
6389 + *
6390 + * XXX: this code, and the callers of this code, should be redone using
6391 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6392 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6393 + * function and a method that other file systems can call.
6394 + */
6395 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6396 +{
6397 +       int err = 0;
6398 +#ifdef ALLOC_LOWER_ND_FILE
6399 +       /*
6400 +        * XXX: one day we may need to have the lower return an open file
6401 +        * for us.  It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6402 +        * very well be needed for nfs4.
6403 +        */
6404 +       struct file *file;
6405 +#endif /* ALLOC_LOWER_ND_FILE */
6406 +
6407 +       memset(nd, 0, sizeof(struct nameidata));
6408 +       if (!flags)
6409 +               return err;
6410 +
6411 +       switch (flags) {
6412 +       case LOOKUP_CREATE:
6413 +               nd->intent.open.flags |= O_CREAT;
6414 +               /* fall through: shared code for create/open cases */
6415 +       case LOOKUP_OPEN:
6416 +               nd->flags = flags;
6417 +               nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6418 +#ifdef ALLOC_LOWER_ND_FILE
6419 +               file = kzalloc(sizeof(struct file), GFP_KERNEL);
6420 +               if (unlikely(!file)) {
6421 +                       err = -ENOMEM;
6422 +                       break; /* exit switch statement and thus return */
6423 +               }
6424 +               nd->intent.open.file = file;
6425 +#endif /* ALLOC_LOWER_ND_FILE */
6426 +               break;
6427 +       case LOOKUP_ACCESS:
6428 +               nd->flags = flags;
6429 +               break;
6430 +       default:
6431 +               /*
6432 +                * We should never get here, for now.
6433 +                * We can add new cases here later on.
6434 +                */
6435 +               pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6436 +               BUG();
6437 +               break;
6438 +       }
6439 +
6440 +       return err;
6441 +}
6442 +
6443 +void release_lower_nd(struct nameidata *nd, int err)
6444 +{
6445 +       if (!nd->intent.open.file)
6446 +               return;
6447 +       else if (!err)
6448 +               release_open_intent(nd);
6449 +#ifdef ALLOC_LOWER_ND_FILE
6450 +       kfree(nd->intent.open.file);
6451 +#endif /* ALLOC_LOWER_ND_FILE */
6452 +}
6453 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6454 new file mode 100644
6455 index 0000000..ffb0da1
6456 --- /dev/null
6457 +++ b/fs/unionfs/main.c
6458 @@ -0,0 +1,783 @@
6459 +/*
6460 + * Copyright (c) 2003-2007 Erez Zadok
6461 + * Copyright (c) 2003-2006 Charles P. Wright
6462 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6463 + * Copyright (c) 2005-2006 Junjiro Okajima
6464 + * Copyright (c) 2005      Arun M. Krishnakumar
6465 + * Copyright (c) 2004-2006 David P. Quigley
6466 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6467 + * Copyright (c) 2003      Puja Gupta
6468 + * Copyright (c) 2003      Harikesavan Krishnan
6469 + * Copyright (c) 2003-2007 Stony Brook University
6470 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
6471 + *
6472 + * This program is free software; you can redistribute it and/or modify
6473 + * it under the terms of the GNU General Public License version 2 as
6474 + * published by the Free Software Foundation.
6475 + */
6476 +
6477 +#include "union.h"
6478 +#include <linux/module.h>
6479 +#include <linux/moduleparam.h>
6480 +
6481 +static void unionfs_fill_inode(struct dentry *dentry,
6482 +                              struct inode *inode)
6483 +{
6484 +       struct inode *lower_inode;
6485 +       struct dentry *lower_dentry;
6486 +       int bindex, bstart, bend;
6487 +
6488 +       bstart = dbstart(dentry);
6489 +       bend = dbend(dentry);
6490 +
6491 +       for (bindex = bstart; bindex <= bend; bindex++) {
6492 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6493 +               if (!lower_dentry) {
6494 +                       unionfs_set_lower_inode_idx(inode, bindex, NULL);
6495 +                       continue;
6496 +               }
6497 +
6498 +               /* Initialize the lower inode to the new lower inode. */
6499 +               if (!lower_dentry->d_inode)
6500 +                       continue;
6501 +
6502 +               unionfs_set_lower_inode_idx(inode, bindex,
6503 +                                           igrab(lower_dentry->d_inode));
6504 +       }
6505 +
6506 +       ibstart(inode) = dbstart(dentry);
6507 +       ibend(inode) = dbend(dentry);
6508 +
6509 +       /* Use attributes from the first branch. */
6510 +       lower_inode = unionfs_lower_inode(inode);
6511 +
6512 +       /* Use different set of inode ops for symlinks & directories */
6513 +       if (S_ISLNK(lower_inode->i_mode))
6514 +               inode->i_op = &unionfs_symlink_iops;
6515 +       else if (S_ISDIR(lower_inode->i_mode))
6516 +               inode->i_op = &unionfs_dir_iops;
6517 +
6518 +       /* Use different set of file ops for directories */
6519 +       if (S_ISDIR(lower_inode->i_mode))
6520 +               inode->i_fop = &unionfs_dir_fops;
6521 +
6522 +       /* properly initialize special inodes */
6523 +       if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6524 +           S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6525 +               init_special_inode(inode, lower_inode->i_mode,
6526 +                                  lower_inode->i_rdev);
6527 +
6528 +       /* all well, copy inode attributes */
6529 +       unionfs_copy_attr_all(inode, lower_inode);
6530 +       fsstack_copy_inode_size(inode, lower_inode);
6531 +}
6532 +
6533 +/*
6534 + * Connect a unionfs inode dentry/inode with several lower ones.  This is
6535 + * the classic stackable file system "vnode interposition" action.
6536 + *
6537 + * @sb: unionfs's super_block
6538 + */
6539 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6540 +                                int flag)
6541 +{
6542 +       int err = 0;
6543 +       struct inode *inode;
6544 +       int is_negative_dentry = 1;
6545 +       int bindex, bstart, bend;
6546 +       int need_fill_inode = 1;
6547 +       struct dentry *spliced = NULL;
6548 +
6549 +       verify_locked(dentry);
6550 +
6551 +       bstart = dbstart(dentry);
6552 +       bend = dbend(dentry);
6553 +
6554 +       /* Make sure that we didn't get a negative dentry. */
6555 +       for (bindex = bstart; bindex <= bend; bindex++) {
6556 +               if (unionfs_lower_dentry_idx(dentry, bindex) &&
6557 +                   unionfs_lower_dentry_idx(dentry, bindex)->d_inode) {
6558 +                       is_negative_dentry = 0;
6559 +                       break;
6560 +               }
6561 +       }
6562 +       BUG_ON(is_negative_dentry);
6563 +
6564 +       /*
6565 +        * We allocate our new inode below, by calling iget.
6566 +        * iget will call our read_inode which will initialize some
6567 +        * of the new inode's fields
6568 +        */
6569 +
6570 +       /*
6571 +        * On revalidate we've already got our own inode and just need
6572 +        * to fix it up.
6573 +        */
6574 +       if (flag == INTERPOSE_REVAL) {
6575 +               inode = dentry->d_inode;
6576 +               UNIONFS_I(inode)->bstart = -1;
6577 +               UNIONFS_I(inode)->bend = -1;
6578 +               atomic_set(&UNIONFS_I(inode)->generation,
6579 +                          atomic_read(&UNIONFS_SB(sb)->generation));
6580 +
6581 +               UNIONFS_I(inode)->lower_inodes =
6582 +                       kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6583 +               if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6584 +                       err = -ENOMEM;
6585 +                       goto out;
6586 +               }
6587 +       } else {
6588 +               /* get unique inode number for unionfs */
6589 +               inode = iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6590 +               if (!inode) {
6591 +                       err = -EACCES;
6592 +                       goto out;
6593 +               }
6594 +               if (atomic_read(&inode->i_count) > 1)
6595 +                       goto skip;
6596 +       }
6597 +
6598 +       need_fill_inode = 0;
6599 +       unionfs_fill_inode(dentry, inode);
6600 +
6601 +skip:
6602 +       /* only (our) lookup wants to do a d_add */
6603 +       switch (flag) {
6604 +       case INTERPOSE_DEFAULT:
6605 +       case INTERPOSE_REVAL_NEG:
6606 +               d_instantiate(dentry, inode);
6607 +               break;
6608 +       case INTERPOSE_LOOKUP:
6609 +               spliced = d_splice_alias(inode, dentry);
6610 +               if (IS_ERR(spliced)) {
6611 +                       err = PTR_ERR(spliced);
6612 +               } else if (spliced && spliced != dentry) {
6613 +                       /*
6614 +                        * d_splice can return a dentry if it was
6615 +                        * disconnected and had to be moved.  We must ensure
6616 +                        * that the private data of the new dentry is
6617 +                        * correct and that the inode info was filled
6618 +                        * properly.  Finally we must return this new
6619 +                        * dentry.
6620 +                        */
6621 +                       spliced->d_op = &unionfs_dops;
6622 +                       spliced->d_fsdata = dentry->d_fsdata;
6623 +                       dentry->d_fsdata = NULL;
6624 +                       dentry = spliced;
6625 +                       if (need_fill_inode) {
6626 +                               need_fill_inode = 0;
6627 +                               unionfs_fill_inode(dentry, inode);
6628 +                       }
6629 +                       goto out_spliced;
6630 +               }
6631 +               break;
6632 +       case INTERPOSE_REVAL:
6633 +               /* Do nothing. */
6634 +               break;
6635 +       default:
6636 +               printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6637 +               BUG();
6638 +       }
6639 +       goto out;
6640 +
6641 +out_spliced:
6642 +       if (!err)
6643 +               return spliced;
6644 +out:
6645 +       return ERR_PTR(err);
6646 +}
6647 +
6648 +/* like interpose above, but for an already existing dentry */
6649 +void unionfs_reinterpose(struct dentry *dentry)
6650 +{
6651 +       struct dentry *lower_dentry;
6652 +       struct inode *inode;
6653 +       int bindex, bstart, bend;
6654 +
6655 +       verify_locked(dentry);
6656 +
6657 +       /* This is pre-allocated inode */
6658 +       inode = dentry->d_inode;
6659 +
6660 +       bstart = dbstart(dentry);
6661 +       bend = dbend(dentry);
6662 +       for (bindex = bstart; bindex <= bend; bindex++) {
6663 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6664 +               if (!lower_dentry)
6665 +                       continue;
6666 +
6667 +               if (!lower_dentry->d_inode)
6668 +                       continue;
6669 +               if (unionfs_lower_inode_idx(inode, bindex))
6670 +                       continue;
6671 +               unionfs_set_lower_inode_idx(inode, bindex,
6672 +                                           igrab(lower_dentry->d_inode));
6673 +       }
6674 +       ibstart(inode) = dbstart(dentry);
6675 +       ibend(inode) = dbend(dentry);
6676 +}
6677 +
6678 +/*
6679 + * make sure the branch we just looked up (nd) makes sense:
6680 + *
6681 + * 1) we're not trying to stack unionfs on top of unionfs
6682 + * 2) it exists
6683 + * 3) is a directory
6684 + */
6685 +int check_branch(struct nameidata *nd)
6686 +{
6687 +       /* XXX: remove in ODF code -- stacking unions allowed there */
6688 +       if (!strcmp(nd->dentry->d_sb->s_type->name, UNIONFS_NAME))
6689 +               return -EINVAL;
6690 +       if (!nd->dentry->d_inode)
6691 +               return -ENOENT;
6692 +       if (!S_ISDIR(nd->dentry->d_inode->i_mode))
6693 +               return -ENOTDIR;
6694 +       return 0;
6695 +}
6696 +
6697 +/* checks if two lower_dentries have overlapping branches */
6698 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6699 +{
6700 +       struct dentry *dent = NULL;
6701 +
6702 +       dent = dent1;
6703 +       while ((dent != dent2) && (dent->d_parent != dent))
6704 +               dent = dent->d_parent;
6705 +
6706 +       if (dent == dent2)
6707 +               return 1;
6708 +
6709 +       dent = dent2;
6710 +       while ((dent != dent1) && (dent->d_parent != dent))
6711 +               dent = dent->d_parent;
6712 +
6713 +       return (dent == dent1);
6714 +}
6715 +
6716 +/*
6717 + * Parse branch mode helper function
6718 + */
6719 +int __parse_branch_mode(const char *name)
6720 +{
6721 +       if (!name)
6722 +               return 0;
6723 +       if (!strcmp(name, "ro"))
6724 +               return MAY_READ;
6725 +       if (!strcmp(name, "rw"))
6726 +               return (MAY_READ | MAY_WRITE);
6727 +       return 0;
6728 +}
6729 +
6730 +/*
6731 + * Parse "ro" or "rw" options, but default to "rw" of no mode options
6732 + * was specified.
6733 + */
6734 +int parse_branch_mode(const char *name)
6735 +{
6736 +       int perms = __parse_branch_mode(name);
6737 +
6738 +       if (perms == 0)
6739 +               perms = MAY_READ | MAY_WRITE;
6740 +       return perms;
6741 +}
6742 +
6743 +/*
6744 + * parse the dirs= mount argument
6745 + *
6746 + * We don't need to lock the superblock private data's rwsem, as we get
6747 + * called only by unionfs_read_super - it is still a long time before anyone
6748 + * can even get a reference to us.
6749 + */
6750 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6751 +                            *lower_root_info, char *options)
6752 +{
6753 +       struct nameidata nd;
6754 +       char *name;
6755 +       int err = 0;
6756 +       int branches = 1;
6757 +       int bindex = 0;
6758 +       int i = 0;
6759 +       int j = 0;
6760 +       struct dentry *dent1;
6761 +       struct dentry *dent2;
6762 +
6763 +       if (options[0] == '\0') {
6764 +               printk(KERN_ERR "unionfs: no branches specified\n");
6765 +               err = -EINVAL;
6766 +               goto out;
6767 +       }
6768 +
6769 +       /*
6770 +        * Each colon means we have a separator, this is really just a rough
6771 +        * guess, since strsep will handle empty fields for us.
6772 +        */
6773 +       for (i = 0; options[i]; i++)
6774 +               if (options[i] == ':')
6775 +                       branches++;
6776 +
6777 +       /* allocate space for underlying pointers to lower dentry */
6778 +       UNIONFS_SB(sb)->data =
6779 +               kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6780 +       if (unlikely(!UNIONFS_SB(sb)->data)) {
6781 +               err = -ENOMEM;
6782 +               goto out;
6783 +       }
6784 +
6785 +       lower_root_info->lower_paths =
6786 +               kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6787 +       if (unlikely(!lower_root_info->lower_paths)) {
6788 +               err = -ENOMEM;
6789 +               goto out;
6790 +       }
6791 +
6792 +       /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6793 +       branches = 0;
6794 +       while ((name = strsep(&options, ":")) != NULL) {
6795 +               int perms;
6796 +               char *mode = strchr(name, '=');
6797 +
6798 +               if (!name)
6799 +                       continue;
6800 +               if (!*name) {   /* bad use of ':' (extra colons) */
6801 +                       err = -EINVAL;
6802 +                       goto out;
6803 +               }
6804 +
6805 +               branches++;
6806 +
6807 +               /* strip off '=' if any */
6808 +               if (mode)
6809 +                       *mode++ = '\0';
6810 +
6811 +               perms = parse_branch_mode(mode);
6812 +               if (!bindex && !(perms & MAY_WRITE)) {
6813 +                       err = -EINVAL;
6814 +                       goto out;
6815 +               }
6816 +
6817 +               err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6818 +               if (err) {
6819 +                       printk(KERN_ERR "unionfs: error accessing "
6820 +                              "lower directory '%s' (error %d)\n",
6821 +                              name, err);
6822 +                       goto out;
6823 +               }
6824 +
6825 +               err = check_branch(&nd);
6826 +               if (err) {
6827 +                       printk(KERN_ERR "unionfs: lower directory "
6828 +                              "'%s' is not a valid branch\n", name);
6829 +                       path_release(&nd);
6830 +                       goto out;
6831 +               }
6832 +
6833 +               lower_root_info->lower_paths[bindex].dentry = nd.dentry;
6834 +               lower_root_info->lower_paths[bindex].mnt = nd.mnt;
6835 +
6836 +               set_branchperms(sb, bindex, perms);
6837 +               set_branch_count(sb, bindex, 0);
6838 +               new_branch_id(sb, bindex);
6839 +
6840 +               if (lower_root_info->bstart < 0)
6841 +                       lower_root_info->bstart = bindex;
6842 +               lower_root_info->bend = bindex;
6843 +               bindex++;
6844 +       }
6845 +
6846 +       if (branches == 0) {
6847 +               printk(KERN_ERR "unionfs: no branches specified\n");
6848 +               err = -EINVAL;
6849 +               goto out;
6850 +       }
6851 +
6852 +       BUG_ON(branches != (lower_root_info->bend + 1));
6853 +
6854 +       /*
6855 +        * Ensure that no overlaps exist in the branches.
6856 +        *
6857 +        * This test is required because the Linux kernel has no support
6858 +        * currently for ensuring coherency between stackable layers and
6859 +        * branches.  If we were to allow overlapping branches, it would be
6860 +        * possible, for example, to delete a file via one branch, which
6861 +        * would not be reflected in another branch.  Such incoherency could
6862 +        * lead to inconsistencies and even kernel oopses.  Rather than
6863 +        * implement hacks to work around some of these cache-coherency
6864 +        * problems, we prevent branch overlapping, for now.  A complete
6865 +        * solution will involve proper kernel/VFS support for cache
6866 +        * coherency, at which time we could safely remove this
6867 +        * branch-overlapping test.
6868 +        */
6869 +       for (i = 0; i < branches; i++) {
6870 +               dent1 = lower_root_info->lower_paths[i].dentry;
6871 +               for (j = i + 1; j < branches; j++) {
6872 +                       dent2 = lower_root_info->lower_paths[j].dentry;
6873 +                       if (is_branch_overlap(dent1, dent2)) {
6874 +                               printk(KERN_ERR "unionfs: branches %d and "
6875 +                                      "%d overlap\n", i, j);
6876 +                               err = -EINVAL;
6877 +                               goto out;
6878 +                       }
6879 +               }
6880 +       }
6881 +
6882 +out:
6883 +       if (err) {
6884 +               for (i = 0; i < branches; i++)
6885 +                       if (lower_root_info->lower_paths[i].dentry) {
6886 +                               dput(lower_root_info->lower_paths[i].dentry);
6887 +                               /* initialize: can't use unionfs_mntput here */
6888 +                               mntput(lower_root_info->lower_paths[i].mnt);
6889 +                       }
6890 +
6891 +               kfree(lower_root_info->lower_paths);
6892 +               kfree(UNIONFS_SB(sb)->data);
6893 +
6894 +               /*
6895 +                * MUST clear the pointers to prevent potential double free if
6896 +                * the caller dies later on
6897 +                */
6898 +               lower_root_info->lower_paths = NULL;
6899 +               UNIONFS_SB(sb)->data = NULL;
6900 +       }
6901 +       return err;
6902 +}
6903 +
6904 +/*
6905 + * Parse mount options.  See the manual page for usage instructions.
6906 + *
6907 + * Returns the dentry object of the lower-level (lower) directory;
6908 + * We want to mount our stackable file system on top of that lower directory.
6909 + */
6910 +static struct unionfs_dentry_info *unionfs_parse_options(
6911 +                                        struct super_block *sb,
6912 +                                        char *options)
6913 +{
6914 +       struct unionfs_dentry_info *lower_root_info;
6915 +       char *optname;
6916 +       int err = 0;
6917 +       int bindex;
6918 +       int dirsfound = 0;
6919 +
6920 +       /* allocate private data area */
6921 +       err = -ENOMEM;
6922 +       lower_root_info =
6923 +               kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6924 +       if (unlikely(!lower_root_info))
6925 +               goto out_error;
6926 +       lower_root_info->bstart = -1;
6927 +       lower_root_info->bend = -1;
6928 +       lower_root_info->bopaque = -1;
6929 +
6930 +       while ((optname = strsep(&options, ",")) != NULL) {
6931 +               char *optarg;
6932 +               char *endptr;
6933 +               int intval;
6934 +
6935 +               if (!optname || !*optname)
6936 +                       continue;
6937 +
6938 +               optarg = strchr(optname, '=');
6939 +               if (optarg)
6940 +                       *optarg++ = '\0';
6941 +
6942 +               /*
6943 +                * All of our options take an argument now. Insert ones that
6944 +                * don't, above this check.
6945 +                */
6946 +               if (!optarg) {
6947 +                       printk(KERN_ERR "unionfs: %s requires an argument\n",
6948 +                              optname);
6949 +                       err = -EINVAL;
6950 +                       goto out_error;
6951 +               }
6952 +
6953 +               if (!strcmp("dirs", optname)) {
6954 +                       if (++dirsfound > 1) {
6955 +                               printk(KERN_ERR
6956 +                                      "unionfs: multiple dirs specified\n");
6957 +                               err = -EINVAL;
6958 +                               goto out_error;
6959 +                       }
6960 +                       err = parse_dirs_option(sb, lower_root_info, optarg);
6961 +                       if (err)
6962 +                               goto out_error;
6963 +                       continue;
6964 +               }
6965 +
6966 +               /* All of these options require an integer argument. */
6967 +               intval = simple_strtoul(optarg, &endptr, 0);
6968 +               if (*endptr) {
6969 +                       printk(KERN_ERR
6970 +                              "unionfs: invalid %s option '%s'\n",
6971 +                              optname, optarg);
6972 +                       err = -EINVAL;
6973 +                       goto out_error;
6974 +               }
6975 +
6976 +               err = -EINVAL;
6977 +               printk(KERN_ERR
6978 +                      "unionfs: unrecognized option '%s'\n", optname);
6979 +               goto out_error;
6980 +       }
6981 +       if (dirsfound != 1) {
6982 +               printk(KERN_ERR "unionfs: dirs option required\n");
6983 +               err = -EINVAL;
6984 +               goto out_error;
6985 +       }
6986 +       goto out;
6987 +
6988 +out_error:
6989 +       if (lower_root_info && lower_root_info->lower_paths) {
6990 +               for (bindex = lower_root_info->bstart;
6991 +                    bindex >= 0 && bindex <= lower_root_info->bend;
6992 +                    bindex++) {
6993 +                       struct dentry *d;
6994 +                       struct vfsmount *m;
6995 +
6996 +                       d = lower_root_info->lower_paths[bindex].dentry;
6997 +                       m = lower_root_info->lower_paths[bindex].mnt;
6998 +
6999 +                       dput(d);
7000 +                       /* initializing: can't use unionfs_mntput here */
7001 +                       mntput(m);
7002 +               }
7003 +       }
7004 +
7005 +       kfree(lower_root_info->lower_paths);
7006 +       kfree(lower_root_info);
7007 +
7008 +       kfree(UNIONFS_SB(sb)->data);
7009 +       UNIONFS_SB(sb)->data = NULL;
7010 +
7011 +       lower_root_info = ERR_PTR(err);
7012 +out:
7013 +       return lower_root_info;
7014 +}
7015 +
7016 +/*
7017 + * our custom d_alloc_root work-alike
7018 + *
7019 + * we can't use d_alloc_root if we want to use our own interpose function
7020 + * unchanged, so we simply call our own "fake" d_alloc_root
7021 + */
7022 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7023 +{
7024 +       struct dentry *ret = NULL;
7025 +
7026 +       if (sb) {
7027 +               static const struct qstr name = {
7028 +                       .name = "/",
7029 +                       .len = 1
7030 +               };
7031 +
7032 +               ret = d_alloc(NULL, &name);
7033 +               if (likely(ret)) {
7034 +                       ret->d_op = &unionfs_dops;
7035 +                       ret->d_sb = sb;
7036 +                       ret->d_parent = ret;
7037 +               }
7038 +       }
7039 +       return ret;
7040 +}
7041 +
7042 +/*
7043 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7044 + * way anyone can have a reference to the superblock at this point in time.
7045 + */
7046 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7047 +                             int silent)
7048 +{
7049 +       int err = 0;
7050 +       struct unionfs_dentry_info *lower_root_info = NULL;
7051 +       int bindex, bstart, bend;
7052 +
7053 +       if (!raw_data) {
7054 +               printk(KERN_ERR
7055 +                      "unionfs: read_super: missing data argument\n");
7056 +               err = -EINVAL;
7057 +               goto out;
7058 +       }
7059 +
7060 +       /* Allocate superblock private data */
7061 +       sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7062 +       if (unlikely(!UNIONFS_SB(sb))) {
7063 +               printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7064 +               err = -ENOMEM;
7065 +               goto out;
7066 +       }
7067 +
7068 +       UNIONFS_SB(sb)->bend = -1;
7069 +       atomic_set(&UNIONFS_SB(sb)->generation, 1);
7070 +       init_rwsem(&UNIONFS_SB(sb)->rwsem);
7071 +       UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7072 +
7073 +       lower_root_info = unionfs_parse_options(sb, raw_data);
7074 +       if (IS_ERR(lower_root_info)) {
7075 +               printk(KERN_ERR
7076 +                      "unionfs: read_super: error while parsing options "
7077 +                      "(err = %ld)\n", PTR_ERR(lower_root_info));
7078 +               err = PTR_ERR(lower_root_info);
7079 +               lower_root_info = NULL;
7080 +               goto out_free;
7081 +       }
7082 +       if (lower_root_info->bstart == -1) {
7083 +               err = -ENOENT;
7084 +               goto out_free;
7085 +       }
7086 +
7087 +       /* set the lower superblock field of upper superblock */
7088 +       bstart = lower_root_info->bstart;
7089 +       BUG_ON(bstart != 0);
7090 +       sbend(sb) = bend = lower_root_info->bend;
7091 +       for (bindex = bstart; bindex <= bend; bindex++) {
7092 +               struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7093 +               unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7094 +       }
7095 +
7096 +       /* max Bytes is the maximum bytes from highest priority branch */
7097 +       sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7098 +
7099 +       sb->s_op = &unionfs_sops;
7100 +
7101 +       /* See comment next to the definition of unionfs_d_alloc_root */
7102 +       sb->s_root = unionfs_d_alloc_root(sb);
7103 +       if (unlikely(!sb->s_root)) {
7104 +               err = -ENOMEM;
7105 +               goto out_dput;
7106 +       }
7107 +
7108 +       /* link the upper and lower dentries */
7109 +       sb->s_root->d_fsdata = NULL;
7110 +       err = new_dentry_private_data(sb->s_root);
7111 +       if (unlikely(err))
7112 +               goto out_freedpd;
7113 +
7114 +       /* Set the lower dentries for s_root */
7115 +       for (bindex = bstart; bindex <= bend; bindex++) {
7116 +               struct dentry *d;
7117 +               struct vfsmount *m;
7118 +
7119 +               d = lower_root_info->lower_paths[bindex].dentry;
7120 +               m = lower_root_info->lower_paths[bindex].mnt;
7121 +
7122 +               unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7123 +               unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7124 +       }
7125 +       set_dbstart(sb->s_root, bstart);
7126 +       set_dbend(sb->s_root, bend);
7127 +
7128 +       /* Set the generation number to one, since this is for the mount. */
7129 +       atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7130 +
7131 +       /*
7132 +        * Call interpose to create the upper level inode.  Only
7133 +        * INTERPOSE_LOOKUP can return a value other than 0 on err.
7134 +        */
7135 +       err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7136 +       unionfs_unlock_dentry(sb->s_root);
7137 +       if (!err)
7138 +               goto out;
7139 +       /* else fall through */
7140 +
7141 +out_freedpd:
7142 +       if (UNIONFS_D(sb->s_root)) {
7143 +               kfree(UNIONFS_D(sb->s_root)->lower_paths);
7144 +               free_dentry_private_data(sb->s_root);
7145 +       }
7146 +       dput(sb->s_root);
7147 +
7148 +out_dput:
7149 +       if (lower_root_info && !IS_ERR(lower_root_info)) {
7150 +               for (bindex = lower_root_info->bstart;
7151 +                    bindex <= lower_root_info->bend; bindex++) {
7152 +                       struct dentry *d;
7153 +                       struct vfsmount *m;
7154 +
7155 +                       d = lower_root_info->lower_paths[bindex].dentry;
7156 +                       m = lower_root_info->lower_paths[bindex].mnt;
7157 +
7158 +                       dput(d);
7159 +                       /* initializing: can't use unionfs_mntput here */
7160 +                       mntput(m);
7161 +               }
7162 +               kfree(lower_root_info->lower_paths);
7163 +               kfree(lower_root_info);
7164 +               lower_root_info = NULL;
7165 +       }
7166 +
7167 +out_free:
7168 +       kfree(UNIONFS_SB(sb)->data);
7169 +       kfree(UNIONFS_SB(sb));
7170 +       sb->s_fs_info = NULL;
7171 +
7172 +out:
7173 +       if (lower_root_info && !IS_ERR(lower_root_info)) {
7174 +               kfree(lower_root_info->lower_paths);
7175 +               kfree(lower_root_info);
7176 +       }
7177 +       return err;
7178 +}
7179 +
7180 +static int unionfs_get_sb(struct file_system_type *fs_type,
7181 +                         int flags, const char *dev_name,
7182 +                         void *raw_data, struct vfsmount *mnt)
7183 +{
7184 +       return get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7185 +}
7186 +
7187 +static struct file_system_type unionfs_fs_type = {
7188 +       .owner          = THIS_MODULE,
7189 +       .name           = UNIONFS_NAME,
7190 +       .get_sb         = unionfs_get_sb,
7191 +       .kill_sb        = generic_shutdown_super,
7192 +       .fs_flags       = FS_REVAL_DOT,
7193 +};
7194 +
7195 +static int __init init_unionfs_fs(void)
7196 +{
7197 +       int err;
7198 +
7199 +       pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7200 +
7201 +       err = unionfs_init_filldir_cache();
7202 +       if (unlikely(err))
7203 +               goto out;
7204 +       err = unionfs_init_inode_cache();
7205 +       if (unlikely(err))
7206 +               goto out;
7207 +       err = unionfs_init_dentry_cache();
7208 +       if (unlikely(err))
7209 +               goto out;
7210 +       err = init_sioq();
7211 +       if (unlikely(err))
7212 +               goto out;
7213 +       err = register_filesystem(&unionfs_fs_type);
7214 +out:
7215 +       if (unlikely(err)) {
7216 +               stop_sioq();
7217 +               unionfs_destroy_filldir_cache();
7218 +               unionfs_destroy_inode_cache();
7219 +               unionfs_destroy_dentry_cache();
7220 +       }
7221 +       return err;
7222 +}
7223 +
7224 +static void __exit exit_unionfs_fs(void)
7225 +{
7226 +       stop_sioq();
7227 +       unionfs_destroy_filldir_cache();
7228 +       unionfs_destroy_inode_cache();
7229 +       unionfs_destroy_dentry_cache();
7230 +       unregister_filesystem(&unionfs_fs_type);
7231 +       pr_info("Completed unionfs module unload\n");
7232 +}
7233 +
7234 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7235 +             " (http://www.fsl.cs.sunysb.edu)");
7236 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7237 +                  " (http://unionfs.filesystems.org)");
7238 +MODULE_LICENSE("GPL");
7239 +
7240 +module_init(init_unionfs_fs);
7241 +module_exit(exit_unionfs_fs);
7242 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7243 new file mode 100644
7244 index 0000000..6440282
7245 --- /dev/null
7246 +++ b/fs/unionfs/mmap.c
7247 @@ -0,0 +1,381 @@
7248 +/*
7249 + * Copyright (c) 2003-2007 Erez Zadok
7250 + * Copyright (c) 2003-2006 Charles P. Wright
7251 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7252 + * Copyright (c) 2005-2006 Junjiro Okajima
7253 + * Copyright (c) 2006      Shaya Potter
7254 + * Copyright (c) 2005      Arun M. Krishnakumar
7255 + * Copyright (c) 2004-2006 David P. Quigley
7256 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7257 + * Copyright (c) 2003      Puja Gupta
7258 + * Copyright (c) 2003      Harikesavan Krishnan
7259 + * Copyright (c) 2003-2007 Stony Brook University
7260 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
7261 + *
7262 + * This program is free software; you can redistribute it and/or modify
7263 + * it under the terms of the GNU General Public License version 2 as
7264 + * published by the Free Software Foundation.
7265 + */
7266 +
7267 +#include "union.h"
7268 +
7269 +/*
7270 + * Unionfs doesn't implement ->writepages, which is OK with the VFS and
7271 + * keeps our code simpler and smaller.  Nevertheless, somehow, our own
7272 + * ->writepage must be called so we can sync the upper pages with the lower
7273 + * pages: otherwise data changed at the upper layer won't get written to the
7274 + * lower layer.
7275 + *
7276 + * Some lower file systems (e.g., NFS) expect the VFS to call its writepages
7277 + * only, which in turn will call generic_writepages and invoke each of the
7278 + * lower file system's ->writepage.  NFS in particular uses the
7279 + * wbc->fs_private field in its nfs_writepage, which is set in its
7280 + * nfs_writepages.  So if we don't call the lower nfs_writepages first, then
7281 + * NFS's nfs_writepage will dereference a NULL wbc->fs_private and cause an
7282 + * OOPS.  If, however, we implement a unionfs_writepages and then we do call
7283 + * the lower nfs_writepages, then we "lose control" over the pages we're
7284 + * trying to write to the lower file system: we won't be writing our own
7285 + * new/modified data from the upper pages to the lower pages, and any
7286 + * mmap-based changes are lost.
7287 + *
7288 + * This is a fundamental cache-coherency problem in Linux.  The kernel isn't
7289 + * able to support such stacking abstractions cleanly.  One possible clean
7290 + * way would be that a lower file system's ->writepage method have some sort
7291 + * of a callback to validate if any upper pages for the same file+offset
7292 + * exist and have newer content in them.
7293 + *
7294 + * This whole NULL ptr dereference is triggered at the lower file system
7295 + * (NFS) because the wbc->for_writepages is set to 1.  Therefore, to avoid
7296 + * this NULL pointer dereference, we set this flag to 0 and restore it upon
7297 + * exit.  This probably means that we're slightly less efficient in writing
7298 + * pages out, doing them one at a time, but at least we avoid the oops until
7299 + * such day as Linux can better support address_space_ops in a stackable
7300 + * fashion.
7301 + */
7302 +static int unionfs_writepage(struct page *page, struct writeback_control *wbc)
7303 +{
7304 +       int err = -EIO;
7305 +       struct inode *inode;
7306 +       struct inode *lower_inode;
7307 +       struct page *lower_page;
7308 +       char *kaddr, *lower_kaddr;
7309 +       int saved_for_writepages = wbc->for_writepages;
7310 +
7311 +       inode = page->mapping->host;
7312 +       lower_inode = unionfs_lower_inode(inode);
7313 +
7314 +       /*
7315 +        * find lower page (returns a locked page)
7316 +        *
7317 +        * NOTE: we used to call grab_cache_page(), but that was unnecessary
7318 +        * as it would have tried to create a new lower page if it didn't
7319 +        * exist, leading to deadlocks (esp. under memory-pressure
7320 +        * conditions, when it is really a bad idea to *consume* more
7321 +        * memory).  Instead, we assume the lower page exists, and if we can
7322 +        * find it, then we ->writepage on it; if we can't find it, then it
7323 +        * couldn't have disappeared unless the kernel already flushed it,
7324 +        * in which case we're still OK.  This is especially correct if
7325 +        * wbc->sync_mode is WB_SYNC_NONE (as per
7326 +        * Documentation/filesystems/vfs.txt).  If we can't flush our page
7327 +        * because we can't find a lower page, then at least we re-mark our
7328 +        * page as dirty, and return AOP_WRITEPAGE_ACTIVATE as the VFS
7329 +        * expects us to.  (Note, if in the future it'd turn out that we
7330 +        * have to find a lower page no matter what, then we'd have to
7331 +        * resort to RAIF's page pointer flipping trick.)
7332 +        */
7333 +       lower_page = find_lock_page(lower_inode->i_mapping, page->index);
7334 +       if (!lower_page) {
7335 +               err = AOP_WRITEPAGE_ACTIVATE;
7336 +               set_page_dirty(page);
7337 +               goto out;
7338 +       }
7339 +
7340 +       /* get page address, and encode it */
7341 +       kaddr = kmap(page);
7342 +       lower_kaddr = kmap(lower_page);
7343 +
7344 +       memcpy(lower_kaddr, kaddr, PAGE_CACHE_SIZE);
7345 +
7346 +       kunmap(page);
7347 +       kunmap(lower_page);
7348 +
7349 +       BUG_ON(!lower_inode->i_mapping->a_ops->writepage);
7350 +
7351 +       /* workaround for some lower file systems: see big comment on top */
7352 +       if (wbc->for_writepages && !wbc->fs_private)
7353 +               wbc->for_writepages = 0;
7354 +
7355 +       /* call lower writepage (expects locked page) */
7356 +       clear_page_dirty_for_io(lower_page); /* emulate VFS behavior */
7357 +       err = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
7358 +       wbc->for_writepages = saved_for_writepages; /* restore value */
7359 +
7360 +       /* b/c find_lock_page locked it and ->writepage unlocks on success */
7361 +       if (err)
7362 +               unlock_page(lower_page);
7363 +       /* b/c grab_cache_page increased refcnt */
7364 +       page_cache_release(lower_page);
7365 +
7366 +       if (err < 0) {
7367 +               ClearPageUptodate(page);
7368 +               goto out;
7369 +       }
7370 +       if (err == AOP_WRITEPAGE_ACTIVATE) {
7371 +               /*
7372 +                * Lower file systems such as ramfs and tmpfs, may return
7373 +                * AOP_WRITEPAGE_ACTIVATE so that the VM won't try to
7374 +                * (pointlessly) write the page again for a while.  But
7375 +                * those lower file systems also set the page dirty bit back
7376 +                * again.  So we mimic that behaviour here.
7377 +                */
7378 +               if (PageDirty(lower_page))
7379 +                       set_page_dirty(page);
7380 +               goto out;
7381 +       }
7382 +
7383 +       /* all is well */
7384 +       SetPageUptodate(page);
7385 +       /* lower mtimes has changed: update ours */
7386 +       unionfs_copy_attr_times(inode);
7387 +
7388 +       unlock_page(page);
7389 +
7390 +out:
7391 +       return err;
7392 +}
7393 +
7394 +/*
7395 + * readpage is called from generic_page_read and the fault handler.
7396 + * If your file system uses generic_page_read for the read op, it
7397 + * must implement readpage.
7398 + *
7399 + * Readpage expects a locked page, and must unlock it.
7400 + */
7401 +static int unionfs_do_readpage(struct file *file, struct page *page)
7402 +{
7403 +       int err = -EIO;
7404 +       struct file *lower_file;
7405 +       struct inode *inode;
7406 +       mm_segment_t old_fs;
7407 +       char *page_data = NULL;
7408 +       loff_t offset;
7409 +
7410 +       if (!UNIONFS_F(file)) {
7411 +               err = -ENOENT;
7412 +               goto out;
7413 +       }
7414 +
7415 +       lower_file = unionfs_lower_file(file);
7416 +       /* FIXME: is this assertion right here? */
7417 +       BUG_ON(lower_file == NULL);
7418 +
7419 +       inode = file->f_path.dentry->d_inode;
7420 +
7421 +       page_data = (char *) kmap(page);
7422 +       /*
7423 +        * Use vfs_read because some lower file systems don't have a
7424 +        * readpage method, and some file systems (esp. distributed ones)
7425 +        * don't like their pages to be accessed directly.  Using vfs_read
7426 +        * may be a little slower, but a lot safer, as the VFS does a lot of
7427 +        * the necessary magic for us.
7428 +        */
7429 +       lower_file->f_pos = page_offset(page);
7430 +       offset = page_offset(page);
7431 +       old_fs = get_fs();
7432 +       set_fs(KERNEL_DS);
7433 +       err = vfs_read(lower_file, page_data, PAGE_CACHE_SIZE,
7434 +                      &lower_file->f_pos);
7435 +       set_fs(old_fs);
7436 +
7437 +       kunmap(page);
7438 +
7439 +       if (err < 0)
7440 +               goto out;
7441 +       err = 0;
7442 +
7443 +       /* if vfs_read succeeded above, sync up our times */
7444 +       unionfs_copy_attr_times(inode);
7445 +
7446 +       flush_dcache_page(page);
7447 +
7448 +out:
7449 +       if (err == 0)
7450 +               SetPageUptodate(page);
7451 +       else
7452 +               ClearPageUptodate(page);
7453 +
7454 +       return err;
7455 +}
7456 +
7457 +static int unionfs_readpage(struct file *file, struct page *page)
7458 +{
7459 +       int err;
7460 +
7461 +       unionfs_read_lock(file->f_path.dentry->d_sb);
7462 +       err = unionfs_file_revalidate(file, false);
7463 +       if (unlikely(err))
7464 +               goto out;
7465 +       unionfs_check_file(file);
7466 +
7467 +       err = unionfs_do_readpage(file, page);
7468 +
7469 +       if (!err) {
7470 +               touch_atime(unionfs_lower_mnt(file->f_path.dentry),
7471 +                           unionfs_lower_dentry(file->f_path.dentry));
7472 +               unionfs_copy_attr_times(file->f_path.dentry->d_inode);
7473 +       }
7474 +
7475 +       /*
7476 +        * we have to unlock our page, b/c we _might_ have gotten a locked
7477 +        * page.  but we no longer have to wakeup on our page here, b/c
7478 +        * UnlockPage does it
7479 +        */
7480 +out:
7481 +       unlock_page(page);
7482 +       unionfs_check_file(file);
7483 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
7484 +
7485 +       return err;
7486 +}
7487 +
7488 +static int unionfs_prepare_write(struct file *file, struct page *page,
7489 +                                unsigned from, unsigned to)
7490 +{
7491 +       int err;
7492 +
7493 +       unionfs_read_lock(file->f_path.dentry->d_sb);
7494 +       /*
7495 +        * This is the only place where we unconditionally copy the lower
7496 +        * attribute times before calling unionfs_file_revalidate.  The
7497 +        * reason is that our ->write calls do_sync_write which in turn will
7498 +        * call our ->prepare_write and then ->commit_write.  Before our
7499 +        * ->write is called, the lower mtimes are in sync, but by the time
7500 +        * the VFS calls our ->commit_write, the lower mtimes have changed.
7501 +        * Therefore, the only reasonable time for us to sync up from the
7502 +        * changed lower mtimes, and avoid an invariant violation warning,
7503 +        * is here, in ->prepare_write.
7504 +        */
7505 +       unionfs_copy_attr_times(file->f_path.dentry->d_inode);
7506 +       err = unionfs_file_revalidate(file, true);
7507 +       unionfs_check_file(file);
7508 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
7509 +
7510 +       return err;
7511 +}
7512 +
7513 +static int unionfs_commit_write(struct file *file, struct page *page,
7514 +                               unsigned from, unsigned to)
7515 +{
7516 +       int err = -ENOMEM;
7517 +       struct inode *inode, *lower_inode;
7518 +       struct file *lower_file = NULL;
7519 +       loff_t pos;
7520 +       unsigned bytes = to - from;
7521 +       char *page_data = NULL;
7522 +       mm_segment_t old_fs;
7523 +
7524 +       BUG_ON(file == NULL);
7525 +
7526 +       unionfs_read_lock(file->f_path.dentry->d_sb);
7527 +       err = unionfs_file_revalidate(file, true);
7528 +       if (unlikely(err))
7529 +               goto out;
7530 +       unionfs_check_file(file);
7531 +
7532 +       inode = page->mapping->host;
7533 +       lower_inode = unionfs_lower_inode(inode);
7534 +
7535 +       if (UNIONFS_F(file) != NULL)
7536 +               lower_file = unionfs_lower_file(file);
7537 +
7538 +       /* FIXME: is this assertion right here? */
7539 +       BUG_ON(lower_file == NULL);
7540 +
7541 +       page_data = (char *)kmap(page);
7542 +       lower_file->f_pos = page_offset(page) + from;
7543 +
7544 +       /*
7545 +        * We use vfs_write instead of copying page data and the
7546 +        * prepare_write/commit_write combo because file system's like
7547 +        * GFS/OCFS2 don't like things touching those directly,
7548 +        * calling the underlying write op, while a little bit slower, will
7549 +        * call all the FS specific code as well
7550 +        */
7551 +       old_fs = get_fs();
7552 +       set_fs(KERNEL_DS);
7553 +       err = vfs_write(lower_file, page_data + from, bytes,
7554 +                       &lower_file->f_pos);
7555 +       set_fs(old_fs);
7556 +
7557 +       kunmap(page);
7558 +
7559 +       if (err < 0)
7560 +               goto out;
7561 +
7562 +       inode->i_blocks = lower_inode->i_blocks;
7563 +       /* we may have to update i_size */
7564 +       pos = page_offset(page) + to;
7565 +       if (pos > i_size_read(inode))
7566 +               i_size_write(inode, pos);
7567 +       /* if vfs_write succeeded above, sync up our times */
7568 +       unionfs_copy_attr_times(inode);
7569 +       mark_inode_dirty_sync(inode);
7570 +
7571 +out:
7572 +       if (err < 0)
7573 +               ClearPageUptodate(page);
7574 +
7575 +       unionfs_read_unlock(file->f_path.dentry->d_sb);
7576 +       unionfs_check_file(file);
7577 +       return err;             /* assume all is ok */
7578 +}
7579 +
7580 +static void unionfs_sync_page(struct page *page)
7581 +{
7582 +       struct inode *inode;
7583 +       struct inode *lower_inode;
7584 +       struct page *lower_page;
7585 +       struct address_space *mapping;
7586 +
7587 +       inode = page->mapping->host;
7588 +       lower_inode = unionfs_lower_inode(inode);
7589 +
7590 +       /*
7591 +        * Find lower page (returns a locked page).
7592 +        *
7593 +        * NOTE: we used to call grab_cache_page(), but that was unnecessary
7594 +        * as it would have tried to create a new lower page if it didn't
7595 +        * exist, leading to deadlocks.  All our sync_page method needs to
7596 +        * do is ensure that pending I/O gets done.
7597 +        */
7598 +       lower_page = find_lock_page(lower_inode->i_mapping, page->index);
7599 +       if (!lower_page) {
7600 +               printk(KERN_ERR "unionfs: find_lock_page failed\n");
7601 +               goto out;
7602 +       }
7603 +
7604 +       /* do the actual sync */
7605 +       mapping = lower_page->mapping;
7606 +       /*
7607 +        * XXX: can we optimize ala RAIF and set the lower page to be
7608 +        * discarded after a successful sync_page?
7609 +        */
7610 +       if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
7611 +               mapping->a_ops->sync_page(lower_page);
7612 +
7613 +       /* b/c find_lock_page locked it */
7614 +       unlock_page(lower_page);
7615 +       /* b/c find_lock_page increased refcnt */
7616 +       page_cache_release(lower_page);
7617 +
7618 +out:
7619 +       return;
7620 +}
7621 +
7622 +struct address_space_operations unionfs_aops = {
7623 +       .writepage      = unionfs_writepage,
7624 +       .readpage       = unionfs_readpage,
7625 +       .prepare_write  = unionfs_prepare_write,
7626 +       .commit_write   = unionfs_commit_write,
7627 +       .sync_page      = unionfs_sync_page,
7628 +};
7629 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7630 new file mode 100644
7631 index 0000000..0df5f52
7632 --- /dev/null
7633 +++ b/fs/unionfs/rdstate.c
7634 @@ -0,0 +1,282 @@
7635 +/*
7636 + * Copyright (c) 2003-2007 Erez Zadok
7637 + * Copyright (c) 2003-2006 Charles P. Wright
7638 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7639 + * Copyright (c) 2005-2006 Junjiro Okajima
7640 + * Copyright (c) 2005      Arun M. Krishnakumar
7641 + * Copyright (c) 2004-2006 David P. Quigley
7642 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7643 + * Copyright (c) 2003      Puja Gupta
7644 + * Copyright (c) 2003      Harikesavan Krishnan
7645 + * Copyright (c) 2003-2007 Stony Brook University
7646 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
7647 + *
7648 + * This program is free software; you can redistribute it and/or modify
7649 + * it under the terms of the GNU General Public License version 2 as
7650 + * published by the Free Software Foundation.
7651 + */
7652 +
7653 +#include "union.h"
7654 +
7655 +/* This file contains the routines for maintaining readdir state. */
7656 +
7657 +/*
7658 + * There are two structures here, rdstate which is a hash table
7659 + * of the second structure which is a filldir_node.
7660 + */
7661 +
7662 +/*
7663 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7664 + * of them and they shouldn't waste memory.  If the node has a small name
7665 + * (as defined by the dentry structure), then we use an inline name to
7666 + * preserve kmalloc space.
7667 + */
7668 +static struct kmem_cache *unionfs_filldir_cachep;
7669 +
7670 +int unionfs_init_filldir_cache(void)
7671 +{
7672 +       unionfs_filldir_cachep =
7673 +               kmem_cache_create("unionfs_filldir",
7674 +                                 sizeof(struct filldir_node), 0,
7675 +                                 SLAB_RECLAIM_ACCOUNT, NULL);
7676 +
7677 +       return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7678 +}
7679 +
7680 +void unionfs_destroy_filldir_cache(void)
7681 +{
7682 +       if (unionfs_filldir_cachep)
7683 +               kmem_cache_destroy(unionfs_filldir_cachep);
7684 +}
7685 +
7686 +/*
7687 + * This is a tuning parameter that tells us roughly how big to make the
7688 + * hash table in directory entries per page.  This isn't perfect, but
7689 + * at least we get a hash table size that shouldn't be too overloaded.
7690 + * The following averages are based on my home directory.
7691 + * 14.44693    Overall
7692 + * 12.29       Single Page Directories
7693 + * 117.93      Multi-page directories
7694 + */
7695 +#define DENTPAGE 4096
7696 +#define DENTPERONEPAGE 12
7697 +#define DENTPERPAGE 118
7698 +#define MINHASHSIZE 1
7699 +static int guesstimate_hash_size(struct inode *inode)
7700 +{
7701 +       struct inode *lower_inode;
7702 +       int bindex;
7703 +       int hashsize = MINHASHSIZE;
7704 +
7705 +       if (UNIONFS_I(inode)->hashsize > 0)
7706 +               return UNIONFS_I(inode)->hashsize;
7707 +
7708 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7709 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
7710 +               if (!lower_inode)
7711 +                       continue;
7712 +
7713 +               if (lower_inode->i_size == DENTPAGE)
7714 +                       hashsize += DENTPERONEPAGE;
7715 +               else
7716 +                       hashsize += (lower_inode->i_size / DENTPAGE) *
7717 +                               DENTPERPAGE;
7718 +       }
7719 +
7720 +       return hashsize;
7721 +}
7722 +
7723 +int init_rdstate(struct file *file)
7724 +{
7725 +       BUG_ON(sizeof(loff_t) !=
7726 +              (sizeof(unsigned int) + sizeof(unsigned int)));
7727 +       BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7728 +
7729 +       UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7730 +                                                fbstart(file));
7731 +
7732 +       return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7733 +}
7734 +
7735 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7736 +{
7737 +       struct unionfs_dir_state *rdstate = NULL;
7738 +       struct list_head *pos;
7739 +
7740 +       spin_lock(&UNIONFS_I(inode)->rdlock);
7741 +       list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7742 +               struct unionfs_dir_state *r =
7743 +                       list_entry(pos, struct unionfs_dir_state, cache);
7744 +               if (fpos == rdstate2offset(r)) {
7745 +                       UNIONFS_I(inode)->rdcount--;
7746 +                       list_del(&r->cache);
7747 +                       rdstate = r;
7748 +                       break;
7749 +               }
7750 +       }
7751 +       spin_unlock(&UNIONFS_I(inode)->rdlock);
7752 +       return rdstate;
7753 +}
7754 +
7755 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7756 +{
7757 +       int i = 0;
7758 +       int hashsize;
7759 +       unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7760 +       struct unionfs_dir_state *rdstate;
7761 +
7762 +       hashsize = guesstimate_hash_size(inode);
7763 +       mallocsize += hashsize * sizeof(struct list_head);
7764 +       mallocsize = __roundup_pow_of_two(mallocsize);
7765 +
7766 +       /* This should give us about 500 entries anyway. */
7767 +       if (mallocsize > PAGE_SIZE)
7768 +               mallocsize = PAGE_SIZE;
7769 +
7770 +       hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7771 +               sizeof(struct list_head);
7772 +
7773 +       rdstate = kmalloc(mallocsize, GFP_KERNEL);
7774 +       if (unlikely(!rdstate))
7775 +               return NULL;
7776 +
7777 +       spin_lock(&UNIONFS_I(inode)->rdlock);
7778 +       if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7779 +               UNIONFS_I(inode)->cookie = 1;
7780 +       else
7781 +               UNIONFS_I(inode)->cookie++;
7782 +
7783 +       rdstate->cookie = UNIONFS_I(inode)->cookie;
7784 +       spin_unlock(&UNIONFS_I(inode)->rdlock);
7785 +       rdstate->offset = 1;
7786 +       rdstate->access = jiffies;
7787 +       rdstate->bindex = bindex;
7788 +       rdstate->dirpos = 0;
7789 +       rdstate->hashentries = 0;
7790 +       rdstate->size = hashsize;
7791 +       for (i = 0; i < rdstate->size; i++)
7792 +               INIT_LIST_HEAD(&rdstate->list[i]);
7793 +
7794 +       return rdstate;
7795 +}
7796 +
7797 +static void free_filldir_node(struct filldir_node *node)
7798 +{
7799 +       if (node->namelen >= DNAME_INLINE_LEN_MIN)
7800 +               kfree(node->name);
7801 +       kmem_cache_free(unionfs_filldir_cachep, node);
7802 +}
7803 +
7804 +void free_rdstate(struct unionfs_dir_state *state)
7805 +{
7806 +       struct filldir_node *tmp;
7807 +       int i;
7808 +
7809 +       for (i = 0; i < state->size; i++) {
7810 +               struct list_head *head = &(state->list[i]);
7811 +               struct list_head *pos, *n;
7812 +
7813 +               /* traverse the list and deallocate space */
7814 +               list_for_each_safe(pos, n, head) {
7815 +                       tmp = list_entry(pos, struct filldir_node, file_list);
7816 +                       list_del(&tmp->file_list);
7817 +                       free_filldir_node(tmp);
7818 +               }
7819 +       }
7820 +
7821 +       kfree(state);
7822 +}
7823 +
7824 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7825 +                                      const char *name, int namelen)
7826 +{
7827 +       int index;
7828 +       unsigned int hash;
7829 +       struct list_head *head;
7830 +       struct list_head *pos;
7831 +       struct filldir_node *cursor = NULL;
7832 +       int found = 0;
7833 +
7834 +       BUG_ON(namelen <= 0);
7835 +
7836 +       hash = full_name_hash(name, namelen);
7837 +       index = hash % rdstate->size;
7838 +
7839 +       head = &(rdstate->list[index]);
7840 +       list_for_each(pos, head) {
7841 +               cursor = list_entry(pos, struct filldir_node, file_list);
7842 +
7843 +               if (cursor->namelen == namelen && cursor->hash == hash &&
7844 +                   !strncmp(cursor->name, name, namelen)) {
7845 +                       /*
7846 +                        * a duplicate exists, and hence no need to create
7847 +                        * entry to the list
7848 +                        */
7849 +                       found = 1;
7850 +
7851 +                       /*
7852 +                        * if the duplicate is in this branch, then the file
7853 +                        * system is corrupted.
7854 +                        */
7855 +                       if (unlikely(cursor->bindex == rdstate->bindex))
7856 +                               printk(KERN_ERR "unionfs: filldir: possible "
7857 +                                      "I/O error: a file is duplicated "
7858 +                                      "in the same branch %d: %s\n",
7859 +                                      rdstate->bindex, cursor->name);
7860 +                       break;
7861 +               }
7862 +       }
7863 +
7864 +       if (!found)
7865 +               cursor = NULL;
7866 +
7867 +       return cursor;
7868 +}
7869 +
7870 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7871 +                    int namelen, int bindex, int whiteout)
7872 +{
7873 +       struct filldir_node *new;
7874 +       unsigned int hash;
7875 +       int index;
7876 +       int err = 0;
7877 +       struct list_head *head;
7878 +
7879 +       BUG_ON(namelen <= 0);
7880 +
7881 +       hash = full_name_hash(name, namelen);
7882 +       index = hash % rdstate->size;
7883 +       head = &(rdstate->list[index]);
7884 +
7885 +       new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7886 +       if (unlikely(!new)) {
7887 +               err = -ENOMEM;
7888 +               goto out;
7889 +       }
7890 +
7891 +       INIT_LIST_HEAD(&new->file_list);
7892 +       new->namelen = namelen;
7893 +       new->hash = hash;
7894 +       new->bindex = bindex;
7895 +       new->whiteout = whiteout;
7896 +
7897 +       if (namelen < DNAME_INLINE_LEN_MIN) {
7898 +               new->name = new->iname;
7899 +       } else {
7900 +               new->name = kmalloc(namelen + 1, GFP_KERNEL);
7901 +               if (unlikely(!new->name)) {
7902 +                       kmem_cache_free(unionfs_filldir_cachep, new);
7903 +                       new = NULL;
7904 +                       goto out;
7905 +               }
7906 +       }
7907 +
7908 +       memcpy(new->name, name, namelen);
7909 +       new->name[namelen] = '\0';
7910 +
7911 +       rdstate->hashentries++;
7912 +
7913 +       list_add(&(new->file_list), head);
7914 +out:
7915 +       return err;
7916 +}
7917 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7918 new file mode 100644
7919 index 0000000..91d41d4
7920 --- /dev/null
7921 +++ b/fs/unionfs/rename.c
7922 @@ -0,0 +1,531 @@
7923 +/*
7924 + * Copyright (c) 2003-2007 Erez Zadok
7925 + * Copyright (c) 2003-2006 Charles P. Wright
7926 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7927 + * Copyright (c) 2005-2006 Junjiro Okajima
7928 + * Copyright (c) 2005      Arun M. Krishnakumar
7929 + * Copyright (c) 2004-2006 David P. Quigley
7930 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7931 + * Copyright (c) 2003      Puja Gupta
7932 + * Copyright (c) 2003      Harikesavan Krishnan
7933 + * Copyright (c) 2003-2007 Stony Brook University
7934 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
7935 + *
7936 + * This program is free software; you can redistribute it and/or modify
7937 + * it under the terms of the GNU General Public License version 2 as
7938 + * published by the Free Software Foundation.
7939 + */
7940 +
7941 +#include "union.h"
7942 +
7943 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7944 +                           struct inode *new_dir, struct dentry *new_dentry,
7945 +                           int bindex, struct dentry **wh_old)
7946 +{
7947 +       int err = 0;
7948 +       struct dentry *lower_old_dentry;
7949 +       struct dentry *lower_new_dentry;
7950 +       struct dentry *lower_old_dir_dentry;
7951 +       struct dentry *lower_new_dir_dentry;
7952 +       struct dentry *lower_wh_dentry;
7953 +       struct dentry *lower_wh_dir_dentry;
7954 +       char *wh_name = NULL;
7955 +
7956 +       lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7957 +       lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7958 +
7959 +       if (!lower_new_dentry) {
7960 +               lower_new_dentry =
7961 +                       create_parents(new_dentry->d_parent->d_inode,
7962 +                                      new_dentry, new_dentry->d_name.name,
7963 +                                      bindex);
7964 +               if (IS_ERR(lower_new_dentry)) {
7965 +                       printk(KERN_ERR "unionfs: error creating directory "
7966 +                              "tree for rename, bindex = %d, err = %ld\n",
7967 +                              bindex, PTR_ERR(lower_new_dentry));
7968 +                       err = PTR_ERR(lower_new_dentry);
7969 +                       goto out;
7970 +               }
7971 +       }
7972 +
7973 +       wh_name = alloc_whname(new_dentry->d_name.name,
7974 +                              new_dentry->d_name.len);
7975 +       if (unlikely(IS_ERR(wh_name))) {
7976 +               err = PTR_ERR(wh_name);
7977 +               goto out;
7978 +       }
7979 +
7980 +       lower_wh_dentry = lookup_one_len(wh_name, lower_new_dentry->d_parent,
7981 +                                        new_dentry->d_name.len +
7982 +                                        UNIONFS_WHLEN);
7983 +       if (IS_ERR(lower_wh_dentry)) {
7984 +               err = PTR_ERR(lower_wh_dentry);
7985 +               goto out;
7986 +       }
7987 +
7988 +       if (lower_wh_dentry->d_inode) {
7989 +               /* get rid of the whiteout that is existing */
7990 +               if (lower_new_dentry->d_inode) {
7991 +                       printk(KERN_ERR "unionfs: both a whiteout and a "
7992 +                              "dentry exist when doing a rename!\n");
7993 +                       err = -EIO;
7994 +
7995 +                       dput(lower_wh_dentry);
7996 +                       goto out;
7997 +               }
7998 +
7999 +               lower_wh_dir_dentry = lock_parent(lower_wh_dentry);
8000 +               err = is_robranch_super(old_dentry->d_sb, bindex);
8001 +               if (!err)
8002 +                       err = vfs_unlink(lower_wh_dir_dentry->d_inode,
8003 +                                        lower_wh_dentry);
8004 +
8005 +               dput(lower_wh_dentry);
8006 +               unlock_dir(lower_wh_dir_dentry);
8007 +               if (err)
8008 +                       goto out;
8009 +       } else {
8010 +               dput(lower_wh_dentry);
8011 +       }
8012 +
8013 +       dget(lower_old_dentry);
8014 +       lower_old_dir_dentry = dget_parent(lower_old_dentry);
8015 +       lower_new_dir_dentry = dget_parent(lower_new_dentry);
8016 +
8017 +       lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
8018 +
8019 +       err = is_robranch_super(old_dentry->d_sb, bindex);
8020 +       if (err)
8021 +               goto out_unlock;
8022 +
8023 +       /*
8024 +        * ready to whiteout for old_dentry. caller will create the actual
8025 +        * whiteout, and must dput(*wh_old)
8026 +        */
8027 +       if (wh_old) {
8028 +               char *whname;
8029 +               whname = alloc_whname(old_dentry->d_name.name,
8030 +                                     old_dentry->d_name.len);
8031 +               err = PTR_ERR(whname);
8032 +               if (unlikely(IS_ERR(whname)))
8033 +                       goto out_unlock;
8034 +               *wh_old = lookup_one_len(whname, lower_old_dir_dentry,
8035 +                                        old_dentry->d_name.len +
8036 +                                        UNIONFS_WHLEN);
8037 +               kfree(whname);
8038 +               err = PTR_ERR(*wh_old);
8039 +               if (IS_ERR(*wh_old)) {
8040 +                       *wh_old = NULL;
8041 +                       goto out_unlock;
8042 +               }
8043 +       }
8044 +
8045 +       err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
8046 +                        lower_new_dir_dentry->d_inode, lower_new_dentry);
8047 +
8048 +out_unlock:
8049 +       unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
8050 +
8051 +       dput(lower_old_dir_dentry);
8052 +       dput(lower_new_dir_dentry);
8053 +       dput(lower_old_dentry);
8054 +
8055 +out:
8056 +       if (!err) {
8057 +               /* Fixup the new_dentry. */
8058 +               if (bindex < dbstart(new_dentry))
8059 +                       set_dbstart(new_dentry, bindex);
8060 +               else if (bindex > dbend(new_dentry))
8061 +                       set_dbend(new_dentry, bindex);
8062 +       }
8063 +
8064 +       kfree(wh_name);
8065 +
8066 +       return err;
8067 +}
8068 +
8069 +/*
8070 + * Main rename code.  This is sufficiently complex, that it's documented in
8071 + * Documentation/filesystems/unionfs/rename.txt.  This routine calls
8072 + * __unionfs_rename() above to perform some of the work.
8073 + */
8074 +static int do_unionfs_rename(struct inode *old_dir,
8075 +                            struct dentry *old_dentry,
8076 +                            struct inode *new_dir,
8077 +                            struct dentry *new_dentry)
8078 +{
8079 +       int err = 0;
8080 +       int bindex, bwh_old;
8081 +       int old_bstart, old_bend;
8082 +       int new_bstart, new_bend;
8083 +       int do_copyup = -1;
8084 +       struct dentry *parent_dentry;
8085 +       int local_err = 0;
8086 +       int eio = 0;
8087 +       int revert = 0;
8088 +       struct dentry *wh_old = NULL;
8089 +
8090 +       old_bstart = dbstart(old_dentry);
8091 +       bwh_old = old_bstart;
8092 +       old_bend = dbend(old_dentry);
8093 +       parent_dentry = old_dentry->d_parent;
8094 +
8095 +       new_bstart = dbstart(new_dentry);
8096 +       new_bend = dbend(new_dentry);
8097 +
8098 +       /* Rename source to destination. */
8099 +       err = __unionfs_rename(old_dir, old_dentry, new_dir, new_dentry,
8100 +                              old_bstart, &wh_old);
8101 +       if (err) {
8102 +               if (!IS_COPYUP_ERR(err))
8103 +                       goto out;
8104 +               do_copyup = old_bstart - 1;
8105 +       } else {
8106 +               revert = 1;
8107 +       }
8108 +
8109 +       /*
8110 +        * Unlink all instances of destination that exist to the left of
8111 +        * bstart of source. On error, revert back, goto out.
8112 +        */
8113 +       for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
8114 +               struct dentry *unlink_dentry;
8115 +               struct dentry *unlink_dir_dentry;
8116 +
8117 +               unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
8118 +               if (!unlink_dentry)
8119 +                       continue;
8120 +
8121 +               unlink_dir_dentry = lock_parent(unlink_dentry);
8122 +               err = is_robranch_super(old_dir->i_sb, bindex);
8123 +               if (!err)
8124 +                       err = vfs_unlink(unlink_dir_dentry->d_inode,
8125 +                                        unlink_dentry);
8126 +
8127 +               fsstack_copy_attr_times(new_dentry->d_parent->d_inode,
8128 +                                       unlink_dir_dentry->d_inode);
8129 +               /* propagate number of hard-links */
8130 +               new_dentry->d_parent->d_inode->i_nlink =
8131 +                       unionfs_get_nlinks(new_dentry->d_parent->d_inode);
8132 +
8133 +               unlock_dir(unlink_dir_dentry);
8134 +               if (!err) {
8135 +                       if (bindex != new_bstart) {
8136 +                               dput(unlink_dentry);
8137 +                               unionfs_set_lower_dentry_idx(new_dentry,
8138 +                                                            bindex, NULL);
8139 +                       }
8140 +               } else if (IS_COPYUP_ERR(err)) {
8141 +                       do_copyup = bindex - 1;
8142 +               } else if (revert) {
8143 +                       dput(wh_old);
8144 +                       goto revert;
8145 +               }
8146 +       }
8147 +
8148 +       if (do_copyup != -1) {
8149 +               for (bindex = do_copyup; bindex >= 0; bindex--) {
8150 +                       /*
8151 +                        * copyup the file into some left directory, so that
8152 +                        * you can rename it
8153 +                        */
8154 +                       err = copyup_dentry(old_dentry->d_parent->d_inode,
8155 +                                           old_dentry, old_bstart, bindex,
8156 +                                           old_dentry->d_name.name,
8157 +                                           old_dentry->d_name.len,
8158 +                                           NULL, old_dentry->d_inode->i_size);
8159 +                       /* if copyup failed, try next branch to the left */
8160 +                       if (err)
8161 +                               continue;
8162 +                       dput(wh_old);
8163 +                       bwh_old = bindex;
8164 +                       err = __unionfs_rename(old_dir, old_dentry,
8165 +                                              new_dir, new_dentry,
8166 +                                              bindex, &wh_old);
8167 +                       break;
8168 +               }
8169 +       }
8170 +
8171 +       /* make it opaque */
8172 +       if (S_ISDIR(old_dentry->d_inode->i_mode)) {
8173 +               err = make_dir_opaque(old_dentry, dbstart(old_dentry));
8174 +               if (err)
8175 +                       goto revert;
8176 +       }
8177 +
8178 +       /*
8179 +        * Create whiteout for source, only if:
8180 +        * (1) There is more than one underlying instance of source.
8181 +        * (2) We did a copy_up
8182 +        */
8183 +       if ((old_bstart != old_bend) || (do_copyup != -1)) {
8184 +               struct dentry *lower_parent;
8185 +               struct nameidata nd;
8186 +               if (!wh_old || wh_old->d_inode || bwh_old < 0) {
8187 +                       printk(KERN_ERR "unionfs: rename error "
8188 +                              "(wh_old=%p/%p bwh_old=%d)\n", wh_old,
8189 +                              (wh_old ? wh_old->d_inode : NULL), bwh_old);
8190 +                       err = -EIO;
8191 +                       goto out;
8192 +               }
8193 +               err = init_lower_nd(&nd, LOOKUP_CREATE);
8194 +               if (unlikely(err < 0))
8195 +                       goto out;
8196 +               lower_parent = lock_parent(wh_old);
8197 +               local_err = vfs_create(lower_parent->d_inode, wh_old, S_IRUGO,
8198 +                                      &nd);
8199 +               unlock_dir(lower_parent);
8200 +               if (!local_err) {
8201 +                       set_dbopaque(old_dentry, bwh_old);
8202 +               } else {
8203 +                       /*
8204 +                        * we can't fix anything now, so we cop-out and use
8205 +                        * -EIO.
8206 +                        */
8207 +                       printk(KERN_ERR "unionfs: can't create a whiteout for "
8208 +                              "the source in rename!\n");
8209 +                       err = -EIO;
8210 +               }
8211 +               release_lower_nd(&nd, local_err);
8212 +       }
8213 +
8214 +out:
8215 +       dput(wh_old);
8216 +       return err;
8217 +
8218 +revert:
8219 +       /* Do revert here. */
8220 +       local_err = unionfs_refresh_lower_dentry(new_dentry, old_bstart);
8221 +       if (local_err) {
8222 +               printk(KERN_ERR "unionfs: revert failed in rename: "
8223 +                      "the new refresh failed\n");
8224 +               eio = -EIO;
8225 +       }
8226 +
8227 +       local_err = unionfs_refresh_lower_dentry(old_dentry, old_bstart);
8228 +       if (local_err) {
8229 +               printk(KERN_ERR "unionfs: revert failed in rename: "
8230 +                      "the old refresh failed\n");
8231 +               eio = -EIO;
8232 +               goto revert_out;
8233 +       }
8234 +
8235 +       if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
8236 +           !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
8237 +               printk(KERN_ERR "unionfs: revert failed in rename: "
8238 +                      "the object disappeared from under us!\n");
8239 +               eio = -EIO;
8240 +               goto revert_out;
8241 +       }
8242 +
8243 +       if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
8244 +           unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
8245 +               printk(KERN_ERR "unionfs: revert failed in rename: "
8246 +                      "the object was created underneath us!\n");
8247 +               eio = -EIO;
8248 +               goto revert_out;
8249 +       }
8250 +
8251 +       local_err = __unionfs_rename(new_dir, new_dentry,
8252 +                                    old_dir, old_dentry, old_bstart, NULL);
8253 +
8254 +       /* If we can't fix it, then we cop-out with -EIO. */
8255 +       if (local_err) {
8256 +               printk(KERN_ERR "unionfs: revert failed in rename!\n");
8257 +               eio = -EIO;
8258 +       }
8259 +
8260 +       local_err = unionfs_refresh_lower_dentry(new_dentry, bindex);
8261 +       if (local_err)
8262 +               eio = -EIO;
8263 +       local_err = unionfs_refresh_lower_dentry(old_dentry, bindex);
8264 +       if (local_err)
8265 +               eio = -EIO;
8266 +
8267 +revert_out:
8268 +       if (eio)
8269 +               err = eio;
8270 +       return err;
8271 +}
8272 +
8273 +static struct dentry *lookup_whiteout(struct dentry *dentry)
8274 +{
8275 +       char *whname;
8276 +       int bindex = -1, bstart = -1, bend = -1;
8277 +       struct dentry *parent, *lower_parent, *wh_dentry;
8278 +
8279 +       whname = alloc_whname(dentry->d_name.name, dentry->d_name.len);
8280 +       if (unlikely(IS_ERR(whname)))
8281 +               return (void *)whname;
8282 +
8283 +       parent = dget_parent(dentry);
8284 +       unionfs_lock_dentry(parent);
8285 +       bstart = dbstart(parent);
8286 +       bend = dbend(parent);
8287 +       wh_dentry = ERR_PTR(-ENOENT);
8288 +       for (bindex = bstart; bindex <= bend; bindex++) {
8289 +               lower_parent = unionfs_lower_dentry_idx(parent, bindex);
8290 +               if (!lower_parent)
8291 +                       continue;
8292 +               wh_dentry = lookup_one_len(whname, lower_parent,
8293 +                                          dentry->d_name.len + UNIONFS_WHLEN);
8294 +               if (IS_ERR(wh_dentry))
8295 +                       continue;
8296 +               if (wh_dentry->d_inode)
8297 +                       break;
8298 +               dput(wh_dentry);
8299 +               wh_dentry = ERR_PTR(-ENOENT);
8300 +       }
8301 +       unionfs_unlock_dentry(parent);
8302 +       dput(parent);
8303 +       kfree(whname);
8304 +       return wh_dentry;
8305 +}
8306 +
8307 +/*
8308 + * We can't copyup a directory, because it may involve huge numbers of
8309 + * children, etc.  Doing that in the kernel would be bad, so instead we
8310 + * return EXDEV to the user-space utility that caused this, and let the
8311 + * user-space recurse and ask us to copy up each file separately.
8312 + */
8313 +static int may_rename_dir(struct dentry *dentry)
8314 +{
8315 +       int err, bstart;
8316 +
8317 +       err = check_empty(dentry, NULL);
8318 +       if (err == -ENOTEMPTY) {
8319 +               if (is_robranch(dentry))
8320 +                       return -EXDEV;
8321 +       } else if (err) {
8322 +               return err;
8323 +       }
8324 +
8325 +       bstart = dbstart(dentry);
8326 +       if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8327 +               return 0;
8328 +
8329 +       set_dbstart(dentry, bstart + 1);
8330 +       err = check_empty(dentry, NULL);
8331 +       set_dbstart(dentry, bstart);
8332 +       if (err == -ENOTEMPTY)
8333 +               err = -EXDEV;
8334 +       return err;
8335 +}
8336 +
8337 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8338 +                  struct inode *new_dir, struct dentry *new_dentry)
8339 +{
8340 +       int err = 0;
8341 +       struct dentry *wh_dentry;
8342 +
8343 +       unionfs_read_lock(old_dentry->d_sb);
8344 +       unionfs_double_lock_dentry(old_dentry, new_dentry);
8345 +
8346 +       if (unlikely(!__unionfs_d_revalidate_chain(old_dentry, NULL, false))) {
8347 +               err = -ESTALE;
8348 +               goto out;
8349 +       }
8350 +       if (unlikely(!d_deleted(new_dentry) && new_dentry->d_inode &&
8351 +                    !__unionfs_d_revalidate_chain(new_dentry, NULL, false))) {
8352 +               err = -ESTALE;
8353 +               goto out;
8354 +       }
8355 +
8356 +       if (!S_ISDIR(old_dentry->d_inode->i_mode))
8357 +               err = unionfs_partial_lookup(old_dentry);
8358 +       else
8359 +               err = may_rename_dir(old_dentry);
8360 +
8361 +       if (err)
8362 +               goto out;
8363 +
8364 +       err = unionfs_partial_lookup(new_dentry);
8365 +       if (err)
8366 +               goto out;
8367 +
8368 +       /*
8369 +        * if new_dentry is already lower because of whiteout,
8370 +        * simply override it even if the whited-out dir is not empty.
8371 +        */
8372 +       wh_dentry = lookup_whiteout(new_dentry);
8373 +       if (!IS_ERR(wh_dentry)) {
8374 +               dput(wh_dentry);
8375 +       } else if (new_dentry->d_inode) {
8376 +               if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8377 +                   S_ISDIR(new_dentry->d_inode->i_mode)) {
8378 +                       err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8379 +                               -ENOTDIR : -EISDIR;
8380 +                       goto out;
8381 +               }
8382 +
8383 +               if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8384 +                       struct unionfs_dir_state *namelist;
8385 +                       /* check if this unionfs directory is empty or not */
8386 +                       err = check_empty(new_dentry, &namelist);
8387 +                       if (err)
8388 +                               goto out;
8389 +
8390 +                       if (!is_robranch(new_dentry))
8391 +                               err = delete_whiteouts(new_dentry,
8392 +                                                      dbstart(new_dentry),
8393 +                                                      namelist);
8394 +
8395 +                       free_rdstate(namelist);
8396 +
8397 +                       if (err)
8398 +                               goto out;
8399 +               }
8400 +       }
8401 +       err = do_unionfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8402 +out:
8403 +       if (err) {
8404 +               /* clear the new_dentry stuff created */
8405 +               d_drop(new_dentry);
8406 +       } else {
8407 +               /*
8408 +                * force re-lookup since the dir on ro branch is not renamed,
8409 +                * and lower dentries still indicate the un-renamed ones.
8410 +                */
8411 +               if (S_ISDIR(old_dentry->d_inode->i_mode))
8412 +                       atomic_dec(&UNIONFS_D(old_dentry)->generation);
8413 +               else
8414 +                       unionfs_postcopyup_release(old_dentry);
8415 +               if (new_dentry->d_inode &&
8416 +                   !S_ISDIR(new_dentry->d_inode->i_mode)) {
8417 +                       unionfs_postcopyup_release(new_dentry);
8418 +                       unionfs_postcopyup_setmnt(new_dentry);
8419 +                       if (!unionfs_lower_inode(new_dentry->d_inode)) {
8420 +                               /*
8421 +                                * If we get here, it means that no copyup
8422 +                                * was needed, and that a file by the old
8423 +                                * name already existing on the destination
8424 +                                * branch; that file got renamed earlier in
8425 +                                * this function, so all we need to do here
8426 +                                * is set the lower inode.
8427 +                                */
8428 +                               struct inode *inode;
8429 +                               inode = unionfs_lower_inode(
8430 +                                       old_dentry->d_inode);
8431 +                               igrab(inode);
8432 +                               unionfs_set_lower_inode_idx(
8433 +                                       new_dentry->d_inode,
8434 +                                       dbstart(new_dentry), inode);
8435 +                       }
8436 +
8437 +               }
8438 +               /* if all of this renaming succeeded, update our times */
8439 +               unionfs_copy_attr_times(old_dir);
8440 +               unionfs_copy_attr_times(new_dir);
8441 +               unionfs_copy_attr_times(old_dentry->d_inode);
8442 +               unionfs_copy_attr_times(new_dentry->d_inode);
8443 +               unionfs_check_inode(old_dir);
8444 +               unionfs_check_inode(new_dir);
8445 +               unionfs_check_dentry(old_dentry);
8446 +               unionfs_check_dentry(new_dentry);
8447 +       }
8448 +
8449 +       unionfs_unlock_dentry(new_dentry);
8450 +       unionfs_unlock_dentry(old_dentry);
8451 +       unionfs_read_unlock(old_dentry->d_sb);
8452 +       return err;
8453 +}
8454 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8455 new file mode 100644
8456 index 0000000..2a8c88e
8457 --- /dev/null
8458 +++ b/fs/unionfs/sioq.c
8459 @@ -0,0 +1,119 @@
8460 +/*
8461 + * Copyright (c) 2006-2007 Erez Zadok
8462 + * Copyright (c) 2006      Charles P. Wright
8463 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8464 + * Copyright (c) 2006      Junjiro Okajima
8465 + * Copyright (c) 2006      David P. Quigley
8466 + * Copyright (c) 2006-2007 Stony Brook University
8467 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
8468 + *
8469 + * This program is free software; you can redistribute it and/or modify
8470 + * it under the terms of the GNU General Public License version 2 as
8471 + * published by the Free Software Foundation.
8472 + */
8473 +
8474 +#include "union.h"
8475 +
8476 +/*
8477 + * Super-user IO work Queue - sometimes we need to perform actions which
8478 + * would fail due to the unix permissions on the parent directory (e.g.,
8479 + * rmdir a directory which appears empty, but in reality contains
8480 + * whiteouts).
8481 + */
8482 +
8483 +static struct workqueue_struct *superio_workqueue;
8484 +
8485 +int __init init_sioq(void)
8486 +{
8487 +       int err;
8488 +
8489 +       superio_workqueue = create_workqueue("unionfs_siod");
8490 +       if (!IS_ERR(superio_workqueue))
8491 +               return 0;
8492 +
8493 +       err = PTR_ERR(superio_workqueue);
8494 +       printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8495 +       superio_workqueue = NULL;
8496 +       return err;
8497 +}
8498 +
8499 +void stop_sioq(void)
8500 +{
8501 +       if (superio_workqueue)
8502 +               destroy_workqueue(superio_workqueue);
8503 +}
8504 +
8505 +void run_sioq(work_func_t func, struct sioq_args *args)
8506 +{
8507 +       INIT_WORK(&args->work, func);
8508 +
8509 +       init_completion(&args->comp);
8510 +       while (!queue_work(superio_workqueue, &args->work)) {
8511 +               /* TODO: do accounting if needed */
8512 +               schedule();
8513 +       }
8514 +       wait_for_completion(&args->comp);
8515 +}
8516 +
8517 +void __unionfs_create(struct work_struct *work)
8518 +{
8519 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8520 +       struct create_args *c = &args->create;
8521 +
8522 +       args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8523 +       complete(&args->comp);
8524 +}
8525 +
8526 +void __unionfs_mkdir(struct work_struct *work)
8527 +{
8528 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8529 +       struct mkdir_args *m = &args->mkdir;
8530 +
8531 +       args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8532 +       complete(&args->comp);
8533 +}
8534 +
8535 +void __unionfs_mknod(struct work_struct *work)
8536 +{
8537 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8538 +       struct mknod_args *m = &args->mknod;
8539 +
8540 +       args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8541 +       complete(&args->comp);
8542 +}
8543 +
8544 +void __unionfs_symlink(struct work_struct *work)
8545 +{
8546 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8547 +       struct symlink_args *s = &args->symlink;
8548 +
8549 +       args->err = vfs_symlink(s->parent, s->dentry, s->symbuf, s->mode);
8550 +       complete(&args->comp);
8551 +}
8552 +
8553 +void __unionfs_unlink(struct work_struct *work)
8554 +{
8555 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8556 +       struct unlink_args *u = &args->unlink;
8557 +
8558 +       args->err = vfs_unlink(u->parent, u->dentry);
8559 +       complete(&args->comp);
8560 +}
8561 +
8562 +void __delete_whiteouts(struct work_struct *work)
8563 +{
8564 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8565 +       struct deletewh_args *d = &args->deletewh;
8566 +
8567 +       args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
8568 +       complete(&args->comp);
8569 +}
8570 +
8571 +void __is_opaque_dir(struct work_struct *work)
8572 +{
8573 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8574 +
8575 +       args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
8576 +                                  sizeof(UNIONFS_DIR_OPAQUE) - 1);
8577 +       complete(&args->comp);
8578 +}
8579 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8580 new file mode 100644
8581 index 0000000..afb71ee
8582 --- /dev/null
8583 +++ b/fs/unionfs/sioq.h
8584 @@ -0,0 +1,92 @@
8585 +/*
8586 + * Copyright (c) 2006-2007 Erez Zadok
8587 + * Copyright (c) 2006      Charles P. Wright
8588 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8589 + * Copyright (c) 2006      Junjiro Okajima
8590 + * Copyright (c) 2006      David P. Quigley
8591 + * Copyright (c) 2006-2007 Stony Brook University
8592 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
8593 + *
8594 + * This program is free software; you can redistribute it and/or modify
8595 + * it under the terms of the GNU General Public License version 2 as
8596 + * published by the Free Software Foundation.
8597 + */
8598 +
8599 +#ifndef _SIOQ_H
8600 +#define _SIOQ_H
8601 +
8602 +struct deletewh_args {
8603 +       struct unionfs_dir_state *namelist;
8604 +       struct dentry *dentry;
8605 +       int bindex;
8606 +};
8607 +
8608 +struct is_opaque_args {
8609 +       struct dentry *dentry;
8610 +};
8611 +
8612 +struct create_args {
8613 +       struct inode *parent;
8614 +       struct dentry *dentry;
8615 +       umode_t mode;
8616 +       struct nameidata *nd;
8617 +};
8618 +
8619 +struct mkdir_args {
8620 +       struct inode *parent;
8621 +       struct dentry *dentry;
8622 +       umode_t mode;
8623 +};
8624 +
8625 +struct mknod_args {
8626 +       struct inode *parent;
8627 +       struct dentry *dentry;
8628 +       umode_t mode;
8629 +       dev_t dev;
8630 +};
8631 +
8632 +struct symlink_args {
8633 +       struct inode *parent;
8634 +       struct dentry *dentry;
8635 +       char *symbuf;
8636 +       umode_t mode;
8637 +};
8638 +
8639 +struct unlink_args {
8640 +       struct inode *parent;
8641 +       struct dentry *dentry;
8642 +};
8643 +
8644 +
8645 +struct sioq_args {
8646 +       struct completion comp;
8647 +       struct work_struct work;
8648 +       int err;
8649 +       void *ret;
8650 +
8651 +       union {
8652 +               struct deletewh_args deletewh;
8653 +               struct is_opaque_args is_opaque;
8654 +               struct create_args create;
8655 +               struct mkdir_args mkdir;
8656 +               struct mknod_args mknod;
8657 +               struct symlink_args symlink;
8658 +               struct unlink_args unlink;
8659 +       };
8660 +};
8661 +
8662 +/* Extern definitions for SIOQ functions */
8663 +extern int __init init_sioq(void);
8664 +extern void stop_sioq(void);
8665 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8666 +
8667 +/* Extern definitions for our privilege escalation helpers */
8668 +extern void __unionfs_create(struct work_struct *work);
8669 +extern void __unionfs_mkdir(struct work_struct *work);
8670 +extern void __unionfs_mknod(struct work_struct *work);
8671 +extern void __unionfs_symlink(struct work_struct *work);
8672 +extern void __unionfs_unlink(struct work_struct *work);
8673 +extern void __delete_whiteouts(struct work_struct *work);
8674 +extern void __is_opaque_dir(struct work_struct *work);
8675 +
8676 +#endif /* not _SIOQ_H */
8677 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8678 new file mode 100644
8679 index 0000000..d97086a
8680 --- /dev/null
8681 +++ b/fs/unionfs/subr.c
8682 @@ -0,0 +1,224 @@
8683 +/*
8684 + * Copyright (c) 2003-2007 Erez Zadok
8685 + * Copyright (c) 2003-2006 Charles P. Wright
8686 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8687 + * Copyright (c) 2005-2006 Junjiro Okajima
8688 + * Copyright (c) 2005      Arun M. Krishnakumar
8689 + * Copyright (c) 2004-2006 David P. Quigley
8690 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8691 + * Copyright (c) 2003      Puja Gupta
8692 + * Copyright (c) 2003      Harikesavan Krishnan
8693 + * Copyright (c) 2003-2007 Stony Brook University
8694 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
8695 + *
8696 + * This program is free software; you can redistribute it and/or modify
8697 + * it under the terms of the GNU General Public License version 2 as
8698 + * published by the Free Software Foundation.
8699 + */
8700 +
8701 +#include "union.h"
8702 +
8703 +/*
8704 + * Pass an unionfs dentry and an index.  It will try to create a whiteout
8705 + * for the filename in dentry, and will try in branch 'index'.  On error,
8706 + * it will proceed to a branch to the left.
8707 + */
8708 +int create_whiteout(struct dentry *dentry, int start)
8709 +{
8710 +       int bstart, bend, bindex;
8711 +       struct dentry *lower_dir_dentry;
8712 +       struct dentry *lower_dentry;
8713 +       struct dentry *lower_wh_dentry;
8714 +       struct nameidata nd;
8715 +       char *name = NULL;
8716 +       int err = -EINVAL;
8717 +
8718 +       verify_locked(dentry);
8719 +
8720 +       bstart = dbstart(dentry);
8721 +       bend = dbend(dentry);
8722 +
8723 +       /* create dentry's whiteout equivalent */
8724 +       name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
8725 +       if (unlikely(IS_ERR(name))) {
8726 +               err = PTR_ERR(name);
8727 +               goto out;
8728 +       }
8729 +
8730 +       for (bindex = start; bindex >= 0; bindex--) {
8731 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
8732 +
8733 +               if (!lower_dentry) {
8734 +                       /*
8735 +                        * if lower dentry is not present, create the
8736 +                        * entire lower dentry directory structure and go
8737 +                        * ahead.  Since we want to just create whiteout, we
8738 +                        * only want the parent dentry, and hence get rid of
8739 +                        * this dentry.
8740 +                        */
8741 +                       lower_dentry = create_parents(dentry->d_inode,
8742 +                                                     dentry,
8743 +                                                     dentry->d_name.name,
8744 +                                                     bindex);
8745 +                       if (!lower_dentry || IS_ERR(lower_dentry)) {
8746 +                               printk(KERN_ERR "unionfs: create_parents "
8747 +                                      "failed for bindex = %d\n", bindex);
8748 +                               continue;
8749 +                       }
8750 +               }
8751 +
8752 +               lower_wh_dentry =
8753 +                       lookup_one_len(name, lower_dentry->d_parent,
8754 +                                      dentry->d_name.len + UNIONFS_WHLEN);
8755 +               if (IS_ERR(lower_wh_dentry))
8756 +                       continue;
8757 +
8758 +               /*
8759 +                * The whiteout already exists. This used to be impossible,
8760 +                * but now is possible because of opaqueness.
8761 +                */
8762 +               if (lower_wh_dentry->d_inode) {
8763 +                       dput(lower_wh_dentry);
8764 +                       err = 0;
8765 +                       goto out;
8766 +               }
8767 +
8768 +               err = init_lower_nd(&nd, LOOKUP_CREATE);
8769 +               if (unlikely(err < 0))
8770 +                       goto out;
8771 +               lower_dir_dentry = lock_parent(lower_wh_dentry);
8772 +               err = is_robranch_super(dentry->d_sb, bindex);
8773 +               if (!err)
8774 +                       err = vfs_create(lower_dir_dentry->d_inode,
8775 +                                        lower_wh_dentry,
8776 +                                        ~current->fs->umask & S_IRWXUGO,
8777 +                                        &nd);
8778 +               unlock_dir(lower_dir_dentry);
8779 +               dput(lower_wh_dentry);
8780 +               release_lower_nd(&nd, err);
8781 +
8782 +               if (!err || !IS_COPYUP_ERR(err))
8783 +                       break;
8784 +       }
8785 +
8786 +       /* set dbopaque so that lookup will not proceed after this branch */
8787 +       if (!err)
8788 +               set_dbopaque(dentry, bindex);
8789 +
8790 +out:
8791 +       kfree(name);
8792 +       return err;
8793 +}
8794 +
8795 +/*
8796 + * This is a helper function for rename, which ends up with hosed over
8797 + * dentries when it needs to revert.
8798 + */
8799 +int unionfs_refresh_lower_dentry(struct dentry *dentry, int bindex)
8800 +{
8801 +       struct dentry *lower_dentry;
8802 +       struct dentry *lower_parent;
8803 +       int err = 0;
8804 +
8805 +       verify_locked(dentry);
8806 +
8807 +       unionfs_lock_dentry(dentry->d_parent);
8808 +       lower_parent = unionfs_lower_dentry_idx(dentry->d_parent, bindex);
8809 +       unionfs_unlock_dentry(dentry->d_parent);
8810 +
8811 +       BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
8812 +
8813 +       lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
8814 +                                     dentry->d_name.len);
8815 +       if (IS_ERR(lower_dentry)) {
8816 +               err = PTR_ERR(lower_dentry);
8817 +               goto out;
8818 +       }
8819 +
8820 +       dput(unionfs_lower_dentry_idx(dentry, bindex));
8821 +       iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
8822 +       unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
8823 +
8824 +       if (!lower_dentry->d_inode) {
8825 +               dput(lower_dentry);
8826 +               unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
8827 +       } else {
8828 +               unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
8829 +               unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
8830 +                                           igrab(lower_dentry->d_inode));
8831 +       }
8832 +
8833 +out:
8834 +       return err;
8835 +}
8836 +
8837 +int make_dir_opaque(struct dentry *dentry, int bindex)
8838 +{
8839 +       int err = 0;
8840 +       struct dentry *lower_dentry, *diropq;
8841 +       struct inode *lower_dir;
8842 +       struct nameidata nd;
8843 +
8844 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
8845 +       lower_dir = lower_dentry->d_inode;
8846 +       BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
8847 +              !S_ISDIR(lower_dir->i_mode));
8848 +
8849 +       mutex_lock(&lower_dir->i_mutex);
8850 +       diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
8851 +                               sizeof(UNIONFS_DIR_OPAQUE) - 1);
8852 +       if (IS_ERR(diropq)) {
8853 +               err = PTR_ERR(diropq);
8854 +               goto out;
8855 +       }
8856 +
8857 +       err = init_lower_nd(&nd, LOOKUP_CREATE);
8858 +       if (unlikely(err < 0))
8859 +               goto out;
8860 +       if (!diropq->d_inode)
8861 +               err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
8862 +       if (!err)
8863 +               set_dbopaque(dentry, bindex);
8864 +       release_lower_nd(&nd, err);
8865 +
8866 +       dput(diropq);
8867 +
8868 +out:
8869 +       mutex_unlock(&lower_dir->i_mutex);
8870 +       return err;
8871 +}
8872 +
8873 +/*
8874 + * returns the right n_link value based on the inode type
8875 + */
8876 +int unionfs_get_nlinks(const struct inode *inode)
8877 +{
8878 +       /* don't bother to do all the work since we're unlinked */
8879 +       if (inode->i_nlink == 0)
8880 +               return 0;
8881 +
8882 +       if (!S_ISDIR(inode->i_mode))
8883 +               return unionfs_lower_inode(inode)->i_nlink;
8884 +
8885 +       /*
8886 +        * For directories, we return 1. The only place that could cares
8887 +        * about links is readdir, and there's d_type there so even that
8888 +        * doesn't matter.
8889 +        */
8890 +       return 1;
8891 +}
8892 +
8893 +/* construct whiteout filename */
8894 +char *alloc_whname(const char *name, int len)
8895 +{
8896 +       char *buf;
8897 +
8898 +       buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
8899 +       if (unlikely(!buf))
8900 +               return ERR_PTR(-ENOMEM);
8901 +
8902 +       strcpy(buf, UNIONFS_WHPFX);
8903 +       strlcat(buf, name, len + UNIONFS_WHLEN + 1);
8904 +
8905 +       return buf;
8906 +}
8907 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8908 new file mode 100644
8909 index 0000000..515689d
8910 --- /dev/null
8911 +++ b/fs/unionfs/super.c
8912 @@ -0,0 +1,1016 @@
8913 +/*
8914 + * Copyright (c) 2003-2007 Erez Zadok
8915 + * Copyright (c) 2003-2006 Charles P. Wright
8916 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8917 + * Copyright (c) 2005-2006 Junjiro Okajima
8918 + * Copyright (c) 2005      Arun M. Krishnakumar
8919 + * Copyright (c) 2004-2006 David P. Quigley
8920 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8921 + * Copyright (c) 2003      Puja Gupta
8922 + * Copyright (c) 2003      Harikesavan Krishnan
8923 + * Copyright (c) 2003-2007 Stony Brook University
8924 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
8925 + *
8926 + * This program is free software; you can redistribute it and/or modify
8927 + * it under the terms of the GNU General Public License version 2 as
8928 + * published by the Free Software Foundation.
8929 + */
8930 +
8931 +#include "union.h"
8932 +
8933 +/*
8934 + * The inode cache is used with alloc_inode for both our inode info and the
8935 + * vfs inode.
8936 + */
8937 +static struct kmem_cache *unionfs_inode_cachep;
8938 +
8939 +static void unionfs_read_inode(struct inode *inode)
8940 +{
8941 +       int size;
8942 +       struct unionfs_inode_info *info = UNIONFS_I(inode);
8943 +
8944 +       unionfs_read_lock(inode->i_sb);
8945 +
8946 +       memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8947 +       info->bstart = -1;
8948 +       info->bend = -1;
8949 +       atomic_set(&info->generation,
8950 +                  atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8951 +       spin_lock_init(&info->rdlock);
8952 +       info->rdcount = 1;
8953 +       info->hashsize = -1;
8954 +       INIT_LIST_HEAD(&info->readdircache);
8955 +
8956 +       size = sbmax(inode->i_sb) * sizeof(struct inode *);
8957 +       info->lower_inodes = kzalloc(size, GFP_KERNEL);
8958 +       if (unlikely(!info->lower_inodes)) {
8959 +               printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8960 +                      "lower-pointer array!\n");
8961 +               BUG();
8962 +       }
8963 +
8964 +       inode->i_version++;
8965 +       inode->i_op = &unionfs_main_iops;
8966 +       inode->i_fop = &unionfs_main_fops;
8967 +
8968 +       inode->i_mapping->a_ops = &unionfs_aops;
8969 +
8970 +       unionfs_read_unlock(inode->i_sb);
8971 +}
8972 +
8973 +/*
8974 + * we now define delete_inode, because there are two VFS paths that may
8975 + * destroy an inode: one of them calls clear inode before doing everything
8976 + * else that's needed, and the other is fine.  This way we truncate the inode
8977 + * size (and its pages) and then clear our own inode, which will do an iput
8978 + * on our and the lower inode.
8979 + *
8980 + * No need to lock sb info's rwsem.
8981 + */
8982 +static void unionfs_delete_inode(struct inode *inode)
8983 +{
8984 +       inode->i_size = 0;      /* every f/s seems to do that */
8985 +
8986 +       if (inode->i_data.nrpages)
8987 +               truncate_inode_pages(&inode->i_data, 0);
8988 +
8989 +       clear_inode(inode);
8990 +}
8991 +
8992 +/*
8993 + * final actions when unmounting a file system
8994 + *
8995 + * No need to lock rwsem.
8996 + */
8997 +static void unionfs_put_super(struct super_block *sb)
8998 +{
8999 +       int bindex, bstart, bend;
9000 +       struct unionfs_sb_info *spd;
9001 +       int leaks = 0;
9002 +
9003 +       spd = UNIONFS_SB(sb);
9004 +       if (!spd)
9005 +               return;
9006 +
9007 +       bstart = sbstart(sb);
9008 +       bend = sbend(sb);
9009 +
9010 +       /* Make sure we have no leaks of branchget/branchput. */
9011 +       for (bindex = bstart; bindex <= bend; bindex++)
9012 +               if (unlikely(branch_count(sb, bindex) != 0)) {
9013 +                       printk(KERN_CRIT
9014 +                              "unionfs: branch %d has %d references left!\n",
9015 +                              bindex, branch_count(sb, bindex));
9016 +                       leaks = 1;
9017 +               }
9018 +       BUG_ON(leaks != 0);
9019 +
9020 +       kfree(spd->data);
9021 +       kfree(spd);
9022 +       sb->s_fs_info = NULL;
9023 +}
9024 +
9025 +/*
9026 + * Since people use this to answer the "How big of a file can I write?"
9027 + * question, we report the size of the highest priority branch as the size of
9028 + * the union.
9029 + */
9030 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
9031 +{
9032 +       int err = 0;
9033 +       struct super_block *sb;
9034 +       struct dentry *lower_dentry;
9035 +
9036 +       sb = dentry->d_sb;
9037 +
9038 +       unionfs_read_lock(sb);
9039 +       unionfs_lock_dentry(dentry);
9040 +
9041 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
9042 +               err = -ESTALE;
9043 +               goto out;
9044 +       }
9045 +       unionfs_check_dentry(dentry);
9046 +
9047 +       lower_dentry = unionfs_lower_dentry(sb->s_root);
9048 +       err = vfs_statfs(lower_dentry, buf);
9049 +
9050 +       /* set return buf to our f/s to avoid confusing user-level utils */
9051 +       buf->f_type = UNIONFS_SUPER_MAGIC;
9052 +       /*
9053 +        * Our maximum file name can is shorter by a few bytes because every
9054 +        * file name could potentially be whited-out.
9055 +        *
9056 +        * XXX: this restriction goes away with ODF.
9057 +        */
9058 +       buf->f_namelen -= UNIONFS_WHLEN;
9059 +
9060 +       /*
9061 +        * reset two fields to avoid confusing user-land.
9062 +        * XXX: is this still necessary?
9063 +        */
9064 +       memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
9065 +       memset(&buf->f_spare, 0, sizeof(buf->f_spare));
9066 +
9067 +out:
9068 +       unionfs_unlock_dentry(dentry);
9069 +       unionfs_check_dentry(dentry);
9070 +       unionfs_read_unlock(sb);
9071 +       return err;
9072 +}
9073 +
9074 +/* handle mode changing during remount */
9075 +static noinline int do_remount_mode_option(char *optarg, int cur_branches,
9076 +                                          struct unionfs_data *new_data,
9077 +                                          struct path *new_lower_paths)
9078 +{
9079 +       int err = -EINVAL;
9080 +       int perms, idx;
9081 +       char *modename = strchr(optarg, '=');
9082 +       struct nameidata nd;
9083 +
9084 +       /* by now, optarg contains the branch name */
9085 +       if (!*optarg) {
9086 +               printk(KERN_ERR
9087 +                      "unionfs: no branch specified for mode change\n");
9088 +               goto out;
9089 +       }
9090 +       if (!modename) {
9091 +               printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
9092 +                      optarg);
9093 +               goto out;
9094 +       }
9095 +       *modename++ = '\0';
9096 +       perms = __parse_branch_mode(modename);
9097 +       if (perms == 0) {
9098 +               printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
9099 +                      modename, optarg);
9100 +               goto out;
9101 +       }
9102 +
9103 +       /*
9104 +        * Find matching branch index.  For now, this assumes that nothing
9105 +        * has been mounted on top of this Unionfs stack.  Once we have /odf
9106 +        * and cache-coherency resolved, we'll address the branch-path
9107 +        * uniqueness.
9108 +        */
9109 +       err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
9110 +       if (err) {
9111 +               printk(KERN_ERR "unionfs: error accessing "
9112 +                      "lower directory \"%s\" (error %d)\n",
9113 +                      optarg, err);
9114 +               goto out;
9115 +       }
9116 +       for (idx = 0; idx < cur_branches; idx++)
9117 +               if (nd.mnt == new_lower_paths[idx].mnt &&
9118 +                   nd.dentry == new_lower_paths[idx].dentry)
9119 +                       break;
9120 +       path_release(&nd);      /* no longer needed */
9121 +       if (idx == cur_branches) {
9122 +               err = -ENOENT;  /* err may have been reset above */
9123 +               printk(KERN_ERR "unionfs: branch \"%s\" "
9124 +                      "not found\n", optarg);
9125 +               goto out;
9126 +       }
9127 +       /* check/change mode for existing branch */
9128 +       /* we don't warn if perms==branchperms */
9129 +       new_data[idx].branchperms = perms;
9130 +       err = 0;
9131 +out:
9132 +       return err;
9133 +}
9134 +
9135 +/* handle branch deletion during remount */
9136 +static noinline int do_remount_del_option(char *optarg, int cur_branches,
9137 +                                         struct unionfs_data *new_data,
9138 +                                         struct path *new_lower_paths)
9139 +{
9140 +       int err = -EINVAL;
9141 +       int idx;
9142 +       struct nameidata nd;
9143 +
9144 +       /* optarg contains the branch name to delete */
9145 +
9146 +       /*
9147 +        * Find matching branch index.  For now, this assumes that nothing
9148 +        * has been mounted on top of this Unionfs stack.  Once we have /odf
9149 +        * and cache-coherency resolved, we'll address the branch-path
9150 +        * uniqueness.
9151 +        */
9152 +       err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
9153 +       if (err) {
9154 +               printk(KERN_ERR "unionfs: error accessing "
9155 +                      "lower directory \"%s\" (error %d)\n",
9156 +                      optarg, err);
9157 +               goto out;
9158 +       }
9159 +       for (idx = 0; idx < cur_branches; idx++)
9160 +               if (nd.mnt == new_lower_paths[idx].mnt &&
9161 +                   nd.dentry == new_lower_paths[idx].dentry)
9162 +                       break;
9163 +       path_release(&nd);      /* no longer needed */
9164 +       if (idx == cur_branches) {
9165 +               printk(KERN_ERR "unionfs: branch \"%s\" "
9166 +                      "not found\n", optarg);
9167 +               err = -ENOENT;
9168 +               goto out;
9169 +       }
9170 +       /* check if there are any open files on the branch to be deleted */
9171 +       if (atomic_read(&new_data[idx].open_files) > 0) {
9172 +               err = -EBUSY;
9173 +               goto out;
9174 +       }
9175 +
9176 +       /*
9177 +        * Now we have to delete the branch.  First, release any handles it
9178 +        * has.  Then, move the remaining array indexes past "idx" in
9179 +        * new_data and new_lower_paths one to the left.  Finally, adjust
9180 +        * cur_branches.
9181 +        */
9182 +       pathput(&new_lower_paths[idx]);
9183 +
9184 +       if (idx < cur_branches - 1) {
9185 +               /* if idx==cur_branches-1, we delete last branch: easy */
9186 +               memmove(&new_data[idx], &new_data[idx+1],
9187 +                       (cur_branches - 1 - idx) *
9188 +                       sizeof(struct unionfs_data));
9189 +               memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
9190 +                       (cur_branches - 1 - idx) * sizeof(struct path));
9191 +       }
9192 +
9193 +       err = 0;
9194 +out:
9195 +       return err;
9196 +}
9197 +
9198 +/* handle branch insertion during remount */
9199 +static noinline int do_remount_add_option(char *optarg, int cur_branches,
9200 +                                         struct unionfs_data *new_data,
9201 +                                         struct path *new_lower_paths,
9202 +                                         int *high_branch_id)
9203 +{
9204 +       int err = -EINVAL;
9205 +       int perms;
9206 +       int idx = 0;            /* default: insert at beginning */
9207 +       char *new_branch , *modename = NULL;
9208 +       struct nameidata nd;
9209 +
9210 +       /*
9211 +        * optarg can be of several forms:
9212 +        *
9213 +        * /bar:/foo            insert /foo before /bar
9214 +        * /bar:/foo=ro         insert /foo in ro mode before /bar
9215 +        * /foo                 insert /foo in the beginning (prepend)
9216 +        * :/foo                insert /foo at the end (append)
9217 +        */
9218 +       if (*optarg == ':') {   /* append? */
9219 +               new_branch = optarg + 1; /* skip ':' */
9220 +               idx = cur_branches;
9221 +               goto found_insertion_point;
9222 +       }
9223 +       new_branch = strchr(optarg, ':');
9224 +       if (!new_branch) {      /* prepend? */
9225 +               new_branch = optarg;
9226 +               goto found_insertion_point;
9227 +       }
9228 +       *new_branch++ = '\0';   /* holds path+mode of new branch */
9229 +
9230 +       /*
9231 +        * Find matching branch index.  For now, this assumes that nothing
9232 +        * has been mounted on top of this Unionfs stack.  Once we have /odf
9233 +        * and cache-coherency resolved, we'll address the branch-path
9234 +        * uniqueness.
9235 +        */
9236 +       err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
9237 +       if (err) {
9238 +               printk(KERN_ERR "unionfs: error accessing "
9239 +                      "lower directory \"%s\" (error %d)\n",
9240 +                      optarg, err);
9241 +               goto out;
9242 +       }
9243 +       for (idx = 0; idx < cur_branches; idx++)
9244 +               if (nd.mnt == new_lower_paths[idx].mnt &&
9245 +                   nd.dentry == new_lower_paths[idx].dentry)
9246 +                       break;
9247 +       path_release(&nd);      /* no longer needed */
9248 +       if (idx == cur_branches) {
9249 +               printk(KERN_ERR "unionfs: branch \"%s\" "
9250 +                      "not found\n", optarg);
9251 +               err = -ENOENT;
9252 +               goto out;
9253 +       }
9254 +
9255 +       /*
9256 +        * At this point idx will hold the index where the new branch should
9257 +        * be inserted before.
9258 +        */
9259 +found_insertion_point:
9260 +       /* find the mode for the new branch */
9261 +       if (new_branch)
9262 +               modename = strchr(new_branch, '=');
9263 +       if (modename)
9264 +               *modename++ = '\0';
9265 +       perms = parse_branch_mode(modename);
9266 +
9267 +       if (!new_branch || !*new_branch) {
9268 +               printk(KERN_ERR "unionfs: null new branch\n");
9269 +               err = -EINVAL;
9270 +               goto out;
9271 +       }
9272 +       err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
9273 +       if (err) {
9274 +               printk(KERN_ERR "unionfs: error accessing "
9275 +                      "lower directory \"%s\" (error %d)\n",
9276 +                      new_branch, err);
9277 +               goto out;
9278 +       }
9279 +       /*
9280 +        * It's probably safe to check_mode the new branch to insert.  Note:
9281 +        * we don't allow inserting branches which are unionfs's by
9282 +        * themselves (check_branch returns EINVAL in that case).  This is
9283 +        * because this code base doesn't support stacking unionfs: the ODF
9284 +        * code base supports that correctly.
9285 +        */
9286 +       err = check_branch(&nd);
9287 +       if (err) {
9288 +               printk(KERN_ERR "unionfs: lower directory "
9289 +                      "\"%s\" is not a valid branch\n", optarg);
9290 +               path_release(&nd);
9291 +               goto out;
9292 +       }
9293 +
9294 +       /*
9295 +        * Now we have to insert the new branch.  But first, move the bits
9296 +        * to make space for the new branch, if needed.  Finally, adjust
9297 +        * cur_branches.
9298 +        * We don't release nd here; it's kept until umount/remount.
9299 +        */
9300 +       if (idx < cur_branches) {
9301 +               /* if idx==cur_branches, we append: easy */
9302 +               memmove(&new_data[idx+1], &new_data[idx],
9303 +                       (cur_branches - idx) * sizeof(struct unionfs_data));
9304 +               memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
9305 +                       (cur_branches - idx) * sizeof(struct path));
9306 +       }
9307 +       new_lower_paths[idx].dentry = nd.dentry;
9308 +       new_lower_paths[idx].mnt = nd.mnt;
9309 +
9310 +       new_data[idx].sb = nd.dentry->d_sb;
9311 +       atomic_set(&new_data[idx].open_files, 0);
9312 +       new_data[idx].branchperms = perms;
9313 +       new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
9314 +
9315 +       err = 0;
9316 +out:
9317 +       return err;
9318 +}
9319 +
9320 +
9321 +/*
9322 + * Support branch management options on remount.
9323 + *
9324 + * See Documentation/filesystems/unionfs/ for details.
9325 + *
9326 + * @flags: numeric mount options
9327 + * @options: mount options string
9328 + *
9329 + * This function can rearrange a mounted union dynamically, adding and
9330 + * removing branches, including changing branch modes.  Clearly this has to
9331 + * be done safely and atomically.  Luckily, the VFS already calls this
9332 + * function with lock_super(sb) and lock_kernel() held, preventing
9333 + * concurrent mixing of new mounts, remounts, and unmounts.  Moreover,
9334 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
9335 + * to purge dentries/inodes from our superblock, and also called
9336 + * fsync_super(sb) to purge any dirty pages.  So we're good.
9337 + *
9338 + * XXX: however, our remount code may also need to invalidate mapped pages
9339 + * so as to force them to be re-gotten from the (newly reconfigured) lower
9340 + * branches.  This has to wait for proper mmap and cache coherency support
9341 + * in the VFS.
9342 + *
9343 + */
9344 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
9345 +                             char *options)
9346 +{
9347 +       int err = 0;
9348 +       int i;
9349 +       char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
9350 +       char *optname;
9351 +       int cur_branches = 0;   /* no. of current branches */
9352 +       int new_branches = 0;   /* no. of branches actually left in the end */
9353 +       int add_branches;       /* est. no. of branches to add */
9354 +       int del_branches;       /* est. no. of branches to del */
9355 +       int max_branches;       /* max possible no. of branches */
9356 +       struct unionfs_data *new_data = NULL, *tmp_data = NULL;
9357 +       struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
9358 +       struct inode **new_lower_inodes = NULL;
9359 +       int new_high_branch_id; /* new high branch ID */
9360 +       int size;               /* memory allocation size, temp var */
9361 +       int old_ibstart, old_ibend;
9362 +
9363 +       unionfs_write_lock(sb);
9364 +
9365 +       /*
9366 +        * The VFS will take care of "ro" and "rw" flags, and we can safely
9367 +        * ignore MS_SILENT, but anything else left over is an error.  So we
9368 +        * need to check if any other flags may have been passed (none are
9369 +        * allowed/supported as of now).
9370 +        */
9371 +       if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
9372 +               printk(KERN_ERR
9373 +                      "unionfs: remount flags 0x%x unsupported\n", *flags);
9374 +               err = -EINVAL;
9375 +               goto out_error;
9376 +       }
9377 +
9378 +       /*
9379 +        * If 'options' is NULL, it's probably because the user just changed
9380 +        * the union to a "ro" or "rw" and the VFS took care of it.  So
9381 +        * nothing to do and we're done.
9382 +        */
9383 +       if (!options || options[0] == '\0')
9384 +               goto out_error;
9385 +
9386 +       /*
9387 +        * Find out how many branches we will have in the end, counting
9388 +        * "add" and "del" commands.  Copy the "options" string because
9389 +        * strsep modifies the string and we need it later.
9390 +        */
9391 +       tmp_to_free = kstrdup(options, GFP_KERNEL);
9392 +       optionstmp = tmp_to_free;
9393 +       if (unlikely(!optionstmp)) {
9394 +               err = -ENOMEM;
9395 +               goto out_free;
9396 +       }
9397 +       cur_branches = sbmax(sb); /* current no. branches */
9398 +       new_branches = sbmax(sb);
9399 +       del_branches = 0;
9400 +       add_branches = 0;
9401 +       new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9402 +       while ((optname = strsep(&optionstmp, ",")) != NULL) {
9403 +               char *optarg;
9404 +
9405 +               if (!optname || !*optname)
9406 +                       continue;
9407 +
9408 +               optarg = strchr(optname, '=');
9409 +               if (optarg)
9410 +                       *optarg++ = '\0';
9411 +
9412 +               if (!strcmp("add", optname))
9413 +                       add_branches++;
9414 +               else if (!strcmp("del", optname))
9415 +                       del_branches++;
9416 +       }
9417 +       kfree(tmp_to_free);
9418 +       /* after all changes, will we have at least one branch left? */
9419 +       if ((new_branches + add_branches - del_branches) < 1) {
9420 +               printk(KERN_ERR
9421 +                      "unionfs: no branches left after remount\n");
9422 +               err = -EINVAL;
9423 +               goto out_free;
9424 +       }
9425 +
9426 +       /*
9427 +        * Since we haven't actually parsed all the add/del options, nor
9428 +        * have we checked them for errors, we don't know for sure how many
9429 +        * branches we will have after all changes have taken place.  In
9430 +        * fact, the total number of branches left could be less than what
9431 +        * we have now.  So we need to allocate space for a temporary
9432 +        * placeholder that is at least as large as the maximum number of
9433 +        * branches we *could* have, which is the current number plus all
9434 +        * the additions.  Once we're done with these temp placeholders, we
9435 +        * may have to re-allocate the final size, copy over from the temp,
9436 +        * and then free the temps (done near the end of this function).
9437 +        */
9438 +       max_branches = cur_branches + add_branches;
9439 +       /* allocate space for new pointers to lower dentry */
9440 +       tmp_data = kcalloc(max_branches,
9441 +                          sizeof(struct unionfs_data), GFP_KERNEL);
9442 +       if (unlikely(!tmp_data)) {
9443 +               err = -ENOMEM;
9444 +               goto out_free;
9445 +       }
9446 +       /* allocate space for new pointers to lower paths */
9447 +       tmp_lower_paths = kcalloc(max_branches,
9448 +                                 sizeof(struct path), GFP_KERNEL);
9449 +       if (unlikely(!tmp_lower_paths)) {
9450 +               err = -ENOMEM;
9451 +               goto out_free;
9452 +       }
9453 +       /* copy current info into new placeholders, incrementing refcnts */
9454 +       memcpy(tmp_data, UNIONFS_SB(sb)->data,
9455 +              cur_branches * sizeof(struct unionfs_data));
9456 +       memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9457 +              cur_branches * sizeof(struct path));
9458 +       for (i = 0; i < cur_branches; i++)
9459 +               pathget(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9460 +
9461 +       /*******************************************************************
9462 +        * For each branch command, do path_lookup on the requested branch,
9463 +        * and apply the change to a temp branch list.  To handle errors, we
9464 +        * already dup'ed the old arrays (above), and increased the refcnts
9465 +        * on various f/s objects.  So now we can do all the path_lookups
9466 +        * and branch-management commands on the new arrays.  If it fail mid
9467 +        * way, we free the tmp arrays and *put all objects.  If we succeed,
9468 +        * then we free old arrays and *put its objects, and then replace
9469 +        * the arrays with the new tmp list (we may have to re-allocate the
9470 +        * memory because the temp lists could have been larger than what we
9471 +        * actually needed).
9472 +        *******************************************************************/
9473 +
9474 +       while ((optname = strsep(&options, ",")) != NULL) {
9475 +               char *optarg;
9476 +
9477 +               if (!optname || !*optname)
9478 +                       continue;
9479 +               /*
9480 +                * At this stage optname holds a comma-delimited option, but
9481 +                * without the commas.  Next, we need to break the string on
9482 +                * the '=' symbol to separate CMD=ARG, where ARG itself can
9483 +                * be KEY=VAL.  For example, in mode=/foo=rw, CMD is "mode",
9484 +                * KEY is "/foo", and VAL is "rw".
9485 +                */
9486 +               optarg = strchr(optname, '=');
9487 +               if (optarg)
9488 +                       *optarg++ = '\0';
9489 +               /* incgen remount option (instead of old ioctl) */
9490 +               if (!strcmp("incgen", optname)) {
9491 +                       err = 0;
9492 +                       goto out_no_change;
9493 +               }
9494 +
9495 +               /*
9496 +                * All of our options take an argument now.  (Insert ones
9497 +                * that don't above this check.)  So at this stage optname
9498 +                * contains the CMD part and optarg contains the ARG part.
9499 +                */
9500 +               if (!optarg || !*optarg) {
9501 +                       printk(KERN_ERR "unionfs: all remount options require "
9502 +                              "an argument (%s)\n", optname);
9503 +                       err = -EINVAL;
9504 +                       goto out_release;
9505 +               }
9506 +
9507 +               if (!strcmp("add", optname)) {
9508 +                       err = do_remount_add_option(optarg, new_branches,
9509 +                                                   tmp_data,
9510 +                                                   tmp_lower_paths,
9511 +                                                   &new_high_branch_id);
9512 +                       if (err)
9513 +                               goto out_release;
9514 +                       new_branches++;
9515 +                       if (new_branches > UNIONFS_MAX_BRANCHES) {
9516 +                               printk(KERN_ERR "unionfs: command exceeds "
9517 +                                      "%d branches\n", UNIONFS_MAX_BRANCHES);
9518 +                               err = -E2BIG;
9519 +                               goto out_release;
9520 +                       }
9521 +                       continue;
9522 +               }
9523 +               if (!strcmp("del", optname)) {
9524 +                       err = do_remount_del_option(optarg, new_branches,
9525 +                                                   tmp_data,
9526 +                                                   tmp_lower_paths);
9527 +                       if (err)
9528 +                               goto out_release;
9529 +                       new_branches--;
9530 +                       continue;
9531 +               }
9532 +               if (!strcmp("mode", optname)) {
9533 +                       err = do_remount_mode_option(optarg, new_branches,
9534 +                                                    tmp_data,
9535 +                                                    tmp_lower_paths);
9536 +                       if (err)
9537 +                               goto out_release;
9538 +                       continue;
9539 +               }
9540 +
9541 +               /*
9542 +                * When you use "mount -o remount,ro", mount(8) will
9543 +                * reportedly pass the original dirs= string from
9544 +                * /proc/mounts.  So for now, we have to ignore dirs= and
9545 +                * not consider it an error, unless we want to allow users
9546 +                * to pass dirs= in remount.  Note that to allow the VFS to
9547 +                * actually process the ro/rw remount options, we have to
9548 +                * return 0 from this function.
9549 +                */
9550 +               if (!strcmp("dirs", optname)) {
9551 +                       printk(KERN_WARNING
9552 +                              "unionfs: remount ignoring option \"%s\"\n",
9553 +                              optname);
9554 +                       continue;
9555 +               }
9556 +
9557 +               err = -EINVAL;
9558 +               printk(KERN_ERR
9559 +                      "unionfs: unrecognized option \"%s\"\n", optname);
9560 +               goto out_release;
9561 +       }
9562 +
9563 +out_no_change:
9564 +
9565 +       /******************************************************************
9566 +        * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9567 +        * see if we need to allocate a small-sized new vector, copy the
9568 +        * vectors to their correct place, release the refcnt of the older
9569 +        * ones, and return.  Also handle invalidating any pages that will
9570 +        * have to be re-read.
9571 +        *******************************************************************/
9572 +
9573 +       if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9574 +               printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9575 +                      "(use \"remount,ro\" to create a read-only union)\n");
9576 +               err = -EINVAL;
9577 +               goto out_release;
9578 +       }
9579 +
9580 +       /* (re)allocate space for new pointers to lower dentry */
9581 +       size = new_branches * sizeof(struct unionfs_data);
9582 +       new_data = krealloc(tmp_data, size, GFP_KERNEL);
9583 +       if (unlikely(!new_data)) {
9584 +               err = -ENOMEM;
9585 +               goto out_release;
9586 +       }
9587 +
9588 +       /* allocate space for new pointers to lower paths */
9589 +       size = new_branches * sizeof(struct path);
9590 +       new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9591 +       if (unlikely(!new_lower_paths)) {
9592 +               err = -ENOMEM;
9593 +               goto out_release;
9594 +       }
9595 +
9596 +       /* allocate space for new pointers to lower inodes */
9597 +       new_lower_inodes = kcalloc(new_branches,
9598 +                                  sizeof(struct inode *), GFP_KERNEL);
9599 +       if (unlikely(!new_lower_inodes)) {
9600 +               err = -ENOMEM;
9601 +               goto out_release;
9602 +       }
9603 +
9604 +       /*
9605 +        * OK, just before we actually put the new set of branches in place,
9606 +        * we need to ensure that our own f/s has no dirty objects left.
9607 +        * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9608 +        * fsync_super(sb), taking care of dentries, inodes, and dirty
9609 +        * pages.  So all that's left is for us to invalidate any leftover
9610 +        * (non-dirty) pages to ensure that they will be re-read from the
9611 +        * new lower branches (and to support mmap).
9612 +        */
9613 +
9614 +       /*
9615 +        * Now we call drop_pagecache_sb() to invalidate all pages in this
9616 +        * super.  This function calls invalidate_inode_pages(mapping),
9617 +        * which calls invalidate_mapping_pages(): the latter, however, will
9618 +        * not invalidate pages which are dirty, locked, under writeback, or
9619 +        * mapped into page tables.  We shouldn't have to worry about dirty
9620 +        * or under-writeback pages, because do_remount_sb() called
9621 +        * fsync_super() which would not have returned until all dirty pages
9622 +        * were flushed.
9623 +        *
9624 +        * But do we have to worry about locked pages?  Is there any chance
9625 +        * that in here we'll get locked pages?
9626 +        *
9627 +        * XXX: what about pages mapped into pagetables?  Are these pages
9628 +        * which user processes may have mmap(2)'ed?  If so, then we need to
9629 +        * invalidate those too, no?  Maybe we'll have to write our own
9630 +        * version of invalidate_mapping_pages() which also handled mapped
9631 +        * pages.
9632 +        *
9633 +        * XXX: Alternatively, maybe we should call truncate_inode_pages(),
9634 +        * which use two passes over the pages list, and will truncate all
9635 +        * pages.
9636 +        */
9637 +       drop_pagecache_sb(sb);
9638 +
9639 +       /* copy new vectors into their correct place */
9640 +       tmp_data = UNIONFS_SB(sb)->data;
9641 +       UNIONFS_SB(sb)->data = new_data;
9642 +       new_data = NULL;        /* so don't free good pointers below */
9643 +       tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9644 +       UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9645 +       new_lower_paths = NULL; /* so don't free good pointers below */
9646 +
9647 +       /* update our unionfs_sb_info and root dentry index of last branch */
9648 +       i = sbmax(sb);          /* save no. of branches to release at end */
9649 +       sbend(sb) = new_branches - 1;
9650 +       set_dbend(sb->s_root, new_branches - 1);
9651 +       old_ibstart = ibstart(sb->s_root->d_inode);
9652 +       old_ibend = ibend(sb->s_root->d_inode);
9653 +       ibend(sb->s_root->d_inode) = new_branches - 1;
9654 +       UNIONFS_D(sb->s_root)->bcount = new_branches;
9655 +       new_branches = i; /* no. of branches to release below */
9656 +
9657 +       /*
9658 +        * Update lower inodes: 3 steps
9659 +        * 1. grab ref on all new lower inodes
9660 +        */
9661 +       for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9662 +               struct dentry *lower_dentry =
9663 +                       unionfs_lower_dentry_idx(sb->s_root, i);
9664 +               igrab(lower_dentry->d_inode);
9665 +               new_lower_inodes[i] = lower_dentry->d_inode;
9666 +       }
9667 +       /* 2. release reference on all older lower inodes */
9668 +       for (i = old_ibstart; i <= old_ibend; i++) {
9669 +               iput(unionfs_lower_inode_idx(sb->s_root->d_inode, i));
9670 +               unionfs_set_lower_inode_idx(sb->s_root->d_inode, i, NULL);
9671 +       }
9672 +       kfree(UNIONFS_I(sb->s_root->d_inode)->lower_inodes);
9673 +       /* 3. update root dentry's inode to new lower_inodes array */
9674 +       UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9675 +       new_lower_inodes = NULL;
9676 +
9677 +       /* maxbytes may have changed */
9678 +       sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9679 +       /* update high branch ID */
9680 +       sbhbid(sb) = new_high_branch_id;
9681 +
9682 +       /* update our sb->generation for revalidating objects */
9683 +       i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9684 +       atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9685 +       atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9686 +       if (!(*flags & MS_SILENT))
9687 +               pr_info("unionfs: new generation number %d\n", i);
9688 +       /* finally, update the root dentry's times */
9689 +       unionfs_copy_attr_times(sb->s_root->d_inode);
9690 +       err = 0;                /* reset to success */
9691 +
9692 +       /*
9693 +        * The code above falls through to the next label, and releases the
9694 +        * refcnts of the older ones (stored in tmp_*): if we fell through
9695 +        * here, it means success.  However, if we jump directly to this
9696 +        * label from any error above, then an error occurred after we
9697 +        * grabbed various refcnts, and so we have to release the
9698 +        * temporarily constructed structures.
9699 +        */
9700 +out_release:
9701 +       /* no need to cleanup/release anything in tmp_data */
9702 +       if (tmp_lower_paths)
9703 +               for (i = 0; i < new_branches; i++)
9704 +                       pathput(&tmp_lower_paths[i]);
9705 +out_free:
9706 +       kfree(tmp_lower_paths);
9707 +       kfree(tmp_data);
9708 +       kfree(new_lower_paths);
9709 +       kfree(new_data);
9710 +       kfree(new_lower_inodes);
9711 +out_error:
9712 +       unionfs_write_unlock(sb);
9713 +       unionfs_check_dentry(sb->s_root);
9714 +       return err;
9715 +}
9716 +
9717 +/*
9718 + * Called by iput() when the inode reference count reached zero
9719 + * and the inode is not hashed anywhere.  Used to clear anything
9720 + * that needs to be, before the inode is completely destroyed and put
9721 + * on the inode free list.
9722 + *
9723 + * No need to lock sb info's rwsem.
9724 + */
9725 +static void unionfs_clear_inode(struct inode *inode)
9726 +{
9727 +       int bindex, bstart, bend;
9728 +       struct inode *lower_inode;
9729 +       struct list_head *pos, *n;
9730 +       struct unionfs_dir_state *rdstate;
9731 +
9732 +       list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9733 +               rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9734 +               list_del(&rdstate->cache);
9735 +               free_rdstate(rdstate);
9736 +       }
9737 +
9738 +       /*
9739 +        * Decrement a reference to a lower_inode, which was incremented
9740 +        * by our read_inode when it was created initially.
9741 +        */
9742 +       bstart = ibstart(inode);
9743 +       bend = ibend(inode);
9744 +       if (bstart >= 0) {
9745 +               for (bindex = bstart; bindex <= bend; bindex++) {
9746 +                       lower_inode = unionfs_lower_inode_idx(inode, bindex);
9747 +                       if (!lower_inode)
9748 +                               continue;
9749 +                       iput(lower_inode);
9750 +               }
9751 +       }
9752 +
9753 +       kfree(UNIONFS_I(inode)->lower_inodes);
9754 +       UNIONFS_I(inode)->lower_inodes = NULL;
9755 +}
9756 +
9757 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9758 +{
9759 +       struct unionfs_inode_info *i;
9760 +
9761 +       i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9762 +       if (unlikely(!i))
9763 +               return NULL;
9764 +
9765 +       /* memset everything up to the inode to 0 */
9766 +       memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9767 +
9768 +       i->vfs_inode.i_version = 1;
9769 +       return &i->vfs_inode;
9770 +}
9771 +
9772 +static void unionfs_destroy_inode(struct inode *inode)
9773 +{
9774 +       kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9775 +}
9776 +
9777 +/* unionfs inode cache constructor */
9778 +static void init_once(void *v, struct kmem_cache *cachep, unsigned long flags)
9779 +{
9780 +       struct unionfs_inode_info *i = v;
9781 +
9782 +       inode_init_once(&i->vfs_inode);
9783 +}
9784 +
9785 +int unionfs_init_inode_cache(void)
9786 +{
9787 +       int err = 0;
9788 +
9789 +       unionfs_inode_cachep =
9790 +               kmem_cache_create("unionfs_inode_cache",
9791 +                                 sizeof(struct unionfs_inode_info), 0,
9792 +                                 SLAB_RECLAIM_ACCOUNT, init_once);
9793 +       if (unlikely(!unionfs_inode_cachep))
9794 +               err = -ENOMEM;
9795 +       return err;
9796 +}
9797 +
9798 +/* unionfs inode cache destructor */
9799 +void unionfs_destroy_inode_cache(void)
9800 +{
9801 +       if (unionfs_inode_cachep)
9802 +               kmem_cache_destroy(unionfs_inode_cachep);
9803 +}
9804 +
9805 +/*
9806 + * Called when we have a dirty inode, right here we only throw out
9807 + * parts of our readdir list that are too old.
9808 + *
9809 + * No need to grab sb info's rwsem.
9810 + */
9811 +static int unionfs_write_inode(struct inode *inode, int sync)
9812 +{
9813 +       struct list_head *pos, *n;
9814 +       struct unionfs_dir_state *rdstate;
9815 +
9816 +       spin_lock(&UNIONFS_I(inode)->rdlock);
9817 +       list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9818 +               rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9819 +               /* We keep this list in LRU order. */
9820 +               if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9821 +                       break;
9822 +               UNIONFS_I(inode)->rdcount--;
9823 +               list_del(&rdstate->cache);
9824 +               free_rdstate(rdstate);
9825 +       }
9826 +       spin_unlock(&UNIONFS_I(inode)->rdlock);
9827 +
9828 +       return 0;
9829 +}
9830 +
9831 +/*
9832 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9833 + * code can actually succeed and won't leave tasks that need handling.
9834 + */
9835 +static void unionfs_umount_begin(struct vfsmount *mnt, int flags)
9836 +{
9837 +       struct super_block *sb, *lower_sb;
9838 +       struct vfsmount *lower_mnt;
9839 +       int bindex, bstart, bend;
9840 +
9841 +       if (!(flags & MNT_FORCE))
9842 +               /*
9843 +                * we are not being MNT_FORCE'd, therefore we should emulate
9844 +                * old behavior
9845 +                */
9846 +               return;
9847 +
9848 +       sb = mnt->mnt_sb;
9849 +
9850 +       unionfs_read_lock(sb);
9851 +
9852 +       bstart = sbstart(sb);
9853 +       bend = sbend(sb);
9854 +       for (bindex = bstart; bindex <= bend; bindex++) {
9855 +               lower_mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9856 +               lower_sb = unionfs_lower_super_idx(sb, bindex);
9857 +
9858 +               if (lower_mnt && lower_sb && lower_sb->s_op &&
9859 +                   lower_sb->s_op->umount_begin)
9860 +                       lower_sb->s_op->umount_begin(lower_mnt, flags);
9861 +       }
9862 +
9863 +       unionfs_read_unlock(sb);
9864 +}
9865 +
9866 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9867 +{
9868 +       struct super_block *sb = mnt->mnt_sb;
9869 +       int ret = 0;
9870 +       char *tmp_page;
9871 +       char *path;
9872 +       int bindex, bstart, bend;
9873 +       int perms;
9874 +
9875 +       unionfs_read_lock(sb);
9876 +
9877 +       unionfs_lock_dentry(sb->s_root);
9878 +
9879 +       tmp_page = (char *) __get_free_page(GFP_KERNEL);
9880 +       if (unlikely(!tmp_page)) {
9881 +               ret = -ENOMEM;
9882 +               goto out;
9883 +       }
9884 +
9885 +       bstart = sbstart(sb);
9886 +       bend = sbend(sb);
9887 +
9888 +       seq_printf(m, ",dirs=");
9889 +       for (bindex = bstart; bindex <= bend; bindex++) {
9890 +               path = d_path(unionfs_lower_dentry_idx(sb->s_root, bindex),
9891 +                             unionfs_lower_mnt_idx(sb->s_root, bindex),
9892 +                             tmp_page, PAGE_SIZE);
9893 +               if (IS_ERR(path)) {
9894 +                       ret = PTR_ERR(path);
9895 +                       goto out;
9896 +               }
9897 +
9898 +               perms = branchperms(sb, bindex);
9899 +
9900 +               seq_printf(m, "%s=%s", path,
9901 +                          perms & MAY_WRITE ? "rw" : "ro");
9902 +               if (bindex != bend)
9903 +                       seq_printf(m, ":");
9904 +       }
9905 +
9906 +out:
9907 +       free_page((unsigned long) tmp_page);
9908 +
9909 +       unionfs_unlock_dentry(sb->s_root);
9910 +
9911 +       unionfs_read_unlock(sb);
9912 +
9913 +       return ret;
9914 +}
9915 +
9916 +struct super_operations unionfs_sops = {
9917 +       .read_inode     = unionfs_read_inode,
9918 +       .delete_inode   = unionfs_delete_inode,
9919 +       .put_super      = unionfs_put_super,
9920 +       .statfs         = unionfs_statfs,
9921 +       .remount_fs     = unionfs_remount_fs,
9922 +       .clear_inode    = unionfs_clear_inode,
9923 +       .umount_begin   = unionfs_umount_begin,
9924 +       .show_options   = unionfs_show_options,
9925 +       .write_inode    = unionfs_write_inode,
9926 +       .alloc_inode    = unionfs_alloc_inode,
9927 +       .destroy_inode  = unionfs_destroy_inode,
9928 +};
9929 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9930 new file mode 100644
9931 index 0000000..22a4b91
9932 --- /dev/null
9933 +++ b/fs/unionfs/union.h
9934 @@ -0,0 +1,566 @@
9935 +/*
9936 + * Copyright (c) 2003-2007 Erez Zadok
9937 + * Copyright (c) 2003-2006 Charles P. Wright
9938 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9939 + * Copyright (c) 2005      Arun M. Krishnakumar
9940 + * Copyright (c) 2004-2006 David P. Quigley
9941 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9942 + * Copyright (c) 2003      Puja Gupta
9943 + * Copyright (c) 2003      Harikesavan Krishnan
9944 + * Copyright (c) 2003-2007 Stony Brook University
9945 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
9946 + *
9947 + * This program is free software; you can redistribute it and/or modify
9948 + * it under the terms of the GNU General Public License version 2 as
9949 + * published by the Free Software Foundation.
9950 + */
9951 +
9952 +#ifndef _UNION_H_
9953 +#define _UNION_H_
9954 +
9955 +#include <linux/dcache.h>
9956 +#include <linux/file.h>
9957 +#include <linux/list.h>
9958 +#include <linux/fs.h>
9959 +#include <linux/mm.h>
9960 +#include <linux/module.h>
9961 +#include <linux/mount.h>
9962 +#include <linux/namei.h>
9963 +#include <linux/page-flags.h>
9964 +#include <linux/pagemap.h>
9965 +#include <linux/poll.h>
9966 +#include <linux/security.h>
9967 +#include <linux/seq_file.h>
9968 +#include <linux/slab.h>
9969 +#include <linux/spinlock.h>
9970 +#include <linux/smp_lock.h>
9971 +#include <linux/statfs.h>
9972 +#include <linux/string.h>
9973 +#include <linux/vmalloc.h>
9974 +#include <linux/writeback.h>
9975 +#include <linux/buffer_head.h>
9976 +#include <linux/xattr.h>
9977 +#include <linux/fs_stack.h>
9978 +#include <linux/magic.h>
9979 +#include <linux/log2.h>
9980 +#include <linux/poison.h>
9981 +#include <linux/mman.h>
9982 +
9983 +#include <asm/system.h>
9984 +
9985 +#include <linux/union_fs.h>
9986 +
9987 +/* the file system name */
9988 +#define UNIONFS_NAME "unionfs"
9989 +
9990 +/* unionfs root inode number */
9991 +#define UNIONFS_ROOT_INO     1
9992 +
9993 +/* number of times we try to get a unique temporary file name */
9994 +#define GET_TMPNAM_MAX_RETRY   5
9995 +
9996 +/* maximum number of branches we support, to avoid memory blowup */
9997 +#define UNIONFS_MAX_BRANCHES   128
9998 +
9999 +/* Operations vectors defined in specific files. */
10000 +extern struct file_operations unionfs_main_fops;
10001 +extern struct file_operations unionfs_dir_fops;
10002 +extern struct inode_operations unionfs_main_iops;
10003 +extern struct inode_operations unionfs_dir_iops;
10004 +extern struct inode_operations unionfs_symlink_iops;
10005 +extern struct super_operations unionfs_sops;
10006 +extern struct dentry_operations unionfs_dops;
10007 +extern struct address_space_operations unionfs_aops;
10008 +
10009 +/* How long should an entry be allowed to persist */
10010 +#define RDCACHE_JIFFIES        (5*HZ)
10011 +
10012 +/* file private data. */
10013 +struct unionfs_file_info {
10014 +       int bstart;
10015 +       int bend;
10016 +       atomic_t generation;
10017 +
10018 +       struct unionfs_dir_state *rdstate;
10019 +       struct file **lower_files;
10020 +       int *saved_branch_ids; /* IDs of branches when file was opened */
10021 +};
10022 +
10023 +/* unionfs inode data in memory */
10024 +struct unionfs_inode_info {
10025 +       int bstart;
10026 +       int bend;
10027 +       atomic_t generation;
10028 +       int stale;
10029 +       /* Stuff for readdir over NFS. */
10030 +       spinlock_t rdlock;
10031 +       struct list_head readdircache;
10032 +       int rdcount;
10033 +       int hashsize;
10034 +       int cookie;
10035 +
10036 +       /* The lower inodes */
10037 +       struct inode **lower_inodes;
10038 +       /* to keep track of reads/writes for unlinks before closes */
10039 +       atomic_t totalopens;
10040 +
10041 +       struct inode vfs_inode;
10042 +};
10043 +
10044 +/* unionfs dentry data in memory */
10045 +struct unionfs_dentry_info {
10046 +       /*
10047 +        * The semaphore is used to lock the dentry as soon as we get into a
10048 +        * unionfs function from the VFS.  Our lock ordering is that children
10049 +        * go before their parents.
10050 +        */
10051 +       struct mutex lock;
10052 +       int bstart;
10053 +       int bend;
10054 +       int bopaque;
10055 +       int bcount;
10056 +       atomic_t generation;
10057 +       struct path *lower_paths;
10058 +};
10059 +
10060 +/* These are the pointers to our various objects. */
10061 +struct unionfs_data {
10062 +       struct super_block *sb;
10063 +       atomic_t open_files;    /* number of open files on branch */
10064 +       int branchperms;
10065 +       int branch_id;          /* unique branch ID at re/mount time */
10066 +};
10067 +
10068 +/* unionfs super-block data in memory */
10069 +struct unionfs_sb_info {
10070 +       int bend;
10071 +
10072 +       atomic_t generation;
10073 +
10074 +       /*
10075 +        * This rwsem is used to make sure that a branch management
10076 +        * operation...
10077 +        *   1) will not begin before all currently in-flight operations
10078 +        *      complete
10079 +        *   2) any new operations do not execute until the currently
10080 +        *      running branch management operation completes
10081 +        */
10082 +#ifdef CONFIG_PREEMPT_RT
10083 +       struct compat_rw_semaphore rwsem;
10084 +#else /* not CONFIG_PREEMPT_RT */
10085 +       struct rw_semaphore rwsem;
10086 +#endif /* not CONFIG_PREEMPT_RT */
10087 +       int high_branch_id;     /* last unique branch ID given */
10088 +       struct unionfs_data *data;
10089 +};
10090 +
10091 +/*
10092 + * structure for making the linked list of entries by readdir on left branch
10093 + * to compare with entries on right branch
10094 + */
10095 +struct filldir_node {
10096 +       struct list_head file_list;     /* list for directory entries */
10097 +       char *name;             /* name entry */
10098 +       int hash;               /* name hash */
10099 +       int namelen;            /* name len since name is not 0 terminated */
10100 +
10101 +       /*
10102 +        * we can check for duplicate whiteouts and files in the same branch
10103 +        * in order to return -EIO.
10104 +        */
10105 +       int bindex;
10106 +
10107 +       /* is this a whiteout entry? */
10108 +       int whiteout;
10109 +
10110 +       /* Inline name, so we don't need to separately kmalloc small ones */
10111 +       char iname[DNAME_INLINE_LEN_MIN];
10112 +};
10113 +
10114 +/* Directory hash table. */
10115 +struct unionfs_dir_state {
10116 +       unsigned int cookie;    /* the cookie, based off of rdversion */
10117 +       unsigned int offset;    /* The entry we have returned. */
10118 +       int bindex;
10119 +       loff_t dirpos;          /* offset within the lower level directory */
10120 +       int size;               /* How big is the hash table? */
10121 +       int hashentries;        /* How many entries have been inserted? */
10122 +       unsigned long access;
10123 +
10124 +       /* This cache list is used when the inode keeps us around. */
10125 +       struct list_head cache;
10126 +       struct list_head list[0];
10127 +};
10128 +
10129 +/* externs needed for fanout.h or sioq.h */
10130 +extern int unionfs_get_nlinks(const struct inode *inode);
10131 +
10132 +/* include miscellaneous macros */
10133 +#include "fanout.h"
10134 +#include "sioq.h"
10135 +
10136 +/* externs for cache creation/deletion routines */
10137 +extern void unionfs_destroy_filldir_cache(void);
10138 +extern int unionfs_init_filldir_cache(void);
10139 +extern int unionfs_init_inode_cache(void);
10140 +extern void unionfs_destroy_inode_cache(void);
10141 +extern int unionfs_init_dentry_cache(void);
10142 +extern void unionfs_destroy_dentry_cache(void);
10143 +
10144 +/* Initialize and free readdir-specific  state. */
10145 +extern int init_rdstate(struct file *file);
10146 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
10147 +                                              int bindex);
10148 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
10149 +                                             loff_t fpos);
10150 +extern void free_rdstate(struct unionfs_dir_state *state);
10151 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
10152 +                           const char *name, int namelen, int bindex,
10153 +                           int whiteout);
10154 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
10155 +                                             const char *name, int namelen);
10156 +
10157 +extern struct dentry **alloc_new_dentries(int objs);
10158 +extern struct unionfs_data *alloc_new_data(int objs);
10159 +
10160 +/* We can only use 32-bits of offset for rdstate --- blech! */
10161 +#define DIREOF (0xfffff)
10162 +#define RDOFFBITS 20           /* This is the number of bits in DIREOF. */
10163 +#define MAXRDCOOKIE (0xfff)
10164 +/* Turn an rdstate into an offset. */
10165 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
10166 +{
10167 +       off_t tmp;
10168 +
10169 +       tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
10170 +               | (buf->offset & DIREOF);
10171 +       return tmp;
10172 +}
10173 +
10174 +#define unionfs_read_lock(sb)   down_read(&UNIONFS_SB(sb)->rwsem)
10175 +#define unionfs_read_unlock(sb)         up_read(&UNIONFS_SB(sb)->rwsem)
10176 +#define unionfs_write_lock(sb)  down_write(&UNIONFS_SB(sb)->rwsem)
10177 +#define unionfs_write_unlock(sb) up_write(&UNIONFS_SB(sb)->rwsem)
10178 +
10179 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
10180 +                                             struct dentry *d2)
10181 +{
10182 +       if (d2 < d1) {
10183 +               struct dentry *tmp = d1;
10184 +               d1 = d2;
10185 +               d2 = tmp;
10186 +       }
10187 +       unionfs_lock_dentry(d1);
10188 +       unionfs_lock_dentry(d2);
10189 +}
10190 +
10191 +extern int new_dentry_private_data(struct dentry *dentry);
10192 +extern void free_dentry_private_data(struct dentry *dentry);
10193 +extern void update_bstart(struct dentry *dentry);
10194 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
10195 +extern void release_lower_nd(struct nameidata *nd, int err);
10196 +
10197 +/*
10198 + * EXTERNALS:
10199 + */
10200 +
10201 +/* replicates the directory structure up to given dentry in given branch */
10202 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
10203 +                                    const char *name, int bindex);
10204 +extern int make_dir_opaque(struct dentry *dir, int bindex);
10205 +
10206 +/* partial lookup */
10207 +extern int unionfs_partial_lookup(struct dentry *dentry);
10208 +
10209 +/*
10210 + * Pass an unionfs dentry and an index and it will try to create a whiteout
10211 + * in branch 'index'.
10212 + *
10213 + * On error, it will proceed to a branch to the left
10214 + */
10215 +extern int create_whiteout(struct dentry *dentry, int start);
10216 +/* copies a file from dbstart to newbindex branch */
10217 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
10218 +                      int newbindex, loff_t size);
10219 +extern int copyup_named_file(struct inode *dir, struct file *file,
10220 +                            char *name, int bstart, int new_bindex,
10221 +                            loff_t len);
10222 +/* copies a dentry from dbstart to newbindex branch */
10223 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
10224 +                        int bstart, int new_bindex, const char *name,
10225 +                        int namelen, struct file **copyup_file, loff_t len);
10226 +/* helper functions for post-copyup actions */
10227 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
10228 +extern void unionfs_postcopyup_release(struct dentry *dentry);
10229 +
10230 +extern int remove_whiteouts(struct dentry *dentry,
10231 +                           struct dentry *lower_dentry, int bindex);
10232 +
10233 +extern int do_delete_whiteouts(struct dentry *dentry, int bindex,
10234 +                              struct unionfs_dir_state *namelist);
10235 +
10236 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
10237 +extern int check_empty(struct dentry *dentry,
10238 +                      struct unionfs_dir_state **namelist);
10239 +/* Delete whiteouts from this directory in branch bindex. */
10240 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
10241 +                           struct unionfs_dir_state *namelist);
10242 +
10243 +/* Re-lookup a lower dentry. */
10244 +extern int unionfs_refresh_lower_dentry(struct dentry *dentry, int bindex);
10245 +
10246 +extern void unionfs_reinterpose(struct dentry *this_dentry);
10247 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
10248 +
10249 +/* Locking functions. */
10250 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
10251 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
10252 +
10253 +/* Common file operations. */
10254 +extern int unionfs_file_revalidate(struct file *file, bool willwrite);
10255 +extern int unionfs_open(struct inode *inode, struct file *file);
10256 +extern int unionfs_file_release(struct inode *inode, struct file *file);
10257 +extern int unionfs_flush(struct file *file, fl_owner_t id);
10258 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
10259 +                         unsigned long arg);
10260 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
10261 +                        int datasync);
10262 +extern int unionfs_fasync(int fd, struct file *file, int flag);
10263 +
10264 +/* Inode operations */
10265 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
10266 +                         struct inode *new_dir, struct dentry *new_dentry);
10267 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
10268 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
10269 +
10270 +extern bool __unionfs_d_revalidate_chain(struct dentry *dentry,
10271 +                                        struct nameidata *nd, bool willwrite);
10272 +extern bool is_newer_lower(const struct dentry *dentry);
10273 +
10274 +/* The values for unionfs_interpose's flag. */
10275 +#define INTERPOSE_DEFAULT      0
10276 +#define INTERPOSE_LOOKUP       1
10277 +#define INTERPOSE_REVAL                2
10278 +#define INTERPOSE_REVAL_NEG    3
10279 +#define INTERPOSE_PARTIAL      4
10280 +
10281 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
10282 +                                       struct super_block *sb, int flag);
10283 +
10284 +#ifdef CONFIG_UNION_FS_XATTR
10285 +/* Extended attribute functions. */
10286 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
10287 +static inline void unionfs_xattr_kfree(const void *p)
10288 +{
10289 +       kfree(p);
10290 +}
10291 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
10292 +                               void *value, size_t size);
10293 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
10294 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
10295 +                                size_t size);
10296 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
10297 +                           const void *value, size_t size, int flags);
10298 +#endif /* CONFIG_UNION_FS_XATTR */
10299 +
10300 +/* The root directory is unhashed, but isn't deleted. */
10301 +static inline int d_deleted(struct dentry *d)
10302 +{
10303 +       return d_unhashed(d) && (d != d->d_sb->s_root);
10304 +}
10305 +
10306 +struct dentry *unionfs_lookup_backend(struct dentry *dentry,
10307 +                                     struct nameidata *nd, int lookupmode);
10308 +
10309 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10310 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10311 +
10312 +/* unionfs_open, check if we need to copyup the file */
10313 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10314 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10315 +
10316 +static inline int branchperms(const struct super_block *sb, int index)
10317 +{
10318 +       BUG_ON(index < 0);
10319 +       return UNIONFS_SB(sb)->data[index].branchperms;
10320 +}
10321 +
10322 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10323 +{
10324 +       BUG_ON(index < 0);
10325 +       UNIONFS_SB(sb)->data[index].branchperms = perms;
10326 +       return perms;
10327 +}
10328 +
10329 +/* Is this file on a read-only branch? */
10330 +static inline int is_robranch_super(const struct super_block *sb, int index)
10331 +{
10332 +       int ret;
10333 +
10334 +       ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10335 +       return ret;
10336 +}
10337 +
10338 +/* Is this file on a read-only branch? */
10339 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10340 +{
10341 +       struct super_block *lower_sb;
10342 +
10343 +       BUG_ON(index < 0);
10344 +
10345 +       if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10346 +               return -EROFS;
10347 +
10348 +       lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10349 +       BUG_ON(lower_sb == NULL);
10350 +       /*
10351 +        * test sb flags directly, not IS_RDONLY(lower_inode) because the
10352 +        * lower_dentry could be a negative.
10353 +        */
10354 +       if (lower_sb->s_flags & MS_RDONLY)
10355 +               return -EROFS;
10356 +
10357 +       return 0;
10358 +}
10359 +
10360 +static inline int is_robranch(const struct dentry *dentry)
10361 +{
10362 +       int index;
10363 +
10364 +       index = UNIONFS_D(dentry)->bstart;
10365 +       BUG_ON(index < 0);
10366 +
10367 +       return is_robranch_idx(dentry, index);
10368 +}
10369 +
10370 +/* What do we use for whiteouts. */
10371 +#define UNIONFS_WHPFX ".wh."
10372 +#define UNIONFS_WHLEN 4
10373 +/*
10374 + * If a directory contains this file, then it is opaque.  We start with the
10375 + * .wh. flag so that it is blocked by lookup.
10376 + */
10377 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10378 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10379 +
10380 +/*
10381 + * EXTERNALS:
10382 + */
10383 +extern char *alloc_whname(const char *name, int len);
10384 +extern int check_branch(struct nameidata *nd);
10385 +extern int __parse_branch_mode(const char *name);
10386 +extern int parse_branch_mode(const char *name);
10387 +
10388 +/*
10389 + * These two functions are here because it is kind of daft to copy and paste
10390 + * the contents of the two functions to 32+ places in unionfs
10391 + */
10392 +static inline struct dentry *lock_parent(struct dentry *dentry)
10393 +{
10394 +       struct dentry *dir = dget(dentry->d_parent);
10395 +
10396 +       mutex_lock(&dir->d_inode->i_mutex);
10397 +       return dir;
10398 +}
10399 +
10400 +static inline void unlock_dir(struct dentry *dir)
10401 +{
10402 +       mutex_unlock(&dir->d_inode->i_mutex);
10403 +       dput(dir);
10404 +}
10405 +
10406 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10407 +                                             int bindex)
10408 +{
10409 +       struct vfsmount *mnt;
10410 +
10411 +       BUG_ON(!dentry || bindex < 0);
10412 +
10413 +       mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10414 +#ifdef CONFIG_UNION_FS_DEBUG
10415 +       if (!mnt)
10416 +               pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10417 +                        mnt, bindex);
10418 +#endif /* CONFIG_UNION_FS_DEBUG */
10419 +
10420 +       return mnt;
10421 +}
10422 +
10423 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10424 +{
10425 +       struct vfsmount *mnt;
10426 +
10427 +       if (!dentry && bindex < 0)
10428 +               return;
10429 +       BUG_ON(!dentry || bindex < 0);
10430 +
10431 +       mnt = unionfs_lower_mnt_idx(dentry, bindex);
10432 +#ifdef CONFIG_UNION_FS_DEBUG
10433 +       /*
10434 +        * Directories can have NULL lower objects in between start/end, but
10435 +        * NOT if at the start/end range.  We cannot verify that this dentry
10436 +        * is a type=DIR, because it may already be a negative dentry.  But
10437 +        * if dbstart is greater than dbend, we know that this couldn't have
10438 +        * been a regular file: it had to have been a directory.
10439 +        */
10440 +       if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10441 +               pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10442 +#endif /* CONFIG_UNION_FS_DEBUG */
10443 +       mntput(mnt);
10444 +}
10445 +
10446 +#ifdef CONFIG_UNION_FS_DEBUG
10447 +
10448 +/* useful for tracking code reachability */
10449 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__)
10450 +
10451 +#define unionfs_check_inode(i) __unionfs_check_inode((i),      \
10452 +       __FILE__, __FUNCTION__, __LINE__)
10453 +#define unionfs_check_dentry(d)        __unionfs_check_dentry((d),     \
10454 +       __FILE__, __FUNCTION__, __LINE__)
10455 +#define unionfs_check_file(f)  __unionfs_check_file((f),       \
10456 +       __FILE__, __FUNCTION__, __LINE__)
10457 +#define unionfs_check_nd(n)    __unionfs_check_nd((n),         \
10458 +       __FILE__, __FUNCTION__, __LINE__)
10459 +#define show_branch_counts(sb) __show_branch_counts((sb),      \
10460 +       __FILE__, __FUNCTION__, __LINE__)
10461 +#define show_inode_times(i)    __show_inode_times((i),         \
10462 +       __FILE__, __FUNCTION__, __LINE__)
10463 +#define show_dinode_times(d)   __show_dinode_times((d),        \
10464 +       __FILE__, __FUNCTION__, __LINE__)
10465 +#define show_inode_counts(i)   __show_inode_counts((i),        \
10466 +       __FILE__, __FUNCTION__, __LINE__)
10467 +
10468 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10469 +                                 const char *fxn, int line);
10470 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10471 +                                  const char *fname, const char *fxn,
10472 +                                  int line);
10473 +extern void __unionfs_check_file(const struct file *file,
10474 +                                const char *fname, const char *fxn, int line);
10475 +extern void __unionfs_check_nd(const struct nameidata *nd,
10476 +                              const char *fname, const char *fxn, int line);
10477 +extern void __show_branch_counts(const struct super_block *sb,
10478 +                                const char *file, const char *fxn, int line);
10479 +extern void __show_inode_times(const struct inode *inode,
10480 +                              const char *file, const char *fxn, int line);
10481 +extern void __show_dinode_times(const struct dentry *dentry,
10482 +                               const char *file, const char *fxn, int line);
10483 +extern void __show_inode_counts(const struct inode *inode,
10484 +                               const char *file, const char *fxn, int line);
10485 +
10486 +#else /* not CONFIG_UNION_FS_DEBUG */
10487 +
10488 +/* we leave useful hooks for these check functions throughout the code */
10489 +#define unionfs_check_inode(i)         do { } while (0)
10490 +#define unionfs_check_dentry(d)                do { } while (0)
10491 +#define unionfs_check_file(f)          do { } while (0)
10492 +#define unionfs_check_nd(n)            do { } while (0)
10493 +#define show_branch_counts(sb)         do { } while (0)
10494 +#define show_inode_times(i)            do { } while (0)
10495 +#define show_dinode_times(d)           do { } while (0)
10496 +#define show_inode_counts(i)           do { } while (0)
10497 +
10498 +#endif /* not CONFIG_UNION_FS_DEBUG */
10499 +
10500 +#endif /* not _UNION_H_ */
10501 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10502 new file mode 100644
10503 index 0000000..a8de672
10504 --- /dev/null
10505 +++ b/fs/unionfs/unlink.c
10506 @@ -0,0 +1,196 @@
10507 +/*
10508 + * Copyright (c) 2003-2007 Erez Zadok
10509 + * Copyright (c) 2003-2006 Charles P. Wright
10510 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10511 + * Copyright (c) 2005-2006 Junjiro Okajima
10512 + * Copyright (c) 2005      Arun M. Krishnakumar
10513 + * Copyright (c) 2004-2006 David P. Quigley
10514 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10515 + * Copyright (c) 2003      Puja Gupta
10516 + * Copyright (c) 2003      Harikesavan Krishnan
10517 + * Copyright (c) 2003-2007 Stony Brook University
10518 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
10519 + *
10520 + * This program is free software; you can redistribute it and/or modify
10521 + * it under the terms of the GNU General Public License version 2 as
10522 + * published by the Free Software Foundation.
10523 + */
10524 +
10525 +#include "union.h"
10526 +
10527 +/* unlink a file by creating a whiteout */
10528 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry)
10529 +{
10530 +       struct dentry *lower_dentry;
10531 +       struct dentry *lower_dir_dentry;
10532 +       int bindex;
10533 +       int err = 0;
10534 +
10535 +       err = unionfs_partial_lookup(dentry);
10536 +       if (err)
10537 +               goto out;
10538 +
10539 +       bindex = dbstart(dentry);
10540 +
10541 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10542 +       if (!lower_dentry)
10543 +               goto out;
10544 +
10545 +       lower_dir_dentry = lock_parent(lower_dentry);
10546 +
10547 +       /* avoid destroying the lower inode if the file is in use */
10548 +       dget(lower_dentry);
10549 +       err = is_robranch_super(dentry->d_sb, bindex);
10550 +       if (!err)
10551 +               err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
10552 +       /* if vfs_unlink succeeded, update our inode's times */
10553 +       if (!err)
10554 +               unionfs_copy_attr_times(dentry->d_inode);
10555 +       dput(lower_dentry);
10556 +       fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10557 +       unlock_dir(lower_dir_dentry);
10558 +
10559 +       if (err && !IS_COPYUP_ERR(err))
10560 +               goto out;
10561 +
10562 +       if (err) {
10563 +               if (dbstart(dentry) == 0)
10564 +                       goto out;
10565 +               err = create_whiteout(dentry, dbstart(dentry) - 1);
10566 +       } else if (dbopaque(dentry) != -1) {
10567 +               /* There is a lower lower-priority file with the same name. */
10568 +               err = create_whiteout(dentry, dbopaque(dentry));
10569 +       } else {
10570 +               err = create_whiteout(dentry, dbstart(dentry));
10571 +       }
10572 +
10573 +out:
10574 +       if (!err)
10575 +               dentry->d_inode->i_nlink--;
10576 +
10577 +       /* We don't want to leave negative leftover dentries for revalidate. */
10578 +       if (!err && (dbopaque(dentry) != -1))
10579 +               update_bstart(dentry);
10580 +
10581 +       return err;
10582 +}
10583 +
10584 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10585 +{
10586 +       int err = 0;
10587 +
10588 +       unionfs_read_lock(dentry->d_sb);
10589 +       unionfs_lock_dentry(dentry);
10590 +
10591 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10592 +               err = -ESTALE;
10593 +               goto out;
10594 +       }
10595 +       unionfs_check_dentry(dentry);
10596 +
10597 +       err = unionfs_unlink_whiteout(dir, dentry);
10598 +       /* call d_drop so the system "forgets" about us */
10599 +       if (!err) {
10600 +               if (!S_ISDIR(dentry->d_inode->i_mode))
10601 +                       unionfs_postcopyup_release(dentry);
10602 +               d_drop(dentry);
10603 +               /*
10604 +                * if unlink/whiteout succeeded, parent dir mtime has
10605 +                * changed
10606 +                */
10607 +               unionfs_copy_attr_times(dir);
10608 +       }
10609 +
10610 +out:
10611 +       if (!err) {
10612 +               unionfs_check_dentry(dentry);
10613 +               unionfs_check_inode(dir);
10614 +       }
10615 +       unionfs_unlock_dentry(dentry);
10616 +       unionfs_read_unlock(dentry->d_sb);
10617 +       return err;
10618 +}
10619 +
10620 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10621 +                              struct unionfs_dir_state *namelist)
10622 +{
10623 +       int err;
10624 +       struct dentry *lower_dentry;
10625 +       struct dentry *lower_dir_dentry = NULL;
10626 +
10627 +       /* Here we need to remove whiteout entries. */
10628 +       err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10629 +       if (err)
10630 +               goto out;
10631 +
10632 +       lower_dentry = unionfs_lower_dentry(dentry);
10633 +
10634 +       lower_dir_dentry = lock_parent(lower_dentry);
10635 +
10636 +       /* avoid destroying the lower inode if the file is in use */
10637 +       dget(lower_dentry);
10638 +       err = is_robranch(dentry);
10639 +       if (!err)
10640 +               err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10641 +       dput(lower_dentry);
10642 +
10643 +       fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10644 +       /* propagate number of hard-links */
10645 +       dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10646 +
10647 +out:
10648 +       if (lower_dir_dentry)
10649 +               unlock_dir(lower_dir_dentry);
10650 +       return err;
10651 +}
10652 +
10653 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10654 +{
10655 +       int err = 0;
10656 +       struct unionfs_dir_state *namelist = NULL;
10657 +
10658 +       unionfs_read_lock(dentry->d_sb);
10659 +       unionfs_lock_dentry(dentry);
10660 +
10661 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10662 +               err = -ESTALE;
10663 +               goto out;
10664 +       }
10665 +       unionfs_check_dentry(dentry);
10666 +
10667 +       /* check if this unionfs directory is empty or not */
10668 +       err = check_empty(dentry, &namelist);
10669 +       if (err)
10670 +               goto out;
10671 +
10672 +       err = unionfs_rmdir_first(dir, dentry, namelist);
10673 +       /* create whiteout */
10674 +       if (!err) {
10675 +               err = create_whiteout(dentry, dbstart(dentry));
10676 +       } else {
10677 +               int new_err;
10678 +
10679 +               if (dbstart(dentry) == 0)
10680 +                       goto out;
10681 +
10682 +               /* exit if the error returned was NOT -EROFS */
10683 +               if (!IS_COPYUP_ERR(err))
10684 +                       goto out;
10685 +
10686 +               new_err = create_whiteout(dentry, dbstart(dentry) - 1);
10687 +               if (new_err != -EEXIST)
10688 +                       err = new_err;
10689 +       }
10690 +
10691 +out:
10692 +       /* call d_drop so the system "forgets" about us */
10693 +       if (!err)
10694 +               d_drop(dentry);
10695 +
10696 +       if (namelist)
10697 +               free_rdstate(namelist);
10698 +
10699 +       unionfs_unlock_dentry(dentry);
10700 +       unionfs_read_unlock(dentry->d_sb);
10701 +       return err;
10702 +}
10703 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
10704 new file mode 100644
10705 index 0000000..602cedf
10706 --- /dev/null
10707 +++ b/fs/unionfs/xattr.c
10708 @@ -0,0 +1,153 @@
10709 +/*
10710 + * Copyright (c) 2003-2007 Erez Zadok
10711 + * Copyright (c) 2003-2006 Charles P. Wright
10712 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10713 + * Copyright (c) 2005-2006 Junjiro Okajima
10714 + * Copyright (c) 2005      Arun M. Krishnakumar
10715 + * Copyright (c) 2004-2006 David P. Quigley
10716 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10717 + * Copyright (c) 2003      Puja Gupta
10718 + * Copyright (c) 2003      Harikesavan Krishnan
10719 + * Copyright (c) 2003-2007 Stony Brook University
10720 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
10721 + *
10722 + * This program is free software; you can redistribute it and/or modify
10723 + * it under the terms of the GNU General Public License version 2 as
10724 + * published by the Free Software Foundation.
10725 + */
10726 +
10727 +#include "union.h"
10728 +
10729 +/* This is lifted from fs/xattr.c */
10730 +void *unionfs_xattr_alloc(size_t size, size_t limit)
10731 +{
10732 +       void *ptr;
10733 +
10734 +       if (size > limit)
10735 +               return ERR_PTR(-E2BIG);
10736 +
10737 +       if (!size)              /* size request, no buffer is needed */
10738 +               return NULL;
10739 +
10740 +       ptr = kmalloc(size, GFP_KERNEL);
10741 +       if (unlikely(!ptr))
10742 +               return ERR_PTR(-ENOMEM);
10743 +       return ptr;
10744 +}
10745 +
10746 +/*
10747 + * BKL held by caller.
10748 + * dentry->d_inode->i_mutex locked
10749 + */
10750 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
10751 +                        size_t size)
10752 +{
10753 +       struct dentry *lower_dentry = NULL;
10754 +       int err = -EOPNOTSUPP;
10755 +
10756 +       unionfs_read_lock(dentry->d_sb);
10757 +       unionfs_lock_dentry(dentry);
10758 +
10759 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10760 +               err = -ESTALE;
10761 +               goto out;
10762 +       }
10763 +
10764 +       lower_dentry = unionfs_lower_dentry(dentry);
10765 +
10766 +       err = vfs_getxattr(lower_dentry, (char *) name, value, size);
10767 +
10768 +out:
10769 +       unionfs_unlock_dentry(dentry);
10770 +       unionfs_check_dentry(dentry);
10771 +       unionfs_read_unlock(dentry->d_sb);
10772 +       return err;
10773 +}
10774 +
10775 +/*
10776 + * BKL held by caller.
10777 + * dentry->d_inode->i_mutex locked
10778 + */
10779 +int unionfs_setxattr(struct dentry *dentry, const char *name,
10780 +                    const void *value, size_t size, int flags)
10781 +{
10782 +       struct dentry *lower_dentry = NULL;
10783 +       int err = -EOPNOTSUPP;
10784 +
10785 +       unionfs_read_lock(dentry->d_sb);
10786 +       unionfs_lock_dentry(dentry);
10787 +
10788 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10789 +               err = -ESTALE;
10790 +               goto out;
10791 +       }
10792 +
10793 +       lower_dentry = unionfs_lower_dentry(dentry);
10794 +
10795 +       err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
10796 +                          size, flags);
10797 +
10798 +out:
10799 +       unionfs_unlock_dentry(dentry);
10800 +       unionfs_check_dentry(dentry);
10801 +       unionfs_read_unlock(dentry->d_sb);
10802 +       return err;
10803 +}
10804 +
10805 +/*
10806 + * BKL held by caller.
10807 + * dentry->d_inode->i_mutex locked
10808 + */
10809 +int unionfs_removexattr(struct dentry *dentry, const char *name)
10810 +{
10811 +       struct dentry *lower_dentry = NULL;
10812 +       int err = -EOPNOTSUPP;
10813 +
10814 +       unionfs_read_lock(dentry->d_sb);
10815 +       unionfs_lock_dentry(dentry);
10816 +
10817 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10818 +               err = -ESTALE;
10819 +               goto out;
10820 +       }
10821 +
10822 +       lower_dentry = unionfs_lower_dentry(dentry);
10823 +
10824 +       err = vfs_removexattr(lower_dentry, (char *) name);
10825 +
10826 +out:
10827 +       unionfs_unlock_dentry(dentry);
10828 +       unionfs_check_dentry(dentry);
10829 +       unionfs_read_unlock(dentry->d_sb);
10830 +       return err;
10831 +}
10832 +
10833 +/*
10834 + * BKL held by caller.
10835 + * dentry->d_inode->i_mutex locked
10836 + */
10837 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
10838 +{
10839 +       struct dentry *lower_dentry = NULL;
10840 +       int err = -EOPNOTSUPP;
10841 +       char *encoded_list = NULL;
10842 +
10843 +       unionfs_read_lock(dentry->d_sb);
10844 +       unionfs_lock_dentry(dentry);
10845 +
10846 +       if (unlikely(!__unionfs_d_revalidate_chain(dentry, NULL, false))) {
10847 +               err = -ESTALE;
10848 +               goto out;
10849 +       }
10850 +
10851 +       lower_dentry = unionfs_lower_dentry(dentry);
10852 +
10853 +       encoded_list = list;
10854 +       err = vfs_listxattr(lower_dentry, encoded_list, size);
10855 +
10856 +out:
10857 +       unionfs_unlock_dentry(dentry);
10858 +       unionfs_check_dentry(dentry);
10859 +       unionfs_read_unlock(dentry->d_sb);
10860 +       return err;
10861 +}
10862 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
10863 index bb516ce..6b52faf 100644
10864 --- a/include/linux/fs_stack.h
10865 +++ b/include/linux/fs_stack.h
10866 @@ -1,17 +1,28 @@
10867 +/*
10868 + * Copyright (c) 2006-2007 Erez Zadok
10869 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
10870 + * Copyright (c) 2006-2007 Stony Brook University
10871 + * Copyright (c) 2006-2007 The Research Foundation of SUNY
10872 + *
10873 + * This program is free software; you can redistribute it and/or modify
10874 + * it under the terms of the GNU General Public License version 2 as
10875 + * published by the Free Software Foundation.
10876 + */
10877 +
10878  #ifndef _LINUX_FS_STACK_H
10879  #define _LINUX_FS_STACK_H
10880  
10881 -/* This file defines generic functions used primarily by stackable
10882 +/*
10883 + * This file defines generic functions used primarily by stackable
10884   * filesystems; none of these functions require i_mutex to be held.
10885   */
10886  
10887  #include <linux/fs.h>
10888  
10889  /* externs for fs/stack.c */
10890 -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
10891 -                               int (*get_nlinks)(struct inode *));
10892 -
10893 -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
10894 +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
10895 +extern void fsstack_copy_inode_size(struct inode *dst,
10896 +                                   const struct inode *src);
10897  
10898  /* inlines */
10899  static inline void fsstack_copy_attr_atime(struct inode *dest,
10900 diff --git a/include/linux/magic.h b/include/linux/magic.h
10901 index 36cc20d..8dac50b 100644
10902 --- a/include/linux/magic.h
10903 +++ b/include/linux/magic.h
10904 @@ -35,6 +35,8 @@
10905  #define REISER2FS_SUPER_MAGIC_STRING   "ReIsEr2Fs"
10906  #define REISER2FS_JR_SUPER_MAGIC_STRING        "ReIsEr3Fs"
10907  
10908 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
10909 +
10910  #define SMB_SUPER_MAGIC                0x517B
10911  #define USBDEVICE_SUPER_MAGIC  0x9fa2
10912  
10913 diff --git a/include/linux/mm.h b/include/linux/mm.h
10914 index 1692dd6..aee99b6 100644
10915 --- a/include/linux/mm.h
10916 +++ b/include/linux/mm.h
10917 @@ -20,6 +20,7 @@ struct anon_vma;
10918  struct file_ra_state;
10919  struct user_struct;
10920  struct writeback_control;
10921 +struct super_block;
10922  
10923  #ifndef CONFIG_DISCONTIGMEM          /* Don't use mapnrs, do it properly */
10924  extern unsigned long max_mapnr;
10925 @@ -1207,6 +1208,7 @@ int drop_caches_sysctl_handler(struct ctl_table *, int, struct file *,
10926                                         void __user *, size_t *, loff_t *);
10927  unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
10928                         unsigned long lru_pages);
10929 +extern void drop_pagecache_sb(struct super_block *);
10930  void drop_pagecache(void);
10931  void drop_slab(void);
10932  
10933 diff --git a/include/linux/namei.h b/include/linux/namei.h
10934 index 6c38efb..e9477b5 100644
10935 --- a/include/linux/namei.h
10936 +++ b/include/linux/namei.h
10937 @@ -3,6 +3,7 @@
10938  
10939  #include <linux/dcache.h>
10940  #include <linux/linkage.h>
10941 +#include <linux/mount.h>
10942  
10943  struct vfsmount;
10944  
10945 @@ -100,4 +101,16 @@ static inline char *nd_get_link(struct nameidata *nd)
10946         return nd->saved_names[nd->depth];
10947  }
10948  
10949 +static inline void pathget(struct path *path)
10950 +{
10951 +       mntget(path->mnt);
10952 +       dget(path->dentry);
10953 +}
10954 +
10955 +static inline void pathput(struct path *path)
10956 +{
10957 +       dput(path->dentry);
10958 +       mntput(path->mnt);
10959 +}
10960 +
10961  #endif /* _LINUX_NAMEI_H */
10962 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
10963 new file mode 100644
10964 index 0000000..d13eb48
10965 --- /dev/null
10966 +++ b/include/linux/union_fs.h
10967 @@ -0,0 +1,25 @@
10968 +/*
10969 + * Copyright (c) 2003-2007 Erez Zadok
10970 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10971 + * Copyright (c) 2003-2007 Stony Brook University
10972 + * Copyright (c) 2003-2007 The Research Foundation of SUNY
10973 + *
10974 + * This program is free software; you can redistribute it and/or modify
10975 + * it under the terms of the GNU General Public License version 2 as
10976 + * published by the Free Software Foundation.
10977 + */
10978 +
10979 +#ifndef _LINUX_UNION_FS_H
10980 +#define _LINUX_UNION_FS_H
10981 +
10982 +/*
10983 + * DEFINITIONS FOR USER AND KERNEL CODE:
10984 + */
10985 +# define UNIONFS_IOCTL_INCGEN          _IOR(0x15, 11, int)
10986 +# define UNIONFS_IOCTL_QUERYFILE       _IOR(0x15, 15, int)
10987 +
10988 +/* We don't support normal remount, but unionctl uses it. */
10989 +# define UNIONFS_REMOUNT_MAGIC         0x4a5a4380
10990 +
10991 +#endif /* _LINUX_UNIONFS_H */
10992 +
This page took 1.624482 seconds and 3 git commands to generate.