Magellan Linux

Annotation of /trunk/kernel26-alx/patches-2.6.29-r2/0153-2.6.29-unionfs-2.5.1-2.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1022 - (hide annotations) (download)
Sun Apr 25 14:59:21 2010 UTC (14 years ago) by niro
File size: 342054 byte(s)
-added alx kernel

1 niro 1022 diff -Naur linux-2.6.29/Documentation/filesystems/00-INDEX linux-2.6.29-magellan/Documentation/filesystems/00-INDEX
2     --- linux-2.6.29/Documentation/filesystems/00-INDEX 2009-03-24 00:12:14.000000000 +0100
3     +++ linux-2.6.29-magellan/Documentation/filesystems/00-INDEX 2009-04-23 19:41:06.000000000 +0200
4     @@ -106,6 +106,8 @@
5     - info and mount options for the UDF filesystem.
6     ufs.txt
7     - info on the ufs filesystem.
8     +unionfs/
9     + - info on the unionfs filesystem
10     vfat.txt
11     - info on using the VFAT filesystem used in Windows NT and Windows 95
12     vfs.txt
13     diff -Naur linux-2.6.29/Documentation/filesystems/unionfs/00-INDEX linux-2.6.29-magellan/Documentation/filesystems/unionfs/00-INDEX
14     --- linux-2.6.29/Documentation/filesystems/unionfs/00-INDEX 1970-01-01 01:00:00.000000000 +0100
15     +++ linux-2.6.29-magellan/Documentation/filesystems/unionfs/00-INDEX 2009-04-23 19:41:06.000000000 +0200
16     @@ -0,0 +1,10 @@
17     +00-INDEX
18     + - this file.
19     +concepts.txt
20     + - A brief introduction of concepts.
21     +issues.txt
22     + - A summary of known issues with unionfs.
23     +rename.txt
24     + - Information regarding rename operations.
25     +usage.txt
26     + - Usage information and examples.
27     diff -Naur linux-2.6.29/Documentation/filesystems/unionfs/concepts.txt linux-2.6.29-magellan/Documentation/filesystems/unionfs/concepts.txt
28     --- linux-2.6.29/Documentation/filesystems/unionfs/concepts.txt 1970-01-01 01:00:00.000000000 +0100
29     +++ linux-2.6.29-magellan/Documentation/filesystems/unionfs/concepts.txt 2009-04-23 19:41:06.000000000 +0200
30     @@ -0,0 +1,287 @@
31     +Unionfs 2.x CONCEPTS:
32     +=====================
33     +
34     +This file describes the concepts needed by a namespace unification file
35     +system.
36     +
37     +
38     +Branch Priority:
39     +================
40     +
41     +Each branch is assigned a unique priority - starting from 0 (highest
42     +priority). No two branches can have the same priority.
43     +
44     +
45     +Branch Mode:
46     +============
47     +
48     +Each branch is assigned a mode - read-write or read-only. This allows
49     +directories on media mounted read-write to be used in a read-only manner.
50     +
51     +
52     +Whiteouts:
53     +==========
54     +
55     +A whiteout removes a file name from the namespace. Whiteouts are needed when
56     +one attempts to remove a file on a read-only branch.
57     +
58     +Suppose we have a two-branch union, where branch 0 is read-write and branch
59     +1 is read-only. And a file 'foo' on branch 1:
60     +
61     +./b0/
62     +./b1/
63     +./b1/foo
64     +
65     +The unified view would simply be:
66     +
67     +./union/
68     +./union/foo
69     +
70     +Since 'foo' is stored on a read-only branch, it cannot be removed. A
71     +whiteout is used to remove the name 'foo' from the unified namespace. Again,
72     +since branch 1 is read-only, the whiteout cannot be created there. So, we
73     +try on a higher priority (lower numerically) branch and create the whiteout
74     +there.
75     +
76     +./b0/
77     +./b0/.wh.foo
78     +./b1/
79     +./b1/foo
80     +
81     +Later, when Unionfs traverses branches (due to lookup or readdir), it
82     +eliminate 'foo' from the namespace (as well as the whiteout itself.)
83     +
84     +
85     +Opaque Directories:
86     +===================
87     +
88     +Assume we have a unionfs mount comprising of two branches. Branch 0 is
89     +empty; branch 1 has the directory /a and file /a/f. Let's say we mount a
90     +union of branch 0 as read-write and branch 1 as read-only. Now, let's say
91     +we try to perform the following operation in the union:
92     +
93     + rm -fr a
94     +
95     +Because branch 1 is not writable, we cannot physically remove the file /a/f
96     +or the directory /a. So instead, we will create a whiteout in branch 0
97     +named /.wh.a, masking out the name "a" from branch 1. Next, let's say we
98     +try to create a directory named "a" as follows:
99     +
100     + mkdir a
101     +
102     +Because we have a whiteout for "a" already, Unionfs behaves as if "a"
103     +doesn't exist, and thus will delete the whiteout and replace it with an
104     +actual directory named "a".
105     +
106     +The problem now is that if you try to "ls" in the union, Unionfs will
107     +perform is normal directory name unification, for *all* directories named
108     +"a" in all branches. This will cause the file /a/f from branch 1 to
109     +re-appear in the union's namespace, which violates Unix semantics.
110     +
111     +To avoid this problem, we have a different form of whiteouts for
112     +directories, called "opaque directories" (same as BSD Union Mount does).
113     +Whenever we replace a whiteout with a directory, that directory is marked as
114     +opaque. In Unionfs 2.x, it means that we create a file named
115     +/a/.wh.__dir_opaque in branch 0, after having created directory /a there.
116     +When unionfs notices that a directory is opaque, it stops all namespace
117     +operations (including merging readdir contents) at that opaque directory.
118     +This prevents re-exposing names from masked out directories.
119     +
120     +
121     +Duplicate Elimination:
122     +======================
123     +
124     +It is possible for files on different branches to have the same name.
125     +Unionfs then has to select which instance of the file to show to the user.
126     +Given the fact that each branch has a priority associated with it, the
127     +simplest solution is to take the instance from the highest priority
128     +(numerically lowest value) and "hide" the others.
129     +
130     +
131     +Unlinking:
132     +=========
133     +
134     +Unlink operation on non-directory instances is optimized to remove the
135     +maximum possible objects in case multiple underlying branches have the same
136     +file name. The unlink operation will first try to delete file instances
137     +from highest priority branch and then move further to delete from remaining
138     +branches in order of their decreasing priority. Consider a case (F..D..F),
139     +where F is a file and D is a directory of the same name; here, some
140     +intermediate branch could have an empty directory instance with the same
141     +name, so this operation also tries to delete this directory instance and
142     +proceed further to delete from next possible lower priority branch. The
143     +unionfs unlink operation will smoothly delete the files with same name from
144     +all possible underlying branches. In case if some error occurs, it creates
145     +whiteout in highest priority branch that will hide file instance in rest of
146     +the branches. An error could occur either if an unlink operations in any of
147     +the underlying branch failed or if a branch has no write permission.
148     +
149     +This unlinking policy is known as "delete all" and it has the benefit of
150     +overall reducing the number of inodes used by duplicate files, and further
151     +reducing the total number of inodes consumed by whiteouts. The cost is of
152     +extra processing, but testing shows this extra processing is well worth the
153     +savings.
154     +
155     +
156     +Copyup:
157     +=======
158     +
159     +When a change is made to the contents of a file's data or meta-data, they
160     +have to be stored somewhere. The best way is to create a copy of the
161     +original file on a branch that is writable, and then redirect the write
162     +though to this copy. The copy must be made on a higher priority branch so
163     +that lookup and readdir return this newer "version" of the file rather than
164     +the original (see duplicate elimination).
165     +
166     +An entire unionfs mount can be read-only or read-write. If it's read-only,
167     +then none of the branches will be written to, even if some of the branches
168     +are physically writeable. If the unionfs mount is read-write, then the
169     +leftmost (highest priority) branch must be writeable (for copyup to take
170     +place); the remaining branches can be any mix of read-write and read-only.
171     +
172     +In a writeable mount, unionfs will create new files/dir in the leftmost
173     +branch. If one tries to modify a file in a read-only branch/media, unionfs
174     +will copyup the file to the leftmost branch and modify it there. If you try
175     +to modify a file from a writeable branch which is not the leftmost branch,
176     +then unionfs will modify it in that branch; this is useful if you, say,
177     +unify differnet packages (e.g., apache, sendmail, ftpd, etc.) and you want
178     +changes to specific package files to remain logically in the directory where
179     +they came from.
180     +
181     +Cache Coherency:
182     +================
183     +
184     +Unionfs users often want to be able to modify files and directories directly
185     +on the lower branches, and have those changes be visible at the Unionfs
186     +level. This means that data (e.g., pages) and meta-data (dentries, inodes,
187     +open files, etc.) have to be synchronized between the upper and lower
188     +layers. In other words, the newest changes from a layer below have to be
189     +propagated to the Unionfs layer above. If the two layers are not in sync, a
190     +cache incoherency ensues, which could lead to application failures and even
191     +oopses. The Linux kernel, however, has a rather limited set of mechanisms
192     +to ensure this inter-layer cache coherency---so Unionfs has to do most of
193     +the hard work on its own.
194     +
195     +Maintaining Invariants:
196     +
197     +The way Unionfs ensures cache coherency is as follows. At each entry point
198     +to a Unionfs file system method, we call a utility function to validate the
199     +primary objects of this method. Generally, we call unionfs_file_revalidate
200     +on open files, and __unionfs_d_revalidate_chain on dentries (which also
201     +validates inodes). These utility functions check to see whether the upper
202     +Unionfs object is in sync with any of the lower objects that it represents.
203     +The checks we perform include whether the Unionfs superblock has a newer
204     +generation number, or if any of the lower objects mtime's or ctime's are
205     +newer. (Note: generation numbers change when branch-management commands are
206     +issued, so in a way, maintaining cache coherency is also very important for
207     +branch-management.) If indeed we determine that any Unionfs object is no
208     +longer in sync with its lower counterparts, then we rebuild that object
209     +similarly to how we do so for branch-management.
210     +
211     +While rebuilding Unionfs's objects, we also purge any page mappings and
212     +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data). This is to
213     +ensure that Unionfs will re-get the newer data from the lower branches. We
214     +perform this purging only if the Unionfs operation in question is a reading
215     +operation; if Unionfs is performing a data writing operation (e.g., ->write,
216     +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
217     +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
218     +considered more authoritative anyway, as they are newer and will overwrite
219     +any lower pages.
220     +
221     +Unionfs maintains the following important invariant regarding mtime's,
222     +ctime's, and atime's: the upper inode object's times are the max() of all of
223     +the lower ones. For non-directory objects, there's only one object below,
224     +so the mapping is simple; for directory objects, there could me multiple
225     +lower objects and we have to sync up with the newest one of all the lower
226     +ones. This invariant is important to maintain, especially for directories
227     +(besides, we need this to be POSIX compliant). A union could comprise
228     +multiple writable branches, each of which could change. If we don't reflect
229     +the newest possible mtime/ctime, some applications could fail. For example,
230     +NFSv2/v3 exports check for newer directory mtimes on the server to determine
231     +if the client-side attribute cache should be purged.
232     +
233     +To maintain these important invariants, of course, Unionfs carefully
234     +synchronizes upper and lower times in various places. For example, if we
235     +copy-up a file to a top-level branch, the parent directory where the file
236     +was copied up to will now have a new mtime: so after a successful copy-up,
237     +we sync up with the new top-level branch's parent directory mtime.
238     +
239     +Implementation:
240     +
241     +This cache-coherency implementation is efficient because it defers any
242     +synchronizing between the upper and lower layers until absolutely needed.
243     +Consider the example a common situation where users perform a lot of lower
244     +changes, such as untarring a whole package. While these take place,
245     +typically the user doesn't access the files via Unionfs; only after the
246     +lower changes are done, does the user try to access the lower files. With
247     +our cache-coherency implementation, the entirety of the changes to the lower
248     +branches will not result in a single CPU cycle spent at the Unionfs level
249     +until the user invokes a system call that goes through Unionfs.
250     +
251     +We have considered two alternate cache-coherency designs. (1) Using the
252     +dentry/inode notify functionality to register interest in finding out about
253     +any lower changes. This is a somewhat limited and also a heavy-handed
254     +approach which could result in many notifications to the Unionfs layer upon
255     +each small change at the lower layer (imagine a file being modified multiple
256     +times in rapid succession). (2) Rewriting the VFS to support explicit
257     +callbacks from lower objects to upper objects. We began exploring such an
258     +implementation, but found it to be very complicated--it would have resulted
259     +in massive VFS/MM changes which are unlikely to be accepted by the LKML
260     +community. We therefore believe that our current cache-coherency design and
261     +implementation represent the best approach at this time.
262     +
263     +Limitations:
264     +
265     +Our implementation works in that as long as a user process will have caused
266     +Unionfs to be called, directly or indirectly, even to just do
267     +->d_revalidate; then we will have purged the current Unionfs data and the
268     +process will see the new data. For example, a process that continually
269     +re-reads the same file's data will see the NEW data as soon as the lower
270     +file had changed, upon the next read(2) syscall (even if the file is still
271     +open!) However, this doesn't work when the process re-reads the open file's
272     +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
273     +it). Once we respond to ->readpage(s), then the kernel maps the page into
274     +the process's address space and there doesn't appear to be a way to force
275     +the kernel to invalidate those pages/mappings, and force the process to
276     +re-issue ->readpage. If there's a way to invalidate active mappings and
277     +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
278     +the trick).
279     +
280     +Our current Unionfs code has to perform many file-revalidation calls. It
281     +would be really nice if the VFS would export an optional file system hook
282     +->file_revalidate (similarly to dentry->d_revalidate) that will be called
283     +before each VFS op that has a "struct file" in it.
284     +
285     +Certain file systems have micro-second granularity (or better) for inode
286     +times, and asynchronous actions could cause those times to change with some
287     +small delay. In such cases, Unionfs may see a changed inode time that only
288     +differs by a tiny fraction of a second: such a change may be a false
289     +positive indication that the lower object has changed, whereas if unionfs
290     +waits a little longer, that false indication will not be seen. (These false
291     +positives are harmless, because they would at most cause unionfs to
292     +re-validate an object that may need no revalidation, and print a debugging
293     +message that clutters the console/logs.) Therefore, to minimize the chances
294     +of these situations, we delay the detection of changed times by a small
295     +factor of a few seconds, called UNIONFS_MIN_CC_TIME (which defaults to 3
296     +seconds, as does NFS). This means that we will detect the change, only a
297     +couple of seconds later, if indeed the time change persists in the lower
298     +file object. This delayed detection has an added performance benefit: we
299     +reduce the number of times that unionfs has to revalidate objects, in case
300     +there's a lot of concurrent activity on both the upper and lower objects,
301     +for the same file(s). Lastly, this delayed time attribute detection is
302     +similar to how NFS clients operate (e.g., acregmin).
303     +
304     +Finally, there is no way currently in Linux to prevent lower directories
305     +from being moved around (i.e., topology changes); there's no way to prevent
306     +modifications to directory sub-trees of whole file systems which are mounted
307     +read-write. It is therefore possible for in-flight operations in unionfs to
308     +take place, while a lower directory is being moved around. Therefore, if
309     +you try to, say, create a new file in a directory through unionfs, while the
310     +directory is being moved around directly, then the new file may get created
311     +in the new location where that directory was moved to. This is a somewhat
312     +similar behaviour in NFS: an NFS client could be creating a new file while
313     +th NFS server is moving th directory around; the file will get successfully
314     +created in the new location. (The one exception in unionfs is that if the
315     +branch is marked read-only by unionfs, then a copyup will take place.)
316     +
317     +For more information, see <http://unionfs.filesystems.org/>.
318     diff -Naur linux-2.6.29/Documentation/filesystems/unionfs/issues.txt linux-2.6.29-magellan/Documentation/filesystems/unionfs/issues.txt
319     --- linux-2.6.29/Documentation/filesystems/unionfs/issues.txt 1970-01-01 01:00:00.000000000 +0100
320     +++ linux-2.6.29-magellan/Documentation/filesystems/unionfs/issues.txt 2009-04-23 19:41:06.000000000 +0200
321     @@ -0,0 +1,28 @@
322     +KNOWN Unionfs 2.x ISSUES:
323     +=========================
324     +
325     +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
326     + confuses NFSv4. Currently, unionfs_lookup() passes lookup intents to the
327     + lower file-system, this eliminates part of the problem. The remaining
328     + calls to lookup_one_len may need to be changed to pass an intent. We are
329     + currently introducing VFS changes to fs/namei.c's do_path_lookup() to
330     + allow proper file lookup and opening in stackable file systems.
331     +
332     +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
333     + incorrectly complains about locking problems. The problem boils down to
334     + this: Lockdep considers all objects of a certain type to be in the same
335     + class, for example, all inodes. Lockdep doesn't like to see a lock held
336     + on two inodes within the same task, and warns that it could lead to a
337     + deadlock. However, stackable file systems do precisely that: they lock
338     + an upper object, and then a lower object, in a strict order to avoid
339     + locking problems; in addition, Unionfs, as a fan-out file system, may
340     + have to lock several lower inodes. We are currently looking into Lockdep
341     + to see how to make it aware of stackable file systems. For now, we
342     + temporarily disable lockdep when calling vfs methods on lower objects,
343     + but only for those places where lockdep complained. While this solution
344     + may seem unclean, it is not without precedent: other places in the kernel
345     + also do similar temporary disabling, of course after carefully having
346     + checked that it is the right thing to do. Anyway, you get any warnings
347     + from Lockdep, please report them to the Unionfs maintainers.
348     +
349     +For more information, see <http://unionfs.filesystems.org/>.
350     diff -Naur linux-2.6.29/Documentation/filesystems/unionfs/rename.txt linux-2.6.29-magellan/Documentation/filesystems/unionfs/rename.txt
351     --- linux-2.6.29/Documentation/filesystems/unionfs/rename.txt 1970-01-01 01:00:00.000000000 +0100
352     +++ linux-2.6.29-magellan/Documentation/filesystems/unionfs/rename.txt 2009-04-23 19:41:06.000000000 +0200
353     @@ -0,0 +1,31 @@
354     +Rename is a complex beast. The following table shows which rename(2) operations
355     +should succeed and which should fail.
356     +
357     +o: success
358     +E: error (either unionfs or vfs)
359     +X: EXDEV
360     +
361     +none = file does not exist
362     +file = file is a file
363     +dir = file is a empty directory
364     +child= file is a non-empty directory
365     +wh = file is a directory containing only whiteouts; this makes it logically
366     + empty
367     +
368     + none file dir child wh
369     +file o o E E E
370     +dir o E o E o
371     +child X E X E X
372     +wh o E o E o
373     +
374     +
375     +Renaming directories:
376     +=====================
377     +
378     +Whenever a empty (either physically or logically) directory is being renamed,
379     +the following sequence of events should take place:
380     +
381     +1) Remove whiteouts from both source and destination directory
382     +2) Rename source to destination
383     +3) Make destination opaque to prevent anything under it from showing up
384     +
385     diff -Naur linux-2.6.29/Documentation/filesystems/unionfs/usage.txt linux-2.6.29-magellan/Documentation/filesystems/unionfs/usage.txt
386     --- linux-2.6.29/Documentation/filesystems/unionfs/usage.txt 1970-01-01 01:00:00.000000000 +0100
387     +++ linux-2.6.29-magellan/Documentation/filesystems/unionfs/usage.txt 2009-04-23 19:41:06.000000000 +0200
388     @@ -0,0 +1,134 @@
389     +Unionfs is a stackable unification file system, which can appear to merge
390     +the contents of several directories (branches), while keeping their physical
391     +content separate. Unionfs is useful for unified source tree management,
392     +merged contents of split CD-ROM, merged separate software package
393     +directories, data grids, and more. Unionfs allows any mix of read-only and
394     +read-write branches, as well as insertion and deletion of branches anywhere
395     +in the fan-out. To maintain Unix semantics, Unionfs handles elimination of
396     +duplicates, partial-error conditions, and more.
397     +
398     +GENERAL SYNTAX
399     +==============
400     +
401     +# mount -t unionfs -o <OPTIONS>,<BRANCH-OPTIONS> none MOUNTPOINT
402     +
403     +OPTIONS can be any legal combination of:
404     +
405     +- ro # mount file system read-only
406     +- rw # mount file system read-write
407     +- remount # remount the file system (see Branch Management below)
408     +- incgen # increment generation no. (see Cache Consistency below)
409     +
410     +BRANCH-OPTIONS can be either (1) a list of branches given to the "dirs="
411     +option, or (2) a list of individual branch manipulation commands, combined
412     +with the "remount" option, and is further described in the "Branch
413     +Management" section below.
414     +
415     +The syntax for the "dirs=" mount option is:
416     +
417     + dirs=branch[=ro|=rw][:...]
418     +
419     +The "dirs=" option takes a colon-delimited list of directories to compose
420     +the union, with an optional branch mode for each of those directories.
421     +Directories that come earlier (specified first, on the left) in the list
422     +have a higher precedence than those which come later. Additionally,
423     +read-only or read-write permissions of the branch can be specified by
424     +appending =ro or =rw (default) to each directory. See the Copyup section in
425     +concepts.txt, for a description of Unionfs's behavior when mixing read-only
426     +and read-write branches and mounts.
427     +
428     +Syntax:
429     +
430     + dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
431     +
432     +Example:
433     +
434     + dirs=/writable_branch=rw:/read-only_branch=ro
435     +
436     +
437     +BRANCH MANAGEMENT
438     +=================
439     +
440     +Once you mount your union for the first time, using the "dirs=" option, you
441     +can then change the union's overall mode or reconfigure the branches, using
442     +the remount option, as follows.
443     +
444     +To downgrade a union from read-write to read-only:
445     +
446     +# mount -t unionfs -o remount,ro none MOUNTPOINT
447     +
448     +To upgrade a union from read-only to read-write:
449     +
450     +# mount -t unionfs -o remount,rw none MOUNTPOINT
451     +
452     +To delete a branch /foo, regardless where it is in the current union:
453     +
454     +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
455     +
456     +To insert (add) a branch /foo before /bar:
457     +
458     +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
459     +
460     +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
461     +
462     +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
463     +
464     +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
465     +new highest-priority branch), you can use the above syntax, or use a short
466     +hand version as follows:
467     +
468     +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
469     +
470     +To append a branch to the very end (new lowest-priority branch):
471     +
472     +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
473     +
474     +To append a branch to the very end (new lowest-priority branch), in
475     +read-only mode:
476     +
477     +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
478     +
479     +Finally, to change the mode of one existing branch, say /foo, from read-only
480     +to read-write, and change /bar from read-write to read-only:
481     +
482     +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
483     +
484     +Note: in Unionfs 2.x, you cannot set the leftmost branch to readonly because
485     +then Unionfs won't have any writable place for copyups to take place.
486     +Moreover, the VFS can get confused when it tries to modify something in a
487     +file system mounted read-write, but isn't permitted to write to it.
488     +Instead, you should set the whole union as readonly, as described above.
489     +If, however, you must set the leftmost branch as readonly, perhaps so you
490     +can get a snapshot of it at a point in time, then you should insert a new
491     +writable top-level branch, and mark the one you want as readonly. This can
492     +be accomplished as follows, assuming that /foo is your current leftmost
493     +branch:
494     +
495     +# mount -t tmpfs -o size=NNN /new
496     +# mount -t unionfs -o remount,add=/new,mode=/foo=ro none MOUNTPOINT
497     +<do what you want safely in /foo>
498     +# mount -t unionfs -o remount,del=/new,mode=/foo=rw none MOUNTPOINT
499     +<check if there's anything in /new you want to preserve>
500     +# umount /new
501     +
502     +CACHE CONSISTENCY
503     +=================
504     +
505     +If you modify any file on any of the lower branches directly, while there is
506     +a Unionfs 2.x mounted above any of those branches, you should tell Unionfs
507     +to purge its caches and re-get the objects. To do that, you have to
508     +increment the generation number of the superblock using the following
509     +command:
510     +
511     +# mount -t unionfs -o remount,incgen none MOUNTPOINT
512     +
513     +Note that the older way of incrementing the generation number using an
514     +ioctl, is no longer supported in Unionfs 2.0 and newer. Ioctls in general
515     +are not encouraged. Plus, an ioctl is per-file concept, whereas the
516     +generation number is a per-file-system concept. Worse, such an ioctl
517     +requires an open file, which then has to be invalidated by the very nature
518     +of the generation number increase (read: the old generation increase ioctl
519     +was pretty racy).
520     +
521     +
522     +For more information, see <http://unionfs.filesystems.org/>.
523     diff -Naur linux-2.6.29/fs/ecryptfs/dentry.c linux-2.6.29-magellan/fs/ecryptfs/dentry.c
524     --- linux-2.6.29/fs/ecryptfs/dentry.c 2009-03-24 00:12:14.000000000 +0100
525     +++ linux-2.6.29-magellan/fs/ecryptfs/dentry.c 2009-04-23 19:41:06.000000000 +0200
526     @@ -62,7 +62,7 @@
527     struct inode *lower_inode =
528     ecryptfs_inode_to_lower(dentry->d_inode);
529    
530     - fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
531     + fsstack_copy_attr_all(dentry->d_inode, lower_inode);
532     }
533     out:
534     return rc;
535     diff -Naur linux-2.6.29/fs/ecryptfs/inode.c linux-2.6.29-magellan/fs/ecryptfs/inode.c
536     --- linux-2.6.29/fs/ecryptfs/inode.c 2009-03-24 00:12:14.000000000 +0100
537     +++ linux-2.6.29-magellan/fs/ecryptfs/inode.c 2009-04-23 19:41:06.000000000 +0200
538     @@ -620,9 +620,9 @@
539     lower_new_dir_dentry->d_inode, lower_new_dentry);
540     if (rc)
541     goto out_lock;
542     - fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
543     + fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
544     if (new_dir != old_dir)
545     - fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
546     + fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
547     out_lock:
548     unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
549     dput(lower_new_dentry->d_parent);
550     @@ -944,7 +944,7 @@
551     rc = notify_change(lower_dentry, ia);
552     mutex_unlock(&lower_dentry->d_inode->i_mutex);
553     out:
554     - fsstack_copy_attr_all(inode, lower_inode, NULL);
555     + fsstack_copy_attr_all(inode, lower_inode);
556     return rc;
557     }
558    
559     diff -Naur linux-2.6.29/fs/ecryptfs/main.c linux-2.6.29-magellan/fs/ecryptfs/main.c
560     --- linux-2.6.29/fs/ecryptfs/main.c 2009-03-24 00:12:14.000000000 +0100
561     +++ linux-2.6.29-magellan/fs/ecryptfs/main.c 2009-04-23 19:41:06.000000000 +0200
562     @@ -194,7 +194,7 @@
563     d_add(dentry, inode);
564     else
565     d_instantiate(dentry, inode);
566     - fsstack_copy_attr_all(inode, lower_inode, NULL);
567     + fsstack_copy_attr_all(inode, lower_inode);
568     /* This size will be overwritten for real files w/ headers and
569     * other metadata */
570     fsstack_copy_inode_size(inode, lower_inode);
571     diff -Naur linux-2.6.29/fs/Kconfig linux-2.6.29-magellan/fs/Kconfig
572     --- linux-2.6.29/fs/Kconfig 2009-03-24 00:12:14.000000000 +0100
573     +++ linux-2.6.29-magellan/fs/Kconfig 2009-04-23 19:47:22.000000000 +0200
574     @@ -186,6 +186,36 @@
575    
576     endmenu
577    
578     +
579     +menu "Layered filesystems"
580     +
581     +config UNION_FS
582     + tristate "Union file system (EXPERIMENTAL)"
583     + depends on EXPERIMENTAL
584     + help
585     + Unionfs is a stackable unification file system, which appears to
586     + merge the contents of several directories (branches), while keeping
587     + their physical content separate.
588     +
589     + See <http://unionfs.filesystems.org/> for details.
590     +
591     +config UNION_FS_XATTR
592     + bool "Unionfs extended attributes"
593     + depends on UNION_FS
594     + help
595     + Extended attributes are name:value pairs associated with inodes by
596     + the kernel or by users (see the attr(5) manual page).
597     +
598     + If unsure, say N.
599     +
600     +config UNION_FS_DEBUG
601     + bool "Debug Unionfs"
602     + depends on UNION_FS
603     + help
604     + If you say Y here, you can turn on debugging output from Unionfs.
605     +
606     +endmenu
607     +
608     menuconfig MISC_FILESYSTEMS
609     bool "Miscellaneous filesystems"
610     default y
611     diff -Naur linux-2.6.29/fs/Makefile linux-2.6.29-magellan/fs/Makefile
612     --- linux-2.6.29/fs/Makefile 2009-03-24 00:12:14.000000000 +0100
613     +++ linux-2.6.29-magellan/fs/Makefile 2009-04-23 19:41:06.000000000 +0200
614     @@ -87,6 +87,7 @@
615     obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
616     obj-$(CONFIG_HFS_FS) += hfs/
617     obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
618     +obj-$(CONFIG_UNION_FS) += unionfs/
619     obj-$(CONFIG_VXFS_FS) += freevxfs/
620     obj-$(CONFIG_NFS_FS) += nfs/
621     obj-$(CONFIG_EXPORTFS) += exportfs/
622     diff -Naur linux-2.6.29/fs/namei.c linux-2.6.29-magellan/fs/namei.c
623     --- linux-2.6.29/fs/namei.c 2009-03-24 00:12:14.000000000 +0100
624     +++ linux-2.6.29-magellan/fs/namei.c 2009-04-23 19:41:06.000000000 +0200
625     @@ -373,6 +373,7 @@
626     else
627     fput(nd->intent.open.file);
628     }
629     +EXPORT_SYMBOL_GPL(release_open_intent);
630    
631     static inline struct dentry *
632     do_revalidate(struct dentry *dentry, struct nameidata *nd)
633     diff -Naur linux-2.6.29/fs/splice.c linux-2.6.29-magellan/fs/splice.c
634     --- linux-2.6.29/fs/splice.c 2009-03-24 00:12:14.000000000 +0100
635     +++ linux-2.6.29-magellan/fs/splice.c 2009-04-23 19:41:06.000000000 +0200
636     @@ -888,8 +888,8 @@
637     /*
638     * Attempt to initiate a splice from pipe to file.
639     */
640     -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
641     - loff_t *ppos, size_t len, unsigned int flags)
642     +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
643     + loff_t *ppos, size_t len, unsigned int flags)
644     {
645     int ret;
646    
647     @@ -908,13 +908,14 @@
648    
649     return out->f_op->splice_write(pipe, out, ppos, len, flags);
650     }
651     +EXPORT_SYMBOL_GPL(vfs_splice_from);
652    
653     /*
654     * Attempt to initiate a splice from a file to a pipe.
655     */
656     -static long do_splice_to(struct file *in, loff_t *ppos,
657     - struct pipe_inode_info *pipe, size_t len,
658     - unsigned int flags)
659     +long vfs_splice_to(struct file *in, loff_t *ppos,
660     + struct pipe_inode_info *pipe, size_t len,
661     + unsigned int flags)
662     {
663     int ret;
664    
665     @@ -930,6 +931,7 @@
666    
667     return in->f_op->splice_read(in, ppos, pipe, len, flags);
668     }
669     +EXPORT_SYMBOL_GPL(vfs_splice_to);
670    
671     /**
672     * splice_direct_to_actor - splices data directly between two non-pipes
673     @@ -999,7 +1001,7 @@
674     size_t read_len;
675     loff_t pos = sd->pos, prev_pos = pos;
676    
677     - ret = do_splice_to(in, &pos, pipe, len, flags);
678     + ret = vfs_splice_to(in, &pos, pipe, len, flags);
679     if (unlikely(ret <= 0))
680     goto out_release;
681    
682     @@ -1058,7 +1060,7 @@
683     {
684     struct file *file = sd->u.file;
685    
686     - return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
687     + return vfs_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
688     }
689    
690     /**
691     @@ -1132,7 +1134,7 @@
692     } else
693     off = &out->f_pos;
694    
695     - ret = do_splice_from(pipe, out, off, len, flags);
696     + ret = vfs_splice_from(pipe, out, off, len, flags);
697    
698     if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
699     ret = -EFAULT;
700     @@ -1153,7 +1155,7 @@
701     } else
702     off = &in->f_pos;
703    
704     - ret = do_splice_to(in, off, pipe, len, flags);
705     + ret = vfs_splice_to(in, off, pipe, len, flags);
706    
707     if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
708     ret = -EFAULT;
709     diff -Naur linux-2.6.29/fs/stack.c linux-2.6.29-magellan/fs/stack.c
710     --- linux-2.6.29/fs/stack.c 2009-03-24 00:12:14.000000000 +0100
711     +++ linux-2.6.29-magellan/fs/stack.c 2009-04-23 19:41:06.000000000 +0200
712     @@ -1,24 +1,82 @@
713     +/*
714     + * Copyright (c) 2006-2009 Erez Zadok
715     + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
716     + * Copyright (c) 2006-2009 Stony Brook University
717     + * Copyright (c) 2006-2009 The Research Foundation of SUNY
718     + *
719     + * This program is free software; you can redistribute it and/or modify
720     + * it under the terms of the GNU General Public License version 2 as
721     + * published by the Free Software Foundation.
722     + */
723     +
724     #include <linux/module.h>
725     #include <linux/fs.h>
726     #include <linux/fs_stack.h>
727    
728     -/* does _NOT_ require i_mutex to be held.
729     +/*
730     + * does _NOT_ require i_mutex to be held.
731     *
732     * This function cannot be inlined since i_size_{read,write} is rather
733     * heavy-weight on 32-bit systems
734     */
735     -void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
736     +void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
737     {
738     - i_size_write(dst, i_size_read((struct inode *)src));
739     - dst->i_blocks = src->i_blocks;
740     + loff_t i_size;
741     + blkcnt_t i_blocks;
742     +
743     + /*
744     + * i_size_read() includes its own seqlocking and protection from
745     + * preemption (see include/linux/fs.h): we need nothing extra for
746     + * that here, and prefer to avoid nesting locks than attempt to
747     + * keep i_size and i_blocks in synch together.
748     + */
749     + i_size = i_size_read(src);
750     +
751     + /*
752     + * But if CONFIG_LSF (on 32-bit), we ought to make an effort to keep
753     + * the two halves of i_blocks in synch despite SMP or PREEMPT - though
754     + * stat's generic_fillattr() doesn't bother, and we won't be applying
755     + * quotas (where i_blocks does become important) at the upper level.
756     + *
757     + * We don't actually know what locking is used at the lower level; but
758     + * if it's a filesystem that supports quotas, it will be using i_lock
759     + * as in inode_add_bytes(). tmpfs uses other locking, and its 32-bit
760     + * is (just) able to exceed 2TB i_size with the aid of holes; but its
761     + * i_blocks cannot carry into the upper long without almost 2TB swap -
762     + * let's ignore that case.
763     + */
764     + if (sizeof(i_blocks) > sizeof(long))
765     + spin_lock(&src->i_lock);
766     + i_blocks = src->i_blocks;
767     + if (sizeof(i_blocks) > sizeof(long))
768     + spin_unlock(&src->i_lock);
769     +
770     + /*
771     + * If CONFIG_SMP on 32-bit, it's vital for fsstack_copy_inode_size()
772     + * to hold some lock around i_size_write(), otherwise i_size_read()
773     + * may spin forever (see include/linux/fs.h). We don't necessarily
774     + * hold i_mutex when this is called, so take i_lock for that case.
775     + *
776     + * And if CONFIG_LSF (on 32-bit), continue our effort to keep the
777     + * two halves of i_blocks in synch despite SMP or PREEMPT: use i_lock
778     + * for that case too, and do both at once by combining the tests.
779     + *
780     + * There is none of this locking overhead in the 64-bit case.
781     + */
782     + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
783     + spin_lock(&dst->i_lock);
784     + i_size_write(dst, i_size);
785     + dst->i_blocks = i_blocks;
786     + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
787     + spin_unlock(&dst->i_lock);
788     }
789     EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
790    
791     -/* copy all attributes; get_nlinks is optional way to override the i_nlink
792     +/*
793     + * copy all attributes; get_nlinks is optional way to override the i_nlink
794     * copying
795     */
796     -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
797     - int (*get_nlinks)(struct inode *))
798     +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
799     {
800     dest->i_mode = src->i_mode;
801     dest->i_uid = src->i_uid;
802     @@ -29,14 +87,6 @@
803     dest->i_ctime = src->i_ctime;
804     dest->i_blkbits = src->i_blkbits;
805     dest->i_flags = src->i_flags;
806     -
807     - /*
808     - * Update the nlinks AFTER updating the above fields, because the
809     - * get_links callback may depend on them.
810     - */
811     - if (!get_nlinks)
812     - dest->i_nlink = src->i_nlink;
813     - else
814     - dest->i_nlink = (*get_nlinks)(dest);
815     + dest->i_nlink = src->i_nlink;
816     }
817     EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
818     diff -Naur linux-2.6.29/fs/unionfs/commonfops.c linux-2.6.29-magellan/fs/unionfs/commonfops.c
819     --- linux-2.6.29/fs/unionfs/commonfops.c 1970-01-01 01:00:00.000000000 +0100
820     +++ linux-2.6.29-magellan/fs/unionfs/commonfops.c 2009-04-23 19:41:06.000000000 +0200
821     @@ -0,0 +1,880 @@
822     +/*
823     + * Copyright (c) 2003-2009 Erez Zadok
824     + * Copyright (c) 2003-2006 Charles P. Wright
825     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
826     + * Copyright (c) 2005-2006 Junjiro Okajima
827     + * Copyright (c) 2005 Arun M. Krishnakumar
828     + * Copyright (c) 2004-2006 David P. Quigley
829     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
830     + * Copyright (c) 2003 Puja Gupta
831     + * Copyright (c) 2003 Harikesavan Krishnan
832     + * Copyright (c) 2003-2009 Stony Brook University
833     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
834     + *
835     + * This program is free software; you can redistribute it and/or modify
836     + * it under the terms of the GNU General Public License version 2 as
837     + * published by the Free Software Foundation.
838     + */
839     +
840     +#include "union.h"
841     +
842     +/*
843     + * 1) Copyup the file
844     + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
845     + * stolen from NFS's silly rename
846     + */
847     +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
848     + struct dentry *parent, int bstart, int bindex)
849     +{
850     + static unsigned int counter;
851     + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
852     + const int countersize = sizeof(counter) * 2;
853     + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
854     + char name[nlen + 1];
855     + int err;
856     + struct dentry *tmp_dentry = NULL;
857     + struct dentry *lower_dentry;
858     + struct dentry *lower_dir_dentry = NULL;
859     +
860     + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
861     +
862     + sprintf(name, ".unionfs%*.*lx",
863     + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
864     +
865     + /*
866     + * Loop, looking for an unused temp name to copyup to.
867     + *
868     + * It's somewhat silly that we look for a free temp tmp name in the
869     + * source branch (bstart) instead of the dest branch (bindex), where
870     + * the final name will be created. We _will_ catch it if somehow
871     + * the name exists in the dest branch, but it'd be nice to catch it
872     + * sooner than later.
873     + */
874     +retry:
875     + tmp_dentry = NULL;
876     + do {
877     + char *suffix = name + nlen - countersize;
878     +
879     + dput(tmp_dentry);
880     + counter++;
881     + sprintf(suffix, "%*.*x", countersize, countersize, counter);
882     +
883     + pr_debug("unionfs: trying to rename %s to %s\n",
884     + dentry->d_name.name, name);
885     +
886     + tmp_dentry = lookup_one_len(name, lower_dentry->d_parent,
887     + nlen);
888     + if (IS_ERR(tmp_dentry)) {
889     + err = PTR_ERR(tmp_dentry);
890     + goto out;
891     + }
892     + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
893     + dput(tmp_dentry);
894     +
895     + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
896     + i_size_read(file->f_path.dentry->d_inode));
897     + if (err) {
898     + if (unlikely(err == -EEXIST))
899     + goto retry;
900     + goto out;
901     + }
902     +
903     + /* bring it to the same state as an unlinked file */
904     + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
905     + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
906     + atomic_inc(&lower_dentry->d_inode->i_count);
907     + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
908     + lower_dentry->d_inode);
909     + }
910     + lower_dir_dentry = lock_parent(lower_dentry);
911     + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
912     + unlock_dir(lower_dir_dentry);
913     +
914     +out:
915     + if (!err)
916     + unionfs_check_dentry(dentry);
917     + return err;
918     +}
919     +
920     +/*
921     + * put all references held by upper struct file and free lower file pointer
922     + * array
923     + */
924     +static void cleanup_file(struct file *file)
925     +{
926     + int bindex, bstart, bend;
927     + struct file **lower_files;
928     + struct file *lower_file;
929     + struct super_block *sb = file->f_path.dentry->d_sb;
930     +
931     + lower_files = UNIONFS_F(file)->lower_files;
932     + bstart = fbstart(file);
933     + bend = fbend(file);
934     +
935     + for (bindex = bstart; bindex <= bend; bindex++) {
936     + int i; /* holds (possibly) updated branch index */
937     + int old_bid;
938     +
939     + lower_file = unionfs_lower_file_idx(file, bindex);
940     + if (!lower_file)
941     + continue;
942     +
943     + /*
944     + * Find new index of matching branch with an open
945     + * file, since branches could have been added or
946     + * deleted causing the one with open files to shift.
947     + */
948     + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
949     + i = branch_id_to_idx(sb, old_bid);
950     + if (unlikely(i < 0)) {
951     + printk(KERN_ERR "unionfs: no superblock for "
952     + "file %p\n", file);
953     + continue;
954     + }
955     +
956     + /* decrement count of open files */
957     + branchput(sb, i);
958     + /*
959     + * fput will perform an mntput for us on the correct branch.
960     + * Although we're using the file's old branch configuration,
961     + * bindex, which is the old index, correctly points to the
962     + * right branch in the file's branch list. In other words,
963     + * we're going to mntput the correct branch even if branches
964     + * have been added/removed.
965     + */
966     + fput(lower_file);
967     + UNIONFS_F(file)->lower_files[bindex] = NULL;
968     + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
969     + }
970     +
971     + UNIONFS_F(file)->lower_files = NULL;
972     + kfree(lower_files);
973     + kfree(UNIONFS_F(file)->saved_branch_ids);
974     + /* set to NULL because caller needs to know if to kfree on error */
975     + UNIONFS_F(file)->saved_branch_ids = NULL;
976     +}
977     +
978     +/* open all lower files for a given file */
979     +static int open_all_files(struct file *file)
980     +{
981     + int bindex, bstart, bend, err = 0;
982     + struct file *lower_file;
983     + struct dentry *lower_dentry;
984     + struct dentry *dentry = file->f_path.dentry;
985     + struct super_block *sb = dentry->d_sb;
986     +
987     + bstart = dbstart(dentry);
988     + bend = dbend(dentry);
989     +
990     + for (bindex = bstart; bindex <= bend; bindex++) {
991     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
992     + if (!lower_dentry)
993     + continue;
994     +
995     + dget(lower_dentry);
996     + unionfs_mntget(dentry, bindex);
997     + branchget(sb, bindex);
998     +
999     + lower_file =
1000     + dentry_open(lower_dentry,
1001     + unionfs_lower_mnt_idx(dentry, bindex),
1002     + file->f_flags, current_cred());
1003     + if (IS_ERR(lower_file)) {
1004     + branchput(sb, bindex);
1005     + err = PTR_ERR(lower_file);
1006     + goto out;
1007     + } else {
1008     + unionfs_set_lower_file_idx(file, bindex, lower_file);
1009     + }
1010     + }
1011     +out:
1012     + return err;
1013     +}
1014     +
1015     +/* open the highest priority file for a given upper file */
1016     +static int open_highest_file(struct file *file, bool willwrite)
1017     +{
1018     + int bindex, bstart, bend, err = 0;
1019     + struct file *lower_file;
1020     + struct dentry *lower_dentry;
1021     + struct dentry *dentry = file->f_path.dentry;
1022     + struct dentry *parent = dget_parent(dentry);
1023     + struct inode *parent_inode = parent->d_inode;
1024     + struct super_block *sb = dentry->d_sb;
1025     +
1026     + bstart = dbstart(dentry);
1027     + bend = dbend(dentry);
1028     +
1029     + lower_dentry = unionfs_lower_dentry(dentry);
1030     + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
1031     + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1032     + err = copyup_file(parent_inode, file, bstart, bindex,
1033     + i_size_read(dentry->d_inode));
1034     + if (!err)
1035     + break;
1036     + }
1037     + atomic_set(&UNIONFS_F(file)->generation,
1038     + atomic_read(&UNIONFS_I(dentry->d_inode)->
1039     + generation));
1040     + goto out;
1041     + }
1042     +
1043     + dget(lower_dentry);
1044     + unionfs_mntget(dentry, bstart);
1045     + lower_file = dentry_open(lower_dentry,
1046     + unionfs_lower_mnt_idx(dentry, bstart),
1047     + file->f_flags, current_cred());
1048     + if (IS_ERR(lower_file)) {
1049     + err = PTR_ERR(lower_file);
1050     + goto out;
1051     + }
1052     + branchget(sb, bstart);
1053     + unionfs_set_lower_file(file, lower_file);
1054     + /* Fix up the position. */
1055     + lower_file->f_pos = file->f_pos;
1056     +
1057     + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1058     +out:
1059     + dput(parent);
1060     + return err;
1061     +}
1062     +
1063     +/* perform a delayed copyup of a read-write file on a read-only branch */
1064     +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1065     +{
1066     + int bindex, bstart, bend, err = 0;
1067     + struct dentry *dentry = file->f_path.dentry;
1068     + struct inode *parent_inode = parent->d_inode;
1069     +
1070     + bstart = fbstart(file);
1071     + bend = fbend(file);
1072     +
1073     + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1074     +
1075     + unionfs_check_file(file);
1076     + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1077     + if (!d_deleted(dentry))
1078     + err = copyup_file(parent_inode, file, bstart,
1079     + bindex,
1080     + i_size_read(dentry->d_inode));
1081     + else
1082     + err = copyup_deleted_file(file, dentry, parent,
1083     + bstart, bindex);
1084     + /* if succeeded, set lower open-file flags and break */
1085     + if (!err) {
1086     + struct file *lower_file;
1087     + lower_file = unionfs_lower_file_idx(file, bindex);
1088     + lower_file->f_flags = file->f_flags;
1089     + break;
1090     + }
1091     + }
1092     + if (err || (bstart <= fbstart(file)))
1093     + goto out;
1094     + bend = fbend(file);
1095     + for (bindex = bstart; bindex <= bend; bindex++) {
1096     + if (unionfs_lower_file_idx(file, bindex)) {
1097     + branchput(dentry->d_sb, bindex);
1098     + fput(unionfs_lower_file_idx(file, bindex));
1099     + unionfs_set_lower_file_idx(file, bindex, NULL);
1100     + }
1101     + }
1102     + path_put_lowers(dentry, bstart, bend, false);
1103     + iput_lowers(dentry->d_inode, bstart, bend, false);
1104     + /* for reg file, we only open it "once" */
1105     + fbend(file) = fbstart(file);
1106     + dbend(dentry) = dbstart(dentry);
1107     + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1108     +
1109     +out:
1110     + unionfs_check_file(file);
1111     + return err;
1112     +}
1113     +
1114     +/*
1115     + * Helper function for unionfs_file_revalidate/locked.
1116     + * Expects dentry/parent to be locked already, and revalidated.
1117     + */
1118     +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1119     + struct dentry *parent,
1120     + struct super_block *sb, int sbgen,
1121     + int dgen, bool willwrite)
1122     +{
1123     + int fgen;
1124     + int bstart, bend, orig_brid;
1125     + int size;
1126     + int err = 0;
1127     +
1128     + fgen = atomic_read(&UNIONFS_F(file)->generation);
1129     +
1130     + /*
1131     + * There are two cases we are interested in. The first is if the
1132     + * generation is lower than the super-block. The second is if
1133     + * someone has copied up this file from underneath us, we also need
1134     + * to refresh things.
1135     + */
1136     + if (d_deleted(dentry) ||
1137     + (sbgen <= fgen &&
1138     + dbstart(dentry) == fbstart(file) &&
1139     + unionfs_lower_file(file)))
1140     + goto out_may_copyup;
1141     +
1142     + /* save orig branch ID */
1143     + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1144     +
1145     + /* First we throw out the existing files. */
1146     + cleanup_file(file);
1147     +
1148     + /* Now we reopen the file(s) as in unionfs_open. */
1149     + bstart = fbstart(file) = dbstart(dentry);
1150     + bend = fbend(file) = dbend(dentry);
1151     +
1152     + size = sizeof(struct file *) * sbmax(sb);
1153     + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1154     + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1155     + err = -ENOMEM;
1156     + goto out;
1157     + }
1158     + size = sizeof(int) * sbmax(sb);
1159     + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1160     + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1161     + err = -ENOMEM;
1162     + goto out;
1163     + }
1164     +
1165     + if (S_ISDIR(dentry->d_inode->i_mode)) {
1166     + /* We need to open all the files. */
1167     + err = open_all_files(file);
1168     + if (err)
1169     + goto out;
1170     + } else {
1171     + int new_brid;
1172     + /* We only open the highest priority branch. */
1173     + err = open_highest_file(file, willwrite);
1174     + if (err)
1175     + goto out;
1176     + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1177     + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1178     + /*
1179     + * If we re-opened the file on a different branch
1180     + * than the original one, and this was due to a new
1181     + * branch inserted, then update the mnt counts of
1182     + * the old and new branches accordingly.
1183     + */
1184     + unionfs_mntget(dentry, bstart);
1185     + unionfs_mntput(sb->s_root,
1186     + branch_id_to_idx(sb, orig_brid));
1187     + }
1188     + /* regular files have only one open lower file */
1189     + fbend(file) = fbstart(file);
1190     + }
1191     + atomic_set(&UNIONFS_F(file)->generation,
1192     + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1193     +
1194     +out_may_copyup:
1195     + /* Copyup on the first write to a file on a readonly branch. */
1196     + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1197     + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1198     + is_robranch(dentry)) {
1199     + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1200     + dentry->d_name.name);
1201     + err = do_delayed_copyup(file, parent);
1202     + /* regular files have only one open lower file */
1203     + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1204     + fbend(file) = fbstart(file);
1205     + }
1206     +
1207     +out:
1208     + if (err) {
1209     + kfree(UNIONFS_F(file)->lower_files);
1210     + kfree(UNIONFS_F(file)->saved_branch_ids);
1211     + }
1212     + return err;
1213     +}
1214     +
1215     +/*
1216     + * Revalidate the struct file
1217     + * @file: file to revalidate
1218     + * @parent: parent dentry (locked by caller)
1219     + * @willwrite: true if caller may cause changes to the file; false otherwise.
1220     + * Caller must lock/unlock dentry's branch configuration.
1221     + */
1222     +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1223     + bool willwrite)
1224     +{
1225     + struct super_block *sb;
1226     + struct dentry *dentry;
1227     + int sbgen, dgen;
1228     + int err = 0;
1229     +
1230     + dentry = file->f_path.dentry;
1231     + sb = dentry->d_sb;
1232     + verify_locked(dentry);
1233     + verify_locked(parent);
1234     +
1235     + /*
1236     + * First revalidate the dentry inside struct file,
1237     + * but not unhashed dentries.
1238     + */
1239     + if (!d_deleted(dentry) &&
1240     + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1241     + err = -ESTALE;
1242     + goto out;
1243     + }
1244     +
1245     + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1246     + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1247     +
1248     + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1249     + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1250     + dentry->d_name.name);
1251     + err = -ESTALE;
1252     + goto out;
1253     + }
1254     +
1255     + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1256     + sbgen, dgen, willwrite);
1257     +out:
1258     + return err;
1259     +}
1260     +
1261     +/* unionfs_open helper function: open a directory */
1262     +static int __open_dir(struct inode *inode, struct file *file)
1263     +{
1264     + struct dentry *lower_dentry;
1265     + struct file *lower_file;
1266     + int bindex, bstart, bend;
1267     + struct vfsmount *mnt;
1268     +
1269     + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1270     + bend = fbend(file) = dbend(file->f_path.dentry);
1271     +
1272     + for (bindex = bstart; bindex <= bend; bindex++) {
1273     + lower_dentry =
1274     + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1275     + if (!lower_dentry)
1276     + continue;
1277     +
1278     + dget(lower_dentry);
1279     + unionfs_mntget(file->f_path.dentry, bindex);
1280     + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1281     + lower_file = dentry_open(lower_dentry, mnt, file->f_flags,
1282     + current_cred());
1283     + if (IS_ERR(lower_file))
1284     + return PTR_ERR(lower_file);
1285     +
1286     + unionfs_set_lower_file_idx(file, bindex, lower_file);
1287     +
1288     + /*
1289     + * The branchget goes after the open, because otherwise
1290     + * we would miss the reference on release.
1291     + */
1292     + branchget(inode->i_sb, bindex);
1293     + }
1294     +
1295     + return 0;
1296     +}
1297     +
1298     +/* unionfs_open helper function: open a file */
1299     +static int __open_file(struct inode *inode, struct file *file,
1300     + struct dentry *parent)
1301     +{
1302     + struct dentry *lower_dentry;
1303     + struct file *lower_file;
1304     + int lower_flags;
1305     + int bindex, bstart, bend;
1306     +
1307     + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1308     + lower_flags = file->f_flags;
1309     +
1310     + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1311     + bend = fbend(file) = dbend(file->f_path.dentry);
1312     +
1313     + /*
1314     + * check for the permission for lower file. If the error is
1315     + * COPYUP_ERR, copyup the file.
1316     + */
1317     + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1318     + /*
1319     + * if the open will change the file, copy it up otherwise
1320     + * defer it.
1321     + */
1322     + if (lower_flags & O_TRUNC) {
1323     + int size = 0;
1324     + int err = -EROFS;
1325     +
1326     + /* copyup the file */
1327     + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1328     + err = copyup_file(parent->d_inode, file,
1329     + bstart, bindex, size);
1330     + if (!err)
1331     + break;
1332     + }
1333     + return err;
1334     + } else {
1335     + /*
1336     + * turn off writeable flags, to force delayed copyup
1337     + * by caller.
1338     + */
1339     + lower_flags &= ~(OPEN_WRITE_FLAGS);
1340     + }
1341     + }
1342     +
1343     + dget(lower_dentry);
1344     +
1345     + /*
1346     + * dentry_open will decrement mnt refcnt if err.
1347     + * otherwise fput() will do an mntput() for us upon file close.
1348     + */
1349     + unionfs_mntget(file->f_path.dentry, bstart);
1350     + lower_file =
1351     + dentry_open(lower_dentry,
1352     + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1353     + lower_flags, current_cred());
1354     + if (IS_ERR(lower_file))
1355     + return PTR_ERR(lower_file);
1356     +
1357     + unionfs_set_lower_file(file, lower_file);
1358     + branchget(inode->i_sb, bstart);
1359     +
1360     + return 0;
1361     +}
1362     +
1363     +int unionfs_open(struct inode *inode, struct file *file)
1364     +{
1365     + int err = 0;
1366     + struct file *lower_file = NULL;
1367     + struct dentry *dentry = file->f_path.dentry;
1368     + struct dentry *parent;
1369     + int bindex = 0, bstart = 0, bend = 0;
1370     + int size;
1371     + int valid = 0;
1372     +
1373     + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1374     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1375     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1376     +
1377     + /* don't open unhashed/deleted files */
1378     + if (d_deleted(dentry)) {
1379     + err = -ENOENT;
1380     + goto out_nofree;
1381     + }
1382     +
1383     + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1384     + valid = __unionfs_d_revalidate(dentry, parent, false);
1385     + if (unlikely(!valid)) {
1386     + err = -ESTALE;
1387     + goto out_nofree;
1388     + }
1389     +
1390     + file->private_data =
1391     + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1392     + if (unlikely(!UNIONFS_F(file))) {
1393     + err = -ENOMEM;
1394     + goto out_nofree;
1395     + }
1396     + fbstart(file) = -1;
1397     + fbend(file) = -1;
1398     + atomic_set(&UNIONFS_F(file)->generation,
1399     + atomic_read(&UNIONFS_I(inode)->generation));
1400     +
1401     + size = sizeof(struct file *) * sbmax(inode->i_sb);
1402     + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1403     + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1404     + err = -ENOMEM;
1405     + goto out;
1406     + }
1407     + size = sizeof(int) * sbmax(inode->i_sb);
1408     + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1409     + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1410     + err = -ENOMEM;
1411     + goto out;
1412     + }
1413     +
1414     + bstart = fbstart(file) = dbstart(dentry);
1415     + bend = fbend(file) = dbend(dentry);
1416     +
1417     + /*
1418     + * open all directories and make the unionfs file struct point to
1419     + * these lower file structs
1420     + */
1421     + if (S_ISDIR(inode->i_mode))
1422     + err = __open_dir(inode, file); /* open a dir */
1423     + else
1424     + err = __open_file(inode, file, parent); /* open a file */
1425     +
1426     + /* freeing the allocated resources, and fput the opened files */
1427     + if (err) {
1428     + for (bindex = bstart; bindex <= bend; bindex++) {
1429     + lower_file = unionfs_lower_file_idx(file, bindex);
1430     + if (!lower_file)
1431     + continue;
1432     +
1433     + branchput(dentry->d_sb, bindex);
1434     + /* fput calls dput for lower_dentry */
1435     + fput(lower_file);
1436     + }
1437     + }
1438     +
1439     +out:
1440     + if (err) {
1441     + kfree(UNIONFS_F(file)->lower_files);
1442     + kfree(UNIONFS_F(file)->saved_branch_ids);
1443     + kfree(UNIONFS_F(file));
1444     + }
1445     +out_nofree:
1446     + if (!err) {
1447     + unionfs_postcopyup_setmnt(dentry);
1448     + unionfs_copy_attr_times(inode);
1449     + unionfs_check_file(file);
1450     + unionfs_check_inode(inode);
1451     + }
1452     + unionfs_unlock_dentry(dentry);
1453     + unionfs_unlock_parent(dentry, parent);
1454     + unionfs_read_unlock(inode->i_sb);
1455     + return err;
1456     +}
1457     +
1458     +/*
1459     + * release all lower object references & free the file info structure
1460     + *
1461     + * No need to grab sb info's rwsem.
1462     + */
1463     +int unionfs_file_release(struct inode *inode, struct file *file)
1464     +{
1465     + struct file *lower_file = NULL;
1466     + struct unionfs_file_info *fileinfo;
1467     + struct unionfs_inode_info *inodeinfo;
1468     + struct super_block *sb = inode->i_sb;
1469     + struct dentry *dentry = file->f_path.dentry;
1470     + struct dentry *parent;
1471     + int bindex, bstart, bend;
1472     + int fgen, err = 0;
1473     +
1474     + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1475     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1476     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1477     +
1478     + /*
1479     + * We try to revalidate, but the VFS ignores return return values
1480     + * from file->release, so we must always try to succeed here,
1481     + * including to do the kfree and dput below. So if revalidation
1482     + * failed, all we can do is print some message and keep going.
1483     + */
1484     + err = unionfs_file_revalidate(file, parent,
1485     + UNIONFS_F(file)->wrote_to_file);
1486     + if (!err)
1487     + unionfs_check_file(file);
1488     + fileinfo = UNIONFS_F(file);
1489     + BUG_ON(file->f_path.dentry->d_inode != inode);
1490     + inodeinfo = UNIONFS_I(inode);
1491     +
1492     + /* fput all the lower files */
1493     + fgen = atomic_read(&fileinfo->generation);
1494     + bstart = fbstart(file);
1495     + bend = fbend(file);
1496     +
1497     + for (bindex = bstart; bindex <= bend; bindex++) {
1498     + lower_file = unionfs_lower_file_idx(file, bindex);
1499     +
1500     + if (lower_file) {
1501     + unionfs_set_lower_file_idx(file, bindex, NULL);
1502     + fput(lower_file);
1503     + branchput(sb, bindex);
1504     + }
1505     +
1506     + /* if there are no more refs to the dentry, dput it */
1507     + if (d_deleted(dentry)) {
1508     + dput(unionfs_lower_dentry_idx(dentry, bindex));
1509     + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1510     + }
1511     + }
1512     +
1513     + kfree(fileinfo->lower_files);
1514     + kfree(fileinfo->saved_branch_ids);
1515     +
1516     + if (fileinfo->rdstate) {
1517     + fileinfo->rdstate->access = jiffies;
1518     + spin_lock(&inodeinfo->rdlock);
1519     + inodeinfo->rdcount++;
1520     + list_add_tail(&fileinfo->rdstate->cache,
1521     + &inodeinfo->readdircache);
1522     + mark_inode_dirty(inode);
1523     + spin_unlock(&inodeinfo->rdlock);
1524     + fileinfo->rdstate = NULL;
1525     + }
1526     + kfree(fileinfo);
1527     +
1528     + unionfs_unlock_dentry(dentry);
1529     + unionfs_unlock_parent(dentry, parent);
1530     + unionfs_read_unlock(sb);
1531     + return err;
1532     +}
1533     +
1534     +/* pass the ioctl to the lower fs */
1535     +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1536     +{
1537     + struct file *lower_file;
1538     + int err;
1539     +
1540     + lower_file = unionfs_lower_file(file);
1541     +
1542     + err = -ENOTTY;
1543     + if (!lower_file || !lower_file->f_op)
1544     + goto out;
1545     + if (lower_file->f_op->unlocked_ioctl) {
1546     + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1547     + } else if (lower_file->f_op->ioctl) {
1548     + lock_kernel();
1549     + err = lower_file->f_op->ioctl(
1550     + lower_file->f_path.dentry->d_inode,
1551     + lower_file, cmd, arg);
1552     + unlock_kernel();
1553     + }
1554     +
1555     +out:
1556     + return err;
1557     +}
1558     +
1559     +/*
1560     + * return to user-space the branch indices containing the file in question
1561     + *
1562     + * We use fd_set and therefore we are limited to the number of the branches
1563     + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1564     + */
1565     +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1566     + unsigned int cmd, unsigned long arg)
1567     +{
1568     + int err = 0;
1569     + fd_set branchlist;
1570     + int bstart = 0, bend = 0, bindex = 0;
1571     + int orig_bstart, orig_bend;
1572     + struct dentry *dentry, *lower_dentry;
1573     + struct vfsmount *mnt;
1574     +
1575     + dentry = file->f_path.dentry;
1576     + orig_bstart = dbstart(dentry);
1577     + orig_bend = dbend(dentry);
1578     + err = unionfs_partial_lookup(dentry, parent);
1579     + if (err)
1580     + goto out;
1581     + bstart = dbstart(dentry);
1582     + bend = dbend(dentry);
1583     +
1584     + FD_ZERO(&branchlist);
1585     +
1586     + for (bindex = bstart; bindex <= bend; bindex++) {
1587     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1588     + if (!lower_dentry)
1589     + continue;
1590     + if (likely(lower_dentry->d_inode))
1591     + FD_SET(bindex, &branchlist);
1592     + /* purge any lower objects after partial_lookup */
1593     + if (bindex < orig_bstart || bindex > orig_bend) {
1594     + dput(lower_dentry);
1595     + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1596     + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1597     + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1598     + NULL);
1599     + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1600     + if (!mnt)
1601     + continue;
1602     + unionfs_mntput(dentry, bindex);
1603     + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1604     + }
1605     + }
1606     + /* restore original dentry's offsets */
1607     + dbstart(dentry) = orig_bstart;
1608     + dbend(dentry) = orig_bend;
1609     + ibstart(dentry->d_inode) = orig_bstart;
1610     + ibend(dentry->d_inode) = orig_bend;
1611     +
1612     + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1613     + if (unlikely(err))
1614     + err = -EFAULT;
1615     +
1616     +out:
1617     + return err < 0 ? err : bend;
1618     +}
1619     +
1620     +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1621     +{
1622     + long err;
1623     + struct dentry *dentry = file->f_path.dentry;
1624     + struct dentry *parent;
1625     +
1626     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1627     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1628     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1629     +
1630     + err = unionfs_file_revalidate(file, parent, true);
1631     + if (unlikely(err))
1632     + goto out;
1633     +
1634     + /* check if asked for local commands */
1635     + switch (cmd) {
1636     + case UNIONFS_IOCTL_INCGEN:
1637     + /* Increment the superblock generation count */
1638     + pr_info("unionfs: incgen ioctl deprecated; "
1639     + "use \"-o remount,incgen\"\n");
1640     + err = -ENOSYS;
1641     + break;
1642     +
1643     + case UNIONFS_IOCTL_QUERYFILE:
1644     + /* Return list of branches containing the given file */
1645     + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1646     + break;
1647     +
1648     + default:
1649     + /* pass the ioctl down */
1650     + err = do_ioctl(file, cmd, arg);
1651     + break;
1652     + }
1653     +
1654     +out:
1655     + unionfs_check_file(file);
1656     + unionfs_unlock_dentry(dentry);
1657     + unionfs_unlock_parent(dentry, parent);
1658     + unionfs_read_unlock(dentry->d_sb);
1659     + return err;
1660     +}
1661     +
1662     +int unionfs_flush(struct file *file, fl_owner_t id)
1663     +{
1664     + int err = 0;
1665     + struct file *lower_file = NULL;
1666     + struct dentry *dentry = file->f_path.dentry;
1667     + struct dentry *parent;
1668     + int bindex, bstart, bend;
1669     +
1670     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1671     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1672     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1673     +
1674     + err = unionfs_file_revalidate(file, parent,
1675     + UNIONFS_F(file)->wrote_to_file);
1676     + if (unlikely(err))
1677     + goto out;
1678     + unionfs_check_file(file);
1679     +
1680     + bstart = fbstart(file);
1681     + bend = fbend(file);
1682     + for (bindex = bstart; bindex <= bend; bindex++) {
1683     + lower_file = unionfs_lower_file_idx(file, bindex);
1684     +
1685     + if (lower_file && lower_file->f_op &&
1686     + lower_file->f_op->flush) {
1687     + err = lower_file->f_op->flush(lower_file, id);
1688     + if (err)
1689     + goto out;
1690     + }
1691     +
1692     + }
1693     +
1694     +out:
1695     + if (!err)
1696     + unionfs_check_file(file);
1697     + unionfs_unlock_dentry(dentry);
1698     + unionfs_unlock_parent(dentry, parent);
1699     + unionfs_read_unlock(dentry->d_sb);
1700     + return err;
1701     +}
1702     diff -Naur linux-2.6.29/fs/unionfs/copyup.c linux-2.6.29-magellan/fs/unionfs/copyup.c
1703     --- linux-2.6.29/fs/unionfs/copyup.c 1970-01-01 01:00:00.000000000 +0100
1704     +++ linux-2.6.29-magellan/fs/unionfs/copyup.c 2009-04-23 19:41:06.000000000 +0200
1705     @@ -0,0 +1,897 @@
1706     +/*
1707     + * Copyright (c) 2003-2009 Erez Zadok
1708     + * Copyright (c) 2003-2006 Charles P. Wright
1709     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1710     + * Copyright (c) 2005-2006 Junjiro Okajima
1711     + * Copyright (c) 2005 Arun M. Krishnakumar
1712     + * Copyright (c) 2004-2006 David P. Quigley
1713     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1714     + * Copyright (c) 2003 Puja Gupta
1715     + * Copyright (c) 2003 Harikesavan Krishnan
1716     + * Copyright (c) 2003-2009 Stony Brook University
1717     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
1718     + *
1719     + * This program is free software; you can redistribute it and/or modify
1720     + * it under the terms of the GNU General Public License version 2 as
1721     + * published by the Free Software Foundation.
1722     + */
1723     +
1724     +#include "union.h"
1725     +
1726     +/*
1727     + * For detailed explanation of copyup see:
1728     + * Documentation/filesystems/unionfs/concepts.txt
1729     + */
1730     +
1731     +#ifdef CONFIG_UNION_FS_XATTR
1732     +/* copyup all extended attrs for a given dentry */
1733     +static int copyup_xattrs(struct dentry *old_lower_dentry,
1734     + struct dentry *new_lower_dentry)
1735     +{
1736     + int err = 0;
1737     + ssize_t list_size = -1;
1738     + char *name_list = NULL;
1739     + char *attr_value = NULL;
1740     + char *name_list_buf = NULL;
1741     +
1742     + /* query the actual size of the xattr list */
1743     + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1744     + if (list_size <= 0) {
1745     + err = list_size;
1746     + goto out;
1747     + }
1748     +
1749     + /* allocate space for the actual list */
1750     + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1751     + if (unlikely(!name_list || IS_ERR(name_list))) {
1752     + err = PTR_ERR(name_list);
1753     + goto out;
1754     + }
1755     +
1756     + name_list_buf = name_list; /* save for kfree at end */
1757     +
1758     + /* now get the actual xattr list of the source file */
1759     + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1760     + if (list_size <= 0) {
1761     + err = list_size;
1762     + goto out;
1763     + }
1764     +
1765     + /* allocate space to hold each xattr's value */
1766     + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1767     + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1768     + err = PTR_ERR(name_list);
1769     + goto out;
1770     + }
1771     +
1772     + /* in a loop, get and set each xattr from src to dst file */
1773     + while (*name_list) {
1774     + ssize_t size;
1775     +
1776     + /* Lock here since vfs_getxattr doesn't lock for us */
1777     + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1778     + size = vfs_getxattr(old_lower_dentry, name_list,
1779     + attr_value, XATTR_SIZE_MAX);
1780     + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1781     + if (size < 0) {
1782     + err = size;
1783     + goto out;
1784     + }
1785     + if (size > XATTR_SIZE_MAX) {
1786     + err = -E2BIG;
1787     + goto out;
1788     + }
1789     + /* Don't lock here since vfs_setxattr does it for us. */
1790     + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1791     + size, 0);
1792     + /*
1793     + * Selinux depends on "security.*" xattrs, so to maintain
1794     + * the security of copied-up files, if Selinux is active,
1795     + * then we must copy these xattrs as well. So we need to
1796     + * temporarily get FOWNER privileges.
1797     + * XXX: move entire copyup code to SIOQ.
1798     + */
1799     + if (err == -EPERM && !capable(CAP_FOWNER)) {
1800     + const struct cred *old_creds;
1801     + struct cred *new_creds;
1802     +
1803     + new_creds = prepare_creds();
1804     + if (unlikely(!new_creds)) {
1805     + err = -ENOMEM;
1806     + goto out;
1807     + }
1808     + cap_raise(new_creds->cap_effective, CAP_FOWNER);
1809     + old_creds = override_creds(new_creds);
1810     + err = vfs_setxattr(new_lower_dentry, name_list,
1811     + attr_value, size, 0);
1812     + revert_creds(old_creds);
1813     + }
1814     + if (err < 0)
1815     + goto out;
1816     + name_list += strlen(name_list) + 1;
1817     + }
1818     +out:
1819     + unionfs_xattr_kfree(name_list_buf);
1820     + unionfs_xattr_kfree(attr_value);
1821     + /* Ignore if xattr isn't supported */
1822     + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1823     + err = 0;
1824     + return err;
1825     +}
1826     +#endif /* CONFIG_UNION_FS_XATTR */
1827     +
1828     +/*
1829     + * Determine the mode based on the copyup flags, and the existing dentry.
1830     + *
1831     + * Handle file systems which may not support certain options. For example
1832     + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1833     + * errors, rather than propagating them up, which results in copyup errors
1834     + * and errors returned back to users.
1835     + */
1836     +static int copyup_permissions(struct super_block *sb,
1837     + struct dentry *old_lower_dentry,
1838     + struct dentry *new_lower_dentry)
1839     +{
1840     + struct inode *i = old_lower_dentry->d_inode;
1841     + struct iattr newattrs;
1842     + int err;
1843     +
1844     + newattrs.ia_atime = i->i_atime;
1845     + newattrs.ia_mtime = i->i_mtime;
1846     + newattrs.ia_ctime = i->i_ctime;
1847     + newattrs.ia_gid = i->i_gid;
1848     + newattrs.ia_uid = i->i_uid;
1849     + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1850     + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1851     + ATTR_GID | ATTR_UID;
1852     + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1853     + err = notify_change(new_lower_dentry, &newattrs);
1854     + if (err)
1855     + goto out;
1856     +
1857     + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1858     + newattrs.ia_mode = i->i_mode;
1859     + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1860     + err = notify_change(new_lower_dentry, &newattrs);
1861     + if (err == -EOPNOTSUPP &&
1862     + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1863     + printk(KERN_WARNING
1864     + "unionfs: changing \"%s\" symlink mode unsupported\n",
1865     + new_lower_dentry->d_name.name);
1866     + err = 0;
1867     + }
1868     +
1869     +out:
1870     + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1871     + return err;
1872     +}
1873     +
1874     +/*
1875     + * create the new device/file/directory - use copyup_permission to copyup
1876     + * times, and mode
1877     + *
1878     + * if the object being copied up is a regular file, the file is only created,
1879     + * the contents have to be copied up separately
1880     + */
1881     +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1882     + struct dentry *new_lower_dentry,
1883     + struct dentry *new_lower_parent_dentry,
1884     + char *symbuf)
1885     +{
1886     + int err = 0;
1887     + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1888     + struct sioq_args args;
1889     +
1890     + if (S_ISDIR(old_mode)) {
1891     + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1892     + args.mkdir.dentry = new_lower_dentry;
1893     + args.mkdir.mode = old_mode;
1894     +
1895     + run_sioq(__unionfs_mkdir, &args);
1896     + err = args.err;
1897     + } else if (S_ISLNK(old_mode)) {
1898     + args.symlink.parent = new_lower_parent_dentry->d_inode;
1899     + args.symlink.dentry = new_lower_dentry;
1900     + args.symlink.symbuf = symbuf;
1901     +
1902     + run_sioq(__unionfs_symlink, &args);
1903     + err = args.err;
1904     + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1905     + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1906     + args.mknod.parent = new_lower_parent_dentry->d_inode;
1907     + args.mknod.dentry = new_lower_dentry;
1908     + args.mknod.mode = old_mode;
1909     + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1910     +
1911     + run_sioq(__unionfs_mknod, &args);
1912     + err = args.err;
1913     + } else if (S_ISREG(old_mode)) {
1914     + struct nameidata nd;
1915     + err = init_lower_nd(&nd, LOOKUP_CREATE);
1916     + if (unlikely(err < 0))
1917     + goto out;
1918     + args.create.nd = &nd;
1919     + args.create.parent = new_lower_parent_dentry->d_inode;
1920     + args.create.dentry = new_lower_dentry;
1921     + args.create.mode = old_mode;
1922     +
1923     + run_sioq(__unionfs_create, &args);
1924     + err = args.err;
1925     + release_lower_nd(&nd, err);
1926     + } else {
1927     + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
1928     + old_mode);
1929     + BUG();
1930     + }
1931     +
1932     +out:
1933     + return err;
1934     +}
1935     +
1936     +static int __copyup_reg_data(struct dentry *dentry,
1937     + struct dentry *new_lower_dentry, int new_bindex,
1938     + struct dentry *old_lower_dentry, int old_bindex,
1939     + struct file **copyup_file, loff_t len)
1940     +{
1941     + struct super_block *sb = dentry->d_sb;
1942     + struct file *input_file;
1943     + struct file *output_file;
1944     + struct vfsmount *output_mnt;
1945     + mm_segment_t old_fs;
1946     + char *buf = NULL;
1947     + ssize_t read_bytes, write_bytes;
1948     + loff_t size;
1949     + int err = 0;
1950     +
1951     + /* open old file */
1952     + unionfs_mntget(dentry, old_bindex);
1953     + branchget(sb, old_bindex);
1954     + /* dentry_open calls dput and mntput if it returns an error */
1955     + input_file = dentry_open(old_lower_dentry,
1956     + unionfs_lower_mnt_idx(dentry, old_bindex),
1957     + O_RDONLY | O_LARGEFILE, current_cred());
1958     + if (IS_ERR(input_file)) {
1959     + dput(old_lower_dentry);
1960     + err = PTR_ERR(input_file);
1961     + goto out;
1962     + }
1963     + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
1964     + err = -EINVAL;
1965     + goto out_close_in;
1966     + }
1967     +
1968     + /* open new file */
1969     + dget(new_lower_dentry);
1970     + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
1971     + branchget(sb, new_bindex);
1972     + output_file = dentry_open(new_lower_dentry, output_mnt,
1973     + O_RDWR | O_LARGEFILE, current_cred());
1974     + if (IS_ERR(output_file)) {
1975     + err = PTR_ERR(output_file);
1976     + goto out_close_in2;
1977     + }
1978     + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
1979     + err = -EINVAL;
1980     + goto out_close_out;
1981     + }
1982     +
1983     + /* allocating a buffer */
1984     + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1985     + if (unlikely(!buf)) {
1986     + err = -ENOMEM;
1987     + goto out_close_out;
1988     + }
1989     +
1990     + input_file->f_pos = 0;
1991     + output_file->f_pos = 0;
1992     +
1993     + old_fs = get_fs();
1994     + set_fs(KERNEL_DS);
1995     +
1996     + size = len;
1997     + err = 0;
1998     + do {
1999     + if (len >= PAGE_SIZE)
2000     + size = PAGE_SIZE;
2001     + else if ((len < PAGE_SIZE) && (len > 0))
2002     + size = len;
2003     +
2004     + len -= PAGE_SIZE;
2005     +
2006     + read_bytes =
2007     + input_file->f_op->read(input_file,
2008     + (char __user *)buf, size,
2009     + &input_file->f_pos);
2010     + if (read_bytes <= 0) {
2011     + err = read_bytes;
2012     + break;
2013     + }
2014     +
2015     + /* see Documentation/filesystems/unionfs/issues.txt */
2016     + lockdep_off();
2017     + write_bytes =
2018     + output_file->f_op->write(output_file,
2019     + (char __user *)buf,
2020     + read_bytes,
2021     + &output_file->f_pos);
2022     + lockdep_on();
2023     + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2024     + err = write_bytes;
2025     + break;
2026     + }
2027     + } while ((read_bytes > 0) && (len > 0));
2028     +
2029     + set_fs(old_fs);
2030     +
2031     + kfree(buf);
2032     +
2033     + if (!err)
2034     + err = output_file->f_op->fsync(output_file,
2035     + new_lower_dentry, 0);
2036     +
2037     + if (err)
2038     + goto out_close_out;
2039     +
2040     + if (copyup_file) {
2041     + *copyup_file = output_file;
2042     + goto out_close_in;
2043     + }
2044     +
2045     +out_close_out:
2046     + fput(output_file);
2047     +
2048     +out_close_in2:
2049     + branchput(sb, new_bindex);
2050     +
2051     +out_close_in:
2052     + fput(input_file);
2053     +
2054     +out:
2055     + branchput(sb, old_bindex);
2056     +
2057     + return err;
2058     +}
2059     +
2060     +/*
2061     + * dput the lower references for old and new dentry & clear a lower dentry
2062     + * pointer
2063     + */
2064     +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2065     + int old_bstart, int old_bend,
2066     + struct dentry *new_lower_dentry, int new_bindex)
2067     +{
2068     + /* get rid of the lower dentry and all its traces */
2069     + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2070     + dbstart(dentry) = old_bstart;
2071     + dbend(dentry) = old_bend;
2072     +
2073     + dput(new_lower_dentry);
2074     + dput(old_lower_dentry);
2075     +}
2076     +
2077     +/*
2078     + * Copy up a dentry to a file of specified name.
2079     + *
2080     + * @dir: used to pull the ->i_sb to access other branches
2081     + * @dentry: the non-negative dentry whose lower_inode we should copy
2082     + * @bstart: the branch of the lower_inode to copy from
2083     + * @new_bindex: the branch to create the new file in
2084     + * @name: the name of the file to create
2085     + * @namelen: length of @name
2086     + * @copyup_file: the "struct file" to return (optional)
2087     + * @len: how many bytes to copy-up?
2088     + */
2089     +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2090     + int new_bindex, const char *name, int namelen,
2091     + struct file **copyup_file, loff_t len)
2092     +{
2093     + struct dentry *new_lower_dentry;
2094     + struct dentry *old_lower_dentry = NULL;
2095     + struct super_block *sb;
2096     + int err = 0;
2097     + int old_bindex;
2098     + int old_bstart;
2099     + int old_bend;
2100     + struct dentry *new_lower_parent_dentry = NULL;
2101     + mm_segment_t oldfs;
2102     + char *symbuf = NULL;
2103     +
2104     + verify_locked(dentry);
2105     +
2106     + old_bindex = bstart;
2107     + old_bstart = dbstart(dentry);
2108     + old_bend = dbend(dentry);
2109     +
2110     + BUG_ON(new_bindex < 0);
2111     + BUG_ON(new_bindex >= old_bindex);
2112     +
2113     + sb = dir->i_sb;
2114     +
2115     + err = is_robranch_super(sb, new_bindex);
2116     + if (err)
2117     + goto out;
2118     +
2119     + /* Create the directory structure above this dentry. */
2120     + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2121     + if (IS_ERR(new_lower_dentry)) {
2122     + err = PTR_ERR(new_lower_dentry);
2123     + goto out;
2124     + }
2125     +
2126     + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2127     + /* we conditionally dput this old_lower_dentry at end of function */
2128     + dget(old_lower_dentry);
2129     +
2130     + /* For symlinks, we must read the link before we lock the directory. */
2131     + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2132     +
2133     + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2134     + if (unlikely(!symbuf)) {
2135     + __clear(dentry, old_lower_dentry,
2136     + old_bstart, old_bend,
2137     + new_lower_dentry, new_bindex);
2138     + err = -ENOMEM;
2139     + goto out_free;
2140     + }
2141     +
2142     + oldfs = get_fs();
2143     + set_fs(KERNEL_DS);
2144     + err = old_lower_dentry->d_inode->i_op->readlink(
2145     + old_lower_dentry,
2146     + (char __user *)symbuf,
2147     + PATH_MAX);
2148     + set_fs(oldfs);
2149     + if (err < 0) {
2150     + __clear(dentry, old_lower_dentry,
2151     + old_bstart, old_bend,
2152     + new_lower_dentry, new_bindex);
2153     + goto out_free;
2154     + }
2155     + symbuf[err] = '\0';
2156     + }
2157     +
2158     + /* Now we lock the parent, and create the object in the new branch. */
2159     + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2160     +
2161     + /* create the new inode */
2162     + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2163     + new_lower_parent_dentry, symbuf);
2164     +
2165     + if (err) {
2166     + __clear(dentry, old_lower_dentry,
2167     + old_bstart, old_bend,
2168     + new_lower_dentry, new_bindex);
2169     + goto out_unlock;
2170     + }
2171     +
2172     + /* We actually copyup the file here. */
2173     + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2174     + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2175     + old_lower_dentry, old_bindex,
2176     + copyup_file, len);
2177     + if (err)
2178     + goto out_unlink;
2179     +
2180     + /* Set permissions. */
2181     + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2182     + if (err)
2183     + goto out_unlink;
2184     +
2185     +#ifdef CONFIG_UNION_FS_XATTR
2186     + /* Selinux uses extended attributes for permissions. */
2187     + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2188     + if (err)
2189     + goto out_unlink;
2190     +#endif /* CONFIG_UNION_FS_XATTR */
2191     +
2192     + /* do not allow files getting deleted to be re-interposed */
2193     + if (!d_deleted(dentry))
2194     + unionfs_reinterpose(dentry);
2195     +
2196     + goto out_unlock;
2197     +
2198     +out_unlink:
2199     + /*
2200     + * copyup failed, because we possibly ran out of space or
2201     + * quota, or something else happened so let's unlink; we don't
2202     + * really care about the return value of vfs_unlink
2203     + */
2204     + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2205     +
2206     + if (copyup_file) {
2207     + /* need to close the file */
2208     +
2209     + fput(*copyup_file);
2210     + branchput(sb, new_bindex);
2211     + }
2212     +
2213     + /*
2214     + * TODO: should we reset the error to something like -EIO?
2215     + *
2216     + * If we don't reset, the user may get some nonsensical errors, but
2217     + * on the other hand, if we reset to EIO, we guarantee that the user
2218     + * will get a "confusing" error message.
2219     + */
2220     +
2221     +out_unlock:
2222     + unlock_dir(new_lower_parent_dentry);
2223     +
2224     +out_free:
2225     + /*
2226     + * If old_lower_dentry was not a file, then we need to dput it. If
2227     + * it was a file, then it was already dput indirectly by other
2228     + * functions we call above which operate on regular files.
2229     + */
2230     + if (old_lower_dentry && old_lower_dentry->d_inode &&
2231     + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2232     + dput(old_lower_dentry);
2233     + kfree(symbuf);
2234     +
2235     + if (err) {
2236     + /*
2237     + * if directory creation succeeded, but inode copyup failed,
2238     + * then purge new dentries.
2239     + */
2240     + if (dbstart(dentry) < old_bstart &&
2241     + ibstart(dentry->d_inode) > dbstart(dentry))
2242     + __clear(dentry, NULL, old_bstart, old_bend,
2243     + unionfs_lower_dentry(dentry), dbstart(dentry));
2244     + goto out;
2245     + }
2246     + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2247     + unionfs_postcopyup_release(dentry);
2248     + if (!unionfs_lower_inode(dentry->d_inode)) {
2249     + /*
2250     + * If we got here, then we copied up to an
2251     + * unlinked-open file, whose name is .unionfsXXXXX.
2252     + */
2253     + struct inode *inode = new_lower_dentry->d_inode;
2254     + atomic_inc(&inode->i_count);
2255     + unionfs_set_lower_inode_idx(dentry->d_inode,
2256     + ibstart(dentry->d_inode),
2257     + inode);
2258     + }
2259     + }
2260     + unionfs_postcopyup_setmnt(dentry);
2261     + /* sync inode times from copied-up inode to our inode */
2262     + unionfs_copy_attr_times(dentry->d_inode);
2263     + unionfs_check_inode(dir);
2264     + unionfs_check_dentry(dentry);
2265     +out:
2266     + return err;
2267     +}
2268     +
2269     +/*
2270     + * This function creates a copy of a file represented by 'file' which
2271     + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2272     + * will be named "name".
2273     + */
2274     +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2275     + int bstart, int new_bindex, loff_t len)
2276     +{
2277     + int err = 0;
2278     + struct file *output_file = NULL;
2279     +
2280     + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2281     + name, strlen(name), &output_file, len);
2282     + if (!err) {
2283     + fbstart(file) = new_bindex;
2284     + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2285     + }
2286     +
2287     + return err;
2288     +}
2289     +
2290     +/*
2291     + * This function creates a copy of a file represented by 'file' which
2292     + * currently resides in branch 'bstart' to branch 'new_bindex'.
2293     + */
2294     +int copyup_file(struct inode *dir, struct file *file, int bstart,
2295     + int new_bindex, loff_t len)
2296     +{
2297     + int err = 0;
2298     + struct file *output_file = NULL;
2299     + struct dentry *dentry = file->f_path.dentry;
2300     +
2301     + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2302     + dentry->d_name.name, dentry->d_name.len,
2303     + &output_file, len);
2304     + if (!err) {
2305     + fbstart(file) = new_bindex;
2306     + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2307     + }
2308     +
2309     + return err;
2310     +}
2311     +
2312     +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2313     +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2314     + int old_bstart, int old_bend)
2315     +{
2316     + int loop_start;
2317     + int loop_end;
2318     + int new_bstart = -1;
2319     + int new_bend = -1;
2320     + int i;
2321     +
2322     + loop_start = min(old_bstart, bindex);
2323     + loop_end = max(old_bend, bindex);
2324     +
2325     + /*
2326     + * This loop sets the bstart and bend for the new dentry by
2327     + * traversing from left to right. It also dputs all negative
2328     + * dentries except bindex
2329     + */
2330     + for (i = loop_start; i <= loop_end; i++) {
2331     + if (!unionfs_lower_dentry_idx(dentry, i))
2332     + continue;
2333     +
2334     + if (i == bindex) {
2335     + new_bend = i;
2336     + if (new_bstart < 0)
2337     + new_bstart = i;
2338     + continue;
2339     + }
2340     +
2341     + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2342     + dput(unionfs_lower_dentry_idx(dentry, i));
2343     + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2344     +
2345     + unionfs_mntput(dentry, i);
2346     + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2347     + } else {
2348     + if (new_bstart < 0)
2349     + new_bstart = i;
2350     + new_bend = i;
2351     + }
2352     + }
2353     +
2354     + if (new_bstart < 0)
2355     + new_bstart = bindex;
2356     + if (new_bend < 0)
2357     + new_bend = bindex;
2358     + dbstart(dentry) = new_bstart;
2359     + dbend(dentry) = new_bend;
2360     +
2361     +}
2362     +
2363     +/* set lower inode ptr and update bstart & bend if necessary */
2364     +static void __set_inode(struct dentry *upper, struct dentry *lower,
2365     + int bindex)
2366     +{
2367     + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2368     + igrab(lower->d_inode));
2369     + if (likely(ibstart(upper->d_inode) > bindex))
2370     + ibstart(upper->d_inode) = bindex;
2371     + if (likely(ibend(upper->d_inode) < bindex))
2372     + ibend(upper->d_inode) = bindex;
2373     +
2374     +}
2375     +
2376     +/* set lower dentry ptr and update bstart & bend if necessary */
2377     +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2378     + int bindex)
2379     +{
2380     + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2381     + if (likely(dbstart(upper) > bindex))
2382     + dbstart(upper) = bindex;
2383     + if (likely(dbend(upper) < bindex))
2384     + dbend(upper) = bindex;
2385     +}
2386     +
2387     +/*
2388     + * This function replicates the directory structure up-to given dentry
2389     + * in the bindex branch.
2390     + */
2391     +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2392     + const char *name, int bindex)
2393     +{
2394     + int err;
2395     + struct dentry *child_dentry;
2396     + struct dentry *parent_dentry;
2397     + struct dentry *lower_parent_dentry = NULL;
2398     + struct dentry *lower_dentry = NULL;
2399     + const char *childname;
2400     + unsigned int childnamelen;
2401     + int nr_dentry;
2402     + int count = 0;
2403     + int old_bstart;
2404     + int old_bend;
2405     + struct dentry **path = NULL;
2406     + struct super_block *sb;
2407     +
2408     + verify_locked(dentry);
2409     +
2410     + err = is_robranch_super(dir->i_sb, bindex);
2411     + if (err) {
2412     + lower_dentry = ERR_PTR(err);
2413     + goto out;
2414     + }
2415     +
2416     + old_bstart = dbstart(dentry);
2417     + old_bend = dbend(dentry);
2418     +
2419     + lower_dentry = ERR_PTR(-ENOMEM);
2420     +
2421     + /* There is no sense allocating any less than the minimum. */
2422     + nr_dentry = 1;
2423     + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2424     + if (unlikely(!path))
2425     + goto out;
2426     +
2427     + /* assume the negative dentry of unionfs as the parent dentry */
2428     + parent_dentry = dentry;
2429     +
2430     + /*
2431     + * This loop finds the first parent that exists in the given branch.
2432     + * We start building the directory structure from there. At the end
2433     + * of the loop, the following should hold:
2434     + * - child_dentry is the first nonexistent child
2435     + * - parent_dentry is the first existent parent
2436     + * - path[0] is the = deepest child
2437     + * - path[count] is the first child to create
2438     + */
2439     + do {
2440     + child_dentry = parent_dentry;
2441     +
2442     + /* find the parent directory dentry in unionfs */
2443     + parent_dentry = dget_parent(child_dentry);
2444     +
2445     + /* find out the lower_parent_dentry in the given branch */
2446     + lower_parent_dentry =
2447     + unionfs_lower_dentry_idx(parent_dentry, bindex);
2448     +
2449     + /* grow path table */
2450     + if (count == nr_dentry) {
2451     + void *p;
2452     +
2453     + nr_dentry *= 2;
2454     + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2455     + GFP_KERNEL);
2456     + if (unlikely(!p)) {
2457     + lower_dentry = ERR_PTR(-ENOMEM);
2458     + goto out;
2459     + }
2460     + path = p;
2461     + }
2462     +
2463     + /* store the child dentry */
2464     + path[count++] = child_dentry;
2465     + } while (!lower_parent_dentry);
2466     + count--;
2467     +
2468     + sb = dentry->d_sb;
2469     +
2470     + /*
2471     + * This code goes between the begin/end labels and basically
2472     + * emulates a while(child_dentry != dentry), only cleaner and
2473     + * shorter than what would be a much longer while loop.
2474     + */
2475     +begin:
2476     + /* get lower parent dir in the current branch */
2477     + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2478     + dput(parent_dentry);
2479     +
2480     + /* init the values to lookup */
2481     + childname = child_dentry->d_name.name;
2482     + childnamelen = child_dentry->d_name.len;
2483     +
2484     + if (child_dentry != dentry) {
2485     + /* lookup child in the underlying file system */
2486     + lower_dentry = lookup_one_len(childname, lower_parent_dentry,
2487     + childnamelen);
2488     + if (IS_ERR(lower_dentry))
2489     + goto out;
2490     + } else {
2491     + /*
2492     + * Is the name a whiteout of the child name ? lookup the
2493     + * whiteout child in the underlying file system
2494     + */
2495     + lower_dentry = lookup_one_len(name, lower_parent_dentry,
2496     + strlen(name));
2497     + if (IS_ERR(lower_dentry))
2498     + goto out;
2499     +
2500     + /* Replace the current dentry (if any) with the new one */
2501     + dput(unionfs_lower_dentry_idx(dentry, bindex));
2502     + unionfs_set_lower_dentry_idx(dentry, bindex,
2503     + lower_dentry);
2504     +
2505     + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2506     + goto out;
2507     + }
2508     +
2509     + if (lower_dentry->d_inode) {
2510     + /*
2511     + * since this already exists we dput to avoid
2512     + * multiple references on the same dentry
2513     + */
2514     + dput(lower_dentry);
2515     + } else {
2516     + struct sioq_args args;
2517     +
2518     + /* it's a negative dentry, create a new dir */
2519     + lower_parent_dentry = lock_parent(lower_dentry);
2520     +
2521     + args.mkdir.parent = lower_parent_dentry->d_inode;
2522     + args.mkdir.dentry = lower_dentry;
2523     + args.mkdir.mode = child_dentry->d_inode->i_mode;
2524     +
2525     + run_sioq(__unionfs_mkdir, &args);
2526     + err = args.err;
2527     +
2528     + if (!err)
2529     + err = copyup_permissions(dir->i_sb, child_dentry,
2530     + lower_dentry);
2531     + unlock_dir(lower_parent_dentry);
2532     + if (err) {
2533     + dput(lower_dentry);
2534     + lower_dentry = ERR_PTR(err);
2535     + goto out;
2536     + }
2537     +
2538     + }
2539     +
2540     + __set_inode(child_dentry, lower_dentry, bindex);
2541     + __set_dentry(child_dentry, lower_dentry, bindex);
2542     + /*
2543     + * update times of this dentry, but also the parent, because if
2544     + * we changed, the parent may have changed too.
2545     + */
2546     + fsstack_copy_attr_times(parent_dentry->d_inode,
2547     + lower_parent_dentry->d_inode);
2548     + unionfs_copy_attr_times(child_dentry->d_inode);
2549     +
2550     + parent_dentry = child_dentry;
2551     + child_dentry = path[--count];
2552     + goto begin;
2553     +out:
2554     + /* cleanup any leftover locks from the do/while loop above */
2555     + if (IS_ERR(lower_dentry))
2556     + while (count)
2557     + dput(path[count--]);
2558     + kfree(path);
2559     + return lower_dentry;
2560     +}
2561     +
2562     +/*
2563     + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2564     + * dentry+parents to the first parent node that has an mnt.
2565     + */
2566     +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2567     +{
2568     + struct dentry *parent, *hasone;
2569     + int bindex = dbstart(dentry);
2570     +
2571     + if (unionfs_lower_mnt_idx(dentry, bindex))
2572     + return;
2573     + hasone = dentry->d_parent;
2574     + /* this loop should stop at root dentry */
2575     + while (!unionfs_lower_mnt_idx(hasone, bindex))
2576     + hasone = hasone->d_parent;
2577     + parent = dentry;
2578     + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2579     + unionfs_set_lower_mnt_idx(parent, bindex,
2580     + unionfs_mntget(hasone, bindex));
2581     + parent = parent->d_parent;
2582     + }
2583     +}
2584     +
2585     +/*
2586     + * Post-copyup helper to release all non-directory source objects of a
2587     + * copied-up file. Regular files should have only one lower object.
2588     + */
2589     +void unionfs_postcopyup_release(struct dentry *dentry)
2590     +{
2591     + int bstart, bend;
2592     +
2593     + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2594     + bstart = dbstart(dentry);
2595     + bend = dbend(dentry);
2596     +
2597     + path_put_lowers(dentry, bstart + 1, bend, false);
2598     + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2599     +
2600     + dbend(dentry) = bstart;
2601     + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2602     +}
2603     diff -Naur linux-2.6.29/fs/unionfs/debug.c linux-2.6.29-magellan/fs/unionfs/debug.c
2604     --- linux-2.6.29/fs/unionfs/debug.c 1970-01-01 01:00:00.000000000 +0100
2605     +++ linux-2.6.29-magellan/fs/unionfs/debug.c 2009-04-23 19:41:06.000000000 +0200
2606     @@ -0,0 +1,533 @@
2607     +/*
2608     + * Copyright (c) 2003-2009 Erez Zadok
2609     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2610     + * Copyright (c) 2003-2009 Stony Brook University
2611     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
2612     + *
2613     + * This program is free software; you can redistribute it and/or modify
2614     + * it under the terms of the GNU General Public License version 2 as
2615     + * published by the Free Software Foundation.
2616     + */
2617     +
2618     +#include "union.h"
2619     +
2620     +/*
2621     + * Helper debugging functions for maintainers (and for users to report back
2622     + * useful information back to maintainers)
2623     + */
2624     +
2625     +/* it's always useful to know what part of the code called us */
2626     +#define PRINT_CALLER(fname, fxn, line) \
2627     + do { \
2628     + if (!printed_caller) { \
2629     + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2630     + printed_caller = 1; \
2631     + } \
2632     + } while (0)
2633     +
2634     +/*
2635     + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2636     + * the fan-out of various Unionfs objects. We check that no lower objects
2637     + * exist outside the start/end branch range; that all objects within are
2638     + * non-NULL (with some allowed exceptions); that for every lower file
2639     + * there's a lower dentry+inode; that the start/end ranges match for all
2640     + * corresponding lower objects; that open files/symlinks have only one lower
2641     + * objects, but directories can have several; and more.
2642     + */
2643     +void __unionfs_check_inode(const struct inode *inode,
2644     + const char *fname, const char *fxn, int line)
2645     +{
2646     + int bindex;
2647     + int istart, iend;
2648     + struct inode *lower_inode;
2649     + struct super_block *sb;
2650     + int printed_caller = 0;
2651     + void *poison_ptr;
2652     +
2653     + /* for inodes now */
2654     + BUG_ON(!inode);
2655     + sb = inode->i_sb;
2656     + istart = ibstart(inode);
2657     + iend = ibend(inode);
2658     + /* don't check inode if no lower branches */
2659     + if (istart < 0 && iend < 0)
2660     + return;
2661     + if (unlikely(istart > iend)) {
2662     + PRINT_CALLER(fname, fxn, line);
2663     + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2664     + inode, istart, iend);
2665     + }
2666     + if (unlikely((istart == -1 && iend != -1) ||
2667     + (istart != -1 && iend == -1))) {
2668     + PRINT_CALLER(fname, fxn, line);
2669     + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2670     + inode, istart, iend);
2671     + }
2672     + if (!S_ISDIR(inode->i_mode)) {
2673     + if (unlikely(iend != istart)) {
2674     + PRINT_CALLER(fname, fxn, line);
2675     + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2676     + inode, istart, iend);
2677     + }
2678     + }
2679     +
2680     + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2681     + if (unlikely(!UNIONFS_I(inode))) {
2682     + PRINT_CALLER(fname, fxn, line);
2683     + pr_debug(" Ci3: no inode_info %p\n", inode);
2684     + return;
2685     + }
2686     + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2687     + PRINT_CALLER(fname, fxn, line);
2688     + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2689     + return;
2690     + }
2691     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2692     + if (lower_inode) {
2693     + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2694     + if (unlikely(bindex < istart || bindex > iend)) {
2695     + PRINT_CALLER(fname, fxn, line);
2696     + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2697     + "istart/end=%d:%d\n", inode,
2698     + lower_inode, bindex, istart, iend);
2699     + } else if (unlikely(lower_inode == poison_ptr)) {
2700     + /* freed inode! */
2701     + PRINT_CALLER(fname, fxn, line);
2702     + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2703     + "istart/end=%d:%d\n", inode,
2704     + lower_inode, bindex, istart, iend);
2705     + }
2706     + continue;
2707     + }
2708     + /* if we get here, then lower_inode == NULL */
2709     + if (bindex < istart || bindex > iend)
2710     + continue;
2711     + /*
2712     + * directories can have NULL lower inodes in b/t start/end,
2713     + * but NOT if at the start/end range.
2714     + */
2715     + if (unlikely(S_ISDIR(inode->i_mode) &&
2716     + bindex > istart && bindex < iend))
2717     + continue;
2718     + PRINT_CALLER(fname, fxn, line);
2719     + pr_debug(" Ci7: inode/linode=%p:%p "
2720     + "bindex=%d istart/end=%d:%d\n",
2721     + inode, lower_inode, bindex, istart, iend);
2722     + }
2723     +}
2724     +
2725     +void __unionfs_check_dentry(const struct dentry *dentry,
2726     + const char *fname, const char *fxn, int line)
2727     +{
2728     + int bindex;
2729     + int dstart, dend, istart, iend;
2730     + struct dentry *lower_dentry;
2731     + struct inode *inode, *lower_inode;
2732     + struct super_block *sb;
2733     + struct vfsmount *lower_mnt;
2734     + int printed_caller = 0;
2735     + void *poison_ptr;
2736     +
2737     + BUG_ON(!dentry);
2738     + sb = dentry->d_sb;
2739     + inode = dentry->d_inode;
2740     + dstart = dbstart(dentry);
2741     + dend = dbend(dentry);
2742     + /* don't check dentry/mnt if no lower branches */
2743     + if (dstart < 0 && dend < 0)
2744     + goto check_inode;
2745     + BUG_ON(dstart > dend);
2746     +
2747     + if (unlikely((dstart == -1 && dend != -1) ||
2748     + (dstart != -1 && dend == -1))) {
2749     + PRINT_CALLER(fname, fxn, line);
2750     + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2751     + dentry, dstart, dend);
2752     + }
2753     + /*
2754     + * check for NULL dentries inside the start/end range, or
2755     + * non-NULL dentries outside the start/end range.
2756     + */
2757     + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2758     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2759     + if (lower_dentry) {
2760     + if (unlikely(bindex < dstart || bindex > dend)) {
2761     + PRINT_CALLER(fname, fxn, line);
2762     + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2763     + "bindex=%d dstart/end=%d:%d\n",
2764     + dentry, lower_dentry,
2765     + (lower_dentry ? lower_dentry->d_inode :
2766     + (void *) -1L),
2767     + bindex, dstart, dend);
2768     + }
2769     + } else { /* lower_dentry == NULL */
2770     + if (bindex < dstart || bindex > dend)
2771     + continue;
2772     + /*
2773     + * Directories can have NULL lower inodes in b/t
2774     + * start/end, but NOT if at the start/end range.
2775     + * Ignore this rule, however, if this is a NULL
2776     + * dentry or a deleted dentry.
2777     + */
2778     + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2779     + inode &&
2780     + !(inode && S_ISDIR(inode->i_mode) &&
2781     + bindex > dstart && bindex < dend))) {
2782     + PRINT_CALLER(fname, fxn, line);
2783     + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2784     + "bindex=%d dstart/end=%d:%d\n",
2785     + dentry, lower_dentry,
2786     + (lower_dentry ?
2787     + lower_dentry->d_inode :
2788     + (void *) -1L),
2789     + bindex, dstart, dend);
2790     + }
2791     + }
2792     + }
2793     +
2794     + /* check for vfsmounts same as for dentries */
2795     + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2796     + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2797     + if (lower_mnt) {
2798     + if (unlikely(bindex < dstart || bindex > dend)) {
2799     + PRINT_CALLER(fname, fxn, line);
2800     + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2801     + "dstart/end=%d:%d\n", dentry,
2802     + lower_mnt, bindex, dstart, dend);
2803     + }
2804     + } else { /* lower_mnt == NULL */
2805     + if (bindex < dstart || bindex > dend)
2806     + continue;
2807     + /*
2808     + * Directories can have NULL lower inodes in b/t
2809     + * start/end, but NOT if at the start/end range.
2810     + * Ignore this rule, however, if this is a NULL
2811     + * dentry.
2812     + */
2813     + if (unlikely(inode &&
2814     + !(inode && S_ISDIR(inode->i_mode) &&
2815     + bindex > dstart && bindex < dend))) {
2816     + PRINT_CALLER(fname, fxn, line);
2817     + pr_debug(" CM1: dentry/lmnt=%p:%p "
2818     + "bindex=%d dstart/end=%d:%d\n",
2819     + dentry, lower_mnt, bindex,
2820     + dstart, dend);
2821     + }
2822     + }
2823     + }
2824     +
2825     +check_inode:
2826     + /* for inodes now */
2827     + if (!inode)
2828     + return;
2829     + istart = ibstart(inode);
2830     + iend = ibend(inode);
2831     + /* don't check inode if no lower branches */
2832     + if (istart < 0 && iend < 0)
2833     + return;
2834     + BUG_ON(istart > iend);
2835     + if (unlikely((istart == -1 && iend != -1) ||
2836     + (istart != -1 && iend == -1))) {
2837     + PRINT_CALLER(fname, fxn, line);
2838     + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2839     + dentry, inode, istart, iend);
2840     + }
2841     + if (unlikely(istart != dstart)) {
2842     + PRINT_CALLER(fname, fxn, line);
2843     + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2844     + dentry, inode, istart, dstart);
2845     + }
2846     + if (unlikely(iend != dend)) {
2847     + PRINT_CALLER(fname, fxn, line);
2848     + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2849     + dentry, inode, iend, dend);
2850     + }
2851     +
2852     + if (!S_ISDIR(inode->i_mode)) {
2853     + if (unlikely(dend != dstart)) {
2854     + PRINT_CALLER(fname, fxn, line);
2855     + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2856     + dentry, inode, dstart, dend);
2857     + }
2858     + if (unlikely(iend != istart)) {
2859     + PRINT_CALLER(fname, fxn, line);
2860     + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2861     + dentry, inode, istart, iend);
2862     + }
2863     + }
2864     +
2865     + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2866     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2867     + if (lower_inode) {
2868     + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2869     + if (unlikely(bindex < istart || bindex > iend)) {
2870     + PRINT_CALLER(fname, fxn, line);
2871     + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2872     + "istart/end=%d:%d\n", dentry,
2873     + lower_inode, bindex, istart, iend);
2874     + } else if (unlikely(lower_inode == poison_ptr)) {
2875     + /* freed inode! */
2876     + PRINT_CALLER(fname, fxn, line);
2877     + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2878     + "istart/end=%d:%d\n", dentry,
2879     + lower_inode, bindex, istart, iend);
2880     + }
2881     + continue;
2882     + }
2883     + /* if we get here, then lower_inode == NULL */
2884     + if (bindex < istart || bindex > iend)
2885     + continue;
2886     + /*
2887     + * directories can have NULL lower inodes in b/t start/end,
2888     + * but NOT if at the start/end range.
2889     + */
2890     + if (unlikely(S_ISDIR(inode->i_mode) &&
2891     + bindex > istart && bindex < iend))
2892     + continue;
2893     + PRINT_CALLER(fname, fxn, line);
2894     + pr_debug(" CI7: dentry/linode=%p:%p "
2895     + "bindex=%d istart/end=%d:%d\n",
2896     + dentry, lower_inode, bindex, istart, iend);
2897     + }
2898     +
2899     + /*
2900     + * If it's a directory, then intermediate objects b/t start/end can
2901     + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2902     + * and inode.
2903     + */
2904     + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2905     + for (bindex = dstart+1; bindex < dend; bindex++) {
2906     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2907     + lower_dentry = unionfs_lower_dentry_idx(dentry,
2908     + bindex);
2909     + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2910     + if (unlikely(!((lower_inode && lower_dentry &&
2911     + lower_mnt) ||
2912     + (!lower_inode &&
2913     + !lower_dentry && !lower_mnt)))) {
2914     + PRINT_CALLER(fname, fxn, line);
2915     + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
2916     + "bindex=%d dstart/end=%d:%d\n",
2917     + lower_mnt, lower_dentry, lower_inode,
2918     + bindex, dstart, dend);
2919     + }
2920     + }
2921     + /* check if lower inode is newer than upper one (it shouldn't) */
2922     + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
2923     + PRINT_CALLER(fname, fxn, line);
2924     + for (bindex = ibstart(inode); bindex <= ibend(inode);
2925     + bindex++) {
2926     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2927     + if (unlikely(!lower_inode))
2928     + continue;
2929     + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
2930     + "ctime/lctime=%lu.%lu/%lu.%lu\n",
2931     + bindex,
2932     + inode->i_mtime.tv_sec,
2933     + inode->i_mtime.tv_nsec,
2934     + lower_inode->i_mtime.tv_sec,
2935     + lower_inode->i_mtime.tv_nsec,
2936     + inode->i_ctime.tv_sec,
2937     + inode->i_ctime.tv_nsec,
2938     + lower_inode->i_ctime.tv_sec,
2939     + lower_inode->i_ctime.tv_nsec);
2940     + }
2941     + }
2942     +}
2943     +
2944     +void __unionfs_check_file(const struct file *file,
2945     + const char *fname, const char *fxn, int line)
2946     +{
2947     + int bindex;
2948     + int dstart, dend, fstart, fend;
2949     + struct dentry *dentry;
2950     + struct file *lower_file;
2951     + struct inode *inode;
2952     + struct super_block *sb;
2953     + int printed_caller = 0;
2954     +
2955     + BUG_ON(!file);
2956     + dentry = file->f_path.dentry;
2957     + sb = dentry->d_sb;
2958     + dstart = dbstart(dentry);
2959     + dend = dbend(dentry);
2960     + BUG_ON(dstart > dend);
2961     + fstart = fbstart(file);
2962     + fend = fbend(file);
2963     + BUG_ON(fstart > fend);
2964     +
2965     + if (unlikely((fstart == -1 && fend != -1) ||
2966     + (fstart != -1 && fend == -1))) {
2967     + PRINT_CALLER(fname, fxn, line);
2968     + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
2969     + file, dentry, fstart, fend);
2970     + }
2971     + if (unlikely(fstart != dstart)) {
2972     + PRINT_CALLER(fname, fxn, line);
2973     + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
2974     + file, dentry, fstart, dstart);
2975     + }
2976     + if (unlikely(fend != dend)) {
2977     + PRINT_CALLER(fname, fxn, line);
2978     + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
2979     + file, dentry, fend, dend);
2980     + }
2981     + inode = dentry->d_inode;
2982     + if (!S_ISDIR(inode->i_mode)) {
2983     + if (unlikely(fend != fstart)) {
2984     + PRINT_CALLER(fname, fxn, line);
2985     + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
2986     + file, inode, fstart, fend);
2987     + }
2988     + if (unlikely(dend != dstart)) {
2989     + PRINT_CALLER(fname, fxn, line);
2990     + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
2991     + file, dentry, dstart, dend);
2992     + }
2993     + }
2994     +
2995     + /*
2996     + * check for NULL dentries inside the start/end range, or
2997     + * non-NULL dentries outside the start/end range.
2998     + */
2999     + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
3000     + lower_file = unionfs_lower_file_idx(file, bindex);
3001     + if (lower_file) {
3002     + if (unlikely(bindex < fstart || bindex > fend)) {
3003     + PRINT_CALLER(fname, fxn, line);
3004     + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
3005     + "fstart/end=%d:%d\n", file,
3006     + lower_file, bindex, fstart, fend);
3007     + }
3008     + } else { /* lower_file == NULL */
3009     + if (bindex >= fstart && bindex <= fend) {
3010     + /*
3011     + * directories can have NULL lower inodes in
3012     + * b/t start/end, but NOT if at the
3013     + * start/end range.
3014     + */
3015     + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3016     + bindex > fstart &&
3017     + bindex < fend))) {
3018     + PRINT_CALLER(fname, fxn, line);
3019     + pr_debug(" CF6: file/lower=%p:%p "
3020     + "bindex=%d fstart/end=%d:%d\n",
3021     + file, lower_file, bindex,
3022     + fstart, fend);
3023     + }
3024     + }
3025     + }
3026     + }
3027     +
3028     + __unionfs_check_dentry(dentry, fname, fxn, line);
3029     +}
3030     +
3031     +void __unionfs_check_nd(const struct nameidata *nd,
3032     + const char *fname, const char *fxn, int line)
3033     +{
3034     + struct file *file;
3035     + int printed_caller = 0;
3036     +
3037     + if (unlikely(!nd))
3038     + return;
3039     + if (nd->flags & LOOKUP_OPEN) {
3040     + file = nd->intent.open.file;
3041     + if (unlikely(file->f_path.dentry &&
3042     + strcmp(file->f_path.dentry->d_sb->s_type->name,
3043     + UNIONFS_NAME))) {
3044     + PRINT_CALLER(fname, fxn, line);
3045     + pr_debug(" CND1: lower_file of type %s\n",
3046     + file->f_path.dentry->d_sb->s_type->name);
3047     + BUG();
3048     + }
3049     + }
3050     +}
3051     +
3052     +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3053     +void __show_branch_counts(const struct super_block *sb,
3054     + const char *file, const char *fxn, int line)
3055     +{
3056     + int i;
3057     + struct vfsmount *mnt;
3058     +
3059     + pr_debug("BC:");
3060     + for (i = 0; i < sbmax(sb); i++) {
3061     + if (likely(sb->s_root))
3062     + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3063     + else
3064     + mnt = NULL;
3065     + printk(KERN_CONT "%d:",
3066     + (mnt ? atomic_read(&mnt->mnt_count) : -99));
3067     + }
3068     + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3069     +}
3070     +
3071     +void __show_inode_times(const struct inode *inode,
3072     + const char *file, const char *fxn, int line)
3073     +{
3074     + struct inode *lower_inode;
3075     + int bindex;
3076     +
3077     + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3078     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3079     + if (unlikely(!lower_inode))
3080     + continue;
3081     + pr_debug("IT(%lu:%d): %s:%s:%d "
3082     + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3083     + inode->i_ino, bindex,
3084     + file, fxn, line,
3085     + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3086     + lower_inode->i_mtime.tv_sec,
3087     + lower_inode->i_mtime.tv_nsec,
3088     + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3089     + lower_inode->i_ctime.tv_sec,
3090     + lower_inode->i_ctime.tv_nsec);
3091     + }
3092     +}
3093     +
3094     +void __show_dinode_times(const struct dentry *dentry,
3095     + const char *file, const char *fxn, int line)
3096     +{
3097     + struct inode *inode = dentry->d_inode;
3098     + struct inode *lower_inode;
3099     + int bindex;
3100     +
3101     + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3102     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3103     + if (!lower_inode)
3104     + continue;
3105     + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3106     + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3107     + dentry->d_name.name, inode->i_ino, bindex,
3108     + file, fxn, line,
3109     + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3110     + lower_inode->i_mtime.tv_sec,
3111     + lower_inode->i_mtime.tv_nsec,
3112     + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3113     + lower_inode->i_ctime.tv_sec,
3114     + lower_inode->i_ctime.tv_nsec);
3115     + }
3116     +}
3117     +
3118     +void __show_inode_counts(const struct inode *inode,
3119     + const char *file, const char *fxn, int line)
3120     +{
3121     + struct inode *lower_inode;
3122     + int bindex;
3123     +
3124     + if (unlikely(!inode)) {
3125     + pr_debug("SiC: Null inode\n");
3126     + return;
3127     + }
3128     + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3129     + bindex++) {
3130     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3131     + if (unlikely(!lower_inode))
3132     + continue;
3133     + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3134     + inode->i_ino, bindex,
3135     + atomic_read(&(inode)->i_count),
3136     + atomic_read(&(lower_inode)->i_count),
3137     + file, fxn, line);
3138     + }
3139     +}
3140     diff -Naur linux-2.6.29/fs/unionfs/dentry.c linux-2.6.29-magellan/fs/unionfs/dentry.c
3141     --- linux-2.6.29/fs/unionfs/dentry.c 1970-01-01 01:00:00.000000000 +0100
3142     +++ linux-2.6.29-magellan/fs/unionfs/dentry.c 2009-04-23 19:41:06.000000000 +0200
3143     @@ -0,0 +1,397 @@
3144     +/*
3145     + * Copyright (c) 2003-2009 Erez Zadok
3146     + * Copyright (c) 2003-2006 Charles P. Wright
3147     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3148     + * Copyright (c) 2005-2006 Junjiro Okajima
3149     + * Copyright (c) 2005 Arun M. Krishnakumar
3150     + * Copyright (c) 2004-2006 David P. Quigley
3151     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3152     + * Copyright (c) 2003 Puja Gupta
3153     + * Copyright (c) 2003 Harikesavan Krishnan
3154     + * Copyright (c) 2003-2009 Stony Brook University
3155     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3156     + *
3157     + * This program is free software; you can redistribute it and/or modify
3158     + * it under the terms of the GNU General Public License version 2 as
3159     + * published by the Free Software Foundation.
3160     + */
3161     +
3162     +#include "union.h"
3163     +
3164     +bool is_negative_lower(const struct dentry *dentry)
3165     +{
3166     + int bindex;
3167     + struct dentry *lower_dentry;
3168     +
3169     + BUG_ON(!dentry);
3170     + /* cache coherency: check if file was deleted on lower branch */
3171     + if (dbstart(dentry) < 0)
3172     + return true;
3173     + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3174     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3175     + /* unhashed (i.e., unlinked) lower dentries don't count */
3176     + if (lower_dentry && lower_dentry->d_inode &&
3177     + !d_deleted(lower_dentry) &&
3178     + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3179     + return false;
3180     + }
3181     + return true;
3182     +}
3183     +
3184     +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3185     +{
3186     + struct dentry *lower_dentry;
3187     + int bindex;
3188     +
3189     + if (start < 0)
3190     + return;
3191     + for (bindex = start; bindex <= end; bindex++) {
3192     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3193     + if (!lower_dentry)
3194     + continue;
3195     + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3196     + dput(lower_dentry);
3197     + }
3198     +}
3199     +
3200     +/*
3201     + * Purge and invalidate as many data pages of a unionfs inode. This is
3202     + * called when the lower inode has changed, and we want to force processes
3203     + * to re-get the new data.
3204     + */
3205     +static inline void purge_inode_data(struct inode *inode)
3206     +{
3207     + /* remove all non-private mappings */
3208     + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3209     + /* invalidate as many pages as possible */
3210     + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3211     + /*
3212     + * Don't try to truncate_inode_pages here, because this could lead
3213     + * to a deadlock between some of address_space ops and dentry
3214     + * revalidation: the address space op is invoked with a lock on our
3215     + * own page, and truncate_inode_pages will block on locked pages.
3216     + */
3217     +}
3218     +
3219     +/*
3220     + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3221     + * of the @dentry and its @parent are locked. Assume parent is valid,
3222     + * otherwise return false (and let's hope the VFS will try to re-lookup this
3223     + * dentry). Returns true if valid, false otherwise.
3224     + */
3225     +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3226     + bool willwrite)
3227     +{
3228     + bool valid = true; /* default is valid */
3229     + struct dentry *lower_dentry;
3230     + struct dentry *result;
3231     + int bindex, bstart, bend;
3232     + int sbgen, dgen, pdgen;
3233     + int positive = 0;
3234     + int interpose_flag;
3235     +
3236     + verify_locked(dentry);
3237     + verify_locked(parent);
3238     +
3239     + /* if the dentry is unhashed, do NOT revalidate */
3240     + if (d_deleted(dentry))
3241     + goto out;
3242     +
3243     + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3244     +
3245     + if (is_newer_lower(dentry)) {
3246     + /* root dentry is always valid */
3247     + if (IS_ROOT(dentry)) {
3248     + unionfs_copy_attr_times(dentry->d_inode);
3249     + } else {
3250     + /*
3251     + * reset generation number to zero, guaranteed to be
3252     + * "old"
3253     + */
3254     + dgen = 0;
3255     + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3256     + }
3257     + if (!willwrite)
3258     + purge_inode_data(dentry->d_inode);
3259     + }
3260     +
3261     + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3262     +
3263     + BUG_ON(dbstart(dentry) == -1);
3264     + if (dentry->d_inode)
3265     + positive = 1;
3266     +
3267     + /* if our dentry is valid, then validate all lower ones */
3268     + if (sbgen == dgen)
3269     + goto validate_lowers;
3270     +
3271     + /* The root entry should always be valid */
3272     + BUG_ON(IS_ROOT(dentry));
3273     +
3274     + /* We can't work correctly if our parent isn't valid. */
3275     + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3276     +
3277     + /* Free the pointers for our inodes and this dentry. */
3278     + path_put_lowers_all(dentry, false);
3279     +
3280     + interpose_flag = INTERPOSE_REVAL_NEG;
3281     + if (positive) {
3282     + interpose_flag = INTERPOSE_REVAL;
3283     + iput_lowers_all(dentry->d_inode, true);
3284     + }
3285     +
3286     + if (realloc_dentry_private_data(dentry) != 0) {
3287     + valid = false;
3288     + goto out;
3289     + }
3290     +
3291     + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3292     + if (result) {
3293     + if (IS_ERR(result)) {
3294     + valid = false;
3295     + goto out;
3296     + }
3297     + /*
3298     + * current unionfs_lookup_backend() doesn't return
3299     + * a valid dentry
3300     + */
3301     + dput(dentry);
3302     + dentry = result;
3303     + }
3304     +
3305     + if (unlikely(positive && is_negative_lower(dentry))) {
3306     + /* call make_bad_inode here ? */
3307     + d_drop(dentry);
3308     + valid = false;
3309     + goto out;
3310     + }
3311     +
3312     + /*
3313     + * if we got here then we have revalidated our dentry and all lower
3314     + * ones, so we can return safely.
3315     + */
3316     + if (!valid) /* lower dentry revalidation failed */
3317     + goto out;
3318     +
3319     + /*
3320     + * If the parent's gen no. matches the superblock's gen no., then
3321     + * we can update our denty's gen no. If they didn't match, then it
3322     + * was OK to revalidate this dentry with a stale parent, but we'll
3323     + * purposely not update our dentry's gen no. (so it can be redone);
3324     + * and, we'll mark our parent dentry as invalid so it'll force it
3325     + * (and our dentry) to be revalidated.
3326     + */
3327     + if (pdgen == sbgen)
3328     + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3329     + goto out;
3330     +
3331     +validate_lowers:
3332     +
3333     + /* The revalidation must occur across all branches */
3334     + bstart = dbstart(dentry);
3335     + bend = dbend(dentry);
3336     + BUG_ON(bstart == -1);
3337     + for (bindex = bstart; bindex <= bend; bindex++) {
3338     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3339     + if (!lower_dentry || !lower_dentry->d_op
3340     + || !lower_dentry->d_op->d_revalidate)
3341     + continue;
3342     + /*
3343     + * Don't pass nameidata to lower file system, because we
3344     + * don't want an arbitrary lower file being opened or
3345     + * returned to us: it may be useless to us because of the
3346     + * fanout nature of unionfs (cf. file/directory open-file
3347     + * invariants). We will open lower files as and when needed
3348     + * later on.
3349     + */
3350     + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3351     + valid = false;
3352     + }
3353     +
3354     + if (!dentry->d_inode ||
3355     + ibstart(dentry->d_inode) < 0 ||
3356     + ibend(dentry->d_inode) < 0) {
3357     + valid = false;
3358     + goto out;
3359     + }
3360     +
3361     + if (valid) {
3362     + /*
3363     + * If we get here, and we copy the meta-data from the lower
3364     + * inode to our inode, then it is vital that we have already
3365     + * purged all unionfs-level file data. We do that in the
3366     + * caller (__unionfs_d_revalidate) by calling
3367     + * purge_inode_data.
3368     + */
3369     + unionfs_copy_attr_all(dentry->d_inode,
3370     + unionfs_lower_inode(dentry->d_inode));
3371     + fsstack_copy_inode_size(dentry->d_inode,
3372     + unionfs_lower_inode(dentry->d_inode));
3373     + }
3374     +
3375     +out:
3376     + return valid;
3377     +}
3378     +
3379     +/*
3380     + * Determine if the lower inode objects have changed from below the unionfs
3381     + * inode. Return true if changed, false otherwise.
3382     + *
3383     + * We check if the mtime or ctime have changed. However, the inode times
3384     + * can be changed by anyone without much protection, including
3385     + * asynchronously. This can sometimes cause unionfs to find that the lower
3386     + * file system doesn't change its inode times quick enough, resulting in a
3387     + * false positive indication (which is harmless, it just makes unionfs do
3388     + * extra work in re-validating the objects). To minimize the chances of
3389     + * these situations, we still consider such small time changes valid, but we
3390     + * don't print debugging messages unless the time changes are greater than
3391     + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3392     + * because significant changes are more likely due to users manually
3393     + * touching lower files.
3394     + */
3395     +bool is_newer_lower(const struct dentry *dentry)
3396     +{
3397     + int bindex;
3398     + struct inode *inode;
3399     + struct inode *lower_inode;
3400     +
3401     + /* ignore if we're called on semi-initialized dentries/inodes */
3402     + if (!dentry || !UNIONFS_D(dentry))
3403     + return false;
3404     + inode = dentry->d_inode;
3405     + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3406     + ibstart(inode) < 0 || ibend(inode) < 0)
3407     + return false;
3408     +
3409     + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3410     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3411     + if (!lower_inode)
3412     + continue;
3413     +
3414     + /* check if mtime/ctime have changed */
3415     + if (unlikely(timespec_compare(&inode->i_mtime,
3416     + &lower_inode->i_mtime) < 0)) {
3417     + if ((lower_inode->i_mtime.tv_sec -
3418     + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3419     + pr_info("unionfs: new lower inode mtime "
3420     + "(bindex=%d, name=%s)\n", bindex,
3421     + dentry->d_name.name);
3422     + show_dinode_times(dentry);
3423     + }
3424     + return true;
3425     + }
3426     + if (unlikely(timespec_compare(&inode->i_ctime,
3427     + &lower_inode->i_ctime) < 0)) {
3428     + if ((lower_inode->i_ctime.tv_sec -
3429     + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3430     + pr_info("unionfs: new lower inode ctime "
3431     + "(bindex=%d, name=%s)\n", bindex,
3432     + dentry->d_name.name);
3433     + show_dinode_times(dentry);
3434     + }
3435     + return true;
3436     + }
3437     + }
3438     +
3439     + /*
3440     + * Last check: if this is a positive dentry, but somehow all lower
3441     + * dentries are negative or unhashed, then this dentry needs to be
3442     + * revalidated, because someone probably deleted the objects from
3443     + * the lower branches directly.
3444     + */
3445     + if (is_negative_lower(dentry))
3446     + return true;
3447     +
3448     + return false; /* default: lower is not newer */
3449     +}
3450     +
3451     +static int unionfs_d_revalidate(struct dentry *dentry,
3452     + struct nameidata *nd_unused)
3453     +{
3454     + bool valid = true;
3455     + int err = 1; /* 1 means valid for the VFS */
3456     + struct dentry *parent;
3457     +
3458     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3459     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3460     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3461     +
3462     + valid = __unionfs_d_revalidate(dentry, parent, false);
3463     + if (valid) {
3464     + unionfs_postcopyup_setmnt(dentry);
3465     + unionfs_check_dentry(dentry);
3466     + } else {
3467     + d_drop(dentry);
3468     + err = valid;
3469     + }
3470     + unionfs_unlock_dentry(dentry);
3471     + unionfs_unlock_parent(dentry, parent);
3472     + unionfs_read_unlock(dentry->d_sb);
3473     +
3474     + return err;
3475     +}
3476     +
3477     +static void unionfs_d_release(struct dentry *dentry)
3478     +{
3479     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3480     + if (unlikely(!UNIONFS_D(dentry)))
3481     + goto out; /* skip if no lower branches */
3482     + /* must lock our branch configuration here */
3483     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3484     +
3485     + unionfs_check_dentry(dentry);
3486     + /* this could be a negative dentry, so check first */
3487     + if (dbstart(dentry) < 0) {
3488     + unionfs_unlock_dentry(dentry);
3489     + goto out; /* due to a (normal) failed lookup */
3490     + }
3491     +
3492     + /* Release all the lower dentries */
3493     + path_put_lowers_all(dentry, true);
3494     +
3495     + unionfs_unlock_dentry(dentry);
3496     +
3497     +out:
3498     + free_dentry_private_data(dentry);
3499     + unionfs_read_unlock(dentry->d_sb);
3500     + return;
3501     +}
3502     +
3503     +/*
3504     + * Called when we're removing the last reference to our dentry. So we
3505     + * should drop all lower references too.
3506     + */
3507     +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3508     +{
3509     + int rc;
3510     +
3511     + BUG_ON(!dentry);
3512     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3513     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3514     +
3515     + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3516     + goto drop_lower_inodes;
3517     + path_put_lowers_all(dentry, false);
3518     +
3519     +drop_lower_inodes:
3520     + rc = atomic_read(&inode->i_count);
3521     + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3522     + /* see Documentation/filesystems/unionfs/issues.txt */
3523     + lockdep_off();
3524     + iput(unionfs_lower_inode(inode));
3525     + lockdep_on();
3526     + unionfs_set_lower_inode(inode, NULL);
3527     + /* XXX: may need to set start/end to -1? */
3528     + }
3529     +
3530     + iput(inode);
3531     +
3532     + unionfs_unlock_dentry(dentry);
3533     + unionfs_read_unlock(dentry->d_sb);
3534     +}
3535     +
3536     +struct dentry_operations unionfs_dops = {
3537     + .d_revalidate = unionfs_d_revalidate,
3538     + .d_release = unionfs_d_release,
3539     + .d_iput = unionfs_d_iput,
3540     +};
3541     diff -Naur linux-2.6.29/fs/unionfs/dirfops.c linux-2.6.29-magellan/fs/unionfs/dirfops.c
3542     --- linux-2.6.29/fs/unionfs/dirfops.c 1970-01-01 01:00:00.000000000 +0100
3543     +++ linux-2.6.29-magellan/fs/unionfs/dirfops.c 2009-04-23 19:41:06.000000000 +0200
3544     @@ -0,0 +1,302 @@
3545     +/*
3546     + * Copyright (c) 2003-2009 Erez Zadok
3547     + * Copyright (c) 2003-2006 Charles P. Wright
3548     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3549     + * Copyright (c) 2005-2006 Junjiro Okajima
3550     + * Copyright (c) 2005 Arun M. Krishnakumar
3551     + * Copyright (c) 2004-2006 David P. Quigley
3552     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3553     + * Copyright (c) 2003 Puja Gupta
3554     + * Copyright (c) 2003 Harikesavan Krishnan
3555     + * Copyright (c) 2003-2009 Stony Brook University
3556     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3557     + *
3558     + * This program is free software; you can redistribute it and/or modify
3559     + * it under the terms of the GNU General Public License version 2 as
3560     + * published by the Free Software Foundation.
3561     + */
3562     +
3563     +#include "union.h"
3564     +
3565     +/* Make sure our rdstate is playing by the rules. */
3566     +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3567     +{
3568     + BUG_ON(rdstate->offset >= DIREOF);
3569     + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3570     +}
3571     +
3572     +struct unionfs_getdents_callback {
3573     + struct unionfs_dir_state *rdstate;
3574     + void *dirent;
3575     + int entries_written;
3576     + int filldir_called;
3577     + int filldir_error;
3578     + filldir_t filldir;
3579     + struct super_block *sb;
3580     +};
3581     +
3582     +/* based on generic filldir in fs/readir.c */
3583     +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3584     + loff_t offset, u64 ino, unsigned int d_type)
3585     +{
3586     + struct unionfs_getdents_callback *buf = dirent;
3587     + struct filldir_node *found = NULL;
3588     + int err = 0;
3589     + int is_whiteout;
3590     + char *name = (char *) oname;
3591     +
3592     + buf->filldir_called++;
3593     +
3594     + is_whiteout = is_whiteout_name(&name, &namelen);
3595     +
3596     + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3597     +
3598     + if (found) {
3599     + /*
3600     + * If we had non-whiteout entry in dir cache, then mark it
3601     + * as a whiteout and but leave it in the dir cache.
3602     + */
3603     + if (is_whiteout && !found->whiteout)
3604     + found->whiteout = is_whiteout;
3605     + goto out;
3606     + }
3607     +
3608     + /* if 'name' isn't a whiteout, filldir it. */
3609     + if (!is_whiteout) {
3610     + off_t pos = rdstate2offset(buf->rdstate);
3611     + u64 unionfs_ino = ino;
3612     +
3613     + err = buf->filldir(buf->dirent, name, namelen, pos,
3614     + unionfs_ino, d_type);
3615     + buf->rdstate->offset++;
3616     + verify_rdstate_offset(buf->rdstate);
3617     + }
3618     + /*
3619     + * If we did fill it, stuff it in our hash, otherwise return an
3620     + * error.
3621     + */
3622     + if (err) {
3623     + buf->filldir_error = err;
3624     + goto out;
3625     + }
3626     + buf->entries_written++;
3627     + err = add_filldir_node(buf->rdstate, name, namelen,
3628     + buf->rdstate->bindex, is_whiteout);
3629     + if (err)
3630     + buf->filldir_error = err;
3631     +
3632     +out:
3633     + return err;
3634     +}
3635     +
3636     +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3637     +{
3638     + int err = 0;
3639     + struct file *lower_file = NULL;
3640     + struct dentry *dentry = file->f_path.dentry;
3641     + struct dentry *parent;
3642     + struct inode *inode = NULL;
3643     + struct unionfs_getdents_callback buf;
3644     + struct unionfs_dir_state *uds;
3645     + int bend;
3646     + loff_t offset;
3647     +
3648     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3649     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3650     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3651     +
3652     + err = unionfs_file_revalidate(file, parent, false);
3653     + if (unlikely(err))
3654     + goto out;
3655     +
3656     + inode = dentry->d_inode;
3657     +
3658     + uds = UNIONFS_F(file)->rdstate;
3659     + if (!uds) {
3660     + if (file->f_pos == DIREOF) {
3661     + goto out;
3662     + } else if (file->f_pos > 0) {
3663     + uds = find_rdstate(inode, file->f_pos);
3664     + if (unlikely(!uds)) {
3665     + err = -ESTALE;
3666     + goto out;
3667     + }
3668     + UNIONFS_F(file)->rdstate = uds;
3669     + } else {
3670     + init_rdstate(file);
3671     + uds = UNIONFS_F(file)->rdstate;
3672     + }
3673     + }
3674     + bend = fbend(file);
3675     +
3676     + while (uds->bindex <= bend) {
3677     + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3678     + if (!lower_file) {
3679     + uds->bindex++;
3680     + uds->dirpos = 0;
3681     + continue;
3682     + }
3683     +
3684     + /* prepare callback buffer */
3685     + buf.filldir_called = 0;
3686     + buf.filldir_error = 0;
3687     + buf.entries_written = 0;
3688     + buf.dirent = dirent;
3689     + buf.filldir = filldir;
3690     + buf.rdstate = uds;
3691     + buf.sb = inode->i_sb;
3692     +
3693     + /* Read starting from where we last left off. */
3694     + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3695     + if (offset < 0) {
3696     + err = offset;
3697     + goto out;
3698     + }
3699     + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3700     +
3701     + /* Save the position for when we continue. */
3702     + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3703     + if (offset < 0) {
3704     + err = offset;
3705     + goto out;
3706     + }
3707     + uds->dirpos = offset;
3708     +
3709     + /* Copy the atime. */
3710     + fsstack_copy_attr_atime(inode,
3711     + lower_file->f_path.dentry->d_inode);
3712     +
3713     + if (err < 0)
3714     + goto out;
3715     +
3716     + if (buf.filldir_error)
3717     + break;
3718     +
3719     + if (!buf.entries_written) {
3720     + uds->bindex++;
3721     + uds->dirpos = 0;
3722     + }
3723     + }
3724     +
3725     + if (!buf.filldir_error && uds->bindex >= bend) {
3726     + /* Save the number of hash entries for next time. */
3727     + UNIONFS_I(inode)->hashsize = uds->hashentries;
3728     + free_rdstate(uds);
3729     + UNIONFS_F(file)->rdstate = NULL;
3730     + file->f_pos = DIREOF;
3731     + } else {
3732     + file->f_pos = rdstate2offset(uds);
3733     + }
3734     +
3735     +out:
3736     + if (!err)
3737     + unionfs_check_file(file);
3738     + unionfs_unlock_dentry(dentry);
3739     + unionfs_unlock_parent(dentry, parent);
3740     + unionfs_read_unlock(dentry->d_sb);
3741     + return err;
3742     +}
3743     +
3744     +/*
3745     + * This is not meant to be a generic repositioning function. If you do
3746     + * things that aren't supported, then we return EINVAL.
3747     + *
3748     + * What is allowed:
3749     + * (1) seeking to the same position that you are currently at
3750     + * This really has no effect, but returns where you are.
3751     + * (2) seeking to the beginning of the file
3752     + * This throws out all state, and lets you begin again.
3753     + */
3754     +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3755     +{
3756     + struct unionfs_dir_state *rdstate;
3757     + struct dentry *dentry = file->f_path.dentry;
3758     + struct dentry *parent;
3759     + loff_t err;
3760     +
3761     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3762     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3763     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3764     +
3765     + err = unionfs_file_revalidate(file, parent, false);
3766     + if (unlikely(err))
3767     + goto out;
3768     +
3769     + rdstate = UNIONFS_F(file)->rdstate;
3770     +
3771     + /*
3772     + * we let users seek to their current position, but not anywhere
3773     + * else.
3774     + */
3775     + if (!offset) {
3776     + switch (origin) {
3777     + case SEEK_SET:
3778     + if (rdstate) {
3779     + free_rdstate(rdstate);
3780     + UNIONFS_F(file)->rdstate = NULL;
3781     + }
3782     + init_rdstate(file);
3783     + err = 0;
3784     + break;
3785     + case SEEK_CUR:
3786     + err = file->f_pos;
3787     + break;
3788     + case SEEK_END:
3789     + /* Unsupported, because we would break everything. */
3790     + err = -EINVAL;
3791     + break;
3792     + }
3793     + } else {
3794     + switch (origin) {
3795     + case SEEK_SET:
3796     + if (rdstate) {
3797     + if (offset == rdstate2offset(rdstate))
3798     + err = offset;
3799     + else if (file->f_pos == DIREOF)
3800     + err = DIREOF;
3801     + else
3802     + err = -EINVAL;
3803     + } else {
3804     + struct inode *inode;
3805     + inode = dentry->d_inode;
3806     + rdstate = find_rdstate(inode, offset);
3807     + if (rdstate) {
3808     + UNIONFS_F(file)->rdstate = rdstate;
3809     + err = rdstate->offset;
3810     + } else {
3811     + err = -EINVAL;
3812     + }
3813     + }
3814     + break;
3815     + case SEEK_CUR:
3816     + case SEEK_END:
3817     + /* Unsupported, because we would break everything. */
3818     + err = -EINVAL;
3819     + break;
3820     + }
3821     + }
3822     +
3823     +out:
3824     + if (!err)
3825     + unionfs_check_file(file);
3826     + unionfs_unlock_dentry(dentry);
3827     + unionfs_unlock_parent(dentry, parent);
3828     + unionfs_read_unlock(dentry->d_sb);
3829     + return err;
3830     +}
3831     +
3832     +/*
3833     + * Trimmed directory options, we shouldn't pass everything down since
3834     + * we don't want to operate on partial directories.
3835     + */
3836     +struct file_operations unionfs_dir_fops = {
3837     + .llseek = unionfs_dir_llseek,
3838     + .read = generic_read_dir,
3839     + .readdir = unionfs_readdir,
3840     + .unlocked_ioctl = unionfs_ioctl,
3841     + .open = unionfs_open,
3842     + .release = unionfs_file_release,
3843     + .flush = unionfs_flush,
3844     + .fsync = unionfs_fsync,
3845     + .fasync = unionfs_fasync,
3846     +};
3847     diff -Naur linux-2.6.29/fs/unionfs/dirhelper.c linux-2.6.29-magellan/fs/unionfs/dirhelper.c
3848     --- linux-2.6.29/fs/unionfs/dirhelper.c 1970-01-01 01:00:00.000000000 +0100
3849     +++ linux-2.6.29-magellan/fs/unionfs/dirhelper.c 2009-04-23 19:41:06.000000000 +0200
3850     @@ -0,0 +1,158 @@
3851     +/*
3852     + * Copyright (c) 2003-2009 Erez Zadok
3853     + * Copyright (c) 2003-2006 Charles P. Wright
3854     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3855     + * Copyright (c) 2005-2006 Junjiro Okajima
3856     + * Copyright (c) 2005 Arun M. Krishnakumar
3857     + * Copyright (c) 2004-2006 David P. Quigley
3858     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3859     + * Copyright (c) 2003 Puja Gupta
3860     + * Copyright (c) 2003 Harikesavan Krishnan
3861     + * Copyright (c) 2003-2009 Stony Brook University
3862     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3863     + *
3864     + * This program is free software; you can redistribute it and/or modify
3865     + * it under the terms of the GNU General Public License version 2 as
3866     + * published by the Free Software Foundation.
3867     + */
3868     +
3869     +#include "union.h"
3870     +
3871     +#define RD_NONE 0
3872     +#define RD_CHECK_EMPTY 1
3873     +/* The callback structure for check_empty. */
3874     +struct unionfs_rdutil_callback {
3875     + int err;
3876     + int filldir_called;
3877     + struct unionfs_dir_state *rdstate;
3878     + int mode;
3879     +};
3880     +
3881     +/* This filldir function makes sure only whiteouts exist within a directory. */
3882     +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3883     + loff_t offset, u64 ino, unsigned int d_type)
3884     +{
3885     + int err = 0;
3886     + struct unionfs_rdutil_callback *buf = dirent;
3887     + int is_whiteout;
3888     + struct filldir_node *found;
3889     + char *name = (char *) oname;
3890     +
3891     + buf->filldir_called = 1;
3892     +
3893     + if (name[0] == '.' && (namelen == 1 ||
3894     + (name[1] == '.' && namelen == 2)))
3895     + goto out;
3896     +
3897     + is_whiteout = is_whiteout_name(&name, &namelen);
3898     +
3899     + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3900     + /* If it was found in the table there was a previous whiteout. */
3901     + if (found)
3902     + goto out;
3903     +
3904     + /*
3905     + * if it wasn't found and isn't a whiteout, the directory isn't
3906     + * empty.
3907     + */
3908     + err = -ENOTEMPTY;
3909     + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
3910     + goto out;
3911     +
3912     + err = add_filldir_node(buf->rdstate, name, namelen,
3913     + buf->rdstate->bindex, is_whiteout);
3914     +
3915     +out:
3916     + buf->err = err;
3917     + return err;
3918     +}
3919     +
3920     +/* Is a directory logically empty? */
3921     +int check_empty(struct dentry *dentry, struct dentry *parent,
3922     + struct unionfs_dir_state **namelist)
3923     +{
3924     + int err = 0;
3925     + struct dentry *lower_dentry = NULL;
3926     + struct vfsmount *mnt;
3927     + struct super_block *sb;
3928     + struct file *lower_file;
3929     + struct unionfs_rdutil_callback *buf = NULL;
3930     + int bindex, bstart, bend, bopaque;
3931     +
3932     + sb = dentry->d_sb;
3933     +
3934     +
3935     + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
3936     +
3937     + err = unionfs_partial_lookup(dentry, parent);
3938     + if (err)
3939     + goto out;
3940     +
3941     + bstart = dbstart(dentry);
3942     + bend = dbend(dentry);
3943     + bopaque = dbopaque(dentry);
3944     + if (0 <= bopaque && bopaque < bend)
3945     + bend = bopaque;
3946     +
3947     + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
3948     + if (unlikely(!buf)) {
3949     + err = -ENOMEM;
3950     + goto out;
3951     + }
3952     + buf->err = 0;
3953     + buf->mode = RD_CHECK_EMPTY;
3954     + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
3955     + if (unlikely(!buf->rdstate)) {
3956     + err = -ENOMEM;
3957     + goto out;
3958     + }
3959     +
3960     + /* Process the lower directories with rdutil_callback as a filldir. */
3961     + for (bindex = bstart; bindex <= bend; bindex++) {
3962     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3963     + if (!lower_dentry)
3964     + continue;
3965     + if (!lower_dentry->d_inode)
3966     + continue;
3967     + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
3968     + continue;
3969     +
3970     + dget(lower_dentry);
3971     + mnt = unionfs_mntget(dentry, bindex);
3972     + branchget(sb, bindex);
3973     + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
3974     + if (IS_ERR(lower_file)) {
3975     + err = PTR_ERR(lower_file);
3976     + branchput(sb, bindex);
3977     + goto out;
3978     + }
3979     +
3980     + do {
3981     + buf->filldir_called = 0;
3982     + buf->rdstate->bindex = bindex;
3983     + err = vfs_readdir(lower_file,
3984     + readdir_util_callback, buf);
3985     + if (buf->err)
3986     + err = buf->err;
3987     + } while ((err >= 0) && buf->filldir_called);
3988     +
3989     + /* fput calls dput for lower_dentry */
3990     + fput(lower_file);
3991     + branchput(sb, bindex);
3992     +
3993     + if (err < 0)
3994     + goto out;
3995     + }
3996     +
3997     +out:
3998     + if (buf) {
3999     + if (namelist && !err)
4000     + *namelist = buf->rdstate;
4001     + else if (buf->rdstate)
4002     + free_rdstate(buf->rdstate);
4003     + kfree(buf);
4004     + }
4005     +
4006     +
4007     + return err;
4008     +}
4009     diff -Naur linux-2.6.29/fs/unionfs/fanout.h linux-2.6.29-magellan/fs/unionfs/fanout.h
4010     --- linux-2.6.29/fs/unionfs/fanout.h 1970-01-01 01:00:00.000000000 +0100
4011     +++ linux-2.6.29-magellan/fs/unionfs/fanout.h 2009-04-23 19:41:06.000000000 +0200
4012     @@ -0,0 +1,407 @@
4013     +/*
4014     + * Copyright (c) 2003-2009 Erez Zadok
4015     + * Copyright (c) 2003-2006 Charles P. Wright
4016     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4017     + * Copyright (c) 2005 Arun M. Krishnakumar
4018     + * Copyright (c) 2004-2006 David P. Quigley
4019     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4020     + * Copyright (c) 2003 Puja Gupta
4021     + * Copyright (c) 2003 Harikesavan Krishnan
4022     + * Copyright (c) 2003-2009 Stony Brook University
4023     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4024     + *
4025     + * This program is free software; you can redistribute it and/or modify
4026     + * it under the terms of the GNU General Public License version 2 as
4027     + * published by the Free Software Foundation.
4028     + */
4029     +
4030     +#ifndef _FANOUT_H_
4031     +#define _FANOUT_H_
4032     +
4033     +/*
4034     + * Inode to private data
4035     + *
4036     + * Since we use containers and the struct inode is _inside_ the
4037     + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4038     + * inode pointer), return a valid non-NULL pointer.
4039     + */
4040     +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4041     +{
4042     + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4043     +}
4044     +
4045     +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4046     +#define ibend(ino) (UNIONFS_I(ino)->bend)
4047     +
4048     +/* Dentry to private data */
4049     +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4050     +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4051     +#define dbend(dent) (UNIONFS_D(dent)->bend)
4052     +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4053     +
4054     +/* Superblock to private data */
4055     +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4056     +#define sbstart(sb) 0
4057     +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4058     +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4059     +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4060     +
4061     +/* File to private Data */
4062     +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4063     +#define fbstart(file) (UNIONFS_F(file)->bstart)
4064     +#define fbend(file) (UNIONFS_F(file)->bend)
4065     +
4066     +/* macros to manipulate branch IDs in stored in our superblock */
4067     +static inline int branch_id(struct super_block *sb, int index)
4068     +{
4069     + BUG_ON(!sb || index < 0);
4070     + return UNIONFS_SB(sb)->data[index].branch_id;
4071     +}
4072     +
4073     +static inline void set_branch_id(struct super_block *sb, int index, int val)
4074     +{
4075     + BUG_ON(!sb || index < 0);
4076     + UNIONFS_SB(sb)->data[index].branch_id = val;
4077     +}
4078     +
4079     +static inline void new_branch_id(struct super_block *sb, int index)
4080     +{
4081     + BUG_ON(!sb || index < 0);
4082     + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4083     +}
4084     +
4085     +/*
4086     + * Find new index of matching branch with an existing superblock of a known
4087     + * (possibly old) id. This is needed because branches could have been
4088     + * added/deleted causing the branches of any open files to shift.
4089     + *
4090     + * @sb: the new superblock which may have new/different branch IDs
4091     + * @id: the old/existing id we're looking for
4092     + * Returns index of newly found branch (0 or greater), -1 otherwise.
4093     + */
4094     +static inline int branch_id_to_idx(struct super_block *sb, int id)
4095     +{
4096     + int i;
4097     + for (i = 0; i < sbmax(sb); i++) {
4098     + if (branch_id(sb, i) == id)
4099     + return i;
4100     + }
4101     + /* in the non-ODF code, this should really never happen */
4102     + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4103     + return -1;
4104     +}
4105     +
4106     +/* File to lower file. */
4107     +static inline struct file *unionfs_lower_file(const struct file *f)
4108     +{
4109     + BUG_ON(!f);
4110     + return UNIONFS_F(f)->lower_files[fbstart(f)];
4111     +}
4112     +
4113     +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4114     + int index)
4115     +{
4116     + BUG_ON(!f || index < 0);
4117     + return UNIONFS_F(f)->lower_files[index];
4118     +}
4119     +
4120     +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4121     + struct file *val)
4122     +{
4123     + BUG_ON(!f || index < 0);
4124     + UNIONFS_F(f)->lower_files[index] = val;
4125     + /* save branch ID (may be redundant?) */
4126     + UNIONFS_F(f)->saved_branch_ids[index] =
4127     + branch_id((f)->f_path.dentry->d_sb, index);
4128     +}
4129     +
4130     +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4131     +{
4132     + BUG_ON(!f);
4133     + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4134     +}
4135     +
4136     +/* Inode to lower inode. */
4137     +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4138     +{
4139     + BUG_ON(!i);
4140     + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4141     +}
4142     +
4143     +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4144     + int index)
4145     +{
4146     + BUG_ON(!i || index < 0);
4147     + return UNIONFS_I(i)->lower_inodes[index];
4148     +}
4149     +
4150     +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4151     + struct inode *val)
4152     +{
4153     + BUG_ON(!i || index < 0);
4154     + UNIONFS_I(i)->lower_inodes[index] = val;
4155     +}
4156     +
4157     +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4158     +{
4159     + BUG_ON(!i);
4160     + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4161     +}
4162     +
4163     +/* Superblock to lower superblock. */
4164     +static inline struct super_block *unionfs_lower_super(
4165     + const struct super_block *sb)
4166     +{
4167     + BUG_ON(!sb);
4168     + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4169     +}
4170     +
4171     +static inline struct super_block *unionfs_lower_super_idx(
4172     + const struct super_block *sb,
4173     + int index)
4174     +{
4175     + BUG_ON(!sb || index < 0);
4176     + return UNIONFS_SB(sb)->data[index].sb;
4177     +}
4178     +
4179     +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4180     + int index,
4181     + struct super_block *val)
4182     +{
4183     + BUG_ON(!sb || index < 0);
4184     + UNIONFS_SB(sb)->data[index].sb = val;
4185     +}
4186     +
4187     +static inline void unionfs_set_lower_super(struct super_block *sb,
4188     + struct super_block *val)
4189     +{
4190     + BUG_ON(!sb);
4191     + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4192     +}
4193     +
4194     +/* Branch count macros. */
4195     +static inline int branch_count(const struct super_block *sb, int index)
4196     +{
4197     + BUG_ON(!sb || index < 0);
4198     + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4199     +}
4200     +
4201     +static inline void set_branch_count(struct super_block *sb, int index, int val)
4202     +{
4203     + BUG_ON(!sb || index < 0);
4204     + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4205     +}
4206     +
4207     +static inline void branchget(struct super_block *sb, int index)
4208     +{
4209     + BUG_ON(!sb || index < 0);
4210     + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4211     +}
4212     +
4213     +static inline void branchput(struct super_block *sb, int index)
4214     +{
4215     + BUG_ON(!sb || index < 0);
4216     + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4217     +}
4218     +
4219     +/* Dentry macros */
4220     +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4221     + struct dentry *val)
4222     +{
4223     + BUG_ON(!dent || index < 0);
4224     + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4225     +}
4226     +
4227     +static inline struct dentry *unionfs_lower_dentry_idx(
4228     + const struct dentry *dent,
4229     + int index)
4230     +{
4231     + BUG_ON(!dent || index < 0);
4232     + return UNIONFS_D(dent)->lower_paths[index].dentry;
4233     +}
4234     +
4235     +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4236     +{
4237     + BUG_ON(!dent);
4238     + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4239     +}
4240     +
4241     +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4242     + struct vfsmount *mnt)
4243     +{
4244     + BUG_ON(!dent || index < 0);
4245     + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4246     +}
4247     +
4248     +static inline struct vfsmount *unionfs_lower_mnt_idx(
4249     + const struct dentry *dent,
4250     + int index)
4251     +{
4252     + BUG_ON(!dent || index < 0);
4253     + return UNIONFS_D(dent)->lower_paths[index].mnt;
4254     +}
4255     +
4256     +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4257     +{
4258     + BUG_ON(!dent);
4259     + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4260     +}
4261     +
4262     +/* Macros for locking a dentry. */
4263     +enum unionfs_dentry_lock_class {
4264     + UNIONFS_DMUTEX_NORMAL,
4265     + UNIONFS_DMUTEX_ROOT,
4266     + UNIONFS_DMUTEX_PARENT,
4267     + UNIONFS_DMUTEX_CHILD,
4268     + UNIONFS_DMUTEX_WHITEOUT,
4269     + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4270     + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4271     +};
4272     +
4273     +static inline void unionfs_lock_dentry(struct dentry *d,
4274     + unsigned int subclass)
4275     +{
4276     + BUG_ON(!d);
4277     + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4278     +}
4279     +
4280     +static inline void unionfs_unlock_dentry(struct dentry *d)
4281     +{
4282     + BUG_ON(!d);
4283     + mutex_unlock(&UNIONFS_D(d)->lock);
4284     +}
4285     +
4286     +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4287     + unsigned int subclass)
4288     +{
4289     + struct dentry *p;
4290     +
4291     + BUG_ON(!d);
4292     + p = dget_parent(d);
4293     + if (p != d)
4294     + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4295     + return p;
4296     +}
4297     +
4298     +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4299     +{
4300     + BUG_ON(!d);
4301     + BUG_ON(!p);
4302     + if (p != d) {
4303     + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4304     + mutex_unlock(&UNIONFS_D(p)->lock);
4305     + }
4306     + dput(p);
4307     +}
4308     +
4309     +static inline void verify_locked(struct dentry *d)
4310     +{
4311     + BUG_ON(!d);
4312     + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4313     +}
4314     +
4315     +/* macros to put lower objects */
4316     +
4317     +/*
4318     + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4319     + * @free_lower is true, then also kfree the memory used to hold the lower
4320     + * object pointers.
4321     + */
4322     +static inline void iput_lowers(struct inode *inode,
4323     + int bstart, int bend, bool free_lower)
4324     +{
4325     + struct inode *lower_inode;
4326     + int bindex;
4327     +
4328     + BUG_ON(!inode);
4329     + BUG_ON(!UNIONFS_I(inode));
4330     + BUG_ON(bstart < 0);
4331     +
4332     + for (bindex = bstart; bindex <= bend; bindex++) {
4333     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4334     + if (lower_inode) {
4335     + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4336     + /* see Documentation/filesystems/unionfs/issues.txt */
4337     + lockdep_off();
4338     + iput(lower_inode);
4339     + lockdep_on();
4340     + }
4341     + }
4342     +
4343     + if (free_lower) {
4344     + kfree(UNIONFS_I(inode)->lower_inodes);
4345     + UNIONFS_I(inode)->lower_inodes = NULL;
4346     + }
4347     +}
4348     +
4349     +/* iput all lower inodes, and reset start/end branch indices to -1 */
4350     +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4351     +{
4352     + int bstart, bend;
4353     +
4354     + BUG_ON(!inode);
4355     + BUG_ON(!UNIONFS_I(inode));
4356     + bstart = ibstart(inode);
4357     + bend = ibend(inode);
4358     + BUG_ON(bstart < 0);
4359     +
4360     + iput_lowers(inode, bstart, bend, free_lower);
4361     + ibstart(inode) = ibend(inode) = -1;
4362     +}
4363     +
4364     +/*
4365     + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4366     + * bstart to bend. If @free_lower is true, then also kfree the memory used
4367     + * to hold the lower object pointers.
4368     + *
4369     + * XXX: implement using path_put VFS macros
4370     + */
4371     +static inline void path_put_lowers(struct dentry *dentry,
4372     + int bstart, int bend, bool free_lower)
4373     +{
4374     + struct dentry *lower_dentry;
4375     + struct vfsmount *lower_mnt;
4376     + int bindex;
4377     +
4378     + BUG_ON(!dentry);
4379     + BUG_ON(!UNIONFS_D(dentry));
4380     + BUG_ON(bstart < 0);
4381     +
4382     + for (bindex = bstart; bindex <= bend; bindex++) {
4383     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4384     + if (lower_dentry) {
4385     + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4386     + dput(lower_dentry);
4387     + }
4388     + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4389     + if (lower_mnt) {
4390     + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4391     + mntput(lower_mnt);
4392     + }
4393     + }
4394     +
4395     + if (free_lower) {
4396     + kfree(UNIONFS_D(dentry)->lower_paths);
4397     + UNIONFS_D(dentry)->lower_paths = NULL;
4398     + }
4399     +}
4400     +
4401     +/*
4402     + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4403     + * indices to -1.
4404     + */
4405     +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4406     +{
4407     + int bstart, bend;
4408     +
4409     + BUG_ON(!dentry);
4410     + BUG_ON(!UNIONFS_D(dentry));
4411     + bstart = dbstart(dentry);
4412     + bend = dbend(dentry);
4413     + BUG_ON(bstart < 0);
4414     +
4415     + path_put_lowers(dentry, bstart, bend, free_lower);
4416     + dbstart(dentry) = dbend(dentry) = -1;
4417     +}
4418     +
4419     +#endif /* not _FANOUT_H */
4420     diff -Naur linux-2.6.29/fs/unionfs/file.c linux-2.6.29-magellan/fs/unionfs/file.c
4421     --- linux-2.6.29/fs/unionfs/file.c 1970-01-01 01:00:00.000000000 +0100
4422     +++ linux-2.6.29-magellan/fs/unionfs/file.c 2009-04-23 19:41:06.000000000 +0200
4423     @@ -0,0 +1,380 @@
4424     +/*
4425     + * Copyright (c) 2003-2009 Erez Zadok
4426     + * Copyright (c) 2003-2006 Charles P. Wright
4427     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4428     + * Copyright (c) 2005-2006 Junjiro Okajima
4429     + * Copyright (c) 2005 Arun M. Krishnakumar
4430     + * Copyright (c) 2004-2006 David P. Quigley
4431     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4432     + * Copyright (c) 2003 Puja Gupta
4433     + * Copyright (c) 2003 Harikesavan Krishnan
4434     + * Copyright (c) 2003-2009 Stony Brook University
4435     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4436     + *
4437     + * This program is free software; you can redistribute it and/or modify
4438     + * it under the terms of the GNU General Public License version 2 as
4439     + * published by the Free Software Foundation.
4440     + */
4441     +
4442     +#include "union.h"
4443     +
4444     +static ssize_t unionfs_read(struct file *file, char __user *buf,
4445     + size_t count, loff_t *ppos)
4446     +{
4447     + int err;
4448     + struct file *lower_file;
4449     + struct dentry *dentry = file->f_path.dentry;
4450     + struct dentry *parent;
4451     +
4452     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4453     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4454     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4455     +
4456     + err = unionfs_file_revalidate(file, parent, false);
4457     + if (unlikely(err))
4458     + goto out;
4459     +
4460     + lower_file = unionfs_lower_file(file);
4461     + err = vfs_read(lower_file, buf, count, ppos);
4462     + /* update our inode atime upon a successful lower read */
4463     + if (err >= 0) {
4464     + fsstack_copy_attr_atime(dentry->d_inode,
4465     + lower_file->f_path.dentry->d_inode);
4466     + unionfs_check_file(file);
4467     + }
4468     +
4469     +out:
4470     + unionfs_unlock_dentry(dentry);
4471     + unionfs_unlock_parent(dentry, parent);
4472     + unionfs_read_unlock(dentry->d_sb);
4473     + return err;
4474     +}
4475     +
4476     +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4477     + size_t count, loff_t *ppos)
4478     +{
4479     + int err = 0;
4480     + struct file *lower_file;
4481     + struct dentry *dentry = file->f_path.dentry;
4482     + struct dentry *parent;
4483     +
4484     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4485     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4486     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4487     +
4488     + err = unionfs_file_revalidate(file, parent, true);
4489     + if (unlikely(err))
4490     + goto out;
4491     +
4492     + lower_file = unionfs_lower_file(file);
4493     + err = vfs_write(lower_file, buf, count, ppos);
4494     + /* update our inode times+sizes upon a successful lower write */
4495     + if (err >= 0) {
4496     + fsstack_copy_inode_size(dentry->d_inode,
4497     + lower_file->f_path.dentry->d_inode);
4498     + fsstack_copy_attr_times(dentry->d_inode,
4499     + lower_file->f_path.dentry->d_inode);
4500     + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4501     + unionfs_check_file(file);
4502     + }
4503     +
4504     +out:
4505     + unionfs_unlock_dentry(dentry);
4506     + unionfs_unlock_parent(dentry, parent);
4507     + unionfs_read_unlock(dentry->d_sb);
4508     + return err;
4509     +}
4510     +
4511     +static int unionfs_file_readdir(struct file *file, void *dirent,
4512     + filldir_t filldir)
4513     +{
4514     + return -ENOTDIR;
4515     +}
4516     +
4517     +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4518     +{
4519     + int err = 0;
4520     + bool willwrite;
4521     + struct file *lower_file;
4522     + struct dentry *dentry = file->f_path.dentry;
4523     + struct dentry *parent;
4524     + struct vm_operations_struct *saved_vm_ops = NULL;
4525     +
4526     + /*
4527     + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4528     + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4529     + * has been causing false positives in file system stacking layers.
4530     + * In particular, our ->mmap is called after sys_mmap2 already holds
4531     + * mmap_sem, then we lock our own mutexes; but earlier, it's
4532     + * possible for lockdep to have locked our mutexes first, and then
4533     + * we call a lower ->readdir which could call might_fault. The
4534     + * different ordering of the locks is what lockdep complains about
4535     + * -- unnecessarily. Therefore, we have no choice but to tell
4536     + * lockdep to temporarily turn off lockdep here. Note: the comments
4537     + * inside might_sleep also suggest that it would have been
4538     + * nicer to only annotate paths that needs that might_lock_read.
4539     + */
4540     + lockdep_off();
4541     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4542     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4543     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4544     +
4545     + /* This might be deferred to mmap's writepage */
4546     + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4547     + err = unionfs_file_revalidate(file, parent, willwrite);
4548     + if (unlikely(err))
4549     + goto out;
4550     + unionfs_check_file(file);
4551     +
4552     + /*
4553     + * File systems which do not implement ->writepage may use
4554     + * generic_file_readonly_mmap as their ->mmap op. If you call
4555     + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4556     + * But we cannot call the lower ->mmap op, so we can't tell that
4557     + * writeable mappings won't work. Therefore, our only choice is to
4558     + * check if the lower file system supports the ->writepage, and if
4559     + * not, return EINVAL (the same error that
4560     + * generic_file_readonly_mmap returns in that case).
4561     + */
4562     + lower_file = unionfs_lower_file(file);
4563     + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4564     + err = -EINVAL;
4565     + printk(KERN_ERR "unionfs: branch %d file system does not "
4566     + "support writeable mmap\n", fbstart(file));
4567     + goto out;
4568     + }
4569     +
4570     + /*
4571     + * find and save lower vm_ops.
4572     + *
4573     + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4574     + */
4575     + if (!UNIONFS_F(file)->lower_vm_ops) {
4576     + err = lower_file->f_op->mmap(lower_file, vma);
4577     + if (err) {
4578     + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4579     + goto out;
4580     + }
4581     + saved_vm_ops = vma->vm_ops;
4582     + err = do_munmap(current->mm, vma->vm_start,
4583     + vma->vm_end - vma->vm_start);
4584     + if (err) {
4585     + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4586     + goto out;
4587     + }
4588     + }
4589     +
4590     + file->f_mapping->a_ops = &unionfs_dummy_aops;
4591     + err = generic_file_mmap(file, vma);
4592     + file->f_mapping->a_ops = &unionfs_aops;
4593     + if (err) {
4594     + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4595     + goto out;
4596     + }
4597     + vma->vm_ops = &unionfs_vm_ops;
4598     + if (!UNIONFS_F(file)->lower_vm_ops)
4599     + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4600     +
4601     +out:
4602     + if (!err) {
4603     + /* copyup could cause parent dir times to change */
4604     + unionfs_copy_attr_times(parent->d_inode);
4605     + unionfs_check_file(file);
4606     + }
4607     + unionfs_unlock_dentry(dentry);
4608     + unionfs_unlock_parent(dentry, parent);
4609     + unionfs_read_unlock(dentry->d_sb);
4610     + lockdep_on();
4611     + return err;
4612     +}
4613     +
4614     +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4615     +{
4616     + int bindex, bstart, bend;
4617     + struct file *lower_file;
4618     + struct dentry *lower_dentry;
4619     + struct dentry *parent;
4620     + struct inode *lower_inode, *inode;
4621     + int err = -EINVAL;
4622     +
4623     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4624     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4625     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4626     +
4627     + err = unionfs_file_revalidate(file, parent, true);
4628     + if (unlikely(err))
4629     + goto out;
4630     + unionfs_check_file(file);
4631     +
4632     + bstart = fbstart(file);
4633     + bend = fbend(file);
4634     + if (bstart < 0 || bend < 0)
4635     + goto out;
4636     +
4637     + inode = dentry->d_inode;
4638     + if (unlikely(!inode)) {
4639     + printk(KERN_ERR
4640     + "unionfs: null lower inode in unionfs_fsync\n");
4641     + goto out;
4642     + }
4643     + for (bindex = bstart; bindex <= bend; bindex++) {
4644     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4645     + if (!lower_inode || !lower_inode->i_fop->fsync)
4646     + continue;
4647     + lower_file = unionfs_lower_file_idx(file, bindex);
4648     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4649     + mutex_lock(&lower_inode->i_mutex);
4650     + err = lower_inode->i_fop->fsync(lower_file,
4651     + lower_dentry,
4652     + datasync);
4653     + if (!err && bindex == bstart)
4654     + fsstack_copy_attr_times(inode, lower_inode);
4655     + mutex_unlock(&lower_inode->i_mutex);
4656     + if (err)
4657     + goto out;
4658     + }
4659     +
4660     +out:
4661     + if (!err)
4662     + unionfs_check_file(file);
4663     + unionfs_unlock_dentry(dentry);
4664     + unionfs_unlock_parent(dentry, parent);
4665     + unionfs_read_unlock(dentry->d_sb);
4666     + return err;
4667     +}
4668     +
4669     +int unionfs_fasync(int fd, struct file *file, int flag)
4670     +{
4671     + int bindex, bstart, bend;
4672     + struct file *lower_file;
4673     + struct dentry *dentry = file->f_path.dentry;
4674     + struct dentry *parent;
4675     + struct inode *lower_inode, *inode;
4676     + int err = 0;
4677     +
4678     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4679     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4680     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4681     +
4682     + err = unionfs_file_revalidate(file, parent, true);
4683     + if (unlikely(err))
4684     + goto out;
4685     + unionfs_check_file(file);
4686     +
4687     + bstart = fbstart(file);
4688     + bend = fbend(file);
4689     + if (bstart < 0 || bend < 0)
4690     + goto out;
4691     +
4692     + inode = dentry->d_inode;
4693     + if (unlikely(!inode)) {
4694     + printk(KERN_ERR
4695     + "unionfs: null lower inode in unionfs_fasync\n");
4696     + goto out;
4697     + }
4698     + for (bindex = bstart; bindex <= bend; bindex++) {
4699     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4700     + if (!lower_inode || !lower_inode->i_fop->fasync)
4701     + continue;
4702     + lower_file = unionfs_lower_file_idx(file, bindex);
4703     + mutex_lock(&lower_inode->i_mutex);
4704     + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4705     + if (!err && bindex == bstart)
4706     + fsstack_copy_attr_times(inode, lower_inode);
4707     + mutex_unlock(&lower_inode->i_mutex);
4708     + if (err)
4709     + goto out;
4710     + }
4711     +
4712     +out:
4713     + if (!err)
4714     + unionfs_check_file(file);
4715     + unionfs_unlock_dentry(dentry);
4716     + unionfs_unlock_parent(dentry, parent);
4717     + unionfs_read_unlock(dentry->d_sb);
4718     + return err;
4719     +}
4720     +
4721     +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4722     + struct pipe_inode_info *pipe, size_t len,
4723     + unsigned int flags)
4724     +{
4725     + ssize_t err;
4726     + struct file *lower_file;
4727     + struct dentry *dentry = file->f_path.dentry;
4728     + struct dentry *parent;
4729     +
4730     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4731     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4732     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4733     +
4734     + err = unionfs_file_revalidate(file, parent, false);
4735     + if (unlikely(err))
4736     + goto out;
4737     +
4738     + lower_file = unionfs_lower_file(file);
4739     + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4740     + /* update our inode atime upon a successful lower splice-read */
4741     + if (err >= 0) {
4742     + fsstack_copy_attr_atime(dentry->d_inode,
4743     + lower_file->f_path.dentry->d_inode);
4744     + unionfs_check_file(file);
4745     + }
4746     +
4747     +out:
4748     + unionfs_unlock_dentry(dentry);
4749     + unionfs_unlock_parent(dentry, parent);
4750     + unionfs_read_unlock(dentry->d_sb);
4751     + return err;
4752     +}
4753     +
4754     +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4755     + struct file *file, loff_t *ppos,
4756     + size_t len, unsigned int flags)
4757     +{
4758     + ssize_t err = 0;
4759     + struct file *lower_file;
4760     + struct dentry *dentry = file->f_path.dentry;
4761     + struct dentry *parent;
4762     +
4763     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4764     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4765     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4766     +
4767     + err = unionfs_file_revalidate(file, parent, true);
4768     + if (unlikely(err))
4769     + goto out;
4770     +
4771     + lower_file = unionfs_lower_file(file);
4772     + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4773     + /* update our inode times+sizes upon a successful lower write */
4774     + if (err >= 0) {
4775     + fsstack_copy_inode_size(dentry->d_inode,
4776     + lower_file->f_path.dentry->d_inode);
4777     + fsstack_copy_attr_times(dentry->d_inode,
4778     + lower_file->f_path.dentry->d_inode);
4779     + unionfs_check_file(file);
4780     + }
4781     +
4782     +out:
4783     + unionfs_unlock_dentry(dentry);
4784     + unionfs_unlock_parent(dentry, parent);
4785     + unionfs_read_unlock(dentry->d_sb);
4786     + return err;
4787     +}
4788     +
4789     +struct file_operations unionfs_main_fops = {
4790     + .llseek = generic_file_llseek,
4791     + .read = unionfs_read,
4792     + .write = unionfs_write,
4793     + .readdir = unionfs_file_readdir,
4794     + .unlocked_ioctl = unionfs_ioctl,
4795     + .mmap = unionfs_mmap,
4796     + .open = unionfs_open,
4797     + .flush = unionfs_flush,
4798     + .release = unionfs_file_release,
4799     + .fsync = unionfs_fsync,
4800     + .fasync = unionfs_fasync,
4801     + .splice_read = unionfs_splice_read,
4802     + .splice_write = unionfs_splice_write,
4803     +};
4804     diff -Naur linux-2.6.29/fs/unionfs/inode.c linux-2.6.29-magellan/fs/unionfs/inode.c
4805     --- linux-2.6.29/fs/unionfs/inode.c 1970-01-01 01:00:00.000000000 +0100
4806     +++ linux-2.6.29-magellan/fs/unionfs/inode.c 2009-04-23 19:41:06.000000000 +0200
4807     @@ -0,0 +1,1035 @@
4808     +/*
4809     + * Copyright (c) 2003-2009 Erez Zadok
4810     + * Copyright (c) 2003-2006 Charles P. Wright
4811     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4812     + * Copyright (c) 2005-2006 Junjiro Okajima
4813     + * Copyright (c) 2005 Arun M. Krishnakumar
4814     + * Copyright (c) 2004-2006 David P. Quigley
4815     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4816     + * Copyright (c) 2003 Puja Gupta
4817     + * Copyright (c) 2003 Harikesavan Krishnan
4818     + * Copyright (c) 2003-2009 Stony Brook University
4819     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4820     + *
4821     + * This program is free software; you can redistribute it and/or modify
4822     + * it under the terms of the GNU General Public License version 2 as
4823     + * published by the Free Software Foundation.
4824     + */
4825     +
4826     +#include "union.h"
4827     +
4828     +/*
4829     + * Find a writeable branch to create new object in. Checks all writeble
4830     + * branches of the parent inode, from istart to iend order; if none are
4831     + * suitable, also tries branch 0 (which may require a copyup).
4832     + *
4833     + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4834     + */
4835     +static struct dentry *find_writeable_branch(struct inode *parent,
4836     + struct dentry *dentry)
4837     +{
4838     + int err = -EINVAL;
4839     + int bindex, istart, iend;
4840     + struct dentry *lower_dentry = NULL;
4841     +
4842     + istart = ibstart(parent);
4843     + iend = ibend(parent);
4844     + if (istart < 0)
4845     + goto out;
4846     +
4847     +begin:
4848     + for (bindex = istart; bindex <= iend; bindex++) {
4849     + /* skip non-writeable branches */
4850     + err = is_robranch_super(dentry->d_sb, bindex);
4851     + if (err) {
4852     + err = -EROFS;
4853     + continue;
4854     + }
4855     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4856     + if (!lower_dentry)
4857     + continue;
4858     + /*
4859     + * check for whiteouts in writeable branch, and remove them
4860     + * if necessary.
4861     + */
4862     + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4863     + if (err > 0) /* ignore if whiteout found and removed */
4864     + err = 0;
4865     + if (err)
4866     + continue;
4867     + /* if get here, we can write to the branch */
4868     + break;
4869     + }
4870     + /*
4871     + * If istart wasn't already branch 0, and we got any error, then try
4872     + * branch 0 (which may require copyup)
4873     + */
4874     + if (err && istart > 0) {
4875     + istart = iend = 0;
4876     + goto begin;
4877     + }
4878     +
4879     + /*
4880     + * If we tried even branch 0, and still got an error, abort. But if
4881     + * the error was an EROFS, then we should try to copyup.
4882     + */
4883     + if (err && err != -EROFS)
4884     + goto out;
4885     +
4886     + /*
4887     + * If we get here, then check if copyup needed. If lower_dentry is
4888     + * NULL, create the entire dentry directory structure in branch 0.
4889     + */
4890     + if (!lower_dentry) {
4891     + bindex = 0;
4892     + lower_dentry = create_parents(parent, dentry,
4893     + dentry->d_name.name, bindex);
4894     + if (IS_ERR(lower_dentry)) {
4895     + err = PTR_ERR(lower_dentry);
4896     + goto out;
4897     + }
4898     + }
4899     + err = 0; /* all's well */
4900     +out:
4901     + if (err)
4902     + return ERR_PTR(err);
4903     + return lower_dentry;
4904     +}
4905     +
4906     +static int unionfs_create(struct inode *dir, struct dentry *dentry,
4907     + int mode, struct nameidata *nd_unused)
4908     +{
4909     + int err = 0;
4910     + struct dentry *lower_dentry = NULL;
4911     + struct dentry *lower_parent_dentry = NULL;
4912     + struct dentry *parent;
4913     + int valid = 0;
4914     + struct nameidata lower_nd;
4915     +
4916     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4917     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4918     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4919     +
4920     + valid = __unionfs_d_revalidate(dentry, parent, false);
4921     + if (unlikely(!valid)) {
4922     + err = -ESTALE; /* same as what real_lookup does */
4923     + goto out;
4924     + }
4925     +
4926     + lower_dentry = find_writeable_branch(dir, dentry);
4927     + if (IS_ERR(lower_dentry)) {
4928     + err = PTR_ERR(lower_dentry);
4929     + goto out;
4930     + }
4931     +
4932     + lower_parent_dentry = lock_parent(lower_dentry);
4933     + if (IS_ERR(lower_parent_dentry)) {
4934     + err = PTR_ERR(lower_parent_dentry);
4935     + goto out;
4936     + }
4937     +
4938     + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
4939     + if (unlikely(err < 0))
4940     + goto out;
4941     + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
4942     + &lower_nd);
4943     + release_lower_nd(&lower_nd, err);
4944     +
4945     + if (!err) {
4946     + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
4947     + if (!err) {
4948     + unionfs_copy_attr_times(dir);
4949     + fsstack_copy_inode_size(dir,
4950     + lower_parent_dentry->d_inode);
4951     + /* update no. of links on parent directory */
4952     + dir->i_nlink = unionfs_get_nlinks(dir);
4953     + }
4954     + }
4955     +
4956     + unlock_dir(lower_parent_dentry);
4957     +
4958     +out:
4959     + if (!err) {
4960     + unionfs_postcopyup_setmnt(dentry);
4961     + unionfs_check_inode(dir);
4962     + unionfs_check_dentry(dentry);
4963     + }
4964     + unionfs_unlock_dentry(dentry);
4965     + unionfs_unlock_parent(dentry, parent);
4966     + unionfs_read_unlock(dentry->d_sb);
4967     + return err;
4968     +}
4969     +
4970     +/*
4971     + * unionfs_lookup is the only special function which takes a dentry, yet we
4972     + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
4973     + * we don't have a valid dentry here yet.
4974     + */
4975     +static struct dentry *unionfs_lookup(struct inode *dir,
4976     + struct dentry *dentry,
4977     + struct nameidata *nd_unused)
4978     +{
4979     + struct dentry *ret, *parent;
4980     + int err = 0;
4981     +
4982     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
4983     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4984     +
4985     + /*
4986     + * As long as we lock/dget the parent, then can skip validating the
4987     + * parent now; we may have to rebuild this dentry on the next
4988     + * ->d_revalidate, however.
4989     + */
4990     +
4991     + /* allocate dentry private data. We free it in ->d_release */
4992     + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
4993     + if (unlikely(err)) {
4994     + ret = ERR_PTR(err);
4995     + goto out;
4996     + }
4997     +
4998     + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
4999     +
5000     + if (!IS_ERR(ret)) {
5001     + if (ret)
5002     + dentry = ret;
5003     + /* lookup_full can return multiple positive dentries */
5004     + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5005     + BUG_ON(dbstart(dentry) < 0);
5006     + unionfs_postcopyup_release(dentry);
5007     + }
5008     + unionfs_copy_attr_times(dentry->d_inode);
5009     + }
5010     +
5011     + unionfs_check_inode(dir);
5012     + if (!IS_ERR(ret))
5013     + unionfs_check_dentry(dentry);
5014     + unionfs_check_dentry(parent);
5015     + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5016     +
5017     +out:
5018     + unionfs_unlock_parent(dentry, parent);
5019     + unionfs_read_unlock(dentry->d_sb);
5020     +
5021     + return ret;
5022     +}
5023     +
5024     +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5025     + struct dentry *new_dentry)
5026     +{
5027     + int err = 0;
5028     + struct dentry *lower_old_dentry = NULL;
5029     + struct dentry *lower_new_dentry = NULL;
5030     + struct dentry *lower_dir_dentry = NULL;
5031     + struct dentry *old_parent, *new_parent;
5032     + char *name = NULL;
5033     + bool valid;
5034     +
5035     + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5036     + old_parent = dget_parent(old_dentry);
5037     + new_parent = dget_parent(new_dentry);
5038     + unionfs_double_lock_parents(old_parent, new_parent);
5039     + unionfs_double_lock_dentry(old_dentry, new_dentry);
5040     +
5041     + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5042     + if (unlikely(!valid)) {
5043     + err = -ESTALE;
5044     + goto out;
5045     + }
5046     + if (new_dentry->d_inode) {
5047     + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5048     + if (unlikely(!valid)) {
5049     + err = -ESTALE;
5050     + goto out;
5051     + }
5052     + }
5053     +
5054     + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5055     +
5056     + /* check for a whiteout in new dentry branch, and delete it */
5057     + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5058     + dbstart(new_dentry));
5059     + if (err > 0) { /* whiteout found and removed successfully */
5060     + lower_dir_dentry = dget_parent(lower_new_dentry);
5061     + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5062     + dput(lower_dir_dentry);
5063     + dir->i_nlink = unionfs_get_nlinks(dir);
5064     + err = 0;
5065     + }
5066     + if (err)
5067     + goto out;
5068     +
5069     + /* check if parent hierachy is needed, then link in same branch */
5070     + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5071     + lower_new_dentry = create_parents(dir, new_dentry,
5072     + new_dentry->d_name.name,
5073     + dbstart(old_dentry));
5074     + err = PTR_ERR(lower_new_dentry);
5075     + if (IS_COPYUP_ERR(err))
5076     + goto docopyup;
5077     + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5078     + goto out;
5079     + }
5080     + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5081     + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5082     +
5083     + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5084     + lower_dir_dentry = lock_parent(lower_new_dentry);
5085     + err = is_robranch(old_dentry);
5086     + if (!err) {
5087     + /* see Documentation/filesystems/unionfs/issues.txt */
5088     + lockdep_off();
5089     + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5090     + lower_new_dentry);
5091     + lockdep_on();
5092     + }
5093     + unlock_dir(lower_dir_dentry);
5094     +
5095     +docopyup:
5096     + if (IS_COPYUP_ERR(err)) {
5097     + int old_bstart = dbstart(old_dentry);
5098     + int bindex;
5099     +
5100     + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5101     + err = copyup_dentry(old_parent->d_inode,
5102     + old_dentry, old_bstart,
5103     + bindex, old_dentry->d_name.name,
5104     + old_dentry->d_name.len, NULL,
5105     + i_size_read(old_dentry->d_inode));
5106     + if (err)
5107     + continue;
5108     + lower_new_dentry =
5109     + create_parents(dir, new_dentry,
5110     + new_dentry->d_name.name,
5111     + bindex);
5112     + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5113     + lower_dir_dentry = lock_parent(lower_new_dentry);
5114     + /* see Documentation/filesystems/unionfs/issues.txt */
5115     + lockdep_off();
5116     + /* do vfs_link */
5117     + err = vfs_link(lower_old_dentry,
5118     + lower_dir_dentry->d_inode,
5119     + lower_new_dentry);
5120     + lockdep_on();
5121     + unlock_dir(lower_dir_dentry);
5122     + goto check_link;
5123     + }
5124     + goto out;
5125     + }
5126     +
5127     +check_link:
5128     + if (err || !lower_new_dentry->d_inode)
5129     + goto out;
5130     +
5131     + /* Its a hard link, so use the same inode */
5132     + new_dentry->d_inode = igrab(old_dentry->d_inode);
5133     + d_add(new_dentry, new_dentry->d_inode);
5134     + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5135     + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5136     +
5137     + /* propagate number of hard-links */
5138     + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5139     + /* new dentry's ctime may have changed due to hard-link counts */
5140     + unionfs_copy_attr_times(new_dentry->d_inode);
5141     +
5142     +out:
5143     + if (!new_dentry->d_inode)
5144     + d_drop(new_dentry);
5145     +
5146     + kfree(name);
5147     + if (!err)
5148     + unionfs_postcopyup_setmnt(new_dentry);
5149     +
5150     + unionfs_check_inode(dir);
5151     + unionfs_check_dentry(new_dentry);
5152     + unionfs_check_dentry(old_dentry);
5153     +
5154     + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5155     + unionfs_double_unlock_parents(old_parent, new_parent);
5156     + dput(new_parent);
5157     + dput(old_parent);
5158     + unionfs_read_unlock(old_dentry->d_sb);
5159     +
5160     + return err;
5161     +}
5162     +
5163     +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5164     + const char *symname)
5165     +{
5166     + int err = 0;
5167     + struct dentry *lower_dentry = NULL;
5168     + struct dentry *wh_dentry = NULL;
5169     + struct dentry *lower_parent_dentry = NULL;
5170     + struct dentry *parent;
5171     + char *name = NULL;
5172     + int valid = 0;
5173     + umode_t mode;
5174     +
5175     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5176     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5177     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5178     +
5179     + valid = __unionfs_d_revalidate(dentry, parent, false);
5180     + if (unlikely(!valid)) {
5181     + err = -ESTALE;
5182     + goto out;
5183     + }
5184     +
5185     + /*
5186     + * It's only a bug if this dentry was not negative and couldn't be
5187     + * revalidated (shouldn't happen).
5188     + */
5189     + BUG_ON(!valid && dentry->d_inode);
5190     +
5191     + lower_dentry = find_writeable_branch(dir, dentry);
5192     + if (IS_ERR(lower_dentry)) {
5193     + err = PTR_ERR(lower_dentry);
5194     + goto out;
5195     + }
5196     +
5197     + lower_parent_dentry = lock_parent(lower_dentry);
5198     + if (IS_ERR(lower_parent_dentry)) {
5199     + err = PTR_ERR(lower_parent_dentry);
5200     + goto out;
5201     + }
5202     +
5203     + mode = S_IALLUGO;
5204     + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5205     + if (!err) {
5206     + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5207     + if (!err) {
5208     + unionfs_copy_attr_times(dir);
5209     + fsstack_copy_inode_size(dir,
5210     + lower_parent_dentry->d_inode);
5211     + /* update no. of links on parent directory */
5212     + dir->i_nlink = unionfs_get_nlinks(dir);
5213     + }
5214     + }
5215     +
5216     + unlock_dir(lower_parent_dentry);
5217     +
5218     +out:
5219     + dput(wh_dentry);
5220     + kfree(name);
5221     +
5222     + if (!err) {
5223     + unionfs_postcopyup_setmnt(dentry);
5224     + unionfs_check_inode(dir);
5225     + unionfs_check_dentry(dentry);
5226     + }
5227     + unionfs_unlock_dentry(dentry);
5228     + unionfs_unlock_parent(dentry, parent);
5229     + unionfs_read_unlock(dentry->d_sb);
5230     + return err;
5231     +}
5232     +
5233     +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5234     +{
5235     + int err = 0;
5236     + struct dentry *lower_dentry = NULL;
5237     + struct dentry *lower_parent_dentry = NULL;
5238     + struct dentry *parent;
5239     + int bindex = 0, bstart;
5240     + char *name = NULL;
5241     + int valid;
5242     +
5243     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5244     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5245     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5246     +
5247     + valid = __unionfs_d_revalidate(dentry, parent, false);
5248     + if (unlikely(!valid)) {
5249     + err = -ESTALE; /* same as what real_lookup does */
5250     + goto out;
5251     + }
5252     +
5253     + bstart = dbstart(dentry);
5254     +
5255     + lower_dentry = unionfs_lower_dentry(dentry);
5256     +
5257     + /* check for a whiteout in new dentry branch, and delete it */
5258     + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5259     + if (err > 0) /* whiteout found and removed successfully */
5260     + err = 0;
5261     + if (err) {
5262     + /* exit if the error returned was NOT -EROFS */
5263     + if (!IS_COPYUP_ERR(err))
5264     + goto out;
5265     + bstart--;
5266     + }
5267     +
5268     + /* check if copyup's needed, and mkdir */
5269     + for (bindex = bstart; bindex >= 0; bindex--) {
5270     + int i;
5271     + int bend = dbend(dentry);
5272     +
5273     + if (is_robranch_super(dentry->d_sb, bindex))
5274     + continue;
5275     +
5276     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5277     + if (!lower_dentry) {
5278     + lower_dentry = create_parents(dir, dentry,
5279     + dentry->d_name.name,
5280     + bindex);
5281     + if (!lower_dentry || IS_ERR(lower_dentry)) {
5282     + printk(KERN_ERR "unionfs: lower dentry "
5283     + " NULL for bindex = %d\n", bindex);
5284     + continue;
5285     + }
5286     + }
5287     +
5288     + lower_parent_dentry = lock_parent(lower_dentry);
5289     +
5290     + if (IS_ERR(lower_parent_dentry)) {
5291     + err = PTR_ERR(lower_parent_dentry);
5292     + goto out;
5293     + }
5294     +
5295     + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5296     + mode);
5297     +
5298     + unlock_dir(lower_parent_dentry);
5299     +
5300     + /* did the mkdir succeed? */
5301     + if (err)
5302     + break;
5303     +
5304     + for (i = bindex + 1; i <= bend; i++) {
5305     + /* XXX: use path_put_lowers? */
5306     + if (unionfs_lower_dentry_idx(dentry, i)) {
5307     + dput(unionfs_lower_dentry_idx(dentry, i));
5308     + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5309     + }
5310     + }
5311     + dbend(dentry) = bindex;
5312     +
5313     + /*
5314     + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5315     + * err.
5316     + */
5317     + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5318     + if (!err) {
5319     + unionfs_copy_attr_times(dir);
5320     + fsstack_copy_inode_size(dir,
5321     + lower_parent_dentry->d_inode);
5322     +
5323     + /* update number of links on parent directory */
5324     + dir->i_nlink = unionfs_get_nlinks(dir);
5325     + }
5326     +
5327     + err = make_dir_opaque(dentry, dbstart(dentry));
5328     + if (err) {
5329     + printk(KERN_ERR "unionfs: mkdir: error creating "
5330     + ".wh.__dir_opaque: %d\n", err);
5331     + goto out;
5332     + }
5333     +
5334     + /* we are done! */
5335     + break;
5336     + }
5337     +
5338     +out:
5339     + if (!dentry->d_inode)
5340     + d_drop(dentry);
5341     +
5342     + kfree(name);
5343     +
5344     + if (!err) {
5345     + unionfs_copy_attr_times(dentry->d_inode);
5346     + unionfs_postcopyup_setmnt(dentry);
5347     + }
5348     + unionfs_check_inode(dir);
5349     + unionfs_check_dentry(dentry);
5350     + unionfs_unlock_dentry(dentry);
5351     + unionfs_unlock_parent(dentry, parent);
5352     + unionfs_read_unlock(dentry->d_sb);
5353     +
5354     + return err;
5355     +}
5356     +
5357     +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5358     + dev_t dev)
5359     +{
5360     + int err = 0;
5361     + struct dentry *lower_dentry = NULL;
5362     + struct dentry *wh_dentry = NULL;
5363     + struct dentry *lower_parent_dentry = NULL;
5364     + struct dentry *parent;
5365     + char *name = NULL;
5366     + int valid = 0;
5367     +
5368     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5369     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5370     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5371     +
5372     + valid = __unionfs_d_revalidate(dentry, parent, false);
5373     + if (unlikely(!valid)) {
5374     + err = -ESTALE;
5375     + goto out;
5376     + }
5377     +
5378     + /*
5379     + * It's only a bug if this dentry was not negative and couldn't be
5380     + * revalidated (shouldn't happen).
5381     + */
5382     + BUG_ON(!valid && dentry->d_inode);
5383     +
5384     + lower_dentry = find_writeable_branch(dir, dentry);
5385     + if (IS_ERR(lower_dentry)) {
5386     + err = PTR_ERR(lower_dentry);
5387     + goto out;
5388     + }
5389     +
5390     + lower_parent_dentry = lock_parent(lower_dentry);
5391     + if (IS_ERR(lower_parent_dentry)) {
5392     + err = PTR_ERR(lower_parent_dentry);
5393     + goto out;
5394     + }
5395     +
5396     + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5397     + if (!err) {
5398     + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5399     + if (!err) {
5400     + unionfs_copy_attr_times(dir);
5401     + fsstack_copy_inode_size(dir,
5402     + lower_parent_dentry->d_inode);
5403     + /* update no. of links on parent directory */
5404     + dir->i_nlink = unionfs_get_nlinks(dir);
5405     + }
5406     + }
5407     +
5408     + unlock_dir(lower_parent_dentry);
5409     +
5410     +out:
5411     + dput(wh_dentry);
5412     + kfree(name);
5413     +
5414     + if (!err) {
5415     + unionfs_postcopyup_setmnt(dentry);
5416     + unionfs_check_inode(dir);
5417     + unionfs_check_dentry(dentry);
5418     + }
5419     + unionfs_unlock_dentry(dentry);
5420     + unionfs_unlock_parent(dentry, parent);
5421     + unionfs_read_unlock(dentry->d_sb);
5422     + return err;
5423     +}
5424     +
5425     +/* requires sb, dentry, and parent to already be locked */
5426     +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5427     + int bufsiz)
5428     +{
5429     + int err;
5430     + struct dentry *lower_dentry;
5431     +
5432     + lower_dentry = unionfs_lower_dentry(dentry);
5433     +
5434     + if (!lower_dentry->d_inode->i_op ||
5435     + !lower_dentry->d_inode->i_op->readlink) {
5436     + err = -EINVAL;
5437     + goto out;
5438     + }
5439     +
5440     + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5441     + buf, bufsiz);
5442     + if (err >= 0)
5443     + fsstack_copy_attr_atime(dentry->d_inode,
5444     + lower_dentry->d_inode);
5445     +
5446     +out:
5447     + return err;
5448     +}
5449     +
5450     +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5451     + int bufsiz)
5452     +{
5453     + int err;
5454     + struct dentry *parent;
5455     +
5456     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5457     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5458     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5459     +
5460     + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5461     + err = -ESTALE;
5462     + goto out;
5463     + }
5464     +
5465     + err = __unionfs_readlink(dentry, buf, bufsiz);
5466     +
5467     +out:
5468     + unionfs_check_dentry(dentry);
5469     + unionfs_unlock_dentry(dentry);
5470     + unionfs_unlock_parent(dentry, parent);
5471     + unionfs_read_unlock(dentry->d_sb);
5472     +
5473     + return err;
5474     +}
5475     +
5476     +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5477     +{
5478     + char *buf;
5479     + int len = PAGE_SIZE, err;
5480     + mm_segment_t old_fs;
5481     + struct dentry *parent;
5482     +
5483     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5484     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5485     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5486     +
5487     + /* This is freed by the put_link method assuming a successful call. */
5488     + buf = kmalloc(len, GFP_KERNEL);
5489     + if (unlikely(!buf)) {
5490     + err = -ENOMEM;
5491     + goto out;
5492     + }
5493     +
5494     + /* read the symlink, and then we will follow it */
5495     + old_fs = get_fs();
5496     + set_fs(KERNEL_DS);
5497     + err = __unionfs_readlink(dentry, buf, len);
5498     + set_fs(old_fs);
5499     + if (err < 0) {
5500     + kfree(buf);
5501     + buf = NULL;
5502     + goto out;
5503     + }
5504     + buf[err] = 0;
5505     + nd_set_link(nd, buf);
5506     + err = 0;
5507     +
5508     +out:
5509     + if (err >= 0) {
5510     + unionfs_check_nd(nd);
5511     + unionfs_check_dentry(dentry);
5512     + }
5513     +
5514     + unionfs_unlock_dentry(dentry);
5515     + unionfs_unlock_parent(dentry, parent);
5516     + unionfs_read_unlock(dentry->d_sb);
5517     +
5518     + return ERR_PTR(err);
5519     +}
5520     +
5521     +/* this @nd *IS* still used */
5522     +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5523     + void *cookie)
5524     +{
5525     + struct dentry *parent;
5526     +
5527     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5528     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5529     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5530     +
5531     + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5532     + printk(KERN_ERR
5533     + "unionfs: put_link failed to revalidate dentry\n");
5534     +
5535     + unionfs_check_dentry(dentry);
5536     + unionfs_check_nd(nd);
5537     + kfree(nd_get_link(nd));
5538     + unionfs_unlock_dentry(dentry);
5539     + unionfs_unlock_parent(dentry, parent);
5540     + unionfs_read_unlock(dentry->d_sb);
5541     +}
5542     +
5543     +/*
5544     + * This is a variant of fs/namei.c:permission() or inode_permission() which
5545     + * skips over EROFS tests (because we perform copyup on EROFS).
5546     + */
5547     +static int __inode_permission(struct inode *inode, int mask)
5548     +{
5549     + int retval;
5550     +
5551     + /* nobody gets write access to an immutable file */
5552     + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5553     + return -EACCES;
5554     +
5555     + /* Ordinary permission routines do not understand MAY_APPEND. */
5556     + if (inode->i_op && inode->i_op->permission) {
5557     + retval = inode->i_op->permission(inode, mask);
5558     + if (!retval) {
5559     + /*
5560     + * Exec permission on a regular file is denied if none
5561     + * of the execute bits are set.
5562     + *
5563     + * This check should be done by the ->permission()
5564     + * method.
5565     + */
5566     + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5567     + !(inode->i_mode & S_IXUGO))
5568     + return -EACCES;
5569     + }
5570     + } else {
5571     + retval = generic_permission(inode, mask, NULL);
5572     + }
5573     + if (retval)
5574     + return retval;
5575     +
5576     + return security_inode_permission(inode,
5577     + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5578     +}
5579     +
5580     +/*
5581     + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5582     + * a deadlock with the branch-management "add branch" code (which grabbed
5583     + * the write lock). It is safe to not grab the read lock here, because even
5584     + * with branch management taking place, there is no chance that
5585     + * unionfs_permission, or anything it calls, will use stale branch
5586     + * information.
5587     + */
5588     +static int unionfs_permission(struct inode *inode, int mask)
5589     +{
5590     + struct inode *lower_inode = NULL;
5591     + int err = 0;
5592     + int bindex, bstart, bend;
5593     + const int is_file = !S_ISDIR(inode->i_mode);
5594     + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5595     + struct inode *inode_grabbed = igrab(inode);
5596     + struct dentry *dentry = d_find_alias(inode);
5597     +
5598     + if (dentry)
5599     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5600     +
5601     + if (!UNIONFS_I(inode)->lower_inodes) {
5602     + if (is_file) /* dirs can be unlinked but chdir'ed to */
5603     + err = -ESTALE; /* force revalidate */
5604     + goto out;
5605     + }
5606     + bstart = ibstart(inode);
5607     + bend = ibend(inode);
5608     + if (unlikely(bstart < 0 || bend < 0)) {
5609     + /*
5610     + * With branch-management, we can get a stale inode here.
5611     + * If so, we return ESTALE back to link_path_walk, which
5612     + * would discard the dcache entry and re-lookup the
5613     + * dentry+inode. This should be equivalent to issuing
5614     + * __unionfs_d_revalidate_chain on nd.dentry here.
5615     + */
5616     + if (is_file) /* dirs can be unlinked but chdir'ed to */
5617     + err = -ESTALE; /* force revalidate */
5618     + goto out;
5619     + }
5620     +
5621     + for (bindex = bstart; bindex <= bend; bindex++) {
5622     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5623     + if (!lower_inode)
5624     + continue;
5625     +
5626     + /*
5627     + * check the condition for D-F-D underlying files/directories,
5628     + * we don't have to check for files, if we are checking for
5629     + * directories.
5630     + */
5631     + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5632     + continue;
5633     +
5634     + /*
5635     + * We check basic permissions, but we ignore any conditions
5636     + * such as readonly file systems or branches marked as
5637     + * readonly, because those conditions should lead to a
5638     + * copyup taking place later on. However, if user never had
5639     + * access to the file, then no copyup could ever take place.
5640     + */
5641     + err = __inode_permission(lower_inode, mask);
5642     + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5643     + umode_t mode = lower_inode->i_mode;
5644     + if ((is_robranch_super(inode->i_sb, bindex) ||
5645     + IS_RDONLY(lower_inode)) &&
5646     + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5647     + err = 0;
5648     + if (IS_COPYUP_ERR(err))
5649     + err = 0;
5650     + }
5651     +
5652     + /*
5653     + * The permissions are an intersection of the overall directory
5654     + * permissions, so we fail if one fails.
5655     + */
5656     + if (err)
5657     + goto out;
5658     +
5659     + /* only the leftmost file matters. */
5660     + if (is_file || write_mask) {
5661     + if (is_file && write_mask) {
5662     + err = get_write_access(lower_inode);
5663     + if (!err)
5664     + put_write_access(lower_inode);
5665     + }
5666     + break;
5667     + }
5668     + }
5669     + /* sync times which may have changed (asynchronously) below */
5670     + unionfs_copy_attr_times(inode);
5671     +
5672     +out:
5673     + unionfs_check_inode(inode);
5674     + if (dentry) {
5675     + unionfs_unlock_dentry(dentry);
5676     + dput(dentry);
5677     + }
5678     + iput(inode_grabbed);
5679     + return err;
5680     +}
5681     +
5682     +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5683     +{
5684     + int err = 0;
5685     + struct dentry *lower_dentry;
5686     + struct dentry *parent;
5687     + struct inode *inode;
5688     + struct inode *lower_inode;
5689     + int bstart, bend, bindex;
5690     + loff_t size;
5691     +
5692     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5693     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5694     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5695     +
5696     + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5697     + err = -ESTALE;
5698     + goto out;
5699     + }
5700     +
5701     + bstart = dbstart(dentry);
5702     + bend = dbend(dentry);
5703     + inode = dentry->d_inode;
5704     +
5705     + /*
5706     + * mode change is for clearing setuid/setgid. Allow lower filesystem
5707     + * to reinterpret it in its own way.
5708     + */
5709     + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5710     + ia->ia_valid &= ~ATTR_MODE;
5711     +
5712     + lower_dentry = unionfs_lower_dentry(dentry);
5713     + if (!lower_dentry) { /* should never happen after above revalidate */
5714     + err = -EINVAL;
5715     + goto out;
5716     + }
5717     + lower_inode = unionfs_lower_inode(inode);
5718     +
5719     + /* check if user has permission to change lower inode */
5720     + err = inode_change_ok(lower_inode, ia);
5721     + if (err)
5722     + goto out;
5723     +
5724     + /* copyup if the file is on a read only branch */
5725     + if (is_robranch_super(dentry->d_sb, bstart)
5726     + || IS_RDONLY(lower_inode)) {
5727     + /* check if we have a branch to copy up to */
5728     + if (bstart <= 0) {
5729     + err = -EACCES;
5730     + goto out;
5731     + }
5732     +
5733     + if (ia->ia_valid & ATTR_SIZE)
5734     + size = ia->ia_size;
5735     + else
5736     + size = i_size_read(inode);
5737     + /* copyup to next available branch */
5738     + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5739     + err = copyup_dentry(parent->d_inode,
5740     + dentry, bstart, bindex,
5741     + dentry->d_name.name,
5742     + dentry->d_name.len,
5743     + NULL, size);
5744     + if (!err)
5745     + break;
5746     + }
5747     + if (err)
5748     + goto out;
5749     + /* get updated lower_dentry/inode after copyup */
5750     + lower_dentry = unionfs_lower_dentry(dentry);
5751     + lower_inode = unionfs_lower_inode(inode);
5752     + }
5753     +
5754     + /*
5755     + * If shrinking, first truncate upper level to cancel writing dirty
5756     + * pages beyond the new eof; and also if its' maxbytes is more
5757     + * limiting (fail with -EFBIG before making any change to the lower
5758     + * level). There is no need to vmtruncate the upper level
5759     + * afterwards in the other cases: we fsstack_copy_inode_size from
5760     + * the lower level.
5761     + */
5762     + if (ia->ia_valid & ATTR_SIZE) {
5763     + size = i_size_read(inode);
5764     + if (ia->ia_size < size || (ia->ia_size > size &&
5765     + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5766     + err = vmtruncate(inode, ia->ia_size);
5767     + if (err)
5768     + goto out;
5769     + }
5770     + }
5771     +
5772     + /* notify the (possibly copied-up) lower inode */
5773     + mutex_lock(&lower_inode->i_mutex);
5774     + err = notify_change(lower_dentry, ia);
5775     + mutex_unlock(&lower_inode->i_mutex);
5776     + if (err)
5777     + goto out;
5778     +
5779     + /* get attributes from the first lower inode */
5780     + unionfs_copy_attr_all(inode, lower_inode);
5781     + /*
5782     + * unionfs_copy_attr_all will copy the lower times to our inode if
5783     + * the lower ones are newer (useful for cache coherency). However,
5784     + * ->setattr is the only place in which we may have to copy the
5785     + * lower inode times absolutely, to support utimes(2).
5786     + */
5787     + if (ia->ia_valid & ATTR_MTIME_SET)
5788     + inode->i_mtime = lower_inode->i_mtime;
5789     + if (ia->ia_valid & ATTR_CTIME)
5790     + inode->i_ctime = lower_inode->i_ctime;
5791     + if (ia->ia_valid & ATTR_ATIME_SET)
5792     + inode->i_atime = lower_inode->i_atime;
5793     + fsstack_copy_inode_size(inode, lower_inode);
5794     +
5795     +out:
5796     + if (!err)
5797     + unionfs_check_dentry(dentry);
5798     + unionfs_unlock_dentry(dentry);
5799     + unionfs_unlock_parent(dentry, parent);
5800     + unionfs_read_unlock(dentry->d_sb);
5801     +
5802     + return err;
5803     +}
5804     +
5805     +struct inode_operations unionfs_symlink_iops = {
5806     + .readlink = unionfs_readlink,
5807     + .permission = unionfs_permission,
5808     + .follow_link = unionfs_follow_link,
5809     + .setattr = unionfs_setattr,
5810     + .put_link = unionfs_put_link,
5811     +};
5812     +
5813     +struct inode_operations unionfs_dir_iops = {
5814     + .create = unionfs_create,
5815     + .lookup = unionfs_lookup,
5816     + .link = unionfs_link,
5817     + .unlink = unionfs_unlink,
5818     + .symlink = unionfs_symlink,
5819     + .mkdir = unionfs_mkdir,
5820     + .rmdir = unionfs_rmdir,
5821     + .mknod = unionfs_mknod,
5822     + .rename = unionfs_rename,
5823     + .permission = unionfs_permission,
5824     + .setattr = unionfs_setattr,
5825     +#ifdef CONFIG_UNION_FS_XATTR
5826     + .setxattr = unionfs_setxattr,
5827     + .getxattr = unionfs_getxattr,
5828     + .removexattr = unionfs_removexattr,
5829     + .listxattr = unionfs_listxattr,
5830     +#endif /* CONFIG_UNION_FS_XATTR */
5831     +};
5832     +
5833     +struct inode_operations unionfs_main_iops = {
5834     + .permission = unionfs_permission,
5835     + .setattr = unionfs_setattr,
5836     +#ifdef CONFIG_UNION_FS_XATTR
5837     + .setxattr = unionfs_setxattr,
5838     + .getxattr = unionfs_getxattr,
5839     + .removexattr = unionfs_removexattr,
5840     + .listxattr = unionfs_listxattr,
5841     +#endif /* CONFIG_UNION_FS_XATTR */
5842     +};
5843     diff -Naur linux-2.6.29/fs/unionfs/lookup.c linux-2.6.29-magellan/fs/unionfs/lookup.c
5844     --- linux-2.6.29/fs/unionfs/lookup.c 1970-01-01 01:00:00.000000000 +0100
5845     +++ linux-2.6.29-magellan/fs/unionfs/lookup.c 2009-04-23 19:41:06.000000000 +0200
5846     @@ -0,0 +1,569 @@
5847     +/*
5848     + * Copyright (c) 2003-2009 Erez Zadok
5849     + * Copyright (c) 2003-2006 Charles P. Wright
5850     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5851     + * Copyright (c) 2005-2006 Junjiro Okajima
5852     + * Copyright (c) 2005 Arun M. Krishnakumar
5853     + * Copyright (c) 2004-2006 David P. Quigley
5854     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5855     + * Copyright (c) 2003 Puja Gupta
5856     + * Copyright (c) 2003 Harikesavan Krishnan
5857     + * Copyright (c) 2003-2009 Stony Brook University
5858     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
5859     + *
5860     + * This program is free software; you can redistribute it and/or modify
5861     + * it under the terms of the GNU General Public License version 2 as
5862     + * published by the Free Software Foundation.
5863     + */
5864     +
5865     +#include "union.h"
5866     +
5867     +/*
5868     + * Lookup one path component @name relative to a <base,mnt> path pair.
5869     + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5870     + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5871     + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5872     + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5873     + * @dentry and @new_mnt.
5874     + */
5875     +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5876     + const char *name, struct vfsmount **new_mnt)
5877     +{
5878     + struct dentry *dentry = NULL;
5879     + struct nameidata lower_nd;
5880     + int err;
5881     +
5882     + /* we use flags=0 to get basic lookup */
5883     + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
5884     +
5885     + switch (err) {
5886     + case 0: /* no error */
5887     + dentry = lower_nd.path.dentry;
5888     + if (new_mnt)
5889     + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
5890     + break;
5891     + case -ENOENT:
5892     + /*
5893     + * We don't consider ENOENT an error, and we want to return
5894     + * a negative dentry (ala lookup_one_len). As we know
5895     + * there was no inode for this name before (-ENOENT), then
5896     + * it's safe to call lookup_one_len (which doesn't take a
5897     + * vfsmount).
5898     + */
5899     + dentry = lookup_one_len(name, base, strlen(name));
5900     + if (new_mnt)
5901     + *new_mnt = mntget(lower_nd.path.mnt);
5902     + break;
5903     + default: /* all other real errors */
5904     + dentry = ERR_PTR(err);
5905     + break;
5906     + }
5907     +
5908     + return dentry;
5909     +}
5910     +
5911     +/*
5912     + * This is a utility function that fills in a unionfs dentry.
5913     + * Caller must lock this dentry with unionfs_lock_dentry.
5914     + *
5915     + * Returns: 0 (ok), or -ERRNO if an error occurred.
5916     + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
5917     + */
5918     +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
5919     +{
5920     + struct dentry *tmp;
5921     + int err = -ENOSYS;
5922     +
5923     + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
5924     +
5925     + if (!tmp) {
5926     + err = 0;
5927     + goto out;
5928     + }
5929     + if (IS_ERR(tmp)) {
5930     + err = PTR_ERR(tmp);
5931     + goto out;
5932     + }
5933     + /* XXX: need to change the interface */
5934     + BUG_ON(tmp != dentry);
5935     +out:
5936     + return err;
5937     +}
5938     +
5939     +/* The dentry cache is just so we have properly sized dentries. */
5940     +static struct kmem_cache *unionfs_dentry_cachep;
5941     +int unionfs_init_dentry_cache(void)
5942     +{
5943     + unionfs_dentry_cachep =
5944     + kmem_cache_create("unionfs_dentry",
5945     + sizeof(struct unionfs_dentry_info),
5946     + 0, SLAB_RECLAIM_ACCOUNT, NULL);
5947     +
5948     + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
5949     +}
5950     +
5951     +void unionfs_destroy_dentry_cache(void)
5952     +{
5953     + if (unionfs_dentry_cachep)
5954     + kmem_cache_destroy(unionfs_dentry_cachep);
5955     +}
5956     +
5957     +void free_dentry_private_data(struct dentry *dentry)
5958     +{
5959     + if (!dentry || !dentry->d_fsdata)
5960     + return;
5961     + kfree(UNIONFS_D(dentry)->lower_paths);
5962     + UNIONFS_D(dentry)->lower_paths = NULL;
5963     + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
5964     + dentry->d_fsdata = NULL;
5965     +}
5966     +
5967     +static inline int __realloc_dentry_private_data(struct dentry *dentry)
5968     +{
5969     + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
5970     + void *p;
5971     + int size;
5972     +
5973     + BUG_ON(!info);
5974     +
5975     + size = sizeof(struct path) * sbmax(dentry->d_sb);
5976     + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
5977     + if (unlikely(!p))
5978     + return -ENOMEM;
5979     +
5980     + info->lower_paths = p;
5981     +
5982     + info->bstart = -1;
5983     + info->bend = -1;
5984     + info->bopaque = -1;
5985     + info->bcount = sbmax(dentry->d_sb);
5986     + atomic_set(&info->generation,
5987     + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
5988     +
5989     + memset(info->lower_paths, 0, size);
5990     +
5991     + return 0;
5992     +}
5993     +
5994     +/* UNIONFS_D(dentry)->lock must be locked */
5995     +int realloc_dentry_private_data(struct dentry *dentry)
5996     +{
5997     + if (!__realloc_dentry_private_data(dentry))
5998     + return 0;
5999     +
6000     + kfree(UNIONFS_D(dentry)->lower_paths);
6001     + free_dentry_private_data(dentry);
6002     + return -ENOMEM;
6003     +}
6004     +
6005     +/* allocate new dentry private data */
6006     +int new_dentry_private_data(struct dentry *dentry, int subclass)
6007     +{
6008     + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6009     +
6010     + BUG_ON(info);
6011     +
6012     + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6013     + if (unlikely(!info))
6014     + return -ENOMEM;
6015     +
6016     + mutex_init(&info->lock);
6017     + mutex_lock_nested(&info->lock, subclass);
6018     +
6019     + info->lower_paths = NULL;
6020     +
6021     + dentry->d_fsdata = info;
6022     +
6023     + if (!__realloc_dentry_private_data(dentry))
6024     + return 0;
6025     +
6026     + mutex_unlock(&info->lock);
6027     + free_dentry_private_data(dentry);
6028     + return -ENOMEM;
6029     +}
6030     +
6031     +/*
6032     + * scan through the lower dentry objects, and set bstart to reflect the
6033     + * starting branch
6034     + */
6035     +void update_bstart(struct dentry *dentry)
6036     +{
6037     + int bindex;
6038     + int bstart = dbstart(dentry);
6039     + int bend = dbend(dentry);
6040     + struct dentry *lower_dentry;
6041     +
6042     + for (bindex = bstart; bindex <= bend; bindex++) {
6043     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6044     + if (!lower_dentry)
6045     + continue;
6046     + if (lower_dentry->d_inode) {
6047     + dbstart(dentry) = bindex;
6048     + break;
6049     + }
6050     + dput(lower_dentry);
6051     + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6052     + }
6053     +}
6054     +
6055     +
6056     +/*
6057     + * Initialize a nameidata structure (the intent part) we can pass to a lower
6058     + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6059     + * Inside that nd structure, this function may also return an allocated
6060     + * struct file (for open intents). The caller, when done with this nd, must
6061     + * kfree the intent file (using release_lower_nd).
6062     + *
6063     + * XXX: this code, and the callers of this code, should be redone using
6064     + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6065     + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6066     + * function and a method that other file systems can call.
6067     + */
6068     +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6069     +{
6070     + int err = 0;
6071     +#ifdef ALLOC_LOWER_ND_FILE
6072     + /*
6073     + * XXX: one day we may need to have the lower return an open file
6074     + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6075     + * very well be needed for nfs4.
6076     + */
6077     + struct file *file;
6078     +#endif /* ALLOC_LOWER_ND_FILE */
6079     +
6080     + memset(nd, 0, sizeof(struct nameidata));
6081     + if (!flags)
6082     + return err;
6083     +
6084     + switch (flags) {
6085     + case LOOKUP_CREATE:
6086     + nd->intent.open.flags |= O_CREAT;
6087     + /* fall through: shared code for create/open cases */
6088     + case LOOKUP_OPEN:
6089     + nd->flags = flags;
6090     + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6091     +#ifdef ALLOC_LOWER_ND_FILE
6092     + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6093     + if (unlikely(!file)) {
6094     + err = -ENOMEM;
6095     + break; /* exit switch statement and thus return */
6096     + }
6097     + nd->intent.open.file = file;
6098     +#endif /* ALLOC_LOWER_ND_FILE */
6099     + break;
6100     + default:
6101     + /*
6102     + * We should never get here, for now.
6103     + * We can add new cases here later on.
6104     + */
6105     + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6106     + BUG();
6107     + break;
6108     + }
6109     +
6110     + return err;
6111     +}
6112     +
6113     +void release_lower_nd(struct nameidata *nd, int err)
6114     +{
6115     + if (!nd->intent.open.file)
6116     + return;
6117     + else if (!err)
6118     + release_open_intent(nd);
6119     +#ifdef ALLOC_LOWER_ND_FILE
6120     + kfree(nd->intent.open.file);
6121     +#endif /* ALLOC_LOWER_ND_FILE */
6122     +}
6123     +
6124     +/*
6125     + * Main (and complex) driver function for Unionfs's lookup
6126     + *
6127     + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6128     + * PTR if d_splice returned a different dentry.
6129     + *
6130     + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6131     + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6132     + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6133     + * dentry's info, which the caller must unlock.
6134     + */
6135     +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6136     + struct dentry *parent, int lookupmode)
6137     +{
6138     + int err = 0;
6139     + struct dentry *lower_dentry = NULL;
6140     + struct vfsmount *lower_mnt;
6141     + struct vfsmount *lower_dir_mnt;
6142     + struct dentry *wh_lower_dentry = NULL;
6143     + struct dentry *lower_dir_dentry = NULL;
6144     + struct dentry *d_interposed = NULL;
6145     + int bindex, bstart, bend, bopaque;
6146     + int opaque, num_positive = 0;
6147     + const char *name;
6148     + int namelen;
6149     + int pos_start, pos_end;
6150     +
6151     + /*
6152     + * We should already have a lock on this dentry in the case of a
6153     + * partial lookup, or a revalidation. Otherwise it is returned from
6154     + * new_dentry_private_data already locked.
6155     + */
6156     + verify_locked(dentry);
6157     + verify_locked(parent);
6158     +
6159     + /* must initialize dentry operations */
6160     + dentry->d_op = &unionfs_dops;
6161     +
6162     + /* We never partial lookup the root directory. */
6163     + if (IS_ROOT(dentry))
6164     + goto out;
6165     +
6166     + name = dentry->d_name.name;
6167     + namelen = dentry->d_name.len;
6168     +
6169     + /* No dentries should get created for possible whiteout names. */
6170     + if (!is_validname(name)) {
6171     + err = -EPERM;
6172     + goto out_free;
6173     + }
6174     +
6175     + /* Now start the actual lookup procedure. */
6176     + bstart = dbstart(parent);
6177     + bend = dbend(parent);
6178     + bopaque = dbopaque(parent);
6179     + BUG_ON(bstart < 0);
6180     +
6181     + /* adjust bend to bopaque if needed */
6182     + if ((bopaque >= 0) && (bopaque < bend))
6183     + bend = bopaque;
6184     +
6185     + /* lookup all possible dentries */
6186     + for (bindex = bstart; bindex <= bend; bindex++) {
6187     +
6188     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6189     + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6190     +
6191     + /* skip if we already have a positive lower dentry */
6192     + if (lower_dentry) {
6193     + if (dbstart(dentry) < 0)
6194     + dbstart(dentry) = bindex;
6195     + if (bindex > dbend(dentry))
6196     + dbend(dentry) = bindex;
6197     + if (lower_dentry->d_inode)
6198     + num_positive++;
6199     + continue;
6200     + }
6201     +
6202     + lower_dir_dentry =
6203     + unionfs_lower_dentry_idx(parent, bindex);
6204     + /* if the lower dentry's parent does not exist, skip this */
6205     + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6206     + continue;
6207     +
6208     + /* also skip it if the parent isn't a directory. */
6209     + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6210     + continue; /* XXX: should be BUG_ON */
6211     +
6212     + /* check for whiteouts: stop lookup if found */
6213     + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6214     + if (IS_ERR(wh_lower_dentry)) {
6215     + err = PTR_ERR(wh_lower_dentry);
6216     + goto out_free;
6217     + }
6218     + if (wh_lower_dentry->d_inode) {
6219     + dbend(dentry) = dbopaque(dentry) = bindex;
6220     + if (dbstart(dentry) < 0)
6221     + dbstart(dentry) = bindex;
6222     + dput(wh_lower_dentry);
6223     + break;
6224     + }
6225     + dput(wh_lower_dentry);
6226     +
6227     + /* Now do regular lookup; lookup @name */
6228     + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6229     + lower_mnt = NULL; /* XXX: needed? */
6230     +
6231     + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6232     + name, &lower_mnt);
6233     +
6234     + if (IS_ERR(lower_dentry)) {
6235     + err = PTR_ERR(lower_dentry);
6236     + goto out_free;
6237     + }
6238     + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6239     + if (!lower_mnt)
6240     + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6241     + bindex);
6242     + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6243     +
6244     + /* adjust dbstart/end */
6245     + if (dbstart(dentry) < 0)
6246     + dbstart(dentry) = bindex;
6247     + if (bindex > dbend(dentry))
6248     + dbend(dentry) = bindex;
6249     + /*
6250     + * We always store the lower dentries above, and update
6251     + * dbstart/dbend, even if the whole unionfs dentry is
6252     + * negative (i.e., no lower inodes).
6253     + */
6254     + if (!lower_dentry->d_inode)
6255     + continue;
6256     + num_positive++;
6257     +
6258     + /*
6259     + * check if we just found an opaque directory, if so, stop
6260     + * lookups here.
6261     + */
6262     + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6263     + continue;
6264     + opaque = is_opaque_dir(dentry, bindex);
6265     + if (opaque < 0) {
6266     + err = opaque;
6267     + goto out_free;
6268     + } else if (opaque) {
6269     + dbend(dentry) = dbopaque(dentry) = bindex;
6270     + break;
6271     + }
6272     + dbend(dentry) = bindex;
6273     +
6274     + /* update parent directory's atime with the bindex */
6275     + fsstack_copy_attr_atime(parent->d_inode,
6276     + lower_dir_dentry->d_inode);
6277     + }
6278     +
6279     + /* sanity checks, then decide if to process a negative dentry */
6280     + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6281     + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6282     +
6283     + if (num_positive > 0)
6284     + goto out_positive;
6285     +
6286     + /*** handle NEGATIVE dentries ***/
6287     +
6288     + /*
6289     + * If negative, keep only first lower negative dentry, to save on
6290     + * memory.
6291     + */
6292     + if (dbstart(dentry) < dbend(dentry)) {
6293     + path_put_lowers(dentry, dbstart(dentry) + 1,
6294     + dbend(dentry), false);
6295     + dbend(dentry) = dbstart(dentry);
6296     + }
6297     + if (lookupmode == INTERPOSE_PARTIAL)
6298     + goto out;
6299     + if (lookupmode == INTERPOSE_LOOKUP) {
6300     + /*
6301     + * If all we found was a whiteout in the first available
6302     + * branch, then create a negative dentry for a possibly new
6303     + * file to be created.
6304     + */
6305     + if (dbopaque(dentry) < 0)
6306     + goto out;
6307     + /* XXX: need to get mnt here */
6308     + bindex = dbstart(dentry);
6309     + if (unionfs_lower_dentry_idx(dentry, bindex))
6310     + goto out;
6311     + lower_dir_dentry =
6312     + unionfs_lower_dentry_idx(parent, bindex);
6313     + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6314     + goto out;
6315     + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6316     + goto out; /* XXX: should be BUG_ON */
6317     + /* XXX: do we need to cross bind mounts here? */
6318     + lower_dentry = lookup_one_len(name, lower_dir_dentry, namelen);
6319     + if (IS_ERR(lower_dentry)) {
6320     + err = PTR_ERR(lower_dentry);
6321     + goto out;
6322     + }
6323     + /* XXX: need to mntget/mntput as needed too! */
6324     + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6325     + /* XXX: wrong mnt for crossing bind mounts! */
6326     + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6327     + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6328     +
6329     + goto out;
6330     + }
6331     +
6332     + /* if we're revalidating a positive dentry, don't make it negative */
6333     + if (lookupmode != INTERPOSE_REVAL)
6334     + d_add(dentry, NULL);
6335     +
6336     + goto out;
6337     +
6338     +out_positive:
6339     + /*** handle POSITIVE dentries ***/
6340     +
6341     + /*
6342     + * This unionfs dentry is positive (at least one lower inode
6343     + * exists), so scan entire dentry from beginning to end, and remove
6344     + * any negative lower dentries, if any. Then, update dbstart/dbend
6345     + * to reflect the start/end of positive dentries.
6346     + */
6347     + pos_start = pos_end = -1;
6348     + for (bindex = bstart; bindex <= bend; bindex++) {
6349     + lower_dentry = unionfs_lower_dentry_idx(dentry,
6350     + bindex);
6351     + if (lower_dentry && lower_dentry->d_inode) {
6352     + if (pos_start < 0)
6353     + pos_start = bindex;
6354     + if (bindex > pos_end)
6355     + pos_end = bindex;
6356     + continue;
6357     + }
6358     + path_put_lowers(dentry, bindex, bindex, false);
6359     + }
6360     + if (pos_start >= 0)
6361     + dbstart(dentry) = pos_start;
6362     + if (pos_end >= 0)
6363     + dbend(dentry) = pos_end;
6364     +
6365     + /* Partial lookups need to re-interpose, or throw away older negs. */
6366     + if (lookupmode == INTERPOSE_PARTIAL) {
6367     + if (dentry->d_inode) {
6368     + unionfs_reinterpose(dentry);
6369     + goto out;
6370     + }
6371     +
6372     + /*
6373     + * This dentry was positive, so it is as if we had a
6374     + * negative revalidation.
6375     + */
6376     + lookupmode = INTERPOSE_REVAL_NEG;
6377     + update_bstart(dentry);
6378     + }
6379     +
6380     + /*
6381     + * Interpose can return a dentry if d_splice returned a different
6382     + * dentry.
6383     + */
6384     + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6385     + if (IS_ERR(d_interposed))
6386     + err = PTR_ERR(d_interposed);
6387     + else if (d_interposed)
6388     + dentry = d_interposed;
6389     +
6390     + if (!err)
6391     + goto out;
6392     + d_drop(dentry);
6393     +
6394     +out_free:
6395     + /* should dput/mntput all the underlying dentries on error condition */
6396     + if (dbstart(dentry) >= 0)
6397     + path_put_lowers_all(dentry, false);
6398     + /* free lower_paths unconditionally */
6399     + kfree(UNIONFS_D(dentry)->lower_paths);
6400     + UNIONFS_D(dentry)->lower_paths = NULL;
6401     +
6402     +out:
6403     + if (dentry && UNIONFS_D(dentry)) {
6404     + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6405     + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6406     + }
6407     + if (d_interposed && UNIONFS_D(d_interposed)) {
6408     + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6409     + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6410     + }
6411     +
6412     + if (!err && d_interposed)
6413     + return d_interposed;
6414     + return ERR_PTR(err);
6415     +}
6416     diff -Naur linux-2.6.29/fs/unionfs/main.c linux-2.6.29-magellan/fs/unionfs/main.c
6417     --- linux-2.6.29/fs/unionfs/main.c 1970-01-01 01:00:00.000000000 +0100
6418     +++ linux-2.6.29-magellan/fs/unionfs/main.c 2009-04-23 19:41:06.000000000 +0200
6419     @@ -0,0 +1,758 @@
6420     +/*
6421     + * Copyright (c) 2003-2009 Erez Zadok
6422     + * Copyright (c) 2003-2006 Charles P. Wright
6423     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6424     + * Copyright (c) 2005-2006 Junjiro Okajima
6425     + * Copyright (c) 2005 Arun M. Krishnakumar
6426     + * Copyright (c) 2004-2006 David P. Quigley
6427     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6428     + * Copyright (c) 2003 Puja Gupta
6429     + * Copyright (c) 2003 Harikesavan Krishnan
6430     + * Copyright (c) 2003-2009 Stony Brook University
6431     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
6432     + *
6433     + * This program is free software; you can redistribute it and/or modify
6434     + * it under the terms of the GNU General Public License version 2 as
6435     + * published by the Free Software Foundation.
6436     + */
6437     +
6438     +#include "union.h"
6439     +#include <linux/module.h>
6440     +#include <linux/moduleparam.h>
6441     +
6442     +static void unionfs_fill_inode(struct dentry *dentry,
6443     + struct inode *inode)
6444     +{
6445     + struct inode *lower_inode;
6446     + struct dentry *lower_dentry;
6447     + int bindex, bstart, bend;
6448     +
6449     + bstart = dbstart(dentry);
6450     + bend = dbend(dentry);
6451     +
6452     + for (bindex = bstart; bindex <= bend; bindex++) {
6453     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6454     + if (!lower_dentry) {
6455     + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6456     + continue;
6457     + }
6458     +
6459     + /* Initialize the lower inode to the new lower inode. */
6460     + if (!lower_dentry->d_inode)
6461     + continue;
6462     +
6463     + unionfs_set_lower_inode_idx(inode, bindex,
6464     + igrab(lower_dentry->d_inode));
6465     + }
6466     +
6467     + ibstart(inode) = dbstart(dentry);
6468     + ibend(inode) = dbend(dentry);
6469     +
6470     + /* Use attributes from the first branch. */
6471     + lower_inode = unionfs_lower_inode(inode);
6472     +
6473     + /* Use different set of inode ops for symlinks & directories */
6474     + if (S_ISLNK(lower_inode->i_mode))
6475     + inode->i_op = &unionfs_symlink_iops;
6476     + else if (S_ISDIR(lower_inode->i_mode))
6477     + inode->i_op = &unionfs_dir_iops;
6478     +
6479     + /* Use different set of file ops for directories */
6480     + if (S_ISDIR(lower_inode->i_mode))
6481     + inode->i_fop = &unionfs_dir_fops;
6482     +
6483     + /* properly initialize special inodes */
6484     + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6485     + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6486     + init_special_inode(inode, lower_inode->i_mode,
6487     + lower_inode->i_rdev);
6488     +
6489     + /* all well, copy inode attributes */
6490     + unionfs_copy_attr_all(inode, lower_inode);
6491     + fsstack_copy_inode_size(inode, lower_inode);
6492     +}
6493     +
6494     +/*
6495     + * Connect a unionfs inode dentry/inode with several lower ones. This is
6496     + * the classic stackable file system "vnode interposition" action.
6497     + *
6498     + * @sb: unionfs's super_block
6499     + */
6500     +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6501     + int flag)
6502     +{
6503     + int err = 0;
6504     + struct inode *inode;
6505     + int need_fill_inode = 1;
6506     + struct dentry *spliced = NULL;
6507     +
6508     + verify_locked(dentry);
6509     +
6510     + /*
6511     + * We allocate our new inode below by calling unionfs_iget,
6512     + * which will initialize some of the new inode's fields
6513     + */
6514     +
6515     + /*
6516     + * On revalidate we've already got our own inode and just need
6517     + * to fix it up.
6518     + */
6519     + if (flag == INTERPOSE_REVAL) {
6520     + inode = dentry->d_inode;
6521     + UNIONFS_I(inode)->bstart = -1;
6522     + UNIONFS_I(inode)->bend = -1;
6523     + atomic_set(&UNIONFS_I(inode)->generation,
6524     + atomic_read(&UNIONFS_SB(sb)->generation));
6525     +
6526     + UNIONFS_I(inode)->lower_inodes =
6527     + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6528     + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6529     + err = -ENOMEM;
6530     + goto out;
6531     + }
6532     + } else {
6533     + /* get unique inode number for unionfs */
6534     + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6535     + if (IS_ERR(inode)) {
6536     + err = PTR_ERR(inode);
6537     + goto out;
6538     + }
6539     + if (atomic_read(&inode->i_count) > 1)
6540     + goto skip;
6541     + }
6542     +
6543     + need_fill_inode = 0;
6544     + unionfs_fill_inode(dentry, inode);
6545     +
6546     +skip:
6547     + /* only (our) lookup wants to do a d_add */
6548     + switch (flag) {
6549     + case INTERPOSE_DEFAULT:
6550     + /* for operations which create new inodes */
6551     + d_add(dentry, inode);
6552     + break;
6553     + case INTERPOSE_REVAL_NEG:
6554     + d_instantiate(dentry, inode);
6555     + break;
6556     + case INTERPOSE_LOOKUP:
6557     + spliced = d_splice_alias(inode, dentry);
6558     + if (spliced && spliced != dentry) {
6559     + /*
6560     + * d_splice can return a dentry if it was
6561     + * disconnected and had to be moved. We must ensure
6562     + * that the private data of the new dentry is
6563     + * correct and that the inode info was filled
6564     + * properly. Finally we must return this new
6565     + * dentry.
6566     + */
6567     + spliced->d_op = &unionfs_dops;
6568     + spliced->d_fsdata = dentry->d_fsdata;
6569     + dentry->d_fsdata = NULL;
6570     + dentry = spliced;
6571     + if (need_fill_inode) {
6572     + need_fill_inode = 0;
6573     + unionfs_fill_inode(dentry, inode);
6574     + }
6575     + goto out_spliced;
6576     + } else if (!spliced) {
6577     + if (need_fill_inode) {
6578     + need_fill_inode = 0;
6579     + unionfs_fill_inode(dentry, inode);
6580     + goto out_spliced;
6581     + }
6582     + }
6583     + break;
6584     + case INTERPOSE_REVAL:
6585     + /* Do nothing. */
6586     + break;
6587     + default:
6588     + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6589     + BUG();
6590     + }
6591     + goto out;
6592     +
6593     +out_spliced:
6594     + if (!err)
6595     + return spliced;
6596     +out:
6597     + return ERR_PTR(err);
6598     +}
6599     +
6600     +/* like interpose above, but for an already existing dentry */
6601     +void unionfs_reinterpose(struct dentry *dentry)
6602     +{
6603     + struct dentry *lower_dentry;
6604     + struct inode *inode;
6605     + int bindex, bstart, bend;
6606     +
6607     + verify_locked(dentry);
6608     +
6609     + /* This is pre-allocated inode */
6610     + inode = dentry->d_inode;
6611     +
6612     + bstart = dbstart(dentry);
6613     + bend = dbend(dentry);
6614     + for (bindex = bstart; bindex <= bend; bindex++) {
6615     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6616     + if (!lower_dentry)
6617     + continue;
6618     +
6619     + if (!lower_dentry->d_inode)
6620     + continue;
6621     + if (unionfs_lower_inode_idx(inode, bindex))
6622     + continue;
6623     + unionfs_set_lower_inode_idx(inode, bindex,
6624     + igrab(lower_dentry->d_inode));
6625     + }
6626     + ibstart(inode) = dbstart(dentry);
6627     + ibend(inode) = dbend(dentry);
6628     +}
6629     +
6630     +/*
6631     + * make sure the branch we just looked up (nd) makes sense:
6632     + *
6633     + * 1) we're not trying to stack unionfs on top of unionfs
6634     + * 2) it exists
6635     + * 3) is a directory
6636     + */
6637     +int check_branch(struct nameidata *nd)
6638     +{
6639     + /* XXX: remove in ODF code -- stacking unions allowed there */
6640     + if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6641     + return -EINVAL;
6642     + if (!nd->path.dentry->d_inode)
6643     + return -ENOENT;
6644     + if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6645     + return -ENOTDIR;
6646     + return 0;
6647     +}
6648     +
6649     +/* checks if two lower_dentries have overlapping branches */
6650     +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6651     +{
6652     + struct dentry *dent = NULL;
6653     +
6654     + dent = dent1;
6655     + while ((dent != dent2) && (dent->d_parent != dent))
6656     + dent = dent->d_parent;
6657     +
6658     + if (dent == dent2)
6659     + return 1;
6660     +
6661     + dent = dent2;
6662     + while ((dent != dent1) && (dent->d_parent != dent))
6663     + dent = dent->d_parent;
6664     +
6665     + return (dent == dent1);
6666     +}
6667     +
6668     +/*
6669     + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6670     + * specified. Fill the mode bits in @perms. If encounter an unknown
6671     + * string, return -EINVAL. Otherwise return 0.
6672     + */
6673     +int parse_branch_mode(const char *name, int *perms)
6674     +{
6675     + if (!name || !strcmp(name, "rw")) {
6676     + *perms = MAY_READ | MAY_WRITE;
6677     + return 0;
6678     + }
6679     + if (!strcmp(name, "ro")) {
6680     + *perms = MAY_READ;
6681     + return 0;
6682     + }
6683     + return -EINVAL;
6684     +}
6685     +
6686     +/*
6687     + * parse the dirs= mount argument
6688     + *
6689     + * We don't need to lock the superblock private data's rwsem, as we get
6690     + * called only by unionfs_read_super - it is still a long time before anyone
6691     + * can even get a reference to us.
6692     + */
6693     +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6694     + *lower_root_info, char *options)
6695     +{
6696     + struct nameidata nd;
6697     + char *name;
6698     + int err = 0;
6699     + int branches = 1;
6700     + int bindex = 0;
6701     + int i = 0;
6702     + int j = 0;
6703     + struct dentry *dent1;
6704     + struct dentry *dent2;
6705     +
6706     + if (options[0] == '\0') {
6707     + printk(KERN_ERR "unionfs: no branches specified\n");
6708     + err = -EINVAL;
6709     + goto out;
6710     + }
6711     +
6712     + /*
6713     + * Each colon means we have a separator, this is really just a rough
6714     + * guess, since strsep will handle empty fields for us.
6715     + */
6716     + for (i = 0; options[i]; i++)
6717     + if (options[i] == ':')
6718     + branches++;
6719     +
6720     + /* allocate space for underlying pointers to lower dentry */
6721     + UNIONFS_SB(sb)->data =
6722     + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6723     + if (unlikely(!UNIONFS_SB(sb)->data)) {
6724     + err = -ENOMEM;
6725     + goto out;
6726     + }
6727     +
6728     + lower_root_info->lower_paths =
6729     + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6730     + if (unlikely(!lower_root_info->lower_paths)) {
6731     + err = -ENOMEM;
6732     + goto out;
6733     + }
6734     +
6735     + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6736     + branches = 0;
6737     + while ((name = strsep(&options, ":")) != NULL) {
6738     + int perms;
6739     + char *mode = strchr(name, '=');
6740     +
6741     + if (!name)
6742     + continue;
6743     + if (!*name) { /* bad use of ':' (extra colons) */
6744     + err = -EINVAL;
6745     + goto out;
6746     + }
6747     +
6748     + branches++;
6749     +
6750     + /* strip off '=' if any */
6751     + if (mode)
6752     + *mode++ = '\0';
6753     +
6754     + err = parse_branch_mode(mode, &perms);
6755     + if (err) {
6756     + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6757     + "branch %d\n", mode, bindex);
6758     + goto out;
6759     + }
6760     + /* ensure that leftmost branch is writeable */
6761     + if (!bindex && !(perms & MAY_WRITE)) {
6762     + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6763     + "read-only (use \"-o ro\" to create a "
6764     + "read-only union)\n");
6765     + err = -EINVAL;
6766     + goto out;
6767     + }
6768     +
6769     + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6770     + if (err) {
6771     + printk(KERN_ERR "unionfs: error accessing "
6772     + "lower directory '%s' (error %d)\n",
6773     + name, err);
6774     + goto out;
6775     + }
6776     +
6777     + err = check_branch(&nd);
6778     + if (err) {
6779     + printk(KERN_ERR "unionfs: lower directory "
6780     + "'%s' is not a valid branch\n", name);
6781     + path_put(&nd.path);
6782     + goto out;
6783     + }
6784     +
6785     + lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6786     + lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6787     +
6788     + set_branchperms(sb, bindex, perms);
6789     + set_branch_count(sb, bindex, 0);
6790     + new_branch_id(sb, bindex);
6791     +
6792     + if (lower_root_info->bstart < 0)
6793     + lower_root_info->bstart = bindex;
6794     + lower_root_info->bend = bindex;
6795     + bindex++;
6796     + }
6797     +
6798     + if (branches == 0) {
6799     + printk(KERN_ERR "unionfs: no branches specified\n");
6800     + err = -EINVAL;
6801     + goto out;
6802     + }
6803     +
6804     + BUG_ON(branches != (lower_root_info->bend + 1));
6805     +
6806     + /*
6807     + * Ensure that no overlaps exist in the branches.
6808     + *
6809     + * This test is required because the Linux kernel has no support
6810     + * currently for ensuring coherency between stackable layers and
6811     + * branches. If we were to allow overlapping branches, it would be
6812     + * possible, for example, to delete a file via one branch, which
6813     + * would not be reflected in another branch. Such incoherency could
6814     + * lead to inconsistencies and even kernel oopses. Rather than
6815     + * implement hacks to work around some of these cache-coherency
6816     + * problems, we prevent branch overlapping, for now. A complete
6817     + * solution will involve proper kernel/VFS support for cache
6818     + * coherency, at which time we could safely remove this
6819     + * branch-overlapping test.
6820     + */
6821     + for (i = 0; i < branches; i++) {
6822     + dent1 = lower_root_info->lower_paths[i].dentry;
6823     + for (j = i + 1; j < branches; j++) {
6824     + dent2 = lower_root_info->lower_paths[j].dentry;
6825     + if (is_branch_overlap(dent1, dent2)) {
6826     + printk(KERN_ERR "unionfs: branches %d and "
6827     + "%d overlap\n", i, j);
6828     + err = -EINVAL;
6829     + goto out;
6830     + }
6831     + }
6832     + }
6833     +
6834     +out:
6835     + if (err) {
6836     + for (i = 0; i < branches; i++)
6837     + path_put(&lower_root_info->lower_paths[i]);
6838     +
6839     + kfree(lower_root_info->lower_paths);
6840     + kfree(UNIONFS_SB(sb)->data);
6841     +
6842     + /*
6843     + * MUST clear the pointers to prevent potential double free if
6844     + * the caller dies later on
6845     + */
6846     + lower_root_info->lower_paths = NULL;
6847     + UNIONFS_SB(sb)->data = NULL;
6848     + }
6849     + return err;
6850     +}
6851     +
6852     +/*
6853     + * Parse mount options. See the manual page for usage instructions.
6854     + *
6855     + * Returns the dentry object of the lower-level (lower) directory;
6856     + * We want to mount our stackable file system on top of that lower directory.
6857     + */
6858     +static struct unionfs_dentry_info *unionfs_parse_options(
6859     + struct super_block *sb,
6860     + char *options)
6861     +{
6862     + struct unionfs_dentry_info *lower_root_info;
6863     + char *optname;
6864     + int err = 0;
6865     + int bindex;
6866     + int dirsfound = 0;
6867     +
6868     + /* allocate private data area */
6869     + err = -ENOMEM;
6870     + lower_root_info =
6871     + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6872     + if (unlikely(!lower_root_info))
6873     + goto out_error;
6874     + lower_root_info->bstart = -1;
6875     + lower_root_info->bend = -1;
6876     + lower_root_info->bopaque = -1;
6877     +
6878     + while ((optname = strsep(&options, ",")) != NULL) {
6879     + char *optarg;
6880     +
6881     + if (!optname || !*optname)
6882     + continue;
6883     +
6884     + optarg = strchr(optname, '=');
6885     + if (optarg)
6886     + *optarg++ = '\0';
6887     +
6888     + /*
6889     + * All of our options take an argument now. Insert ones that
6890     + * don't, above this check.
6891     + */
6892     + if (!optarg) {
6893     + printk(KERN_ERR "unionfs: %s requires an argument\n",
6894     + optname);
6895     + err = -EINVAL;
6896     + goto out_error;
6897     + }
6898     +
6899     + if (!strcmp("dirs", optname)) {
6900     + if (++dirsfound > 1) {
6901     + printk(KERN_ERR
6902     + "unionfs: multiple dirs specified\n");
6903     + err = -EINVAL;
6904     + goto out_error;
6905     + }
6906     + err = parse_dirs_option(sb, lower_root_info, optarg);
6907     + if (err)
6908     + goto out_error;
6909     + continue;
6910     + }
6911     +
6912     + err = -EINVAL;
6913     + printk(KERN_ERR
6914     + "unionfs: unrecognized option '%s'\n", optname);
6915     + goto out_error;
6916     + }
6917     + if (dirsfound != 1) {
6918     + printk(KERN_ERR "unionfs: dirs option required\n");
6919     + err = -EINVAL;
6920     + goto out_error;
6921     + }
6922     + goto out;
6923     +
6924     +out_error:
6925     + if (lower_root_info && lower_root_info->lower_paths) {
6926     + for (bindex = lower_root_info->bstart;
6927     + bindex >= 0 && bindex <= lower_root_info->bend;
6928     + bindex++)
6929     + path_put(&lower_root_info->lower_paths[bindex]);
6930     + }
6931     +
6932     + kfree(lower_root_info->lower_paths);
6933     + kfree(lower_root_info);
6934     +
6935     + kfree(UNIONFS_SB(sb)->data);
6936     + UNIONFS_SB(sb)->data = NULL;
6937     +
6938     + lower_root_info = ERR_PTR(err);
6939     +out:
6940     + return lower_root_info;
6941     +}
6942     +
6943     +/*
6944     + * our custom d_alloc_root work-alike
6945     + *
6946     + * we can't use d_alloc_root if we want to use our own interpose function
6947     + * unchanged, so we simply call our own "fake" d_alloc_root
6948     + */
6949     +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
6950     +{
6951     + struct dentry *ret = NULL;
6952     +
6953     + if (sb) {
6954     + static const struct qstr name = {
6955     + .name = "/",
6956     + .len = 1
6957     + };
6958     +
6959     + ret = d_alloc(NULL, &name);
6960     + if (likely(ret)) {
6961     + ret->d_op = &unionfs_dops;
6962     + ret->d_sb = sb;
6963     + ret->d_parent = ret;
6964     + }
6965     + }
6966     + return ret;
6967     +}
6968     +
6969     +/*
6970     + * There is no need to lock the unionfs_super_info's rwsem as there is no
6971     + * way anyone can have a reference to the superblock at this point in time.
6972     + */
6973     +static int unionfs_read_super(struct super_block *sb, void *raw_data,
6974     + int silent)
6975     +{
6976     + int err = 0;
6977     + struct unionfs_dentry_info *lower_root_info = NULL;
6978     + int bindex, bstart, bend;
6979     +
6980     + if (!raw_data) {
6981     + printk(KERN_ERR
6982     + "unionfs: read_super: missing data argument\n");
6983     + err = -EINVAL;
6984     + goto out;
6985     + }
6986     +
6987     + /* Allocate superblock private data */
6988     + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
6989     + if (unlikely(!UNIONFS_SB(sb))) {
6990     + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
6991     + err = -ENOMEM;
6992     + goto out;
6993     + }
6994     +
6995     + UNIONFS_SB(sb)->bend = -1;
6996     + atomic_set(&UNIONFS_SB(sb)->generation, 1);
6997     + init_rwsem(&UNIONFS_SB(sb)->rwsem);
6998     + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
6999     +
7000     + lower_root_info = unionfs_parse_options(sb, raw_data);
7001     + if (IS_ERR(lower_root_info)) {
7002     + printk(KERN_ERR
7003     + "unionfs: read_super: error while parsing options "
7004     + "(err = %ld)\n", PTR_ERR(lower_root_info));
7005     + err = PTR_ERR(lower_root_info);
7006     + lower_root_info = NULL;
7007     + goto out_free;
7008     + }
7009     + if (lower_root_info->bstart == -1) {
7010     + err = -ENOENT;
7011     + goto out_free;
7012     + }
7013     +
7014     + /* set the lower superblock field of upper superblock */
7015     + bstart = lower_root_info->bstart;
7016     + BUG_ON(bstart != 0);
7017     + sbend(sb) = bend = lower_root_info->bend;
7018     + for (bindex = bstart; bindex <= bend; bindex++) {
7019     + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7020     + atomic_inc(&d->d_sb->s_active);
7021     + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7022     + }
7023     +
7024     + /* max Bytes is the maximum bytes from highest priority branch */
7025     + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7026     +
7027     + /*
7028     + * Our c/m/atime granularity is 1 ns because we may stack on file
7029     + * systems whose granularity is as good. This is important for our
7030     + * time-based cache coherency.
7031     + */
7032     + sb->s_time_gran = 1;
7033     +
7034     + sb->s_op = &unionfs_sops;
7035     +
7036     + /* See comment next to the definition of unionfs_d_alloc_root */
7037     + sb->s_root = unionfs_d_alloc_root(sb);
7038     + if (unlikely(!sb->s_root)) {
7039     + err = -ENOMEM;
7040     + goto out_dput;
7041     + }
7042     +
7043     + /* link the upper and lower dentries */
7044     + sb->s_root->d_fsdata = NULL;
7045     + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7046     + if (unlikely(err))
7047     + goto out_freedpd;
7048     +
7049     + /* Set the lower dentries for s_root */
7050     + for (bindex = bstart; bindex <= bend; bindex++) {
7051     + struct dentry *d;
7052     + struct vfsmount *m;
7053     +
7054     + d = lower_root_info->lower_paths[bindex].dentry;
7055     + m = lower_root_info->lower_paths[bindex].mnt;
7056     +
7057     + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7058     + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7059     + }
7060     + dbstart(sb->s_root) = bstart;
7061     + dbend(sb->s_root) = bend;
7062     +
7063     + /* Set the generation number to one, since this is for the mount. */
7064     + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7065     +
7066     + /*
7067     + * Call interpose to create the upper level inode. Only
7068     + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7069     + */
7070     + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7071     + unionfs_unlock_dentry(sb->s_root);
7072     + if (!err)
7073     + goto out;
7074     + /* else fall through */
7075     +
7076     +out_freedpd:
7077     + if (UNIONFS_D(sb->s_root)) {
7078     + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7079     + free_dentry_private_data(sb->s_root);
7080     + }
7081     + dput(sb->s_root);
7082     +
7083     +out_dput:
7084     + if (lower_root_info && !IS_ERR(lower_root_info)) {
7085     + for (bindex = lower_root_info->bstart;
7086     + bindex <= lower_root_info->bend; bindex++) {
7087     + struct dentry *d;
7088     + d = lower_root_info->lower_paths[bindex].dentry;
7089     + /* drop refs we took earlier */
7090     + atomic_dec(&d->d_sb->s_active);
7091     + path_put(&lower_root_info->lower_paths[bindex]);
7092     + }
7093     + kfree(lower_root_info->lower_paths);
7094     + kfree(lower_root_info);
7095     + lower_root_info = NULL;
7096     + }
7097     +
7098     +out_free:
7099     + kfree(UNIONFS_SB(sb)->data);
7100     + kfree(UNIONFS_SB(sb));
7101     + sb->s_fs_info = NULL;
7102     +
7103     +out:
7104     + if (lower_root_info && !IS_ERR(lower_root_info)) {
7105     + kfree(lower_root_info->lower_paths);
7106     + kfree(lower_root_info);
7107     + }
7108     + return err;
7109     +}
7110     +
7111     +static int unionfs_get_sb(struct file_system_type *fs_type,
7112     + int flags, const char *dev_name,
7113     + void *raw_data, struct vfsmount *mnt)
7114     +{
7115     + int err;
7116     + err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7117     + if (!err)
7118     + UNIONFS_SB(mnt->mnt_sb)->dev_name =
7119     + kstrdup(dev_name, GFP_KERNEL);
7120     + return err;
7121     +}
7122     +
7123     +static struct file_system_type unionfs_fs_type = {
7124     + .owner = THIS_MODULE,
7125     + .name = UNIONFS_NAME,
7126     + .get_sb = unionfs_get_sb,
7127     + .kill_sb = generic_shutdown_super,
7128     + .fs_flags = FS_REVAL_DOT,
7129     +};
7130     +
7131     +static int __init init_unionfs_fs(void)
7132     +{
7133     + int err;
7134     +
7135     + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7136     +
7137     + err = unionfs_init_filldir_cache();
7138     + if (unlikely(err))
7139     + goto out;
7140     + err = unionfs_init_inode_cache();
7141     + if (unlikely(err))
7142     + goto out;
7143     + err = unionfs_init_dentry_cache();
7144     + if (unlikely(err))
7145     + goto out;
7146     + err = init_sioq();
7147     + if (unlikely(err))
7148     + goto out;
7149     + err = register_filesystem(&unionfs_fs_type);
7150     +out:
7151     + if (unlikely(err)) {
7152     + stop_sioq();
7153     + unionfs_destroy_filldir_cache();
7154     + unionfs_destroy_inode_cache();
7155     + unionfs_destroy_dentry_cache();
7156     + }
7157     + return err;
7158     +}
7159     +
7160     +static void __exit exit_unionfs_fs(void)
7161     +{
7162     + stop_sioq();
7163     + unionfs_destroy_filldir_cache();
7164     + unionfs_destroy_inode_cache();
7165     + unionfs_destroy_dentry_cache();
7166     + unregister_filesystem(&unionfs_fs_type);
7167     + pr_info("Completed unionfs module unload\n");
7168     +}
7169     +
7170     +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7171     + " (http://www.fsl.cs.sunysb.edu)");
7172     +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7173     + " (http://unionfs.filesystems.org)");
7174     +MODULE_LICENSE("GPL");
7175     +
7176     +module_init(init_unionfs_fs);
7177     +module_exit(exit_unionfs_fs);
7178     diff -Naur linux-2.6.29/fs/unionfs/Makefile linux-2.6.29-magellan/fs/unionfs/Makefile
7179     --- linux-2.6.29/fs/unionfs/Makefile 1970-01-01 01:00:00.000000000 +0100
7180     +++ linux-2.6.29-magellan/fs/unionfs/Makefile 2009-04-23 19:41:06.000000000 +0200
7181     @@ -0,0 +1,17 @@
7182     +UNIONFS_VERSION="2.5.1 (for 2.6.29-rc2)"
7183     +
7184     +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
7185     +
7186     +obj-$(CONFIG_UNION_FS) += unionfs.o
7187     +
7188     +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
7189     + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
7190     + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
7191     +
7192     +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
7193     +
7194     +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
7195     +
7196     +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
7197     +EXTRA_CFLAGS += -DDEBUG
7198     +endif
7199     diff -Naur linux-2.6.29/fs/unionfs/mmap.c linux-2.6.29-magellan/fs/unionfs/mmap.c
7200     --- linux-2.6.29/fs/unionfs/mmap.c 1970-01-01 01:00:00.000000000 +0100
7201     +++ linux-2.6.29-magellan/fs/unionfs/mmap.c 2009-04-23 19:41:06.000000000 +0200
7202     @@ -0,0 +1,89 @@
7203     +/*
7204     + * Copyright (c) 2003-2009 Erez Zadok
7205     + * Copyright (c) 2003-2006 Charles P. Wright
7206     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7207     + * Copyright (c) 2005-2006 Junjiro Okajima
7208     + * Copyright (c) 2006 Shaya Potter
7209     + * Copyright (c) 2005 Arun M. Krishnakumar
7210     + * Copyright (c) 2004-2006 David P. Quigley
7211     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7212     + * Copyright (c) 2003 Puja Gupta
7213     + * Copyright (c) 2003 Harikesavan Krishnan
7214     + * Copyright (c) 2003-2009 Stony Brook University
7215     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7216     + *
7217     + * This program is free software; you can redistribute it and/or modify
7218     + * it under the terms of the GNU General Public License version 2 as
7219     + * published by the Free Software Foundation.
7220     + */
7221     +
7222     +#include "union.h"
7223     +
7224     +
7225     +/*
7226     + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7227     + * use in unionfs_mmap) checks for the existence of
7228     + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7229     + * be fixed to allow a file system to define vm_ops->fault without any
7230     + * address_space_ops whatsoever.
7231     + *
7232     + * Otherwise, we don't want to use our readpage method at all.
7233     + */
7234     +static int unionfs_readpage(struct file *file, struct page *page)
7235     +{
7236     + BUG();
7237     + return -EINVAL;
7238     +}
7239     +
7240     +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7241     +{
7242     + int err;
7243     + struct file *file, *lower_file;
7244     + struct vm_operations_struct *lower_vm_ops;
7245     + struct vm_area_struct lower_vma;
7246     +
7247     + BUG_ON(!vma);
7248     + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7249     + file = lower_vma.vm_file;
7250     + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7251     + BUG_ON(!lower_vm_ops);
7252     +
7253     + lower_file = unionfs_lower_file(file);
7254     + BUG_ON(!lower_file);
7255     + /*
7256     + * XXX: vm_ops->fault may be called in parallel. Because we have to
7257     + * resort to temporarily changing the vma->vm_file to point to the
7258     + * lower file, a concurrent invocation of unionfs_fault could see a
7259     + * different value. In this workaround, we keep a different copy of
7260     + * the vma structure in our stack, so we never expose a different
7261     + * value of the vma->vm_file called to us, even temporarily. A
7262     + * better fix would be to change the calling semantics of ->fault to
7263     + * take an explicit file pointer.
7264     + */
7265     + lower_vma.vm_file = lower_file;
7266     + err = lower_vm_ops->fault(&lower_vma, vmf);
7267     + return err;
7268     +}
7269     +
7270     +/*
7271     + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7272     + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7273     + * the a_ops vector to be non-NULL.
7274     + */
7275     +struct address_space_operations unionfs_aops = {
7276     + /* empty on purpose */
7277     +};
7278     +
7279     +/*
7280     + * XXX: we need a second, dummy address_space_ops vector, to be used
7281     + * temporarily during unionfs_mmap, because the latter calls
7282     + * generic_file_mmap, which checks if ->readpage exists, else returns
7283     + * -ENOEXEC.
7284     + */
7285     +struct address_space_operations unionfs_dummy_aops = {
7286     + .readpage = unionfs_readpage,
7287     +};
7288     +
7289     +struct vm_operations_struct unionfs_vm_ops = {
7290     + .fault = unionfs_fault,
7291     +};
7292     diff -Naur linux-2.6.29/fs/unionfs/rdstate.c linux-2.6.29-magellan/fs/unionfs/rdstate.c
7293     --- linux-2.6.29/fs/unionfs/rdstate.c 1970-01-01 01:00:00.000000000 +0100
7294     +++ linux-2.6.29-magellan/fs/unionfs/rdstate.c 2009-04-23 19:41:06.000000000 +0200
7295     @@ -0,0 +1,285 @@
7296     +/*
7297     + * Copyright (c) 2003-2009 Erez Zadok
7298     + * Copyright (c) 2003-2006 Charles P. Wright
7299     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7300     + * Copyright (c) 2005-2006 Junjiro Okajima
7301     + * Copyright (c) 2005 Arun M. Krishnakumar
7302     + * Copyright (c) 2004-2006 David P. Quigley
7303     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7304     + * Copyright (c) 2003 Puja Gupta
7305     + * Copyright (c) 2003 Harikesavan Krishnan
7306     + * Copyright (c) 2003-2009 Stony Brook University
7307     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7308     + *
7309     + * This program is free software; you can redistribute it and/or modify
7310     + * it under the terms of the GNU General Public License version 2 as
7311     + * published by the Free Software Foundation.
7312     + */
7313     +
7314     +#include "union.h"
7315     +
7316     +/* This file contains the routines for maintaining readdir state. */
7317     +
7318     +/*
7319     + * There are two structures here, rdstate which is a hash table
7320     + * of the second structure which is a filldir_node.
7321     + */
7322     +
7323     +/*
7324     + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7325     + * of them and they shouldn't waste memory. If the node has a small name
7326     + * (as defined by the dentry structure), then we use an inline name to
7327     + * preserve kmalloc space.
7328     + */
7329     +static struct kmem_cache *unionfs_filldir_cachep;
7330     +
7331     +int unionfs_init_filldir_cache(void)
7332     +{
7333     + unionfs_filldir_cachep =
7334     + kmem_cache_create("unionfs_filldir",
7335     + sizeof(struct filldir_node), 0,
7336     + SLAB_RECLAIM_ACCOUNT, NULL);
7337     +
7338     + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7339     +}
7340     +
7341     +void unionfs_destroy_filldir_cache(void)
7342     +{
7343     + if (unionfs_filldir_cachep)
7344     + kmem_cache_destroy(unionfs_filldir_cachep);
7345     +}
7346     +
7347     +/*
7348     + * This is a tuning parameter that tells us roughly how big to make the
7349     + * hash table in directory entries per page. This isn't perfect, but
7350     + * at least we get a hash table size that shouldn't be too overloaded.
7351     + * The following averages are based on my home directory.
7352     + * 14.44693 Overall
7353     + * 12.29 Single Page Directories
7354     + * 117.93 Multi-page directories
7355     + */
7356     +#define DENTPAGE 4096
7357     +#define DENTPERONEPAGE 12
7358     +#define DENTPERPAGE 118
7359     +#define MINHASHSIZE 1
7360     +static int guesstimate_hash_size(struct inode *inode)
7361     +{
7362     + struct inode *lower_inode;
7363     + int bindex;
7364     + int hashsize = MINHASHSIZE;
7365     +
7366     + if (UNIONFS_I(inode)->hashsize > 0)
7367     + return UNIONFS_I(inode)->hashsize;
7368     +
7369     + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7370     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7371     + if (!lower_inode)
7372     + continue;
7373     +
7374     + if (i_size_read(lower_inode) == DENTPAGE)
7375     + hashsize += DENTPERONEPAGE;
7376     + else
7377     + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7378     + DENTPERPAGE;
7379     + }
7380     +
7381     + return hashsize;
7382     +}
7383     +
7384     +int init_rdstate(struct file *file)
7385     +{
7386     + BUG_ON(sizeof(loff_t) !=
7387     + (sizeof(unsigned int) + sizeof(unsigned int)));
7388     + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7389     +
7390     + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7391     + fbstart(file));
7392     +
7393     + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7394     +}
7395     +
7396     +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7397     +{
7398     + struct unionfs_dir_state *rdstate = NULL;
7399     + struct list_head *pos;
7400     +
7401     + spin_lock(&UNIONFS_I(inode)->rdlock);
7402     + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7403     + struct unionfs_dir_state *r =
7404     + list_entry(pos, struct unionfs_dir_state, cache);
7405     + if (fpos == rdstate2offset(r)) {
7406     + UNIONFS_I(inode)->rdcount--;
7407     + list_del(&r->cache);
7408     + rdstate = r;
7409     + break;
7410     + }
7411     + }
7412     + spin_unlock(&UNIONFS_I(inode)->rdlock);
7413     + return rdstate;
7414     +}
7415     +
7416     +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7417     +{
7418     + int i = 0;
7419     + int hashsize;
7420     + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7421     + struct unionfs_dir_state *rdstate;
7422     +
7423     + hashsize = guesstimate_hash_size(inode);
7424     + mallocsize += hashsize * sizeof(struct list_head);
7425     + mallocsize = __roundup_pow_of_two(mallocsize);
7426     +
7427     + /* This should give us about 500 entries anyway. */
7428     + if (mallocsize > PAGE_SIZE)
7429     + mallocsize = PAGE_SIZE;
7430     +
7431     + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7432     + sizeof(struct list_head);
7433     +
7434     + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7435     + if (unlikely(!rdstate))
7436     + return NULL;
7437     +
7438     + spin_lock(&UNIONFS_I(inode)->rdlock);
7439     + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7440     + UNIONFS_I(inode)->cookie = 1;
7441     + else
7442     + UNIONFS_I(inode)->cookie++;
7443     +
7444     + rdstate->cookie = UNIONFS_I(inode)->cookie;
7445     + spin_unlock(&UNIONFS_I(inode)->rdlock);
7446     + rdstate->offset = 1;
7447     + rdstate->access = jiffies;
7448     + rdstate->bindex = bindex;
7449     + rdstate->dirpos = 0;
7450     + rdstate->hashentries = 0;
7451     + rdstate->size = hashsize;
7452     + for (i = 0; i < rdstate->size; i++)
7453     + INIT_LIST_HEAD(&rdstate->list[i]);
7454     +
7455     + return rdstate;
7456     +}
7457     +
7458     +static void free_filldir_node(struct filldir_node *node)
7459     +{
7460     + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7461     + kfree(node->name);
7462     + kmem_cache_free(unionfs_filldir_cachep, node);
7463     +}
7464     +
7465     +void free_rdstate(struct unionfs_dir_state *state)
7466     +{
7467     + struct filldir_node *tmp;
7468     + int i;
7469     +
7470     + for (i = 0; i < state->size; i++) {
7471     + struct list_head *head = &(state->list[i]);
7472     + struct list_head *pos, *n;
7473     +
7474     + /* traverse the list and deallocate space */
7475     + list_for_each_safe(pos, n, head) {
7476     + tmp = list_entry(pos, struct filldir_node, file_list);
7477     + list_del(&tmp->file_list);
7478     + free_filldir_node(tmp);
7479     + }
7480     + }
7481     +
7482     + kfree(state);
7483     +}
7484     +
7485     +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7486     + const char *name, int namelen,
7487     + int is_whiteout)
7488     +{
7489     + int index;
7490     + unsigned int hash;
7491     + struct list_head *head;
7492     + struct list_head *pos;
7493     + struct filldir_node *cursor = NULL;
7494     + int found = 0;
7495     +
7496     + BUG_ON(namelen <= 0);
7497     +
7498     + hash = full_name_hash(name, namelen);
7499     + index = hash % rdstate->size;
7500     +
7501     + head = &(rdstate->list[index]);
7502     + list_for_each(pos, head) {
7503     + cursor = list_entry(pos, struct filldir_node, file_list);
7504     +
7505     + if (cursor->namelen == namelen && cursor->hash == hash &&
7506     + !strncmp(cursor->name, name, namelen)) {
7507     + /*
7508     + * a duplicate exists, and hence no need to create
7509     + * entry to the list
7510     + */
7511     + found = 1;
7512     +
7513     + /*
7514     + * if a duplicate is found in this branch, and is
7515     + * not due to the caller looking for an entry to
7516     + * whiteout, then the file system may be corrupted.
7517     + */
7518     + if (unlikely(!is_whiteout &&
7519     + cursor->bindex == rdstate->bindex))
7520     + printk(KERN_ERR "unionfs: filldir: possible "
7521     + "I/O error: a file is duplicated "
7522     + "in the same branch %d: %s\n",
7523     + rdstate->bindex, cursor->name);
7524     + break;
7525     + }
7526     + }
7527     +
7528     + if (!found)
7529     + cursor = NULL;
7530     +
7531     + return cursor;
7532     +}
7533     +
7534     +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7535     + int namelen, int bindex, int whiteout)
7536     +{
7537     + struct filldir_node *new;
7538     + unsigned int hash;
7539     + int index;
7540     + int err = 0;
7541     + struct list_head *head;
7542     +
7543     + BUG_ON(namelen <= 0);
7544     +
7545     + hash = full_name_hash(name, namelen);
7546     + index = hash % rdstate->size;
7547     + head = &(rdstate->list[index]);
7548     +
7549     + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7550     + if (unlikely(!new)) {
7551     + err = -ENOMEM;
7552     + goto out;
7553     + }
7554     +
7555     + INIT_LIST_HEAD(&new->file_list);
7556     + new->namelen = namelen;
7557     + new->hash = hash;
7558     + new->bindex = bindex;
7559     + new->whiteout = whiteout;
7560     +
7561     + if (namelen < DNAME_INLINE_LEN_MIN) {
7562     + new->name = new->iname;
7563     + } else {
7564     + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7565     + if (unlikely(!new->name)) {
7566     + kmem_cache_free(unionfs_filldir_cachep, new);
7567     + new = NULL;
7568     + goto out;
7569     + }
7570     + }
7571     +
7572     + memcpy(new->name, name, namelen);
7573     + new->name[namelen] = '\0';
7574     +
7575     + rdstate->hashentries++;
7576     +
7577     + list_add(&(new->file_list), head);
7578     +out:
7579     + return err;
7580     +}
7581     diff -Naur linux-2.6.29/fs/unionfs/rename.c linux-2.6.29-magellan/fs/unionfs/rename.c
7582     --- linux-2.6.29/fs/unionfs/rename.c 1970-01-01 01:00:00.000000000 +0100
7583     +++ linux-2.6.29-magellan/fs/unionfs/rename.c 2009-04-23 19:41:06.000000000 +0200
7584     @@ -0,0 +1,520 @@
7585     +/*
7586     + * Copyright (c) 2003-2009 Erez Zadok
7587     + * Copyright (c) 2003-2006 Charles P. Wright
7588     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7589     + * Copyright (c) 2005-2006 Junjiro Okajima
7590     + * Copyright (c) 2005 Arun M. Krishnakumar
7591     + * Copyright (c) 2004-2006 David P. Quigley
7592     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7593     + * Copyright (c) 2003 Puja Gupta
7594     + * Copyright (c) 2003 Harikesavan Krishnan
7595     + * Copyright (c) 2003-2009 Stony Brook University
7596     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7597     + *
7598     + * This program is free software; you can redistribute it and/or modify
7599     + * it under the terms of the GNU General Public License version 2 as
7600     + * published by the Free Software Foundation.
7601     + */
7602     +
7603     +#include "union.h"
7604     +
7605     +/*
7606     + * This is a helper function for rename, used when rename ends up with hosed
7607     + * over dentries and we need to revert.
7608     + */
7609     +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7610     + struct dentry *parent, int bindex)
7611     +{
7612     + struct dentry *lower_dentry;
7613     + struct dentry *lower_parent;
7614     + int err = 0;
7615     +
7616     + verify_locked(dentry);
7617     +
7618     + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7619     +
7620     + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7621     +
7622     + lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
7623     + dentry->d_name.len);
7624     + if (IS_ERR(lower_dentry)) {
7625     + err = PTR_ERR(lower_dentry);
7626     + goto out;
7627     + }
7628     +
7629     + dput(unionfs_lower_dentry_idx(dentry, bindex));
7630     + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7631     + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7632     +
7633     + if (!lower_dentry->d_inode) {
7634     + dput(lower_dentry);
7635     + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7636     + } else {
7637     + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7638     + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7639     + igrab(lower_dentry->d_inode));
7640     + }
7641     +
7642     +out:
7643     + return err;
7644     +}
7645     +
7646     +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7647     + struct dentry *old_parent,
7648     + struct inode *new_dir, struct dentry *new_dentry,
7649     + struct dentry *new_parent,
7650     + int bindex)
7651     +{
7652     + int err = 0;
7653     + struct dentry *lower_old_dentry;
7654     + struct dentry *lower_new_dentry;
7655     + struct dentry *lower_old_dir_dentry;
7656     + struct dentry *lower_new_dir_dentry;
7657     + struct dentry *trap;
7658     +
7659     + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7660     + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7661     +
7662     + if (!lower_new_dentry) {
7663     + lower_new_dentry =
7664     + create_parents(new_parent->d_inode,
7665     + new_dentry, new_dentry->d_name.name,
7666     + bindex);
7667     + if (IS_ERR(lower_new_dentry)) {
7668     + err = PTR_ERR(lower_new_dentry);
7669     + if (IS_COPYUP_ERR(err))
7670     + goto out;
7671     + printk(KERN_ERR "unionfs: error creating directory "
7672     + "tree for rename, bindex=%d err=%d\n",
7673     + bindex, err);
7674     + goto out;
7675     + }
7676     + }
7677     +
7678     + /* check for and remove whiteout, if any */
7679     + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7680     + if (err > 0) /* ignore if whiteout found and successfully removed */
7681     + err = 0;
7682     + if (err)
7683     + goto out;
7684     +
7685     + /* check of old_dentry branch is writable */
7686     + err = is_robranch_super(old_dentry->d_sb, bindex);
7687     + if (err)
7688     + goto out;
7689     +
7690     + dget(lower_old_dentry);
7691     + dget(lower_new_dentry);
7692     + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7693     + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7694     +
7695     + /* see Documentation/filesystems/unionfs/issues.txt */
7696     + lockdep_off();
7697     + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7698     + /* source should not be ancenstor of target */
7699     + if (trap == lower_old_dentry) {
7700     + err = -EINVAL;
7701     + goto out_err_unlock;
7702     + }
7703     + /* target should not be ancenstor of source */
7704     + if (trap == lower_new_dentry) {
7705     + err = -ENOTEMPTY;
7706     + goto out_err_unlock;
7707     + }
7708     + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7709     + lower_new_dir_dentry->d_inode, lower_new_dentry);
7710     +out_err_unlock:
7711     + if (!err) {
7712     + /* update parent dir times */
7713     + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7714     + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7715     + }
7716     + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7717     + lockdep_on();
7718     +
7719     + dput(lower_old_dir_dentry);
7720     + dput(lower_new_dir_dentry);
7721     + dput(lower_old_dentry);
7722     + dput(lower_new_dentry);
7723     +
7724     +out:
7725     + if (!err) {
7726     + /* Fixup the new_dentry. */
7727     + if (bindex < dbstart(new_dentry))
7728     + dbstart(new_dentry) = bindex;
7729     + else if (bindex > dbend(new_dentry))
7730     + dbend(new_dentry) = bindex;
7731     + }
7732     +
7733     + return err;
7734     +}
7735     +
7736     +/*
7737     + * Main rename code. This is sufficiently complex, that it's documented in
7738     + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7739     + * __unionfs_rename() above to perform some of the work.
7740     + */
7741     +static int do_unionfs_rename(struct inode *old_dir,
7742     + struct dentry *old_dentry,
7743     + struct dentry *old_parent,
7744     + struct inode *new_dir,
7745     + struct dentry *new_dentry,
7746     + struct dentry *new_parent)
7747     +{
7748     + int err = 0;
7749     + int bindex;
7750     + int old_bstart, old_bend;
7751     + int new_bstart, new_bend;
7752     + int do_copyup = -1;
7753     + int local_err = 0;
7754     + int eio = 0;
7755     + int revert = 0;
7756     +
7757     + old_bstart = dbstart(old_dentry);
7758     + old_bend = dbend(old_dentry);
7759     +
7760     + new_bstart = dbstart(new_dentry);
7761     + new_bend = dbend(new_dentry);
7762     +
7763     + /* Rename source to destination. */
7764     + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7765     + new_dir, new_dentry, new_parent,
7766     + old_bstart);
7767     + if (err) {
7768     + if (!IS_COPYUP_ERR(err))
7769     + goto out;
7770     + do_copyup = old_bstart - 1;
7771     + } else {
7772     + revert = 1;
7773     + }
7774     +
7775     + /*
7776     + * Unlink all instances of destination that exist to the left of
7777     + * bstart of source. On error, revert back, goto out.
7778     + */
7779     + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7780     + struct dentry *unlink_dentry;
7781     + struct dentry *unlink_dir_dentry;
7782     +
7783     + BUG_ON(bindex < 0);
7784     + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7785     + if (!unlink_dentry)
7786     + continue;
7787     +
7788     + unlink_dir_dentry = lock_parent(unlink_dentry);
7789     + err = is_robranch_super(old_dir->i_sb, bindex);
7790     + if (!err)
7791     + err = vfs_unlink(unlink_dir_dentry->d_inode,
7792     + unlink_dentry);
7793     +
7794     + fsstack_copy_attr_times(new_parent->d_inode,
7795     + unlink_dir_dentry->d_inode);
7796     + /* propagate number of hard-links */
7797     + new_parent->d_inode->i_nlink =
7798     + unionfs_get_nlinks(new_parent->d_inode);
7799     +
7800     + unlock_dir(unlink_dir_dentry);
7801     + if (!err) {
7802     + if (bindex != new_bstart) {
7803     + dput(unlink_dentry);
7804     + unionfs_set_lower_dentry_idx(new_dentry,
7805     + bindex, NULL);
7806     + }
7807     + } else if (IS_COPYUP_ERR(err)) {
7808     + do_copyup = bindex - 1;
7809     + } else if (revert) {
7810     + goto revert;
7811     + }
7812     + }
7813     +
7814     + if (do_copyup != -1) {
7815     + for (bindex = do_copyup; bindex >= 0; bindex--) {
7816     + /*
7817     + * copyup the file into some left directory, so that
7818     + * you can rename it
7819     + */
7820     + err = copyup_dentry(old_parent->d_inode,
7821     + old_dentry, old_bstart, bindex,
7822     + old_dentry->d_name.name,
7823     + old_dentry->d_name.len, NULL,
7824     + i_size_read(old_dentry->d_inode));
7825     + /* if copyup failed, try next branch to the left */
7826     + if (err)
7827     + continue;
7828     + /*
7829     + * create whiteout before calling __unionfs_rename
7830     + * because the latter will change the old_dentry's
7831     + * lower name and parent dir, resulting in the
7832     + * whiteout getting created in the wrong dir.
7833     + */
7834     + err = create_whiteout(old_dentry, bindex);
7835     + if (err) {
7836     + printk(KERN_ERR "unionfs: can't create a "
7837     + "whiteout for %s in rename (err=%d)\n",
7838     + old_dentry->d_name.name, err);
7839     + continue;
7840     + }
7841     + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7842     + new_dir, new_dentry, new_parent,
7843     + bindex);
7844     + break;
7845     + }
7846     + }
7847     +
7848     + /* make it opaque */
7849     + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7850     + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7851     + if (err)
7852     + goto revert;
7853     + }
7854     +
7855     + /*
7856     + * Create whiteout for source, only if:
7857     + * (1) There is more than one underlying instance of source.
7858     + * (We did a copy_up is taken care of above).
7859     + */
7860     + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7861     + err = create_whiteout(old_dentry, old_bstart);
7862     + if (err) {
7863     + /* can't fix anything now, so we exit with -EIO */
7864     + printk(KERN_ERR "unionfs: can't create a whiteout for "
7865     + "%s in rename!\n", old_dentry->d_name.name);
7866     + err = -EIO;
7867     + }
7868     + }
7869     +
7870     +out:
7871     + return err;
7872     +
7873     +revert:
7874     + /* Do revert here. */
7875     + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7876     + old_bstart);
7877     + if (local_err) {
7878     + printk(KERN_ERR "unionfs: revert failed in rename: "
7879     + "the new refresh failed\n");
7880     + eio = -EIO;
7881     + }
7882     +
7883     + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7884     + old_bstart);
7885     + if (local_err) {
7886     + printk(KERN_ERR "unionfs: revert failed in rename: "
7887     + "the old refresh failed\n");
7888     + eio = -EIO;
7889     + goto revert_out;
7890     + }
7891     +
7892     + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7893     + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7894     + printk(KERN_ERR "unionfs: revert failed in rename: "
7895     + "the object disappeared from under us!\n");
7896     + eio = -EIO;
7897     + goto revert_out;
7898     + }
7899     +
7900     + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
7901     + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
7902     + printk(KERN_ERR "unionfs: revert failed in rename: "
7903     + "the object was created underneath us!\n");
7904     + eio = -EIO;
7905     + goto revert_out;
7906     + }
7907     +
7908     + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
7909     + old_dir, old_dentry, old_parent,
7910     + old_bstart);
7911     +
7912     + /* If we can't fix it, then we cop-out with -EIO. */
7913     + if (local_err) {
7914     + printk(KERN_ERR "unionfs: revert failed in rename!\n");
7915     + eio = -EIO;
7916     + }
7917     +
7918     + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7919     + bindex);
7920     + if (local_err)
7921     + eio = -EIO;
7922     + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7923     + bindex);
7924     + if (local_err)
7925     + eio = -EIO;
7926     +
7927     +revert_out:
7928     + if (eio)
7929     + err = eio;
7930     + return err;
7931     +}
7932     +
7933     +/*
7934     + * We can't copyup a directory, because it may involve huge numbers of
7935     + * children, etc. Doing that in the kernel would be bad, so instead we
7936     + * return EXDEV to the user-space utility that caused this, and let the
7937     + * user-space recurse and ask us to copy up each file separately.
7938     + */
7939     +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
7940     +{
7941     + int err, bstart;
7942     +
7943     + err = check_empty(dentry, parent, NULL);
7944     + if (err == -ENOTEMPTY) {
7945     + if (is_robranch(dentry))
7946     + return -EXDEV;
7947     + } else if (err) {
7948     + return err;
7949     + }
7950     +
7951     + bstart = dbstart(dentry);
7952     + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
7953     + return 0;
7954     +
7955     + dbstart(dentry) = bstart + 1;
7956     + err = check_empty(dentry, parent, NULL);
7957     + dbstart(dentry) = bstart;
7958     + if (err == -ENOTEMPTY)
7959     + err = -EXDEV;
7960     + return err;
7961     +}
7962     +
7963     +/*
7964     + * The locking rules in unionfs_rename are complex. We could use a simpler
7965     + * superblock-level name-space lock for renames and copy-ups.
7966     + */
7967     +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7968     + struct inode *new_dir, struct dentry *new_dentry)
7969     +{
7970     + int err = 0;
7971     + struct dentry *wh_dentry;
7972     + struct dentry *old_parent, *new_parent;
7973     + int valid = true;
7974     +
7975     + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
7976     + old_parent = dget_parent(old_dentry);
7977     + new_parent = dget_parent(new_dentry);
7978     + /* un/lock parent dentries only if they differ from old/new_dentry */
7979     + if (old_parent != old_dentry &&
7980     + old_parent != new_dentry)
7981     + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
7982     + if (new_parent != old_dentry &&
7983     + new_parent != new_dentry &&
7984     + new_parent != old_parent)
7985     + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
7986     + unionfs_double_lock_dentry(old_dentry, new_dentry);
7987     +
7988     + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
7989     + if (!valid) {
7990     + err = -ESTALE;
7991     + goto out;
7992     + }
7993     + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
7994     + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
7995     + if (!valid) {
7996     + err = -ESTALE;
7997     + goto out;
7998     + }
7999     + }
8000     +
8001     + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8002     + err = unionfs_partial_lookup(old_dentry, old_parent);
8003     + else
8004     + err = may_rename_dir(old_dentry, old_parent);
8005     +
8006     + if (err)
8007     + goto out;
8008     +
8009     + err = unionfs_partial_lookup(new_dentry, new_parent);
8010     + if (err)
8011     + goto out;
8012     +
8013     + /*
8014     + * if new_dentry is already lower because of whiteout,
8015     + * simply override it even if the whited-out dir is not empty.
8016     + */
8017     + wh_dentry = find_first_whiteout(new_dentry);
8018     + if (!IS_ERR(wh_dentry)) {
8019     + dput(wh_dentry);
8020     + } else if (new_dentry->d_inode) {
8021     + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8022     + S_ISDIR(new_dentry->d_inode->i_mode)) {
8023     + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8024     + -ENOTDIR : -EISDIR;
8025     + goto out;
8026     + }
8027     +
8028     + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8029     + struct unionfs_dir_state *namelist = NULL;
8030     + /* check if this unionfs directory is empty or not */
8031     + err = check_empty(new_dentry, new_parent, &namelist);
8032     + if (err)
8033     + goto out;
8034     +
8035     + if (!is_robranch(new_dentry))
8036     + err = delete_whiteouts(new_dentry,
8037     + dbstart(new_dentry),
8038     + namelist);
8039     +
8040     + free_rdstate(namelist);
8041     +
8042     + if (err)
8043     + goto out;
8044     + }
8045     + }
8046     +
8047     + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8048     + new_dir, new_dentry, new_parent);
8049     + if (err)
8050     + goto out;
8051     +
8052     + /*
8053     + * force re-lookup since the dir on ro branch is not renamed, and
8054     + * lower dentries still indicate the un-renamed ones.
8055     + */
8056     + if (S_ISDIR(old_dentry->d_inode->i_mode))
8057     + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8058     + else
8059     + unionfs_postcopyup_release(old_dentry);
8060     + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8061     + unionfs_postcopyup_release(new_dentry);
8062     + unionfs_postcopyup_setmnt(new_dentry);
8063     + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8064     + /*
8065     + * If we get here, it means that no copyup was
8066     + * needed, and that a file by the old name already
8067     + * existing on the destination branch; that file got
8068     + * renamed earlier in this function, so all we need
8069     + * to do here is set the lower inode.
8070     + */
8071     + struct inode *inode;
8072     + inode = unionfs_lower_inode(old_dentry->d_inode);
8073     + igrab(inode);
8074     + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8075     + dbstart(new_dentry),
8076     + inode);
8077     + }
8078     + }
8079     + /* if all of this renaming succeeded, update our times */
8080     + unionfs_copy_attr_times(old_dentry->d_inode);
8081     + unionfs_copy_attr_times(new_dentry->d_inode);
8082     + unionfs_check_inode(old_dir);
8083     + unionfs_check_inode(new_dir);
8084     + unionfs_check_dentry(old_dentry);
8085     + unionfs_check_dentry(new_dentry);
8086     +
8087     +out:
8088     + if (err) /* clear the new_dentry stuff created */
8089     + d_drop(new_dentry);
8090     +
8091     + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8092     + if (new_parent != old_dentry &&
8093     + new_parent != new_dentry &&
8094     + new_parent != old_parent)
8095     + unionfs_unlock_dentry(new_parent);
8096     + if (old_parent != old_dentry &&
8097     + old_parent != new_dentry)
8098     + unionfs_unlock_dentry(old_parent);
8099     + dput(new_parent);
8100     + dput(old_parent);
8101     + unionfs_read_unlock(old_dentry->d_sb);
8102     +
8103     + return err;
8104     +}
8105     diff -Naur linux-2.6.29/fs/unionfs/sioq.c linux-2.6.29-magellan/fs/unionfs/sioq.c
8106     --- linux-2.6.29/fs/unionfs/sioq.c 1970-01-01 01:00:00.000000000 +0100
8107     +++ linux-2.6.29-magellan/fs/unionfs/sioq.c 2009-04-23 19:41:06.000000000 +0200
8108     @@ -0,0 +1,101 @@
8109     +/*
8110     + * Copyright (c) 2006-2009 Erez Zadok
8111     + * Copyright (c) 2006 Charles P. Wright
8112     + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8113     + * Copyright (c) 2006 Junjiro Okajima
8114     + * Copyright (c) 2006 David P. Quigley
8115     + * Copyright (c) 2006-2009 Stony Brook University
8116     + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8117     + *
8118     + * This program is free software; you can redistribute it and/or modify
8119     + * it under the terms of the GNU General Public License version 2 as
8120     + * published by the Free Software Foundation.
8121     + */
8122     +
8123     +#include "union.h"
8124     +
8125     +/*
8126     + * Super-user IO work Queue - sometimes we need to perform actions which
8127     + * would fail due to the unix permissions on the parent directory (e.g.,
8128     + * rmdir a directory which appears empty, but in reality contains
8129     + * whiteouts).
8130     + */
8131     +
8132     +static struct workqueue_struct *superio_workqueue;
8133     +
8134     +int __init init_sioq(void)
8135     +{
8136     + int err;
8137     +
8138     + superio_workqueue = create_workqueue("unionfs_siod");
8139     + if (!IS_ERR(superio_workqueue))
8140     + return 0;
8141     +
8142     + err = PTR_ERR(superio_workqueue);
8143     + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8144     + superio_workqueue = NULL;
8145     + return err;
8146     +}
8147     +
8148     +void stop_sioq(void)
8149     +{
8150     + if (superio_workqueue)
8151     + destroy_workqueue(superio_workqueue);
8152     +}
8153     +
8154     +void run_sioq(work_func_t func, struct sioq_args *args)
8155     +{
8156     + INIT_WORK(&args->work, func);
8157     +
8158     + init_completion(&args->comp);
8159     + while (!queue_work(superio_workqueue, &args->work)) {
8160     + /* TODO: do accounting if needed */
8161     + schedule();
8162     + }
8163     + wait_for_completion(&args->comp);
8164     +}
8165     +
8166     +void __unionfs_create(struct work_struct *work)
8167     +{
8168     + struct sioq_args *args = container_of(work, struct sioq_args, work);
8169     + struct create_args *c = &args->create;
8170     +
8171     + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8172     + complete(&args->comp);
8173     +}
8174     +
8175     +void __unionfs_mkdir(struct work_struct *work)
8176     +{
8177     + struct sioq_args *args = container_of(work, struct sioq_args, work);
8178     + struct mkdir_args *m = &args->mkdir;
8179     +
8180     + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8181     + complete(&args->comp);
8182     +}
8183     +
8184     +void __unionfs_mknod(struct work_struct *work)
8185     +{
8186     + struct sioq_args *args = container_of(work, struct sioq_args, work);
8187     + struct mknod_args *m = &args->mknod;
8188     +
8189     + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8190     + complete(&args->comp);
8191     +}
8192     +
8193     +void __unionfs_symlink(struct work_struct *work)
8194     +{
8195     + struct sioq_args *args = container_of(work, struct sioq_args, work);
8196     + struct symlink_args *s = &args->symlink;
8197     +
8198     + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8199     + complete(&args->comp);
8200     +}
8201     +
8202     +void __unionfs_unlink(struct work_struct *work)
8203     +{
8204     + struct sioq_args *args = container_of(work, struct sioq_args, work);
8205     + struct unlink_args *u = &args->unlink;
8206     +
8207     + args->err = vfs_unlink(u->parent, u->dentry);
8208     + complete(&args->comp);
8209     +}
8210     diff -Naur linux-2.6.29/fs/unionfs/sioq.h linux-2.6.29-magellan/fs/unionfs/sioq.h
8211     --- linux-2.6.29/fs/unionfs/sioq.h 1970-01-01 01:00:00.000000000 +0100
8212     +++ linux-2.6.29-magellan/fs/unionfs/sioq.h 2009-04-23 19:41:06.000000000 +0200
8213     @@ -0,0 +1,91 @@
8214     +/*
8215     + * Copyright (c) 2006-2009 Erez Zadok
8216     + * Copyright (c) 2006 Charles P. Wright
8217     + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8218     + * Copyright (c) 2006 Junjiro Okajima
8219     + * Copyright (c) 2006 David P. Quigley
8220     + * Copyright (c) 2006-2009 Stony Brook University
8221     + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8222     + *
8223     + * This program is free software; you can redistribute it and/or modify
8224     + * it under the terms of the GNU General Public License version 2 as
8225     + * published by the Free Software Foundation.
8226     + */
8227     +
8228     +#ifndef _SIOQ_H
8229     +#define _SIOQ_H
8230     +
8231     +struct deletewh_args {
8232     + struct unionfs_dir_state *namelist;
8233     + struct dentry *dentry;
8234     + int bindex;
8235     +};
8236     +
8237     +struct is_opaque_args {
8238     + struct dentry *dentry;
8239     +};
8240     +
8241     +struct create_args {
8242     + struct inode *parent;
8243     + struct dentry *dentry;
8244     + umode_t mode;
8245     + struct nameidata *nd;
8246     +};
8247     +
8248     +struct mkdir_args {
8249     + struct inode *parent;
8250     + struct dentry *dentry;
8251     + umode_t mode;
8252     +};
8253     +
8254     +struct mknod_args {
8255     + struct inode *parent;
8256     + struct dentry *dentry;
8257     + umode_t mode;
8258     + dev_t dev;
8259     +};
8260     +
8261     +struct symlink_args {
8262     + struct inode *parent;
8263     + struct dentry *dentry;
8264     + char *symbuf;
8265     +};
8266     +
8267     +struct unlink_args {
8268     + struct inode *parent;
8269     + struct dentry *dentry;
8270     +};
8271     +
8272     +
8273     +struct sioq_args {
8274     + struct completion comp;
8275     + struct work_struct work;
8276     + int err;
8277     + void *ret;
8278     +
8279     + union {
8280     + struct deletewh_args deletewh;
8281     + struct is_opaque_args is_opaque;
8282     + struct create_args create;
8283     + struct mkdir_args mkdir;
8284     + struct mknod_args mknod;
8285     + struct symlink_args symlink;
8286     + struct unlink_args unlink;
8287     + };
8288     +};
8289     +
8290     +/* Extern definitions for SIOQ functions */
8291     +extern int __init init_sioq(void);
8292     +extern void stop_sioq(void);
8293     +extern void run_sioq(work_func_t func, struct sioq_args *args);
8294     +
8295     +/* Extern definitions for our privilege escalation helpers */
8296     +extern void __unionfs_create(struct work_struct *work);
8297     +extern void __unionfs_mkdir(struct work_struct *work);
8298     +extern void __unionfs_mknod(struct work_struct *work);
8299     +extern void __unionfs_symlink(struct work_struct *work);
8300     +extern void __unionfs_unlink(struct work_struct *work);
8301     +extern void __delete_whiteouts(struct work_struct *work);
8302     +extern void __is_opaque_dir(struct work_struct *work);
8303     +
8304     +#endif /* not _SIOQ_H */
8305     diff -Naur linux-2.6.29/fs/unionfs/subr.c linux-2.6.29-magellan/fs/unionfs/subr.c
8306     --- linux-2.6.29/fs/unionfs/subr.c 1970-01-01 01:00:00.000000000 +0100
8307     +++ linux-2.6.29-magellan/fs/unionfs/subr.c 2009-04-23 19:41:06.000000000 +0200
8308     @@ -0,0 +1,95 @@
8309     +/*
8310     + * Copyright (c) 2003-2009 Erez Zadok
8311     + * Copyright (c) 2003-2006 Charles P. Wright
8312     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8313     + * Copyright (c) 2005-2006 Junjiro Okajima
8314     + * Copyright (c) 2005 Arun M. Krishnakumar
8315     + * Copyright (c) 2004-2006 David P. Quigley
8316     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8317     + * Copyright (c) 2003 Puja Gupta
8318     + * Copyright (c) 2003 Harikesavan Krishnan
8319     + * Copyright (c) 2003-2009 Stony Brook University
8320     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8321     + *
8322     + * This program is free software; you can redistribute it and/or modify
8323     + * it under the terms of the GNU General Public License version 2 as
8324     + * published by the Free Software Foundation.
8325     + */
8326     +
8327     +#include "union.h"
8328     +
8329     +/*
8330     + * returns the right n_link value based on the inode type
8331     + */
8332     +int unionfs_get_nlinks(const struct inode *inode)
8333     +{
8334     + /* don't bother to do all the work since we're unlinked */
8335     + if (inode->i_nlink == 0)
8336     + return 0;
8337     +
8338     + if (!S_ISDIR(inode->i_mode))
8339     + return unionfs_lower_inode(inode)->i_nlink;
8340     +
8341     + /*
8342     + * For directories, we return 1. The only place that could cares
8343     + * about links is readdir, and there's d_type there so even that
8344     + * doesn't matter.
8345     + */
8346     + return 1;
8347     +}
8348     +
8349     +/* copy a/m/ctime from the lower branch with the newest times */
8350     +void unionfs_copy_attr_times(struct inode *upper)
8351     +{
8352     + int bindex;
8353     + struct inode *lower;
8354     +
8355     + if (!upper)
8356     + return;
8357     + if (ibstart(upper) < 0) {
8358     +#ifdef CONFIG_UNION_FS_DEBUG
8359     + WARN_ON(ibstart(upper) < 0);
8360     +#endif /* CONFIG_UNION_FS_DEBUG */
8361     + return;
8362     + }
8363     + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8364     + lower = unionfs_lower_inode_idx(upper, bindex);
8365     + if (!lower)
8366     + continue; /* not all lower dir objects may exist */
8367     + if (unlikely(timespec_compare(&upper->i_mtime,
8368     + &lower->i_mtime) < 0))
8369     + upper->i_mtime = lower->i_mtime;
8370     + if (unlikely(timespec_compare(&upper->i_ctime,
8371     + &lower->i_ctime) < 0))
8372     + upper->i_ctime = lower->i_ctime;
8373     + if (unlikely(timespec_compare(&upper->i_atime,
8374     + &lower->i_atime) < 0))
8375     + upper->i_atime = lower->i_atime;
8376     + }
8377     +}
8378     +
8379     +/*
8380     + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8381     + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8382     + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8383     + * important if the lower inode is a directory type)
8384     + */
8385     +void unionfs_copy_attr_all(struct inode *dest,
8386     + const struct inode *src)
8387     +{
8388     + dest->i_mode = src->i_mode;
8389     + dest->i_uid = src->i_uid;
8390     + dest->i_gid = src->i_gid;
8391     + dest->i_rdev = src->i_rdev;
8392     +
8393     + unionfs_copy_attr_times(dest);
8394     +
8395     + dest->i_blkbits = src->i_blkbits;
8396     + dest->i_flags = src->i_flags;
8397     +
8398     + /*
8399     + * Update the nlinks AFTER updating the above fields, because the
8400     + * get_links callback may depend on them.
8401     + */
8402     + dest->i_nlink = unionfs_get_nlinks(dest);
8403     +}
8404     diff -Naur linux-2.6.29/fs/unionfs/super.c linux-2.6.29-magellan/fs/unionfs/super.c
8405     --- linux-2.6.29/fs/unionfs/super.c 1970-01-01 01:00:00.000000000 +0100
8406     +++ linux-2.6.29-magellan/fs/unionfs/super.c 2009-04-23 19:41:06.000000000 +0200
8407     @@ -0,0 +1,1047 @@
8408     +/*
8409     + * Copyright (c) 2003-2009 Erez Zadok
8410     + * Copyright (c) 2003-2006 Charles P. Wright
8411     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8412     + * Copyright (c) 2005-2006 Junjiro Okajima
8413     + * Copyright (c) 2005 Arun M. Krishnakumar
8414     + * Copyright (c) 2004-2006 David P. Quigley
8415     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8416     + * Copyright (c) 2003 Puja Gupta
8417     + * Copyright (c) 2003 Harikesavan Krishnan
8418     + * Copyright (c) 2003-2009 Stony Brook University
8419     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8420     + *
8421     + * This program is free software; you can redistribute it and/or modify
8422     + * it under the terms of the GNU General Public License version 2 as
8423     + * published by the Free Software Foundation.
8424     + */
8425     +
8426     +#include "union.h"
8427     +
8428     +/*
8429     + * The inode cache is used with alloc_inode for both our inode info and the
8430     + * vfs inode.
8431     + */
8432     +static struct kmem_cache *unionfs_inode_cachep;
8433     +
8434     +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8435     +{
8436     + int size;
8437     + struct unionfs_inode_info *info;
8438     + struct inode *inode;
8439     +
8440     + inode = iget_locked(sb, ino);
8441     + if (!inode)
8442     + return ERR_PTR(-ENOMEM);
8443     + if (!(inode->i_state & I_NEW))
8444     + return inode;
8445     +
8446     + info = UNIONFS_I(inode);
8447     + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8448     + info->bstart = -1;
8449     + info->bend = -1;
8450     + atomic_set(&info->generation,
8451     + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8452     + spin_lock_init(&info->rdlock);
8453     + info->rdcount = 1;
8454     + info->hashsize = -1;
8455     + INIT_LIST_HEAD(&info->readdircache);
8456     +
8457     + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8458     + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8459     + if (unlikely(!info->lower_inodes)) {
8460     + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8461     + "lower-pointer array!\n");
8462     + iget_failed(inode);
8463     + return ERR_PTR(-ENOMEM);
8464     + }
8465     +
8466     + inode->i_version++;
8467     + inode->i_op = &unionfs_main_iops;
8468     + inode->i_fop = &unionfs_main_fops;
8469     +
8470     + inode->i_mapping->a_ops = &unionfs_aops;
8471     +
8472     + /*
8473     + * reset times so unionfs_copy_attr_all can keep out time invariants
8474     + * right (upper inode time being the max of all lower ones).
8475     + */
8476     + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8477     + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8478     + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8479     + unlock_new_inode(inode);
8480     + return inode;
8481     +}
8482     +
8483     +/*
8484     + * we now define delete_inode, because there are two VFS paths that may
8485     + * destroy an inode: one of them calls clear inode before doing everything
8486     + * else that's needed, and the other is fine. This way we truncate the inode
8487     + * size (and its pages) and then clear our own inode, which will do an iput
8488     + * on our and the lower inode.
8489     + *
8490     + * No need to lock sb info's rwsem.
8491     + */
8492     +static void unionfs_delete_inode(struct inode *inode)
8493     +{
8494     +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8495     + spin_lock(&inode->i_lock);
8496     +#endif
8497     + i_size_write(inode, 0); /* every f/s seems to do that */
8498     +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8499     + spin_unlock(&inode->i_lock);
8500     +#endif
8501     +
8502     + if (inode->i_data.nrpages)
8503     + truncate_inode_pages(&inode->i_data, 0);
8504     +
8505     + clear_inode(inode);
8506     +}
8507     +
8508     +/*
8509     + * final actions when unmounting a file system
8510     + *
8511     + * No need to lock rwsem.
8512     + */
8513     +static void unionfs_put_super(struct super_block *sb)
8514     +{
8515     + int bindex, bstart, bend;
8516     + struct unionfs_sb_info *spd;
8517     + int leaks = 0;
8518     +
8519     + spd = UNIONFS_SB(sb);
8520     + if (!spd)
8521     + return;
8522     +
8523     + bstart = sbstart(sb);
8524     + bend = sbend(sb);
8525     +
8526     + /* Make sure we have no leaks of branchget/branchput. */
8527     + for (bindex = bstart; bindex <= bend; bindex++)
8528     + if (unlikely(branch_count(sb, bindex) != 0)) {
8529     + printk(KERN_CRIT
8530     + "unionfs: branch %d has %d references left!\n",
8531     + bindex, branch_count(sb, bindex));
8532     + leaks = 1;
8533     + }
8534     + WARN_ON(leaks != 0);
8535     +
8536     + /* decrement lower super references */
8537     + for (bindex = bstart; bindex <= bend; bindex++) {
8538     + struct super_block *s;
8539     + s = unionfs_lower_super_idx(sb, bindex);
8540     + unionfs_set_lower_super_idx(sb, bindex, NULL);
8541     + atomic_dec(&s->s_active);
8542     + }
8543     +
8544     + kfree(spd->dev_name);
8545     + kfree(spd->data);
8546     + kfree(spd);
8547     + sb->s_fs_info = NULL;
8548     +}
8549     +
8550     +/*
8551     + * Since people use this to answer the "How big of a file can I write?"
8552     + * question, we report the size of the highest priority branch as the size of
8553     + * the union.
8554     + */
8555     +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8556     +{
8557     + int err = 0;
8558     + struct super_block *sb;
8559     + struct dentry *lower_dentry;
8560     + struct dentry *parent;
8561     + bool valid;
8562     +
8563     + sb = dentry->d_sb;
8564     +
8565     + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8566     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8567     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8568     +
8569     + valid = __unionfs_d_revalidate(dentry, parent, false);
8570     + if (unlikely(!valid)) {
8571     + err = -ESTALE;
8572     + goto out;
8573     + }
8574     + unionfs_check_dentry(dentry);
8575     +
8576     + lower_dentry = unionfs_lower_dentry(sb->s_root);
8577     + err = vfs_statfs(lower_dentry, buf);
8578     +
8579     + /* set return buf to our f/s to avoid confusing user-level utils */
8580     + buf->f_type = UNIONFS_SUPER_MAGIC;
8581     + /*
8582     + * Our maximum file name can is shorter by a few bytes because every
8583     + * file name could potentially be whited-out.
8584     + *
8585     + * XXX: this restriction goes away with ODF.
8586     + */
8587     + unionfs_set_max_namelen(&buf->f_namelen);
8588     +
8589     + /*
8590     + * reset two fields to avoid confusing user-land.
8591     + * XXX: is this still necessary?
8592     + */
8593     + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8594     + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8595     +
8596     +out:
8597     + unionfs_check_dentry(dentry);
8598     + unionfs_unlock_dentry(dentry);
8599     + unionfs_unlock_parent(dentry, parent);
8600     + unionfs_read_unlock(sb);
8601     + return err;
8602     +}
8603     +
8604     +/* handle mode changing during remount */
8605     +static noinline_for_stack int do_remount_mode_option(
8606     + char *optarg,
8607     + int cur_branches,
8608     + struct unionfs_data *new_data,
8609     + struct path *new_lower_paths)
8610     +{
8611     + int err = -EINVAL;
8612     + int perms, idx;
8613     + char *modename = strchr(optarg, '=');
8614     + struct nameidata nd;
8615     +
8616     + /* by now, optarg contains the branch name */
8617     + if (!*optarg) {
8618     + printk(KERN_ERR
8619     + "unionfs: no branch specified for mode change\n");
8620     + goto out;
8621     + }
8622     + if (!modename) {
8623     + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8624     + optarg);
8625     + goto out;
8626     + }
8627     + *modename++ = '\0';
8628     + err = parse_branch_mode(modename, &perms);
8629     + if (err) {
8630     + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8631     + modename, optarg);
8632     + goto out;
8633     + }
8634     +
8635     + /*
8636     + * Find matching branch index. For now, this assumes that nothing
8637     + * has been mounted on top of this Unionfs stack. Once we have /odf
8638     + * and cache-coherency resolved, we'll address the branch-path
8639     + * uniqueness.
8640     + */
8641     + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8642     + if (err) {
8643     + printk(KERN_ERR "unionfs: error accessing "
8644     + "lower directory \"%s\" (error %d)\n",
8645     + optarg, err);
8646     + goto out;
8647     + }
8648     + for (idx = 0; idx < cur_branches; idx++)
8649     + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8650     + nd.path.dentry == new_lower_paths[idx].dentry)
8651     + break;
8652     + path_put(&nd.path); /* no longer needed */
8653     + if (idx == cur_branches) {
8654     + err = -ENOENT; /* err may have been reset above */
8655     + printk(KERN_ERR "unionfs: branch \"%s\" "
8656     + "not found\n", optarg);
8657     + goto out;
8658     + }
8659     + /* check/change mode for existing branch */
8660     + /* we don't warn if perms==branchperms */
8661     + new_data[idx].branchperms = perms;
8662     + err = 0;
8663     +out:
8664     + return err;
8665     +}
8666     +
8667     +/* handle branch deletion during remount */
8668     +static noinline_for_stack int do_remount_del_option(
8669     + char *optarg, int cur_branches,
8670     + struct unionfs_data *new_data,
8671     + struct path *new_lower_paths)
8672     +{
8673     + int err = -EINVAL;
8674     + int idx;
8675     + struct nameidata nd;
8676     +
8677     + /* optarg contains the branch name to delete */
8678     +
8679     + /*
8680     + * Find matching branch index. For now, this assumes that nothing
8681     + * has been mounted on top of this Unionfs stack. Once we have /odf
8682     + * and cache-coherency resolved, we'll address the branch-path
8683     + * uniqueness.
8684     + */
8685     + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8686     + if (err) {
8687     + printk(KERN_ERR "unionfs: error accessing "
8688     + "lower directory \"%s\" (error %d)\n",
8689     + optarg, err);
8690     + goto out;
8691     + }
8692     + for (idx = 0; idx < cur_branches; idx++)
8693     + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8694     + nd.path.dentry == new_lower_paths[idx].dentry)
8695     + break;
8696     + path_put(&nd.path); /* no longer needed */
8697     + if (idx == cur_branches) {
8698     + printk(KERN_ERR "unionfs: branch \"%s\" "
8699     + "not found\n", optarg);
8700     + err = -ENOENT;
8701     + goto out;
8702     + }
8703     + /* check if there are any open files on the branch to be deleted */
8704     + if (atomic_read(&new_data[idx].open_files) > 0) {
8705     + err = -EBUSY;
8706     + goto out;
8707     + }
8708     +
8709     + /*
8710     + * Now we have to delete the branch. First, release any handles it
8711     + * has. Then, move the remaining array indexes past "idx" in
8712     + * new_data and new_lower_paths one to the left. Finally, adjust
8713     + * cur_branches.
8714     + */
8715     + path_put(&new_lower_paths[idx]);
8716     +
8717     + if (idx < cur_branches - 1) {
8718     + /* if idx==cur_branches-1, we delete last branch: easy */
8719     + memmove(&new_data[idx], &new_data[idx+1],
8720     + (cur_branches - 1 - idx) *
8721     + sizeof(struct unionfs_data));
8722     + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8723     + (cur_branches - 1 - idx) * sizeof(struct path));
8724     + }
8725     +
8726     + err = 0;
8727     +out:
8728     + return err;
8729     +}
8730     +
8731     +/* handle branch insertion during remount */
8732     +static noinline_for_stack int do_remount_add_option(
8733     + char *optarg, int cur_branches,
8734     + struct unionfs_data *new_data,
8735     + struct path *new_lower_paths,
8736     + int *high_branch_id)
8737     +{
8738     + int err = -EINVAL;
8739     + int perms;
8740     + int idx = 0; /* default: insert at beginning */
8741     + char *new_branch , *modename = NULL;
8742     + struct nameidata nd;
8743     +
8744     + /*
8745     + * optarg can be of several forms:
8746     + *
8747     + * /bar:/foo insert /foo before /bar
8748     + * /bar:/foo=ro insert /foo in ro mode before /bar
8749     + * /foo insert /foo in the beginning (prepend)
8750     + * :/foo insert /foo at the end (append)
8751     + */
8752     + if (*optarg == ':') { /* append? */
8753     + new_branch = optarg + 1; /* skip ':' */
8754     + idx = cur_branches;
8755     + goto found_insertion_point;
8756     + }
8757     + new_branch = strchr(optarg, ':');
8758     + if (!new_branch) { /* prepend? */
8759     + new_branch = optarg;
8760     + goto found_insertion_point;
8761     + }
8762     + *new_branch++ = '\0'; /* holds path+mode of new branch */
8763     +
8764     + /*
8765     + * Find matching branch index. For now, this assumes that nothing
8766     + * has been mounted on top of this Unionfs stack. Once we have /odf
8767     + * and cache-coherency resolved, we'll address the branch-path
8768     + * uniqueness.
8769     + */
8770     + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8771     + if (err) {
8772     + printk(KERN_ERR "unionfs: error accessing "
8773     + "lower directory \"%s\" (error %d)\n",
8774     + optarg, err);
8775     + goto out;
8776     + }
8777     + for (idx = 0; idx < cur_branches; idx++)
8778     + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8779     + nd.path.dentry == new_lower_paths[idx].dentry)
8780     + break;
8781     + path_put(&nd.path); /* no longer needed */
8782     + if (idx == cur_branches) {
8783     + printk(KERN_ERR "unionfs: branch \"%s\" "
8784     + "not found\n", optarg);
8785     + err = -ENOENT;
8786     + goto out;
8787     + }
8788     +
8789     + /*
8790     + * At this point idx will hold the index where the new branch should
8791     + * be inserted before.
8792     + */
8793     +found_insertion_point:
8794     + /* find the mode for the new branch */
8795     + if (new_branch)
8796     + modename = strchr(new_branch, '=');
8797     + if (modename)
8798     + *modename++ = '\0';
8799     + if (!new_branch || !*new_branch) {
8800     + printk(KERN_ERR "unionfs: null new branch\n");
8801     + err = -EINVAL;
8802     + goto out;
8803     + }
8804     + err = parse_branch_mode(modename, &perms);
8805     + if (err) {
8806     + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8807     + "branch \"%s\"\n", modename, new_branch);
8808     + goto out;
8809     + }
8810     + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8811     + if (err) {
8812     + printk(KERN_ERR "unionfs: error accessing "
8813     + "lower directory \"%s\" (error %d)\n",
8814     + new_branch, err);
8815     + goto out;
8816     + }
8817     + /*
8818     + * It's probably safe to check_mode the new branch to insert. Note:
8819     + * we don't allow inserting branches which are unionfs's by
8820     + * themselves (check_branch returns EINVAL in that case). This is
8821     + * because this code base doesn't support stacking unionfs: the ODF
8822     + * code base supports that correctly.
8823     + */
8824     + err = check_branch(&nd);
8825     + if (err) {
8826     + printk(KERN_ERR "unionfs: lower directory "
8827     + "\"%s\" is not a valid branch\n", optarg);
8828     + path_put(&nd.path);
8829     + goto out;
8830     + }
8831     +
8832     + /*
8833     + * Now we have to insert the new branch. But first, move the bits
8834     + * to make space for the new branch, if needed. Finally, adjust
8835     + * cur_branches.
8836     + * We don't release nd here; it's kept until umount/remount.
8837     + */
8838     + if (idx < cur_branches) {
8839     + /* if idx==cur_branches, we append: easy */
8840     + memmove(&new_data[idx+1], &new_data[idx],
8841     + (cur_branches - idx) * sizeof(struct unionfs_data));
8842     + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8843     + (cur_branches - idx) * sizeof(struct path));
8844     + }
8845     + new_lower_paths[idx].dentry = nd.path.dentry;
8846     + new_lower_paths[idx].mnt = nd.path.mnt;
8847     +
8848     + new_data[idx].sb = nd.path.dentry->d_sb;
8849     + atomic_set(&new_data[idx].open_files, 0);
8850     + new_data[idx].branchperms = perms;
8851     + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8852     +
8853     + err = 0;
8854     +out:
8855     + return err;
8856     +}
8857     +
8858     +
8859     +/*
8860     + * Support branch management options on remount.
8861     + *
8862     + * See Documentation/filesystems/unionfs/ for details.
8863     + *
8864     + * @flags: numeric mount options
8865     + * @options: mount options string
8866     + *
8867     + * This function can rearrange a mounted union dynamically, adding and
8868     + * removing branches, including changing branch modes. Clearly this has to
8869     + * be done safely and atomically. Luckily, the VFS already calls this
8870     + * function with lock_super(sb) and lock_kernel() held, preventing
8871     + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8872     + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8873     + * to purge dentries/inodes from our superblock, and also called
8874     + * fsync_super(sb) to purge any dirty pages. So we're good.
8875     + *
8876     + * XXX: however, our remount code may also need to invalidate mapped pages
8877     + * so as to force them to be re-gotten from the (newly reconfigured) lower
8878     + * branches. This has to wait for proper mmap and cache coherency support
8879     + * in the VFS.
8880     + *
8881     + */
8882     +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8883     + char *options)
8884     +{
8885     + int err = 0;
8886     + int i;
8887     + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
8888     + char *optname;
8889     + int cur_branches = 0; /* no. of current branches */
8890     + int new_branches = 0; /* no. of branches actually left in the end */
8891     + int add_branches; /* est. no. of branches to add */
8892     + int del_branches; /* est. no. of branches to del */
8893     + int max_branches; /* max possible no. of branches */
8894     + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
8895     + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
8896     + struct inode **new_lower_inodes = NULL;
8897     + int new_high_branch_id; /* new high branch ID */
8898     + int size; /* memory allocation size, temp var */
8899     + int old_ibstart, old_ibend;
8900     +
8901     + unionfs_write_lock(sb);
8902     +
8903     + /*
8904     + * The VFS will take care of "ro" and "rw" flags, and we can safely
8905     + * ignore MS_SILENT, but anything else left over is an error. So we
8906     + * need to check if any other flags may have been passed (none are
8907     + * allowed/supported as of now).
8908     + */
8909     + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
8910     + printk(KERN_ERR
8911     + "unionfs: remount flags 0x%x unsupported\n", *flags);
8912     + err = -EINVAL;
8913     + goto out_error;
8914     + }
8915     +
8916     + /*
8917     + * If 'options' is NULL, it's probably because the user just changed
8918     + * the union to a "ro" or "rw" and the VFS took care of it. So
8919     + * nothing to do and we're done.
8920     + */
8921     + if (!options || options[0] == '\0')
8922     + goto out_error;
8923     +
8924     + /*
8925     + * Find out how many branches we will have in the end, counting
8926     + * "add" and "del" commands. Copy the "options" string because
8927     + * strsep modifies the string and we need it later.
8928     + */
8929     + tmp_to_free = kstrdup(options, GFP_KERNEL);
8930     + optionstmp = tmp_to_free;
8931     + if (unlikely(!optionstmp)) {
8932     + err = -ENOMEM;
8933     + goto out_free;
8934     + }
8935     + cur_branches = sbmax(sb); /* current no. branches */
8936     + new_branches = sbmax(sb);
8937     + del_branches = 0;
8938     + add_branches = 0;
8939     + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
8940     + while ((optname = strsep(&optionstmp, ",")) != NULL) {
8941     + char *optarg;
8942     +
8943     + if (!optname || !*optname)
8944     + continue;
8945     +
8946     + optarg = strchr(optname, '=');
8947     + if (optarg)
8948     + *optarg++ = '\0';
8949     +
8950     + if (!strcmp("add", optname))
8951     + add_branches++;
8952     + else if (!strcmp("del", optname))
8953     + del_branches++;
8954     + }
8955     + kfree(tmp_to_free);
8956     + /* after all changes, will we have at least one branch left? */
8957     + if ((new_branches + add_branches - del_branches) < 1) {
8958     + printk(KERN_ERR
8959     + "unionfs: no branches left after remount\n");
8960     + err = -EINVAL;
8961     + goto out_free;
8962     + }
8963     +
8964     + /*
8965     + * Since we haven't actually parsed all the add/del options, nor
8966     + * have we checked them for errors, we don't know for sure how many
8967     + * branches we will have after all changes have taken place. In
8968     + * fact, the total number of branches left could be less than what
8969     + * we have now. So we need to allocate space for a temporary
8970     + * placeholder that is at least as large as the maximum number of
8971     + * branches we *could* have, which is the current number plus all
8972     + * the additions. Once we're done with these temp placeholders, we
8973     + * may have to re-allocate the final size, copy over from the temp,
8974     + * and then free the temps (done near the end of this function).
8975     + */
8976     + max_branches = cur_branches + add_branches;
8977     + /* allocate space for new pointers to lower dentry */
8978     + tmp_data = kcalloc(max_branches,
8979     + sizeof(struct unionfs_data), GFP_KERNEL);
8980     + if (unlikely(!tmp_data)) {
8981     + err = -ENOMEM;
8982     + goto out_free;
8983     + }
8984     + /* allocate space for new pointers to lower paths */
8985     + tmp_lower_paths = kcalloc(max_branches,
8986     + sizeof(struct path), GFP_KERNEL);
8987     + if (unlikely(!tmp_lower_paths)) {
8988     + err = -ENOMEM;
8989     + goto out_free;
8990     + }
8991     + /* copy current info into new placeholders, incrementing refcnts */
8992     + memcpy(tmp_data, UNIONFS_SB(sb)->data,
8993     + cur_branches * sizeof(struct unionfs_data));
8994     + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
8995     + cur_branches * sizeof(struct path));
8996     + for (i = 0; i < cur_branches; i++)
8997     + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
8998     +
8999     + /*******************************************************************
9000     + * For each branch command, do path_lookup on the requested branch,
9001     + * and apply the change to a temp branch list. To handle errors, we
9002     + * already dup'ed the old arrays (above), and increased the refcnts
9003     + * on various f/s objects. So now we can do all the path_lookups
9004     + * and branch-management commands on the new arrays. If it fail mid
9005     + * way, we free the tmp arrays and *put all objects. If we succeed,
9006     + * then we free old arrays and *put its objects, and then replace
9007     + * the arrays with the new tmp list (we may have to re-allocate the
9008     + * memory because the temp lists could have been larger than what we
9009     + * actually needed).
9010     + *******************************************************************/
9011     +
9012     + while ((optname = strsep(&options, ",")) != NULL) {
9013     + char *optarg;
9014     +
9015     + if (!optname || !*optname)
9016     + continue;
9017     + /*
9018     + * At this stage optname holds a comma-delimited option, but
9019     + * without the commas. Next, we need to break the string on
9020     + * the '=' symbol to separate CMD=ARG, where ARG itself can
9021     + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9022     + * KEY is "/foo", and VAL is "rw".
9023     + */
9024     + optarg = strchr(optname, '=');
9025     + if (optarg)
9026     + *optarg++ = '\0';
9027     + /* incgen remount option (instead of old ioctl) */
9028     + if (!strcmp("incgen", optname)) {
9029     + err = 0;
9030     + goto out_no_change;
9031     + }
9032     +
9033     + /*
9034     + * All of our options take an argument now. (Insert ones
9035     + * that don't above this check.) So at this stage optname
9036     + * contains the CMD part and optarg contains the ARG part.
9037     + */
9038     + if (!optarg || !*optarg) {
9039     + printk(KERN_ERR "unionfs: all remount options require "
9040     + "an argument (%s)\n", optname);
9041     + err = -EINVAL;
9042     + goto out_release;
9043     + }
9044     +
9045     + if (!strcmp("add", optname)) {
9046     + err = do_remount_add_option(optarg, new_branches,
9047     + tmp_data,
9048     + tmp_lower_paths,
9049     + &new_high_branch_id);
9050     + if (err)
9051     + goto out_release;
9052     + new_branches++;
9053     + if (new_branches > UNIONFS_MAX_BRANCHES) {
9054     + printk(KERN_ERR "unionfs: command exceeds "
9055     + "%d branches\n", UNIONFS_MAX_BRANCHES);
9056     + err = -E2BIG;
9057     + goto out_release;
9058     + }
9059     + continue;
9060     + }
9061     + if (!strcmp("del", optname)) {
9062     + err = do_remount_del_option(optarg, new_branches,
9063     + tmp_data,
9064     + tmp_lower_paths);
9065     + if (err)
9066     + goto out_release;
9067     + new_branches--;
9068     + continue;
9069     + }
9070     + if (!strcmp("mode", optname)) {
9071     + err = do_remount_mode_option(optarg, new_branches,
9072     + tmp_data,
9073     + tmp_lower_paths);
9074     + if (err)
9075     + goto out_release;
9076     + continue;
9077     + }
9078     +
9079     + /*
9080     + * When you use "mount -o remount,ro", mount(8) will
9081     + * reportedly pass the original dirs= string from
9082     + * /proc/mounts. So for now, we have to ignore dirs= and
9083     + * not consider it an error, unless we want to allow users
9084     + * to pass dirs= in remount. Note that to allow the VFS to
9085     + * actually process the ro/rw remount options, we have to
9086     + * return 0 from this function.
9087     + */
9088     + if (!strcmp("dirs", optname)) {
9089     + printk(KERN_WARNING
9090     + "unionfs: remount ignoring option \"%s\"\n",
9091     + optname);
9092     + continue;
9093     + }
9094     +
9095     + err = -EINVAL;
9096     + printk(KERN_ERR
9097     + "unionfs: unrecognized option \"%s\"\n", optname);
9098     + goto out_release;
9099     + }
9100     +
9101     +out_no_change:
9102     +
9103     + /******************************************************************
9104     + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9105     + * see if we need to allocate a small-sized new vector, copy the
9106     + * vectors to their correct place, release the refcnt of the older
9107     + * ones, and return. Also handle invalidating any pages that will
9108     + * have to be re-read.
9109     + *******************************************************************/
9110     +
9111     + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9112     + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9113     + "(use \"remount,ro\" to create a read-only union)\n");
9114     + err = -EINVAL;
9115     + goto out_release;
9116     + }
9117     +
9118     + /* (re)allocate space for new pointers to lower dentry */
9119     + size = new_branches * sizeof(struct unionfs_data);
9120     + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9121     + if (unlikely(!new_data)) {
9122     + err = -ENOMEM;
9123     + goto out_release;
9124     + }
9125     +
9126     + /* allocate space for new pointers to lower paths */
9127     + size = new_branches * sizeof(struct path);
9128     + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9129     + if (unlikely(!new_lower_paths)) {
9130     + err = -ENOMEM;
9131     + goto out_release;
9132     + }
9133     +
9134     + /* allocate space for new pointers to lower inodes */
9135     + new_lower_inodes = kcalloc(new_branches,
9136     + sizeof(struct inode *), GFP_KERNEL);
9137     + if (unlikely(!new_lower_inodes)) {
9138     + err = -ENOMEM;
9139     + goto out_release;
9140     + }
9141     +
9142     + /*
9143     + * OK, just before we actually put the new set of branches in place,
9144     + * we need to ensure that our own f/s has no dirty objects left.
9145     + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9146     + * fsync_super(sb), taking care of dentries, inodes, and dirty
9147     + * pages. So all that's left is for us to invalidate any leftover
9148     + * (non-dirty) pages to ensure that they will be re-read from the
9149     + * new lower branches (and to support mmap).
9150     + */
9151     +
9152     + /*
9153     + * Once we finish the remounting successfully, our superblock
9154     + * generation number will have increased. This will be detected by
9155     + * our dentry-revalidation code upon subsequent f/s operations
9156     + * through unionfs. The revalidation code will rebuild the union of
9157     + * lower inodes for a given unionfs inode and invalidate any pages
9158     + * of such "stale" inodes (by calling our purge_inode_data
9159     + * function). This revalidation will happen lazily and
9160     + * incrementally, as users perform operations on cached inodes. We
9161     + * would like to encourage this revalidation to happen sooner if
9162     + * possible, so we like to try to invalidate as many other pages in
9163     + * our superblock as we can. We used to call drop_pagecache_sb() or
9164     + * a variant thereof, but either method was racy (drop_caches alone
9165     + * is known to be racy). So now we let the revalidation happen on a
9166     + * per file basis in ->d_revalidate.
9167     + */
9168     +
9169     + /* grab new lower super references; release old ones */
9170     + for (i = 0; i < new_branches; i++)
9171     + atomic_inc(&new_data[i].sb->s_active);
9172     + for (i = 0; i < sbmax(sb); i++)
9173     + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9174     +
9175     + /* copy new vectors into their correct place */
9176     + tmp_data = UNIONFS_SB(sb)->data;
9177     + UNIONFS_SB(sb)->data = new_data;
9178     + new_data = NULL; /* so don't free good pointers below */
9179     + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9180     + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9181     + new_lower_paths = NULL; /* so don't free good pointers below */
9182     +
9183     + /* update our unionfs_sb_info and root dentry index of last branch */
9184     + i = sbmax(sb); /* save no. of branches to release at end */
9185     + sbend(sb) = new_branches - 1;
9186     + dbend(sb->s_root) = new_branches - 1;
9187     + old_ibstart = ibstart(sb->s_root->d_inode);
9188     + old_ibend = ibend(sb->s_root->d_inode);
9189     + ibend(sb->s_root->d_inode) = new_branches - 1;
9190     + UNIONFS_D(sb->s_root)->bcount = new_branches;
9191     + new_branches = i; /* no. of branches to release below */
9192     +
9193     + /*
9194     + * Update lower inodes: 3 steps
9195     + * 1. grab ref on all new lower inodes
9196     + */
9197     + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9198     + struct dentry *lower_dentry =
9199     + unionfs_lower_dentry_idx(sb->s_root, i);
9200     + igrab(lower_dentry->d_inode);
9201     + new_lower_inodes[i] = lower_dentry->d_inode;
9202     + }
9203     + /* 2. release reference on all older lower inodes */
9204     + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9205     + /* 3. update root dentry's inode to new lower_inodes array */
9206     + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9207     + new_lower_inodes = NULL;
9208     +
9209     + /* maxbytes may have changed */
9210     + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9211     + /* update high branch ID */
9212     + sbhbid(sb) = new_high_branch_id;
9213     +
9214     + /* update our sb->generation for revalidating objects */
9215     + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9216     + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9217     + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9218     + if (!(*flags & MS_SILENT))
9219     + pr_info("unionfs: %s: new generation number %d\n",
9220     + UNIONFS_SB(sb)->dev_name, i);
9221     + /* finally, update the root dentry's times */
9222     + unionfs_copy_attr_times(sb->s_root->d_inode);
9223     + err = 0; /* reset to success */
9224     +
9225     + /*
9226     + * The code above falls through to the next label, and releases the
9227     + * refcnts of the older ones (stored in tmp_*): if we fell through
9228     + * here, it means success. However, if we jump directly to this
9229     + * label from any error above, then an error occurred after we
9230     + * grabbed various refcnts, and so we have to release the
9231     + * temporarily constructed structures.
9232     + */
9233     +out_release:
9234     + /* no need to cleanup/release anything in tmp_data */
9235     + if (tmp_lower_paths)
9236     + for (i = 0; i < new_branches; i++)
9237     + path_put(&tmp_lower_paths[i]);
9238     +out_free:
9239     + kfree(tmp_lower_paths);
9240     + kfree(tmp_data);
9241     + kfree(new_lower_paths);
9242     + kfree(new_data);
9243     + kfree(new_lower_inodes);
9244     +out_error:
9245     + unionfs_check_dentry(sb->s_root);
9246     + unionfs_write_unlock(sb);
9247     + return err;
9248     +}
9249     +
9250     +/*
9251     + * Called by iput() when the inode reference count reached zero
9252     + * and the inode is not hashed anywhere. Used to clear anything
9253     + * that needs to be, before the inode is completely destroyed and put
9254     + * on the inode free list.
9255     + *
9256     + * No need to lock sb info's rwsem.
9257     + */
9258     +static void unionfs_clear_inode(struct inode *inode)
9259     +{
9260     + int bindex, bstart, bend;
9261     + struct inode *lower_inode;
9262     + struct list_head *pos, *n;
9263     + struct unionfs_dir_state *rdstate;
9264     +
9265     + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9266     + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9267     + list_del(&rdstate->cache);
9268     + free_rdstate(rdstate);
9269     + }
9270     +
9271     + /*
9272     + * Decrement a reference to a lower_inode, which was incremented
9273     + * by our read_inode when it was created initially.
9274     + */
9275     + bstart = ibstart(inode);
9276     + bend = ibend(inode);
9277     + if (bstart >= 0) {
9278     + for (bindex = bstart; bindex <= bend; bindex++) {
9279     + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9280     + if (!lower_inode)
9281     + continue;
9282     + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9283     + /* see Documentation/filesystems/unionfs/issues.txt */
9284     + lockdep_off();
9285     + iput(lower_inode);
9286     + lockdep_on();
9287     + }
9288     + }
9289     +
9290     + kfree(UNIONFS_I(inode)->lower_inodes);
9291     + UNIONFS_I(inode)->lower_inodes = NULL;
9292     +}
9293     +
9294     +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9295     +{
9296     + struct unionfs_inode_info *i;
9297     +
9298     + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9299     + if (unlikely(!i))
9300     + return NULL;
9301     +
9302     + /* memset everything up to the inode to 0 */
9303     + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9304     +
9305     + i->vfs_inode.i_version = 1;
9306     + return &i->vfs_inode;
9307     +}
9308     +
9309     +static void unionfs_destroy_inode(struct inode *inode)
9310     +{
9311     + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9312     +}
9313     +
9314     +/* unionfs inode cache constructor */
9315     +static void init_once(void *obj)
9316     +{
9317     + struct unionfs_inode_info *i = obj;
9318     +
9319     + inode_init_once(&i->vfs_inode);
9320     +}
9321     +
9322     +int unionfs_init_inode_cache(void)
9323     +{
9324     + int err = 0;
9325     +
9326     + unionfs_inode_cachep =
9327     + kmem_cache_create("unionfs_inode_cache",
9328     + sizeof(struct unionfs_inode_info), 0,
9329     + SLAB_RECLAIM_ACCOUNT, init_once);
9330     + if (unlikely(!unionfs_inode_cachep))
9331     + err = -ENOMEM;
9332     + return err;
9333     +}
9334     +
9335     +/* unionfs inode cache destructor */
9336     +void unionfs_destroy_inode_cache(void)
9337     +{
9338     + if (unionfs_inode_cachep)
9339     + kmem_cache_destroy(unionfs_inode_cachep);
9340     +}
9341     +
9342     +/*
9343     + * Called when we have a dirty inode, right here we only throw out
9344     + * parts of our readdir list that are too old.
9345     + *
9346     + * No need to grab sb info's rwsem.
9347     + */
9348     +static int unionfs_write_inode(struct inode *inode, int sync)
9349     +{
9350     + struct list_head *pos, *n;
9351     + struct unionfs_dir_state *rdstate;
9352     +
9353     + spin_lock(&UNIONFS_I(inode)->rdlock);
9354     + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9355     + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9356     + /* We keep this list in LRU order. */
9357     + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9358     + break;
9359     + UNIONFS_I(inode)->rdcount--;
9360     + list_del(&rdstate->cache);
9361     + free_rdstate(rdstate);
9362     + }
9363     + spin_unlock(&UNIONFS_I(inode)->rdlock);
9364     +
9365     + return 0;
9366     +}
9367     +
9368     +/*
9369     + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9370     + * code can actually succeed and won't leave tasks that need handling.
9371     + */
9372     +static void unionfs_umount_begin(struct super_block *sb)
9373     +{
9374     + struct super_block *lower_sb;
9375     + int bindex, bstart, bend;
9376     +
9377     + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9378     +
9379     + bstart = sbstart(sb);
9380     + bend = sbend(sb);
9381     + for (bindex = bstart; bindex <= bend; bindex++) {
9382     + lower_sb = unionfs_lower_super_idx(sb, bindex);
9383     +
9384     + if (lower_sb && lower_sb->s_op &&
9385     + lower_sb->s_op->umount_begin)
9386     + lower_sb->s_op->umount_begin(lower_sb);
9387     + }
9388     +
9389     + unionfs_read_unlock(sb);
9390     +}
9391     +
9392     +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9393     +{
9394     + struct super_block *sb = mnt->mnt_sb;
9395     + int ret = 0;
9396     + char *tmp_page;
9397     + char *path;
9398     + int bindex, bstart, bend;
9399     + int perms;
9400     +
9401     + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9402     +
9403     + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9404     +
9405     + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9406     + if (unlikely(!tmp_page)) {
9407     + ret = -ENOMEM;
9408     + goto out;
9409     + }
9410     +
9411     + bstart = sbstart(sb);
9412     + bend = sbend(sb);
9413     +
9414     + seq_printf(m, ",dirs=");
9415     + for (bindex = bstart; bindex <= bend; bindex++) {
9416     + struct path p;
9417     + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9418     + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9419     + path = d_path(&p, tmp_page, PAGE_SIZE);
9420     + if (IS_ERR(path)) {
9421     + ret = PTR_ERR(path);
9422     + goto out;
9423     + }
9424     +
9425     + perms = branchperms(sb, bindex);
9426     +
9427     + seq_printf(m, "%s=%s", path,
9428     + perms & MAY_WRITE ? "rw" : "ro");
9429     + if (bindex != bend)
9430     + seq_printf(m, ":");
9431     + }
9432     +
9433     +out:
9434     + free_page((unsigned long) tmp_page);
9435     +
9436     + unionfs_unlock_dentry(sb->s_root);
9437     +
9438     + unionfs_read_unlock(sb);
9439     +
9440     + return ret;
9441     +}
9442     +
9443     +struct super_operations unionfs_sops = {
9444     + .delete_inode = unionfs_delete_inode,
9445     + .put_super = unionfs_put_super,
9446     + .statfs = unionfs_statfs,
9447     + .remount_fs = unionfs_remount_fs,
9448     + .clear_inode = unionfs_clear_inode,
9449     + .umount_begin = unionfs_umount_begin,
9450     + .show_options = unionfs_show_options,
9451     + .write_inode = unionfs_write_inode,
9452     + .alloc_inode = unionfs_alloc_inode,
9453     + .destroy_inode = unionfs_destroy_inode,
9454     +};
9455     diff -Naur linux-2.6.29/fs/unionfs/union.h linux-2.6.29-magellan/fs/unionfs/union.h
9456     --- linux-2.6.29/fs/unionfs/union.h 1970-01-01 01:00:00.000000000 +0100
9457     +++ linux-2.6.29-magellan/fs/unionfs/union.h 2009-04-23 19:41:06.000000000 +0200
9458     @@ -0,0 +1,650 @@
9459     +/*
9460     + * Copyright (c) 2003-2009 Erez Zadok
9461     + * Copyright (c) 2003-2006 Charles P. Wright
9462     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9463     + * Copyright (c) 2005 Arun M. Krishnakumar
9464     + * Copyright (c) 2004-2006 David P. Quigley
9465     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9466     + * Copyright (c) 2003 Puja Gupta
9467     + * Copyright (c) 2003 Harikesavan Krishnan
9468     + * Copyright (c) 2003-2009 Stony Brook University
9469     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
9470     + *
9471     + * This program is free software; you can redistribute it and/or modify
9472     + * it under the terms of the GNU General Public License version 2 as
9473     + * published by the Free Software Foundation.
9474     + */
9475     +
9476     +#ifndef _UNION_H_
9477     +#define _UNION_H_
9478     +
9479     +#include <linux/dcache.h>
9480     +#include <linux/file.h>
9481     +#include <linux/list.h>
9482     +#include <linux/fs.h>
9483     +#include <linux/mm.h>
9484     +#include <linux/module.h>
9485     +#include <linux/mount.h>
9486     +#include <linux/namei.h>
9487     +#include <linux/page-flags.h>
9488     +#include <linux/pagemap.h>
9489     +#include <linux/poll.h>
9490     +#include <linux/security.h>
9491     +#include <linux/seq_file.h>
9492     +#include <linux/slab.h>
9493     +#include <linux/spinlock.h>
9494     +#include <linux/smp_lock.h>
9495     +#include <linux/statfs.h>
9496     +#include <linux/string.h>
9497     +#include <linux/vmalloc.h>
9498     +#include <linux/writeback.h>
9499     +#include <linux/buffer_head.h>
9500     +#include <linux/xattr.h>
9501     +#include <linux/fs_stack.h>
9502     +#include <linux/magic.h>
9503     +#include <linux/log2.h>
9504     +#include <linux/poison.h>
9505     +#include <linux/mman.h>
9506     +#include <linux/backing-dev.h>
9507     +#include <linux/splice.h>
9508     +
9509     +#include <asm/system.h>
9510     +
9511     +#include <linux/union_fs.h>
9512     +
9513     +/* the file system name */
9514     +#define UNIONFS_NAME "unionfs"
9515     +
9516     +/* unionfs root inode number */
9517     +#define UNIONFS_ROOT_INO 1
9518     +
9519     +/* number of times we try to get a unique temporary file name */
9520     +#define GET_TMPNAM_MAX_RETRY 5
9521     +
9522     +/* maximum number of branches we support, to avoid memory blowup */
9523     +#define UNIONFS_MAX_BRANCHES 128
9524     +
9525     +/* minimum time (seconds) required for time-based cache-coherency */
9526     +#define UNIONFS_MIN_CC_TIME 3
9527     +
9528     +/* Operations vectors defined in specific files. */
9529     +extern struct file_operations unionfs_main_fops;
9530     +extern struct file_operations unionfs_dir_fops;
9531     +extern struct inode_operations unionfs_main_iops;
9532     +extern struct inode_operations unionfs_dir_iops;
9533     +extern struct inode_operations unionfs_symlink_iops;
9534     +extern struct super_operations unionfs_sops;
9535     +extern struct dentry_operations unionfs_dops;
9536     +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9537     +extern struct vm_operations_struct unionfs_vm_ops;
9538     +
9539     +/* How long should an entry be allowed to persist */
9540     +#define RDCACHE_JIFFIES (5*HZ)
9541     +
9542     +/* compatibility with Real-Time patches */
9543     +#ifdef CONFIG_PREEMPT_RT
9544     +# define unionfs_rw_semaphore compat_rw_semaphore
9545     +#else /* not CONFIG_PREEMPT_RT */
9546     +# define unionfs_rw_semaphore rw_semaphore
9547     +#endif /* not CONFIG_PREEMPT_RT */
9548     +
9549     +/* file private data. */
9550     +struct unionfs_file_info {
9551     + int bstart;
9552     + int bend;
9553     + atomic_t generation;
9554     +
9555     + struct unionfs_dir_state *rdstate;
9556     + struct file **lower_files;
9557     + int *saved_branch_ids; /* IDs of branches when file was opened */
9558     + struct vm_operations_struct *lower_vm_ops;
9559     + bool wrote_to_file; /* for delayed copyup */
9560     +};
9561     +
9562     +/* unionfs inode data in memory */
9563     +struct unionfs_inode_info {
9564     + int bstart;
9565     + int bend;
9566     + atomic_t generation;
9567     + /* Stuff for readdir over NFS. */
9568     + spinlock_t rdlock;
9569     + struct list_head readdircache;
9570     + int rdcount;
9571     + int hashsize;
9572     + int cookie;
9573     +
9574     + /* The lower inodes */
9575     + struct inode **lower_inodes;
9576     +
9577     + struct inode vfs_inode;
9578     +};
9579     +
9580     +/* unionfs dentry data in memory */
9581     +struct unionfs_dentry_info {
9582     + /*
9583     + * The semaphore is used to lock the dentry as soon as we get into a
9584     + * unionfs function from the VFS. Our lock ordering is that children
9585     + * go before their parents.
9586     + */
9587     + struct mutex lock;
9588     + int bstart;
9589     + int bend;
9590     + int bopaque;
9591     + int bcount;
9592     + atomic_t generation;
9593     + struct path *lower_paths;
9594     +};
9595     +
9596     +/* These are the pointers to our various objects. */
9597     +struct unionfs_data {
9598     + struct super_block *sb; /* lower super_block */
9599     + atomic_t open_files; /* number of open files on branch */
9600     + int branchperms;
9601     + int branch_id; /* unique branch ID at re/mount time */
9602     +};
9603     +
9604     +/* unionfs super-block data in memory */
9605     +struct unionfs_sb_info {
9606     + int bend;
9607     +
9608     + atomic_t generation;
9609     +
9610     + /*
9611     + * This rwsem is used to make sure that a branch management
9612     + * operation...
9613     + * 1) will not begin before all currently in-flight operations
9614     + * complete.
9615     + * 2) any new operations do not execute until the currently
9616     + * running branch management operation completes.
9617     + *
9618     + * The write_lock_owner records the PID of the task which grabbed
9619     + * the rw_sem for writing. If the same task also tries to grab the
9620     + * read lock, we allow it. This prevents a self-deadlock when
9621     + * branch-management is used on a pivot_root'ed union, because we
9622     + * have to ->lookup paths which belong to the same union.
9623     + */
9624     + struct unionfs_rw_semaphore rwsem;
9625     + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9626     + int high_branch_id; /* last unique branch ID given */
9627     + char *dev_name; /* to identify different unions in pr_debug */
9628     + struct unionfs_data *data;
9629     +};
9630     +
9631     +/*
9632     + * structure for making the linked list of entries by readdir on left branch
9633     + * to compare with entries on right branch
9634     + */
9635     +struct filldir_node {
9636     + struct list_head file_list; /* list for directory entries */
9637     + char *name; /* name entry */
9638     + int hash; /* name hash */
9639     + int namelen; /* name len since name is not 0 terminated */
9640     +
9641     + /*
9642     + * we can check for duplicate whiteouts and files in the same branch
9643     + * in order to return -EIO.
9644     + */
9645     + int bindex;
9646     +
9647     + /* is this a whiteout entry? */
9648     + int whiteout;
9649     +
9650     + /* Inline name, so we don't need to separately kmalloc small ones */
9651     + char iname[DNAME_INLINE_LEN_MIN];
9652     +};
9653     +
9654     +/* Directory hash table. */
9655     +struct unionfs_dir_state {
9656     + unsigned int cookie; /* the cookie, based off of rdversion */
9657     + unsigned int offset; /* The entry we have returned. */
9658     + int bindex;
9659     + loff_t dirpos; /* offset within the lower level directory */
9660     + int size; /* How big is the hash table? */
9661     + int hashentries; /* How many entries have been inserted? */
9662     + unsigned long access;
9663     +
9664     + /* This cache list is used when the inode keeps us around. */
9665     + struct list_head cache;
9666     + struct list_head list[0];
9667     +};
9668     +
9669     +/* externs needed for fanout.h or sioq.h */
9670     +extern int unionfs_get_nlinks(const struct inode *inode);
9671     +extern void unionfs_copy_attr_times(struct inode *upper);
9672     +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9673     +
9674     +/* include miscellaneous macros */
9675     +#include "fanout.h"
9676     +#include "sioq.h"
9677     +
9678     +/* externs for cache creation/deletion routines */
9679     +extern void unionfs_destroy_filldir_cache(void);
9680     +extern int unionfs_init_filldir_cache(void);
9681     +extern int unionfs_init_inode_cache(void);
9682     +extern void unionfs_destroy_inode_cache(void);
9683     +extern int unionfs_init_dentry_cache(void);
9684     +extern void unionfs_destroy_dentry_cache(void);
9685     +
9686     +/* Initialize and free readdir-specific state. */
9687     +extern int init_rdstate(struct file *file);
9688     +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9689     + int bindex);
9690     +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9691     + loff_t fpos);
9692     +extern void free_rdstate(struct unionfs_dir_state *state);
9693     +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9694     + const char *name, int namelen, int bindex,
9695     + int whiteout);
9696     +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9697     + const char *name, int namelen,
9698     + int is_whiteout);
9699     +
9700     +extern struct dentry **alloc_new_dentries(int objs);
9701     +extern struct unionfs_data *alloc_new_data(int objs);
9702     +
9703     +/* We can only use 32-bits of offset for rdstate --- blech! */
9704     +#define DIREOF (0xfffff)
9705     +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9706     +#define MAXRDCOOKIE (0xfff)
9707     +/* Turn an rdstate into an offset. */
9708     +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9709     +{
9710     + off_t tmp;
9711     +
9712     + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9713     + | (buf->offset & DIREOF);
9714     + return tmp;
9715     +}
9716     +
9717     +/* Macros for locking a super_block. */
9718     +enum unionfs_super_lock_class {
9719     + UNIONFS_SMUTEX_NORMAL,
9720     + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9721     + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9722     +};
9723     +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9724     +{
9725     + if (UNIONFS_SB(sb)->write_lock_owner &&
9726     + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9727     + return;
9728     + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9729     +}
9730     +static inline void unionfs_read_unlock(struct super_block *sb)
9731     +{
9732     + if (UNIONFS_SB(sb)->write_lock_owner &&
9733     + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9734     + return;
9735     + up_read(&UNIONFS_SB(sb)->rwsem);
9736     +}
9737     +static inline void unionfs_write_lock(struct super_block *sb)
9738     +{
9739     + down_write(&UNIONFS_SB(sb)->rwsem);
9740     + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9741     +}
9742     +static inline void unionfs_write_unlock(struct super_block *sb)
9743     +{
9744     + up_write(&UNIONFS_SB(sb)->rwsem);
9745     + UNIONFS_SB(sb)->write_lock_owner = 0;
9746     +}
9747     +
9748     +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9749     + struct dentry *d2)
9750     +{
9751     + BUG_ON(d1 == d2);
9752     + if (d1 < d2) {
9753     + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9754     + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9755     + } else {
9756     + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9757     + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9758     + }
9759     +}
9760     +
9761     +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9762     + struct dentry *d2)
9763     +{
9764     + BUG_ON(d1 == d2);
9765     + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9766     + unionfs_unlock_dentry(d1);
9767     + unionfs_unlock_dentry(d2);
9768     + } else {
9769     + unionfs_unlock_dentry(d2);
9770     + unionfs_unlock_dentry(d1);
9771     + }
9772     +}
9773     +
9774     +static inline void unionfs_double_lock_parents(struct dentry *p1,
9775     + struct dentry *p2)
9776     +{
9777     + if (p1 == p2) {
9778     + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9779     + return;
9780     + }
9781     + if (p1 < p2) {
9782     + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9783     + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9784     + } else {
9785     + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9786     + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9787     + }
9788     +}
9789     +
9790     +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9791     + struct dentry *p2)
9792     +{
9793     + if (p1 == p2) {
9794     + unionfs_unlock_dentry(p1);
9795     + return;
9796     + }
9797     + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9798     + unionfs_unlock_dentry(p1);
9799     + unionfs_unlock_dentry(p2);
9800     + } else {
9801     + unionfs_unlock_dentry(p2);
9802     + unionfs_unlock_dentry(p1);
9803     + }
9804     +}
9805     +
9806     +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9807     +extern int realloc_dentry_private_data(struct dentry *dentry);
9808     +extern void free_dentry_private_data(struct dentry *dentry);
9809     +extern void update_bstart(struct dentry *dentry);
9810     +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9811     +extern void release_lower_nd(struct nameidata *nd, int err);
9812     +
9813     +/*
9814     + * EXTERNALS:
9815     + */
9816     +
9817     +/* replicates the directory structure up to given dentry in given branch */
9818     +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9819     + const char *name, int bindex);
9820     +
9821     +/* partial lookup */
9822     +extern int unionfs_partial_lookup(struct dentry *dentry,
9823     + struct dentry *parent);
9824     +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9825     + struct dentry *parent,
9826     + int lookupmode);
9827     +
9828     +/* copies a file from dbstart to newbindex branch */
9829     +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9830     + int newbindex, loff_t size);
9831     +extern int copyup_named_file(struct inode *dir, struct file *file,
9832     + char *name, int bstart, int new_bindex,
9833     + loff_t len);
9834     +/* copies a dentry from dbstart to newbindex branch */
9835     +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9836     + int bstart, int new_bindex, const char *name,
9837     + int namelen, struct file **copyup_file, loff_t len);
9838     +/* helper functions for post-copyup actions */
9839     +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9840     +extern void unionfs_postcopyup_release(struct dentry *dentry);
9841     +
9842     +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9843     +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9844     + struct unionfs_dir_state **namelist);
9845     +/* whiteout and opaque directory helpers */
9846     +extern char *alloc_whname(const char *name, int len);
9847     +extern bool is_whiteout_name(char **namep, int *namelenp);
9848     +extern bool is_validname(const char *name);
9849     +extern struct dentry *lookup_whiteout(const char *name,
9850     + struct dentry *lower_parent);
9851     +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9852     +extern int unlink_whiteout(struct dentry *wh_dentry);
9853     +extern int check_unlink_whiteout(struct dentry *dentry,
9854     + struct dentry *lower_dentry, int bindex);
9855     +extern int create_whiteout(struct dentry *dentry, int start);
9856     +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9857     + struct unionfs_dir_state *namelist);
9858     +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9859     +extern int make_dir_opaque(struct dentry *dir, int bindex);
9860     +extern void unionfs_set_max_namelen(long *namelen);
9861     +
9862     +extern void unionfs_reinterpose(struct dentry *this_dentry);
9863     +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9864     +
9865     +/* Locking functions. */
9866     +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9867     +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9868     +
9869     +/* Common file operations. */
9870     +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9871     + bool willwrite);
9872     +extern int unionfs_open(struct inode *inode, struct file *file);
9873     +extern int unionfs_file_release(struct inode *inode, struct file *file);
9874     +extern int unionfs_flush(struct file *file, fl_owner_t id);
9875     +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9876     + unsigned long arg);
9877     +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
9878     + int datasync);
9879     +extern int unionfs_fasync(int fd, struct file *file, int flag);
9880     +
9881     +/* Inode operations */
9882     +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9883     +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9884     + struct inode *new_dir, struct dentry *new_dentry);
9885     +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
9886     +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
9887     +
9888     +extern bool __unionfs_d_revalidate(struct dentry *dentry,
9889     + struct dentry *parent, bool willwrite);
9890     +extern bool is_negative_lower(const struct dentry *dentry);
9891     +extern bool is_newer_lower(const struct dentry *dentry);
9892     +extern void purge_sb_data(struct super_block *sb);
9893     +
9894     +/* The values for unionfs_interpose's flag. */
9895     +#define INTERPOSE_DEFAULT 0
9896     +#define INTERPOSE_LOOKUP 1
9897     +#define INTERPOSE_REVAL 2
9898     +#define INTERPOSE_REVAL_NEG 3
9899     +#define INTERPOSE_PARTIAL 4
9900     +
9901     +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
9902     + struct super_block *sb, int flag);
9903     +
9904     +#ifdef CONFIG_UNION_FS_XATTR
9905     +/* Extended attribute functions. */
9906     +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
9907     +static inline void unionfs_xattr_kfree(const void *p)
9908     +{
9909     + kfree(p);
9910     +}
9911     +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
9912     + void *value, size_t size);
9913     +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
9914     +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
9915     + size_t size);
9916     +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
9917     + const void *value, size_t size, int flags);
9918     +#endif /* CONFIG_UNION_FS_XATTR */
9919     +
9920     +/* The root directory is unhashed, but isn't deleted. */
9921     +static inline int d_deleted(struct dentry *d)
9922     +{
9923     + return d_unhashed(d) && (d != d->d_sb->s_root);
9924     +}
9925     +
9926     +/* unionfs_permission, check if we should bypass error to facilitate copyup */
9927     +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
9928     +
9929     +/* unionfs_open, check if we need to copyup the file */
9930     +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
9931     +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
9932     +
9933     +static inline int branchperms(const struct super_block *sb, int index)
9934     +{
9935     + BUG_ON(index < 0);
9936     + return UNIONFS_SB(sb)->data[index].branchperms;
9937     +}
9938     +
9939     +static inline int set_branchperms(struct super_block *sb, int index, int perms)
9940     +{
9941     + BUG_ON(index < 0);
9942     + UNIONFS_SB(sb)->data[index].branchperms = perms;
9943     + return perms;
9944     +}
9945     +
9946     +/* Is this file on a read-only branch? */
9947     +static inline int is_robranch_super(const struct super_block *sb, int index)
9948     +{
9949     + int ret;
9950     +
9951     + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
9952     + return ret;
9953     +}
9954     +
9955     +/* Is this file on a read-only branch? */
9956     +static inline int is_robranch_idx(const struct dentry *dentry, int index)
9957     +{
9958     + struct super_block *lower_sb;
9959     +
9960     + BUG_ON(index < 0);
9961     +
9962     + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
9963     + return -EROFS;
9964     +
9965     + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
9966     + BUG_ON(lower_sb == NULL);
9967     + /*
9968     + * test sb flags directly, not IS_RDONLY(lower_inode) because the
9969     + * lower_dentry could be a negative.
9970     + */
9971     + if (lower_sb->s_flags & MS_RDONLY)
9972     + return -EROFS;
9973     +
9974     + return 0;
9975     +}
9976     +
9977     +static inline int is_robranch(const struct dentry *dentry)
9978     +{
9979     + int index;
9980     +
9981     + index = UNIONFS_D(dentry)->bstart;
9982     + BUG_ON(index < 0);
9983     +
9984     + return is_robranch_idx(dentry, index);
9985     +}
9986     +
9987     +/*
9988     + * EXTERNALS:
9989     + */
9990     +extern int check_branch(struct nameidata *nd);
9991     +extern int parse_branch_mode(const char *name, int *perms);
9992     +
9993     +/* locking helpers */
9994     +static inline struct dentry *lock_parent(struct dentry *dentry)
9995     +{
9996     + struct dentry *dir = dget_parent(dentry);
9997     + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
9998     + return dir;
9999     +}
10000     +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10001     +{
10002     + struct dentry *dir = dget_parent(dentry);
10003     +
10004     + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10005     + return dir;
10006     +}
10007     +
10008     +static inline void unlock_dir(struct dentry *dir)
10009     +{
10010     + mutex_unlock(&dir->d_inode->i_mutex);
10011     + dput(dir);
10012     +}
10013     +
10014     +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10015     + int bindex)
10016     +{
10017     + struct vfsmount *mnt;
10018     +
10019     + BUG_ON(!dentry || bindex < 0);
10020     +
10021     + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10022     +#ifdef CONFIG_UNION_FS_DEBUG
10023     + if (!mnt)
10024     + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10025     + mnt, bindex);
10026     +#endif /* CONFIG_UNION_FS_DEBUG */
10027     +
10028     + return mnt;
10029     +}
10030     +
10031     +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10032     +{
10033     + struct vfsmount *mnt;
10034     +
10035     + if (!dentry && bindex < 0)
10036     + return;
10037     + BUG_ON(!dentry || bindex < 0);
10038     +
10039     + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10040     +#ifdef CONFIG_UNION_FS_DEBUG
10041     + /*
10042     + * Directories can have NULL lower objects in between start/end, but
10043     + * NOT if at the start/end range. We cannot verify that this dentry
10044     + * is a type=DIR, because it may already be a negative dentry. But
10045     + * if dbstart is greater than dbend, we know that this couldn't have
10046     + * been a regular file: it had to have been a directory.
10047     + */
10048     + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10049     + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10050     +#endif /* CONFIG_UNION_FS_DEBUG */
10051     + mntput(mnt);
10052     +}
10053     +
10054     +#ifdef CONFIG_UNION_FS_DEBUG
10055     +
10056     +/* useful for tracking code reachability */
10057     +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10058     +
10059     +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10060     + __FILE__, __func__, __LINE__)
10061     +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10062     + __FILE__, __func__, __LINE__)
10063     +#define unionfs_check_file(f) __unionfs_check_file((f), \
10064     + __FILE__, __func__, __LINE__)
10065     +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10066     + __FILE__, __func__, __LINE__)
10067     +#define show_branch_counts(sb) __show_branch_counts((sb), \
10068     + __FILE__, __func__, __LINE__)
10069     +#define show_inode_times(i) __show_inode_times((i), \
10070     + __FILE__, __func__, __LINE__)
10071     +#define show_dinode_times(d) __show_dinode_times((d), \
10072     + __FILE__, __func__, __LINE__)
10073     +#define show_inode_counts(i) __show_inode_counts((i), \
10074     + __FILE__, __func__, __LINE__)
10075     +
10076     +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10077     + const char *fxn, int line);
10078     +extern void __unionfs_check_dentry(const struct dentry *dentry,
10079     + const char *fname, const char *fxn,
10080     + int line);
10081     +extern void __unionfs_check_file(const struct file *file,
10082     + const char *fname, const char *fxn, int line);
10083     +extern void __unionfs_check_nd(const struct nameidata *nd,
10084     + const char *fname, const char *fxn, int line);
10085     +extern void __show_branch_counts(const struct super_block *sb,
10086     + const char *file, const char *fxn, int line);
10087     +extern void __show_inode_times(const struct inode *inode,
10088     + const char *file, const char *fxn, int line);
10089     +extern void __show_dinode_times(const struct dentry *dentry,
10090     + const char *file, const char *fxn, int line);
10091     +extern void __show_inode_counts(const struct inode *inode,
10092     + const char *file, const char *fxn, int line);
10093     +
10094     +#else /* not CONFIG_UNION_FS_DEBUG */
10095     +
10096     +/* we leave useful hooks for these check functions throughout the code */
10097     +#define unionfs_check_inode(i) do { } while (0)
10098     +#define unionfs_check_dentry(d) do { } while (0)
10099     +#define unionfs_check_file(f) do { } while (0)
10100     +#define unionfs_check_nd(n) do { } while (0)
10101     +#define show_branch_counts(sb) do { } while (0)
10102     +#define show_inode_times(i) do { } while (0)
10103     +#define show_dinode_times(d) do { } while (0)
10104     +#define show_inode_counts(i) do { } while (0)
10105     +
10106     +#endif /* not CONFIG_UNION_FS_DEBUG */
10107     +
10108     +#endif /* not _UNION_H_ */
10109     diff -Naur linux-2.6.29/fs/unionfs/unlink.c linux-2.6.29-magellan/fs/unionfs/unlink.c
10110     --- linux-2.6.29/fs/unionfs/unlink.c 1970-01-01 01:00:00.000000000 +0100
10111     +++ linux-2.6.29-magellan/fs/unionfs/unlink.c 2009-04-23 19:41:06.000000000 +0200
10112     @@ -0,0 +1,282 @@
10113     +/*
10114     + * Copyright (c) 2003-2009 Erez Zadok
10115     + * Copyright (c) 2003-2006 Charles P. Wright
10116     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10117     + * Copyright (c) 2005-2006 Junjiro Okajima
10118     + * Copyright (c) 2005 Arun M. Krishnakumar
10119     + * Copyright (c) 2004-2006 David P. Quigley
10120     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10121     + * Copyright (c) 2003 Puja Gupta
10122     + * Copyright (c) 2003 Harikesavan Krishnan
10123     + * Copyright (c) 2003-2009 Stony Brook University
10124     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10125     + *
10126     + * This program is free software; you can redistribute it and/or modify
10127     + * it under the terms of the GNU General Public License version 2 as
10128     + * published by the Free Software Foundation.
10129     + */
10130     +
10131     +#include "union.h"
10132     +
10133     +/*
10134     + * Helper function for Unionfs's unlink operation.
10135     + *
10136     + * The main goal of this function is to optimize the unlinking of non-dir
10137     + * objects in unionfs by deleting all possible lower inode objects from the
10138     + * underlying branches having same dentry name as the non-dir dentry on
10139     + * which this unlink operation is called. This way we delete as many lower
10140     + * inodes as possible, and save space. Whiteouts need to be created in
10141     + * branch0 only if unlinking fails on any of the lower branch other than
10142     + * branch0, or if a lower branch is marked read-only.
10143     + *
10144     + * Also, while unlinking a file, if we encounter any dir type entry in any
10145     + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10146     + * The following special cases are also handled:
10147     +
10148     + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10149     + * appropriate error.
10150     + *
10151     + * (2) If we get an error during unlink in any of other lower branch other
10152     + * than branch0, then we create a whiteout in branch0.
10153     + *
10154     + * (3) If a whiteout already exists in any intermediate branch, we delete
10155     + * all possible inodes only up to that branch (this is an "opaqueness"
10156     + * as as per Documentation/filesystems/unionfs/concepts.txt).
10157     + *
10158     + */
10159     +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10160     + struct dentry *parent)
10161     +{
10162     + struct dentry *lower_dentry;
10163     + struct dentry *lower_dir_dentry;
10164     + int bindex;
10165     + int err = 0;
10166     +
10167     + err = unionfs_partial_lookup(dentry, parent);
10168     + if (err)
10169     + goto out;
10170     +
10171     + /* trying to unlink all possible valid instances */
10172     + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10173     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10174     + if (!lower_dentry || !lower_dentry->d_inode)
10175     + continue;
10176     +
10177     + lower_dir_dentry = lock_parent(lower_dentry);
10178     +
10179     + /* avoid destroying the lower inode if the object is in use */
10180     + dget(lower_dentry);
10181     + err = is_robranch_super(dentry->d_sb, bindex);
10182     + if (!err) {
10183     + /* see Documentation/filesystems/unionfs/issues.txt */
10184     + lockdep_off();
10185     + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10186     + err = vfs_unlink(lower_dir_dentry->d_inode,
10187     + lower_dentry);
10188     + else
10189     + err = vfs_rmdir(lower_dir_dentry->d_inode,
10190     + lower_dentry);
10191     + lockdep_on();
10192     + }
10193     +
10194     + /* if lower object deletion succeeds, update inode's times */
10195     + if (!err)
10196     + unionfs_copy_attr_times(dentry->d_inode);
10197     + dput(lower_dentry);
10198     + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10199     + unlock_dir(lower_dir_dentry);
10200     +
10201     + if (err)
10202     + break;
10203     + }
10204     +
10205     + /*
10206     + * Create the whiteout in branch 0 (highest priority) only if (a)
10207     + * there was an error in any intermediate branch other than branch 0
10208     + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10209     + * mounted read-only.
10210     + */
10211     + if (err) {
10212     + if ((bindex == 0) ||
10213     + ((bindex == dbstart(dentry)) &&
10214     + (!IS_COPYUP_ERR(err))))
10215     + goto out;
10216     + else {
10217     + if (!IS_COPYUP_ERR(err))
10218     + pr_debug("unionfs: lower object deletion "
10219     + "failed in branch:%d\n", bindex);
10220     + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10221     + }
10222     + }
10223     +
10224     +out:
10225     + if (!err)
10226     + inode_dec_link_count(dentry->d_inode);
10227     +
10228     + /* We don't want to leave negative leftover dentries for revalidate. */
10229     + if (!err && (dbopaque(dentry) != -1))
10230     + update_bstart(dentry);
10231     +
10232     + return err;
10233     +}
10234     +
10235     +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10236     +{
10237     + int err = 0;
10238     + struct inode *inode = dentry->d_inode;
10239     + struct dentry *parent;
10240     + int valid;
10241     +
10242     + BUG_ON(S_ISDIR(inode->i_mode));
10243     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10244     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10245     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10246     +
10247     + valid = __unionfs_d_revalidate(dentry, parent, false);
10248     + if (unlikely(!valid)) {
10249     + err = -ESTALE;
10250     + goto out;
10251     + }
10252     + unionfs_check_dentry(dentry);
10253     +
10254     + err = unionfs_unlink_whiteout(dir, dentry, parent);
10255     + /* call d_drop so the system "forgets" about us */
10256     + if (!err) {
10257     + unionfs_postcopyup_release(dentry);
10258     + unionfs_postcopyup_setmnt(parent);
10259     + if (inode->i_nlink == 0) /* drop lower inodes */
10260     + iput_lowers_all(inode, false);
10261     + d_drop(dentry);
10262     + /*
10263     + * if unlink/whiteout succeeded, parent dir mtime has
10264     + * changed
10265     + */
10266     + unionfs_copy_attr_times(dir);
10267     + }
10268     +
10269     +out:
10270     + if (!err) {
10271     + unionfs_check_dentry(dentry);
10272     + unionfs_check_inode(dir);
10273     + }
10274     + unionfs_unlock_dentry(dentry);
10275     + unionfs_unlock_parent(dentry, parent);
10276     + unionfs_read_unlock(dentry->d_sb);
10277     + return err;
10278     +}
10279     +
10280     +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10281     + struct unionfs_dir_state *namelist)
10282     +{
10283     + int err;
10284     + struct dentry *lower_dentry;
10285     + struct dentry *lower_dir_dentry = NULL;
10286     +
10287     + /* Here we need to remove whiteout entries. */
10288     + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10289     + if (err)
10290     + goto out;
10291     +
10292     + lower_dentry = unionfs_lower_dentry(dentry);
10293     +
10294     + lower_dir_dentry = lock_parent(lower_dentry);
10295     +
10296     + /* avoid destroying the lower inode if the file is in use */
10297     + dget(lower_dentry);
10298     + err = is_robranch(dentry);
10299     + if (!err) {
10300     + /* see Documentation/filesystems/unionfs/issues.txt */
10301     + lockdep_off();
10302     + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10303     + lockdep_on();
10304     + }
10305     + dput(lower_dentry);
10306     +
10307     + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10308     + /* propagate number of hard-links */
10309     + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10310     +
10311     +out:
10312     + if (lower_dir_dentry)
10313     + unlock_dir(lower_dir_dentry);
10314     + return err;
10315     +}
10316     +
10317     +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10318     +{
10319     + int err = 0;
10320     + struct unionfs_dir_state *namelist = NULL;
10321     + struct dentry *parent;
10322     + int dstart, dend;
10323     + bool valid;
10324     +
10325     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10326     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10327     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10328     +
10329     + valid = __unionfs_d_revalidate(dentry, parent, false);
10330     + if (unlikely(!valid)) {
10331     + err = -ESTALE;
10332     + goto out;
10333     + }
10334     + unionfs_check_dentry(dentry);
10335     +
10336     + /* check if this unionfs directory is empty or not */
10337     + err = check_empty(dentry, parent, &namelist);
10338     + if (err)
10339     + goto out;
10340     +
10341     + err = unionfs_rmdir_first(dir, dentry, namelist);
10342     + dstart = dbstart(dentry);
10343     + dend = dbend(dentry);
10344     + /*
10345     + * We create a whiteout for the directory if there was an error to
10346     + * rmdir the first directory entry in the union. Otherwise, we
10347     + * create a whiteout only if there is no chance that a lower
10348     + * priority branch might also have the same named directory. IOW,
10349     + * if there is not another same-named directory at a lower priority
10350     + * branch, then we don't need to create a whiteout for it.
10351     + */
10352     + if (!err) {
10353     + if (dstart < dend)
10354     + err = create_whiteout(dentry, dstart);
10355     + } else {
10356     + int new_err;
10357     +
10358     + if (dstart == 0)
10359     + goto out;
10360     +
10361     + /* exit if the error returned was NOT -EROFS */
10362     + if (!IS_COPYUP_ERR(err))
10363     + goto out;
10364     +
10365     + new_err = create_whiteout(dentry, dstart - 1);
10366     + if (new_err != -EEXIST)
10367     + err = new_err;
10368     + }
10369     +
10370     +out:
10371     + /*
10372     + * Drop references to lower dentry/inode so storage space for them
10373     + * can be reclaimed. Then, call d_drop so the system "forgets"
10374     + * about us.
10375     + */
10376     + if (!err) {
10377     + iput_lowers_all(dentry->d_inode, false);
10378     + dput(unionfs_lower_dentry_idx(dentry, dstart));
10379     + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10380     + d_drop(dentry);
10381     + /* update our lower vfsmnts, in case a copyup took place */
10382     + unionfs_postcopyup_setmnt(dentry);
10383     + unionfs_check_dentry(dentry);
10384     + unionfs_check_inode(dir);
10385     + }
10386     +
10387     + if (namelist)
10388     + free_rdstate(namelist);
10389     +
10390     + unionfs_unlock_dentry(dentry);
10391     + unionfs_unlock_parent(dentry, parent);
10392     + unionfs_read_unlock(dentry->d_sb);
10393     + return err;
10394     +}
10395     diff -Naur linux-2.6.29/fs/unionfs/whiteout.c linux-2.6.29-magellan/fs/unionfs/whiteout.c
10396     --- linux-2.6.29/fs/unionfs/whiteout.c 1970-01-01 01:00:00.000000000 +0100
10397     +++ linux-2.6.29-magellan/fs/unionfs/whiteout.c 2009-04-23 19:41:06.000000000 +0200
10398     @@ -0,0 +1,584 @@
10399     +/*
10400     + * Copyright (c) 2003-2009 Erez Zadok
10401     + * Copyright (c) 2003-2006 Charles P. Wright
10402     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10403     + * Copyright (c) 2005-2006 Junjiro Okajima
10404     + * Copyright (c) 2005 Arun M. Krishnakumar
10405     + * Copyright (c) 2004-2006 David P. Quigley
10406     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10407     + * Copyright (c) 2003 Puja Gupta
10408     + * Copyright (c) 2003 Harikesavan Krishnan
10409     + * Copyright (c) 2003-2009 Stony Brook University
10410     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10411     + *
10412     + * This program is free software; you can redistribute it and/or modify
10413     + * it under the terms of the GNU General Public License version 2 as
10414     + * published by the Free Software Foundation.
10415     + */
10416     +
10417     +#include "union.h"
10418     +
10419     +/*
10420     + * whiteout and opaque directory helpers
10421     + */
10422     +
10423     +/* What do we use for whiteouts. */
10424     +#define UNIONFS_WHPFX ".wh."
10425     +#define UNIONFS_WHLEN 4
10426     +/*
10427     + * If a directory contains this file, then it is opaque. We start with the
10428     + * .wh. flag so that it is blocked by lookup.
10429     + */
10430     +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10431     +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10432     +
10433     +/* construct whiteout filename */
10434     +char *alloc_whname(const char *name, int len)
10435     +{
10436     + char *buf;
10437     +
10438     + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10439     + if (unlikely(!buf))
10440     + return ERR_PTR(-ENOMEM);
10441     +
10442     + strcpy(buf, UNIONFS_WHPFX);
10443     + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10444     +
10445     + return buf;
10446     +}
10447     +
10448     +/*
10449     + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10450     + * code in one place.
10451     + */
10452     +void unionfs_set_max_namelen(long *namelen)
10453     +{
10454     + *namelen -= UNIONFS_WHLEN;
10455     +}
10456     +
10457     +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10458     +bool is_whiteout_name(char **namep, int *namelenp)
10459     +{
10460     + if (*namelenp > UNIONFS_WHLEN &&
10461     + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10462     + *namep += UNIONFS_WHLEN;
10463     + *namelenp -= UNIONFS_WHLEN;
10464     + return true;
10465     + }
10466     + return false;
10467     +}
10468     +
10469     +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10470     +bool is_validname(const char *name)
10471     +{
10472     + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10473     + return false;
10474     + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10475     + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10476     + return false;
10477     + return true;
10478     +}
10479     +
10480     +/*
10481     + * Look for a whiteout @name in @lower_parent directory. If error, return
10482     + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10483     + *
10484     + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10485     + * free then re-malloc calls. Need to provide a different API for those
10486     + * callers.
10487     + */
10488     +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10489     +{
10490     + char *whname = NULL;
10491     + int err = 0, namelen;
10492     + struct dentry *wh_dentry = NULL;
10493     +
10494     + namelen = strlen(name);
10495     + whname = alloc_whname(name, namelen);
10496     + if (unlikely(IS_ERR(whname))) {
10497     + err = PTR_ERR(whname);
10498     + goto out;
10499     + }
10500     +
10501     + /* check if whiteout exists in this branch: lookup .wh.foo */
10502     + wh_dentry = lookup_one_len(whname, lower_parent, strlen(whname));
10503     + if (IS_ERR(wh_dentry)) {
10504     + err = PTR_ERR(wh_dentry);
10505     + goto out;
10506     + }
10507     +
10508     + /* check if negative dentry (ENOENT) */
10509     + if (!wh_dentry->d_inode)
10510     + goto out;
10511     +
10512     + /* whiteout found: check if valid type */
10513     + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10514     + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10515     + whname, wh_dentry->d_inode->i_mode);
10516     + dput(wh_dentry);
10517     + err = -EIO;
10518     + goto out;
10519     + }
10520     +
10521     +out:
10522     + kfree(whname);
10523     + if (err)
10524     + wh_dentry = ERR_PTR(err);
10525     + return wh_dentry;
10526     +}
10527     +
10528     +/* find and return first whiteout in parent directory, else ENOENT */
10529     +struct dentry *find_first_whiteout(struct dentry *dentry)
10530     +{
10531     + int bindex, bstart, bend;
10532     + struct dentry *parent, *lower_parent, *wh_dentry;
10533     +
10534     + parent = dget_parent(dentry);
10535     +
10536     + bstart = dbstart(parent);
10537     + bend = dbend(parent);
10538     + wh_dentry = ERR_PTR(-ENOENT);
10539     +
10540     + for (bindex = bstart; bindex <= bend; bindex++) {
10541     + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10542     + if (!lower_parent)
10543     + continue;
10544     + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10545     + if (IS_ERR(wh_dentry))
10546     + continue;
10547     + if (wh_dentry->d_inode)
10548     + break;
10549     + dput(wh_dentry);
10550     + wh_dentry = ERR_PTR(-ENOENT);
10551     + }
10552     +
10553     + dput(parent);
10554     +
10555     + return wh_dentry;
10556     +}
10557     +
10558     +/*
10559     + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10560     + * release dentry reference.
10561     + */
10562     +int unlink_whiteout(struct dentry *wh_dentry)
10563     +{
10564     + int err;
10565     + struct dentry *lower_dir_dentry;
10566     +
10567     + /* dget and lock parent dentry */
10568     + lower_dir_dentry = lock_parent_wh(wh_dentry);
10569     +
10570     + /* see Documentation/filesystems/unionfs/issues.txt */
10571     + lockdep_off();
10572     + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10573     + lockdep_on();
10574     + unlock_dir(lower_dir_dentry);
10575     +
10576     + /*
10577     + * Whiteouts are special files and should be deleted no matter what
10578     + * (as if they never existed), in order to allow this create
10579     + * operation to succeed. This is especially important in sticky
10580     + * directories: a whiteout may have been created by one user, but
10581     + * the newly created file may be created by another user.
10582     + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10583     + * above failed, then we have to try to directly unlink the
10584     + * whiteout. Note: in the ODF version of unionfs, whiteout are
10585     + * handled much more cleanly.
10586     + */
10587     + if (err == -EPERM) {
10588     + struct inode *inode = lower_dir_dentry->d_inode;
10589     + err = inode->i_op->unlink(inode, wh_dentry);
10590     + }
10591     + if (err)
10592     + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10593     + "err = %d\n", wh_dentry->d_name.name, err);
10594     +
10595     + return err;
10596     +
10597     +}
10598     +
10599     +/*
10600     + * Helper function when creating new objects (create, symlink, mknod, etc.).
10601     + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10602     + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10603     + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10604     + * If it finds both a regular file and a whiteout, return -EIO (this should
10605     + * never happen).
10606     + *
10607     + * Return 0 if no whiteout was found. Return 1 if one was found and
10608     + * successfully removed. Therefore a value >= 0 tells the caller that
10609     + * @lower_dentry belongs to a good branch to create the new object in).
10610     + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10611     + * unlink the whiteout.
10612     + */
10613     +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10614     + int bindex)
10615     +{
10616     + int err;
10617     + struct dentry *wh_dentry = NULL;
10618     + struct dentry *lower_dir_dentry = NULL;
10619     +
10620     + /* look for whiteout dentry first */
10621     + lower_dir_dentry = dget_parent(lower_dentry);
10622     + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10623     + dput(lower_dir_dentry);
10624     + if (IS_ERR(wh_dentry)) {
10625     + err = PTR_ERR(wh_dentry);
10626     + goto out;
10627     + }
10628     +
10629     + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10630     + err = 0;
10631     + goto out_dput;
10632     + }
10633     +
10634     + /* check if regular file and whiteout were both found */
10635     + if (unlikely(lower_dentry->d_inode)) {
10636     + err = -EIO;
10637     + printk(KERN_ERR "unionfs: found both whiteout and regular "
10638     + "file in directory %s (branch %d)\n",
10639     + lower_dir_dentry->d_name.name, bindex);
10640     + goto out_dput;
10641     + }
10642     +
10643     + /* check if branch is writeable */
10644     + err = is_robranch_super(dentry->d_sb, bindex);
10645     + if (err)
10646     + goto out_dput;
10647     +
10648     + /* .wh.foo has been found, so let's unlink it */
10649     + err = unlink_whiteout(wh_dentry);
10650     + if (!err)
10651     + err = 1; /* a whiteout was found and successfully removed */
10652     +out_dput:
10653     + dput(wh_dentry);
10654     +out:
10655     + return err;
10656     +}
10657     +
10658     +/*
10659     + * Pass an unionfs dentry and an index. It will try to create a whiteout
10660     + * for the filename in dentry, and will try in branch 'index'. On error,
10661     + * it will proceed to a branch to the left.
10662     + */
10663     +int create_whiteout(struct dentry *dentry, int start)
10664     +{
10665     + int bstart, bend, bindex;
10666     + struct dentry *lower_dir_dentry;
10667     + struct dentry *lower_dentry;
10668     + struct dentry *lower_wh_dentry;
10669     + struct nameidata nd;
10670     + char *name = NULL;
10671     + int err = -EINVAL;
10672     +
10673     + verify_locked(dentry);
10674     +
10675     + bstart = dbstart(dentry);
10676     + bend = dbend(dentry);
10677     +
10678     + /* create dentry's whiteout equivalent */
10679     + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10680     + if (unlikely(IS_ERR(name))) {
10681     + err = PTR_ERR(name);
10682     + goto out;
10683     + }
10684     +
10685     + for (bindex = start; bindex >= 0; bindex--) {
10686     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10687     +
10688     + if (!lower_dentry) {
10689     + /*
10690     + * if lower dentry is not present, create the
10691     + * entire lower dentry directory structure and go
10692     + * ahead. Since we want to just create whiteout, we
10693     + * only want the parent dentry, and hence get rid of
10694     + * this dentry.
10695     + */
10696     + lower_dentry = create_parents(dentry->d_inode,
10697     + dentry,
10698     + dentry->d_name.name,
10699     + bindex);
10700     + if (!lower_dentry || IS_ERR(lower_dentry)) {
10701     + int ret = PTR_ERR(lower_dentry);
10702     + if (!IS_COPYUP_ERR(ret))
10703     + printk(KERN_ERR
10704     + "unionfs: create_parents for "
10705     + "whiteout failed: bindex=%d "
10706     + "err=%d\n", bindex, ret);
10707     + continue;
10708     + }
10709     + }
10710     +
10711     + lower_wh_dentry =
10712     + lookup_one_len(name, lower_dentry->d_parent,
10713     + dentry->d_name.len + UNIONFS_WHLEN);
10714     + if (IS_ERR(lower_wh_dentry))
10715     + continue;
10716     +
10717     + /*
10718     + * The whiteout already exists. This used to be impossible,
10719     + * but now is possible because of opaqueness.
10720     + */
10721     + if (lower_wh_dentry->d_inode) {
10722     + dput(lower_wh_dentry);
10723     + err = 0;
10724     + goto out;
10725     + }
10726     +
10727     + err = init_lower_nd(&nd, LOOKUP_CREATE);
10728     + if (unlikely(err < 0))
10729     + goto out;
10730     + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10731     + err = is_robranch_super(dentry->d_sb, bindex);
10732     + if (!err)
10733     + err = vfs_create(lower_dir_dentry->d_inode,
10734     + lower_wh_dentry,
10735     + ~current->fs->umask & S_IRUGO,
10736     + &nd);
10737     + unlock_dir(lower_dir_dentry);
10738     + dput(lower_wh_dentry);
10739     + release_lower_nd(&nd, err);
10740     +
10741     + if (!err || !IS_COPYUP_ERR(err))
10742     + break;
10743     + }
10744     +
10745     + /* set dbopaque so that lookup will not proceed after this branch */
10746     + if (!err)
10747     + dbopaque(dentry) = bindex;
10748     +
10749     +out:
10750     + kfree(name);
10751     + return err;
10752     +}
10753     +
10754     +/*
10755     + * Delete all of the whiteouts in a given directory for rmdir.
10756     + *
10757     + * lower directory inode should be locked
10758     + */
10759     +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10760     + struct unionfs_dir_state *namelist)
10761     +{
10762     + int err = 0;
10763     + struct dentry *lower_dir_dentry = NULL;
10764     + struct dentry *lower_dentry;
10765     + char *name = NULL, *p;
10766     + struct inode *lower_dir;
10767     + int i;
10768     + struct list_head *pos;
10769     + struct filldir_node *cursor;
10770     +
10771     + /* Find out lower parent dentry */
10772     + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10773     + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10774     + lower_dir = lower_dir_dentry->d_inode;
10775     + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10776     +
10777     + err = -ENOMEM;
10778     + name = __getname();
10779     + if (unlikely(!name))
10780     + goto out;
10781     + strcpy(name, UNIONFS_WHPFX);
10782     + p = name + UNIONFS_WHLEN;
10783     +
10784     + err = 0;
10785     + for (i = 0; !err && i < namelist->size; i++) {
10786     + list_for_each(pos, &namelist->list[i]) {
10787     + cursor =
10788     + list_entry(pos, struct filldir_node,
10789     + file_list);
10790     + /* Only operate on whiteouts in this branch. */
10791     + if (cursor->bindex != bindex)
10792     + continue;
10793     + if (!cursor->whiteout)
10794     + continue;
10795     +
10796     + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10797     + lower_dentry =
10798     + lookup_one_len(name, lower_dir_dentry,
10799     + cursor->namelen +
10800     + UNIONFS_WHLEN);
10801     + if (IS_ERR(lower_dentry)) {
10802     + err = PTR_ERR(lower_dentry);
10803     + break;
10804     + }
10805     + if (lower_dentry->d_inode)
10806     + err = vfs_unlink(lower_dir, lower_dentry);
10807     + dput(lower_dentry);
10808     + if (err)
10809     + break;
10810     + }
10811     + }
10812     +
10813     + __putname(name);
10814     +
10815     + /* After all of the removals, we should copy the attributes once. */
10816     + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10817     +
10818     +out:
10819     + return err;
10820     +}
10821     +
10822     +
10823     +void __delete_whiteouts(struct work_struct *work)
10824     +{
10825     + struct sioq_args *args = container_of(work, struct sioq_args, work);
10826     + struct deletewh_args *d = &args->deletewh;
10827     +
10828     + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10829     + complete(&args->comp);
10830     +}
10831     +
10832     +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10833     +int delete_whiteouts(struct dentry *dentry, int bindex,
10834     + struct unionfs_dir_state *namelist)
10835     +{
10836     + int err;
10837     + struct super_block *sb;
10838     + struct dentry *lower_dir_dentry;
10839     + struct inode *lower_dir;
10840     + struct sioq_args args;
10841     +
10842     + sb = dentry->d_sb;
10843     +
10844     + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10845     + BUG_ON(bindex < dbstart(dentry));
10846     + BUG_ON(bindex > dbend(dentry));
10847     + err = is_robranch_super(sb, bindex);
10848     + if (err)
10849     + goto out;
10850     +
10851     + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10852     + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10853     + lower_dir = lower_dir_dentry->d_inode;
10854     + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10855     +
10856     + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10857     + err = do_delete_whiteouts(dentry, bindex, namelist);
10858     + } else {
10859     + args.deletewh.namelist = namelist;
10860     + args.deletewh.dentry = dentry;
10861     + args.deletewh.bindex = bindex;
10862     + run_sioq(__delete_whiteouts, &args);
10863     + err = args.err;
10864     + }
10865     +
10866     +out:
10867     + return err;
10868     +}
10869     +
10870     +/****************************************************************************
10871     + * Opaque directory helpers *
10872     + ****************************************************************************/
10873     +
10874     +/*
10875     + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
10876     + * -errno if an error occurred trying to figure this out.
10877     + */
10878     +int is_opaque_dir(struct dentry *dentry, int bindex)
10879     +{
10880     + int err = 0;
10881     + struct dentry *lower_dentry;
10882     + struct dentry *wh_lower_dentry;
10883     + struct inode *lower_inode;
10884     + struct sioq_args args;
10885     +
10886     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10887     + lower_inode = lower_dentry->d_inode;
10888     +
10889     + BUG_ON(!S_ISDIR(lower_inode->i_mode));
10890     +
10891     + mutex_lock(&lower_inode->i_mutex);
10892     +
10893     + if (!inode_permission(lower_inode, MAY_EXEC)) {
10894     + wh_lower_dentry =
10895     + lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
10896     + sizeof(UNIONFS_DIR_OPAQUE) - 1);
10897     + } else {
10898     + args.is_opaque.dentry = lower_dentry;
10899     + run_sioq(__is_opaque_dir, &args);
10900     + wh_lower_dentry = args.ret;
10901     + }
10902     +
10903     + mutex_unlock(&lower_inode->i_mutex);
10904     +
10905     + if (IS_ERR(wh_lower_dentry)) {
10906     + err = PTR_ERR(wh_lower_dentry);
10907     + goto out;
10908     + }
10909     +
10910     + /* This is an opaque dir iff wh_lower_dentry is positive */
10911     + err = !!wh_lower_dentry->d_inode;
10912     +
10913     + dput(wh_lower_dentry);
10914     +out:
10915     + return err;
10916     +}
10917     +
10918     +void __is_opaque_dir(struct work_struct *work)
10919     +{
10920     + struct sioq_args *args = container_of(work, struct sioq_args, work);
10921     +
10922     + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
10923     + sizeof(UNIONFS_DIR_OPAQUE) - 1);
10924     + complete(&args->comp);
10925     +}
10926     +
10927     +int make_dir_opaque(struct dentry *dentry, int bindex)
10928     +{
10929     + int err = 0;
10930     + struct dentry *lower_dentry, *diropq;
10931     + struct inode *lower_dir;
10932     + struct nameidata nd;
10933     + const struct cred *old_creds;
10934     + struct cred *new_creds;
10935     +
10936     + /*
10937     + * Opaque directory whiteout markers are special files (like regular
10938     + * whiteouts), and should appear to the users as if they don't
10939     + * exist. They should be created/deleted regardless of directory
10940     + * search/create permissions, but only for the duration of this
10941     + * creation of the .wh.__dir_opaque: file. Note, this does not
10942     + * circumvent normal ->permission).
10943     + */
10944     + new_creds = prepare_creds();
10945     + if (unlikely(!new_creds)) {
10946     + err = -ENOMEM;
10947     + goto out_err;
10948     + }
10949     + cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
10950     + cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
10951     + old_creds = override_creds(new_creds);
10952     +
10953     + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10954     + lower_dir = lower_dentry->d_inode;
10955     + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
10956     + !S_ISDIR(lower_dir->i_mode));
10957     +
10958     + mutex_lock(&lower_dir->i_mutex);
10959     + diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
10960     + sizeof(UNIONFS_DIR_OPAQUE) - 1);
10961     + if (IS_ERR(diropq)) {
10962     + err = PTR_ERR(diropq);
10963     + goto out;
10964     + }
10965     +
10966     + err = init_lower_nd(&nd, LOOKUP_CREATE);
10967     + if (unlikely(err < 0))
10968     + goto out;
10969     + if (!diropq->d_inode)
10970     + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
10971     + if (!err)
10972     + dbopaque(dentry) = bindex;
10973     + release_lower_nd(&nd, err);
10974     +
10975     + dput(diropq);
10976     +
10977     +out:
10978     + mutex_unlock(&lower_dir->i_mutex);
10979     + revert_creds(old_creds);
10980     +out_err:
10981     + return err;
10982     +}
10983     diff -Naur linux-2.6.29/fs/unionfs/xattr.c linux-2.6.29-magellan/fs/unionfs/xattr.c
10984     --- linux-2.6.29/fs/unionfs/xattr.c 1970-01-01 01:00:00.000000000 +0100
10985     +++ linux-2.6.29-magellan/fs/unionfs/xattr.c 2009-04-23 19:41:06.000000000 +0200
10986     @@ -0,0 +1,173 @@
10987     +/*
10988     + * Copyright (c) 2003-2009 Erez Zadok
10989     + * Copyright (c) 2003-2006 Charles P. Wright
10990     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10991     + * Copyright (c) 2005-2006 Junjiro Okajima
10992     + * Copyright (c) 2005 Arun M. Krishnakumar
10993     + * Copyright (c) 2004-2006 David P. Quigley
10994     + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10995     + * Copyright (c) 2003 Puja Gupta
10996     + * Copyright (c) 2003 Harikesavan Krishnan
10997     + * Copyright (c) 2003-2009 Stony Brook University
10998     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10999     + *
11000     + * This program is free software; you can redistribute it and/or modify
11001     + * it under the terms of the GNU General Public License version 2 as
11002     + * published by the Free Software Foundation.
11003     + */
11004     +
11005     +#include "union.h"
11006     +
11007     +/* This is lifted from fs/xattr.c */
11008     +void *unionfs_xattr_alloc(size_t size, size_t limit)
11009     +{
11010     + void *ptr;
11011     +
11012     + if (size > limit)
11013     + return ERR_PTR(-E2BIG);
11014     +
11015     + if (!size) /* size request, no buffer is needed */
11016     + return NULL;
11017     +
11018     + ptr = kmalloc(size, GFP_KERNEL);
11019     + if (unlikely(!ptr))
11020     + return ERR_PTR(-ENOMEM);
11021     + return ptr;
11022     +}
11023     +
11024     +/*
11025     + * BKL held by caller.
11026     + * dentry->d_inode->i_mutex locked
11027     + */
11028     +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11029     + size_t size)
11030     +{
11031     + struct dentry *lower_dentry = NULL;
11032     + struct dentry *parent;
11033     + int err = -EOPNOTSUPP;
11034     + bool valid;
11035     +
11036     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11037     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11038     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11039     +
11040     + valid = __unionfs_d_revalidate(dentry, parent, false);
11041     + if (unlikely(!valid)) {
11042     + err = -ESTALE;
11043     + goto out;
11044     + }
11045     +
11046     + lower_dentry = unionfs_lower_dentry(dentry);
11047     +
11048     + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11049     +
11050     +out:
11051     + unionfs_check_dentry(dentry);
11052     + unionfs_unlock_dentry(dentry);
11053     + unionfs_unlock_parent(dentry, parent);
11054     + unionfs_read_unlock(dentry->d_sb);
11055     + return err;
11056     +}
11057     +
11058     +/*
11059     + * BKL held by caller.
11060     + * dentry->d_inode->i_mutex locked
11061     + */
11062     +int unionfs_setxattr(struct dentry *dentry, const char *name,
11063     + const void *value, size_t size, int flags)
11064     +{
11065     + struct dentry *lower_dentry = NULL;
11066     + struct dentry *parent;
11067     + int err = -EOPNOTSUPP;
11068     + bool valid;
11069     +
11070     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11071     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11072     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11073     +
11074     + valid = __unionfs_d_revalidate(dentry, parent, false);
11075     + if (unlikely(!valid)) {
11076     + err = -ESTALE;
11077     + goto out;
11078     + }
11079     +
11080     + lower_dentry = unionfs_lower_dentry(dentry);
11081     +
11082     + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11083     + size, flags);
11084     +
11085     +out:
11086     + unionfs_check_dentry(dentry);
11087     + unionfs_unlock_dentry(dentry);
11088     + unionfs_unlock_parent(dentry, parent);
11089     + unionfs_read_unlock(dentry->d_sb);
11090     + return err;
11091     +}
11092     +
11093     +/*
11094     + * BKL held by caller.
11095     + * dentry->d_inode->i_mutex locked
11096     + */
11097     +int unionfs_removexattr(struct dentry *dentry, const char *name)
11098     +{
11099     + struct dentry *lower_dentry = NULL;
11100     + struct dentry *parent;
11101     + int err = -EOPNOTSUPP;
11102     + bool valid;
11103     +
11104     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11105     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11106     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11107     +
11108     + valid = __unionfs_d_revalidate(dentry, parent, false);
11109     + if (unlikely(!valid)) {
11110     + err = -ESTALE;
11111     + goto out;
11112     + }
11113     +
11114     + lower_dentry = unionfs_lower_dentry(dentry);
11115     +
11116     + err = vfs_removexattr(lower_dentry, (char *) name);
11117     +
11118     +out:
11119     + unionfs_check_dentry(dentry);
11120     + unionfs_unlock_dentry(dentry);
11121     + unionfs_unlock_parent(dentry, parent);
11122     + unionfs_read_unlock(dentry->d_sb);
11123     + return err;
11124     +}
11125     +
11126     +/*
11127     + * BKL held by caller.
11128     + * dentry->d_inode->i_mutex locked
11129     + */
11130     +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11131     +{
11132     + struct dentry *lower_dentry = NULL;
11133     + struct dentry *parent;
11134     + int err = -EOPNOTSUPP;
11135     + char *encoded_list = NULL;
11136     + bool valid;
11137     +
11138     + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11139     + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11140     + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11141     +
11142     + valid = __unionfs_d_revalidate(dentry, parent, false);
11143     + if (unlikely(!valid)) {
11144     + err = -ESTALE;
11145     + goto out;
11146     + }
11147     +
11148     + lower_dentry = unionfs_lower_dentry(dentry);
11149     +
11150     + encoded_list = list;
11151     + err = vfs_listxattr(lower_dentry, encoded_list, size);
11152     +
11153     +out:
11154     + unionfs_check_dentry(dentry);
11155     + unionfs_unlock_dentry(dentry);
11156     + unionfs_unlock_parent(dentry, parent);
11157     + unionfs_read_unlock(dentry->d_sb);
11158     + return err;
11159     +}
11160     diff -Naur linux-2.6.29/include/linux/fs_stack.h linux-2.6.29-magellan/include/linux/fs_stack.h
11161     --- linux-2.6.29/include/linux/fs_stack.h 2009-03-24 00:12:14.000000000 +0100
11162     +++ linux-2.6.29-magellan/include/linux/fs_stack.h 2009-04-23 19:41:06.000000000 +0200
11163     @@ -1,17 +1,27 @@
11164     +/*
11165     + * Copyright (c) 2006-2009 Erez Zadok
11166     + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11167     + * Copyright (c) 2006-2009 Stony Brook University
11168     + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11169     + *
11170     + * This program is free software; you can redistribute it and/or modify
11171     + * it under the terms of the GNU General Public License version 2 as
11172     + * published by the Free Software Foundation.
11173     + */
11174     +
11175     #ifndef _LINUX_FS_STACK_H
11176     #define _LINUX_FS_STACK_H
11177    
11178     -/* This file defines generic functions used primarily by stackable
11179     +/*
11180     + * This file defines generic functions used primarily by stackable
11181     * filesystems; none of these functions require i_mutex to be held.
11182     */
11183    
11184     #include <linux/fs.h>
11185    
11186     /* externs for fs/stack.c */
11187     -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
11188     - int (*get_nlinks)(struct inode *));
11189     -
11190     -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
11191     +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
11192     +extern void fsstack_copy_inode_size(struct inode *dst, struct inode *src);
11193    
11194     /* inlines */
11195     static inline void fsstack_copy_attr_atime(struct inode *dest,
11196     diff -Naur linux-2.6.29/include/linux/magic.h linux-2.6.29-magellan/include/linux/magic.h
11197     --- linux-2.6.29/include/linux/magic.h 2009-03-24 00:12:14.000000000 +0100
11198     +++ linux-2.6.29-magellan/include/linux/magic.h 2009-04-23 19:41:06.000000000 +0200
11199     @@ -42,6 +42,8 @@
11200     #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11201     #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11202    
11203     +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11204     +
11205     #define SMB_SUPER_MAGIC 0x517B
11206     #define USBDEVICE_SUPER_MAGIC 0x9fa2
11207     #define CGROUP_SUPER_MAGIC 0x27e0eb
11208     diff -Naur linux-2.6.29/include/linux/splice.h linux-2.6.29-magellan/include/linux/splice.h
11209     --- linux-2.6.29/include/linux/splice.h 2009-03-24 00:12:14.000000000 +0100
11210     +++ linux-2.6.29-magellan/include/linux/splice.h 2009-04-23 19:41:06.000000000 +0200
11211     @@ -70,5 +70,10 @@
11212     struct splice_pipe_desc *);
11213     extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11214     splice_direct_actor *);
11215     +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11216     + loff_t *ppos, size_t len, unsigned int flags);
11217     +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11218     + struct pipe_inode_info *pipe, size_t len,
11219     + unsigned int flags);
11220    
11221     #endif
11222     diff -Naur linux-2.6.29/include/linux/union_fs.h linux-2.6.29-magellan/include/linux/union_fs.h
11223     --- linux-2.6.29/include/linux/union_fs.h 1970-01-01 01:00:00.000000000 +0100
11224     +++ linux-2.6.29-magellan/include/linux/union_fs.h 2009-04-23 19:41:06.000000000 +0200
11225     @@ -0,0 +1,22 @@
11226     +/*
11227     + * Copyright (c) 2003-2009 Erez Zadok
11228     + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11229     + * Copyright (c) 2003-2009 Stony Brook University
11230     + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11231     + *
11232     + * This program is free software; you can redistribute it and/or modify
11233     + * it under the terms of the GNU General Public License version 2 as
11234     + * published by the Free Software Foundation.
11235     + */
11236     +
11237     +#ifndef _LINUX_UNION_FS_H
11238     +#define _LINUX_UNION_FS_H
11239     +
11240     +/*
11241     + * DEFINITIONS FOR USER AND KERNEL CODE:
11242     + */
11243     +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11244     +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11245     +
11246     +#endif /* _LINUX_UNIONFS_H */
11247     +
11248     diff -Naur linux-2.6.29/MAINTAINERS linux-2.6.29-magellan/MAINTAINERS
11249     --- linux-2.6.29/MAINTAINERS 2009-03-24 00:12:14.000000000 +0100
11250     +++ linux-2.6.29-magellan/MAINTAINERS 2009-04-23 19:41:06.000000000 +0200
11251     @@ -4397,6 +4397,14 @@
11252     W: http://www.kernel.dk
11253     S: Maintained
11254    
11255     +UNIONFS
11256     +P: Erez Zadok
11257     +M: ezk@cs.sunysb.edu
11258     +L: unionfs@filesystems.org
11259     +W: http://unionfs.filesystems.org/
11260     +T: git git.kernel.org/pub/scm/linux/kernel/git/ezk/unionfs.git
11261     +S: Maintained
11262     +
11263     UNSORTED BLOCK IMAGES (UBI)
11264     P: Artem Bityutskiy
11265     M: dedekind@infradead.org
11266     diff -Naur linux-2.6.29/security/security.c linux-2.6.29-magellan/security/security.c
11267     --- linux-2.6.29/security/security.c 2009-03-24 00:12:14.000000000 +0100
11268     +++ linux-2.6.29-magellan/security/security.c 2009-04-23 19:41:06.000000000 +0200
11269     @@ -520,6 +520,7 @@
11270     return 0;
11271     return security_ops->inode_permission(inode, mask);
11272     }
11273     +EXPORT_SYMBOL(security_inode_permission);
11274    
11275     int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
11276     {