2 This is a rebase to 2.6.35 of the upstream unionfs-2.5.4_for_2.6.34-rc0.diff.gz
4 Signed-off-by: Thomas Backlund <tmb@mandriva.org>
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 +++
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(-)
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.
56 - info on the ufs filesystem.
58 + - info on the unionfs filesystem
60 - info on using the VFAT filesystem used in Windows NT and Windows 95
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
69 + - A brief introduction of concepts.
71 + - A summary of known issues with unionfs.
73 + - Information regarding rename operations.
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
80 +Unionfs 2.x CONCEPTS:
81 +=====================
83 +This file describes the concepts needed by a namespace unification file
90 +Each branch is assigned a unique priority - starting from 0 (highest
91 +priority). No two branches can have the same priority.
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.
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.
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:
114 +The unified view would simply be:
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
130 +Later, when Unionfs traverses branches (due to lookup or readdir), it
131 +eliminate 'foo' from the namespace (as well as the whiteout itself.)
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:
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:
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".
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.
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.
170 +Duplicate Elimination:
171 +======================
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.
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.
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
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).
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.
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
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.
244 +Maintaining Invariants:
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.
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
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.
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.
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.
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.
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
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.
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).
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.)
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
371 +KNOWN Unionfs 2.x ISSUES:
372 +=========================
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.
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.
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
403 +Rename is a complex beast. The following table shows which rename(2) operations
404 +should succeed and which should fail.
407 +E: error (either unionfs or vfs)
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
417 + none file dir child wh
424 +Renaming directories:
425 +=====================
427 +Whenever a empty (either physically or logically) directory is being renamed,
428 +the following sequence of events should take place:
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
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
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.
450 +# mount -t unionfs -o <OPTIONS>,<BRANCH-OPTIONS> none MOUNTPOINT
452 +OPTIONS can be any legal combination of:
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)
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.
464 +The syntax for the "dirs=" mount option is:
466 + dirs=branch[=ro|=rw][:...]
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.
479 + dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
483 + dirs=/writable_branch=rw:/read-only_branch=ro
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.
493 +To downgrade a union from read-write to read-only:
495 +# mount -t unionfs -o remount,ro none MOUNTPOINT
497 +To upgrade a union from read-only to read-write:
499 +# mount -t unionfs -o remount,rw none MOUNTPOINT
501 +To delete a branch /foo, regardless where it is in the current union:
503 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
505 +To insert (add) a branch /foo before /bar:
507 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
509 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
511 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
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:
517 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
519 +To append a branch to the very end (new lowest-priority branch):
521 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
523 +To append a branch to the very end (new lowest-priority branch), in
526 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
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:
531 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
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
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>
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
560 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
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
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
599 fput(nd->intent.open.file);
601 +EXPORT_SYMBOL_GPL(release_open_intent);
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);
610 * Attempt to initiate a splice from pipe to file.
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)
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
621 return splice_write(pipe, out, ppos, len, flags);
623 +EXPORT_SYMBOL_GPL(vfs_splice_from);
626 * Attempt to initiate a splice from a file to a pipe.
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)
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
639 return splice_read(in, ppos, pipe, len, flags);
641 +EXPORT_SYMBOL_GPL(vfs_splice_to);
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
647 loff_t pos = sd->pos, prev_pos = pos;
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))
654 @@ -1282,7 +1284,7 @@ static int direct_splice_actor(struct pi
656 struct file *file = sd->u.file;
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,
663 @@ -1380,7 +1382,7 @@ static long do_splice(struct file *in, l
667 - ret = do_splice_from(ipipe, out, off, len, flags);
668 + ret = vfs_splice_from(ipipe, out, off, len, flags);
670 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
672 @@ -1400,7 +1402,7 @@ static long do_splice(struct file *in, l
676 - ret = do_splice_to(in, off, opipe, len, flags);
677 + ret = vfs_splice_to(in, off, opipe, len, flags);
679 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
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
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
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.
696 #include <linux/module.h>
697 #include <linux/fs.h>
698 #include <linux/fs_stack.h>
700 -/* does _NOT_ require i_mutex to be held.
702 + * does _NOT_ require i_mutex to be held.
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
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
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.
731 + * 1) Copyup the file
732 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
733 + * stolen from NFS's silly rename
735 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
736 + struct dentry *parent, int bstart, int bindex)
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];
744 + struct dentry *tmp_dentry = NULL;
745 + struct dentry *lower_dentry;
746 + struct dentry *lower_dir_dentry = NULL;
748 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
750 + sprintf(name, ".unionfs%*.*lx",
751 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
754 + * Loop, looking for an unused temp name to copyup to.
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.
765 + char *suffix = name + nlen - countersize;
769 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
771 + pr_debug("unionfs: trying to rename %s to %s\n",
772 + dentry->d_name.name, name);
774 + tmp_dentry = lookup_lck_len(name, lower_dentry->d_parent,
776 + if (IS_ERR(tmp_dentry)) {
777 + err = PTR_ERR(tmp_dentry);
780 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
783 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
784 + i_size_read(file->f_path.dentry->d_inode));
786 + if (unlikely(err == -EEXIST))
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);
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);
804 + unionfs_check_dentry(dentry);
809 + * put all references held by upper struct file and free lower file pointer
812 +static void cleanup_file(struct file *file)
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;
819 + lower_files = UNIONFS_F(file)->lower_files;
820 + bstart = fbstart(file);
821 + bend = fbend(file);
823 + for (bindex = bstart; bindex <= bend; bindex++) {
824 + int i; /* holds (possibly) updated branch index */
827 + lower_file = unionfs_lower_file_idx(file, bindex);
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.
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);
844 + /* decrement count of open files */
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.
855 + UNIONFS_F(file)->lower_files[bindex] = NULL;
856 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
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;
866 +/* open all lower files for a given file */
867 +static int open_all_files(struct file *file)
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;
875 + bstart = dbstart(dentry);
876 + bend = dbend(dentry);
878 + for (bindex = bstart; bindex <= bend; bindex++) {
879 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
883 + dget(lower_dentry);
884 + unionfs_mntget(dentry, bindex);
885 + branchget(sb, bindex);
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);
896 + unionfs_set_lower_file_idx(file, bindex, lower_file);
903 +/* open the highest priority file for a given upper file */
904 +static int open_highest_file(struct file *file, bool willwrite)
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;
914 + bstart = dbstart(dentry);
915 + bend = dbend(dentry);
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));
925 + atomic_set(&UNIONFS_F(file)->generation,
926 + atomic_read(&UNIONFS_I(dentry->d_inode)->
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);
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;
945 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
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)
954 + int bindex, bstart, bend, err = 0;
955 + struct dentry *dentry = file->f_path.dentry;
956 + struct inode *parent_inode = parent->d_inode;
958 + bstart = fbstart(file);
959 + bend = fbend(file);
961 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
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,
968 + i_size_read(dentry->d_inode));
970 + err = copyup_deleted_file(file, dentry, parent,
972 + /* if succeeded, set lower open-file flags and break */
974 + struct file *lower_file;
975 + lower_file = unionfs_lower_file_idx(file, bindex);
976 + lower_file->f_flags = file->f_flags;
980 + if (err || (bstart <= fbstart(file)))
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);
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);
998 + unionfs_check_file(file);
1003 + * Helper function for unionfs_file_revalidate/locked.
1004 + * Expects dentry/parent to be locked already, and revalidated.
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)
1012 + int bstart, bend, orig_brid;
1016 + fgen = atomic_read(&UNIONFS_F(file)->generation);
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.
1024 + if (d_deleted(dentry) ||
1026 + dbstart(dentry) == fbstart(file) &&
1027 + unionfs_lower_file(file)))
1028 + goto out_may_copyup;
1030 + /* save orig branch ID */
1031 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1033 + /* First we throw out the existing files. */
1034 + cleanup_file(file);
1036 + /* Now we reopen the file(s) as in unionfs_open. */
1037 + bstart = fbstart(file) = dbstart(dentry);
1038 + bend = fbend(file) = dbend(dentry);
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)) {
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)) {
1053 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1054 + /* We need to open all the files. */
1055 + err = open_all_files(file);
1060 + /* We only open the highest priority branch. */
1061 + err = open_highest_file(file, willwrite);
1064 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1065 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
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.
1072 + unionfs_mntget(dentry, bstart);
1073 + unionfs_mntput(sb->s_root,
1074 + branch_id_to_idx(sb, orig_brid));
1076 + /* regular files have only one open lower file */
1077 + fbend(file) = fbstart(file);
1079 + atomic_set(&UNIONFS_F(file)->generation,
1080 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
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);
1097 + kfree(UNIONFS_F(file)->lower_files);
1098 + kfree(UNIONFS_F(file)->saved_branch_ids);
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.
1110 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1113 + struct super_block *sb;
1114 + struct dentry *dentry;
1118 + dentry = file->f_path.dentry;
1119 + sb = dentry->d_sb;
1120 + verify_locked(dentry);
1121 + verify_locked(parent);
1124 + * First revalidate the dentry inside struct file,
1125 + * but not unhashed dentries.
1127 + if (!d_deleted(dentry) &&
1128 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1133 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1134 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1136 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1137 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1138 + dentry->d_name.name);
1143 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1144 + sbgen, dgen, willwrite);
1149 +/* unionfs_open helper function: open a directory */
1150 +static int __open_dir(struct inode *inode, struct file *file)
1152 + struct dentry *lower_dentry;
1153 + struct file *lower_file;
1154 + int bindex, bstart, bend;
1155 + struct vfsmount *mnt;
1157 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1158 + bend = fbend(file) = dbend(file->f_path.dentry);
1160 + for (bindex = bstart; bindex <= bend; bindex++) {
1162 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1163 + if (!lower_dentry)
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,
1171 + if (IS_ERR(lower_file))
1172 + return PTR_ERR(lower_file);
1174 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1177 + * The branchget goes after the open, because otherwise
1178 + * we would miss the reference on release.
1180 + branchget(inode->i_sb, bindex);
1186 +/* unionfs_open helper function: open a file */
1187 +static int __open_file(struct inode *inode, struct file *file,
1188 + struct dentry *parent)
1190 + struct dentry *lower_dentry;
1191 + struct file *lower_file;
1193 + int bindex, bstart, bend;
1195 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1196 + lower_flags = file->f_flags;
1198 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1199 + bend = fbend(file) = dbend(file->f_path.dentry);
1202 + * check for the permission for lower file. If the error is
1203 + * COPYUP_ERR, copyup the file.
1205 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1207 + * if the open will change the file, copy it up otherwise
1210 + if (lower_flags & O_TRUNC) {
1214 + /* copyup the file */
1215 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1216 + err = copyup_file(parent->d_inode, file,
1217 + bstart, bindex, size);
1224 + * turn off writeable flags, to force delayed copyup
1227 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1231 + dget(lower_dentry);
1234 + * dentry_open will decrement mnt refcnt if err.
1235 + * otherwise fput() will do an mntput() for us upon file close.
1237 + unionfs_mntget(file->f_path.dentry, bstart);
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);
1245 + unionfs_set_lower_file(file, lower_file);
1246 + branchget(inode->i_sb, bstart);
1251 +int unionfs_open(struct inode *inode, struct file *file)
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;
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);
1265 + /* don't open unhashed/deleted files */
1266 + if (d_deleted(dentry)) {
1271 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1272 + valid = __unionfs_d_revalidate(dentry, parent, false);
1273 + if (unlikely(!valid)) {
1278 + file->private_data =
1279 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1280 + if (unlikely(!UNIONFS_F(file))) {
1284 + fbstart(file) = -1;
1286 + atomic_set(&UNIONFS_F(file)->generation,
1287 + atomic_read(&UNIONFS_I(inode)->generation));
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)) {
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)) {
1302 + bstart = fbstart(file) = dbstart(dentry);
1303 + bend = fbend(file) = dbend(dentry);
1306 + * open all directories and make the unionfs file struct point to
1307 + * these lower file structs
1309 + if (S_ISDIR(inode->i_mode))
1310 + err = __open_dir(inode, file); /* open a dir */
1312 + err = __open_file(inode, file, parent); /* open a file */
1314 + /* freeing the allocated resources, and fput the opened files */
1316 + for (bindex = bstart; bindex <= bend; bindex++) {
1317 + lower_file = unionfs_lower_file_idx(file, bindex);
1321 + branchput(dentry->d_sb, bindex);
1322 + /* fput calls dput for lower_dentry */
1329 + kfree(UNIONFS_F(file)->lower_files);
1330 + kfree(UNIONFS_F(file)->saved_branch_ids);
1331 + kfree(UNIONFS_F(file));
1335 + unionfs_postcopyup_setmnt(dentry);
1336 + unionfs_copy_attr_times(inode);
1337 + unionfs_check_file(file);
1338 + unionfs_check_inode(inode);
1340 + unionfs_unlock_dentry(dentry);
1341 + unionfs_unlock_parent(dentry, parent);
1342 + unionfs_read_unlock(inode->i_sb);
1347 + * release all lower object references & free the file info structure
1349 + * No need to grab sb info's rwsem.
1351 +int unionfs_file_release(struct inode *inode, struct file *file)
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;
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.
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);
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.
1387 + err = unionfs_file_revalidate(file, parent,
1388 + UNIONFS_F(file)->wrote_to_file);
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);
1395 + /* fput all the lower files */
1396 + fgen = atomic_read(&fileinfo->generation);
1397 + bstart = fbstart(file);
1398 + bend = fbend(file);
1400 + for (bindex = bstart; bindex <= bend; bindex++) {
1401 + lower_file = unionfs_lower_file_idx(file, bindex);
1404 + unionfs_set_lower_file_idx(file, bindex, NULL);
1406 + branchput(sb, bindex);
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);
1416 + kfree(fileinfo->lower_files);
1417 + kfree(fileinfo->saved_branch_ids);
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;
1431 + unionfs_unlock_dentry(dentry);
1432 + unionfs_unlock_parent(dentry, parent);
1433 + unionfs_read_unlock(sb);
1438 +/* pass the ioctl to the lower fs */
1439 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1441 + struct file *lower_file;
1444 + lower_file = unionfs_lower_file(file);
1447 + if (!lower_file || !lower_file->f_op)
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) {
1453 + err = lower_file->f_op->ioctl(
1454 + lower_file->f_path.dentry->d_inode,
1455 + lower_file, cmd, arg);
1464 + * return to user-space the branch indices containing the file in question
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
1469 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1470 + unsigned int cmd, unsigned long arg)
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;
1479 + dentry = file->f_path.dentry;
1480 + orig_bstart = dbstart(dentry);
1481 + orig_bend = dbend(dentry);
1482 + err = unionfs_partial_lookup(dentry, parent);
1485 + bstart = dbstart(dentry);
1486 + bend = dbend(dentry);
1488 + FD_ZERO(&branchlist);
1490 + for (bindex = bstart; bindex <= bend; bindex++) {
1491 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1492 + if (!lower_dentry)
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,
1503 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1506 + unionfs_mntput(dentry, bindex);
1507 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
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;
1516 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1517 + if (unlikely(err))
1521 + return err < 0 ? err : bend;
1524 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1527 + struct dentry *dentry = file->f_path.dentry;
1528 + struct dentry *parent;
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);
1534 + err = unionfs_file_revalidate(file, parent, true);
1535 + if (unlikely(err))
1538 + /* check if asked for local commands */
1540 + case UNIONFS_IOCTL_INCGEN:
1541 + /* Increment the superblock generation count */
1542 + pr_info("unionfs: incgen ioctl deprecated; "
1543 + "use \"-o remount,incgen\"\n");
1547 + case UNIONFS_IOCTL_QUERYFILE:
1548 + /* Return list of branches containing the given file */
1549 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1553 + /* pass the ioctl down */
1554 + err = do_ioctl(file, cmd, arg);
1559 + unionfs_check_file(file);
1560 + unionfs_unlock_dentry(dentry);
1561 + unionfs_unlock_parent(dentry, parent);
1562 + unionfs_read_unlock(dentry->d_sb);
1566 +int unionfs_flush(struct file *file, fl_owner_t id)
1569 + struct file *lower_file = NULL;
1570 + struct dentry *dentry = file->f_path.dentry;
1571 + struct dentry *parent;
1572 + int bindex, bstart, bend;
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);
1578 + err = unionfs_file_revalidate(file, parent,
1579 + UNIONFS_F(file)->wrote_to_file);
1580 + if (unlikely(err))
1582 + unionfs_check_file(file);
1584 + bstart = fbstart(file);
1585 + bend = fbend(file);
1586 + for (bindex = bstart; bindex <= bend; bindex++) {
1587 + lower_file = unionfs_lower_file_idx(file, bindex);
1589 + if (lower_file && lower_file->f_op &&
1590 + lower_file->f_op->flush) {
1591 + err = lower_file->f_op->flush(lower_file, id);
1600 + unionfs_check_file(file);
1601 + unionfs_unlock_dentry(dentry);
1602 + unionfs_unlock_parent(dentry, parent);
1603 + unionfs_read_unlock(dentry->d_sb);
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
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
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.
1631 + * For detailed explanation of copyup see:
1632 + * Documentation/filesystems/unionfs/concepts.txt
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)
1641 + ssize_t list_size = -1;
1642 + char *name_list = NULL;
1643 + char *attr_value = NULL;
1644 + char *name_list_buf = NULL;
1646 + /* query the actual size of the xattr list */
1647 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1648 + if (list_size <= 0) {
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);
1660 + name_list_buf = name_list; /* save for kfree at end */
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) {
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);
1676 + /* in a loop, get and set each xattr from src to dst file */
1677 + while (*name_list) {
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);
1689 + if (size > XATTR_SIZE_MAX) {
1693 + /* Don't lock here since vfs_setxattr does it for us. */
1694 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
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.
1703 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1704 + const struct cred *old_creds;
1705 + struct cred *new_creds;
1707 + new_creds = prepare_creds();
1708 + if (unlikely(!new_creds)) {
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);
1720 + name_list += strlen(name_list) + 1;
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)
1730 +#endif /* CONFIG_UNION_FS_XATTR */
1733 + * Determine the mode based on the copyup flags, and the existing dentry.
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.
1740 +static int copyup_permissions(struct super_block *sb,
1741 + struct dentry *old_lower_dentry,
1742 + struct dentry *new_lower_dentry)
1744 + struct inode *i = old_lower_dentry->d_inode;
1745 + struct iattr newattrs;
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);
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);
1774 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1779 + * create the new device/file/directory - use copyup_permission to copyup
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
1785 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1786 + struct dentry *new_lower_dentry,
1787 + struct dentry *new_lower_parent_dentry,
1791 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1792 + struct sioq_args args;
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;
1799 + run_sioq(__unionfs_mkdir, &args);
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;
1806 + run_sioq(__unionfs_symlink, &args);
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;
1815 + run_sioq(__unionfs_mknod, &args);
1817 + } else if (S_ISREG(old_mode)) {
1818 + struct nameidata nd;
1819 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1820 + if (unlikely(err < 0))
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;
1827 + run_sioq(__unionfs_create, &args);
1829 + release_lower_nd(&nd, err);
1831 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
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)
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;
1851 + ssize_t read_bytes, write_bytes;
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);
1867 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1869 + goto out_close_in;
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;
1882 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1884 + goto out_close_out;
1887 + /* allocating a buffer */
1888 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1889 + if (unlikely(!buf)) {
1891 + goto out_close_out;
1894 + input_file->f_pos = 0;
1895 + output_file->f_pos = 0;
1897 + old_fs = get_fs();
1898 + set_fs(KERNEL_DS);
1903 + if (len >= PAGE_SIZE)
1905 + else if ((len < PAGE_SIZE) && (len > 0))
1911 + input_file->f_op->read(input_file,
1912 + (char __user *)buf, size,
1913 + &input_file->f_pos);
1914 + if (read_bytes <= 0) {
1919 + /* see Documentation/filesystems/unionfs/issues.txt */
1922 + output_file->f_op->write(output_file,
1923 + (char __user *)buf,
1925 + &output_file->f_pos);
1927 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
1928 + err = write_bytes;
1931 + } while ((read_bytes > 0) && (len > 0));
1938 + err = output_file->f_op->fsync(output_file,
1939 + new_lower_dentry, 0);
1942 + goto out_close_out;
1944 + if (copyup_file) {
1945 + *copyup_file = output_file;
1946 + goto out_close_in;
1950 + fput(output_file);
1953 + branchput(sb, new_bindex);
1959 + branchput(sb, old_bindex);
1965 + * dput the lower references for old and new dentry & clear a lower dentry
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)
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;
1977 + dput(new_lower_dentry);
1978 + dput(old_lower_dentry);
1982 + * Copy up a dentry to a file of specified name.
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?
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)
1997 + struct dentry *new_lower_dentry;
1998 + struct dentry *old_lower_dentry = NULL;
1999 + struct super_block *sb;
2004 + struct dentry *new_lower_parent_dentry = NULL;
2005 + mm_segment_t oldfs;
2006 + char *symbuf = NULL;
2008 + verify_locked(dentry);
2010 + old_bindex = bstart;
2011 + old_bstart = dbstart(dentry);
2012 + old_bend = dbend(dentry);
2014 + BUG_ON(new_bindex < 0);
2015 + BUG_ON(new_bindex >= old_bindex);
2019 + err = is_robranch_super(sb, new_bindex);
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);
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);
2034 + /* For symlinks, we must read the link before we lock the directory. */
2035 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
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);
2047 + set_fs(KERNEL_DS);
2048 + err = old_lower_dentry->d_inode->i_op->readlink(
2050 + (char __user *)symbuf,
2054 + __clear(dentry, old_lower_dentry,
2055 + old_bstart, old_bend,
2056 + new_lower_dentry, new_bindex);
2059 + symbuf[err] = '\0';
2062 + /* Now we lock the parent, and create the object in the new branch. */
2063 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2065 + /* create the new inode */
2066 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2067 + new_lower_parent_dentry, symbuf);
2070 + __clear(dentry, old_lower_dentry,
2071 + old_bstart, old_bend,
2072 + new_lower_dentry, new_bindex);
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);
2084 + /* Set permissions. */
2085 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2089 +#ifdef CONFIG_UNION_FS_XATTR
2090 + /* Selinux uses extended attributes for permissions. */
2091 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2094 +#endif /* CONFIG_UNION_FS_XATTR */
2096 + /* do not allow files getting deleted to be re-interposed */
2097 + if (!d_deleted(dentry))
2098 + unionfs_reinterpose(dentry);
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
2108 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2110 + if (copyup_file) {
2111 + /* need to close the file */
2113 + fput(*copyup_file);
2114 + branchput(sb, new_bindex);
2118 + * TODO: should we reset the error to something like -EIO?
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.
2126 + unlock_dir(new_lower_parent_dentry);
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.
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);
2141 + * if directory creation succeeded, but inode copyup failed,
2142 + * then purge new dentries.
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));
2150 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2151 + unionfs_postcopyup_release(dentry);
2152 + if (!unionfs_lower_inode(dentry->d_inode)) {
2154 + * If we got here, then we copied up to an
2155 + * unlinked-open file, whose name is .unionfsXXXXX.
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),
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);
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".
2178 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2179 + int bstart, int new_bindex, loff_t len)
2182 + struct file *output_file = NULL;
2184 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2185 + name, strlen(name), &output_file, len);
2187 + fbstart(file) = new_bindex;
2188 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2195 + * This function creates a copy of a file represented by 'file' which
2196 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2198 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2199 + int new_bindex, loff_t len)
2202 + struct file *output_file = NULL;
2203 + struct dentry *dentry = file->f_path.dentry;
2205 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2206 + dentry->d_name.name, dentry->d_name.len,
2207 + &output_file, len);
2209 + fbstart(file) = new_bindex;
2210 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
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)
2222 + int new_bstart = -1;
2223 + int new_bend = -1;
2226 + loop_start = min(old_bstart, bindex);
2227 + loop_end = max(old_bend, bindex);
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
2234 + for (i = loop_start; i <= loop_end; i++) {
2235 + if (!unionfs_lower_dentry_idx(dentry, i))
2238 + if (i == bindex) {
2240 + if (new_bstart < 0)
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);
2249 + unionfs_mntput(dentry, i);
2250 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2252 + if (new_bstart < 0)
2258 + if (new_bstart < 0)
2259 + new_bstart = bindex;
2261 + new_bend = bindex;
2262 + dbstart(dentry) = new_bstart;
2263 + dbend(dentry) = new_bend;
2267 +/* set lower inode ptr and update bstart & bend if necessary */
2268 +static void __set_inode(struct dentry *upper, struct dentry *lower,
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;
2280 +/* set lower dentry ptr and update bstart & bend if necessary */
2281 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
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;
2292 + * This function replicates the directory structure up-to given dentry
2293 + * in the bindex branch.
2295 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2296 + const char *name, int bindex)
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;
2309 + struct dentry **path = NULL;
2310 + struct super_block *sb;
2312 + verify_locked(dentry);
2314 + err = is_robranch_super(dir->i_sb, bindex);
2316 + lower_dentry = ERR_PTR(err);
2320 + old_bstart = dbstart(dentry);
2321 + old_bend = dbend(dentry);
2323 + lower_dentry = ERR_PTR(-ENOMEM);
2325 + /* There is no sense allocating any less than the minimum. */
2327 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2328 + if (unlikely(!path))
2331 + /* assume the negative dentry of unionfs as the parent dentry */
2332 + parent_dentry = dentry;
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
2344 + child_dentry = parent_dentry;
2346 + /* find the parent directory dentry in unionfs */
2347 + parent_dentry = dget_parent(child_dentry);
2349 + /* find out the lower_parent_dentry in the given branch */
2350 + lower_parent_dentry =
2351 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2353 + /* grow path table */
2354 + if (count == nr_dentry) {
2358 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2360 + if (unlikely(!p)) {
2361 + lower_dentry = ERR_PTR(-ENOMEM);
2367 + /* store the child dentry */
2368 + path[count++] = child_dentry;
2369 + } while (!lower_parent_dentry);
2372 + sb = dentry->d_sb;
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.
2380 + /* get lower parent dir in the current branch */
2381 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2382 + dput(parent_dentry);
2384 + /* init the values to lookup */
2385 + childname = child_dentry->d_name.name;
2386 + childnamelen = child_dentry->d_name.len;
2388 + if (child_dentry != dentry) {
2389 + /* lookup child in the underlying file system */
2390 + lower_dentry = lookup_lck_len(childname, lower_parent_dentry,
2392 + if (IS_ERR(lower_dentry))
2396 + * Is the name a whiteout of the child name ? lookup the
2397 + * whiteout child in the underlying file system
2399 + lower_dentry = lookup_lck_len(name, lower_parent_dentry,
2401 + if (IS_ERR(lower_dentry))
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,
2409 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2413 + if (lower_dentry->d_inode) {
2415 + * since this already exists we dput to avoid
2416 + * multiple references on the same dentry
2418 + dput(lower_dentry);
2420 + struct sioq_args args;
2422 + /* it's a negative dentry, create a new dir */
2423 + lower_parent_dentry = lock_parent(lower_dentry);
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;
2429 + run_sioq(__unionfs_mkdir, &args);
2433 + err = copyup_permissions(dir->i_sb, child_dentry,
2435 + unlock_dir(lower_parent_dentry);
2437 + dput(lower_dentry);
2438 + lower_dentry = ERR_PTR(err);
2444 + __set_inode(child_dentry, lower_dentry, bindex);
2445 + __set_dentry(child_dentry, lower_dentry, bindex);
2447 + * update times of this dentry, but also the parent, because if
2448 + * we changed, the parent may have changed too.
2450 + fsstack_copy_attr_times(parent_dentry->d_inode,
2451 + lower_parent_dentry->d_inode);
2452 + unionfs_copy_attr_times(child_dentry->d_inode);
2454 + parent_dentry = child_dentry;
2455 + child_dentry = path[--count];
2458 + /* cleanup any leftover locks from the do/while loop above */
2459 + if (IS_ERR(lower_dentry))
2461 + dput(path[count--]);
2463 + return lower_dentry;
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.
2470 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2472 + struct dentry *parent, *hasone;
2473 + int bindex = dbstart(dentry);
2475 + if (unionfs_lower_mnt_idx(dentry, bindex))
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;
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;
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.
2493 +void unionfs_postcopyup_release(struct dentry *dentry)
2497 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2498 + bstart = dbstart(dentry);
2499 + bend = dbend(dentry);
2501 + path_put_lowers(dentry, bstart + 1, bend, false);
2502 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2504 + dbend(dentry) = bstart;
2505 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
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
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
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.
2525 + * Helper debugging functions for maintainers (and for users to report back
2526 + * useful information back to maintainers)
2529 +/* it's always useful to know what part of the code called us */
2530 +#define PRINT_CALLER(fname, fxn, line) \
2532 + if (!printed_caller) { \
2533 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2534 + printed_caller = 1; \
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.
2547 +void __unionfs_check_inode(const struct inode *inode,
2548 + const char *fname, const char *fxn, int line)
2552 + struct inode *lower_inode;
2553 + struct super_block *sb;
2554 + int printed_caller = 0;
2557 + /* for inodes now */
2560 + istart = ibstart(inode);
2561 + iend = ibend(inode);
2562 + /* don't check inode if no lower branches */
2563 + if (istart < 0 && iend < 0)
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);
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);
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);
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);
2590 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2591 + PRINT_CALLER(fname, fxn, line);
2592 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
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);
2612 + /* if we get here, then lower_inode == NULL */
2613 + if (bindex < istart || bindex > iend)
2616 + * directories can have NULL lower inodes in b/t start/end,
2617 + * but NOT if at the start/end range.
2619 + if (unlikely(S_ISDIR(inode->i_mode) &&
2620 + bindex > istart && bindex < iend))
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);
2629 +void __unionfs_check_dentry(const struct dentry *dentry,
2630 + const char *fname, const char *fxn, int line)
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;
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)
2649 + BUG_ON(dstart > dend);
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);
2658 + * check for NULL dentries inside the start/end range, or
2659 + * non-NULL dentries outside the start/end range.
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 :
2671 + bindex, dstart, dend);
2673 + } else { /* lower_dentry == NULL */
2674 + if (bindex < dstart || bindex > dend)
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.
2682 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
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,
2691 + lower_dentry->d_inode :
2693 + bindex, dstart, dend);
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);
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);
2708 + } else { /* lower_mnt == NULL */
2709 + if (bindex < dstart || bindex > dend)
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
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,
2730 + /* for inodes now */
2733 + istart = ibstart(inode);
2734 + iend = ibend(inode);
2735 + /* don't check inode if no lower branches */
2736 + if (istart < 0 && iend < 0)
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);
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);
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);
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);
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);
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);
2787 + /* if we get here, then lower_inode == NULL */
2788 + if (bindex < istart || bindex > iend)
2791 + * directories can have NULL lower inodes in b/t start/end,
2792 + * but NOT if at the start/end range.
2794 + if (unlikely(S_ISDIR(inode->i_mode) &&
2795 + bindex > istart && bindex < iend))
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);
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,
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,
2813 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2814 + if (unlikely(!((lower_inode && lower_dentry &&
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);
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);
2830 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2831 + if (unlikely(!lower_inode))
2833 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2834 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
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);
2848 +void __unionfs_check_file(const struct file *file,
2849 + const char *fname, const char *fxn, int line)
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;
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);
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);
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);
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);
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);
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);
2900 + * check for NULL dentries inside the start/end range, or
2901 + * non-NULL dentries outside the start/end range.
2903 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2904 + lower_file = unionfs_lower_file_idx(file, bindex);
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);
2912 + } else { /* lower_file == NULL */
2913 + if (bindex >= fstart && bindex <= fend) {
2915 + * directories can have NULL lower inodes in
2916 + * b/t start/end, but NOT if at the
2917 + * start/end range.
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,
2932 + __unionfs_check_dentry(dentry, fname, fxn, line);
2935 +void __unionfs_check_nd(const struct nameidata *nd,
2936 + const char *fname, const char *fxn, int line)
2938 + struct file *file;
2939 + int printed_caller = 0;
2941 + if (unlikely(!nd))
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,
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);
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)
2961 + struct vfsmount *mnt;
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;
2969 + printk(KERN_CONT "%d:",
2970 + (mnt ? atomic_read(&mnt->mnt_count) : -99));
2972 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
2975 +void __show_inode_times(const struct inode *inode,
2976 + const char *file, const char *fxn, int line)
2978 + struct inode *lower_inode;
2981 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
2982 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2983 + if (unlikely(!lower_inode))
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,
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);
2998 +void __show_dinode_times(const struct dentry *dentry,
2999 + const char *file, const char *fxn, int line)
3001 + struct inode *inode = dentry->d_inode;
3002 + struct inode *lower_inode;
3005 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3006 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
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,
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);
3022 +void __show_inode_counts(const struct inode *inode,
3023 + const char *file, const char *fxn, int line)
3025 + struct inode *lower_inode;
3028 + if (unlikely(!inode)) {
3029 + pr_debug("SiC: Null inode\n");
3032 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3034 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3035 + if (unlikely(!lower_inode))
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),
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
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
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.
3068 +bool is_negative_lower(const struct dentry *dentry)
3071 + struct dentry *lower_dentry;
3074 + /* cache coherency: check if file was deleted on lower branch */
3075 + if (dbstart(dentry) < 0)
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))
3088 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3090 + struct dentry *lower_dentry;
3095 + for (bindex = start; bindex <= end; bindex++) {
3096 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3097 + if (!lower_dentry)
3099 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3100 + dput(lower_dentry);
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.
3109 +static inline void purge_inode_data(struct inode *inode)
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);
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.
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.
3129 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
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;
3138 + int interpose_flag;
3140 + verify_locked(dentry);
3141 + verify_locked(parent);
3143 + /* if the dentry is unhashed, do NOT revalidate */
3144 + if (d_deleted(dentry))
3147 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
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);
3155 + * reset generation number to zero, guaranteed to be
3159 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3162 + purge_inode_data(dentry->d_inode);
3165 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3167 + BUG_ON(dbstart(dentry) == -1);
3168 + if (dentry->d_inode)
3171 + /* if our dentry is valid, then validate all lower ones */
3172 + if (sbgen == dgen)
3173 + goto validate_lowers;
3175 + /* The root entry should always be valid */
3176 + BUG_ON(IS_ROOT(dentry));
3178 + /* We can't work correctly if our parent isn't valid. */
3179 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3181 + /* Free the pointers for our inodes and this dentry. */
3182 + path_put_lowers_all(dentry, false);
3184 + interpose_flag = INTERPOSE_REVAL_NEG;
3186 + interpose_flag = INTERPOSE_REVAL;
3187 + iput_lowers_all(dentry->d_inode, true);
3190 + if (realloc_dentry_private_data(dentry) != 0) {
3195 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3197 + if (IS_ERR(result)) {
3202 + * current unionfs_lookup_backend() doesn't return
3209 + if (unlikely(positive && is_negative_lower(dentry))) {
3210 + /* call make_bad_inode here ? */
3217 + * if we got here then we have revalidated our dentry and all lower
3218 + * ones, so we can return safely.
3220 + if (!valid) /* lower dentry revalidation failed */
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.
3231 + if (pdgen == sbgen)
3232 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
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)
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
3254 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3258 + if (!dentry->d_inode ||
3259 + ibstart(dentry->d_inode) < 0 ||
3260 + ibend(dentry->d_inode) < 0) {
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.
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));
3284 + * Determine if the lower inode objects have changed from below the unionfs
3285 + * inode. Return true if changed, false otherwise.
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.
3299 +bool is_newer_lower(const struct dentry *dentry)
3302 + struct inode *inode;
3303 + struct inode *lower_inode;
3305 + /* ignore if we're called on semi-initialized dentries/inodes */
3306 + if (!dentry || !UNIONFS_D(dentry))
3308 + inode = dentry->d_inode;
3309 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3310 + ibstart(inode) < 0 || ibend(inode) < 0)
3313 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3314 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
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);
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);
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.
3349 + if (is_negative_lower(dentry))
3352 + return false; /* default: lower is not newer */
3355 +static int unionfs_d_revalidate(struct dentry *dentry,
3356 + struct nameidata *nd_unused)
3358 + bool valid = true;
3359 + int err = 1; /* 1 means valid for the VFS */
3360 + struct dentry *parent;
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);
3366 + valid = __unionfs_d_revalidate(dentry, parent, false);
3368 + unionfs_postcopyup_setmnt(dentry);
3369 + unionfs_check_dentry(dentry);
3374 + unionfs_unlock_dentry(dentry);
3375 + unionfs_unlock_parent(dentry, parent);
3376 + unionfs_read_unlock(dentry->d_sb);
3381 +static void unionfs_d_release(struct dentry *dentry)
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);
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 */
3396 + /* Release all the lower dentries */
3397 + path_put_lowers_all(dentry, true);
3399 + unionfs_unlock_dentry(dentry);
3402 + free_dentry_private_data(dentry);
3403 + unionfs_read_unlock(dentry->d_sb);
3408 + * Called when we're removing the last reference to our dentry. So we
3409 + * should drop all lower references too.
3411 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3416 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3417 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3419 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3420 + goto drop_lower_inodes;
3421 + path_put_lowers_all(dentry, false);
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 */
3428 + iput(unionfs_lower_inode(inode));
3430 + unionfs_set_lower_inode(inode, NULL);
3431 + /* XXX: may need to set start/end to -1? */
3436 + unionfs_unlock_dentry(dentry);
3437 + unionfs_read_unlock(dentry->d_sb);
3440 +struct dentry_operations unionfs_dops = {
3441 + .d_revalidate = unionfs_d_revalidate,
3442 + .d_release = unionfs_d_release,
3443 + .d_iput = unionfs_d_iput,
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
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
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.
3469 +/* Make sure our rdstate is playing by the rules. */
3470 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3472 + BUG_ON(rdstate->offset >= DIREOF);
3473 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3476 +struct unionfs_getdents_callback {
3477 + struct unionfs_dir_state *rdstate;
3479 + int entries_written;
3480 + int filldir_called;
3481 + int filldir_error;
3482 + filldir_t filldir;
3483 + struct super_block *sb;
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)
3490 + struct unionfs_getdents_callback *buf = dirent;
3491 + struct filldir_node *found = NULL;
3494 + char *name = (char *) oname;
3496 + buf->filldir_called++;
3498 + is_whiteout = is_whiteout_name(&name, &namelen);
3500 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
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.
3507 + if (is_whiteout && !found->whiteout)
3508 + found->whiteout = is_whiteout;
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;
3517 + err = buf->filldir(buf->dirent, name, namelen, pos,
3518 + unionfs_ino, d_type);
3519 + buf->rdstate->offset++;
3520 + verify_rdstate_offset(buf->rdstate);
3523 + * If we did fill it, stuff it in our hash, otherwise return an
3527 + buf->filldir_error = err;
3530 + buf->entries_written++;
3531 + err = add_filldir_node(buf->rdstate, name, namelen,
3532 + buf->rdstate->bindex, is_whiteout);
3534 + buf->filldir_error = err;
3540 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
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;
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);
3556 + err = unionfs_file_revalidate(file, parent, false);
3557 + if (unlikely(err))
3560 + inode = dentry->d_inode;
3562 + uds = UNIONFS_F(file)->rdstate;
3564 + if (file->f_pos == DIREOF) {
3566 + } else if (file->f_pos > 0) {
3567 + uds = find_rdstate(inode, file->f_pos);
3568 + if (unlikely(!uds)) {
3572 + UNIONFS_F(file)->rdstate = uds;
3574 + init_rdstate(file);
3575 + uds = UNIONFS_F(file)->rdstate;
3578 + bend = fbend(file);
3580 + while (uds->bindex <= bend) {
3581 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3582 + if (!lower_file) {
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;
3597 + /* Read starting from where we last left off. */
3598 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3603 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3605 + /* Save the position for when we continue. */
3606 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3611 + uds->dirpos = offset;
3613 + /* Copy the atime. */
3614 + fsstack_copy_attr_atime(inode,
3615 + lower_file->f_path.dentry->d_inode);
3620 + if (buf.filldir_error)
3623 + if (!buf.entries_written) {
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;
3636 + file->f_pos = rdstate2offset(uds);
3641 + unionfs_check_file(file);
3642 + unionfs_unlock_dentry(dentry);
3643 + unionfs_unlock_parent(dentry, parent);
3644 + unionfs_read_unlock(dentry->d_sb);
3649 + * This is not meant to be a generic repositioning function. If you do
3650 + * things that aren't supported, then we return EINVAL.
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.
3658 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3660 + struct unionfs_dir_state *rdstate;
3661 + struct dentry *dentry = file->f_path.dentry;
3662 + struct dentry *parent;
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);
3669 + err = unionfs_file_revalidate(file, parent, false);
3670 + if (unlikely(err))
3673 + rdstate = UNIONFS_F(file)->rdstate;
3676 + * we let users seek to their current position, but not anywhere
3683 + free_rdstate(rdstate);
3684 + UNIONFS_F(file)->rdstate = NULL;
3686 + init_rdstate(file);
3690 + err = file->f_pos;
3693 + /* Unsupported, because we would break everything. */
3701 + if (offset == rdstate2offset(rdstate))
3703 + else if (file->f_pos == DIREOF)
3708 + struct inode *inode;
3709 + inode = dentry->d_inode;
3710 + rdstate = find_rdstate(inode, offset);
3712 + UNIONFS_F(file)->rdstate = rdstate;
3713 + err = rdstate->offset;
3721 + /* Unsupported, because we would break everything. */
3729 + unionfs_check_file(file);
3730 + unionfs_unlock_dentry(dentry);
3731 + unionfs_unlock_parent(dentry, parent);
3732 + unionfs_read_unlock(dentry->d_sb);
3737 + * Trimmed directory options, we shouldn't pass everything down since
3738 + * we don't want to operate on partial directories.
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,
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
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
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.
3776 +#define RD_CHECK_EMPTY 1
3777 +/* The callback structure for check_empty. */
3778 +struct unionfs_rdutil_callback {
3780 + int filldir_called;
3781 + struct unionfs_dir_state *rdstate;
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)
3790 + struct unionfs_rdutil_callback *buf = dirent;
3792 + struct filldir_node *found;
3793 + char *name = (char *) oname;
3795 + buf->filldir_called = 1;
3797 + if (name[0] == '.' && (namelen == 1 ||
3798 + (name[1] == '.' && namelen == 2)))
3801 + is_whiteout = is_whiteout_name(&name, &namelen);
3803 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3804 + /* If it was found in the table there was a previous whiteout. */
3809 + * if it wasn't found and isn't a whiteout, the directory isn't
3813 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3816 + err = add_filldir_node(buf->rdstate, name, namelen,
3817 + buf->rdstate->bindex, is_whiteout);
3824 +/* Is a directory logically empty? */
3825 +int check_empty(struct dentry *dentry, struct dentry *parent,
3826 + struct unionfs_dir_state **namelist)
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;
3836 + sb = dentry->d_sb;
3839 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3841 + err = unionfs_partial_lookup(dentry, parent);
3845 + bstart = dbstart(dentry);
3846 + bend = dbend(dentry);
3847 + bopaque = dbopaque(dentry);
3848 + if (0 <= bopaque && bopaque < bend)
3851 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3852 + if (unlikely(!buf)) {
3857 + buf->mode = RD_CHECK_EMPTY;
3858 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3859 + if (unlikely(!buf->rdstate)) {
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)
3869 + if (!lower_dentry->d_inode)
3871 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
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);
3885 + buf->filldir_called = 0;
3886 + buf->rdstate->bindex = bindex;
3887 + err = vfs_readdir(lower_file,
3888 + readdir_util_callback, buf);
3891 + } while ((err >= 0) && buf->filldir_called);
3893 + /* fput calls dput for lower_dentry */
3895 + branchput(sb, bindex);
3903 + if (namelist && !err)
3904 + *namelist = buf->rdstate;
3905 + else if (buf->rdstate)
3906 + free_rdstate(buf->rdstate);
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
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
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.
3938 + * Inode to private data
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.
3944 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
3946 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
3949 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
3950 +#define ibend(ino) (UNIONFS_I(ino)->bend)
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)
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)
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)
3970 +/* macros to manipulate branch IDs in stored in our superblock */
3971 +static inline int branch_id(struct super_block *sb, int index)
3973 + BUG_ON(!sb || index < 0);
3974 + return UNIONFS_SB(sb)->data[index].branch_id;
3977 +static inline void set_branch_id(struct super_block *sb, int index, int val)
3979 + BUG_ON(!sb || index < 0);
3980 + UNIONFS_SB(sb)->data[index].branch_id = val;
3983 +static inline void new_branch_id(struct super_block *sb, int index)
3985 + BUG_ON(!sb || index < 0);
3986 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
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.
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.
3998 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4001 + for (i = 0; i < sbmax(sb); i++) {
4002 + if (branch_id(sb, i) == id)
4005 + /* in the non-ODF code, this should really never happen */
4006 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4010 +/* File to lower file. */
4011 +static inline struct file *unionfs_lower_file(const struct file *f)
4014 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4017 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4020 + BUG_ON(!f || index < 0);
4021 + return UNIONFS_F(f)->lower_files[index];
4024 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
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);
4034 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4037 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4040 +/* Inode to lower inode. */
4041 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4044 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4047 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4050 + BUG_ON(!i || index < 0);
4051 + return UNIONFS_I(i)->lower_inodes[index];
4054 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4055 + struct inode *val)
4057 + BUG_ON(!i || index < 0);
4058 + UNIONFS_I(i)->lower_inodes[index] = val;
4061 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4064 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4067 +/* Superblock to lower superblock. */
4068 +static inline struct super_block *unionfs_lower_super(
4069 + const struct super_block *sb)
4072 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4075 +static inline struct super_block *unionfs_lower_super_idx(
4076 + const struct super_block *sb,
4079 + BUG_ON(!sb || index < 0);
4080 + return UNIONFS_SB(sb)->data[index].sb;
4083 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4085 + struct super_block *val)
4087 + BUG_ON(!sb || index < 0);
4088 + UNIONFS_SB(sb)->data[index].sb = val;
4091 +static inline void unionfs_set_lower_super(struct super_block *sb,
4092 + struct super_block *val)
4095 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4098 +/* Branch count macros. */
4099 +static inline int branch_count(const struct super_block *sb, int index)
4101 + BUG_ON(!sb || index < 0);
4102 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4105 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4107 + BUG_ON(!sb || index < 0);
4108 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4111 +static inline void branchget(struct super_block *sb, int index)
4113 + BUG_ON(!sb || index < 0);
4114 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4117 +static inline void branchput(struct super_block *sb, int index)
4119 + BUG_ON(!sb || index < 0);
4120 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4123 +/* Dentry macros */
4124 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4125 + struct dentry *val)
4127 + BUG_ON(!dent || index < 0);
4128 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4131 +static inline struct dentry *unionfs_lower_dentry_idx(
4132 + const struct dentry *dent,
4135 + BUG_ON(!dent || index < 0);
4136 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4139 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4142 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4145 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4146 + struct vfsmount *mnt)
4148 + BUG_ON(!dent || index < 0);
4149 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4152 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4153 + const struct dentry *dent,
4156 + BUG_ON(!dent || index < 0);
4157 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4160 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4163 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
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 */
4177 +static inline void unionfs_lock_dentry(struct dentry *d,
4178 + unsigned int subclass)
4181 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4184 +static inline void unionfs_unlock_dentry(struct dentry *d)
4187 + mutex_unlock(&UNIONFS_D(d)->lock);
4190 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4191 + unsigned int subclass)
4196 + p = dget_parent(d);
4198 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4202 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4207 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4208 + mutex_unlock(&UNIONFS_D(p)->lock);
4213 +static inline void verify_locked(struct dentry *d)
4216 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4219 +/* macros to put lower objects */
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.
4226 +static inline void iput_lowers(struct inode *inode,
4227 + int bstart, int bend, bool free_lower)
4229 + struct inode *lower_inode;
4233 + BUG_ON(!UNIONFS_I(inode));
4234 + BUG_ON(bstart < 0);
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 */
4242 + iput(lower_inode);
4248 + kfree(UNIONFS_I(inode)->lower_inodes);
4249 + UNIONFS_I(inode)->lower_inodes = NULL;
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)
4259 + BUG_ON(!UNIONFS_I(inode));
4260 + bstart = ibstart(inode);
4261 + bend = ibend(inode);
4262 + BUG_ON(bstart < 0);
4264 + iput_lowers(inode, bstart, bend, free_lower);
4265 + ibstart(inode) = ibend(inode) = -1;
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.
4273 + * XXX: implement using path_put VFS macros
4275 +static inline void path_put_lowers(struct dentry *dentry,
4276 + int bstart, int bend, bool free_lower)
4278 + struct dentry *lower_dentry;
4279 + struct vfsmount *lower_mnt;
4283 + BUG_ON(!UNIONFS_D(dentry));
4284 + BUG_ON(bstart < 0);
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);
4292 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4294 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4295 + mntput(lower_mnt);
4300 + kfree(UNIONFS_D(dentry)->lower_paths);
4301 + UNIONFS_D(dentry)->lower_paths = NULL;
4306 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4309 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4314 + BUG_ON(!UNIONFS_D(dentry));
4315 + bstart = dbstart(dentry);
4316 + bend = dbend(dentry);
4317 + BUG_ON(bstart < 0);
4319 + path_put_lowers(dentry, bstart, bend, free_lower);
4320 + dbstart(dentry) = dbend(dentry) = -1;
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
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
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.
4348 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4349 + size_t count, loff_t *ppos)
4352 + struct file *lower_file;
4353 + struct dentry *dentry = file->f_path.dentry;
4354 + struct dentry *parent;
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);
4360 + err = unionfs_file_revalidate(file, parent, false);
4361 + if (unlikely(err))
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 */
4368 + fsstack_copy_attr_atime(dentry->d_inode,
4369 + lower_file->f_path.dentry->d_inode);
4370 + unionfs_check_file(file);
4374 + unionfs_unlock_dentry(dentry);
4375 + unionfs_unlock_parent(dentry, parent);
4376 + unionfs_read_unlock(dentry->d_sb);
4380 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4381 + size_t count, loff_t *ppos)
4384 + struct file *lower_file;
4385 + struct dentry *dentry = file->f_path.dentry;
4386 + struct dentry *parent;
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);
4392 + err = unionfs_file_revalidate(file, parent, true);
4393 + if (unlikely(err))
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 */
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);
4409 + unionfs_unlock_dentry(dentry);
4410 + unionfs_unlock_parent(dentry, parent);
4411 + unionfs_read_unlock(dentry->d_sb);
4415 +static int unionfs_file_readdir(struct file *file, void *dirent,
4416 + filldir_t filldir)
4421 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
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;
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.
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);
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))
4454 + unionfs_check_file(file);
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).
4466 + lower_file = unionfs_lower_file(file);
4467 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4469 + printk(KERN_ERR "unionfs: branch %d file system does not "
4470 + "support writeable mmap\n", fbstart(file));
4475 + * find and save lower vm_ops.
4477 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4479 + if (!UNIONFS_F(file)->lower_vm_ops) {
4480 + err = lower_file->f_op->mmap(lower_file, vma);
4482 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4485 + saved_vm_ops = vma->vm_ops;
4486 + err = do_munmap(current->mm, vma->vm_start,
4487 + vma->vm_end - vma->vm_start);
4489 + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4494 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4495 + err = generic_file_mmap(file, vma);
4496 + file->f_mapping->a_ops = &unionfs_aops;
4498 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
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;
4507 + /* copyup could cause parent dir times to change */
4508 + unionfs_copy_attr_times(parent->d_inode);
4509 + unionfs_check_file(file);
4511 + unionfs_unlock_dentry(dentry);
4512 + unionfs_unlock_parent(dentry, parent);
4513 + unionfs_read_unlock(dentry->d_sb);
4518 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
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;
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);
4531 + err = unionfs_file_revalidate(file, parent, true);
4532 + if (unlikely(err))
4534 + unionfs_check_file(file);
4536 + bstart = fbstart(file);
4537 + bend = fbend(file);
4538 + if (bstart < 0 || bend < 0)
4541 + inode = dentry->d_inode;
4542 + if (unlikely(!inode)) {
4544 + "unionfs: null lower inode in unionfs_fsync\n");
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)
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,
4557 + if (!err && bindex == bstart)
4558 + fsstack_copy_attr_times(inode, lower_inode);
4559 + mutex_unlock(&lower_inode->i_mutex);
4566 + unionfs_check_file(file);
4567 + unionfs_unlock_dentry(dentry);
4568 + unionfs_unlock_parent(dentry, parent);
4569 + unionfs_read_unlock(dentry->d_sb);
4573 +int unionfs_fasync(int fd, struct file *file, int flag)
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;
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);
4586 + err = unionfs_file_revalidate(file, parent, true);
4587 + if (unlikely(err))
4589 + unionfs_check_file(file);
4591 + bstart = fbstart(file);
4592 + bend = fbend(file);
4593 + if (bstart < 0 || bend < 0)
4596 + inode = dentry->d_inode;
4597 + if (unlikely(!inode)) {
4599 + "unionfs: null lower inode in unionfs_fasync\n");
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)
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);
4618 + unionfs_check_file(file);
4619 + unionfs_unlock_dentry(dentry);
4620 + unionfs_unlock_parent(dentry, parent);
4621 + unionfs_read_unlock(dentry->d_sb);
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)
4630 + struct file *lower_file;
4631 + struct dentry *dentry = file->f_path.dentry;
4632 + struct dentry *parent;
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);
4638 + err = unionfs_file_revalidate(file, parent, false);
4639 + if (unlikely(err))
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 */
4646 + fsstack_copy_attr_atime(dentry->d_inode,
4647 + lower_file->f_path.dentry->d_inode);
4648 + unionfs_check_file(file);
4652 + unionfs_unlock_dentry(dentry);
4653 + unionfs_unlock_parent(dentry, parent);
4654 + unionfs_read_unlock(dentry->d_sb);
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)
4663 + struct file *lower_file;
4664 + struct dentry *dentry = file->f_path.dentry;
4665 + struct dentry *parent;
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);
4671 + err = unionfs_file_revalidate(file, parent, true);
4672 + if (unlikely(err))
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 */
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);
4687 + unionfs_unlock_dentry(dentry);
4688 + unionfs_unlock_parent(dentry, parent);
4689 + unionfs_read_unlock(dentry->d_sb);
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,
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
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
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.
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).
4737 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4739 +static struct dentry *find_writeable_branch(struct inode *parent,
4740 + struct dentry *dentry)
4742 + int err = -EINVAL;
4743 + int bindex, istart, iend;
4744 + struct dentry *lower_dentry = NULL;
4746 + istart = ibstart(parent);
4747 + iend = ibend(parent);
4752 + for (bindex = istart; bindex <= iend; bindex++) {
4753 + /* skip non-writeable branches */
4754 + err = is_robranch_super(dentry->d_sb, bindex);
4759 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4760 + if (!lower_dentry)
4763 + * check for whiteouts in writeable branch, and remove them
4766 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4767 + if (err > 0) /* ignore if whiteout found and removed */
4771 + /* if get here, we can write to the branch */
4775 + * If istart wasn't already branch 0, and we got any error, then try
4776 + * branch 0 (which may require copyup)
4778 + if (err && istart > 0) {
4779 + istart = iend = 0;
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.
4787 + if (err && err != -EROFS)
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.
4794 + if (!lower_dentry) {
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);
4803 + err = 0; /* all's well */
4806 + return ERR_PTR(err);
4807 + return lower_dentry;
4810 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4811 + int mode, struct nameidata *nd_unused)
4814 + struct dentry *lower_dentry = NULL;
4815 + struct dentry *lower_parent_dentry = NULL;
4816 + struct dentry *parent;
4818 + struct nameidata lower_nd;
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);
4824 + valid = __unionfs_d_revalidate(dentry, parent, false);
4825 + if (unlikely(!valid)) {
4826 + err = -ESTALE; /* same as what real_lookup does */
4830 + lower_dentry = find_writeable_branch(dir, dentry);
4831 + if (IS_ERR(lower_dentry)) {
4832 + err = PTR_ERR(lower_dentry);
4836 + lower_parent_dentry = lock_parent(lower_dentry);
4837 + if (IS_ERR(lower_parent_dentry)) {
4838 + err = PTR_ERR(lower_parent_dentry);
4842 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4843 + if (unlikely(err < 0))
4845 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4847 + release_lower_nd(&lower_nd, err);
4850 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
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);
4861 + unlock_dir(lower_parent_dentry);
4864 + unionfs_postcopyup_setmnt(dentry);
4865 + unionfs_check_inode(dir);
4866 + unionfs_check_dentry(dentry);
4868 + unionfs_unlock_dentry(dentry);
4869 + unionfs_unlock_parent(dentry, parent);
4870 + unionfs_read_unlock(dentry->d_sb);
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.
4879 +static struct dentry *unionfs_lookup(struct inode *dir,
4880 + struct dentry *dentry,
4881 + struct nameidata *nd_unused)
4883 + struct dentry *ret, *parent;
4886 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4887 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
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.
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);
4902 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
4904 + if (!IS_ERR(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);
4912 + unionfs_copy_attr_times(dentry->d_inode);
4915 + unionfs_check_inode(dir);
4917 + unionfs_check_dentry(dentry);
4918 + unionfs_check_dentry(parent);
4919 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
4922 + unionfs_unlock_parent(dentry, parent);
4923 + unionfs_read_unlock(dentry->d_sb);
4928 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
4929 + struct dentry *new_dentry)
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;
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);
4945 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
4946 + if (unlikely(!valid)) {
4950 + if (new_dentry->d_inode) {
4951 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
4952 + if (unlikely(!valid)) {
4958 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
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);
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))
4981 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
4984 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
4985 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
4987 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
4988 + lower_dir_dentry = lock_parent(lower_new_dentry);
4989 + err = is_robranch(old_dentry);
4991 + /* see Documentation/filesystems/unionfs/issues.txt */
4993 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
4994 + lower_new_dentry);
4997 + unlock_dir(lower_dir_dentry);
5000 + if (IS_COPYUP_ERR(err)) {
5001 + int old_bstart = dbstart(old_dentry);
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));
5012 + lower_new_dentry =
5013 + create_parents(dir, new_dentry,
5014 + new_dentry->d_name.name,
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 */
5021 + err = vfs_link(lower_old_dentry,
5022 + lower_dir_dentry->d_inode,
5023 + lower_new_dentry);
5025 + unlock_dir(lower_dir_dentry);
5032 + if (err || !lower_new_dentry->d_inode)
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);
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);
5047 + if (!new_dentry->d_inode)
5048 + d_drop(new_dentry);
5052 + unionfs_postcopyup_setmnt(new_dentry);
5054 + unionfs_check_inode(dir);
5055 + unionfs_check_dentry(new_dentry);
5056 + unionfs_check_dentry(old_dentry);
5058 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5059 + unionfs_double_unlock_parents(old_parent, new_parent);
5062 + unionfs_read_unlock(old_dentry->d_sb);
5067 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5068 + const char *symname)
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;
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);
5083 + valid = __unionfs_d_revalidate(dentry, parent, false);
5084 + if (unlikely(!valid)) {
5090 + * It's only a bug if this dentry was not negative and couldn't be
5091 + * revalidated (shouldn't happen).
5093 + BUG_ON(!valid && dentry->d_inode);
5095 + lower_dentry = find_writeable_branch(dir, dentry);
5096 + if (IS_ERR(lower_dentry)) {
5097 + err = PTR_ERR(lower_dentry);
5101 + lower_parent_dentry = lock_parent(lower_dentry);
5102 + if (IS_ERR(lower_parent_dentry)) {
5103 + err = PTR_ERR(lower_parent_dentry);
5108 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5110 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
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);
5121 + unlock_dir(lower_parent_dentry);
5127 + unionfs_postcopyup_setmnt(dentry);
5128 + unionfs_check_inode(dir);
5129 + unionfs_check_dentry(dentry);
5131 + unionfs_unlock_dentry(dentry);
5132 + unionfs_unlock_parent(dentry, parent);
5133 + unionfs_read_unlock(dentry->d_sb);
5137 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
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;
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);
5151 + valid = __unionfs_d_revalidate(dentry, parent, false);
5152 + if (unlikely(!valid)) {
5153 + err = -ESTALE; /* same as what real_lookup does */
5157 + bstart = dbstart(dentry);
5159 + lower_dentry = unionfs_lower_dentry(dentry);
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 */
5166 + /* exit if the error returned was NOT -EROFS */
5167 + if (!IS_COPYUP_ERR(err))
5172 + /* check if copyup's needed, and mkdir */
5173 + for (bindex = bstart; bindex >= 0; bindex--) {
5175 + int bend = dbend(dentry);
5177 + if (is_robranch_super(dentry->d_sb, bindex))
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,
5185 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5186 + printk(KERN_ERR "unionfs: lower dentry "
5187 + " NULL for bindex = %d\n", bindex);
5192 + lower_parent_dentry = lock_parent(lower_dentry);
5194 + if (IS_ERR(lower_parent_dentry)) {
5195 + err = PTR_ERR(lower_parent_dentry);
5199 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5202 + unlock_dir(lower_parent_dentry);
5204 + /* did the mkdir succeed? */
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);
5215 + dbend(dentry) = bindex;
5218 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5221 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5223 + unionfs_copy_attr_times(dir);
5224 + fsstack_copy_inode_size(dir,
5225 + lower_parent_dentry->d_inode);
5227 + /* update number of links on parent directory */
5228 + dir->i_nlink = unionfs_get_nlinks(dir);
5231 + err = make_dir_opaque(dentry, dbstart(dentry));
5233 + printk(KERN_ERR "unionfs: mkdir: error creating "
5234 + ".wh.__dir_opaque: %d\n", err);
5238 + /* we are done! */
5243 + if (!dentry->d_inode)
5249 + unionfs_copy_attr_times(dentry->d_inode);
5250 + unionfs_postcopyup_setmnt(dentry);
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);
5261 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
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;
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);
5276 + valid = __unionfs_d_revalidate(dentry, parent, false);
5277 + if (unlikely(!valid)) {
5283 + * It's only a bug if this dentry was not negative and couldn't be
5284 + * revalidated (shouldn't happen).
5286 + BUG_ON(!valid && dentry->d_inode);
5288 + lower_dentry = find_writeable_branch(dir, dentry);
5289 + if (IS_ERR(lower_dentry)) {
5290 + err = PTR_ERR(lower_dentry);
5294 + lower_parent_dentry = lock_parent(lower_dentry);
5295 + if (IS_ERR(lower_parent_dentry)) {
5296 + err = PTR_ERR(lower_parent_dentry);
5300 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5302 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
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);
5313 + unlock_dir(lower_parent_dentry);
5319 + unionfs_postcopyup_setmnt(dentry);
5320 + unionfs_check_inode(dir);
5321 + unionfs_check_dentry(dentry);
5323 + unionfs_unlock_dentry(dentry);
5324 + unionfs_unlock_parent(dentry, parent);
5325 + unionfs_read_unlock(dentry->d_sb);
5329 +/* requires sb, dentry, and parent to already be locked */
5330 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5334 + struct dentry *lower_dentry;
5336 + lower_dentry = unionfs_lower_dentry(dentry);
5338 + if (!lower_dentry->d_inode->i_op ||
5339 + !lower_dentry->d_inode->i_op->readlink) {
5344 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5347 + fsstack_copy_attr_atime(dentry->d_inode,
5348 + lower_dentry->d_inode);
5354 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5358 + struct dentry *parent;
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);
5364 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5369 + err = __unionfs_readlink(dentry, buf, bufsiz);
5372 + unionfs_check_dentry(dentry);
5373 + unionfs_unlock_dentry(dentry);
5374 + unionfs_unlock_parent(dentry, parent);
5375 + unionfs_read_unlock(dentry->d_sb);
5380 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5383 + int len = PAGE_SIZE, err;
5384 + mm_segment_t old_fs;
5385 + struct dentry *parent;
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);
5391 + /* This is freed by the put_link method assuming a successful call. */
5392 + buf = kmalloc(len, GFP_KERNEL);
5393 + if (unlikely(!buf)) {
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);
5409 + nd_set_link(nd, buf);
5414 + unionfs_check_nd(nd);
5415 + unionfs_check_dentry(dentry);
5418 + unionfs_unlock_dentry(dentry);
5419 + unionfs_unlock_parent(dentry, parent);
5420 + unionfs_read_unlock(dentry->d_sb);
5422 + return ERR_PTR(err);
5425 +/* this @nd *IS* still used */
5426 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5429 + struct dentry *parent;
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);
5435 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5437 + "unionfs: put_link failed to revalidate dentry\n");
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);
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).
5451 +static int __inode_permission(struct inode *inode, int mask)
5455 + /* nobody gets write access to an immutable file */
5456 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
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);
5464 + * Exec permission on a regular file is denied if none
5465 + * of the execute bits are set.
5467 + * This check should be done by the ->permission()
5470 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5471 + !(inode->i_mode & S_IXUGO))
5475 + retval = generic_permission(inode, mask, NULL);
5480 + return security_inode_permission(inode,
5481 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
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
5492 +static int unionfs_permission(struct inode *inode, int mask)
5494 + struct inode *lower_inode = NULL;
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);
5503 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5505 + if (!UNIONFS_I(inode)->lower_inodes) {
5506 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5507 + err = -ESTALE; /* force revalidate */
5510 + bstart = ibstart(inode);
5511 + bend = ibend(inode);
5512 + if (unlikely(bstart < 0 || bend < 0)) {
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.
5520 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5521 + err = -ESTALE; /* force revalidate */
5525 + for (bindex = bstart; bindex <= bend; bindex++) {
5526 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
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
5535 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
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.
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)))
5552 + if (IS_COPYUP_ERR(err))
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.
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);
5571 + * The permissions are an intersection of the overall directory
5572 + * permissions, so we fail if one fails.
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);
5582 + put_write_access(lower_inode);
5587 + /* sync times which may have changed (asynchronously) below */
5588 + unionfs_copy_attr_times(inode);
5591 + unionfs_check_inode(inode);
5593 + unionfs_unlock_dentry(dentry);
5596 + iput(inode_grabbed);
5600 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5603 + struct dentry *lower_dentry;
5604 + struct dentry *parent;
5605 + struct inode *inode;
5606 + struct inode *lower_inode;
5607 + int bstart, bend, bindex;
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);
5614 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5619 + bstart = dbstart(dentry);
5620 + bend = dbend(dentry);
5621 + inode = dentry->d_inode;
5624 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5625 + * to reinterpret it in its own way.
5627 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5628 + ia->ia_valid &= ~ATTR_MODE;
5630 + lower_dentry = unionfs_lower_dentry(dentry);
5631 + if (!lower_dentry) { /* should never happen after above revalidate */
5635 + lower_inode = unionfs_lower_inode(inode);
5637 + /* check if user has permission to change lower inode */
5638 + err = inode_change_ok(lower_inode, ia);
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) {
5651 + if (ia->ia_valid & ATTR_SIZE)
5652 + size = ia->ia_size;
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,
5667 + /* get updated lower_dentry/inode after copyup */
5668 + lower_dentry = unionfs_lower_dentry(dentry);
5669 + lower_inode = unionfs_lower_inode(inode);
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.
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);
5690 + /* notify the (possibly copied-up) lower inode */
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.
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);
5702 + /* get attributes from the first lower inode */
5703 + if (ibstart(inode) >= 0)
5704 + unionfs_copy_attr_all(inode, lower_inode);
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).
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);
5721 + unionfs_check_dentry(dentry);
5722 + unionfs_unlock_dentry(dentry);
5723 + unionfs_unlock_parent(dentry, parent);
5724 + unionfs_read_unlock(dentry->d_sb);
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,
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 */
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 */
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
5772 + tristate "Union file system (EXPERIMENTAL)"
5773 + depends on EXPERIMENTAL
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.
5779 + See <http://unionfs.filesystems.org> for details
5781 +config UNION_FS_XATTR
5782 + bool "Unionfs extended attributes"
5783 + depends on UNION_FS
5785 + Extended attributes are name:value pairs associated with inodes by
5786 + the kernel or by users (see the attr(5) manual page).
5790 +config UNION_FS_DEBUG
5791 + bool "Debug Unionfs"
5792 + depends on UNION_FS
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
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
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.
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.
5827 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5828 + const char *name, struct vfsmount **new_mnt)
5830 + struct dentry *dentry = NULL;
5831 + struct nameidata lower_nd;
5834 + /* we use flags=0 to get basic lookup */
5835 + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
5838 + case 0: /* no error */
5839 + dentry = lower_nd.path.dentry;
5841 + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
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
5851 + dentry = lookup_lck_len(name, base, strlen(name));
5853 + *new_mnt = mntget(lower_nd.path.mnt);
5855 + default: /* all other real errors */
5856 + dentry = ERR_PTR(err);
5864 + * This is a utility function that fills in a unionfs dentry.
5865 + * Caller must lock this dentry with unionfs_lock_dentry.
5867 + * Returns: 0 (ok), or -ERRNO if an error occurred.
5868 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
5870 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
5872 + struct dentry *tmp;
5873 + int err = -ENOSYS;
5875 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
5881 + if (IS_ERR(tmp)) {
5882 + err = PTR_ERR(tmp);
5885 + /* XXX: need to change the interface */
5886 + BUG_ON(tmp != dentry);
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)
5895 + unionfs_dentry_cachep =
5896 + kmem_cache_create("unionfs_dentry",
5897 + sizeof(struct unionfs_dentry_info),
5898 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
5900 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
5903 +void unionfs_destroy_dentry_cache(void)
5905 + if (unionfs_dentry_cachep)
5906 + kmem_cache_destroy(unionfs_dentry_cachep);
5909 +void free_dentry_private_data(struct dentry *dentry)
5911 + if (!dentry || !dentry->d_fsdata)
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;
5919 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
5921 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
5927 + size = sizeof(struct path) * sbmax(dentry->d_sb);
5928 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
5932 + info->lower_paths = p;
5934 + info->bstart = -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));
5941 + memset(info->lower_paths, 0, size);
5946 +/* UNIONFS_D(dentry)->lock must be locked */
5947 +int realloc_dentry_private_data(struct dentry *dentry)
5949 + if (!__realloc_dentry_private_data(dentry))
5952 + kfree(UNIONFS_D(dentry)->lower_paths);
5953 + free_dentry_private_data(dentry);
5957 +/* allocate new dentry private data */
5958 +int new_dentry_private_data(struct dentry *dentry, int subclass)
5960 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
5964 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
5965 + if (unlikely(!info))
5968 + mutex_init(&info->lock);
5969 + mutex_lock_nested(&info->lock, subclass);
5971 + info->lower_paths = NULL;
5973 + dentry->d_fsdata = info;
5975 + if (!__realloc_dentry_private_data(dentry))
5978 + mutex_unlock(&info->lock);
5979 + free_dentry_private_data(dentry);
5984 + * scan through the lower dentry objects, and set bstart to reflect the
5987 +void update_bstart(struct dentry *dentry)
5990 + int bstart = dbstart(dentry);
5991 + int bend = dbend(dentry);
5992 + struct dentry *lower_dentry;
5994 + for (bindex = bstart; bindex <= bend; bindex++) {
5995 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5996 + if (!lower_dentry)
5998 + if (lower_dentry->d_inode) {
5999 + dbstart(dentry) = bindex;
6002 + dput(lower_dentry);
6003 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
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).
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.
6020 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6023 +#ifdef ALLOC_LOWER_ND_FILE
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.
6029 + struct file *file;
6030 +#endif /* ALLOC_LOWER_ND_FILE */
6032 + memset(nd, 0, sizeof(struct nameidata));
6037 + case LOOKUP_CREATE:
6038 + nd->intent.open.flags |= O_CREAT;
6039 + /* fall through: shared code for create/open cases */
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)) {
6047 + break; /* exit switch statement and thus return */
6049 + nd->intent.open.file = file;
6050 +#endif /* ALLOC_LOWER_ND_FILE */
6054 + * We should never get here, for now.
6055 + * We can add new cases here later on.
6057 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6065 +void release_lower_nd(struct nameidata *nd, int err)
6067 + if (!nd->intent.open.file)
6070 + release_open_intent(nd);
6071 +#ifdef ALLOC_LOWER_ND_FILE
6072 + kfree(nd->intent.open.file);
6073 +#endif /* ALLOC_LOWER_ND_FILE */
6077 + * Main (and complex) driver function for Unionfs's lookup
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.
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.
6087 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6088 + struct dentry *parent, int lookupmode)
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;
6101 + int pos_start, pos_end;
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.
6108 + verify_locked(dentry);
6109 + verify_locked(parent);
6111 + /* must initialize dentry operations */
6112 + dentry->d_op = &unionfs_dops;
6114 + /* We never partial lookup the root directory. */
6115 + if (IS_ROOT(dentry))
6118 + name = dentry->d_name.name;
6119 + namelen = dentry->d_name.len;
6121 + /* No dentries should get created for possible whiteout names. */
6122 + if (!is_validname(name)) {
6127 + /* Now start the actual lookup procedure. */
6128 + bstart = dbstart(parent);
6129 + bend = dbend(parent);
6130 + bopaque = dbopaque(parent);
6131 + BUG_ON(bstart < 0);
6133 + /* adjust bend to bopaque if needed */
6134 + if ((bopaque >= 0) && (bopaque < bend))
6137 + /* lookup all possible dentries */
6138 + for (bindex = bstart; bindex <= bend; bindex++) {
6140 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6141 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
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)
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)
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 */
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);
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);
6177 + dput(wh_lower_dentry);
6179 + /* Now do regular lookup; lookup @name */
6180 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6181 + lower_mnt = NULL; /* XXX: needed? */
6183 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6184 + name, &lower_mnt);
6186 + if (IS_ERR(lower_dentry)) {
6187 + err = PTR_ERR(lower_dentry);
6190 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6192 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6194 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6196 + /* adjust dbstart/end */
6197 + if (dbstart(dentry) < 0)
6198 + dbstart(dentry) = bindex;
6199 + if (bindex > dbend(dentry))
6200 + dbend(dentry) = bindex;
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).
6206 + if (!lower_dentry->d_inode)
6211 + * check if we just found an opaque directory, if so, stop
6214 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6216 + opaque = is_opaque_dir(dentry, bindex);
6220 + } else if (opaque) {
6221 + dbend(dentry) = dbopaque(dentry) = bindex;
6224 + dbend(dentry) = bindex;
6226 + /* update parent directory's atime with the bindex */
6227 + fsstack_copy_attr_atime(parent->d_inode,
6228 + lower_dir_dentry->d_inode);
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);
6235 + if (num_positive > 0)
6236 + goto out_positive;
6238 + /*** handle NEGATIVE dentries ***/
6241 + * If negative, keep only first lower negative dentry, to save on
6244 + if (dbstart(dentry) < dbend(dentry)) {
6245 + path_put_lowers(dentry, dbstart(dentry) + 1,
6246 + dbend(dentry), false);
6247 + dbend(dentry) = dbstart(dentry);
6249 + if (lookupmode == INTERPOSE_PARTIAL)
6251 + if (lookupmode == INTERPOSE_LOOKUP) {
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.
6257 + if (dbopaque(dentry) < 0)
6259 + /* XXX: need to get mnt here */
6260 + bindex = dbstart(dentry);
6261 + if (unionfs_lower_dentry_idx(dentry, bindex))
6263 + lower_dir_dentry =
6264 + unionfs_lower_dentry_idx(parent, bindex);
6265 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
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);
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);
6284 + /* if we're revalidating a positive dentry, don't make it negative */
6285 + if (lookupmode != INTERPOSE_REVAL)
6286 + d_add(dentry, NULL);
6291 + /*** handle POSITIVE dentries ***/
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.
6299 + pos_start = pos_end = -1;
6300 + for (bindex = bstart; bindex <= bend; bindex++) {
6301 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6303 + if (lower_dentry && lower_dentry->d_inode) {
6304 + if (pos_start < 0)
6305 + pos_start = bindex;
6306 + if (bindex > pos_end)
6310 + path_put_lowers(dentry, bindex, bindex, false);
6312 + if (pos_start >= 0)
6313 + dbstart(dentry) = pos_start;
6315 + dbend(dentry) = pos_end;
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);
6325 + * This dentry was positive, so it is as if we had a
6326 + * negative revalidation.
6328 + lookupmode = INTERPOSE_REVAL_NEG;
6329 + update_bstart(dentry);
6333 + * Interpose can return a dentry if d_splice returned a different
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;
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;
6355 + if (dentry && UNIONFS_D(dentry)) {
6356 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6357 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
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);
6364 + if (!err && d_interposed)
6365 + return d_interposed;
6366 + return ERR_PTR(err);
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
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
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.
6391 +#include <linux/module.h>
6392 +#include <linux/moduleparam.h>
6394 +static void unionfs_fill_inode(struct dentry *dentry,
6395 + struct inode *inode)
6397 + struct inode *lower_inode;
6398 + struct dentry *lower_dentry;
6399 + int bindex, bstart, bend;
6401 + bstart = dbstart(dentry);
6402 + bend = dbend(dentry);
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);
6411 + /* Initialize the lower inode to the new lower inode. */
6412 + if (!lower_dentry->d_inode)
6415 + unionfs_set_lower_inode_idx(inode, bindex,
6416 + igrab(lower_dentry->d_inode));
6419 + ibstart(inode) = dbstart(dentry);
6420 + ibend(inode) = dbend(dentry);
6422 + /* Use attributes from the first branch. */
6423 + lower_inode = unionfs_lower_inode(inode);
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;
6431 + /* Use different set of file ops for directories */
6432 + if (S_ISDIR(lower_inode->i_mode))
6433 + inode->i_fop = &unionfs_dir_fops;
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);
6441 + /* all well, copy inode attributes */
6442 + unionfs_copy_attr_all(inode, lower_inode);
6443 + fsstack_copy_inode_size(inode, lower_inode);
6447 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6448 + * the classic stackable file system "vnode interposition" action.
6450 + * @sb: unionfs's super_block
6452 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6456 + struct inode *inode;
6457 + int need_fill_inode = 1;
6458 + struct dentry *spliced = NULL;
6460 + verify_locked(dentry);
6463 + * We allocate our new inode below by calling unionfs_iget,
6464 + * which will initialize some of the new inode's fields
6468 + * On revalidate we've already got our own inode and just need
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));
6478 + UNIONFS_I(inode)->lower_inodes =
6479 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6480 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
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);
6491 + if (atomic_read(&inode->i_count) > 1)
6495 + need_fill_inode = 0;
6496 + unionfs_fill_inode(dentry, inode);
6499 + /* only (our) lookup wants to do a d_add */
6501 + case INTERPOSE_DEFAULT:
6502 + /* for operations which create new inodes */
6503 + d_add(dentry, inode);
6505 + case INTERPOSE_REVAL_NEG:
6506 + d_instantiate(dentry, inode);
6508 + case INTERPOSE_LOOKUP:
6509 + spliced = d_splice_alias(inode, dentry);
6510 + if (spliced && spliced != dentry) {
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
6519 + spliced->d_op = &unionfs_dops;
6520 + spliced->d_fsdata = dentry->d_fsdata;
6521 + dentry->d_fsdata = NULL;
6523 + if (need_fill_inode) {
6524 + need_fill_inode = 0;
6525 + unionfs_fill_inode(dentry, inode);
6528 + } else if (!spliced) {
6529 + if (need_fill_inode) {
6530 + need_fill_inode = 0;
6531 + unionfs_fill_inode(dentry, inode);
6536 + case INTERPOSE_REVAL:
6540 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6549 + return ERR_PTR(err);
6552 +/* like interpose above, but for an already existing dentry */
6553 +void unionfs_reinterpose(struct dentry *dentry)
6555 + struct dentry *lower_dentry;
6556 + struct inode *inode;
6557 + int bindex, bstart, bend;
6559 + verify_locked(dentry);
6561 + /* This is pre-allocated inode */
6562 + inode = dentry->d_inode;
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)
6571 + if (!lower_dentry->d_inode)
6573 + if (unionfs_lower_inode_idx(inode, bindex))
6575 + unionfs_set_lower_inode_idx(inode, bindex,
6576 + igrab(lower_dentry->d_inode));
6578 + ibstart(inode) = dbstart(dentry);
6579 + ibend(inode) = dbend(dentry);
6583 + * make sure the branch we just looked up (nd) makes sense:
6585 + * 1) we're not trying to stack unionfs on top of unionfs
6587 + * 3) is a directory
6589 +int check_branch(struct nameidata *nd)
6591 + /* XXX: remove in ODF code -- stacking unions allowed there */
6592 + if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6594 + if (!nd->path.dentry->d_inode)
6596 + if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6601 +/* checks if two lower_dentries have overlapping branches */
6602 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6604 + struct dentry *dent = NULL;
6607 + while ((dent != dent2) && (dent->d_parent != dent))
6608 + dent = dent->d_parent;
6610 + if (dent == dent2)
6614 + while ((dent != dent1) && (dent->d_parent != dent))
6615 + dent = dent->d_parent;
6617 + return (dent == dent1);
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.
6625 +int parse_branch_mode(const char *name, int *perms)
6627 + if (!name || !strcmp(name, "rw")) {
6628 + *perms = MAY_READ | MAY_WRITE;
6631 + if (!strcmp(name, "ro")) {
6632 + *perms = MAY_READ;
6639 + * parse the dirs= mount argument
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.
6645 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6646 + *lower_root_info, char *options)
6648 + struct nameidata nd;
6655 + struct dentry *dent1;
6656 + struct dentry *dent2;
6658 + if (options[0] == '\0') {
6659 + printk(KERN_ERR "unionfs: no branches specified\n");
6665 + * Each colon means we have a separator, this is really just a rough
6666 + * guess, since strsep will handle empty fields for us.
6668 + for (i = 0; options[i]; i++)
6669 + if (options[i] == ':')
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)) {
6680 + lower_root_info->lower_paths =
6681 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6682 + if (unlikely(!lower_root_info->lower_paths)) {
6687 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6689 + while ((name = strsep(&options, ":")) != NULL) {
6691 + char *mode = strchr(name, '=');
6695 + if (!*name) { /* bad use of ':' (extra colons) */
6702 + /* strip off '=' if any */
6706 + err = parse_branch_mode(mode, &perms);
6708 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6709 + "branch %d\n", mode, bindex);
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");
6721 + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6723 + printk(KERN_ERR "unionfs: error accessing "
6724 + "lower directory '%s' (error %d)\n",
6729 + err = check_branch(&nd);
6731 + printk(KERN_ERR "unionfs: lower directory "
6732 + "'%s' is not a valid branch\n", name);
6733 + path_put(&nd.path);
6737 + lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6738 + lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6740 + set_branchperms(sb, bindex, perms);
6741 + set_branch_count(sb, bindex, 0);
6742 + new_branch_id(sb, bindex);
6744 + if (lower_root_info->bstart < 0)
6745 + lower_root_info->bstart = bindex;
6746 + lower_root_info->bend = bindex;
6750 + if (branches == 0) {
6751 + printk(KERN_ERR "unionfs: no branches specified\n");
6756 + BUG_ON(branches != (lower_root_info->bend + 1));
6759 + * Ensure that no overlaps exist in the branches.
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.
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);
6788 + for (i = 0; i < branches; i++)
6789 + path_put(&lower_root_info->lower_paths[i]);
6791 + kfree(lower_root_info->lower_paths);
6792 + kfree(UNIONFS_SB(sb)->data);
6795 + * MUST clear the pointers to prevent potential double free if
6796 + * the caller dies later on
6798 + lower_root_info->lower_paths = NULL;
6799 + UNIONFS_SB(sb)->data = NULL;
6805 + * Parse mount options. See the manual page for usage instructions.
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.
6810 +static struct unionfs_dentry_info *unionfs_parse_options(
6811 + struct super_block *sb,
6814 + struct unionfs_dentry_info *lower_root_info;
6818 + int dirsfound = 0;
6820 + /* allocate private data area */
6823 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6824 + if (unlikely(!lower_root_info))
6826 + lower_root_info->bstart = -1;
6827 + lower_root_info->bend = -1;
6828 + lower_root_info->bopaque = -1;
6830 + while ((optname = strsep(&options, ",")) != NULL) {
6833 + if (!optname || !*optname)
6836 + optarg = strchr(optname, '=');
6841 + * All of our options take an argument now. Insert ones that
6842 + * don't, above this check.
6845 + printk(KERN_ERR "unionfs: %s requires an argument\n",
6851 + if (!strcmp("dirs", optname)) {
6852 + if (++dirsfound > 1) {
6854 + "unionfs: multiple dirs specified\n");
6858 + err = parse_dirs_option(sb, lower_root_info, optarg);
6866 + "unionfs: unrecognized option '%s'\n", optname);
6869 + if (dirsfound != 1) {
6870 + printk(KERN_ERR "unionfs: dirs option required\n");
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;
6881 + path_put(&lower_root_info->lower_paths[bindex]);
6884 + kfree(lower_root_info->lower_paths);
6885 + kfree(lower_root_info);
6887 + kfree(UNIONFS_SB(sb)->data);
6888 + UNIONFS_SB(sb)->data = NULL;
6890 + lower_root_info = ERR_PTR(err);
6892 + return lower_root_info;
6896 + * our custom d_alloc_root work-alike
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
6901 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
6903 + struct dentry *ret = NULL;
6906 + static const struct qstr name = {
6911 + ret = d_alloc(NULL, &name);
6912 + if (likely(ret)) {
6913 + ret->d_op = &unionfs_dops;
6915 + ret->d_parent = ret;
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.
6925 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
6929 + struct unionfs_dentry_info *lower_root_info = NULL;
6930 + int bindex, bstart, bend;
6934 + "unionfs: read_super: missing data argument\n");
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");
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 */
6952 + lower_root_info = unionfs_parse_options(sb, raw_data);
6953 + if (IS_ERR(lower_root_info)) {
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;
6961 + if (lower_root_info->bstart == -1) {
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);
6976 + /* max Bytes is the maximum bytes from highest priority branch */
6977 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
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.
6984 + sb->s_time_gran = 1;
6986 + sb->s_op = &unionfs_sops;
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)) {
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))
7001 + /* Set the lower dentries for s_root */
7002 + for (bindex = bstart; bindex <= bend; bindex++) {
7004 + struct vfsmount *m;
7006 + d = lower_root_info->lower_paths[bindex].dentry;
7007 + m = lower_root_info->lower_paths[bindex].mnt;
7009 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7010 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7012 + dbstart(sb->s_root) = bstart;
7013 + dbend(sb->s_root) = bend;
7015 + /* Set the generation number to one, since this is for the mount. */
7016 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7019 + * Call interpose to create the upper level inode. Only
7020 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7022 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7023 + unionfs_unlock_dentry(sb->s_root);
7026 + /* else fall through */
7029 + if (UNIONFS_D(sb->s_root)) {
7030 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7031 + free_dentry_private_data(sb->s_root);
7036 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7037 + for (bindex = lower_root_info->bstart;
7038 + bindex <= lower_root_info->bend; bindex++) {
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]);
7045 + kfree(lower_root_info->lower_paths);
7046 + kfree(lower_root_info);
7047 + lower_root_info = NULL;
7051 + kfree(UNIONFS_SB(sb)->data);
7052 + kfree(UNIONFS_SB(sb));
7053 + sb->s_fs_info = NULL;
7056 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7057 + kfree(lower_root_info->lower_paths);
7058 + kfree(lower_root_info);
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)
7068 + err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7070 + UNIONFS_SB(mnt->mnt_sb)->dev_name =
7071 + kstrdup(dev_name, GFP_KERNEL);
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,
7083 +static int __init init_unionfs_fs(void)
7087 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7089 + err = unionfs_init_filldir_cache();
7090 + if (unlikely(err))
7092 + err = unionfs_init_inode_cache();
7093 + if (unlikely(err))
7095 + err = unionfs_init_dentry_cache();
7096 + if (unlikely(err))
7098 + err = init_sioq();
7099 + if (unlikely(err))
7101 + err = register_filesystem(&unionfs_fs_type);
7103 + if (unlikely(err)) {
7105 + unionfs_destroy_filldir_cache();
7106 + unionfs_destroy_inode_cache();
7107 + unionfs_destroy_dentry_cache();
7112 +static void __exit exit_unionfs_fs(void)
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");
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");
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
7134 +UNIONFS_VERSION="2.5.4 (for 2.6.34-rc0)"
7136 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
7138 +obj-$(CONFIG_UNION_FS) += unionfs.o
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
7144 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
7146 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
7148 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
7149 +EXTRA_CFLAGS += -DDEBUG
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
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
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.
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.
7184 + * Otherwise, we don't want to use our readpage method at all.
7186 +static int unionfs_readpage(struct file *file, struct page *page)
7192 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7195 + struct file *file, *lower_file;
7196 + const struct vm_operations_struct *lower_vm_ops;
7197 + struct vm_area_struct lower_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);
7205 + lower_file = unionfs_lower_file(file);
7206 + BUG_ON(!lower_file);
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.
7217 + lower_vma.vm_file = lower_file;
7218 + err = lower_vm_ops->fault(&lower_vma, vmf);
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.
7227 +struct address_space_operations unionfs_aops = {
7228 + /* empty on purpose */
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
7237 +struct address_space_operations unionfs_dummy_aops = {
7238 + .readpage = unionfs_readpage,
7241 +struct vm_operations_struct unionfs_vm_ops = {
7242 + .fault = unionfs_fault,
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
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
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.
7268 +/* This file contains the routines for maintaining readdir state. */
7271 + * There are two structures here, rdstate which is a hash table
7272 + * of the second structure which is a filldir_node.
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.
7281 +static struct kmem_cache *unionfs_filldir_cachep;
7283 +int unionfs_init_filldir_cache(void)
7285 + unionfs_filldir_cachep =
7286 + kmem_cache_create("unionfs_filldir",
7287 + sizeof(struct filldir_node), 0,
7288 + SLAB_RECLAIM_ACCOUNT, NULL);
7290 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7293 +void unionfs_destroy_filldir_cache(void)
7295 + if (unionfs_filldir_cachep)
7296 + kmem_cache_destroy(unionfs_filldir_cachep);
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
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)
7314 + struct inode *lower_inode;
7316 + int hashsize = MINHASHSIZE;
7318 + if (UNIONFS_I(inode)->hashsize > 0)
7319 + return UNIONFS_I(inode)->hashsize;
7321 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7322 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7326 + if (i_size_read(lower_inode) == DENTPAGE)
7327 + hashsize += DENTPERONEPAGE;
7329 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7336 +int init_rdstate(struct file *file)
7338 + BUG_ON(sizeof(loff_t) !=
7339 + (sizeof(unsigned int) + sizeof(unsigned int)));
7340 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7342 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7345 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7348 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7350 + struct unionfs_dir_state *rdstate = NULL;
7351 + struct list_head *pos;
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);
7364 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7368 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7372 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7373 + struct unionfs_dir_state *rdstate;
7375 + hashsize = guesstimate_hash_size(inode);
7376 + mallocsize += hashsize * sizeof(struct list_head);
7377 + mallocsize = __roundup_pow_of_two(mallocsize);
7379 + /* This should give us about 500 entries anyway. */
7380 + if (mallocsize > PAGE_SIZE)
7381 + mallocsize = PAGE_SIZE;
7383 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7384 + sizeof(struct list_head);
7386 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7387 + if (unlikely(!rdstate))
7390 + spin_lock(&UNIONFS_I(inode)->rdlock);
7391 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7392 + UNIONFS_I(inode)->cookie = 1;
7394 + UNIONFS_I(inode)->cookie++;
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]);
7410 +static void free_filldir_node(struct filldir_node *node)
7412 + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7413 + kfree(node->name);
7414 + kmem_cache_free(unionfs_filldir_cachep, node);
7417 +void free_rdstate(struct unionfs_dir_state *state)
7419 + struct filldir_node *tmp;
7422 + for (i = 0; i < state->size; i++) {
7423 + struct list_head *head = &(state->list[i]);
7424 + struct list_head *pos, *n;
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);
7437 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7438 + const char *name, int namelen,
7442 + unsigned int hash;
7443 + struct list_head *head;
7444 + struct list_head *pos;
7445 + struct filldir_node *cursor = NULL;
7448 + BUG_ON(namelen <= 0);
7450 + hash = full_name_hash(name, namelen);
7451 + index = hash % rdstate->size;
7453 + head = &(rdstate->list[index]);
7454 + list_for_each(pos, head) {
7455 + cursor = list_entry(pos, struct filldir_node, file_list);
7457 + if (cursor->namelen == namelen && cursor->hash == hash &&
7458 + !strncmp(cursor->name, name, namelen)) {
7460 + * a duplicate exists, and hence no need to create
7461 + * entry to the list
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.
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);
7486 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7487 + int namelen, int bindex, int whiteout)
7489 + struct filldir_node *new;
7490 + unsigned int hash;
7493 + struct list_head *head;
7495 + BUG_ON(namelen <= 0);
7497 + hash = full_name_hash(name, namelen);
7498 + index = hash % rdstate->size;
7499 + head = &(rdstate->list[index]);
7501 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7502 + if (unlikely(!new)) {
7507 + INIT_LIST_HEAD(&new->file_list);
7508 + new->namelen = namelen;
7510 + new->bindex = bindex;
7511 + new->whiteout = whiteout;
7513 + if (namelen < DNAME_INLINE_LEN_MIN) {
7514 + new->name = new->iname;
7516 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7517 + if (unlikely(!new->name)) {
7518 + kmem_cache_free(unionfs_filldir_cachep, new);
7524 + memcpy(new->name, name, namelen);
7525 + new->name[namelen] = '\0';
7527 + rdstate->hashentries++;
7529 + list_add(&(new->file_list), head);
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
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
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.
7558 + * This is a helper function for rename, used when rename ends up with hosed
7559 + * over dentries and we need to revert.
7561 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7562 + struct dentry *parent, int bindex)
7564 + struct dentry *lower_dentry;
7565 + struct dentry *lower_parent;
7568 + verify_locked(dentry);
7570 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7572 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
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);
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);
7585 + if (!lower_dentry->d_inode) {
7586 + dput(lower_dentry);
7587 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
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));
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,
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;
7611 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7612 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7614 + if (!lower_new_dentry) {
7615 + lower_new_dentry =
7616 + create_parents(new_parent->d_inode,
7617 + new_dentry, new_dentry->d_name.name,
7619 + if (IS_ERR(lower_new_dentry)) {
7620 + err = PTR_ERR(lower_new_dentry);
7621 + if (IS_COPYUP_ERR(err))
7623 + printk(KERN_ERR "unionfs: error creating directory "
7624 + "tree for rename, bindex=%d err=%d\n",
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 */
7637 + /* check of old_dentry branch is writable */
7638 + err = is_robranch_super(old_dentry->d_sb, bindex);
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);
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) {
7651 + goto out_err_unlock;
7653 + /* target should not be ancenstor of source */
7654 + if (trap == lower_new_dentry) {
7656 + goto out_err_unlock;
7658 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7659 + lower_new_dir_dentry->d_inode, lower_new_dentry);
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);
7666 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7668 + dput(lower_old_dir_dentry);
7669 + dput(lower_new_dir_dentry);
7670 + dput(lower_old_dentry);
7671 + dput(lower_new_dentry);
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;
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.
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)
7699 + int old_bstart, old_bend;
7700 + int new_bstart, new_bend;
7701 + int do_copyup = -1;
7702 + int local_err = 0;
7706 + old_bstart = dbstart(old_dentry);
7707 + old_bend = dbend(old_dentry);
7709 + new_bstart = dbstart(new_dentry);
7710 + new_bend = dbend(new_dentry);
7712 + /* Rename source to destination. */
7713 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7714 + new_dir, new_dentry, new_parent,
7717 + if (!IS_COPYUP_ERR(err))
7719 + do_copyup = old_bstart - 1;
7725 + * Unlink all instances of destination that exist to the left of
7726 + * bstart of source. On error, revert back, goto out.
7728 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7729 + struct dentry *unlink_dentry;
7730 + struct dentry *unlink_dir_dentry;
7732 + BUG_ON(bindex < 0);
7733 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7734 + if (!unlink_dentry)
7737 + unlink_dir_dentry = lock_parent(unlink_dentry);
7738 + err = is_robranch_super(old_dir->i_sb, bindex);
7740 + err = vfs_unlink(unlink_dir_dentry->d_inode,
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);
7749 + unlock_dir(unlink_dir_dentry);
7751 + if (bindex != new_bstart) {
7752 + dput(unlink_dentry);
7753 + unionfs_set_lower_dentry_idx(new_dentry,
7756 + } else if (IS_COPYUP_ERR(err)) {
7757 + do_copyup = bindex - 1;
7758 + } else if (revert) {
7763 + if (do_copyup != -1) {
7764 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7766 + * copyup the file into some left directory, so that
7767 + * you can rename it
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 */
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.
7783 + err = create_whiteout(old_dentry, bindex);
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);
7790 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7791 + new_dir, new_dentry, new_parent,
7797 + /* make it opaque */
7798 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7799 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
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).
7809 + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7810 + err = create_whiteout(old_dentry, old_bstart);
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);
7823 + /* Do revert here. */
7824 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7827 + printk(KERN_ERR "unionfs: revert failed in rename: "
7828 + "the new refresh failed\n");
7832 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7835 + printk(KERN_ERR "unionfs: revert failed in rename: "
7836 + "the old refresh failed\n");
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");
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");
7857 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
7858 + old_dir, old_dentry, old_parent,
7861 + /* If we can't fix it, then we cop-out with -EIO. */
7863 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
7867 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7871 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
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.
7888 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
7892 + err = check_empty(dentry, parent, NULL);
7893 + if (err == -ENOTEMPTY) {
7894 + if (is_robranch(dentry))
7900 + bstart = dbstart(dentry);
7901 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
7904 + dbstart(dentry) = bstart + 1;
7905 + err = check_empty(dentry, parent, NULL);
7906 + dbstart(dentry) = bstart;
7907 + if (err == -ENOTEMPTY)
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.
7916 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7917 + struct inode *new_dir, struct dentry *new_dentry)
7920 + struct dentry *wh_dentry;
7921 + struct dentry *old_parent, *new_parent;
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);
7937 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
7942 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
7943 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
7950 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
7951 + err = unionfs_partial_lookup(old_dentry, old_parent);
7953 + err = may_rename_dir(old_dentry, old_parent);
7958 + err = unionfs_partial_lookup(new_dentry, new_parent);
7963 + * if new_dentry is already lower because of whiteout,
7964 + * simply override it even if the whited-out dir is not empty.
7966 + wh_dentry = find_first_whiteout(new_dentry);
7967 + if (!IS_ERR(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;
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);
7984 + if (!is_robranch(new_dentry))
7985 + err = delete_whiteouts(new_dentry,
7986 + dbstart(new_dentry),
7989 + free_rdstate(namelist);
7996 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
7997 + new_dir, new_dentry, new_parent);
8002 + * force re-lookup since the dir on ro branch is not renamed, and
8003 + * lower dentries still indicate the un-renamed ones.
8005 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8006 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
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)) {
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.
8020 + struct inode *inode;
8021 + inode = unionfs_lower_inode(old_dentry->d_inode);
8023 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8024 + dbstart(new_dentry),
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);
8037 + if (err) /* clear the new_dentry stuff created */
8038 + d_drop(new_dentry);
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);
8050 + unionfs_read_unlock(old_dentry->d_sb);
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
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
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.
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
8081 +static struct workqueue_struct *superio_workqueue;
8083 +int __init init_sioq(void)
8087 + superio_workqueue = create_workqueue("unionfs_siod");
8088 + if (!IS_ERR(superio_workqueue))
8091 + err = PTR_ERR(superio_workqueue);
8092 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8093 + superio_workqueue = NULL;
8097 +void stop_sioq(void)
8099 + if (superio_workqueue)
8100 + destroy_workqueue(superio_workqueue);
8103 +void run_sioq(work_func_t func, struct sioq_args *args)
8105 + INIT_WORK(&args->work, func);
8107 + init_completion(&args->comp);
8108 + while (!queue_work(superio_workqueue, &args->work)) {
8109 + /* TODO: do accounting if needed */
8112 + wait_for_completion(&args->comp);
8115 +void __unionfs_create(struct work_struct *work)
8117 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8118 + struct create_args *c = &args->create;
8120 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8121 + complete(&args->comp);
8124 +void __unionfs_mkdir(struct work_struct *work)
8126 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8127 + struct mkdir_args *m = &args->mkdir;
8129 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8130 + complete(&args->comp);
8133 +void __unionfs_mknod(struct work_struct *work)
8135 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8136 + struct mknod_args *m = &args->mknod;
8138 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8139 + complete(&args->comp);
8142 +void __unionfs_symlink(struct work_struct *work)
8144 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8145 + struct symlink_args *s = &args->symlink;
8147 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8148 + complete(&args->comp);
8151 +void __unionfs_unlink(struct work_struct *work)
8153 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8154 + struct unlink_args *u = &args->unlink;
8156 + args->err = vfs_unlink(u->parent, u->dentry);
8157 + complete(&args->comp);
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
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
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.
8180 +struct deletewh_args {
8181 + struct unionfs_dir_state *namelist;
8182 + struct dentry *dentry;
8186 +struct is_opaque_args {
8187 + struct dentry *dentry;
8190 +struct create_args {
8191 + struct inode *parent;
8192 + struct dentry *dentry;
8194 + struct nameidata *nd;
8197 +struct mkdir_args {
8198 + struct inode *parent;
8199 + struct dentry *dentry;
8203 +struct mknod_args {
8204 + struct inode *parent;
8205 + struct dentry *dentry;
8210 +struct symlink_args {
8211 + struct inode *parent;
8212 + struct dentry *dentry;
8216 +struct unlink_args {
8217 + struct inode *parent;
8218 + struct dentry *dentry;
8223 + struct completion comp;
8224 + struct work_struct work;
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;
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);
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);
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
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
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.
8279 + * returns the right n_link value based on the inode type
8281 +int unionfs_get_nlinks(const struct inode *inode)
8283 + /* don't bother to do all the work since we're unlinked */
8284 + if (inode->i_nlink == 0)
8287 + if (!S_ISDIR(inode->i_mode))
8288 + return unionfs_lower_inode(inode)->i_nlink;
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
8298 +/* copy a/m/ctime from the lower branch with the newest times */
8299 +void unionfs_copy_attr_times(struct inode *upper)
8302 + struct inode *lower;
8306 + if (ibstart(upper) < 0) {
8307 +#ifdef CONFIG_UNION_FS_DEBUG
8308 + WARN_ON(ibstart(upper) < 0);
8309 +#endif /* CONFIG_UNION_FS_DEBUG */
8312 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8313 + lower = unionfs_lower_inode_idx(upper, bindex);
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;
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)
8334 +void unionfs_copy_attr_all(struct inode *dest,
8335 + const struct inode *src)
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;
8342 + unionfs_copy_attr_times(dest);
8344 + dest->i_blkbits = src->i_blkbits;
8345 + dest->i_flags = src->i_flags;
8348 + * Update the nlinks AFTER updating the above fields, because the
8349 + * get_links callback may depend on them.
8351 + dest->i_nlink = unionfs_get_nlinks(dest);
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
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
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.
8378 + * The inode cache is used with alloc_inode for both our inode info and the
8381 +static struct kmem_cache *unionfs_inode_cachep;
8383 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8386 + struct unionfs_inode_info *info;
8387 + struct inode *inode;
8389 + inode = iget_locked(sb, ino);
8391 + return ERR_PTR(-ENOMEM);
8392 + if (!(inode->i_state & I_NEW))
8395 + info = UNIONFS_I(inode);
8396 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8397 + info->bstart = -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);
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);
8415 + inode->i_version++;
8416 + inode->i_op = &unionfs_main_iops;
8417 + inode->i_fop = &unionfs_main_fops;
8419 + inode->i_mapping->a_ops = &unionfs_aops;
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).
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);
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.
8439 + * No need to lock sb info's rwsem.
8441 +static void unionfs_delete_inode(struct inode *inode)
8443 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8444 + spin_lock(&inode->i_lock);
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);
8451 + if (inode->i_data.nrpages)
8452 + truncate_inode_pages(&inode->i_data, 0);
8454 + clear_inode(inode);
8458 + * final actions when unmounting a file system
8460 + * No need to lock rwsem.
8462 +static void unionfs_put_super(struct super_block *sb)
8464 + int bindex, bstart, bend;
8465 + struct unionfs_sb_info *spd;
8468 + spd = UNIONFS_SB(sb);
8472 + bstart = sbstart(sb);
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)) {
8479 + "unionfs: branch %d has %d references left!\n",
8480 + bindex, branch_count(sb, bindex));
8483 + WARN_ON(leaks != 0);
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);
8493 + kfree(spd->dev_name);
8496 + sb->s_fs_info = NULL;
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
8504 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8507 + struct super_block *sb;
8508 + struct dentry *lower_dentry;
8509 + struct dentry *parent;
8512 + sb = dentry->d_sb;
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);
8518 + valid = __unionfs_d_revalidate(dentry, parent, false);
8519 + if (unlikely(!valid)) {
8523 + unionfs_check_dentry(dentry);
8525 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8526 + err = vfs_statfs(lower_dentry, buf);
8528 + /* set return buf to our f/s to avoid confusing user-level utils */
8529 + buf->f_type = UNIONFS_SUPER_MAGIC;
8531 + * Our maximum file name can is shorter by a few bytes because every
8532 + * file name could potentially be whited-out.
8534 + * XXX: this restriction goes away with ODF.
8536 + unionfs_set_max_namelen(&buf->f_namelen);
8539 + * reset two fields to avoid confusing user-land.
8540 + * XXX: is this still necessary?
8542 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8543 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8546 + unionfs_check_dentry(dentry);
8547 + unionfs_unlock_dentry(dentry);
8548 + unionfs_unlock_parent(dentry, parent);
8549 + unionfs_read_unlock(sb);
8553 +/* handle mode changing during remount */
8554 +static noinline_for_stack int do_remount_mode_option(
8557 + struct unionfs_data *new_data,
8558 + struct path *new_lower_paths)
8560 + int err = -EINVAL;
8562 + char *modename = strchr(optarg, '=');
8563 + struct nameidata nd;
8565 + /* by now, optarg contains the branch name */
8568 + "unionfs: no branch specified for mode change\n");
8572 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8576 + *modename++ = '\0';
8577 + err = parse_branch_mode(modename, &perms);
8579 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8580 + modename, optarg);
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
8590 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8592 + printk(KERN_ERR "unionfs: error accessing "
8593 + "lower directory \"%s\" (error %d)\n",
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)
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);
8608 + /* check/change mode for existing branch */
8609 + /* we don't warn if perms==branchperms */
8610 + new_data[idx].branchperms = perms;
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)
8622 + int err = -EINVAL;
8624 + struct nameidata nd;
8626 + /* optarg contains the branch name to delete */
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
8634 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8636 + printk(KERN_ERR "unionfs: error accessing "
8637 + "lower directory \"%s\" (error %d)\n",
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)
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);
8652 + /* check if there are any open files on the branch to be deleted */
8653 + if (atomic_read(&new_data[idx].open_files) > 0) {
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
8664 + path_put(&new_lower_paths[idx]);
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));
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)
8687 + int err = -EINVAL;
8689 + int idx = 0; /* default: insert at beginning */
8690 + char *new_branch , *modename = NULL;
8691 + struct nameidata nd;
8694 + * optarg can be of several forms:
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)
8701 + if (*optarg == ':') { /* append? */
8702 + new_branch = optarg + 1; /* skip ':' */
8703 + idx = cur_branches;
8704 + goto found_insertion_point;
8706 + new_branch = strchr(optarg, ':');
8707 + if (!new_branch) { /* prepend? */
8708 + new_branch = optarg;
8709 + goto found_insertion_point;
8711 + *new_branch++ = '\0'; /* holds path+mode of new branch */
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
8719 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8721 + printk(KERN_ERR "unionfs: error accessing "
8722 + "lower directory \"%s\" (error %d)\n",
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)
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);
8739 + * At this point idx will hold the index where the new branch should
8740 + * be inserted before.
8742 +found_insertion_point:
8743 + /* find the mode for the new branch */
8745 + modename = strchr(new_branch, '=');
8747 + *modename++ = '\0';
8748 + if (!new_branch || !*new_branch) {
8749 + printk(KERN_ERR "unionfs: null new branch\n");
8753 + err = parse_branch_mode(modename, &perms);
8755 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8756 + "branch \"%s\"\n", modename, new_branch);
8759 + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8761 + printk(KERN_ERR "unionfs: error accessing "
8762 + "lower directory \"%s\" (error %d)\n",
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.
8773 + err = check_branch(&nd);
8775 + printk(KERN_ERR "unionfs: lower directory "
8776 + "\"%s\" is not a valid branch\n", optarg);
8777 + path_put(&nd.path);
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
8785 + * We don't release nd here; it's kept until umount/remount.
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));
8794 + new_lower_paths[idx].dentry = nd.path.dentry;
8795 + new_lower_paths[idx].mnt = nd.path.mnt;
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 */
8809 + * Support branch management options on remount.
8811 + * See Documentation/filesystems/unionfs/ for details.
8813 + * @flags: numeric mount options
8814 + * @options: mount options string
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.
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
8831 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8836 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
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;
8850 + unionfs_write_lock(sb);
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).
8858 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8860 + "unionfs: remount flags 0x%x unsupported\n", *flags);
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.
8870 + if (!options || options[0] == '\0')
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.
8878 + tmp_to_free = kstrdup(options, GFP_KERNEL);
8879 + optionstmp = tmp_to_free;
8880 + if (unlikely(!optionstmp)) {
8884 + cur_branches = sbmax(sb); /* current no. branches */
8885 + new_branches = sbmax(sb);
8888 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
8889 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
8892 + if (!optname || !*optname)
8895 + optarg = strchr(optname, '=');
8899 + if (!strcmp("add", optname))
8901 + else if (!strcmp("del", optname))
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) {
8908 + "unionfs: no branches left after remount\n");
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).
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)) {
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)) {
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 */
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 + *******************************************************************/
8961 + while ((optname = strsep(&options, ",")) != NULL) {
8964 + if (!optname || !*optname)
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".
8973 + optarg = strchr(optname, '=');
8976 + /* incgen remount option (instead of old ioctl) */
8977 + if (!strcmp("incgen", optname)) {
8979 + goto out_no_change;
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.
8987 + if (!optarg || !*optarg) {
8988 + printk(KERN_ERR "unionfs: all remount options require "
8989 + "an argument (%s)\n", optname);
8994 + if (!strcmp("add", optname)) {
8995 + err = do_remount_add_option(optarg, new_branches,
8998 + &new_high_branch_id);
9002 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9003 + printk(KERN_ERR "unionfs: command exceeds "
9004 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9010 + if (!strcmp("del", optname)) {
9011 + err = do_remount_del_option(optarg, new_branches,
9019 + if (!strcmp("mode", optname)) {
9020 + err = do_remount_mode_option(optarg, new_branches,
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.
9037 + if (!strcmp("dirs", optname)) {
9038 + printk(KERN_WARNING
9039 + "unionfs: remount ignoring option \"%s\"\n",
9046 + "unionfs: unrecognized option \"%s\"\n", optname);
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 + *******************************************************************/
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");
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)) {
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)) {
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)) {
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).
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.
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);
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 */
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 */
9143 + * Update lower inodes: 3 steps
9144 + * 1. grab ref on all new lower inodes
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;
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;
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;
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 */
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.
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]);
9188 + kfree(tmp_lower_paths);
9190 + kfree(new_lower_paths);
9192 + kfree(new_lower_inodes);
9194 + unionfs_check_dentry(sb->s_root);
9195 + unionfs_write_unlock(sb);
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.
9205 + * No need to lock sb info's rwsem.
9207 +static void unionfs_clear_inode(struct inode *inode)
9209 + int bindex, bstart, bend;
9210 + struct inode *lower_inode;
9211 + struct list_head *pos, *n;
9212 + struct unionfs_dir_state *rdstate;
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);
9221 + * Decrement a reference to a lower_inode, which was incremented
9222 + * by our read_inode when it was created initially.
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);
9231 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9232 + /* see Documentation/filesystems/unionfs/issues.txt */
9234 + iput(lower_inode);
9239 + kfree(UNIONFS_I(inode)->lower_inodes);
9240 + UNIONFS_I(inode)->lower_inodes = NULL;
9243 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9245 + struct unionfs_inode_info *i;
9247 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9251 + /* memset everything up to the inode to 0 */
9252 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9254 + i->vfs_inode.i_version = 1;
9255 + return &i->vfs_inode;
9258 +static void unionfs_destroy_inode(struct inode *inode)
9260 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9263 +/* unionfs inode cache constructor */
9264 +static void init_once(void *obj)
9266 + struct unionfs_inode_info *i = obj;
9268 + inode_init_once(&i->vfs_inode);
9271 +int unionfs_init_inode_cache(void)
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))
9284 +/* unionfs inode cache destructor */
9285 +void unionfs_destroy_inode_cache(void)
9287 + if (unionfs_inode_cachep)
9288 + kmem_cache_destroy(unionfs_inode_cachep);
9292 + * Called when we have a dirty inode, right here we only throw out
9293 + * parts of our readdir list that are too old.
9295 + * No need to grab sb info's rwsem.
9297 +static int unionfs_write_inode(struct inode *inode, int sync)
9299 + struct list_head *pos, *n;
9300 + struct unionfs_dir_state *rdstate;
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)
9308 + UNIONFS_I(inode)->rdcount--;
9309 + list_del(&rdstate->cache);
9310 + free_rdstate(rdstate);
9312 + spin_unlock(&UNIONFS_I(inode)->rdlock);
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.
9321 +static void unionfs_umount_begin(struct super_block *sb)
9323 + struct super_block *lower_sb;
9324 + int bindex, bstart, bend;
9326 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9328 + bstart = sbstart(sb);
9330 + for (bindex = bstart; bindex <= bend; bindex++) {
9331 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9333 + if (lower_sb && lower_sb->s_op &&
9334 + lower_sb->s_op->umount_begin)
9335 + lower_sb->s_op->umount_begin(lower_sb);
9338 + unionfs_read_unlock(sb);
9341 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9343 + struct super_block *sb = mnt->mnt_sb;
9347 + int bindex, bstart, bend;
9350 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9352 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9354 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9355 + if (unlikely(!tmp_page)) {
9360 + bstart = sbstart(sb);
9363 + seq_printf(m, ",dirs=");
9364 + for (bindex = bstart; bindex <= bend; bindex++) {
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);
9374 + perms = branchperms(sb, bindex);
9376 + seq_printf(m, "%s=%s", path,
9377 + perms & MAY_WRITE ? "rw" : "ro");
9378 + if (bindex != bend)
9379 + seq_printf(m, ":");
9383 + free_page((unsigned long) tmp_page);
9385 + unionfs_unlock_dentry(sb->s_root);
9387 + unionfs_read_unlock(sb);
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,
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
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
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.
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>
9458 +#include <asm/system.h>
9460 +#include <linux/union_fs.h>
9462 +/* the file system name */
9463 +#define UNIONFS_NAME "unionfs"
9465 +/* unionfs root inode number */
9466 +#define UNIONFS_ROOT_INO 1
9468 +/* number of times we try to get a unique temporary file name */
9469 +#define GET_TMPNAM_MAX_RETRY 5
9471 +/* maximum number of branches we support, to avoid memory blowup */
9472 +#define UNIONFS_MAX_BRANCHES 128
9474 +/* minimum time (seconds) required for time-based cache-coherency */
9475 +#define UNIONFS_MIN_CC_TIME 3
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;
9488 +/* How long should an entry be allowed to persist */
9489 +#define RDCACHE_JIFFIES (5*HZ)
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 */
9498 +/* file private data. */
9499 +struct unionfs_file_info {
9502 + atomic_t generation;
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 */
9511 +/* unionfs inode data in memory */
9512 +struct unionfs_inode_info {
9515 + atomic_t generation;
9516 + /* Stuff for readdir over NFS. */
9517 + spinlock_t rdlock;
9518 + struct list_head readdircache;
9523 + /* The lower inodes */
9524 + struct inode **lower_inodes;
9526 + struct inode vfs_inode;
9529 +/* unionfs dentry data in memory */
9530 +struct unionfs_dentry_info {
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.
9536 + struct mutex lock;
9541 + atomic_t generation;
9542 + struct path *lower_paths;
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 */
9550 + int branch_id; /* unique branch ID at re/mount time */
9553 +/* unionfs super-block data in memory */
9554 +struct unionfs_sb_info {
9557 + atomic_t generation;
9560 + * This rwsem is used to make sure that a branch management
9562 + * 1) will not begin before all currently in-flight operations
9564 + * 2) any new operations do not execute until the currently
9565 + * running branch management operation completes.
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.
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;
9581 + * structure for making the linked list of entries by readdir on left branch
9582 + * to compare with entries on right branch
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 */
9591 + * we can check for duplicate whiteouts and files in the same branch
9592 + * in order to return -EIO.
9596 + /* is this a whiteout entry? */
9599 + /* Inline name, so we don't need to separately kmalloc small ones */
9600 + char iname[DNAME_INLINE_LEN_MIN];
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. */
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;
9613 + /* This cache list is used when the inode keeps us around. */
9614 + struct list_head cache;
9615 + struct list_head list[0];
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);
9623 +/* include miscellaneous macros */
9624 +#include "fanout.h"
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);
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,
9639 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
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,
9645 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9646 + const char *name, int namelen,
9649 +extern struct dentry **alloc_new_dentries(int objs);
9650 +extern struct unionfs_data *alloc_new_data(int objs);
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)
9661 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9662 + | (buf->offset & DIREOF);
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 */
9672 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9674 + if (UNIONFS_SB(sb)->write_lock_owner &&
9675 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9677 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9679 +static inline void unionfs_read_unlock(struct super_block *sb)
9681 + if (UNIONFS_SB(sb)->write_lock_owner &&
9682 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9684 + up_read(&UNIONFS_SB(sb)->rwsem);
9686 +static inline void unionfs_write_lock(struct super_block *sb)
9688 + down_write(&UNIONFS_SB(sb)->rwsem);
9689 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9691 +static inline void unionfs_write_unlock(struct super_block *sb)
9693 + up_write(&UNIONFS_SB(sb)->rwsem);
9694 + UNIONFS_SB(sb)->write_lock_owner = 0;
9697 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9698 + struct dentry *d2)
9702 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9703 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9705 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9706 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9710 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9711 + struct dentry *d2)
9714 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9715 + unionfs_unlock_dentry(d1);
9716 + unionfs_unlock_dentry(d2);
9718 + unionfs_unlock_dentry(d2);
9719 + unionfs_unlock_dentry(d1);
9723 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9724 + struct dentry *p2)
9727 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9731 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9732 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9734 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9735 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9739 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9740 + struct dentry *p2)
9743 + unionfs_unlock_dentry(p1);
9746 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9747 + unionfs_unlock_dentry(p1);
9748 + unionfs_unlock_dentry(p2);
9750 + unionfs_unlock_dentry(p2);
9751 + unionfs_unlock_dentry(p1);
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);
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);
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,
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,
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);
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);
9811 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9812 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
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);
9818 +/* Common file operations. */
9819 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
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,
9828 +extern int unionfs_fasync(int fd, struct file *file, int flag);
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);
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);
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
9850 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9851 + struct super_block *sb, int flag);
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)
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,
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 */
9869 +/* The root directory is unhashed, but isn't deleted. */
9870 +static inline int d_deleted(struct dentry *d)
9872 + return d_unhashed(d) && (d != d->d_sb->s_root);
9875 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
9876 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
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)
9882 +static inline int branchperms(const struct super_block *sb, int index)
9884 + BUG_ON(index < 0);
9885 + return UNIONFS_SB(sb)->data[index].branchperms;
9888 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
9890 + BUG_ON(index < 0);
9891 + UNIONFS_SB(sb)->data[index].branchperms = perms;
9895 +/* check if readonly lower inode, but possibly unlinked (no inode->i_sb) */
9896 +static inline int __is_rdonly(const struct inode *inode)
9898 + /* if unlinked, can't be readonly (?) */
9901 + return IS_RDONLY(inode);
9904 +/* Is this file on a read-only branch? */
9905 +static inline int is_robranch_super(const struct super_block *sb, int index)
9909 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
9913 +/* Is this file on a read-only branch? */
9914 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
9916 + struct super_block *lower_sb;
9918 + BUG_ON(index < 0);
9920 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
9923 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
9924 + BUG_ON(lower_sb == NULL);
9926 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
9927 + * lower_dentry could be a negative.
9929 + if (lower_sb->s_flags & MS_RDONLY)
9935 +static inline int is_robranch(const struct dentry *dentry)
9939 + index = UNIONFS_D(dentry)->bstart;
9940 + BUG_ON(index < 0);
9942 + return is_robranch_idx(dentry, index);
9948 +extern int check_branch(struct nameidata *nd);
9949 +extern int parse_branch_mode(const char *name, int *perms);
9951 +/* locking helpers */
9952 +static inline struct dentry *lock_parent(struct dentry *dentry)
9954 + struct dentry *dir = dget_parent(dentry);
9955 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
9958 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
9960 + struct dentry *dir = dget_parent(dentry);
9962 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
9966 +static inline void unlock_dir(struct dentry *dir)
9968 + mutex_unlock(&dir->d_inode->i_mutex);
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)
9977 + mutex_lock(&base->d_inode->i_mutex);
9978 + d = lookup_one_len(name, base, len);
9979 + mutex_unlock(&base->d_inode->i_mutex);
9983 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
9986 + struct vfsmount *mnt;
9988 + BUG_ON(!dentry || bindex < 0);
9990 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
9991 +#ifdef CONFIG_UNION_FS_DEBUG
9993 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
9995 +#endif /* CONFIG_UNION_FS_DEBUG */
10000 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10002 + struct vfsmount *mnt;
10004 + if (!dentry && bindex < 0)
10006 + BUG_ON(!dentry || bindex < 0);
10008 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10009 +#ifdef CONFIG_UNION_FS_DEBUG
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.
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 */
10023 +#ifdef CONFIG_UNION_FS_DEBUG
10025 +/* useful for tracking code reachability */
10026 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
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__)
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,
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);
10063 +#else /* not CONFIG_UNION_FS_DEBUG */
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)
10075 +#endif /* not CONFIG_UNION_FS_DEBUG */
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
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
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.
10100 +#include "union.h"
10103 + * Helper function for Unionfs's unlink operation.
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.
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:
10117 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10118 + * appropriate error.
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.
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).
10128 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10129 + struct dentry *parent)
10131 + struct dentry *lower_dentry;
10132 + struct dentry *lower_dir_dentry;
10136 + err = unionfs_partial_lookup(dentry, parent);
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)
10146 + lower_dir_dentry = lock_parent(lower_dentry);
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);
10152 + /* see Documentation/filesystems/unionfs/issues.txt */
10154 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10155 + err = vfs_unlink(lower_dir_dentry->d_inode,
10158 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10163 + /* if lower object deletion succeeds, update inode's times */
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);
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.
10181 + if ((bindex == 0) ||
10182 + ((bindex == dbstart(dentry)) &&
10183 + (!IS_COPYUP_ERR(err))))
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));
10195 + inode_dec_link_count(dentry->d_inode);
10197 + /* We don't want to leave negative leftover dentries for revalidate. */
10198 + if (!err && (dbopaque(dentry) != -1))
10199 + update_bstart(dentry);
10204 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10207 + struct inode *inode = dentry->d_inode;
10208 + struct dentry *parent;
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);
10216 + valid = __unionfs_d_revalidate(dentry, parent, false);
10217 + if (unlikely(!valid)) {
10221 + unionfs_check_dentry(dentry);
10223 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10224 + /* call d_drop so the system "forgets" about us */
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);
10232 + * if unlink/whiteout succeeded, parent dir mtime has
10235 + unionfs_copy_attr_times(dir);
10240 + unionfs_check_dentry(dentry);
10241 + unionfs_check_inode(dir);
10243 + unionfs_unlock_dentry(dentry);
10244 + unionfs_unlock_parent(dentry, parent);
10245 + unionfs_read_unlock(dentry->d_sb);
10249 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10250 + struct unionfs_dir_state *namelist)
10253 + struct dentry *lower_dentry;
10254 + struct dentry *lower_dir_dentry = NULL;
10256 + /* Here we need to remove whiteout entries. */
10257 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10261 + lower_dentry = unionfs_lower_dentry(dentry);
10263 + lower_dir_dentry = lock_parent(lower_dentry);
10265 + /* avoid destroying the lower inode if the file is in use */
10266 + dget(lower_dentry);
10267 + err = is_robranch(dentry);
10269 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10270 + dput(lower_dentry);
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);
10277 + if (lower_dir_dentry)
10278 + unlock_dir(lower_dir_dentry);
10282 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10285 + struct unionfs_dir_state *namelist = NULL;
10286 + struct dentry *parent;
10287 + int dstart, dend;
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);
10294 + valid = __unionfs_d_revalidate(dentry, parent, false);
10295 + if (unlikely(!valid)) {
10299 + unionfs_check_dentry(dentry);
10301 + /* check if this unionfs directory is empty or not */
10302 + err = check_empty(dentry, parent, &namelist);
10306 + err = unionfs_rmdir_first(dir, dentry, namelist);
10307 + dstart = dbstart(dentry);
10308 + dend = dbend(dentry);
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.
10318 + if (dstart < dend)
10319 + err = create_whiteout(dentry, dstart);
10326 + /* exit if the error returned was NOT -EROFS */
10327 + if (!IS_COPYUP_ERR(err))
10330 + new_err = create_whiteout(dentry, dstart - 1);
10331 + if (new_err != -EEXIST)
10337 + * Drop references to lower dentry/inode so storage space for them
10338 + * can be reclaimed. Then, call d_drop so the system "forgets"
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);
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);
10353 + free_rdstate(namelist);
10355 + unionfs_unlock_dentry(dentry);
10356 + unionfs_unlock_parent(dentry, parent);
10357 + unionfs_read_unlock(dentry->d_sb);
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
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
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.
10382 +#include "union.h"
10385 + * whiteout and opaque directory helpers
10388 +/* What do we use for whiteouts. */
10389 +#define UNIONFS_WHPFX ".wh."
10390 +#define UNIONFS_WHLEN 4
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.
10395 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10396 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10398 +/* construct whiteout filename */
10399 +char *alloc_whname(const char *name, int len)
10403 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10404 + if (unlikely(!buf))
10405 + return ERR_PTR(-ENOMEM);
10407 + strcpy(buf, UNIONFS_WHPFX);
10408 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10414 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10415 + * code in one place.
10417 +void unionfs_set_max_namelen(long *namelen)
10419 + *namelen -= UNIONFS_WHLEN;
10422 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10423 +bool is_whiteout_name(char **namep, int *namelenp)
10425 + if (*namelenp > UNIONFS_WHLEN &&
10426 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10427 + *namep += UNIONFS_WHLEN;
10428 + *namelenp -= UNIONFS_WHLEN;
10434 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10435 +bool is_validname(const char *name)
10437 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10439 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10440 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
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.
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
10453 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10455 + char *whname = NULL;
10456 + int err = 0, namelen;
10457 + struct dentry *wh_dentry = NULL;
10459 + namelen = strlen(name);
10460 + whname = alloc_whname(name, namelen);
10461 + if (unlikely(IS_ERR(whname))) {
10462 + err = PTR_ERR(whname);
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);
10473 + /* check if negative dentry (ENOENT) */
10474 + if (!wh_dentry->d_inode)
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);
10489 + wh_dentry = ERR_PTR(err);
10490 + return wh_dentry;
10493 +/* find and return first whiteout in parent directory, else ENOENT */
10494 +struct dentry *find_first_whiteout(struct dentry *dentry)
10496 + int bindex, bstart, bend;
10497 + struct dentry *parent, *lower_parent, *wh_dentry;
10499 + parent = dget_parent(dentry);
10501 + bstart = dbstart(parent);
10502 + bend = dbend(parent);
10503 + wh_dentry = ERR_PTR(-ENOENT);
10505 + for (bindex = bstart; bindex <= bend; bindex++) {
10506 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10507 + if (!lower_parent)
10509 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10510 + if (IS_ERR(wh_dentry))
10512 + if (wh_dentry->d_inode)
10515 + wh_dentry = ERR_PTR(-ENOENT);
10520 + return wh_dentry;
10524 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10525 + * release dentry reference.
10527 +int unlink_whiteout(struct dentry *wh_dentry)
10530 + struct dentry *lower_dir_dentry;
10532 + /* dget and lock parent dentry */
10533 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10535 + /* see Documentation/filesystems/unionfs/issues.txt */
10537 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10539 + unlock_dir(lower_dir_dentry);
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.
10552 + if (err == -EPERM) {
10553 + struct inode *inode = lower_dir_dentry->d_inode;
10554 + err = inode->i_op->unlink(inode, wh_dentry);
10557 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10558 + "err = %d\n", wh_dentry->d_name.name, err);
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
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.
10578 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10582 + struct dentry *wh_dentry = NULL;
10583 + struct dentry *lower_dir_dentry = NULL;
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);
10594 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10599 + /* check if regular file and whiteout were both found */
10600 + if (unlikely(lower_dentry->d_inode)) {
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);
10608 + /* check if branch is writeable */
10609 + err = is_robranch_super(dentry->d_sb, bindex);
10613 + /* .wh.foo has been found, so let's unlink it */
10614 + err = unlink_whiteout(wh_dentry);
10616 + err = 1; /* a whiteout was found and successfully removed */
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.
10628 +int create_whiteout(struct dentry *dentry, int start)
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;
10638 + verify_locked(dentry);
10640 + bstart = dbstart(dentry);
10641 + bend = dbend(dentry);
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);
10650 + for (bindex = start; bindex >= 0; bindex--) {
10651 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10653 + if (!lower_dentry) {
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
10661 + lower_dentry = create_parents(dentry->d_inode,
10663 + dentry->d_name.name,
10665 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10666 + int ret = PTR_ERR(lower_dentry);
10667 + if (!IS_COPYUP_ERR(ret))
10669 + "unionfs: create_parents for "
10670 + "whiteout failed: bindex=%d "
10671 + "err=%d\n", bindex, ret);
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))
10683 + * The whiteout already exists. This used to be impossible,
10684 + * but now is possible because of opaqueness.
10686 + if (lower_wh_dentry->d_inode) {
10687 + dput(lower_wh_dentry);
10692 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10693 + if (unlikely(err < 0))
10695 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10696 + err = is_robranch_super(dentry->d_sb, bindex);
10698 + err = vfs_create(lower_dir_dentry->d_inode,
10700 + current_umask() & S_IRUGO,
10702 + unlock_dir(lower_dir_dentry);
10703 + dput(lower_wh_dentry);
10704 + release_lower_nd(&nd, err);
10706 + if (!err || !IS_COPYUP_ERR(err))
10710 + /* set dbopaque so that lookup will not proceed after this branch */
10712 + dbopaque(dentry) = bindex;
10720 + * Delete all of the whiteouts in a given directory for rmdir.
10722 + * lower directory inode should be locked
10724 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10725 + struct unionfs_dir_state *namelist)
10728 + struct dentry *lower_dir_dentry = NULL;
10729 + struct dentry *lower_dentry;
10730 + char *name = NULL, *p;
10731 + struct inode *lower_dir;
10733 + struct list_head *pos;
10734 + struct filldir_node *cursor;
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));
10743 + name = __getname();
10744 + if (unlikely(!name))
10746 + strcpy(name, UNIONFS_WHPFX);
10747 + p = name + UNIONFS_WHLEN;
10750 + for (i = 0; !err && i < namelist->size; i++) {
10751 + list_for_each(pos, &namelist->list[i]) {
10753 + list_entry(pos, struct filldir_node,
10755 + /* Only operate on whiteouts in this branch. */
10756 + if (cursor->bindex != bindex)
10758 + if (!cursor->whiteout)
10761 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10763 + lookup_lck_len(name, lower_dir_dentry,
10764 + cursor->namelen +
10766 + if (IS_ERR(lower_dentry)) {
10767 + err = PTR_ERR(lower_dentry);
10770 + if (lower_dentry->d_inode)
10771 + err = vfs_unlink(lower_dir, lower_dentry);
10772 + dput(lower_dentry);
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);
10788 +void __delete_whiteouts(struct work_struct *work)
10790 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10791 + struct deletewh_args *d = &args->deletewh;
10793 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10794 + complete(&args->comp);
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)
10802 + struct super_block *sb;
10803 + struct dentry *lower_dir_dentry;
10804 + struct inode *lower_dir;
10805 + struct sioq_args args;
10807 + sb = dentry->d_sb;
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);
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));
10821 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10822 + err = do_delete_whiteouts(dentry, bindex, namelist);
10824 + args.deletewh.namelist = namelist;
10825 + args.deletewh.dentry = dentry;
10826 + args.deletewh.bindex = bindex;
10827 + run_sioq(__delete_whiteouts, &args);
10835 +/****************************************************************************
10836 + * Opaque directory helpers *
10837 + ****************************************************************************/
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.
10843 +int is_opaque_dir(struct dentry *dentry, int bindex)
10846 + struct dentry *lower_dentry;
10847 + struct dentry *wh_lower_dentry;
10848 + struct inode *lower_inode;
10849 + struct sioq_args args;
10851 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10852 + lower_inode = lower_dentry->d_inode;
10854 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
10856 + mutex_lock(&lower_inode->i_mutex);
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);
10863 + args.is_opaque.dentry = lower_dentry;
10864 + run_sioq(__is_opaque_dir, &args);
10865 + wh_lower_dentry = args.ret;
10868 + mutex_unlock(&lower_inode->i_mutex);
10870 + if (IS_ERR(wh_lower_dentry)) {
10871 + err = PTR_ERR(wh_lower_dentry);
10875 + /* This is an opaque dir iff wh_lower_dentry is positive */
10876 + err = !!wh_lower_dentry->d_inode;
10878 + dput(wh_lower_dentry);
10883 +void __is_opaque_dir(struct work_struct *work)
10885 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10887 + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
10888 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
10889 + complete(&args->comp);
10892 +int make_dir_opaque(struct dentry *dentry, int bindex)
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;
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).
10909 + new_creds = prepare_creds();
10910 + if (unlikely(!new_creds)) {
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);
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));
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);
10931 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10932 + if (unlikely(err < 0))
10934 + if (!diropq->d_inode)
10935 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
10937 + dbopaque(dentry) = bindex;
10938 + release_lower_nd(&nd, err);
10943 + mutex_unlock(&lower_dir->i_mutex);
10944 + revert_creds(old_creds);
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
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
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.
10970 +#include "union.h"
10972 +/* This is lifted from fs/xattr.c */
10973 +void *unionfs_xattr_alloc(size_t size, size_t limit)
10977 + if (size > limit)
10978 + return ERR_PTR(-E2BIG);
10980 + if (!size) /* size request, no buffer is needed */
10983 + ptr = kmalloc(size, GFP_KERNEL);
10984 + if (unlikely(!ptr))
10985 + return ERR_PTR(-ENOMEM);
10990 + * BKL held by caller.
10991 + * dentry->d_inode->i_mutex locked
10993 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
10996 + struct dentry *lower_dentry = NULL;
10997 + struct dentry *parent;
10998 + int err = -EOPNOTSUPP;
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);
11005 + valid = __unionfs_d_revalidate(dentry, parent, false);
11006 + if (unlikely(!valid)) {
11011 + lower_dentry = unionfs_lower_dentry(dentry);
11013 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11016 + unionfs_check_dentry(dentry);
11017 + unionfs_unlock_dentry(dentry);
11018 + unionfs_unlock_parent(dentry, parent);
11019 + unionfs_read_unlock(dentry->d_sb);
11024 + * BKL held by caller.
11025 + * dentry->d_inode->i_mutex locked
11027 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11028 + const void *value, size_t size, int flags)
11030 + struct dentry *lower_dentry = NULL;
11031 + struct dentry *parent;
11032 + int err = -EOPNOTSUPP;
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);
11039 + valid = __unionfs_d_revalidate(dentry, parent, false);
11040 + if (unlikely(!valid)) {
11045 + lower_dentry = unionfs_lower_dentry(dentry);
11047 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11051 + unionfs_check_dentry(dentry);
11052 + unionfs_unlock_dentry(dentry);
11053 + unionfs_unlock_parent(dentry, parent);
11054 + unionfs_read_unlock(dentry->d_sb);
11059 + * BKL held by caller.
11060 + * dentry->d_inode->i_mutex locked
11062 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11064 + struct dentry *lower_dentry = NULL;
11065 + struct dentry *parent;
11066 + int err = -EOPNOTSUPP;
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);
11073 + valid = __unionfs_d_revalidate(dentry, parent, false);
11074 + if (unlikely(!valid)) {
11079 + lower_dentry = unionfs_lower_dentry(dentry);
11081 + err = vfs_removexattr(lower_dentry, (char *) name);
11084 + unionfs_check_dentry(dentry);
11085 + unionfs_unlock_dentry(dentry);
11086 + unionfs_unlock_parent(dentry, parent);
11087 + unionfs_read_unlock(dentry->d_sb);
11092 + * BKL held by caller.
11093 + * dentry->d_inode->i_mutex locked
11095 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11097 + struct dentry *lower_dentry = NULL;
11098 + struct dentry *parent;
11099 + int err = -EOPNOTSUPP;
11100 + char *encoded_list = NULL;
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);
11107 + valid = __unionfs_d_revalidate(dentry, parent, false);
11108 + if (unlikely(!valid)) {
11113 + lower_dentry = unionfs_lower_dentry(dentry);
11115 + encoded_list = list;
11116 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11119 + unionfs_check_dentry(dentry);
11120 + unionfs_unlock_dentry(dentry);
11121 + unionfs_unlock_parent(dentry, parent);
11122 + unionfs_read_unlock(dentry->d_sb);
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
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
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.
11140 #ifndef _LINUX_FS_STACK_H
11141 #define _LINUX_FS_STACK_H
11143 -/* This file defines generic functions used primarily by stackable
11145 + * This file defines generic functions used primarily by stackable
11146 * filesystems; none of these functions require i_mutex to be held.
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
11153 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11154 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11156 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
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
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 *);
11170 extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
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);
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
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
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.
11202 +#ifndef _LINUX_UNION_FS_H
11203 +#define _LINUX_UNION_FS_H
11206 + * DEFINITIONS FOR USER AND KERNEL CODE:
11208 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11209 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11211 +#endif /* _LINUX_UNIONFS_H */
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
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
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
11236 return security_ops->inode_permission(inode, mask);
11238 +EXPORT_SYMBOL(security_inode_permission);
11240 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
11243 This adapts unionfs to vfs changes in 2.6.35
11245 Signed-off-by: Thomas Backlund <tmb@mandriva.org>
11247 fs/copyup.c | 3 +--
11248 fs/file.c | 7 +++----
11251 4 files changed, 6 insertions(+), 9 deletions(-)
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
11260 - err = output_file->f_op->fsync(output_file,
11261 - new_lower_dentry, 0);
11262 + err = output_file->f_op->fsync(output_file, 0);
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:
11273 -int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
11274 +int unionfs_fsync(struct file *file, int datasync)
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,
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)
11296 * No need to grab sb info's rwsem.
11298 -static int unionfs_write_inode(struct inode *inode, int sync)
11299 +static int unionfs_write_inode(struct inode *inode, struct writeback_control *wbc)
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,
11312 +extern int unionfs_fsync(struct file *file, int datasync);
11313 extern int unionfs_fasync(int fd, struct file *file, int flag);
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
11321 Signed-off-by: Pascal Terjan <pterjan@mandriva.com>
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 @@
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);
11335 * if unlink/whiteout succeeded, parent dir mtime has
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.
11346 security/smack/smack_lsm.c | 23 +++++++++++++++++++++++
11347 1 file changed, 23 insertions(+)
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
11354 sbsp = sbp->s_security;
11357 + * If the superblock isn't initialized someone
11358 + * has cobbled together a filesystem that never
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;
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
11371 final = smack_known_star.smk_known;
11374 + case UNIONFS_SUPER_MAGIC:
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.
11380 + * Let the real filesystem inodes provide the
11381 + * Smack label on access checks.
11383 + final = smack_known_star.smk_known;
11385 case DEVPTS_SUPER_MAGIC:
11387 * devpts seems content with the label of the task.