Magellan Linux

Annotation of /trunk/kernel26-magellan/patches-2.6.35-r2/0153-2.6.35-unionfs-2.5.5.patch

Parent Directory Parent Directory | Revision Log Revision Log


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