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