Magellan Linux

Contents of /trunk/kernel26-alx/patches-2.6.23-r1/0153-2.6.23-unionfs-2.3.3.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 658 - (show annotations) (download)
Mon Jun 23 21:39:39 2008 UTC (15 years, 10 months ago) by niro
File size: 336878 byte(s)
2.6.23-alx-r1: new default as we fix the via epia clocksource=tsc quircks
-linux-2.6.23.17
-fbcondecor-0.9.4
-squashfs-3.3
-unionfs-2.3.3
-ipw3945-1.2.2
-mptbase-vmware fix

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