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