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