Magellan Linux

Contents of /trunk/kernel26-magellan-server/patches-2.6.25-r4/0153-2.6.25-unionfs-2.3.3.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 665 - (show annotations) (download)
Wed Jul 30 14:06:06 2008 UTC (15 years, 9 months ago) by niro
File size: 336771 byte(s)
-2.6.25-magellan-r4; updated to linux-2.6.25.10

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