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