]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-unionfs.patch
- move vserver configs to kernel-vserver.config; update config
[packages/kernel.git] / kernel-unionfs.patch
1
2 This is a rebase to 2.6.35 of the upstream unionfs-2.5.4_for_2.6.34-rc0.diff.gz
3
4 Signed-off-by: Thomas Backlund <tmb@mandriva.org>
5
6  Documentation/filesystems/00-INDEX             |    2 
7  Documentation/filesystems/unionfs/00-INDEX     |   10 
8  Documentation/filesystems/unionfs/concepts.txt |  287 ++++++
9  Documentation/filesystems/unionfs/issues.txt   |   28 
10  Documentation/filesystems/unionfs/rename.txt   |   31 
11  Documentation/filesystems/unionfs/usage.txt    |  134 +++
12  MAINTAINERS                                    |    8 
13  fs/Kconfig                                     |    1 
14  fs/Makefile                                    |    1 
15  fs/namei.c                                     |    1 
16  fs/splice.c                                    |   20 
17  fs/stack.c                                     |   14 
18  fs/unionfs/Kconfig                             |   24 
19  fs/unionfs/Makefile                            |   17 
20  fs/unionfs/commonfops.c                        |  896 +++++++++++++++++++++
21  fs/unionfs/copyup.c                            |  897 +++++++++++++++++++++
22  fs/unionfs/debug.c                             |  533 ++++++++++++
23  fs/unionfs/dentry.c                            |  397 +++++++++
24  fs/unionfs/dirfops.c                           |  302 +++++++
25  fs/unionfs/dirhelper.c                         |  158 +++
26  fs/unionfs/fanout.h                            |  407 +++++++++
27  fs/unionfs/file.c                              |  380 +++++++++
28  fs/unionfs/inode.c                             | 1055 +++++++++++++++++++++++++
29  fs/unionfs/lookup.c                            |  569 +++++++++++++
30  fs/unionfs/main.c                              |  758 +++++++++++++++++
31  fs/unionfs/mmap.c                              |   89 ++
32  fs/unionfs/rdstate.c                           |  285 ++++++
33  fs/unionfs/rename.c                            |  517 ++++++++++++
34  fs/unionfs/sioq.c                              |  101 ++
35  fs/unionfs/sioq.h                              |   91 ++
36  fs/unionfs/subr.c                              |   95 ++
37  fs/unionfs/super.c                             | 1047 ++++++++++++++++++++++++
38  fs/unionfs/union.h                             |  670 +++++++++++++++
39  fs/unionfs/unlink.c                            |  278 ++++++
40  fs/unionfs/whiteout.c                          |  584 +++++++++++++
41  fs/unionfs/xattr.c                             |  173 ++++
42  include/linux/fs_stack.h                       |   14 
43  include/linux/magic.h                          |    2 
44  include/linux/namei.h                          |    1 
45  include/linux/splice.h                         |    5 
46  include/linux/union_fs.h                       |   22 
47  security/security.c                            |    1 
48  42 files changed, 10894 insertions(+), 11 deletions(-)
49
50 diff -Nurp linux-2.6.35-rc6-git//Documentation/filesystems/00-INDEX linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/00-INDEX
51 --- linux-2.6.35-rc6-git//Documentation/filesystems/00-INDEX    2010-05-17 00:17:36.000000000 +0300
52 +++ linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/00-INDEX    2010-07-30 15:50:42.994235028 +0300
53 @@ -112,6 +112,8 @@ udf.txt
54         - info and mount options for the UDF filesystem.
55  ufs.txt
56         - info on the ufs filesystem.
57 +unionfs/
58 +       - info on the unionfs filesystem
59  vfat.txt
60         - info on using the VFAT filesystem used in Windows NT and Windows 95
61  vfs.txt
62 diff -Nurp linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/00-INDEX linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/00-INDEX
63 --- linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/00-INDEX    1970-01-01 02:00:00.000000000 +0200
64 +++ linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/00-INDEX    2010-07-30 15:50:42.994235028 +0300
65 @@ -0,0 +1,10 @@
66 +00-INDEX
67 +       - this file.
68 +concepts.txt
69 +       - A brief introduction of concepts.
70 +issues.txt
71 +       - A summary of known issues with unionfs.
72 +rename.txt
73 +       - Information regarding rename operations.
74 +usage.txt
75 +       - Usage information and examples.
76 diff -Nurp linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/concepts.txt linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/concepts.txt
77 --- linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/concepts.txt        1970-01-01 02:00:00.000000000 +0200
78 +++ linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/concepts.txt        2010-07-30 15:50:42.994947708 +0300
79 @@ -0,0 +1,287 @@
80 +Unionfs 2.x CONCEPTS:
81 +=====================
82 +
83 +This file describes the concepts needed by a namespace unification file
84 +system.
85 +
86 +
87 +Branch Priority:
88 +================
89 +
90 +Each branch is assigned a unique priority - starting from 0 (highest
91 +priority).  No two branches can have the same priority.
92 +
93 +
94 +Branch Mode:
95 +============
96 +
97 +Each branch is assigned a mode - read-write or read-only. This allows
98 +directories on media mounted read-write to be used in a read-only manner.
99 +
100 +
101 +Whiteouts:
102 +==========
103 +
104 +A whiteout removes a file name from the namespace. Whiteouts are needed when
105 +one attempts to remove a file on a read-only branch.
106 +
107 +Suppose we have a two-branch union, where branch 0 is read-write and branch
108 +1 is read-only. And a file 'foo' on branch 1:
109 +
110 +./b0/
111 +./b1/
112 +./b1/foo
113 +
114 +The unified view would simply be:
115 +
116 +./union/
117 +./union/foo
118 +
119 +Since 'foo' is stored on a read-only branch, it cannot be removed. A
120 +whiteout is used to remove the name 'foo' from the unified namespace. Again,
121 +since branch 1 is read-only, the whiteout cannot be created there. So, we
122 +try on a higher priority (lower numerically) branch and create the whiteout
123 +there.
124 +
125 +./b0/
126 +./b0/.wh.foo
127 +./b1/
128 +./b1/foo
129 +
130 +Later, when Unionfs traverses branches (due to lookup or readdir), it
131 +eliminate 'foo' from the namespace (as well as the whiteout itself.)
132 +
133 +
134 +Opaque Directories:
135 +===================
136 +
137 +Assume we have a unionfs mount comprising of two branches.  Branch 0 is
138 +empty; branch 1 has the directory /a and file /a/f.  Let's say we mount a
139 +union of branch 0 as read-write and branch 1 as read-only.  Now, let's say
140 +we try to perform the following operation in the union:
141 +
142 +       rm -fr a
143 +
144 +Because branch 1 is not writable, we cannot physically remove the file /a/f
145 +or the directory /a.  So instead, we will create a whiteout in branch 0
146 +named /.wh.a, masking out the name "a" from branch 1.  Next, let's say we
147 +try to create a directory named "a" as follows:
148 +
149 +       mkdir a
150 +
151 +Because we have a whiteout for "a" already, Unionfs behaves as if "a"
152 +doesn't exist, and thus will delete the whiteout and replace it with an
153 +actual directory named "a".
154 +
155 +The problem now is that if you try to "ls" in the union, Unionfs will
156 +perform is normal directory name unification, for *all* directories named
157 +"a" in all branches.  This will cause the file /a/f from branch 1 to
158 +re-appear in the union's namespace, which violates Unix semantics.
159 +
160 +To avoid this problem, we have a different form of whiteouts for
161 +directories, called "opaque directories" (same as BSD Union Mount does).
162 +Whenever we replace a whiteout with a directory, that directory is marked as
163 +opaque.  In Unionfs 2.x, it means that we create a file named
164 +/a/.wh.__dir_opaque in branch 0, after having created directory /a there.
165 +When unionfs notices that a directory is opaque, it stops all namespace
166 +operations (including merging readdir contents) at that opaque directory.
167 +This prevents re-exposing names from masked out directories.
168 +
169 +
170 +Duplicate Elimination:
171 +======================
172 +
173 +It is possible for files on different branches to have the same name.
174 +Unionfs then has to select which instance of the file to show to the user.
175 +Given the fact that each branch has a priority associated with it, the
176 +simplest solution is to take the instance from the highest priority
177 +(numerically lowest value) and "hide" the others.
178 +
179 +
180 +Unlinking:
181 +=========
182 +
183 +Unlink operation on non-directory instances is optimized to remove the
184 +maximum possible objects in case multiple underlying branches have the same
185 +file name.  The unlink operation will first try to delete file instances
186 +from highest priority branch and then move further to delete from remaining
187 +branches in order of their decreasing priority.  Consider a case (F..D..F),
188 +where F is a file and D is a directory of the same name; here, some
189 +intermediate branch could have an empty directory instance with the same
190 +name, so this operation also tries to delete this directory instance and
191 +proceed further to delete from next possible lower priority branch.  The
192 +unionfs unlink operation will smoothly delete the files with same name from
193 +all possible underlying branches.  In case if some error occurs, it creates
194 +whiteout in highest priority branch that will hide file instance in rest of
195 +the branches.  An error could occur either if an unlink operations in any of
196 +the underlying branch failed or if a branch has no write permission.
197 +
198 +This unlinking policy is known as "delete all" and it has the benefit of
199 +overall reducing the number of inodes used by duplicate files, and further
200 +reducing the total number of inodes consumed by whiteouts.  The cost is of
201 +extra processing, but testing shows this extra processing is well worth the
202 +savings.
203 +
204 +
205 +Copyup:
206 +=======
207 +
208 +When a change is made to the contents of a file's data or meta-data, they
209 +have to be stored somewhere.  The best way is to create a copy of the
210 +original file on a branch that is writable, and then redirect the write
211 +though to this copy.  The copy must be made on a higher priority branch so
212 +that lookup and readdir return this newer "version" of the file rather than
213 +the original (see duplicate elimination).
214 +
215 +An entire unionfs mount can be read-only or read-write.  If it's read-only,
216 +then none of the branches will be written to, even if some of the branches
217 +are physically writeable.  If the unionfs mount is read-write, then the
218 +leftmost (highest priority) branch must be writeable (for copyup to take
219 +place); the remaining branches can be any mix of read-write and read-only.
220 +
221 +In a writeable mount, unionfs will create new files/dir in the leftmost
222 +branch.  If one tries to modify a file in a read-only branch/media, unionfs
223 +will copyup the file to the leftmost branch and modify it there.  If you try
224 +to modify a file from a writeable branch which is not the leftmost branch,
225 +then unionfs will modify it in that branch; this is useful if you, say,
226 +unify differnet packages (e.g., apache, sendmail, ftpd, etc.) and you want
227 +changes to specific package files to remain logically in the directory where
228 +they came from.
229 +
230 +Cache Coherency:
231 +================
232 +
233 +Unionfs users often want to be able to modify files and directories directly
234 +on the lower branches, and have those changes be visible at the Unionfs
235 +level.  This means that data (e.g., pages) and meta-data (dentries, inodes,
236 +open files, etc.) have to be synchronized between the upper and lower
237 +layers.  In other words, the newest changes from a layer below have to be
238 +propagated to the Unionfs layer above.  If the two layers are not in sync, a
239 +cache incoherency ensues, which could lead to application failures and even
240 +oopses.  The Linux kernel, however, has a rather limited set of mechanisms
241 +to ensure this inter-layer cache coherency---so Unionfs has to do most of
242 +the hard work on its own.
243 +
244 +Maintaining Invariants:
245 +
246 +The way Unionfs ensures cache coherency is as follows.  At each entry point
247 +to a Unionfs file system method, we call a utility function to validate the
248 +primary objects of this method.  Generally, we call unionfs_file_revalidate
249 +on open files, and __unionfs_d_revalidate_chain on dentries (which also
250 +validates inodes).  These utility functions check to see whether the upper
251 +Unionfs object is in sync with any of the lower objects that it represents.
252 +The checks we perform include whether the Unionfs superblock has a newer
253 +generation number, or if any of the lower objects mtime's or ctime's are
254 +newer.  (Note: generation numbers change when branch-management commands are
255 +issued, so in a way, maintaining cache coherency is also very important for
256 +branch-management.)  If indeed we determine that any Unionfs object is no
257 +longer in sync with its lower counterparts, then we rebuild that object
258 +similarly to how we do so for branch-management.
259 +
260 +While rebuilding Unionfs's objects, we also purge any page mappings and
261 +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data).  This is to
262 +ensure that Unionfs will re-get the newer data from the lower branches.  We
263 +perform this purging only if the Unionfs operation in question is a reading
264 +operation; if Unionfs is performing a data writing operation (e.g., ->write,
265 +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
266 +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
267 +considered more authoritative anyway, as they are newer and will overwrite
268 +any lower pages.
269 +
270 +Unionfs maintains the following important invariant regarding mtime's,
271 +ctime's, and atime's: the upper inode object's times are the max() of all of
272 +the lower ones.  For non-directory objects, there's only one object below,
273 +so the mapping is simple; for directory objects, there could me multiple
274 +lower objects and we have to sync up with the newest one of all the lower
275 +ones.  This invariant is important to maintain, especially for directories
276 +(besides, we need this to be POSIX compliant).  A union could comprise
277 +multiple writable branches, each of which could change.  If we don't reflect
278 +the newest possible mtime/ctime, some applications could fail.  For example,
279 +NFSv2/v3 exports check for newer directory mtimes on the server to determine
280 +if the client-side attribute cache should be purged.
281 +
282 +To maintain these important invariants, of course, Unionfs carefully
283 +synchronizes upper and lower times in various places.  For example, if we
284 +copy-up a file to a top-level branch, the parent directory where the file
285 +was copied up to will now have a new mtime: so after a successful copy-up,
286 +we sync up with the new top-level branch's parent directory mtime.
287 +
288 +Implementation:
289 +
290 +This cache-coherency implementation is efficient because it defers any
291 +synchronizing between the upper and lower layers until absolutely needed.
292 +Consider the example a common situation where users perform a lot of lower
293 +changes, such as untarring a whole package.  While these take place,
294 +typically the user doesn't access the files via Unionfs; only after the
295 +lower changes are done, does the user try to access the lower files.  With
296 +our cache-coherency implementation, the entirety of the changes to the lower
297 +branches will not result in a single CPU cycle spent at the Unionfs level
298 +until the user invokes a system call that goes through Unionfs.
299 +
300 +We have considered two alternate cache-coherency designs.  (1) Using the
301 +dentry/inode notify functionality to register interest in finding out about
302 +any lower changes.  This is a somewhat limited and also a heavy-handed
303 +approach which could result in many notifications to the Unionfs layer upon
304 +each small change at the lower layer (imagine a file being modified multiple
305 +times in rapid succession).  (2) Rewriting the VFS to support explicit
306 +callbacks from lower objects to upper objects.  We began exploring such an
307 +implementation, but found it to be very complicated--it would have resulted
308 +in massive VFS/MM changes which are unlikely to be accepted by the LKML
309 +community.  We therefore believe that our current cache-coherency design and
310 +implementation represent the best approach at this time.
311 +
312 +Limitations:
313 +
314 +Our implementation works in that as long as a user process will have caused
315 +Unionfs to be called, directly or indirectly, even to just do
316 +->d_revalidate; then we will have purged the current Unionfs data and the
317 +process will see the new data.  For example, a process that continually
318 +re-reads the same file's data will see the NEW data as soon as the lower
319 +file had changed, upon the next read(2) syscall (even if the file is still
320 +open!)  However, this doesn't work when the process re-reads the open file's
321 +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
322 +it).  Once we respond to ->readpage(s), then the kernel maps the page into
323 +the process's address space and there doesn't appear to be a way to force
324 +the kernel to invalidate those pages/mappings, and force the process to
325 +re-issue ->readpage.  If there's a way to invalidate active mappings and
326 +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
327 +the trick).
328 +
329 +Our current Unionfs code has to perform many file-revalidation calls.  It
330 +would be really nice if the VFS would export an optional file system hook
331 +->file_revalidate (similarly to dentry->d_revalidate) that will be called
332 +before each VFS op that has a "struct file" in it.
333 +
334 +Certain file systems have micro-second granularity (or better) for inode
335 +times, and asynchronous actions could cause those times to change with some
336 +small delay.  In such cases, Unionfs may see a changed inode time that only
337 +differs by a tiny fraction of a second: such a change may be a false
338 +positive indication that the lower object has changed, whereas if unionfs
339 +waits a little longer, that false indication will not be seen.  (These false
340 +positives are harmless, because they would at most cause unionfs to
341 +re-validate an object that may need no revalidation, and print a debugging
342 +message that clutters the console/logs.)  Therefore, to minimize the chances
343 +of these situations, we delay the detection of changed times by a small
344 +factor of a few seconds, called UNIONFS_MIN_CC_TIME (which defaults to 3
345 +seconds, as does NFS).  This means that we will detect the change, only a
346 +couple of seconds later, if indeed the time change persists in the lower
347 +file object.  This delayed detection has an added performance benefit: we
348 +reduce the number of times that unionfs has to revalidate objects, in case
349 +there's a lot of concurrent activity on both the upper and lower objects,
350 +for the same file(s).  Lastly, this delayed time attribute detection is
351 +similar to how NFS clients operate (e.g., acregmin).
352 +
353 +Finally, there is no way currently in Linux to prevent lower directories
354 +from being moved around (i.e., topology changes); there's no way to prevent
355 +modifications to directory sub-trees of whole file systems which are mounted
356 +read-write.  It is therefore possible for in-flight operations in unionfs to
357 +take place, while a lower directory is being moved around.  Therefore, if
358 +you try to, say, create a new file in a directory through unionfs, while the
359 +directory is being moved around directly, then the new file may get created
360 +in the new location where that directory was moved to.  This is a somewhat
361 +similar behaviour in NFS: an NFS client could be creating a new file while
362 +th NFS server is moving th directory around; the file will get successfully
363 +created in the new location.  (The one exception in unionfs is that if the
364 +branch is marked read-only by unionfs, then a copyup will take place.)
365 +
366 +For more information, see <http://unionfs.filesystems.org/>.
367 diff -Nurp linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/issues.txt linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/issues.txt
368 --- linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/issues.txt  1970-01-01 02:00:00.000000000 +0200
369 +++ linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/issues.txt  2010-07-30 15:50:42.994947708 +0300
370 @@ -0,0 +1,28 @@
371 +KNOWN Unionfs 2.x ISSUES:
372 +=========================
373 +
374 +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
375 +   confuses NFSv4.  Currently, unionfs_lookup() passes lookup intents to the
376 +   lower file-system, this eliminates part of the problem.  The remaining
377 +   calls to lookup_one_len may need to be changed to pass an intent.  We are
378 +   currently introducing VFS changes to fs/namei.c's do_path_lookup() to
379 +   allow proper file lookup and opening in stackable file systems.
380 +
381 +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
382 +   incorrectly complains about locking problems.  The problem boils down to
383 +   this: Lockdep considers all objects of a certain type to be in the same
384 +   class, for example, all inodes.  Lockdep doesn't like to see a lock held
385 +   on two inodes within the same task, and warns that it could lead to a
386 +   deadlock.  However, stackable file systems do precisely that: they lock
387 +   an upper object, and then a lower object, in a strict order to avoid
388 +   locking problems; in addition, Unionfs, as a fan-out file system, may
389 +   have to lock several lower inodes.  We are currently looking into Lockdep
390 +   to see how to make it aware of stackable file systems.  For now, we
391 +   temporarily disable lockdep when calling vfs methods on lower objects,
392 +   but only for those places where lockdep complained.  While this solution
393 +   may seem unclean, it is not without precedent: other places in the kernel
394 +   also do similar temporary disabling, of course after carefully having
395 +   checked that it is the right thing to do.  Anyway, you get any warnings
396 +   from Lockdep, please report them to the Unionfs maintainers.
397 +
398 +For more information, see <http://unionfs.filesystems.org/>.
399 diff -Nurp linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/rename.txt linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/rename.txt
400 --- linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/rename.txt  1970-01-01 02:00:00.000000000 +0200
401 +++ linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/rename.txt  2010-07-30 15:50:42.994947708 +0300
402 @@ -0,0 +1,31 @@
403 +Rename is a complex beast. The following table shows which rename(2) operations
404 +should succeed and which should fail.
405 +
406 +o: success
407 +E: error (either unionfs or vfs)
408 +X: EXDEV
409 +
410 +none = file does not exist
411 +file = file is a file
412 +dir  = file is a empty directory
413 +child= file is a non-empty directory
414 +wh   = file is a directory containing only whiteouts; this makes it logically
415 +               empty
416 +
417 +                      none    file    dir     child   wh
418 +file                  o       o       E       E       E
419 +dir                   o       E       o       E       o
420 +child                 X       E       X       E       X
421 +wh                    o       E       o       E       o
422 +
423 +
424 +Renaming directories:
425 +=====================
426 +
427 +Whenever a empty (either physically or logically) directory is being renamed,
428 +the following sequence of events should take place:
429 +
430 +1) Remove whiteouts from both source and destination directory
431 +2) Rename source to destination
432 +3) Make destination opaque to prevent anything under it from showing up
433 +
434 diff -Nurp linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/usage.txt linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/usage.txt
435 --- linux-2.6.35-rc6-git//Documentation/filesystems/unionfs/usage.txt   1970-01-01 02:00:00.000000000 +0200
436 +++ linux-2.6.35-rc6-git-unionfs//Documentation/filesystems/unionfs/usage.txt   2010-07-30 15:50:42.994947708 +0300
437 @@ -0,0 +1,134 @@
438 +Unionfs is a stackable unification file system, which can appear to merge
439 +the contents of several directories (branches), while keeping their physical
440 +content separate.  Unionfs is useful for unified source tree management,
441 +merged contents of split CD-ROM, merged separate software package
442 +directories, data grids, and more.  Unionfs allows any mix of read-only and
443 +read-write branches, as well as insertion and deletion of branches anywhere
444 +in the fan-out.  To maintain Unix semantics, Unionfs handles elimination of
445 +duplicates, partial-error conditions, and more.
446 +
447 +GENERAL SYNTAX
448 +==============
449 +
450 +# mount -t unionfs -o <OPTIONS>,<BRANCH-OPTIONS> none MOUNTPOINT
451 +
452 +OPTIONS can be any legal combination of:
453 +
454 +- ro           # mount file system read-only
455 +- rw           # mount file system read-write
456 +- remount      # remount the file system (see Branch Management below)
457 +- incgen       # increment generation no. (see Cache Consistency below)
458 +
459 +BRANCH-OPTIONS can be either (1) a list of branches given to the "dirs="
460 +option, or (2) a list of individual branch manipulation commands, combined
461 +with the "remount" option, and is further described in the "Branch
462 +Management" section below.
463 +
464 +The syntax for the "dirs=" mount option is:
465 +
466 +       dirs=branch[=ro|=rw][:...]
467 +
468 +The "dirs=" option takes a colon-delimited list of directories to compose
469 +the union, with an optional branch mode for each of those directories.
470 +Directories that come earlier (specified first, on the left) in the list
471 +have a higher precedence than those which come later.  Additionally,
472 +read-only or read-write permissions of the branch can be specified by
473 +appending =ro or =rw (default) to each directory.  See the Copyup section in
474 +concepts.txt, for a description of Unionfs's behavior when mixing read-only
475 +and read-write branches and mounts.
476 +
477 +Syntax:
478 +
479 +       dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
480 +
481 +Example:
482 +
483 +       dirs=/writable_branch=rw:/read-only_branch=ro
484 +
485 +
486 +BRANCH MANAGEMENT
487 +=================
488 +
489 +Once you mount your union for the first time, using the "dirs=" option, you
490 +can then change the union's overall mode or reconfigure the branches, using
491 +the remount option, as follows.
492 +
493 +To downgrade a union from read-write to read-only:
494 +
495 +# mount -t unionfs -o remount,ro none MOUNTPOINT
496 +
497 +To upgrade a union from read-only to read-write:
498 +
499 +# mount -t unionfs -o remount,rw none MOUNTPOINT
500 +
501 +To delete a branch /foo, regardless where it is in the current union:
502 +
503 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
504 +
505 +To insert (add) a branch /foo before /bar:
506 +
507 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
508 +
509 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
510 +
511 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
512 +
513 +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
514 +new highest-priority branch), you can use the above syntax, or use a short
515 +hand version as follows:
516 +
517 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
518 +
519 +To append a branch to the very end (new lowest-priority branch):
520 +
521 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
522 +
523 +To append a branch to the very end (new lowest-priority branch), in
524 +read-only mode:
525 +
526 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
527 +
528 +Finally, to change the mode of one existing branch, say /foo, from read-only
529 +to read-write, and change /bar from read-write to read-only:
530 +
531 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
532 +
533 +Note: in Unionfs 2.x, you cannot set the leftmost branch to readonly because
534 +then Unionfs won't have any writable place for copyups to take place.
535 +Moreover, the VFS can get confused when it tries to modify something in a
536 +file system mounted read-write, but isn't permitted to write to it.
537 +Instead, you should set the whole union as readonly, as described above.
538 +If, however, you must set the leftmost branch as readonly, perhaps so you
539 +can get a snapshot of it at a point in time, then you should insert a new
540 +writable top-level branch, and mark the one you want as readonly.  This can
541 +be accomplished as follows, assuming that /foo is your current leftmost
542 +branch:
543 +
544 +# mount -t tmpfs -o size=NNN /new
545 +# mount -t unionfs -o remount,add=/new,mode=/foo=ro none MOUNTPOINT
546 +<do what you want safely in /foo>
547 +# mount -t unionfs -o remount,del=/new,mode=/foo=rw none MOUNTPOINT
548 +<check if there's anything in /new you want to preserve>
549 +# umount /new
550 +
551 +CACHE CONSISTENCY
552 +=================
553 +
554 +If you modify any file on any of the lower branches directly, while there is
555 +a Unionfs 2.x mounted above any of those branches, you should tell Unionfs
556 +to purge its caches and re-get the objects.  To do that, you have to
557 +increment the generation number of the superblock using the following
558 +command:
559 +
560 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
561 +
562 +Note that the older way of incrementing the generation number using an
563 +ioctl, is no longer supported in Unionfs 2.0 and newer.  Ioctls in general
564 +are not encouraged.  Plus, an ioctl is per-file concept, whereas the
565 +generation number is a per-file-system concept.  Worse, such an ioctl
566 +requires an open file, which then has to be invalidated by the very nature
567 +of the generation number increase (read: the old generation increase ioctl
568 +was pretty racy).
569 +
570 +
571 +For more information, see <http://unionfs.filesystems.org/>.
572 diff -Nurp linux-2.6.35-rc6-git//fs/Kconfig linux-2.6.35-rc6-git-unionfs//fs/Kconfig
573 --- linux-2.6.35-rc6-git//fs/Kconfig    2010-05-17 00:17:36.000000000 +0300
574 +++ linux-2.6.35-rc6-git-unionfs//fs/Kconfig    2010-07-30 15:50:42.995947739 +0300
575 @@ -169,6 +169,7 @@ if MISC_FILESYSTEMS
576  source "fs/adfs/Kconfig"
577  source "fs/affs/Kconfig"
578  source "fs/ecryptfs/Kconfig"
579 +source "fs/unionfs/Kconfig"
580  source "fs/hfs/Kconfig"
581  source "fs/hfsplus/Kconfig"
582  source "fs/befs/Kconfig"
583 diff -Nurp linux-2.6.35-rc6-git//fs/Makefile linux-2.6.35-rc6-git-unionfs//fs/Makefile
584 --- linux-2.6.35-rc6-git//fs/Makefile   2010-07-30 14:05:39.182947749 +0300
585 +++ linux-2.6.35-rc6-git-unionfs//fs/Makefile   2010-07-30 15:50:42.995947739 +0300
586 @@ -84,6 +84,7 @@ obj-$(CONFIG_ISO9660_FS)      += isofs/
587  obj-$(CONFIG_HFSPLUS_FS)       += hfsplus/ # Before hfs to find wrapped HFS+
588  obj-$(CONFIG_HFS_FS)           += hfs/
589  obj-$(CONFIG_ECRYPT_FS)                += ecryptfs/
590 +obj-$(CONFIG_UNION_FS)         += unionfs/
591  obj-$(CONFIG_VXFS_FS)          += freevxfs/
592  obj-$(CONFIG_NFS_FS)           += nfs/
593  obj-$(CONFIG_EXPORTFS)         += exportfs/
594 diff -Nurp linux-2.6.35-rc6-git//fs/namei.c linux-2.6.35-rc6-git-unionfs//fs/namei.c
595 --- linux-2.6.35-rc6-git//fs/namei.c    2010-07-30 14:05:39.270978942 +0300
596 +++ linux-2.6.35-rc6-git-unionfs//fs/namei.c    2010-07-30 15:50:42.995947739 +0300
597 @@ -386,6 +386,7 @@ void release_open_intent(struct nameidat
598         else
599                 fput(nd->intent.open.file);
600  }
601 +EXPORT_SYMBOL_GPL(release_open_intent);
602  
603  static inline struct dentry *
604  do_revalidate(struct dentry *dentry, struct nameidata *nd)
605 diff -Nurp linux-2.6.35-rc6-git//fs/splice.c linux-2.6.35-rc6-git-unionfs//fs/splice.c
606 --- linux-2.6.35-rc6-git//fs/splice.c   2010-07-30 14:05:39.323970675 +0300
607 +++ linux-2.6.35-rc6-git-unionfs//fs/splice.c   2010-07-30 16:16:25.730042698 +0300
608 @@ -1104,8 +1104,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
609  /*
610   * Attempt to initiate a splice from pipe to file.
611   */
612 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
613 -                          loff_t *ppos, size_t len, unsigned int flags)
614 +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
615 +                    loff_t *ppos, size_t len, unsigned int flags)
616  {
617         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
618                                 loff_t *, size_t, unsigned int);
619 @@ -1128,13 +1128,14 @@ static long do_splice_from(struct pipe_i
620  
621         return splice_write(pipe, out, ppos, len, flags);
622  }
623 +EXPORT_SYMBOL_GPL(vfs_splice_from);
624  
625  /*
626   * Attempt to initiate a splice from a file to a pipe.
627   */
628 -static long do_splice_to(struct file *in, loff_t *ppos,
629 -                        struct pipe_inode_info *pipe, size_t len,
630 -                        unsigned int flags)
631 +long vfs_splice_to(struct file *in, loff_t *ppos,
632 +                  struct pipe_inode_info *pipe, size_t len,
633 +                  unsigned int flags)
634  {
635         ssize_t (*splice_read)(struct file *, loff_t *,
636                                struct pipe_inode_info *, size_t, unsigned int);
637 @@ -1154,6 +1155,7 @@ static long do_splice_to(struct file *in
638  
639         return splice_read(in, ppos, pipe, len, flags);
640  }
641 +EXPORT_SYMBOL_GPL(vfs_splice_to);
642  
643  /**
644   * splice_direct_to_actor - splices data directly between two non-pipes
645 @@ -1223,7 +1225,7 @@ ssize_t splice_direct_to_actor(struct fi
646                 size_t read_len;
647                 loff_t pos = sd->pos, prev_pos = pos;
648  
649 -               ret = do_splice_to(in, &pos, pipe, len, flags);
650 +               ret = vfs_splice_to(in, &pos, pipe, len, flags);
651                 if (unlikely(ret <= 0))
652                         goto out_release;
653  
654 @@ -1282,7 +1284,7 @@ static int direct_splice_actor(struct pi
655  {
656         struct file *file = sd->u.file;
657  
658 -       return do_splice_from(pipe, file, &file->f_pos, sd->total_len,
659 +       return vfs_splice_from(pipe, file, &file->f_pos, sd->total_len,
660                               sd->flags);
661  }
662  
663 @@ -1380,7 +1382,7 @@ static long do_splice(struct file *in, l
664                 } else
665                         off = &out->f_pos;
666  
667 -               ret = do_splice_from(ipipe, out, off, len, flags);
668 +               ret = vfs_splice_from(ipipe, out, off, len, flags);
669  
670                 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
671                         ret = -EFAULT;
672 @@ -1400,7 +1402,7 @@ static long do_splice(struct file *in, l
673                 } else
674                         off = &in->f_pos;
675  
676 -               ret = do_splice_to(in, off, opipe, len, flags);
677 +               ret = vfs_splice_to(in, off, opipe, len, flags);
678  
679                 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
680                         ret = -EFAULT;
681 diff -Nurp linux-2.6.35-rc6-git//fs/stack.c linux-2.6.35-rc6-git-unionfs//fs/stack.c
682 --- linux-2.6.35-rc6-git//fs/stack.c    2010-05-17 00:17:36.000000000 +0300
683 +++ linux-2.6.35-rc6-git-unionfs//fs/stack.c    2010-07-30 15:50:42.996947572 +0300
684 @@ -1,8 +1,20 @@
685 +/*
686 + * Copyright (c) 2006-2009 Erez Zadok
687 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
688 + * Copyright (c) 2006-2009 Stony Brook University
689 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
690 + *
691 + * This program is free software; you can redistribute it and/or modify
692 + * it under the terms of the GNU General Public License version 2 as
693 + * published by the Free Software Foundation.
694 + */
695 +
696  #include <linux/module.h>
697  #include <linux/fs.h>
698  #include <linux/fs_stack.h>
699  
700 -/* does _NOT_ require i_mutex to be held.
701 +/*
702 + * does _NOT_ require i_mutex to be held.
703   *
704   * This function cannot be inlined since i_size_{read,write} is rather
705   * heavy-weight on 32-bit systems
706 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/commonfops.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/commonfops.c
707 --- linux-2.6.35-rc6-git//fs/unionfs/commonfops.c       1970-01-01 02:00:00.000000000 +0200
708 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/commonfops.c       2010-07-30 15:50:50.557108046 +0300
709 @@ -0,0 +1,896 @@
710 +/*
711 + * Copyright (c) 2003-2010 Erez Zadok
712 + * Copyright (c) 2003-2006 Charles P. Wright
713 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
714 + * Copyright (c) 2005-2006 Junjiro Okajima
715 + * Copyright (c) 2005      Arun M. Krishnakumar
716 + * Copyright (c) 2004-2006 David P. Quigley
717 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
718 + * Copyright (c) 2003      Puja Gupta
719 + * Copyright (c) 2003      Harikesavan Krishnan
720 + * Copyright (c) 2003-2010 Stony Brook University
721 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
722 + *
723 + * This program is free software; you can redistribute it and/or modify
724 + * it under the terms of the GNU General Public License version 2 as
725 + * published by the Free Software Foundation.
726 + */
727 +
728 +#include "union.h"
729 +
730 +/*
731 + * 1) Copyup the file
732 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
733 + * stolen from NFS's silly rename
734 + */
735 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
736 +                              struct dentry *parent, int bstart, int bindex)
737 +{
738 +       static unsigned int counter;
739 +       const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
740 +       const int countersize = sizeof(counter) * 2;
741 +       const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
742 +       char name[nlen + 1];
743 +       int err;
744 +       struct dentry *tmp_dentry = NULL;
745 +       struct dentry *lower_dentry;
746 +       struct dentry *lower_dir_dentry = NULL;
747 +
748 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
749 +
750 +       sprintf(name, ".unionfs%*.*lx",
751 +               i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
752 +
753 +       /*
754 +        * Loop, looking for an unused temp name to copyup to.
755 +        *
756 +        * It's somewhat silly that we look for a free temp tmp name in the
757 +        * source branch (bstart) instead of the dest branch (bindex), where
758 +        * the final name will be created.  We _will_ catch it if somehow
759 +        * the name exists in the dest branch, but it'd be nice to catch it
760 +        * sooner than later.
761 +        */
762 +retry:
763 +       tmp_dentry = NULL;
764 +       do {
765 +               char *suffix = name + nlen - countersize;
766 +
767 +               dput(tmp_dentry);
768 +               counter++;
769 +               sprintf(suffix, "%*.*x", countersize, countersize, counter);
770 +
771 +               pr_debug("unionfs: trying to rename %s to %s\n",
772 +                        dentry->d_name.name, name);
773 +
774 +               tmp_dentry = lookup_lck_len(name, lower_dentry->d_parent,
775 +                                           nlen);
776 +               if (IS_ERR(tmp_dentry)) {
777 +                       err = PTR_ERR(tmp_dentry);
778 +                       goto out;
779 +               }
780 +       } while (tmp_dentry->d_inode != NULL);  /* need negative dentry */
781 +       dput(tmp_dentry);
782 +
783 +       err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
784 +                               i_size_read(file->f_path.dentry->d_inode));
785 +       if (err) {
786 +               if (unlikely(err == -EEXIST))
787 +                       goto retry;
788 +               goto out;
789 +       }
790 +
791 +       /* bring it to the same state as an unlinked file */
792 +       lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
793 +       if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
794 +               atomic_inc(&lower_dentry->d_inode->i_count);
795 +               unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
796 +                                           lower_dentry->d_inode);
797 +       }
798 +       lower_dir_dentry = lock_parent(lower_dentry);
799 +       err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
800 +       unlock_dir(lower_dir_dentry);
801 +
802 +out:
803 +       if (!err)
804 +               unionfs_check_dentry(dentry);
805 +       return err;
806 +}
807 +
808 +/*
809 + * put all references held by upper struct file and free lower file pointer
810 + * array
811 + */
812 +static void cleanup_file(struct file *file)
813 +{
814 +       int bindex, bstart, bend;
815 +       struct file **lower_files;
816 +       struct file *lower_file;
817 +       struct super_block *sb = file->f_path.dentry->d_sb;
818 +
819 +       lower_files = UNIONFS_F(file)->lower_files;
820 +       bstart = fbstart(file);
821 +       bend = fbend(file);
822 +
823 +       for (bindex = bstart; bindex <= bend; bindex++) {
824 +               int i;  /* holds (possibly) updated branch index */
825 +               int old_bid;
826 +
827 +               lower_file = unionfs_lower_file_idx(file, bindex);
828 +               if (!lower_file)
829 +                       continue;
830 +
831 +               /*
832 +                * Find new index of matching branch with an open
833 +                * file, since branches could have been added or
834 +                * deleted causing the one with open files to shift.
835 +                */
836 +               old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
837 +               i = branch_id_to_idx(sb, old_bid);
838 +               if (unlikely(i < 0)) {
839 +                       printk(KERN_ERR "unionfs: no superblock for "
840 +                              "file %p\n", file);
841 +                       continue;
842 +               }
843 +
844 +               /* decrement count of open files */
845 +               branchput(sb, i);
846 +               /*
847 +                * fput will perform an mntput for us on the correct branch.
848 +                * Although we're using the file's old branch configuration,
849 +                * bindex, which is the old index, correctly points to the
850 +                * right branch in the file's branch list.  In other words,
851 +                * we're going to mntput the correct branch even if branches
852 +                * have been added/removed.
853 +                */
854 +               fput(lower_file);
855 +               UNIONFS_F(file)->lower_files[bindex] = NULL;
856 +               UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
857 +       }
858 +
859 +       UNIONFS_F(file)->lower_files = NULL;
860 +       kfree(lower_files);
861 +       kfree(UNIONFS_F(file)->saved_branch_ids);
862 +       /* set to NULL because caller needs to know if to kfree on error */
863 +       UNIONFS_F(file)->saved_branch_ids = NULL;
864 +}
865 +
866 +/* open all lower files for a given file */
867 +static int open_all_files(struct file *file)
868 +{
869 +       int bindex, bstart, bend, err = 0;
870 +       struct file *lower_file;
871 +       struct dentry *lower_dentry;
872 +       struct dentry *dentry = file->f_path.dentry;
873 +       struct super_block *sb = dentry->d_sb;
874 +
875 +       bstart = dbstart(dentry);
876 +       bend = dbend(dentry);
877 +
878 +       for (bindex = bstart; bindex <= bend; bindex++) {
879 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
880 +               if (!lower_dentry)
881 +                       continue;
882 +
883 +               dget(lower_dentry);
884 +               unionfs_mntget(dentry, bindex);
885 +               branchget(sb, bindex);
886 +
887 +               lower_file =
888 +                       dentry_open(lower_dentry,
889 +                                   unionfs_lower_mnt_idx(dentry, bindex),
890 +                                   file->f_flags, current_cred());
891 +               if (IS_ERR(lower_file)) {
892 +                       branchput(sb, bindex);
893 +                       err = PTR_ERR(lower_file);
894 +                       goto out;
895 +               } else {
896 +                       unionfs_set_lower_file_idx(file, bindex, lower_file);
897 +               }
898 +       }
899 +out:
900 +       return err;
901 +}
902 +
903 +/* open the highest priority file for a given upper file */
904 +static int open_highest_file(struct file *file, bool willwrite)
905 +{
906 +       int bindex, bstart, bend, err = 0;
907 +       struct file *lower_file;
908 +       struct dentry *lower_dentry;
909 +       struct dentry *dentry = file->f_path.dentry;
910 +       struct dentry *parent = dget_parent(dentry);
911 +       struct inode *parent_inode = parent->d_inode;
912 +       struct super_block *sb = dentry->d_sb;
913 +
914 +       bstart = dbstart(dentry);
915 +       bend = dbend(dentry);
916 +
917 +       lower_dentry = unionfs_lower_dentry(dentry);
918 +       if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
919 +               for (bindex = bstart - 1; bindex >= 0; bindex--) {
920 +                       err = copyup_file(parent_inode, file, bstart, bindex,
921 +                                         i_size_read(dentry->d_inode));
922 +                       if (!err)
923 +                               break;
924 +               }
925 +               atomic_set(&UNIONFS_F(file)->generation,
926 +                          atomic_read(&UNIONFS_I(dentry->d_inode)->
927 +                                      generation));
928 +               goto out;
929 +       }
930 +
931 +       dget(lower_dentry);
932 +       unionfs_mntget(dentry, bstart);
933 +       lower_file = dentry_open(lower_dentry,
934 +                                unionfs_lower_mnt_idx(dentry, bstart),
935 +                                file->f_flags, current_cred());
936 +       if (IS_ERR(lower_file)) {
937 +               err = PTR_ERR(lower_file);
938 +               goto out;
939 +       }
940 +       branchget(sb, bstart);
941 +       unionfs_set_lower_file(file, lower_file);
942 +       /* Fix up the position. */
943 +       lower_file->f_pos = file->f_pos;
944 +
945 +       memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
946 +out:
947 +       dput(parent);
948 +       return err;
949 +}
950 +
951 +/* perform a delayed copyup of a read-write file on a read-only branch */
952 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
953 +{
954 +       int bindex, bstart, bend, err = 0;
955 +       struct dentry *dentry = file->f_path.dentry;
956 +       struct inode *parent_inode = parent->d_inode;
957 +
958 +       bstart = fbstart(file);
959 +       bend = fbend(file);
960 +
961 +       BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
962 +
963 +       unionfs_check_file(file);
964 +       for (bindex = bstart - 1; bindex >= 0; bindex--) {
965 +               if (!d_deleted(dentry))
966 +                       err = copyup_file(parent_inode, file, bstart,
967 +                                         bindex,
968 +                                         i_size_read(dentry->d_inode));
969 +               else
970 +                       err = copyup_deleted_file(file, dentry, parent,
971 +                                                 bstart, bindex);
972 +               /* if succeeded, set lower open-file flags and break */
973 +               if (!err) {
974 +                       struct file *lower_file;
975 +                       lower_file = unionfs_lower_file_idx(file, bindex);
976 +                       lower_file->f_flags = file->f_flags;
977 +                       break;
978 +               }
979 +       }
980 +       if (err || (bstart <= fbstart(file)))
981 +               goto out;
982 +       bend = fbend(file);
983 +       for (bindex = bstart; bindex <= bend; bindex++) {
984 +               if (unionfs_lower_file_idx(file, bindex)) {
985 +                       branchput(dentry->d_sb, bindex);
986 +                       fput(unionfs_lower_file_idx(file, bindex));
987 +                       unionfs_set_lower_file_idx(file, bindex, NULL);
988 +               }
989 +       }
990 +       path_put_lowers(dentry, bstart, bend, false);
991 +       iput_lowers(dentry->d_inode, bstart, bend, false);
992 +       /* for reg file, we only open it "once" */
993 +       fbend(file) = fbstart(file);
994 +       dbend(dentry) = dbstart(dentry);
995 +       ibend(dentry->d_inode) = ibstart(dentry->d_inode);
996 +
997 +out:
998 +       unionfs_check_file(file);
999 +       return err;
1000 +}
1001 +
1002 +/*
1003 + * Helper function for unionfs_file_revalidate/locked.
1004 + * Expects dentry/parent to be locked already, and revalidated.
1005 + */
1006 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1007 +                                    struct dentry *parent,
1008 +                                    struct super_block *sb, int sbgen,
1009 +                                    int dgen, bool willwrite)
1010 +{
1011 +       int fgen;
1012 +       int bstart, bend, orig_brid;
1013 +       int size;
1014 +       int err = 0;
1015 +
1016 +       fgen = atomic_read(&UNIONFS_F(file)->generation);
1017 +
1018 +       /*
1019 +        * There are two cases we are interested in.  The first is if the
1020 +        * generation is lower than the super-block.  The second is if
1021 +        * someone has copied up this file from underneath us, we also need
1022 +        * to refresh things.
1023 +        */
1024 +       if (d_deleted(dentry) ||
1025 +           (sbgen <= fgen &&
1026 +            dbstart(dentry) == fbstart(file) &&
1027 +            unionfs_lower_file(file)))
1028 +               goto out_may_copyup;
1029 +
1030 +       /* save orig branch ID */
1031 +       orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1032 +
1033 +       /* First we throw out the existing files. */
1034 +       cleanup_file(file);
1035 +
1036 +       /* Now we reopen the file(s) as in unionfs_open. */
1037 +       bstart = fbstart(file) = dbstart(dentry);
1038 +       bend = fbend(file) = dbend(dentry);
1039 +
1040 +       size = sizeof(struct file *) * sbmax(sb);
1041 +       UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1042 +       if (unlikely(!UNIONFS_F(file)->lower_files)) {
1043 +               err = -ENOMEM;
1044 +               goto out;
1045 +       }
1046 +       size = sizeof(int) * sbmax(sb);
1047 +       UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1048 +       if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1049 +               err = -ENOMEM;
1050 +               goto out;
1051 +       }
1052 +
1053 +       if (S_ISDIR(dentry->d_inode->i_mode)) {
1054 +               /* We need to open all the files. */
1055 +               err = open_all_files(file);
1056 +               if (err)
1057 +                       goto out;
1058 +       } else {
1059 +               int new_brid;
1060 +               /* We only open the highest priority branch. */
1061 +               err = open_highest_file(file, willwrite);
1062 +               if (err)
1063 +                       goto out;
1064 +               new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1065 +               if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1066 +                       /*
1067 +                        * If we re-opened the file on a different branch
1068 +                        * than the original one, and this was due to a new
1069 +                        * branch inserted, then update the mnt counts of
1070 +                        * the old and new branches accordingly.
1071 +                        */
1072 +                       unionfs_mntget(dentry, bstart);
1073 +                       unionfs_mntput(sb->s_root,
1074 +                                      branch_id_to_idx(sb, orig_brid));
1075 +               }
1076 +               /* regular files have only one open lower file */
1077 +               fbend(file) = fbstart(file);
1078 +       }
1079 +       atomic_set(&UNIONFS_F(file)->generation,
1080 +                  atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1081 +
1082 +out_may_copyup:
1083 +       /* Copyup on the first write to a file on a readonly branch. */
1084 +       if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1085 +           !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1086 +           is_robranch(dentry)) {
1087 +               pr_debug("unionfs: do delay copyup of \"%s\"\n",
1088 +                        dentry->d_name.name);
1089 +               err = do_delayed_copyup(file, parent);
1090 +               /* regular files have only one open lower file */
1091 +               if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1092 +                       fbend(file) = fbstart(file);
1093 +       }
1094 +
1095 +out:
1096 +       if (err) {
1097 +               kfree(UNIONFS_F(file)->lower_files);
1098 +               kfree(UNIONFS_F(file)->saved_branch_ids);
1099 +       }
1100 +       return err;
1101 +}
1102 +
1103 +/*
1104 + * Revalidate the struct file
1105 + * @file: file to revalidate
1106 + * @parent: parent dentry (locked by caller)
1107 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1108 + * Caller must lock/unlock dentry's branch configuration.
1109 + */
1110 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1111 +                           bool willwrite)
1112 +{
1113 +       struct super_block *sb;
1114 +       struct dentry *dentry;
1115 +       int sbgen, dgen;
1116 +       int err = 0;
1117 +
1118 +       dentry = file->f_path.dentry;
1119 +       sb = dentry->d_sb;
1120 +       verify_locked(dentry);
1121 +       verify_locked(parent);
1122 +
1123 +       /*
1124 +        * First revalidate the dentry inside struct file,
1125 +        * but not unhashed dentries.
1126 +        */
1127 +       if (!d_deleted(dentry) &&
1128 +           !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1129 +               err = -ESTALE;
1130 +               goto out;
1131 +       }
1132 +
1133 +       sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1134 +       dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1135 +
1136 +       if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1137 +               pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1138 +                        dentry->d_name.name);
1139 +               err = -ESTALE;
1140 +               goto out;
1141 +       }
1142 +
1143 +       err = __unionfs_file_revalidate(file, dentry, parent, sb,
1144 +                                       sbgen, dgen, willwrite);
1145 +out:
1146 +       return err;
1147 +}
1148 +
1149 +/* unionfs_open helper function: open a directory */
1150 +static int __open_dir(struct inode *inode, struct file *file)
1151 +{
1152 +       struct dentry *lower_dentry;
1153 +       struct file *lower_file;
1154 +       int bindex, bstart, bend;
1155 +       struct vfsmount *mnt;
1156 +
1157 +       bstart = fbstart(file) = dbstart(file->f_path.dentry);
1158 +       bend = fbend(file) = dbend(file->f_path.dentry);
1159 +
1160 +       for (bindex = bstart; bindex <= bend; bindex++) {
1161 +               lower_dentry =
1162 +                       unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1163 +               if (!lower_dentry)
1164 +                       continue;
1165 +
1166 +               dget(lower_dentry);
1167 +               unionfs_mntget(file->f_path.dentry, bindex);
1168 +               mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1169 +               lower_file = dentry_open(lower_dentry, mnt, file->f_flags,
1170 +                                        current_cred());
1171 +               if (IS_ERR(lower_file))
1172 +                       return PTR_ERR(lower_file);
1173 +
1174 +               unionfs_set_lower_file_idx(file, bindex, lower_file);
1175 +
1176 +               /*
1177 +                * The branchget goes after the open, because otherwise
1178 +                * we would miss the reference on release.
1179 +                */
1180 +               branchget(inode->i_sb, bindex);
1181 +       }
1182 +
1183 +       return 0;
1184 +}
1185 +
1186 +/* unionfs_open helper function: open a file */
1187 +static int __open_file(struct inode *inode, struct file *file,
1188 +                      struct dentry *parent)
1189 +{
1190 +       struct dentry *lower_dentry;
1191 +       struct file *lower_file;
1192 +       int lower_flags;
1193 +       int bindex, bstart, bend;
1194 +
1195 +       lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1196 +       lower_flags = file->f_flags;
1197 +
1198 +       bstart = fbstart(file) = dbstart(file->f_path.dentry);
1199 +       bend = fbend(file) = dbend(file->f_path.dentry);
1200 +
1201 +       /*
1202 +        * check for the permission for lower file.  If the error is
1203 +        * COPYUP_ERR, copyup the file.
1204 +        */
1205 +       if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1206 +               /*
1207 +                * if the open will change the file, copy it up otherwise
1208 +                * defer it.
1209 +                */
1210 +               if (lower_flags & O_TRUNC) {
1211 +                       int size = 0;
1212 +                       int err = -EROFS;
1213 +
1214 +                       /* copyup the file */
1215 +                       for (bindex = bstart - 1; bindex >= 0; bindex--) {
1216 +                               err = copyup_file(parent->d_inode, file,
1217 +                                                 bstart, bindex, size);
1218 +                               if (!err)
1219 +                                       break;
1220 +                       }
1221 +                       return err;
1222 +               } else {
1223 +                       /*
1224 +                        * turn off writeable flags, to force delayed copyup
1225 +                        * by caller.
1226 +                        */
1227 +                       lower_flags &= ~(OPEN_WRITE_FLAGS);
1228 +               }
1229 +       }
1230 +
1231 +       dget(lower_dentry);
1232 +
1233 +       /*
1234 +        * dentry_open will decrement mnt refcnt if err.
1235 +        * otherwise fput() will do an mntput() for us upon file close.
1236 +        */
1237 +       unionfs_mntget(file->f_path.dentry, bstart);
1238 +       lower_file =
1239 +               dentry_open(lower_dentry,
1240 +                           unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1241 +                           lower_flags, current_cred());
1242 +       if (IS_ERR(lower_file))
1243 +               return PTR_ERR(lower_file);
1244 +
1245 +       unionfs_set_lower_file(file, lower_file);
1246 +       branchget(inode->i_sb, bstart);
1247 +
1248 +       return 0;
1249 +}
1250 +
1251 +int unionfs_open(struct inode *inode, struct file *file)
1252 +{
1253 +       int err = 0;
1254 +       struct file *lower_file = NULL;
1255 +       struct dentry *dentry = file->f_path.dentry;
1256 +       struct dentry *parent;
1257 +       int bindex = 0, bstart = 0, bend = 0;
1258 +       int size;
1259 +       int valid = 0;
1260 +
1261 +       unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1262 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1263 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1264 +
1265 +       /* don't open unhashed/deleted files */
1266 +       if (d_deleted(dentry)) {
1267 +               err = -ENOENT;
1268 +               goto out_nofree;
1269 +       }
1270 +
1271 +       /* XXX: should I change 'false' below to the 'willwrite' flag? */
1272 +       valid = __unionfs_d_revalidate(dentry, parent, false);
1273 +       if (unlikely(!valid)) {
1274 +               err = -ESTALE;
1275 +               goto out_nofree;
1276 +       }
1277 +
1278 +       file->private_data =
1279 +               kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1280 +       if (unlikely(!UNIONFS_F(file))) {
1281 +               err = -ENOMEM;
1282 +               goto out_nofree;
1283 +       }
1284 +       fbstart(file) = -1;
1285 +       fbend(file) = -1;
1286 +       atomic_set(&UNIONFS_F(file)->generation,
1287 +                  atomic_read(&UNIONFS_I(inode)->generation));
1288 +
1289 +       size = sizeof(struct file *) * sbmax(inode->i_sb);
1290 +       UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1291 +       if (unlikely(!UNIONFS_F(file)->lower_files)) {
1292 +               err = -ENOMEM;
1293 +               goto out;
1294 +       }
1295 +       size = sizeof(int) * sbmax(inode->i_sb);
1296 +       UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1297 +       if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1298 +               err = -ENOMEM;
1299 +               goto out;
1300 +       }
1301 +
1302 +       bstart = fbstart(file) = dbstart(dentry);
1303 +       bend = fbend(file) = dbend(dentry);
1304 +
1305 +       /*
1306 +        * open all directories and make the unionfs file struct point to
1307 +        * these lower file structs
1308 +        */
1309 +       if (S_ISDIR(inode->i_mode))
1310 +               err = __open_dir(inode, file);  /* open a dir */
1311 +       else
1312 +               err = __open_file(inode, file, parent); /* open a file */
1313 +
1314 +       /* freeing the allocated resources, and fput the opened files */
1315 +       if (err) {
1316 +               for (bindex = bstart; bindex <= bend; bindex++) {
1317 +                       lower_file = unionfs_lower_file_idx(file, bindex);
1318 +                       if (!lower_file)
1319 +                               continue;
1320 +
1321 +                       branchput(dentry->d_sb, bindex);
1322 +                       /* fput calls dput for lower_dentry */
1323 +                       fput(lower_file);
1324 +               }
1325 +       }
1326 +
1327 +out:
1328 +       if (err) {
1329 +               kfree(UNIONFS_F(file)->lower_files);
1330 +               kfree(UNIONFS_F(file)->saved_branch_ids);
1331 +               kfree(UNIONFS_F(file));
1332 +       }
1333 +out_nofree:
1334 +       if (!err) {
1335 +               unionfs_postcopyup_setmnt(dentry);
1336 +               unionfs_copy_attr_times(inode);
1337 +               unionfs_check_file(file);
1338 +               unionfs_check_inode(inode);
1339 +       }
1340 +       unionfs_unlock_dentry(dentry);
1341 +       unionfs_unlock_parent(dentry, parent);
1342 +       unionfs_read_unlock(inode->i_sb);
1343 +       return err;
1344 +}
1345 +
1346 +/*
1347 + * release all lower object references & free the file info structure
1348 + *
1349 + * No need to grab sb info's rwsem.
1350 + */
1351 +int unionfs_file_release(struct inode *inode, struct file *file)
1352 +{
1353 +       struct file *lower_file = NULL;
1354 +       struct unionfs_file_info *fileinfo;
1355 +       struct unionfs_inode_info *inodeinfo;
1356 +       struct super_block *sb = inode->i_sb;
1357 +       struct dentry *dentry = file->f_path.dentry;
1358 +       struct dentry *parent;
1359 +       int bindex, bstart, bend;
1360 +       int fgen, err = 0;
1361 +
1362 +       /*
1363 +        * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
1364 +        * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
1365 +        * has been causing false positives in file system stacking layers.
1366 +        * In particular, our ->mmap is called after sys_mmap2 already holds
1367 +        * mmap_sem, then we lock our own mutexes; but earlier, it's
1368 +        * possible for lockdep to have locked our mutexes first, and then
1369 +        * we call a lower ->readdir which could call might_fault.  The
1370 +        * different ordering of the locks is what lockdep complains about
1371 +        * -- unnecessarily.  Therefore, we have no choice but to tell
1372 +        * lockdep to temporarily turn off lockdep here.  Note: the comments
1373 +        * inside might_sleep also suggest that it would have been
1374 +        * nicer to only annotate paths that needs that might_lock_read.
1375 +        */
1376 +       lockdep_off();
1377 +       unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1378 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1379 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1380 +
1381 +       /*
1382 +        * We try to revalidate, but the VFS ignores return return values
1383 +        * from file->release, so we must always try to succeed here,
1384 +        * including to do the kfree and dput below.  So if revalidation
1385 +        * failed, all we can do is print some message and keep going.
1386 +        */
1387 +       err = unionfs_file_revalidate(file, parent,
1388 +                                     UNIONFS_F(file)->wrote_to_file);
1389 +       if (!err)
1390 +               unionfs_check_file(file);
1391 +       fileinfo = UNIONFS_F(file);
1392 +       BUG_ON(file->f_path.dentry->d_inode != inode);
1393 +       inodeinfo = UNIONFS_I(inode);
1394 +
1395 +       /* fput all the lower files */
1396 +       fgen = atomic_read(&fileinfo->generation);
1397 +       bstart = fbstart(file);
1398 +       bend = fbend(file);
1399 +
1400 +       for (bindex = bstart; bindex <= bend; bindex++) {
1401 +               lower_file = unionfs_lower_file_idx(file, bindex);
1402 +
1403 +               if (lower_file) {
1404 +                       unionfs_set_lower_file_idx(file, bindex, NULL);
1405 +                       fput(lower_file);
1406 +                       branchput(sb, bindex);
1407 +               }
1408 +
1409 +               /* if there are no more refs to the dentry, dput it */
1410 +               if (d_deleted(dentry)) {
1411 +                       dput(unionfs_lower_dentry_idx(dentry, bindex));
1412 +                       unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1413 +               }
1414 +       }
1415 +
1416 +       kfree(fileinfo->lower_files);
1417 +       kfree(fileinfo->saved_branch_ids);
1418 +
1419 +       if (fileinfo->rdstate) {
1420 +               fileinfo->rdstate->access = jiffies;
1421 +               spin_lock(&inodeinfo->rdlock);
1422 +               inodeinfo->rdcount++;
1423 +               list_add_tail(&fileinfo->rdstate->cache,
1424 +                             &inodeinfo->readdircache);
1425 +               mark_inode_dirty(inode);
1426 +               spin_unlock(&inodeinfo->rdlock);
1427 +               fileinfo->rdstate = NULL;
1428 +       }
1429 +       kfree(fileinfo);
1430 +
1431 +       unionfs_unlock_dentry(dentry);
1432 +       unionfs_unlock_parent(dentry, parent);
1433 +       unionfs_read_unlock(sb);
1434 +       lockdep_on();
1435 +       return err;
1436 +}
1437 +
1438 +/* pass the ioctl to the lower fs */
1439 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1440 +{
1441 +       struct file *lower_file;
1442 +       int err;
1443 +
1444 +       lower_file = unionfs_lower_file(file);
1445 +
1446 +       err = -ENOTTY;
1447 +       if (!lower_file || !lower_file->f_op)
1448 +               goto out;
1449 +       if (lower_file->f_op->unlocked_ioctl) {
1450 +               err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1451 +       } else if (lower_file->f_op->ioctl) {
1452 +               lock_kernel();
1453 +               err = lower_file->f_op->ioctl(
1454 +                       lower_file->f_path.dentry->d_inode,
1455 +                       lower_file, cmd, arg);
1456 +               unlock_kernel();
1457 +       }
1458 +
1459 +out:
1460 +       return err;
1461 +}
1462 +
1463 +/*
1464 + * return to user-space the branch indices containing the file in question
1465 + *
1466 + * We use fd_set and therefore we are limited to the number of the branches
1467 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1468 + */
1469 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1470 +                                  unsigned int cmd, unsigned long arg)
1471 +{
1472 +       int err = 0;
1473 +       fd_set branchlist;
1474 +       int bstart = 0, bend = 0, bindex = 0;
1475 +       int orig_bstart, orig_bend;
1476 +       struct dentry *dentry, *lower_dentry;
1477 +       struct vfsmount *mnt;
1478 +
1479 +       dentry = file->f_path.dentry;
1480 +       orig_bstart = dbstart(dentry);
1481 +       orig_bend = dbend(dentry);
1482 +       err = unionfs_partial_lookup(dentry, parent);
1483 +       if (err)
1484 +               goto out;
1485 +       bstart = dbstart(dentry);
1486 +       bend = dbend(dentry);
1487 +
1488 +       FD_ZERO(&branchlist);
1489 +
1490 +       for (bindex = bstart; bindex <= bend; bindex++) {
1491 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1492 +               if (!lower_dentry)
1493 +                       continue;
1494 +               if (likely(lower_dentry->d_inode))
1495 +                       FD_SET(bindex, &branchlist);
1496 +               /* purge any lower objects after partial_lookup */
1497 +               if (bindex < orig_bstart || bindex > orig_bend) {
1498 +                       dput(lower_dentry);
1499 +                       unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1500 +                       iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1501 +                       unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1502 +                                                   NULL);
1503 +                       mnt = unionfs_lower_mnt_idx(dentry, bindex);
1504 +                       if (!mnt)
1505 +                               continue;
1506 +                       unionfs_mntput(dentry, bindex);
1507 +                       unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1508 +               }
1509 +       }
1510 +       /* restore original dentry's offsets */
1511 +       dbstart(dentry) = orig_bstart;
1512 +       dbend(dentry) = orig_bend;
1513 +       ibstart(dentry->d_inode) = orig_bstart;
1514 +       ibend(dentry->d_inode) = orig_bend;
1515 +
1516 +       err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1517 +       if (unlikely(err))
1518 +               err = -EFAULT;
1519 +
1520 +out:
1521 +       return err < 0 ? err : bend;
1522 +}
1523 +
1524 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1525 +{
1526 +       long err;
1527 +       struct dentry *dentry = file->f_path.dentry;
1528 +       struct dentry *parent;
1529 +
1530 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1531 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1532 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1533 +
1534 +       err = unionfs_file_revalidate(file, parent, true);
1535 +       if (unlikely(err))
1536 +               goto out;
1537 +
1538 +       /* check if asked for local commands */
1539 +       switch (cmd) {
1540 +       case UNIONFS_IOCTL_INCGEN:
1541 +               /* Increment the superblock generation count */
1542 +               pr_info("unionfs: incgen ioctl deprecated; "
1543 +                       "use \"-o remount,incgen\"\n");
1544 +               err = -ENOSYS;
1545 +               break;
1546 +
1547 +       case UNIONFS_IOCTL_QUERYFILE:
1548 +               /* Return list of branches containing the given file */
1549 +               err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1550 +               break;
1551 +
1552 +       default:
1553 +               /* pass the ioctl down */
1554 +               err = do_ioctl(file, cmd, arg);
1555 +               break;
1556 +       }
1557 +
1558 +out:
1559 +       unionfs_check_file(file);
1560 +       unionfs_unlock_dentry(dentry);
1561 +       unionfs_unlock_parent(dentry, parent);
1562 +       unionfs_read_unlock(dentry->d_sb);
1563 +       return err;
1564 +}
1565 +
1566 +int unionfs_flush(struct file *file, fl_owner_t id)
1567 +{
1568 +       int err = 0;
1569 +       struct file *lower_file = NULL;
1570 +       struct dentry *dentry = file->f_path.dentry;
1571 +       struct dentry *parent;
1572 +       int bindex, bstart, bend;
1573 +
1574 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1575 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1576 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1577 +
1578 +       err = unionfs_file_revalidate(file, parent,
1579 +                                     UNIONFS_F(file)->wrote_to_file);
1580 +       if (unlikely(err))
1581 +               goto out;
1582 +       unionfs_check_file(file);
1583 +
1584 +       bstart = fbstart(file);
1585 +       bend = fbend(file);
1586 +       for (bindex = bstart; bindex <= bend; bindex++) {
1587 +               lower_file = unionfs_lower_file_idx(file, bindex);
1588 +
1589 +               if (lower_file && lower_file->f_op &&
1590 +                   lower_file->f_op->flush) {
1591 +                       err = lower_file->f_op->flush(lower_file, id);
1592 +                       if (err)
1593 +                               goto out;
1594 +               }
1595 +
1596 +       }
1597 +
1598 +out:
1599 +       if (!err)
1600 +               unionfs_check_file(file);
1601 +       unionfs_unlock_dentry(dentry);
1602 +       unionfs_unlock_parent(dentry, parent);
1603 +       unionfs_read_unlock(dentry->d_sb);
1604 +       return err;
1605 +}
1606 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/copyup.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/copyup.c
1607 --- linux-2.6.35-rc6-git//fs/unionfs/copyup.c   1970-01-01 02:00:00.000000000 +0200
1608 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/copyup.c   2010-07-30 15:50:50.558010270 +0300
1609 @@ -0,0 +1,897 @@
1610 +/*
1611 + * Copyright (c) 2003-2010 Erez Zadok
1612 + * Copyright (c) 2003-2006 Charles P. Wright
1613 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1614 + * Copyright (c) 2005-2006 Junjiro Okajima
1615 + * Copyright (c) 2005      Arun M. Krishnakumar
1616 + * Copyright (c) 2004-2006 David P. Quigley
1617 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1618 + * Copyright (c) 2003      Puja Gupta
1619 + * Copyright (c) 2003      Harikesavan Krishnan
1620 + * Copyright (c) 2003-2010 Stony Brook University
1621 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
1622 + *
1623 + * This program is free software; you can redistribute it and/or modify
1624 + * it under the terms of the GNU General Public License version 2 as
1625 + * published by the Free Software Foundation.
1626 + */
1627 +
1628 +#include "union.h"
1629 +
1630 +/*
1631 + * For detailed explanation of copyup see:
1632 + * Documentation/filesystems/unionfs/concepts.txt
1633 + */
1634 +
1635 +#ifdef CONFIG_UNION_FS_XATTR
1636 +/* copyup all extended attrs for a given dentry */
1637 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1638 +                        struct dentry *new_lower_dentry)
1639 +{
1640 +       int err = 0;
1641 +       ssize_t list_size = -1;
1642 +       char *name_list = NULL;
1643 +       char *attr_value = NULL;
1644 +       char *name_list_buf = NULL;
1645 +
1646 +       /* query the actual size of the xattr list */
1647 +       list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1648 +       if (list_size <= 0) {
1649 +               err = list_size;
1650 +               goto out;
1651 +       }
1652 +
1653 +       /* allocate space for the actual list */
1654 +       name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1655 +       if (unlikely(!name_list || IS_ERR(name_list))) {
1656 +               err = PTR_ERR(name_list);
1657 +               goto out;
1658 +       }
1659 +
1660 +       name_list_buf = name_list; /* save for kfree at end */
1661 +
1662 +       /* now get the actual xattr list of the source file */
1663 +       list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1664 +       if (list_size <= 0) {
1665 +               err = list_size;
1666 +               goto out;
1667 +       }
1668 +
1669 +       /* allocate space to hold each xattr's value */
1670 +       attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1671 +       if (unlikely(!attr_value || IS_ERR(attr_value))) {
1672 +               err = PTR_ERR(name_list);
1673 +               goto out;
1674 +       }
1675 +
1676 +       /* in a loop, get and set each xattr from src to dst file */
1677 +       while (*name_list) {
1678 +               ssize_t size;
1679 +
1680 +               /* Lock here since vfs_getxattr doesn't lock for us */
1681 +               mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1682 +               size = vfs_getxattr(old_lower_dentry, name_list,
1683 +                                   attr_value, XATTR_SIZE_MAX);
1684 +               mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1685 +               if (size < 0) {
1686 +                       err = size;
1687 +                       goto out;
1688 +               }
1689 +               if (size > XATTR_SIZE_MAX) {
1690 +                       err = -E2BIG;
1691 +                       goto out;
1692 +               }
1693 +               /* Don't lock here since vfs_setxattr does it for us. */
1694 +               err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1695 +                                  size, 0);
1696 +               /*
1697 +                * Selinux depends on "security.*" xattrs, so to maintain
1698 +                * the security of copied-up files, if Selinux is active,
1699 +                * then we must copy these xattrs as well.  So we need to
1700 +                * temporarily get FOWNER privileges.
1701 +                * XXX: move entire copyup code to SIOQ.
1702 +                */
1703 +               if (err == -EPERM && !capable(CAP_FOWNER)) {
1704 +                       const struct cred *old_creds;
1705 +                       struct cred *new_creds;
1706 +
1707 +                       new_creds = prepare_creds();
1708 +                       if (unlikely(!new_creds)) {
1709 +                               err = -ENOMEM;
1710 +                               goto out;
1711 +                       }
1712 +                       cap_raise(new_creds->cap_effective, CAP_FOWNER);
1713 +                       old_creds = override_creds(new_creds);
1714 +                       err = vfs_setxattr(new_lower_dentry, name_list,
1715 +                                          attr_value, size, 0);
1716 +                       revert_creds(old_creds);
1717 +               }
1718 +               if (err < 0)
1719 +                       goto out;
1720 +               name_list += strlen(name_list) + 1;
1721 +       }
1722 +out:
1723 +       unionfs_xattr_kfree(name_list_buf);
1724 +       unionfs_xattr_kfree(attr_value);
1725 +       /* Ignore if xattr isn't supported */
1726 +       if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1727 +               err = 0;
1728 +       return err;
1729 +}
1730 +#endif /* CONFIG_UNION_FS_XATTR */
1731 +
1732 +/*
1733 + * Determine the mode based on the copyup flags, and the existing dentry.
1734 + *
1735 + * Handle file systems which may not support certain options.  For example
1736 + * jffs2 doesn't allow one to chmod a symlink.  So we ignore such harmless
1737 + * errors, rather than propagating them up, which results in copyup errors
1738 + * and errors returned back to users.
1739 + */
1740 +static int copyup_permissions(struct super_block *sb,
1741 +                             struct dentry *old_lower_dentry,
1742 +                             struct dentry *new_lower_dentry)
1743 +{
1744 +       struct inode *i = old_lower_dentry->d_inode;
1745 +       struct iattr newattrs;
1746 +       int err;
1747 +
1748 +       newattrs.ia_atime = i->i_atime;
1749 +       newattrs.ia_mtime = i->i_mtime;
1750 +       newattrs.ia_ctime = i->i_ctime;
1751 +       newattrs.ia_gid = i->i_gid;
1752 +       newattrs.ia_uid = i->i_uid;
1753 +       newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1754 +               ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1755 +               ATTR_GID | ATTR_UID;
1756 +       mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1757 +       err = notify_change(new_lower_dentry, &newattrs);
1758 +       if (err)
1759 +               goto out;
1760 +
1761 +       /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1762 +       newattrs.ia_mode = i->i_mode;
1763 +       newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1764 +       err = notify_change(new_lower_dentry, &newattrs);
1765 +       if (err == -EOPNOTSUPP &&
1766 +           S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1767 +               printk(KERN_WARNING
1768 +                      "unionfs: changing \"%s\" symlink mode unsupported\n",
1769 +                      new_lower_dentry->d_name.name);
1770 +               err = 0;
1771 +       }
1772 +
1773 +out:
1774 +       mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1775 +       return err;
1776 +}
1777 +
1778 +/*
1779 + * create the new device/file/directory - use copyup_permission to copyup
1780 + * times, and mode
1781 + *
1782 + * if the object being copied up is a regular file, the file is only created,
1783 + * the contents have to be copied up separately
1784 + */
1785 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1786 +                           struct dentry *new_lower_dentry,
1787 +                           struct dentry *new_lower_parent_dentry,
1788 +                           char *symbuf)
1789 +{
1790 +       int err = 0;
1791 +       umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1792 +       struct sioq_args args;
1793 +
1794 +       if (S_ISDIR(old_mode)) {
1795 +               args.mkdir.parent = new_lower_parent_dentry->d_inode;
1796 +               args.mkdir.dentry = new_lower_dentry;
1797 +               args.mkdir.mode = old_mode;
1798 +
1799 +               run_sioq(__unionfs_mkdir, &args);
1800 +               err = args.err;
1801 +       } else if (S_ISLNK(old_mode)) {
1802 +               args.symlink.parent = new_lower_parent_dentry->d_inode;
1803 +               args.symlink.dentry = new_lower_dentry;
1804 +               args.symlink.symbuf = symbuf;
1805 +
1806 +               run_sioq(__unionfs_symlink, &args);
1807 +               err = args.err;
1808 +       } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1809 +                  S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1810 +               args.mknod.parent = new_lower_parent_dentry->d_inode;
1811 +               args.mknod.dentry = new_lower_dentry;
1812 +               args.mknod.mode = old_mode;
1813 +               args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1814 +
1815 +               run_sioq(__unionfs_mknod, &args);
1816 +               err = args.err;
1817 +       } else if (S_ISREG(old_mode)) {
1818 +               struct nameidata nd;
1819 +               err = init_lower_nd(&nd, LOOKUP_CREATE);
1820 +               if (unlikely(err < 0))
1821 +                       goto out;
1822 +               args.create.nd = &nd;
1823 +               args.create.parent = new_lower_parent_dentry->d_inode;
1824 +               args.create.dentry = new_lower_dentry;
1825 +               args.create.mode = old_mode;
1826 +
1827 +               run_sioq(__unionfs_create, &args);
1828 +               err = args.err;
1829 +               release_lower_nd(&nd, err);
1830 +       } else {
1831 +               printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1832 +                      old_mode);
1833 +               BUG();
1834 +       }
1835 +
1836 +out:
1837 +       return err;
1838 +}
1839 +
1840 +static int __copyup_reg_data(struct dentry *dentry,
1841 +                            struct dentry *new_lower_dentry, int new_bindex,
1842 +                            struct dentry *old_lower_dentry, int old_bindex,
1843 +                            struct file **copyup_file, loff_t len)
1844 +{
1845 +       struct super_block *sb = dentry->d_sb;
1846 +       struct file *input_file;
1847 +       struct file *output_file;
1848 +       struct vfsmount *output_mnt;
1849 +       mm_segment_t old_fs;
1850 +       char *buf = NULL;
1851 +       ssize_t read_bytes, write_bytes;
1852 +       loff_t size;
1853 +       int err = 0;
1854 +
1855 +       /* open old file */
1856 +       unionfs_mntget(dentry, old_bindex);
1857 +       branchget(sb, old_bindex);
1858 +       /* dentry_open calls dput and mntput if it returns an error */
1859 +       input_file = dentry_open(old_lower_dentry,
1860 +                                unionfs_lower_mnt_idx(dentry, old_bindex),
1861 +                                O_RDONLY | O_LARGEFILE, current_cred());
1862 +       if (IS_ERR(input_file)) {
1863 +               dput(old_lower_dentry);
1864 +               err = PTR_ERR(input_file);
1865 +               goto out;
1866 +       }
1867 +       if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1868 +               err = -EINVAL;
1869 +               goto out_close_in;
1870 +       }
1871 +
1872 +       /* open new file */
1873 +       dget(new_lower_dentry);
1874 +       output_mnt = unionfs_mntget(sb->s_root, new_bindex);
1875 +       branchget(sb, new_bindex);
1876 +       output_file = dentry_open(new_lower_dentry, output_mnt,
1877 +                                 O_RDWR | O_LARGEFILE, current_cred());
1878 +       if (IS_ERR(output_file)) {
1879 +               err = PTR_ERR(output_file);
1880 +               goto out_close_in2;
1881 +       }
1882 +       if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1883 +               err = -EINVAL;
1884 +               goto out_close_out;
1885 +       }
1886 +
1887 +       /* allocating a buffer */
1888 +       buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1889 +       if (unlikely(!buf)) {
1890 +               err = -ENOMEM;
1891 +               goto out_close_out;
1892 +       }
1893 +
1894 +       input_file->f_pos = 0;
1895 +       output_file->f_pos = 0;
1896 +
1897 +       old_fs = get_fs();
1898 +       set_fs(KERNEL_DS);
1899 +
1900 +       size = len;
1901 +       err = 0;
1902 +       do {
1903 +               if (len >= PAGE_SIZE)
1904 +                       size = PAGE_SIZE;
1905 +               else if ((len < PAGE_SIZE) && (len > 0))
1906 +                       size = len;
1907 +
1908 +               len -= PAGE_SIZE;
1909 +
1910 +               read_bytes =
1911 +                       input_file->f_op->read(input_file,
1912 +                                              (char __user *)buf, size,
1913 +                                              &input_file->f_pos);
1914 +               if (read_bytes <= 0) {
1915 +                       err = read_bytes;
1916 +                       break;
1917 +               }
1918 +
1919 +               /* see Documentation/filesystems/unionfs/issues.txt */
1920 +               lockdep_off();
1921 +               write_bytes =
1922 +                       output_file->f_op->write(output_file,
1923 +                                                (char __user *)buf,
1924 +                                                read_bytes,
1925 +                                                &output_file->f_pos);
1926 +               lockdep_on();
1927 +               if ((write_bytes < 0) || (write_bytes < read_bytes)) {
1928 +                       err = write_bytes;
1929 +                       break;
1930 +               }
1931 +       } while ((read_bytes > 0) && (len > 0));
1932 +
1933 +       set_fs(old_fs);
1934 +
1935 +       kfree(buf);
1936 +
1937 +       if (!err)
1938 +               err = output_file->f_op->fsync(output_file,
1939 +                                              new_lower_dentry, 0);
1940 +
1941 +       if (err)
1942 +               goto out_close_out;
1943 +
1944 +       if (copyup_file) {
1945 +               *copyup_file = output_file;
1946 +               goto out_close_in;
1947 +       }
1948 +
1949 +out_close_out:
1950 +       fput(output_file);
1951 +
1952 +out_close_in2:
1953 +       branchput(sb, new_bindex);
1954 +
1955 +out_close_in:
1956 +       fput(input_file);
1957 +
1958 +out:
1959 +       branchput(sb, old_bindex);
1960 +
1961 +       return err;
1962 +}
1963 +
1964 +/*
1965 + * dput the lower references for old and new dentry & clear a lower dentry
1966 + * pointer
1967 + */
1968 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
1969 +                   int old_bstart, int old_bend,
1970 +                   struct dentry *new_lower_dentry, int new_bindex)
1971 +{
1972 +       /* get rid of the lower dentry and all its traces */
1973 +       unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
1974 +       dbstart(dentry) = old_bstart;
1975 +       dbend(dentry) = old_bend;
1976 +
1977 +       dput(new_lower_dentry);
1978 +       dput(old_lower_dentry);
1979 +}
1980 +
1981 +/*
1982 + * Copy up a dentry to a file of specified name.
1983 + *
1984 + * @dir: used to pull the ->i_sb to access other branches
1985 + * @dentry: the non-negative dentry whose lower_inode we should copy
1986 + * @bstart: the branch of the lower_inode to copy from
1987 + * @new_bindex: the branch to create the new file in
1988 + * @name: the name of the file to create
1989 + * @namelen: length of @name
1990 + * @copyup_file: the "struct file" to return (optional)
1991 + * @len: how many bytes to copy-up?
1992 + */
1993 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
1994 +                 int new_bindex, const char *name, int namelen,
1995 +                 struct file **copyup_file, loff_t len)
1996 +{
1997 +       struct dentry *new_lower_dentry;
1998 +       struct dentry *old_lower_dentry = NULL;
1999 +       struct super_block *sb;
2000 +       int err = 0;
2001 +       int old_bindex;
2002 +       int old_bstart;
2003 +       int old_bend;
2004 +       struct dentry *new_lower_parent_dentry = NULL;
2005 +       mm_segment_t oldfs;
2006 +       char *symbuf = NULL;
2007 +
2008 +       verify_locked(dentry);
2009 +
2010 +       old_bindex = bstart;
2011 +       old_bstart = dbstart(dentry);
2012 +       old_bend = dbend(dentry);
2013 +
2014 +       BUG_ON(new_bindex < 0);
2015 +       BUG_ON(new_bindex >= old_bindex);
2016 +
2017 +       sb = dir->i_sb;
2018 +
2019 +       err = is_robranch_super(sb, new_bindex);
2020 +       if (err)
2021 +               goto out;
2022 +
2023 +       /* Create the directory structure above this dentry. */
2024 +       new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2025 +       if (IS_ERR(new_lower_dentry)) {
2026 +               err = PTR_ERR(new_lower_dentry);
2027 +               goto out;
2028 +       }
2029 +
2030 +       old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2031 +       /* we conditionally dput this old_lower_dentry at end of function */
2032 +       dget(old_lower_dentry);
2033 +
2034 +       /* For symlinks, we must read the link before we lock the directory. */
2035 +       if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2036 +
2037 +               symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2038 +               if (unlikely(!symbuf)) {
2039 +                       __clear(dentry, old_lower_dentry,
2040 +                               old_bstart, old_bend,
2041 +                               new_lower_dentry, new_bindex);
2042 +                       err = -ENOMEM;
2043 +                       goto out_free;
2044 +               }
2045 +
2046 +               oldfs = get_fs();
2047 +               set_fs(KERNEL_DS);
2048 +               err = old_lower_dentry->d_inode->i_op->readlink(
2049 +                       old_lower_dentry,
2050 +                       (char __user *)symbuf,
2051 +                       PATH_MAX);
2052 +               set_fs(oldfs);
2053 +               if (err < 0) {
2054 +                       __clear(dentry, old_lower_dentry,
2055 +                               old_bstart, old_bend,
2056 +                               new_lower_dentry, new_bindex);
2057 +                       goto out_free;
2058 +               }
2059 +               symbuf[err] = '\0';
2060 +       }
2061 +
2062 +       /* Now we lock the parent, and create the object in the new branch. */
2063 +       new_lower_parent_dentry = lock_parent(new_lower_dentry);
2064 +
2065 +       /* create the new inode */
2066 +       err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2067 +                              new_lower_parent_dentry, symbuf);
2068 +
2069 +       if (err) {
2070 +               __clear(dentry, old_lower_dentry,
2071 +                       old_bstart, old_bend,
2072 +                       new_lower_dentry, new_bindex);
2073 +               goto out_unlock;
2074 +       }
2075 +
2076 +       /* We actually copyup the file here. */
2077 +       if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2078 +               err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2079 +                                       old_lower_dentry, old_bindex,
2080 +                                       copyup_file, len);
2081 +       if (err)
2082 +               goto out_unlink;
2083 +
2084 +       /* Set permissions. */
2085 +       err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2086 +       if (err)
2087 +               goto out_unlink;
2088 +
2089 +#ifdef CONFIG_UNION_FS_XATTR
2090 +       /* Selinux uses extended attributes for permissions. */
2091 +       err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2092 +       if (err)
2093 +               goto out_unlink;
2094 +#endif /* CONFIG_UNION_FS_XATTR */
2095 +
2096 +       /* do not allow files getting deleted to be re-interposed */
2097 +       if (!d_deleted(dentry))
2098 +               unionfs_reinterpose(dentry);
2099 +
2100 +       goto out_unlock;
2101 +
2102 +out_unlink:
2103 +       /*
2104 +        * copyup failed, because we possibly ran out of space or
2105 +        * quota, or something else happened so let's unlink; we don't
2106 +        * really care about the return value of vfs_unlink
2107 +        */
2108 +       vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2109 +
2110 +       if (copyup_file) {
2111 +               /* need to close the file */
2112 +
2113 +               fput(*copyup_file);
2114 +               branchput(sb, new_bindex);
2115 +       }
2116 +
2117 +       /*
2118 +        * TODO: should we reset the error to something like -EIO?
2119 +        *
2120 +        * If we don't reset, the user may get some nonsensical errors, but
2121 +        * on the other hand, if we reset to EIO, we guarantee that the user
2122 +        * will get a "confusing" error message.
2123 +        */
2124 +
2125 +out_unlock:
2126 +       unlock_dir(new_lower_parent_dentry);
2127 +
2128 +out_free:
2129 +       /*
2130 +        * If old_lower_dentry was not a file, then we need to dput it.  If
2131 +        * it was a file, then it was already dput indirectly by other
2132 +        * functions we call above which operate on regular files.
2133 +        */
2134 +       if (old_lower_dentry && old_lower_dentry->d_inode &&
2135 +           !S_ISREG(old_lower_dentry->d_inode->i_mode))
2136 +               dput(old_lower_dentry);
2137 +       kfree(symbuf);
2138 +
2139 +       if (err) {
2140 +               /*
2141 +                * if directory creation succeeded, but inode copyup failed,
2142 +                * then purge new dentries.
2143 +                */
2144 +               if (dbstart(dentry) < old_bstart &&
2145 +                   ibstart(dentry->d_inode) > dbstart(dentry))
2146 +                       __clear(dentry, NULL, old_bstart, old_bend,
2147 +                               unionfs_lower_dentry(dentry), dbstart(dentry));
2148 +               goto out;
2149 +       }
2150 +       if (!S_ISDIR(dentry->d_inode->i_mode)) {
2151 +               unionfs_postcopyup_release(dentry);
2152 +               if (!unionfs_lower_inode(dentry->d_inode)) {
2153 +                       /*
2154 +                        * If we got here, then we copied up to an
2155 +                        * unlinked-open file, whose name is .unionfsXXXXX.
2156 +                        */
2157 +                       struct inode *inode = new_lower_dentry->d_inode;
2158 +                       atomic_inc(&inode->i_count);
2159 +                       unionfs_set_lower_inode_idx(dentry->d_inode,
2160 +                                                   ibstart(dentry->d_inode),
2161 +                                                   inode);
2162 +               }
2163 +       }
2164 +       unionfs_postcopyup_setmnt(dentry);
2165 +       /* sync inode times from copied-up inode to our inode */
2166 +       unionfs_copy_attr_times(dentry->d_inode);
2167 +       unionfs_check_inode(dir);
2168 +       unionfs_check_dentry(dentry);
2169 +out:
2170 +       return err;
2171 +}
2172 +
2173 +/*
2174 + * This function creates a copy of a file represented by 'file' which
2175 + * currently resides in branch 'bstart' to branch 'new_bindex.'  The copy
2176 + * will be named "name".
2177 + */
2178 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2179 +                     int bstart, int new_bindex, loff_t len)
2180 +{
2181 +       int err = 0;
2182 +       struct file *output_file = NULL;
2183 +
2184 +       err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2185 +                           name, strlen(name), &output_file, len);
2186 +       if (!err) {
2187 +               fbstart(file) = new_bindex;
2188 +               unionfs_set_lower_file_idx(file, new_bindex, output_file);
2189 +       }
2190 +
2191 +       return err;
2192 +}
2193 +
2194 +/*
2195 + * This function creates a copy of a file represented by 'file' which
2196 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2197 + */
2198 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2199 +               int new_bindex, loff_t len)
2200 +{
2201 +       int err = 0;
2202 +       struct file *output_file = NULL;
2203 +       struct dentry *dentry = file->f_path.dentry;
2204 +
2205 +       err = copyup_dentry(dir, dentry, bstart, new_bindex,
2206 +                           dentry->d_name.name, dentry->d_name.len,
2207 +                           &output_file, len);
2208 +       if (!err) {
2209 +               fbstart(file) = new_bindex;
2210 +               unionfs_set_lower_file_idx(file, new_bindex, output_file);
2211 +       }
2212 +
2213 +       return err;
2214 +}
2215 +
2216 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2217 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2218 +                            int old_bstart, int old_bend)
2219 +{
2220 +       int loop_start;
2221 +       int loop_end;
2222 +       int new_bstart = -1;
2223 +       int new_bend = -1;
2224 +       int i;
2225 +
2226 +       loop_start = min(old_bstart, bindex);
2227 +       loop_end = max(old_bend, bindex);
2228 +
2229 +       /*
2230 +        * This loop sets the bstart and bend for the new dentry by
2231 +        * traversing from left to right.  It also dputs all negative
2232 +        * dentries except bindex
2233 +        */
2234 +       for (i = loop_start; i <= loop_end; i++) {
2235 +               if (!unionfs_lower_dentry_idx(dentry, i))
2236 +                       continue;
2237 +
2238 +               if (i == bindex) {
2239 +                       new_bend = i;
2240 +                       if (new_bstart < 0)
2241 +                               new_bstart = i;
2242 +                       continue;
2243 +               }
2244 +
2245 +               if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2246 +                       dput(unionfs_lower_dentry_idx(dentry, i));
2247 +                       unionfs_set_lower_dentry_idx(dentry, i, NULL);
2248 +
2249 +                       unionfs_mntput(dentry, i);
2250 +                       unionfs_set_lower_mnt_idx(dentry, i, NULL);
2251 +               } else {
2252 +                       if (new_bstart < 0)
2253 +                               new_bstart = i;
2254 +                       new_bend = i;
2255 +               }
2256 +       }
2257 +
2258 +       if (new_bstart < 0)
2259 +               new_bstart = bindex;
2260 +       if (new_bend < 0)
2261 +               new_bend = bindex;
2262 +       dbstart(dentry) = new_bstart;
2263 +       dbend(dentry) = new_bend;
2264 +
2265 +}
2266 +
2267 +/* set lower inode ptr and update bstart & bend if necessary */
2268 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2269 +                       int bindex)
2270 +{
2271 +       unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2272 +                                   igrab(lower->d_inode));
2273 +       if (likely(ibstart(upper->d_inode) > bindex))
2274 +               ibstart(upper->d_inode) = bindex;
2275 +       if (likely(ibend(upper->d_inode) < bindex))
2276 +               ibend(upper->d_inode) = bindex;
2277 +
2278 +}
2279 +
2280 +/* set lower dentry ptr and update bstart & bend if necessary */
2281 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2282 +                        int bindex)
2283 +{
2284 +       unionfs_set_lower_dentry_idx(upper, bindex, lower);
2285 +       if (likely(dbstart(upper) > bindex))
2286 +               dbstart(upper) = bindex;
2287 +       if (likely(dbend(upper) < bindex))
2288 +               dbend(upper) = bindex;
2289 +}
2290 +
2291 +/*
2292 + * This function replicates the directory structure up-to given dentry
2293 + * in the bindex branch.
2294 + */
2295 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2296 +                             const char *name, int bindex)
2297 +{
2298 +       int err;
2299 +       struct dentry *child_dentry;
2300 +       struct dentry *parent_dentry;
2301 +       struct dentry *lower_parent_dentry = NULL;
2302 +       struct dentry *lower_dentry = NULL;
2303 +       const char *childname;
2304 +       unsigned int childnamelen;
2305 +       int nr_dentry;
2306 +       int count = 0;
2307 +       int old_bstart;
2308 +       int old_bend;
2309 +       struct dentry **path = NULL;
2310 +       struct super_block *sb;
2311 +
2312 +       verify_locked(dentry);
2313 +
2314 +       err = is_robranch_super(dir->i_sb, bindex);
2315 +       if (err) {
2316 +               lower_dentry = ERR_PTR(err);
2317 +               goto out;
2318 +       }
2319 +
2320 +       old_bstart = dbstart(dentry);
2321 +       old_bend = dbend(dentry);
2322 +
2323 +       lower_dentry = ERR_PTR(-ENOMEM);
2324 +
2325 +       /* There is no sense allocating any less than the minimum. */
2326 +       nr_dentry = 1;
2327 +       path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2328 +       if (unlikely(!path))
2329 +               goto out;
2330 +
2331 +       /* assume the negative dentry of unionfs as the parent dentry */
2332 +       parent_dentry = dentry;
2333 +
2334 +       /*
2335 +        * This loop finds the first parent that exists in the given branch.
2336 +        * We start building the directory structure from there.  At the end
2337 +        * of the loop, the following should hold:
2338 +        *  - child_dentry is the first nonexistent child
2339 +        *  - parent_dentry is the first existent parent
2340 +        *  - path[0] is the = deepest child
2341 +        *  - path[count] is the first child to create
2342 +        */
2343 +       do {
2344 +               child_dentry = parent_dentry;
2345 +
2346 +               /* find the parent directory dentry in unionfs */
2347 +               parent_dentry = dget_parent(child_dentry);
2348 +
2349 +               /* find out the lower_parent_dentry in the given branch */
2350 +               lower_parent_dentry =
2351 +                       unionfs_lower_dentry_idx(parent_dentry, bindex);
2352 +
2353 +               /* grow path table */
2354 +               if (count == nr_dentry) {
2355 +                       void *p;
2356 +
2357 +                       nr_dentry *= 2;
2358 +                       p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2359 +                                    GFP_KERNEL);
2360 +                       if (unlikely(!p)) {
2361 +                               lower_dentry = ERR_PTR(-ENOMEM);
2362 +                               goto out;
2363 +                       }
2364 +                       path = p;
2365 +               }
2366 +
2367 +               /* store the child dentry */
2368 +               path[count++] = child_dentry;
2369 +       } while (!lower_parent_dentry);
2370 +       count--;
2371 +
2372 +       sb = dentry->d_sb;
2373 +
2374 +       /*
2375 +        * This code goes between the begin/end labels and basically
2376 +        * emulates a while(child_dentry != dentry), only cleaner and
2377 +        * shorter than what would be a much longer while loop.
2378 +        */
2379 +begin:
2380 +       /* get lower parent dir in the current branch */
2381 +       lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2382 +       dput(parent_dentry);
2383 +
2384 +       /* init the values to lookup */
2385 +       childname = child_dentry->d_name.name;
2386 +       childnamelen = child_dentry->d_name.len;
2387 +
2388 +       if (child_dentry != dentry) {
2389 +               /* lookup child in the underlying file system */
2390 +               lower_dentry = lookup_lck_len(childname, lower_parent_dentry,
2391 +                                             childnamelen);
2392 +               if (IS_ERR(lower_dentry))
2393 +                       goto out;
2394 +       } else {
2395 +               /*
2396 +                * Is the name a whiteout of the child name ?  lookup the
2397 +                * whiteout child in the underlying file system
2398 +                */
2399 +               lower_dentry = lookup_lck_len(name, lower_parent_dentry,
2400 +                                             strlen(name));
2401 +               if (IS_ERR(lower_dentry))
2402 +                       goto out;
2403 +
2404 +               /* Replace the current dentry (if any) with the new one */
2405 +               dput(unionfs_lower_dentry_idx(dentry, bindex));
2406 +               unionfs_set_lower_dentry_idx(dentry, bindex,
2407 +                                            lower_dentry);
2408 +
2409 +               __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2410 +               goto out;
2411 +       }
2412 +
2413 +       if (lower_dentry->d_inode) {
2414 +               /*
2415 +                * since this already exists we dput to avoid
2416 +                * multiple references on the same dentry
2417 +                */
2418 +               dput(lower_dentry);
2419 +       } else {
2420 +               struct sioq_args args;
2421 +
2422 +               /* it's a negative dentry, create a new dir */
2423 +               lower_parent_dentry = lock_parent(lower_dentry);
2424 +
2425 +               args.mkdir.parent = lower_parent_dentry->d_inode;
2426 +               args.mkdir.dentry = lower_dentry;
2427 +               args.mkdir.mode = child_dentry->d_inode->i_mode;
2428 +
2429 +               run_sioq(__unionfs_mkdir, &args);
2430 +               err = args.err;
2431 +
2432 +               if (!err)
2433 +                       err = copyup_permissions(dir->i_sb, child_dentry,
2434 +                                                lower_dentry);
2435 +               unlock_dir(lower_parent_dentry);
2436 +               if (err) {
2437 +                       dput(lower_dentry);
2438 +                       lower_dentry = ERR_PTR(err);
2439 +                       goto out;
2440 +               }
2441 +
2442 +       }
2443 +
2444 +       __set_inode(child_dentry, lower_dentry, bindex);
2445 +       __set_dentry(child_dentry, lower_dentry, bindex);
2446 +       /*
2447 +        * update times of this dentry, but also the parent, because if
2448 +        * we changed, the parent may have changed too.
2449 +        */
2450 +       fsstack_copy_attr_times(parent_dentry->d_inode,
2451 +                               lower_parent_dentry->d_inode);
2452 +       unionfs_copy_attr_times(child_dentry->d_inode);
2453 +
2454 +       parent_dentry = child_dentry;
2455 +       child_dentry = path[--count];
2456 +       goto begin;
2457 +out:
2458 +       /* cleanup any leftover locks from the do/while loop above */
2459 +       if (IS_ERR(lower_dentry))
2460 +               while (count)
2461 +                       dput(path[count--]);
2462 +       kfree(path);
2463 +       return lower_dentry;
2464 +}
2465 +
2466 +/*
2467 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2468 + * dentry+parents to the first parent node that has an mnt.
2469 + */
2470 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2471 +{
2472 +       struct dentry *parent, *hasone;
2473 +       int bindex = dbstart(dentry);
2474 +
2475 +       if (unionfs_lower_mnt_idx(dentry, bindex))
2476 +               return;
2477 +       hasone = dentry->d_parent;
2478 +       /* this loop should stop at root dentry */
2479 +       while (!unionfs_lower_mnt_idx(hasone, bindex))
2480 +               hasone = hasone->d_parent;
2481 +       parent = dentry;
2482 +       while (!unionfs_lower_mnt_idx(parent, bindex)) {
2483 +               unionfs_set_lower_mnt_idx(parent, bindex,
2484 +                                         unionfs_mntget(hasone, bindex));
2485 +               parent = parent->d_parent;
2486 +       }
2487 +}
2488 +
2489 +/*
2490 + * Post-copyup helper to release all non-directory source objects of a
2491 + * copied-up file.  Regular files should have only one lower object.
2492 + */
2493 +void unionfs_postcopyup_release(struct dentry *dentry)
2494 +{
2495 +       int bstart, bend;
2496 +
2497 +       BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2498 +       bstart = dbstart(dentry);
2499 +       bend = dbend(dentry);
2500 +
2501 +       path_put_lowers(dentry, bstart + 1, bend, false);
2502 +       iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2503 +
2504 +       dbend(dentry) = bstart;
2505 +       ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2506 +}
2507 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/debug.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/debug.c
2508 --- linux-2.6.35-rc6-git//fs/unionfs/debug.c    1970-01-01 02:00:00.000000000 +0200
2509 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/debug.c    2010-07-30 15:50:50.558010270 +0300
2510 @@ -0,0 +1,533 @@
2511 +/*
2512 + * Copyright (c) 2003-2010 Erez Zadok
2513 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2514 + * Copyright (c) 2003-2010 Stony Brook University
2515 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
2516 + *
2517 + * This program is free software; you can redistribute it and/or modify
2518 + * it under the terms of the GNU General Public License version 2 as
2519 + * published by the Free Software Foundation.
2520 + */
2521 +
2522 +#include "union.h"
2523 +
2524 +/*
2525 + * Helper debugging functions for maintainers (and for users to report back
2526 + * useful information back to maintainers)
2527 + */
2528 +
2529 +/* it's always useful to know what part of the code called us */
2530 +#define PRINT_CALLER(fname, fxn, line)                                 \
2531 +       do {                                                            \
2532 +               if (!printed_caller) {                                  \
2533 +                       pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2534 +                       printed_caller = 1;                             \
2535 +               }                                                       \
2536 +       } while (0)
2537 +
2538 +/*
2539 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2540 + * the fan-out of various Unionfs objects.  We check that no lower objects
2541 + * exist  outside the start/end branch range; that all objects within are
2542 + * non-NULL (with some allowed exceptions); that for every lower file
2543 + * there's a lower dentry+inode; that the start/end ranges match for all
2544 + * corresponding lower objects; that open files/symlinks have only one lower
2545 + * objects, but directories can have several; and more.
2546 + */
2547 +void __unionfs_check_inode(const struct inode *inode,
2548 +                          const char *fname, const char *fxn, int line)
2549 +{
2550 +       int bindex;
2551 +       int istart, iend;
2552 +       struct inode *lower_inode;
2553 +       struct super_block *sb;
2554 +       int printed_caller = 0;
2555 +       void *poison_ptr;
2556 +
2557 +       /* for inodes now */
2558 +       BUG_ON(!inode);
2559 +       sb = inode->i_sb;
2560 +       istart = ibstart(inode);
2561 +       iend = ibend(inode);
2562 +       /* don't check inode if no lower branches */
2563 +       if (istart < 0 && iend < 0)
2564 +               return;
2565 +       if (unlikely(istart > iend)) {
2566 +               PRINT_CALLER(fname, fxn, line);
2567 +               pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2568 +                        inode, istart, iend);
2569 +       }
2570 +       if (unlikely((istart == -1 && iend != -1) ||
2571 +                    (istart != -1 && iend == -1))) {
2572 +               PRINT_CALLER(fname, fxn, line);
2573 +               pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2574 +                        inode, istart, iend);
2575 +       }
2576 +       if (!S_ISDIR(inode->i_mode)) {
2577 +               if (unlikely(iend != istart)) {
2578 +                       PRINT_CALLER(fname, fxn, line);
2579 +                       pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2580 +                                inode, istart, iend);
2581 +               }
2582 +       }
2583 +
2584 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2585 +               if (unlikely(!UNIONFS_I(inode))) {
2586 +                       PRINT_CALLER(fname, fxn, line);
2587 +                       pr_debug(" Ci3: no inode_info %p\n", inode);
2588 +                       return;
2589 +               }
2590 +               if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2591 +                       PRINT_CALLER(fname, fxn, line);
2592 +                       pr_debug(" Ci4: no lower_inodes %p\n", inode);
2593 +                       return;
2594 +               }
2595 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2596 +               if (lower_inode) {
2597 +                       memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2598 +                       if (unlikely(bindex < istart || bindex > iend)) {
2599 +                               PRINT_CALLER(fname, fxn, line);
2600 +                               pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2601 +                                        "istart/end=%d:%d\n", inode,
2602 +                                        lower_inode, bindex, istart, iend);
2603 +                       } else if (unlikely(lower_inode == poison_ptr)) {
2604 +                               /* freed inode! */
2605 +                               PRINT_CALLER(fname, fxn, line);
2606 +                               pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2607 +                                        "istart/end=%d:%d\n", inode,
2608 +                                        lower_inode, bindex, istart, iend);
2609 +                       }
2610 +                       continue;
2611 +               }
2612 +               /* if we get here, then lower_inode == NULL */
2613 +               if (bindex < istart || bindex > iend)
2614 +                       continue;
2615 +               /*
2616 +                * directories can have NULL lower inodes in b/t start/end,
2617 +                * but NOT if at the start/end range.
2618 +                */
2619 +               if (unlikely(S_ISDIR(inode->i_mode) &&
2620 +                            bindex > istart && bindex < iend))
2621 +                       continue;
2622 +               PRINT_CALLER(fname, fxn, line);
2623 +               pr_debug(" Ci7: inode/linode=%p:%p "
2624 +                        "bindex=%d istart/end=%d:%d\n",
2625 +                        inode, lower_inode, bindex, istart, iend);
2626 +       }
2627 +}
2628 +
2629 +void __unionfs_check_dentry(const struct dentry *dentry,
2630 +                           const char *fname, const char *fxn, int line)
2631 +{
2632 +       int bindex;
2633 +       int dstart, dend, istart, iend;
2634 +       struct dentry *lower_dentry;
2635 +       struct inode *inode, *lower_inode;
2636 +       struct super_block *sb;
2637 +       struct vfsmount *lower_mnt;
2638 +       int printed_caller = 0;
2639 +       void *poison_ptr;
2640 +
2641 +       BUG_ON(!dentry);
2642 +       sb = dentry->d_sb;
2643 +       inode = dentry->d_inode;
2644 +       dstart = dbstart(dentry);
2645 +       dend = dbend(dentry);
2646 +       /* don't check dentry/mnt if no lower branches */
2647 +       if (dstart < 0 && dend < 0)
2648 +               goto check_inode;
2649 +       BUG_ON(dstart > dend);
2650 +
2651 +       if (unlikely((dstart == -1 && dend != -1) ||
2652 +                    (dstart != -1 && dend == -1))) {
2653 +               PRINT_CALLER(fname, fxn, line);
2654 +               pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2655 +                        dentry, dstart, dend);
2656 +       }
2657 +       /*
2658 +        * check for NULL dentries inside the start/end range, or
2659 +        * non-NULL dentries outside the start/end range.
2660 +        */
2661 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2662 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2663 +               if (lower_dentry) {
2664 +                       if (unlikely(bindex < dstart || bindex > dend)) {
2665 +                               PRINT_CALLER(fname, fxn, line);
2666 +                               pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2667 +                                        "bindex=%d dstart/end=%d:%d\n",
2668 +                                        dentry, lower_dentry,
2669 +                                        (lower_dentry ? lower_dentry->d_inode :
2670 +                                         (void *) -1L),
2671 +                                        bindex, dstart, dend);
2672 +                       }
2673 +               } else {        /* lower_dentry == NULL */
2674 +                       if (bindex < dstart || bindex > dend)
2675 +                               continue;
2676 +                       /*
2677 +                        * Directories can have NULL lower inodes in b/t
2678 +                        * start/end, but NOT if at the start/end range.
2679 +                        * Ignore this rule, however, if this is a NULL
2680 +                        * dentry or a deleted dentry.
2681 +                        */
2682 +                       if (unlikely(!d_deleted((struct dentry *) dentry) &&
2683 +                                    inode &&
2684 +                                    !(inode && S_ISDIR(inode->i_mode) &&
2685 +                                      bindex > dstart && bindex < dend))) {
2686 +                               PRINT_CALLER(fname, fxn, line);
2687 +                               pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2688 +                                        "bindex=%d dstart/end=%d:%d\n",
2689 +                                        dentry, lower_dentry,
2690 +                                        (lower_dentry ?
2691 +                                         lower_dentry->d_inode :
2692 +                                         (void *) -1L),
2693 +                                        bindex, dstart, dend);
2694 +                       }
2695 +               }
2696 +       }
2697 +
2698 +       /* check for vfsmounts same as for dentries */
2699 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2700 +               lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2701 +               if (lower_mnt) {
2702 +                       if (unlikely(bindex < dstart || bindex > dend)) {
2703 +                               PRINT_CALLER(fname, fxn, line);
2704 +                               pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2705 +                                        "dstart/end=%d:%d\n", dentry,
2706 +                                        lower_mnt, bindex, dstart, dend);
2707 +                       }
2708 +               } else {        /* lower_mnt == NULL */
2709 +                       if (bindex < dstart || bindex > dend)
2710 +                               continue;
2711 +                       /*
2712 +                        * Directories can have NULL lower inodes in b/t
2713 +                        * start/end, but NOT if at the start/end range.
2714 +                        * Ignore this rule, however, if this is a NULL
2715 +                        * dentry.
2716 +                        */
2717 +                       if (unlikely(inode &&
2718 +                                    !(inode && S_ISDIR(inode->i_mode) &&
2719 +                                      bindex > dstart && bindex < dend))) {
2720 +                               PRINT_CALLER(fname, fxn, line);
2721 +                               pr_debug(" CM1: dentry/lmnt=%p:%p "
2722 +                                        "bindex=%d dstart/end=%d:%d\n",
2723 +                                        dentry, lower_mnt, bindex,
2724 +                                        dstart, dend);
2725 +                       }
2726 +               }
2727 +       }
2728 +
2729 +check_inode:
2730 +       /* for inodes now */
2731 +       if (!inode)
2732 +               return;
2733 +       istart = ibstart(inode);
2734 +       iend = ibend(inode);
2735 +       /* don't check inode if no lower branches */
2736 +       if (istart < 0 && iend < 0)
2737 +               return;
2738 +       BUG_ON(istart > iend);
2739 +       if (unlikely((istart == -1 && iend != -1) ||
2740 +                    (istart != -1 && iend == -1))) {
2741 +               PRINT_CALLER(fname, fxn, line);
2742 +               pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2743 +                        dentry, inode, istart, iend);
2744 +       }
2745 +       if (unlikely(istart != dstart)) {
2746 +               PRINT_CALLER(fname, fxn, line);
2747 +               pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2748 +                        dentry, inode, istart, dstart);
2749 +       }
2750 +       if (unlikely(iend != dend)) {
2751 +               PRINT_CALLER(fname, fxn, line);
2752 +               pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2753 +                        dentry, inode, iend, dend);
2754 +       }
2755 +
2756 +       if (!S_ISDIR(inode->i_mode)) {
2757 +               if (unlikely(dend != dstart)) {
2758 +                       PRINT_CALLER(fname, fxn, line);
2759 +                       pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2760 +                                dentry, inode, dstart, dend);
2761 +               }
2762 +               if (unlikely(iend != istart)) {
2763 +                       PRINT_CALLER(fname, fxn, line);
2764 +                       pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2765 +                                dentry, inode, istart, iend);
2766 +               }
2767 +       }
2768 +
2769 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2770 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2771 +               if (lower_inode) {
2772 +                       memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2773 +                       if (unlikely(bindex < istart || bindex > iend)) {
2774 +                               PRINT_CALLER(fname, fxn, line);
2775 +                               pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2776 +                                        "istart/end=%d:%d\n", dentry,
2777 +                                        lower_inode, bindex, istart, iend);
2778 +                       } else if (unlikely(lower_inode == poison_ptr)) {
2779 +                               /* freed inode! */
2780 +                               PRINT_CALLER(fname, fxn, line);
2781 +                               pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2782 +                                        "istart/end=%d:%d\n", dentry,
2783 +                                        lower_inode, bindex, istart, iend);
2784 +                       }
2785 +                       continue;
2786 +               }
2787 +               /* if we get here, then lower_inode == NULL */
2788 +               if (bindex < istart || bindex > iend)
2789 +                       continue;
2790 +               /*
2791 +                * directories can have NULL lower inodes in b/t start/end,
2792 +                * but NOT if at the start/end range.
2793 +                */
2794 +               if (unlikely(S_ISDIR(inode->i_mode) &&
2795 +                            bindex > istart && bindex < iend))
2796 +                       continue;
2797 +               PRINT_CALLER(fname, fxn, line);
2798 +               pr_debug(" CI7: dentry/linode=%p:%p "
2799 +                        "bindex=%d istart/end=%d:%d\n",
2800 +                        dentry, lower_inode, bindex, istart, iend);
2801 +       }
2802 +
2803 +       /*
2804 +        * If it's a directory, then intermediate objects b/t start/end can
2805 +        * be NULL.  But, check that all three are NULL: lower dentry, mnt,
2806 +        * and inode.
2807 +        */
2808 +       if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2809 +               for (bindex = dstart+1; bindex < dend; bindex++) {
2810 +                       lower_inode = unionfs_lower_inode_idx(inode, bindex);
2811 +                       lower_dentry = unionfs_lower_dentry_idx(dentry,
2812 +                                                               bindex);
2813 +                       lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2814 +                       if (unlikely(!((lower_inode && lower_dentry &&
2815 +                                       lower_mnt) ||
2816 +                                      (!lower_inode &&
2817 +                                       !lower_dentry && !lower_mnt)))) {
2818 +                               PRINT_CALLER(fname, fxn, line);
2819 +                               pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2820 +                                        "bindex=%d dstart/end=%d:%d\n",
2821 +                                        lower_mnt, lower_dentry, lower_inode,
2822 +                                        bindex, dstart, dend);
2823 +                       }
2824 +               }
2825 +       /* check if lower inode is newer than upper one (it shouldn't) */
2826 +       if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
2827 +               PRINT_CALLER(fname, fxn, line);
2828 +               for (bindex = ibstart(inode); bindex <= ibend(inode);
2829 +                    bindex++) {
2830 +                       lower_inode = unionfs_lower_inode_idx(inode, bindex);
2831 +                       if (unlikely(!lower_inode))
2832 +                               continue;
2833 +                       pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2834 +                                "ctime/lctime=%lu.%lu/%lu.%lu\n",
2835 +                                bindex,
2836 +                                inode->i_mtime.tv_sec,
2837 +                                inode->i_mtime.tv_nsec,
2838 +                                lower_inode->i_mtime.tv_sec,
2839 +                                lower_inode->i_mtime.tv_nsec,
2840 +                                inode->i_ctime.tv_sec,
2841 +                                inode->i_ctime.tv_nsec,
2842 +                                lower_inode->i_ctime.tv_sec,
2843 +                                lower_inode->i_ctime.tv_nsec);
2844 +               }
2845 +       }
2846 +}
2847 +
2848 +void __unionfs_check_file(const struct file *file,
2849 +                         const char *fname, const char *fxn, int line)
2850 +{
2851 +       int bindex;
2852 +       int dstart, dend, fstart, fend;
2853 +       struct dentry *dentry;
2854 +       struct file *lower_file;
2855 +       struct inode *inode;
2856 +       struct super_block *sb;
2857 +       int printed_caller = 0;
2858 +
2859 +       BUG_ON(!file);
2860 +       dentry = file->f_path.dentry;
2861 +       sb = dentry->d_sb;
2862 +       dstart = dbstart(dentry);
2863 +       dend = dbend(dentry);
2864 +       BUG_ON(dstart > dend);
2865 +       fstart = fbstart(file);
2866 +       fend = fbend(file);
2867 +       BUG_ON(fstart > fend);
2868 +
2869 +       if (unlikely((fstart == -1 && fend != -1) ||
2870 +                    (fstart != -1 && fend == -1))) {
2871 +               PRINT_CALLER(fname, fxn, line);
2872 +               pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
2873 +                        file, dentry, fstart, fend);
2874 +       }
2875 +       if (unlikely(fstart != dstart)) {
2876 +               PRINT_CALLER(fname, fxn, line);
2877 +               pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
2878 +                        file, dentry, fstart, dstart);
2879 +       }
2880 +       if (unlikely(fend != dend)) {
2881 +               PRINT_CALLER(fname, fxn, line);
2882 +               pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
2883 +                        file, dentry, fend, dend);
2884 +       }
2885 +       inode = dentry->d_inode;
2886 +       if (!S_ISDIR(inode->i_mode)) {
2887 +               if (unlikely(fend != fstart)) {
2888 +                       PRINT_CALLER(fname, fxn, line);
2889 +                       pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
2890 +                                file, inode, fstart, fend);
2891 +               }
2892 +               if (unlikely(dend != dstart)) {
2893 +                       PRINT_CALLER(fname, fxn, line);
2894 +                       pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
2895 +                                file, dentry, dstart, dend);
2896 +               }
2897 +       }
2898 +
2899 +       /*
2900 +        * check for NULL dentries inside the start/end range, or
2901 +        * non-NULL dentries outside the start/end range.
2902 +        */
2903 +       for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2904 +               lower_file = unionfs_lower_file_idx(file, bindex);
2905 +               if (lower_file) {
2906 +                       if (unlikely(bindex < fstart || bindex > fend)) {
2907 +                               PRINT_CALLER(fname, fxn, line);
2908 +                               pr_debug(" CF5: file/lower=%p:%p bindex=%d "
2909 +                                        "fstart/end=%d:%d\n", file,
2910 +                                        lower_file, bindex, fstart, fend);
2911 +                       }
2912 +               } else {        /* lower_file == NULL */
2913 +                       if (bindex >= fstart && bindex <= fend) {
2914 +                               /*
2915 +                                * directories can have NULL lower inodes in
2916 +                                * b/t start/end, but NOT if at the
2917 +                                * start/end range.
2918 +                                */
2919 +                               if (unlikely(!(S_ISDIR(inode->i_mode) &&
2920 +                                              bindex > fstart &&
2921 +                                              bindex < fend))) {
2922 +                                       PRINT_CALLER(fname, fxn, line);
2923 +                                       pr_debug(" CF6: file/lower=%p:%p "
2924 +                                                "bindex=%d fstart/end=%d:%d\n",
2925 +                                                file, lower_file, bindex,
2926 +                                                fstart, fend);
2927 +                               }
2928 +                       }
2929 +               }
2930 +       }
2931 +
2932 +       __unionfs_check_dentry(dentry, fname, fxn, line);
2933 +}
2934 +
2935 +void __unionfs_check_nd(const struct nameidata *nd,
2936 +                       const char *fname, const char *fxn, int line)
2937 +{
2938 +       struct file *file;
2939 +       int printed_caller = 0;
2940 +
2941 +       if (unlikely(!nd))
2942 +               return;
2943 +       if (nd->flags & LOOKUP_OPEN) {
2944 +               file = nd->intent.open.file;
2945 +               if (unlikely(file->f_path.dentry &&
2946 +                            strcmp(file->f_path.dentry->d_sb->s_type->name,
2947 +                                   UNIONFS_NAME))) {
2948 +                       PRINT_CALLER(fname, fxn, line);
2949 +                       pr_debug(" CND1: lower_file of type %s\n",
2950 +                                file->f_path.dentry->d_sb->s_type->name);
2951 +                       BUG();
2952 +               }
2953 +       }
2954 +}
2955 +
2956 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
2957 +void __show_branch_counts(const struct super_block *sb,
2958 +                         const char *file, const char *fxn, int line)
2959 +{
2960 +       int i;
2961 +       struct vfsmount *mnt;
2962 +
2963 +       pr_debug("BC:");
2964 +       for (i = 0; i < sbmax(sb); i++) {
2965 +               if (likely(sb->s_root))
2966 +                       mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
2967 +               else
2968 +                       mnt = NULL;
2969 +               printk(KERN_CONT "%d:",
2970 +                      (mnt ? atomic_read(&mnt->mnt_count) : -99));
2971 +       }
2972 +       printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
2973 +}
2974 +
2975 +void __show_inode_times(const struct inode *inode,
2976 +                       const char *file, const char *fxn, int line)
2977 +{
2978 +       struct inode *lower_inode;
2979 +       int bindex;
2980 +
2981 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
2982 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
2983 +               if (unlikely(!lower_inode))
2984 +                       continue;
2985 +               pr_debug("IT(%lu:%d): %s:%s:%d "
2986 +                        "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
2987 +                        inode->i_ino, bindex,
2988 +                        file, fxn, line,
2989 +                        inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2990 +                        lower_inode->i_mtime.tv_sec,
2991 +                        lower_inode->i_mtime.tv_nsec,
2992 +                        inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2993 +                        lower_inode->i_ctime.tv_sec,
2994 +                        lower_inode->i_ctime.tv_nsec);
2995 +       }
2996 +}
2997 +
2998 +void __show_dinode_times(const struct dentry *dentry,
2999 +                       const char *file, const char *fxn, int line)
3000 +{
3001 +       struct inode *inode = dentry->d_inode;
3002 +       struct inode *lower_inode;
3003 +       int bindex;
3004 +
3005 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3006 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
3007 +               if (!lower_inode)
3008 +                       continue;
3009 +               pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3010 +                        "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3011 +                        dentry->d_name.name, inode->i_ino, bindex,
3012 +                        file, fxn, line,
3013 +                        inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3014 +                        lower_inode->i_mtime.tv_sec,
3015 +                        lower_inode->i_mtime.tv_nsec,
3016 +                        inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3017 +                        lower_inode->i_ctime.tv_sec,
3018 +                        lower_inode->i_ctime.tv_nsec);
3019 +       }
3020 +}
3021 +
3022 +void __show_inode_counts(const struct inode *inode,
3023 +                       const char *file, const char *fxn, int line)
3024 +{
3025 +       struct inode *lower_inode;
3026 +       int bindex;
3027 +
3028 +       if (unlikely(!inode)) {
3029 +               pr_debug("SiC: Null inode\n");
3030 +               return;
3031 +       }
3032 +       for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3033 +            bindex++) {
3034 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
3035 +               if (unlikely(!lower_inode))
3036 +                       continue;
3037 +               pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3038 +                        inode->i_ino, bindex,
3039 +                        atomic_read(&(inode)->i_count),
3040 +                        atomic_read(&(lower_inode)->i_count),
3041 +                        file, fxn, line);
3042 +       }
3043 +}
3044 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/dentry.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/dentry.c
3045 --- linux-2.6.35-rc6-git//fs/unionfs/dentry.c   1970-01-01 02:00:00.000000000 +0200
3046 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/dentry.c   2010-07-30 15:50:50.558010270 +0300
3047 @@ -0,0 +1,397 @@
3048 +/*
3049 + * Copyright (c) 2003-2010 Erez Zadok
3050 + * Copyright (c) 2003-2006 Charles P. Wright
3051 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3052 + * Copyright (c) 2005-2006 Junjiro Okajima
3053 + * Copyright (c) 2005      Arun M. Krishnakumar
3054 + * Copyright (c) 2004-2006 David P. Quigley
3055 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3056 + * Copyright (c) 2003      Puja Gupta
3057 + * Copyright (c) 2003      Harikesavan Krishnan
3058 + * Copyright (c) 2003-2010 Stony Brook University
3059 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
3060 + *
3061 + * This program is free software; you can redistribute it and/or modify
3062 + * it under the terms of the GNU General Public License version 2 as
3063 + * published by the Free Software Foundation.
3064 + */
3065 +
3066 +#include "union.h"
3067 +
3068 +bool is_negative_lower(const struct dentry *dentry)
3069 +{
3070 +       int bindex;
3071 +       struct dentry *lower_dentry;
3072 +
3073 +       BUG_ON(!dentry);
3074 +       /* cache coherency: check if file was deleted on lower branch */
3075 +       if (dbstart(dentry) < 0)
3076 +               return true;
3077 +       for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3078 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3079 +               /* unhashed (i.e., unlinked) lower dentries don't count */
3080 +               if (lower_dentry && lower_dentry->d_inode &&
3081 +                   !d_deleted(lower_dentry) &&
3082 +                   !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3083 +                       return false;
3084 +       }
3085 +       return true;
3086 +}
3087 +
3088 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3089 +{
3090 +       struct dentry *lower_dentry;
3091 +       int bindex;
3092 +
3093 +       if (start < 0)
3094 +               return;
3095 +       for (bindex = start; bindex <= end; bindex++) {
3096 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3097 +               if (!lower_dentry)
3098 +                       continue;
3099 +               unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3100 +               dput(lower_dentry);
3101 +       }
3102 +}
3103 +
3104 +/*
3105 + * Purge and invalidate as many data pages of a unionfs inode.  This is
3106 + * called when the lower inode has changed, and we want to force processes
3107 + * to re-get the new data.
3108 + */
3109 +static inline void purge_inode_data(struct inode *inode)
3110 +{
3111 +       /* remove all non-private mappings */
3112 +       unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3113 +       /* invalidate as many pages as possible */
3114 +       invalidate_mapping_pages(inode->i_mapping, 0, -1);
3115 +       /*
3116 +        * Don't try to truncate_inode_pages here, because this could lead
3117 +        * to a deadlock between some of address_space ops and dentry
3118 +        * revalidation: the address space op is invoked with a lock on our
3119 +        * own page, and truncate_inode_pages will block on locked pages.
3120 +        */
3121 +}
3122 +
3123 +/*
3124 + * Revalidate a single file/symlink/special dentry.  Assume that info nodes
3125 + * of the @dentry and its @parent are locked.  Assume parent is valid,
3126 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3127 + * dentry).  Returns true if valid, false otherwise.
3128 + */
3129 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3130 +                           bool willwrite)
3131 +{
3132 +       bool valid = true;      /* default is valid */
3133 +       struct dentry *lower_dentry;
3134 +       struct dentry *result;
3135 +       int bindex, bstart, bend;
3136 +       int sbgen, dgen, pdgen;
3137 +       int positive = 0;
3138 +       int interpose_flag;
3139 +
3140 +       verify_locked(dentry);
3141 +       verify_locked(parent);
3142 +
3143 +       /* if the dentry is unhashed, do NOT revalidate */
3144 +       if (d_deleted(dentry))
3145 +               goto out;
3146 +
3147 +       dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3148 +
3149 +       if (is_newer_lower(dentry)) {
3150 +               /* root dentry is always valid */
3151 +               if (IS_ROOT(dentry)) {
3152 +                       unionfs_copy_attr_times(dentry->d_inode);
3153 +               } else {
3154 +                       /*
3155 +                        * reset generation number to zero, guaranteed to be
3156 +                        * "old"
3157 +                        */
3158 +                       dgen = 0;
3159 +                       atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3160 +               }
3161 +               if (!willwrite)
3162 +                       purge_inode_data(dentry->d_inode);
3163 +       }
3164 +
3165 +       sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3166 +
3167 +       BUG_ON(dbstart(dentry) == -1);
3168 +       if (dentry->d_inode)
3169 +               positive = 1;
3170 +
3171 +       /* if our dentry is valid, then validate all lower ones */
3172 +       if (sbgen == dgen)
3173 +               goto validate_lowers;
3174 +
3175 +       /* The root entry should always be valid */
3176 +       BUG_ON(IS_ROOT(dentry));
3177 +
3178 +       /* We can't work correctly if our parent isn't valid. */
3179 +       pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3180 +
3181 +       /* Free the pointers for our inodes and this dentry. */
3182 +       path_put_lowers_all(dentry, false);
3183 +
3184 +       interpose_flag = INTERPOSE_REVAL_NEG;
3185 +       if (positive) {
3186 +               interpose_flag = INTERPOSE_REVAL;
3187 +               iput_lowers_all(dentry->d_inode, true);
3188 +       }
3189 +
3190 +       if (realloc_dentry_private_data(dentry) != 0) {
3191 +               valid = false;
3192 +               goto out;
3193 +       }
3194 +
3195 +       result = unionfs_lookup_full(dentry, parent, interpose_flag);
3196 +       if (result) {
3197 +               if (IS_ERR(result)) {
3198 +                       valid = false;
3199 +                       goto out;
3200 +               }
3201 +               /*
3202 +                * current unionfs_lookup_backend() doesn't return
3203 +                * a valid dentry
3204 +                */
3205 +               dput(dentry);
3206 +               dentry = result;
3207 +       }
3208 +
3209 +       if (unlikely(positive && is_negative_lower(dentry))) {
3210 +               /* call make_bad_inode here ? */
3211 +               d_drop(dentry);
3212 +               valid = false;
3213 +               goto out;
3214 +       }
3215 +
3216 +       /*
3217 +        * if we got here then we have revalidated our dentry and all lower
3218 +        * ones, so we can return safely.
3219 +        */
3220 +       if (!valid)             /* lower dentry revalidation failed */
3221 +               goto out;
3222 +
3223 +       /*
3224 +        * If the parent's gen no.  matches the superblock's gen no., then
3225 +        * we can update our denty's gen no.  If they didn't match, then it
3226 +        * was OK to revalidate this dentry with a stale parent, but we'll
3227 +        * purposely not update our dentry's gen no. (so it can be redone);
3228 +        * and, we'll mark our parent dentry as invalid so it'll force it
3229 +        * (and our dentry) to be revalidated.
3230 +        */
3231 +       if (pdgen == sbgen)
3232 +               atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3233 +       goto out;
3234 +
3235 +validate_lowers:
3236 +
3237 +       /* The revalidation must occur across all branches */
3238 +       bstart = dbstart(dentry);
3239 +       bend = dbend(dentry);
3240 +       BUG_ON(bstart == -1);
3241 +       for (bindex = bstart; bindex <= bend; bindex++) {
3242 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3243 +               if (!lower_dentry || !lower_dentry->d_op
3244 +                   || !lower_dentry->d_op->d_revalidate)
3245 +                       continue;
3246 +               /*
3247 +                * Don't pass nameidata to lower file system, because we
3248 +                * don't want an arbitrary lower file being opened or
3249 +                * returned to us: it may be useless to us because of the
3250 +                * fanout nature of unionfs (cf. file/directory open-file
3251 +                * invariants).  We will open lower files as and when needed
3252 +                * later on.
3253 +                */
3254 +               if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3255 +                       valid = false;
3256 +       }
3257 +
3258 +       if (!dentry->d_inode ||
3259 +           ibstart(dentry->d_inode) < 0 ||
3260 +           ibend(dentry->d_inode) < 0) {
3261 +               valid = false;
3262 +               goto out;
3263 +       }
3264 +
3265 +       if (valid) {
3266 +               /*
3267 +                * If we get here, and we copy the meta-data from the lower
3268 +                * inode to our inode, then it is vital that we have already
3269 +                * purged all unionfs-level file data.  We do that in the
3270 +                * caller (__unionfs_d_revalidate) by calling
3271 +                * purge_inode_data.
3272 +                */
3273 +               unionfs_copy_attr_all(dentry->d_inode,
3274 +                                     unionfs_lower_inode(dentry->d_inode));
3275 +               fsstack_copy_inode_size(dentry->d_inode,
3276 +                                       unionfs_lower_inode(dentry->d_inode));
3277 +       }
3278 +
3279 +out:
3280 +       return valid;
3281 +}
3282 +
3283 +/*
3284 + * Determine if the lower inode objects have changed from below the unionfs
3285 + * inode.  Return true if changed, false otherwise.
3286 + *
3287 + * We check if the mtime or ctime have changed.  However, the inode times
3288 + * can be changed by anyone without much protection, including
3289 + * asynchronously.  This can sometimes cause unionfs to find that the lower
3290 + * file system doesn't change its inode times quick enough, resulting in a
3291 + * false positive indication (which is harmless, it just makes unionfs do
3292 + * extra work in re-validating the objects).  To minimize the chances of
3293 + * these situations, we still consider such small time changes valid, but we
3294 + * don't print debugging messages unless the time changes are greater than
3295 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3296 + * because significant changes are more likely due to users manually
3297 + * touching lower files.
3298 + */
3299 +bool is_newer_lower(const struct dentry *dentry)
3300 +{
3301 +       int bindex;
3302 +       struct inode *inode;
3303 +       struct inode *lower_inode;
3304 +
3305 +       /* ignore if we're called on semi-initialized dentries/inodes */
3306 +       if (!dentry || !UNIONFS_D(dentry))
3307 +               return false;
3308 +       inode = dentry->d_inode;
3309 +       if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3310 +           ibstart(inode) < 0 || ibend(inode) < 0)
3311 +               return false;
3312 +
3313 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3314 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
3315 +               if (!lower_inode)
3316 +                       continue;
3317 +
3318 +               /* check if mtime/ctime have changed */
3319 +               if (unlikely(timespec_compare(&inode->i_mtime,
3320 +                                             &lower_inode->i_mtime) < 0)) {
3321 +                       if ((lower_inode->i_mtime.tv_sec -
3322 +                            inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3323 +                               pr_info("unionfs: new lower inode mtime "
3324 +                                       "(bindex=%d, name=%s)\n", bindex,
3325 +                                       dentry->d_name.name);
3326 +                               show_dinode_times(dentry);
3327 +                       }
3328 +                       return true;
3329 +               }
3330 +               if (unlikely(timespec_compare(&inode->i_ctime,
3331 +                                             &lower_inode->i_ctime) < 0)) {
3332 +                       if ((lower_inode->i_ctime.tv_sec -
3333 +                            inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3334 +                               pr_info("unionfs: new lower inode ctime "
3335 +                                       "(bindex=%d, name=%s)\n", bindex,
3336 +                                       dentry->d_name.name);
3337 +                               show_dinode_times(dentry);
3338 +                       }
3339 +                       return true;
3340 +               }
3341 +       }
3342 +
3343 +       /*
3344 +        * Last check: if this is a positive dentry, but somehow all lower
3345 +        * dentries are negative or unhashed, then this dentry needs to be
3346 +        * revalidated, because someone probably deleted the objects from
3347 +        * the lower branches directly.
3348 +        */
3349 +       if (is_negative_lower(dentry))
3350 +               return true;
3351 +
3352 +       return false;           /* default: lower is not newer */
3353 +}
3354 +
3355 +static int unionfs_d_revalidate(struct dentry *dentry,
3356 +                               struct nameidata *nd_unused)
3357 +{
3358 +       bool valid = true;
3359 +       int err = 1;            /* 1 means valid for the VFS */
3360 +       struct dentry *parent;
3361 +
3362 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3363 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3364 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3365 +
3366 +       valid = __unionfs_d_revalidate(dentry, parent, false);
3367 +       if (valid) {
3368 +               unionfs_postcopyup_setmnt(dentry);
3369 +               unionfs_check_dentry(dentry);
3370 +       } else {
3371 +               d_drop(dentry);
3372 +               err = valid;
3373 +       }
3374 +       unionfs_unlock_dentry(dentry);
3375 +       unionfs_unlock_parent(dentry, parent);
3376 +       unionfs_read_unlock(dentry->d_sb);
3377 +
3378 +       return err;
3379 +}
3380 +
3381 +static void unionfs_d_release(struct dentry *dentry)
3382 +{
3383 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3384 +       if (unlikely(!UNIONFS_D(dentry)))
3385 +               goto out;       /* skip if no lower branches */
3386 +       /* must lock our branch configuration here */
3387 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3388 +
3389 +       unionfs_check_dentry(dentry);
3390 +       /* this could be a negative dentry, so check first */
3391 +       if (dbstart(dentry) < 0) {
3392 +               unionfs_unlock_dentry(dentry);
3393 +               goto out;       /* due to a (normal) failed lookup */
3394 +       }
3395 +
3396 +       /* Release all the lower dentries */
3397 +       path_put_lowers_all(dentry, true);
3398 +
3399 +       unionfs_unlock_dentry(dentry);
3400 +
3401 +out:
3402 +       free_dentry_private_data(dentry);
3403 +       unionfs_read_unlock(dentry->d_sb);
3404 +       return;
3405 +}
3406 +
3407 +/*
3408 + * Called when we're removing the last reference to our dentry.  So we
3409 + * should drop all lower references too.
3410 + */
3411 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3412 +{
3413 +       int rc;
3414 +
3415 +       BUG_ON(!dentry);
3416 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3417 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3418 +
3419 +       if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3420 +               goto drop_lower_inodes;
3421 +       path_put_lowers_all(dentry, false);
3422 +
3423 +drop_lower_inodes:
3424 +       rc = atomic_read(&inode->i_count);
3425 +       if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3426 +               /* see Documentation/filesystems/unionfs/issues.txt */
3427 +               lockdep_off();
3428 +               iput(unionfs_lower_inode(inode));
3429 +               lockdep_on();
3430 +               unionfs_set_lower_inode(inode, NULL);
3431 +               /* XXX: may need to set start/end to -1? */
3432 +       }
3433 +
3434 +       iput(inode);
3435 +
3436 +       unionfs_unlock_dentry(dentry);
3437 +       unionfs_read_unlock(dentry->d_sb);
3438 +}
3439 +
3440 +struct dentry_operations unionfs_dops = {
3441 +       .d_revalidate   = unionfs_d_revalidate,
3442 +       .d_release      = unionfs_d_release,
3443 +       .d_iput         = unionfs_d_iput,
3444 +};
3445 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/dirfops.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/dirfops.c
3446 --- linux-2.6.35-rc6-git//fs/unionfs/dirfops.c  1970-01-01 02:00:00.000000000 +0200
3447 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/dirfops.c  2010-07-30 15:50:50.559010056 +0300
3448 @@ -0,0 +1,302 @@
3449 +/*
3450 + * Copyright (c) 2003-2010 Erez Zadok
3451 + * Copyright (c) 2003-2006 Charles P. Wright
3452 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3453 + * Copyright (c) 2005-2006 Junjiro Okajima
3454 + * Copyright (c) 2005      Arun M. Krishnakumar
3455 + * Copyright (c) 2004-2006 David P. Quigley
3456 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3457 + * Copyright (c) 2003      Puja Gupta
3458 + * Copyright (c) 2003      Harikesavan Krishnan
3459 + * Copyright (c) 2003-2010 Stony Brook University
3460 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
3461 + *
3462 + * This program is free software; you can redistribute it and/or modify
3463 + * it under the terms of the GNU General Public License version 2 as
3464 + * published by the Free Software Foundation.
3465 + */
3466 +
3467 +#include "union.h"
3468 +
3469 +/* Make sure our rdstate is playing by the rules. */
3470 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3471 +{
3472 +       BUG_ON(rdstate->offset >= DIREOF);
3473 +       BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3474 +}
3475 +
3476 +struct unionfs_getdents_callback {
3477 +       struct unionfs_dir_state *rdstate;
3478 +       void *dirent;
3479 +       int entries_written;
3480 +       int filldir_called;
3481 +       int filldir_error;
3482 +       filldir_t filldir;
3483 +       struct super_block *sb;
3484 +};
3485 +
3486 +/* based on generic filldir in fs/readir.c */
3487 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3488 +                          loff_t offset, u64 ino, unsigned int d_type)
3489 +{
3490 +       struct unionfs_getdents_callback *buf = dirent;
3491 +       struct filldir_node *found = NULL;
3492 +       int err = 0;
3493 +       int is_whiteout;
3494 +       char *name = (char *) oname;
3495 +
3496 +       buf->filldir_called++;
3497 +
3498 +       is_whiteout = is_whiteout_name(&name, &namelen);
3499 +
3500 +       found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3501 +
3502 +       if (found) {
3503 +               /*
3504 +                * If we had non-whiteout entry in dir cache, then mark it
3505 +                * as a whiteout and but leave it in the dir cache.
3506 +                */
3507 +               if (is_whiteout && !found->whiteout)
3508 +                       found->whiteout = is_whiteout;
3509 +               goto out;
3510 +       }
3511 +
3512 +       /* if 'name' isn't a whiteout, filldir it. */
3513 +       if (!is_whiteout) {
3514 +               off_t pos = rdstate2offset(buf->rdstate);
3515 +               u64 unionfs_ino = ino;
3516 +
3517 +               err = buf->filldir(buf->dirent, name, namelen, pos,
3518 +                                  unionfs_ino, d_type);
3519 +               buf->rdstate->offset++;
3520 +               verify_rdstate_offset(buf->rdstate);
3521 +       }
3522 +       /*
3523 +        * If we did fill it, stuff it in our hash, otherwise return an
3524 +        * error.
3525 +        */
3526 +       if (err) {
3527 +               buf->filldir_error = err;
3528 +               goto out;
3529 +       }
3530 +       buf->entries_written++;
3531 +       err = add_filldir_node(buf->rdstate, name, namelen,
3532 +                              buf->rdstate->bindex, is_whiteout);
3533 +       if (err)
3534 +               buf->filldir_error = err;
3535 +
3536 +out:
3537 +       return err;
3538 +}
3539 +
3540 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3541 +{
3542 +       int err = 0;
3543 +       struct file *lower_file = NULL;
3544 +       struct dentry *dentry = file->f_path.dentry;
3545 +       struct dentry *parent;
3546 +       struct inode *inode = NULL;
3547 +       struct unionfs_getdents_callback buf;
3548 +       struct unionfs_dir_state *uds;
3549 +       int bend;
3550 +       loff_t offset;
3551 +
3552 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3553 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3554 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3555 +
3556 +       err = unionfs_file_revalidate(file, parent, false);
3557 +       if (unlikely(err))
3558 +               goto out;
3559 +
3560 +       inode = dentry->d_inode;
3561 +
3562 +       uds = UNIONFS_F(file)->rdstate;
3563 +       if (!uds) {
3564 +               if (file->f_pos == DIREOF) {
3565 +                       goto out;
3566 +               } else if (file->f_pos > 0) {
3567 +                       uds = find_rdstate(inode, file->f_pos);
3568 +                       if (unlikely(!uds)) {
3569 +                               err = -ESTALE;
3570 +                               goto out;
3571 +                       }
3572 +                       UNIONFS_F(file)->rdstate = uds;
3573 +               } else {
3574 +                       init_rdstate(file);
3575 +                       uds = UNIONFS_F(file)->rdstate;
3576 +               }
3577 +       }
3578 +       bend = fbend(file);
3579 +
3580 +       while (uds->bindex <= bend) {
3581 +               lower_file = unionfs_lower_file_idx(file, uds->bindex);
3582 +               if (!lower_file) {
3583 +                       uds->bindex++;
3584 +                       uds->dirpos = 0;
3585 +                       continue;
3586 +               }
3587 +
3588 +               /* prepare callback buffer */
3589 +               buf.filldir_called = 0;
3590 +               buf.filldir_error = 0;
3591 +               buf.entries_written = 0;
3592 +               buf.dirent = dirent;
3593 +               buf.filldir = filldir;
3594 +               buf.rdstate = uds;
3595 +               buf.sb = inode->i_sb;
3596 +
3597 +               /* Read starting from where we last left off. */
3598 +               offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3599 +               if (offset < 0) {
3600 +                       err = offset;
3601 +                       goto out;
3602 +               }
3603 +               err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3604 +
3605 +               /* Save the position for when we continue. */
3606 +               offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3607 +               if (offset < 0) {
3608 +                       err = offset;
3609 +                       goto out;
3610 +               }
3611 +               uds->dirpos = offset;
3612 +
3613 +               /* Copy the atime. */
3614 +               fsstack_copy_attr_atime(inode,
3615 +                                       lower_file->f_path.dentry->d_inode);
3616 +
3617 +               if (err < 0)
3618 +                       goto out;
3619 +
3620 +               if (buf.filldir_error)
3621 +                       break;
3622 +
3623 +               if (!buf.entries_written) {
3624 +                       uds->bindex++;
3625 +                       uds->dirpos = 0;
3626 +               }
3627 +       }
3628 +
3629 +       if (!buf.filldir_error && uds->bindex >= bend) {
3630 +               /* Save the number of hash entries for next time. */
3631 +               UNIONFS_I(inode)->hashsize = uds->hashentries;
3632 +               free_rdstate(uds);
3633 +               UNIONFS_F(file)->rdstate = NULL;
3634 +               file->f_pos = DIREOF;
3635 +       } else {
3636 +               file->f_pos = rdstate2offset(uds);
3637 +       }
3638 +
3639 +out:
3640 +       if (!err)
3641 +               unionfs_check_file(file);
3642 +       unionfs_unlock_dentry(dentry);
3643 +       unionfs_unlock_parent(dentry, parent);
3644 +       unionfs_read_unlock(dentry->d_sb);
3645 +       return err;
3646 +}
3647 +
3648 +/*
3649 + * This is not meant to be a generic repositioning function.  If you do
3650 + * things that aren't supported, then we return EINVAL.
3651 + *
3652 + * What is allowed:
3653 + *  (1) seeking to the same position that you are currently at
3654 + *     This really has no effect, but returns where you are.
3655 + *  (2) seeking to the beginning of the file
3656 + *     This throws out all state, and lets you begin again.
3657 + */
3658 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3659 +{
3660 +       struct unionfs_dir_state *rdstate;
3661 +       struct dentry *dentry = file->f_path.dentry;
3662 +       struct dentry *parent;
3663 +       loff_t err;
3664 +
3665 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3666 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3667 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3668 +
3669 +       err = unionfs_file_revalidate(file, parent, false);
3670 +       if (unlikely(err))
3671 +               goto out;
3672 +
3673 +       rdstate = UNIONFS_F(file)->rdstate;
3674 +
3675 +       /*
3676 +        * we let users seek to their current position, but not anywhere
3677 +        * else.
3678 +        */
3679 +       if (!offset) {
3680 +               switch (origin) {
3681 +               case SEEK_SET:
3682 +                       if (rdstate) {
3683 +                               free_rdstate(rdstate);
3684 +                               UNIONFS_F(file)->rdstate = NULL;
3685 +                       }
3686 +                       init_rdstate(file);
3687 +                       err = 0;
3688 +                       break;
3689 +               case SEEK_CUR:
3690 +                       err = file->f_pos;
3691 +                       break;
3692 +               case SEEK_END:
3693 +                       /* Unsupported, because we would break everything.  */
3694 +                       err = -EINVAL;
3695 +                       break;
3696 +               }
3697 +       } else {
3698 +               switch (origin) {
3699 +               case SEEK_SET:
3700 +                       if (rdstate) {
3701 +                               if (offset == rdstate2offset(rdstate))
3702 +                                       err = offset;
3703 +                               else if (file->f_pos == DIREOF)
3704 +                                       err = DIREOF;
3705 +                               else
3706 +                                       err = -EINVAL;
3707 +                       } else {
3708 +                               struct inode *inode;
3709 +                               inode = dentry->d_inode;
3710 +                               rdstate = find_rdstate(inode, offset);
3711 +                               if (rdstate) {
3712 +                                       UNIONFS_F(file)->rdstate = rdstate;
3713 +                                       err = rdstate->offset;
3714 +                               } else {
3715 +                                       err = -EINVAL;
3716 +                               }
3717 +                       }
3718 +                       break;
3719 +               case SEEK_CUR:
3720 +               case SEEK_END:
3721 +                       /* Unsupported, because we would break everything.  */
3722 +                       err = -EINVAL;
3723 +                       break;
3724 +               }
3725 +       }
3726 +
3727 +out:
3728 +       if (!err)
3729 +               unionfs_check_file(file);
3730 +       unionfs_unlock_dentry(dentry);
3731 +       unionfs_unlock_parent(dentry, parent);
3732 +       unionfs_read_unlock(dentry->d_sb);
3733 +       return err;
3734 +}
3735 +
3736 +/*
3737 + * Trimmed directory options, we shouldn't pass everything down since
3738 + * we don't want to operate on partial directories.
3739 + */
3740 +struct file_operations unionfs_dir_fops = {
3741 +       .llseek         = unionfs_dir_llseek,
3742 +       .read           = generic_read_dir,
3743 +       .readdir        = unionfs_readdir,
3744 +       .unlocked_ioctl = unionfs_ioctl,
3745 +       .open           = unionfs_open,
3746 +       .release        = unionfs_file_release,
3747 +       .flush          = unionfs_flush,
3748 +       .fsync          = unionfs_fsync,
3749 +       .fasync         = unionfs_fasync,
3750 +};
3751 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/dirhelper.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/dirhelper.c
3752 --- linux-2.6.35-rc6-git//fs/unionfs/dirhelper.c        1970-01-01 02:00:00.000000000 +0200
3753 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/dirhelper.c        2010-07-30 15:50:50.559010056 +0300
3754 @@ -0,0 +1,158 @@
3755 +/*
3756 + * Copyright (c) 2003-2010 Erez Zadok
3757 + * Copyright (c) 2003-2006 Charles P. Wright
3758 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3759 + * Copyright (c) 2005-2006 Junjiro Okajima
3760 + * Copyright (c) 2005      Arun M. Krishnakumar
3761 + * Copyright (c) 2004-2006 David P. Quigley
3762 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3763 + * Copyright (c) 2003      Puja Gupta
3764 + * Copyright (c) 2003      Harikesavan Krishnan
3765 + * Copyright (c) 2003-2010 Stony Brook University
3766 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
3767 + *
3768 + * This program is free software; you can redistribute it and/or modify
3769 + * it under the terms of the GNU General Public License version 2 as
3770 + * published by the Free Software Foundation.
3771 + */
3772 +
3773 +#include "union.h"
3774 +
3775 +#define RD_NONE 0
3776 +#define RD_CHECK_EMPTY 1
3777 +/* The callback structure for check_empty. */
3778 +struct unionfs_rdutil_callback {
3779 +       int err;
3780 +       int filldir_called;
3781 +       struct unionfs_dir_state *rdstate;
3782 +       int mode;
3783 +};
3784 +
3785 +/* This filldir function makes sure only whiteouts exist within a directory. */
3786 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3787 +                                loff_t offset, u64 ino, unsigned int d_type)
3788 +{
3789 +       int err = 0;
3790 +       struct unionfs_rdutil_callback *buf = dirent;
3791 +       int is_whiteout;
3792 +       struct filldir_node *found;
3793 +       char *name = (char *) oname;
3794 +
3795 +       buf->filldir_called = 1;
3796 +
3797 +       if (name[0] == '.' && (namelen == 1 ||
3798 +                              (name[1] == '.' && namelen == 2)))
3799 +               goto out;
3800 +
3801 +       is_whiteout = is_whiteout_name(&name, &namelen);
3802 +
3803 +       found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3804 +       /* If it was found in the table there was a previous whiteout. */
3805 +       if (found)
3806 +               goto out;
3807 +
3808 +       /*
3809 +        * if it wasn't found and isn't a whiteout, the directory isn't
3810 +        * empty.
3811 +        */
3812 +       err = -ENOTEMPTY;
3813 +       if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3814 +               goto out;
3815 +
3816 +       err = add_filldir_node(buf->rdstate, name, namelen,
3817 +                              buf->rdstate->bindex, is_whiteout);
3818 +
3819 +out:
3820 +       buf->err = err;
3821 +       return err;
3822 +}
3823 +
3824 +/* Is a directory logically empty? */
3825 +int check_empty(struct dentry *dentry, struct dentry *parent,
3826 +               struct unionfs_dir_state **namelist)
3827 +{
3828 +       int err = 0;
3829 +       struct dentry *lower_dentry = NULL;
3830 +       struct vfsmount *mnt;
3831 +       struct super_block *sb;
3832 +       struct file *lower_file;
3833 +       struct unionfs_rdutil_callback *buf = NULL;
3834 +       int bindex, bstart, bend, bopaque;
3835 +
3836 +       sb = dentry->d_sb;
3837 +
3838 +
3839 +       BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3840 +
3841 +       err = unionfs_partial_lookup(dentry, parent);
3842 +       if (err)
3843 +               goto out;
3844 +
3845 +       bstart = dbstart(dentry);
3846 +       bend = dbend(dentry);
3847 +       bopaque = dbopaque(dentry);
3848 +       if (0 <= bopaque && bopaque < bend)
3849 +               bend = bopaque;
3850 +
3851 +       buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3852 +       if (unlikely(!buf)) {
3853 +               err = -ENOMEM;
3854 +               goto out;
3855 +       }
3856 +       buf->err = 0;
3857 +       buf->mode = RD_CHECK_EMPTY;
3858 +       buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3859 +       if (unlikely(!buf->rdstate)) {
3860 +               err = -ENOMEM;
3861 +               goto out;
3862 +       }
3863 +
3864 +       /* Process the lower directories with rdutil_callback as a filldir. */
3865 +       for (bindex = bstart; bindex <= bend; bindex++) {
3866 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3867 +               if (!lower_dentry)
3868 +                       continue;
3869 +               if (!lower_dentry->d_inode)
3870 +                       continue;
3871 +               if (!S_ISDIR(lower_dentry->d_inode->i_mode))
3872 +                       continue;
3873 +
3874 +               dget(lower_dentry);
3875 +               mnt = unionfs_mntget(dentry, bindex);
3876 +               branchget(sb, bindex);
3877 +               lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
3878 +               if (IS_ERR(lower_file)) {
3879 +                       err = PTR_ERR(lower_file);
3880 +                       branchput(sb, bindex);
3881 +                       goto out;
3882 +               }
3883 +
3884 +               do {
3885 +                       buf->filldir_called = 0;
3886 +                       buf->rdstate->bindex = bindex;
3887 +                       err = vfs_readdir(lower_file,
3888 +                                         readdir_util_callback, buf);
3889 +                       if (buf->err)
3890 +                               err = buf->err;
3891 +               } while ((err >= 0) && buf->filldir_called);
3892 +
3893 +               /* fput calls dput for lower_dentry */
3894 +               fput(lower_file);
3895 +               branchput(sb, bindex);
3896 +
3897 +               if (err < 0)
3898 +                       goto out;
3899 +       }
3900 +
3901 +out:
3902 +       if (buf) {
3903 +               if (namelist && !err)
3904 +                       *namelist = buf->rdstate;
3905 +               else if (buf->rdstate)
3906 +                       free_rdstate(buf->rdstate);
3907 +               kfree(buf);
3908 +       }
3909 +
3910 +
3911 +       return err;
3912 +}
3913 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/fanout.h linux-2.6.35-rc6-git-unionfs//fs/unionfs/fanout.h
3914 --- linux-2.6.35-rc6-git//fs/unionfs/fanout.h   1970-01-01 02:00:00.000000000 +0200
3915 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/fanout.h   2010-07-30 15:50:50.559010056 +0300
3916 @@ -0,0 +1,407 @@
3917 +/*
3918 + * Copyright (c) 2003-2010 Erez Zadok
3919 + * Copyright (c) 2003-2006 Charles P. Wright
3920 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3921 + * Copyright (c) 2005      Arun M. Krishnakumar
3922 + * Copyright (c) 2004-2006 David P. Quigley
3923 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3924 + * Copyright (c) 2003      Puja Gupta
3925 + * Copyright (c) 2003      Harikesavan Krishnan
3926 + * Copyright (c) 2003-2010 Stony Brook University
3927 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
3928 + *
3929 + * This program is free software; you can redistribute it and/or modify
3930 + * it under the terms of the GNU General Public License version 2 as
3931 + * published by the Free Software Foundation.
3932 + */
3933 +
3934 +#ifndef _FANOUT_H_
3935 +#define _FANOUT_H_
3936 +
3937 +/*
3938 + * Inode to private data
3939 + *
3940 + * Since we use containers and the struct inode is _inside_ the
3941 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
3942 + * inode pointer), return a valid non-NULL pointer.
3943 + */
3944 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
3945 +{
3946 +       return container_of(inode, struct unionfs_inode_info, vfs_inode);
3947 +}
3948 +
3949 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
3950 +#define ibend(ino) (UNIONFS_I(ino)->bend)
3951 +
3952 +/* Dentry to private data */
3953 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
3954 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
3955 +#define dbend(dent) (UNIONFS_D(dent)->bend)
3956 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
3957 +
3958 +/* Superblock to private data */
3959 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
3960 +#define sbstart(sb) 0
3961 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
3962 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
3963 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
3964 +
3965 +/* File to private Data */
3966 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
3967 +#define fbstart(file) (UNIONFS_F(file)->bstart)
3968 +#define fbend(file) (UNIONFS_F(file)->bend)
3969 +
3970 +/* macros to manipulate branch IDs in stored in our superblock */
3971 +static inline int branch_id(struct super_block *sb, int index)
3972 +{
3973 +       BUG_ON(!sb || index < 0);
3974 +       return UNIONFS_SB(sb)->data[index].branch_id;
3975 +}
3976 +
3977 +static inline void set_branch_id(struct super_block *sb, int index, int val)
3978 +{
3979 +       BUG_ON(!sb || index < 0);
3980 +       UNIONFS_SB(sb)->data[index].branch_id = val;
3981 +}
3982 +
3983 +static inline void new_branch_id(struct super_block *sb, int index)
3984 +{
3985 +       BUG_ON(!sb || index < 0);
3986 +       set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
3987 +}
3988 +
3989 +/*
3990 + * Find new index of matching branch with an existing superblock of a known
3991 + * (possibly old) id.  This is needed because branches could have been
3992 + * added/deleted causing the branches of any open files to shift.
3993 + *
3994 + * @sb: the new superblock which may have new/different branch IDs
3995 + * @id: the old/existing id we're looking for
3996 + * Returns index of newly found branch (0 or greater), -1 otherwise.
3997 + */
3998 +static inline int branch_id_to_idx(struct super_block *sb, int id)
3999 +{
4000 +       int i;
4001 +       for (i = 0; i < sbmax(sb); i++) {
4002 +               if (branch_id(sb, i) == id)
4003 +                       return i;
4004 +       }
4005 +       /* in the non-ODF code, this should really never happen */
4006 +       printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4007 +       return -1;
4008 +}
4009 +
4010 +/* File to lower file. */
4011 +static inline struct file *unionfs_lower_file(const struct file *f)
4012 +{
4013 +       BUG_ON(!f);
4014 +       return UNIONFS_F(f)->lower_files[fbstart(f)];
4015 +}
4016 +
4017 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4018 +                                                 int index)
4019 +{
4020 +       BUG_ON(!f || index < 0);
4021 +       return UNIONFS_F(f)->lower_files[index];
4022 +}
4023 +
4024 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4025 +                                             struct file *val)
4026 +{
4027 +       BUG_ON(!f || index < 0);
4028 +       UNIONFS_F(f)->lower_files[index] = val;
4029 +       /* save branch ID (may be redundant?) */
4030 +       UNIONFS_F(f)->saved_branch_ids[index] =
4031 +               branch_id((f)->f_path.dentry->d_sb, index);
4032 +}
4033 +
4034 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4035 +{
4036 +       BUG_ON(!f);
4037 +       unionfs_set_lower_file_idx((f), fbstart(f), (val));
4038 +}
4039 +
4040 +/* Inode to lower inode. */
4041 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4042 +{
4043 +       BUG_ON(!i);
4044 +       return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4045 +}
4046 +
4047 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4048 +                                                   int index)
4049 +{
4050 +       BUG_ON(!i || index < 0);
4051 +       return UNIONFS_I(i)->lower_inodes[index];
4052 +}
4053 +
4054 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4055 +                                              struct inode *val)
4056 +{
4057 +       BUG_ON(!i || index < 0);
4058 +       UNIONFS_I(i)->lower_inodes[index] = val;
4059 +}
4060 +
4061 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4062 +{
4063 +       BUG_ON(!i);
4064 +       UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4065 +}
4066 +
4067 +/* Superblock to lower superblock. */
4068 +static inline struct super_block *unionfs_lower_super(
4069 +                                       const struct super_block *sb)
4070 +{
4071 +       BUG_ON(!sb);
4072 +       return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4073 +}
4074 +
4075 +static inline struct super_block *unionfs_lower_super_idx(
4076 +                                       const struct super_block *sb,
4077 +                                       int index)
4078 +{
4079 +       BUG_ON(!sb || index < 0);
4080 +       return UNIONFS_SB(sb)->data[index].sb;
4081 +}
4082 +
4083 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4084 +                                              int index,
4085 +                                              struct super_block *val)
4086 +{
4087 +       BUG_ON(!sb || index < 0);
4088 +       UNIONFS_SB(sb)->data[index].sb = val;
4089 +}
4090 +
4091 +static inline void unionfs_set_lower_super(struct super_block *sb,
4092 +                                          struct super_block *val)
4093 +{
4094 +       BUG_ON(!sb);
4095 +       UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4096 +}
4097 +
4098 +/* Branch count macros. */
4099 +static inline int branch_count(const struct super_block *sb, int index)
4100 +{
4101 +       BUG_ON(!sb || index < 0);
4102 +       return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4103 +}
4104 +
4105 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4106 +{
4107 +       BUG_ON(!sb || index < 0);
4108 +       atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4109 +}
4110 +
4111 +static inline void branchget(struct super_block *sb, int index)
4112 +{
4113 +       BUG_ON(!sb || index < 0);
4114 +       atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4115 +}
4116 +
4117 +static inline void branchput(struct super_block *sb, int index)
4118 +{
4119 +       BUG_ON(!sb || index < 0);
4120 +       atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4121 +}
4122 +
4123 +/* Dentry macros */
4124 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4125 +                                               struct dentry *val)
4126 +{
4127 +       BUG_ON(!dent || index < 0);
4128 +       UNIONFS_D(dent)->lower_paths[index].dentry = val;
4129 +}
4130 +
4131 +static inline struct dentry *unionfs_lower_dentry_idx(
4132 +                               const struct dentry *dent,
4133 +                               int index)
4134 +{
4135 +       BUG_ON(!dent || index < 0);
4136 +       return UNIONFS_D(dent)->lower_paths[index].dentry;
4137 +}
4138 +
4139 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4140 +{
4141 +       BUG_ON(!dent);
4142 +       return unionfs_lower_dentry_idx(dent, dbstart(dent));
4143 +}
4144 +
4145 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4146 +                                            struct vfsmount *mnt)
4147 +{
4148 +       BUG_ON(!dent || index < 0);
4149 +       UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4150 +}
4151 +
4152 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4153 +                                       const struct dentry *dent,
4154 +                                       int index)
4155 +{
4156 +       BUG_ON(!dent || index < 0);
4157 +       return UNIONFS_D(dent)->lower_paths[index].mnt;
4158 +}
4159 +
4160 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4161 +{
4162 +       BUG_ON(!dent);
4163 +       return unionfs_lower_mnt_idx(dent, dbstart(dent));
4164 +}
4165 +
4166 +/* Macros for locking a dentry. */
4167 +enum unionfs_dentry_lock_class {
4168 +       UNIONFS_DMUTEX_NORMAL,
4169 +       UNIONFS_DMUTEX_ROOT,
4170 +       UNIONFS_DMUTEX_PARENT,
4171 +       UNIONFS_DMUTEX_CHILD,
4172 +       UNIONFS_DMUTEX_WHITEOUT,
4173 +       UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4174 +       UNIONFS_DMUTEX_REVAL_CHILD,   /* for file/dentry revalidate */
4175 +};
4176 +
4177 +static inline void unionfs_lock_dentry(struct dentry *d,
4178 +                                      unsigned int subclass)
4179 +{
4180 +       BUG_ON(!d);
4181 +       mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4182 +}
4183 +
4184 +static inline void unionfs_unlock_dentry(struct dentry *d)
4185 +{
4186 +       BUG_ON(!d);
4187 +       mutex_unlock(&UNIONFS_D(d)->lock);
4188 +}
4189 +
4190 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4191 +                                                unsigned int subclass)
4192 +{
4193 +       struct dentry *p;
4194 +
4195 +       BUG_ON(!d);
4196 +       p = dget_parent(d);
4197 +       if (p != d)
4198 +               mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4199 +       return p;
4200 +}
4201 +
4202 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4203 +{
4204 +       BUG_ON(!d);
4205 +       BUG_ON(!p);
4206 +       if (p != d) {
4207 +               BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4208 +               mutex_unlock(&UNIONFS_D(p)->lock);
4209 +       }
4210 +       dput(p);
4211 +}
4212 +
4213 +static inline void verify_locked(struct dentry *d)
4214 +{
4215 +       BUG_ON(!d);
4216 +       BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4217 +}
4218 +
4219 +/* macros to put lower objects */
4220 +
4221 +/*
4222 + * iput lower inodes of an unionfs dentry, from bstart to bend.  If
4223 + * @free_lower is true, then also kfree the memory used to hold the lower
4224 + * object pointers.
4225 + */
4226 +static inline void iput_lowers(struct inode *inode,
4227 +                              int bstart, int bend, bool free_lower)
4228 +{
4229 +       struct inode *lower_inode;
4230 +       int bindex;
4231 +
4232 +       BUG_ON(!inode);
4233 +       BUG_ON(!UNIONFS_I(inode));
4234 +       BUG_ON(bstart < 0);
4235 +
4236 +       for (bindex = bstart; bindex <= bend; bindex++) {
4237 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
4238 +               if (lower_inode) {
4239 +                       unionfs_set_lower_inode_idx(inode, bindex, NULL);
4240 +                       /* see Documentation/filesystems/unionfs/issues.txt */
4241 +                       lockdep_off();
4242 +                       iput(lower_inode);
4243 +                       lockdep_on();
4244 +               }
4245 +       }
4246 +
4247 +       if (free_lower) {
4248 +               kfree(UNIONFS_I(inode)->lower_inodes);
4249 +               UNIONFS_I(inode)->lower_inodes = NULL;
4250 +       }
4251 +}
4252 +
4253 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4254 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4255 +{
4256 +       int bstart, bend;
4257 +
4258 +       BUG_ON(!inode);
4259 +       BUG_ON(!UNIONFS_I(inode));
4260 +       bstart = ibstart(inode);
4261 +       bend = ibend(inode);
4262 +       BUG_ON(bstart < 0);
4263 +
4264 +       iput_lowers(inode, bstart, bend, free_lower);
4265 +       ibstart(inode) = ibend(inode) = -1;
4266 +}
4267 +
4268 +/*
4269 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4270 + * bstart to bend.  If @free_lower is true, then also kfree the memory used
4271 + * to hold the lower object pointers.
4272 + *
4273 + * XXX: implement using path_put VFS macros
4274 + */
4275 +static inline void path_put_lowers(struct dentry *dentry,
4276 +                                  int bstart, int bend, bool free_lower)
4277 +{
4278 +       struct dentry *lower_dentry;
4279 +       struct vfsmount *lower_mnt;
4280 +       int bindex;
4281 +
4282 +       BUG_ON(!dentry);
4283 +       BUG_ON(!UNIONFS_D(dentry));
4284 +       BUG_ON(bstart < 0);
4285 +
4286 +       for (bindex = bstart; bindex <= bend; bindex++) {
4287 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4288 +               if (lower_dentry) {
4289 +                       unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4290 +                       dput(lower_dentry);
4291 +               }
4292 +               lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4293 +               if (lower_mnt) {
4294 +                       unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4295 +                       mntput(lower_mnt);
4296 +               }
4297 +       }
4298 +
4299 +       if (free_lower) {
4300 +               kfree(UNIONFS_D(dentry)->lower_paths);
4301 +               UNIONFS_D(dentry)->lower_paths = NULL;
4302 +       }
4303 +}
4304 +
4305 +/*
4306 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4307 + * indices to -1.
4308 + */
4309 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4310 +{
4311 +       int bstart, bend;
4312 +
4313 +       BUG_ON(!dentry);
4314 +       BUG_ON(!UNIONFS_D(dentry));
4315 +       bstart = dbstart(dentry);
4316 +       bend = dbend(dentry);
4317 +       BUG_ON(bstart < 0);
4318 +
4319 +       path_put_lowers(dentry, bstart, bend, free_lower);
4320 +       dbstart(dentry) = dbend(dentry) = -1;
4321 +}
4322 +
4323 +#endif /* not _FANOUT_H */
4324 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/file.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/file.c
4325 --- linux-2.6.35-rc6-git//fs/unionfs/file.c     1970-01-01 02:00:00.000000000 +0200
4326 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/file.c     2010-07-30 15:50:50.559010056 +0300
4327 @@ -0,0 +1,380 @@
4328 +/*
4329 + * Copyright (c) 2003-2010 Erez Zadok
4330 + * Copyright (c) 2003-2006 Charles P. Wright
4331 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4332 + * Copyright (c) 2005-2006 Junjiro Okajima
4333 + * Copyright (c) 2005      Arun M. Krishnakumar
4334 + * Copyright (c) 2004-2006 David P. Quigley
4335 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4336 + * Copyright (c) 2003      Puja Gupta
4337 + * Copyright (c) 2003      Harikesavan Krishnan
4338 + * Copyright (c) 2003-2010 Stony Brook University
4339 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
4340 + *
4341 + * This program is free software; you can redistribute it and/or modify
4342 + * it under the terms of the GNU General Public License version 2 as
4343 + * published by the Free Software Foundation.
4344 + */
4345 +
4346 +#include "union.h"
4347 +
4348 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4349 +                           size_t count, loff_t *ppos)
4350 +{
4351 +       int err;
4352 +       struct file *lower_file;
4353 +       struct dentry *dentry = file->f_path.dentry;
4354 +       struct dentry *parent;
4355 +
4356 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4357 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4358 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4359 +
4360 +       err = unionfs_file_revalidate(file, parent, false);
4361 +       if (unlikely(err))
4362 +               goto out;
4363 +
4364 +       lower_file = unionfs_lower_file(file);
4365 +       err = vfs_read(lower_file, buf, count, ppos);
4366 +       /* update our inode atime upon a successful lower read */
4367 +       if (err >= 0) {
4368 +               fsstack_copy_attr_atime(dentry->d_inode,
4369 +                                       lower_file->f_path.dentry->d_inode);
4370 +               unionfs_check_file(file);
4371 +       }
4372 +
4373 +out:
4374 +       unionfs_unlock_dentry(dentry);
4375 +       unionfs_unlock_parent(dentry, parent);
4376 +       unionfs_read_unlock(dentry->d_sb);
4377 +       return err;
4378 +}
4379 +
4380 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4381 +                            size_t count, loff_t *ppos)
4382 +{
4383 +       int err = 0;
4384 +       struct file *lower_file;
4385 +       struct dentry *dentry = file->f_path.dentry;
4386 +       struct dentry *parent;
4387 +
4388 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4389 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4390 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4391 +
4392 +       err = unionfs_file_revalidate(file, parent, true);
4393 +       if (unlikely(err))
4394 +               goto out;
4395 +
4396 +       lower_file = unionfs_lower_file(file);
4397 +       err = vfs_write(lower_file, buf, count, ppos);
4398 +       /* update our inode times+sizes upon a successful lower write */
4399 +       if (err >= 0) {
4400 +               fsstack_copy_inode_size(dentry->d_inode,
4401 +                                       lower_file->f_path.dentry->d_inode);
4402 +               fsstack_copy_attr_times(dentry->d_inode,
4403 +                                       lower_file->f_path.dentry->d_inode);
4404 +               UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4405 +               unionfs_check_file(file);
4406 +       }
4407 +
4408 +out:
4409 +       unionfs_unlock_dentry(dentry);
4410 +       unionfs_unlock_parent(dentry, parent);
4411 +       unionfs_read_unlock(dentry->d_sb);
4412 +       return err;
4413 +}
4414 +
4415 +static int unionfs_file_readdir(struct file *file, void *dirent,
4416 +                               filldir_t filldir)
4417 +{
4418 +       return -ENOTDIR;
4419 +}
4420 +
4421 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4422 +{
4423 +       int err = 0;
4424 +       bool willwrite;
4425 +       struct file *lower_file;
4426 +       struct dentry *dentry = file->f_path.dentry;
4427 +       struct dentry *parent;
4428 +       const struct vm_operations_struct *saved_vm_ops = NULL;
4429 +
4430 +       /*
4431 +        * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4432 +        * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4433 +        * has been causing false positives in file system stacking layers.
4434 +        * In particular, our ->mmap is called after sys_mmap2 already holds
4435 +        * mmap_sem, then we lock our own mutexes; but earlier, it's
4436 +        * possible for lockdep to have locked our mutexes first, and then
4437 +        * we call a lower ->readdir which could call might_fault.  The
4438 +        * different ordering of the locks is what lockdep complains about
4439 +        * -- unnecessarily.  Therefore, we have no choice but to tell
4440 +        * lockdep to temporarily turn off lockdep here.  Note: the comments
4441 +        * inside might_sleep also suggest that it would have been
4442 +        * nicer to only annotate paths that needs that might_lock_read.
4443 +        */
4444 +       lockdep_off();
4445 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4446 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4447 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4448 +
4449 +       /* This might be deferred to mmap's writepage */
4450 +       willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4451 +       err = unionfs_file_revalidate(file, parent, willwrite);
4452 +       if (unlikely(err))
4453 +               goto out;
4454 +       unionfs_check_file(file);
4455 +
4456 +       /*
4457 +        * File systems which do not implement ->writepage may use
4458 +        * generic_file_readonly_mmap as their ->mmap op.  If you call
4459 +        * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4460 +        * But we cannot call the lower ->mmap op, so we can't tell that
4461 +        * writeable mappings won't work.  Therefore, our only choice is to
4462 +        * check if the lower file system supports the ->writepage, and if
4463 +        * not, return EINVAL (the same error that
4464 +        * generic_file_readonly_mmap returns in that case).
4465 +        */
4466 +       lower_file = unionfs_lower_file(file);
4467 +       if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4468 +               err = -EINVAL;
4469 +               printk(KERN_ERR "unionfs: branch %d file system does not "
4470 +                      "support writeable mmap\n", fbstart(file));
4471 +               goto out;
4472 +       }
4473 +
4474 +       /*
4475 +        * find and save lower vm_ops.
4476 +        *
4477 +        * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4478 +        */
4479 +       if (!UNIONFS_F(file)->lower_vm_ops) {
4480 +               err = lower_file->f_op->mmap(lower_file, vma);
4481 +               if (err) {
4482 +                       printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4483 +                       goto out;
4484 +               }
4485 +               saved_vm_ops = vma->vm_ops;
4486 +               err = do_munmap(current->mm, vma->vm_start,
4487 +                               vma->vm_end - vma->vm_start);
4488 +               if (err) {
4489 +                       printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4490 +                       goto out;
4491 +               }
4492 +       }
4493 +
4494 +       file->f_mapping->a_ops = &unionfs_dummy_aops;
4495 +       err = generic_file_mmap(file, vma);
4496 +       file->f_mapping->a_ops = &unionfs_aops;
4497 +       if (err) {
4498 +               printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4499 +               goto out;
4500 +       }
4501 +       vma->vm_ops = &unionfs_vm_ops;
4502 +       if (!UNIONFS_F(file)->lower_vm_ops)
4503 +               UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4504 +
4505 +out:
4506 +       if (!err) {
4507 +               /* copyup could cause parent dir times to change */
4508 +               unionfs_copy_attr_times(parent->d_inode);
4509 +               unionfs_check_file(file);
4510 +       }
4511 +       unionfs_unlock_dentry(dentry);
4512 +       unionfs_unlock_parent(dentry, parent);
4513 +       unionfs_read_unlock(dentry->d_sb);
4514 +       lockdep_on();
4515 +       return err;
4516 +}
4517 +
4518 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4519 +{
4520 +       int bindex, bstart, bend;
4521 +       struct file *lower_file;
4522 +       struct dentry *lower_dentry;
4523 +       struct dentry *parent;
4524 +       struct inode *lower_inode, *inode;
4525 +       int err = -EINVAL;
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, true);
4532 +       if (unlikely(err))
4533 +               goto out;
4534 +       unionfs_check_file(file);
4535 +
4536 +       bstart = fbstart(file);
4537 +       bend = fbend(file);
4538 +       if (bstart < 0 || bend < 0)
4539 +               goto out;
4540 +
4541 +       inode = dentry->d_inode;
4542 +       if (unlikely(!inode)) {
4543 +               printk(KERN_ERR
4544 +                      "unionfs: null lower inode in unionfs_fsync\n");
4545 +               goto out;
4546 +       }
4547 +       for (bindex = bstart; bindex <= bend; bindex++) {
4548 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
4549 +               if (!lower_inode || !lower_inode->i_fop->fsync)
4550 +                       continue;
4551 +               lower_file = unionfs_lower_file_idx(file, bindex);
4552 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4553 +               mutex_lock(&lower_inode->i_mutex);
4554 +               err = lower_inode->i_fop->fsync(lower_file,
4555 +                                               lower_dentry,
4556 +                                               datasync);
4557 +               if (!err && bindex == bstart)
4558 +                       fsstack_copy_attr_times(inode, lower_inode);
4559 +               mutex_unlock(&lower_inode->i_mutex);
4560 +               if (err)
4561 +                       goto out;
4562 +       }
4563 +
4564 +out:
4565 +       if (!err)
4566 +               unionfs_check_file(file);
4567 +       unionfs_unlock_dentry(dentry);
4568 +       unionfs_unlock_parent(dentry, parent);
4569 +       unionfs_read_unlock(dentry->d_sb);
4570 +       return err;
4571 +}
4572 +
4573 +int unionfs_fasync(int fd, struct file *file, int flag)
4574 +{
4575 +       int bindex, bstart, bend;
4576 +       struct file *lower_file;
4577 +       struct dentry *dentry = file->f_path.dentry;
4578 +       struct dentry *parent;
4579 +       struct inode *lower_inode, *inode;
4580 +       int err = 0;
4581 +
4582 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4583 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4584 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4585 +
4586 +       err = unionfs_file_revalidate(file, parent, true);
4587 +       if (unlikely(err))
4588 +               goto out;
4589 +       unionfs_check_file(file);
4590 +
4591 +       bstart = fbstart(file);
4592 +       bend = fbend(file);
4593 +       if (bstart < 0 || bend < 0)
4594 +               goto out;
4595 +
4596 +       inode = dentry->d_inode;
4597 +       if (unlikely(!inode)) {
4598 +               printk(KERN_ERR
4599 +                      "unionfs: null lower inode in unionfs_fasync\n");
4600 +               goto out;
4601 +       }
4602 +       for (bindex = bstart; bindex <= bend; bindex++) {
4603 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
4604 +               if (!lower_inode || !lower_inode->i_fop->fasync)
4605 +                       continue;
4606 +               lower_file = unionfs_lower_file_idx(file, bindex);
4607 +               mutex_lock(&lower_inode->i_mutex);
4608 +               err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4609 +               if (!err && bindex == bstart)
4610 +                       fsstack_copy_attr_times(inode, lower_inode);
4611 +               mutex_unlock(&lower_inode->i_mutex);
4612 +               if (err)
4613 +                       goto out;
4614 +       }
4615 +
4616 +out:
4617 +       if (!err)
4618 +               unionfs_check_file(file);
4619 +       unionfs_unlock_dentry(dentry);
4620 +       unionfs_unlock_parent(dentry, parent);
4621 +       unionfs_read_unlock(dentry->d_sb);
4622 +       return err;
4623 +}
4624 +
4625 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4626 +                                  struct pipe_inode_info *pipe, size_t len,
4627 +                                  unsigned int flags)
4628 +{
4629 +       ssize_t err;
4630 +       struct file *lower_file;
4631 +       struct dentry *dentry = file->f_path.dentry;
4632 +       struct dentry *parent;
4633 +
4634 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4635 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4636 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4637 +
4638 +       err = unionfs_file_revalidate(file, parent, false);
4639 +       if (unlikely(err))
4640 +               goto out;
4641 +
4642 +       lower_file = unionfs_lower_file(file);
4643 +       err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4644 +       /* update our inode atime upon a successful lower splice-read */
4645 +       if (err >= 0) {
4646 +               fsstack_copy_attr_atime(dentry->d_inode,
4647 +                                       lower_file->f_path.dentry->d_inode);
4648 +               unionfs_check_file(file);
4649 +       }
4650 +
4651 +out:
4652 +       unionfs_unlock_dentry(dentry);
4653 +       unionfs_unlock_parent(dentry, parent);
4654 +       unionfs_read_unlock(dentry->d_sb);
4655 +       return err;
4656 +}
4657 +
4658 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4659 +                                   struct file *file, loff_t *ppos,
4660 +                                   size_t len, unsigned int flags)
4661 +{
4662 +       ssize_t err = 0;
4663 +       struct file *lower_file;
4664 +       struct dentry *dentry = file->f_path.dentry;
4665 +       struct dentry *parent;
4666 +
4667 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4668 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4669 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4670 +
4671 +       err = unionfs_file_revalidate(file, parent, true);
4672 +       if (unlikely(err))
4673 +               goto out;
4674 +
4675 +       lower_file = unionfs_lower_file(file);
4676 +       err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4677 +       /* update our inode times+sizes upon a successful lower write */
4678 +       if (err >= 0) {
4679 +               fsstack_copy_inode_size(dentry->d_inode,
4680 +                                       lower_file->f_path.dentry->d_inode);
4681 +               fsstack_copy_attr_times(dentry->d_inode,
4682 +                                       lower_file->f_path.dentry->d_inode);
4683 +               unionfs_check_file(file);
4684 +       }
4685 +
4686 +out:
4687 +       unionfs_unlock_dentry(dentry);
4688 +       unionfs_unlock_parent(dentry, parent);
4689 +       unionfs_read_unlock(dentry->d_sb);
4690 +       return err;
4691 +}
4692 +
4693 +struct file_operations unionfs_main_fops = {
4694 +       .llseek         = generic_file_llseek,
4695 +       .read           = unionfs_read,
4696 +       .write          = unionfs_write,
4697 +       .readdir        = unionfs_file_readdir,
4698 +       .unlocked_ioctl = unionfs_ioctl,
4699 +       .mmap           = unionfs_mmap,
4700 +       .open           = unionfs_open,
4701 +       .flush          = unionfs_flush,
4702 +       .release        = unionfs_file_release,
4703 +       .fsync          = unionfs_fsync,
4704 +       .fasync         = unionfs_fasync,
4705 +       .splice_read    = unionfs_splice_read,
4706 +       .splice_write   = unionfs_splice_write,
4707 +};
4708 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/inode.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/inode.c
4709 --- linux-2.6.35-rc6-git//fs/unionfs/inode.c    1970-01-01 02:00:00.000000000 +0200
4710 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/inode.c    2010-07-30 15:50:50.560010144 +0300
4711 @@ -0,0 +1,1055 @@
4712 +/*
4713 + * Copyright (c) 2003-2010 Erez Zadok
4714 + * Copyright (c) 2003-2006 Charles P. Wright
4715 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4716 + * Copyright (c) 2005-2006 Junjiro Okajima
4717 + * Copyright (c) 2005      Arun M. Krishnakumar
4718 + * Copyright (c) 2004-2006 David P. Quigley
4719 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4720 + * Copyright (c) 2003      Puja Gupta
4721 + * Copyright (c) 2003      Harikesavan Krishnan
4722 + * Copyright (c) 2003-2010 Stony Brook University
4723 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
4724 + *
4725 + * This program is free software; you can redistribute it and/or modify
4726 + * it under the terms of the GNU General Public License version 2 as
4727 + * published by the Free Software Foundation.
4728 + */
4729 +
4730 +#include "union.h"
4731 +
4732 +/*
4733 + * Find a writeable branch to create new object in.  Checks all writeble
4734 + * branches of the parent inode, from istart to iend order; if none are
4735 + * suitable, also tries branch 0 (which may require a copyup).
4736 + *
4737 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4738 + */
4739 +static struct dentry *find_writeable_branch(struct inode *parent,
4740 +                                           struct dentry *dentry)
4741 +{
4742 +       int err = -EINVAL;
4743 +       int bindex, istart, iend;
4744 +       struct dentry *lower_dentry = NULL;
4745 +
4746 +       istart = ibstart(parent);
4747 +       iend = ibend(parent);
4748 +       if (istart < 0)
4749 +               goto out;
4750 +
4751 +begin:
4752 +       for (bindex = istart; bindex <= iend; bindex++) {
4753 +               /* skip non-writeable branches */
4754 +               err = is_robranch_super(dentry->d_sb, bindex);
4755 +               if (err) {
4756 +                       err = -EROFS;
4757 +                       continue;
4758 +               }
4759 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4760 +               if (!lower_dentry)
4761 +                       continue;
4762 +               /*
4763 +                * check for whiteouts in writeable branch, and remove them
4764 +                * if necessary.
4765 +                */
4766 +               err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4767 +               if (err > 0)    /* ignore if whiteout found and removed */
4768 +                       err = 0;
4769 +               if (err)
4770 +                       continue;
4771 +               /* if get here, we can write to the branch */
4772 +               break;
4773 +       }
4774 +       /*
4775 +        * If istart wasn't already branch 0, and we got any error, then try
4776 +        * branch 0 (which may require copyup)
4777 +        */
4778 +       if (err && istart > 0) {
4779 +               istart = iend = 0;
4780 +               goto begin;
4781 +       }
4782 +
4783 +       /*
4784 +        * If we tried even branch 0, and still got an error, abort.  But if
4785 +        * the error was an EROFS, then we should try to copyup.
4786 +        */
4787 +       if (err && err != -EROFS)
4788 +               goto out;
4789 +
4790 +       /*
4791 +        * If we get here, then check if copyup needed.  If lower_dentry is
4792 +        * NULL, create the entire dentry directory structure in branch 0.
4793 +        */
4794 +       if (!lower_dentry) {
4795 +               bindex = 0;
4796 +               lower_dentry = create_parents(parent, dentry,
4797 +                                             dentry->d_name.name, bindex);
4798 +               if (IS_ERR(lower_dentry)) {
4799 +                       err = PTR_ERR(lower_dentry);
4800 +                       goto out;
4801 +               }
4802 +       }
4803 +       err = 0;                /* all's well */
4804 +out:
4805 +       if (err)
4806 +               return ERR_PTR(err);
4807 +       return lower_dentry;
4808 +}
4809 +
4810 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4811 +                         int mode, struct nameidata *nd_unused)
4812 +{
4813 +       int err = 0;
4814 +       struct dentry *lower_dentry = NULL;
4815 +       struct dentry *lower_parent_dentry = NULL;
4816 +       struct dentry *parent;
4817 +       int valid = 0;
4818 +       struct nameidata lower_nd;
4819 +
4820 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4821 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4822 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4823 +
4824 +       valid = __unionfs_d_revalidate(dentry, parent, false);
4825 +       if (unlikely(!valid)) {
4826 +               err = -ESTALE;  /* same as what real_lookup does */
4827 +               goto out;
4828 +       }
4829 +
4830 +       lower_dentry = find_writeable_branch(dir, dentry);
4831 +       if (IS_ERR(lower_dentry)) {
4832 +               err = PTR_ERR(lower_dentry);
4833 +               goto out;
4834 +       }
4835 +
4836 +       lower_parent_dentry = lock_parent(lower_dentry);
4837 +       if (IS_ERR(lower_parent_dentry)) {
4838 +               err = PTR_ERR(lower_parent_dentry);
4839 +               goto out_unlock;
4840 +       }
4841 +
4842 +       err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4843 +       if (unlikely(err < 0))
4844 +               goto out_unlock;
4845 +       err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4846 +                        &lower_nd);
4847 +       release_lower_nd(&lower_nd, err);
4848 +
4849 +       if (!err) {
4850 +               err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
4851 +               if (!err) {
4852 +                       unionfs_copy_attr_times(dir);
4853 +                       fsstack_copy_inode_size(dir,
4854 +                                               lower_parent_dentry->d_inode);
4855 +                       /* update no. of links on parent directory */
4856 +                       dir->i_nlink = unionfs_get_nlinks(dir);
4857 +               }
4858 +       }
4859 +
4860 +out_unlock:
4861 +       unlock_dir(lower_parent_dentry);
4862 +out:
4863 +       if (!err) {
4864 +               unionfs_postcopyup_setmnt(dentry);
4865 +               unionfs_check_inode(dir);
4866 +               unionfs_check_dentry(dentry);
4867 +       }
4868 +       unionfs_unlock_dentry(dentry);
4869 +       unionfs_unlock_parent(dentry, parent);
4870 +       unionfs_read_unlock(dentry->d_sb);
4871 +       return err;
4872 +}
4873 +
4874 +/*
4875 + * unionfs_lookup is the only special function which takes a dentry, yet we
4876 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
4877 + * we don't have a valid dentry here yet.
4878 + */
4879 +static struct dentry *unionfs_lookup(struct inode *dir,
4880 +                                    struct dentry *dentry,
4881 +                                    struct nameidata *nd_unused)
4882 +{
4883 +       struct dentry *ret, *parent;
4884 +       int err = 0;
4885 +
4886 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4887 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4888 +
4889 +       /*
4890 +        * As long as we lock/dget the parent, then can skip validating the
4891 +        * parent now; we may have to rebuild this dentry on the next
4892 +        * ->d_revalidate, however.
4893 +        */
4894 +
4895 +       /* allocate dentry private data.  We free it in ->d_release */
4896 +       err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
4897 +       if (unlikely(err)) {
4898 +               ret = ERR_PTR(err);
4899 +               goto out;
4900 +       }
4901 +
4902 +       ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
4903 +
4904 +       if (!IS_ERR(ret)) {
4905 +               if (ret)
4906 +                       dentry = ret;
4907 +               /* lookup_full can return multiple positive dentries */
4908 +               if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
4909 +                       BUG_ON(dbstart(dentry) < 0);
4910 +                       unionfs_postcopyup_release(dentry);
4911 +               }
4912 +               unionfs_copy_attr_times(dentry->d_inode);
4913 +       }
4914 +
4915 +       unionfs_check_inode(dir);
4916 +       if (!IS_ERR(ret))
4917 +               unionfs_check_dentry(dentry);
4918 +       unionfs_check_dentry(parent);
4919 +       unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
4920 +
4921 +out:
4922 +       unionfs_unlock_parent(dentry, parent);
4923 +       unionfs_read_unlock(dentry->d_sb);
4924 +
4925 +       return ret;
4926 +}
4927 +
4928 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
4929 +                       struct dentry *new_dentry)
4930 +{
4931 +       int err = 0;
4932 +       struct dentry *lower_old_dentry = NULL;
4933 +       struct dentry *lower_new_dentry = NULL;
4934 +       struct dentry *lower_dir_dentry = NULL;
4935 +       struct dentry *old_parent, *new_parent;
4936 +       char *name = NULL;
4937 +       bool valid;
4938 +
4939 +       unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4940 +       old_parent = dget_parent(old_dentry);
4941 +       new_parent = dget_parent(new_dentry);
4942 +       unionfs_double_lock_parents(old_parent, new_parent);
4943 +       unionfs_double_lock_dentry(old_dentry, new_dentry);
4944 +
4945 +       valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
4946 +       if (unlikely(!valid)) {
4947 +               err = -ESTALE;
4948 +               goto out;
4949 +       }
4950 +       if (new_dentry->d_inode) {
4951 +               valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
4952 +               if (unlikely(!valid)) {
4953 +                       err = -ESTALE;
4954 +                       goto out;
4955 +               }
4956 +       }
4957 +
4958 +       lower_new_dentry = unionfs_lower_dentry(new_dentry);
4959 +
4960 +       /* check for a whiteout in new dentry branch, and delete it */
4961 +       err = check_unlink_whiteout(new_dentry, lower_new_dentry,
4962 +                                   dbstart(new_dentry));
4963 +       if (err > 0) {         /* whiteout found and removed successfully */
4964 +               lower_dir_dentry = dget_parent(lower_new_dentry);
4965 +               fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
4966 +               dput(lower_dir_dentry);
4967 +               dir->i_nlink = unionfs_get_nlinks(dir);
4968 +               err = 0;
4969 +       }
4970 +       if (err)
4971 +               goto out;
4972 +
4973 +       /* check if parent hierachy is needed, then link in same branch */
4974 +       if (dbstart(old_dentry) != dbstart(new_dentry)) {
4975 +               lower_new_dentry = create_parents(dir, new_dentry,
4976 +                                                 new_dentry->d_name.name,
4977 +                                                 dbstart(old_dentry));
4978 +               err = PTR_ERR(lower_new_dentry);
4979 +               if (IS_COPYUP_ERR(err))
4980 +                       goto docopyup;
4981 +               if (!lower_new_dentry || IS_ERR(lower_new_dentry))
4982 +                       goto out;
4983 +       }
4984 +       lower_new_dentry = unionfs_lower_dentry(new_dentry);
4985 +       lower_old_dentry = unionfs_lower_dentry(old_dentry);
4986 +
4987 +       BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
4988 +       lower_dir_dentry = lock_parent(lower_new_dentry);
4989 +       err = is_robranch(old_dentry);
4990 +       if (!err) {
4991 +               /* see Documentation/filesystems/unionfs/issues.txt */
4992 +               lockdep_off();
4993 +               err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
4994 +                              lower_new_dentry);
4995 +               lockdep_on();
4996 +       }
4997 +       unlock_dir(lower_dir_dentry);
4998 +
4999 +docopyup:
5000 +       if (IS_COPYUP_ERR(err)) {
5001 +               int old_bstart = dbstart(old_dentry);
5002 +               int bindex;
5003 +
5004 +               for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5005 +                       err = copyup_dentry(old_parent->d_inode,
5006 +                                           old_dentry, old_bstart,
5007 +                                           bindex, old_dentry->d_name.name,
5008 +                                           old_dentry->d_name.len, NULL,
5009 +                                           i_size_read(old_dentry->d_inode));
5010 +                       if (err)
5011 +                               continue;
5012 +                       lower_new_dentry =
5013 +                               create_parents(dir, new_dentry,
5014 +                                              new_dentry->d_name.name,
5015 +                                              bindex);
5016 +                       lower_old_dentry = unionfs_lower_dentry(old_dentry);
5017 +                       lower_dir_dentry = lock_parent(lower_new_dentry);
5018 +                       /* see Documentation/filesystems/unionfs/issues.txt */
5019 +                       lockdep_off();
5020 +                       /* do vfs_link */
5021 +                       err = vfs_link(lower_old_dentry,
5022 +                                      lower_dir_dentry->d_inode,
5023 +                                      lower_new_dentry);
5024 +                       lockdep_on();
5025 +                       unlock_dir(lower_dir_dentry);
5026 +                       goto check_link;
5027 +               }
5028 +               goto out;
5029 +       }
5030 +
5031 +check_link:
5032 +       if (err || !lower_new_dentry->d_inode)
5033 +               goto out;
5034 +
5035 +       /* Its a hard link, so use the same inode */
5036 +       new_dentry->d_inode = igrab(old_dentry->d_inode);
5037 +       d_add(new_dentry, new_dentry->d_inode);
5038 +       unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5039 +       fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5040 +
5041 +       /* propagate number of hard-links */
5042 +       old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5043 +       /* new dentry's ctime may have changed due to hard-link counts */
5044 +       unionfs_copy_attr_times(new_dentry->d_inode);
5045 +
5046 +out:
5047 +       if (!new_dentry->d_inode)
5048 +               d_drop(new_dentry);
5049 +
5050 +       kfree(name);
5051 +       if (!err)
5052 +               unionfs_postcopyup_setmnt(new_dentry);
5053 +
5054 +       unionfs_check_inode(dir);
5055 +       unionfs_check_dentry(new_dentry);
5056 +       unionfs_check_dentry(old_dentry);
5057 +
5058 +       unionfs_double_unlock_dentry(old_dentry, new_dentry);
5059 +       unionfs_double_unlock_parents(old_parent, new_parent);
5060 +       dput(new_parent);
5061 +       dput(old_parent);
5062 +       unionfs_read_unlock(old_dentry->d_sb);
5063 +
5064 +       return err;
5065 +}
5066 +
5067 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5068 +                          const char *symname)
5069 +{
5070 +       int err = 0;
5071 +       struct dentry *lower_dentry = NULL;
5072 +       struct dentry *wh_dentry = NULL;
5073 +       struct dentry *lower_parent_dentry = NULL;
5074 +       struct dentry *parent;
5075 +       char *name = NULL;
5076 +       int valid = 0;
5077 +       umode_t mode;
5078 +
5079 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5080 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5081 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5082 +
5083 +       valid = __unionfs_d_revalidate(dentry, parent, false);
5084 +       if (unlikely(!valid)) {
5085 +               err = -ESTALE;
5086 +               goto out;
5087 +       }
5088 +
5089 +       /*
5090 +        * It's only a bug if this dentry was not negative and couldn't be
5091 +        * revalidated (shouldn't happen).
5092 +        */
5093 +       BUG_ON(!valid && dentry->d_inode);
5094 +
5095 +       lower_dentry = find_writeable_branch(dir, dentry);
5096 +       if (IS_ERR(lower_dentry)) {
5097 +               err = PTR_ERR(lower_dentry);
5098 +               goto out;
5099 +       }
5100 +
5101 +       lower_parent_dentry = lock_parent(lower_dentry);
5102 +       if (IS_ERR(lower_parent_dentry)) {
5103 +               err = PTR_ERR(lower_parent_dentry);
5104 +               goto out_unlock;
5105 +       }
5106 +
5107 +       mode = S_IALLUGO;
5108 +       err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5109 +       if (!err) {
5110 +               err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5111 +               if (!err) {
5112 +                       unionfs_copy_attr_times(dir);
5113 +                       fsstack_copy_inode_size(dir,
5114 +                                               lower_parent_dentry->d_inode);
5115 +                       /* update no. of links on parent directory */
5116 +                       dir->i_nlink = unionfs_get_nlinks(dir);
5117 +               }
5118 +       }
5119 +
5120 +out_unlock:
5121 +       unlock_dir(lower_parent_dentry);
5122 +out:
5123 +       dput(wh_dentry);
5124 +       kfree(name);
5125 +
5126 +       if (!err) {
5127 +               unionfs_postcopyup_setmnt(dentry);
5128 +               unionfs_check_inode(dir);
5129 +               unionfs_check_dentry(dentry);
5130 +       }
5131 +       unionfs_unlock_dentry(dentry);
5132 +       unionfs_unlock_parent(dentry, parent);
5133 +       unionfs_read_unlock(dentry->d_sb);
5134 +       return err;
5135 +}
5136 +
5137 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5138 +{
5139 +       int err = 0;
5140 +       struct dentry *lower_dentry = NULL;
5141 +       struct dentry *lower_parent_dentry = NULL;
5142 +       struct dentry *parent;
5143 +       int bindex = 0, bstart;
5144 +       char *name = NULL;
5145 +       int valid;
5146 +
5147 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5148 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5149 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5150 +
5151 +       valid = __unionfs_d_revalidate(dentry, parent, false);
5152 +       if (unlikely(!valid)) {
5153 +               err = -ESTALE;  /* same as what real_lookup does */
5154 +               goto out;
5155 +       }
5156 +
5157 +       bstart = dbstart(dentry);
5158 +
5159 +       lower_dentry = unionfs_lower_dentry(dentry);
5160 +
5161 +       /* check for a whiteout in new dentry branch, and delete it */
5162 +       err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5163 +       if (err > 0)           /* whiteout found and removed successfully */
5164 +               err = 0;
5165 +       if (err) {
5166 +               /* exit if the error returned was NOT -EROFS */
5167 +               if (!IS_COPYUP_ERR(err))
5168 +                       goto out;
5169 +               bstart--;
5170 +       }
5171 +
5172 +       /* check if copyup's needed, and mkdir */
5173 +       for (bindex = bstart; bindex >= 0; bindex--) {
5174 +               int i;
5175 +               int bend = dbend(dentry);
5176 +
5177 +               if (is_robranch_super(dentry->d_sb, bindex))
5178 +                       continue;
5179 +
5180 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5181 +               if (!lower_dentry) {
5182 +                       lower_dentry = create_parents(dir, dentry,
5183 +                                                     dentry->d_name.name,
5184 +                                                     bindex);
5185 +                       if (!lower_dentry || IS_ERR(lower_dentry)) {
5186 +                               printk(KERN_ERR "unionfs: lower dentry "
5187 +                                      " NULL for bindex = %d\n", bindex);
5188 +                               continue;
5189 +                       }
5190 +               }
5191 +
5192 +               lower_parent_dentry = lock_parent(lower_dentry);
5193 +
5194 +               if (IS_ERR(lower_parent_dentry)) {
5195 +                       err = PTR_ERR(lower_parent_dentry);
5196 +                       goto out;
5197 +               }
5198 +
5199 +               err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5200 +                               mode);
5201 +
5202 +               unlock_dir(lower_parent_dentry);
5203 +
5204 +               /* did the mkdir succeed? */
5205 +               if (err)
5206 +                       break;
5207 +
5208 +               for (i = bindex + 1; i <= bend; i++) {
5209 +                       /* XXX: use path_put_lowers? */
5210 +                       if (unionfs_lower_dentry_idx(dentry, i)) {
5211 +                               dput(unionfs_lower_dentry_idx(dentry, i));
5212 +                               unionfs_set_lower_dentry_idx(dentry, i, NULL);
5213 +                       }
5214 +               }
5215 +               dbend(dentry) = bindex;
5216 +
5217 +               /*
5218 +                * Only INTERPOSE_LOOKUP can return a value other than 0 on
5219 +                * err.
5220 +                */
5221 +               err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5222 +               if (!err) {
5223 +                       unionfs_copy_attr_times(dir);
5224 +                       fsstack_copy_inode_size(dir,
5225 +                                               lower_parent_dentry->d_inode);
5226 +
5227 +                       /* update number of links on parent directory */
5228 +                       dir->i_nlink = unionfs_get_nlinks(dir);
5229 +               }
5230 +
5231 +               err = make_dir_opaque(dentry, dbstart(dentry));
5232 +               if (err) {
5233 +                       printk(KERN_ERR "unionfs: mkdir: error creating "
5234 +                              ".wh.__dir_opaque: %d\n", err);
5235 +                       goto out;
5236 +               }
5237 +
5238 +               /* we are done! */
5239 +               break;
5240 +       }
5241 +
5242 +out:
5243 +       if (!dentry->d_inode)
5244 +               d_drop(dentry);
5245 +
5246 +       kfree(name);
5247 +
5248 +       if (!err) {
5249 +               unionfs_copy_attr_times(dentry->d_inode);
5250 +               unionfs_postcopyup_setmnt(dentry);
5251 +       }
5252 +       unionfs_check_inode(dir);
5253 +       unionfs_check_dentry(dentry);
5254 +       unionfs_unlock_dentry(dentry);
5255 +       unionfs_unlock_parent(dentry, parent);
5256 +       unionfs_read_unlock(dentry->d_sb);
5257 +
5258 +       return err;
5259 +}
5260 +
5261 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5262 +                        dev_t dev)
5263 +{
5264 +       int err = 0;
5265 +       struct dentry *lower_dentry = NULL;
5266 +       struct dentry *wh_dentry = NULL;
5267 +       struct dentry *lower_parent_dentry = NULL;
5268 +       struct dentry *parent;
5269 +       char *name = NULL;
5270 +       int valid = 0;
5271 +
5272 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5273 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5274 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5275 +
5276 +       valid = __unionfs_d_revalidate(dentry, parent, false);
5277 +       if (unlikely(!valid)) {
5278 +               err = -ESTALE;
5279 +               goto out;
5280 +       }
5281 +
5282 +       /*
5283 +        * It's only a bug if this dentry was not negative and couldn't be
5284 +        * revalidated (shouldn't happen).
5285 +        */
5286 +       BUG_ON(!valid && dentry->d_inode);
5287 +
5288 +       lower_dentry = find_writeable_branch(dir, dentry);
5289 +       if (IS_ERR(lower_dentry)) {
5290 +               err = PTR_ERR(lower_dentry);
5291 +               goto out;
5292 +       }
5293 +
5294 +       lower_parent_dentry = lock_parent(lower_dentry);
5295 +       if (IS_ERR(lower_parent_dentry)) {
5296 +               err = PTR_ERR(lower_parent_dentry);
5297 +               goto out_unlock;
5298 +       }
5299 +
5300 +       err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5301 +       if (!err) {
5302 +               err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5303 +               if (!err) {
5304 +                       unionfs_copy_attr_times(dir);
5305 +                       fsstack_copy_inode_size(dir,
5306 +                                               lower_parent_dentry->d_inode);
5307 +                       /* update no. of links on parent directory */
5308 +                       dir->i_nlink = unionfs_get_nlinks(dir);
5309 +               }
5310 +       }
5311 +
5312 +out_unlock:
5313 +       unlock_dir(lower_parent_dentry);
5314 +out:
5315 +       dput(wh_dentry);
5316 +       kfree(name);
5317 +
5318 +       if (!err) {
5319 +               unionfs_postcopyup_setmnt(dentry);
5320 +               unionfs_check_inode(dir);
5321 +               unionfs_check_dentry(dentry);
5322 +       }
5323 +       unionfs_unlock_dentry(dentry);
5324 +       unionfs_unlock_parent(dentry, parent);
5325 +       unionfs_read_unlock(dentry->d_sb);
5326 +       return err;
5327 +}
5328 +
5329 +/* requires sb, dentry, and parent to already be locked */
5330 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5331 +                             int bufsiz)
5332 +{
5333 +       int err;
5334 +       struct dentry *lower_dentry;
5335 +
5336 +       lower_dentry = unionfs_lower_dentry(dentry);
5337 +
5338 +       if (!lower_dentry->d_inode->i_op ||
5339 +           !lower_dentry->d_inode->i_op->readlink) {
5340 +               err = -EINVAL;
5341 +               goto out;
5342 +       }
5343 +
5344 +       err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5345 +                                                   buf, bufsiz);
5346 +       if (err >= 0)
5347 +               fsstack_copy_attr_atime(dentry->d_inode,
5348 +                                       lower_dentry->d_inode);
5349 +
5350 +out:
5351 +       return err;
5352 +}
5353 +
5354 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5355 +                           int bufsiz)
5356 +{
5357 +       int err;
5358 +       struct dentry *parent;
5359 +
5360 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5361 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5362 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5363 +
5364 +       if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5365 +               err = -ESTALE;
5366 +               goto out;
5367 +       }
5368 +
5369 +       err = __unionfs_readlink(dentry, buf, bufsiz);
5370 +
5371 +out:
5372 +       unionfs_check_dentry(dentry);
5373 +       unionfs_unlock_dentry(dentry);
5374 +       unionfs_unlock_parent(dentry, parent);
5375 +       unionfs_read_unlock(dentry->d_sb);
5376 +
5377 +       return err;
5378 +}
5379 +
5380 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5381 +{
5382 +       char *buf;
5383 +       int len = PAGE_SIZE, err;
5384 +       mm_segment_t old_fs;
5385 +       struct dentry *parent;
5386 +
5387 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5388 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5389 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5390 +
5391 +       /* This is freed by the put_link method assuming a successful call. */
5392 +       buf = kmalloc(len, GFP_KERNEL);
5393 +       if (unlikely(!buf)) {
5394 +               err = -ENOMEM;
5395 +               goto out;
5396 +       }
5397 +
5398 +       /* read the symlink, and then we will follow it */
5399 +       old_fs = get_fs();
5400 +       set_fs(KERNEL_DS);
5401 +       err = __unionfs_readlink(dentry, buf, len);
5402 +       set_fs(old_fs);
5403 +       if (err < 0) {
5404 +               kfree(buf);
5405 +               buf = NULL;
5406 +               goto out;
5407 +       }
5408 +       buf[err] = 0;
5409 +       nd_set_link(nd, buf);
5410 +       err = 0;
5411 +
5412 +out:
5413 +       if (err >= 0) {
5414 +               unionfs_check_nd(nd);
5415 +               unionfs_check_dentry(dentry);
5416 +       }
5417 +
5418 +       unionfs_unlock_dentry(dentry);
5419 +       unionfs_unlock_parent(dentry, parent);
5420 +       unionfs_read_unlock(dentry->d_sb);
5421 +
5422 +       return ERR_PTR(err);
5423 +}
5424 +
5425 +/* this @nd *IS* still used */
5426 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5427 +                            void *cookie)
5428 +{
5429 +       struct dentry *parent;
5430 +
5431 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5432 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5433 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5434 +
5435 +       if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5436 +               printk(KERN_ERR
5437 +                      "unionfs: put_link failed to revalidate dentry\n");
5438 +
5439 +       unionfs_check_dentry(dentry);
5440 +       unionfs_check_nd(nd);
5441 +       kfree(nd_get_link(nd));
5442 +       unionfs_unlock_dentry(dentry);
5443 +       unionfs_unlock_parent(dentry, parent);
5444 +       unionfs_read_unlock(dentry->d_sb);
5445 +}
5446 +
5447 +/*
5448 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5449 + * skips over EROFS tests (because we perform copyup on EROFS).
5450 + */
5451 +static int __inode_permission(struct inode *inode, int mask)
5452 +{
5453 +       int retval;
5454 +
5455 +       /* nobody gets write access to an immutable file */
5456 +       if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5457 +               return -EACCES;
5458 +
5459 +       /* Ordinary permission routines do not understand MAY_APPEND. */
5460 +       if (inode->i_op && inode->i_op->permission) {
5461 +               retval = inode->i_op->permission(inode, mask);
5462 +               if (!retval) {
5463 +                       /*
5464 +                        * Exec permission on a regular file is denied if none
5465 +                        * of the execute bits are set.
5466 +                        *
5467 +                        * This check should be done by the ->permission()
5468 +                        * method.
5469 +                        */
5470 +                       if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5471 +                           !(inode->i_mode & S_IXUGO))
5472 +                               return -EACCES;
5473 +               }
5474 +       } else {
5475 +               retval = generic_permission(inode, mask, NULL);
5476 +       }
5477 +       if (retval)
5478 +               return retval;
5479 +
5480 +       return security_inode_permission(inode,
5481 +                       mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5482 +}
5483 +
5484 +/*
5485 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5486 + * a deadlock with the branch-management "add branch" code (which grabbed
5487 + * the write lock).  It is safe to not grab the read lock here, because even
5488 + * with branch management taking place, there is no chance that
5489 + * unionfs_permission, or anything it calls, will use stale branch
5490 + * information.
5491 + */
5492 +static int unionfs_permission(struct inode *inode, int mask)
5493 +{
5494 +       struct inode *lower_inode = NULL;
5495 +       int err = 0;
5496 +       int bindex, bstart, bend;
5497 +       const int is_file = !S_ISDIR(inode->i_mode);
5498 +       const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5499 +       struct inode *inode_grabbed = igrab(inode);
5500 +       struct dentry *dentry = d_find_alias(inode);
5501 +
5502 +       if (dentry)
5503 +               unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5504 +
5505 +       if (!UNIONFS_I(inode)->lower_inodes) {
5506 +               if (is_file)    /* dirs can be unlinked but chdir'ed to */
5507 +                       err = -ESTALE;  /* force revalidate */
5508 +               goto out;
5509 +       }
5510 +       bstart = ibstart(inode);
5511 +       bend = ibend(inode);
5512 +       if (unlikely(bstart < 0 || bend < 0)) {
5513 +               /*
5514 +                * With branch-management, we can get a stale inode here.
5515 +                * If so, we return ESTALE back to link_path_walk, which
5516 +                * would discard the dcache entry and re-lookup the
5517 +                * dentry+inode.  This should be equivalent to issuing
5518 +                * __unionfs_d_revalidate_chain on nd.dentry here.
5519 +                */
5520 +               if (is_file)    /* dirs can be unlinked but chdir'ed to */
5521 +                       err = -ESTALE;  /* force revalidate */
5522 +               goto out;
5523 +       }
5524 +
5525 +       for (bindex = bstart; bindex <= bend; bindex++) {
5526 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
5527 +               if (!lower_inode)
5528 +                       continue;
5529 +
5530 +               /*
5531 +                * check the condition for D-F-D underlying files/directories,
5532 +                * we don't have to check for files, if we are checking for
5533 +                * directories.
5534 +                */
5535 +               if (!is_file && !S_ISDIR(lower_inode->i_mode))
5536 +                       continue;
5537 +
5538 +               /*
5539 +                * We check basic permissions, but we ignore any conditions
5540 +                * such as readonly file systems or branches marked as
5541 +                * readonly, because those conditions should lead to a
5542 +                * copyup taking place later on.  However, if user never had
5543 +                * access to the file, then no copyup could ever take place.
5544 +                */
5545 +               err = __inode_permission(lower_inode, mask);
5546 +               if (err && err != -EACCES && err != EPERM && bindex > 0) {
5547 +                       umode_t mode = lower_inode->i_mode;
5548 +                       if ((is_robranch_super(inode->i_sb, bindex) ||
5549 +                            __is_rdonly(lower_inode)) &&
5550 +                           (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5551 +                               err = 0;
5552 +                       if (IS_COPYUP_ERR(err))
5553 +                               err = 0;
5554 +               }
5555 +
5556 +               /*
5557 +                * NFS HACK: NFSv2/3 return EACCES on readonly-exported,
5558 +                * locally readonly-mounted file systems, instead of EROFS
5559 +                * like other file systems do.  So we have no choice here
5560 +                * but to intercept this and ignore it for NFS branches
5561 +                * marked readonly.  Specifically, we avoid using NFS's own
5562 +                * "broken" ->permission method, and rely on
5563 +                * generic_permission() to do basic checking for us.
5564 +                */
5565 +               if (err && err == -EACCES &&
5566 +                   is_robranch_super(inode->i_sb, bindex) &&
5567 +                   lower_inode->i_sb->s_magic == NFS_SUPER_MAGIC)
5568 +                       err = generic_permission(lower_inode, mask, NULL);
5569 +
5570 +               /*
5571 +                * The permissions are an intersection of the overall directory
5572 +                * permissions, so we fail if one fails.
5573 +                */
5574 +               if (err)
5575 +                       goto out;
5576 +
5577 +               /* only the leftmost file matters. */
5578 +               if (is_file || write_mask) {
5579 +                       if (is_file && write_mask) {
5580 +                               err = get_write_access(lower_inode);
5581 +                               if (!err)
5582 +                                       put_write_access(lower_inode);
5583 +                       }
5584 +                       break;
5585 +               }
5586 +       }
5587 +       /* sync times which may have changed (asynchronously) below */
5588 +       unionfs_copy_attr_times(inode);
5589 +
5590 +out:
5591 +       unionfs_check_inode(inode);
5592 +       if (dentry) {
5593 +               unionfs_unlock_dentry(dentry);
5594 +               dput(dentry);
5595 +       }
5596 +       iput(inode_grabbed);
5597 +       return err;
5598 +}
5599 +
5600 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5601 +{
5602 +       int err = 0;
5603 +       struct dentry *lower_dentry;
5604 +       struct dentry *parent;
5605 +       struct inode *inode;
5606 +       struct inode *lower_inode;
5607 +       int bstart, bend, bindex;
5608 +       loff_t size;
5609 +
5610 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5611 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5612 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5613 +
5614 +       if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5615 +               err = -ESTALE;
5616 +               goto out;
5617 +       }
5618 +
5619 +       bstart = dbstart(dentry);
5620 +       bend = dbend(dentry);
5621 +       inode = dentry->d_inode;
5622 +
5623 +       /*
5624 +        * mode change is for clearing setuid/setgid. Allow lower filesystem
5625 +        * to reinterpret it in its own way.
5626 +        */
5627 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5628 +               ia->ia_valid &= ~ATTR_MODE;
5629 +
5630 +       lower_dentry = unionfs_lower_dentry(dentry);
5631 +       if (!lower_dentry) { /* should never happen after above revalidate */
5632 +               err = -EINVAL;
5633 +               goto out;
5634 +       }
5635 +       lower_inode = unionfs_lower_inode(inode);
5636 +
5637 +       /* check if user has permission to change lower inode */
5638 +       err = inode_change_ok(lower_inode, ia);
5639 +       if (err)
5640 +               goto out;
5641 +
5642 +       /* copyup if the file is on a read only branch */
5643 +       if (is_robranch_super(dentry->d_sb, bstart)
5644 +           || __is_rdonly(lower_inode)) {
5645 +               /* check if we have a branch to copy up to */
5646 +               if (bstart <= 0) {
5647 +                       err = -EACCES;
5648 +                       goto out;
5649 +               }
5650 +
5651 +               if (ia->ia_valid & ATTR_SIZE)
5652 +                       size = ia->ia_size;
5653 +               else
5654 +                       size = i_size_read(inode);
5655 +               /* copyup to next available branch */
5656 +               for (bindex = bstart - 1; bindex >= 0; bindex--) {
5657 +                       err = copyup_dentry(parent->d_inode,
5658 +                                           dentry, bstart, bindex,
5659 +                                           dentry->d_name.name,
5660 +                                           dentry->d_name.len,
5661 +                                           NULL, size);
5662 +                       if (!err)
5663 +                               break;
5664 +               }
5665 +               if (err)
5666 +                       goto out;
5667 +               /* get updated lower_dentry/inode after copyup */
5668 +               lower_dentry = unionfs_lower_dentry(dentry);
5669 +               lower_inode = unionfs_lower_inode(inode);
5670 +       }
5671 +
5672 +       /*
5673 +        * If shrinking, first truncate upper level to cancel writing dirty
5674 +        * pages beyond the new eof; and also if its' maxbytes is more
5675 +        * limiting (fail with -EFBIG before making any change to the lower
5676 +        * level).  There is no need to vmtruncate the upper level
5677 +        * afterwards in the other cases: we fsstack_copy_inode_size from
5678 +        * the lower level.
5679 +        */
5680 +       if (ia->ia_valid & ATTR_SIZE) {
5681 +               size = i_size_read(inode);
5682 +               if (ia->ia_size < size || (ia->ia_size > size &&
5683 +                   inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5684 +                       err = vmtruncate(inode, ia->ia_size);
5685 +                       if (err)
5686 +                               goto out;
5687 +               }
5688 +       }
5689 +
5690 +       /* notify the (possibly copied-up) lower inode */
5691 +       /*
5692 +        * Note: we use lower_dentry->d_inode, because lower_inode may be
5693 +        * unlinked (no inode->i_sb and i_ino==0.  This happens if someone
5694 +        * tries to open(), unlink(), then ftruncate() a file.
5695 +        */
5696 +       mutex_lock(&lower_dentry->d_inode->i_mutex);
5697 +       err = notify_change(lower_dentry, ia);
5698 +       mutex_unlock(&lower_dentry->d_inode->i_mutex);
5699 +       if (err)
5700 +               goto out;
5701 +
5702 +       /* get attributes from the first lower inode */
5703 +       if (ibstart(inode) >= 0)
5704 +               unionfs_copy_attr_all(inode, lower_inode);
5705 +       /*
5706 +        * unionfs_copy_attr_all will copy the lower times to our inode if
5707 +        * the lower ones are newer (useful for cache coherency).  However,
5708 +        * ->setattr is the only place in which we may have to copy the
5709 +        * lower inode times absolutely, to support utimes(2).
5710 +        */
5711 +       if (ia->ia_valid & ATTR_MTIME_SET)
5712 +               inode->i_mtime = lower_inode->i_mtime;
5713 +       if (ia->ia_valid & ATTR_CTIME)
5714 +               inode->i_ctime = lower_inode->i_ctime;
5715 +       if (ia->ia_valid & ATTR_ATIME_SET)
5716 +               inode->i_atime = lower_inode->i_atime;
5717 +       fsstack_copy_inode_size(inode, lower_inode);
5718 +
5719 +out:
5720 +       if (!err)
5721 +               unionfs_check_dentry(dentry);
5722 +       unionfs_unlock_dentry(dentry);
5723 +       unionfs_unlock_parent(dentry, parent);
5724 +       unionfs_read_unlock(dentry->d_sb);
5725 +
5726 +       return err;
5727 +}
5728 +
5729 +struct inode_operations unionfs_symlink_iops = {
5730 +       .readlink       = unionfs_readlink,
5731 +       .permission     = unionfs_permission,
5732 +       .follow_link    = unionfs_follow_link,
5733 +       .setattr        = unionfs_setattr,
5734 +       .put_link       = unionfs_put_link,
5735 +};
5736 +
5737 +struct inode_operations unionfs_dir_iops = {
5738 +       .create         = unionfs_create,
5739 +       .lookup         = unionfs_lookup,
5740 +       .link           = unionfs_link,
5741 +       .unlink         = unionfs_unlink,
5742 +       .symlink        = unionfs_symlink,
5743 +       .mkdir          = unionfs_mkdir,
5744 +       .rmdir          = unionfs_rmdir,
5745 +       .mknod          = unionfs_mknod,
5746 +       .rename         = unionfs_rename,
5747 +       .permission     = unionfs_permission,
5748 +       .setattr        = unionfs_setattr,
5749 +#ifdef CONFIG_UNION_FS_XATTR
5750 +       .setxattr       = unionfs_setxattr,
5751 +       .getxattr       = unionfs_getxattr,
5752 +       .removexattr    = unionfs_removexattr,
5753 +       .listxattr      = unionfs_listxattr,
5754 +#endif /* CONFIG_UNION_FS_XATTR */
5755 +};
5756 +
5757 +struct inode_operations unionfs_main_iops = {
5758 +       .permission     = unionfs_permission,
5759 +       .setattr        = unionfs_setattr,
5760 +#ifdef CONFIG_UNION_FS_XATTR
5761 +       .setxattr       = unionfs_setxattr,
5762 +       .getxattr       = unionfs_getxattr,
5763 +       .removexattr    = unionfs_removexattr,
5764 +       .listxattr      = unionfs_listxattr,
5765 +#endif /* CONFIG_UNION_FS_XATTR */
5766 +};
5767 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/Kconfig linux-2.6.35-rc6-git-unionfs//fs/unionfs/Kconfig
5768 --- linux-2.6.35-rc6-git//fs/unionfs/Kconfig    1970-01-01 02:00:00.000000000 +0200
5769 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/Kconfig    2010-07-30 15:50:50.557108046 +0300
5770 @@ -0,0 +1,24 @@
5771 +config UNION_FS
5772 +       tristate "Union file system (EXPERIMENTAL)"
5773 +       depends on EXPERIMENTAL
5774 +       help
5775 +         Unionfs is a stackable unification file system, which appears to
5776 +         merge the contents of several directories (branches), while keeping
5777 +         their physical content separate.
5778 +
5779 +         See <http://unionfs.filesystems.org> for details
5780 +
5781 +config UNION_FS_XATTR
5782 +       bool "Unionfs extended attributes"
5783 +       depends on UNION_FS
5784 +       help
5785 +         Extended attributes are name:value pairs associated with inodes by
5786 +         the kernel or by users (see the attr(5) manual page).
5787 +
5788 +         If unsure, say N.
5789 +
5790 +config UNION_FS_DEBUG
5791 +       bool "Debug Unionfs"
5792 +       depends on UNION_FS
5793 +       help
5794 +         If you say Y here, you can turn on debugging output from Unionfs.
5795 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/lookup.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/lookup.c
5796 --- linux-2.6.35-rc6-git//fs/unionfs/lookup.c   1970-01-01 02:00:00.000000000 +0200
5797 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/lookup.c   2010-07-30 15:50:50.560010144 +0300
5798 @@ -0,0 +1,569 @@
5799 +/*
5800 + * Copyright (c) 2003-2010 Erez Zadok
5801 + * Copyright (c) 2003-2006 Charles P. Wright
5802 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5803 + * Copyright (c) 2005-2006 Junjiro Okajima
5804 + * Copyright (c) 2005      Arun M. Krishnakumar
5805 + * Copyright (c) 2004-2006 David P. Quigley
5806 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5807 + * Copyright (c) 2003      Puja Gupta
5808 + * Copyright (c) 2003      Harikesavan Krishnan
5809 + * Copyright (c) 2003-2010 Stony Brook University
5810 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
5811 + *
5812 + * This program is free software; you can redistribute it and/or modify
5813 + * it under the terms of the GNU General Public License version 2 as
5814 + * published by the Free Software Foundation.
5815 + */
5816 +
5817 +#include "union.h"
5818 +
5819 +/*
5820 + * Lookup one path component @name relative to a <base,mnt> path pair.
5821 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5822 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5823 + * other lower mounts properly.  If @new_mnt is non-null, will fill in the
5824 + * new mnt there.  Caller is responsible to dput/mntput/path_put returned
5825 + * @dentry and @new_mnt.
5826 + */
5827 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5828 +                           const char *name, struct vfsmount **new_mnt)
5829 +{
5830 +       struct dentry *dentry = NULL;
5831 +       struct nameidata lower_nd;
5832 +       int err;
5833 +
5834 +       /* we use flags=0 to get basic lookup */
5835 +       err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
5836 +
5837 +       switch (err) {
5838 +       case 0: /* no error */
5839 +               dentry = lower_nd.path.dentry;
5840 +               if (new_mnt)
5841 +                       *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
5842 +               break;
5843 +       case -ENOENT:
5844 +                /*
5845 +                 * We don't consider ENOENT an error, and we want to return
5846 +                 * a negative dentry (ala lookup_one_len).  As we know
5847 +                 * there was no inode for this name before (-ENOENT), then
5848 +                 * it's safe to call lookup_one_len (which doesn't take a
5849 +                 * vfsmount).
5850 +                 */
5851 +               dentry = lookup_lck_len(name, base, strlen(name));
5852 +               if (new_mnt)
5853 +                       *new_mnt = mntget(lower_nd.path.mnt);
5854 +               break;
5855 +       default: /* all other real errors */
5856 +               dentry = ERR_PTR(err);
5857 +               break;
5858 +       }
5859 +
5860 +       return dentry;
5861 +}
5862 +
5863 +/*
5864 + * This is a utility function that fills in a unionfs dentry.
5865 + * Caller must lock this dentry with unionfs_lock_dentry.
5866 + *
5867 + * Returns: 0 (ok), or -ERRNO if an error occurred.
5868 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
5869 + */
5870 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
5871 +{
5872 +       struct dentry *tmp;
5873 +       int err = -ENOSYS;
5874 +
5875 +       tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
5876 +
5877 +       if (!tmp) {
5878 +               err = 0;
5879 +               goto out;
5880 +       }
5881 +       if (IS_ERR(tmp)) {
5882 +               err = PTR_ERR(tmp);
5883 +               goto out;
5884 +       }
5885 +       /* XXX: need to change the interface */
5886 +       BUG_ON(tmp != dentry);
5887 +out:
5888 +       return err;
5889 +}
5890 +
5891 +/* The dentry cache is just so we have properly sized dentries. */
5892 +static struct kmem_cache *unionfs_dentry_cachep;
5893 +int unionfs_init_dentry_cache(void)
5894 +{
5895 +       unionfs_dentry_cachep =
5896 +               kmem_cache_create("unionfs_dentry",
5897 +                                 sizeof(struct unionfs_dentry_info),
5898 +                                 0, SLAB_RECLAIM_ACCOUNT, NULL);
5899 +
5900 +       return (unionfs_dentry_cachep ? 0 : -ENOMEM);
5901 +}
5902 +
5903 +void unionfs_destroy_dentry_cache(void)
5904 +{
5905 +       if (unionfs_dentry_cachep)
5906 +               kmem_cache_destroy(unionfs_dentry_cachep);
5907 +}
5908 +
5909 +void free_dentry_private_data(struct dentry *dentry)
5910 +{
5911 +       if (!dentry || !dentry->d_fsdata)
5912 +               return;
5913 +       kfree(UNIONFS_D(dentry)->lower_paths);
5914 +       UNIONFS_D(dentry)->lower_paths = NULL;
5915 +       kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
5916 +       dentry->d_fsdata = NULL;
5917 +}
5918 +
5919 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
5920 +{
5921 +       struct unionfs_dentry_info *info = UNIONFS_D(dentry);
5922 +       void *p;
5923 +       int size;
5924 +
5925 +       BUG_ON(!info);
5926 +
5927 +       size = sizeof(struct path) * sbmax(dentry->d_sb);
5928 +       p = krealloc(info->lower_paths, size, GFP_ATOMIC);
5929 +       if (unlikely(!p))
5930 +               return -ENOMEM;
5931 +
5932 +       info->lower_paths = p;
5933 +
5934 +       info->bstart = -1;
5935 +       info->bend = -1;
5936 +       info->bopaque = -1;
5937 +       info->bcount = sbmax(dentry->d_sb);
5938 +       atomic_set(&info->generation,
5939 +                       atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
5940 +
5941 +       memset(info->lower_paths, 0, size);
5942 +
5943 +       return 0;
5944 +}
5945 +
5946 +/* UNIONFS_D(dentry)->lock must be locked */
5947 +int realloc_dentry_private_data(struct dentry *dentry)
5948 +{
5949 +       if (!__realloc_dentry_private_data(dentry))
5950 +               return 0;
5951 +
5952 +       kfree(UNIONFS_D(dentry)->lower_paths);
5953 +       free_dentry_private_data(dentry);
5954 +       return -ENOMEM;
5955 +}
5956 +
5957 +/* allocate new dentry private data */
5958 +int new_dentry_private_data(struct dentry *dentry, int subclass)
5959 +{
5960 +       struct unionfs_dentry_info *info = UNIONFS_D(dentry);
5961 +
5962 +       BUG_ON(info);
5963 +
5964 +       info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
5965 +       if (unlikely(!info))
5966 +               return -ENOMEM;
5967 +
5968 +       mutex_init(&info->lock);
5969 +       mutex_lock_nested(&info->lock, subclass);
5970 +
5971 +       info->lower_paths = NULL;
5972 +
5973 +       dentry->d_fsdata = info;
5974 +
5975 +       if (!__realloc_dentry_private_data(dentry))
5976 +               return 0;
5977 +
5978 +       mutex_unlock(&info->lock);
5979 +       free_dentry_private_data(dentry);
5980 +       return -ENOMEM;
5981 +}
5982 +
5983 +/*
5984 + * scan through the lower dentry objects, and set bstart to reflect the
5985 + * starting branch
5986 + */
5987 +void update_bstart(struct dentry *dentry)
5988 +{
5989 +       int bindex;
5990 +       int bstart = dbstart(dentry);
5991 +       int bend = dbend(dentry);
5992 +       struct dentry *lower_dentry;
5993 +
5994 +       for (bindex = bstart; bindex <= bend; bindex++) {
5995 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5996 +               if (!lower_dentry)
5997 +                       continue;
5998 +               if (lower_dentry->d_inode) {
5999 +                       dbstart(dentry) = bindex;
6000 +                       break;
6001 +               }
6002 +               dput(lower_dentry);
6003 +               unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6004 +       }
6005 +}
6006 +
6007 +
6008 +/*
6009 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6010 + * file system.  Returns 0 on success or -error (only -ENOMEM possible).
6011 + * Inside that nd structure, this function may also return an allocated
6012 + * struct file (for open intents).  The caller, when done with this nd, must
6013 + * kfree the intent file (using release_lower_nd).
6014 + *
6015 + * XXX: this code, and the callers of this code, should be redone using
6016 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6017 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6018 + * function and a method that other file systems can call.
6019 + */
6020 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6021 +{
6022 +       int err = 0;
6023 +#ifdef ALLOC_LOWER_ND_FILE
6024 +       /*
6025 +        * XXX: one day we may need to have the lower return an open file
6026 +        * for us.  It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6027 +        * very well be needed for nfs4.
6028 +        */
6029 +       struct file *file;
6030 +#endif /* ALLOC_LOWER_ND_FILE */
6031 +
6032 +       memset(nd, 0, sizeof(struct nameidata));
6033 +       if (!flags)
6034 +               return err;
6035 +
6036 +       switch (flags) {
6037 +       case LOOKUP_CREATE:
6038 +               nd->intent.open.flags |= O_CREAT;
6039 +               /* fall through: shared code for create/open cases */
6040 +       case LOOKUP_OPEN:
6041 +               nd->flags = flags;
6042 +               nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6043 +#ifdef ALLOC_LOWER_ND_FILE
6044 +               file = kzalloc(sizeof(struct file), GFP_KERNEL);
6045 +               if (unlikely(!file)) {
6046 +                       err = -ENOMEM;
6047 +                       break; /* exit switch statement and thus return */
6048 +               }
6049 +               nd->intent.open.file = file;
6050 +#endif /* ALLOC_LOWER_ND_FILE */
6051 +               break;
6052 +       default:
6053 +               /*
6054 +                * We should never get here, for now.
6055 +                * We can add new cases here later on.
6056 +                */
6057 +               pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6058 +               BUG();
6059 +               break;
6060 +       }
6061 +
6062 +       return err;
6063 +}
6064 +
6065 +void release_lower_nd(struct nameidata *nd, int err)
6066 +{
6067 +       if (!nd->intent.open.file)
6068 +               return;
6069 +       else if (!err)
6070 +               release_open_intent(nd);
6071 +#ifdef ALLOC_LOWER_ND_FILE
6072 +       kfree(nd->intent.open.file);
6073 +#endif /* ALLOC_LOWER_ND_FILE */
6074 +}
6075 +
6076 +/*
6077 + * Main (and complex) driver function for Unionfs's lookup
6078 + *
6079 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6080 + * PTR if d_splice returned a different dentry.
6081 + *
6082 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6083 + * inode info must be locked.  If lookupmode is INTERPOSE_LOOKUP (i.e., a
6084 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6085 + * dentry's info, which the caller must unlock.
6086 + */
6087 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6088 +                                  struct dentry *parent, int lookupmode)
6089 +{
6090 +       int err = 0;
6091 +       struct dentry *lower_dentry = NULL;
6092 +       struct vfsmount *lower_mnt;
6093 +       struct vfsmount *lower_dir_mnt;
6094 +       struct dentry *wh_lower_dentry = NULL;
6095 +       struct dentry *lower_dir_dentry = NULL;
6096 +       struct dentry *d_interposed = NULL;
6097 +       int bindex, bstart, bend, bopaque;
6098 +       int opaque, num_positive = 0;
6099 +       const char *name;
6100 +       int namelen;
6101 +       int pos_start, pos_end;
6102 +
6103 +       /*
6104 +        * We should already have a lock on this dentry in the case of a
6105 +        * partial lookup, or a revalidation.  Otherwise it is returned from
6106 +        * new_dentry_private_data already locked.
6107 +        */
6108 +       verify_locked(dentry);
6109 +       verify_locked(parent);
6110 +
6111 +       /* must initialize dentry operations */
6112 +       dentry->d_op = &unionfs_dops;
6113 +
6114 +       /* We never partial lookup the root directory. */
6115 +       if (IS_ROOT(dentry))
6116 +               goto out;
6117 +
6118 +       name = dentry->d_name.name;
6119 +       namelen = dentry->d_name.len;
6120 +
6121 +       /* No dentries should get created for possible whiteout names. */
6122 +       if (!is_validname(name)) {
6123 +               err = -EPERM;
6124 +               goto out_free;
6125 +       }
6126 +
6127 +       /* Now start the actual lookup procedure. */
6128 +       bstart = dbstart(parent);
6129 +       bend = dbend(parent);
6130 +       bopaque = dbopaque(parent);
6131 +       BUG_ON(bstart < 0);
6132 +
6133 +       /* adjust bend to bopaque if needed */
6134 +       if ((bopaque >= 0) && (bopaque < bend))
6135 +               bend = bopaque;
6136 +
6137 +       /* lookup all possible dentries */
6138 +       for (bindex = bstart; bindex <= bend; bindex++) {
6139 +
6140 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6141 +               lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6142 +
6143 +               /* skip if we already have a positive lower dentry */
6144 +               if (lower_dentry) {
6145 +                       if (dbstart(dentry) < 0)
6146 +                               dbstart(dentry) = bindex;
6147 +                       if (bindex > dbend(dentry))
6148 +                               dbend(dentry) = bindex;
6149 +                       if (lower_dentry->d_inode)
6150 +                               num_positive++;
6151 +                       continue;
6152 +               }
6153 +
6154 +               lower_dir_dentry =
6155 +                       unionfs_lower_dentry_idx(parent, bindex);
6156 +               /* if the lower dentry's parent does not exist, skip this */
6157 +               if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6158 +                       continue;
6159 +
6160 +               /* also skip it if the parent isn't a directory. */
6161 +               if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6162 +                       continue; /* XXX: should be BUG_ON */
6163 +
6164 +               /* check for whiteouts: stop lookup if found */
6165 +               wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6166 +               if (IS_ERR(wh_lower_dentry)) {
6167 +                       err = PTR_ERR(wh_lower_dentry);
6168 +                       goto out_free;
6169 +               }
6170 +               if (wh_lower_dentry->d_inode) {
6171 +                       dbend(dentry) = dbopaque(dentry) = bindex;
6172 +                       if (dbstart(dentry) < 0)
6173 +                               dbstart(dentry) = bindex;
6174 +                       dput(wh_lower_dentry);
6175 +                       break;
6176 +               }
6177 +               dput(wh_lower_dentry);
6178 +
6179 +               /* Now do regular lookup; lookup @name */
6180 +               lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6181 +               lower_mnt = NULL; /* XXX: needed? */
6182 +
6183 +               lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6184 +                                           name, &lower_mnt);
6185 +
6186 +               if (IS_ERR(lower_dentry)) {
6187 +                       err = PTR_ERR(lower_dentry);
6188 +                       goto out_free;
6189 +               }
6190 +               unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6191 +               if (!lower_mnt)
6192 +                       lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6193 +                                                  bindex);
6194 +               unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6195 +
6196 +               /* adjust dbstart/end */
6197 +               if (dbstart(dentry) < 0)
6198 +                       dbstart(dentry) = bindex;
6199 +               if (bindex > dbend(dentry))
6200 +                       dbend(dentry) = bindex;
6201 +               /*
6202 +                * We always store the lower dentries above, and update
6203 +                * dbstart/dbend, even if the whole unionfs dentry is
6204 +                * negative (i.e., no lower inodes).
6205 +                */
6206 +               if (!lower_dentry->d_inode)
6207 +                       continue;
6208 +               num_positive++;
6209 +
6210 +               /*
6211 +                * check if we just found an opaque directory, if so, stop
6212 +                * lookups here.
6213 +                */
6214 +               if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6215 +                       continue;
6216 +               opaque = is_opaque_dir(dentry, bindex);
6217 +               if (opaque < 0) {
6218 +                       err = opaque;
6219 +                       goto out_free;
6220 +               } else if (opaque) {
6221 +                       dbend(dentry) = dbopaque(dentry) = bindex;
6222 +                       break;
6223 +               }
6224 +               dbend(dentry) = bindex;
6225 +
6226 +               /* update parent directory's atime with the bindex */
6227 +               fsstack_copy_attr_atime(parent->d_inode,
6228 +                                       lower_dir_dentry->d_inode);
6229 +       }
6230 +
6231 +       /* sanity checks, then decide if to process a negative dentry */
6232 +       BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6233 +       BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6234 +
6235 +       if (num_positive > 0)
6236 +               goto out_positive;
6237 +
6238 +       /*** handle NEGATIVE dentries ***/
6239 +
6240 +       /*
6241 +        * If negative, keep only first lower negative dentry, to save on
6242 +        * memory.
6243 +        */
6244 +       if (dbstart(dentry) < dbend(dentry)) {
6245 +               path_put_lowers(dentry, dbstart(dentry) + 1,
6246 +                               dbend(dentry), false);
6247 +               dbend(dentry) = dbstart(dentry);
6248 +       }
6249 +       if (lookupmode == INTERPOSE_PARTIAL)
6250 +               goto out;
6251 +       if (lookupmode == INTERPOSE_LOOKUP) {
6252 +               /*
6253 +                * If all we found was a whiteout in the first available
6254 +                * branch, then create a negative dentry for a possibly new
6255 +                * file to be created.
6256 +                */
6257 +               if (dbopaque(dentry) < 0)
6258 +                       goto out;
6259 +               /* XXX: need to get mnt here */
6260 +               bindex = dbstart(dentry);
6261 +               if (unionfs_lower_dentry_idx(dentry, bindex))
6262 +                       goto out;
6263 +               lower_dir_dentry =
6264 +                       unionfs_lower_dentry_idx(parent, bindex);
6265 +               if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6266 +                       goto out;
6267 +               if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6268 +                       goto out; /* XXX: should be BUG_ON */
6269 +               /* XXX: do we need to cross bind mounts here? */
6270 +               lower_dentry = lookup_lck_len(name, lower_dir_dentry, namelen);
6271 +               if (IS_ERR(lower_dentry)) {
6272 +                       err = PTR_ERR(lower_dentry);
6273 +                       goto out;
6274 +               }
6275 +               /* XXX: need to mntget/mntput as needed too! */
6276 +               unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6277 +               /* XXX: wrong mnt for crossing bind mounts! */
6278 +               lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6279 +               unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6280 +
6281 +               goto out;
6282 +       }
6283 +
6284 +       /* if we're revalidating a positive dentry, don't make it negative */
6285 +       if (lookupmode != INTERPOSE_REVAL)
6286 +               d_add(dentry, NULL);
6287 +
6288 +       goto out;
6289 +
6290 +out_positive:
6291 +       /*** handle POSITIVE dentries ***/
6292 +
6293 +       /*
6294 +        * This unionfs dentry is positive (at least one lower inode
6295 +        * exists), so scan entire dentry from beginning to end, and remove
6296 +        * any negative lower dentries, if any.  Then, update dbstart/dbend
6297 +        * to reflect the start/end of positive dentries.
6298 +        */
6299 +       pos_start = pos_end = -1;
6300 +       for (bindex = bstart; bindex <= bend; bindex++) {
6301 +               lower_dentry = unionfs_lower_dentry_idx(dentry,
6302 +                                                       bindex);
6303 +               if (lower_dentry && lower_dentry->d_inode) {
6304 +                       if (pos_start < 0)
6305 +                               pos_start = bindex;
6306 +                       if (bindex > pos_end)
6307 +                               pos_end = bindex;
6308 +                       continue;
6309 +               }
6310 +               path_put_lowers(dentry, bindex, bindex, false);
6311 +       }
6312 +       if (pos_start >= 0)
6313 +               dbstart(dentry) = pos_start;
6314 +       if (pos_end >= 0)
6315 +               dbend(dentry) = pos_end;
6316 +
6317 +       /* Partial lookups need to re-interpose, or throw away older negs. */
6318 +       if (lookupmode == INTERPOSE_PARTIAL) {
6319 +               if (dentry->d_inode) {
6320 +                       unionfs_reinterpose(dentry);
6321 +                       goto out;
6322 +               }
6323 +
6324 +               /*
6325 +                * This dentry was positive, so it is as if we had a
6326 +                * negative revalidation.
6327 +                */
6328 +               lookupmode = INTERPOSE_REVAL_NEG;
6329 +               update_bstart(dentry);
6330 +       }
6331 +
6332 +       /*
6333 +        * Interpose can return a dentry if d_splice returned a different
6334 +        * dentry.
6335 +        */
6336 +       d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6337 +       if (IS_ERR(d_interposed))
6338 +               err = PTR_ERR(d_interposed);
6339 +       else if (d_interposed)
6340 +               dentry = d_interposed;
6341 +
6342 +       if (!err)
6343 +               goto out;
6344 +       d_drop(dentry);
6345 +
6346 +out_free:
6347 +       /* should dput/mntput all the underlying dentries on error condition */
6348 +       if (dbstart(dentry) >= 0)
6349 +               path_put_lowers_all(dentry, false);
6350 +       /* free lower_paths unconditionally */
6351 +       kfree(UNIONFS_D(dentry)->lower_paths);
6352 +       UNIONFS_D(dentry)->lower_paths = NULL;
6353 +
6354 +out:
6355 +       if (dentry && UNIONFS_D(dentry)) {
6356 +               BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6357 +               BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6358 +       }
6359 +       if (d_interposed && UNIONFS_D(d_interposed)) {
6360 +               BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6361 +               BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6362 +       }
6363 +
6364 +       if (!err && d_interposed)
6365 +               return d_interposed;
6366 +       return ERR_PTR(err);
6367 +}
6368 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/main.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/main.c
6369 --- linux-2.6.35-rc6-git//fs/unionfs/main.c     1970-01-01 02:00:00.000000000 +0200
6370 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/main.c     2010-07-30 15:50:50.561010133 +0300
6371 @@ -0,0 +1,758 @@
6372 +/*
6373 + * Copyright (c) 2003-2010 Erez Zadok
6374 + * Copyright (c) 2003-2006 Charles P. Wright
6375 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6376 + * Copyright (c) 2005-2006 Junjiro Okajima
6377 + * Copyright (c) 2005      Arun M. Krishnakumar
6378 + * Copyright (c) 2004-2006 David P. Quigley
6379 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6380 + * Copyright (c) 2003      Puja Gupta
6381 + * Copyright (c) 2003      Harikesavan Krishnan
6382 + * Copyright (c) 2003-2010 Stony Brook University
6383 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
6384 + *
6385 + * This program is free software; you can redistribute it and/or modify
6386 + * it under the terms of the GNU General Public License version 2 as
6387 + * published by the Free Software Foundation.
6388 + */
6389 +
6390 +#include "union.h"
6391 +#include <linux/module.h>
6392 +#include <linux/moduleparam.h>
6393 +
6394 +static void unionfs_fill_inode(struct dentry *dentry,
6395 +                              struct inode *inode)
6396 +{
6397 +       struct inode *lower_inode;
6398 +       struct dentry *lower_dentry;
6399 +       int bindex, bstart, bend;
6400 +
6401 +       bstart = dbstart(dentry);
6402 +       bend = dbend(dentry);
6403 +
6404 +       for (bindex = bstart; bindex <= bend; bindex++) {
6405 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6406 +               if (!lower_dentry) {
6407 +                       unionfs_set_lower_inode_idx(inode, bindex, NULL);
6408 +                       continue;
6409 +               }
6410 +
6411 +               /* Initialize the lower inode to the new lower inode. */
6412 +               if (!lower_dentry->d_inode)
6413 +                       continue;
6414 +
6415 +               unionfs_set_lower_inode_idx(inode, bindex,
6416 +                                           igrab(lower_dentry->d_inode));
6417 +       }
6418 +
6419 +       ibstart(inode) = dbstart(dentry);
6420 +       ibend(inode) = dbend(dentry);
6421 +
6422 +       /* Use attributes from the first branch. */
6423 +       lower_inode = unionfs_lower_inode(inode);
6424 +
6425 +       /* Use different set of inode ops for symlinks & directories */
6426 +       if (S_ISLNK(lower_inode->i_mode))
6427 +               inode->i_op = &unionfs_symlink_iops;
6428 +       else if (S_ISDIR(lower_inode->i_mode))
6429 +               inode->i_op = &unionfs_dir_iops;
6430 +
6431 +       /* Use different set of file ops for directories */
6432 +       if (S_ISDIR(lower_inode->i_mode))
6433 +               inode->i_fop = &unionfs_dir_fops;
6434 +
6435 +       /* properly initialize special inodes */
6436 +       if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6437 +           S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6438 +               init_special_inode(inode, lower_inode->i_mode,
6439 +                                  lower_inode->i_rdev);
6440 +
6441 +       /* all well, copy inode attributes */
6442 +       unionfs_copy_attr_all(inode, lower_inode);
6443 +       fsstack_copy_inode_size(inode, lower_inode);
6444 +}
6445 +
6446 +/*
6447 + * Connect a unionfs inode dentry/inode with several lower ones.  This is
6448 + * the classic stackable file system "vnode interposition" action.
6449 + *
6450 + * @sb: unionfs's super_block
6451 + */
6452 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6453 +                                int flag)
6454 +{
6455 +       int err = 0;
6456 +       struct inode *inode;
6457 +       int need_fill_inode = 1;
6458 +       struct dentry *spliced = NULL;
6459 +
6460 +       verify_locked(dentry);
6461 +
6462 +       /*
6463 +        * We allocate our new inode below by calling unionfs_iget,
6464 +        * which will initialize some of the new inode's fields
6465 +        */
6466 +
6467 +       /*
6468 +        * On revalidate we've already got our own inode and just need
6469 +        * to fix it up.
6470 +        */
6471 +       if (flag == INTERPOSE_REVAL) {
6472 +               inode = dentry->d_inode;
6473 +               UNIONFS_I(inode)->bstart = -1;
6474 +               UNIONFS_I(inode)->bend = -1;
6475 +               atomic_set(&UNIONFS_I(inode)->generation,
6476 +                          atomic_read(&UNIONFS_SB(sb)->generation));
6477 +
6478 +               UNIONFS_I(inode)->lower_inodes =
6479 +                       kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6480 +               if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6481 +                       err = -ENOMEM;
6482 +                       goto out;
6483 +               }
6484 +       } else {
6485 +               /* get unique inode number for unionfs */
6486 +               inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6487 +               if (IS_ERR(inode)) {
6488 +                       err = PTR_ERR(inode);
6489 +                       goto out;
6490 +               }
6491 +               if (atomic_read(&inode->i_count) > 1)
6492 +                       goto skip;
6493 +       }
6494 +
6495 +       need_fill_inode = 0;
6496 +       unionfs_fill_inode(dentry, inode);
6497 +
6498 +skip:
6499 +       /* only (our) lookup wants to do a d_add */
6500 +       switch (flag) {
6501 +       case INTERPOSE_DEFAULT:
6502 +               /* for operations which create new inodes */
6503 +               d_add(dentry, inode);
6504 +               break;
6505 +       case INTERPOSE_REVAL_NEG:
6506 +               d_instantiate(dentry, inode);
6507 +               break;
6508 +       case INTERPOSE_LOOKUP:
6509 +               spliced = d_splice_alias(inode, dentry);
6510 +               if (spliced && spliced != dentry) {
6511 +                       /*
6512 +                        * d_splice can return a dentry if it was
6513 +                        * disconnected and had to be moved.  We must ensure
6514 +                        * that the private data of the new dentry is
6515 +                        * correct and that the inode info was filled
6516 +                        * properly.  Finally we must return this new
6517 +                        * dentry.
6518 +                        */
6519 +                       spliced->d_op = &unionfs_dops;
6520 +                       spliced->d_fsdata = dentry->d_fsdata;
6521 +                       dentry->d_fsdata = NULL;
6522 +                       dentry = spliced;
6523 +                       if (need_fill_inode) {
6524 +                               need_fill_inode = 0;
6525 +                               unionfs_fill_inode(dentry, inode);
6526 +                       }
6527 +                       goto out_spliced;
6528 +               } else if (!spliced) {
6529 +                       if (need_fill_inode) {
6530 +                               need_fill_inode = 0;
6531 +                               unionfs_fill_inode(dentry, inode);
6532 +                               goto out_spliced;
6533 +                       }
6534 +               }
6535 +               break;
6536 +       case INTERPOSE_REVAL:
6537 +               /* Do nothing. */
6538 +               break;
6539 +       default:
6540 +               printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6541 +               BUG();
6542 +       }
6543 +       goto out;
6544 +
6545 +out_spliced:
6546 +       if (!err)
6547 +               return spliced;
6548 +out:
6549 +       return ERR_PTR(err);
6550 +}
6551 +
6552 +/* like interpose above, but for an already existing dentry */
6553 +void unionfs_reinterpose(struct dentry *dentry)
6554 +{
6555 +       struct dentry *lower_dentry;
6556 +       struct inode *inode;
6557 +       int bindex, bstart, bend;
6558 +
6559 +       verify_locked(dentry);
6560 +
6561 +       /* This is pre-allocated inode */
6562 +       inode = dentry->d_inode;
6563 +
6564 +       bstart = dbstart(dentry);
6565 +       bend = dbend(dentry);
6566 +       for (bindex = bstart; bindex <= bend; bindex++) {
6567 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6568 +               if (!lower_dentry)
6569 +                       continue;
6570 +
6571 +               if (!lower_dentry->d_inode)
6572 +                       continue;
6573 +               if (unionfs_lower_inode_idx(inode, bindex))
6574 +                       continue;
6575 +               unionfs_set_lower_inode_idx(inode, bindex,
6576 +                                           igrab(lower_dentry->d_inode));
6577 +       }
6578 +       ibstart(inode) = dbstart(dentry);
6579 +       ibend(inode) = dbend(dentry);
6580 +}
6581 +
6582 +/*
6583 + * make sure the branch we just looked up (nd) makes sense:
6584 + *
6585 + * 1) we're not trying to stack unionfs on top of unionfs
6586 + * 2) it exists
6587 + * 3) is a directory
6588 + */
6589 +int check_branch(struct nameidata *nd)
6590 +{
6591 +       /* XXX: remove in ODF code -- stacking unions allowed there */
6592 +       if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6593 +               return -EINVAL;
6594 +       if (!nd->path.dentry->d_inode)
6595 +               return -ENOENT;
6596 +       if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6597 +               return -ENOTDIR;
6598 +       return 0;
6599 +}
6600 +
6601 +/* checks if two lower_dentries have overlapping branches */
6602 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6603 +{
6604 +       struct dentry *dent = NULL;
6605 +
6606 +       dent = dent1;
6607 +       while ((dent != dent2) && (dent->d_parent != dent))
6608 +               dent = dent->d_parent;
6609 +
6610 +       if (dent == dent2)
6611 +               return 1;
6612 +
6613 +       dent = dent2;
6614 +       while ((dent != dent1) && (dent->d_parent != dent))
6615 +               dent = dent->d_parent;
6616 +
6617 +       return (dent == dent1);
6618 +}
6619 +
6620 +/*
6621 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6622 + * specified.  Fill the mode bits in @perms.  If encounter an unknown
6623 + * string, return -EINVAL.  Otherwise return 0.
6624 + */
6625 +int parse_branch_mode(const char *name, int *perms)
6626 +{
6627 +       if (!name || !strcmp(name, "rw")) {
6628 +               *perms = MAY_READ | MAY_WRITE;
6629 +               return 0;
6630 +       }
6631 +       if (!strcmp(name, "ro")) {
6632 +               *perms = MAY_READ;
6633 +               return 0;
6634 +       }
6635 +       return -EINVAL;
6636 +}
6637 +
6638 +/*
6639 + * parse the dirs= mount argument
6640 + *
6641 + * We don't need to lock the superblock private data's rwsem, as we get
6642 + * called only by unionfs_read_super - it is still a long time before anyone
6643 + * can even get a reference to us.
6644 + */
6645 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6646 +                            *lower_root_info, char *options)
6647 +{
6648 +       struct nameidata nd;
6649 +       char *name;
6650 +       int err = 0;
6651 +       int branches = 1;
6652 +       int bindex = 0;
6653 +       int i = 0;
6654 +       int j = 0;
6655 +       struct dentry *dent1;
6656 +       struct dentry *dent2;
6657 +
6658 +       if (options[0] == '\0') {
6659 +               printk(KERN_ERR "unionfs: no branches specified\n");
6660 +               err = -EINVAL;
6661 +               goto out;
6662 +       }
6663 +
6664 +       /*
6665 +        * Each colon means we have a separator, this is really just a rough
6666 +        * guess, since strsep will handle empty fields for us.
6667 +        */
6668 +       for (i = 0; options[i]; i++)
6669 +               if (options[i] == ':')
6670 +                       branches++;
6671 +
6672 +       /* allocate space for underlying pointers to lower dentry */
6673 +       UNIONFS_SB(sb)->data =
6674 +               kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6675 +       if (unlikely(!UNIONFS_SB(sb)->data)) {
6676 +               err = -ENOMEM;
6677 +               goto out;
6678 +       }
6679 +
6680 +       lower_root_info->lower_paths =
6681 +               kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6682 +       if (unlikely(!lower_root_info->lower_paths)) {
6683 +               err = -ENOMEM;
6684 +               goto out;
6685 +       }
6686 +
6687 +       /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6688 +       branches = 0;
6689 +       while ((name = strsep(&options, ":")) != NULL) {
6690 +               int perms;
6691 +               char *mode = strchr(name, '=');
6692 +
6693 +               if (!name)
6694 +                       continue;
6695 +               if (!*name) {   /* bad use of ':' (extra colons) */
6696 +                       err = -EINVAL;
6697 +                       goto out;
6698 +               }
6699 +
6700 +               branches++;
6701 +
6702 +               /* strip off '=' if any */
6703 +               if (mode)
6704 +                       *mode++ = '\0';
6705 +
6706 +               err = parse_branch_mode(mode, &perms);
6707 +               if (err) {
6708 +                       printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6709 +                              "branch %d\n", mode, bindex);
6710 +                       goto out;
6711 +               }
6712 +               /* ensure that leftmost branch is writeable */
6713 +               if (!bindex && !(perms & MAY_WRITE)) {
6714 +                       printk(KERN_ERR "unionfs: leftmost branch cannot be "
6715 +                              "read-only (use \"-o ro\" to create a "
6716 +                              "read-only union)\n");
6717 +                       err = -EINVAL;
6718 +                       goto out;
6719 +               }
6720 +
6721 +               err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6722 +               if (err) {
6723 +                       printk(KERN_ERR "unionfs: error accessing "
6724 +                              "lower directory '%s' (error %d)\n",
6725 +                              name, err);
6726 +                       goto out;
6727 +               }
6728 +
6729 +               err = check_branch(&nd);
6730 +               if (err) {
6731 +                       printk(KERN_ERR "unionfs: lower directory "
6732 +                              "'%s' is not a valid branch\n", name);
6733 +                       path_put(&nd.path);
6734 +                       goto out;
6735 +               }
6736 +
6737 +               lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6738 +               lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6739 +
6740 +               set_branchperms(sb, bindex, perms);
6741 +               set_branch_count(sb, bindex, 0);
6742 +               new_branch_id(sb, bindex);
6743 +
6744 +               if (lower_root_info->bstart < 0)
6745 +                       lower_root_info->bstart = bindex;
6746 +               lower_root_info->bend = bindex;
6747 +               bindex++;
6748 +       }
6749 +
6750 +       if (branches == 0) {
6751 +               printk(KERN_ERR "unionfs: no branches specified\n");
6752 +               err = -EINVAL;
6753 +               goto out;
6754 +       }
6755 +
6756 +       BUG_ON(branches != (lower_root_info->bend + 1));
6757 +
6758 +       /*
6759 +        * Ensure that no overlaps exist in the branches.
6760 +        *
6761 +        * This test is required because the Linux kernel has no support
6762 +        * currently for ensuring coherency between stackable layers and
6763 +        * branches.  If we were to allow overlapping branches, it would be
6764 +        * possible, for example, to delete a file via one branch, which
6765 +        * would not be reflected in another branch.  Such incoherency could
6766 +        * lead to inconsistencies and even kernel oopses.  Rather than
6767 +        * implement hacks to work around some of these cache-coherency
6768 +        * problems, we prevent branch overlapping, for now.  A complete
6769 +        * solution will involve proper kernel/VFS support for cache
6770 +        * coherency, at which time we could safely remove this
6771 +        * branch-overlapping test.
6772 +        */
6773 +       for (i = 0; i < branches; i++) {
6774 +               dent1 = lower_root_info->lower_paths[i].dentry;
6775 +               for (j = i + 1; j < branches; j++) {
6776 +                       dent2 = lower_root_info->lower_paths[j].dentry;
6777 +                       if (is_branch_overlap(dent1, dent2)) {
6778 +                               printk(KERN_ERR "unionfs: branches %d and "
6779 +                                      "%d overlap\n", i, j);
6780 +                               err = -EINVAL;
6781 +                               goto out;
6782 +                       }
6783 +               }
6784 +       }
6785 +
6786 +out:
6787 +       if (err) {
6788 +               for (i = 0; i < branches; i++)
6789 +                       path_put(&lower_root_info->lower_paths[i]);
6790 +
6791 +               kfree(lower_root_info->lower_paths);
6792 +               kfree(UNIONFS_SB(sb)->data);
6793 +
6794 +               /*
6795 +                * MUST clear the pointers to prevent potential double free if
6796 +                * the caller dies later on
6797 +                */
6798 +               lower_root_info->lower_paths = NULL;
6799 +               UNIONFS_SB(sb)->data = NULL;
6800 +       }
6801 +       return err;
6802 +}
6803 +
6804 +/*
6805 + * Parse mount options.  See the manual page for usage instructions.
6806 + *
6807 + * Returns the dentry object of the lower-level (lower) directory;
6808 + * We want to mount our stackable file system on top of that lower directory.
6809 + */
6810 +static struct unionfs_dentry_info *unionfs_parse_options(
6811 +                                        struct super_block *sb,
6812 +                                        char *options)
6813 +{
6814 +       struct unionfs_dentry_info *lower_root_info;
6815 +       char *optname;
6816 +       int err = 0;
6817 +       int bindex;
6818 +       int dirsfound = 0;
6819 +
6820 +       /* allocate private data area */
6821 +       err = -ENOMEM;
6822 +       lower_root_info =
6823 +               kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6824 +       if (unlikely(!lower_root_info))
6825 +               goto out_error;
6826 +       lower_root_info->bstart = -1;
6827 +       lower_root_info->bend = -1;
6828 +       lower_root_info->bopaque = -1;
6829 +
6830 +       while ((optname = strsep(&options, ",")) != NULL) {
6831 +               char *optarg;
6832 +
6833 +               if (!optname || !*optname)
6834 +                       continue;
6835 +
6836 +               optarg = strchr(optname, '=');
6837 +               if (optarg)
6838 +                       *optarg++ = '\0';
6839 +
6840 +               /*
6841 +                * All of our options take an argument now. Insert ones that
6842 +                * don't, above this check.
6843 +                */
6844 +               if (!optarg) {
6845 +                       printk(KERN_ERR "unionfs: %s requires an argument\n",
6846 +                              optname);
6847 +                       err = -EINVAL;
6848 +                       goto out_error;
6849 +               }
6850 +
6851 +               if (!strcmp("dirs", optname)) {
6852 +                       if (++dirsfound > 1) {
6853 +                               printk(KERN_ERR
6854 +                                      "unionfs: multiple dirs specified\n");
6855 +                               err = -EINVAL;
6856 +                               goto out_error;
6857 +                       }
6858 +                       err = parse_dirs_option(sb, lower_root_info, optarg);
6859 +                       if (err)
6860 +                               goto out_error;
6861 +                       continue;
6862 +               }
6863 +
6864 +               err = -EINVAL;
6865 +               printk(KERN_ERR
6866 +                      "unionfs: unrecognized option '%s'\n", optname);
6867 +               goto out_error;
6868 +       }
6869 +       if (dirsfound != 1) {
6870 +               printk(KERN_ERR "unionfs: dirs option required\n");
6871 +               err = -EINVAL;
6872 +               goto out_error;
6873 +       }
6874 +       goto out;
6875 +
6876 +out_error:
6877 +       if (lower_root_info && lower_root_info->lower_paths) {
6878 +               for (bindex = lower_root_info->bstart;
6879 +                    bindex >= 0 && bindex <= lower_root_info->bend;
6880 +                    bindex++)
6881 +                       path_put(&lower_root_info->lower_paths[bindex]);
6882 +       }
6883 +
6884 +       kfree(lower_root_info->lower_paths);
6885 +       kfree(lower_root_info);
6886 +
6887 +       kfree(UNIONFS_SB(sb)->data);
6888 +       UNIONFS_SB(sb)->data = NULL;
6889 +
6890 +       lower_root_info = ERR_PTR(err);
6891 +out:
6892 +       return lower_root_info;
6893 +}
6894 +
6895 +/*
6896 + * our custom d_alloc_root work-alike
6897 + *
6898 + * we can't use d_alloc_root if we want to use our own interpose function
6899 + * unchanged, so we simply call our own "fake" d_alloc_root
6900 + */
6901 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
6902 +{
6903 +       struct dentry *ret = NULL;
6904 +
6905 +       if (sb) {
6906 +               static const struct qstr name = {
6907 +                       .name = "/",
6908 +                       .len = 1
6909 +               };
6910 +
6911 +               ret = d_alloc(NULL, &name);
6912 +               if (likely(ret)) {
6913 +                       ret->d_op = &unionfs_dops;
6914 +                       ret->d_sb = sb;
6915 +                       ret->d_parent = ret;
6916 +               }
6917 +       }
6918 +       return ret;
6919 +}
6920 +
6921 +/*
6922 + * There is no need to lock the unionfs_super_info's rwsem as there is no
6923 + * way anyone can have a reference to the superblock at this point in time.
6924 + */
6925 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
6926 +                             int silent)
6927 +{
6928 +       int err = 0;
6929 +       struct unionfs_dentry_info *lower_root_info = NULL;
6930 +       int bindex, bstart, bend;
6931 +
6932 +       if (!raw_data) {
6933 +               printk(KERN_ERR
6934 +                      "unionfs: read_super: missing data argument\n");
6935 +               err = -EINVAL;
6936 +               goto out;
6937 +       }
6938 +
6939 +       /* Allocate superblock private data */
6940 +       sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
6941 +       if (unlikely(!UNIONFS_SB(sb))) {
6942 +               printk(KERN_CRIT "unionfs: read_super: out of memory\n");
6943 +               err = -ENOMEM;
6944 +               goto out;
6945 +       }
6946 +
6947 +       UNIONFS_SB(sb)->bend = -1;
6948 +       atomic_set(&UNIONFS_SB(sb)->generation, 1);
6949 +       init_rwsem(&UNIONFS_SB(sb)->rwsem);
6950 +       UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
6951 +
6952 +       lower_root_info = unionfs_parse_options(sb, raw_data);
6953 +       if (IS_ERR(lower_root_info)) {
6954 +               printk(KERN_ERR
6955 +                      "unionfs: read_super: error while parsing options "
6956 +                      "(err = %ld)\n", PTR_ERR(lower_root_info));
6957 +               err = PTR_ERR(lower_root_info);
6958 +               lower_root_info = NULL;
6959 +               goto out_free;
6960 +       }
6961 +       if (lower_root_info->bstart == -1) {
6962 +               err = -ENOENT;
6963 +               goto out_free;
6964 +       }
6965 +
6966 +       /* set the lower superblock field of upper superblock */
6967 +       bstart = lower_root_info->bstart;
6968 +       BUG_ON(bstart != 0);
6969 +       sbend(sb) = bend = lower_root_info->bend;
6970 +       for (bindex = bstart; bindex <= bend; bindex++) {
6971 +               struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
6972 +               atomic_inc(&d->d_sb->s_active);
6973 +               unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
6974 +       }
6975 +
6976 +       /* max Bytes is the maximum bytes from highest priority branch */
6977 +       sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
6978 +
6979 +       /*
6980 +        * Our c/m/atime granularity is 1 ns because we may stack on file
6981 +        * systems whose granularity is as good.  This is important for our
6982 +        * time-based cache coherency.
6983 +        */
6984 +       sb->s_time_gran = 1;
6985 +
6986 +       sb->s_op = &unionfs_sops;
6987 +
6988 +       /* See comment next to the definition of unionfs_d_alloc_root */
6989 +       sb->s_root = unionfs_d_alloc_root(sb);
6990 +       if (unlikely(!sb->s_root)) {
6991 +               err = -ENOMEM;
6992 +               goto out_dput;
6993 +       }
6994 +
6995 +       /* link the upper and lower dentries */
6996 +       sb->s_root->d_fsdata = NULL;
6997 +       err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
6998 +       if (unlikely(err))
6999 +               goto out_freedpd;
7000 +
7001 +       /* Set the lower dentries for s_root */
7002 +       for (bindex = bstart; bindex <= bend; bindex++) {
7003 +               struct dentry *d;
7004 +               struct vfsmount *m;
7005 +
7006 +               d = lower_root_info->lower_paths[bindex].dentry;
7007 +               m = lower_root_info->lower_paths[bindex].mnt;
7008 +
7009 +               unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7010 +               unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7011 +       }
7012 +       dbstart(sb->s_root) = bstart;
7013 +       dbend(sb->s_root) = bend;
7014 +
7015 +       /* Set the generation number to one, since this is for the mount. */
7016 +       atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7017 +
7018 +       /*
7019 +        * Call interpose to create the upper level inode.  Only
7020 +        * INTERPOSE_LOOKUP can return a value other than 0 on err.
7021 +        */
7022 +       err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7023 +       unionfs_unlock_dentry(sb->s_root);
7024 +       if (!err)
7025 +               goto out;
7026 +       /* else fall through */
7027 +
7028 +out_freedpd:
7029 +       if (UNIONFS_D(sb->s_root)) {
7030 +               kfree(UNIONFS_D(sb->s_root)->lower_paths);
7031 +               free_dentry_private_data(sb->s_root);
7032 +       }
7033 +       dput(sb->s_root);
7034 +
7035 +out_dput:
7036 +       if (lower_root_info && !IS_ERR(lower_root_info)) {
7037 +               for (bindex = lower_root_info->bstart;
7038 +                    bindex <= lower_root_info->bend; bindex++) {
7039 +                       struct dentry *d;
7040 +                       d = lower_root_info->lower_paths[bindex].dentry;
7041 +                       /* drop refs we took earlier */
7042 +                       atomic_dec(&d->d_sb->s_active);
7043 +                       path_put(&lower_root_info->lower_paths[bindex]);
7044 +               }
7045 +               kfree(lower_root_info->lower_paths);
7046 +               kfree(lower_root_info);
7047 +               lower_root_info = NULL;
7048 +       }
7049 +
7050 +out_free:
7051 +       kfree(UNIONFS_SB(sb)->data);
7052 +       kfree(UNIONFS_SB(sb));
7053 +       sb->s_fs_info = NULL;
7054 +
7055 +out:
7056 +       if (lower_root_info && !IS_ERR(lower_root_info)) {
7057 +               kfree(lower_root_info->lower_paths);
7058 +               kfree(lower_root_info);
7059 +       }
7060 +       return err;
7061 +}
7062 +
7063 +static int unionfs_get_sb(struct file_system_type *fs_type,
7064 +                         int flags, const char *dev_name,
7065 +                         void *raw_data, struct vfsmount *mnt)
7066 +{
7067 +       int err;
7068 +       err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7069 +       if (!err)
7070 +               UNIONFS_SB(mnt->mnt_sb)->dev_name =
7071 +                       kstrdup(dev_name, GFP_KERNEL);
7072 +       return err;
7073 +}
7074 +
7075 +static struct file_system_type unionfs_fs_type = {
7076 +       .owner          = THIS_MODULE,
7077 +       .name           = UNIONFS_NAME,
7078 +       .get_sb         = unionfs_get_sb,
7079 +       .kill_sb        = generic_shutdown_super,
7080 +       .fs_flags       = FS_REVAL_DOT,
7081 +};
7082 +
7083 +static int __init init_unionfs_fs(void)
7084 +{
7085 +       int err;
7086 +
7087 +       pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7088 +
7089 +       err = unionfs_init_filldir_cache();
7090 +       if (unlikely(err))
7091 +               goto out;
7092 +       err = unionfs_init_inode_cache();
7093 +       if (unlikely(err))
7094 +               goto out;
7095 +       err = unionfs_init_dentry_cache();
7096 +       if (unlikely(err))
7097 +               goto out;
7098 +       err = init_sioq();
7099 +       if (unlikely(err))
7100 +               goto out;
7101 +       err = register_filesystem(&unionfs_fs_type);
7102 +out:
7103 +       if (unlikely(err)) {
7104 +               stop_sioq();
7105 +               unionfs_destroy_filldir_cache();
7106 +               unionfs_destroy_inode_cache();
7107 +               unionfs_destroy_dentry_cache();
7108 +       }
7109 +       return err;
7110 +}
7111 +
7112 +static void __exit exit_unionfs_fs(void)
7113 +{
7114 +       stop_sioq();
7115 +       unionfs_destroy_filldir_cache();
7116 +       unionfs_destroy_inode_cache();
7117 +       unionfs_destroy_dentry_cache();
7118 +       unregister_filesystem(&unionfs_fs_type);
7119 +       pr_info("Completed unionfs module unload\n");
7120 +}
7121 +
7122 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7123 +             " (http://www.fsl.cs.sunysb.edu)");
7124 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7125 +                  " (http://unionfs.filesystems.org)");
7126 +MODULE_LICENSE("GPL");
7127 +
7128 +module_init(init_unionfs_fs);
7129 +module_exit(exit_unionfs_fs);
7130 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/Makefile linux-2.6.35-rc6-git-unionfs//fs/unionfs/Makefile
7131 --- linux-2.6.35-rc6-git//fs/unionfs/Makefile   1970-01-01 02:00:00.000000000 +0200
7132 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/Makefile   2010-07-30 15:50:50.557108046 +0300
7133 @@ -0,0 +1,17 @@
7134 +UNIONFS_VERSION="2.5.4 (for 2.6.34-rc0)"
7135 +
7136 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
7137 +
7138 +obj-$(CONFIG_UNION_FS) += unionfs.o
7139 +
7140 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
7141 +       rdstate.o copyup.o dirhelper.o rename.o unlink.o \
7142 +       lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
7143 +
7144 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
7145 +
7146 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
7147 +
7148 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
7149 +EXTRA_CFLAGS += -DDEBUG
7150 +endif
7151 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/mmap.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/mmap.c
7152 --- linux-2.6.35-rc6-git//fs/unionfs/mmap.c     1970-01-01 02:00:00.000000000 +0200
7153 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/mmap.c     2010-07-30 15:50:50.561010133 +0300
7154 @@ -0,0 +1,89 @@
7155 +/*
7156 + * Copyright (c) 2003-2010 Erez Zadok
7157 + * Copyright (c) 2003-2006 Charles P. Wright
7158 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7159 + * Copyright (c) 2005-2006 Junjiro Okajima
7160 + * Copyright (c) 2006      Shaya Potter
7161 + * Copyright (c) 2005      Arun M. Krishnakumar
7162 + * Copyright (c) 2004-2006 David P. Quigley
7163 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7164 + * Copyright (c) 2003      Puja Gupta
7165 + * Copyright (c) 2003      Harikesavan Krishnan
7166 + * Copyright (c) 2003-2010 Stony Brook University
7167 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
7168 + *
7169 + * This program is free software; you can redistribute it and/or modify
7170 + * it under the terms of the GNU General Public License version 2 as
7171 + * published by the Free Software Foundation.
7172 + */
7173 +
7174 +#include "union.h"
7175 +
7176 +
7177 +/*
7178 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7179 + * use in unionfs_mmap) checks for the existence of
7180 + * mapping->a_ops->readpage, else it returns -ENOEXEC.  The VFS will need to
7181 + * be fixed to allow a file system to define vm_ops->fault without any
7182 + * address_space_ops whatsoever.
7183 + *
7184 + * Otherwise, we don't want to use our readpage method at all.
7185 + */
7186 +static int unionfs_readpage(struct file *file, struct page *page)
7187 +{
7188 +       BUG();
7189 +       return -EINVAL;
7190 +}
7191 +
7192 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7193 +{
7194 +       int err;
7195 +       struct file *file, *lower_file;
7196 +       const struct vm_operations_struct *lower_vm_ops;
7197 +       struct vm_area_struct lower_vma;
7198 +
7199 +       BUG_ON(!vma);
7200 +       memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7201 +       file = lower_vma.vm_file;
7202 +       lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7203 +       BUG_ON(!lower_vm_ops);
7204 +
7205 +       lower_file = unionfs_lower_file(file);
7206 +       BUG_ON(!lower_file);
7207 +       /*
7208 +        * XXX: vm_ops->fault may be called in parallel.  Because we have to
7209 +        * resort to temporarily changing the vma->vm_file to point to the
7210 +        * lower file, a concurrent invocation of unionfs_fault could see a
7211 +        * different value.  In this workaround, we keep a different copy of
7212 +        * the vma structure in our stack, so we never expose a different
7213 +        * value of the vma->vm_file called to us, even temporarily.  A
7214 +        * better fix would be to change the calling semantics of ->fault to
7215 +        * take an explicit file pointer.
7216 +        */
7217 +       lower_vma.vm_file = lower_file;
7218 +       err = lower_vm_ops->fault(&lower_vma, vmf);
7219 +       return err;
7220 +}
7221 +
7222 +/*
7223 + * XXX: the default address_space_ops for unionfs is empty.  We cannot set
7224 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7225 + * the a_ops vector to be non-NULL.
7226 + */
7227 +struct address_space_operations unionfs_aops = {
7228 +       /* empty on purpose */
7229 +};
7230 +
7231 +/*
7232 + * XXX: we need a second, dummy address_space_ops vector, to be used
7233 + * temporarily during unionfs_mmap, because the latter calls
7234 + * generic_file_mmap, which checks if ->readpage exists, else returns
7235 + * -ENOEXEC.
7236 + */
7237 +struct address_space_operations unionfs_dummy_aops = {
7238 +       .readpage       = unionfs_readpage,
7239 +};
7240 +
7241 +struct vm_operations_struct unionfs_vm_ops = {
7242 +       .fault          = unionfs_fault,
7243 +};
7244 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/rdstate.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/rdstate.c
7245 --- linux-2.6.35-rc6-git//fs/unionfs/rdstate.c  1970-01-01 02:00:00.000000000 +0200
7246 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/rdstate.c  2010-07-30 15:50:50.561010133 +0300
7247 @@ -0,0 +1,285 @@
7248 +/*
7249 + * Copyright (c) 2003-2010 Erez Zadok
7250 + * Copyright (c) 2003-2006 Charles P. Wright
7251 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7252 + * Copyright (c) 2005-2006 Junjiro Okajima
7253 + * Copyright (c) 2005      Arun M. Krishnakumar
7254 + * Copyright (c) 2004-2006 David P. Quigley
7255 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7256 + * Copyright (c) 2003      Puja Gupta
7257 + * Copyright (c) 2003      Harikesavan Krishnan
7258 + * Copyright (c) 2003-2010 Stony Brook University
7259 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
7260 + *
7261 + * This program is free software; you can redistribute it and/or modify
7262 + * it under the terms of the GNU General Public License version 2 as
7263 + * published by the Free Software Foundation.
7264 + */
7265 +
7266 +#include "union.h"
7267 +
7268 +/* This file contains the routines for maintaining readdir state. */
7269 +
7270 +/*
7271 + * There are two structures here, rdstate which is a hash table
7272 + * of the second structure which is a filldir_node.
7273 + */
7274 +
7275 +/*
7276 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7277 + * of them and they shouldn't waste memory.  If the node has a small name
7278 + * (as defined by the dentry structure), then we use an inline name to
7279 + * preserve kmalloc space.
7280 + */
7281 +static struct kmem_cache *unionfs_filldir_cachep;
7282 +
7283 +int unionfs_init_filldir_cache(void)
7284 +{
7285 +       unionfs_filldir_cachep =
7286 +               kmem_cache_create("unionfs_filldir",
7287 +                                 sizeof(struct filldir_node), 0,
7288 +                                 SLAB_RECLAIM_ACCOUNT, NULL);
7289 +
7290 +       return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7291 +}
7292 +
7293 +void unionfs_destroy_filldir_cache(void)
7294 +{
7295 +       if (unionfs_filldir_cachep)
7296 +               kmem_cache_destroy(unionfs_filldir_cachep);
7297 +}
7298 +
7299 +/*
7300 + * This is a tuning parameter that tells us roughly how big to make the
7301 + * hash table in directory entries per page.  This isn't perfect, but
7302 + * at least we get a hash table size that shouldn't be too overloaded.
7303 + * The following averages are based on my home directory.
7304 + * 14.44693    Overall
7305 + * 12.29       Single Page Directories
7306 + * 117.93      Multi-page directories
7307 + */
7308 +#define DENTPAGE 4096
7309 +#define DENTPERONEPAGE 12
7310 +#define DENTPERPAGE 118
7311 +#define MINHASHSIZE 1
7312 +static int guesstimate_hash_size(struct inode *inode)
7313 +{
7314 +       struct inode *lower_inode;
7315 +       int bindex;
7316 +       int hashsize = MINHASHSIZE;
7317 +
7318 +       if (UNIONFS_I(inode)->hashsize > 0)
7319 +               return UNIONFS_I(inode)->hashsize;
7320 +
7321 +       for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7322 +               lower_inode = unionfs_lower_inode_idx(inode, bindex);
7323 +               if (!lower_inode)
7324 +                       continue;
7325 +
7326 +               if (i_size_read(lower_inode) == DENTPAGE)
7327 +                       hashsize += DENTPERONEPAGE;
7328 +               else
7329 +                       hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7330 +                               DENTPERPAGE;
7331 +       }
7332 +
7333 +       return hashsize;
7334 +}
7335 +
7336 +int init_rdstate(struct file *file)
7337 +{
7338 +       BUG_ON(sizeof(loff_t) !=
7339 +              (sizeof(unsigned int) + sizeof(unsigned int)));
7340 +       BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7341 +
7342 +       UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7343 +                                                fbstart(file));
7344 +
7345 +       return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7346 +}
7347 +
7348 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7349 +{
7350 +       struct unionfs_dir_state *rdstate = NULL;
7351 +       struct list_head *pos;
7352 +
7353 +       spin_lock(&UNIONFS_I(inode)->rdlock);
7354 +       list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7355 +               struct unionfs_dir_state *r =
7356 +                       list_entry(pos, struct unionfs_dir_state, cache);
7357 +               if (fpos == rdstate2offset(r)) {
7358 +                       UNIONFS_I(inode)->rdcount--;
7359 +                       list_del(&r->cache);
7360 +                       rdstate = r;
7361 +                       break;
7362 +               }
7363 +       }
7364 +       spin_unlock(&UNIONFS_I(inode)->rdlock);
7365 +       return rdstate;
7366 +}
7367 +
7368 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7369 +{
7370 +       int i = 0;
7371 +       int hashsize;
7372 +       unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7373 +       struct unionfs_dir_state *rdstate;
7374 +
7375 +       hashsize = guesstimate_hash_size(inode);
7376 +       mallocsize += hashsize * sizeof(struct list_head);
7377 +       mallocsize = __roundup_pow_of_two(mallocsize);
7378 +
7379 +       /* This should give us about 500 entries anyway. */
7380 +       if (mallocsize > PAGE_SIZE)
7381 +               mallocsize = PAGE_SIZE;
7382 +
7383 +       hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7384 +               sizeof(struct list_head);
7385 +
7386 +       rdstate = kmalloc(mallocsize, GFP_KERNEL);
7387 +       if (unlikely(!rdstate))
7388 +               return NULL;
7389 +
7390 +       spin_lock(&UNIONFS_I(inode)->rdlock);
7391 +       if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7392 +               UNIONFS_I(inode)->cookie = 1;
7393 +       else
7394 +               UNIONFS_I(inode)->cookie++;
7395 +
7396 +       rdstate->cookie = UNIONFS_I(inode)->cookie;
7397 +       spin_unlock(&UNIONFS_I(inode)->rdlock);
7398 +       rdstate->offset = 1;
7399 +       rdstate->access = jiffies;
7400 +       rdstate->bindex = bindex;
7401 +       rdstate->dirpos = 0;
7402 +       rdstate->hashentries = 0;
7403 +       rdstate->size = hashsize;
7404 +       for (i = 0; i < rdstate->size; i++)
7405 +               INIT_LIST_HEAD(&rdstate->list[i]);
7406 +
7407 +       return rdstate;
7408 +}
7409 +
7410 +static void free_filldir_node(struct filldir_node *node)
7411 +{
7412 +       if (node->namelen >= DNAME_INLINE_LEN_MIN)
7413 +               kfree(node->name);
7414 +       kmem_cache_free(unionfs_filldir_cachep, node);
7415 +}
7416 +
7417 +void free_rdstate(struct unionfs_dir_state *state)
7418 +{
7419 +       struct filldir_node *tmp;
7420 +       int i;
7421 +
7422 +       for (i = 0; i < state->size; i++) {
7423 +               struct list_head *head = &(state->list[i]);
7424 +               struct list_head *pos, *n;
7425 +
7426 +               /* traverse the list and deallocate space */
7427 +               list_for_each_safe(pos, n, head) {
7428 +                       tmp = list_entry(pos, struct filldir_node, file_list);
7429 +                       list_del(&tmp->file_list);
7430 +                       free_filldir_node(tmp);
7431 +               }
7432 +       }
7433 +
7434 +       kfree(state);
7435 +}
7436 +
7437 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7438 +                                      const char *name, int namelen,
7439 +                                      int is_whiteout)
7440 +{
7441 +       int index;
7442 +       unsigned int hash;
7443 +       struct list_head *head;
7444 +       struct list_head *pos;
7445 +       struct filldir_node *cursor = NULL;
7446 +       int found = 0;
7447 +
7448 +       BUG_ON(namelen <= 0);
7449 +
7450 +       hash = full_name_hash(name, namelen);
7451 +       index = hash % rdstate->size;
7452 +
7453 +       head = &(rdstate->list[index]);
7454 +       list_for_each(pos, head) {
7455 +               cursor = list_entry(pos, struct filldir_node, file_list);
7456 +
7457 +               if (cursor->namelen == namelen && cursor->hash == hash &&
7458 +                   !strncmp(cursor->name, name, namelen)) {
7459 +                       /*
7460 +                        * a duplicate exists, and hence no need to create
7461 +                        * entry to the list
7462 +                        */
7463 +                       found = 1;
7464 +
7465 +                       /*
7466 +                        * if a duplicate is found in this branch, and is
7467 +                        * not due to the caller looking for an entry to
7468 +                        * whiteout, then the file system may be corrupted.
7469 +                        */
7470 +                       if (unlikely(!is_whiteout &&
7471 +                                    cursor->bindex == rdstate->bindex))
7472 +                               printk(KERN_ERR "unionfs: filldir: possible "
7473 +                                      "I/O error: a file is duplicated "
7474 +                                      "in the same branch %d: %s\n",
7475 +                                      rdstate->bindex, cursor->name);
7476 +                       break;
7477 +               }
7478 +       }
7479 +
7480 +       if (!found)
7481 +               cursor = NULL;
7482 +
7483 +       return cursor;
7484 +}
7485 +
7486 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7487 +                    int namelen, int bindex, int whiteout)
7488 +{
7489 +       struct filldir_node *new;
7490 +       unsigned int hash;
7491 +       int index;
7492 +       int err = 0;
7493 +       struct list_head *head;
7494 +
7495 +       BUG_ON(namelen <= 0);
7496 +
7497 +       hash = full_name_hash(name, namelen);
7498 +       index = hash % rdstate->size;
7499 +       head = &(rdstate->list[index]);
7500 +
7501 +       new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7502 +       if (unlikely(!new)) {
7503 +               err = -ENOMEM;
7504 +               goto out;
7505 +       }
7506 +
7507 +       INIT_LIST_HEAD(&new->file_list);
7508 +       new->namelen = namelen;
7509 +       new->hash = hash;
7510 +       new->bindex = bindex;
7511 +       new->whiteout = whiteout;
7512 +
7513 +       if (namelen < DNAME_INLINE_LEN_MIN) {
7514 +               new->name = new->iname;
7515 +       } else {
7516 +               new->name = kmalloc(namelen + 1, GFP_KERNEL);
7517 +               if (unlikely(!new->name)) {
7518 +                       kmem_cache_free(unionfs_filldir_cachep, new);
7519 +                       new = NULL;
7520 +                       goto out;
7521 +               }
7522 +       }
7523 +
7524 +       memcpy(new->name, name, namelen);
7525 +       new->name[namelen] = '\0';
7526 +
7527 +       rdstate->hashentries++;
7528 +
7529 +       list_add(&(new->file_list), head);
7530 +out:
7531 +       return err;
7532 +}
7533 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/rename.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/rename.c
7534 --- linux-2.6.35-rc6-git//fs/unionfs/rename.c   1970-01-01 02:00:00.000000000 +0200
7535 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/rename.c   2010-07-30 15:50:50.561010133 +0300
7536 @@ -0,0 +1,517 @@
7537 +/*
7538 + * Copyright (c) 2003-2010 Erez Zadok
7539 + * Copyright (c) 2003-2006 Charles P. Wright
7540 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7541 + * Copyright (c) 2005-2006 Junjiro Okajima
7542 + * Copyright (c) 2005      Arun M. Krishnakumar
7543 + * Copyright (c) 2004-2006 David P. Quigley
7544 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7545 + * Copyright (c) 2003      Puja Gupta
7546 + * Copyright (c) 2003      Harikesavan Krishnan
7547 + * Copyright (c) 2003-2010 Stony Brook University
7548 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
7549 + *
7550 + * This program is free software; you can redistribute it and/or modify
7551 + * it under the terms of the GNU General Public License version 2 as
7552 + * published by the Free Software Foundation.
7553 + */
7554 +
7555 +#include "union.h"
7556 +
7557 +/*
7558 + * This is a helper function for rename, used when rename ends up with hosed
7559 + * over dentries and we need to revert.
7560 + */
7561 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7562 +                                       struct dentry *parent, int bindex)
7563 +{
7564 +       struct dentry *lower_dentry;
7565 +       struct dentry *lower_parent;
7566 +       int err = 0;
7567 +
7568 +       verify_locked(dentry);
7569 +
7570 +       lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7571 +
7572 +       BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7573 +
7574 +       lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
7575 +                                     dentry->d_name.len);
7576 +       if (IS_ERR(lower_dentry)) {
7577 +               err = PTR_ERR(lower_dentry);
7578 +               goto out;
7579 +       }
7580 +
7581 +       dput(unionfs_lower_dentry_idx(dentry, bindex));
7582 +       iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7583 +       unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7584 +
7585 +       if (!lower_dentry->d_inode) {
7586 +               dput(lower_dentry);
7587 +               unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7588 +       } else {
7589 +               unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7590 +               unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7591 +                                           igrab(lower_dentry->d_inode));
7592 +       }
7593 +
7594 +out:
7595 +       return err;
7596 +}
7597 +
7598 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7599 +                           struct dentry *old_parent,
7600 +                           struct inode *new_dir, struct dentry *new_dentry,
7601 +                           struct dentry *new_parent,
7602 +                           int bindex)
7603 +{
7604 +       int err = 0;
7605 +       struct dentry *lower_old_dentry;
7606 +       struct dentry *lower_new_dentry;
7607 +       struct dentry *lower_old_dir_dentry;
7608 +       struct dentry *lower_new_dir_dentry;
7609 +       struct dentry *trap;
7610 +
7611 +       lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7612 +       lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7613 +
7614 +       if (!lower_new_dentry) {
7615 +               lower_new_dentry =
7616 +                       create_parents(new_parent->d_inode,
7617 +                                      new_dentry, new_dentry->d_name.name,
7618 +                                      bindex);
7619 +               if (IS_ERR(lower_new_dentry)) {
7620 +                       err = PTR_ERR(lower_new_dentry);
7621 +                       if (IS_COPYUP_ERR(err))
7622 +                               goto out;
7623 +                       printk(KERN_ERR "unionfs: error creating directory "
7624 +                              "tree for rename, bindex=%d err=%d\n",
7625 +                              bindex, err);
7626 +                       goto out;
7627 +               }
7628 +       }
7629 +
7630 +       /* check for and remove whiteout, if any */
7631 +       err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7632 +       if (err > 0) /* ignore if whiteout found and successfully removed */
7633 +               err = 0;
7634 +       if (err)
7635 +               goto out;
7636 +
7637 +       /* check of old_dentry branch is writable */
7638 +       err = is_robranch_super(old_dentry->d_sb, bindex);
7639 +       if (err)
7640 +               goto out;
7641 +
7642 +       dget(lower_old_dentry);
7643 +       dget(lower_new_dentry);
7644 +       lower_old_dir_dentry = dget_parent(lower_old_dentry);
7645 +       lower_new_dir_dentry = dget_parent(lower_new_dentry);
7646 +
7647 +       trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7648 +       /* source should not be ancenstor of target */
7649 +       if (trap == lower_old_dentry) {
7650 +               err = -EINVAL;
7651 +               goto out_err_unlock;
7652 +       }
7653 +       /* target should not be ancenstor of source */
7654 +       if (trap == lower_new_dentry) {
7655 +               err = -ENOTEMPTY;
7656 +               goto out_err_unlock;
7657 +       }
7658 +       err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7659 +                        lower_new_dir_dentry->d_inode, lower_new_dentry);
7660 +out_err_unlock:
7661 +       if (!err) {
7662 +               /* update parent dir times */
7663 +               fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7664 +               fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7665 +       }
7666 +       unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7667 +
7668 +       dput(lower_old_dir_dentry);
7669 +       dput(lower_new_dir_dentry);
7670 +       dput(lower_old_dentry);
7671 +       dput(lower_new_dentry);
7672 +
7673 +out:
7674 +       if (!err) {
7675 +               /* Fixup the new_dentry. */
7676 +               if (bindex < dbstart(new_dentry))
7677 +                       dbstart(new_dentry) = bindex;
7678 +               else if (bindex > dbend(new_dentry))
7679 +                       dbend(new_dentry) = bindex;
7680 +       }
7681 +
7682 +       return err;
7683 +}
7684 +
7685 +/*
7686 + * Main rename code.  This is sufficiently complex, that it's documented in
7687 + * Documentation/filesystems/unionfs/rename.txt.  This routine calls
7688 + * __unionfs_rename() above to perform some of the work.
7689 + */
7690 +static int do_unionfs_rename(struct inode *old_dir,
7691 +                            struct dentry *old_dentry,
7692 +                            struct dentry *old_parent,
7693 +                            struct inode *new_dir,
7694 +                            struct dentry *new_dentry,
7695 +                            struct dentry *new_parent)
7696 +{
7697 +       int err = 0;
7698 +       int bindex;
7699 +       int old_bstart, old_bend;
7700 +       int new_bstart, new_bend;
7701 +       int do_copyup = -1;
7702 +       int local_err = 0;
7703 +       int eio = 0;
7704 +       int revert = 0;
7705 +
7706 +       old_bstart = dbstart(old_dentry);
7707 +       old_bend = dbend(old_dentry);
7708 +
7709 +       new_bstart = dbstart(new_dentry);
7710 +       new_bend = dbend(new_dentry);
7711 +
7712 +       /* Rename source to destination. */
7713 +       err = __unionfs_rename(old_dir, old_dentry, old_parent,
7714 +                              new_dir, new_dentry, new_parent,
7715 +                              old_bstart);
7716 +       if (err) {
7717 +               if (!IS_COPYUP_ERR(err))
7718 +                       goto out;
7719 +               do_copyup = old_bstart - 1;
7720 +       } else {
7721 +               revert = 1;
7722 +       }
7723 +
7724 +       /*
7725 +        * Unlink all instances of destination that exist to the left of
7726 +        * bstart of source. On error, revert back, goto out.
7727 +        */
7728 +       for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7729 +               struct dentry *unlink_dentry;
7730 +               struct dentry *unlink_dir_dentry;
7731 +
7732 +               BUG_ON(bindex < 0);
7733 +               unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7734 +               if (!unlink_dentry)
7735 +                       continue;
7736 +
7737 +               unlink_dir_dentry = lock_parent(unlink_dentry);
7738 +               err = is_robranch_super(old_dir->i_sb, bindex);
7739 +               if (!err)
7740 +                       err = vfs_unlink(unlink_dir_dentry->d_inode,
7741 +                                        unlink_dentry);
7742 +
7743 +               fsstack_copy_attr_times(new_parent->d_inode,
7744 +                                       unlink_dir_dentry->d_inode);
7745 +               /* propagate number of hard-links */
7746 +               new_parent->d_inode->i_nlink =
7747 +                       unionfs_get_nlinks(new_parent->d_inode);
7748 +
7749 +               unlock_dir(unlink_dir_dentry);
7750 +               if (!err) {
7751 +                       if (bindex != new_bstart) {
7752 +                               dput(unlink_dentry);
7753 +                               unionfs_set_lower_dentry_idx(new_dentry,
7754 +                                                            bindex, NULL);
7755 +                       }
7756 +               } else if (IS_COPYUP_ERR(err)) {
7757 +                       do_copyup = bindex - 1;
7758 +               } else if (revert) {
7759 +                       goto revert;
7760 +               }
7761 +       }
7762 +
7763 +       if (do_copyup != -1) {
7764 +               for (bindex = do_copyup; bindex >= 0; bindex--) {
7765 +                       /*
7766 +                        * copyup the file into some left directory, so that
7767 +                        * you can rename it
7768 +                        */
7769 +                       err = copyup_dentry(old_parent->d_inode,
7770 +                                           old_dentry, old_bstart, bindex,
7771 +                                           old_dentry->d_name.name,
7772 +                                           old_dentry->d_name.len, NULL,
7773 +                                           i_size_read(old_dentry->d_inode));
7774 +                       /* if copyup failed, try next branch to the left */
7775 +                       if (err)
7776 +                               continue;
7777 +                       /*
7778 +                        * create whiteout before calling __unionfs_rename
7779 +                        * because the latter will change the old_dentry's
7780 +                        * lower name and parent dir, resulting in the
7781 +                        * whiteout getting created in the wrong dir.
7782 +                        */
7783 +                       err = create_whiteout(old_dentry, bindex);
7784 +                       if (err) {
7785 +                               printk(KERN_ERR "unionfs: can't create a "
7786 +                                      "whiteout for %s in rename (err=%d)\n",
7787 +                                      old_dentry->d_name.name, err);
7788 +                               continue;
7789 +                       }
7790 +                       err = __unionfs_rename(old_dir, old_dentry, old_parent,
7791 +                                              new_dir, new_dentry, new_parent,
7792 +                                              bindex);
7793 +                       break;
7794 +               }
7795 +       }
7796 +
7797 +       /* make it opaque */
7798 +       if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7799 +               err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7800 +               if (err)
7801 +                       goto revert;
7802 +       }
7803 +
7804 +       /*
7805 +        * Create whiteout for source, only if:
7806 +        * (1) There is more than one underlying instance of source.
7807 +        * (We did a copy_up is taken care of above).
7808 +        */
7809 +       if ((old_bstart != old_bend) && (do_copyup == -1)) {
7810 +               err = create_whiteout(old_dentry, old_bstart);
7811 +               if (err) {
7812 +                       /* can't fix anything now, so we exit with -EIO */
7813 +                       printk(KERN_ERR "unionfs: can't create a whiteout for "
7814 +                              "%s in rename!\n", old_dentry->d_name.name);
7815 +                       err = -EIO;
7816 +               }
7817 +       }
7818 +
7819 +out:
7820 +       return err;
7821 +
7822 +revert:
7823 +       /* Do revert here. */
7824 +       local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7825 +                                                old_bstart);
7826 +       if (local_err) {
7827 +               printk(KERN_ERR "unionfs: revert failed in rename: "
7828 +                      "the new refresh failed\n");
7829 +               eio = -EIO;
7830 +       }
7831 +
7832 +       local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7833 +                                                old_bstart);
7834 +       if (local_err) {
7835 +               printk(KERN_ERR "unionfs: revert failed in rename: "
7836 +                      "the old refresh failed\n");
7837 +               eio = -EIO;
7838 +               goto revert_out;
7839 +       }
7840 +
7841 +       if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7842 +           !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7843 +               printk(KERN_ERR "unionfs: revert failed in rename: "
7844 +                      "the object disappeared from under us!\n");
7845 +               eio = -EIO;
7846 +               goto revert_out;
7847 +       }
7848 +
7849 +       if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
7850 +           unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
7851 +               printk(KERN_ERR "unionfs: revert failed in rename: "
7852 +                      "the object was created underneath us!\n");
7853 +               eio = -EIO;
7854 +               goto revert_out;
7855 +       }
7856 +
7857 +       local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
7858 +                                    old_dir, old_dentry, old_parent,
7859 +                                    old_bstart);
7860 +
7861 +       /* If we can't fix it, then we cop-out with -EIO. */
7862 +       if (local_err) {
7863 +               printk(KERN_ERR "unionfs: revert failed in rename!\n");
7864 +               eio = -EIO;
7865 +       }
7866 +
7867 +       local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7868 +                                                bindex);
7869 +       if (local_err)
7870 +               eio = -EIO;
7871 +       local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7872 +                                                bindex);
7873 +       if (local_err)
7874 +               eio = -EIO;
7875 +
7876 +revert_out:
7877 +       if (eio)
7878 +               err = eio;
7879 +       return err;
7880 +}
7881 +
7882 +/*
7883 + * We can't copyup a directory, because it may involve huge numbers of
7884 + * children, etc.  Doing that in the kernel would be bad, so instead we
7885 + * return EXDEV to the user-space utility that caused this, and let the
7886 + * user-space recurse and ask us to copy up each file separately.
7887 + */
7888 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
7889 +{
7890 +       int err, bstart;
7891 +
7892 +       err = check_empty(dentry, parent, NULL);
7893 +       if (err == -ENOTEMPTY) {
7894 +               if (is_robranch(dentry))
7895 +                       return -EXDEV;
7896 +       } else if (err) {
7897 +               return err;
7898 +       }
7899 +
7900 +       bstart = dbstart(dentry);
7901 +       if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
7902 +               return 0;
7903 +
7904 +       dbstart(dentry) = bstart + 1;
7905 +       err = check_empty(dentry, parent, NULL);
7906 +       dbstart(dentry) = bstart;
7907 +       if (err == -ENOTEMPTY)
7908 +               err = -EXDEV;
7909 +       return err;
7910 +}
7911 +
7912 +/*
7913 + * The locking rules in unionfs_rename are complex.  We could use a simpler
7914 + * superblock-level name-space lock for renames and copy-ups.
7915 + */
7916 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7917 +                  struct inode *new_dir, struct dentry *new_dentry)
7918 +{
7919 +       int err = 0;
7920 +       struct dentry *wh_dentry;
7921 +       struct dentry *old_parent, *new_parent;
7922 +       int valid = true;
7923 +
7924 +       unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
7925 +       old_parent = dget_parent(old_dentry);
7926 +       new_parent = dget_parent(new_dentry);
7927 +       /* un/lock parent dentries only if they differ from old/new_dentry */
7928 +       if (old_parent != old_dentry &&
7929 +           old_parent != new_dentry)
7930 +               unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
7931 +       if (new_parent != old_dentry &&
7932 +           new_parent != new_dentry &&
7933 +           new_parent != old_parent)
7934 +               unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
7935 +       unionfs_double_lock_dentry(old_dentry, new_dentry);
7936 +
7937 +       valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
7938 +       if (!valid) {
7939 +               err = -ESTALE;
7940 +               goto out;
7941 +       }
7942 +       if (!d_deleted(new_dentry) && new_dentry->d_inode) {
7943 +               valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
7944 +               if (!valid) {
7945 +                       err = -ESTALE;
7946 +                       goto out;
7947 +               }
7948 +       }
7949 +
7950 +       if (!S_ISDIR(old_dentry->d_inode->i_mode))
7951 +               err = unionfs_partial_lookup(old_dentry, old_parent);
7952 +       else
7953 +               err = may_rename_dir(old_dentry, old_parent);
7954 +
7955 +       if (err)
7956 +               goto out;
7957 +
7958 +       err = unionfs_partial_lookup(new_dentry, new_parent);
7959 +       if (err)
7960 +               goto out;
7961 +
7962 +       /*
7963 +        * if new_dentry is already lower because of whiteout,
7964 +        * simply override it even if the whited-out dir is not empty.
7965 +        */
7966 +       wh_dentry = find_first_whiteout(new_dentry);
7967 +       if (!IS_ERR(wh_dentry)) {
7968 +               dput(wh_dentry);
7969 +       } else if (new_dentry->d_inode) {
7970 +               if (S_ISDIR(old_dentry->d_inode->i_mode) !=
7971 +                   S_ISDIR(new_dentry->d_inode->i_mode)) {
7972 +                       err = S_ISDIR(old_dentry->d_inode->i_mode) ?
7973 +                               -ENOTDIR : -EISDIR;
7974 +                       goto out;
7975 +               }
7976 +
7977 +               if (S_ISDIR(new_dentry->d_inode->i_mode)) {
7978 +                       struct unionfs_dir_state *namelist = NULL;
7979 +                       /* check if this unionfs directory is empty or not */
7980 +                       err = check_empty(new_dentry, new_parent, &namelist);
7981 +                       if (err)
7982 +                               goto out;
7983 +
7984 +                       if (!is_robranch(new_dentry))
7985 +                               err = delete_whiteouts(new_dentry,
7986 +                                                      dbstart(new_dentry),
7987 +                                                      namelist);
7988 +
7989 +                       free_rdstate(namelist);
7990 +
7991 +                       if (err)
7992 +                               goto out;
7993 +               }
7994 +       }
7995 +
7996 +       err = do_unionfs_rename(old_dir, old_dentry, old_parent,
7997 +                               new_dir, new_dentry, new_parent);
7998 +       if (err)
7999 +               goto out;
8000 +
8001 +       /*
8002 +        * force re-lookup since the dir on ro branch is not renamed, and
8003 +        * lower dentries still indicate the un-renamed ones.
8004 +        */
8005 +       if (S_ISDIR(old_dentry->d_inode->i_mode))
8006 +               atomic_dec(&UNIONFS_D(old_dentry)->generation);
8007 +       else
8008 +               unionfs_postcopyup_release(old_dentry);
8009 +       if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8010 +               unionfs_postcopyup_release(new_dentry);
8011 +               unionfs_postcopyup_setmnt(new_dentry);
8012 +               if (!unionfs_lower_inode(new_dentry->d_inode)) {
8013 +                       /*
8014 +                        * If we get here, it means that no copyup was
8015 +                        * needed, and that a file by the old name already
8016 +                        * existing on the destination branch; that file got
8017 +                        * renamed earlier in this function, so all we need
8018 +                        * to do here is set the lower inode.
8019 +                        */
8020 +                       struct inode *inode;
8021 +                       inode = unionfs_lower_inode(old_dentry->d_inode);
8022 +                       igrab(inode);
8023 +                       unionfs_set_lower_inode_idx(new_dentry->d_inode,
8024 +                                                   dbstart(new_dentry),
8025 +                                                   inode);
8026 +               }
8027 +       }
8028 +       /* if all of this renaming succeeded, update our times */
8029 +       unionfs_copy_attr_times(old_dentry->d_inode);
8030 +       unionfs_copy_attr_times(new_dentry->d_inode);
8031 +       unionfs_check_inode(old_dir);
8032 +       unionfs_check_inode(new_dir);
8033 +       unionfs_check_dentry(old_dentry);
8034 +       unionfs_check_dentry(new_dentry);
8035 +
8036 +out:
8037 +       if (err)                /* clear the new_dentry stuff created */
8038 +               d_drop(new_dentry);
8039 +
8040 +       unionfs_double_unlock_dentry(old_dentry, new_dentry);
8041 +       if (new_parent != old_dentry &&
8042 +           new_parent != new_dentry &&
8043 +           new_parent != old_parent)
8044 +               unionfs_unlock_dentry(new_parent);
8045 +       if (old_parent != old_dentry &&
8046 +           old_parent != new_dentry)
8047 +               unionfs_unlock_dentry(old_parent);
8048 +       dput(new_parent);
8049 +       dput(old_parent);
8050 +       unionfs_read_unlock(old_dentry->d_sb);
8051 +
8052 +       return err;
8053 +}
8054 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/sioq.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/sioq.c
8055 --- linux-2.6.35-rc6-git//fs/unionfs/sioq.c     1970-01-01 02:00:00.000000000 +0200
8056 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/sioq.c     2010-07-30 15:50:50.561010133 +0300
8057 @@ -0,0 +1,101 @@
8058 +/*
8059 + * Copyright (c) 2006-2010 Erez Zadok
8060 + * Copyright (c) 2006      Charles P. Wright
8061 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8062 + * Copyright (c) 2006      Junjiro Okajima
8063 + * Copyright (c) 2006      David P. Quigley
8064 + * Copyright (c) 2006-2010 Stony Brook University
8065 + * Copyright (c) 2006-2010 The Research Foundation of SUNY
8066 + *
8067 + * This program is free software; you can redistribute it and/or modify
8068 + * it under the terms of the GNU General Public License version 2 as
8069 + * published by the Free Software Foundation.
8070 + */
8071 +
8072 +#include "union.h"
8073 +
8074 +/*
8075 + * Super-user IO work Queue - sometimes we need to perform actions which
8076 + * would fail due to the unix permissions on the parent directory (e.g.,
8077 + * rmdir a directory which appears empty, but in reality contains
8078 + * whiteouts).
8079 + */
8080 +
8081 +static struct workqueue_struct *superio_workqueue;
8082 +
8083 +int __init init_sioq(void)
8084 +{
8085 +       int err;
8086 +
8087 +       superio_workqueue = create_workqueue("unionfs_siod");
8088 +       if (!IS_ERR(superio_workqueue))
8089 +               return 0;
8090 +
8091 +       err = PTR_ERR(superio_workqueue);
8092 +       printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8093 +       superio_workqueue = NULL;
8094 +       return err;
8095 +}
8096 +
8097 +void stop_sioq(void)
8098 +{
8099 +       if (superio_workqueue)
8100 +               destroy_workqueue(superio_workqueue);
8101 +}
8102 +
8103 +void run_sioq(work_func_t func, struct sioq_args *args)
8104 +{
8105 +       INIT_WORK(&args->work, func);
8106 +
8107 +       init_completion(&args->comp);
8108 +       while (!queue_work(superio_workqueue, &args->work)) {
8109 +               /* TODO: do accounting if needed */
8110 +               schedule();
8111 +       }
8112 +       wait_for_completion(&args->comp);
8113 +}
8114 +
8115 +void __unionfs_create(struct work_struct *work)
8116 +{
8117 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8118 +       struct create_args *c = &args->create;
8119 +
8120 +       args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8121 +       complete(&args->comp);
8122 +}
8123 +
8124 +void __unionfs_mkdir(struct work_struct *work)
8125 +{
8126 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8127 +       struct mkdir_args *m = &args->mkdir;
8128 +
8129 +       args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8130 +       complete(&args->comp);
8131 +}
8132 +
8133 +void __unionfs_mknod(struct work_struct *work)
8134 +{
8135 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8136 +       struct mknod_args *m = &args->mknod;
8137 +
8138 +       args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8139 +       complete(&args->comp);
8140 +}
8141 +
8142 +void __unionfs_symlink(struct work_struct *work)
8143 +{
8144 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8145 +       struct symlink_args *s = &args->symlink;
8146 +
8147 +       args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8148 +       complete(&args->comp);
8149 +}
8150 +
8151 +void __unionfs_unlink(struct work_struct *work)
8152 +{
8153 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
8154 +       struct unlink_args *u = &args->unlink;
8155 +
8156 +       args->err = vfs_unlink(u->parent, u->dentry);
8157 +       complete(&args->comp);
8158 +}
8159 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/sioq.h linux-2.6.35-rc6-git-unionfs//fs/unionfs/sioq.h
8160 --- linux-2.6.35-rc6-git//fs/unionfs/sioq.h     1970-01-01 02:00:00.000000000 +0200
8161 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/sioq.h     2010-07-30 15:50:50.561010133 +0300
8162 @@ -0,0 +1,91 @@
8163 +/*
8164 + * Copyright (c) 2006-2010 Erez Zadok
8165 + * Copyright (c) 2006      Charles P. Wright
8166 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8167 + * Copyright (c) 2006      Junjiro Okajima
8168 + * Copyright (c) 2006      David P. Quigley
8169 + * Copyright (c) 2006-2010 Stony Brook University
8170 + * Copyright (c) 2006-2010 The Research Foundation of SUNY
8171 + *
8172 + * This program is free software; you can redistribute it and/or modify
8173 + * it under the terms of the GNU General Public License version 2 as
8174 + * published by the Free Software Foundation.
8175 + */
8176 +
8177 +#ifndef _SIOQ_H
8178 +#define _SIOQ_H
8179 +
8180 +struct deletewh_args {
8181 +       struct unionfs_dir_state *namelist;
8182 +       struct dentry *dentry;
8183 +       int bindex;
8184 +};
8185 +
8186 +struct is_opaque_args {
8187 +       struct dentry *dentry;
8188 +};
8189 +
8190 +struct create_args {
8191 +       struct inode *parent;
8192 +       struct dentry *dentry;
8193 +       umode_t mode;
8194 +       struct nameidata *nd;
8195 +};
8196 +
8197 +struct mkdir_args {
8198 +       struct inode *parent;
8199 +       struct dentry *dentry;
8200 +       umode_t mode;
8201 +};
8202 +
8203 +struct mknod_args {
8204 +       struct inode *parent;
8205 +       struct dentry *dentry;
8206 +       umode_t mode;
8207 +       dev_t dev;
8208 +};
8209 +
8210 +struct symlink_args {
8211 +       struct inode *parent;
8212 +       struct dentry *dentry;
8213 +       char *symbuf;
8214 +};
8215 +
8216 +struct unlink_args {
8217 +       struct inode *parent;
8218 +       struct dentry *dentry;
8219 +};
8220 +
8221 +
8222 +struct sioq_args {
8223 +       struct completion comp;
8224 +       struct work_struct work;
8225 +       int err;
8226 +       void *ret;
8227 +
8228 +       union {
8229 +               struct deletewh_args deletewh;
8230 +               struct is_opaque_args is_opaque;
8231 +               struct create_args create;
8232 +               struct mkdir_args mkdir;
8233 +               struct mknod_args mknod;
8234 +               struct symlink_args symlink;
8235 +               struct unlink_args unlink;
8236 +       };
8237 +};
8238 +
8239 +/* Extern definitions for SIOQ functions */
8240 +extern int __init init_sioq(void);
8241 +extern void stop_sioq(void);
8242 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8243 +
8244 +/* Extern definitions for our privilege escalation helpers */
8245 +extern void __unionfs_create(struct work_struct *work);
8246 +extern void __unionfs_mkdir(struct work_struct *work);
8247 +extern void __unionfs_mknod(struct work_struct *work);
8248 +extern void __unionfs_symlink(struct work_struct *work);
8249 +extern void __unionfs_unlink(struct work_struct *work);
8250 +extern void __delete_whiteouts(struct work_struct *work);
8251 +extern void __is_opaque_dir(struct work_struct *work);
8252 +
8253 +#endif /* not _SIOQ_H */
8254 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/subr.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/subr.c
8255 --- linux-2.6.35-rc6-git//fs/unionfs/subr.c     1970-01-01 02:00:00.000000000 +0200
8256 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/subr.c     2010-07-30 15:50:50.561010133 +0300
8257 @@ -0,0 +1,95 @@
8258 +/*
8259 + * Copyright (c) 2003-2010 Erez Zadok
8260 + * Copyright (c) 2003-2006 Charles P. Wright
8261 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8262 + * Copyright (c) 2005-2006 Junjiro Okajima
8263 + * Copyright (c) 2005      Arun M. Krishnakumar
8264 + * Copyright (c) 2004-2006 David P. Quigley
8265 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8266 + * Copyright (c) 2003      Puja Gupta
8267 + * Copyright (c) 2003      Harikesavan Krishnan
8268 + * Copyright (c) 2003-2010 Stony Brook University
8269 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
8270 + *
8271 + * This program is free software; you can redistribute it and/or modify
8272 + * it under the terms of the GNU General Public License version 2 as
8273 + * published by the Free Software Foundation.
8274 + */
8275 +
8276 +#include "union.h"
8277 +
8278 +/*
8279 + * returns the right n_link value based on the inode type
8280 + */
8281 +int unionfs_get_nlinks(const struct inode *inode)
8282 +{
8283 +       /* don't bother to do all the work since we're unlinked */
8284 +       if (inode->i_nlink == 0)
8285 +               return 0;
8286 +
8287 +       if (!S_ISDIR(inode->i_mode))
8288 +               return unionfs_lower_inode(inode)->i_nlink;
8289 +
8290 +       /*
8291 +        * For directories, we return 1. The only place that could cares
8292 +        * about links is readdir, and there's d_type there so even that
8293 +        * doesn't matter.
8294 +        */
8295 +       return 1;
8296 +}
8297 +
8298 +/* copy a/m/ctime from the lower branch with the newest times */
8299 +void unionfs_copy_attr_times(struct inode *upper)
8300 +{
8301 +       int bindex;
8302 +       struct inode *lower;
8303 +
8304 +       if (!upper)
8305 +               return;
8306 +       if (ibstart(upper) < 0) {
8307 +#ifdef CONFIG_UNION_FS_DEBUG
8308 +               WARN_ON(ibstart(upper) < 0);
8309 +#endif /* CONFIG_UNION_FS_DEBUG */
8310 +               return;
8311 +       }
8312 +       for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8313 +               lower = unionfs_lower_inode_idx(upper, bindex);
8314 +               if (!lower)
8315 +                       continue; /* not all lower dir objects may exist */
8316 +               if (unlikely(timespec_compare(&upper->i_mtime,
8317 +                                             &lower->i_mtime) < 0))
8318 +                       upper->i_mtime = lower->i_mtime;
8319 +               if (unlikely(timespec_compare(&upper->i_ctime,
8320 +                                             &lower->i_ctime) < 0))
8321 +                       upper->i_ctime = lower->i_ctime;
8322 +               if (unlikely(timespec_compare(&upper->i_atime,
8323 +                                             &lower->i_atime) < 0))
8324 +                       upper->i_atime = lower->i_atime;
8325 +       }
8326 +}
8327 +
8328 +/*
8329 + * A unionfs/fanout version of fsstack_copy_attr_all.  Uses a
8330 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8331 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8332 + * important if the lower inode is a directory type)
8333 + */
8334 +void unionfs_copy_attr_all(struct inode *dest,
8335 +                          const struct inode *src)
8336 +{
8337 +       dest->i_mode = src->i_mode;
8338 +       dest->i_uid = src->i_uid;
8339 +       dest->i_gid = src->i_gid;
8340 +       dest->i_rdev = src->i_rdev;
8341 +
8342 +       unionfs_copy_attr_times(dest);
8343 +
8344 +       dest->i_blkbits = src->i_blkbits;
8345 +       dest->i_flags = src->i_flags;
8346 +
8347 +       /*
8348 +        * Update the nlinks AFTER updating the above fields, because the
8349 +        * get_links callback may depend on them.
8350 +        */
8351 +       dest->i_nlink = unionfs_get_nlinks(dest);
8352 +}
8353 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/super.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/super.c
8354 --- linux-2.6.35-rc6-git//fs/unionfs/super.c    1970-01-01 02:00:00.000000000 +0200
8355 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/super.c    2010-07-30 15:50:50.562010141 +0300
8356 @@ -0,0 +1,1047 @@
8357 +/*
8358 + * Copyright (c) 2003-2010 Erez Zadok
8359 + * Copyright (c) 2003-2006 Charles P. Wright
8360 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8361 + * Copyright (c) 2005-2006 Junjiro Okajima
8362 + * Copyright (c) 2005      Arun M. Krishnakumar
8363 + * Copyright (c) 2004-2006 David P. Quigley
8364 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8365 + * Copyright (c) 2003      Puja Gupta
8366 + * Copyright (c) 2003      Harikesavan Krishnan
8367 + * Copyright (c) 2003-2010 Stony Brook University
8368 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
8369 + *
8370 + * This program is free software; you can redistribute it and/or modify
8371 + * it under the terms of the GNU General Public License version 2 as
8372 + * published by the Free Software Foundation.
8373 + */
8374 +
8375 +#include "union.h"
8376 +
8377 +/*
8378 + * The inode cache is used with alloc_inode for both our inode info and the
8379 + * vfs inode.
8380 + */
8381 +static struct kmem_cache *unionfs_inode_cachep;
8382 +
8383 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8384 +{
8385 +       int size;
8386 +       struct unionfs_inode_info *info;
8387 +       struct inode *inode;
8388 +
8389 +       inode = iget_locked(sb, ino);
8390 +       if (!inode)
8391 +               return ERR_PTR(-ENOMEM);
8392 +       if (!(inode->i_state & I_NEW))
8393 +               return inode;
8394 +
8395 +       info = UNIONFS_I(inode);
8396 +       memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8397 +       info->bstart = -1;
8398 +       info->bend = -1;
8399 +       atomic_set(&info->generation,
8400 +                  atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8401 +       spin_lock_init(&info->rdlock);
8402 +       info->rdcount = 1;
8403 +       info->hashsize = -1;
8404 +       INIT_LIST_HEAD(&info->readdircache);
8405 +
8406 +       size = sbmax(inode->i_sb) * sizeof(struct inode *);
8407 +       info->lower_inodes = kzalloc(size, GFP_KERNEL);
8408 +       if (unlikely(!info->lower_inodes)) {
8409 +               printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8410 +                      "lower-pointer array!\n");
8411 +               iget_failed(inode);
8412 +               return ERR_PTR(-ENOMEM);
8413 +       }
8414 +
8415 +       inode->i_version++;
8416 +       inode->i_op = &unionfs_main_iops;
8417 +       inode->i_fop = &unionfs_main_fops;
8418 +
8419 +       inode->i_mapping->a_ops = &unionfs_aops;
8420 +
8421 +       /*
8422 +        * reset times so unionfs_copy_attr_all can keep out time invariants
8423 +        * right (upper inode time being the max of all lower ones).
8424 +        */
8425 +       inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8426 +       inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8427 +       inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8428 +       unlock_new_inode(inode);
8429 +       return inode;
8430 +}
8431 +
8432 +/*
8433 + * we now define delete_inode, because there are two VFS paths that may
8434 + * destroy an inode: one of them calls clear inode before doing everything
8435 + * else that's needed, and the other is fine.  This way we truncate the inode
8436 + * size (and its pages) and then clear our own inode, which will do an iput
8437 + * on our and the lower inode.
8438 + *
8439 + * No need to lock sb info's rwsem.
8440 + */
8441 +static void unionfs_delete_inode(struct inode *inode)
8442 +{
8443 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8444 +       spin_lock(&inode->i_lock);
8445 +#endif
8446 +       i_size_write(inode, 0); /* every f/s seems to do that */
8447 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8448 +       spin_unlock(&inode->i_lock);
8449 +#endif
8450 +
8451 +       if (inode->i_data.nrpages)
8452 +               truncate_inode_pages(&inode->i_data, 0);
8453 +
8454 +       clear_inode(inode);
8455 +}
8456 +
8457 +/*
8458 + * final actions when unmounting a file system
8459 + *
8460 + * No need to lock rwsem.
8461 + */
8462 +static void unionfs_put_super(struct super_block *sb)
8463 +{
8464 +       int bindex, bstart, bend;
8465 +       struct unionfs_sb_info *spd;
8466 +       int leaks = 0;
8467 +
8468 +       spd = UNIONFS_SB(sb);
8469 +       if (!spd)
8470 +               return;
8471 +
8472 +       bstart = sbstart(sb);
8473 +       bend = sbend(sb);
8474 +
8475 +       /* Make sure we have no leaks of branchget/branchput. */
8476 +       for (bindex = bstart; bindex <= bend; bindex++)
8477 +               if (unlikely(branch_count(sb, bindex) != 0)) {
8478 +                       printk(KERN_CRIT
8479 +                              "unionfs: branch %d has %d references left!\n",
8480 +                              bindex, branch_count(sb, bindex));
8481 +                       leaks = 1;
8482 +               }
8483 +       WARN_ON(leaks != 0);
8484 +
8485 +       /* decrement lower super references */
8486 +       for (bindex = bstart; bindex <= bend; bindex++) {
8487 +               struct super_block *s;
8488 +               s = unionfs_lower_super_idx(sb, bindex);
8489 +               unionfs_set_lower_super_idx(sb, bindex, NULL);
8490 +               atomic_dec(&s->s_active);
8491 +       }
8492 +
8493 +       kfree(spd->dev_name);
8494 +       kfree(spd->data);
8495 +       kfree(spd);
8496 +       sb->s_fs_info = NULL;
8497 +}
8498 +
8499 +/*
8500 + * Since people use this to answer the "How big of a file can I write?"
8501 + * question, we report the size of the highest priority branch as the size of
8502 + * the union.
8503 + */
8504 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8505 +{
8506 +       int err = 0;
8507 +       struct super_block *sb;
8508 +       struct dentry *lower_dentry;
8509 +       struct dentry *parent;
8510 +       bool valid;
8511 +
8512 +       sb = dentry->d_sb;
8513 +
8514 +       unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8515 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8516 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8517 +
8518 +       valid = __unionfs_d_revalidate(dentry, parent, false);
8519 +       if (unlikely(!valid)) {
8520 +               err = -ESTALE;
8521 +               goto out;
8522 +       }
8523 +       unionfs_check_dentry(dentry);
8524 +
8525 +       lower_dentry = unionfs_lower_dentry(sb->s_root);
8526 +       err = vfs_statfs(lower_dentry, buf);
8527 +
8528 +       /* set return buf to our f/s to avoid confusing user-level utils */
8529 +       buf->f_type = UNIONFS_SUPER_MAGIC;
8530 +       /*
8531 +        * Our maximum file name can is shorter by a few bytes because every
8532 +        * file name could potentially be whited-out.
8533 +        *
8534 +        * XXX: this restriction goes away with ODF.
8535 +        */
8536 +       unionfs_set_max_namelen(&buf->f_namelen);
8537 +
8538 +       /*
8539 +        * reset two fields to avoid confusing user-land.
8540 +        * XXX: is this still necessary?
8541 +        */
8542 +       memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8543 +       memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8544 +
8545 +out:
8546 +       unionfs_check_dentry(dentry);
8547 +       unionfs_unlock_dentry(dentry);
8548 +       unionfs_unlock_parent(dentry, parent);
8549 +       unionfs_read_unlock(sb);
8550 +       return err;
8551 +}
8552 +
8553 +/* handle mode changing during remount */
8554 +static noinline_for_stack int do_remount_mode_option(
8555 +                                       char *optarg,
8556 +                                       int cur_branches,
8557 +                                       struct unionfs_data *new_data,
8558 +                                       struct path *new_lower_paths)
8559 +{
8560 +       int err = -EINVAL;
8561 +       int perms, idx;
8562 +       char *modename = strchr(optarg, '=');
8563 +       struct nameidata nd;
8564 +
8565 +       /* by now, optarg contains the branch name */
8566 +       if (!*optarg) {
8567 +               printk(KERN_ERR
8568 +                      "unionfs: no branch specified for mode change\n");
8569 +               goto out;
8570 +       }
8571 +       if (!modename) {
8572 +               printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8573 +                      optarg);
8574 +               goto out;
8575 +       }
8576 +       *modename++ = '\0';
8577 +       err = parse_branch_mode(modename, &perms);
8578 +       if (err) {
8579 +               printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8580 +                      modename, optarg);
8581 +               goto out;
8582 +       }
8583 +
8584 +       /*
8585 +        * Find matching branch index.  For now, this assumes that nothing
8586 +        * has been mounted on top of this Unionfs stack.  Once we have /odf
8587 +        * and cache-coherency resolved, we'll address the branch-path
8588 +        * uniqueness.
8589 +        */
8590 +       err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8591 +       if (err) {
8592 +               printk(KERN_ERR "unionfs: error accessing "
8593 +                      "lower directory \"%s\" (error %d)\n",
8594 +                      optarg, err);
8595 +               goto out;
8596 +       }
8597 +       for (idx = 0; idx < cur_branches; idx++)
8598 +               if (nd.path.mnt == new_lower_paths[idx].mnt &&
8599 +                   nd.path.dentry == new_lower_paths[idx].dentry)
8600 +                       break;
8601 +       path_put(&nd.path);     /* no longer needed */
8602 +       if (idx == cur_branches) {
8603 +               err = -ENOENT;  /* err may have been reset above */
8604 +               printk(KERN_ERR "unionfs: branch \"%s\" "
8605 +                      "not found\n", optarg);
8606 +               goto out;
8607 +       }
8608 +       /* check/change mode for existing branch */
8609 +       /* we don't warn if perms==branchperms */
8610 +       new_data[idx].branchperms = perms;
8611 +       err = 0;
8612 +out:
8613 +       return err;
8614 +}
8615 +
8616 +/* handle branch deletion during remount */
8617 +static noinline_for_stack int do_remount_del_option(
8618 +                                       char *optarg, int cur_branches,
8619 +                                       struct unionfs_data *new_data,
8620 +                                       struct path *new_lower_paths)
8621 +{
8622 +       int err = -EINVAL;
8623 +       int idx;
8624 +       struct nameidata nd;
8625 +
8626 +       /* optarg contains the branch name to delete */
8627 +
8628 +       /*
8629 +        * Find matching branch index.  For now, this assumes that nothing
8630 +        * has been mounted on top of this Unionfs stack.  Once we have /odf
8631 +        * and cache-coherency resolved, we'll address the branch-path
8632 +        * uniqueness.
8633 +        */
8634 +       err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8635 +       if (err) {
8636 +               printk(KERN_ERR "unionfs: error accessing "
8637 +                      "lower directory \"%s\" (error %d)\n",
8638 +                      optarg, err);
8639 +               goto out;
8640 +       }
8641 +       for (idx = 0; idx < cur_branches; idx++)
8642 +               if (nd.path.mnt == new_lower_paths[idx].mnt &&
8643 +                   nd.path.dentry == new_lower_paths[idx].dentry)
8644 +                       break;
8645 +       path_put(&nd.path);     /* no longer needed */
8646 +       if (idx == cur_branches) {
8647 +               printk(KERN_ERR "unionfs: branch \"%s\" "
8648 +                      "not found\n", optarg);
8649 +               err = -ENOENT;
8650 +               goto out;
8651 +       }
8652 +       /* check if there are any open files on the branch to be deleted */
8653 +       if (atomic_read(&new_data[idx].open_files) > 0) {
8654 +               err = -EBUSY;
8655 +               goto out;
8656 +       }
8657 +
8658 +       /*
8659 +        * Now we have to delete the branch.  First, release any handles it
8660 +        * has.  Then, move the remaining array indexes past "idx" in
8661 +        * new_data and new_lower_paths one to the left.  Finally, adjust
8662 +        * cur_branches.
8663 +        */
8664 +       path_put(&new_lower_paths[idx]);
8665 +
8666 +       if (idx < cur_branches - 1) {
8667 +               /* if idx==cur_branches-1, we delete last branch: easy */
8668 +               memmove(&new_data[idx], &new_data[idx+1],
8669 +                       (cur_branches - 1 - idx) *
8670 +                       sizeof(struct unionfs_data));
8671 +               memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8672 +                       (cur_branches - 1 - idx) * sizeof(struct path));
8673 +       }
8674 +
8675 +       err = 0;
8676 +out:
8677 +       return err;
8678 +}
8679 +
8680 +/* handle branch insertion during remount */
8681 +static noinline_for_stack int do_remount_add_option(
8682 +                                       char *optarg, int cur_branches,
8683 +                                       struct unionfs_data *new_data,
8684 +                                       struct path *new_lower_paths,
8685 +                                       int *high_branch_id)
8686 +{
8687 +       int err = -EINVAL;
8688 +       int perms;
8689 +       int idx = 0;            /* default: insert at beginning */
8690 +       char *new_branch , *modename = NULL;
8691 +       struct nameidata nd;
8692 +
8693 +       /*
8694 +        * optarg can be of several forms:
8695 +        *
8696 +        * /bar:/foo            insert /foo before /bar
8697 +        * /bar:/foo=ro         insert /foo in ro mode before /bar
8698 +        * /foo                 insert /foo in the beginning (prepend)
8699 +        * :/foo                insert /foo at the end (append)
8700 +        */
8701 +       if (*optarg == ':') {   /* append? */
8702 +               new_branch = optarg + 1; /* skip ':' */
8703 +               idx = cur_branches;
8704 +               goto found_insertion_point;
8705 +       }
8706 +       new_branch = strchr(optarg, ':');
8707 +       if (!new_branch) {      /* prepend? */
8708 +               new_branch = optarg;
8709 +               goto found_insertion_point;
8710 +       }
8711 +       *new_branch++ = '\0';   /* holds path+mode of new branch */
8712 +
8713 +       /*
8714 +        * Find matching branch index.  For now, this assumes that nothing
8715 +        * has been mounted on top of this Unionfs stack.  Once we have /odf
8716 +        * and cache-coherency resolved, we'll address the branch-path
8717 +        * uniqueness.
8718 +        */
8719 +       err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8720 +       if (err) {
8721 +               printk(KERN_ERR "unionfs: error accessing "
8722 +                      "lower directory \"%s\" (error %d)\n",
8723 +                      optarg, err);
8724 +               goto out;
8725 +       }
8726 +       for (idx = 0; idx < cur_branches; idx++)
8727 +               if (nd.path.mnt == new_lower_paths[idx].mnt &&
8728 +                   nd.path.dentry == new_lower_paths[idx].dentry)
8729 +                       break;
8730 +       path_put(&nd.path);     /* no longer needed */
8731 +       if (idx == cur_branches) {
8732 +               printk(KERN_ERR "unionfs: branch \"%s\" "
8733 +                      "not found\n", optarg);
8734 +               err = -ENOENT;
8735 +               goto out;
8736 +       }
8737 +
8738 +       /*
8739 +        * At this point idx will hold the index where the new branch should
8740 +        * be inserted before.
8741 +        */
8742 +found_insertion_point:
8743 +       /* find the mode for the new branch */
8744 +       if (new_branch)
8745 +               modename = strchr(new_branch, '=');
8746 +       if (modename)
8747 +               *modename++ = '\0';
8748 +       if (!new_branch || !*new_branch) {
8749 +               printk(KERN_ERR "unionfs: null new branch\n");
8750 +               err = -EINVAL;
8751 +               goto out;
8752 +       }
8753 +       err = parse_branch_mode(modename, &perms);
8754 +       if (err) {
8755 +               printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8756 +                      "branch \"%s\"\n", modename, new_branch);
8757 +               goto out;
8758 +       }
8759 +       err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8760 +       if (err) {
8761 +               printk(KERN_ERR "unionfs: error accessing "
8762 +                      "lower directory \"%s\" (error %d)\n",
8763 +                      new_branch, err);
8764 +               goto out;
8765 +       }
8766 +       /*
8767 +        * It's probably safe to check_mode the new branch to insert.  Note:
8768 +        * we don't allow inserting branches which are unionfs's by
8769 +        * themselves (check_branch returns EINVAL in that case).  This is
8770 +        * because this code base doesn't support stacking unionfs: the ODF
8771 +        * code base supports that correctly.
8772 +        */
8773 +       err = check_branch(&nd);
8774 +       if (err) {
8775 +               printk(KERN_ERR "unionfs: lower directory "
8776 +                      "\"%s\" is not a valid branch\n", optarg);
8777 +               path_put(&nd.path);
8778 +               goto out;
8779 +       }
8780 +
8781 +       /*
8782 +        * Now we have to insert the new branch.  But first, move the bits
8783 +        * to make space for the new branch, if needed.  Finally, adjust
8784 +        * cur_branches.
8785 +        * We don't release nd here; it's kept until umount/remount.
8786 +        */
8787 +       if (idx < cur_branches) {
8788 +               /* if idx==cur_branches, we append: easy */
8789 +               memmove(&new_data[idx+1], &new_data[idx],
8790 +                       (cur_branches - idx) * sizeof(struct unionfs_data));
8791 +               memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8792 +                       (cur_branches - idx) * sizeof(struct path));
8793 +       }
8794 +       new_lower_paths[idx].dentry = nd.path.dentry;
8795 +       new_lower_paths[idx].mnt = nd.path.mnt;
8796 +
8797 +       new_data[idx].sb = nd.path.dentry->d_sb;
8798 +       atomic_set(&new_data[idx].open_files, 0);
8799 +       new_data[idx].branchperms = perms;
8800 +       new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8801 +
8802 +       err = 0;
8803 +out:
8804 +       return err;
8805 +}
8806 +
8807 +
8808 +/*
8809 + * Support branch management options on remount.
8810 + *
8811 + * See Documentation/filesystems/unionfs/ for details.
8812 + *
8813 + * @flags: numeric mount options
8814 + * @options: mount options string
8815 + *
8816 + * This function can rearrange a mounted union dynamically, adding and
8817 + * removing branches, including changing branch modes.  Clearly this has to
8818 + * be done safely and atomically.  Luckily, the VFS already calls this
8819 + * function with lock_super(sb) and lock_kernel() held, preventing
8820 + * concurrent mixing of new mounts, remounts, and unmounts.  Moreover,
8821 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8822 + * to purge dentries/inodes from our superblock, and also called
8823 + * fsync_super(sb) to purge any dirty pages.  So we're good.
8824 + *
8825 + * XXX: however, our remount code may also need to invalidate mapped pages
8826 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8827 + * branches.  This has to wait for proper mmap and cache coherency support
8828 + * in the VFS.
8829 + *
8830 + */
8831 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8832 +                             char *options)
8833 +{
8834 +       int err = 0;
8835 +       int i;
8836 +       char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
8837 +       char *optname;
8838 +       int cur_branches = 0;   /* no. of current branches */
8839 +       int new_branches = 0;   /* no. of branches actually left in the end */
8840 +       int add_branches;       /* est. no. of branches to add */
8841 +       int del_branches;       /* est. no. of branches to del */
8842 +       int max_branches;       /* max possible no. of branches */
8843 +       struct unionfs_data *new_data = NULL, *tmp_data = NULL;
8844 +       struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
8845 +       struct inode **new_lower_inodes = NULL;
8846 +       int new_high_branch_id; /* new high branch ID */
8847 +       int size;               /* memory allocation size, temp var */
8848 +       int old_ibstart, old_ibend;
8849 +
8850 +       unionfs_write_lock(sb);
8851 +
8852 +       /*
8853 +        * The VFS will take care of "ro" and "rw" flags, and we can safely
8854 +        * ignore MS_SILENT, but anything else left over is an error.  So we
8855 +        * need to check if any other flags may have been passed (none are
8856 +        * allowed/supported as of now).
8857 +        */
8858 +       if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8859 +               printk(KERN_ERR
8860 +                      "unionfs: remount flags 0x%x unsupported\n", *flags);
8861 +               err = -EINVAL;
8862 +               goto out_error;
8863 +       }
8864 +
8865 +       /*
8866 +        * If 'options' is NULL, it's probably because the user just changed
8867 +        * the union to a "ro" or "rw" and the VFS took care of it.  So
8868 +        * nothing to do and we're done.
8869 +        */
8870 +       if (!options || options[0] == '\0')
8871 +               goto out_error;
8872 +
8873 +       /*
8874 +        * Find out how many branches we will have in the end, counting
8875 +        * "add" and "del" commands.  Copy the "options" string because
8876 +        * strsep modifies the string and we need it later.
8877 +        */
8878 +       tmp_to_free = kstrdup(options, GFP_KERNEL);
8879 +       optionstmp = tmp_to_free;
8880 +       if (unlikely(!optionstmp)) {
8881 +               err = -ENOMEM;
8882 +               goto out_free;
8883 +       }
8884 +       cur_branches = sbmax(sb); /* current no. branches */
8885 +       new_branches = sbmax(sb);
8886 +       del_branches = 0;
8887 +       add_branches = 0;
8888 +       new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
8889 +       while ((optname = strsep(&optionstmp, ",")) != NULL) {
8890 +               char *optarg;
8891 +
8892 +               if (!optname || !*optname)
8893 +                       continue;
8894 +
8895 +               optarg = strchr(optname, '=');
8896 +               if (optarg)
8897 +                       *optarg++ = '\0';
8898 +
8899 +               if (!strcmp("add", optname))
8900 +                       add_branches++;
8901 +               else if (!strcmp("del", optname))
8902 +                       del_branches++;
8903 +       }
8904 +       kfree(tmp_to_free);
8905 +       /* after all changes, will we have at least one branch left? */
8906 +       if ((new_branches + add_branches - del_branches) < 1) {
8907 +               printk(KERN_ERR
8908 +                      "unionfs: no branches left after remount\n");
8909 +               err = -EINVAL;
8910 +               goto out_free;
8911 +       }
8912 +
8913 +       /*
8914 +        * Since we haven't actually parsed all the add/del options, nor
8915 +        * have we checked them for errors, we don't know for sure how many
8916 +        * branches we will have after all changes have taken place.  In
8917 +        * fact, the total number of branches left could be less than what
8918 +        * we have now.  So we need to allocate space for a temporary
8919 +        * placeholder that is at least as large as the maximum number of
8920 +        * branches we *could* have, which is the current number plus all
8921 +        * the additions.  Once we're done with these temp placeholders, we
8922 +        * may have to re-allocate the final size, copy over from the temp,
8923 +        * and then free the temps (done near the end of this function).
8924 +        */
8925 +       max_branches = cur_branches + add_branches;
8926 +       /* allocate space for new pointers to lower dentry */
8927 +       tmp_data = kcalloc(max_branches,
8928 +                          sizeof(struct unionfs_data), GFP_KERNEL);
8929 +       if (unlikely(!tmp_data)) {
8930 +               err = -ENOMEM;
8931 +               goto out_free;
8932 +       }
8933 +       /* allocate space for new pointers to lower paths */
8934 +       tmp_lower_paths = kcalloc(max_branches,
8935 +                                 sizeof(struct path), GFP_KERNEL);
8936 +       if (unlikely(!tmp_lower_paths)) {
8937 +               err = -ENOMEM;
8938 +               goto out_free;
8939 +       }
8940 +       /* copy current info into new placeholders, incrementing refcnts */
8941 +       memcpy(tmp_data, UNIONFS_SB(sb)->data,
8942 +              cur_branches * sizeof(struct unionfs_data));
8943 +       memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
8944 +              cur_branches * sizeof(struct path));
8945 +       for (i = 0; i < cur_branches; i++)
8946 +               path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
8947 +
8948 +       /*******************************************************************
8949 +        * For each branch command, do path_lookup on the requested branch,
8950 +        * and apply the change to a temp branch list.  To handle errors, we
8951 +        * already dup'ed the old arrays (above), and increased the refcnts
8952 +        * on various f/s objects.  So now we can do all the path_lookups
8953 +        * and branch-management commands on the new arrays.  If it fail mid
8954 +        * way, we free the tmp arrays and *put all objects.  If we succeed,
8955 +        * then we free old arrays and *put its objects, and then replace
8956 +        * the arrays with the new tmp list (we may have to re-allocate the
8957 +        * memory because the temp lists could have been larger than what we
8958 +        * actually needed).
8959 +        *******************************************************************/
8960 +
8961 +       while ((optname = strsep(&options, ",")) != NULL) {
8962 +               char *optarg;
8963 +
8964 +               if (!optname || !*optname)
8965 +                       continue;
8966 +               /*
8967 +                * At this stage optname holds a comma-delimited option, but
8968 +                * without the commas.  Next, we need to break the string on
8969 +                * the '=' symbol to separate CMD=ARG, where ARG itself can
8970 +                * be KEY=VAL.  For example, in mode=/foo=rw, CMD is "mode",
8971 +                * KEY is "/foo", and VAL is "rw".
8972 +                */
8973 +               optarg = strchr(optname, '=');
8974 +               if (optarg)
8975 +                       *optarg++ = '\0';
8976 +               /* incgen remount option (instead of old ioctl) */
8977 +               if (!strcmp("incgen", optname)) {
8978 +                       err = 0;
8979 +                       goto out_no_change;
8980 +               }
8981 +
8982 +               /*
8983 +                * All of our options take an argument now.  (Insert ones
8984 +                * that don't above this check.)  So at this stage optname
8985 +                * contains the CMD part and optarg contains the ARG part.
8986 +                */
8987 +               if (!optarg || !*optarg) {
8988 +                       printk(KERN_ERR "unionfs: all remount options require "
8989 +                              "an argument (%s)\n", optname);
8990 +                       err = -EINVAL;
8991 +                       goto out_release;
8992 +               }
8993 +
8994 +               if (!strcmp("add", optname)) {
8995 +                       err = do_remount_add_option(optarg, new_branches,
8996 +                                                   tmp_data,
8997 +                                                   tmp_lower_paths,
8998 +                                                   &new_high_branch_id);
8999 +                       if (err)
9000 +                               goto out_release;
9001 +                       new_branches++;
9002 +                       if (new_branches > UNIONFS_MAX_BRANCHES) {
9003 +                               printk(KERN_ERR "unionfs: command exceeds "
9004 +                                      "%d branches\n", UNIONFS_MAX_BRANCHES);
9005 +                               err = -E2BIG;
9006 +                               goto out_release;
9007 +                       }
9008 +                       continue;
9009 +               }
9010 +               if (!strcmp("del", optname)) {
9011 +                       err = do_remount_del_option(optarg, new_branches,
9012 +                                                   tmp_data,
9013 +                                                   tmp_lower_paths);
9014 +                       if (err)
9015 +                               goto out_release;
9016 +                       new_branches--;
9017 +                       continue;
9018 +               }
9019 +               if (!strcmp("mode", optname)) {
9020 +                       err = do_remount_mode_option(optarg, new_branches,
9021 +                                                    tmp_data,
9022 +                                                    tmp_lower_paths);
9023 +                       if (err)
9024 +                               goto out_release;
9025 +                       continue;
9026 +               }
9027 +
9028 +               /*
9029 +                * When you use "mount -o remount,ro", mount(8) will
9030 +                * reportedly pass the original dirs= string from
9031 +                * /proc/mounts.  So for now, we have to ignore dirs= and
9032 +                * not consider it an error, unless we want to allow users
9033 +                * to pass dirs= in remount.  Note that to allow the VFS to
9034 +                * actually process the ro/rw remount options, we have to
9035 +                * return 0 from this function.
9036 +                */
9037 +               if (!strcmp("dirs", optname)) {
9038 +                       printk(KERN_WARNING
9039 +                              "unionfs: remount ignoring option \"%s\"\n",
9040 +                              optname);
9041 +                       continue;
9042 +               }
9043 +
9044 +               err = -EINVAL;
9045 +               printk(KERN_ERR
9046 +                      "unionfs: unrecognized option \"%s\"\n", optname);
9047 +               goto out_release;
9048 +       }
9049 +
9050 +out_no_change:
9051 +
9052 +       /******************************************************************
9053 +        * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9054 +        * see if we need to allocate a small-sized new vector, copy the
9055 +        * vectors to their correct place, release the refcnt of the older
9056 +        * ones, and return.  Also handle invalidating any pages that will
9057 +        * have to be re-read.
9058 +        *******************************************************************/
9059 +
9060 +       if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9061 +               printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9062 +                      "(use \"remount,ro\" to create a read-only union)\n");
9063 +               err = -EINVAL;
9064 +               goto out_release;
9065 +       }
9066 +
9067 +       /* (re)allocate space for new pointers to lower dentry */
9068 +       size = new_branches * sizeof(struct unionfs_data);
9069 +       new_data = krealloc(tmp_data, size, GFP_KERNEL);
9070 +       if (unlikely(!new_data)) {
9071 +               err = -ENOMEM;
9072 +               goto out_release;
9073 +       }
9074 +
9075 +       /* allocate space for new pointers to lower paths */
9076 +       size = new_branches * sizeof(struct path);
9077 +       new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9078 +       if (unlikely(!new_lower_paths)) {
9079 +               err = -ENOMEM;
9080 +               goto out_release;
9081 +       }
9082 +
9083 +       /* allocate space for new pointers to lower inodes */
9084 +       new_lower_inodes = kcalloc(new_branches,
9085 +                                  sizeof(struct inode *), GFP_KERNEL);
9086 +       if (unlikely(!new_lower_inodes)) {
9087 +               err = -ENOMEM;
9088 +               goto out_release;
9089 +       }
9090 +
9091 +       /*
9092 +        * OK, just before we actually put the new set of branches in place,
9093 +        * we need to ensure that our own f/s has no dirty objects left.
9094 +        * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9095 +        * fsync_super(sb), taking care of dentries, inodes, and dirty
9096 +        * pages.  So all that's left is for us to invalidate any leftover
9097 +        * (non-dirty) pages to ensure that they will be re-read from the
9098 +        * new lower branches (and to support mmap).
9099 +        */
9100 +
9101 +       /*
9102 +        * Once we finish the remounting successfully, our superblock
9103 +        * generation number will have increased.  This will be detected by
9104 +        * our dentry-revalidation code upon subsequent f/s operations
9105 +        * through unionfs.  The revalidation code will rebuild the union of
9106 +        * lower inodes for a given unionfs inode and invalidate any pages
9107 +        * of such "stale" inodes (by calling our purge_inode_data
9108 +        * function).  This revalidation will happen lazily and
9109 +        * incrementally, as users perform operations on cached inodes.  We
9110 +        * would like to encourage this revalidation to happen sooner if
9111 +        * possible, so we like to try to invalidate as many other pages in
9112 +        * our superblock as we can.  We used to call drop_pagecache_sb() or
9113 +        * a variant thereof, but either method was racy (drop_caches alone
9114 +        * is known to be racy).  So now we let the revalidation happen on a
9115 +        * per file basis in ->d_revalidate.
9116 +        */
9117 +
9118 +       /* grab new lower super references; release old ones */
9119 +       for (i = 0; i < new_branches; i++)
9120 +               atomic_inc(&new_data[i].sb->s_active);
9121 +       for (i = 0; i < sbmax(sb); i++)
9122 +               atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9123 +
9124 +       /* copy new vectors into their correct place */
9125 +       tmp_data = UNIONFS_SB(sb)->data;
9126 +       UNIONFS_SB(sb)->data = new_data;
9127 +       new_data = NULL;        /* so don't free good pointers below */
9128 +       tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9129 +       UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9130 +       new_lower_paths = NULL; /* so don't free good pointers below */
9131 +
9132 +       /* update our unionfs_sb_info and root dentry index of last branch */
9133 +       i = sbmax(sb);          /* save no. of branches to release at end */
9134 +       sbend(sb) = new_branches - 1;
9135 +       dbend(sb->s_root) = new_branches - 1;
9136 +       old_ibstart = ibstart(sb->s_root->d_inode);
9137 +       old_ibend = ibend(sb->s_root->d_inode);
9138 +       ibend(sb->s_root->d_inode) = new_branches - 1;
9139 +       UNIONFS_D(sb->s_root)->bcount = new_branches;
9140 +       new_branches = i; /* no. of branches to release below */
9141 +
9142 +       /*
9143 +        * Update lower inodes: 3 steps
9144 +        * 1. grab ref on all new lower inodes
9145 +        */
9146 +       for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9147 +               struct dentry *lower_dentry =
9148 +                       unionfs_lower_dentry_idx(sb->s_root, i);
9149 +               igrab(lower_dentry->d_inode);
9150 +               new_lower_inodes[i] = lower_dentry->d_inode;
9151 +       }
9152 +       /* 2. release reference on all older lower inodes */
9153 +       iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9154 +       /* 3. update root dentry's inode to new lower_inodes array */
9155 +       UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9156 +       new_lower_inodes = NULL;
9157 +
9158 +       /* maxbytes may have changed */
9159 +       sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9160 +       /* update high branch ID */
9161 +       sbhbid(sb) = new_high_branch_id;
9162 +
9163 +       /* update our sb->generation for revalidating objects */
9164 +       i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9165 +       atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9166 +       atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9167 +       if (!(*flags & MS_SILENT))
9168 +               pr_info("unionfs: %s: new generation number %d\n",
9169 +                       UNIONFS_SB(sb)->dev_name, i);
9170 +       /* finally, update the root dentry's times */
9171 +       unionfs_copy_attr_times(sb->s_root->d_inode);
9172 +       err = 0;                /* reset to success */
9173 +
9174 +       /*
9175 +        * The code above falls through to the next label, and releases the
9176 +        * refcnts of the older ones (stored in tmp_*): if we fell through
9177 +        * here, it means success.  However, if we jump directly to this
9178 +        * label from any error above, then an error occurred after we
9179 +        * grabbed various refcnts, and so we have to release the
9180 +        * temporarily constructed structures.
9181 +        */
9182 +out_release:
9183 +       /* no need to cleanup/release anything in tmp_data */
9184 +       if (tmp_lower_paths)
9185 +               for (i = 0; i < new_branches; i++)
9186 +                       path_put(&tmp_lower_paths[i]);
9187 +out_free:
9188 +       kfree(tmp_lower_paths);
9189 +       kfree(tmp_data);
9190 +       kfree(new_lower_paths);
9191 +       kfree(new_data);
9192 +       kfree(new_lower_inodes);
9193 +out_error:
9194 +       unionfs_check_dentry(sb->s_root);
9195 +       unionfs_write_unlock(sb);
9196 +       return err;
9197 +}
9198 +
9199 +/*
9200 + * Called by iput() when the inode reference count reached zero
9201 + * and the inode is not hashed anywhere.  Used to clear anything
9202 + * that needs to be, before the inode is completely destroyed and put
9203 + * on the inode free list.
9204 + *
9205 + * No need to lock sb info's rwsem.
9206 + */
9207 +static void unionfs_clear_inode(struct inode *inode)
9208 +{
9209 +       int bindex, bstart, bend;
9210 +       struct inode *lower_inode;
9211 +       struct list_head *pos, *n;
9212 +       struct unionfs_dir_state *rdstate;
9213 +
9214 +       list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9215 +               rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9216 +               list_del(&rdstate->cache);
9217 +               free_rdstate(rdstate);
9218 +       }
9219 +
9220 +       /*
9221 +        * Decrement a reference to a lower_inode, which was incremented
9222 +        * by our read_inode when it was created initially.
9223 +        */
9224 +       bstart = ibstart(inode);
9225 +       bend = ibend(inode);
9226 +       if (bstart >= 0) {
9227 +               for (bindex = bstart; bindex <= bend; bindex++) {
9228 +                       lower_inode = unionfs_lower_inode_idx(inode, bindex);
9229 +                       if (!lower_inode)
9230 +                               continue;
9231 +                       unionfs_set_lower_inode_idx(inode, bindex, NULL);
9232 +                       /* see Documentation/filesystems/unionfs/issues.txt */
9233 +                       lockdep_off();
9234 +                       iput(lower_inode);
9235 +                       lockdep_on();
9236 +               }
9237 +       }
9238 +
9239 +       kfree(UNIONFS_I(inode)->lower_inodes);
9240 +       UNIONFS_I(inode)->lower_inodes = NULL;
9241 +}
9242 +
9243 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9244 +{
9245 +       struct unionfs_inode_info *i;
9246 +
9247 +       i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9248 +       if (unlikely(!i))
9249 +               return NULL;
9250 +
9251 +       /* memset everything up to the inode to 0 */
9252 +       memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9253 +
9254 +       i->vfs_inode.i_version = 1;
9255 +       return &i->vfs_inode;
9256 +}
9257 +
9258 +static void unionfs_destroy_inode(struct inode *inode)
9259 +{
9260 +       kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9261 +}
9262 +
9263 +/* unionfs inode cache constructor */
9264 +static void init_once(void *obj)
9265 +{
9266 +       struct unionfs_inode_info *i = obj;
9267 +
9268 +       inode_init_once(&i->vfs_inode);
9269 +}
9270 +
9271 +int unionfs_init_inode_cache(void)
9272 +{
9273 +       int err = 0;
9274 +
9275 +       unionfs_inode_cachep =
9276 +               kmem_cache_create("unionfs_inode_cache",
9277 +                                 sizeof(struct unionfs_inode_info), 0,
9278 +                                 SLAB_RECLAIM_ACCOUNT, init_once);
9279 +       if (unlikely(!unionfs_inode_cachep))
9280 +               err = -ENOMEM;
9281 +       return err;
9282 +}
9283 +
9284 +/* unionfs inode cache destructor */
9285 +void unionfs_destroy_inode_cache(void)
9286 +{
9287 +       if (unionfs_inode_cachep)
9288 +               kmem_cache_destroy(unionfs_inode_cachep);
9289 +}
9290 +
9291 +/*
9292 + * Called when we have a dirty inode, right here we only throw out
9293 + * parts of our readdir list that are too old.
9294 + *
9295 + * No need to grab sb info's rwsem.
9296 + */
9297 +static int unionfs_write_inode(struct inode *inode, int sync)
9298 +{
9299 +       struct list_head *pos, *n;
9300 +       struct unionfs_dir_state *rdstate;
9301 +
9302 +       spin_lock(&UNIONFS_I(inode)->rdlock);
9303 +       list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9304 +               rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9305 +               /* We keep this list in LRU order. */
9306 +               if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9307 +                       break;
9308 +               UNIONFS_I(inode)->rdcount--;
9309 +               list_del(&rdstate->cache);
9310 +               free_rdstate(rdstate);
9311 +       }
9312 +       spin_unlock(&UNIONFS_I(inode)->rdlock);
9313 +
9314 +       return 0;
9315 +}
9316 +
9317 +/*
9318 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9319 + * code can actually succeed and won't leave tasks that need handling.
9320 + */
9321 +static void unionfs_umount_begin(struct super_block *sb)
9322 +{
9323 +       struct super_block *lower_sb;
9324 +       int bindex, bstart, bend;
9325 +
9326 +       unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9327 +
9328 +       bstart = sbstart(sb);
9329 +       bend = sbend(sb);
9330 +       for (bindex = bstart; bindex <= bend; bindex++) {
9331 +               lower_sb = unionfs_lower_super_idx(sb, bindex);
9332 +
9333 +               if (lower_sb && lower_sb->s_op &&
9334 +                   lower_sb->s_op->umount_begin)
9335 +                       lower_sb->s_op->umount_begin(lower_sb);
9336 +       }
9337 +
9338 +       unionfs_read_unlock(sb);
9339 +}
9340 +
9341 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9342 +{
9343 +       struct super_block *sb = mnt->mnt_sb;
9344 +       int ret = 0;
9345 +       char *tmp_page;
9346 +       char *path;
9347 +       int bindex, bstart, bend;
9348 +       int perms;
9349 +
9350 +       unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9351 +
9352 +       unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9353 +
9354 +       tmp_page = (char *) __get_free_page(GFP_KERNEL);
9355 +       if (unlikely(!tmp_page)) {
9356 +               ret = -ENOMEM;
9357 +               goto out;
9358 +       }
9359 +
9360 +       bstart = sbstart(sb);
9361 +       bend = sbend(sb);
9362 +
9363 +       seq_printf(m, ",dirs=");
9364 +       for (bindex = bstart; bindex <= bend; bindex++) {
9365 +               struct path p;
9366 +               p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9367 +               p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9368 +               path = d_path(&p, tmp_page, PAGE_SIZE);
9369 +               if (IS_ERR(path)) {
9370 +                       ret = PTR_ERR(path);
9371 +                       goto out;
9372 +               }
9373 +
9374 +               perms = branchperms(sb, bindex);
9375 +
9376 +               seq_printf(m, "%s=%s", path,
9377 +                          perms & MAY_WRITE ? "rw" : "ro");
9378 +               if (bindex != bend)
9379 +                       seq_printf(m, ":");
9380 +       }
9381 +
9382 +out:
9383 +       free_page((unsigned long) tmp_page);
9384 +
9385 +       unionfs_unlock_dentry(sb->s_root);
9386 +
9387 +       unionfs_read_unlock(sb);
9388 +
9389 +       return ret;
9390 +}
9391 +
9392 +struct super_operations unionfs_sops = {
9393 +       .delete_inode   = unionfs_delete_inode,
9394 +       .put_super      = unionfs_put_super,
9395 +       .statfs         = unionfs_statfs,
9396 +       .remount_fs     = unionfs_remount_fs,
9397 +       .clear_inode    = unionfs_clear_inode,
9398 +       .umount_begin   = unionfs_umount_begin,
9399 +       .show_options   = unionfs_show_options,
9400 +       .write_inode    = unionfs_write_inode,
9401 +       .alloc_inode    = unionfs_alloc_inode,
9402 +       .destroy_inode  = unionfs_destroy_inode,
9403 +};
9404 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/union.h linux-2.6.35-rc6-git-unionfs//fs/unionfs/union.h
9405 --- linux-2.6.35-rc6-git//fs/unionfs/union.h    1970-01-01 02:00:00.000000000 +0200
9406 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/union.h    2010-07-30 15:50:50.563010175 +0300
9407 @@ -0,0 +1,670 @@
9408 +/*
9409 + * Copyright (c) 2003-2010 Erez Zadok
9410 + * Copyright (c) 2003-2006 Charles P. Wright
9411 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9412 + * Copyright (c) 2005      Arun M. Krishnakumar
9413 + * Copyright (c) 2004-2006 David P. Quigley
9414 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9415 + * Copyright (c) 2003      Puja Gupta
9416 + * Copyright (c) 2003      Harikesavan Krishnan
9417 + * Copyright (c) 2003-2010 Stony Brook University
9418 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
9419 + *
9420 + * This program is free software; you can redistribute it and/or modify
9421 + * it under the terms of the GNU General Public License version 2 as
9422 + * published by the Free Software Foundation.
9423 + */
9424 +
9425 +#ifndef _UNION_H_
9426 +#define _UNION_H_
9427 +
9428 +#include <linux/dcache.h>
9429 +#include <linux/file.h>
9430 +#include <linux/list.h>
9431 +#include <linux/fs.h>
9432 +#include <linux/mm.h>
9433 +#include <linux/module.h>
9434 +#include <linux/mount.h>
9435 +#include <linux/namei.h>
9436 +#include <linux/page-flags.h>
9437 +#include <linux/pagemap.h>
9438 +#include <linux/poll.h>
9439 +#include <linux/security.h>
9440 +#include <linux/seq_file.h>
9441 +#include <linux/slab.h>
9442 +#include <linux/spinlock.h>
9443 +#include <linux/smp_lock.h>
9444 +#include <linux/statfs.h>
9445 +#include <linux/string.h>
9446 +#include <linux/vmalloc.h>
9447 +#include <linux/writeback.h>
9448 +#include <linux/buffer_head.h>
9449 +#include <linux/xattr.h>
9450 +#include <linux/fs_stack.h>
9451 +#include <linux/magic.h>
9452 +#include <linux/log2.h>
9453 +#include <linux/poison.h>
9454 +#include <linux/mman.h>
9455 +#include <linux/backing-dev.h>
9456 +#include <linux/splice.h>
9457 +
9458 +#include <asm/system.h>
9459 +
9460 +#include <linux/union_fs.h>
9461 +
9462 +/* the file system name */
9463 +#define UNIONFS_NAME "unionfs"
9464 +
9465 +/* unionfs root inode number */
9466 +#define UNIONFS_ROOT_INO     1
9467 +
9468 +/* number of times we try to get a unique temporary file name */
9469 +#define GET_TMPNAM_MAX_RETRY   5
9470 +
9471 +/* maximum number of branches we support, to avoid memory blowup */
9472 +#define UNIONFS_MAX_BRANCHES   128
9473 +
9474 +/* minimum time (seconds) required for time-based cache-coherency */
9475 +#define UNIONFS_MIN_CC_TIME    3
9476 +
9477 +/* Operations vectors defined in specific files. */
9478 +extern struct file_operations unionfs_main_fops;
9479 +extern struct file_operations unionfs_dir_fops;
9480 +extern struct inode_operations unionfs_main_iops;
9481 +extern struct inode_operations unionfs_dir_iops;
9482 +extern struct inode_operations unionfs_symlink_iops;
9483 +extern struct super_operations unionfs_sops;
9484 +extern struct dentry_operations unionfs_dops;
9485 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9486 +extern struct vm_operations_struct unionfs_vm_ops;
9487 +
9488 +/* How long should an entry be allowed to persist */
9489 +#define RDCACHE_JIFFIES        (5*HZ)
9490 +
9491 +/* compatibility with Real-Time patches */
9492 +#ifdef CONFIG_PREEMPT_RT
9493 +# define unionfs_rw_semaphore  compat_rw_semaphore
9494 +#else /* not CONFIG_PREEMPT_RT */
9495 +# define unionfs_rw_semaphore  rw_semaphore
9496 +#endif /* not CONFIG_PREEMPT_RT */
9497 +
9498 +/* file private data. */
9499 +struct unionfs_file_info {
9500 +       int bstart;
9501 +       int bend;
9502 +       atomic_t generation;
9503 +
9504 +       struct unionfs_dir_state *rdstate;
9505 +       struct file **lower_files;
9506 +       int *saved_branch_ids; /* IDs of branches when file was opened */
9507 +       const struct vm_operations_struct *lower_vm_ops;
9508 +       bool wrote_to_file;     /* for delayed copyup */
9509 +};
9510 +
9511 +/* unionfs inode data in memory */
9512 +struct unionfs_inode_info {
9513 +       int bstart;
9514 +       int bend;
9515 +       atomic_t generation;
9516 +       /* Stuff for readdir over NFS. */
9517 +       spinlock_t rdlock;
9518 +       struct list_head readdircache;
9519 +       int rdcount;
9520 +       int hashsize;
9521 +       int cookie;
9522 +
9523 +       /* The lower inodes */
9524 +       struct inode **lower_inodes;
9525 +
9526 +       struct inode vfs_inode;
9527 +};
9528 +
9529 +/* unionfs dentry data in memory */
9530 +struct unionfs_dentry_info {
9531 +       /*
9532 +        * The semaphore is used to lock the dentry as soon as we get into a
9533 +        * unionfs function from the VFS.  Our lock ordering is that children
9534 +        * go before their parents.
9535 +        */
9536 +       struct mutex lock;
9537 +       int bstart;
9538 +       int bend;
9539 +       int bopaque;
9540 +       int bcount;
9541 +       atomic_t generation;
9542 +       struct path *lower_paths;
9543 +};
9544 +
9545 +/* These are the pointers to our various objects. */
9546 +struct unionfs_data {
9547 +       struct super_block *sb; /* lower super_block */
9548 +       atomic_t open_files;    /* number of open files on branch */
9549 +       int branchperms;
9550 +       int branch_id;          /* unique branch ID at re/mount time */
9551 +};
9552 +
9553 +/* unionfs super-block data in memory */
9554 +struct unionfs_sb_info {
9555 +       int bend;
9556 +
9557 +       atomic_t generation;
9558 +
9559 +       /*
9560 +        * This rwsem is used to make sure that a branch management
9561 +        * operation...
9562 +        *   1) will not begin before all currently in-flight operations
9563 +        *      complete.
9564 +        *   2) any new operations do not execute until the currently
9565 +        *      running branch management operation completes.
9566 +        *
9567 +        * The write_lock_owner records the PID of the task which grabbed
9568 +        * the rw_sem for writing.  If the same task also tries to grab the
9569 +        * read lock, we allow it.  This prevents a self-deadlock when
9570 +        * branch-management is used on a pivot_root'ed union, because we
9571 +        * have to ->lookup paths which belong to the same union.
9572 +        */
9573 +       struct unionfs_rw_semaphore rwsem;
9574 +       pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9575 +       int high_branch_id;     /* last unique branch ID given */
9576 +       char *dev_name;         /* to identify different unions in pr_debug */
9577 +       struct unionfs_data *data;
9578 +};
9579 +
9580 +/*
9581 + * structure for making the linked list of entries by readdir on left branch
9582 + * to compare with entries on right branch
9583 + */
9584 +struct filldir_node {
9585 +       struct list_head file_list;     /* list for directory entries */
9586 +       char *name;             /* name entry */
9587 +       int hash;               /* name hash */
9588 +       int namelen;            /* name len since name is not 0 terminated */
9589 +
9590 +       /*
9591 +        * we can check for duplicate whiteouts and files in the same branch
9592 +        * in order to return -EIO.
9593 +        */
9594 +       int bindex;
9595 +
9596 +       /* is this a whiteout entry? */
9597 +       int whiteout;
9598 +
9599 +       /* Inline name, so we don't need to separately kmalloc small ones */
9600 +       char iname[DNAME_INLINE_LEN_MIN];
9601 +};
9602 +
9603 +/* Directory hash table. */
9604 +struct unionfs_dir_state {
9605 +       unsigned int cookie;    /* the cookie, based off of rdversion */
9606 +       unsigned int offset;    /* The entry we have returned. */
9607 +       int bindex;
9608 +       loff_t dirpos;          /* offset within the lower level directory */
9609 +       int size;               /* How big is the hash table? */
9610 +       int hashentries;        /* How many entries have been inserted? */
9611 +       unsigned long access;
9612 +
9613 +       /* This cache list is used when the inode keeps us around. */
9614 +       struct list_head cache;
9615 +       struct list_head list[0];
9616 +};
9617 +
9618 +/* externs needed for fanout.h or sioq.h */
9619 +extern int unionfs_get_nlinks(const struct inode *inode);
9620 +extern void unionfs_copy_attr_times(struct inode *upper);
9621 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9622 +
9623 +/* include miscellaneous macros */
9624 +#include "fanout.h"
9625 +#include "sioq.h"
9626 +
9627 +/* externs for cache creation/deletion routines */
9628 +extern void unionfs_destroy_filldir_cache(void);
9629 +extern int unionfs_init_filldir_cache(void);
9630 +extern int unionfs_init_inode_cache(void);
9631 +extern void unionfs_destroy_inode_cache(void);
9632 +extern int unionfs_init_dentry_cache(void);
9633 +extern void unionfs_destroy_dentry_cache(void);
9634 +
9635 +/* Initialize and free readdir-specific  state. */
9636 +extern int init_rdstate(struct file *file);
9637 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9638 +                                              int bindex);
9639 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9640 +                                             loff_t fpos);
9641 +extern void free_rdstate(struct unionfs_dir_state *state);
9642 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9643 +                           const char *name, int namelen, int bindex,
9644 +                           int whiteout);
9645 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9646 +                                             const char *name, int namelen,
9647 +                                             int is_whiteout);
9648 +
9649 +extern struct dentry **alloc_new_dentries(int objs);
9650 +extern struct unionfs_data *alloc_new_data(int objs);
9651 +
9652 +/* We can only use 32-bits of offset for rdstate --- blech! */
9653 +#define DIREOF (0xfffff)
9654 +#define RDOFFBITS 20           /* This is the number of bits in DIREOF. */
9655 +#define MAXRDCOOKIE (0xfff)
9656 +/* Turn an rdstate into an offset. */
9657 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9658 +{
9659 +       off_t tmp;
9660 +
9661 +       tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9662 +               | (buf->offset & DIREOF);
9663 +       return tmp;
9664 +}
9665 +
9666 +/* Macros for locking a super_block. */
9667 +enum unionfs_super_lock_class {
9668 +       UNIONFS_SMUTEX_NORMAL,
9669 +       UNIONFS_SMUTEX_PARENT,  /* when locking on behalf of file */
9670 +       UNIONFS_SMUTEX_CHILD,   /* when locking on behalf of dentry */
9671 +};
9672 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9673 +{
9674 +       if (UNIONFS_SB(sb)->write_lock_owner &&
9675 +           UNIONFS_SB(sb)->write_lock_owner == current->pid)
9676 +               return;
9677 +       down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9678 +}
9679 +static inline void unionfs_read_unlock(struct super_block *sb)
9680 +{
9681 +       if (UNIONFS_SB(sb)->write_lock_owner &&
9682 +           UNIONFS_SB(sb)->write_lock_owner == current->pid)
9683 +               return;
9684 +       up_read(&UNIONFS_SB(sb)->rwsem);
9685 +}
9686 +static inline void unionfs_write_lock(struct super_block *sb)
9687 +{
9688 +       down_write(&UNIONFS_SB(sb)->rwsem);
9689 +       UNIONFS_SB(sb)->write_lock_owner = current->pid;
9690 +}
9691 +static inline void unionfs_write_unlock(struct super_block *sb)
9692 +{
9693 +       up_write(&UNIONFS_SB(sb)->rwsem);
9694 +       UNIONFS_SB(sb)->write_lock_owner = 0;
9695 +}
9696 +
9697 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9698 +                                             struct dentry *d2)
9699 +{
9700 +       BUG_ON(d1 == d2);
9701 +       if (d1 < d2) {
9702 +               unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9703 +               unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9704 +       } else {
9705 +               unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9706 +               unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9707 +       }
9708 +}
9709 +
9710 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9711 +                                               struct dentry *d2)
9712 +{
9713 +       BUG_ON(d1 == d2);
9714 +       if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9715 +               unionfs_unlock_dentry(d1);
9716 +               unionfs_unlock_dentry(d2);
9717 +       } else {
9718 +               unionfs_unlock_dentry(d2);
9719 +               unionfs_unlock_dentry(d1);
9720 +       }
9721 +}
9722 +
9723 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9724 +                                              struct dentry *p2)
9725 +{
9726 +       if (p1 == p2) {
9727 +               unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9728 +               return;
9729 +       }
9730 +       if (p1 < p2) {
9731 +               unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9732 +               unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9733 +       } else {
9734 +               unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9735 +               unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9736 +       }
9737 +}
9738 +
9739 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9740 +                                                struct dentry *p2)
9741 +{
9742 +       if (p1 == p2) {
9743 +               unionfs_unlock_dentry(p1);
9744 +               return;
9745 +       }
9746 +       if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9747 +               unionfs_unlock_dentry(p1);
9748 +               unionfs_unlock_dentry(p2);
9749 +       } else {
9750 +               unionfs_unlock_dentry(p2);
9751 +               unionfs_unlock_dentry(p1);
9752 +       }
9753 +}
9754 +
9755 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9756 +extern int realloc_dentry_private_data(struct dentry *dentry);
9757 +extern void free_dentry_private_data(struct dentry *dentry);
9758 +extern void update_bstart(struct dentry *dentry);
9759 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9760 +extern void release_lower_nd(struct nameidata *nd, int err);
9761 +
9762 +/*
9763 + * EXTERNALS:
9764 + */
9765 +
9766 +/* replicates the directory structure up to given dentry in given branch */
9767 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9768 +                                    const char *name, int bindex);
9769 +
9770 +/* partial lookup */
9771 +extern int unionfs_partial_lookup(struct dentry *dentry,
9772 +                                 struct dentry *parent);
9773 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9774 +                                         struct dentry *parent,
9775 +                                         int lookupmode);
9776 +
9777 +/* copies a file from dbstart to newbindex branch */
9778 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9779 +                      int newbindex, loff_t size);
9780 +extern int copyup_named_file(struct inode *dir, struct file *file,
9781 +                            char *name, int bstart, int new_bindex,
9782 +                            loff_t len);
9783 +/* copies a dentry from dbstart to newbindex branch */
9784 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9785 +                        int bstart, int new_bindex, const char *name,
9786 +                        int namelen, struct file **copyup_file, loff_t len);
9787 +/* helper functions for post-copyup actions */
9788 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9789 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9790 +
9791 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9792 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9793 +                      struct unionfs_dir_state **namelist);
9794 +/* whiteout and opaque directory helpers */
9795 +extern char *alloc_whname(const char *name, int len);
9796 +extern bool is_whiteout_name(char **namep, int *namelenp);
9797 +extern bool is_validname(const char *name);
9798 +extern struct dentry *lookup_whiteout(const char *name,
9799 +                                     struct dentry *lower_parent);
9800 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9801 +extern int unlink_whiteout(struct dentry *wh_dentry);
9802 +extern int check_unlink_whiteout(struct dentry *dentry,
9803 +                                struct dentry *lower_dentry, int bindex);
9804 +extern int create_whiteout(struct dentry *dentry, int start);
9805 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9806 +                           struct unionfs_dir_state *namelist);
9807 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9808 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9809 +extern void unionfs_set_max_namelen(long *namelen);
9810 +
9811 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9812 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9813 +
9814 +/* Locking functions. */
9815 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9816 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9817 +
9818 +/* Common file operations. */
9819 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9820 +                                  bool willwrite);
9821 +extern int unionfs_open(struct inode *inode, struct file *file);
9822 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9823 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9824 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9825 +                         unsigned long arg);
9826 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
9827 +                        int datasync);
9828 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9829 +
9830 +/* Inode operations */
9831 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9832 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9833 +                         struct inode *new_dir, struct dentry *new_dentry);
9834 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
9835 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
9836 +
9837 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
9838 +                                  struct dentry *parent, bool willwrite);
9839 +extern bool is_negative_lower(const struct dentry *dentry);
9840 +extern bool is_newer_lower(const struct dentry *dentry);
9841 +extern void purge_sb_data(struct super_block *sb);
9842 +
9843 +/* The values for unionfs_interpose's flag. */
9844 +#define INTERPOSE_DEFAULT      0
9845 +#define INTERPOSE_LOOKUP       1
9846 +#define INTERPOSE_REVAL                2
9847 +#define INTERPOSE_REVAL_NEG    3
9848 +#define INTERPOSE_PARTIAL      4
9849 +
9850 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9851 +                                       struct super_block *sb, int flag);
9852 +
9853 +#ifdef CONFIG_UNION_FS_XATTR
9854 +/* Extended attribute functions. */
9855 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
9856 +static inline void unionfs_xattr_kfree(const void *p)
9857 +{
9858 +       kfree(p);
9859 +}
9860 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
9861 +                               void *value, size_t size);
9862 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
9863 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
9864 +                                size_t size);
9865 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
9866 +                           const void *value, size_t size, int flags);
9867 +#endif /* CONFIG_UNION_FS_XATTR */
9868 +
9869 +/* The root directory is unhashed, but isn't deleted. */
9870 +static inline int d_deleted(struct dentry *d)
9871 +{
9872 +       return d_unhashed(d) && (d != d->d_sb->s_root);
9873 +}
9874 +
9875 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
9876 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
9877 +
9878 +/* unionfs_open, check if we need to copyup the file */
9879 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
9880 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
9881 +
9882 +static inline int branchperms(const struct super_block *sb, int index)
9883 +{
9884 +       BUG_ON(index < 0);
9885 +       return UNIONFS_SB(sb)->data[index].branchperms;
9886 +}
9887 +
9888 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
9889 +{
9890 +       BUG_ON(index < 0);
9891 +       UNIONFS_SB(sb)->data[index].branchperms = perms;
9892 +       return perms;
9893 +}
9894 +
9895 +/* check if readonly lower inode, but possibly unlinked (no inode->i_sb) */
9896 +static inline int __is_rdonly(const struct inode *inode)
9897 +{
9898 +       /* if unlinked, can't be readonly (?) */
9899 +       if (!inode->i_sb)
9900 +               return 0;
9901 +       return IS_RDONLY(inode);
9902 +
9903 +}
9904 +/* Is this file on a read-only branch? */
9905 +static inline int is_robranch_super(const struct super_block *sb, int index)
9906 +{
9907 +       int ret;
9908 +
9909 +       ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
9910 +       return ret;
9911 +}
9912 +
9913 +/* Is this file on a read-only branch? */
9914 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
9915 +{
9916 +       struct super_block *lower_sb;
9917 +
9918 +       BUG_ON(index < 0);
9919 +
9920 +       if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
9921 +               return -EROFS;
9922 +
9923 +       lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
9924 +       BUG_ON(lower_sb == NULL);
9925 +       /*
9926 +        * test sb flags directly, not IS_RDONLY(lower_inode) because the
9927 +        * lower_dentry could be a negative.
9928 +        */
9929 +       if (lower_sb->s_flags & MS_RDONLY)
9930 +               return -EROFS;
9931 +
9932 +       return 0;
9933 +}
9934 +
9935 +static inline int is_robranch(const struct dentry *dentry)
9936 +{
9937 +       int index;
9938 +
9939 +       index = UNIONFS_D(dentry)->bstart;
9940 +       BUG_ON(index < 0);
9941 +
9942 +       return is_robranch_idx(dentry, index);
9943 +}
9944 +
9945 +/*
9946 + * EXTERNALS:
9947 + */
9948 +extern int check_branch(struct nameidata *nd);
9949 +extern int parse_branch_mode(const char *name, int *perms);
9950 +
9951 +/* locking helpers */
9952 +static inline struct dentry *lock_parent(struct dentry *dentry)
9953 +{
9954 +       struct dentry *dir = dget_parent(dentry);
9955 +       mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
9956 +       return dir;
9957 +}
9958 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
9959 +{
9960 +       struct dentry *dir = dget_parent(dentry);
9961 +
9962 +       mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
9963 +       return dir;
9964 +}
9965 +
9966 +static inline void unlock_dir(struct dentry *dir)
9967 +{
9968 +       mutex_unlock(&dir->d_inode->i_mutex);
9969 +       dput(dir);
9970 +}
9971 +
9972 +/* lock base inode mutex before calling lookup_one_len */
9973 +static inline struct dentry *lookup_lck_len(const char *name,
9974 +                                           struct dentry *base, int len)
9975 +{
9976 +       struct dentry *d;
9977 +       mutex_lock(&base->d_inode->i_mutex);
9978 +       d = lookup_one_len(name, base, len);
9979 +       mutex_unlock(&base->d_inode->i_mutex);
9980 +       return d;
9981 +}
9982 +
9983 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
9984 +                                             int bindex)
9985 +{
9986 +       struct vfsmount *mnt;
9987 +
9988 +       BUG_ON(!dentry || bindex < 0);
9989 +
9990 +       mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
9991 +#ifdef CONFIG_UNION_FS_DEBUG
9992 +       if (!mnt)
9993 +               pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
9994 +                        mnt, bindex);
9995 +#endif /* CONFIG_UNION_FS_DEBUG */
9996 +
9997 +       return mnt;
9998 +}
9999 +
10000 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10001 +{
10002 +       struct vfsmount *mnt;
10003 +
10004 +       if (!dentry && bindex < 0)
10005 +               return;
10006 +       BUG_ON(!dentry || bindex < 0);
10007 +
10008 +       mnt = unionfs_lower_mnt_idx(dentry, bindex);
10009 +#ifdef CONFIG_UNION_FS_DEBUG
10010 +       /*
10011 +        * Directories can have NULL lower objects in between start/end, but
10012 +        * NOT if at the start/end range.  We cannot verify that this dentry
10013 +        * is a type=DIR, because it may already be a negative dentry.  But
10014 +        * if dbstart is greater than dbend, we know that this couldn't have
10015 +        * been a regular file: it had to have been a directory.
10016 +        */
10017 +       if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10018 +               pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10019 +#endif /* CONFIG_UNION_FS_DEBUG */
10020 +       mntput(mnt);
10021 +}
10022 +
10023 +#ifdef CONFIG_UNION_FS_DEBUG
10024 +
10025 +/* useful for tracking code reachability */
10026 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10027 +
10028 +#define unionfs_check_inode(i) __unionfs_check_inode((i),      \
10029 +       __FILE__, __func__, __LINE__)
10030 +#define unionfs_check_dentry(d)        __unionfs_check_dentry((d),     \
10031 +       __FILE__, __func__, __LINE__)
10032 +#define unionfs_check_file(f)  __unionfs_check_file((f),       \
10033 +       __FILE__, __func__, __LINE__)
10034 +#define unionfs_check_nd(n)    __unionfs_check_nd((n),         \
10035 +       __FILE__, __func__, __LINE__)
10036 +#define show_branch_counts(sb) __show_branch_counts((sb),      \
10037 +       __FILE__, __func__, __LINE__)
10038 +#define show_inode_times(i)    __show_inode_times((i),         \
10039 +       __FILE__, __func__, __LINE__)
10040 +#define show_dinode_times(d)   __show_dinode_times((d),        \
10041 +       __FILE__, __func__, __LINE__)
10042 +#define show_inode_counts(i)   __show_inode_counts((i),        \
10043 +       __FILE__, __func__, __LINE__)
10044 +
10045 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10046 +                                 const char *fxn, int line);
10047 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10048 +                                  const char *fname, const char *fxn,
10049 +                                  int line);
10050 +extern void __unionfs_check_file(const struct file *file,
10051 +                                const char *fname, const char *fxn, int line);
10052 +extern void __unionfs_check_nd(const struct nameidata *nd,
10053 +                              const char *fname, const char *fxn, int line);
10054 +extern void __show_branch_counts(const struct super_block *sb,
10055 +                                const char *file, const char *fxn, int line);
10056 +extern void __show_inode_times(const struct inode *inode,
10057 +                              const char *file, const char *fxn, int line);
10058 +extern void __show_dinode_times(const struct dentry *dentry,
10059 +                               const char *file, const char *fxn, int line);
10060 +extern void __show_inode_counts(const struct inode *inode,
10061 +                               const char *file, const char *fxn, int line);
10062 +
10063 +#else /* not CONFIG_UNION_FS_DEBUG */
10064 +
10065 +/* we leave useful hooks for these check functions throughout the code */
10066 +#define unionfs_check_inode(i)         do { } while (0)
10067 +#define unionfs_check_dentry(d)                do { } while (0)
10068 +#define unionfs_check_file(f)          do { } while (0)
10069 +#define unionfs_check_nd(n)            do { } while (0)
10070 +#define show_branch_counts(sb)         do { } while (0)
10071 +#define show_inode_times(i)            do { } while (0)
10072 +#define show_dinode_times(d)           do { } while (0)
10073 +#define show_inode_counts(i)           do { } while (0)
10074 +
10075 +#endif /* not CONFIG_UNION_FS_DEBUG */
10076 +
10077 +#endif /* not _UNION_H_ */
10078 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/unlink.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/unlink.c
10079 --- linux-2.6.35-rc6-git//fs/unionfs/unlink.c   1970-01-01 02:00:00.000000000 +0200
10080 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/unlink.c   2010-07-30 15:50:50.563010175 +0300
10081 @@ -0,0 +1,278 @@
10082 +/*
10083 + * Copyright (c) 2003-2010 Erez Zadok
10084 + * Copyright (c) 2003-2006 Charles P. Wright
10085 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10086 + * Copyright (c) 2005-2006 Junjiro Okajima
10087 + * Copyright (c) 2005      Arun M. Krishnakumar
10088 + * Copyright (c) 2004-2006 David P. Quigley
10089 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10090 + * Copyright (c) 2003      Puja Gupta
10091 + * Copyright (c) 2003      Harikesavan Krishnan
10092 + * Copyright (c) 2003-2010 Stony Brook University
10093 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
10094 + *
10095 + * This program is free software; you can redistribute it and/or modify
10096 + * it under the terms of the GNU General Public License version 2 as
10097 + * published by the Free Software Foundation.
10098 + */
10099 +
10100 +#include "union.h"
10101 +
10102 +/*
10103 + * Helper function for Unionfs's unlink operation.
10104 + *
10105 + * The main goal of this function is to optimize the unlinking of non-dir
10106 + * objects in unionfs by deleting all possible lower inode objects from the
10107 + * underlying branches having same dentry name as the non-dir dentry on
10108 + * which this unlink operation is called.  This way we delete as many lower
10109 + * inodes as possible, and save space.  Whiteouts need to be created in
10110 + * branch0 only if unlinking fails on any of the lower branch other than
10111 + * branch0, or if a lower branch is marked read-only.
10112 + *
10113 + * Also, while unlinking a file, if we encounter any dir type entry in any
10114 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10115 + * The following special cases are also handled:
10116 +
10117 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10118 + *     appropriate error.
10119 + *
10120 + * (2) If we get an error during unlink in any of other lower branch other
10121 + *     than branch0, then we create a whiteout in branch0.
10122 + *
10123 + * (3) If a whiteout already exists in any intermediate branch, we delete
10124 + *     all possible inodes only up to that branch (this is an "opaqueness"
10125 + *     as as per Documentation/filesystems/unionfs/concepts.txt).
10126 + *
10127 + */
10128 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10129 +                                  struct dentry *parent)
10130 +{
10131 +       struct dentry *lower_dentry;
10132 +       struct dentry *lower_dir_dentry;
10133 +       int bindex;
10134 +       int err = 0;
10135 +
10136 +       err = unionfs_partial_lookup(dentry, parent);
10137 +       if (err)
10138 +               goto out;
10139 +
10140 +       /* trying to unlink all possible valid instances */
10141 +       for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10142 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10143 +               if (!lower_dentry || !lower_dentry->d_inode)
10144 +                       continue;
10145 +
10146 +               lower_dir_dentry = lock_parent(lower_dentry);
10147 +
10148 +               /* avoid destroying the lower inode if the object is in use */
10149 +               dget(lower_dentry);
10150 +               err = is_robranch_super(dentry->d_sb, bindex);
10151 +               if (!err) {
10152 +                       /* see Documentation/filesystems/unionfs/issues.txt */
10153 +                       lockdep_off();
10154 +                       if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10155 +                               err = vfs_unlink(lower_dir_dentry->d_inode,
10156 +                                                               lower_dentry);
10157 +                       else
10158 +                               err = vfs_rmdir(lower_dir_dentry->d_inode,
10159 +                                                               lower_dentry);
10160 +                       lockdep_on();
10161 +               }
10162 +
10163 +               /* if lower object deletion succeeds, update inode's times */
10164 +               if (!err)
10165 +                       unionfs_copy_attr_times(dentry->d_inode);
10166 +               dput(lower_dentry);
10167 +               fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10168 +               unlock_dir(lower_dir_dentry);
10169 +
10170 +               if (err)
10171 +                       break;
10172 +       }
10173 +
10174 +       /*
10175 +        * Create the whiteout in branch 0 (highest priority) only if (a)
10176 +        * there was an error in any intermediate branch other than branch 0
10177 +        * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10178 +        * mounted read-only.
10179 +        */
10180 +       if (err) {
10181 +               if ((bindex == 0) ||
10182 +                   ((bindex == dbstart(dentry)) &&
10183 +                    (!IS_COPYUP_ERR(err))))
10184 +                       goto out;
10185 +               else {
10186 +                       if (!IS_COPYUP_ERR(err))
10187 +                               pr_debug("unionfs: lower object deletion "
10188 +                                            "failed in branch:%d\n", bindex);
10189 +                       err = create_whiteout(dentry, sbstart(dentry->d_sb));
10190 +               }
10191 +       }
10192 +
10193 +out:
10194 +       if (!err)
10195 +               inode_dec_link_count(dentry->d_inode);
10196 +
10197 +       /* We don't want to leave negative leftover dentries for revalidate. */
10198 +       if (!err && (dbopaque(dentry) != -1))
10199 +               update_bstart(dentry);
10200 +
10201 +       return err;
10202 +}
10203 +
10204 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10205 +{
10206 +       int err = 0;
10207 +       struct inode *inode = dentry->d_inode;
10208 +       struct dentry *parent;
10209 +       int valid;
10210 +
10211 +       BUG_ON(S_ISDIR(inode->i_mode));
10212 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10213 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10214 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10215 +
10216 +       valid = __unionfs_d_revalidate(dentry, parent, false);
10217 +       if (unlikely(!valid)) {
10218 +               err = -ESTALE;
10219 +               goto out;
10220 +       }
10221 +       unionfs_check_dentry(dentry);
10222 +
10223 +       err = unionfs_unlink_whiteout(dir, dentry, parent);
10224 +       /* call d_drop so the system "forgets" about us */
10225 +       if (!err) {
10226 +               unionfs_postcopyup_release(dentry);
10227 +               unionfs_postcopyup_setmnt(parent);
10228 +               if (inode->i_nlink == 0) /* drop lower inodes */
10229 +                       iput_lowers_all(inode, false);
10230 +               d_drop(dentry);
10231 +               /*
10232 +                * if unlink/whiteout succeeded, parent dir mtime has
10233 +                * changed
10234 +                */
10235 +               unionfs_copy_attr_times(dir);
10236 +       }
10237 +
10238 +out:
10239 +       if (!err) {
10240 +               unionfs_check_dentry(dentry);
10241 +               unionfs_check_inode(dir);
10242 +       }
10243 +       unionfs_unlock_dentry(dentry);
10244 +       unionfs_unlock_parent(dentry, parent);
10245 +       unionfs_read_unlock(dentry->d_sb);
10246 +       return err;
10247 +}
10248 +
10249 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10250 +                              struct unionfs_dir_state *namelist)
10251 +{
10252 +       int err;
10253 +       struct dentry *lower_dentry;
10254 +       struct dentry *lower_dir_dentry = NULL;
10255 +
10256 +       /* Here we need to remove whiteout entries. */
10257 +       err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10258 +       if (err)
10259 +               goto out;
10260 +
10261 +       lower_dentry = unionfs_lower_dentry(dentry);
10262 +
10263 +       lower_dir_dentry = lock_parent(lower_dentry);
10264 +
10265 +       /* avoid destroying the lower inode if the file is in use */
10266 +       dget(lower_dentry);
10267 +       err = is_robranch(dentry);
10268 +       if (!err)
10269 +               err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10270 +       dput(lower_dentry);
10271 +
10272 +       fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10273 +       /* propagate number of hard-links */
10274 +       dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10275 +
10276 +out:
10277 +       if (lower_dir_dentry)
10278 +               unlock_dir(lower_dir_dentry);
10279 +       return err;
10280 +}
10281 +
10282 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10283 +{
10284 +       int err = 0;
10285 +       struct unionfs_dir_state *namelist = NULL;
10286 +       struct dentry *parent;
10287 +       int dstart, dend;
10288 +       bool valid;
10289 +
10290 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10291 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10292 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10293 +
10294 +       valid = __unionfs_d_revalidate(dentry, parent, false);
10295 +       if (unlikely(!valid)) {
10296 +               err = -ESTALE;
10297 +               goto out;
10298 +       }
10299 +       unionfs_check_dentry(dentry);
10300 +
10301 +       /* check if this unionfs directory is empty or not */
10302 +       err = check_empty(dentry, parent, &namelist);
10303 +       if (err)
10304 +               goto out;
10305 +
10306 +       err = unionfs_rmdir_first(dir, dentry, namelist);
10307 +       dstart = dbstart(dentry);
10308 +       dend = dbend(dentry);
10309 +       /*
10310 +        * We create a whiteout for the directory if there was an error to
10311 +        * rmdir the first directory entry in the union.  Otherwise, we
10312 +        * create a whiteout only if there is no chance that a lower
10313 +        * priority branch might also have the same named directory.  IOW,
10314 +        * if there is not another same-named directory at a lower priority
10315 +        * branch, then we don't need to create a whiteout for it.
10316 +        */
10317 +       if (!err) {
10318 +               if (dstart < dend)
10319 +                       err = create_whiteout(dentry, dstart);
10320 +       } else {
10321 +               int new_err;
10322 +
10323 +               if (dstart == 0)
10324 +                       goto out;
10325 +
10326 +               /* exit if the error returned was NOT -EROFS */
10327 +               if (!IS_COPYUP_ERR(err))
10328 +                       goto out;
10329 +
10330 +               new_err = create_whiteout(dentry, dstart - 1);
10331 +               if (new_err != -EEXIST)
10332 +                       err = new_err;
10333 +       }
10334 +
10335 +out:
10336 +       /*
10337 +        * Drop references to lower dentry/inode so storage space for them
10338 +        * can be reclaimed.  Then, call d_drop so the system "forgets"
10339 +        * about us.
10340 +        */
10341 +       if (!err) {
10342 +               iput_lowers_all(dentry->d_inode, false);
10343 +               dput(unionfs_lower_dentry_idx(dentry, dstart));
10344 +               unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10345 +               d_drop(dentry);
10346 +               /* update our lower vfsmnts, in case a copyup took place */
10347 +               unionfs_postcopyup_setmnt(dentry);
10348 +               unionfs_check_dentry(dentry);
10349 +               unionfs_check_inode(dir);
10350 +       }
10351 +
10352 +       if (namelist)
10353 +               free_rdstate(namelist);
10354 +
10355 +       unionfs_unlock_dentry(dentry);
10356 +       unionfs_unlock_parent(dentry, parent);
10357 +       unionfs_read_unlock(dentry->d_sb);
10358 +       return err;
10359 +}
10360 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/whiteout.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/whiteout.c
10361 --- linux-2.6.35-rc6-git//fs/unionfs/whiteout.c 1970-01-01 02:00:00.000000000 +0200
10362 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/whiteout.c 2010-07-30 15:50:50.563010175 +0300
10363 @@ -0,0 +1,584 @@
10364 +/*
10365 + * Copyright (c) 2003-2010 Erez Zadok
10366 + * Copyright (c) 2003-2006 Charles P. Wright
10367 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10368 + * Copyright (c) 2005-2006 Junjiro Okajima
10369 + * Copyright (c) 2005      Arun M. Krishnakumar
10370 + * Copyright (c) 2004-2006 David P. Quigley
10371 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10372 + * Copyright (c) 2003      Puja Gupta
10373 + * Copyright (c) 2003      Harikesavan Krishnan
10374 + * Copyright (c) 2003-2010 Stony Brook University
10375 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
10376 + *
10377 + * This program is free software; you can redistribute it and/or modify
10378 + * it under the terms of the GNU General Public License version 2 as
10379 + * published by the Free Software Foundation.
10380 + */
10381 +
10382 +#include "union.h"
10383 +
10384 +/*
10385 + * whiteout and opaque directory helpers
10386 + */
10387 +
10388 +/* What do we use for whiteouts. */
10389 +#define UNIONFS_WHPFX ".wh."
10390 +#define UNIONFS_WHLEN 4
10391 +/*
10392 + * If a directory contains this file, then it is opaque.  We start with the
10393 + * .wh. flag so that it is blocked by lookup.
10394 + */
10395 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10396 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10397 +
10398 +/* construct whiteout filename */
10399 +char *alloc_whname(const char *name, int len)
10400 +{
10401 +       char *buf;
10402 +
10403 +       buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10404 +       if (unlikely(!buf))
10405 +               return ERR_PTR(-ENOMEM);
10406 +
10407 +       strcpy(buf, UNIONFS_WHPFX);
10408 +       strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10409 +
10410 +       return buf;
10411 +}
10412 +
10413 +/*
10414 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10415 + * code in one place.
10416 + */
10417 +void unionfs_set_max_namelen(long *namelen)
10418 +{
10419 +       *namelen -= UNIONFS_WHLEN;
10420 +}
10421 +
10422 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10423 +bool is_whiteout_name(char **namep, int *namelenp)
10424 +{
10425 +       if (*namelenp > UNIONFS_WHLEN &&
10426 +           !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10427 +               *namep += UNIONFS_WHLEN;
10428 +               *namelenp -= UNIONFS_WHLEN;
10429 +               return true;
10430 +       }
10431 +       return false;
10432 +}
10433 +
10434 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10435 +bool is_validname(const char *name)
10436 +{
10437 +       if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10438 +               return false;
10439 +       if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10440 +                    sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10441 +               return false;
10442 +       return true;
10443 +}
10444 +
10445 +/*
10446 + * Look for a whiteout @name in @lower_parent directory.  If error, return
10447 + * ERR_PTR.  Caller must dput() the returned dentry if not an error.
10448 + *
10449 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10450 + * free then re-malloc calls.  Need to provide a different API for those
10451 + * callers.
10452 + */
10453 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10454 +{
10455 +       char *whname = NULL;
10456 +       int err = 0, namelen;
10457 +       struct dentry *wh_dentry = NULL;
10458 +
10459 +       namelen = strlen(name);
10460 +       whname = alloc_whname(name, namelen);
10461 +       if (unlikely(IS_ERR(whname))) {
10462 +               err = PTR_ERR(whname);
10463 +               goto out;
10464 +       }
10465 +
10466 +       /* check if whiteout exists in this branch: lookup .wh.foo */
10467 +       wh_dentry = lookup_lck_len(whname, lower_parent, strlen(whname));
10468 +       if (IS_ERR(wh_dentry)) {
10469 +               err = PTR_ERR(wh_dentry);
10470 +               goto out;
10471 +       }
10472 +
10473 +       /* check if negative dentry (ENOENT) */
10474 +       if (!wh_dentry->d_inode)
10475 +               goto out;
10476 +
10477 +       /* whiteout found: check if valid type */
10478 +       if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10479 +               printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10480 +                      whname, wh_dentry->d_inode->i_mode);
10481 +               dput(wh_dentry);
10482 +               err = -EIO;
10483 +               goto out;
10484 +       }
10485 +
10486 +out:
10487 +       kfree(whname);
10488 +       if (err)
10489 +               wh_dentry = ERR_PTR(err);
10490 +       return wh_dentry;
10491 +}
10492 +
10493 +/* find and return first whiteout in parent directory, else ENOENT */
10494 +struct dentry *find_first_whiteout(struct dentry *dentry)
10495 +{
10496 +       int bindex, bstart, bend;
10497 +       struct dentry *parent, *lower_parent, *wh_dentry;
10498 +
10499 +       parent = dget_parent(dentry);
10500 +
10501 +       bstart = dbstart(parent);
10502 +       bend = dbend(parent);
10503 +       wh_dentry = ERR_PTR(-ENOENT);
10504 +
10505 +       for (bindex = bstart; bindex <= bend; bindex++) {
10506 +               lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10507 +               if (!lower_parent)
10508 +                       continue;
10509 +               wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10510 +               if (IS_ERR(wh_dentry))
10511 +                       continue;
10512 +               if (wh_dentry->d_inode)
10513 +                       break;
10514 +               dput(wh_dentry);
10515 +               wh_dentry = ERR_PTR(-ENOENT);
10516 +       }
10517 +
10518 +       dput(parent);
10519 +
10520 +       return wh_dentry;
10521 +}
10522 +
10523 +/*
10524 + * Unlink a whiteout dentry.  Returns 0 or -errno.  Caller must hold and
10525 + * release dentry reference.
10526 + */
10527 +int unlink_whiteout(struct dentry *wh_dentry)
10528 +{
10529 +       int err;
10530 +       struct dentry *lower_dir_dentry;
10531 +
10532 +       /* dget and lock parent dentry */
10533 +       lower_dir_dentry = lock_parent_wh(wh_dentry);
10534 +
10535 +       /* see Documentation/filesystems/unionfs/issues.txt */
10536 +       lockdep_off();
10537 +       err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10538 +       lockdep_on();
10539 +       unlock_dir(lower_dir_dentry);
10540 +
10541 +       /*
10542 +        * Whiteouts are special files and should be deleted no matter what
10543 +        * (as if they never existed), in order to allow this create
10544 +        * operation to succeed.  This is especially important in sticky
10545 +        * directories: a whiteout may have been created by one user, but
10546 +        * the newly created file may be created by another user.
10547 +        * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10548 +        * above failed, then we have to try to directly unlink the
10549 +        * whiteout.  Note: in the ODF version of unionfs, whiteout are
10550 +        * handled much more cleanly.
10551 +        */
10552 +       if (err == -EPERM) {
10553 +               struct inode *inode = lower_dir_dentry->d_inode;
10554 +               err = inode->i_op->unlink(inode, wh_dentry);
10555 +       }
10556 +       if (err)
10557 +               printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10558 +                      "err = %d\n", wh_dentry->d_name.name, err);
10559 +
10560 +       return err;
10561 +
10562 +}
10563 +
10564 +/*
10565 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10566 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10567 + * whose name is taken from @dentry.  Then tries to remove that whiteout, if
10568 + * found.  If <dentry,bindex> is a branch marked readonly, return -EROFS.
10569 + * If it finds both a regular file and a whiteout, return -EIO (this should
10570 + * never happen).
10571 + *
10572 + * Return 0 if no whiteout was found.  Return 1 if one was found and
10573 + * successfully removed.  Therefore a value >= 0 tells the caller that
10574 + * @lower_dentry belongs to a good branch to create the new object in).
10575 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10576 + * unlink the whiteout.
10577 + */
10578 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10579 +                         int bindex)
10580 +{
10581 +       int err;
10582 +       struct dentry *wh_dentry = NULL;
10583 +       struct dentry *lower_dir_dentry = NULL;
10584 +
10585 +       /* look for whiteout dentry first */
10586 +       lower_dir_dentry = dget_parent(lower_dentry);
10587 +       wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10588 +       dput(lower_dir_dentry);
10589 +       if (IS_ERR(wh_dentry)) {
10590 +               err = PTR_ERR(wh_dentry);
10591 +               goto out;
10592 +       }
10593 +
10594 +       if (!wh_dentry->d_inode) { /* no whiteout exists*/
10595 +               err = 0;
10596 +               goto out_dput;
10597 +       }
10598 +
10599 +       /* check if regular file and whiteout were both found */
10600 +       if (unlikely(lower_dentry->d_inode)) {
10601 +               err = -EIO;
10602 +               printk(KERN_ERR "unionfs: found both whiteout and regular "
10603 +                      "file in directory %s (branch %d)\n",
10604 +                      lower_dir_dentry->d_name.name, bindex);
10605 +               goto out_dput;
10606 +       }
10607 +
10608 +       /* check if branch is writeable */
10609 +       err = is_robranch_super(dentry->d_sb, bindex);
10610 +       if (err)
10611 +               goto out_dput;
10612 +
10613 +       /* .wh.foo has been found, so let's unlink it */
10614 +       err = unlink_whiteout(wh_dentry);
10615 +       if (!err)
10616 +               err = 1; /* a whiteout was found and successfully removed */
10617 +out_dput:
10618 +       dput(wh_dentry);
10619 +out:
10620 +       return err;
10621 +}
10622 +
10623 +/*
10624 + * Pass an unionfs dentry and an index.  It will try to create a whiteout
10625 + * for the filename in dentry, and will try in branch 'index'.  On error,
10626 + * it will proceed to a branch to the left.
10627 + */
10628 +int create_whiteout(struct dentry *dentry, int start)
10629 +{
10630 +       int bstart, bend, bindex;
10631 +       struct dentry *lower_dir_dentry;
10632 +       struct dentry *lower_dentry;
10633 +       struct dentry *lower_wh_dentry;
10634 +       struct nameidata nd;
10635 +       char *name = NULL;
10636 +       int err = -EINVAL;
10637 +
10638 +       verify_locked(dentry);
10639 +
10640 +       bstart = dbstart(dentry);
10641 +       bend = dbend(dentry);
10642 +
10643 +       /* create dentry's whiteout equivalent */
10644 +       name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10645 +       if (unlikely(IS_ERR(name))) {
10646 +               err = PTR_ERR(name);
10647 +               goto out;
10648 +       }
10649 +
10650 +       for (bindex = start; bindex >= 0; bindex--) {
10651 +               lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10652 +
10653 +               if (!lower_dentry) {
10654 +                       /*
10655 +                        * if lower dentry is not present, create the
10656 +                        * entire lower dentry directory structure and go
10657 +                        * ahead.  Since we want to just create whiteout, we
10658 +                        * only want the parent dentry, and hence get rid of
10659 +                        * this dentry.
10660 +                        */
10661 +                       lower_dentry = create_parents(dentry->d_inode,
10662 +                                                     dentry,
10663 +                                                     dentry->d_name.name,
10664 +                                                     bindex);
10665 +                       if (!lower_dentry || IS_ERR(lower_dentry)) {
10666 +                               int ret = PTR_ERR(lower_dentry);
10667 +                               if (!IS_COPYUP_ERR(ret))
10668 +                                       printk(KERN_ERR
10669 +                                              "unionfs: create_parents for "
10670 +                                              "whiteout failed: bindex=%d "
10671 +                                              "err=%d\n", bindex, ret);
10672 +                               continue;
10673 +                       }
10674 +               }
10675 +
10676 +               lower_wh_dentry =
10677 +                       lookup_lck_len(name, lower_dentry->d_parent,
10678 +                                      dentry->d_name.len + UNIONFS_WHLEN);
10679 +               if (IS_ERR(lower_wh_dentry))
10680 +                       continue;
10681 +
10682 +               /*
10683 +                * The whiteout already exists. This used to be impossible,
10684 +                * but now is possible because of opaqueness.
10685 +                */
10686 +               if (lower_wh_dentry->d_inode) {
10687 +                       dput(lower_wh_dentry);
10688 +                       err = 0;
10689 +                       goto out;
10690 +               }
10691 +
10692 +               err = init_lower_nd(&nd, LOOKUP_CREATE);
10693 +               if (unlikely(err < 0))
10694 +                       goto out;
10695 +               lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10696 +               err = is_robranch_super(dentry->d_sb, bindex);
10697 +               if (!err)
10698 +                       err = vfs_create(lower_dir_dentry->d_inode,
10699 +                                        lower_wh_dentry,
10700 +                                        current_umask() & S_IRUGO,
10701 +                                        &nd);
10702 +               unlock_dir(lower_dir_dentry);
10703 +               dput(lower_wh_dentry);
10704 +               release_lower_nd(&nd, err);
10705 +
10706 +               if (!err || !IS_COPYUP_ERR(err))
10707 +                       break;
10708 +       }
10709 +
10710 +       /* set dbopaque so that lookup will not proceed after this branch */
10711 +       if (!err)
10712 +               dbopaque(dentry) = bindex;
10713 +
10714 +out:
10715 +       kfree(name);
10716 +       return err;
10717 +}
10718 +
10719 +/*
10720 + * Delete all of the whiteouts in a given directory for rmdir.
10721 + *
10722 + * lower directory inode should be locked
10723 + */
10724 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10725 +                              struct unionfs_dir_state *namelist)
10726 +{
10727 +       int err = 0;
10728 +       struct dentry *lower_dir_dentry = NULL;
10729 +       struct dentry *lower_dentry;
10730 +       char *name = NULL, *p;
10731 +       struct inode *lower_dir;
10732 +       int i;
10733 +       struct list_head *pos;
10734 +       struct filldir_node *cursor;
10735 +
10736 +       /* Find out lower parent dentry */
10737 +       lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10738 +       BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10739 +       lower_dir = lower_dir_dentry->d_inode;
10740 +       BUG_ON(!S_ISDIR(lower_dir->i_mode));
10741 +
10742 +       err = -ENOMEM;
10743 +       name = __getname();
10744 +       if (unlikely(!name))
10745 +               goto out;
10746 +       strcpy(name, UNIONFS_WHPFX);
10747 +       p = name + UNIONFS_WHLEN;
10748 +
10749 +       err = 0;
10750 +       for (i = 0; !err && i < namelist->size; i++) {
10751 +               list_for_each(pos, &namelist->list[i]) {
10752 +                       cursor =
10753 +                               list_entry(pos, struct filldir_node,
10754 +                                          file_list);
10755 +                       /* Only operate on whiteouts in this branch. */
10756 +                       if (cursor->bindex != bindex)
10757 +                               continue;
10758 +                       if (!cursor->whiteout)
10759 +                               continue;
10760 +
10761 +                       strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10762 +                       lower_dentry =
10763 +                               lookup_lck_len(name, lower_dir_dentry,
10764 +                                              cursor->namelen +
10765 +                                              UNIONFS_WHLEN);
10766 +                       if (IS_ERR(lower_dentry)) {
10767 +                               err = PTR_ERR(lower_dentry);
10768 +                               break;
10769 +                       }
10770 +                       if (lower_dentry->d_inode)
10771 +                               err = vfs_unlink(lower_dir, lower_dentry);
10772 +                       dput(lower_dentry);
10773 +                       if (err)
10774 +                               break;
10775 +               }
10776 +       }
10777 +
10778 +       __putname(name);
10779 +
10780 +       /* After all of the removals, we should copy the attributes once. */
10781 +       fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10782 +
10783 +out:
10784 +       return err;
10785 +}
10786 +
10787 +
10788 +void __delete_whiteouts(struct work_struct *work)
10789 +{
10790 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
10791 +       struct deletewh_args *d = &args->deletewh;
10792 +
10793 +       args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10794 +       complete(&args->comp);
10795 +}
10796 +
10797 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10798 +int delete_whiteouts(struct dentry *dentry, int bindex,
10799 +                    struct unionfs_dir_state *namelist)
10800 +{
10801 +       int err;
10802 +       struct super_block *sb;
10803 +       struct dentry *lower_dir_dentry;
10804 +       struct inode *lower_dir;
10805 +       struct sioq_args args;
10806 +
10807 +       sb = dentry->d_sb;
10808 +
10809 +       BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10810 +       BUG_ON(bindex < dbstart(dentry));
10811 +       BUG_ON(bindex > dbend(dentry));
10812 +       err = is_robranch_super(sb, bindex);
10813 +       if (err)
10814 +               goto out;
10815 +
10816 +       lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10817 +       BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10818 +       lower_dir = lower_dir_dentry->d_inode;
10819 +       BUG_ON(!S_ISDIR(lower_dir->i_mode));
10820 +
10821 +       if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10822 +               err = do_delete_whiteouts(dentry, bindex, namelist);
10823 +       } else {
10824 +               args.deletewh.namelist = namelist;
10825 +               args.deletewh.dentry = dentry;
10826 +               args.deletewh.bindex = bindex;
10827 +               run_sioq(__delete_whiteouts, &args);
10828 +               err = args.err;
10829 +       }
10830 +
10831 +out:
10832 +       return err;
10833 +}
10834 +
10835 +/****************************************************************************
10836 + * Opaque directory helpers                                                 *
10837 + ****************************************************************************/
10838 +
10839 +/*
10840 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
10841 + * -errno if an error occurred trying to figure this out.
10842 + */
10843 +int is_opaque_dir(struct dentry *dentry, int bindex)
10844 +{
10845 +       int err = 0;
10846 +       struct dentry *lower_dentry;
10847 +       struct dentry *wh_lower_dentry;
10848 +       struct inode *lower_inode;
10849 +       struct sioq_args args;
10850 +
10851 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10852 +       lower_inode = lower_dentry->d_inode;
10853 +
10854 +       BUG_ON(!S_ISDIR(lower_inode->i_mode));
10855 +
10856 +       mutex_lock(&lower_inode->i_mutex);
10857 +
10858 +       if (!inode_permission(lower_inode, MAY_EXEC)) {
10859 +               wh_lower_dentry =
10860 +                       lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
10861 +                                      sizeof(UNIONFS_DIR_OPAQUE) - 1);
10862 +       } else {
10863 +               args.is_opaque.dentry = lower_dentry;
10864 +               run_sioq(__is_opaque_dir, &args);
10865 +               wh_lower_dentry = args.ret;
10866 +       }
10867 +
10868 +       mutex_unlock(&lower_inode->i_mutex);
10869 +
10870 +       if (IS_ERR(wh_lower_dentry)) {
10871 +               err = PTR_ERR(wh_lower_dentry);
10872 +               goto out;
10873 +       }
10874 +
10875 +       /* This is an opaque dir iff wh_lower_dentry is positive */
10876 +       err = !!wh_lower_dentry->d_inode;
10877 +
10878 +       dput(wh_lower_dentry);
10879 +out:
10880 +       return err;
10881 +}
10882 +
10883 +void __is_opaque_dir(struct work_struct *work)
10884 +{
10885 +       struct sioq_args *args = container_of(work, struct sioq_args, work);
10886 +
10887 +       args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
10888 +                                  sizeof(UNIONFS_DIR_OPAQUE) - 1);
10889 +       complete(&args->comp);
10890 +}
10891 +
10892 +int make_dir_opaque(struct dentry *dentry, int bindex)
10893 +{
10894 +       int err = 0;
10895 +       struct dentry *lower_dentry, *diropq;
10896 +       struct inode *lower_dir;
10897 +       struct nameidata nd;
10898 +       const struct cred *old_creds;
10899 +       struct cred *new_creds;
10900 +
10901 +       /*
10902 +        * Opaque directory whiteout markers are special files (like regular
10903 +        * whiteouts), and should appear to the users as if they don't
10904 +        * exist.  They should be created/deleted regardless of directory
10905 +        * search/create permissions, but only for the duration of this
10906 +        * creation of the .wh.__dir_opaque: file.  Note, this does not
10907 +        * circumvent normal ->permission).
10908 +        */
10909 +       new_creds = prepare_creds();
10910 +       if (unlikely(!new_creds)) {
10911 +               err = -ENOMEM;
10912 +               goto out_err;
10913 +       }
10914 +       cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
10915 +       cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
10916 +       old_creds = override_creds(new_creds);
10917 +
10918 +       lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10919 +       lower_dir = lower_dentry->d_inode;
10920 +       BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
10921 +              !S_ISDIR(lower_dir->i_mode));
10922 +
10923 +       mutex_lock(&lower_dir->i_mutex);
10924 +       diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
10925 +                               sizeof(UNIONFS_DIR_OPAQUE) - 1);
10926 +       if (IS_ERR(diropq)) {
10927 +               err = PTR_ERR(diropq);
10928 +               goto out;
10929 +       }
10930 +
10931 +       err = init_lower_nd(&nd, LOOKUP_CREATE);
10932 +       if (unlikely(err < 0))
10933 +               goto out;
10934 +       if (!diropq->d_inode)
10935 +               err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
10936 +       if (!err)
10937 +               dbopaque(dentry) = bindex;
10938 +       release_lower_nd(&nd, err);
10939 +
10940 +       dput(diropq);
10941 +
10942 +out:
10943 +       mutex_unlock(&lower_dir->i_mutex);
10944 +       revert_creds(old_creds);
10945 +out_err:
10946 +       return err;
10947 +}
10948 diff -Nurp linux-2.6.35-rc6-git//fs/unionfs/xattr.c linux-2.6.35-rc6-git-unionfs//fs/unionfs/xattr.c
10949 --- linux-2.6.35-rc6-git//fs/unionfs/xattr.c    1970-01-01 02:00:00.000000000 +0200
10950 +++ linux-2.6.35-rc6-git-unionfs//fs/unionfs/xattr.c    2010-07-30 15:50:50.563010175 +0300
10951 @@ -0,0 +1,173 @@
10952 +/*
10953 + * Copyright (c) 2003-2010 Erez Zadok
10954 + * Copyright (c) 2003-2006 Charles P. Wright
10955 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10956 + * Copyright (c) 2005-2006 Junjiro Okajima
10957 + * Copyright (c) 2005      Arun M. Krishnakumar
10958 + * Copyright (c) 2004-2006 David P. Quigley
10959 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10960 + * Copyright (c) 2003      Puja Gupta
10961 + * Copyright (c) 2003      Harikesavan Krishnan
10962 + * Copyright (c) 2003-2010 Stony Brook University
10963 + * Copyright (c) 2003-2010 The Research Foundation of SUNY
10964 + *
10965 + * This program is free software; you can redistribute it and/or modify
10966 + * it under the terms of the GNU General Public License version 2 as
10967 + * published by the Free Software Foundation.
10968 + */
10969 +
10970 +#include "union.h"
10971 +
10972 +/* This is lifted from fs/xattr.c */
10973 +void *unionfs_xattr_alloc(size_t size, size_t limit)
10974 +{
10975 +       void *ptr;
10976 +
10977 +       if (size > limit)
10978 +               return ERR_PTR(-E2BIG);
10979 +
10980 +       if (!size)              /* size request, no buffer is needed */
10981 +               return NULL;
10982 +
10983 +       ptr = kmalloc(size, GFP_KERNEL);
10984 +       if (unlikely(!ptr))
10985 +               return ERR_PTR(-ENOMEM);
10986 +       return ptr;
10987 +}
10988 +
10989 +/*
10990 + * BKL held by caller.
10991 + * dentry->d_inode->i_mutex locked
10992 + */
10993 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
10994 +                        size_t size)
10995 +{
10996 +       struct dentry *lower_dentry = NULL;
10997 +       struct dentry *parent;
10998 +       int err = -EOPNOTSUPP;
10999 +       bool valid;
11000 +
11001 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11002 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11003 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11004 +
11005 +       valid = __unionfs_d_revalidate(dentry, parent, false);
11006 +       if (unlikely(!valid)) {
11007 +               err = -ESTALE;
11008 +               goto out;
11009 +       }
11010 +
11011 +       lower_dentry = unionfs_lower_dentry(dentry);
11012 +
11013 +       err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11014 +
11015 +out:
11016 +       unionfs_check_dentry(dentry);
11017 +       unionfs_unlock_dentry(dentry);
11018 +       unionfs_unlock_parent(dentry, parent);
11019 +       unionfs_read_unlock(dentry->d_sb);
11020 +       return err;
11021 +}
11022 +
11023 +/*
11024 + * BKL held by caller.
11025 + * dentry->d_inode->i_mutex locked
11026 + */
11027 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11028 +                    const void *value, size_t size, int flags)
11029 +{
11030 +       struct dentry *lower_dentry = NULL;
11031 +       struct dentry *parent;
11032 +       int err = -EOPNOTSUPP;
11033 +       bool valid;
11034 +
11035 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11036 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11037 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11038 +
11039 +       valid = __unionfs_d_revalidate(dentry, parent, false);
11040 +       if (unlikely(!valid)) {
11041 +               err = -ESTALE;
11042 +               goto out;
11043 +       }
11044 +
11045 +       lower_dentry = unionfs_lower_dentry(dentry);
11046 +
11047 +       err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11048 +                          size, flags);
11049 +
11050 +out:
11051 +       unionfs_check_dentry(dentry);
11052 +       unionfs_unlock_dentry(dentry);
11053 +       unionfs_unlock_parent(dentry, parent);
11054 +       unionfs_read_unlock(dentry->d_sb);
11055 +       return err;
11056 +}
11057 +
11058 +/*
11059 + * BKL held by caller.
11060 + * dentry->d_inode->i_mutex locked
11061 + */
11062 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11063 +{
11064 +       struct dentry *lower_dentry = NULL;
11065 +       struct dentry *parent;
11066 +       int err = -EOPNOTSUPP;
11067 +       bool valid;
11068 +
11069 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11070 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11071 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11072 +
11073 +       valid = __unionfs_d_revalidate(dentry, parent, false);
11074 +       if (unlikely(!valid)) {
11075 +               err = -ESTALE;
11076 +               goto out;
11077 +       }
11078 +
11079 +       lower_dentry = unionfs_lower_dentry(dentry);
11080 +
11081 +       err = vfs_removexattr(lower_dentry, (char *) name);
11082 +
11083 +out:
11084 +       unionfs_check_dentry(dentry);
11085 +       unionfs_unlock_dentry(dentry);
11086 +       unionfs_unlock_parent(dentry, parent);
11087 +       unionfs_read_unlock(dentry->d_sb);
11088 +       return err;
11089 +}
11090 +
11091 +/*
11092 + * BKL held by caller.
11093 + * dentry->d_inode->i_mutex locked
11094 + */
11095 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11096 +{
11097 +       struct dentry *lower_dentry = NULL;
11098 +       struct dentry *parent;
11099 +       int err = -EOPNOTSUPP;
11100 +       char *encoded_list = NULL;
11101 +       bool valid;
11102 +
11103 +       unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11104 +       parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11105 +       unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11106 +
11107 +       valid = __unionfs_d_revalidate(dentry, parent, false);
11108 +       if (unlikely(!valid)) {
11109 +               err = -ESTALE;
11110 +               goto out;
11111 +       }
11112 +
11113 +       lower_dentry = unionfs_lower_dentry(dentry);
11114 +
11115 +       encoded_list = list;
11116 +       err = vfs_listxattr(lower_dentry, encoded_list, size);
11117 +
11118 +out:
11119 +       unionfs_check_dentry(dentry);
11120 +       unionfs_unlock_dentry(dentry);
11121 +       unionfs_unlock_parent(dentry, parent);
11122 +       unionfs_read_unlock(dentry->d_sb);
11123 +       return err;
11124 +}
11125 diff -Nurp linux-2.6.35-rc6-git//include/linux/fs_stack.h linux-2.6.35-rc6-git-unionfs//include/linux/fs_stack.h
11126 --- linux-2.6.35-rc6-git//include/linux/fs_stack.h      2010-05-17 00:17:36.000000000 +0300
11127 +++ linux-2.6.35-rc6-git-unionfs//include/linux/fs_stack.h      2010-07-30 15:50:52.713110440 +0300
11128 @@ -1,7 +1,19 @@
11129 +/*
11130 + * Copyright (c) 2006-2009 Erez Zadok
11131 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11132 + * Copyright (c) 2006-2009 Stony Brook University
11133 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11134 + *
11135 + * This program is free software; you can redistribute it and/or modify
11136 + * it under the terms of the GNU General Public License version 2 as
11137 + * published by the Free Software Foundation.
11138 + */
11139 +
11140  #ifndef _LINUX_FS_STACK_H
11141  #define _LINUX_FS_STACK_H
11142  
11143 -/* This file defines generic functions used primarily by stackable
11144 +/*
11145 + * This file defines generic functions used primarily by stackable
11146   * filesystems; none of these functions require i_mutex to be held.
11147   */
11148  
11149 diff -Nurp linux-2.6.35-rc6-git//include/linux/magic.h linux-2.6.35-rc6-git-unionfs//include/linux/magic.h
11150 --- linux-2.6.35-rc6-git//include/linux/magic.h 2010-05-17 00:17:36.000000000 +0300
11151 +++ linux-2.6.35-rc6-git-unionfs//include/linux/magic.h 2010-07-30 15:50:52.713110440 +0300
11152 @@ -47,6 +47,8 @@
11153  #define REISER2FS_SUPER_MAGIC_STRING   "ReIsEr2Fs"
11154  #define REISER2FS_JR_SUPER_MAGIC_STRING        "ReIsEr3Fs"
11155  
11156 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11157 +
11158  #define SMB_SUPER_MAGIC                0x517B
11159  #define USBDEVICE_SUPER_MAGIC  0x9fa2
11160  #define CGROUP_SUPER_MAGIC     0x27e0eb
11161 diff -Nurp linux-2.6.35-rc6-git//include/linux/namei.h linux-2.6.35-rc6-git-unionfs//include/linux/namei.h
11162 --- linux-2.6.35-rc6-git//include/linux/namei.h 2010-05-17 00:17:36.000000000 +0300
11163 +++ linux-2.6.35-rc6-git-unionfs//include/linux/namei.h 2010-07-30 15:50:52.713110440 +0300
11164 @@ -72,6 +72,7 @@ extern int vfs_path_lookup(struct dentry
11165  
11166  extern struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
11167                 int (*open)(struct inode *, struct file *));
11168 +extern void release_open_intent(struct nameidata *);
11169  
11170  extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
11171  
11172 diff -Nurp linux-2.6.35-rc6-git//include/linux/splice.h linux-2.6.35-rc6-git-unionfs//include/linux/splice.h
11173 --- linux-2.6.35-rc6-git//include/linux/splice.h        2010-07-30 14:05:39.412885409 +0300
11174 +++ linux-2.6.35-rc6-git-unionfs//include/linux/splice.h        2010-07-30 15:50:52.713110440 +0300
11175 @@ -81,6 +81,11 @@ extern ssize_t splice_to_pipe(struct pip
11176                               struct splice_pipe_desc *);
11177  extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11178                                       splice_direct_actor *);
11179 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11180 +                           loff_t *ppos, size_t len, unsigned int flags);
11181 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11182 +                         struct pipe_inode_info *pipe, size_t len,
11183 +                         unsigned int flags);
11184  
11185  /*
11186   * for dynamic pipe sizing
11187 diff -Nurp linux-2.6.35-rc6-git//include/linux/union_fs.h linux-2.6.35-rc6-git-unionfs//include/linux/union_fs.h
11188 --- linux-2.6.35-rc6-git//include/linux/union_fs.h      1970-01-01 02:00:00.000000000 +0200
11189 +++ linux-2.6.35-rc6-git-unionfs//include/linux/union_fs.h      2010-07-30 15:50:52.714010579 +0300
11190 @@ -0,0 +1,22 @@
11191 +/*
11192 + * Copyright (c) 2003-2009 Erez Zadok
11193 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11194 + * Copyright (c) 2003-2009 Stony Brook University
11195 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11196 + *
11197 + * This program is free software; you can redistribute it and/or modify
11198 + * it under the terms of the GNU General Public License version 2 as
11199 + * published by the Free Software Foundation.
11200 + */
11201 +
11202 +#ifndef _LINUX_UNION_FS_H
11203 +#define _LINUX_UNION_FS_H
11204 +
11205 +/*
11206 + * DEFINITIONS FOR USER AND KERNEL CODE:
11207 + */
11208 +# define UNIONFS_IOCTL_INCGEN          _IOR(0x15, 11, int)
11209 +# define UNIONFS_IOCTL_QUERYFILE       _IOR(0x15, 15, int)
11210 +
11211 +#endif /* _LINUX_UNIONFS_H */
11212 +
11213 diff -Nurp linux-2.6.35-rc6-git//MAINTAINERS linux-2.6.35-rc6-git-unionfs//MAINTAINERS
11214 --- linux-2.6.35-rc6-git//MAINTAINERS   2010-07-30 14:05:44.879978886 +0300
11215 +++ linux-2.6.35-rc6-git-unionfs//MAINTAINERS   2010-07-30 15:50:42.995947739 +0300
11216 @@ -5766,6 +5766,14 @@ F:       Documentation/cdrom/
11217  F:     drivers/cdrom/cdrom.c
11218  F:     include/linux/cdrom.h
11219  
11220 +UNIONFS
11221 +P:     Erez Zadok
11222 +M:     ezk@cs.sunysb.edu
11223 +L:     unionfs@filesystems.org
11224 +W:     http://unionfs.filesystems.org/
11225 +T:     git git.kernel.org/pub/scm/linux/kernel/git/ezk/unionfs.git
11226 +S:     Maintained
11227 +
11228  UNSORTED BLOCK IMAGES (UBI)
11229  M:     Artem Bityutskiy <dedekind1@gmail.com>
11230  W:     http://www.linux-mtd.infradead.org/
11231 diff -Nurp linux-2.6.35-rc6-git//security/security.c linux-2.6.35-rc6-git-unionfs//security/security.c
11232 --- linux-2.6.35-rc6-git//security/security.c   2010-07-30 14:05:39.660952393 +0300
11233 +++ linux-2.6.35-rc6-git-unionfs//security/security.c   2010-07-30 15:50:52.714010579 +0300
11234 @@ -529,6 +529,7 @@ int security_inode_permission(struct ino
11235                 return 0;
11236         return security_ops->inode_permission(inode, mask);
11237  }
11238 +EXPORT_SYMBOL(security_inode_permission);
11239  
11240  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
11241  {
11242
11243 This adapts unionfs to vfs changes in 2.6.35
11244
11245 Signed-off-by: Thomas Backlund <tmb@mandriva.org>
11246
11247  fs/copyup.c |    3 +--
11248  fs/file.c   |    7 +++----
11249  fs/super.c  |    2 +-
11250  fs/union.h  |    3 +--
11251  4 files changed, 6 insertions(+), 9 deletions(-)
11252
11253 diff -Nurp linux-2.6.35-rc6-git-unionfs//fs/unionfs/copyup.c linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/copyup.c
11254 --- linux-2.6.35-rc6-git-unionfs//fs/unionfs/copyup.c   2010-07-30 15:50:50.558010270 +0300
11255 +++ linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/copyup.c  2010-07-30 16:40:07.224979088 +0300
11256 @@ -326,8 +326,7 @@ static int __copyup_reg_data(struct dent
11257         kfree(buf);
11258  
11259         if (!err)
11260 -               err = output_file->f_op->fsync(output_file,
11261 -                                              new_lower_dentry, 0);
11262 +               err = output_file->f_op->fsync(output_file, 0);
11263  
11264         if (err)
11265                 goto out_close_out;
11266 diff -Nurp linux-2.6.35-rc6-git-unionfs//fs/unionfs/file.c linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/file.c
11267 --- linux-2.6.35-rc6-git-unionfs//fs/unionfs/file.c     2010-07-30 15:50:50.559010056 +0300
11268 +++ linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/file.c    2010-07-30 17:13:17.648885524 +0300
11269 @@ -188,8 +188,9 @@ out:
11270         return err;
11271  }
11272  
11273 -int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
11274 +int unionfs_fsync(struct file *file, int datasync)
11275  {
11276 +       struct dentry *dentry = file->f_path.dentry;
11277         int bindex, bstart, bend;
11278         struct file *lower_file;
11279         struct dentry *lower_dentry;
11280 @@ -224,9 +225,7 @@ int unionfs_fsync(struct file *file, str
11281                 lower_file = unionfs_lower_file_idx(file, bindex);
11282                 lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11283                 mutex_lock(&lower_inode->i_mutex);
11284 -               err = lower_inode->i_fop->fsync(lower_file,
11285 -                                               lower_dentry,
11286 -                                               datasync);
11287 +               err = lower_inode->i_fop->fsync(lower_file, datasync);
11288                 if (!err && bindex == bstart)
11289                         fsstack_copy_attr_times(inode, lower_inode);
11290                 mutex_unlock(&lower_inode->i_mutex);
11291 diff -Nurp linux-2.6.35-rc6-git-unionfs//fs/unionfs/super.c linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/super.c
11292 --- linux-2.6.35-rc6-git-unionfs//fs/unionfs/super.c    2010-07-30 15:50:50.562010141 +0300
11293 +++ linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/super.c   2010-07-30 17:32:08.119042569 +0300
11294 @@ -938,7 +938,7 @@ void unionfs_destroy_inode_cache(void)
11295   *
11296   * No need to grab sb info's rwsem.
11297   */
11298 -static int unionfs_write_inode(struct inode *inode, int sync)
11299 +static int unionfs_write_inode(struct inode *inode, struct writeback_control *wbc)
11300  {
11301         struct list_head *pos, *n;
11302         struct unionfs_dir_state *rdstate;
11303 diff -Nurp linux-2.6.35-rc6-git-unionfs//fs/unionfs/union.h linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/union.h
11304 --- linux-2.6.35-rc6-git-unionfs//fs/unionfs/union.h    2010-07-30 15:50:50.563010175 +0300
11305 +++ linux-2.6.35-rc6-git-unionfs-buildfix//fs/unionfs/union.h   2010-07-30 16:32:43.455947876 +0300
11306 @@ -416,8 +416,7 @@ extern int unionfs_file_release(struct i
11307  extern int unionfs_flush(struct file *file, fl_owner_t id);
11308  extern long unionfs_ioctl(struct file *file, unsigned int cmd,
11309                           unsigned long arg);
11310 -extern int unionfs_fsync(struct file *file, struct dentry *dentry,
11311 -                        int datasync);
11312 +extern int unionfs_fsync(struct file *file, int datasync);
11313  extern int unionfs_fasync(int fd, struct file *file, int flag);
11314  
11315  /* Inode operations */
11316 From 2eac19af5c9a275a78fb98c9e8f232bf88f4a040 Mon Sep 17 00:00:00 2001
11317 From: Pascal Terjan <pterjan@mandriva.com>
11318 Date: Wed, 23 Dec 2009 15:07:41 +0100
11319 Subject: [PATCH] Fix a oops on rpm -e openoffice.org-voikko
11320
11321 Signed-off-by: Pascal Terjan <pterjan@mandriva.com>
11322
11323 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
11324 index bd5a3b3..6b2f16f 100644
11325 --- a/fs/unionfs/unlink.c
11326 +++ b/fs/unionfs/unlink.c
11327 @@ -144,8 +144,6 @@
11328         if (!err) {
11329                 unionfs_postcopyup_release(dentry);
11330                 unionfs_postcopyup_setmnt(parent);
11331 -               if (inode->i_nlink == 0) /* drop lower inodes */
11332 -                       iput_lowers_all(inode, false);
11333                 d_drop(dentry);
11334                 /*
11335                  * if unlink/whiteout succeeded, parent dir mtime has
11336 -- 
11337 1.6.2.5
11338 Ok. Here's a temporary patch that makes everything except looking at
11339 the Smack label work properly. I haven't signed it off because I
11340 don't think it's ready for prime-time, but if the problem is holding
11341 anyone up it will allow them to proceed.
11342
11343 Casey Schaufler
11344
11345 ---
11346  security/smack/smack_lsm.c |   23 +++++++++++++++++++++++
11347  1 file changed, 23 insertions(+)
11348
11349 --- a/security/smack/smack_lsm.c
11350 +++ b/security/smack/smack_lsm.c
11351 @@ -2016,6 +2016,17 @@ static void smack_d_instantiate(struct d
11352  
11353         sbp = inode->i_sb;
11354         sbsp = sbp->s_security;
11355 +
11356 +       /*
11357 +        * If the superblock isn't initialized someone
11358 +        * has cobbled together a filesystem that never
11359 +        * got mounted.
11360 +        */
11361 +       if (sbsp->smk_initialized == 0) {
11362 +               isp->smk_inode = smack_known_star.smk_known;
11363 +               isp->smk_flags |= SMK_INODE_INSTANT;
11364 +               goto unlockandout;
11365 +       }
11366         /*
11367          * We're going to use the superblock default label
11368          * if there's no label on the file.
11369 @@ -2055,6 +2066,18 @@ static void smack_d_instantiate(struct d
11370                  */
11371                 final = smack_known_star.smk_known;
11372                 break;
11373 +       case 0:
11374 +       case UNIONFS_SUPER_MAGIC:
11375 +               /*
11376 +                * It seems that unionfs uses a fake superblock
11377 +                * and doesn't set the magic number therein.
11378 +                * Assume that an unset magic number is unionfs.
11379 +                *
11380 +                * Let the real filesystem inodes provide the
11381 +                * Smack label on access checks.
11382 +                */
11383 +               final = smack_known_star.smk_known;
11384 +               break;
11385         case DEVPTS_SUPER_MAGIC:
11386                 /*
11387                  * devpts seems content with the label of the task.
This page took 0.919625 seconds and 3 git commands to generate.