Magellan Linux

Contents of /trunk/kernel26-magellan/patches-2.6.31-r3/0153-2.6.31-unionfs-2.5.3.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 946 - (show annotations) (download)
Thu Dec 10 13:02:09 2009 UTC (14 years, 4 months ago) by niro
File size: 341319 byte(s)
-2.6.31-magellan-r3: updated to linux-2.6.31.7

1 diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
2 index f15621e..55e2f07 100644
3 --- a/Documentation/filesystems/00-INDEX
4 +++ b/Documentation/filesystems/00-INDEX
5 @@ -112,6 +112,8 @@ udf.txt
6 - info and mount options for the UDF filesystem.
7 ufs.txt
8 - info on the ufs filesystem.
9 +unionfs/
10 + - info on the unionfs filesystem
11 vfat.txt
12 - info on using the VFAT filesystem used in Windows NT and Windows 95
13 vfs.txt
14 diff --git a/Documentation/filesystems/unionfs/00-INDEX b/Documentation/filesystems/unionfs/00-INDEX
15 new file mode 100644
16 index 0000000..96fdf67
17 --- /dev/null
18 +++ b/Documentation/filesystems/unionfs/00-INDEX
19 @@ -0,0 +1,10 @@
20 +00-INDEX
21 + - this file.
22 +concepts.txt
23 + - A brief introduction of concepts.
24 +issues.txt
25 + - A summary of known issues with unionfs.
26 +rename.txt
27 + - Information regarding rename operations.
28 +usage.txt
29 + - Usage information and examples.
30 diff --git a/Documentation/filesystems/unionfs/concepts.txt b/Documentation/filesystems/unionfs/concepts.txt
31 new file mode 100644
32 index 0000000..b853788
33 --- /dev/null
34 +++ b/Documentation/filesystems/unionfs/concepts.txt
35 @@ -0,0 +1,287 @@
36 +Unionfs 2.x CONCEPTS:
37 +=====================
38 +
39 +This file describes the concepts needed by a namespace unification file
40 +system.
41 +
42 +
43 +Branch Priority:
44 +================
45 +
46 +Each branch is assigned a unique priority - starting from 0 (highest
47 +priority). No two branches can have the same priority.
48 +
49 +
50 +Branch Mode:
51 +============
52 +
53 +Each branch is assigned a mode - read-write or read-only. This allows
54 +directories on media mounted read-write to be used in a read-only manner.
55 +
56 +
57 +Whiteouts:
58 +==========
59 +
60 +A whiteout removes a file name from the namespace. Whiteouts are needed when
61 +one attempts to remove a file on a read-only branch.
62 +
63 +Suppose we have a two-branch union, where branch 0 is read-write and branch
64 +1 is read-only. And a file 'foo' on branch 1:
65 +
66 +./b0/
67 +./b1/
68 +./b1/foo
69 +
70 +The unified view would simply be:
71 +
72 +./union/
73 +./union/foo
74 +
75 +Since 'foo' is stored on a read-only branch, it cannot be removed. A
76 +whiteout is used to remove the name 'foo' from the unified namespace. Again,
77 +since branch 1 is read-only, the whiteout cannot be created there. So, we
78 +try on a higher priority (lower numerically) branch and create the whiteout
79 +there.
80 +
81 +./b0/
82 +./b0/.wh.foo
83 +./b1/
84 +./b1/foo
85 +
86 +Later, when Unionfs traverses branches (due to lookup or readdir), it
87 +eliminate 'foo' from the namespace (as well as the whiteout itself.)
88 +
89 +
90 +Opaque Directories:
91 +===================
92 +
93 +Assume we have a unionfs mount comprising of two branches. Branch 0 is
94 +empty; branch 1 has the directory /a and file /a/f. Let's say we mount a
95 +union of branch 0 as read-write and branch 1 as read-only. Now, let's say
96 +we try to perform the following operation in the union:
97 +
98 + rm -fr a
99 +
100 +Because branch 1 is not writable, we cannot physically remove the file /a/f
101 +or the directory /a. So instead, we will create a whiteout in branch 0
102 +named /.wh.a, masking out the name "a" from branch 1. Next, let's say we
103 +try to create a directory named "a" as follows:
104 +
105 + mkdir a
106 +
107 +Because we have a whiteout for "a" already, Unionfs behaves as if "a"
108 +doesn't exist, and thus will delete the whiteout and replace it with an
109 +actual directory named "a".
110 +
111 +The problem now is that if you try to "ls" in the union, Unionfs will
112 +perform is normal directory name unification, for *all* directories named
113 +"a" in all branches. This will cause the file /a/f from branch 1 to
114 +re-appear in the union's namespace, which violates Unix semantics.
115 +
116 +To avoid this problem, we have a different form of whiteouts for
117 +directories, called "opaque directories" (same as BSD Union Mount does).
118 +Whenever we replace a whiteout with a directory, that directory is marked as
119 +opaque. In Unionfs 2.x, it means that we create a file named
120 +/a/.wh.__dir_opaque in branch 0, after having created directory /a there.
121 +When unionfs notices that a directory is opaque, it stops all namespace
122 +operations (including merging readdir contents) at that opaque directory.
123 +This prevents re-exposing names from masked out directories.
124 +
125 +
126 +Duplicate Elimination:
127 +======================
128 +
129 +It is possible for files on different branches to have the same name.
130 +Unionfs then has to select which instance of the file to show to the user.
131 +Given the fact that each branch has a priority associated with it, the
132 +simplest solution is to take the instance from the highest priority
133 +(numerically lowest value) and "hide" the others.
134 +
135 +
136 +Unlinking:
137 +=========
138 +
139 +Unlink operation on non-directory instances is optimized to remove the
140 +maximum possible objects in case multiple underlying branches have the same
141 +file name. The unlink operation will first try to delete file instances
142 +from highest priority branch and then move further to delete from remaining
143 +branches in order of their decreasing priority. Consider a case (F..D..F),
144 +where F is a file and D is a directory of the same name; here, some
145 +intermediate branch could have an empty directory instance with the same
146 +name, so this operation also tries to delete this directory instance and
147 +proceed further to delete from next possible lower priority branch. The
148 +unionfs unlink operation will smoothly delete the files with same name from
149 +all possible underlying branches. In case if some error occurs, it creates
150 +whiteout in highest priority branch that will hide file instance in rest of
151 +the branches. An error could occur either if an unlink operations in any of
152 +the underlying branch failed or if a branch has no write permission.
153 +
154 +This unlinking policy is known as "delete all" and it has the benefit of
155 +overall reducing the number of inodes used by duplicate files, and further
156 +reducing the total number of inodes consumed by whiteouts. The cost is of
157 +extra processing, but testing shows this extra processing is well worth the
158 +savings.
159 +
160 +
161 +Copyup:
162 +=======
163 +
164 +When a change is made to the contents of a file's data or meta-data, they
165 +have to be stored somewhere. The best way is to create a copy of the
166 +original file on a branch that is writable, and then redirect the write
167 +though to this copy. The copy must be made on a higher priority branch so
168 +that lookup and readdir return this newer "version" of the file rather than
169 +the original (see duplicate elimination).
170 +
171 +An entire unionfs mount can be read-only or read-write. If it's read-only,
172 +then none of the branches will be written to, even if some of the branches
173 +are physically writeable. If the unionfs mount is read-write, then the
174 +leftmost (highest priority) branch must be writeable (for copyup to take
175 +place); the remaining branches can be any mix of read-write and read-only.
176 +
177 +In a writeable mount, unionfs will create new files/dir in the leftmost
178 +branch. If one tries to modify a file in a read-only branch/media, unionfs
179 +will copyup the file to the leftmost branch and modify it there. If you try
180 +to modify a file from a writeable branch which is not the leftmost branch,
181 +then unionfs will modify it in that branch; this is useful if you, say,
182 +unify differnet packages (e.g., apache, sendmail, ftpd, etc.) and you want
183 +changes to specific package files to remain logically in the directory where
184 +they came from.
185 +
186 +Cache Coherency:
187 +================
188 +
189 +Unionfs users often want to be able to modify files and directories directly
190 +on the lower branches, and have those changes be visible at the Unionfs
191 +level. This means that data (e.g., pages) and meta-data (dentries, inodes,
192 +open files, etc.) have to be synchronized between the upper and lower
193 +layers. In other words, the newest changes from a layer below have to be
194 +propagated to the Unionfs layer above. If the two layers are not in sync, a
195 +cache incoherency ensues, which could lead to application failures and even
196 +oopses. The Linux kernel, however, has a rather limited set of mechanisms
197 +to ensure this inter-layer cache coherency---so Unionfs has to do most of
198 +the hard work on its own.
199 +
200 +Maintaining Invariants:
201 +
202 +The way Unionfs ensures cache coherency is as follows. At each entry point
203 +to a Unionfs file system method, we call a utility function to validate the
204 +primary objects of this method. Generally, we call unionfs_file_revalidate
205 +on open files, and __unionfs_d_revalidate_chain on dentries (which also
206 +validates inodes). These utility functions check to see whether the upper
207 +Unionfs object is in sync with any of the lower objects that it represents.
208 +The checks we perform include whether the Unionfs superblock has a newer
209 +generation number, or if any of the lower objects mtime's or ctime's are
210 +newer. (Note: generation numbers change when branch-management commands are
211 +issued, so in a way, maintaining cache coherency is also very important for
212 +branch-management.) If indeed we determine that any Unionfs object is no
213 +longer in sync with its lower counterparts, then we rebuild that object
214 +similarly to how we do so for branch-management.
215 +
216 +While rebuilding Unionfs's objects, we also purge any page mappings and
217 +truncate inode pages (see fs/unionfs/dentry.c:purge_inode_data). This is to
218 +ensure that Unionfs will re-get the newer data from the lower branches. We
219 +perform this purging only if the Unionfs operation in question is a reading
220 +operation; if Unionfs is performing a data writing operation (e.g., ->write,
221 +->commit_write, etc.) then we do NOT flush the lower mappings/pages: this is
222 +because (1) a self-deadlock could occur and (2) the upper Unionfs pages are
223 +considered more authoritative anyway, as they are newer and will overwrite
224 +any lower pages.
225 +
226 +Unionfs maintains the following important invariant regarding mtime's,
227 +ctime's, and atime's: the upper inode object's times are the max() of all of
228 +the lower ones. For non-directory objects, there's only one object below,
229 +so the mapping is simple; for directory objects, there could me multiple
230 +lower objects and we have to sync up with the newest one of all the lower
231 +ones. This invariant is important to maintain, especially for directories
232 +(besides, we need this to be POSIX compliant). A union could comprise
233 +multiple writable branches, each of which could change. If we don't reflect
234 +the newest possible mtime/ctime, some applications could fail. For example,
235 +NFSv2/v3 exports check for newer directory mtimes on the server to determine
236 +if the client-side attribute cache should be purged.
237 +
238 +To maintain these important invariants, of course, Unionfs carefully
239 +synchronizes upper and lower times in various places. For example, if we
240 +copy-up a file to a top-level branch, the parent directory where the file
241 +was copied up to will now have a new mtime: so after a successful copy-up,
242 +we sync up with the new top-level branch's parent directory mtime.
243 +
244 +Implementation:
245 +
246 +This cache-coherency implementation is efficient because it defers any
247 +synchronizing between the upper and lower layers until absolutely needed.
248 +Consider the example a common situation where users perform a lot of lower
249 +changes, such as untarring a whole package. While these take place,
250 +typically the user doesn't access the files via Unionfs; only after the
251 +lower changes are done, does the user try to access the lower files. With
252 +our cache-coherency implementation, the entirety of the changes to the lower
253 +branches will not result in a single CPU cycle spent at the Unionfs level
254 +until the user invokes a system call that goes through Unionfs.
255 +
256 +We have considered two alternate cache-coherency designs. (1) Using the
257 +dentry/inode notify functionality to register interest in finding out about
258 +any lower changes. This is a somewhat limited and also a heavy-handed
259 +approach which could result in many notifications to the Unionfs layer upon
260 +each small change at the lower layer (imagine a file being modified multiple
261 +times in rapid succession). (2) Rewriting the VFS to support explicit
262 +callbacks from lower objects to upper objects. We began exploring such an
263 +implementation, but found it to be very complicated--it would have resulted
264 +in massive VFS/MM changes which are unlikely to be accepted by the LKML
265 +community. We therefore believe that our current cache-coherency design and
266 +implementation represent the best approach at this time.
267 +
268 +Limitations:
269 +
270 +Our implementation works in that as long as a user process will have caused
271 +Unionfs to be called, directly or indirectly, even to just do
272 +->d_revalidate; then we will have purged the current Unionfs data and the
273 +process will see the new data. For example, a process that continually
274 +re-reads the same file's data will see the NEW data as soon as the lower
275 +file had changed, upon the next read(2) syscall (even if the file is still
276 +open!) However, this doesn't work when the process re-reads the open file's
277 +data via mmap(2) (unless the user unmaps/closes the file and remaps/reopens
278 +it). Once we respond to ->readpage(s), then the kernel maps the page into
279 +the process's address space and there doesn't appear to be a way to force
280 +the kernel to invalidate those pages/mappings, and force the process to
281 +re-issue ->readpage. If there's a way to invalidate active mappings and
282 +force a ->readpage, let us know please (invalidate_inode_pages2 doesn't do
283 +the trick).
284 +
285 +Our current Unionfs code has to perform many file-revalidation calls. It
286 +would be really nice if the VFS would export an optional file system hook
287 +->file_revalidate (similarly to dentry->d_revalidate) that will be called
288 +before each VFS op that has a "struct file" in it.
289 +
290 +Certain file systems have micro-second granularity (or better) for inode
291 +times, and asynchronous actions could cause those times to change with some
292 +small delay. In such cases, Unionfs may see a changed inode time that only
293 +differs by a tiny fraction of a second: such a change may be a false
294 +positive indication that the lower object has changed, whereas if unionfs
295 +waits a little longer, that false indication will not be seen. (These false
296 +positives are harmless, because they would at most cause unionfs to
297 +re-validate an object that may need no revalidation, and print a debugging
298 +message that clutters the console/logs.) Therefore, to minimize the chances
299 +of these situations, we delay the detection of changed times by a small
300 +factor of a few seconds, called UNIONFS_MIN_CC_TIME (which defaults to 3
301 +seconds, as does NFS). This means that we will detect the change, only a
302 +couple of seconds later, if indeed the time change persists in the lower
303 +file object. This delayed detection has an added performance benefit: we
304 +reduce the number of times that unionfs has to revalidate objects, in case
305 +there's a lot of concurrent activity on both the upper and lower objects,
306 +for the same file(s). Lastly, this delayed time attribute detection is
307 +similar to how NFS clients operate (e.g., acregmin).
308 +
309 +Finally, there is no way currently in Linux to prevent lower directories
310 +from being moved around (i.e., topology changes); there's no way to prevent
311 +modifications to directory sub-trees of whole file systems which are mounted
312 +read-write. It is therefore possible for in-flight operations in unionfs to
313 +take place, while a lower directory is being moved around. Therefore, if
314 +you try to, say, create a new file in a directory through unionfs, while the
315 +directory is being moved around directly, then the new file may get created
316 +in the new location where that directory was moved to. This is a somewhat
317 +similar behaviour in NFS: an NFS client could be creating a new file while
318 +th NFS server is moving th directory around; the file will get successfully
319 +created in the new location. (The one exception in unionfs is that if the
320 +branch is marked read-only by unionfs, then a copyup will take place.)
321 +
322 +For more information, see <http://unionfs.filesystems.org/>.
323 diff --git a/Documentation/filesystems/unionfs/issues.txt b/Documentation/filesystems/unionfs/issues.txt
324 new file mode 100644
325 index 0000000..f4b7e7e
326 --- /dev/null
327 +++ b/Documentation/filesystems/unionfs/issues.txt
328 @@ -0,0 +1,28 @@
329 +KNOWN Unionfs 2.x ISSUES:
330 +=========================
331 +
332 +1. Unionfs should not use lookup_one_len() on the underlying f/s as it
333 + confuses NFSv4. Currently, unionfs_lookup() passes lookup intents to the
334 + lower file-system, this eliminates part of the problem. The remaining
335 + calls to lookup_one_len may need to be changed to pass an intent. We are
336 + currently introducing VFS changes to fs/namei.c's do_path_lookup() to
337 + allow proper file lookup and opening in stackable file systems.
338 +
339 +2. Lockdep (a debugging feature) isn't aware of stacking, and so it
340 + incorrectly complains about locking problems. The problem boils down to
341 + this: Lockdep considers all objects of a certain type to be in the same
342 + class, for example, all inodes. Lockdep doesn't like to see a lock held
343 + on two inodes within the same task, and warns that it could lead to a
344 + deadlock. However, stackable file systems do precisely that: they lock
345 + an upper object, and then a lower object, in a strict order to avoid
346 + locking problems; in addition, Unionfs, as a fan-out file system, may
347 + have to lock several lower inodes. We are currently looking into Lockdep
348 + to see how to make it aware of stackable file systems. For now, we
349 + temporarily disable lockdep when calling vfs methods on lower objects,
350 + but only for those places where lockdep complained. While this solution
351 + may seem unclean, it is not without precedent: other places in the kernel
352 + also do similar temporary disabling, of course after carefully having
353 + checked that it is the right thing to do. Anyway, you get any warnings
354 + from Lockdep, please report them to the Unionfs maintainers.
355 +
356 +For more information, see <http://unionfs.filesystems.org/>.
357 diff --git a/Documentation/filesystems/unionfs/rename.txt b/Documentation/filesystems/unionfs/rename.txt
358 new file mode 100644
359 index 0000000..e20bb82
360 --- /dev/null
361 +++ b/Documentation/filesystems/unionfs/rename.txt
362 @@ -0,0 +1,31 @@
363 +Rename is a complex beast. The following table shows which rename(2) operations
364 +should succeed and which should fail.
365 +
366 +o: success
367 +E: error (either unionfs or vfs)
368 +X: EXDEV
369 +
370 +none = file does not exist
371 +file = file is a file
372 +dir = file is a empty directory
373 +child= file is a non-empty directory
374 +wh = file is a directory containing only whiteouts; this makes it logically
375 + empty
376 +
377 + none file dir child wh
378 +file o o E E E
379 +dir o E o E o
380 +child X E X E X
381 +wh o E o E o
382 +
383 +
384 +Renaming directories:
385 +=====================
386 +
387 +Whenever a empty (either physically or logically) directory is being renamed,
388 +the following sequence of events should take place:
389 +
390 +1) Remove whiteouts from both source and destination directory
391 +2) Rename source to destination
392 +3) Make destination opaque to prevent anything under it from showing up
393 +
394 diff --git a/Documentation/filesystems/unionfs/usage.txt b/Documentation/filesystems/unionfs/usage.txt
395 new file mode 100644
396 index 0000000..1adde69
397 --- /dev/null
398 +++ b/Documentation/filesystems/unionfs/usage.txt
399 @@ -0,0 +1,134 @@
400 +Unionfs is a stackable unification file system, which can appear to merge
401 +the contents of several directories (branches), while keeping their physical
402 +content separate. Unionfs is useful for unified source tree management,
403 +merged contents of split CD-ROM, merged separate software package
404 +directories, data grids, and more. Unionfs allows any mix of read-only and
405 +read-write branches, as well as insertion and deletion of branches anywhere
406 +in the fan-out. To maintain Unix semantics, Unionfs handles elimination of
407 +duplicates, partial-error conditions, and more.
408 +
409 +GENERAL SYNTAX
410 +==============
411 +
412 +# mount -t unionfs -o <OPTIONS>,<BRANCH-OPTIONS> none MOUNTPOINT
413 +
414 +OPTIONS can be any legal combination of:
415 +
416 +- ro # mount file system read-only
417 +- rw # mount file system read-write
418 +- remount # remount the file system (see Branch Management below)
419 +- incgen # increment generation no. (see Cache Consistency below)
420 +
421 +BRANCH-OPTIONS can be either (1) a list of branches given to the "dirs="
422 +option, or (2) a list of individual branch manipulation commands, combined
423 +with the "remount" option, and is further described in the "Branch
424 +Management" section below.
425 +
426 +The syntax for the "dirs=" mount option is:
427 +
428 + dirs=branch[=ro|=rw][:...]
429 +
430 +The "dirs=" option takes a colon-delimited list of directories to compose
431 +the union, with an optional branch mode for each of those directories.
432 +Directories that come earlier (specified first, on the left) in the list
433 +have a higher precedence than those which come later. Additionally,
434 +read-only or read-write permissions of the branch can be specified by
435 +appending =ro or =rw (default) to each directory. See the Copyup section in
436 +concepts.txt, for a description of Unionfs's behavior when mixing read-only
437 +and read-write branches and mounts.
438 +
439 +Syntax:
440 +
441 + dirs=/branch1[=ro|=rw]:/branch2[=ro|=rw]:...:/branchN[=ro|=rw]
442 +
443 +Example:
444 +
445 + dirs=/writable_branch=rw:/read-only_branch=ro
446 +
447 +
448 +BRANCH MANAGEMENT
449 +=================
450 +
451 +Once you mount your union for the first time, using the "dirs=" option, you
452 +can then change the union's overall mode or reconfigure the branches, using
453 +the remount option, as follows.
454 +
455 +To downgrade a union from read-write to read-only:
456 +
457 +# mount -t unionfs -o remount,ro none MOUNTPOINT
458 +
459 +To upgrade a union from read-only to read-write:
460 +
461 +# mount -t unionfs -o remount,rw none MOUNTPOINT
462 +
463 +To delete a branch /foo, regardless where it is in the current union:
464 +
465 +# mount -t unionfs -o remount,del=/foo none MOUNTPOINT
466 +
467 +To insert (add) a branch /foo before /bar:
468 +
469 +# mount -t unionfs -o remount,add=/bar:/foo none MOUNTPOINT
470 +
471 +To insert (add) a branch /foo (with the "rw" mode flag) before /bar:
472 +
473 +# mount -t unionfs -o remount,add=/bar:/foo=rw none MOUNTPOINT
474 +
475 +To insert (add) a branch /foo (in "rw" mode) at the very beginning (i.e., a
476 +new highest-priority branch), you can use the above syntax, or use a short
477 +hand version as follows:
478 +
479 +# mount -t unionfs -o remount,add=/foo none MOUNTPOINT
480 +
481 +To append a branch to the very end (new lowest-priority branch):
482 +
483 +# mount -t unionfs -o remount,add=:/foo none MOUNTPOINT
484 +
485 +To append a branch to the very end (new lowest-priority branch), in
486 +read-only mode:
487 +
488 +# mount -t unionfs -o remount,add=:/foo=ro none MOUNTPOINT
489 +
490 +Finally, to change the mode of one existing branch, say /foo, from read-only
491 +to read-write, and change /bar from read-write to read-only:
492 +
493 +# mount -t unionfs -o remount,mode=/foo=rw,mode=/bar=ro none MOUNTPOINT
494 +
495 +Note: in Unionfs 2.x, you cannot set the leftmost branch to readonly because
496 +then Unionfs won't have any writable place for copyups to take place.
497 +Moreover, the VFS can get confused when it tries to modify something in a
498 +file system mounted read-write, but isn't permitted to write to it.
499 +Instead, you should set the whole union as readonly, as described above.
500 +If, however, you must set the leftmost branch as readonly, perhaps so you
501 +can get a snapshot of it at a point in time, then you should insert a new
502 +writable top-level branch, and mark the one you want as readonly. This can
503 +be accomplished as follows, assuming that /foo is your current leftmost
504 +branch:
505 +
506 +# mount -t tmpfs -o size=NNN /new
507 +# mount -t unionfs -o remount,add=/new,mode=/foo=ro none MOUNTPOINT
508 +<do what you want safely in /foo>
509 +# mount -t unionfs -o remount,del=/new,mode=/foo=rw none MOUNTPOINT
510 +<check if there's anything in /new you want to preserve>
511 +# umount /new
512 +
513 +CACHE CONSISTENCY
514 +=================
515 +
516 +If you modify any file on any of the lower branches directly, while there is
517 +a Unionfs 2.x mounted above any of those branches, you should tell Unionfs
518 +to purge its caches and re-get the objects. To do that, you have to
519 +increment the generation number of the superblock using the following
520 +command:
521 +
522 +# mount -t unionfs -o remount,incgen none MOUNTPOINT
523 +
524 +Note that the older way of incrementing the generation number using an
525 +ioctl, is no longer supported in Unionfs 2.0 and newer. Ioctls in general
526 +are not encouraged. Plus, an ioctl is per-file concept, whereas the
527 +generation number is a per-file-system concept. Worse, such an ioctl
528 +requires an open file, which then has to be invalidated by the very nature
529 +of the generation number increase (read: the old generation increase ioctl
530 +was pretty racy).
531 +
532 +
533 +For more information, see <http://unionfs.filesystems.org/>.
534 diff --git a/MAINTAINERS b/MAINTAINERS
535 index 8dca9d8..4421543 100644
536 --- a/MAINTAINERS
537 +++ b/MAINTAINERS
538 @@ -5095,6 +5095,14 @@ F: Documentation/cdrom/
539 F: drivers/cdrom/cdrom.c
540 F: include/linux/cdrom.h
541
542 +UNIONFS
543 +P: Erez Zadok
544 +M: ezk@cs.sunysb.edu
545 +L: unionfs@filesystems.org
546 +W: http://unionfs.filesystems.org/
547 +T: git git.kernel.org/pub/scm/linux/kernel/git/ezk/unionfs.git
548 +S: Maintained
549 +
550 UNSORTED BLOCK IMAGES (UBI)
551 M: Artem Bityutskiy <dedekind@infradead.org>
552 W: http://www.linux-mtd.infradead.org/
553 diff --git a/fs/Kconfig b/fs/Kconfig
554 index 0e7da7b..b69b6bd 100644
555 --- a/fs/Kconfig
556 +++ b/fs/Kconfig
557 @@ -167,6 +167,7 @@ if MISC_FILESYSTEMS
558 source "fs/adfs/Kconfig"
559 source "fs/affs/Kconfig"
560 source "fs/ecryptfs/Kconfig"
561 +source "fs/unionfs/Kconfig"
562 source "fs/hfs/Kconfig"
563 source "fs/hfsplus/Kconfig"
564 source "fs/befs/Kconfig"
565 diff --git a/fs/Makefile b/fs/Makefile
566 index af6d047..6c254d5 100644
567 --- a/fs/Makefile
568 +++ b/fs/Makefile
569 @@ -84,6 +84,7 @@ obj-$(CONFIG_ISO9660_FS) += isofs/
570 obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
571 obj-$(CONFIG_HFS_FS) += hfs/
572 obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
573 +obj-$(CONFIG_UNION_FS) += unionfs/
574 obj-$(CONFIG_VXFS_FS) += freevxfs/
575 obj-$(CONFIG_NFS_FS) += nfs/
576 obj-$(CONFIG_EXPORTFS) += exportfs/
577 diff --git a/fs/ecryptfs/dentry.c b/fs/ecryptfs/dentry.c
578 index 2dda5ad..8f006a0 100644
579 --- a/fs/ecryptfs/dentry.c
580 +++ b/fs/ecryptfs/dentry.c
581 @@ -62,7 +62,7 @@ static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
582 struct inode *lower_inode =
583 ecryptfs_inode_to_lower(dentry->d_inode);
584
585 - fsstack_copy_attr_all(dentry->d_inode, lower_inode, NULL);
586 + fsstack_copy_attr_all(dentry->d_inode, lower_inode);
587 }
588 out:
589 return rc;
590 diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
591 index 2f0945d..e884c3b 100644
592 --- a/fs/ecryptfs/inode.c
593 +++ b/fs/ecryptfs/inode.c
594 @@ -624,9 +624,9 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
595 lower_new_dir_dentry->d_inode, lower_new_dentry);
596 if (rc)
597 goto out_lock;
598 - fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
599 + fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
600 if (new_dir != old_dir)
601 - fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
602 + fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
603 out_lock:
604 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
605 dput(lower_new_dentry->d_parent);
606 @@ -965,7 +965,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
607 rc = notify_change(lower_dentry, ia);
608 mutex_unlock(&lower_dentry->d_inode->i_mutex);
609 out:
610 - fsstack_copy_attr_all(inode, lower_inode, NULL);
611 + fsstack_copy_attr_all(inode, lower_inode);
612 return rc;
613 }
614
615 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
616 index 9f0aa98..3d94155 100644
617 --- a/fs/ecryptfs/main.c
618 +++ b/fs/ecryptfs/main.c
619 @@ -190,7 +190,7 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
620 init_special_inode(inode, lower_inode->i_mode,
621 lower_inode->i_rdev);
622 dentry->d_op = &ecryptfs_dops;
623 - fsstack_copy_attr_all(inode, lower_inode, NULL);
624 + fsstack_copy_attr_all(inode, lower_inode);
625 /* This size will be overwritten for real files w/ headers and
626 * other metadata */
627 fsstack_copy_inode_size(inode, lower_inode);
628 diff --git a/fs/namei.c b/fs/namei.c
629 index 1f13751..88584c4 100644
630 --- a/fs/namei.c
631 +++ b/fs/namei.c
632 @@ -375,6 +375,7 @@ void release_open_intent(struct nameidata *nd)
633 else
634 fput(nd->intent.open.file);
635 }
636 +EXPORT_SYMBOL_GPL(release_open_intent);
637
638 static inline struct dentry *
639 do_revalidate(struct dentry *dentry, struct nameidata *nd)
640 diff --git a/fs/splice.c b/fs/splice.c
641 index 73766d2..3753029 100644
642 --- a/fs/splice.c
643 +++ b/fs/splice.c
644 @@ -1057,8 +1057,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
645 /*
646 * Attempt to initiate a splice from pipe to file.
647 */
648 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
649 - loff_t *ppos, size_t len, unsigned int flags)
650 +long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
651 + loff_t *ppos, size_t len, unsigned int flags)
652 {
653 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
654 loff_t *, size_t, unsigned int);
655 @@ -1080,13 +1080,14 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
656
657 return splice_write(pipe, out, ppos, len, flags);
658 }
659 +EXPORT_SYMBOL_GPL(vfs_splice_from);
660
661 /*
662 * Attempt to initiate a splice from a file to a pipe.
663 */
664 -static long do_splice_to(struct file *in, loff_t *ppos,
665 - struct pipe_inode_info *pipe, size_t len,
666 - unsigned int flags)
667 +long vfs_splice_to(struct file *in, loff_t *ppos,
668 + struct pipe_inode_info *pipe, size_t len,
669 + unsigned int flags)
670 {
671 ssize_t (*splice_read)(struct file *, loff_t *,
672 struct pipe_inode_info *, size_t, unsigned int);
673 @@ -1105,6 +1106,7 @@ static long do_splice_to(struct file *in, loff_t *ppos,
674
675 return splice_read(in, ppos, pipe, len, flags);
676 }
677 +EXPORT_SYMBOL_GPL(vfs_splice_to);
678
679 /**
680 * splice_direct_to_actor - splices data directly between two non-pipes
681 @@ -1174,7 +1176,7 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
682 size_t read_len;
683 loff_t pos = sd->pos, prev_pos = pos;
684
685 - ret = do_splice_to(in, &pos, pipe, len, flags);
686 + ret = vfs_splice_to(in, &pos, pipe, len, flags);
687 if (unlikely(ret <= 0))
688 goto out_release;
689
690 @@ -1233,7 +1235,7 @@ static int direct_splice_actor(struct pipe_inode_info *pipe,
691 {
692 struct file *file = sd->u.file;
693
694 - return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
695 + return vfs_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
696 }
697
698 /**
699 @@ -1330,7 +1332,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
700 } else
701 off = &out->f_pos;
702
703 - ret = do_splice_from(ipipe, out, off, len, flags);
704 + ret = vfs_splice_from(ipipe, out, off, len, flags);
705
706 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
707 ret = -EFAULT;
708 @@ -1350,7 +1352,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
709 } else
710 off = &in->f_pos;
711
712 - ret = do_splice_to(in, off, opipe, len, flags);
713 + ret = vfs_splice_to(in, off, opipe, len, flags);
714
715 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
716 ret = -EFAULT;
717 diff --git a/fs/stack.c b/fs/stack.c
718 index 67716f6..cc1443d 100644
719 --- a/fs/stack.c
720 +++ b/fs/stack.c
721 @@ -1,24 +1,82 @@
722 +/*
723 + * Copyright (c) 2006-2009 Erez Zadok
724 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
725 + * Copyright (c) 2006-2009 Stony Brook University
726 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
727 + *
728 + * This program is free software; you can redistribute it and/or modify
729 + * it under the terms of the GNU General Public License version 2 as
730 + * published by the Free Software Foundation.
731 + */
732 +
733 #include <linux/module.h>
734 #include <linux/fs.h>
735 #include <linux/fs_stack.h>
736
737 -/* does _NOT_ require i_mutex to be held.
738 +/*
739 + * does _NOT_ require i_mutex to be held.
740 *
741 * This function cannot be inlined since i_size_{read,write} is rather
742 * heavy-weight on 32-bit systems
743 */
744 -void fsstack_copy_inode_size(struct inode *dst, const struct inode *src)
745 +void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
746 {
747 - i_size_write(dst, i_size_read((struct inode *)src));
748 - dst->i_blocks = src->i_blocks;
749 + loff_t i_size;
750 + blkcnt_t i_blocks;
751 +
752 + /*
753 + * i_size_read() includes its own seqlocking and protection from
754 + * preemption (see include/linux/fs.h): we need nothing extra for
755 + * that here, and prefer to avoid nesting locks than attempt to
756 + * keep i_size and i_blocks in synch together.
757 + */
758 + i_size = i_size_read(src);
759 +
760 + /*
761 + * But if CONFIG_LSF (on 32-bit), we ought to make an effort to keep
762 + * the two halves of i_blocks in synch despite SMP or PREEMPT - though
763 + * stat's generic_fillattr() doesn't bother, and we won't be applying
764 + * quotas (where i_blocks does become important) at the upper level.
765 + *
766 + * We don't actually know what locking is used at the lower level; but
767 + * if it's a filesystem that supports quotas, it will be using i_lock
768 + * as in inode_add_bytes(). tmpfs uses other locking, and its 32-bit
769 + * is (just) able to exceed 2TB i_size with the aid of holes; but its
770 + * i_blocks cannot carry into the upper long without almost 2TB swap -
771 + * let's ignore that case.
772 + */
773 + if (sizeof(i_blocks) > sizeof(long))
774 + spin_lock(&src->i_lock);
775 + i_blocks = src->i_blocks;
776 + if (sizeof(i_blocks) > sizeof(long))
777 + spin_unlock(&src->i_lock);
778 +
779 + /*
780 + * If CONFIG_SMP on 32-bit, it's vital for fsstack_copy_inode_size()
781 + * to hold some lock around i_size_write(), otherwise i_size_read()
782 + * may spin forever (see include/linux/fs.h). We don't necessarily
783 + * hold i_mutex when this is called, so take i_lock for that case.
784 + *
785 + * And if CONFIG_LSF (on 32-bit), continue our effort to keep the
786 + * two halves of i_blocks in synch despite SMP or PREEMPT: use i_lock
787 + * for that case too, and do both at once by combining the tests.
788 + *
789 + * There is none of this locking overhead in the 64-bit case.
790 + */
791 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
792 + spin_lock(&dst->i_lock);
793 + i_size_write(dst, i_size);
794 + dst->i_blocks = i_blocks;
795 + if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
796 + spin_unlock(&dst->i_lock);
797 }
798 EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
799
800 -/* copy all attributes; get_nlinks is optional way to override the i_nlink
801 +/*
802 + * copy all attributes; get_nlinks is optional way to override the i_nlink
803 * copying
804 */
805 -void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
806 - int (*get_nlinks)(struct inode *))
807 +void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
808 {
809 dest->i_mode = src->i_mode;
810 dest->i_uid = src->i_uid;
811 @@ -29,14 +87,6 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
812 dest->i_ctime = src->i_ctime;
813 dest->i_blkbits = src->i_blkbits;
814 dest->i_flags = src->i_flags;
815 -
816 - /*
817 - * Update the nlinks AFTER updating the above fields, because the
818 - * get_links callback may depend on them.
819 - */
820 - if (!get_nlinks)
821 - dest->i_nlink = src->i_nlink;
822 - else
823 - dest->i_nlink = (*get_nlinks)(dest);
824 + dest->i_nlink = src->i_nlink;
825 }
826 EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
827 diff --git a/fs/unionfs/Kconfig b/fs/unionfs/Kconfig
828 new file mode 100644
829 index 0000000..f3c1ac4
830 --- /dev/null
831 +++ b/fs/unionfs/Kconfig
832 @@ -0,0 +1,24 @@
833 +config UNION_FS
834 + tristate "Union file system (EXPERIMENTAL)"
835 + depends on EXPERIMENTAL
836 + help
837 + Unionfs is a stackable unification file system, which appears to
838 + merge the contents of several directories (branches), while keeping
839 + their physical content separate.
840 +
841 + See <http://unionfs.filesystems.org> for details
842 +
843 +config UNION_FS_XATTR
844 + bool "Unionfs extended attributes"
845 + depends on UNION_FS
846 + help
847 + Extended attributes are name:value pairs associated with inodes by
848 + the kernel or by users (see the attr(5) manual page).
849 +
850 + If unsure, say N.
851 +
852 +config UNION_FS_DEBUG
853 + bool "Debug Unionfs"
854 + depends on UNION_FS
855 + help
856 + If you say Y here, you can turn on debugging output from Unionfs.
857 diff --git a/fs/unionfs/Makefile b/fs/unionfs/Makefile
858 new file mode 100644
859 index 0000000..9c466a5
860 --- /dev/null
861 +++ b/fs/unionfs/Makefile
862 @@ -0,0 +1,17 @@
863 +UNIONFS_VERSION="2.5.3 (for 2.6.31)"
864 +
865 +EXTRA_CFLAGS += -DUNIONFS_VERSION=\"$(UNIONFS_VERSION)\"
866 +
867 +obj-$(CONFIG_UNION_FS) += unionfs.o
868 +
869 +unionfs-y := subr.o dentry.o file.o inode.o main.o super.o \
870 + rdstate.o copyup.o dirhelper.o rename.o unlink.o \
871 + lookup.o commonfops.o dirfops.o sioq.o mmap.o whiteout.o
872 +
873 +unionfs-$(CONFIG_UNION_FS_XATTR) += xattr.o
874 +
875 +unionfs-$(CONFIG_UNION_FS_DEBUG) += debug.o
876 +
877 +ifeq ($(CONFIG_UNION_FS_DEBUG),y)
878 +EXTRA_CFLAGS += -DDEBUG
879 +endif
880 diff --git a/fs/unionfs/commonfops.c b/fs/unionfs/commonfops.c
881 new file mode 100644
882 index 0000000..587f984
883 --- /dev/null
884 +++ b/fs/unionfs/commonfops.c
885 @@ -0,0 +1,896 @@
886 +/*
887 + * Copyright (c) 2003-2009 Erez Zadok
888 + * Copyright (c) 2003-2006 Charles P. Wright
889 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
890 + * Copyright (c) 2005-2006 Junjiro Okajima
891 + * Copyright (c) 2005 Arun M. Krishnakumar
892 + * Copyright (c) 2004-2006 David P. Quigley
893 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
894 + * Copyright (c) 2003 Puja Gupta
895 + * Copyright (c) 2003 Harikesavan Krishnan
896 + * Copyright (c) 2003-2009 Stony Brook University
897 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
898 + *
899 + * This program is free software; you can redistribute it and/or modify
900 + * it under the terms of the GNU General Public License version 2 as
901 + * published by the Free Software Foundation.
902 + */
903 +
904 +#include "union.h"
905 +
906 +/*
907 + * 1) Copyup the file
908 + * 2) Rename the file to '.unionfs<original inode#><counter>' - obviously
909 + * stolen from NFS's silly rename
910 + */
911 +static int copyup_deleted_file(struct file *file, struct dentry *dentry,
912 + struct dentry *parent, int bstart, int bindex)
913 +{
914 + static unsigned int counter;
915 + const int i_inosize = sizeof(dentry->d_inode->i_ino) * 2;
916 + const int countersize = sizeof(counter) * 2;
917 + const int nlen = sizeof(".unionfs") + i_inosize + countersize - 1;
918 + char name[nlen + 1];
919 + int err;
920 + struct dentry *tmp_dentry = NULL;
921 + struct dentry *lower_dentry;
922 + struct dentry *lower_dir_dentry = NULL;
923 +
924 + lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
925 +
926 + sprintf(name, ".unionfs%*.*lx",
927 + i_inosize, i_inosize, lower_dentry->d_inode->i_ino);
928 +
929 + /*
930 + * Loop, looking for an unused temp name to copyup to.
931 + *
932 + * It's somewhat silly that we look for a free temp tmp name in the
933 + * source branch (bstart) instead of the dest branch (bindex), where
934 + * the final name will be created. We _will_ catch it if somehow
935 + * the name exists in the dest branch, but it'd be nice to catch it
936 + * sooner than later.
937 + */
938 +retry:
939 + tmp_dentry = NULL;
940 + do {
941 + char *suffix = name + nlen - countersize;
942 +
943 + dput(tmp_dentry);
944 + counter++;
945 + sprintf(suffix, "%*.*x", countersize, countersize, counter);
946 +
947 + pr_debug("unionfs: trying to rename %s to %s\n",
948 + dentry->d_name.name, name);
949 +
950 + tmp_dentry = lookup_lck_len(name, lower_dentry->d_parent,
951 + nlen);
952 + if (IS_ERR(tmp_dentry)) {
953 + err = PTR_ERR(tmp_dentry);
954 + goto out;
955 + }
956 + } while (tmp_dentry->d_inode != NULL); /* need negative dentry */
957 + dput(tmp_dentry);
958 +
959 + err = copyup_named_file(parent->d_inode, file, name, bstart, bindex,
960 + i_size_read(file->f_path.dentry->d_inode));
961 + if (err) {
962 + if (unlikely(err == -EEXIST))
963 + goto retry;
964 + goto out;
965 + }
966 +
967 + /* bring it to the same state as an unlinked file */
968 + lower_dentry = unionfs_lower_dentry_idx(dentry, dbstart(dentry));
969 + if (!unionfs_lower_inode_idx(dentry->d_inode, bindex)) {
970 + atomic_inc(&lower_dentry->d_inode->i_count);
971 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
972 + lower_dentry->d_inode);
973 + }
974 + lower_dir_dentry = lock_parent(lower_dentry);
975 + err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
976 + unlock_dir(lower_dir_dentry);
977 +
978 +out:
979 + if (!err)
980 + unionfs_check_dentry(dentry);
981 + return err;
982 +}
983 +
984 +/*
985 + * put all references held by upper struct file and free lower file pointer
986 + * array
987 + */
988 +static void cleanup_file(struct file *file)
989 +{
990 + int bindex, bstart, bend;
991 + struct file **lower_files;
992 + struct file *lower_file;
993 + struct super_block *sb = file->f_path.dentry->d_sb;
994 +
995 + lower_files = UNIONFS_F(file)->lower_files;
996 + bstart = fbstart(file);
997 + bend = fbend(file);
998 +
999 + for (bindex = bstart; bindex <= bend; bindex++) {
1000 + int i; /* holds (possibly) updated branch index */
1001 + int old_bid;
1002 +
1003 + lower_file = unionfs_lower_file_idx(file, bindex);
1004 + if (!lower_file)
1005 + continue;
1006 +
1007 + /*
1008 + * Find new index of matching branch with an open
1009 + * file, since branches could have been added or
1010 + * deleted causing the one with open files to shift.
1011 + */
1012 + old_bid = UNIONFS_F(file)->saved_branch_ids[bindex];
1013 + i = branch_id_to_idx(sb, old_bid);
1014 + if (unlikely(i < 0)) {
1015 + printk(KERN_ERR "unionfs: no superblock for "
1016 + "file %p\n", file);
1017 + continue;
1018 + }
1019 +
1020 + /* decrement count of open files */
1021 + branchput(sb, i);
1022 + /*
1023 + * fput will perform an mntput for us on the correct branch.
1024 + * Although we're using the file's old branch configuration,
1025 + * bindex, which is the old index, correctly points to the
1026 + * right branch in the file's branch list. In other words,
1027 + * we're going to mntput the correct branch even if branches
1028 + * have been added/removed.
1029 + */
1030 + fput(lower_file);
1031 + UNIONFS_F(file)->lower_files[bindex] = NULL;
1032 + UNIONFS_F(file)->saved_branch_ids[bindex] = -1;
1033 + }
1034 +
1035 + UNIONFS_F(file)->lower_files = NULL;
1036 + kfree(lower_files);
1037 + kfree(UNIONFS_F(file)->saved_branch_ids);
1038 + /* set to NULL because caller needs to know if to kfree on error */
1039 + UNIONFS_F(file)->saved_branch_ids = NULL;
1040 +}
1041 +
1042 +/* open all lower files for a given file */
1043 +static int open_all_files(struct file *file)
1044 +{
1045 + int bindex, bstart, bend, err = 0;
1046 + struct file *lower_file;
1047 + struct dentry *lower_dentry;
1048 + struct dentry *dentry = file->f_path.dentry;
1049 + struct super_block *sb = dentry->d_sb;
1050 +
1051 + bstart = dbstart(dentry);
1052 + bend = dbend(dentry);
1053 +
1054 + for (bindex = bstart; bindex <= bend; bindex++) {
1055 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1056 + if (!lower_dentry)
1057 + continue;
1058 +
1059 + dget(lower_dentry);
1060 + unionfs_mntget(dentry, bindex);
1061 + branchget(sb, bindex);
1062 +
1063 + lower_file =
1064 + dentry_open(lower_dentry,
1065 + unionfs_lower_mnt_idx(dentry, bindex),
1066 + file->f_flags, current_cred());
1067 + if (IS_ERR(lower_file)) {
1068 + branchput(sb, bindex);
1069 + err = PTR_ERR(lower_file);
1070 + goto out;
1071 + } else {
1072 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1073 + }
1074 + }
1075 +out:
1076 + return err;
1077 +}
1078 +
1079 +/* open the highest priority file for a given upper file */
1080 +static int open_highest_file(struct file *file, bool willwrite)
1081 +{
1082 + int bindex, bstart, bend, err = 0;
1083 + struct file *lower_file;
1084 + struct dentry *lower_dentry;
1085 + struct dentry *dentry = file->f_path.dentry;
1086 + struct dentry *parent = dget_parent(dentry);
1087 + struct inode *parent_inode = parent->d_inode;
1088 + struct super_block *sb = dentry->d_sb;
1089 +
1090 + bstart = dbstart(dentry);
1091 + bend = dbend(dentry);
1092 +
1093 + lower_dentry = unionfs_lower_dentry(dentry);
1094 + if (willwrite && IS_WRITE_FLAG(file->f_flags) && is_robranch(dentry)) {
1095 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1096 + err = copyup_file(parent_inode, file, bstart, bindex,
1097 + i_size_read(dentry->d_inode));
1098 + if (!err)
1099 + break;
1100 + }
1101 + atomic_set(&UNIONFS_F(file)->generation,
1102 + atomic_read(&UNIONFS_I(dentry->d_inode)->
1103 + generation));
1104 + goto out;
1105 + }
1106 +
1107 + dget(lower_dentry);
1108 + unionfs_mntget(dentry, bstart);
1109 + lower_file = dentry_open(lower_dentry,
1110 + unionfs_lower_mnt_idx(dentry, bstart),
1111 + file->f_flags, current_cred());
1112 + if (IS_ERR(lower_file)) {
1113 + err = PTR_ERR(lower_file);
1114 + goto out;
1115 + }
1116 + branchget(sb, bstart);
1117 + unionfs_set_lower_file(file, lower_file);
1118 + /* Fix up the position. */
1119 + lower_file->f_pos = file->f_pos;
1120 +
1121 + memcpy(&lower_file->f_ra, &file->f_ra, sizeof(struct file_ra_state));
1122 +out:
1123 + dput(parent);
1124 + return err;
1125 +}
1126 +
1127 +/* perform a delayed copyup of a read-write file on a read-only branch */
1128 +static int do_delayed_copyup(struct file *file, struct dentry *parent)
1129 +{
1130 + int bindex, bstart, bend, err = 0;
1131 + struct dentry *dentry = file->f_path.dentry;
1132 + struct inode *parent_inode = parent->d_inode;
1133 +
1134 + bstart = fbstart(file);
1135 + bend = fbend(file);
1136 +
1137 + BUG_ON(!S_ISREG(dentry->d_inode->i_mode));
1138 +
1139 + unionfs_check_file(file);
1140 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1141 + if (!d_deleted(dentry))
1142 + err = copyup_file(parent_inode, file, bstart,
1143 + bindex,
1144 + i_size_read(dentry->d_inode));
1145 + else
1146 + err = copyup_deleted_file(file, dentry, parent,
1147 + bstart, bindex);
1148 + /* if succeeded, set lower open-file flags and break */
1149 + if (!err) {
1150 + struct file *lower_file;
1151 + lower_file = unionfs_lower_file_idx(file, bindex);
1152 + lower_file->f_flags = file->f_flags;
1153 + break;
1154 + }
1155 + }
1156 + if (err || (bstart <= fbstart(file)))
1157 + goto out;
1158 + bend = fbend(file);
1159 + for (bindex = bstart; bindex <= bend; bindex++) {
1160 + if (unionfs_lower_file_idx(file, bindex)) {
1161 + branchput(dentry->d_sb, bindex);
1162 + fput(unionfs_lower_file_idx(file, bindex));
1163 + unionfs_set_lower_file_idx(file, bindex, NULL);
1164 + }
1165 + }
1166 + path_put_lowers(dentry, bstart, bend, false);
1167 + iput_lowers(dentry->d_inode, bstart, bend, false);
1168 + /* for reg file, we only open it "once" */
1169 + fbend(file) = fbstart(file);
1170 + dbend(dentry) = dbstart(dentry);
1171 + ibend(dentry->d_inode) = ibstart(dentry->d_inode);
1172 +
1173 +out:
1174 + unionfs_check_file(file);
1175 + return err;
1176 +}
1177 +
1178 +/*
1179 + * Helper function for unionfs_file_revalidate/locked.
1180 + * Expects dentry/parent to be locked already, and revalidated.
1181 + */
1182 +static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry,
1183 + struct dentry *parent,
1184 + struct super_block *sb, int sbgen,
1185 + int dgen, bool willwrite)
1186 +{
1187 + int fgen;
1188 + int bstart, bend, orig_brid;
1189 + int size;
1190 + int err = 0;
1191 +
1192 + fgen = atomic_read(&UNIONFS_F(file)->generation);
1193 +
1194 + /*
1195 + * There are two cases we are interested in. The first is if the
1196 + * generation is lower than the super-block. The second is if
1197 + * someone has copied up this file from underneath us, we also need
1198 + * to refresh things.
1199 + */
1200 + if (d_deleted(dentry) ||
1201 + (sbgen <= fgen &&
1202 + dbstart(dentry) == fbstart(file) &&
1203 + unionfs_lower_file(file)))
1204 + goto out_may_copyup;
1205 +
1206 + /* save orig branch ID */
1207 + orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1208 +
1209 + /* First we throw out the existing files. */
1210 + cleanup_file(file);
1211 +
1212 + /* Now we reopen the file(s) as in unionfs_open. */
1213 + bstart = fbstart(file) = dbstart(dentry);
1214 + bend = fbend(file) = dbend(dentry);
1215 +
1216 + size = sizeof(struct file *) * sbmax(sb);
1217 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1218 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1219 + err = -ENOMEM;
1220 + goto out;
1221 + }
1222 + size = sizeof(int) * sbmax(sb);
1223 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1224 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1225 + err = -ENOMEM;
1226 + goto out;
1227 + }
1228 +
1229 + if (S_ISDIR(dentry->d_inode->i_mode)) {
1230 + /* We need to open all the files. */
1231 + err = open_all_files(file);
1232 + if (err)
1233 + goto out;
1234 + } else {
1235 + int new_brid;
1236 + /* We only open the highest priority branch. */
1237 + err = open_highest_file(file, willwrite);
1238 + if (err)
1239 + goto out;
1240 + new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)];
1241 + if (unlikely(new_brid != orig_brid && sbgen > fgen)) {
1242 + /*
1243 + * If we re-opened the file on a different branch
1244 + * than the original one, and this was due to a new
1245 + * branch inserted, then update the mnt counts of
1246 + * the old and new branches accordingly.
1247 + */
1248 + unionfs_mntget(dentry, bstart);
1249 + unionfs_mntput(sb->s_root,
1250 + branch_id_to_idx(sb, orig_brid));
1251 + }
1252 + /* regular files have only one open lower file */
1253 + fbend(file) = fbstart(file);
1254 + }
1255 + atomic_set(&UNIONFS_F(file)->generation,
1256 + atomic_read(&UNIONFS_I(dentry->d_inode)->generation));
1257 +
1258 +out_may_copyup:
1259 + /* Copyup on the first write to a file on a readonly branch. */
1260 + if (willwrite && IS_WRITE_FLAG(file->f_flags) &&
1261 + !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) &&
1262 + is_robranch(dentry)) {
1263 + pr_debug("unionfs: do delay copyup of \"%s\"\n",
1264 + dentry->d_name.name);
1265 + err = do_delayed_copyup(file, parent);
1266 + /* regular files have only one open lower file */
1267 + if (!err && !S_ISDIR(dentry->d_inode->i_mode))
1268 + fbend(file) = fbstart(file);
1269 + }
1270 +
1271 +out:
1272 + if (err) {
1273 + kfree(UNIONFS_F(file)->lower_files);
1274 + kfree(UNIONFS_F(file)->saved_branch_ids);
1275 + }
1276 + return err;
1277 +}
1278 +
1279 +/*
1280 + * Revalidate the struct file
1281 + * @file: file to revalidate
1282 + * @parent: parent dentry (locked by caller)
1283 + * @willwrite: true if caller may cause changes to the file; false otherwise.
1284 + * Caller must lock/unlock dentry's branch configuration.
1285 + */
1286 +int unionfs_file_revalidate(struct file *file, struct dentry *parent,
1287 + bool willwrite)
1288 +{
1289 + struct super_block *sb;
1290 + struct dentry *dentry;
1291 + int sbgen, dgen;
1292 + int err = 0;
1293 +
1294 + dentry = file->f_path.dentry;
1295 + sb = dentry->d_sb;
1296 + verify_locked(dentry);
1297 + verify_locked(parent);
1298 +
1299 + /*
1300 + * First revalidate the dentry inside struct file,
1301 + * but not unhashed dentries.
1302 + */
1303 + if (!d_deleted(dentry) &&
1304 + !__unionfs_d_revalidate(dentry, parent, willwrite)) {
1305 + err = -ESTALE;
1306 + goto out;
1307 + }
1308 +
1309 + sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
1310 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
1311 +
1312 + if (unlikely(sbgen > dgen)) { /* XXX: should never happen */
1313 + pr_debug("unionfs: failed to revalidate dentry (%s)\n",
1314 + dentry->d_name.name);
1315 + err = -ESTALE;
1316 + goto out;
1317 + }
1318 +
1319 + err = __unionfs_file_revalidate(file, dentry, parent, sb,
1320 + sbgen, dgen, willwrite);
1321 +out:
1322 + return err;
1323 +}
1324 +
1325 +/* unionfs_open helper function: open a directory */
1326 +static int __open_dir(struct inode *inode, struct file *file)
1327 +{
1328 + struct dentry *lower_dentry;
1329 + struct file *lower_file;
1330 + int bindex, bstart, bend;
1331 + struct vfsmount *mnt;
1332 +
1333 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1334 + bend = fbend(file) = dbend(file->f_path.dentry);
1335 +
1336 + for (bindex = bstart; bindex <= bend; bindex++) {
1337 + lower_dentry =
1338 + unionfs_lower_dentry_idx(file->f_path.dentry, bindex);
1339 + if (!lower_dentry)
1340 + continue;
1341 +
1342 + dget(lower_dentry);
1343 + unionfs_mntget(file->f_path.dentry, bindex);
1344 + mnt = unionfs_lower_mnt_idx(file->f_path.dentry, bindex);
1345 + lower_file = dentry_open(lower_dentry, mnt, file->f_flags,
1346 + current_cred());
1347 + if (IS_ERR(lower_file))
1348 + return PTR_ERR(lower_file);
1349 +
1350 + unionfs_set_lower_file_idx(file, bindex, lower_file);
1351 +
1352 + /*
1353 + * The branchget goes after the open, because otherwise
1354 + * we would miss the reference on release.
1355 + */
1356 + branchget(inode->i_sb, bindex);
1357 + }
1358 +
1359 + return 0;
1360 +}
1361 +
1362 +/* unionfs_open helper function: open a file */
1363 +static int __open_file(struct inode *inode, struct file *file,
1364 + struct dentry *parent)
1365 +{
1366 + struct dentry *lower_dentry;
1367 + struct file *lower_file;
1368 + int lower_flags;
1369 + int bindex, bstart, bend;
1370 +
1371 + lower_dentry = unionfs_lower_dentry(file->f_path.dentry);
1372 + lower_flags = file->f_flags;
1373 +
1374 + bstart = fbstart(file) = dbstart(file->f_path.dentry);
1375 + bend = fbend(file) = dbend(file->f_path.dentry);
1376 +
1377 + /*
1378 + * check for the permission for lower file. If the error is
1379 + * COPYUP_ERR, copyup the file.
1380 + */
1381 + if (lower_dentry->d_inode && is_robranch(file->f_path.dentry)) {
1382 + /*
1383 + * if the open will change the file, copy it up otherwise
1384 + * defer it.
1385 + */
1386 + if (lower_flags & O_TRUNC) {
1387 + int size = 0;
1388 + int err = -EROFS;
1389 +
1390 + /* copyup the file */
1391 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
1392 + err = copyup_file(parent->d_inode, file,
1393 + bstart, bindex, size);
1394 + if (!err)
1395 + break;
1396 + }
1397 + return err;
1398 + } else {
1399 + /*
1400 + * turn off writeable flags, to force delayed copyup
1401 + * by caller.
1402 + */
1403 + lower_flags &= ~(OPEN_WRITE_FLAGS);
1404 + }
1405 + }
1406 +
1407 + dget(lower_dentry);
1408 +
1409 + /*
1410 + * dentry_open will decrement mnt refcnt if err.
1411 + * otherwise fput() will do an mntput() for us upon file close.
1412 + */
1413 + unionfs_mntget(file->f_path.dentry, bstart);
1414 + lower_file =
1415 + dentry_open(lower_dentry,
1416 + unionfs_lower_mnt_idx(file->f_path.dentry, bstart),
1417 + lower_flags, current_cred());
1418 + if (IS_ERR(lower_file))
1419 + return PTR_ERR(lower_file);
1420 +
1421 + unionfs_set_lower_file(file, lower_file);
1422 + branchget(inode->i_sb, bstart);
1423 +
1424 + return 0;
1425 +}
1426 +
1427 +int unionfs_open(struct inode *inode, struct file *file)
1428 +{
1429 + int err = 0;
1430 + struct file *lower_file = NULL;
1431 + struct dentry *dentry = file->f_path.dentry;
1432 + struct dentry *parent;
1433 + int bindex = 0, bstart = 0, bend = 0;
1434 + int size;
1435 + int valid = 0;
1436 +
1437 + unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT);
1438 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1439 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1440 +
1441 + /* don't open unhashed/deleted files */
1442 + if (d_deleted(dentry)) {
1443 + err = -ENOENT;
1444 + goto out_nofree;
1445 + }
1446 +
1447 + /* XXX: should I change 'false' below to the 'willwrite' flag? */
1448 + valid = __unionfs_d_revalidate(dentry, parent, false);
1449 + if (unlikely(!valid)) {
1450 + err = -ESTALE;
1451 + goto out_nofree;
1452 + }
1453 +
1454 + file->private_data =
1455 + kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
1456 + if (unlikely(!UNIONFS_F(file))) {
1457 + err = -ENOMEM;
1458 + goto out_nofree;
1459 + }
1460 + fbstart(file) = -1;
1461 + fbend(file) = -1;
1462 + atomic_set(&UNIONFS_F(file)->generation,
1463 + atomic_read(&UNIONFS_I(inode)->generation));
1464 +
1465 + size = sizeof(struct file *) * sbmax(inode->i_sb);
1466 + UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL);
1467 + if (unlikely(!UNIONFS_F(file)->lower_files)) {
1468 + err = -ENOMEM;
1469 + goto out;
1470 + }
1471 + size = sizeof(int) * sbmax(inode->i_sb);
1472 + UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL);
1473 + if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) {
1474 + err = -ENOMEM;
1475 + goto out;
1476 + }
1477 +
1478 + bstart = fbstart(file) = dbstart(dentry);
1479 + bend = fbend(file) = dbend(dentry);
1480 +
1481 + /*
1482 + * open all directories and make the unionfs file struct point to
1483 + * these lower file structs
1484 + */
1485 + if (S_ISDIR(inode->i_mode))
1486 + err = __open_dir(inode, file); /* open a dir */
1487 + else
1488 + err = __open_file(inode, file, parent); /* open a file */
1489 +
1490 + /* freeing the allocated resources, and fput the opened files */
1491 + if (err) {
1492 + for (bindex = bstart; bindex <= bend; bindex++) {
1493 + lower_file = unionfs_lower_file_idx(file, bindex);
1494 + if (!lower_file)
1495 + continue;
1496 +
1497 + branchput(dentry->d_sb, bindex);
1498 + /* fput calls dput for lower_dentry */
1499 + fput(lower_file);
1500 + }
1501 + }
1502 +
1503 +out:
1504 + if (err) {
1505 + kfree(UNIONFS_F(file)->lower_files);
1506 + kfree(UNIONFS_F(file)->saved_branch_ids);
1507 + kfree(UNIONFS_F(file));
1508 + }
1509 +out_nofree:
1510 + if (!err) {
1511 + unionfs_postcopyup_setmnt(dentry);
1512 + unionfs_copy_attr_times(inode);
1513 + unionfs_check_file(file);
1514 + unionfs_check_inode(inode);
1515 + }
1516 + unionfs_unlock_dentry(dentry);
1517 + unionfs_unlock_parent(dentry, parent);
1518 + unionfs_read_unlock(inode->i_sb);
1519 + return err;
1520 +}
1521 +
1522 +/*
1523 + * release all lower object references & free the file info structure
1524 + *
1525 + * No need to grab sb info's rwsem.
1526 + */
1527 +int unionfs_file_release(struct inode *inode, struct file *file)
1528 +{
1529 + struct file *lower_file = NULL;
1530 + struct unionfs_file_info *fileinfo;
1531 + struct unionfs_inode_info *inodeinfo;
1532 + struct super_block *sb = inode->i_sb;
1533 + struct dentry *dentry = file->f_path.dentry;
1534 + struct dentry *parent;
1535 + int bindex, bstart, bend;
1536 + int fgen, err = 0;
1537 +
1538 + /*
1539 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
1540 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
1541 + * has been causing false positives in file system stacking layers.
1542 + * In particular, our ->mmap is called after sys_mmap2 already holds
1543 + * mmap_sem, then we lock our own mutexes; but earlier, it's
1544 + * possible for lockdep to have locked our mutexes first, and then
1545 + * we call a lower ->readdir which could call might_fault. The
1546 + * different ordering of the locks is what lockdep complains about
1547 + * -- unnecessarily. Therefore, we have no choice but to tell
1548 + * lockdep to temporarily turn off lockdep here. Note: the comments
1549 + * inside might_sleep also suggest that it would have been
1550 + * nicer to only annotate paths that needs that might_lock_read.
1551 + */
1552 + lockdep_off();
1553 + unionfs_read_lock(sb, UNIONFS_SMUTEX_PARENT);
1554 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1555 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1556 +
1557 + /*
1558 + * We try to revalidate, but the VFS ignores return return values
1559 + * from file->release, so we must always try to succeed here,
1560 + * including to do the kfree and dput below. So if revalidation
1561 + * failed, all we can do is print some message and keep going.
1562 + */
1563 + err = unionfs_file_revalidate(file, parent,
1564 + UNIONFS_F(file)->wrote_to_file);
1565 + if (!err)
1566 + unionfs_check_file(file);
1567 + fileinfo = UNIONFS_F(file);
1568 + BUG_ON(file->f_path.dentry->d_inode != inode);
1569 + inodeinfo = UNIONFS_I(inode);
1570 +
1571 + /* fput all the lower files */
1572 + fgen = atomic_read(&fileinfo->generation);
1573 + bstart = fbstart(file);
1574 + bend = fbend(file);
1575 +
1576 + for (bindex = bstart; bindex <= bend; bindex++) {
1577 + lower_file = unionfs_lower_file_idx(file, bindex);
1578 +
1579 + if (lower_file) {
1580 + unionfs_set_lower_file_idx(file, bindex, NULL);
1581 + fput(lower_file);
1582 + branchput(sb, bindex);
1583 + }
1584 +
1585 + /* if there are no more refs to the dentry, dput it */
1586 + if (d_deleted(dentry)) {
1587 + dput(unionfs_lower_dentry_idx(dentry, bindex));
1588 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1589 + }
1590 + }
1591 +
1592 + kfree(fileinfo->lower_files);
1593 + kfree(fileinfo->saved_branch_ids);
1594 +
1595 + if (fileinfo->rdstate) {
1596 + fileinfo->rdstate->access = jiffies;
1597 + spin_lock(&inodeinfo->rdlock);
1598 + inodeinfo->rdcount++;
1599 + list_add_tail(&fileinfo->rdstate->cache,
1600 + &inodeinfo->readdircache);
1601 + mark_inode_dirty(inode);
1602 + spin_unlock(&inodeinfo->rdlock);
1603 + fileinfo->rdstate = NULL;
1604 + }
1605 + kfree(fileinfo);
1606 +
1607 + unionfs_unlock_dentry(dentry);
1608 + unionfs_unlock_parent(dentry, parent);
1609 + unionfs_read_unlock(sb);
1610 + lockdep_on();
1611 + return err;
1612 +}
1613 +
1614 +/* pass the ioctl to the lower fs */
1615 +static long do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1616 +{
1617 + struct file *lower_file;
1618 + int err;
1619 +
1620 + lower_file = unionfs_lower_file(file);
1621 +
1622 + err = -ENOTTY;
1623 + if (!lower_file || !lower_file->f_op)
1624 + goto out;
1625 + if (lower_file->f_op->unlocked_ioctl) {
1626 + err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
1627 + } else if (lower_file->f_op->ioctl) {
1628 + lock_kernel();
1629 + err = lower_file->f_op->ioctl(
1630 + lower_file->f_path.dentry->d_inode,
1631 + lower_file, cmd, arg);
1632 + unlock_kernel();
1633 + }
1634 +
1635 +out:
1636 + return err;
1637 +}
1638 +
1639 +/*
1640 + * return to user-space the branch indices containing the file in question
1641 + *
1642 + * We use fd_set and therefore we are limited to the number of the branches
1643 + * to FD_SETSIZE, which is currently 1024 - plenty for most people
1644 + */
1645 +static int unionfs_ioctl_queryfile(struct file *file, struct dentry *parent,
1646 + unsigned int cmd, unsigned long arg)
1647 +{
1648 + int err = 0;
1649 + fd_set branchlist;
1650 + int bstart = 0, bend = 0, bindex = 0;
1651 + int orig_bstart, orig_bend;
1652 + struct dentry *dentry, *lower_dentry;
1653 + struct vfsmount *mnt;
1654 +
1655 + dentry = file->f_path.dentry;
1656 + orig_bstart = dbstart(dentry);
1657 + orig_bend = dbend(dentry);
1658 + err = unionfs_partial_lookup(dentry, parent);
1659 + if (err)
1660 + goto out;
1661 + bstart = dbstart(dentry);
1662 + bend = dbend(dentry);
1663 +
1664 + FD_ZERO(&branchlist);
1665 +
1666 + for (bindex = bstart; bindex <= bend; bindex++) {
1667 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
1668 + if (!lower_dentry)
1669 + continue;
1670 + if (likely(lower_dentry->d_inode))
1671 + FD_SET(bindex, &branchlist);
1672 + /* purge any lower objects after partial_lookup */
1673 + if (bindex < orig_bstart || bindex > orig_bend) {
1674 + dput(lower_dentry);
1675 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
1676 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
1677 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
1678 + NULL);
1679 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
1680 + if (!mnt)
1681 + continue;
1682 + unionfs_mntput(dentry, bindex);
1683 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
1684 + }
1685 + }
1686 + /* restore original dentry's offsets */
1687 + dbstart(dentry) = orig_bstart;
1688 + dbend(dentry) = orig_bend;
1689 + ibstart(dentry->d_inode) = orig_bstart;
1690 + ibend(dentry->d_inode) = orig_bend;
1691 +
1692 + err = copy_to_user((void __user *)arg, &branchlist, sizeof(fd_set));
1693 + if (unlikely(err))
1694 + err = -EFAULT;
1695 +
1696 +out:
1697 + return err < 0 ? err : bend;
1698 +}
1699 +
1700 +long unionfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1701 +{
1702 + long err;
1703 + struct dentry *dentry = file->f_path.dentry;
1704 + struct dentry *parent;
1705 +
1706 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1707 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1708 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1709 +
1710 + err = unionfs_file_revalidate(file, parent, true);
1711 + if (unlikely(err))
1712 + goto out;
1713 +
1714 + /* check if asked for local commands */
1715 + switch (cmd) {
1716 + case UNIONFS_IOCTL_INCGEN:
1717 + /* Increment the superblock generation count */
1718 + pr_info("unionfs: incgen ioctl deprecated; "
1719 + "use \"-o remount,incgen\"\n");
1720 + err = -ENOSYS;
1721 + break;
1722 +
1723 + case UNIONFS_IOCTL_QUERYFILE:
1724 + /* Return list of branches containing the given file */
1725 + err = unionfs_ioctl_queryfile(file, parent, cmd, arg);
1726 + break;
1727 +
1728 + default:
1729 + /* pass the ioctl down */
1730 + err = do_ioctl(file, cmd, arg);
1731 + break;
1732 + }
1733 +
1734 +out:
1735 + unionfs_check_file(file);
1736 + unionfs_unlock_dentry(dentry);
1737 + unionfs_unlock_parent(dentry, parent);
1738 + unionfs_read_unlock(dentry->d_sb);
1739 + return err;
1740 +}
1741 +
1742 +int unionfs_flush(struct file *file, fl_owner_t id)
1743 +{
1744 + int err = 0;
1745 + struct file *lower_file = NULL;
1746 + struct dentry *dentry = file->f_path.dentry;
1747 + struct dentry *parent;
1748 + int bindex, bstart, bend;
1749 +
1750 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
1751 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
1752 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
1753 +
1754 + err = unionfs_file_revalidate(file, parent,
1755 + UNIONFS_F(file)->wrote_to_file);
1756 + if (unlikely(err))
1757 + goto out;
1758 + unionfs_check_file(file);
1759 +
1760 + bstart = fbstart(file);
1761 + bend = fbend(file);
1762 + for (bindex = bstart; bindex <= bend; bindex++) {
1763 + lower_file = unionfs_lower_file_idx(file, bindex);
1764 +
1765 + if (lower_file && lower_file->f_op &&
1766 + lower_file->f_op->flush) {
1767 + err = lower_file->f_op->flush(lower_file, id);
1768 + if (err)
1769 + goto out;
1770 + }
1771 +
1772 + }
1773 +
1774 +out:
1775 + if (!err)
1776 + unionfs_check_file(file);
1777 + unionfs_unlock_dentry(dentry);
1778 + unionfs_unlock_parent(dentry, parent);
1779 + unionfs_read_unlock(dentry->d_sb);
1780 + return err;
1781 +}
1782 diff --git a/fs/unionfs/copyup.c b/fs/unionfs/copyup.c
1783 new file mode 100644
1784 index 0000000..c43cc7f
1785 --- /dev/null
1786 +++ b/fs/unionfs/copyup.c
1787 @@ -0,0 +1,897 @@
1788 +/*
1789 + * Copyright (c) 2003-2009 Erez Zadok
1790 + * Copyright (c) 2003-2006 Charles P. Wright
1791 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
1792 + * Copyright (c) 2005-2006 Junjiro Okajima
1793 + * Copyright (c) 2005 Arun M. Krishnakumar
1794 + * Copyright (c) 2004-2006 David P. Quigley
1795 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
1796 + * Copyright (c) 2003 Puja Gupta
1797 + * Copyright (c) 2003 Harikesavan Krishnan
1798 + * Copyright (c) 2003-2009 Stony Brook University
1799 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
1800 + *
1801 + * This program is free software; you can redistribute it and/or modify
1802 + * it under the terms of the GNU General Public License version 2 as
1803 + * published by the Free Software Foundation.
1804 + */
1805 +
1806 +#include "union.h"
1807 +
1808 +/*
1809 + * For detailed explanation of copyup see:
1810 + * Documentation/filesystems/unionfs/concepts.txt
1811 + */
1812 +
1813 +#ifdef CONFIG_UNION_FS_XATTR
1814 +/* copyup all extended attrs for a given dentry */
1815 +static int copyup_xattrs(struct dentry *old_lower_dentry,
1816 + struct dentry *new_lower_dentry)
1817 +{
1818 + int err = 0;
1819 + ssize_t list_size = -1;
1820 + char *name_list = NULL;
1821 + char *attr_value = NULL;
1822 + char *name_list_buf = NULL;
1823 +
1824 + /* query the actual size of the xattr list */
1825 + list_size = vfs_listxattr(old_lower_dentry, NULL, 0);
1826 + if (list_size <= 0) {
1827 + err = list_size;
1828 + goto out;
1829 + }
1830 +
1831 + /* allocate space for the actual list */
1832 + name_list = unionfs_xattr_alloc(list_size + 1, XATTR_LIST_MAX);
1833 + if (unlikely(!name_list || IS_ERR(name_list))) {
1834 + err = PTR_ERR(name_list);
1835 + goto out;
1836 + }
1837 +
1838 + name_list_buf = name_list; /* save for kfree at end */
1839 +
1840 + /* now get the actual xattr list of the source file */
1841 + list_size = vfs_listxattr(old_lower_dentry, name_list, list_size);
1842 + if (list_size <= 0) {
1843 + err = list_size;
1844 + goto out;
1845 + }
1846 +
1847 + /* allocate space to hold each xattr's value */
1848 + attr_value = unionfs_xattr_alloc(XATTR_SIZE_MAX, XATTR_SIZE_MAX);
1849 + if (unlikely(!attr_value || IS_ERR(attr_value))) {
1850 + err = PTR_ERR(name_list);
1851 + goto out;
1852 + }
1853 +
1854 + /* in a loop, get and set each xattr from src to dst file */
1855 + while (*name_list) {
1856 + ssize_t size;
1857 +
1858 + /* Lock here since vfs_getxattr doesn't lock for us */
1859 + mutex_lock(&old_lower_dentry->d_inode->i_mutex);
1860 + size = vfs_getxattr(old_lower_dentry, name_list,
1861 + attr_value, XATTR_SIZE_MAX);
1862 + mutex_unlock(&old_lower_dentry->d_inode->i_mutex);
1863 + if (size < 0) {
1864 + err = size;
1865 + goto out;
1866 + }
1867 + if (size > XATTR_SIZE_MAX) {
1868 + err = -E2BIG;
1869 + goto out;
1870 + }
1871 + /* Don't lock here since vfs_setxattr does it for us. */
1872 + err = vfs_setxattr(new_lower_dentry, name_list, attr_value,
1873 + size, 0);
1874 + /*
1875 + * Selinux depends on "security.*" xattrs, so to maintain
1876 + * the security of copied-up files, if Selinux is active,
1877 + * then we must copy these xattrs as well. So we need to
1878 + * temporarily get FOWNER privileges.
1879 + * XXX: move entire copyup code to SIOQ.
1880 + */
1881 + if (err == -EPERM && !capable(CAP_FOWNER)) {
1882 + const struct cred *old_creds;
1883 + struct cred *new_creds;
1884 +
1885 + new_creds = prepare_creds();
1886 + if (unlikely(!new_creds)) {
1887 + err = -ENOMEM;
1888 + goto out;
1889 + }
1890 + cap_raise(new_creds->cap_effective, CAP_FOWNER);
1891 + old_creds = override_creds(new_creds);
1892 + err = vfs_setxattr(new_lower_dentry, name_list,
1893 + attr_value, size, 0);
1894 + revert_creds(old_creds);
1895 + }
1896 + if (err < 0)
1897 + goto out;
1898 + name_list += strlen(name_list) + 1;
1899 + }
1900 +out:
1901 + unionfs_xattr_kfree(name_list_buf);
1902 + unionfs_xattr_kfree(attr_value);
1903 + /* Ignore if xattr isn't supported */
1904 + if (err == -ENOTSUPP || err == -EOPNOTSUPP)
1905 + err = 0;
1906 + return err;
1907 +}
1908 +#endif /* CONFIG_UNION_FS_XATTR */
1909 +
1910 +/*
1911 + * Determine the mode based on the copyup flags, and the existing dentry.
1912 + *
1913 + * Handle file systems which may not support certain options. For example
1914 + * jffs2 doesn't allow one to chmod a symlink. So we ignore such harmless
1915 + * errors, rather than propagating them up, which results in copyup errors
1916 + * and errors returned back to users.
1917 + */
1918 +static int copyup_permissions(struct super_block *sb,
1919 + struct dentry *old_lower_dentry,
1920 + struct dentry *new_lower_dentry)
1921 +{
1922 + struct inode *i = old_lower_dentry->d_inode;
1923 + struct iattr newattrs;
1924 + int err;
1925 +
1926 + newattrs.ia_atime = i->i_atime;
1927 + newattrs.ia_mtime = i->i_mtime;
1928 + newattrs.ia_ctime = i->i_ctime;
1929 + newattrs.ia_gid = i->i_gid;
1930 + newattrs.ia_uid = i->i_uid;
1931 + newattrs.ia_valid = ATTR_CTIME | ATTR_ATIME | ATTR_MTIME |
1932 + ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_FORCE |
1933 + ATTR_GID | ATTR_UID;
1934 + mutex_lock(&new_lower_dentry->d_inode->i_mutex);
1935 + err = notify_change(new_lower_dentry, &newattrs);
1936 + if (err)
1937 + goto out;
1938 +
1939 + /* now try to change the mode and ignore EOPNOTSUPP on symlinks */
1940 + newattrs.ia_mode = i->i_mode;
1941 + newattrs.ia_valid = ATTR_MODE | ATTR_FORCE;
1942 + err = notify_change(new_lower_dentry, &newattrs);
1943 + if (err == -EOPNOTSUPP &&
1944 + S_ISLNK(new_lower_dentry->d_inode->i_mode)) {
1945 + printk(KERN_WARNING
1946 + "unionfs: changing \"%s\" symlink mode unsupported\n",
1947 + new_lower_dentry->d_name.name);
1948 + err = 0;
1949 + }
1950 +
1951 +out:
1952 + mutex_unlock(&new_lower_dentry->d_inode->i_mutex);
1953 + return err;
1954 +}
1955 +
1956 +/*
1957 + * create the new device/file/directory - use copyup_permission to copyup
1958 + * times, and mode
1959 + *
1960 + * if the object being copied up is a regular file, the file is only created,
1961 + * the contents have to be copied up separately
1962 + */
1963 +static int __copyup_ndentry(struct dentry *old_lower_dentry,
1964 + struct dentry *new_lower_dentry,
1965 + struct dentry *new_lower_parent_dentry,
1966 + char *symbuf)
1967 +{
1968 + int err = 0;
1969 + umode_t old_mode = old_lower_dentry->d_inode->i_mode;
1970 + struct sioq_args args;
1971 +
1972 + if (S_ISDIR(old_mode)) {
1973 + args.mkdir.parent = new_lower_parent_dentry->d_inode;
1974 + args.mkdir.dentry = new_lower_dentry;
1975 + args.mkdir.mode = old_mode;
1976 +
1977 + run_sioq(__unionfs_mkdir, &args);
1978 + err = args.err;
1979 + } else if (S_ISLNK(old_mode)) {
1980 + args.symlink.parent = new_lower_parent_dentry->d_inode;
1981 + args.symlink.dentry = new_lower_dentry;
1982 + args.symlink.symbuf = symbuf;
1983 +
1984 + run_sioq(__unionfs_symlink, &args);
1985 + err = args.err;
1986 + } else if (S_ISBLK(old_mode) || S_ISCHR(old_mode) ||
1987 + S_ISFIFO(old_mode) || S_ISSOCK(old_mode)) {
1988 + args.mknod.parent = new_lower_parent_dentry->d_inode;
1989 + args.mknod.dentry = new_lower_dentry;
1990 + args.mknod.mode = old_mode;
1991 + args.mknod.dev = old_lower_dentry->d_inode->i_rdev;
1992 +
1993 + run_sioq(__unionfs_mknod, &args);
1994 + err = args.err;
1995 + } else if (S_ISREG(old_mode)) {
1996 + struct nameidata nd;
1997 + err = init_lower_nd(&nd, LOOKUP_CREATE);
1998 + if (unlikely(err < 0))
1999 + goto out;
2000 + args.create.nd = &nd;
2001 + args.create.parent = new_lower_parent_dentry->d_inode;
2002 + args.create.dentry = new_lower_dentry;
2003 + args.create.mode = old_mode;
2004 +
2005 + run_sioq(__unionfs_create, &args);
2006 + err = args.err;
2007 + release_lower_nd(&nd, err);
2008 + } else {
2009 + printk(KERN_CRIT "unionfs: unknown inode type %d\n",
2010 + old_mode);
2011 + BUG();
2012 + }
2013 +
2014 +out:
2015 + return err;
2016 +}
2017 +
2018 +static int __copyup_reg_data(struct dentry *dentry,
2019 + struct dentry *new_lower_dentry, int new_bindex,
2020 + struct dentry *old_lower_dentry, int old_bindex,
2021 + struct file **copyup_file, loff_t len)
2022 +{
2023 + struct super_block *sb = dentry->d_sb;
2024 + struct file *input_file;
2025 + struct file *output_file;
2026 + struct vfsmount *output_mnt;
2027 + mm_segment_t old_fs;
2028 + char *buf = NULL;
2029 + ssize_t read_bytes, write_bytes;
2030 + loff_t size;
2031 + int err = 0;
2032 +
2033 + /* open old file */
2034 + unionfs_mntget(dentry, old_bindex);
2035 + branchget(sb, old_bindex);
2036 + /* dentry_open calls dput and mntput if it returns an error */
2037 + input_file = dentry_open(old_lower_dentry,
2038 + unionfs_lower_mnt_idx(dentry, old_bindex),
2039 + O_RDONLY | O_LARGEFILE, current_cred());
2040 + if (IS_ERR(input_file)) {
2041 + dput(old_lower_dentry);
2042 + err = PTR_ERR(input_file);
2043 + goto out;
2044 + }
2045 + if (unlikely(!input_file->f_op || !input_file->f_op->read)) {
2046 + err = -EINVAL;
2047 + goto out_close_in;
2048 + }
2049 +
2050 + /* open new file */
2051 + dget(new_lower_dentry);
2052 + output_mnt = unionfs_mntget(sb->s_root, new_bindex);
2053 + branchget(sb, new_bindex);
2054 + output_file = dentry_open(new_lower_dentry, output_mnt,
2055 + O_RDWR | O_LARGEFILE, current_cred());
2056 + if (IS_ERR(output_file)) {
2057 + err = PTR_ERR(output_file);
2058 + goto out_close_in2;
2059 + }
2060 + if (unlikely(!output_file->f_op || !output_file->f_op->write)) {
2061 + err = -EINVAL;
2062 + goto out_close_out;
2063 + }
2064 +
2065 + /* allocating a buffer */
2066 + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2067 + if (unlikely(!buf)) {
2068 + err = -ENOMEM;
2069 + goto out_close_out;
2070 + }
2071 +
2072 + input_file->f_pos = 0;
2073 + output_file->f_pos = 0;
2074 +
2075 + old_fs = get_fs();
2076 + set_fs(KERNEL_DS);
2077 +
2078 + size = len;
2079 + err = 0;
2080 + do {
2081 + if (len >= PAGE_SIZE)
2082 + size = PAGE_SIZE;
2083 + else if ((len < PAGE_SIZE) && (len > 0))
2084 + size = len;
2085 +
2086 + len -= PAGE_SIZE;
2087 +
2088 + read_bytes =
2089 + input_file->f_op->read(input_file,
2090 + (char __user *)buf, size,
2091 + &input_file->f_pos);
2092 + if (read_bytes <= 0) {
2093 + err = read_bytes;
2094 + break;
2095 + }
2096 +
2097 + /* see Documentation/filesystems/unionfs/issues.txt */
2098 + lockdep_off();
2099 + write_bytes =
2100 + output_file->f_op->write(output_file,
2101 + (char __user *)buf,
2102 + read_bytes,
2103 + &output_file->f_pos);
2104 + lockdep_on();
2105 + if ((write_bytes < 0) || (write_bytes < read_bytes)) {
2106 + err = write_bytes;
2107 + break;
2108 + }
2109 + } while ((read_bytes > 0) && (len > 0));
2110 +
2111 + set_fs(old_fs);
2112 +
2113 + kfree(buf);
2114 +
2115 + if (!err)
2116 + err = output_file->f_op->fsync(output_file,
2117 + new_lower_dentry, 0);
2118 +
2119 + if (err)
2120 + goto out_close_out;
2121 +
2122 + if (copyup_file) {
2123 + *copyup_file = output_file;
2124 + goto out_close_in;
2125 + }
2126 +
2127 +out_close_out:
2128 + fput(output_file);
2129 +
2130 +out_close_in2:
2131 + branchput(sb, new_bindex);
2132 +
2133 +out_close_in:
2134 + fput(input_file);
2135 +
2136 +out:
2137 + branchput(sb, old_bindex);
2138 +
2139 + return err;
2140 +}
2141 +
2142 +/*
2143 + * dput the lower references for old and new dentry & clear a lower dentry
2144 + * pointer
2145 + */
2146 +static void __clear(struct dentry *dentry, struct dentry *old_lower_dentry,
2147 + int old_bstart, int old_bend,
2148 + struct dentry *new_lower_dentry, int new_bindex)
2149 +{
2150 + /* get rid of the lower dentry and all its traces */
2151 + unionfs_set_lower_dentry_idx(dentry, new_bindex, NULL);
2152 + dbstart(dentry) = old_bstart;
2153 + dbend(dentry) = old_bend;
2154 +
2155 + dput(new_lower_dentry);
2156 + dput(old_lower_dentry);
2157 +}
2158 +
2159 +/*
2160 + * Copy up a dentry to a file of specified name.
2161 + *
2162 + * @dir: used to pull the ->i_sb to access other branches
2163 + * @dentry: the non-negative dentry whose lower_inode we should copy
2164 + * @bstart: the branch of the lower_inode to copy from
2165 + * @new_bindex: the branch to create the new file in
2166 + * @name: the name of the file to create
2167 + * @namelen: length of @name
2168 + * @copyup_file: the "struct file" to return (optional)
2169 + * @len: how many bytes to copy-up?
2170 + */
2171 +int copyup_dentry(struct inode *dir, struct dentry *dentry, int bstart,
2172 + int new_bindex, const char *name, int namelen,
2173 + struct file **copyup_file, loff_t len)
2174 +{
2175 + struct dentry *new_lower_dentry;
2176 + struct dentry *old_lower_dentry = NULL;
2177 + struct super_block *sb;
2178 + int err = 0;
2179 + int old_bindex;
2180 + int old_bstart;
2181 + int old_bend;
2182 + struct dentry *new_lower_parent_dentry = NULL;
2183 + mm_segment_t oldfs;
2184 + char *symbuf = NULL;
2185 +
2186 + verify_locked(dentry);
2187 +
2188 + old_bindex = bstart;
2189 + old_bstart = dbstart(dentry);
2190 + old_bend = dbend(dentry);
2191 +
2192 + BUG_ON(new_bindex < 0);
2193 + BUG_ON(new_bindex >= old_bindex);
2194 +
2195 + sb = dir->i_sb;
2196 +
2197 + err = is_robranch_super(sb, new_bindex);
2198 + if (err)
2199 + goto out;
2200 +
2201 + /* Create the directory structure above this dentry. */
2202 + new_lower_dentry = create_parents(dir, dentry, name, new_bindex);
2203 + if (IS_ERR(new_lower_dentry)) {
2204 + err = PTR_ERR(new_lower_dentry);
2205 + goto out;
2206 + }
2207 +
2208 + old_lower_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
2209 + /* we conditionally dput this old_lower_dentry at end of function */
2210 + dget(old_lower_dentry);
2211 +
2212 + /* For symlinks, we must read the link before we lock the directory. */
2213 + if (S_ISLNK(old_lower_dentry->d_inode->i_mode)) {
2214 +
2215 + symbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2216 + if (unlikely(!symbuf)) {
2217 + __clear(dentry, old_lower_dentry,
2218 + old_bstart, old_bend,
2219 + new_lower_dentry, new_bindex);
2220 + err = -ENOMEM;
2221 + goto out_free;
2222 + }
2223 +
2224 + oldfs = get_fs();
2225 + set_fs(KERNEL_DS);
2226 + err = old_lower_dentry->d_inode->i_op->readlink(
2227 + old_lower_dentry,
2228 + (char __user *)symbuf,
2229 + PATH_MAX);
2230 + set_fs(oldfs);
2231 + if (err < 0) {
2232 + __clear(dentry, old_lower_dentry,
2233 + old_bstart, old_bend,
2234 + new_lower_dentry, new_bindex);
2235 + goto out_free;
2236 + }
2237 + symbuf[err] = '\0';
2238 + }
2239 +
2240 + /* Now we lock the parent, and create the object in the new branch. */
2241 + new_lower_parent_dentry = lock_parent(new_lower_dentry);
2242 +
2243 + /* create the new inode */
2244 + err = __copyup_ndentry(old_lower_dentry, new_lower_dentry,
2245 + new_lower_parent_dentry, symbuf);
2246 +
2247 + if (err) {
2248 + __clear(dentry, old_lower_dentry,
2249 + old_bstart, old_bend,
2250 + new_lower_dentry, new_bindex);
2251 + goto out_unlock;
2252 + }
2253 +
2254 + /* We actually copyup the file here. */
2255 + if (S_ISREG(old_lower_dentry->d_inode->i_mode))
2256 + err = __copyup_reg_data(dentry, new_lower_dentry, new_bindex,
2257 + old_lower_dentry, old_bindex,
2258 + copyup_file, len);
2259 + if (err)
2260 + goto out_unlink;
2261 +
2262 + /* Set permissions. */
2263 + err = copyup_permissions(sb, old_lower_dentry, new_lower_dentry);
2264 + if (err)
2265 + goto out_unlink;
2266 +
2267 +#ifdef CONFIG_UNION_FS_XATTR
2268 + /* Selinux uses extended attributes for permissions. */
2269 + err = copyup_xattrs(old_lower_dentry, new_lower_dentry);
2270 + if (err)
2271 + goto out_unlink;
2272 +#endif /* CONFIG_UNION_FS_XATTR */
2273 +
2274 + /* do not allow files getting deleted to be re-interposed */
2275 + if (!d_deleted(dentry))
2276 + unionfs_reinterpose(dentry);
2277 +
2278 + goto out_unlock;
2279 +
2280 +out_unlink:
2281 + /*
2282 + * copyup failed, because we possibly ran out of space or
2283 + * quota, or something else happened so let's unlink; we don't
2284 + * really care about the return value of vfs_unlink
2285 + */
2286 + vfs_unlink(new_lower_parent_dentry->d_inode, new_lower_dentry);
2287 +
2288 + if (copyup_file) {
2289 + /* need to close the file */
2290 +
2291 + fput(*copyup_file);
2292 + branchput(sb, new_bindex);
2293 + }
2294 +
2295 + /*
2296 + * TODO: should we reset the error to something like -EIO?
2297 + *
2298 + * If we don't reset, the user may get some nonsensical errors, but
2299 + * on the other hand, if we reset to EIO, we guarantee that the user
2300 + * will get a "confusing" error message.
2301 + */
2302 +
2303 +out_unlock:
2304 + unlock_dir(new_lower_parent_dentry);
2305 +
2306 +out_free:
2307 + /*
2308 + * If old_lower_dentry was not a file, then we need to dput it. If
2309 + * it was a file, then it was already dput indirectly by other
2310 + * functions we call above which operate on regular files.
2311 + */
2312 + if (old_lower_dentry && old_lower_dentry->d_inode &&
2313 + !S_ISREG(old_lower_dentry->d_inode->i_mode))
2314 + dput(old_lower_dentry);
2315 + kfree(symbuf);
2316 +
2317 + if (err) {
2318 + /*
2319 + * if directory creation succeeded, but inode copyup failed,
2320 + * then purge new dentries.
2321 + */
2322 + if (dbstart(dentry) < old_bstart &&
2323 + ibstart(dentry->d_inode) > dbstart(dentry))
2324 + __clear(dentry, NULL, old_bstart, old_bend,
2325 + unionfs_lower_dentry(dentry), dbstart(dentry));
2326 + goto out;
2327 + }
2328 + if (!S_ISDIR(dentry->d_inode->i_mode)) {
2329 + unionfs_postcopyup_release(dentry);
2330 + if (!unionfs_lower_inode(dentry->d_inode)) {
2331 + /*
2332 + * If we got here, then we copied up to an
2333 + * unlinked-open file, whose name is .unionfsXXXXX.
2334 + */
2335 + struct inode *inode = new_lower_dentry->d_inode;
2336 + atomic_inc(&inode->i_count);
2337 + unionfs_set_lower_inode_idx(dentry->d_inode,
2338 + ibstart(dentry->d_inode),
2339 + inode);
2340 + }
2341 + }
2342 + unionfs_postcopyup_setmnt(dentry);
2343 + /* sync inode times from copied-up inode to our inode */
2344 + unionfs_copy_attr_times(dentry->d_inode);
2345 + unionfs_check_inode(dir);
2346 + unionfs_check_dentry(dentry);
2347 +out:
2348 + return err;
2349 +}
2350 +
2351 +/*
2352 + * This function creates a copy of a file represented by 'file' which
2353 + * currently resides in branch 'bstart' to branch 'new_bindex.' The copy
2354 + * will be named "name".
2355 + */
2356 +int copyup_named_file(struct inode *dir, struct file *file, char *name,
2357 + int bstart, int new_bindex, loff_t len)
2358 +{
2359 + int err = 0;
2360 + struct file *output_file = NULL;
2361 +
2362 + err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex,
2363 + name, strlen(name), &output_file, len);
2364 + if (!err) {
2365 + fbstart(file) = new_bindex;
2366 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2367 + }
2368 +
2369 + return err;
2370 +}
2371 +
2372 +/*
2373 + * This function creates a copy of a file represented by 'file' which
2374 + * currently resides in branch 'bstart' to branch 'new_bindex'.
2375 + */
2376 +int copyup_file(struct inode *dir, struct file *file, int bstart,
2377 + int new_bindex, loff_t len)
2378 +{
2379 + int err = 0;
2380 + struct file *output_file = NULL;
2381 + struct dentry *dentry = file->f_path.dentry;
2382 +
2383 + err = copyup_dentry(dir, dentry, bstart, new_bindex,
2384 + dentry->d_name.name, dentry->d_name.len,
2385 + &output_file, len);
2386 + if (!err) {
2387 + fbstart(file) = new_bindex;
2388 + unionfs_set_lower_file_idx(file, new_bindex, output_file);
2389 + }
2390 +
2391 + return err;
2392 +}
2393 +
2394 +/* purge a dentry's lower-branch states (dput/mntput, etc.) */
2395 +static void __cleanup_dentry(struct dentry *dentry, int bindex,
2396 + int old_bstart, int old_bend)
2397 +{
2398 + int loop_start;
2399 + int loop_end;
2400 + int new_bstart = -1;
2401 + int new_bend = -1;
2402 + int i;
2403 +
2404 + loop_start = min(old_bstart, bindex);
2405 + loop_end = max(old_bend, bindex);
2406 +
2407 + /*
2408 + * This loop sets the bstart and bend for the new dentry by
2409 + * traversing from left to right. It also dputs all negative
2410 + * dentries except bindex
2411 + */
2412 + for (i = loop_start; i <= loop_end; i++) {
2413 + if (!unionfs_lower_dentry_idx(dentry, i))
2414 + continue;
2415 +
2416 + if (i == bindex) {
2417 + new_bend = i;
2418 + if (new_bstart < 0)
2419 + new_bstart = i;
2420 + continue;
2421 + }
2422 +
2423 + if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
2424 + dput(unionfs_lower_dentry_idx(dentry, i));
2425 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
2426 +
2427 + unionfs_mntput(dentry, i);
2428 + unionfs_set_lower_mnt_idx(dentry, i, NULL);
2429 + } else {
2430 + if (new_bstart < 0)
2431 + new_bstart = i;
2432 + new_bend = i;
2433 + }
2434 + }
2435 +
2436 + if (new_bstart < 0)
2437 + new_bstart = bindex;
2438 + if (new_bend < 0)
2439 + new_bend = bindex;
2440 + dbstart(dentry) = new_bstart;
2441 + dbend(dentry) = new_bend;
2442 +
2443 +}
2444 +
2445 +/* set lower inode ptr and update bstart & bend if necessary */
2446 +static void __set_inode(struct dentry *upper, struct dentry *lower,
2447 + int bindex)
2448 +{
2449 + unionfs_set_lower_inode_idx(upper->d_inode, bindex,
2450 + igrab(lower->d_inode));
2451 + if (likely(ibstart(upper->d_inode) > bindex))
2452 + ibstart(upper->d_inode) = bindex;
2453 + if (likely(ibend(upper->d_inode) < bindex))
2454 + ibend(upper->d_inode) = bindex;
2455 +
2456 +}
2457 +
2458 +/* set lower dentry ptr and update bstart & bend if necessary */
2459 +static void __set_dentry(struct dentry *upper, struct dentry *lower,
2460 + int bindex)
2461 +{
2462 + unionfs_set_lower_dentry_idx(upper, bindex, lower);
2463 + if (likely(dbstart(upper) > bindex))
2464 + dbstart(upper) = bindex;
2465 + if (likely(dbend(upper) < bindex))
2466 + dbend(upper) = bindex;
2467 +}
2468 +
2469 +/*
2470 + * This function replicates the directory structure up-to given dentry
2471 + * in the bindex branch.
2472 + */
2473 +struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
2474 + const char *name, int bindex)
2475 +{
2476 + int err;
2477 + struct dentry *child_dentry;
2478 + struct dentry *parent_dentry;
2479 + struct dentry *lower_parent_dentry = NULL;
2480 + struct dentry *lower_dentry = NULL;
2481 + const char *childname;
2482 + unsigned int childnamelen;
2483 + int nr_dentry;
2484 + int count = 0;
2485 + int old_bstart;
2486 + int old_bend;
2487 + struct dentry **path = NULL;
2488 + struct super_block *sb;
2489 +
2490 + verify_locked(dentry);
2491 +
2492 + err = is_robranch_super(dir->i_sb, bindex);
2493 + if (err) {
2494 + lower_dentry = ERR_PTR(err);
2495 + goto out;
2496 + }
2497 +
2498 + old_bstart = dbstart(dentry);
2499 + old_bend = dbend(dentry);
2500 +
2501 + lower_dentry = ERR_PTR(-ENOMEM);
2502 +
2503 + /* There is no sense allocating any less than the minimum. */
2504 + nr_dentry = 1;
2505 + path = kmalloc(nr_dentry * sizeof(struct dentry *), GFP_KERNEL);
2506 + if (unlikely(!path))
2507 + goto out;
2508 +
2509 + /* assume the negative dentry of unionfs as the parent dentry */
2510 + parent_dentry = dentry;
2511 +
2512 + /*
2513 + * This loop finds the first parent that exists in the given branch.
2514 + * We start building the directory structure from there. At the end
2515 + * of the loop, the following should hold:
2516 + * - child_dentry is the first nonexistent child
2517 + * - parent_dentry is the first existent parent
2518 + * - path[0] is the = deepest child
2519 + * - path[count] is the first child to create
2520 + */
2521 + do {
2522 + child_dentry = parent_dentry;
2523 +
2524 + /* find the parent directory dentry in unionfs */
2525 + parent_dentry = dget_parent(child_dentry);
2526 +
2527 + /* find out the lower_parent_dentry in the given branch */
2528 + lower_parent_dentry =
2529 + unionfs_lower_dentry_idx(parent_dentry, bindex);
2530 +
2531 + /* grow path table */
2532 + if (count == nr_dentry) {
2533 + void *p;
2534 +
2535 + nr_dentry *= 2;
2536 + p = krealloc(path, nr_dentry * sizeof(struct dentry *),
2537 + GFP_KERNEL);
2538 + if (unlikely(!p)) {
2539 + lower_dentry = ERR_PTR(-ENOMEM);
2540 + goto out;
2541 + }
2542 + path = p;
2543 + }
2544 +
2545 + /* store the child dentry */
2546 + path[count++] = child_dentry;
2547 + } while (!lower_parent_dentry);
2548 + count--;
2549 +
2550 + sb = dentry->d_sb;
2551 +
2552 + /*
2553 + * This code goes between the begin/end labels and basically
2554 + * emulates a while(child_dentry != dentry), only cleaner and
2555 + * shorter than what would be a much longer while loop.
2556 + */
2557 +begin:
2558 + /* get lower parent dir in the current branch */
2559 + lower_parent_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex);
2560 + dput(parent_dentry);
2561 +
2562 + /* init the values to lookup */
2563 + childname = child_dentry->d_name.name;
2564 + childnamelen = child_dentry->d_name.len;
2565 +
2566 + if (child_dentry != dentry) {
2567 + /* lookup child in the underlying file system */
2568 + lower_dentry = lookup_lck_len(childname, lower_parent_dentry,
2569 + childnamelen);
2570 + if (IS_ERR(lower_dentry))
2571 + goto out;
2572 + } else {
2573 + /*
2574 + * Is the name a whiteout of the child name ? lookup the
2575 + * whiteout child in the underlying file system
2576 + */
2577 + lower_dentry = lookup_lck_len(name, lower_parent_dentry,
2578 + strlen(name));
2579 + if (IS_ERR(lower_dentry))
2580 + goto out;
2581 +
2582 + /* Replace the current dentry (if any) with the new one */
2583 + dput(unionfs_lower_dentry_idx(dentry, bindex));
2584 + unionfs_set_lower_dentry_idx(dentry, bindex,
2585 + lower_dentry);
2586 +
2587 + __cleanup_dentry(dentry, bindex, old_bstart, old_bend);
2588 + goto out;
2589 + }
2590 +
2591 + if (lower_dentry->d_inode) {
2592 + /*
2593 + * since this already exists we dput to avoid
2594 + * multiple references on the same dentry
2595 + */
2596 + dput(lower_dentry);
2597 + } else {
2598 + struct sioq_args args;
2599 +
2600 + /* it's a negative dentry, create a new dir */
2601 + lower_parent_dentry = lock_parent(lower_dentry);
2602 +
2603 + args.mkdir.parent = lower_parent_dentry->d_inode;
2604 + args.mkdir.dentry = lower_dentry;
2605 + args.mkdir.mode = child_dentry->d_inode->i_mode;
2606 +
2607 + run_sioq(__unionfs_mkdir, &args);
2608 + err = args.err;
2609 +
2610 + if (!err)
2611 + err = copyup_permissions(dir->i_sb, child_dentry,
2612 + lower_dentry);
2613 + unlock_dir(lower_parent_dentry);
2614 + if (err) {
2615 + dput(lower_dentry);
2616 + lower_dentry = ERR_PTR(err);
2617 + goto out;
2618 + }
2619 +
2620 + }
2621 +
2622 + __set_inode(child_dentry, lower_dentry, bindex);
2623 + __set_dentry(child_dentry, lower_dentry, bindex);
2624 + /*
2625 + * update times of this dentry, but also the parent, because if
2626 + * we changed, the parent may have changed too.
2627 + */
2628 + fsstack_copy_attr_times(parent_dentry->d_inode,
2629 + lower_parent_dentry->d_inode);
2630 + unionfs_copy_attr_times(child_dentry->d_inode);
2631 +
2632 + parent_dentry = child_dentry;
2633 + child_dentry = path[--count];
2634 + goto begin;
2635 +out:
2636 + /* cleanup any leftover locks from the do/while loop above */
2637 + if (IS_ERR(lower_dentry))
2638 + while (count)
2639 + dput(path[count--]);
2640 + kfree(path);
2641 + return lower_dentry;
2642 +}
2643 +
2644 +/*
2645 + * Post-copyup helper to ensure we have valid mnts: set lower mnt of
2646 + * dentry+parents to the first parent node that has an mnt.
2647 + */
2648 +void unionfs_postcopyup_setmnt(struct dentry *dentry)
2649 +{
2650 + struct dentry *parent, *hasone;
2651 + int bindex = dbstart(dentry);
2652 +
2653 + if (unionfs_lower_mnt_idx(dentry, bindex))
2654 + return;
2655 + hasone = dentry->d_parent;
2656 + /* this loop should stop at root dentry */
2657 + while (!unionfs_lower_mnt_idx(hasone, bindex))
2658 + hasone = hasone->d_parent;
2659 + parent = dentry;
2660 + while (!unionfs_lower_mnt_idx(parent, bindex)) {
2661 + unionfs_set_lower_mnt_idx(parent, bindex,
2662 + unionfs_mntget(hasone, bindex));
2663 + parent = parent->d_parent;
2664 + }
2665 +}
2666 +
2667 +/*
2668 + * Post-copyup helper to release all non-directory source objects of a
2669 + * copied-up file. Regular files should have only one lower object.
2670 + */
2671 +void unionfs_postcopyup_release(struct dentry *dentry)
2672 +{
2673 + int bstart, bend;
2674 +
2675 + BUG_ON(S_ISDIR(dentry->d_inode->i_mode));
2676 + bstart = dbstart(dentry);
2677 + bend = dbend(dentry);
2678 +
2679 + path_put_lowers(dentry, bstart + 1, bend, false);
2680 + iput_lowers(dentry->d_inode, bstart + 1, bend, false);
2681 +
2682 + dbend(dentry) = bstart;
2683 + ibend(dentry->d_inode) = ibstart(dentry->d_inode) = bstart;
2684 +}
2685 diff --git a/fs/unionfs/debug.c b/fs/unionfs/debug.c
2686 new file mode 100644
2687 index 0000000..3fd641a
2688 --- /dev/null
2689 +++ b/fs/unionfs/debug.c
2690 @@ -0,0 +1,533 @@
2691 +/*
2692 + * Copyright (c) 2003-2009 Erez Zadok
2693 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
2694 + * Copyright (c) 2003-2009 Stony Brook University
2695 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
2696 + *
2697 + * This program is free software; you can redistribute it and/or modify
2698 + * it under the terms of the GNU General Public License version 2 as
2699 + * published by the Free Software Foundation.
2700 + */
2701 +
2702 +#include "union.h"
2703 +
2704 +/*
2705 + * Helper debugging functions for maintainers (and for users to report back
2706 + * useful information back to maintainers)
2707 + */
2708 +
2709 +/* it's always useful to know what part of the code called us */
2710 +#define PRINT_CALLER(fname, fxn, line) \
2711 + do { \
2712 + if (!printed_caller) { \
2713 + pr_debug("PC:%s:%s:%d\n", (fname), (fxn), (line)); \
2714 + printed_caller = 1; \
2715 + } \
2716 + } while (0)
2717 +
2718 +/*
2719 + * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on
2720 + * the fan-out of various Unionfs objects. We check that no lower objects
2721 + * exist outside the start/end branch range; that all objects within are
2722 + * non-NULL (with some allowed exceptions); that for every lower file
2723 + * there's a lower dentry+inode; that the start/end ranges match for all
2724 + * corresponding lower objects; that open files/symlinks have only one lower
2725 + * objects, but directories can have several; and more.
2726 + */
2727 +void __unionfs_check_inode(const struct inode *inode,
2728 + const char *fname, const char *fxn, int line)
2729 +{
2730 + int bindex;
2731 + int istart, iend;
2732 + struct inode *lower_inode;
2733 + struct super_block *sb;
2734 + int printed_caller = 0;
2735 + void *poison_ptr;
2736 +
2737 + /* for inodes now */
2738 + BUG_ON(!inode);
2739 + sb = inode->i_sb;
2740 + istart = ibstart(inode);
2741 + iend = ibend(inode);
2742 + /* don't check inode if no lower branches */
2743 + if (istart < 0 && iend < 0)
2744 + return;
2745 + if (unlikely(istart > iend)) {
2746 + PRINT_CALLER(fname, fxn, line);
2747 + pr_debug(" Ci0: inode=%p istart/end=%d:%d\n",
2748 + inode, istart, iend);
2749 + }
2750 + if (unlikely((istart == -1 && iend != -1) ||
2751 + (istart != -1 && iend == -1))) {
2752 + PRINT_CALLER(fname, fxn, line);
2753 + pr_debug(" Ci1: inode=%p istart/end=%d:%d\n",
2754 + inode, istart, iend);
2755 + }
2756 + if (!S_ISDIR(inode->i_mode)) {
2757 + if (unlikely(iend != istart)) {
2758 + PRINT_CALLER(fname, fxn, line);
2759 + pr_debug(" Ci2: inode=%p istart=%d iend=%d\n",
2760 + inode, istart, iend);
2761 + }
2762 + }
2763 +
2764 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2765 + if (unlikely(!UNIONFS_I(inode))) {
2766 + PRINT_CALLER(fname, fxn, line);
2767 + pr_debug(" Ci3: no inode_info %p\n", inode);
2768 + return;
2769 + }
2770 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
2771 + PRINT_CALLER(fname, fxn, line);
2772 + pr_debug(" Ci4: no lower_inodes %p\n", inode);
2773 + return;
2774 + }
2775 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2776 + if (lower_inode) {
2777 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2778 + if (unlikely(bindex < istart || bindex > iend)) {
2779 + PRINT_CALLER(fname, fxn, line);
2780 + pr_debug(" Ci5: inode/linode=%p:%p bindex=%d "
2781 + "istart/end=%d:%d\n", inode,
2782 + lower_inode, bindex, istart, iend);
2783 + } else if (unlikely(lower_inode == poison_ptr)) {
2784 + /* freed inode! */
2785 + PRINT_CALLER(fname, fxn, line);
2786 + pr_debug(" Ci6: inode/linode=%p:%p bindex=%d "
2787 + "istart/end=%d:%d\n", inode,
2788 + lower_inode, bindex, istart, iend);
2789 + }
2790 + continue;
2791 + }
2792 + /* if we get here, then lower_inode == NULL */
2793 + if (bindex < istart || bindex > iend)
2794 + continue;
2795 + /*
2796 + * directories can have NULL lower inodes in b/t start/end,
2797 + * but NOT if at the start/end range.
2798 + */
2799 + if (unlikely(S_ISDIR(inode->i_mode) &&
2800 + bindex > istart && bindex < iend))
2801 + continue;
2802 + PRINT_CALLER(fname, fxn, line);
2803 + pr_debug(" Ci7: inode/linode=%p:%p "
2804 + "bindex=%d istart/end=%d:%d\n",
2805 + inode, lower_inode, bindex, istart, iend);
2806 + }
2807 +}
2808 +
2809 +void __unionfs_check_dentry(const struct dentry *dentry,
2810 + const char *fname, const char *fxn, int line)
2811 +{
2812 + int bindex;
2813 + int dstart, dend, istart, iend;
2814 + struct dentry *lower_dentry;
2815 + struct inode *inode, *lower_inode;
2816 + struct super_block *sb;
2817 + struct vfsmount *lower_mnt;
2818 + int printed_caller = 0;
2819 + void *poison_ptr;
2820 +
2821 + BUG_ON(!dentry);
2822 + sb = dentry->d_sb;
2823 + inode = dentry->d_inode;
2824 + dstart = dbstart(dentry);
2825 + dend = dbend(dentry);
2826 + /* don't check dentry/mnt if no lower branches */
2827 + if (dstart < 0 && dend < 0)
2828 + goto check_inode;
2829 + BUG_ON(dstart > dend);
2830 +
2831 + if (unlikely((dstart == -1 && dend != -1) ||
2832 + (dstart != -1 && dend == -1))) {
2833 + PRINT_CALLER(fname, fxn, line);
2834 + pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n",
2835 + dentry, dstart, dend);
2836 + }
2837 + /*
2838 + * check for NULL dentries inside the start/end range, or
2839 + * non-NULL dentries outside the start/end range.
2840 + */
2841 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2842 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
2843 + if (lower_dentry) {
2844 + if (unlikely(bindex < dstart || bindex > dend)) {
2845 + PRINT_CALLER(fname, fxn, line);
2846 + pr_debug(" CD1: dentry/lower=%p:%p(%p) "
2847 + "bindex=%d dstart/end=%d:%d\n",
2848 + dentry, lower_dentry,
2849 + (lower_dentry ? lower_dentry->d_inode :
2850 + (void *) -1L),
2851 + bindex, dstart, dend);
2852 + }
2853 + } else { /* lower_dentry == NULL */
2854 + if (bindex < dstart || bindex > dend)
2855 + continue;
2856 + /*
2857 + * Directories can have NULL lower inodes in b/t
2858 + * start/end, but NOT if at the start/end range.
2859 + * Ignore this rule, however, if this is a NULL
2860 + * dentry or a deleted dentry.
2861 + */
2862 + if (unlikely(!d_deleted((struct dentry *) dentry) &&
2863 + inode &&
2864 + !(inode && S_ISDIR(inode->i_mode) &&
2865 + bindex > dstart && bindex < dend))) {
2866 + PRINT_CALLER(fname, fxn, line);
2867 + pr_debug(" CD2: dentry/lower=%p:%p(%p) "
2868 + "bindex=%d dstart/end=%d:%d\n",
2869 + dentry, lower_dentry,
2870 + (lower_dentry ?
2871 + lower_dentry->d_inode :
2872 + (void *) -1L),
2873 + bindex, dstart, dend);
2874 + }
2875 + }
2876 + }
2877 +
2878 + /* check for vfsmounts same as for dentries */
2879 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2880 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2881 + if (lower_mnt) {
2882 + if (unlikely(bindex < dstart || bindex > dend)) {
2883 + PRINT_CALLER(fname, fxn, line);
2884 + pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d "
2885 + "dstart/end=%d:%d\n", dentry,
2886 + lower_mnt, bindex, dstart, dend);
2887 + }
2888 + } else { /* lower_mnt == NULL */
2889 + if (bindex < dstart || bindex > dend)
2890 + continue;
2891 + /*
2892 + * Directories can have NULL lower inodes in b/t
2893 + * start/end, but NOT if at the start/end range.
2894 + * Ignore this rule, however, if this is a NULL
2895 + * dentry.
2896 + */
2897 + if (unlikely(inode &&
2898 + !(inode && S_ISDIR(inode->i_mode) &&
2899 + bindex > dstart && bindex < dend))) {
2900 + PRINT_CALLER(fname, fxn, line);
2901 + pr_debug(" CM1: dentry/lmnt=%p:%p "
2902 + "bindex=%d dstart/end=%d:%d\n",
2903 + dentry, lower_mnt, bindex,
2904 + dstart, dend);
2905 + }
2906 + }
2907 + }
2908 +
2909 +check_inode:
2910 + /* for inodes now */
2911 + if (!inode)
2912 + return;
2913 + istart = ibstart(inode);
2914 + iend = ibend(inode);
2915 + /* don't check inode if no lower branches */
2916 + if (istart < 0 && iend < 0)
2917 + return;
2918 + BUG_ON(istart > iend);
2919 + if (unlikely((istart == -1 && iend != -1) ||
2920 + (istart != -1 && iend == -1))) {
2921 + PRINT_CALLER(fname, fxn, line);
2922 + pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n",
2923 + dentry, inode, istart, iend);
2924 + }
2925 + if (unlikely(istart != dstart)) {
2926 + PRINT_CALLER(fname, fxn, line);
2927 + pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n",
2928 + dentry, inode, istart, dstart);
2929 + }
2930 + if (unlikely(iend != dend)) {
2931 + PRINT_CALLER(fname, fxn, line);
2932 + pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n",
2933 + dentry, inode, iend, dend);
2934 + }
2935 +
2936 + if (!S_ISDIR(inode->i_mode)) {
2937 + if (unlikely(dend != dstart)) {
2938 + PRINT_CALLER(fname, fxn, line);
2939 + pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n",
2940 + dentry, inode, dstart, dend);
2941 + }
2942 + if (unlikely(iend != istart)) {
2943 + PRINT_CALLER(fname, fxn, line);
2944 + pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n",
2945 + dentry, inode, istart, iend);
2946 + }
2947 + }
2948 +
2949 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
2950 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2951 + if (lower_inode) {
2952 + memset(&poison_ptr, POISON_INUSE, sizeof(void *));
2953 + if (unlikely(bindex < istart || bindex > iend)) {
2954 + PRINT_CALLER(fname, fxn, line);
2955 + pr_debug(" CI5: dentry/linode=%p:%p bindex=%d "
2956 + "istart/end=%d:%d\n", dentry,
2957 + lower_inode, bindex, istart, iend);
2958 + } else if (unlikely(lower_inode == poison_ptr)) {
2959 + /* freed inode! */
2960 + PRINT_CALLER(fname, fxn, line);
2961 + pr_debug(" CI6: dentry/linode=%p:%p bindex=%d "
2962 + "istart/end=%d:%d\n", dentry,
2963 + lower_inode, bindex, istart, iend);
2964 + }
2965 + continue;
2966 + }
2967 + /* if we get here, then lower_inode == NULL */
2968 + if (bindex < istart || bindex > iend)
2969 + continue;
2970 + /*
2971 + * directories can have NULL lower inodes in b/t start/end,
2972 + * but NOT if at the start/end range.
2973 + */
2974 + if (unlikely(S_ISDIR(inode->i_mode) &&
2975 + bindex > istart && bindex < iend))
2976 + continue;
2977 + PRINT_CALLER(fname, fxn, line);
2978 + pr_debug(" CI7: dentry/linode=%p:%p "
2979 + "bindex=%d istart/end=%d:%d\n",
2980 + dentry, lower_inode, bindex, istart, iend);
2981 + }
2982 +
2983 + /*
2984 + * If it's a directory, then intermediate objects b/t start/end can
2985 + * be NULL. But, check that all three are NULL: lower dentry, mnt,
2986 + * and inode.
2987 + */
2988 + if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode))
2989 + for (bindex = dstart+1; bindex < dend; bindex++) {
2990 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
2991 + lower_dentry = unionfs_lower_dentry_idx(dentry,
2992 + bindex);
2993 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
2994 + if (unlikely(!((lower_inode && lower_dentry &&
2995 + lower_mnt) ||
2996 + (!lower_inode &&
2997 + !lower_dentry && !lower_mnt)))) {
2998 + PRINT_CALLER(fname, fxn, line);
2999 + pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p "
3000 + "bindex=%d dstart/end=%d:%d\n",
3001 + lower_mnt, lower_dentry, lower_inode,
3002 + bindex, dstart, dend);
3003 + }
3004 + }
3005 + /* check if lower inode is newer than upper one (it shouldn't) */
3006 + if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) {
3007 + PRINT_CALLER(fname, fxn, line);
3008 + for (bindex = ibstart(inode); bindex <= ibend(inode);
3009 + bindex++) {
3010 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3011 + if (unlikely(!lower_inode))
3012 + continue;
3013 + pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu "
3014 + "ctime/lctime=%lu.%lu/%lu.%lu\n",
3015 + bindex,
3016 + inode->i_mtime.tv_sec,
3017 + inode->i_mtime.tv_nsec,
3018 + lower_inode->i_mtime.tv_sec,
3019 + lower_inode->i_mtime.tv_nsec,
3020 + inode->i_ctime.tv_sec,
3021 + inode->i_ctime.tv_nsec,
3022 + lower_inode->i_ctime.tv_sec,
3023 + lower_inode->i_ctime.tv_nsec);
3024 + }
3025 + }
3026 +}
3027 +
3028 +void __unionfs_check_file(const struct file *file,
3029 + const char *fname, const char *fxn, int line)
3030 +{
3031 + int bindex;
3032 + int dstart, dend, fstart, fend;
3033 + struct dentry *dentry;
3034 + struct file *lower_file;
3035 + struct inode *inode;
3036 + struct super_block *sb;
3037 + int printed_caller = 0;
3038 +
3039 + BUG_ON(!file);
3040 + dentry = file->f_path.dentry;
3041 + sb = dentry->d_sb;
3042 + dstart = dbstart(dentry);
3043 + dend = dbend(dentry);
3044 + BUG_ON(dstart > dend);
3045 + fstart = fbstart(file);
3046 + fend = fbend(file);
3047 + BUG_ON(fstart > fend);
3048 +
3049 + if (unlikely((fstart == -1 && fend != -1) ||
3050 + (fstart != -1 && fend == -1))) {
3051 + PRINT_CALLER(fname, fxn, line);
3052 + pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n",
3053 + file, dentry, fstart, fend);
3054 + }
3055 + if (unlikely(fstart != dstart)) {
3056 + PRINT_CALLER(fname, fxn, line);
3057 + pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n",
3058 + file, dentry, fstart, dstart);
3059 + }
3060 + if (unlikely(fend != dend)) {
3061 + PRINT_CALLER(fname, fxn, line);
3062 + pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n",
3063 + file, dentry, fend, dend);
3064 + }
3065 + inode = dentry->d_inode;
3066 + if (!S_ISDIR(inode->i_mode)) {
3067 + if (unlikely(fend != fstart)) {
3068 + PRINT_CALLER(fname, fxn, line);
3069 + pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n",
3070 + file, inode, fstart, fend);
3071 + }
3072 + if (unlikely(dend != dstart)) {
3073 + PRINT_CALLER(fname, fxn, line);
3074 + pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n",
3075 + file, dentry, dstart, dend);
3076 + }
3077 + }
3078 +
3079 + /*
3080 + * check for NULL dentries inside the start/end range, or
3081 + * non-NULL dentries outside the start/end range.
3082 + */
3083 + for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) {
3084 + lower_file = unionfs_lower_file_idx(file, bindex);
3085 + if (lower_file) {
3086 + if (unlikely(bindex < fstart || bindex > fend)) {
3087 + PRINT_CALLER(fname, fxn, line);
3088 + pr_debug(" CF5: file/lower=%p:%p bindex=%d "
3089 + "fstart/end=%d:%d\n", file,
3090 + lower_file, bindex, fstart, fend);
3091 + }
3092 + } else { /* lower_file == NULL */
3093 + if (bindex >= fstart && bindex <= fend) {
3094 + /*
3095 + * directories can have NULL lower inodes in
3096 + * b/t start/end, but NOT if at the
3097 + * start/end range.
3098 + */
3099 + if (unlikely(!(S_ISDIR(inode->i_mode) &&
3100 + bindex > fstart &&
3101 + bindex < fend))) {
3102 + PRINT_CALLER(fname, fxn, line);
3103 + pr_debug(" CF6: file/lower=%p:%p "
3104 + "bindex=%d fstart/end=%d:%d\n",
3105 + file, lower_file, bindex,
3106 + fstart, fend);
3107 + }
3108 + }
3109 + }
3110 + }
3111 +
3112 + __unionfs_check_dentry(dentry, fname, fxn, line);
3113 +}
3114 +
3115 +void __unionfs_check_nd(const struct nameidata *nd,
3116 + const char *fname, const char *fxn, int line)
3117 +{
3118 + struct file *file;
3119 + int printed_caller = 0;
3120 +
3121 + if (unlikely(!nd))
3122 + return;
3123 + if (nd->flags & LOOKUP_OPEN) {
3124 + file = nd->intent.open.file;
3125 + if (unlikely(file->f_path.dentry &&
3126 + strcmp(file->f_path.dentry->d_sb->s_type->name,
3127 + UNIONFS_NAME))) {
3128 + PRINT_CALLER(fname, fxn, line);
3129 + pr_debug(" CND1: lower_file of type %s\n",
3130 + file->f_path.dentry->d_sb->s_type->name);
3131 + BUG();
3132 + }
3133 + }
3134 +}
3135 +
3136 +/* useful to track vfsmount leaks that could cause EBUSY on unmount */
3137 +void __show_branch_counts(const struct super_block *sb,
3138 + const char *file, const char *fxn, int line)
3139 +{
3140 + int i;
3141 + struct vfsmount *mnt;
3142 +
3143 + pr_debug("BC:");
3144 + for (i = 0; i < sbmax(sb); i++) {
3145 + if (likely(sb->s_root))
3146 + mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt;
3147 + else
3148 + mnt = NULL;
3149 + printk(KERN_CONT "%d:",
3150 + (mnt ? atomic_read(&mnt->mnt_count) : -99));
3151 + }
3152 + printk(KERN_CONT "%s:%s:%d\n", file, fxn, line);
3153 +}
3154 +
3155 +void __show_inode_times(const struct inode *inode,
3156 + const char *file, const char *fxn, int line)
3157 +{
3158 + struct inode *lower_inode;
3159 + int bindex;
3160 +
3161 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3162 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3163 + if (unlikely(!lower_inode))
3164 + continue;
3165 + pr_debug("IT(%lu:%d): %s:%s:%d "
3166 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3167 + inode->i_ino, bindex,
3168 + file, fxn, line,
3169 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3170 + lower_inode->i_mtime.tv_sec,
3171 + lower_inode->i_mtime.tv_nsec,
3172 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3173 + lower_inode->i_ctime.tv_sec,
3174 + lower_inode->i_ctime.tv_nsec);
3175 + }
3176 +}
3177 +
3178 +void __show_dinode_times(const struct dentry *dentry,
3179 + const char *file, const char *fxn, int line)
3180 +{
3181 + struct inode *inode = dentry->d_inode;
3182 + struct inode *lower_inode;
3183 + int bindex;
3184 +
3185 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3186 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3187 + if (!lower_inode)
3188 + continue;
3189 + pr_debug("DT(%s:%lu:%d): %s:%s:%d "
3190 + "um=%lu/%lu lm=%lu/%lu uc=%lu/%lu lc=%lu/%lu\n",
3191 + dentry->d_name.name, inode->i_ino, bindex,
3192 + file, fxn, line,
3193 + inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
3194 + lower_inode->i_mtime.tv_sec,
3195 + lower_inode->i_mtime.tv_nsec,
3196 + inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
3197 + lower_inode->i_ctime.tv_sec,
3198 + lower_inode->i_ctime.tv_nsec);
3199 + }
3200 +}
3201 +
3202 +void __show_inode_counts(const struct inode *inode,
3203 + const char *file, const char *fxn, int line)
3204 +{
3205 + struct inode *lower_inode;
3206 + int bindex;
3207 +
3208 + if (unlikely(!inode)) {
3209 + pr_debug("SiC: Null inode\n");
3210 + return;
3211 + }
3212 + for (bindex = sbstart(inode->i_sb); bindex <= sbend(inode->i_sb);
3213 + bindex++) {
3214 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3215 + if (unlikely(!lower_inode))
3216 + continue;
3217 + pr_debug("SIC(%lu:%d:%d): lc=%d %s:%s:%d\n",
3218 + inode->i_ino, bindex,
3219 + atomic_read(&(inode)->i_count),
3220 + atomic_read(&(lower_inode)->i_count),
3221 + file, fxn, line);
3222 + }
3223 +}
3224 diff --git a/fs/unionfs/dentry.c b/fs/unionfs/dentry.c
3225 new file mode 100644
3226 index 0000000..85b5d3c
3227 --- /dev/null
3228 +++ b/fs/unionfs/dentry.c
3229 @@ -0,0 +1,397 @@
3230 +/*
3231 + * Copyright (c) 2003-2009 Erez Zadok
3232 + * Copyright (c) 2003-2006 Charles P. Wright
3233 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3234 + * Copyright (c) 2005-2006 Junjiro Okajima
3235 + * Copyright (c) 2005 Arun M. Krishnakumar
3236 + * Copyright (c) 2004-2006 David P. Quigley
3237 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3238 + * Copyright (c) 2003 Puja Gupta
3239 + * Copyright (c) 2003 Harikesavan Krishnan
3240 + * Copyright (c) 2003-2009 Stony Brook University
3241 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3242 + *
3243 + * This program is free software; you can redistribute it and/or modify
3244 + * it under the terms of the GNU General Public License version 2 as
3245 + * published by the Free Software Foundation.
3246 + */
3247 +
3248 +#include "union.h"
3249 +
3250 +bool is_negative_lower(const struct dentry *dentry)
3251 +{
3252 + int bindex;
3253 + struct dentry *lower_dentry;
3254 +
3255 + BUG_ON(!dentry);
3256 + /* cache coherency: check if file was deleted on lower branch */
3257 + if (dbstart(dentry) < 0)
3258 + return true;
3259 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
3260 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3261 + /* unhashed (i.e., unlinked) lower dentries don't count */
3262 + if (lower_dentry && lower_dentry->d_inode &&
3263 + !d_deleted(lower_dentry) &&
3264 + !(lower_dentry->d_flags & DCACHE_NFSFS_RENAMED))
3265 + return false;
3266 + }
3267 + return true;
3268 +}
3269 +
3270 +static inline void __dput_lowers(struct dentry *dentry, int start, int end)
3271 +{
3272 + struct dentry *lower_dentry;
3273 + int bindex;
3274 +
3275 + if (start < 0)
3276 + return;
3277 + for (bindex = start; bindex <= end; bindex++) {
3278 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3279 + if (!lower_dentry)
3280 + continue;
3281 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
3282 + dput(lower_dentry);
3283 + }
3284 +}
3285 +
3286 +/*
3287 + * Purge and invalidate as many data pages of a unionfs inode. This is
3288 + * called when the lower inode has changed, and we want to force processes
3289 + * to re-get the new data.
3290 + */
3291 +static inline void purge_inode_data(struct inode *inode)
3292 +{
3293 + /* remove all non-private mappings */
3294 + unmap_mapping_range(inode->i_mapping, 0, 0, 0);
3295 + /* invalidate as many pages as possible */
3296 + invalidate_mapping_pages(inode->i_mapping, 0, -1);
3297 + /*
3298 + * Don't try to truncate_inode_pages here, because this could lead
3299 + * to a deadlock between some of address_space ops and dentry
3300 + * revalidation: the address space op is invoked with a lock on our
3301 + * own page, and truncate_inode_pages will block on locked pages.
3302 + */
3303 +}
3304 +
3305 +/*
3306 + * Revalidate a single file/symlink/special dentry. Assume that info nodes
3307 + * of the @dentry and its @parent are locked. Assume parent is valid,
3308 + * otherwise return false (and let's hope the VFS will try to re-lookup this
3309 + * dentry). Returns true if valid, false otherwise.
3310 + */
3311 +bool __unionfs_d_revalidate(struct dentry *dentry, struct dentry *parent,
3312 + bool willwrite)
3313 +{
3314 + bool valid = true; /* default is valid */
3315 + struct dentry *lower_dentry;
3316 + struct dentry *result;
3317 + int bindex, bstart, bend;
3318 + int sbgen, dgen, pdgen;
3319 + int positive = 0;
3320 + int interpose_flag;
3321 +
3322 + verify_locked(dentry);
3323 + verify_locked(parent);
3324 +
3325 + /* if the dentry is unhashed, do NOT revalidate */
3326 + if (d_deleted(dentry))
3327 + goto out;
3328 +
3329 + dgen = atomic_read(&UNIONFS_D(dentry)->generation);
3330 +
3331 + if (is_newer_lower(dentry)) {
3332 + /* root dentry is always valid */
3333 + if (IS_ROOT(dentry)) {
3334 + unionfs_copy_attr_times(dentry->d_inode);
3335 + } else {
3336 + /*
3337 + * reset generation number to zero, guaranteed to be
3338 + * "old"
3339 + */
3340 + dgen = 0;
3341 + atomic_set(&UNIONFS_D(dentry)->generation, dgen);
3342 + }
3343 + if (!willwrite)
3344 + purge_inode_data(dentry->d_inode);
3345 + }
3346 +
3347 + sbgen = atomic_read(&UNIONFS_SB(dentry->d_sb)->generation);
3348 +
3349 + BUG_ON(dbstart(dentry) == -1);
3350 + if (dentry->d_inode)
3351 + positive = 1;
3352 +
3353 + /* if our dentry is valid, then validate all lower ones */
3354 + if (sbgen == dgen)
3355 + goto validate_lowers;
3356 +
3357 + /* The root entry should always be valid */
3358 + BUG_ON(IS_ROOT(dentry));
3359 +
3360 + /* We can't work correctly if our parent isn't valid. */
3361 + pdgen = atomic_read(&UNIONFS_D(parent)->generation);
3362 +
3363 + /* Free the pointers for our inodes and this dentry. */
3364 + path_put_lowers_all(dentry, false);
3365 +
3366 + interpose_flag = INTERPOSE_REVAL_NEG;
3367 + if (positive) {
3368 + interpose_flag = INTERPOSE_REVAL;
3369 + iput_lowers_all(dentry->d_inode, true);
3370 + }
3371 +
3372 + if (realloc_dentry_private_data(dentry) != 0) {
3373 + valid = false;
3374 + goto out;
3375 + }
3376 +
3377 + result = unionfs_lookup_full(dentry, parent, interpose_flag);
3378 + if (result) {
3379 + if (IS_ERR(result)) {
3380 + valid = false;
3381 + goto out;
3382 + }
3383 + /*
3384 + * current unionfs_lookup_backend() doesn't return
3385 + * a valid dentry
3386 + */
3387 + dput(dentry);
3388 + dentry = result;
3389 + }
3390 +
3391 + if (unlikely(positive && is_negative_lower(dentry))) {
3392 + /* call make_bad_inode here ? */
3393 + d_drop(dentry);
3394 + valid = false;
3395 + goto out;
3396 + }
3397 +
3398 + /*
3399 + * if we got here then we have revalidated our dentry and all lower
3400 + * ones, so we can return safely.
3401 + */
3402 + if (!valid) /* lower dentry revalidation failed */
3403 + goto out;
3404 +
3405 + /*
3406 + * If the parent's gen no. matches the superblock's gen no., then
3407 + * we can update our denty's gen no. If they didn't match, then it
3408 + * was OK to revalidate this dentry with a stale parent, but we'll
3409 + * purposely not update our dentry's gen no. (so it can be redone);
3410 + * and, we'll mark our parent dentry as invalid so it'll force it
3411 + * (and our dentry) to be revalidated.
3412 + */
3413 + if (pdgen == sbgen)
3414 + atomic_set(&UNIONFS_D(dentry)->generation, sbgen);
3415 + goto out;
3416 +
3417 +validate_lowers:
3418 +
3419 + /* The revalidation must occur across all branches */
3420 + bstart = dbstart(dentry);
3421 + bend = dbend(dentry);
3422 + BUG_ON(bstart == -1);
3423 + for (bindex = bstart; bindex <= bend; bindex++) {
3424 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
3425 + if (!lower_dentry || !lower_dentry->d_op
3426 + || !lower_dentry->d_op->d_revalidate)
3427 + continue;
3428 + /*
3429 + * Don't pass nameidata to lower file system, because we
3430 + * don't want an arbitrary lower file being opened or
3431 + * returned to us: it may be useless to us because of the
3432 + * fanout nature of unionfs (cf. file/directory open-file
3433 + * invariants). We will open lower files as and when needed
3434 + * later on.
3435 + */
3436 + if (!lower_dentry->d_op->d_revalidate(lower_dentry, NULL))
3437 + valid = false;
3438 + }
3439 +
3440 + if (!dentry->d_inode ||
3441 + ibstart(dentry->d_inode) < 0 ||
3442 + ibend(dentry->d_inode) < 0) {
3443 + valid = false;
3444 + goto out;
3445 + }
3446 +
3447 + if (valid) {
3448 + /*
3449 + * If we get here, and we copy the meta-data from the lower
3450 + * inode to our inode, then it is vital that we have already
3451 + * purged all unionfs-level file data. We do that in the
3452 + * caller (__unionfs_d_revalidate) by calling
3453 + * purge_inode_data.
3454 + */
3455 + unionfs_copy_attr_all(dentry->d_inode,
3456 + unionfs_lower_inode(dentry->d_inode));
3457 + fsstack_copy_inode_size(dentry->d_inode,
3458 + unionfs_lower_inode(dentry->d_inode));
3459 + }
3460 +
3461 +out:
3462 + return valid;
3463 +}
3464 +
3465 +/*
3466 + * Determine if the lower inode objects have changed from below the unionfs
3467 + * inode. Return true if changed, false otherwise.
3468 + *
3469 + * We check if the mtime or ctime have changed. However, the inode times
3470 + * can be changed by anyone without much protection, including
3471 + * asynchronously. This can sometimes cause unionfs to find that the lower
3472 + * file system doesn't change its inode times quick enough, resulting in a
3473 + * false positive indication (which is harmless, it just makes unionfs do
3474 + * extra work in re-validating the objects). To minimize the chances of
3475 + * these situations, we still consider such small time changes valid, but we
3476 + * don't print debugging messages unless the time changes are greater than
3477 + * UNIONFS_MIN_CC_TIME (which defaults to 3 seconds, as with NFS's acregmin)
3478 + * because significant changes are more likely due to users manually
3479 + * touching lower files.
3480 + */
3481 +bool is_newer_lower(const struct dentry *dentry)
3482 +{
3483 + int bindex;
3484 + struct inode *inode;
3485 + struct inode *lower_inode;
3486 +
3487 + /* ignore if we're called on semi-initialized dentries/inodes */
3488 + if (!dentry || !UNIONFS_D(dentry))
3489 + return false;
3490 + inode = dentry->d_inode;
3491 + if (!inode || !UNIONFS_I(inode)->lower_inodes ||
3492 + ibstart(inode) < 0 || ibend(inode) < 0)
3493 + return false;
3494 +
3495 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
3496 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
3497 + if (!lower_inode)
3498 + continue;
3499 +
3500 + /* check if mtime/ctime have changed */
3501 + if (unlikely(timespec_compare(&inode->i_mtime,
3502 + &lower_inode->i_mtime) < 0)) {
3503 + if ((lower_inode->i_mtime.tv_sec -
3504 + inode->i_mtime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3505 + pr_info("unionfs: new lower inode mtime "
3506 + "(bindex=%d, name=%s)\n", bindex,
3507 + dentry->d_name.name);
3508 + show_dinode_times(dentry);
3509 + }
3510 + return true;
3511 + }
3512 + if (unlikely(timespec_compare(&inode->i_ctime,
3513 + &lower_inode->i_ctime) < 0)) {
3514 + if ((lower_inode->i_ctime.tv_sec -
3515 + inode->i_ctime.tv_sec) > UNIONFS_MIN_CC_TIME) {
3516 + pr_info("unionfs: new lower inode ctime "
3517 + "(bindex=%d, name=%s)\n", bindex,
3518 + dentry->d_name.name);
3519 + show_dinode_times(dentry);
3520 + }
3521 + return true;
3522 + }
3523 + }
3524 +
3525 + /*
3526 + * Last check: if this is a positive dentry, but somehow all lower
3527 + * dentries are negative or unhashed, then this dentry needs to be
3528 + * revalidated, because someone probably deleted the objects from
3529 + * the lower branches directly.
3530 + */
3531 + if (is_negative_lower(dentry))
3532 + return true;
3533 +
3534 + return false; /* default: lower is not newer */
3535 +}
3536 +
3537 +static int unionfs_d_revalidate(struct dentry *dentry,
3538 + struct nameidata *nd_unused)
3539 +{
3540 + bool valid = true;
3541 + int err = 1; /* 1 means valid for the VFS */
3542 + struct dentry *parent;
3543 +
3544 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3545 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3546 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3547 +
3548 + valid = __unionfs_d_revalidate(dentry, parent, false);
3549 + if (valid) {
3550 + unionfs_postcopyup_setmnt(dentry);
3551 + unionfs_check_dentry(dentry);
3552 + } else {
3553 + d_drop(dentry);
3554 + err = valid;
3555 + }
3556 + unionfs_unlock_dentry(dentry);
3557 + unionfs_unlock_parent(dentry, parent);
3558 + unionfs_read_unlock(dentry->d_sb);
3559 +
3560 + return err;
3561 +}
3562 +
3563 +static void unionfs_d_release(struct dentry *dentry)
3564 +{
3565 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3566 + if (unlikely(!UNIONFS_D(dentry)))
3567 + goto out; /* skip if no lower branches */
3568 + /* must lock our branch configuration here */
3569 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3570 +
3571 + unionfs_check_dentry(dentry);
3572 + /* this could be a negative dentry, so check first */
3573 + if (dbstart(dentry) < 0) {
3574 + unionfs_unlock_dentry(dentry);
3575 + goto out; /* due to a (normal) failed lookup */
3576 + }
3577 +
3578 + /* Release all the lower dentries */
3579 + path_put_lowers_all(dentry, true);
3580 +
3581 + unionfs_unlock_dentry(dentry);
3582 +
3583 +out:
3584 + free_dentry_private_data(dentry);
3585 + unionfs_read_unlock(dentry->d_sb);
3586 + return;
3587 +}
3588 +
3589 +/*
3590 + * Called when we're removing the last reference to our dentry. So we
3591 + * should drop all lower references too.
3592 + */
3593 +static void unionfs_d_iput(struct dentry *dentry, struct inode *inode)
3594 +{
3595 + int rc;
3596 +
3597 + BUG_ON(!dentry);
3598 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
3599 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3600 +
3601 + if (!UNIONFS_D(dentry) || dbstart(dentry) < 0)
3602 + goto drop_lower_inodes;
3603 + path_put_lowers_all(dentry, false);
3604 +
3605 +drop_lower_inodes:
3606 + rc = atomic_read(&inode->i_count);
3607 + if (rc == 1 && inode->i_nlink == 1 && ibstart(inode) >= 0) {
3608 + /* see Documentation/filesystems/unionfs/issues.txt */
3609 + lockdep_off();
3610 + iput(unionfs_lower_inode(inode));
3611 + lockdep_on();
3612 + unionfs_set_lower_inode(inode, NULL);
3613 + /* XXX: may need to set start/end to -1? */
3614 + }
3615 +
3616 + iput(inode);
3617 +
3618 + unionfs_unlock_dentry(dentry);
3619 + unionfs_read_unlock(dentry->d_sb);
3620 +}
3621 +
3622 +struct dentry_operations unionfs_dops = {
3623 + .d_revalidate = unionfs_d_revalidate,
3624 + .d_release = unionfs_d_release,
3625 + .d_iput = unionfs_d_iput,
3626 +};
3627 diff --git a/fs/unionfs/dirfops.c b/fs/unionfs/dirfops.c
3628 new file mode 100644
3629 index 0000000..eccb9ae
3630 --- /dev/null
3631 +++ b/fs/unionfs/dirfops.c
3632 @@ -0,0 +1,302 @@
3633 +/*
3634 + * Copyright (c) 2003-2009 Erez Zadok
3635 + * Copyright (c) 2003-2006 Charles P. Wright
3636 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3637 + * Copyright (c) 2005-2006 Junjiro Okajima
3638 + * Copyright (c) 2005 Arun M. Krishnakumar
3639 + * Copyright (c) 2004-2006 David P. Quigley
3640 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3641 + * Copyright (c) 2003 Puja Gupta
3642 + * Copyright (c) 2003 Harikesavan Krishnan
3643 + * Copyright (c) 2003-2009 Stony Brook University
3644 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3645 + *
3646 + * This program is free software; you can redistribute it and/or modify
3647 + * it under the terms of the GNU General Public License version 2 as
3648 + * published by the Free Software Foundation.
3649 + */
3650 +
3651 +#include "union.h"
3652 +
3653 +/* Make sure our rdstate is playing by the rules. */
3654 +static void verify_rdstate_offset(struct unionfs_dir_state *rdstate)
3655 +{
3656 + BUG_ON(rdstate->offset >= DIREOF);
3657 + BUG_ON(rdstate->cookie >= MAXRDCOOKIE);
3658 +}
3659 +
3660 +struct unionfs_getdents_callback {
3661 + struct unionfs_dir_state *rdstate;
3662 + void *dirent;
3663 + int entries_written;
3664 + int filldir_called;
3665 + int filldir_error;
3666 + filldir_t filldir;
3667 + struct super_block *sb;
3668 +};
3669 +
3670 +/* based on generic filldir in fs/readir.c */
3671 +static int unionfs_filldir(void *dirent, const char *oname, int namelen,
3672 + loff_t offset, u64 ino, unsigned int d_type)
3673 +{
3674 + struct unionfs_getdents_callback *buf = dirent;
3675 + struct filldir_node *found = NULL;
3676 + int err = 0;
3677 + int is_whiteout;
3678 + char *name = (char *) oname;
3679 +
3680 + buf->filldir_called++;
3681 +
3682 + is_whiteout = is_whiteout_name(&name, &namelen);
3683 +
3684 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3685 +
3686 + if (found) {
3687 + /*
3688 + * If we had non-whiteout entry in dir cache, then mark it
3689 + * as a whiteout and but leave it in the dir cache.
3690 + */
3691 + if (is_whiteout && !found->whiteout)
3692 + found->whiteout = is_whiteout;
3693 + goto out;
3694 + }
3695 +
3696 + /* if 'name' isn't a whiteout, filldir it. */
3697 + if (!is_whiteout) {
3698 + off_t pos = rdstate2offset(buf->rdstate);
3699 + u64 unionfs_ino = ino;
3700 +
3701 + err = buf->filldir(buf->dirent, name, namelen, pos,
3702 + unionfs_ino, d_type);
3703 + buf->rdstate->offset++;
3704 + verify_rdstate_offset(buf->rdstate);
3705 + }
3706 + /*
3707 + * If we did fill it, stuff it in our hash, otherwise return an
3708 + * error.
3709 + */
3710 + if (err) {
3711 + buf->filldir_error = err;
3712 + goto out;
3713 + }
3714 + buf->entries_written++;
3715 + err = add_filldir_node(buf->rdstate, name, namelen,
3716 + buf->rdstate->bindex, is_whiteout);
3717 + if (err)
3718 + buf->filldir_error = err;
3719 +
3720 +out:
3721 + return err;
3722 +}
3723 +
3724 +static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
3725 +{
3726 + int err = 0;
3727 + struct file *lower_file = NULL;
3728 + struct dentry *dentry = file->f_path.dentry;
3729 + struct dentry *parent;
3730 + struct inode *inode = NULL;
3731 + struct unionfs_getdents_callback buf;
3732 + struct unionfs_dir_state *uds;
3733 + int bend;
3734 + loff_t offset;
3735 +
3736 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3737 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3738 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3739 +
3740 + err = unionfs_file_revalidate(file, parent, false);
3741 + if (unlikely(err))
3742 + goto out;
3743 +
3744 + inode = dentry->d_inode;
3745 +
3746 + uds = UNIONFS_F(file)->rdstate;
3747 + if (!uds) {
3748 + if (file->f_pos == DIREOF) {
3749 + goto out;
3750 + } else if (file->f_pos > 0) {
3751 + uds = find_rdstate(inode, file->f_pos);
3752 + if (unlikely(!uds)) {
3753 + err = -ESTALE;
3754 + goto out;
3755 + }
3756 + UNIONFS_F(file)->rdstate = uds;
3757 + } else {
3758 + init_rdstate(file);
3759 + uds = UNIONFS_F(file)->rdstate;
3760 + }
3761 + }
3762 + bend = fbend(file);
3763 +
3764 + while (uds->bindex <= bend) {
3765 + lower_file = unionfs_lower_file_idx(file, uds->bindex);
3766 + if (!lower_file) {
3767 + uds->bindex++;
3768 + uds->dirpos = 0;
3769 + continue;
3770 + }
3771 +
3772 + /* prepare callback buffer */
3773 + buf.filldir_called = 0;
3774 + buf.filldir_error = 0;
3775 + buf.entries_written = 0;
3776 + buf.dirent = dirent;
3777 + buf.filldir = filldir;
3778 + buf.rdstate = uds;
3779 + buf.sb = inode->i_sb;
3780 +
3781 + /* Read starting from where we last left off. */
3782 + offset = vfs_llseek(lower_file, uds->dirpos, SEEK_SET);
3783 + if (offset < 0) {
3784 + err = offset;
3785 + goto out;
3786 + }
3787 + err = vfs_readdir(lower_file, unionfs_filldir, &buf);
3788 +
3789 + /* Save the position for when we continue. */
3790 + offset = vfs_llseek(lower_file, 0, SEEK_CUR);
3791 + if (offset < 0) {
3792 + err = offset;
3793 + goto out;
3794 + }
3795 + uds->dirpos = offset;
3796 +
3797 + /* Copy the atime. */
3798 + fsstack_copy_attr_atime(inode,
3799 + lower_file->f_path.dentry->d_inode);
3800 +
3801 + if (err < 0)
3802 + goto out;
3803 +
3804 + if (buf.filldir_error)
3805 + break;
3806 +
3807 + if (!buf.entries_written) {
3808 + uds->bindex++;
3809 + uds->dirpos = 0;
3810 + }
3811 + }
3812 +
3813 + if (!buf.filldir_error && uds->bindex >= bend) {
3814 + /* Save the number of hash entries for next time. */
3815 + UNIONFS_I(inode)->hashsize = uds->hashentries;
3816 + free_rdstate(uds);
3817 + UNIONFS_F(file)->rdstate = NULL;
3818 + file->f_pos = DIREOF;
3819 + } else {
3820 + file->f_pos = rdstate2offset(uds);
3821 + }
3822 +
3823 +out:
3824 + if (!err)
3825 + unionfs_check_file(file);
3826 + unionfs_unlock_dentry(dentry);
3827 + unionfs_unlock_parent(dentry, parent);
3828 + unionfs_read_unlock(dentry->d_sb);
3829 + return err;
3830 +}
3831 +
3832 +/*
3833 + * This is not meant to be a generic repositioning function. If you do
3834 + * things that aren't supported, then we return EINVAL.
3835 + *
3836 + * What is allowed:
3837 + * (1) seeking to the same position that you are currently at
3838 + * This really has no effect, but returns where you are.
3839 + * (2) seeking to the beginning of the file
3840 + * This throws out all state, and lets you begin again.
3841 + */
3842 +static loff_t unionfs_dir_llseek(struct file *file, loff_t offset, int origin)
3843 +{
3844 + struct unionfs_dir_state *rdstate;
3845 + struct dentry *dentry = file->f_path.dentry;
3846 + struct dentry *parent;
3847 + loff_t err;
3848 +
3849 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
3850 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
3851 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
3852 +
3853 + err = unionfs_file_revalidate(file, parent, false);
3854 + if (unlikely(err))
3855 + goto out;
3856 +
3857 + rdstate = UNIONFS_F(file)->rdstate;
3858 +
3859 + /*
3860 + * we let users seek to their current position, but not anywhere
3861 + * else.
3862 + */
3863 + if (!offset) {
3864 + switch (origin) {
3865 + case SEEK_SET:
3866 + if (rdstate) {
3867 + free_rdstate(rdstate);
3868 + UNIONFS_F(file)->rdstate = NULL;
3869 + }
3870 + init_rdstate(file);
3871 + err = 0;
3872 + break;
3873 + case SEEK_CUR:
3874 + err = file->f_pos;
3875 + break;
3876 + case SEEK_END:
3877 + /* Unsupported, because we would break everything. */
3878 + err = -EINVAL;
3879 + break;
3880 + }
3881 + } else {
3882 + switch (origin) {
3883 + case SEEK_SET:
3884 + if (rdstate) {
3885 + if (offset == rdstate2offset(rdstate))
3886 + err = offset;
3887 + else if (file->f_pos == DIREOF)
3888 + err = DIREOF;
3889 + else
3890 + err = -EINVAL;
3891 + } else {
3892 + struct inode *inode;
3893 + inode = dentry->d_inode;
3894 + rdstate = find_rdstate(inode, offset);
3895 + if (rdstate) {
3896 + UNIONFS_F(file)->rdstate = rdstate;
3897 + err = rdstate->offset;
3898 + } else {
3899 + err = -EINVAL;
3900 + }
3901 + }
3902 + break;
3903 + case SEEK_CUR:
3904 + case SEEK_END:
3905 + /* Unsupported, because we would break everything. */
3906 + err = -EINVAL;
3907 + break;
3908 + }
3909 + }
3910 +
3911 +out:
3912 + if (!err)
3913 + unionfs_check_file(file);
3914 + unionfs_unlock_dentry(dentry);
3915 + unionfs_unlock_parent(dentry, parent);
3916 + unionfs_read_unlock(dentry->d_sb);
3917 + return err;
3918 +}
3919 +
3920 +/*
3921 + * Trimmed directory options, we shouldn't pass everything down since
3922 + * we don't want to operate on partial directories.
3923 + */
3924 +struct file_operations unionfs_dir_fops = {
3925 + .llseek = unionfs_dir_llseek,
3926 + .read = generic_read_dir,
3927 + .readdir = unionfs_readdir,
3928 + .unlocked_ioctl = unionfs_ioctl,
3929 + .open = unionfs_open,
3930 + .release = unionfs_file_release,
3931 + .flush = unionfs_flush,
3932 + .fsync = unionfs_fsync,
3933 + .fasync = unionfs_fasync,
3934 +};
3935 diff --git a/fs/unionfs/dirhelper.c b/fs/unionfs/dirhelper.c
3936 new file mode 100644
3937 index 0000000..2ecaafa
3938 --- /dev/null
3939 +++ b/fs/unionfs/dirhelper.c
3940 @@ -0,0 +1,158 @@
3941 +/*
3942 + * Copyright (c) 2003-2009 Erez Zadok
3943 + * Copyright (c) 2003-2006 Charles P. Wright
3944 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
3945 + * Copyright (c) 2005-2006 Junjiro Okajima
3946 + * Copyright (c) 2005 Arun M. Krishnakumar
3947 + * Copyright (c) 2004-2006 David P. Quigley
3948 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
3949 + * Copyright (c) 2003 Puja Gupta
3950 + * Copyright (c) 2003 Harikesavan Krishnan
3951 + * Copyright (c) 2003-2009 Stony Brook University
3952 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
3953 + *
3954 + * This program is free software; you can redistribute it and/or modify
3955 + * it under the terms of the GNU General Public License version 2 as
3956 + * published by the Free Software Foundation.
3957 + */
3958 +
3959 +#include "union.h"
3960 +
3961 +#define RD_NONE 0
3962 +#define RD_CHECK_EMPTY 1
3963 +/* The callback structure for check_empty. */
3964 +struct unionfs_rdutil_callback {
3965 + int err;
3966 + int filldir_called;
3967 + struct unionfs_dir_state *rdstate;
3968 + int mode;
3969 +};
3970 +
3971 +/* This filldir function makes sure only whiteouts exist within a directory. */
3972 +static int readdir_util_callback(void *dirent, const char *oname, int namelen,
3973 + loff_t offset, u64 ino, unsigned int d_type)
3974 +{
3975 + int err = 0;
3976 + struct unionfs_rdutil_callback *buf = dirent;
3977 + int is_whiteout;
3978 + struct filldir_node *found;
3979 + char *name = (char *) oname;
3980 +
3981 + buf->filldir_called = 1;
3982 +
3983 + if (name[0] == '.' && (namelen == 1 ||
3984 + (name[1] == '.' && namelen == 2)))
3985 + goto out;
3986 +
3987 + is_whiteout = is_whiteout_name(&name, &namelen);
3988 +
3989 + found = find_filldir_node(buf->rdstate, name, namelen, is_whiteout);
3990 + /* If it was found in the table there was a previous whiteout. */
3991 + if (found)
3992 + goto out;
3993 +
3994 + /*
3995 + * if it wasn't found and isn't a whiteout, the directory isn't
3996 + * empty.
3997 + */
3998 + err = -ENOTEMPTY;
3999 + if ((buf->mode == RD_CHECK_EMPTY) && !is_whiteout)
4000 + goto out;
4001 +
4002 + err = add_filldir_node(buf->rdstate, name, namelen,
4003 + buf->rdstate->bindex, is_whiteout);
4004 +
4005 +out:
4006 + buf->err = err;
4007 + return err;
4008 +}
4009 +
4010 +/* Is a directory logically empty? */
4011 +int check_empty(struct dentry *dentry, struct dentry *parent,
4012 + struct unionfs_dir_state **namelist)
4013 +{
4014 + int err = 0;
4015 + struct dentry *lower_dentry = NULL;
4016 + struct vfsmount *mnt;
4017 + struct super_block *sb;
4018 + struct file *lower_file;
4019 + struct unionfs_rdutil_callback *buf = NULL;
4020 + int bindex, bstart, bend, bopaque;
4021 +
4022 + sb = dentry->d_sb;
4023 +
4024 +
4025 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
4026 +
4027 + err = unionfs_partial_lookup(dentry, parent);
4028 + if (err)
4029 + goto out;
4030 +
4031 + bstart = dbstart(dentry);
4032 + bend = dbend(dentry);
4033 + bopaque = dbopaque(dentry);
4034 + if (0 <= bopaque && bopaque < bend)
4035 + bend = bopaque;
4036 +
4037 + buf = kmalloc(sizeof(struct unionfs_rdutil_callback), GFP_KERNEL);
4038 + if (unlikely(!buf)) {
4039 + err = -ENOMEM;
4040 + goto out;
4041 + }
4042 + buf->err = 0;
4043 + buf->mode = RD_CHECK_EMPTY;
4044 + buf->rdstate = alloc_rdstate(dentry->d_inode, bstart);
4045 + if (unlikely(!buf->rdstate)) {
4046 + err = -ENOMEM;
4047 + goto out;
4048 + }
4049 +
4050 + /* Process the lower directories with rdutil_callback as a filldir. */
4051 + for (bindex = bstart; bindex <= bend; bindex++) {
4052 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4053 + if (!lower_dentry)
4054 + continue;
4055 + if (!lower_dentry->d_inode)
4056 + continue;
4057 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
4058 + continue;
4059 +
4060 + dget(lower_dentry);
4061 + mnt = unionfs_mntget(dentry, bindex);
4062 + branchget(sb, bindex);
4063 + lower_file = dentry_open(lower_dentry, mnt, O_RDONLY, current_cred());
4064 + if (IS_ERR(lower_file)) {
4065 + err = PTR_ERR(lower_file);
4066 + branchput(sb, bindex);
4067 + goto out;
4068 + }
4069 +
4070 + do {
4071 + buf->filldir_called = 0;
4072 + buf->rdstate->bindex = bindex;
4073 + err = vfs_readdir(lower_file,
4074 + readdir_util_callback, buf);
4075 + if (buf->err)
4076 + err = buf->err;
4077 + } while ((err >= 0) && buf->filldir_called);
4078 +
4079 + /* fput calls dput for lower_dentry */
4080 + fput(lower_file);
4081 + branchput(sb, bindex);
4082 +
4083 + if (err < 0)
4084 + goto out;
4085 + }
4086 +
4087 +out:
4088 + if (buf) {
4089 + if (namelist && !err)
4090 + *namelist = buf->rdstate;
4091 + else if (buf->rdstate)
4092 + free_rdstate(buf->rdstate);
4093 + kfree(buf);
4094 + }
4095 +
4096 +
4097 + return err;
4098 +}
4099 diff --git a/fs/unionfs/fanout.h b/fs/unionfs/fanout.h
4100 new file mode 100644
4101 index 0000000..04ffa85
4102 --- /dev/null
4103 +++ b/fs/unionfs/fanout.h
4104 @@ -0,0 +1,407 @@
4105 +/*
4106 + * Copyright (c) 2003-2009 Erez Zadok
4107 + * Copyright (c) 2003-2006 Charles P. Wright
4108 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4109 + * Copyright (c) 2005 Arun M. Krishnakumar
4110 + * Copyright (c) 2004-2006 David P. Quigley
4111 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4112 + * Copyright (c) 2003 Puja Gupta
4113 + * Copyright (c) 2003 Harikesavan Krishnan
4114 + * Copyright (c) 2003-2009 Stony Brook University
4115 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4116 + *
4117 + * This program is free software; you can redistribute it and/or modify
4118 + * it under the terms of the GNU General Public License version 2 as
4119 + * published by the Free Software Foundation.
4120 + */
4121 +
4122 +#ifndef _FANOUT_H_
4123 +#define _FANOUT_H_
4124 +
4125 +/*
4126 + * Inode to private data
4127 + *
4128 + * Since we use containers and the struct inode is _inside_ the
4129 + * unionfs_inode_info structure, UNIONFS_I will always (given a non-NULL
4130 + * inode pointer), return a valid non-NULL pointer.
4131 + */
4132 +static inline struct unionfs_inode_info *UNIONFS_I(const struct inode *inode)
4133 +{
4134 + return container_of(inode, struct unionfs_inode_info, vfs_inode);
4135 +}
4136 +
4137 +#define ibstart(ino) (UNIONFS_I(ino)->bstart)
4138 +#define ibend(ino) (UNIONFS_I(ino)->bend)
4139 +
4140 +/* Dentry to private data */
4141 +#define UNIONFS_D(dent) ((struct unionfs_dentry_info *)(dent)->d_fsdata)
4142 +#define dbstart(dent) (UNIONFS_D(dent)->bstart)
4143 +#define dbend(dent) (UNIONFS_D(dent)->bend)
4144 +#define dbopaque(dent) (UNIONFS_D(dent)->bopaque)
4145 +
4146 +/* Superblock to private data */
4147 +#define UNIONFS_SB(super) ((struct unionfs_sb_info *)(super)->s_fs_info)
4148 +#define sbstart(sb) 0
4149 +#define sbend(sb) (UNIONFS_SB(sb)->bend)
4150 +#define sbmax(sb) (UNIONFS_SB(sb)->bend + 1)
4151 +#define sbhbid(sb) (UNIONFS_SB(sb)->high_branch_id)
4152 +
4153 +/* File to private Data */
4154 +#define UNIONFS_F(file) ((struct unionfs_file_info *)((file)->private_data))
4155 +#define fbstart(file) (UNIONFS_F(file)->bstart)
4156 +#define fbend(file) (UNIONFS_F(file)->bend)
4157 +
4158 +/* macros to manipulate branch IDs in stored in our superblock */
4159 +static inline int branch_id(struct super_block *sb, int index)
4160 +{
4161 + BUG_ON(!sb || index < 0);
4162 + return UNIONFS_SB(sb)->data[index].branch_id;
4163 +}
4164 +
4165 +static inline void set_branch_id(struct super_block *sb, int index, int val)
4166 +{
4167 + BUG_ON(!sb || index < 0);
4168 + UNIONFS_SB(sb)->data[index].branch_id = val;
4169 +}
4170 +
4171 +static inline void new_branch_id(struct super_block *sb, int index)
4172 +{
4173 + BUG_ON(!sb || index < 0);
4174 + set_branch_id(sb, index, ++UNIONFS_SB(sb)->high_branch_id);
4175 +}
4176 +
4177 +/*
4178 + * Find new index of matching branch with an existing superblock of a known
4179 + * (possibly old) id. This is needed because branches could have been
4180 + * added/deleted causing the branches of any open files to shift.
4181 + *
4182 + * @sb: the new superblock which may have new/different branch IDs
4183 + * @id: the old/existing id we're looking for
4184 + * Returns index of newly found branch (0 or greater), -1 otherwise.
4185 + */
4186 +static inline int branch_id_to_idx(struct super_block *sb, int id)
4187 +{
4188 + int i;
4189 + for (i = 0; i < sbmax(sb); i++) {
4190 + if (branch_id(sb, i) == id)
4191 + return i;
4192 + }
4193 + /* in the non-ODF code, this should really never happen */
4194 + printk(KERN_WARNING "unionfs: cannot find branch with id %d\n", id);
4195 + return -1;
4196 +}
4197 +
4198 +/* File to lower file. */
4199 +static inline struct file *unionfs_lower_file(const struct file *f)
4200 +{
4201 + BUG_ON(!f);
4202 + return UNIONFS_F(f)->lower_files[fbstart(f)];
4203 +}
4204 +
4205 +static inline struct file *unionfs_lower_file_idx(const struct file *f,
4206 + int index)
4207 +{
4208 + BUG_ON(!f || index < 0);
4209 + return UNIONFS_F(f)->lower_files[index];
4210 +}
4211 +
4212 +static inline void unionfs_set_lower_file_idx(struct file *f, int index,
4213 + struct file *val)
4214 +{
4215 + BUG_ON(!f || index < 0);
4216 + UNIONFS_F(f)->lower_files[index] = val;
4217 + /* save branch ID (may be redundant?) */
4218 + UNIONFS_F(f)->saved_branch_ids[index] =
4219 + branch_id((f)->f_path.dentry->d_sb, index);
4220 +}
4221 +
4222 +static inline void unionfs_set_lower_file(struct file *f, struct file *val)
4223 +{
4224 + BUG_ON(!f);
4225 + unionfs_set_lower_file_idx((f), fbstart(f), (val));
4226 +}
4227 +
4228 +/* Inode to lower inode. */
4229 +static inline struct inode *unionfs_lower_inode(const struct inode *i)
4230 +{
4231 + BUG_ON(!i);
4232 + return UNIONFS_I(i)->lower_inodes[ibstart(i)];
4233 +}
4234 +
4235 +static inline struct inode *unionfs_lower_inode_idx(const struct inode *i,
4236 + int index)
4237 +{
4238 + BUG_ON(!i || index < 0);
4239 + return UNIONFS_I(i)->lower_inodes[index];
4240 +}
4241 +
4242 +static inline void unionfs_set_lower_inode_idx(struct inode *i, int index,
4243 + struct inode *val)
4244 +{
4245 + BUG_ON(!i || index < 0);
4246 + UNIONFS_I(i)->lower_inodes[index] = val;
4247 +}
4248 +
4249 +static inline void unionfs_set_lower_inode(struct inode *i, struct inode *val)
4250 +{
4251 + BUG_ON(!i);
4252 + UNIONFS_I(i)->lower_inodes[ibstart(i)] = val;
4253 +}
4254 +
4255 +/* Superblock to lower superblock. */
4256 +static inline struct super_block *unionfs_lower_super(
4257 + const struct super_block *sb)
4258 +{
4259 + BUG_ON(!sb);
4260 + return UNIONFS_SB(sb)->data[sbstart(sb)].sb;
4261 +}
4262 +
4263 +static inline struct super_block *unionfs_lower_super_idx(
4264 + const struct super_block *sb,
4265 + int index)
4266 +{
4267 + BUG_ON(!sb || index < 0);
4268 + return UNIONFS_SB(sb)->data[index].sb;
4269 +}
4270 +
4271 +static inline void unionfs_set_lower_super_idx(struct super_block *sb,
4272 + int index,
4273 + struct super_block *val)
4274 +{
4275 + BUG_ON(!sb || index < 0);
4276 + UNIONFS_SB(sb)->data[index].sb = val;
4277 +}
4278 +
4279 +static inline void unionfs_set_lower_super(struct super_block *sb,
4280 + struct super_block *val)
4281 +{
4282 + BUG_ON(!sb);
4283 + UNIONFS_SB(sb)->data[sbstart(sb)].sb = val;
4284 +}
4285 +
4286 +/* Branch count macros. */
4287 +static inline int branch_count(const struct super_block *sb, int index)
4288 +{
4289 + BUG_ON(!sb || index < 0);
4290 + return atomic_read(&UNIONFS_SB(sb)->data[index].open_files);
4291 +}
4292 +
4293 +static inline void set_branch_count(struct super_block *sb, int index, int val)
4294 +{
4295 + BUG_ON(!sb || index < 0);
4296 + atomic_set(&UNIONFS_SB(sb)->data[index].open_files, val);
4297 +}
4298 +
4299 +static inline void branchget(struct super_block *sb, int index)
4300 +{
4301 + BUG_ON(!sb || index < 0);
4302 + atomic_inc(&UNIONFS_SB(sb)->data[index].open_files);
4303 +}
4304 +
4305 +static inline void branchput(struct super_block *sb, int index)
4306 +{
4307 + BUG_ON(!sb || index < 0);
4308 + atomic_dec(&UNIONFS_SB(sb)->data[index].open_files);
4309 +}
4310 +
4311 +/* Dentry macros */
4312 +static inline void unionfs_set_lower_dentry_idx(struct dentry *dent, int index,
4313 + struct dentry *val)
4314 +{
4315 + BUG_ON(!dent || index < 0);
4316 + UNIONFS_D(dent)->lower_paths[index].dentry = val;
4317 +}
4318 +
4319 +static inline struct dentry *unionfs_lower_dentry_idx(
4320 + const struct dentry *dent,
4321 + int index)
4322 +{
4323 + BUG_ON(!dent || index < 0);
4324 + return UNIONFS_D(dent)->lower_paths[index].dentry;
4325 +}
4326 +
4327 +static inline struct dentry *unionfs_lower_dentry(const struct dentry *dent)
4328 +{
4329 + BUG_ON(!dent);
4330 + return unionfs_lower_dentry_idx(dent, dbstart(dent));
4331 +}
4332 +
4333 +static inline void unionfs_set_lower_mnt_idx(struct dentry *dent, int index,
4334 + struct vfsmount *mnt)
4335 +{
4336 + BUG_ON(!dent || index < 0);
4337 + UNIONFS_D(dent)->lower_paths[index].mnt = mnt;
4338 +}
4339 +
4340 +static inline struct vfsmount *unionfs_lower_mnt_idx(
4341 + const struct dentry *dent,
4342 + int index)
4343 +{
4344 + BUG_ON(!dent || index < 0);
4345 + return UNIONFS_D(dent)->lower_paths[index].mnt;
4346 +}
4347 +
4348 +static inline struct vfsmount *unionfs_lower_mnt(const struct dentry *dent)
4349 +{
4350 + BUG_ON(!dent);
4351 + return unionfs_lower_mnt_idx(dent, dbstart(dent));
4352 +}
4353 +
4354 +/* Macros for locking a dentry. */
4355 +enum unionfs_dentry_lock_class {
4356 + UNIONFS_DMUTEX_NORMAL,
4357 + UNIONFS_DMUTEX_ROOT,
4358 + UNIONFS_DMUTEX_PARENT,
4359 + UNIONFS_DMUTEX_CHILD,
4360 + UNIONFS_DMUTEX_WHITEOUT,
4361 + UNIONFS_DMUTEX_REVAL_PARENT, /* for file/dentry revalidate */
4362 + UNIONFS_DMUTEX_REVAL_CHILD, /* for file/dentry revalidate */
4363 +};
4364 +
4365 +static inline void unionfs_lock_dentry(struct dentry *d,
4366 + unsigned int subclass)
4367 +{
4368 + BUG_ON(!d);
4369 + mutex_lock_nested(&UNIONFS_D(d)->lock, subclass);
4370 +}
4371 +
4372 +static inline void unionfs_unlock_dentry(struct dentry *d)
4373 +{
4374 + BUG_ON(!d);
4375 + mutex_unlock(&UNIONFS_D(d)->lock);
4376 +}
4377 +
4378 +static inline struct dentry *unionfs_lock_parent(struct dentry *d,
4379 + unsigned int subclass)
4380 +{
4381 + struct dentry *p;
4382 +
4383 + BUG_ON(!d);
4384 + p = dget_parent(d);
4385 + if (p != d)
4386 + mutex_lock_nested(&UNIONFS_D(p)->lock, subclass);
4387 + return p;
4388 +}
4389 +
4390 +static inline void unionfs_unlock_parent(struct dentry *d, struct dentry *p)
4391 +{
4392 + BUG_ON(!d);
4393 + BUG_ON(!p);
4394 + if (p != d) {
4395 + BUG_ON(!mutex_is_locked(&UNIONFS_D(p)->lock));
4396 + mutex_unlock(&UNIONFS_D(p)->lock);
4397 + }
4398 + dput(p);
4399 +}
4400 +
4401 +static inline void verify_locked(struct dentry *d)
4402 +{
4403 + BUG_ON(!d);
4404 + BUG_ON(!mutex_is_locked(&UNIONFS_D(d)->lock));
4405 +}
4406 +
4407 +/* macros to put lower objects */
4408 +
4409 +/*
4410 + * iput lower inodes of an unionfs dentry, from bstart to bend. If
4411 + * @free_lower is true, then also kfree the memory used to hold the lower
4412 + * object pointers.
4413 + */
4414 +static inline void iput_lowers(struct inode *inode,
4415 + int bstart, int bend, bool free_lower)
4416 +{
4417 + struct inode *lower_inode;
4418 + int bindex;
4419 +
4420 + BUG_ON(!inode);
4421 + BUG_ON(!UNIONFS_I(inode));
4422 + BUG_ON(bstart < 0);
4423 +
4424 + for (bindex = bstart; bindex <= bend; bindex++) {
4425 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4426 + if (lower_inode) {
4427 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
4428 + /* see Documentation/filesystems/unionfs/issues.txt */
4429 + lockdep_off();
4430 + iput(lower_inode);
4431 + lockdep_on();
4432 + }
4433 + }
4434 +
4435 + if (free_lower) {
4436 + kfree(UNIONFS_I(inode)->lower_inodes);
4437 + UNIONFS_I(inode)->lower_inodes = NULL;
4438 + }
4439 +}
4440 +
4441 +/* iput all lower inodes, and reset start/end branch indices to -1 */
4442 +static inline void iput_lowers_all(struct inode *inode, bool free_lower)
4443 +{
4444 + int bstart, bend;
4445 +
4446 + BUG_ON(!inode);
4447 + BUG_ON(!UNIONFS_I(inode));
4448 + bstart = ibstart(inode);
4449 + bend = ibend(inode);
4450 + BUG_ON(bstart < 0);
4451 +
4452 + iput_lowers(inode, bstart, bend, free_lower);
4453 + ibstart(inode) = ibend(inode) = -1;
4454 +}
4455 +
4456 +/*
4457 + * dput/mntput all lower dentries and vfsmounts of an unionfs dentry, from
4458 + * bstart to bend. If @free_lower is true, then also kfree the memory used
4459 + * to hold the lower object pointers.
4460 + *
4461 + * XXX: implement using path_put VFS macros
4462 + */
4463 +static inline void path_put_lowers(struct dentry *dentry,
4464 + int bstart, int bend, bool free_lower)
4465 +{
4466 + struct dentry *lower_dentry;
4467 + struct vfsmount *lower_mnt;
4468 + int bindex;
4469 +
4470 + BUG_ON(!dentry);
4471 + BUG_ON(!UNIONFS_D(dentry));
4472 + BUG_ON(bstart < 0);
4473 +
4474 + for (bindex = bstart; bindex <= bend; bindex++) {
4475 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4476 + if (lower_dentry) {
4477 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
4478 + dput(lower_dentry);
4479 + }
4480 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
4481 + if (lower_mnt) {
4482 + unionfs_set_lower_mnt_idx(dentry, bindex, NULL);
4483 + mntput(lower_mnt);
4484 + }
4485 + }
4486 +
4487 + if (free_lower) {
4488 + kfree(UNIONFS_D(dentry)->lower_paths);
4489 + UNIONFS_D(dentry)->lower_paths = NULL;
4490 + }
4491 +}
4492 +
4493 +/*
4494 + * dput/mntput all lower dentries and vfsmounts, and reset start/end branch
4495 + * indices to -1.
4496 + */
4497 +static inline void path_put_lowers_all(struct dentry *dentry, bool free_lower)
4498 +{
4499 + int bstart, bend;
4500 +
4501 + BUG_ON(!dentry);
4502 + BUG_ON(!UNIONFS_D(dentry));
4503 + bstart = dbstart(dentry);
4504 + bend = dbend(dentry);
4505 + BUG_ON(bstart < 0);
4506 +
4507 + path_put_lowers(dentry, bstart, bend, free_lower);
4508 + dbstart(dentry) = dbend(dentry) = -1;
4509 +}
4510 +
4511 +#endif /* not _FANOUT_H */
4512 diff --git a/fs/unionfs/file.c b/fs/unionfs/file.c
4513 new file mode 100644
4514 index 0000000..281169e
4515 --- /dev/null
4516 +++ b/fs/unionfs/file.c
4517 @@ -0,0 +1,380 @@
4518 +/*
4519 + * Copyright (c) 2003-2009 Erez Zadok
4520 + * Copyright (c) 2003-2006 Charles P. Wright
4521 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4522 + * Copyright (c) 2005-2006 Junjiro Okajima
4523 + * Copyright (c) 2005 Arun M. Krishnakumar
4524 + * Copyright (c) 2004-2006 David P. Quigley
4525 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4526 + * Copyright (c) 2003 Puja Gupta
4527 + * Copyright (c) 2003 Harikesavan Krishnan
4528 + * Copyright (c) 2003-2009 Stony Brook University
4529 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4530 + *
4531 + * This program is free software; you can redistribute it and/or modify
4532 + * it under the terms of the GNU General Public License version 2 as
4533 + * published by the Free Software Foundation.
4534 + */
4535 +
4536 +#include "union.h"
4537 +
4538 +static ssize_t unionfs_read(struct file *file, char __user *buf,
4539 + size_t count, loff_t *ppos)
4540 +{
4541 + int err;
4542 + struct file *lower_file;
4543 + struct dentry *dentry = file->f_path.dentry;
4544 + struct dentry *parent;
4545 +
4546 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4547 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4548 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4549 +
4550 + err = unionfs_file_revalidate(file, parent, false);
4551 + if (unlikely(err))
4552 + goto out;
4553 +
4554 + lower_file = unionfs_lower_file(file);
4555 + err = vfs_read(lower_file, buf, count, ppos);
4556 + /* update our inode atime upon a successful lower read */
4557 + if (err >= 0) {
4558 + fsstack_copy_attr_atime(dentry->d_inode,
4559 + lower_file->f_path.dentry->d_inode);
4560 + unionfs_check_file(file);
4561 + }
4562 +
4563 +out:
4564 + unionfs_unlock_dentry(dentry);
4565 + unionfs_unlock_parent(dentry, parent);
4566 + unionfs_read_unlock(dentry->d_sb);
4567 + return err;
4568 +}
4569 +
4570 +static ssize_t unionfs_write(struct file *file, const char __user *buf,
4571 + size_t count, loff_t *ppos)
4572 +{
4573 + int err = 0;
4574 + struct file *lower_file;
4575 + struct dentry *dentry = file->f_path.dentry;
4576 + struct dentry *parent;
4577 +
4578 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4579 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4580 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4581 +
4582 + err = unionfs_file_revalidate(file, parent, true);
4583 + if (unlikely(err))
4584 + goto out;
4585 +
4586 + lower_file = unionfs_lower_file(file);
4587 + err = vfs_write(lower_file, buf, count, ppos);
4588 + /* update our inode times+sizes upon a successful lower write */
4589 + if (err >= 0) {
4590 + fsstack_copy_inode_size(dentry->d_inode,
4591 + lower_file->f_path.dentry->d_inode);
4592 + fsstack_copy_attr_times(dentry->d_inode,
4593 + lower_file->f_path.dentry->d_inode);
4594 + UNIONFS_F(file)->wrote_to_file = true; /* for delayed copyup */
4595 + unionfs_check_file(file);
4596 + }
4597 +
4598 +out:
4599 + unionfs_unlock_dentry(dentry);
4600 + unionfs_unlock_parent(dentry, parent);
4601 + unionfs_read_unlock(dentry->d_sb);
4602 + return err;
4603 +}
4604 +
4605 +static int unionfs_file_readdir(struct file *file, void *dirent,
4606 + filldir_t filldir)
4607 +{
4608 + return -ENOTDIR;
4609 +}
4610 +
4611 +static int unionfs_mmap(struct file *file, struct vm_area_struct *vma)
4612 +{
4613 + int err = 0;
4614 + bool willwrite;
4615 + struct file *lower_file;
4616 + struct dentry *dentry = file->f_path.dentry;
4617 + struct dentry *parent;
4618 + struct vm_operations_struct *saved_vm_ops = NULL;
4619 +
4620 + /*
4621 + * Since mm/memory.c:might_fault() (under PROVE_LOCKING) was
4622 + * modified in 2.6.29-rc1 to call might_lock_read on mmap_sem, this
4623 + * has been causing false positives in file system stacking layers.
4624 + * In particular, our ->mmap is called after sys_mmap2 already holds
4625 + * mmap_sem, then we lock our own mutexes; but earlier, it's
4626 + * possible for lockdep to have locked our mutexes first, and then
4627 + * we call a lower ->readdir which could call might_fault. The
4628 + * different ordering of the locks is what lockdep complains about
4629 + * -- unnecessarily. Therefore, we have no choice but to tell
4630 + * lockdep to temporarily turn off lockdep here. Note: the comments
4631 + * inside might_sleep also suggest that it would have been
4632 + * nicer to only annotate paths that needs that might_lock_read.
4633 + */
4634 + lockdep_off();
4635 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4636 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4637 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4638 +
4639 + /* This might be deferred to mmap's writepage */
4640 + willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
4641 + err = unionfs_file_revalidate(file, parent, willwrite);
4642 + if (unlikely(err))
4643 + goto out;
4644 + unionfs_check_file(file);
4645 +
4646 + /*
4647 + * File systems which do not implement ->writepage may use
4648 + * generic_file_readonly_mmap as their ->mmap op. If you call
4649 + * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
4650 + * But we cannot call the lower ->mmap op, so we can't tell that
4651 + * writeable mappings won't work. Therefore, our only choice is to
4652 + * check if the lower file system supports the ->writepage, and if
4653 + * not, return EINVAL (the same error that
4654 + * generic_file_readonly_mmap returns in that case).
4655 + */
4656 + lower_file = unionfs_lower_file(file);
4657 + if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
4658 + err = -EINVAL;
4659 + printk(KERN_ERR "unionfs: branch %d file system does not "
4660 + "support writeable mmap\n", fbstart(file));
4661 + goto out;
4662 + }
4663 +
4664 + /*
4665 + * find and save lower vm_ops.
4666 + *
4667 + * XXX: the VFS should have a cleaner way of finding the lower vm_ops
4668 + */
4669 + if (!UNIONFS_F(file)->lower_vm_ops) {
4670 + err = lower_file->f_op->mmap(lower_file, vma);
4671 + if (err) {
4672 + printk(KERN_ERR "unionfs: lower mmap failed %d\n", err);
4673 + goto out;
4674 + }
4675 + saved_vm_ops = vma->vm_ops;
4676 + err = do_munmap(current->mm, vma->vm_start,
4677 + vma->vm_end - vma->vm_start);
4678 + if (err) {
4679 + printk(KERN_ERR "unionfs: do_munmap failed %d\n", err);
4680 + goto out;
4681 + }
4682 + }
4683 +
4684 + file->f_mapping->a_ops = &unionfs_dummy_aops;
4685 + err = generic_file_mmap(file, vma);
4686 + file->f_mapping->a_ops = &unionfs_aops;
4687 + if (err) {
4688 + printk(KERN_ERR "unionfs: generic_file_mmap failed %d\n", err);
4689 + goto out;
4690 + }
4691 + vma->vm_ops = &unionfs_vm_ops;
4692 + if (!UNIONFS_F(file)->lower_vm_ops)
4693 + UNIONFS_F(file)->lower_vm_ops = saved_vm_ops;
4694 +
4695 +out:
4696 + if (!err) {
4697 + /* copyup could cause parent dir times to change */
4698 + unionfs_copy_attr_times(parent->d_inode);
4699 + unionfs_check_file(file);
4700 + }
4701 + unionfs_unlock_dentry(dentry);
4702 + unionfs_unlock_parent(dentry, parent);
4703 + unionfs_read_unlock(dentry->d_sb);
4704 + lockdep_on();
4705 + return err;
4706 +}
4707 +
4708 +int unionfs_fsync(struct file *file, struct dentry *dentry, int datasync)
4709 +{
4710 + int bindex, bstart, bend;
4711 + struct file *lower_file;
4712 + struct dentry *lower_dentry;
4713 + struct dentry *parent;
4714 + struct inode *lower_inode, *inode;
4715 + int err = -EINVAL;
4716 +
4717 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4718 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4719 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4720 +
4721 + err = unionfs_file_revalidate(file, parent, true);
4722 + if (unlikely(err))
4723 + goto out;
4724 + unionfs_check_file(file);
4725 +
4726 + bstart = fbstart(file);
4727 + bend = fbend(file);
4728 + if (bstart < 0 || bend < 0)
4729 + goto out;
4730 +
4731 + inode = dentry->d_inode;
4732 + if (unlikely(!inode)) {
4733 + printk(KERN_ERR
4734 + "unionfs: null lower inode in unionfs_fsync\n");
4735 + goto out;
4736 + }
4737 + for (bindex = bstart; bindex <= bend; bindex++) {
4738 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4739 + if (!lower_inode || !lower_inode->i_fop->fsync)
4740 + continue;
4741 + lower_file = unionfs_lower_file_idx(file, bindex);
4742 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4743 + mutex_lock(&lower_inode->i_mutex);
4744 + err = lower_inode->i_fop->fsync(lower_file,
4745 + lower_dentry,
4746 + datasync);
4747 + if (!err && bindex == bstart)
4748 + fsstack_copy_attr_times(inode, lower_inode);
4749 + mutex_unlock(&lower_inode->i_mutex);
4750 + if (err)
4751 + goto out;
4752 + }
4753 +
4754 +out:
4755 + if (!err)
4756 + unionfs_check_file(file);
4757 + unionfs_unlock_dentry(dentry);
4758 + unionfs_unlock_parent(dentry, parent);
4759 + unionfs_read_unlock(dentry->d_sb);
4760 + return err;
4761 +}
4762 +
4763 +int unionfs_fasync(int fd, struct file *file, int flag)
4764 +{
4765 + int bindex, bstart, bend;
4766 + struct file *lower_file;
4767 + struct dentry *dentry = file->f_path.dentry;
4768 + struct dentry *parent;
4769 + struct inode *lower_inode, *inode;
4770 + int err = 0;
4771 +
4772 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4773 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4774 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4775 +
4776 + err = unionfs_file_revalidate(file, parent, true);
4777 + if (unlikely(err))
4778 + goto out;
4779 + unionfs_check_file(file);
4780 +
4781 + bstart = fbstart(file);
4782 + bend = fbend(file);
4783 + if (bstart < 0 || bend < 0)
4784 + goto out;
4785 +
4786 + inode = dentry->d_inode;
4787 + if (unlikely(!inode)) {
4788 + printk(KERN_ERR
4789 + "unionfs: null lower inode in unionfs_fasync\n");
4790 + goto out;
4791 + }
4792 + for (bindex = bstart; bindex <= bend; bindex++) {
4793 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
4794 + if (!lower_inode || !lower_inode->i_fop->fasync)
4795 + continue;
4796 + lower_file = unionfs_lower_file_idx(file, bindex);
4797 + mutex_lock(&lower_inode->i_mutex);
4798 + err = lower_inode->i_fop->fasync(fd, lower_file, flag);
4799 + if (!err && bindex == bstart)
4800 + fsstack_copy_attr_times(inode, lower_inode);
4801 + mutex_unlock(&lower_inode->i_mutex);
4802 + if (err)
4803 + goto out;
4804 + }
4805 +
4806 +out:
4807 + if (!err)
4808 + unionfs_check_file(file);
4809 + unionfs_unlock_dentry(dentry);
4810 + unionfs_unlock_parent(dentry, parent);
4811 + unionfs_read_unlock(dentry->d_sb);
4812 + return err;
4813 +}
4814 +
4815 +static ssize_t unionfs_splice_read(struct file *file, loff_t *ppos,
4816 + struct pipe_inode_info *pipe, size_t len,
4817 + unsigned int flags)
4818 +{
4819 + ssize_t err;
4820 + struct file *lower_file;
4821 + struct dentry *dentry = file->f_path.dentry;
4822 + struct dentry *parent;
4823 +
4824 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4825 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4826 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4827 +
4828 + err = unionfs_file_revalidate(file, parent, false);
4829 + if (unlikely(err))
4830 + goto out;
4831 +
4832 + lower_file = unionfs_lower_file(file);
4833 + err = vfs_splice_to(lower_file, ppos, pipe, len, flags);
4834 + /* update our inode atime upon a successful lower splice-read */
4835 + if (err >= 0) {
4836 + fsstack_copy_attr_atime(dentry->d_inode,
4837 + lower_file->f_path.dentry->d_inode);
4838 + unionfs_check_file(file);
4839 + }
4840 +
4841 +out:
4842 + unionfs_unlock_dentry(dentry);
4843 + unionfs_unlock_parent(dentry, parent);
4844 + unionfs_read_unlock(dentry->d_sb);
4845 + return err;
4846 +}
4847 +
4848 +static ssize_t unionfs_splice_write(struct pipe_inode_info *pipe,
4849 + struct file *file, loff_t *ppos,
4850 + size_t len, unsigned int flags)
4851 +{
4852 + ssize_t err = 0;
4853 + struct file *lower_file;
4854 + struct dentry *dentry = file->f_path.dentry;
4855 + struct dentry *parent;
4856 +
4857 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_PARENT);
4858 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
4859 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
4860 +
4861 + err = unionfs_file_revalidate(file, parent, true);
4862 + if (unlikely(err))
4863 + goto out;
4864 +
4865 + lower_file = unionfs_lower_file(file);
4866 + err = vfs_splice_from(pipe, lower_file, ppos, len, flags);
4867 + /* update our inode times+sizes upon a successful lower write */
4868 + if (err >= 0) {
4869 + fsstack_copy_inode_size(dentry->d_inode,
4870 + lower_file->f_path.dentry->d_inode);
4871 + fsstack_copy_attr_times(dentry->d_inode,
4872 + lower_file->f_path.dentry->d_inode);
4873 + unionfs_check_file(file);
4874 + }
4875 +
4876 +out:
4877 + unionfs_unlock_dentry(dentry);
4878 + unionfs_unlock_parent(dentry, parent);
4879 + unionfs_read_unlock(dentry->d_sb);
4880 + return err;
4881 +}
4882 +
4883 +struct file_operations unionfs_main_fops = {
4884 + .llseek = generic_file_llseek,
4885 + .read = unionfs_read,
4886 + .write = unionfs_write,
4887 + .readdir = unionfs_file_readdir,
4888 + .unlocked_ioctl = unionfs_ioctl,
4889 + .mmap = unionfs_mmap,
4890 + .open = unionfs_open,
4891 + .flush = unionfs_flush,
4892 + .release = unionfs_file_release,
4893 + .fsync = unionfs_fsync,
4894 + .fasync = unionfs_fasync,
4895 + .splice_read = unionfs_splice_read,
4896 + .splice_write = unionfs_splice_write,
4897 +};
4898 diff --git a/fs/unionfs/inode.c b/fs/unionfs/inode.c
4899 new file mode 100644
4900 index 0000000..bd5a3b3
4901 --- /dev/null
4902 +++ b/fs/unionfs/inode.c
4903 @@ -0,0 +1,1055 @@
4904 +/*
4905 + * Copyright (c) 2003-2009 Erez Zadok
4906 + * Copyright (c) 2003-2006 Charles P. Wright
4907 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
4908 + * Copyright (c) 2005-2006 Junjiro Okajima
4909 + * Copyright (c) 2005 Arun M. Krishnakumar
4910 + * Copyright (c) 2004-2006 David P. Quigley
4911 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
4912 + * Copyright (c) 2003 Puja Gupta
4913 + * Copyright (c) 2003 Harikesavan Krishnan
4914 + * Copyright (c) 2003-2009 Stony Brook University
4915 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
4916 + *
4917 + * This program is free software; you can redistribute it and/or modify
4918 + * it under the terms of the GNU General Public License version 2 as
4919 + * published by the Free Software Foundation.
4920 + */
4921 +
4922 +#include "union.h"
4923 +
4924 +/*
4925 + * Find a writeable branch to create new object in. Checks all writeble
4926 + * branches of the parent inode, from istart to iend order; if none are
4927 + * suitable, also tries branch 0 (which may require a copyup).
4928 + *
4929 + * Return a lower_dentry we can use to create object in, or ERR_PTR.
4930 + */
4931 +static struct dentry *find_writeable_branch(struct inode *parent,
4932 + struct dentry *dentry)
4933 +{
4934 + int err = -EINVAL;
4935 + int bindex, istart, iend;
4936 + struct dentry *lower_dentry = NULL;
4937 +
4938 + istart = ibstart(parent);
4939 + iend = ibend(parent);
4940 + if (istart < 0)
4941 + goto out;
4942 +
4943 +begin:
4944 + for (bindex = istart; bindex <= iend; bindex++) {
4945 + /* skip non-writeable branches */
4946 + err = is_robranch_super(dentry->d_sb, bindex);
4947 + if (err) {
4948 + err = -EROFS;
4949 + continue;
4950 + }
4951 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
4952 + if (!lower_dentry)
4953 + continue;
4954 + /*
4955 + * check for whiteouts in writeable branch, and remove them
4956 + * if necessary.
4957 + */
4958 + err = check_unlink_whiteout(dentry, lower_dentry, bindex);
4959 + if (err > 0) /* ignore if whiteout found and removed */
4960 + err = 0;
4961 + if (err)
4962 + continue;
4963 + /* if get here, we can write to the branch */
4964 + break;
4965 + }
4966 + /*
4967 + * If istart wasn't already branch 0, and we got any error, then try
4968 + * branch 0 (which may require copyup)
4969 + */
4970 + if (err && istart > 0) {
4971 + istart = iend = 0;
4972 + goto begin;
4973 + }
4974 +
4975 + /*
4976 + * If we tried even branch 0, and still got an error, abort. But if
4977 + * the error was an EROFS, then we should try to copyup.
4978 + */
4979 + if (err && err != -EROFS)
4980 + goto out;
4981 +
4982 + /*
4983 + * If we get here, then check if copyup needed. If lower_dentry is
4984 + * NULL, create the entire dentry directory structure in branch 0.
4985 + */
4986 + if (!lower_dentry) {
4987 + bindex = 0;
4988 + lower_dentry = create_parents(parent, dentry,
4989 + dentry->d_name.name, bindex);
4990 + if (IS_ERR(lower_dentry)) {
4991 + err = PTR_ERR(lower_dentry);
4992 + goto out;
4993 + }
4994 + }
4995 + err = 0; /* all's well */
4996 +out:
4997 + if (err)
4998 + return ERR_PTR(err);
4999 + return lower_dentry;
5000 +}
5001 +
5002 +static int unionfs_create(struct inode *dir, struct dentry *dentry,
5003 + int mode, struct nameidata *nd_unused)
5004 +{
5005 + int err = 0;
5006 + struct dentry *lower_dentry = NULL;
5007 + struct dentry *lower_parent_dentry = NULL;
5008 + struct dentry *parent;
5009 + int valid = 0;
5010 + struct nameidata lower_nd;
5011 +
5012 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5013 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5014 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5015 +
5016 + valid = __unionfs_d_revalidate(dentry, parent, false);
5017 + if (unlikely(!valid)) {
5018 + err = -ESTALE; /* same as what real_lookup does */
5019 + goto out;
5020 + }
5021 +
5022 + lower_dentry = find_writeable_branch(dir, dentry);
5023 + if (IS_ERR(lower_dentry)) {
5024 + err = PTR_ERR(lower_dentry);
5025 + goto out;
5026 + }
5027 +
5028 + lower_parent_dentry = lock_parent(lower_dentry);
5029 + if (IS_ERR(lower_parent_dentry)) {
5030 + err = PTR_ERR(lower_parent_dentry);
5031 + goto out;
5032 + }
5033 +
5034 + err = init_lower_nd(&lower_nd, LOOKUP_CREATE);
5035 + if (unlikely(err < 0))
5036 + goto out;
5037 + err = vfs_create(lower_parent_dentry->d_inode, lower_dentry, mode,
5038 + &lower_nd);
5039 + release_lower_nd(&lower_nd, err);
5040 +
5041 + if (!err) {
5042 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5043 + if (!err) {
5044 + unionfs_copy_attr_times(dir);
5045 + fsstack_copy_inode_size(dir,
5046 + lower_parent_dentry->d_inode);
5047 + /* update no. of links on parent directory */
5048 + dir->i_nlink = unionfs_get_nlinks(dir);
5049 + }
5050 + }
5051 +
5052 + unlock_dir(lower_parent_dentry);
5053 +
5054 +out:
5055 + if (!err) {
5056 + unionfs_postcopyup_setmnt(dentry);
5057 + unionfs_check_inode(dir);
5058 + unionfs_check_dentry(dentry);
5059 + }
5060 + unionfs_unlock_dentry(dentry);
5061 + unionfs_unlock_parent(dentry, parent);
5062 + unionfs_read_unlock(dentry->d_sb);
5063 + return err;
5064 +}
5065 +
5066 +/*
5067 + * unionfs_lookup is the only special function which takes a dentry, yet we
5068 + * do NOT want to call __unionfs_d_revalidate_chain because by definition,
5069 + * we don't have a valid dentry here yet.
5070 + */
5071 +static struct dentry *unionfs_lookup(struct inode *dir,
5072 + struct dentry *dentry,
5073 + struct nameidata *nd_unused)
5074 +{
5075 + struct dentry *ret, *parent;
5076 + int err = 0;
5077 +
5078 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5079 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5080 +
5081 + /*
5082 + * As long as we lock/dget the parent, then can skip validating the
5083 + * parent now; we may have to rebuild this dentry on the next
5084 + * ->d_revalidate, however.
5085 + */
5086 +
5087 + /* allocate dentry private data. We free it in ->d_release */
5088 + err = new_dentry_private_data(dentry, UNIONFS_DMUTEX_CHILD);
5089 + if (unlikely(err)) {
5090 + ret = ERR_PTR(err);
5091 + goto out;
5092 + }
5093 +
5094 + ret = unionfs_lookup_full(dentry, parent, INTERPOSE_LOOKUP);
5095 +
5096 + if (!IS_ERR(ret)) {
5097 + if (ret)
5098 + dentry = ret;
5099 + /* lookup_full can return multiple positive dentries */
5100 + if (dentry->d_inode && !S_ISDIR(dentry->d_inode->i_mode)) {
5101 + BUG_ON(dbstart(dentry) < 0);
5102 + unionfs_postcopyup_release(dentry);
5103 + }
5104 + unionfs_copy_attr_times(dentry->d_inode);
5105 + }
5106 +
5107 + unionfs_check_inode(dir);
5108 + if (!IS_ERR(ret))
5109 + unionfs_check_dentry(dentry);
5110 + unionfs_check_dentry(parent);
5111 + unionfs_unlock_dentry(dentry); /* locked in new_dentry_private data */
5112 +
5113 +out:
5114 + unionfs_unlock_parent(dentry, parent);
5115 + unionfs_read_unlock(dentry->d_sb);
5116 +
5117 + return ret;
5118 +}
5119 +
5120 +static int unionfs_link(struct dentry *old_dentry, struct inode *dir,
5121 + struct dentry *new_dentry)
5122 +{
5123 + int err = 0;
5124 + struct dentry *lower_old_dentry = NULL;
5125 + struct dentry *lower_new_dentry = NULL;
5126 + struct dentry *lower_dir_dentry = NULL;
5127 + struct dentry *old_parent, *new_parent;
5128 + char *name = NULL;
5129 + bool valid;
5130 +
5131 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5132 + old_parent = dget_parent(old_dentry);
5133 + new_parent = dget_parent(new_dentry);
5134 + unionfs_double_lock_parents(old_parent, new_parent);
5135 + unionfs_double_lock_dentry(old_dentry, new_dentry);
5136 +
5137 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
5138 + if (unlikely(!valid)) {
5139 + err = -ESTALE;
5140 + goto out;
5141 + }
5142 + if (new_dentry->d_inode) {
5143 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
5144 + if (unlikely(!valid)) {
5145 + err = -ESTALE;
5146 + goto out;
5147 + }
5148 + }
5149 +
5150 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5151 +
5152 + /* check for a whiteout in new dentry branch, and delete it */
5153 + err = check_unlink_whiteout(new_dentry, lower_new_dentry,
5154 + dbstart(new_dentry));
5155 + if (err > 0) { /* whiteout found and removed successfully */
5156 + lower_dir_dentry = dget_parent(lower_new_dentry);
5157 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
5158 + dput(lower_dir_dentry);
5159 + dir->i_nlink = unionfs_get_nlinks(dir);
5160 + err = 0;
5161 + }
5162 + if (err)
5163 + goto out;
5164 +
5165 + /* check if parent hierachy is needed, then link in same branch */
5166 + if (dbstart(old_dentry) != dbstart(new_dentry)) {
5167 + lower_new_dentry = create_parents(dir, new_dentry,
5168 + new_dentry->d_name.name,
5169 + dbstart(old_dentry));
5170 + err = PTR_ERR(lower_new_dentry);
5171 + if (IS_COPYUP_ERR(err))
5172 + goto docopyup;
5173 + if (!lower_new_dentry || IS_ERR(lower_new_dentry))
5174 + goto out;
5175 + }
5176 + lower_new_dentry = unionfs_lower_dentry(new_dentry);
5177 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5178 +
5179 + BUG_ON(dbstart(old_dentry) != dbstart(new_dentry));
5180 + lower_dir_dentry = lock_parent(lower_new_dentry);
5181 + err = is_robranch(old_dentry);
5182 + if (!err) {
5183 + /* see Documentation/filesystems/unionfs/issues.txt */
5184 + lockdep_off();
5185 + err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
5186 + lower_new_dentry);
5187 + lockdep_on();
5188 + }
5189 + unlock_dir(lower_dir_dentry);
5190 +
5191 +docopyup:
5192 + if (IS_COPYUP_ERR(err)) {
5193 + int old_bstart = dbstart(old_dentry);
5194 + int bindex;
5195 +
5196 + for (bindex = old_bstart - 1; bindex >= 0; bindex--) {
5197 + err = copyup_dentry(old_parent->d_inode,
5198 + old_dentry, old_bstart,
5199 + bindex, old_dentry->d_name.name,
5200 + old_dentry->d_name.len, NULL,
5201 + i_size_read(old_dentry->d_inode));
5202 + if (err)
5203 + continue;
5204 + lower_new_dentry =
5205 + create_parents(dir, new_dentry,
5206 + new_dentry->d_name.name,
5207 + bindex);
5208 + lower_old_dentry = unionfs_lower_dentry(old_dentry);
5209 + lower_dir_dentry = lock_parent(lower_new_dentry);
5210 + /* see Documentation/filesystems/unionfs/issues.txt */
5211 + lockdep_off();
5212 + /* do vfs_link */
5213 + err = vfs_link(lower_old_dentry,
5214 + lower_dir_dentry->d_inode,
5215 + lower_new_dentry);
5216 + lockdep_on();
5217 + unlock_dir(lower_dir_dentry);
5218 + goto check_link;
5219 + }
5220 + goto out;
5221 + }
5222 +
5223 +check_link:
5224 + if (err || !lower_new_dentry->d_inode)
5225 + goto out;
5226 +
5227 + /* Its a hard link, so use the same inode */
5228 + new_dentry->d_inode = igrab(old_dentry->d_inode);
5229 + d_add(new_dentry, new_dentry->d_inode);
5230 + unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode);
5231 + fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode);
5232 +
5233 + /* propagate number of hard-links */
5234 + old_dentry->d_inode->i_nlink = unionfs_get_nlinks(old_dentry->d_inode);
5235 + /* new dentry's ctime may have changed due to hard-link counts */
5236 + unionfs_copy_attr_times(new_dentry->d_inode);
5237 +
5238 +out:
5239 + if (!new_dentry->d_inode)
5240 + d_drop(new_dentry);
5241 +
5242 + kfree(name);
5243 + if (!err)
5244 + unionfs_postcopyup_setmnt(new_dentry);
5245 +
5246 + unionfs_check_inode(dir);
5247 + unionfs_check_dentry(new_dentry);
5248 + unionfs_check_dentry(old_dentry);
5249 +
5250 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
5251 + unionfs_double_unlock_parents(old_parent, new_parent);
5252 + dput(new_parent);
5253 + dput(old_parent);
5254 + unionfs_read_unlock(old_dentry->d_sb);
5255 +
5256 + return err;
5257 +}
5258 +
5259 +static int unionfs_symlink(struct inode *dir, struct dentry *dentry,
5260 + const char *symname)
5261 +{
5262 + int err = 0;
5263 + struct dentry *lower_dentry = NULL;
5264 + struct dentry *wh_dentry = NULL;
5265 + struct dentry *lower_parent_dentry = NULL;
5266 + struct dentry *parent;
5267 + char *name = NULL;
5268 + int valid = 0;
5269 + umode_t mode;
5270 +
5271 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5272 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5273 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5274 +
5275 + valid = __unionfs_d_revalidate(dentry, parent, false);
5276 + if (unlikely(!valid)) {
5277 + err = -ESTALE;
5278 + goto out;
5279 + }
5280 +
5281 + /*
5282 + * It's only a bug if this dentry was not negative and couldn't be
5283 + * revalidated (shouldn't happen).
5284 + */
5285 + BUG_ON(!valid && dentry->d_inode);
5286 +
5287 + lower_dentry = find_writeable_branch(dir, dentry);
5288 + if (IS_ERR(lower_dentry)) {
5289 + err = PTR_ERR(lower_dentry);
5290 + goto out;
5291 + }
5292 +
5293 + lower_parent_dentry = lock_parent(lower_dentry);
5294 + if (IS_ERR(lower_parent_dentry)) {
5295 + err = PTR_ERR(lower_parent_dentry);
5296 + goto out;
5297 + }
5298 +
5299 + mode = S_IALLUGO;
5300 + err = vfs_symlink(lower_parent_dentry->d_inode, lower_dentry, symname);
5301 + if (!err) {
5302 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5303 + if (!err) {
5304 + unionfs_copy_attr_times(dir);
5305 + fsstack_copy_inode_size(dir,
5306 + lower_parent_dentry->d_inode);
5307 + /* update no. of links on parent directory */
5308 + dir->i_nlink = unionfs_get_nlinks(dir);
5309 + }
5310 + }
5311 +
5312 + unlock_dir(lower_parent_dentry);
5313 +
5314 +out:
5315 + dput(wh_dentry);
5316 + kfree(name);
5317 +
5318 + if (!err) {
5319 + unionfs_postcopyup_setmnt(dentry);
5320 + unionfs_check_inode(dir);
5321 + unionfs_check_dentry(dentry);
5322 + }
5323 + unionfs_unlock_dentry(dentry);
5324 + unionfs_unlock_parent(dentry, parent);
5325 + unionfs_read_unlock(dentry->d_sb);
5326 + return err;
5327 +}
5328 +
5329 +static int unionfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
5330 +{
5331 + int err = 0;
5332 + struct dentry *lower_dentry = NULL;
5333 + struct dentry *lower_parent_dentry = NULL;
5334 + struct dentry *parent;
5335 + int bindex = 0, bstart;
5336 + char *name = NULL;
5337 + int valid;
5338 +
5339 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5340 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5341 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5342 +
5343 + valid = __unionfs_d_revalidate(dentry, parent, false);
5344 + if (unlikely(!valid)) {
5345 + err = -ESTALE; /* same as what real_lookup does */
5346 + goto out;
5347 + }
5348 +
5349 + bstart = dbstart(dentry);
5350 +
5351 + lower_dentry = unionfs_lower_dentry(dentry);
5352 +
5353 + /* check for a whiteout in new dentry branch, and delete it */
5354 + err = check_unlink_whiteout(dentry, lower_dentry, bstart);
5355 + if (err > 0) /* whiteout found and removed successfully */
5356 + err = 0;
5357 + if (err) {
5358 + /* exit if the error returned was NOT -EROFS */
5359 + if (!IS_COPYUP_ERR(err))
5360 + goto out;
5361 + bstart--;
5362 + }
5363 +
5364 + /* check if copyup's needed, and mkdir */
5365 + for (bindex = bstart; bindex >= 0; bindex--) {
5366 + int i;
5367 + int bend = dbend(dentry);
5368 +
5369 + if (is_robranch_super(dentry->d_sb, bindex))
5370 + continue;
5371 +
5372 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
5373 + if (!lower_dentry) {
5374 + lower_dentry = create_parents(dir, dentry,
5375 + dentry->d_name.name,
5376 + bindex);
5377 + if (!lower_dentry || IS_ERR(lower_dentry)) {
5378 + printk(KERN_ERR "unionfs: lower dentry "
5379 + " NULL for bindex = %d\n", bindex);
5380 + continue;
5381 + }
5382 + }
5383 +
5384 + lower_parent_dentry = lock_parent(lower_dentry);
5385 +
5386 + if (IS_ERR(lower_parent_dentry)) {
5387 + err = PTR_ERR(lower_parent_dentry);
5388 + goto out;
5389 + }
5390 +
5391 + err = vfs_mkdir(lower_parent_dentry->d_inode, lower_dentry,
5392 + mode);
5393 +
5394 + unlock_dir(lower_parent_dentry);
5395 +
5396 + /* did the mkdir succeed? */
5397 + if (err)
5398 + break;
5399 +
5400 + for (i = bindex + 1; i <= bend; i++) {
5401 + /* XXX: use path_put_lowers? */
5402 + if (unionfs_lower_dentry_idx(dentry, i)) {
5403 + dput(unionfs_lower_dentry_idx(dentry, i));
5404 + unionfs_set_lower_dentry_idx(dentry, i, NULL);
5405 + }
5406 + }
5407 + dbend(dentry) = bindex;
5408 +
5409 + /*
5410 + * Only INTERPOSE_LOOKUP can return a value other than 0 on
5411 + * err.
5412 + */
5413 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5414 + if (!err) {
5415 + unionfs_copy_attr_times(dir);
5416 + fsstack_copy_inode_size(dir,
5417 + lower_parent_dentry->d_inode);
5418 +
5419 + /* update number of links on parent directory */
5420 + dir->i_nlink = unionfs_get_nlinks(dir);
5421 + }
5422 +
5423 + err = make_dir_opaque(dentry, dbstart(dentry));
5424 + if (err) {
5425 + printk(KERN_ERR "unionfs: mkdir: error creating "
5426 + ".wh.__dir_opaque: %d\n", err);
5427 + goto out;
5428 + }
5429 +
5430 + /* we are done! */
5431 + break;
5432 + }
5433 +
5434 +out:
5435 + if (!dentry->d_inode)
5436 + d_drop(dentry);
5437 +
5438 + kfree(name);
5439 +
5440 + if (!err) {
5441 + unionfs_copy_attr_times(dentry->d_inode);
5442 + unionfs_postcopyup_setmnt(dentry);
5443 + }
5444 + unionfs_check_inode(dir);
5445 + unionfs_check_dentry(dentry);
5446 + unionfs_unlock_dentry(dentry);
5447 + unionfs_unlock_parent(dentry, parent);
5448 + unionfs_read_unlock(dentry->d_sb);
5449 +
5450 + return err;
5451 +}
5452 +
5453 +static int unionfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
5454 + dev_t dev)
5455 +{
5456 + int err = 0;
5457 + struct dentry *lower_dentry = NULL;
5458 + struct dentry *wh_dentry = NULL;
5459 + struct dentry *lower_parent_dentry = NULL;
5460 + struct dentry *parent;
5461 + char *name = NULL;
5462 + int valid = 0;
5463 +
5464 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5465 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5466 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5467 +
5468 + valid = __unionfs_d_revalidate(dentry, parent, false);
5469 + if (unlikely(!valid)) {
5470 + err = -ESTALE;
5471 + goto out;
5472 + }
5473 +
5474 + /*
5475 + * It's only a bug if this dentry was not negative and couldn't be
5476 + * revalidated (shouldn't happen).
5477 + */
5478 + BUG_ON(!valid && dentry->d_inode);
5479 +
5480 + lower_dentry = find_writeable_branch(dir, dentry);
5481 + if (IS_ERR(lower_dentry)) {
5482 + err = PTR_ERR(lower_dentry);
5483 + goto out;
5484 + }
5485 +
5486 + lower_parent_dentry = lock_parent(lower_dentry);
5487 + if (IS_ERR(lower_parent_dentry)) {
5488 + err = PTR_ERR(lower_parent_dentry);
5489 + goto out;
5490 + }
5491 +
5492 + err = vfs_mknod(lower_parent_dentry->d_inode, lower_dentry, mode, dev);
5493 + if (!err) {
5494 + err = PTR_ERR(unionfs_interpose(dentry, dir->i_sb, 0));
5495 + if (!err) {
5496 + unionfs_copy_attr_times(dir);
5497 + fsstack_copy_inode_size(dir,
5498 + lower_parent_dentry->d_inode);
5499 + /* update no. of links on parent directory */
5500 + dir->i_nlink = unionfs_get_nlinks(dir);
5501 + }
5502 + }
5503 +
5504 + unlock_dir(lower_parent_dentry);
5505 +
5506 +out:
5507 + dput(wh_dentry);
5508 + kfree(name);
5509 +
5510 + if (!err) {
5511 + unionfs_postcopyup_setmnt(dentry);
5512 + unionfs_check_inode(dir);
5513 + unionfs_check_dentry(dentry);
5514 + }
5515 + unionfs_unlock_dentry(dentry);
5516 + unionfs_unlock_parent(dentry, parent);
5517 + unionfs_read_unlock(dentry->d_sb);
5518 + return err;
5519 +}
5520 +
5521 +/* requires sb, dentry, and parent to already be locked */
5522 +static int __unionfs_readlink(struct dentry *dentry, char __user *buf,
5523 + int bufsiz)
5524 +{
5525 + int err;
5526 + struct dentry *lower_dentry;
5527 +
5528 + lower_dentry = unionfs_lower_dentry(dentry);
5529 +
5530 + if (!lower_dentry->d_inode->i_op ||
5531 + !lower_dentry->d_inode->i_op->readlink) {
5532 + err = -EINVAL;
5533 + goto out;
5534 + }
5535 +
5536 + err = lower_dentry->d_inode->i_op->readlink(lower_dentry,
5537 + buf, bufsiz);
5538 + if (err >= 0)
5539 + fsstack_copy_attr_atime(dentry->d_inode,
5540 + lower_dentry->d_inode);
5541 +
5542 +out:
5543 + return err;
5544 +}
5545 +
5546 +static int unionfs_readlink(struct dentry *dentry, char __user *buf,
5547 + int bufsiz)
5548 +{
5549 + int err;
5550 + struct dentry *parent;
5551 +
5552 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5553 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5554 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5555 +
5556 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5557 + err = -ESTALE;
5558 + goto out;
5559 + }
5560 +
5561 + err = __unionfs_readlink(dentry, buf, bufsiz);
5562 +
5563 +out:
5564 + unionfs_check_dentry(dentry);
5565 + unionfs_unlock_dentry(dentry);
5566 + unionfs_unlock_parent(dentry, parent);
5567 + unionfs_read_unlock(dentry->d_sb);
5568 +
5569 + return err;
5570 +}
5571 +
5572 +static void *unionfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5573 +{
5574 + char *buf;
5575 + int len = PAGE_SIZE, err;
5576 + mm_segment_t old_fs;
5577 + struct dentry *parent;
5578 +
5579 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5580 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5581 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5582 +
5583 + /* This is freed by the put_link method assuming a successful call. */
5584 + buf = kmalloc(len, GFP_KERNEL);
5585 + if (unlikely(!buf)) {
5586 + err = -ENOMEM;
5587 + goto out;
5588 + }
5589 +
5590 + /* read the symlink, and then we will follow it */
5591 + old_fs = get_fs();
5592 + set_fs(KERNEL_DS);
5593 + err = __unionfs_readlink(dentry, buf, len);
5594 + set_fs(old_fs);
5595 + if (err < 0) {
5596 + kfree(buf);
5597 + buf = NULL;
5598 + goto out;
5599 + }
5600 + buf[err] = 0;
5601 + nd_set_link(nd, buf);
5602 + err = 0;
5603 +
5604 +out:
5605 + if (err >= 0) {
5606 + unionfs_check_nd(nd);
5607 + unionfs_check_dentry(dentry);
5608 + }
5609 +
5610 + unionfs_unlock_dentry(dentry);
5611 + unionfs_unlock_parent(dentry, parent);
5612 + unionfs_read_unlock(dentry->d_sb);
5613 +
5614 + return ERR_PTR(err);
5615 +}
5616 +
5617 +/* this @nd *IS* still used */
5618 +static void unionfs_put_link(struct dentry *dentry, struct nameidata *nd,
5619 + void *cookie)
5620 +{
5621 + struct dentry *parent;
5622 +
5623 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5624 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5625 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5626 +
5627 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false)))
5628 + printk(KERN_ERR
5629 + "unionfs: put_link failed to revalidate dentry\n");
5630 +
5631 + unionfs_check_dentry(dentry);
5632 + unionfs_check_nd(nd);
5633 + kfree(nd_get_link(nd));
5634 + unionfs_unlock_dentry(dentry);
5635 + unionfs_unlock_parent(dentry, parent);
5636 + unionfs_read_unlock(dentry->d_sb);
5637 +}
5638 +
5639 +/*
5640 + * This is a variant of fs/namei.c:permission() or inode_permission() which
5641 + * skips over EROFS tests (because we perform copyup on EROFS).
5642 + */
5643 +static int __inode_permission(struct inode *inode, int mask)
5644 +{
5645 + int retval;
5646 +
5647 + /* nobody gets write access to an immutable file */
5648 + if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
5649 + return -EACCES;
5650 +
5651 + /* Ordinary permission routines do not understand MAY_APPEND. */
5652 + if (inode->i_op && inode->i_op->permission) {
5653 + retval = inode->i_op->permission(inode, mask);
5654 + if (!retval) {
5655 + /*
5656 + * Exec permission on a regular file is denied if none
5657 + * of the execute bits are set.
5658 + *
5659 + * This check should be done by the ->permission()
5660 + * method.
5661 + */
5662 + if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
5663 + !(inode->i_mode & S_IXUGO))
5664 + return -EACCES;
5665 + }
5666 + } else {
5667 + retval = generic_permission(inode, mask, NULL);
5668 + }
5669 + if (retval)
5670 + return retval;
5671 +
5672 + return security_inode_permission(inode,
5673 + mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
5674 +}
5675 +
5676 +/*
5677 + * Don't grab the superblock read-lock in unionfs_permission, which prevents
5678 + * a deadlock with the branch-management "add branch" code (which grabbed
5679 + * the write lock). It is safe to not grab the read lock here, because even
5680 + * with branch management taking place, there is no chance that
5681 + * unionfs_permission, or anything it calls, will use stale branch
5682 + * information.
5683 + */
5684 +static int unionfs_permission(struct inode *inode, int mask)
5685 +{
5686 + struct inode *lower_inode = NULL;
5687 + int err = 0;
5688 + int bindex, bstart, bend;
5689 + const int is_file = !S_ISDIR(inode->i_mode);
5690 + const int write_mask = (mask & MAY_WRITE) && !(mask & MAY_READ);
5691 + struct inode *inode_grabbed = igrab(inode);
5692 + struct dentry *dentry = d_find_alias(inode);
5693 +
5694 + if (dentry)
5695 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5696 +
5697 + if (!UNIONFS_I(inode)->lower_inodes) {
5698 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5699 + err = -ESTALE; /* force revalidate */
5700 + goto out;
5701 + }
5702 + bstart = ibstart(inode);
5703 + bend = ibend(inode);
5704 + if (unlikely(bstart < 0 || bend < 0)) {
5705 + /*
5706 + * With branch-management, we can get a stale inode here.
5707 + * If so, we return ESTALE back to link_path_walk, which
5708 + * would discard the dcache entry and re-lookup the
5709 + * dentry+inode. This should be equivalent to issuing
5710 + * __unionfs_d_revalidate_chain on nd.dentry here.
5711 + */
5712 + if (is_file) /* dirs can be unlinked but chdir'ed to */
5713 + err = -ESTALE; /* force revalidate */
5714 + goto out;
5715 + }
5716 +
5717 + for (bindex = bstart; bindex <= bend; bindex++) {
5718 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
5719 + if (!lower_inode)
5720 + continue;
5721 +
5722 + /*
5723 + * check the condition for D-F-D underlying files/directories,
5724 + * we don't have to check for files, if we are checking for
5725 + * directories.
5726 + */
5727 + if (!is_file && !S_ISDIR(lower_inode->i_mode))
5728 + continue;
5729 +
5730 + /*
5731 + * We check basic permissions, but we ignore any conditions
5732 + * such as readonly file systems or branches marked as
5733 + * readonly, because those conditions should lead to a
5734 + * copyup taking place later on. However, if user never had
5735 + * access to the file, then no copyup could ever take place.
5736 + */
5737 + err = __inode_permission(lower_inode, mask);
5738 + if (err && err != -EACCES && err != EPERM && bindex > 0) {
5739 + umode_t mode = lower_inode->i_mode;
5740 + if ((is_robranch_super(inode->i_sb, bindex) ||
5741 + __is_rdonly(lower_inode)) &&
5742 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
5743 + err = 0;
5744 + if (IS_COPYUP_ERR(err))
5745 + err = 0;
5746 + }
5747 +
5748 + /*
5749 + * NFS HACK: NFSv2/3 return EACCES on readonly-exported,
5750 + * locally readonly-mounted file systems, instead of EROFS
5751 + * like other file systems do. So we have no choice here
5752 + * but to intercept this and ignore it for NFS branches
5753 + * marked readonly. Specifically, we avoid using NFS's own
5754 + * "broken" ->permission method, and rely on
5755 + * generic_permission() to do basic checking for us.
5756 + */
5757 + if (err && err == -EACCES &&
5758 + is_robranch_super(inode->i_sb, bindex) &&
5759 + lower_inode->i_sb->s_magic == NFS_SUPER_MAGIC)
5760 + err = generic_permission(lower_inode, mask, NULL);
5761 +
5762 + /*
5763 + * The permissions are an intersection of the overall directory
5764 + * permissions, so we fail if one fails.
5765 + */
5766 + if (err)
5767 + goto out;
5768 +
5769 + /* only the leftmost file matters. */
5770 + if (is_file || write_mask) {
5771 + if (is_file && write_mask) {
5772 + err = get_write_access(lower_inode);
5773 + if (!err)
5774 + put_write_access(lower_inode);
5775 + }
5776 + break;
5777 + }
5778 + }
5779 + /* sync times which may have changed (asynchronously) below */
5780 + unionfs_copy_attr_times(inode);
5781 +
5782 +out:
5783 + unionfs_check_inode(inode);
5784 + if (dentry) {
5785 + unionfs_unlock_dentry(dentry);
5786 + dput(dentry);
5787 + }
5788 + iput(inode_grabbed);
5789 + return err;
5790 +}
5791 +
5792 +static int unionfs_setattr(struct dentry *dentry, struct iattr *ia)
5793 +{
5794 + int err = 0;
5795 + struct dentry *lower_dentry;
5796 + struct dentry *parent;
5797 + struct inode *inode;
5798 + struct inode *lower_inode;
5799 + int bstart, bend, bindex;
5800 + loff_t size;
5801 +
5802 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
5803 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
5804 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
5805 +
5806 + if (unlikely(!__unionfs_d_revalidate(dentry, parent, false))) {
5807 + err = -ESTALE;
5808 + goto out;
5809 + }
5810 +
5811 + bstart = dbstart(dentry);
5812 + bend = dbend(dentry);
5813 + inode = dentry->d_inode;
5814 +
5815 + /*
5816 + * mode change is for clearing setuid/setgid. Allow lower filesystem
5817 + * to reinterpret it in its own way.
5818 + */
5819 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
5820 + ia->ia_valid &= ~ATTR_MODE;
5821 +
5822 + lower_dentry = unionfs_lower_dentry(dentry);
5823 + if (!lower_dentry) { /* should never happen after above revalidate */
5824 + err = -EINVAL;
5825 + goto out;
5826 + }
5827 + lower_inode = unionfs_lower_inode(inode);
5828 +
5829 + /* check if user has permission to change lower inode */
5830 + err = inode_change_ok(lower_inode, ia);
5831 + if (err)
5832 + goto out;
5833 +
5834 + /* copyup if the file is on a read only branch */
5835 + if (is_robranch_super(dentry->d_sb, bstart)
5836 + || __is_rdonly(lower_inode)) {
5837 + /* check if we have a branch to copy up to */
5838 + if (bstart <= 0) {
5839 + err = -EACCES;
5840 + goto out;
5841 + }
5842 +
5843 + if (ia->ia_valid & ATTR_SIZE)
5844 + size = ia->ia_size;
5845 + else
5846 + size = i_size_read(inode);
5847 + /* copyup to next available branch */
5848 + for (bindex = bstart - 1; bindex >= 0; bindex--) {
5849 + err = copyup_dentry(parent->d_inode,
5850 + dentry, bstart, bindex,
5851 + dentry->d_name.name,
5852 + dentry->d_name.len,
5853 + NULL, size);
5854 + if (!err)
5855 + break;
5856 + }
5857 + if (err)
5858 + goto out;
5859 + /* get updated lower_dentry/inode after copyup */
5860 + lower_dentry = unionfs_lower_dentry(dentry);
5861 + lower_inode = unionfs_lower_inode(inode);
5862 + }
5863 +
5864 + /*
5865 + * If shrinking, first truncate upper level to cancel writing dirty
5866 + * pages beyond the new eof; and also if its' maxbytes is more
5867 + * limiting (fail with -EFBIG before making any change to the lower
5868 + * level). There is no need to vmtruncate the upper level
5869 + * afterwards in the other cases: we fsstack_copy_inode_size from
5870 + * the lower level.
5871 + */
5872 + if (ia->ia_valid & ATTR_SIZE) {
5873 + size = i_size_read(inode);
5874 + if (ia->ia_size < size || (ia->ia_size > size &&
5875 + inode->i_sb->s_maxbytes < lower_inode->i_sb->s_maxbytes)) {
5876 + err = vmtruncate(inode, ia->ia_size);
5877 + if (err)
5878 + goto out;
5879 + }
5880 + }
5881 +
5882 + /* notify the (possibly copied-up) lower inode */
5883 + /*
5884 + * Note: we use lower_dentry->d_inode, because lower_inode may be
5885 + * unlinked (no inode->i_sb and i_ino==0. This happens if someone
5886 + * tries to open(), unlink(), then ftruncate() a file.
5887 + */
5888 + mutex_lock(&lower_dentry->d_inode->i_mutex);
5889 + err = notify_change(lower_dentry, ia);
5890 + mutex_unlock(&lower_dentry->d_inode->i_mutex);
5891 + if (err)
5892 + goto out;
5893 +
5894 + /* get attributes from the first lower inode */
5895 + if (ibstart(inode) >= 0)
5896 + unionfs_copy_attr_all(inode, lower_inode);
5897 + /*
5898 + * unionfs_copy_attr_all will copy the lower times to our inode if
5899 + * the lower ones are newer (useful for cache coherency). However,
5900 + * ->setattr is the only place in which we may have to copy the
5901 + * lower inode times absolutely, to support utimes(2).
5902 + */
5903 + if (ia->ia_valid & ATTR_MTIME_SET)
5904 + inode->i_mtime = lower_inode->i_mtime;
5905 + if (ia->ia_valid & ATTR_CTIME)
5906 + inode->i_ctime = lower_inode->i_ctime;
5907 + if (ia->ia_valid & ATTR_ATIME_SET)
5908 + inode->i_atime = lower_inode->i_atime;
5909 + fsstack_copy_inode_size(inode, lower_inode);
5910 +
5911 +out:
5912 + if (!err)
5913 + unionfs_check_dentry(dentry);
5914 + unionfs_unlock_dentry(dentry);
5915 + unionfs_unlock_parent(dentry, parent);
5916 + unionfs_read_unlock(dentry->d_sb);
5917 +
5918 + return err;
5919 +}
5920 +
5921 +struct inode_operations unionfs_symlink_iops = {
5922 + .readlink = unionfs_readlink,
5923 + .permission = unionfs_permission,
5924 + .follow_link = unionfs_follow_link,
5925 + .setattr = unionfs_setattr,
5926 + .put_link = unionfs_put_link,
5927 +};
5928 +
5929 +struct inode_operations unionfs_dir_iops = {
5930 + .create = unionfs_create,
5931 + .lookup = unionfs_lookup,
5932 + .link = unionfs_link,
5933 + .unlink = unionfs_unlink,
5934 + .symlink = unionfs_symlink,
5935 + .mkdir = unionfs_mkdir,
5936 + .rmdir = unionfs_rmdir,
5937 + .mknod = unionfs_mknod,
5938 + .rename = unionfs_rename,
5939 + .permission = unionfs_permission,
5940 + .setattr = unionfs_setattr,
5941 +#ifdef CONFIG_UNION_FS_XATTR
5942 + .setxattr = unionfs_setxattr,
5943 + .getxattr = unionfs_getxattr,
5944 + .removexattr = unionfs_removexattr,
5945 + .listxattr = unionfs_listxattr,
5946 +#endif /* CONFIG_UNION_FS_XATTR */
5947 +};
5948 +
5949 +struct inode_operations unionfs_main_iops = {
5950 + .permission = unionfs_permission,
5951 + .setattr = unionfs_setattr,
5952 +#ifdef CONFIG_UNION_FS_XATTR
5953 + .setxattr = unionfs_setxattr,
5954 + .getxattr = unionfs_getxattr,
5955 + .removexattr = unionfs_removexattr,
5956 + .listxattr = unionfs_listxattr,
5957 +#endif /* CONFIG_UNION_FS_XATTR */
5958 +};
5959 diff --git a/fs/unionfs/lookup.c b/fs/unionfs/lookup.c
5960 new file mode 100644
5961 index 0000000..6361541
5962 --- /dev/null
5963 +++ b/fs/unionfs/lookup.c
5964 @@ -0,0 +1,569 @@
5965 +/*
5966 + * Copyright (c) 2003-2009 Erez Zadok
5967 + * Copyright (c) 2003-2006 Charles P. Wright
5968 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5969 + * Copyright (c) 2005-2006 Junjiro Okajima
5970 + * Copyright (c) 2005 Arun M. Krishnakumar
5971 + * Copyright (c) 2004-2006 David P. Quigley
5972 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
5973 + * Copyright (c) 2003 Puja Gupta
5974 + * Copyright (c) 2003 Harikesavan Krishnan
5975 + * Copyright (c) 2003-2009 Stony Brook University
5976 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
5977 + *
5978 + * This program is free software; you can redistribute it and/or modify
5979 + * it under the terms of the GNU General Public License version 2 as
5980 + * published by the Free Software Foundation.
5981 + */
5982 +
5983 +#include "union.h"
5984 +
5985 +/*
5986 + * Lookup one path component @name relative to a <base,mnt> path pair.
5987 + * Behaves nearly the same as lookup_one_len (i.e., return negative dentry
5988 + * on ENOENT), but uses the @mnt passed, so it can cross bind mounts and
5989 + * other lower mounts properly. If @new_mnt is non-null, will fill in the
5990 + * new mnt there. Caller is responsible to dput/mntput/path_put returned
5991 + * @dentry and @new_mnt.
5992 + */
5993 +struct dentry *__lookup_one(struct dentry *base, struct vfsmount *mnt,
5994 + const char *name, struct vfsmount **new_mnt)
5995 +{
5996 + struct dentry *dentry = NULL;
5997 + struct nameidata lower_nd;
5998 + int err;
5999 +
6000 + /* we use flags=0 to get basic lookup */
6001 + err = vfs_path_lookup(base, mnt, name, 0, &lower_nd);
6002 +
6003 + switch (err) {
6004 + case 0: /* no error */
6005 + dentry = lower_nd.path.dentry;
6006 + if (new_mnt)
6007 + *new_mnt = lower_nd.path.mnt; /* rc already inc'ed */
6008 + break;
6009 + case -ENOENT:
6010 + /*
6011 + * We don't consider ENOENT an error, and we want to return
6012 + * a negative dentry (ala lookup_one_len). As we know
6013 + * there was no inode for this name before (-ENOENT), then
6014 + * it's safe to call lookup_one_len (which doesn't take a
6015 + * vfsmount).
6016 + */
6017 + dentry = lookup_lck_len(name, base, strlen(name));
6018 + if (new_mnt)
6019 + *new_mnt = mntget(lower_nd.path.mnt);
6020 + break;
6021 + default: /* all other real errors */
6022 + dentry = ERR_PTR(err);
6023 + break;
6024 + }
6025 +
6026 + return dentry;
6027 +}
6028 +
6029 +/*
6030 + * This is a utility function that fills in a unionfs dentry.
6031 + * Caller must lock this dentry with unionfs_lock_dentry.
6032 + *
6033 + * Returns: 0 (ok), or -ERRNO if an error occurred.
6034 + * XXX: get rid of _partial_lookup and make callers call _lookup_full directly
6035 + */
6036 +int unionfs_partial_lookup(struct dentry *dentry, struct dentry *parent)
6037 +{
6038 + struct dentry *tmp;
6039 + int err = -ENOSYS;
6040 +
6041 + tmp = unionfs_lookup_full(dentry, parent, INTERPOSE_PARTIAL);
6042 +
6043 + if (!tmp) {
6044 + err = 0;
6045 + goto out;
6046 + }
6047 + if (IS_ERR(tmp)) {
6048 + err = PTR_ERR(tmp);
6049 + goto out;
6050 + }
6051 + /* XXX: need to change the interface */
6052 + BUG_ON(tmp != dentry);
6053 +out:
6054 + return err;
6055 +}
6056 +
6057 +/* The dentry cache is just so we have properly sized dentries. */
6058 +static struct kmem_cache *unionfs_dentry_cachep;
6059 +int unionfs_init_dentry_cache(void)
6060 +{
6061 + unionfs_dentry_cachep =
6062 + kmem_cache_create("unionfs_dentry",
6063 + sizeof(struct unionfs_dentry_info),
6064 + 0, SLAB_RECLAIM_ACCOUNT, NULL);
6065 +
6066 + return (unionfs_dentry_cachep ? 0 : -ENOMEM);
6067 +}
6068 +
6069 +void unionfs_destroy_dentry_cache(void)
6070 +{
6071 + if (unionfs_dentry_cachep)
6072 + kmem_cache_destroy(unionfs_dentry_cachep);
6073 +}
6074 +
6075 +void free_dentry_private_data(struct dentry *dentry)
6076 +{
6077 + if (!dentry || !dentry->d_fsdata)
6078 + return;
6079 + kfree(UNIONFS_D(dentry)->lower_paths);
6080 + UNIONFS_D(dentry)->lower_paths = NULL;
6081 + kmem_cache_free(unionfs_dentry_cachep, dentry->d_fsdata);
6082 + dentry->d_fsdata = NULL;
6083 +}
6084 +
6085 +static inline int __realloc_dentry_private_data(struct dentry *dentry)
6086 +{
6087 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6088 + void *p;
6089 + int size;
6090 +
6091 + BUG_ON(!info);
6092 +
6093 + size = sizeof(struct path) * sbmax(dentry->d_sb);
6094 + p = krealloc(info->lower_paths, size, GFP_ATOMIC);
6095 + if (unlikely(!p))
6096 + return -ENOMEM;
6097 +
6098 + info->lower_paths = p;
6099 +
6100 + info->bstart = -1;
6101 + info->bend = -1;
6102 + info->bopaque = -1;
6103 + info->bcount = sbmax(dentry->d_sb);
6104 + atomic_set(&info->generation,
6105 + atomic_read(&UNIONFS_SB(dentry->d_sb)->generation));
6106 +
6107 + memset(info->lower_paths, 0, size);
6108 +
6109 + return 0;
6110 +}
6111 +
6112 +/* UNIONFS_D(dentry)->lock must be locked */
6113 +int realloc_dentry_private_data(struct dentry *dentry)
6114 +{
6115 + if (!__realloc_dentry_private_data(dentry))
6116 + return 0;
6117 +
6118 + kfree(UNIONFS_D(dentry)->lower_paths);
6119 + free_dentry_private_data(dentry);
6120 + return -ENOMEM;
6121 +}
6122 +
6123 +/* allocate new dentry private data */
6124 +int new_dentry_private_data(struct dentry *dentry, int subclass)
6125 +{
6126 + struct unionfs_dentry_info *info = UNIONFS_D(dentry);
6127 +
6128 + BUG_ON(info);
6129 +
6130 + info = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC);
6131 + if (unlikely(!info))
6132 + return -ENOMEM;
6133 +
6134 + mutex_init(&info->lock);
6135 + mutex_lock_nested(&info->lock, subclass);
6136 +
6137 + info->lower_paths = NULL;
6138 +
6139 + dentry->d_fsdata = info;
6140 +
6141 + if (!__realloc_dentry_private_data(dentry))
6142 + return 0;
6143 +
6144 + mutex_unlock(&info->lock);
6145 + free_dentry_private_data(dentry);
6146 + return -ENOMEM;
6147 +}
6148 +
6149 +/*
6150 + * scan through the lower dentry objects, and set bstart to reflect the
6151 + * starting branch
6152 + */
6153 +void update_bstart(struct dentry *dentry)
6154 +{
6155 + int bindex;
6156 + int bstart = dbstart(dentry);
6157 + int bend = dbend(dentry);
6158 + struct dentry *lower_dentry;
6159 +
6160 + for (bindex = bstart; bindex <= bend; bindex++) {
6161 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6162 + if (!lower_dentry)
6163 + continue;
6164 + if (lower_dentry->d_inode) {
6165 + dbstart(dentry) = bindex;
6166 + break;
6167 + }
6168 + dput(lower_dentry);
6169 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
6170 + }
6171 +}
6172 +
6173 +
6174 +/*
6175 + * Initialize a nameidata structure (the intent part) we can pass to a lower
6176 + * file system. Returns 0 on success or -error (only -ENOMEM possible).
6177 + * Inside that nd structure, this function may also return an allocated
6178 + * struct file (for open intents). The caller, when done with this nd, must
6179 + * kfree the intent file (using release_lower_nd).
6180 + *
6181 + * XXX: this code, and the callers of this code, should be redone using
6182 + * vfs_path_lookup() when (1) the nameidata structure is refactored into a
6183 + * separate intent-structure, and (2) open_namei() is broken into a VFS-only
6184 + * function and a method that other file systems can call.
6185 + */
6186 +int init_lower_nd(struct nameidata *nd, unsigned int flags)
6187 +{
6188 + int err = 0;
6189 +#ifdef ALLOC_LOWER_ND_FILE
6190 + /*
6191 + * XXX: one day we may need to have the lower return an open file
6192 + * for us. It is not needed in 2.6.23-rc1 for nfs2/nfs3, but may
6193 + * very well be needed for nfs4.
6194 + */
6195 + struct file *file;
6196 +#endif /* ALLOC_LOWER_ND_FILE */
6197 +
6198 + memset(nd, 0, sizeof(struct nameidata));
6199 + if (!flags)
6200 + return err;
6201 +
6202 + switch (flags) {
6203 + case LOOKUP_CREATE:
6204 + nd->intent.open.flags |= O_CREAT;
6205 + /* fall through: shared code for create/open cases */
6206 + case LOOKUP_OPEN:
6207 + nd->flags = flags;
6208 + nd->intent.open.flags |= (FMODE_READ | FMODE_WRITE);
6209 +#ifdef ALLOC_LOWER_ND_FILE
6210 + file = kzalloc(sizeof(struct file), GFP_KERNEL);
6211 + if (unlikely(!file)) {
6212 + err = -ENOMEM;
6213 + break; /* exit switch statement and thus return */
6214 + }
6215 + nd->intent.open.file = file;
6216 +#endif /* ALLOC_LOWER_ND_FILE */
6217 + break;
6218 + default:
6219 + /*
6220 + * We should never get here, for now.
6221 + * We can add new cases here later on.
6222 + */
6223 + pr_debug("unionfs: unknown nameidata flag 0x%x\n", flags);
6224 + BUG();
6225 + break;
6226 + }
6227 +
6228 + return err;
6229 +}
6230 +
6231 +void release_lower_nd(struct nameidata *nd, int err)
6232 +{
6233 + if (!nd->intent.open.file)
6234 + return;
6235 + else if (!err)
6236 + release_open_intent(nd);
6237 +#ifdef ALLOC_LOWER_ND_FILE
6238 + kfree(nd->intent.open.file);
6239 +#endif /* ALLOC_LOWER_ND_FILE */
6240 +}
6241 +
6242 +/*
6243 + * Main (and complex) driver function for Unionfs's lookup
6244 + *
6245 + * Returns: NULL (ok), ERR_PTR if an error occurred, or a non-null non-error
6246 + * PTR if d_splice returned a different dentry.
6247 + *
6248 + * If lookupmode is INTERPOSE_PARTIAL/REVAL/REVAL_NEG, the passed dentry's
6249 + * inode info must be locked. If lookupmode is INTERPOSE_LOOKUP (i.e., a
6250 + * newly looked-up dentry), then unionfs_lookup_backend will return a locked
6251 + * dentry's info, which the caller must unlock.
6252 + */
6253 +struct dentry *unionfs_lookup_full(struct dentry *dentry,
6254 + struct dentry *parent, int lookupmode)
6255 +{
6256 + int err = 0;
6257 + struct dentry *lower_dentry = NULL;
6258 + struct vfsmount *lower_mnt;
6259 + struct vfsmount *lower_dir_mnt;
6260 + struct dentry *wh_lower_dentry = NULL;
6261 + struct dentry *lower_dir_dentry = NULL;
6262 + struct dentry *d_interposed = NULL;
6263 + int bindex, bstart, bend, bopaque;
6264 + int opaque, num_positive = 0;
6265 + const char *name;
6266 + int namelen;
6267 + int pos_start, pos_end;
6268 +
6269 + /*
6270 + * We should already have a lock on this dentry in the case of a
6271 + * partial lookup, or a revalidation. Otherwise it is returned from
6272 + * new_dentry_private_data already locked.
6273 + */
6274 + verify_locked(dentry);
6275 + verify_locked(parent);
6276 +
6277 + /* must initialize dentry operations */
6278 + dentry->d_op = &unionfs_dops;
6279 +
6280 + /* We never partial lookup the root directory. */
6281 + if (IS_ROOT(dentry))
6282 + goto out;
6283 +
6284 + name = dentry->d_name.name;
6285 + namelen = dentry->d_name.len;
6286 +
6287 + /* No dentries should get created for possible whiteout names. */
6288 + if (!is_validname(name)) {
6289 + err = -EPERM;
6290 + goto out_free;
6291 + }
6292 +
6293 + /* Now start the actual lookup procedure. */
6294 + bstart = dbstart(parent);
6295 + bend = dbend(parent);
6296 + bopaque = dbopaque(parent);
6297 + BUG_ON(bstart < 0);
6298 +
6299 + /* adjust bend to bopaque if needed */
6300 + if ((bopaque >= 0) && (bopaque < bend))
6301 + bend = bopaque;
6302 +
6303 + /* lookup all possible dentries */
6304 + for (bindex = bstart; bindex <= bend; bindex++) {
6305 +
6306 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6307 + lower_mnt = unionfs_lower_mnt_idx(dentry, bindex);
6308 +
6309 + /* skip if we already have a positive lower dentry */
6310 + if (lower_dentry) {
6311 + if (dbstart(dentry) < 0)
6312 + dbstart(dentry) = bindex;
6313 + if (bindex > dbend(dentry))
6314 + dbend(dentry) = bindex;
6315 + if (lower_dentry->d_inode)
6316 + num_positive++;
6317 + continue;
6318 + }
6319 +
6320 + lower_dir_dentry =
6321 + unionfs_lower_dentry_idx(parent, bindex);
6322 + /* if the lower dentry's parent does not exist, skip this */
6323 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6324 + continue;
6325 +
6326 + /* also skip it if the parent isn't a directory. */
6327 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6328 + continue; /* XXX: should be BUG_ON */
6329 +
6330 + /* check for whiteouts: stop lookup if found */
6331 + wh_lower_dentry = lookup_whiteout(name, lower_dir_dentry);
6332 + if (IS_ERR(wh_lower_dentry)) {
6333 + err = PTR_ERR(wh_lower_dentry);
6334 + goto out_free;
6335 + }
6336 + if (wh_lower_dentry->d_inode) {
6337 + dbend(dentry) = dbopaque(dentry) = bindex;
6338 + if (dbstart(dentry) < 0)
6339 + dbstart(dentry) = bindex;
6340 + dput(wh_lower_dentry);
6341 + break;
6342 + }
6343 + dput(wh_lower_dentry);
6344 +
6345 + /* Now do regular lookup; lookup @name */
6346 + lower_dir_mnt = unionfs_lower_mnt_idx(parent, bindex);
6347 + lower_mnt = NULL; /* XXX: needed? */
6348 +
6349 + lower_dentry = __lookup_one(lower_dir_dentry, lower_dir_mnt,
6350 + name, &lower_mnt);
6351 +
6352 + if (IS_ERR(lower_dentry)) {
6353 + err = PTR_ERR(lower_dentry);
6354 + goto out_free;
6355 + }
6356 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6357 + if (!lower_mnt)
6358 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root,
6359 + bindex);
6360 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6361 +
6362 + /* adjust dbstart/end */
6363 + if (dbstart(dentry) < 0)
6364 + dbstart(dentry) = bindex;
6365 + if (bindex > dbend(dentry))
6366 + dbend(dentry) = bindex;
6367 + /*
6368 + * We always store the lower dentries above, and update
6369 + * dbstart/dbend, even if the whole unionfs dentry is
6370 + * negative (i.e., no lower inodes).
6371 + */
6372 + if (!lower_dentry->d_inode)
6373 + continue;
6374 + num_positive++;
6375 +
6376 + /*
6377 + * check if we just found an opaque directory, if so, stop
6378 + * lookups here.
6379 + */
6380 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
6381 + continue;
6382 + opaque = is_opaque_dir(dentry, bindex);
6383 + if (opaque < 0) {
6384 + err = opaque;
6385 + goto out_free;
6386 + } else if (opaque) {
6387 + dbend(dentry) = dbopaque(dentry) = bindex;
6388 + break;
6389 + }
6390 + dbend(dentry) = bindex;
6391 +
6392 + /* update parent directory's atime with the bindex */
6393 + fsstack_copy_attr_atime(parent->d_inode,
6394 + lower_dir_dentry->d_inode);
6395 + }
6396 +
6397 + /* sanity checks, then decide if to process a negative dentry */
6398 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6399 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6400 +
6401 + if (num_positive > 0)
6402 + goto out_positive;
6403 +
6404 + /*** handle NEGATIVE dentries ***/
6405 +
6406 + /*
6407 + * If negative, keep only first lower negative dentry, to save on
6408 + * memory.
6409 + */
6410 + if (dbstart(dentry) < dbend(dentry)) {
6411 + path_put_lowers(dentry, dbstart(dentry) + 1,
6412 + dbend(dentry), false);
6413 + dbend(dentry) = dbstart(dentry);
6414 + }
6415 + if (lookupmode == INTERPOSE_PARTIAL)
6416 + goto out;
6417 + if (lookupmode == INTERPOSE_LOOKUP) {
6418 + /*
6419 + * If all we found was a whiteout in the first available
6420 + * branch, then create a negative dentry for a possibly new
6421 + * file to be created.
6422 + */
6423 + if (dbopaque(dentry) < 0)
6424 + goto out;
6425 + /* XXX: need to get mnt here */
6426 + bindex = dbstart(dentry);
6427 + if (unionfs_lower_dentry_idx(dentry, bindex))
6428 + goto out;
6429 + lower_dir_dentry =
6430 + unionfs_lower_dentry_idx(parent, bindex);
6431 + if (!lower_dir_dentry || !lower_dir_dentry->d_inode)
6432 + goto out;
6433 + if (!S_ISDIR(lower_dir_dentry->d_inode->i_mode))
6434 + goto out; /* XXX: should be BUG_ON */
6435 + /* XXX: do we need to cross bind mounts here? */
6436 + lower_dentry = lookup_lck_len(name, lower_dir_dentry, namelen);
6437 + if (IS_ERR(lower_dentry)) {
6438 + err = PTR_ERR(lower_dentry);
6439 + goto out;
6440 + }
6441 + /* XXX: need to mntget/mntput as needed too! */
6442 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
6443 + /* XXX: wrong mnt for crossing bind mounts! */
6444 + lower_mnt = unionfs_mntget(dentry->d_sb->s_root, bindex);
6445 + unionfs_set_lower_mnt_idx(dentry, bindex, lower_mnt);
6446 +
6447 + goto out;
6448 + }
6449 +
6450 + /* if we're revalidating a positive dentry, don't make it negative */
6451 + if (lookupmode != INTERPOSE_REVAL)
6452 + d_add(dentry, NULL);
6453 +
6454 + goto out;
6455 +
6456 +out_positive:
6457 + /*** handle POSITIVE dentries ***/
6458 +
6459 + /*
6460 + * This unionfs dentry is positive (at least one lower inode
6461 + * exists), so scan entire dentry from beginning to end, and remove
6462 + * any negative lower dentries, if any. Then, update dbstart/dbend
6463 + * to reflect the start/end of positive dentries.
6464 + */
6465 + pos_start = pos_end = -1;
6466 + for (bindex = bstart; bindex <= bend; bindex++) {
6467 + lower_dentry = unionfs_lower_dentry_idx(dentry,
6468 + bindex);
6469 + if (lower_dentry && lower_dentry->d_inode) {
6470 + if (pos_start < 0)
6471 + pos_start = bindex;
6472 + if (bindex > pos_end)
6473 + pos_end = bindex;
6474 + continue;
6475 + }
6476 + path_put_lowers(dentry, bindex, bindex, false);
6477 + }
6478 + if (pos_start >= 0)
6479 + dbstart(dentry) = pos_start;
6480 + if (pos_end >= 0)
6481 + dbend(dentry) = pos_end;
6482 +
6483 + /* Partial lookups need to re-interpose, or throw away older negs. */
6484 + if (lookupmode == INTERPOSE_PARTIAL) {
6485 + if (dentry->d_inode) {
6486 + unionfs_reinterpose(dentry);
6487 + goto out;
6488 + }
6489 +
6490 + /*
6491 + * This dentry was positive, so it is as if we had a
6492 + * negative revalidation.
6493 + */
6494 + lookupmode = INTERPOSE_REVAL_NEG;
6495 + update_bstart(dentry);
6496 + }
6497 +
6498 + /*
6499 + * Interpose can return a dentry if d_splice returned a different
6500 + * dentry.
6501 + */
6502 + d_interposed = unionfs_interpose(dentry, dentry->d_sb, lookupmode);
6503 + if (IS_ERR(d_interposed))
6504 + err = PTR_ERR(d_interposed);
6505 + else if (d_interposed)
6506 + dentry = d_interposed;
6507 +
6508 + if (!err)
6509 + goto out;
6510 + d_drop(dentry);
6511 +
6512 +out_free:
6513 + /* should dput/mntput all the underlying dentries on error condition */
6514 + if (dbstart(dentry) >= 0)
6515 + path_put_lowers_all(dentry, false);
6516 + /* free lower_paths unconditionally */
6517 + kfree(UNIONFS_D(dentry)->lower_paths);
6518 + UNIONFS_D(dentry)->lower_paths = NULL;
6519 +
6520 +out:
6521 + if (dentry && UNIONFS_D(dentry)) {
6522 + BUG_ON(dbstart(dentry) < 0 && dbend(dentry) >= 0);
6523 + BUG_ON(dbstart(dentry) >= 0 && dbend(dentry) < 0);
6524 + }
6525 + if (d_interposed && UNIONFS_D(d_interposed)) {
6526 + BUG_ON(dbstart(d_interposed) < 0 && dbend(d_interposed) >= 0);
6527 + BUG_ON(dbstart(d_interposed) >= 0 && dbend(d_interposed) < 0);
6528 + }
6529 +
6530 + if (!err && d_interposed)
6531 + return d_interposed;
6532 + return ERR_PTR(err);
6533 +}
6534 diff --git a/fs/unionfs/main.c b/fs/unionfs/main.c
6535 new file mode 100644
6536 index 0000000..c58405c
6537 --- /dev/null
6538 +++ b/fs/unionfs/main.c
6539 @@ -0,0 +1,758 @@
6540 +/*
6541 + * Copyright (c) 2003-2009 Erez Zadok
6542 + * Copyright (c) 2003-2006 Charles P. Wright
6543 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
6544 + * Copyright (c) 2005-2006 Junjiro Okajima
6545 + * Copyright (c) 2005 Arun M. Krishnakumar
6546 + * Copyright (c) 2004-2006 David P. Quigley
6547 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
6548 + * Copyright (c) 2003 Puja Gupta
6549 + * Copyright (c) 2003 Harikesavan Krishnan
6550 + * Copyright (c) 2003-2009 Stony Brook University
6551 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
6552 + *
6553 + * This program is free software; you can redistribute it and/or modify
6554 + * it under the terms of the GNU General Public License version 2 as
6555 + * published by the Free Software Foundation.
6556 + */
6557 +
6558 +#include "union.h"
6559 +#include <linux/module.h>
6560 +#include <linux/moduleparam.h>
6561 +
6562 +static void unionfs_fill_inode(struct dentry *dentry,
6563 + struct inode *inode)
6564 +{
6565 + struct inode *lower_inode;
6566 + struct dentry *lower_dentry;
6567 + int bindex, bstart, bend;
6568 +
6569 + bstart = dbstart(dentry);
6570 + bend = dbend(dentry);
6571 +
6572 + for (bindex = bstart; bindex <= bend; bindex++) {
6573 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6574 + if (!lower_dentry) {
6575 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
6576 + continue;
6577 + }
6578 +
6579 + /* Initialize the lower inode to the new lower inode. */
6580 + if (!lower_dentry->d_inode)
6581 + continue;
6582 +
6583 + unionfs_set_lower_inode_idx(inode, bindex,
6584 + igrab(lower_dentry->d_inode));
6585 + }
6586 +
6587 + ibstart(inode) = dbstart(dentry);
6588 + ibend(inode) = dbend(dentry);
6589 +
6590 + /* Use attributes from the first branch. */
6591 + lower_inode = unionfs_lower_inode(inode);
6592 +
6593 + /* Use different set of inode ops for symlinks & directories */
6594 + if (S_ISLNK(lower_inode->i_mode))
6595 + inode->i_op = &unionfs_symlink_iops;
6596 + else if (S_ISDIR(lower_inode->i_mode))
6597 + inode->i_op = &unionfs_dir_iops;
6598 +
6599 + /* Use different set of file ops for directories */
6600 + if (S_ISDIR(lower_inode->i_mode))
6601 + inode->i_fop = &unionfs_dir_fops;
6602 +
6603 + /* properly initialize special inodes */
6604 + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
6605 + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
6606 + init_special_inode(inode, lower_inode->i_mode,
6607 + lower_inode->i_rdev);
6608 +
6609 + /* all well, copy inode attributes */
6610 + unionfs_copy_attr_all(inode, lower_inode);
6611 + fsstack_copy_inode_size(inode, lower_inode);
6612 +}
6613 +
6614 +/*
6615 + * Connect a unionfs inode dentry/inode with several lower ones. This is
6616 + * the classic stackable file system "vnode interposition" action.
6617 + *
6618 + * @sb: unionfs's super_block
6619 + */
6620 +struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
6621 + int flag)
6622 +{
6623 + int err = 0;
6624 + struct inode *inode;
6625 + int need_fill_inode = 1;
6626 + struct dentry *spliced = NULL;
6627 +
6628 + verify_locked(dentry);
6629 +
6630 + /*
6631 + * We allocate our new inode below by calling unionfs_iget,
6632 + * which will initialize some of the new inode's fields
6633 + */
6634 +
6635 + /*
6636 + * On revalidate we've already got our own inode and just need
6637 + * to fix it up.
6638 + */
6639 + if (flag == INTERPOSE_REVAL) {
6640 + inode = dentry->d_inode;
6641 + UNIONFS_I(inode)->bstart = -1;
6642 + UNIONFS_I(inode)->bend = -1;
6643 + atomic_set(&UNIONFS_I(inode)->generation,
6644 + atomic_read(&UNIONFS_SB(sb)->generation));
6645 +
6646 + UNIONFS_I(inode)->lower_inodes =
6647 + kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
6648 + if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
6649 + err = -ENOMEM;
6650 + goto out;
6651 + }
6652 + } else {
6653 + /* get unique inode number for unionfs */
6654 + inode = unionfs_iget(sb, iunique(sb, UNIONFS_ROOT_INO));
6655 + if (IS_ERR(inode)) {
6656 + err = PTR_ERR(inode);
6657 + goto out;
6658 + }
6659 + if (atomic_read(&inode->i_count) > 1)
6660 + goto skip;
6661 + }
6662 +
6663 + need_fill_inode = 0;
6664 + unionfs_fill_inode(dentry, inode);
6665 +
6666 +skip:
6667 + /* only (our) lookup wants to do a d_add */
6668 + switch (flag) {
6669 + case INTERPOSE_DEFAULT:
6670 + /* for operations which create new inodes */
6671 + d_add(dentry, inode);
6672 + break;
6673 + case INTERPOSE_REVAL_NEG:
6674 + d_instantiate(dentry, inode);
6675 + break;
6676 + case INTERPOSE_LOOKUP:
6677 + spliced = d_splice_alias(inode, dentry);
6678 + if (spliced && spliced != dentry) {
6679 + /*
6680 + * d_splice can return a dentry if it was
6681 + * disconnected and had to be moved. We must ensure
6682 + * that the private data of the new dentry is
6683 + * correct and that the inode info was filled
6684 + * properly. Finally we must return this new
6685 + * dentry.
6686 + */
6687 + spliced->d_op = &unionfs_dops;
6688 + spliced->d_fsdata = dentry->d_fsdata;
6689 + dentry->d_fsdata = NULL;
6690 + dentry = spliced;
6691 + if (need_fill_inode) {
6692 + need_fill_inode = 0;
6693 + unionfs_fill_inode(dentry, inode);
6694 + }
6695 + goto out_spliced;
6696 + } else if (!spliced) {
6697 + if (need_fill_inode) {
6698 + need_fill_inode = 0;
6699 + unionfs_fill_inode(dentry, inode);
6700 + goto out_spliced;
6701 + }
6702 + }
6703 + break;
6704 + case INTERPOSE_REVAL:
6705 + /* Do nothing. */
6706 + break;
6707 + default:
6708 + printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
6709 + BUG();
6710 + }
6711 + goto out;
6712 +
6713 +out_spliced:
6714 + if (!err)
6715 + return spliced;
6716 +out:
6717 + return ERR_PTR(err);
6718 +}
6719 +
6720 +/* like interpose above, but for an already existing dentry */
6721 +void unionfs_reinterpose(struct dentry *dentry)
6722 +{
6723 + struct dentry *lower_dentry;
6724 + struct inode *inode;
6725 + int bindex, bstart, bend;
6726 +
6727 + verify_locked(dentry);
6728 +
6729 + /* This is pre-allocated inode */
6730 + inode = dentry->d_inode;
6731 +
6732 + bstart = dbstart(dentry);
6733 + bend = dbend(dentry);
6734 + for (bindex = bstart; bindex <= bend; bindex++) {
6735 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
6736 + if (!lower_dentry)
6737 + continue;
6738 +
6739 + if (!lower_dentry->d_inode)
6740 + continue;
6741 + if (unionfs_lower_inode_idx(inode, bindex))
6742 + continue;
6743 + unionfs_set_lower_inode_idx(inode, bindex,
6744 + igrab(lower_dentry->d_inode));
6745 + }
6746 + ibstart(inode) = dbstart(dentry);
6747 + ibend(inode) = dbend(dentry);
6748 +}
6749 +
6750 +/*
6751 + * make sure the branch we just looked up (nd) makes sense:
6752 + *
6753 + * 1) we're not trying to stack unionfs on top of unionfs
6754 + * 2) it exists
6755 + * 3) is a directory
6756 + */
6757 +int check_branch(struct nameidata *nd)
6758 +{
6759 + /* XXX: remove in ODF code -- stacking unions allowed there */
6760 + if (!strcmp(nd->path.dentry->d_sb->s_type->name, UNIONFS_NAME))
6761 + return -EINVAL;
6762 + if (!nd->path.dentry->d_inode)
6763 + return -ENOENT;
6764 + if (!S_ISDIR(nd->path.dentry->d_inode->i_mode))
6765 + return -ENOTDIR;
6766 + return 0;
6767 +}
6768 +
6769 +/* checks if two lower_dentries have overlapping branches */
6770 +static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
6771 +{
6772 + struct dentry *dent = NULL;
6773 +
6774 + dent = dent1;
6775 + while ((dent != dent2) && (dent->d_parent != dent))
6776 + dent = dent->d_parent;
6777 +
6778 + if (dent == dent2)
6779 + return 1;
6780 +
6781 + dent = dent2;
6782 + while ((dent != dent1) && (dent->d_parent != dent))
6783 + dent = dent->d_parent;
6784 +
6785 + return (dent == dent1);
6786 +}
6787 +
6788 +/*
6789 + * Parse "ro" or "rw" options, but default to "rw" if no mode options was
6790 + * specified. Fill the mode bits in @perms. If encounter an unknown
6791 + * string, return -EINVAL. Otherwise return 0.
6792 + */
6793 +int parse_branch_mode(const char *name, int *perms)
6794 +{
6795 + if (!name || !strcmp(name, "rw")) {
6796 + *perms = MAY_READ | MAY_WRITE;
6797 + return 0;
6798 + }
6799 + if (!strcmp(name, "ro")) {
6800 + *perms = MAY_READ;
6801 + return 0;
6802 + }
6803 + return -EINVAL;
6804 +}
6805 +
6806 +/*
6807 + * parse the dirs= mount argument
6808 + *
6809 + * We don't need to lock the superblock private data's rwsem, as we get
6810 + * called only by unionfs_read_super - it is still a long time before anyone
6811 + * can even get a reference to us.
6812 + */
6813 +static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
6814 + *lower_root_info, char *options)
6815 +{
6816 + struct nameidata nd;
6817 + char *name;
6818 + int err = 0;
6819 + int branches = 1;
6820 + int bindex = 0;
6821 + int i = 0;
6822 + int j = 0;
6823 + struct dentry *dent1;
6824 + struct dentry *dent2;
6825 +
6826 + if (options[0] == '\0') {
6827 + printk(KERN_ERR "unionfs: no branches specified\n");
6828 + err = -EINVAL;
6829 + goto out;
6830 + }
6831 +
6832 + /*
6833 + * Each colon means we have a separator, this is really just a rough
6834 + * guess, since strsep will handle empty fields for us.
6835 + */
6836 + for (i = 0; options[i]; i++)
6837 + if (options[i] == ':')
6838 + branches++;
6839 +
6840 + /* allocate space for underlying pointers to lower dentry */
6841 + UNIONFS_SB(sb)->data =
6842 + kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
6843 + if (unlikely(!UNIONFS_SB(sb)->data)) {
6844 + err = -ENOMEM;
6845 + goto out;
6846 + }
6847 +
6848 + lower_root_info->lower_paths =
6849 + kcalloc(branches, sizeof(struct path), GFP_KERNEL);
6850 + if (unlikely(!lower_root_info->lower_paths)) {
6851 + err = -ENOMEM;
6852 + goto out;
6853 + }
6854 +
6855 + /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
6856 + branches = 0;
6857 + while ((name = strsep(&options, ":")) != NULL) {
6858 + int perms;
6859 + char *mode = strchr(name, '=');
6860 +
6861 + if (!name)
6862 + continue;
6863 + if (!*name) { /* bad use of ':' (extra colons) */
6864 + err = -EINVAL;
6865 + goto out;
6866 + }
6867 +
6868 + branches++;
6869 +
6870 + /* strip off '=' if any */
6871 + if (mode)
6872 + *mode++ = '\0';
6873 +
6874 + err = parse_branch_mode(mode, &perms);
6875 + if (err) {
6876 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
6877 + "branch %d\n", mode, bindex);
6878 + goto out;
6879 + }
6880 + /* ensure that leftmost branch is writeable */
6881 + if (!bindex && !(perms & MAY_WRITE)) {
6882 + printk(KERN_ERR "unionfs: leftmost branch cannot be "
6883 + "read-only (use \"-o ro\" to create a "
6884 + "read-only union)\n");
6885 + err = -EINVAL;
6886 + goto out;
6887 + }
6888 +
6889 + err = path_lookup(name, LOOKUP_FOLLOW, &nd);
6890 + if (err) {
6891 + printk(KERN_ERR "unionfs: error accessing "
6892 + "lower directory '%s' (error %d)\n",
6893 + name, err);
6894 + goto out;
6895 + }
6896 +
6897 + err = check_branch(&nd);
6898 + if (err) {
6899 + printk(KERN_ERR "unionfs: lower directory "
6900 + "'%s' is not a valid branch\n", name);
6901 + path_put(&nd.path);
6902 + goto out;
6903 + }
6904 +
6905 + lower_root_info->lower_paths[bindex].dentry = nd.path.dentry;
6906 + lower_root_info->lower_paths[bindex].mnt = nd.path.mnt;
6907 +
6908 + set_branchperms(sb, bindex, perms);
6909 + set_branch_count(sb, bindex, 0);
6910 + new_branch_id(sb, bindex);
6911 +
6912 + if (lower_root_info->bstart < 0)
6913 + lower_root_info->bstart = bindex;
6914 + lower_root_info->bend = bindex;
6915 + bindex++;
6916 + }
6917 +
6918 + if (branches == 0) {
6919 + printk(KERN_ERR "unionfs: no branches specified\n");
6920 + err = -EINVAL;
6921 + goto out;
6922 + }
6923 +
6924 + BUG_ON(branches != (lower_root_info->bend + 1));
6925 +
6926 + /*
6927 + * Ensure that no overlaps exist in the branches.
6928 + *
6929 + * This test is required because the Linux kernel has no support
6930 + * currently for ensuring coherency between stackable layers and
6931 + * branches. If we were to allow overlapping branches, it would be
6932 + * possible, for example, to delete a file via one branch, which
6933 + * would not be reflected in another branch. Such incoherency could
6934 + * lead to inconsistencies and even kernel oopses. Rather than
6935 + * implement hacks to work around some of these cache-coherency
6936 + * problems, we prevent branch overlapping, for now. A complete
6937 + * solution will involve proper kernel/VFS support for cache
6938 + * coherency, at which time we could safely remove this
6939 + * branch-overlapping test.
6940 + */
6941 + for (i = 0; i < branches; i++) {
6942 + dent1 = lower_root_info->lower_paths[i].dentry;
6943 + for (j = i + 1; j < branches; j++) {
6944 + dent2 = lower_root_info->lower_paths[j].dentry;
6945 + if (is_branch_overlap(dent1, dent2)) {
6946 + printk(KERN_ERR "unionfs: branches %d and "
6947 + "%d overlap\n", i, j);
6948 + err = -EINVAL;
6949 + goto out;
6950 + }
6951 + }
6952 + }
6953 +
6954 +out:
6955 + if (err) {
6956 + for (i = 0; i < branches; i++)
6957 + path_put(&lower_root_info->lower_paths[i]);
6958 +
6959 + kfree(lower_root_info->lower_paths);
6960 + kfree(UNIONFS_SB(sb)->data);
6961 +
6962 + /*
6963 + * MUST clear the pointers to prevent potential double free if
6964 + * the caller dies later on
6965 + */
6966 + lower_root_info->lower_paths = NULL;
6967 + UNIONFS_SB(sb)->data = NULL;
6968 + }
6969 + return err;
6970 +}
6971 +
6972 +/*
6973 + * Parse mount options. See the manual page for usage instructions.
6974 + *
6975 + * Returns the dentry object of the lower-level (lower) directory;
6976 + * We want to mount our stackable file system on top of that lower directory.
6977 + */
6978 +static struct unionfs_dentry_info *unionfs_parse_options(
6979 + struct super_block *sb,
6980 + char *options)
6981 +{
6982 + struct unionfs_dentry_info *lower_root_info;
6983 + char *optname;
6984 + int err = 0;
6985 + int bindex;
6986 + int dirsfound = 0;
6987 +
6988 + /* allocate private data area */
6989 + err = -ENOMEM;
6990 + lower_root_info =
6991 + kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
6992 + if (unlikely(!lower_root_info))
6993 + goto out_error;
6994 + lower_root_info->bstart = -1;
6995 + lower_root_info->bend = -1;
6996 + lower_root_info->bopaque = -1;
6997 +
6998 + while ((optname = strsep(&options, ",")) != NULL) {
6999 + char *optarg;
7000 +
7001 + if (!optname || !*optname)
7002 + continue;
7003 +
7004 + optarg = strchr(optname, '=');
7005 + if (optarg)
7006 + *optarg++ = '\0';
7007 +
7008 + /*
7009 + * All of our options take an argument now. Insert ones that
7010 + * don't, above this check.
7011 + */
7012 + if (!optarg) {
7013 + printk(KERN_ERR "unionfs: %s requires an argument\n",
7014 + optname);
7015 + err = -EINVAL;
7016 + goto out_error;
7017 + }
7018 +
7019 + if (!strcmp("dirs", optname)) {
7020 + if (++dirsfound > 1) {
7021 + printk(KERN_ERR
7022 + "unionfs: multiple dirs specified\n");
7023 + err = -EINVAL;
7024 + goto out_error;
7025 + }
7026 + err = parse_dirs_option(sb, lower_root_info, optarg);
7027 + if (err)
7028 + goto out_error;
7029 + continue;
7030 + }
7031 +
7032 + err = -EINVAL;
7033 + printk(KERN_ERR
7034 + "unionfs: unrecognized option '%s'\n", optname);
7035 + goto out_error;
7036 + }
7037 + if (dirsfound != 1) {
7038 + printk(KERN_ERR "unionfs: dirs option required\n");
7039 + err = -EINVAL;
7040 + goto out_error;
7041 + }
7042 + goto out;
7043 +
7044 +out_error:
7045 + if (lower_root_info && lower_root_info->lower_paths) {
7046 + for (bindex = lower_root_info->bstart;
7047 + bindex >= 0 && bindex <= lower_root_info->bend;
7048 + bindex++)
7049 + path_put(&lower_root_info->lower_paths[bindex]);
7050 + }
7051 +
7052 + kfree(lower_root_info->lower_paths);
7053 + kfree(lower_root_info);
7054 +
7055 + kfree(UNIONFS_SB(sb)->data);
7056 + UNIONFS_SB(sb)->data = NULL;
7057 +
7058 + lower_root_info = ERR_PTR(err);
7059 +out:
7060 + return lower_root_info;
7061 +}
7062 +
7063 +/*
7064 + * our custom d_alloc_root work-alike
7065 + *
7066 + * we can't use d_alloc_root if we want to use our own interpose function
7067 + * unchanged, so we simply call our own "fake" d_alloc_root
7068 + */
7069 +static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
7070 +{
7071 + struct dentry *ret = NULL;
7072 +
7073 + if (sb) {
7074 + static const struct qstr name = {
7075 + .name = "/",
7076 + .len = 1
7077 + };
7078 +
7079 + ret = d_alloc(NULL, &name);
7080 + if (likely(ret)) {
7081 + ret->d_op = &unionfs_dops;
7082 + ret->d_sb = sb;
7083 + ret->d_parent = ret;
7084 + }
7085 + }
7086 + return ret;
7087 +}
7088 +
7089 +/*
7090 + * There is no need to lock the unionfs_super_info's rwsem as there is no
7091 + * way anyone can have a reference to the superblock at this point in time.
7092 + */
7093 +static int unionfs_read_super(struct super_block *sb, void *raw_data,
7094 + int silent)
7095 +{
7096 + int err = 0;
7097 + struct unionfs_dentry_info *lower_root_info = NULL;
7098 + int bindex, bstart, bend;
7099 +
7100 + if (!raw_data) {
7101 + printk(KERN_ERR
7102 + "unionfs: read_super: missing data argument\n");
7103 + err = -EINVAL;
7104 + goto out;
7105 + }
7106 +
7107 + /* Allocate superblock private data */
7108 + sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
7109 + if (unlikely(!UNIONFS_SB(sb))) {
7110 + printk(KERN_CRIT "unionfs: read_super: out of memory\n");
7111 + err = -ENOMEM;
7112 + goto out;
7113 + }
7114 +
7115 + UNIONFS_SB(sb)->bend = -1;
7116 + atomic_set(&UNIONFS_SB(sb)->generation, 1);
7117 + init_rwsem(&UNIONFS_SB(sb)->rwsem);
7118 + UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
7119 +
7120 + lower_root_info = unionfs_parse_options(sb, raw_data);
7121 + if (IS_ERR(lower_root_info)) {
7122 + printk(KERN_ERR
7123 + "unionfs: read_super: error while parsing options "
7124 + "(err = %ld)\n", PTR_ERR(lower_root_info));
7125 + err = PTR_ERR(lower_root_info);
7126 + lower_root_info = NULL;
7127 + goto out_free;
7128 + }
7129 + if (lower_root_info->bstart == -1) {
7130 + err = -ENOENT;
7131 + goto out_free;
7132 + }
7133 +
7134 + /* set the lower superblock field of upper superblock */
7135 + bstart = lower_root_info->bstart;
7136 + BUG_ON(bstart != 0);
7137 + sbend(sb) = bend = lower_root_info->bend;
7138 + for (bindex = bstart; bindex <= bend; bindex++) {
7139 + struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
7140 + atomic_inc(&d->d_sb->s_active);
7141 + unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
7142 + }
7143 +
7144 + /* max Bytes is the maximum bytes from highest priority branch */
7145 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
7146 +
7147 + /*
7148 + * Our c/m/atime granularity is 1 ns because we may stack on file
7149 + * systems whose granularity is as good. This is important for our
7150 + * time-based cache coherency.
7151 + */
7152 + sb->s_time_gran = 1;
7153 +
7154 + sb->s_op = &unionfs_sops;
7155 +
7156 + /* See comment next to the definition of unionfs_d_alloc_root */
7157 + sb->s_root = unionfs_d_alloc_root(sb);
7158 + if (unlikely(!sb->s_root)) {
7159 + err = -ENOMEM;
7160 + goto out_dput;
7161 + }
7162 +
7163 + /* link the upper and lower dentries */
7164 + sb->s_root->d_fsdata = NULL;
7165 + err = new_dentry_private_data(sb->s_root, UNIONFS_DMUTEX_ROOT);
7166 + if (unlikely(err))
7167 + goto out_freedpd;
7168 +
7169 + /* Set the lower dentries for s_root */
7170 + for (bindex = bstart; bindex <= bend; bindex++) {
7171 + struct dentry *d;
7172 + struct vfsmount *m;
7173 +
7174 + d = lower_root_info->lower_paths[bindex].dentry;
7175 + m = lower_root_info->lower_paths[bindex].mnt;
7176 +
7177 + unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
7178 + unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
7179 + }
7180 + dbstart(sb->s_root) = bstart;
7181 + dbend(sb->s_root) = bend;
7182 +
7183 + /* Set the generation number to one, since this is for the mount. */
7184 + atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
7185 +
7186 + /*
7187 + * Call interpose to create the upper level inode. Only
7188 + * INTERPOSE_LOOKUP can return a value other than 0 on err.
7189 + */
7190 + err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
7191 + unionfs_unlock_dentry(sb->s_root);
7192 + if (!err)
7193 + goto out;
7194 + /* else fall through */
7195 +
7196 +out_freedpd:
7197 + if (UNIONFS_D(sb->s_root)) {
7198 + kfree(UNIONFS_D(sb->s_root)->lower_paths);
7199 + free_dentry_private_data(sb->s_root);
7200 + }
7201 + dput(sb->s_root);
7202 +
7203 +out_dput:
7204 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7205 + for (bindex = lower_root_info->bstart;
7206 + bindex <= lower_root_info->bend; bindex++) {
7207 + struct dentry *d;
7208 + d = lower_root_info->lower_paths[bindex].dentry;
7209 + /* drop refs we took earlier */
7210 + atomic_dec(&d->d_sb->s_active);
7211 + path_put(&lower_root_info->lower_paths[bindex]);
7212 + }
7213 + kfree(lower_root_info->lower_paths);
7214 + kfree(lower_root_info);
7215 + lower_root_info = NULL;
7216 + }
7217 +
7218 +out_free:
7219 + kfree(UNIONFS_SB(sb)->data);
7220 + kfree(UNIONFS_SB(sb));
7221 + sb->s_fs_info = NULL;
7222 +
7223 +out:
7224 + if (lower_root_info && !IS_ERR(lower_root_info)) {
7225 + kfree(lower_root_info->lower_paths);
7226 + kfree(lower_root_info);
7227 + }
7228 + return err;
7229 +}
7230 +
7231 +static int unionfs_get_sb(struct file_system_type *fs_type,
7232 + int flags, const char *dev_name,
7233 + void *raw_data, struct vfsmount *mnt)
7234 +{
7235 + int err;
7236 + err = get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
7237 + if (!err)
7238 + UNIONFS_SB(mnt->mnt_sb)->dev_name =
7239 + kstrdup(dev_name, GFP_KERNEL);
7240 + return err;
7241 +}
7242 +
7243 +static struct file_system_type unionfs_fs_type = {
7244 + .owner = THIS_MODULE,
7245 + .name = UNIONFS_NAME,
7246 + .get_sb = unionfs_get_sb,
7247 + .kill_sb = generic_shutdown_super,
7248 + .fs_flags = FS_REVAL_DOT,
7249 +};
7250 +
7251 +static int __init init_unionfs_fs(void)
7252 +{
7253 + int err;
7254 +
7255 + pr_info("Registering unionfs " UNIONFS_VERSION "\n");
7256 +
7257 + err = unionfs_init_filldir_cache();
7258 + if (unlikely(err))
7259 + goto out;
7260 + err = unionfs_init_inode_cache();
7261 + if (unlikely(err))
7262 + goto out;
7263 + err = unionfs_init_dentry_cache();
7264 + if (unlikely(err))
7265 + goto out;
7266 + err = init_sioq();
7267 + if (unlikely(err))
7268 + goto out;
7269 + err = register_filesystem(&unionfs_fs_type);
7270 +out:
7271 + if (unlikely(err)) {
7272 + stop_sioq();
7273 + unionfs_destroy_filldir_cache();
7274 + unionfs_destroy_inode_cache();
7275 + unionfs_destroy_dentry_cache();
7276 + }
7277 + return err;
7278 +}
7279 +
7280 +static void __exit exit_unionfs_fs(void)
7281 +{
7282 + stop_sioq();
7283 + unionfs_destroy_filldir_cache();
7284 + unionfs_destroy_inode_cache();
7285 + unionfs_destroy_dentry_cache();
7286 + unregister_filesystem(&unionfs_fs_type);
7287 + pr_info("Completed unionfs module unload\n");
7288 +}
7289 +
7290 +MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
7291 + " (http://www.fsl.cs.sunysb.edu)");
7292 +MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
7293 + " (http://unionfs.filesystems.org)");
7294 +MODULE_LICENSE("GPL");
7295 +
7296 +module_init(init_unionfs_fs);
7297 +module_exit(exit_unionfs_fs);
7298 diff --git a/fs/unionfs/mmap.c b/fs/unionfs/mmap.c
7299 new file mode 100644
7300 index 0000000..18b05d5
7301 --- /dev/null
7302 +++ b/fs/unionfs/mmap.c
7303 @@ -0,0 +1,89 @@
7304 +/*
7305 + * Copyright (c) 2003-2009 Erez Zadok
7306 + * Copyright (c) 2003-2006 Charles P. Wright
7307 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7308 + * Copyright (c) 2005-2006 Junjiro Okajima
7309 + * Copyright (c) 2006 Shaya Potter
7310 + * Copyright (c) 2005 Arun M. Krishnakumar
7311 + * Copyright (c) 2004-2006 David P. Quigley
7312 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7313 + * Copyright (c) 2003 Puja Gupta
7314 + * Copyright (c) 2003 Harikesavan Krishnan
7315 + * Copyright (c) 2003-2009 Stony Brook University
7316 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7317 + *
7318 + * This program is free software; you can redistribute it and/or modify
7319 + * it under the terms of the GNU General Public License version 2 as
7320 + * published by the Free Software Foundation.
7321 + */
7322 +
7323 +#include "union.h"
7324 +
7325 +
7326 +/*
7327 + * XXX: we need a dummy readpage handler because generic_file_mmap (which we
7328 + * use in unionfs_mmap) checks for the existence of
7329 + * mapping->a_ops->readpage, else it returns -ENOEXEC. The VFS will need to
7330 + * be fixed to allow a file system to define vm_ops->fault without any
7331 + * address_space_ops whatsoever.
7332 + *
7333 + * Otherwise, we don't want to use our readpage method at all.
7334 + */
7335 +static int unionfs_readpage(struct file *file, struct page *page)
7336 +{
7337 + BUG();
7338 + return -EINVAL;
7339 +}
7340 +
7341 +static int unionfs_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
7342 +{
7343 + int err;
7344 + struct file *file, *lower_file;
7345 + struct vm_operations_struct *lower_vm_ops;
7346 + struct vm_area_struct lower_vma;
7347 +
7348 + BUG_ON(!vma);
7349 + memcpy(&lower_vma, vma, sizeof(struct vm_area_struct));
7350 + file = lower_vma.vm_file;
7351 + lower_vm_ops = UNIONFS_F(file)->lower_vm_ops;
7352 + BUG_ON(!lower_vm_ops);
7353 +
7354 + lower_file = unionfs_lower_file(file);
7355 + BUG_ON(!lower_file);
7356 + /*
7357 + * XXX: vm_ops->fault may be called in parallel. Because we have to
7358 + * resort to temporarily changing the vma->vm_file to point to the
7359 + * lower file, a concurrent invocation of unionfs_fault could see a
7360 + * different value. In this workaround, we keep a different copy of
7361 + * the vma structure in our stack, so we never expose a different
7362 + * value of the vma->vm_file called to us, even temporarily. A
7363 + * better fix would be to change the calling semantics of ->fault to
7364 + * take an explicit file pointer.
7365 + */
7366 + lower_vma.vm_file = lower_file;
7367 + err = lower_vm_ops->fault(&lower_vma, vmf);
7368 + return err;
7369 +}
7370 +
7371 +/*
7372 + * XXX: the default address_space_ops for unionfs is empty. We cannot set
7373 + * our inode->i_mapping->a_ops to NULL because too many code paths expect
7374 + * the a_ops vector to be non-NULL.
7375 + */
7376 +struct address_space_operations unionfs_aops = {
7377 + /* empty on purpose */
7378 +};
7379 +
7380 +/*
7381 + * XXX: we need a second, dummy address_space_ops vector, to be used
7382 + * temporarily during unionfs_mmap, because the latter calls
7383 + * generic_file_mmap, which checks if ->readpage exists, else returns
7384 + * -ENOEXEC.
7385 + */
7386 +struct address_space_operations unionfs_dummy_aops = {
7387 + .readpage = unionfs_readpage,
7388 +};
7389 +
7390 +struct vm_operations_struct unionfs_vm_ops = {
7391 + .fault = unionfs_fault,
7392 +};
7393 diff --git a/fs/unionfs/rdstate.c b/fs/unionfs/rdstate.c
7394 new file mode 100644
7395 index 0000000..485464b
7396 --- /dev/null
7397 +++ b/fs/unionfs/rdstate.c
7398 @@ -0,0 +1,285 @@
7399 +/*
7400 + * Copyright (c) 2003-2009 Erez Zadok
7401 + * Copyright (c) 2003-2006 Charles P. Wright
7402 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7403 + * Copyright (c) 2005-2006 Junjiro Okajima
7404 + * Copyright (c) 2005 Arun M. Krishnakumar
7405 + * Copyright (c) 2004-2006 David P. Quigley
7406 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7407 + * Copyright (c) 2003 Puja Gupta
7408 + * Copyright (c) 2003 Harikesavan Krishnan
7409 + * Copyright (c) 2003-2009 Stony Brook University
7410 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7411 + *
7412 + * This program is free software; you can redistribute it and/or modify
7413 + * it under the terms of the GNU General Public License version 2 as
7414 + * published by the Free Software Foundation.
7415 + */
7416 +
7417 +#include "union.h"
7418 +
7419 +/* This file contains the routines for maintaining readdir state. */
7420 +
7421 +/*
7422 + * There are two structures here, rdstate which is a hash table
7423 + * of the second structure which is a filldir_node.
7424 + */
7425 +
7426 +/*
7427 + * This is a struct kmem_cache for filldir nodes, because we allocate a lot
7428 + * of them and they shouldn't waste memory. If the node has a small name
7429 + * (as defined by the dentry structure), then we use an inline name to
7430 + * preserve kmalloc space.
7431 + */
7432 +static struct kmem_cache *unionfs_filldir_cachep;
7433 +
7434 +int unionfs_init_filldir_cache(void)
7435 +{
7436 + unionfs_filldir_cachep =
7437 + kmem_cache_create("unionfs_filldir",
7438 + sizeof(struct filldir_node), 0,
7439 + SLAB_RECLAIM_ACCOUNT, NULL);
7440 +
7441 + return (unionfs_filldir_cachep ? 0 : -ENOMEM);
7442 +}
7443 +
7444 +void unionfs_destroy_filldir_cache(void)
7445 +{
7446 + if (unionfs_filldir_cachep)
7447 + kmem_cache_destroy(unionfs_filldir_cachep);
7448 +}
7449 +
7450 +/*
7451 + * This is a tuning parameter that tells us roughly how big to make the
7452 + * hash table in directory entries per page. This isn't perfect, but
7453 + * at least we get a hash table size that shouldn't be too overloaded.
7454 + * The following averages are based on my home directory.
7455 + * 14.44693 Overall
7456 + * 12.29 Single Page Directories
7457 + * 117.93 Multi-page directories
7458 + */
7459 +#define DENTPAGE 4096
7460 +#define DENTPERONEPAGE 12
7461 +#define DENTPERPAGE 118
7462 +#define MINHASHSIZE 1
7463 +static int guesstimate_hash_size(struct inode *inode)
7464 +{
7465 + struct inode *lower_inode;
7466 + int bindex;
7467 + int hashsize = MINHASHSIZE;
7468 +
7469 + if (UNIONFS_I(inode)->hashsize > 0)
7470 + return UNIONFS_I(inode)->hashsize;
7471 +
7472 + for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) {
7473 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
7474 + if (!lower_inode)
7475 + continue;
7476 +
7477 + if (i_size_read(lower_inode) == DENTPAGE)
7478 + hashsize += DENTPERONEPAGE;
7479 + else
7480 + hashsize += (i_size_read(lower_inode) / DENTPAGE) *
7481 + DENTPERPAGE;
7482 + }
7483 +
7484 + return hashsize;
7485 +}
7486 +
7487 +int init_rdstate(struct file *file)
7488 +{
7489 + BUG_ON(sizeof(loff_t) !=
7490 + (sizeof(unsigned int) + sizeof(unsigned int)));
7491 + BUG_ON(UNIONFS_F(file)->rdstate != NULL);
7492 +
7493 + UNIONFS_F(file)->rdstate = alloc_rdstate(file->f_path.dentry->d_inode,
7494 + fbstart(file));
7495 +
7496 + return (UNIONFS_F(file)->rdstate ? 0 : -ENOMEM);
7497 +}
7498 +
7499 +struct unionfs_dir_state *find_rdstate(struct inode *inode, loff_t fpos)
7500 +{
7501 + struct unionfs_dir_state *rdstate = NULL;
7502 + struct list_head *pos;
7503 +
7504 + spin_lock(&UNIONFS_I(inode)->rdlock);
7505 + list_for_each(pos, &UNIONFS_I(inode)->readdircache) {
7506 + struct unionfs_dir_state *r =
7507 + list_entry(pos, struct unionfs_dir_state, cache);
7508 + if (fpos == rdstate2offset(r)) {
7509 + UNIONFS_I(inode)->rdcount--;
7510 + list_del(&r->cache);
7511 + rdstate = r;
7512 + break;
7513 + }
7514 + }
7515 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7516 + return rdstate;
7517 +}
7518 +
7519 +struct unionfs_dir_state *alloc_rdstate(struct inode *inode, int bindex)
7520 +{
7521 + int i = 0;
7522 + int hashsize;
7523 + unsigned long mallocsize = sizeof(struct unionfs_dir_state);
7524 + struct unionfs_dir_state *rdstate;
7525 +
7526 + hashsize = guesstimate_hash_size(inode);
7527 + mallocsize += hashsize * sizeof(struct list_head);
7528 + mallocsize = __roundup_pow_of_two(mallocsize);
7529 +
7530 + /* This should give us about 500 entries anyway. */
7531 + if (mallocsize > PAGE_SIZE)
7532 + mallocsize = PAGE_SIZE;
7533 +
7534 + hashsize = (mallocsize - sizeof(struct unionfs_dir_state)) /
7535 + sizeof(struct list_head);
7536 +
7537 + rdstate = kmalloc(mallocsize, GFP_KERNEL);
7538 + if (unlikely(!rdstate))
7539 + return NULL;
7540 +
7541 + spin_lock(&UNIONFS_I(inode)->rdlock);
7542 + if (UNIONFS_I(inode)->cookie >= (MAXRDCOOKIE - 1))
7543 + UNIONFS_I(inode)->cookie = 1;
7544 + else
7545 + UNIONFS_I(inode)->cookie++;
7546 +
7547 + rdstate->cookie = UNIONFS_I(inode)->cookie;
7548 + spin_unlock(&UNIONFS_I(inode)->rdlock);
7549 + rdstate->offset = 1;
7550 + rdstate->access = jiffies;
7551 + rdstate->bindex = bindex;
7552 + rdstate->dirpos = 0;
7553 + rdstate->hashentries = 0;
7554 + rdstate->size = hashsize;
7555 + for (i = 0; i < rdstate->size; i++)
7556 + INIT_LIST_HEAD(&rdstate->list[i]);
7557 +
7558 + return rdstate;
7559 +}
7560 +
7561 +static void free_filldir_node(struct filldir_node *node)
7562 +{
7563 + if (node->namelen >= DNAME_INLINE_LEN_MIN)
7564 + kfree(node->name);
7565 + kmem_cache_free(unionfs_filldir_cachep, node);
7566 +}
7567 +
7568 +void free_rdstate(struct unionfs_dir_state *state)
7569 +{
7570 + struct filldir_node *tmp;
7571 + int i;
7572 +
7573 + for (i = 0; i < state->size; i++) {
7574 + struct list_head *head = &(state->list[i]);
7575 + struct list_head *pos, *n;
7576 +
7577 + /* traverse the list and deallocate space */
7578 + list_for_each_safe(pos, n, head) {
7579 + tmp = list_entry(pos, struct filldir_node, file_list);
7580 + list_del(&tmp->file_list);
7581 + free_filldir_node(tmp);
7582 + }
7583 + }
7584 +
7585 + kfree(state);
7586 +}
7587 +
7588 +struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
7589 + const char *name, int namelen,
7590 + int is_whiteout)
7591 +{
7592 + int index;
7593 + unsigned int hash;
7594 + struct list_head *head;
7595 + struct list_head *pos;
7596 + struct filldir_node *cursor = NULL;
7597 + int found = 0;
7598 +
7599 + BUG_ON(namelen <= 0);
7600 +
7601 + hash = full_name_hash(name, namelen);
7602 + index = hash % rdstate->size;
7603 +
7604 + head = &(rdstate->list[index]);
7605 + list_for_each(pos, head) {
7606 + cursor = list_entry(pos, struct filldir_node, file_list);
7607 +
7608 + if (cursor->namelen == namelen && cursor->hash == hash &&
7609 + !strncmp(cursor->name, name, namelen)) {
7610 + /*
7611 + * a duplicate exists, and hence no need to create
7612 + * entry to the list
7613 + */
7614 + found = 1;
7615 +
7616 + /*
7617 + * if a duplicate is found in this branch, and is
7618 + * not due to the caller looking for an entry to
7619 + * whiteout, then the file system may be corrupted.
7620 + */
7621 + if (unlikely(!is_whiteout &&
7622 + cursor->bindex == rdstate->bindex))
7623 + printk(KERN_ERR "unionfs: filldir: possible "
7624 + "I/O error: a file is duplicated "
7625 + "in the same branch %d: %s\n",
7626 + rdstate->bindex, cursor->name);
7627 + break;
7628 + }
7629 + }
7630 +
7631 + if (!found)
7632 + cursor = NULL;
7633 +
7634 + return cursor;
7635 +}
7636 +
7637 +int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
7638 + int namelen, int bindex, int whiteout)
7639 +{
7640 + struct filldir_node *new;
7641 + unsigned int hash;
7642 + int index;
7643 + int err = 0;
7644 + struct list_head *head;
7645 +
7646 + BUG_ON(namelen <= 0);
7647 +
7648 + hash = full_name_hash(name, namelen);
7649 + index = hash % rdstate->size;
7650 + head = &(rdstate->list[index]);
7651 +
7652 + new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
7653 + if (unlikely(!new)) {
7654 + err = -ENOMEM;
7655 + goto out;
7656 + }
7657 +
7658 + INIT_LIST_HEAD(&new->file_list);
7659 + new->namelen = namelen;
7660 + new->hash = hash;
7661 + new->bindex = bindex;
7662 + new->whiteout = whiteout;
7663 +
7664 + if (namelen < DNAME_INLINE_LEN_MIN) {
7665 + new->name = new->iname;
7666 + } else {
7667 + new->name = kmalloc(namelen + 1, GFP_KERNEL);
7668 + if (unlikely(!new->name)) {
7669 + kmem_cache_free(unionfs_filldir_cachep, new);
7670 + new = NULL;
7671 + goto out;
7672 + }
7673 + }
7674 +
7675 + memcpy(new->name, name, namelen);
7676 + new->name[namelen] = '\0';
7677 +
7678 + rdstate->hashentries++;
7679 +
7680 + list_add(&(new->file_list), head);
7681 +out:
7682 + return err;
7683 +}
7684 diff --git a/fs/unionfs/rename.c b/fs/unionfs/rename.c
7685 new file mode 100644
7686 index 0000000..ed13260
7687 --- /dev/null
7688 +++ b/fs/unionfs/rename.c
7689 @@ -0,0 +1,520 @@
7690 +/*
7691 + * Copyright (c) 2003-2009 Erez Zadok
7692 + * Copyright (c) 2003-2006 Charles P. Wright
7693 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
7694 + * Copyright (c) 2005-2006 Junjiro Okajima
7695 + * Copyright (c) 2005 Arun M. Krishnakumar
7696 + * Copyright (c) 2004-2006 David P. Quigley
7697 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
7698 + * Copyright (c) 2003 Puja Gupta
7699 + * Copyright (c) 2003 Harikesavan Krishnan
7700 + * Copyright (c) 2003-2009 Stony Brook University
7701 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
7702 + *
7703 + * This program is free software; you can redistribute it and/or modify
7704 + * it under the terms of the GNU General Public License version 2 as
7705 + * published by the Free Software Foundation.
7706 + */
7707 +
7708 +#include "union.h"
7709 +
7710 +/*
7711 + * This is a helper function for rename, used when rename ends up with hosed
7712 + * over dentries and we need to revert.
7713 + */
7714 +static int unionfs_refresh_lower_dentry(struct dentry *dentry,
7715 + struct dentry *parent, int bindex)
7716 +{
7717 + struct dentry *lower_dentry;
7718 + struct dentry *lower_parent;
7719 + int err = 0;
7720 +
7721 + verify_locked(dentry);
7722 +
7723 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
7724 +
7725 + BUG_ON(!S_ISDIR(lower_parent->d_inode->i_mode));
7726 +
7727 + lower_dentry = lookup_one_len(dentry->d_name.name, lower_parent,
7728 + dentry->d_name.len);
7729 + if (IS_ERR(lower_dentry)) {
7730 + err = PTR_ERR(lower_dentry);
7731 + goto out;
7732 + }
7733 +
7734 + dput(unionfs_lower_dentry_idx(dentry, bindex));
7735 + iput(unionfs_lower_inode_idx(dentry->d_inode, bindex));
7736 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex, NULL);
7737 +
7738 + if (!lower_dentry->d_inode) {
7739 + dput(lower_dentry);
7740 + unionfs_set_lower_dentry_idx(dentry, bindex, NULL);
7741 + } else {
7742 + unionfs_set_lower_dentry_idx(dentry, bindex, lower_dentry);
7743 + unionfs_set_lower_inode_idx(dentry->d_inode, bindex,
7744 + igrab(lower_dentry->d_inode));
7745 + }
7746 +
7747 +out:
7748 + return err;
7749 +}
7750 +
7751 +static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7752 + struct dentry *old_parent,
7753 + struct inode *new_dir, struct dentry *new_dentry,
7754 + struct dentry *new_parent,
7755 + int bindex)
7756 +{
7757 + int err = 0;
7758 + struct dentry *lower_old_dentry;
7759 + struct dentry *lower_new_dentry;
7760 + struct dentry *lower_old_dir_dentry;
7761 + struct dentry *lower_new_dir_dentry;
7762 + struct dentry *trap;
7763 +
7764 + lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7765 + lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
7766 +
7767 + if (!lower_new_dentry) {
7768 + lower_new_dentry =
7769 + create_parents(new_parent->d_inode,
7770 + new_dentry, new_dentry->d_name.name,
7771 + bindex);
7772 + if (IS_ERR(lower_new_dentry)) {
7773 + err = PTR_ERR(lower_new_dentry);
7774 + if (IS_COPYUP_ERR(err))
7775 + goto out;
7776 + printk(KERN_ERR "unionfs: error creating directory "
7777 + "tree for rename, bindex=%d err=%d\n",
7778 + bindex, err);
7779 + goto out;
7780 + }
7781 + }
7782 +
7783 + /* check for and remove whiteout, if any */
7784 + err = check_unlink_whiteout(new_dentry, lower_new_dentry, bindex);
7785 + if (err > 0) /* ignore if whiteout found and successfully removed */
7786 + err = 0;
7787 + if (err)
7788 + goto out;
7789 +
7790 + /* check of old_dentry branch is writable */
7791 + err = is_robranch_super(old_dentry->d_sb, bindex);
7792 + if (err)
7793 + goto out;
7794 +
7795 + dget(lower_old_dentry);
7796 + dget(lower_new_dentry);
7797 + lower_old_dir_dentry = dget_parent(lower_old_dentry);
7798 + lower_new_dir_dentry = dget_parent(lower_new_dentry);
7799 +
7800 + /* see Documentation/filesystems/unionfs/issues.txt */
7801 + lockdep_off();
7802 + trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7803 + /* source should not be ancenstor of target */
7804 + if (trap == lower_old_dentry) {
7805 + err = -EINVAL;
7806 + goto out_err_unlock;
7807 + }
7808 + /* target should not be ancenstor of source */
7809 + if (trap == lower_new_dentry) {
7810 + err = -ENOTEMPTY;
7811 + goto out_err_unlock;
7812 + }
7813 + err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
7814 + lower_new_dir_dentry->d_inode, lower_new_dentry);
7815 +out_err_unlock:
7816 + if (!err) {
7817 + /* update parent dir times */
7818 + fsstack_copy_attr_times(old_dir, lower_old_dir_dentry->d_inode);
7819 + fsstack_copy_attr_times(new_dir, lower_new_dir_dentry->d_inode);
7820 + }
7821 + unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
7822 + lockdep_on();
7823 +
7824 + dput(lower_old_dir_dentry);
7825 + dput(lower_new_dir_dentry);
7826 + dput(lower_old_dentry);
7827 + dput(lower_new_dentry);
7828 +
7829 +out:
7830 + if (!err) {
7831 + /* Fixup the new_dentry. */
7832 + if (bindex < dbstart(new_dentry))
7833 + dbstart(new_dentry) = bindex;
7834 + else if (bindex > dbend(new_dentry))
7835 + dbend(new_dentry) = bindex;
7836 + }
7837 +
7838 + return err;
7839 +}
7840 +
7841 +/*
7842 + * Main rename code. This is sufficiently complex, that it's documented in
7843 + * Documentation/filesystems/unionfs/rename.txt. This routine calls
7844 + * __unionfs_rename() above to perform some of the work.
7845 + */
7846 +static int do_unionfs_rename(struct inode *old_dir,
7847 + struct dentry *old_dentry,
7848 + struct dentry *old_parent,
7849 + struct inode *new_dir,
7850 + struct dentry *new_dentry,
7851 + struct dentry *new_parent)
7852 +{
7853 + int err = 0;
7854 + int bindex;
7855 + int old_bstart, old_bend;
7856 + int new_bstart, new_bend;
7857 + int do_copyup = -1;
7858 + int local_err = 0;
7859 + int eio = 0;
7860 + int revert = 0;
7861 +
7862 + old_bstart = dbstart(old_dentry);
7863 + old_bend = dbend(old_dentry);
7864 +
7865 + new_bstart = dbstart(new_dentry);
7866 + new_bend = dbend(new_dentry);
7867 +
7868 + /* Rename source to destination. */
7869 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7870 + new_dir, new_dentry, new_parent,
7871 + old_bstart);
7872 + if (err) {
7873 + if (!IS_COPYUP_ERR(err))
7874 + goto out;
7875 + do_copyup = old_bstart - 1;
7876 + } else {
7877 + revert = 1;
7878 + }
7879 +
7880 + /*
7881 + * Unlink all instances of destination that exist to the left of
7882 + * bstart of source. On error, revert back, goto out.
7883 + */
7884 + for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) {
7885 + struct dentry *unlink_dentry;
7886 + struct dentry *unlink_dir_dentry;
7887 +
7888 + BUG_ON(bindex < 0);
7889 + unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
7890 + if (!unlink_dentry)
7891 + continue;
7892 +
7893 + unlink_dir_dentry = lock_parent(unlink_dentry);
7894 + err = is_robranch_super(old_dir->i_sb, bindex);
7895 + if (!err)
7896 + err = vfs_unlink(unlink_dir_dentry->d_inode,
7897 + unlink_dentry);
7898 +
7899 + fsstack_copy_attr_times(new_parent->d_inode,
7900 + unlink_dir_dentry->d_inode);
7901 + /* propagate number of hard-links */
7902 + new_parent->d_inode->i_nlink =
7903 + unionfs_get_nlinks(new_parent->d_inode);
7904 +
7905 + unlock_dir(unlink_dir_dentry);
7906 + if (!err) {
7907 + if (bindex != new_bstart) {
7908 + dput(unlink_dentry);
7909 + unionfs_set_lower_dentry_idx(new_dentry,
7910 + bindex, NULL);
7911 + }
7912 + } else if (IS_COPYUP_ERR(err)) {
7913 + do_copyup = bindex - 1;
7914 + } else if (revert) {
7915 + goto revert;
7916 + }
7917 + }
7918 +
7919 + if (do_copyup != -1) {
7920 + for (bindex = do_copyup; bindex >= 0; bindex--) {
7921 + /*
7922 + * copyup the file into some left directory, so that
7923 + * you can rename it
7924 + */
7925 + err = copyup_dentry(old_parent->d_inode,
7926 + old_dentry, old_bstart, bindex,
7927 + old_dentry->d_name.name,
7928 + old_dentry->d_name.len, NULL,
7929 + i_size_read(old_dentry->d_inode));
7930 + /* if copyup failed, try next branch to the left */
7931 + if (err)
7932 + continue;
7933 + /*
7934 + * create whiteout before calling __unionfs_rename
7935 + * because the latter will change the old_dentry's
7936 + * lower name and parent dir, resulting in the
7937 + * whiteout getting created in the wrong dir.
7938 + */
7939 + err = create_whiteout(old_dentry, bindex);
7940 + if (err) {
7941 + printk(KERN_ERR "unionfs: can't create a "
7942 + "whiteout for %s in rename (err=%d)\n",
7943 + old_dentry->d_name.name, err);
7944 + continue;
7945 + }
7946 + err = __unionfs_rename(old_dir, old_dentry, old_parent,
7947 + new_dir, new_dentry, new_parent,
7948 + bindex);
7949 + break;
7950 + }
7951 + }
7952 +
7953 + /* make it opaque */
7954 + if (S_ISDIR(old_dentry->d_inode->i_mode)) {
7955 + err = make_dir_opaque(old_dentry, dbstart(old_dentry));
7956 + if (err)
7957 + goto revert;
7958 + }
7959 +
7960 + /*
7961 + * Create whiteout for source, only if:
7962 + * (1) There is more than one underlying instance of source.
7963 + * (We did a copy_up is taken care of above).
7964 + */
7965 + if ((old_bstart != old_bend) && (do_copyup == -1)) {
7966 + err = create_whiteout(old_dentry, old_bstart);
7967 + if (err) {
7968 + /* can't fix anything now, so we exit with -EIO */
7969 + printk(KERN_ERR "unionfs: can't create a whiteout for "
7970 + "%s in rename!\n", old_dentry->d_name.name);
7971 + err = -EIO;
7972 + }
7973 + }
7974 +
7975 +out:
7976 + return err;
7977 +
7978 +revert:
7979 + /* Do revert here. */
7980 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
7981 + old_bstart);
7982 + if (local_err) {
7983 + printk(KERN_ERR "unionfs: revert failed in rename: "
7984 + "the new refresh failed\n");
7985 + eio = -EIO;
7986 + }
7987 +
7988 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
7989 + old_bstart);
7990 + if (local_err) {
7991 + printk(KERN_ERR "unionfs: revert failed in rename: "
7992 + "the old refresh failed\n");
7993 + eio = -EIO;
7994 + goto revert_out;
7995 + }
7996 +
7997 + if (!unionfs_lower_dentry_idx(new_dentry, bindex) ||
7998 + !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) {
7999 + printk(KERN_ERR "unionfs: revert failed in rename: "
8000 + "the object disappeared from under us!\n");
8001 + eio = -EIO;
8002 + goto revert_out;
8003 + }
8004 +
8005 + if (unionfs_lower_dentry_idx(old_dentry, bindex) &&
8006 + unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) {
8007 + printk(KERN_ERR "unionfs: revert failed in rename: "
8008 + "the object was created underneath us!\n");
8009 + eio = -EIO;
8010 + goto revert_out;
8011 + }
8012 +
8013 + local_err = __unionfs_rename(new_dir, new_dentry, new_parent,
8014 + old_dir, old_dentry, old_parent,
8015 + old_bstart);
8016 +
8017 + /* If we can't fix it, then we cop-out with -EIO. */
8018 + if (local_err) {
8019 + printk(KERN_ERR "unionfs: revert failed in rename!\n");
8020 + eio = -EIO;
8021 + }
8022 +
8023 + local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent,
8024 + bindex);
8025 + if (local_err)
8026 + eio = -EIO;
8027 + local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent,
8028 + bindex);
8029 + if (local_err)
8030 + eio = -EIO;
8031 +
8032 +revert_out:
8033 + if (eio)
8034 + err = eio;
8035 + return err;
8036 +}
8037 +
8038 +/*
8039 + * We can't copyup a directory, because it may involve huge numbers of
8040 + * children, etc. Doing that in the kernel would be bad, so instead we
8041 + * return EXDEV to the user-space utility that caused this, and let the
8042 + * user-space recurse and ask us to copy up each file separately.
8043 + */
8044 +static int may_rename_dir(struct dentry *dentry, struct dentry *parent)
8045 +{
8046 + int err, bstart;
8047 +
8048 + err = check_empty(dentry, parent, NULL);
8049 + if (err == -ENOTEMPTY) {
8050 + if (is_robranch(dentry))
8051 + return -EXDEV;
8052 + } else if (err) {
8053 + return err;
8054 + }
8055 +
8056 + bstart = dbstart(dentry);
8057 + if (dbend(dentry) == bstart || dbopaque(dentry) == bstart)
8058 + return 0;
8059 +
8060 + dbstart(dentry) = bstart + 1;
8061 + err = check_empty(dentry, parent, NULL);
8062 + dbstart(dentry) = bstart;
8063 + if (err == -ENOTEMPTY)
8064 + err = -EXDEV;
8065 + return err;
8066 +}
8067 +
8068 +/*
8069 + * The locking rules in unionfs_rename are complex. We could use a simpler
8070 + * superblock-level name-space lock for renames and copy-ups.
8071 + */
8072 +int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8073 + struct inode *new_dir, struct dentry *new_dentry)
8074 +{
8075 + int err = 0;
8076 + struct dentry *wh_dentry;
8077 + struct dentry *old_parent, *new_parent;
8078 + int valid = true;
8079 +
8080 + unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD);
8081 + old_parent = dget_parent(old_dentry);
8082 + new_parent = dget_parent(new_dentry);
8083 + /* un/lock parent dentries only if they differ from old/new_dentry */
8084 + if (old_parent != old_dentry &&
8085 + old_parent != new_dentry)
8086 + unionfs_lock_dentry(old_parent, UNIONFS_DMUTEX_REVAL_PARENT);
8087 + if (new_parent != old_dentry &&
8088 + new_parent != new_dentry &&
8089 + new_parent != old_parent)
8090 + unionfs_lock_dentry(new_parent, UNIONFS_DMUTEX_REVAL_CHILD);
8091 + unionfs_double_lock_dentry(old_dentry, new_dentry);
8092 +
8093 + valid = __unionfs_d_revalidate(old_dentry, old_parent, false);
8094 + if (!valid) {
8095 + err = -ESTALE;
8096 + goto out;
8097 + }
8098 + if (!d_deleted(new_dentry) && new_dentry->d_inode) {
8099 + valid = __unionfs_d_revalidate(new_dentry, new_parent, false);
8100 + if (!valid) {
8101 + err = -ESTALE;
8102 + goto out;
8103 + }
8104 + }
8105 +
8106 + if (!S_ISDIR(old_dentry->d_inode->i_mode))
8107 + err = unionfs_partial_lookup(old_dentry, old_parent);
8108 + else
8109 + err = may_rename_dir(old_dentry, old_parent);
8110 +
8111 + if (err)
8112 + goto out;
8113 +
8114 + err = unionfs_partial_lookup(new_dentry, new_parent);
8115 + if (err)
8116 + goto out;
8117 +
8118 + /*
8119 + * if new_dentry is already lower because of whiteout,
8120 + * simply override it even if the whited-out dir is not empty.
8121 + */
8122 + wh_dentry = find_first_whiteout(new_dentry);
8123 + if (!IS_ERR(wh_dentry)) {
8124 + dput(wh_dentry);
8125 + } else if (new_dentry->d_inode) {
8126 + if (S_ISDIR(old_dentry->d_inode->i_mode) !=
8127 + S_ISDIR(new_dentry->d_inode->i_mode)) {
8128 + err = S_ISDIR(old_dentry->d_inode->i_mode) ?
8129 + -ENOTDIR : -EISDIR;
8130 + goto out;
8131 + }
8132 +
8133 + if (S_ISDIR(new_dentry->d_inode->i_mode)) {
8134 + struct unionfs_dir_state *namelist = NULL;
8135 + /* check if this unionfs directory is empty or not */
8136 + err = check_empty(new_dentry, new_parent, &namelist);
8137 + if (err)
8138 + goto out;
8139 +
8140 + if (!is_robranch(new_dentry))
8141 + err = delete_whiteouts(new_dentry,
8142 + dbstart(new_dentry),
8143 + namelist);
8144 +
8145 + free_rdstate(namelist);
8146 +
8147 + if (err)
8148 + goto out;
8149 + }
8150 + }
8151 +
8152 + err = do_unionfs_rename(old_dir, old_dentry, old_parent,
8153 + new_dir, new_dentry, new_parent);
8154 + if (err)
8155 + goto out;
8156 +
8157 + /*
8158 + * force re-lookup since the dir on ro branch is not renamed, and
8159 + * lower dentries still indicate the un-renamed ones.
8160 + */
8161 + if (S_ISDIR(old_dentry->d_inode->i_mode))
8162 + atomic_dec(&UNIONFS_D(old_dentry)->generation);
8163 + else
8164 + unionfs_postcopyup_release(old_dentry);
8165 + if (new_dentry->d_inode && !S_ISDIR(new_dentry->d_inode->i_mode)) {
8166 + unionfs_postcopyup_release(new_dentry);
8167 + unionfs_postcopyup_setmnt(new_dentry);
8168 + if (!unionfs_lower_inode(new_dentry->d_inode)) {
8169 + /*
8170 + * If we get here, it means that no copyup was
8171 + * needed, and that a file by the old name already
8172 + * existing on the destination branch; that file got
8173 + * renamed earlier in this function, so all we need
8174 + * to do here is set the lower inode.
8175 + */
8176 + struct inode *inode;
8177 + inode = unionfs_lower_inode(old_dentry->d_inode);
8178 + igrab(inode);
8179 + unionfs_set_lower_inode_idx(new_dentry->d_inode,
8180 + dbstart(new_dentry),
8181 + inode);
8182 + }
8183 + }
8184 + /* if all of this renaming succeeded, update our times */
8185 + unionfs_copy_attr_times(old_dentry->d_inode);
8186 + unionfs_copy_attr_times(new_dentry->d_inode);
8187 + unionfs_check_inode(old_dir);
8188 + unionfs_check_inode(new_dir);
8189 + unionfs_check_dentry(old_dentry);
8190 + unionfs_check_dentry(new_dentry);
8191 +
8192 +out:
8193 + if (err) /* clear the new_dentry stuff created */
8194 + d_drop(new_dentry);
8195 +
8196 + unionfs_double_unlock_dentry(old_dentry, new_dentry);
8197 + if (new_parent != old_dentry &&
8198 + new_parent != new_dentry &&
8199 + new_parent != old_parent)
8200 + unionfs_unlock_dentry(new_parent);
8201 + if (old_parent != old_dentry &&
8202 + old_parent != new_dentry)
8203 + unionfs_unlock_dentry(old_parent);
8204 + dput(new_parent);
8205 + dput(old_parent);
8206 + unionfs_read_unlock(old_dentry->d_sb);
8207 +
8208 + return err;
8209 +}
8210 diff --git a/fs/unionfs/sioq.c b/fs/unionfs/sioq.c
8211 new file mode 100644
8212 index 0000000..5dd487a
8213 --- /dev/null
8214 +++ b/fs/unionfs/sioq.c
8215 @@ -0,0 +1,101 @@
8216 +/*
8217 + * Copyright (c) 2006-2009 Erez Zadok
8218 + * Copyright (c) 2006 Charles P. Wright
8219 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8220 + * Copyright (c) 2006 Junjiro Okajima
8221 + * Copyright (c) 2006 David P. Quigley
8222 + * Copyright (c) 2006-2009 Stony Brook University
8223 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8224 + *
8225 + * This program is free software; you can redistribute it and/or modify
8226 + * it under the terms of the GNU General Public License version 2 as
8227 + * published by the Free Software Foundation.
8228 + */
8229 +
8230 +#include "union.h"
8231 +
8232 +/*
8233 + * Super-user IO work Queue - sometimes we need to perform actions which
8234 + * would fail due to the unix permissions on the parent directory (e.g.,
8235 + * rmdir a directory which appears empty, but in reality contains
8236 + * whiteouts).
8237 + */
8238 +
8239 +static struct workqueue_struct *superio_workqueue;
8240 +
8241 +int __init init_sioq(void)
8242 +{
8243 + int err;
8244 +
8245 + superio_workqueue = create_workqueue("unionfs_siod");
8246 + if (!IS_ERR(superio_workqueue))
8247 + return 0;
8248 +
8249 + err = PTR_ERR(superio_workqueue);
8250 + printk(KERN_ERR "unionfs: create_workqueue failed %d\n", err);
8251 + superio_workqueue = NULL;
8252 + return err;
8253 +}
8254 +
8255 +void stop_sioq(void)
8256 +{
8257 + if (superio_workqueue)
8258 + destroy_workqueue(superio_workqueue);
8259 +}
8260 +
8261 +void run_sioq(work_func_t func, struct sioq_args *args)
8262 +{
8263 + INIT_WORK(&args->work, func);
8264 +
8265 + init_completion(&args->comp);
8266 + while (!queue_work(superio_workqueue, &args->work)) {
8267 + /* TODO: do accounting if needed */
8268 + schedule();
8269 + }
8270 + wait_for_completion(&args->comp);
8271 +}
8272 +
8273 +void __unionfs_create(struct work_struct *work)
8274 +{
8275 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8276 + struct create_args *c = &args->create;
8277 +
8278 + args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
8279 + complete(&args->comp);
8280 +}
8281 +
8282 +void __unionfs_mkdir(struct work_struct *work)
8283 +{
8284 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8285 + struct mkdir_args *m = &args->mkdir;
8286 +
8287 + args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
8288 + complete(&args->comp);
8289 +}
8290 +
8291 +void __unionfs_mknod(struct work_struct *work)
8292 +{
8293 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8294 + struct mknod_args *m = &args->mknod;
8295 +
8296 + args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
8297 + complete(&args->comp);
8298 +}
8299 +
8300 +void __unionfs_symlink(struct work_struct *work)
8301 +{
8302 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8303 + struct symlink_args *s = &args->symlink;
8304 +
8305 + args->err = vfs_symlink(s->parent, s->dentry, s->symbuf);
8306 + complete(&args->comp);
8307 +}
8308 +
8309 +void __unionfs_unlink(struct work_struct *work)
8310 +{
8311 + struct sioq_args *args = container_of(work, struct sioq_args, work);
8312 + struct unlink_args *u = &args->unlink;
8313 +
8314 + args->err = vfs_unlink(u->parent, u->dentry);
8315 + complete(&args->comp);
8316 +}
8317 diff --git a/fs/unionfs/sioq.h b/fs/unionfs/sioq.h
8318 new file mode 100644
8319 index 0000000..3d7869a
8320 --- /dev/null
8321 +++ b/fs/unionfs/sioq.h
8322 @@ -0,0 +1,91 @@
8323 +/*
8324 + * Copyright (c) 2006-2009 Erez Zadok
8325 + * Copyright (c) 2006 Charles P. Wright
8326 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
8327 + * Copyright (c) 2006 Junjiro Okajima
8328 + * Copyright (c) 2006 David P. Quigley
8329 + * Copyright (c) 2006-2009 Stony Brook University
8330 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
8331 + *
8332 + * This program is free software; you can redistribute it and/or modify
8333 + * it under the terms of the GNU General Public License version 2 as
8334 + * published by the Free Software Foundation.
8335 + */
8336 +
8337 +#ifndef _SIOQ_H
8338 +#define _SIOQ_H
8339 +
8340 +struct deletewh_args {
8341 + struct unionfs_dir_state *namelist;
8342 + struct dentry *dentry;
8343 + int bindex;
8344 +};
8345 +
8346 +struct is_opaque_args {
8347 + struct dentry *dentry;
8348 +};
8349 +
8350 +struct create_args {
8351 + struct inode *parent;
8352 + struct dentry *dentry;
8353 + umode_t mode;
8354 + struct nameidata *nd;
8355 +};
8356 +
8357 +struct mkdir_args {
8358 + struct inode *parent;
8359 + struct dentry *dentry;
8360 + umode_t mode;
8361 +};
8362 +
8363 +struct mknod_args {
8364 + struct inode *parent;
8365 + struct dentry *dentry;
8366 + umode_t mode;
8367 + dev_t dev;
8368 +};
8369 +
8370 +struct symlink_args {
8371 + struct inode *parent;
8372 + struct dentry *dentry;
8373 + char *symbuf;
8374 +};
8375 +
8376 +struct unlink_args {
8377 + struct inode *parent;
8378 + struct dentry *dentry;
8379 +};
8380 +
8381 +
8382 +struct sioq_args {
8383 + struct completion comp;
8384 + struct work_struct work;
8385 + int err;
8386 + void *ret;
8387 +
8388 + union {
8389 + struct deletewh_args deletewh;
8390 + struct is_opaque_args is_opaque;
8391 + struct create_args create;
8392 + struct mkdir_args mkdir;
8393 + struct mknod_args mknod;
8394 + struct symlink_args symlink;
8395 + struct unlink_args unlink;
8396 + };
8397 +};
8398 +
8399 +/* Extern definitions for SIOQ functions */
8400 +extern int __init init_sioq(void);
8401 +extern void stop_sioq(void);
8402 +extern void run_sioq(work_func_t func, struct sioq_args *args);
8403 +
8404 +/* Extern definitions for our privilege escalation helpers */
8405 +extern void __unionfs_create(struct work_struct *work);
8406 +extern void __unionfs_mkdir(struct work_struct *work);
8407 +extern void __unionfs_mknod(struct work_struct *work);
8408 +extern void __unionfs_symlink(struct work_struct *work);
8409 +extern void __unionfs_unlink(struct work_struct *work);
8410 +extern void __delete_whiteouts(struct work_struct *work);
8411 +extern void __is_opaque_dir(struct work_struct *work);
8412 +
8413 +#endif /* not _SIOQ_H */
8414 diff --git a/fs/unionfs/subr.c b/fs/unionfs/subr.c
8415 new file mode 100644
8416 index 0000000..018b4fd
8417 --- /dev/null
8418 +++ b/fs/unionfs/subr.c
8419 @@ -0,0 +1,95 @@
8420 +/*
8421 + * Copyright (c) 2003-2009 Erez Zadok
8422 + * Copyright (c) 2003-2006 Charles P. Wright
8423 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8424 + * Copyright (c) 2005-2006 Junjiro Okajima
8425 + * Copyright (c) 2005 Arun M. Krishnakumar
8426 + * Copyright (c) 2004-2006 David P. Quigley
8427 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8428 + * Copyright (c) 2003 Puja Gupta
8429 + * Copyright (c) 2003 Harikesavan Krishnan
8430 + * Copyright (c) 2003-2009 Stony Brook University
8431 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8432 + *
8433 + * This program is free software; you can redistribute it and/or modify
8434 + * it under the terms of the GNU General Public License version 2 as
8435 + * published by the Free Software Foundation.
8436 + */
8437 +
8438 +#include "union.h"
8439 +
8440 +/*
8441 + * returns the right n_link value based on the inode type
8442 + */
8443 +int unionfs_get_nlinks(const struct inode *inode)
8444 +{
8445 + /* don't bother to do all the work since we're unlinked */
8446 + if (inode->i_nlink == 0)
8447 + return 0;
8448 +
8449 + if (!S_ISDIR(inode->i_mode))
8450 + return unionfs_lower_inode(inode)->i_nlink;
8451 +
8452 + /*
8453 + * For directories, we return 1. The only place that could cares
8454 + * about links is readdir, and there's d_type there so even that
8455 + * doesn't matter.
8456 + */
8457 + return 1;
8458 +}
8459 +
8460 +/* copy a/m/ctime from the lower branch with the newest times */
8461 +void unionfs_copy_attr_times(struct inode *upper)
8462 +{
8463 + int bindex;
8464 + struct inode *lower;
8465 +
8466 + if (!upper)
8467 + return;
8468 + if (ibstart(upper) < 0) {
8469 +#ifdef CONFIG_UNION_FS_DEBUG
8470 + WARN_ON(ibstart(upper) < 0);
8471 +#endif /* CONFIG_UNION_FS_DEBUG */
8472 + return;
8473 + }
8474 + for (bindex = ibstart(upper); bindex <= ibend(upper); bindex++) {
8475 + lower = unionfs_lower_inode_idx(upper, bindex);
8476 + if (!lower)
8477 + continue; /* not all lower dir objects may exist */
8478 + if (unlikely(timespec_compare(&upper->i_mtime,
8479 + &lower->i_mtime) < 0))
8480 + upper->i_mtime = lower->i_mtime;
8481 + if (unlikely(timespec_compare(&upper->i_ctime,
8482 + &lower->i_ctime) < 0))
8483 + upper->i_ctime = lower->i_ctime;
8484 + if (unlikely(timespec_compare(&upper->i_atime,
8485 + &lower->i_atime) < 0))
8486 + upper->i_atime = lower->i_atime;
8487 + }
8488 +}
8489 +
8490 +/*
8491 + * A unionfs/fanout version of fsstack_copy_attr_all. Uses a
8492 + * unionfs_get_nlinks to properly calcluate the number of links to a file.
8493 + * Also, copies the max() of all a/m/ctimes for all lower inodes (which is
8494 + * important if the lower inode is a directory type)
8495 + */
8496 +void unionfs_copy_attr_all(struct inode *dest,
8497 + const struct inode *src)
8498 +{
8499 + dest->i_mode = src->i_mode;
8500 + dest->i_uid = src->i_uid;
8501 + dest->i_gid = src->i_gid;
8502 + dest->i_rdev = src->i_rdev;
8503 +
8504 + unionfs_copy_attr_times(dest);
8505 +
8506 + dest->i_blkbits = src->i_blkbits;
8507 + dest->i_flags = src->i_flags;
8508 +
8509 + /*
8510 + * Update the nlinks AFTER updating the above fields, because the
8511 + * get_links callback may depend on them.
8512 + */
8513 + dest->i_nlink = unionfs_get_nlinks(dest);
8514 +}
8515 diff --git a/fs/unionfs/super.c b/fs/unionfs/super.c
8516 new file mode 100644
8517 index 0000000..ded7b84
8518 --- /dev/null
8519 +++ b/fs/unionfs/super.c
8520 @@ -0,0 +1,1047 @@
8521 +/*
8522 + * Copyright (c) 2003-2009 Erez Zadok
8523 + * Copyright (c) 2003-2006 Charles P. Wright
8524 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
8525 + * Copyright (c) 2005-2006 Junjiro Okajima
8526 + * Copyright (c) 2005 Arun M. Krishnakumar
8527 + * Copyright (c) 2004-2006 David P. Quigley
8528 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
8529 + * Copyright (c) 2003 Puja Gupta
8530 + * Copyright (c) 2003 Harikesavan Krishnan
8531 + * Copyright (c) 2003-2009 Stony Brook University
8532 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
8533 + *
8534 + * This program is free software; you can redistribute it and/or modify
8535 + * it under the terms of the GNU General Public License version 2 as
8536 + * published by the Free Software Foundation.
8537 + */
8538 +
8539 +#include "union.h"
8540 +
8541 +/*
8542 + * The inode cache is used with alloc_inode for both our inode info and the
8543 + * vfs inode.
8544 + */
8545 +static struct kmem_cache *unionfs_inode_cachep;
8546 +
8547 +struct inode *unionfs_iget(struct super_block *sb, unsigned long ino)
8548 +{
8549 + int size;
8550 + struct unionfs_inode_info *info;
8551 + struct inode *inode;
8552 +
8553 + inode = iget_locked(sb, ino);
8554 + if (!inode)
8555 + return ERR_PTR(-ENOMEM);
8556 + if (!(inode->i_state & I_NEW))
8557 + return inode;
8558 +
8559 + info = UNIONFS_I(inode);
8560 + memset(info, 0, offsetof(struct unionfs_inode_info, vfs_inode));
8561 + info->bstart = -1;
8562 + info->bend = -1;
8563 + atomic_set(&info->generation,
8564 + atomic_read(&UNIONFS_SB(inode->i_sb)->generation));
8565 + spin_lock_init(&info->rdlock);
8566 + info->rdcount = 1;
8567 + info->hashsize = -1;
8568 + INIT_LIST_HEAD(&info->readdircache);
8569 +
8570 + size = sbmax(inode->i_sb) * sizeof(struct inode *);
8571 + info->lower_inodes = kzalloc(size, GFP_KERNEL);
8572 + if (unlikely(!info->lower_inodes)) {
8573 + printk(KERN_CRIT "unionfs: no kernel memory when allocating "
8574 + "lower-pointer array!\n");
8575 + iget_failed(inode);
8576 + return ERR_PTR(-ENOMEM);
8577 + }
8578 +
8579 + inode->i_version++;
8580 + inode->i_op = &unionfs_main_iops;
8581 + inode->i_fop = &unionfs_main_fops;
8582 +
8583 + inode->i_mapping->a_ops = &unionfs_aops;
8584 +
8585 + /*
8586 + * reset times so unionfs_copy_attr_all can keep out time invariants
8587 + * right (upper inode time being the max of all lower ones).
8588 + */
8589 + inode->i_atime.tv_sec = inode->i_atime.tv_nsec = 0;
8590 + inode->i_mtime.tv_sec = inode->i_mtime.tv_nsec = 0;
8591 + inode->i_ctime.tv_sec = inode->i_ctime.tv_nsec = 0;
8592 + unlock_new_inode(inode);
8593 + return inode;
8594 +}
8595 +
8596 +/*
8597 + * we now define delete_inode, because there are two VFS paths that may
8598 + * destroy an inode: one of them calls clear inode before doing everything
8599 + * else that's needed, and the other is fine. This way we truncate the inode
8600 + * size (and its pages) and then clear our own inode, which will do an iput
8601 + * on our and the lower inode.
8602 + *
8603 + * No need to lock sb info's rwsem.
8604 + */
8605 +static void unionfs_delete_inode(struct inode *inode)
8606 +{
8607 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8608 + spin_lock(&inode->i_lock);
8609 +#endif
8610 + i_size_write(inode, 0); /* every f/s seems to do that */
8611 +#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
8612 + spin_unlock(&inode->i_lock);
8613 +#endif
8614 +
8615 + if (inode->i_data.nrpages)
8616 + truncate_inode_pages(&inode->i_data, 0);
8617 +
8618 + clear_inode(inode);
8619 +}
8620 +
8621 +/*
8622 + * final actions when unmounting a file system
8623 + *
8624 + * No need to lock rwsem.
8625 + */
8626 +static void unionfs_put_super(struct super_block *sb)
8627 +{
8628 + int bindex, bstart, bend;
8629 + struct unionfs_sb_info *spd;
8630 + int leaks = 0;
8631 +
8632 + spd = UNIONFS_SB(sb);
8633 + if (!spd)
8634 + return;
8635 +
8636 + bstart = sbstart(sb);
8637 + bend = sbend(sb);
8638 +
8639 + /* Make sure we have no leaks of branchget/branchput. */
8640 + for (bindex = bstart; bindex <= bend; bindex++)
8641 + if (unlikely(branch_count(sb, bindex) != 0)) {
8642 + printk(KERN_CRIT
8643 + "unionfs: branch %d has %d references left!\n",
8644 + bindex, branch_count(sb, bindex));
8645 + leaks = 1;
8646 + }
8647 + WARN_ON(leaks != 0);
8648 +
8649 + /* decrement lower super references */
8650 + for (bindex = bstart; bindex <= bend; bindex++) {
8651 + struct super_block *s;
8652 + s = unionfs_lower_super_idx(sb, bindex);
8653 + unionfs_set_lower_super_idx(sb, bindex, NULL);
8654 + atomic_dec(&s->s_active);
8655 + }
8656 +
8657 + kfree(spd->dev_name);
8658 + kfree(spd->data);
8659 + kfree(spd);
8660 + sb->s_fs_info = NULL;
8661 +}
8662 +
8663 +/*
8664 + * Since people use this to answer the "How big of a file can I write?"
8665 + * question, we report the size of the highest priority branch as the size of
8666 + * the union.
8667 + */
8668 +static int unionfs_statfs(struct dentry *dentry, struct kstatfs *buf)
8669 +{
8670 + int err = 0;
8671 + struct super_block *sb;
8672 + struct dentry *lower_dentry;
8673 + struct dentry *parent;
8674 + bool valid;
8675 +
8676 + sb = dentry->d_sb;
8677 +
8678 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
8679 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
8680 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
8681 +
8682 + valid = __unionfs_d_revalidate(dentry, parent, false);
8683 + if (unlikely(!valid)) {
8684 + err = -ESTALE;
8685 + goto out;
8686 + }
8687 + unionfs_check_dentry(dentry);
8688 +
8689 + lower_dentry = unionfs_lower_dentry(sb->s_root);
8690 + err = vfs_statfs(lower_dentry, buf);
8691 +
8692 + /* set return buf to our f/s to avoid confusing user-level utils */
8693 + buf->f_type = UNIONFS_SUPER_MAGIC;
8694 + /*
8695 + * Our maximum file name can is shorter by a few bytes because every
8696 + * file name could potentially be whited-out.
8697 + *
8698 + * XXX: this restriction goes away with ODF.
8699 + */
8700 + unionfs_set_max_namelen(&buf->f_namelen);
8701 +
8702 + /*
8703 + * reset two fields to avoid confusing user-land.
8704 + * XXX: is this still necessary?
8705 + */
8706 + memset(&buf->f_fsid, 0, sizeof(__kernel_fsid_t));
8707 + memset(&buf->f_spare, 0, sizeof(buf->f_spare));
8708 +
8709 +out:
8710 + unionfs_check_dentry(dentry);
8711 + unionfs_unlock_dentry(dentry);
8712 + unionfs_unlock_parent(dentry, parent);
8713 + unionfs_read_unlock(sb);
8714 + return err;
8715 +}
8716 +
8717 +/* handle mode changing during remount */
8718 +static noinline_for_stack int do_remount_mode_option(
8719 + char *optarg,
8720 + int cur_branches,
8721 + struct unionfs_data *new_data,
8722 + struct path *new_lower_paths)
8723 +{
8724 + int err = -EINVAL;
8725 + int perms, idx;
8726 + char *modename = strchr(optarg, '=');
8727 + struct nameidata nd;
8728 +
8729 + /* by now, optarg contains the branch name */
8730 + if (!*optarg) {
8731 + printk(KERN_ERR
8732 + "unionfs: no branch specified for mode change\n");
8733 + goto out;
8734 + }
8735 + if (!modename) {
8736 + printk(KERN_ERR "unionfs: branch \"%s\" requires a mode\n",
8737 + optarg);
8738 + goto out;
8739 + }
8740 + *modename++ = '\0';
8741 + err = parse_branch_mode(modename, &perms);
8742 + if (err) {
8743 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for \"%s\"\n",
8744 + modename, optarg);
8745 + goto out;
8746 + }
8747 +
8748 + /*
8749 + * Find matching branch index. For now, this assumes that nothing
8750 + * has been mounted on top of this Unionfs stack. Once we have /odf
8751 + * and cache-coherency resolved, we'll address the branch-path
8752 + * uniqueness.
8753 + */
8754 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8755 + if (err) {
8756 + printk(KERN_ERR "unionfs: error accessing "
8757 + "lower directory \"%s\" (error %d)\n",
8758 + optarg, err);
8759 + goto out;
8760 + }
8761 + for (idx = 0; idx < cur_branches; idx++)
8762 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8763 + nd.path.dentry == new_lower_paths[idx].dentry)
8764 + break;
8765 + path_put(&nd.path); /* no longer needed */
8766 + if (idx == cur_branches) {
8767 + err = -ENOENT; /* err may have been reset above */
8768 + printk(KERN_ERR "unionfs: branch \"%s\" "
8769 + "not found\n", optarg);
8770 + goto out;
8771 + }
8772 + /* check/change mode for existing branch */
8773 + /* we don't warn if perms==branchperms */
8774 + new_data[idx].branchperms = perms;
8775 + err = 0;
8776 +out:
8777 + return err;
8778 +}
8779 +
8780 +/* handle branch deletion during remount */
8781 +static noinline_for_stack int do_remount_del_option(
8782 + char *optarg, int cur_branches,
8783 + struct unionfs_data *new_data,
8784 + struct path *new_lower_paths)
8785 +{
8786 + int err = -EINVAL;
8787 + int idx;
8788 + struct nameidata nd;
8789 +
8790 + /* optarg contains the branch name to delete */
8791 +
8792 + /*
8793 + * Find matching branch index. For now, this assumes that nothing
8794 + * has been mounted on top of this Unionfs stack. Once we have /odf
8795 + * and cache-coherency resolved, we'll address the branch-path
8796 + * uniqueness.
8797 + */
8798 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8799 + if (err) {
8800 + printk(KERN_ERR "unionfs: error accessing "
8801 + "lower directory \"%s\" (error %d)\n",
8802 + optarg, err);
8803 + goto out;
8804 + }
8805 + for (idx = 0; idx < cur_branches; idx++)
8806 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8807 + nd.path.dentry == new_lower_paths[idx].dentry)
8808 + break;
8809 + path_put(&nd.path); /* no longer needed */
8810 + if (idx == cur_branches) {
8811 + printk(KERN_ERR "unionfs: branch \"%s\" "
8812 + "not found\n", optarg);
8813 + err = -ENOENT;
8814 + goto out;
8815 + }
8816 + /* check if there are any open files on the branch to be deleted */
8817 + if (atomic_read(&new_data[idx].open_files) > 0) {
8818 + err = -EBUSY;
8819 + goto out;
8820 + }
8821 +
8822 + /*
8823 + * Now we have to delete the branch. First, release any handles it
8824 + * has. Then, move the remaining array indexes past "idx" in
8825 + * new_data and new_lower_paths one to the left. Finally, adjust
8826 + * cur_branches.
8827 + */
8828 + path_put(&new_lower_paths[idx]);
8829 +
8830 + if (idx < cur_branches - 1) {
8831 + /* if idx==cur_branches-1, we delete last branch: easy */
8832 + memmove(&new_data[idx], &new_data[idx+1],
8833 + (cur_branches - 1 - idx) *
8834 + sizeof(struct unionfs_data));
8835 + memmove(&new_lower_paths[idx], &new_lower_paths[idx+1],
8836 + (cur_branches - 1 - idx) * sizeof(struct path));
8837 + }
8838 +
8839 + err = 0;
8840 +out:
8841 + return err;
8842 +}
8843 +
8844 +/* handle branch insertion during remount */
8845 +static noinline_for_stack int do_remount_add_option(
8846 + char *optarg, int cur_branches,
8847 + struct unionfs_data *new_data,
8848 + struct path *new_lower_paths,
8849 + int *high_branch_id)
8850 +{
8851 + int err = -EINVAL;
8852 + int perms;
8853 + int idx = 0; /* default: insert at beginning */
8854 + char *new_branch , *modename = NULL;
8855 + struct nameidata nd;
8856 +
8857 + /*
8858 + * optarg can be of several forms:
8859 + *
8860 + * /bar:/foo insert /foo before /bar
8861 + * /bar:/foo=ro insert /foo in ro mode before /bar
8862 + * /foo insert /foo in the beginning (prepend)
8863 + * :/foo insert /foo at the end (append)
8864 + */
8865 + if (*optarg == ':') { /* append? */
8866 + new_branch = optarg + 1; /* skip ':' */
8867 + idx = cur_branches;
8868 + goto found_insertion_point;
8869 + }
8870 + new_branch = strchr(optarg, ':');
8871 + if (!new_branch) { /* prepend? */
8872 + new_branch = optarg;
8873 + goto found_insertion_point;
8874 + }
8875 + *new_branch++ = '\0'; /* holds path+mode of new branch */
8876 +
8877 + /*
8878 + * Find matching branch index. For now, this assumes that nothing
8879 + * has been mounted on top of this Unionfs stack. Once we have /odf
8880 + * and cache-coherency resolved, we'll address the branch-path
8881 + * uniqueness.
8882 + */
8883 + err = path_lookup(optarg, LOOKUP_FOLLOW, &nd);
8884 + if (err) {
8885 + printk(KERN_ERR "unionfs: error accessing "
8886 + "lower directory \"%s\" (error %d)\n",
8887 + optarg, err);
8888 + goto out;
8889 + }
8890 + for (idx = 0; idx < cur_branches; idx++)
8891 + if (nd.path.mnt == new_lower_paths[idx].mnt &&
8892 + nd.path.dentry == new_lower_paths[idx].dentry)
8893 + break;
8894 + path_put(&nd.path); /* no longer needed */
8895 + if (idx == cur_branches) {
8896 + printk(KERN_ERR "unionfs: branch \"%s\" "
8897 + "not found\n", optarg);
8898 + err = -ENOENT;
8899 + goto out;
8900 + }
8901 +
8902 + /*
8903 + * At this point idx will hold the index where the new branch should
8904 + * be inserted before.
8905 + */
8906 +found_insertion_point:
8907 + /* find the mode for the new branch */
8908 + if (new_branch)
8909 + modename = strchr(new_branch, '=');
8910 + if (modename)
8911 + *modename++ = '\0';
8912 + if (!new_branch || !*new_branch) {
8913 + printk(KERN_ERR "unionfs: null new branch\n");
8914 + err = -EINVAL;
8915 + goto out;
8916 + }
8917 + err = parse_branch_mode(modename, &perms);
8918 + if (err) {
8919 + printk(KERN_ERR "unionfs: invalid mode \"%s\" for "
8920 + "branch \"%s\"\n", modename, new_branch);
8921 + goto out;
8922 + }
8923 + err = path_lookup(new_branch, LOOKUP_FOLLOW, &nd);
8924 + if (err) {
8925 + printk(KERN_ERR "unionfs: error accessing "
8926 + "lower directory \"%s\" (error %d)\n",
8927 + new_branch, err);
8928 + goto out;
8929 + }
8930 + /*
8931 + * It's probably safe to check_mode the new branch to insert. Note:
8932 + * we don't allow inserting branches which are unionfs's by
8933 + * themselves (check_branch returns EINVAL in that case). This is
8934 + * because this code base doesn't support stacking unionfs: the ODF
8935 + * code base supports that correctly.
8936 + */
8937 + err = check_branch(&nd);
8938 + if (err) {
8939 + printk(KERN_ERR "unionfs: lower directory "
8940 + "\"%s\" is not a valid branch\n", optarg);
8941 + path_put(&nd.path);
8942 + goto out;
8943 + }
8944 +
8945 + /*
8946 + * Now we have to insert the new branch. But first, move the bits
8947 + * to make space for the new branch, if needed. Finally, adjust
8948 + * cur_branches.
8949 + * We don't release nd here; it's kept until umount/remount.
8950 + */
8951 + if (idx < cur_branches) {
8952 + /* if idx==cur_branches, we append: easy */
8953 + memmove(&new_data[idx+1], &new_data[idx],
8954 + (cur_branches - idx) * sizeof(struct unionfs_data));
8955 + memmove(&new_lower_paths[idx+1], &new_lower_paths[idx],
8956 + (cur_branches - idx) * sizeof(struct path));
8957 + }
8958 + new_lower_paths[idx].dentry = nd.path.dentry;
8959 + new_lower_paths[idx].mnt = nd.path.mnt;
8960 +
8961 + new_data[idx].sb = nd.path.dentry->d_sb;
8962 + atomic_set(&new_data[idx].open_files, 0);
8963 + new_data[idx].branchperms = perms;
8964 + new_data[idx].branch_id = ++*high_branch_id; /* assign new branch ID */
8965 +
8966 + err = 0;
8967 +out:
8968 + return err;
8969 +}
8970 +
8971 +
8972 +/*
8973 + * Support branch management options on remount.
8974 + *
8975 + * See Documentation/filesystems/unionfs/ for details.
8976 + *
8977 + * @flags: numeric mount options
8978 + * @options: mount options string
8979 + *
8980 + * This function can rearrange a mounted union dynamically, adding and
8981 + * removing branches, including changing branch modes. Clearly this has to
8982 + * be done safely and atomically. Luckily, the VFS already calls this
8983 + * function with lock_super(sb) and lock_kernel() held, preventing
8984 + * concurrent mixing of new mounts, remounts, and unmounts. Moreover,
8985 + * do_remount_sb(), our caller function, already called shrink_dcache_sb(sb)
8986 + * to purge dentries/inodes from our superblock, and also called
8987 + * fsync_super(sb) to purge any dirty pages. So we're good.
8988 + *
8989 + * XXX: however, our remount code may also need to invalidate mapped pages
8990 + * so as to force them to be re-gotten from the (newly reconfigured) lower
8991 + * branches. This has to wait for proper mmap and cache coherency support
8992 + * in the VFS.
8993 + *
8994 + */
8995 +static int unionfs_remount_fs(struct super_block *sb, int *flags,
8996 + char *options)
8997 +{
8998 + int err = 0;
8999 + int i;
9000 + char *optionstmp, *tmp_to_free; /* kstrdup'ed of "options" */
9001 + char *optname;
9002 + int cur_branches = 0; /* no. of current branches */
9003 + int new_branches = 0; /* no. of branches actually left in the end */
9004 + int add_branches; /* est. no. of branches to add */
9005 + int del_branches; /* est. no. of branches to del */
9006 + int max_branches; /* max possible no. of branches */
9007 + struct unionfs_data *new_data = NULL, *tmp_data = NULL;
9008 + struct path *new_lower_paths = NULL, *tmp_lower_paths = NULL;
9009 + struct inode **new_lower_inodes = NULL;
9010 + int new_high_branch_id; /* new high branch ID */
9011 + int size; /* memory allocation size, temp var */
9012 + int old_ibstart, old_ibend;
9013 +
9014 + unionfs_write_lock(sb);
9015 +
9016 + /*
9017 + * The VFS will take care of "ro" and "rw" flags, and we can safely
9018 + * ignore MS_SILENT, but anything else left over is an error. So we
9019 + * need to check if any other flags may have been passed (none are
9020 + * allowed/supported as of now).
9021 + */
9022 + if ((*flags & ~(MS_RDONLY | MS_SILENT)) != 0) {
9023 + printk(KERN_ERR
9024 + "unionfs: remount flags 0x%x unsupported\n", *flags);
9025 + err = -EINVAL;
9026 + goto out_error;
9027 + }
9028 +
9029 + /*
9030 + * If 'options' is NULL, it's probably because the user just changed
9031 + * the union to a "ro" or "rw" and the VFS took care of it. So
9032 + * nothing to do and we're done.
9033 + */
9034 + if (!options || options[0] == '\0')
9035 + goto out_error;
9036 +
9037 + /*
9038 + * Find out how many branches we will have in the end, counting
9039 + * "add" and "del" commands. Copy the "options" string because
9040 + * strsep modifies the string and we need it later.
9041 + */
9042 + tmp_to_free = kstrdup(options, GFP_KERNEL);
9043 + optionstmp = tmp_to_free;
9044 + if (unlikely(!optionstmp)) {
9045 + err = -ENOMEM;
9046 + goto out_free;
9047 + }
9048 + cur_branches = sbmax(sb); /* current no. branches */
9049 + new_branches = sbmax(sb);
9050 + del_branches = 0;
9051 + add_branches = 0;
9052 + new_high_branch_id = sbhbid(sb); /* save current high_branch_id */
9053 + while ((optname = strsep(&optionstmp, ",")) != NULL) {
9054 + char *optarg;
9055 +
9056 + if (!optname || !*optname)
9057 + continue;
9058 +
9059 + optarg = strchr(optname, '=');
9060 + if (optarg)
9061 + *optarg++ = '\0';
9062 +
9063 + if (!strcmp("add", optname))
9064 + add_branches++;
9065 + else if (!strcmp("del", optname))
9066 + del_branches++;
9067 + }
9068 + kfree(tmp_to_free);
9069 + /* after all changes, will we have at least one branch left? */
9070 + if ((new_branches + add_branches - del_branches) < 1) {
9071 + printk(KERN_ERR
9072 + "unionfs: no branches left after remount\n");
9073 + err = -EINVAL;
9074 + goto out_free;
9075 + }
9076 +
9077 + /*
9078 + * Since we haven't actually parsed all the add/del options, nor
9079 + * have we checked them for errors, we don't know for sure how many
9080 + * branches we will have after all changes have taken place. In
9081 + * fact, the total number of branches left could be less than what
9082 + * we have now. So we need to allocate space for a temporary
9083 + * placeholder that is at least as large as the maximum number of
9084 + * branches we *could* have, which is the current number plus all
9085 + * the additions. Once we're done with these temp placeholders, we
9086 + * may have to re-allocate the final size, copy over from the temp,
9087 + * and then free the temps (done near the end of this function).
9088 + */
9089 + max_branches = cur_branches + add_branches;
9090 + /* allocate space for new pointers to lower dentry */
9091 + tmp_data = kcalloc(max_branches,
9092 + sizeof(struct unionfs_data), GFP_KERNEL);
9093 + if (unlikely(!tmp_data)) {
9094 + err = -ENOMEM;
9095 + goto out_free;
9096 + }
9097 + /* allocate space for new pointers to lower paths */
9098 + tmp_lower_paths = kcalloc(max_branches,
9099 + sizeof(struct path), GFP_KERNEL);
9100 + if (unlikely(!tmp_lower_paths)) {
9101 + err = -ENOMEM;
9102 + goto out_free;
9103 + }
9104 + /* copy current info into new placeholders, incrementing refcnts */
9105 + memcpy(tmp_data, UNIONFS_SB(sb)->data,
9106 + cur_branches * sizeof(struct unionfs_data));
9107 + memcpy(tmp_lower_paths, UNIONFS_D(sb->s_root)->lower_paths,
9108 + cur_branches * sizeof(struct path));
9109 + for (i = 0; i < cur_branches; i++)
9110 + path_get(&tmp_lower_paths[i]); /* drop refs at end of fxn */
9111 +
9112 + /*******************************************************************
9113 + * For each branch command, do path_lookup on the requested branch,
9114 + * and apply the change to a temp branch list. To handle errors, we
9115 + * already dup'ed the old arrays (above), and increased the refcnts
9116 + * on various f/s objects. So now we can do all the path_lookups
9117 + * and branch-management commands on the new arrays. If it fail mid
9118 + * way, we free the tmp arrays and *put all objects. If we succeed,
9119 + * then we free old arrays and *put its objects, and then replace
9120 + * the arrays with the new tmp list (we may have to re-allocate the
9121 + * memory because the temp lists could have been larger than what we
9122 + * actually needed).
9123 + *******************************************************************/
9124 +
9125 + while ((optname = strsep(&options, ",")) != NULL) {
9126 + char *optarg;
9127 +
9128 + if (!optname || !*optname)
9129 + continue;
9130 + /*
9131 + * At this stage optname holds a comma-delimited option, but
9132 + * without the commas. Next, we need to break the string on
9133 + * the '=' symbol to separate CMD=ARG, where ARG itself can
9134 + * be KEY=VAL. For example, in mode=/foo=rw, CMD is "mode",
9135 + * KEY is "/foo", and VAL is "rw".
9136 + */
9137 + optarg = strchr(optname, '=');
9138 + if (optarg)
9139 + *optarg++ = '\0';
9140 + /* incgen remount option (instead of old ioctl) */
9141 + if (!strcmp("incgen", optname)) {
9142 + err = 0;
9143 + goto out_no_change;
9144 + }
9145 +
9146 + /*
9147 + * All of our options take an argument now. (Insert ones
9148 + * that don't above this check.) So at this stage optname
9149 + * contains the CMD part and optarg contains the ARG part.
9150 + */
9151 + if (!optarg || !*optarg) {
9152 + printk(KERN_ERR "unionfs: all remount options require "
9153 + "an argument (%s)\n", optname);
9154 + err = -EINVAL;
9155 + goto out_release;
9156 + }
9157 +
9158 + if (!strcmp("add", optname)) {
9159 + err = do_remount_add_option(optarg, new_branches,
9160 + tmp_data,
9161 + tmp_lower_paths,
9162 + &new_high_branch_id);
9163 + if (err)
9164 + goto out_release;
9165 + new_branches++;
9166 + if (new_branches > UNIONFS_MAX_BRANCHES) {
9167 + printk(KERN_ERR "unionfs: command exceeds "
9168 + "%d branches\n", UNIONFS_MAX_BRANCHES);
9169 + err = -E2BIG;
9170 + goto out_release;
9171 + }
9172 + continue;
9173 + }
9174 + if (!strcmp("del", optname)) {
9175 + err = do_remount_del_option(optarg, new_branches,
9176 + tmp_data,
9177 + tmp_lower_paths);
9178 + if (err)
9179 + goto out_release;
9180 + new_branches--;
9181 + continue;
9182 + }
9183 + if (!strcmp("mode", optname)) {
9184 + err = do_remount_mode_option(optarg, new_branches,
9185 + tmp_data,
9186 + tmp_lower_paths);
9187 + if (err)
9188 + goto out_release;
9189 + continue;
9190 + }
9191 +
9192 + /*
9193 + * When you use "mount -o remount,ro", mount(8) will
9194 + * reportedly pass the original dirs= string from
9195 + * /proc/mounts. So for now, we have to ignore dirs= and
9196 + * not consider it an error, unless we want to allow users
9197 + * to pass dirs= in remount. Note that to allow the VFS to
9198 + * actually process the ro/rw remount options, we have to
9199 + * return 0 from this function.
9200 + */
9201 + if (!strcmp("dirs", optname)) {
9202 + printk(KERN_WARNING
9203 + "unionfs: remount ignoring option \"%s\"\n",
9204 + optname);
9205 + continue;
9206 + }
9207 +
9208 + err = -EINVAL;
9209 + printk(KERN_ERR
9210 + "unionfs: unrecognized option \"%s\"\n", optname);
9211 + goto out_release;
9212 + }
9213 +
9214 +out_no_change:
9215 +
9216 + /******************************************************************
9217 + * WE'RE ALMOST DONE: check if leftmost branch might be read-only,
9218 + * see if we need to allocate a small-sized new vector, copy the
9219 + * vectors to their correct place, release the refcnt of the older
9220 + * ones, and return. Also handle invalidating any pages that will
9221 + * have to be re-read.
9222 + *******************************************************************/
9223 +
9224 + if (!(tmp_data[0].branchperms & MAY_WRITE)) {
9225 + printk(KERN_ERR "unionfs: leftmost branch cannot be read-only "
9226 + "(use \"remount,ro\" to create a read-only union)\n");
9227 + err = -EINVAL;
9228 + goto out_release;
9229 + }
9230 +
9231 + /* (re)allocate space for new pointers to lower dentry */
9232 + size = new_branches * sizeof(struct unionfs_data);
9233 + new_data = krealloc(tmp_data, size, GFP_KERNEL);
9234 + if (unlikely(!new_data)) {
9235 + err = -ENOMEM;
9236 + goto out_release;
9237 + }
9238 +
9239 + /* allocate space for new pointers to lower paths */
9240 + size = new_branches * sizeof(struct path);
9241 + new_lower_paths = krealloc(tmp_lower_paths, size, GFP_KERNEL);
9242 + if (unlikely(!new_lower_paths)) {
9243 + err = -ENOMEM;
9244 + goto out_release;
9245 + }
9246 +
9247 + /* allocate space for new pointers to lower inodes */
9248 + new_lower_inodes = kcalloc(new_branches,
9249 + sizeof(struct inode *), GFP_KERNEL);
9250 + if (unlikely(!new_lower_inodes)) {
9251 + err = -ENOMEM;
9252 + goto out_release;
9253 + }
9254 +
9255 + /*
9256 + * OK, just before we actually put the new set of branches in place,
9257 + * we need to ensure that our own f/s has no dirty objects left.
9258 + * Luckily, do_remount_sb() already calls shrink_dcache_sb(sb) and
9259 + * fsync_super(sb), taking care of dentries, inodes, and dirty
9260 + * pages. So all that's left is for us to invalidate any leftover
9261 + * (non-dirty) pages to ensure that they will be re-read from the
9262 + * new lower branches (and to support mmap).
9263 + */
9264 +
9265 + /*
9266 + * Once we finish the remounting successfully, our superblock
9267 + * generation number will have increased. This will be detected by
9268 + * our dentry-revalidation code upon subsequent f/s operations
9269 + * through unionfs. The revalidation code will rebuild the union of
9270 + * lower inodes for a given unionfs inode and invalidate any pages
9271 + * of such "stale" inodes (by calling our purge_inode_data
9272 + * function). This revalidation will happen lazily and
9273 + * incrementally, as users perform operations on cached inodes. We
9274 + * would like to encourage this revalidation to happen sooner if
9275 + * possible, so we like to try to invalidate as many other pages in
9276 + * our superblock as we can. We used to call drop_pagecache_sb() or
9277 + * a variant thereof, but either method was racy (drop_caches alone
9278 + * is known to be racy). So now we let the revalidation happen on a
9279 + * per file basis in ->d_revalidate.
9280 + */
9281 +
9282 + /* grab new lower super references; release old ones */
9283 + for (i = 0; i < new_branches; i++)
9284 + atomic_inc(&new_data[i].sb->s_active);
9285 + for (i = 0; i < sbmax(sb); i++)
9286 + atomic_dec(&UNIONFS_SB(sb)->data[i].sb->s_active);
9287 +
9288 + /* copy new vectors into their correct place */
9289 + tmp_data = UNIONFS_SB(sb)->data;
9290 + UNIONFS_SB(sb)->data = new_data;
9291 + new_data = NULL; /* so don't free good pointers below */
9292 + tmp_lower_paths = UNIONFS_D(sb->s_root)->lower_paths;
9293 + UNIONFS_D(sb->s_root)->lower_paths = new_lower_paths;
9294 + new_lower_paths = NULL; /* so don't free good pointers below */
9295 +
9296 + /* update our unionfs_sb_info and root dentry index of last branch */
9297 + i = sbmax(sb); /* save no. of branches to release at end */
9298 + sbend(sb) = new_branches - 1;
9299 + dbend(sb->s_root) = new_branches - 1;
9300 + old_ibstart = ibstart(sb->s_root->d_inode);
9301 + old_ibend = ibend(sb->s_root->d_inode);
9302 + ibend(sb->s_root->d_inode) = new_branches - 1;
9303 + UNIONFS_D(sb->s_root)->bcount = new_branches;
9304 + new_branches = i; /* no. of branches to release below */
9305 +
9306 + /*
9307 + * Update lower inodes: 3 steps
9308 + * 1. grab ref on all new lower inodes
9309 + */
9310 + for (i = dbstart(sb->s_root); i <= dbend(sb->s_root); i++) {
9311 + struct dentry *lower_dentry =
9312 + unionfs_lower_dentry_idx(sb->s_root, i);
9313 + igrab(lower_dentry->d_inode);
9314 + new_lower_inodes[i] = lower_dentry->d_inode;
9315 + }
9316 + /* 2. release reference on all older lower inodes */
9317 + iput_lowers(sb->s_root->d_inode, old_ibstart, old_ibend, true);
9318 + /* 3. update root dentry's inode to new lower_inodes array */
9319 + UNIONFS_I(sb->s_root->d_inode)->lower_inodes = new_lower_inodes;
9320 + new_lower_inodes = NULL;
9321 +
9322 + /* maxbytes may have changed */
9323 + sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
9324 + /* update high branch ID */
9325 + sbhbid(sb) = new_high_branch_id;
9326 +
9327 + /* update our sb->generation for revalidating objects */
9328 + i = atomic_inc_return(&UNIONFS_SB(sb)->generation);
9329 + atomic_set(&UNIONFS_D(sb->s_root)->generation, i);
9330 + atomic_set(&UNIONFS_I(sb->s_root->d_inode)->generation, i);
9331 + if (!(*flags & MS_SILENT))
9332 + pr_info("unionfs: %s: new generation number %d\n",
9333 + UNIONFS_SB(sb)->dev_name, i);
9334 + /* finally, update the root dentry's times */
9335 + unionfs_copy_attr_times(sb->s_root->d_inode);
9336 + err = 0; /* reset to success */
9337 +
9338 + /*
9339 + * The code above falls through to the next label, and releases the
9340 + * refcnts of the older ones (stored in tmp_*): if we fell through
9341 + * here, it means success. However, if we jump directly to this
9342 + * label from any error above, then an error occurred after we
9343 + * grabbed various refcnts, and so we have to release the
9344 + * temporarily constructed structures.
9345 + */
9346 +out_release:
9347 + /* no need to cleanup/release anything in tmp_data */
9348 + if (tmp_lower_paths)
9349 + for (i = 0; i < new_branches; i++)
9350 + path_put(&tmp_lower_paths[i]);
9351 +out_free:
9352 + kfree(tmp_lower_paths);
9353 + kfree(tmp_data);
9354 + kfree(new_lower_paths);
9355 + kfree(new_data);
9356 + kfree(new_lower_inodes);
9357 +out_error:
9358 + unionfs_check_dentry(sb->s_root);
9359 + unionfs_write_unlock(sb);
9360 + return err;
9361 +}
9362 +
9363 +/*
9364 + * Called by iput() when the inode reference count reached zero
9365 + * and the inode is not hashed anywhere. Used to clear anything
9366 + * that needs to be, before the inode is completely destroyed and put
9367 + * on the inode free list.
9368 + *
9369 + * No need to lock sb info's rwsem.
9370 + */
9371 +static void unionfs_clear_inode(struct inode *inode)
9372 +{
9373 + int bindex, bstart, bend;
9374 + struct inode *lower_inode;
9375 + struct list_head *pos, *n;
9376 + struct unionfs_dir_state *rdstate;
9377 +
9378 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9379 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9380 + list_del(&rdstate->cache);
9381 + free_rdstate(rdstate);
9382 + }
9383 +
9384 + /*
9385 + * Decrement a reference to a lower_inode, which was incremented
9386 + * by our read_inode when it was created initially.
9387 + */
9388 + bstart = ibstart(inode);
9389 + bend = ibend(inode);
9390 + if (bstart >= 0) {
9391 + for (bindex = bstart; bindex <= bend; bindex++) {
9392 + lower_inode = unionfs_lower_inode_idx(inode, bindex);
9393 + if (!lower_inode)
9394 + continue;
9395 + unionfs_set_lower_inode_idx(inode, bindex, NULL);
9396 + /* see Documentation/filesystems/unionfs/issues.txt */
9397 + lockdep_off();
9398 + iput(lower_inode);
9399 + lockdep_on();
9400 + }
9401 + }
9402 +
9403 + kfree(UNIONFS_I(inode)->lower_inodes);
9404 + UNIONFS_I(inode)->lower_inodes = NULL;
9405 +}
9406 +
9407 +static struct inode *unionfs_alloc_inode(struct super_block *sb)
9408 +{
9409 + struct unionfs_inode_info *i;
9410 +
9411 + i = kmem_cache_alloc(unionfs_inode_cachep, GFP_KERNEL);
9412 + if (unlikely(!i))
9413 + return NULL;
9414 +
9415 + /* memset everything up to the inode to 0 */
9416 + memset(i, 0, offsetof(struct unionfs_inode_info, vfs_inode));
9417 +
9418 + i->vfs_inode.i_version = 1;
9419 + return &i->vfs_inode;
9420 +}
9421 +
9422 +static void unionfs_destroy_inode(struct inode *inode)
9423 +{
9424 + kmem_cache_free(unionfs_inode_cachep, UNIONFS_I(inode));
9425 +}
9426 +
9427 +/* unionfs inode cache constructor */
9428 +static void init_once(void *obj)
9429 +{
9430 + struct unionfs_inode_info *i = obj;
9431 +
9432 + inode_init_once(&i->vfs_inode);
9433 +}
9434 +
9435 +int unionfs_init_inode_cache(void)
9436 +{
9437 + int err = 0;
9438 +
9439 + unionfs_inode_cachep =
9440 + kmem_cache_create("unionfs_inode_cache",
9441 + sizeof(struct unionfs_inode_info), 0,
9442 + SLAB_RECLAIM_ACCOUNT, init_once);
9443 + if (unlikely(!unionfs_inode_cachep))
9444 + err = -ENOMEM;
9445 + return err;
9446 +}
9447 +
9448 +/* unionfs inode cache destructor */
9449 +void unionfs_destroy_inode_cache(void)
9450 +{
9451 + if (unionfs_inode_cachep)
9452 + kmem_cache_destroy(unionfs_inode_cachep);
9453 +}
9454 +
9455 +/*
9456 + * Called when we have a dirty inode, right here we only throw out
9457 + * parts of our readdir list that are too old.
9458 + *
9459 + * No need to grab sb info's rwsem.
9460 + */
9461 +static int unionfs_write_inode(struct inode *inode, int sync)
9462 +{
9463 + struct list_head *pos, *n;
9464 + struct unionfs_dir_state *rdstate;
9465 +
9466 + spin_lock(&UNIONFS_I(inode)->rdlock);
9467 + list_for_each_safe(pos, n, &UNIONFS_I(inode)->readdircache) {
9468 + rdstate = list_entry(pos, struct unionfs_dir_state, cache);
9469 + /* We keep this list in LRU order. */
9470 + if ((rdstate->access + RDCACHE_JIFFIES) > jiffies)
9471 + break;
9472 + UNIONFS_I(inode)->rdcount--;
9473 + list_del(&rdstate->cache);
9474 + free_rdstate(rdstate);
9475 + }
9476 + spin_unlock(&UNIONFS_I(inode)->rdlock);
9477 +
9478 + return 0;
9479 +}
9480 +
9481 +/*
9482 + * Used only in nfs, to kill any pending RPC tasks, so that subsequent
9483 + * code can actually succeed and won't leave tasks that need handling.
9484 + */
9485 +static void unionfs_umount_begin(struct super_block *sb)
9486 +{
9487 + struct super_block *lower_sb;
9488 + int bindex, bstart, bend;
9489 +
9490 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9491 +
9492 + bstart = sbstart(sb);
9493 + bend = sbend(sb);
9494 + for (bindex = bstart; bindex <= bend; bindex++) {
9495 + lower_sb = unionfs_lower_super_idx(sb, bindex);
9496 +
9497 + if (lower_sb && lower_sb->s_op &&
9498 + lower_sb->s_op->umount_begin)
9499 + lower_sb->s_op->umount_begin(lower_sb);
9500 + }
9501 +
9502 + unionfs_read_unlock(sb);
9503 +}
9504 +
9505 +static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
9506 +{
9507 + struct super_block *sb = mnt->mnt_sb;
9508 + int ret = 0;
9509 + char *tmp_page;
9510 + char *path;
9511 + int bindex, bstart, bend;
9512 + int perms;
9513 +
9514 + unionfs_read_lock(sb, UNIONFS_SMUTEX_CHILD);
9515 +
9516 + unionfs_lock_dentry(sb->s_root, UNIONFS_DMUTEX_CHILD);
9517 +
9518 + tmp_page = (char *) __get_free_page(GFP_KERNEL);
9519 + if (unlikely(!tmp_page)) {
9520 + ret = -ENOMEM;
9521 + goto out;
9522 + }
9523 +
9524 + bstart = sbstart(sb);
9525 + bend = sbend(sb);
9526 +
9527 + seq_printf(m, ",dirs=");
9528 + for (bindex = bstart; bindex <= bend; bindex++) {
9529 + struct path p;
9530 + p.dentry = unionfs_lower_dentry_idx(sb->s_root, bindex);
9531 + p.mnt = unionfs_lower_mnt_idx(sb->s_root, bindex);
9532 + path = d_path(&p, tmp_page, PAGE_SIZE);
9533 + if (IS_ERR(path)) {
9534 + ret = PTR_ERR(path);
9535 + goto out;
9536 + }
9537 +
9538 + perms = branchperms(sb, bindex);
9539 +
9540 + seq_printf(m, "%s=%s", path,
9541 + perms & MAY_WRITE ? "rw" : "ro");
9542 + if (bindex != bend)
9543 + seq_printf(m, ":");
9544 + }
9545 +
9546 +out:
9547 + free_page((unsigned long) tmp_page);
9548 +
9549 + unionfs_unlock_dentry(sb->s_root);
9550 +
9551 + unionfs_read_unlock(sb);
9552 +
9553 + return ret;
9554 +}
9555 +
9556 +struct super_operations unionfs_sops = {
9557 + .delete_inode = unionfs_delete_inode,
9558 + .put_super = unionfs_put_super,
9559 + .statfs = unionfs_statfs,
9560 + .remount_fs = unionfs_remount_fs,
9561 + .clear_inode = unionfs_clear_inode,
9562 + .umount_begin = unionfs_umount_begin,
9563 + .show_options = unionfs_show_options,
9564 + .write_inode = unionfs_write_inode,
9565 + .alloc_inode = unionfs_alloc_inode,
9566 + .destroy_inode = unionfs_destroy_inode,
9567 +};
9568 diff --git a/fs/unionfs/union.h b/fs/unionfs/union.h
9569 new file mode 100644
9570 index 0000000..17a0056
9571 --- /dev/null
9572 +++ b/fs/unionfs/union.h
9573 @@ -0,0 +1,670 @@
9574 +/*
9575 + * Copyright (c) 2003-2009 Erez Zadok
9576 + * Copyright (c) 2003-2006 Charles P. Wright
9577 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
9578 + * Copyright (c) 2005 Arun M. Krishnakumar
9579 + * Copyright (c) 2004-2006 David P. Quigley
9580 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9581 + * Copyright (c) 2003 Puja Gupta
9582 + * Copyright (c) 2003 Harikesavan Krishnan
9583 + * Copyright (c) 2003-2009 Stony Brook University
9584 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
9585 + *
9586 + * This program is free software; you can redistribute it and/or modify
9587 + * it under the terms of the GNU General Public License version 2 as
9588 + * published by the Free Software Foundation.
9589 + */
9590 +
9591 +#ifndef _UNION_H_
9592 +#define _UNION_H_
9593 +
9594 +#include <linux/dcache.h>
9595 +#include <linux/file.h>
9596 +#include <linux/list.h>
9597 +#include <linux/fs.h>
9598 +#include <linux/mm.h>
9599 +#include <linux/module.h>
9600 +#include <linux/mount.h>
9601 +#include <linux/namei.h>
9602 +#include <linux/page-flags.h>
9603 +#include <linux/pagemap.h>
9604 +#include <linux/poll.h>
9605 +#include <linux/security.h>
9606 +#include <linux/seq_file.h>
9607 +#include <linux/slab.h>
9608 +#include <linux/spinlock.h>
9609 +#include <linux/smp_lock.h>
9610 +#include <linux/statfs.h>
9611 +#include <linux/string.h>
9612 +#include <linux/vmalloc.h>
9613 +#include <linux/writeback.h>
9614 +#include <linux/buffer_head.h>
9615 +#include <linux/xattr.h>
9616 +#include <linux/fs_stack.h>
9617 +#include <linux/magic.h>
9618 +#include <linux/log2.h>
9619 +#include <linux/poison.h>
9620 +#include <linux/mman.h>
9621 +#include <linux/backing-dev.h>
9622 +#include <linux/splice.h>
9623 +
9624 +#include <asm/system.h>
9625 +
9626 +#include <linux/union_fs.h>
9627 +
9628 +/* the file system name */
9629 +#define UNIONFS_NAME "unionfs"
9630 +
9631 +/* unionfs root inode number */
9632 +#define UNIONFS_ROOT_INO 1
9633 +
9634 +/* number of times we try to get a unique temporary file name */
9635 +#define GET_TMPNAM_MAX_RETRY 5
9636 +
9637 +/* maximum number of branches we support, to avoid memory blowup */
9638 +#define UNIONFS_MAX_BRANCHES 128
9639 +
9640 +/* minimum time (seconds) required for time-based cache-coherency */
9641 +#define UNIONFS_MIN_CC_TIME 3
9642 +
9643 +/* Operations vectors defined in specific files. */
9644 +extern struct file_operations unionfs_main_fops;
9645 +extern struct file_operations unionfs_dir_fops;
9646 +extern struct inode_operations unionfs_main_iops;
9647 +extern struct inode_operations unionfs_dir_iops;
9648 +extern struct inode_operations unionfs_symlink_iops;
9649 +extern struct super_operations unionfs_sops;
9650 +extern struct dentry_operations unionfs_dops;
9651 +extern struct address_space_operations unionfs_aops, unionfs_dummy_aops;
9652 +extern struct vm_operations_struct unionfs_vm_ops;
9653 +
9654 +/* How long should an entry be allowed to persist */
9655 +#define RDCACHE_JIFFIES (5*HZ)
9656 +
9657 +/* compatibility with Real-Time patches */
9658 +#ifdef CONFIG_PREEMPT_RT
9659 +# define unionfs_rw_semaphore compat_rw_semaphore
9660 +#else /* not CONFIG_PREEMPT_RT */
9661 +# define unionfs_rw_semaphore rw_semaphore
9662 +#endif /* not CONFIG_PREEMPT_RT */
9663 +
9664 +/* file private data. */
9665 +struct unionfs_file_info {
9666 + int bstart;
9667 + int bend;
9668 + atomic_t generation;
9669 +
9670 + struct unionfs_dir_state *rdstate;
9671 + struct file **lower_files;
9672 + int *saved_branch_ids; /* IDs of branches when file was opened */
9673 + struct vm_operations_struct *lower_vm_ops;
9674 + bool wrote_to_file; /* for delayed copyup */
9675 +};
9676 +
9677 +/* unionfs inode data in memory */
9678 +struct unionfs_inode_info {
9679 + int bstart;
9680 + int bend;
9681 + atomic_t generation;
9682 + /* Stuff for readdir over NFS. */
9683 + spinlock_t rdlock;
9684 + struct list_head readdircache;
9685 + int rdcount;
9686 + int hashsize;
9687 + int cookie;
9688 +
9689 + /* The lower inodes */
9690 + struct inode **lower_inodes;
9691 +
9692 + struct inode vfs_inode;
9693 +};
9694 +
9695 +/* unionfs dentry data in memory */
9696 +struct unionfs_dentry_info {
9697 + /*
9698 + * The semaphore is used to lock the dentry as soon as we get into a
9699 + * unionfs function from the VFS. Our lock ordering is that children
9700 + * go before their parents.
9701 + */
9702 + struct mutex lock;
9703 + int bstart;
9704 + int bend;
9705 + int bopaque;
9706 + int bcount;
9707 + atomic_t generation;
9708 + struct path *lower_paths;
9709 +};
9710 +
9711 +/* These are the pointers to our various objects. */
9712 +struct unionfs_data {
9713 + struct super_block *sb; /* lower super_block */
9714 + atomic_t open_files; /* number of open files on branch */
9715 + int branchperms;
9716 + int branch_id; /* unique branch ID at re/mount time */
9717 +};
9718 +
9719 +/* unionfs super-block data in memory */
9720 +struct unionfs_sb_info {
9721 + int bend;
9722 +
9723 + atomic_t generation;
9724 +
9725 + /*
9726 + * This rwsem is used to make sure that a branch management
9727 + * operation...
9728 + * 1) will not begin before all currently in-flight operations
9729 + * complete.
9730 + * 2) any new operations do not execute until the currently
9731 + * running branch management operation completes.
9732 + *
9733 + * The write_lock_owner records the PID of the task which grabbed
9734 + * the rw_sem for writing. If the same task also tries to grab the
9735 + * read lock, we allow it. This prevents a self-deadlock when
9736 + * branch-management is used on a pivot_root'ed union, because we
9737 + * have to ->lookup paths which belong to the same union.
9738 + */
9739 + struct unionfs_rw_semaphore rwsem;
9740 + pid_t write_lock_owner; /* PID of rw_sem owner (write lock) */
9741 + int high_branch_id; /* last unique branch ID given */
9742 + char *dev_name; /* to identify different unions in pr_debug */
9743 + struct unionfs_data *data;
9744 +};
9745 +
9746 +/*
9747 + * structure for making the linked list of entries by readdir on left branch
9748 + * to compare with entries on right branch
9749 + */
9750 +struct filldir_node {
9751 + struct list_head file_list; /* list for directory entries */
9752 + char *name; /* name entry */
9753 + int hash; /* name hash */
9754 + int namelen; /* name len since name is not 0 terminated */
9755 +
9756 + /*
9757 + * we can check for duplicate whiteouts and files in the same branch
9758 + * in order to return -EIO.
9759 + */
9760 + int bindex;
9761 +
9762 + /* is this a whiteout entry? */
9763 + int whiteout;
9764 +
9765 + /* Inline name, so we don't need to separately kmalloc small ones */
9766 + char iname[DNAME_INLINE_LEN_MIN];
9767 +};
9768 +
9769 +/* Directory hash table. */
9770 +struct unionfs_dir_state {
9771 + unsigned int cookie; /* the cookie, based off of rdversion */
9772 + unsigned int offset; /* The entry we have returned. */
9773 + int bindex;
9774 + loff_t dirpos; /* offset within the lower level directory */
9775 + int size; /* How big is the hash table? */
9776 + int hashentries; /* How many entries have been inserted? */
9777 + unsigned long access;
9778 +
9779 + /* This cache list is used when the inode keeps us around. */
9780 + struct list_head cache;
9781 + struct list_head list[0];
9782 +};
9783 +
9784 +/* externs needed for fanout.h or sioq.h */
9785 +extern int unionfs_get_nlinks(const struct inode *inode);
9786 +extern void unionfs_copy_attr_times(struct inode *upper);
9787 +extern void unionfs_copy_attr_all(struct inode *dest, const struct inode *src);
9788 +
9789 +/* include miscellaneous macros */
9790 +#include "fanout.h"
9791 +#include "sioq.h"
9792 +
9793 +/* externs for cache creation/deletion routines */
9794 +extern void unionfs_destroy_filldir_cache(void);
9795 +extern int unionfs_init_filldir_cache(void);
9796 +extern int unionfs_init_inode_cache(void);
9797 +extern void unionfs_destroy_inode_cache(void);
9798 +extern int unionfs_init_dentry_cache(void);
9799 +extern void unionfs_destroy_dentry_cache(void);
9800 +
9801 +/* Initialize and free readdir-specific state. */
9802 +extern int init_rdstate(struct file *file);
9803 +extern struct unionfs_dir_state *alloc_rdstate(struct inode *inode,
9804 + int bindex);
9805 +extern struct unionfs_dir_state *find_rdstate(struct inode *inode,
9806 + loff_t fpos);
9807 +extern void free_rdstate(struct unionfs_dir_state *state);
9808 +extern int add_filldir_node(struct unionfs_dir_state *rdstate,
9809 + const char *name, int namelen, int bindex,
9810 + int whiteout);
9811 +extern struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
9812 + const char *name, int namelen,
9813 + int is_whiteout);
9814 +
9815 +extern struct dentry **alloc_new_dentries(int objs);
9816 +extern struct unionfs_data *alloc_new_data(int objs);
9817 +
9818 +/* We can only use 32-bits of offset for rdstate --- blech! */
9819 +#define DIREOF (0xfffff)
9820 +#define RDOFFBITS 20 /* This is the number of bits in DIREOF. */
9821 +#define MAXRDCOOKIE (0xfff)
9822 +/* Turn an rdstate into an offset. */
9823 +static inline off_t rdstate2offset(struct unionfs_dir_state *buf)
9824 +{
9825 + off_t tmp;
9826 +
9827 + tmp = ((buf->cookie & MAXRDCOOKIE) << RDOFFBITS)
9828 + | (buf->offset & DIREOF);
9829 + return tmp;
9830 +}
9831 +
9832 +/* Macros for locking a super_block. */
9833 +enum unionfs_super_lock_class {
9834 + UNIONFS_SMUTEX_NORMAL,
9835 + UNIONFS_SMUTEX_PARENT, /* when locking on behalf of file */
9836 + UNIONFS_SMUTEX_CHILD, /* when locking on behalf of dentry */
9837 +};
9838 +static inline void unionfs_read_lock(struct super_block *sb, int subclass)
9839 +{
9840 + if (UNIONFS_SB(sb)->write_lock_owner &&
9841 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9842 + return;
9843 + down_read_nested(&UNIONFS_SB(sb)->rwsem, subclass);
9844 +}
9845 +static inline void unionfs_read_unlock(struct super_block *sb)
9846 +{
9847 + if (UNIONFS_SB(sb)->write_lock_owner &&
9848 + UNIONFS_SB(sb)->write_lock_owner == current->pid)
9849 + return;
9850 + up_read(&UNIONFS_SB(sb)->rwsem);
9851 +}
9852 +static inline void unionfs_write_lock(struct super_block *sb)
9853 +{
9854 + down_write(&UNIONFS_SB(sb)->rwsem);
9855 + UNIONFS_SB(sb)->write_lock_owner = current->pid;
9856 +}
9857 +static inline void unionfs_write_unlock(struct super_block *sb)
9858 +{
9859 + up_write(&UNIONFS_SB(sb)->rwsem);
9860 + UNIONFS_SB(sb)->write_lock_owner = 0;
9861 +}
9862 +
9863 +static inline void unionfs_double_lock_dentry(struct dentry *d1,
9864 + struct dentry *d2)
9865 +{
9866 + BUG_ON(d1 == d2);
9867 + if (d1 < d2) {
9868 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_PARENT);
9869 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_CHILD);
9870 + } else {
9871 + unionfs_lock_dentry(d2, UNIONFS_DMUTEX_PARENT);
9872 + unionfs_lock_dentry(d1, UNIONFS_DMUTEX_CHILD);
9873 + }
9874 +}
9875 +
9876 +static inline void unionfs_double_unlock_dentry(struct dentry *d1,
9877 + struct dentry *d2)
9878 +{
9879 + BUG_ON(d1 == d2);
9880 + if (d1 < d2) { /* unlock in reverse order than double_lock_dentry */
9881 + unionfs_unlock_dentry(d1);
9882 + unionfs_unlock_dentry(d2);
9883 + } else {
9884 + unionfs_unlock_dentry(d2);
9885 + unionfs_unlock_dentry(d1);
9886 + }
9887 +}
9888 +
9889 +static inline void unionfs_double_lock_parents(struct dentry *p1,
9890 + struct dentry *p2)
9891 +{
9892 + if (p1 == p2) {
9893 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9894 + return;
9895 + }
9896 + if (p1 < p2) {
9897 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_PARENT);
9898 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_CHILD);
9899 + } else {
9900 + unionfs_lock_dentry(p2, UNIONFS_DMUTEX_REVAL_PARENT);
9901 + unionfs_lock_dentry(p1, UNIONFS_DMUTEX_REVAL_CHILD);
9902 + }
9903 +}
9904 +
9905 +static inline void unionfs_double_unlock_parents(struct dentry *p1,
9906 + struct dentry *p2)
9907 +{
9908 + if (p1 == p2) {
9909 + unionfs_unlock_dentry(p1);
9910 + return;
9911 + }
9912 + if (p1 < p2) { /* unlock in reverse order of double_lock_parents */
9913 + unionfs_unlock_dentry(p1);
9914 + unionfs_unlock_dentry(p2);
9915 + } else {
9916 + unionfs_unlock_dentry(p2);
9917 + unionfs_unlock_dentry(p1);
9918 + }
9919 +}
9920 +
9921 +extern int new_dentry_private_data(struct dentry *dentry, int subclass);
9922 +extern int realloc_dentry_private_data(struct dentry *dentry);
9923 +extern void free_dentry_private_data(struct dentry *dentry);
9924 +extern void update_bstart(struct dentry *dentry);
9925 +extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
9926 +extern void release_lower_nd(struct nameidata *nd, int err);
9927 +
9928 +/*
9929 + * EXTERNALS:
9930 + */
9931 +
9932 +/* replicates the directory structure up to given dentry in given branch */
9933 +extern struct dentry *create_parents(struct inode *dir, struct dentry *dentry,
9934 + const char *name, int bindex);
9935 +
9936 +/* partial lookup */
9937 +extern int unionfs_partial_lookup(struct dentry *dentry,
9938 + struct dentry *parent);
9939 +extern struct dentry *unionfs_lookup_full(struct dentry *dentry,
9940 + struct dentry *parent,
9941 + int lookupmode);
9942 +
9943 +/* copies a file from dbstart to newbindex branch */
9944 +extern int copyup_file(struct inode *dir, struct file *file, int bstart,
9945 + int newbindex, loff_t size);
9946 +extern int copyup_named_file(struct inode *dir, struct file *file,
9947 + char *name, int bstart, int new_bindex,
9948 + loff_t len);
9949 +/* copies a dentry from dbstart to newbindex branch */
9950 +extern int copyup_dentry(struct inode *dir, struct dentry *dentry,
9951 + int bstart, int new_bindex, const char *name,
9952 + int namelen, struct file **copyup_file, loff_t len);
9953 +/* helper functions for post-copyup actions */
9954 +extern void unionfs_postcopyup_setmnt(struct dentry *dentry);
9955 +extern void unionfs_postcopyup_release(struct dentry *dentry);
9956 +
9957 +/* Is this directory empty: 0 if it is empty, -ENOTEMPTY if not. */
9958 +extern int check_empty(struct dentry *dentry, struct dentry *parent,
9959 + struct unionfs_dir_state **namelist);
9960 +/* whiteout and opaque directory helpers */
9961 +extern char *alloc_whname(const char *name, int len);
9962 +extern bool is_whiteout_name(char **namep, int *namelenp);
9963 +extern bool is_validname(const char *name);
9964 +extern struct dentry *lookup_whiteout(const char *name,
9965 + struct dentry *lower_parent);
9966 +extern struct dentry *find_first_whiteout(struct dentry *dentry);
9967 +extern int unlink_whiteout(struct dentry *wh_dentry);
9968 +extern int check_unlink_whiteout(struct dentry *dentry,
9969 + struct dentry *lower_dentry, int bindex);
9970 +extern int create_whiteout(struct dentry *dentry, int start);
9971 +extern int delete_whiteouts(struct dentry *dentry, int bindex,
9972 + struct unionfs_dir_state *namelist);
9973 +extern int is_opaque_dir(struct dentry *dentry, int bindex);
9974 +extern int make_dir_opaque(struct dentry *dir, int bindex);
9975 +extern void unionfs_set_max_namelen(long *namelen);
9976 +
9977 +extern void unionfs_reinterpose(struct dentry *this_dentry);
9978 +extern struct super_block *unionfs_duplicate_super(struct super_block *sb);
9979 +
9980 +/* Locking functions. */
9981 +extern int unionfs_setlk(struct file *file, int cmd, struct file_lock *fl);
9982 +extern int unionfs_getlk(struct file *file, struct file_lock *fl);
9983 +
9984 +/* Common file operations. */
9985 +extern int unionfs_file_revalidate(struct file *file, struct dentry *parent,
9986 + bool willwrite);
9987 +extern int unionfs_open(struct inode *inode, struct file *file);
9988 +extern int unionfs_file_release(struct inode *inode, struct file *file);
9989 +extern int unionfs_flush(struct file *file, fl_owner_t id);
9990 +extern long unionfs_ioctl(struct file *file, unsigned int cmd,
9991 + unsigned long arg);
9992 +extern int unionfs_fsync(struct file *file, struct dentry *dentry,
9993 + int datasync);
9994 +extern int unionfs_fasync(int fd, struct file *file, int flag);
9995 +
9996 +/* Inode operations */
9997 +extern struct inode *unionfs_iget(struct super_block *sb, unsigned long ino);
9998 +extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9999 + struct inode *new_dir, struct dentry *new_dentry);
10000 +extern int unionfs_unlink(struct inode *dir, struct dentry *dentry);
10001 +extern int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
10002 +
10003 +extern bool __unionfs_d_revalidate(struct dentry *dentry,
10004 + struct dentry *parent, bool willwrite);
10005 +extern bool is_negative_lower(const struct dentry *dentry);
10006 +extern bool is_newer_lower(const struct dentry *dentry);
10007 +extern void purge_sb_data(struct super_block *sb);
10008 +
10009 +/* The values for unionfs_interpose's flag. */
10010 +#define INTERPOSE_DEFAULT 0
10011 +#define INTERPOSE_LOOKUP 1
10012 +#define INTERPOSE_REVAL 2
10013 +#define INTERPOSE_REVAL_NEG 3
10014 +#define INTERPOSE_PARTIAL 4
10015 +
10016 +extern struct dentry *unionfs_interpose(struct dentry *this_dentry,
10017 + struct super_block *sb, int flag);
10018 +
10019 +#ifdef CONFIG_UNION_FS_XATTR
10020 +/* Extended attribute functions. */
10021 +extern void *unionfs_xattr_alloc(size_t size, size_t limit);
10022 +static inline void unionfs_xattr_kfree(const void *p)
10023 +{
10024 + kfree(p);
10025 +}
10026 +extern ssize_t unionfs_getxattr(struct dentry *dentry, const char *name,
10027 + void *value, size_t size);
10028 +extern int unionfs_removexattr(struct dentry *dentry, const char *name);
10029 +extern ssize_t unionfs_listxattr(struct dentry *dentry, char *list,
10030 + size_t size);
10031 +extern int unionfs_setxattr(struct dentry *dentry, const char *name,
10032 + const void *value, size_t size, int flags);
10033 +#endif /* CONFIG_UNION_FS_XATTR */
10034 +
10035 +/* The root directory is unhashed, but isn't deleted. */
10036 +static inline int d_deleted(struct dentry *d)
10037 +{
10038 + return d_unhashed(d) && (d != d->d_sb->s_root);
10039 +}
10040 +
10041 +/* unionfs_permission, check if we should bypass error to facilitate copyup */
10042 +#define IS_COPYUP_ERR(err) ((err) == -EROFS)
10043 +
10044 +/* unionfs_open, check if we need to copyup the file */
10045 +#define OPEN_WRITE_FLAGS (O_WRONLY | O_RDWR | O_APPEND)
10046 +#define IS_WRITE_FLAG(flag) ((flag) & OPEN_WRITE_FLAGS)
10047 +
10048 +static inline int branchperms(const struct super_block *sb, int index)
10049 +{
10050 + BUG_ON(index < 0);
10051 + return UNIONFS_SB(sb)->data[index].branchperms;
10052 +}
10053 +
10054 +static inline int set_branchperms(struct super_block *sb, int index, int perms)
10055 +{
10056 + BUG_ON(index < 0);
10057 + UNIONFS_SB(sb)->data[index].branchperms = perms;
10058 + return perms;
10059 +}
10060 +
10061 +/* check if readonly lower inode, but possibly unlinked (no inode->i_sb) */
10062 +static inline int __is_rdonly(const struct inode *inode)
10063 +{
10064 + /* if unlinked, can't be readonly (?) */
10065 + if (!inode->i_sb)
10066 + return 0;
10067 + return IS_RDONLY(inode);
10068 +
10069 +}
10070 +/* Is this file on a read-only branch? */
10071 +static inline int is_robranch_super(const struct super_block *sb, int index)
10072 +{
10073 + int ret;
10074 +
10075 + ret = (!(branchperms(sb, index) & MAY_WRITE)) ? -EROFS : 0;
10076 + return ret;
10077 +}
10078 +
10079 +/* Is this file on a read-only branch? */
10080 +static inline int is_robranch_idx(const struct dentry *dentry, int index)
10081 +{
10082 + struct super_block *lower_sb;
10083 +
10084 + BUG_ON(index < 0);
10085 +
10086 + if (!(branchperms(dentry->d_sb, index) & MAY_WRITE))
10087 + return -EROFS;
10088 +
10089 + lower_sb = unionfs_lower_super_idx(dentry->d_sb, index);
10090 + BUG_ON(lower_sb == NULL);
10091 + /*
10092 + * test sb flags directly, not IS_RDONLY(lower_inode) because the
10093 + * lower_dentry could be a negative.
10094 + */
10095 + if (lower_sb->s_flags & MS_RDONLY)
10096 + return -EROFS;
10097 +
10098 + return 0;
10099 +}
10100 +
10101 +static inline int is_robranch(const struct dentry *dentry)
10102 +{
10103 + int index;
10104 +
10105 + index = UNIONFS_D(dentry)->bstart;
10106 + BUG_ON(index < 0);
10107 +
10108 + return is_robranch_idx(dentry, index);
10109 +}
10110 +
10111 +/*
10112 + * EXTERNALS:
10113 + */
10114 +extern int check_branch(struct nameidata *nd);
10115 +extern int parse_branch_mode(const char *name, int *perms);
10116 +
10117 +/* locking helpers */
10118 +static inline struct dentry *lock_parent(struct dentry *dentry)
10119 +{
10120 + struct dentry *dir = dget_parent(dentry);
10121 + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
10122 + return dir;
10123 +}
10124 +static inline struct dentry *lock_parent_wh(struct dentry *dentry)
10125 +{
10126 + struct dentry *dir = dget_parent(dentry);
10127 +
10128 + mutex_lock_nested(&dir->d_inode->i_mutex, UNIONFS_DMUTEX_WHITEOUT);
10129 + return dir;
10130 +}
10131 +
10132 +static inline void unlock_dir(struct dentry *dir)
10133 +{
10134 + mutex_unlock(&dir->d_inode->i_mutex);
10135 + dput(dir);
10136 +}
10137 +
10138 +/* lock base inode mutex before calling lookup_one_len */
10139 +static inline struct dentry *lookup_lck_len(const char *name,
10140 + struct dentry *base, int len)
10141 +{
10142 + struct dentry *d;
10143 + mutex_lock(&base->d_inode->i_mutex);
10144 + d = lookup_one_len(name, base, len);
10145 + mutex_unlock(&base->d_inode->i_mutex);
10146 + return d;
10147 +}
10148 +
10149 +static inline struct vfsmount *unionfs_mntget(struct dentry *dentry,
10150 + int bindex)
10151 +{
10152 + struct vfsmount *mnt;
10153 +
10154 + BUG_ON(!dentry || bindex < 0);
10155 +
10156 + mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex));
10157 +#ifdef CONFIG_UNION_FS_DEBUG
10158 + if (!mnt)
10159 + pr_debug("unionfs: mntget: mnt=%p bindex=%d\n",
10160 + mnt, bindex);
10161 +#endif /* CONFIG_UNION_FS_DEBUG */
10162 +
10163 + return mnt;
10164 +}
10165 +
10166 +static inline void unionfs_mntput(struct dentry *dentry, int bindex)
10167 +{
10168 + struct vfsmount *mnt;
10169 +
10170 + if (!dentry && bindex < 0)
10171 + return;
10172 + BUG_ON(!dentry || bindex < 0);
10173 +
10174 + mnt = unionfs_lower_mnt_idx(dentry, bindex);
10175 +#ifdef CONFIG_UNION_FS_DEBUG
10176 + /*
10177 + * Directories can have NULL lower objects in between start/end, but
10178 + * NOT if at the start/end range. We cannot verify that this dentry
10179 + * is a type=DIR, because it may already be a negative dentry. But
10180 + * if dbstart is greater than dbend, we know that this couldn't have
10181 + * been a regular file: it had to have been a directory.
10182 + */
10183 + if (!mnt && !(bindex > dbstart(dentry) && bindex < dbend(dentry)))
10184 + pr_debug("unionfs: mntput: mnt=%p bindex=%d\n", mnt, bindex);
10185 +#endif /* CONFIG_UNION_FS_DEBUG */
10186 + mntput(mnt);
10187 +}
10188 +
10189 +#ifdef CONFIG_UNION_FS_DEBUG
10190 +
10191 +/* useful for tracking code reachability */
10192 +#define UDBG pr_debug("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
10193 +
10194 +#define unionfs_check_inode(i) __unionfs_check_inode((i), \
10195 + __FILE__, __func__, __LINE__)
10196 +#define unionfs_check_dentry(d) __unionfs_check_dentry((d), \
10197 + __FILE__, __func__, __LINE__)
10198 +#define unionfs_check_file(f) __unionfs_check_file((f), \
10199 + __FILE__, __func__, __LINE__)
10200 +#define unionfs_check_nd(n) __unionfs_check_nd((n), \
10201 + __FILE__, __func__, __LINE__)
10202 +#define show_branch_counts(sb) __show_branch_counts((sb), \
10203 + __FILE__, __func__, __LINE__)
10204 +#define show_inode_times(i) __show_inode_times((i), \
10205 + __FILE__, __func__, __LINE__)
10206 +#define show_dinode_times(d) __show_dinode_times((d), \
10207 + __FILE__, __func__, __LINE__)
10208 +#define show_inode_counts(i) __show_inode_counts((i), \
10209 + __FILE__, __func__, __LINE__)
10210 +
10211 +extern void __unionfs_check_inode(const struct inode *inode, const char *fname,
10212 + const char *fxn, int line);
10213 +extern void __unionfs_check_dentry(const struct dentry *dentry,
10214 + const char *fname, const char *fxn,
10215 + int line);
10216 +extern void __unionfs_check_file(const struct file *file,
10217 + const char *fname, const char *fxn, int line);
10218 +extern void __unionfs_check_nd(const struct nameidata *nd,
10219 + const char *fname, const char *fxn, int line);
10220 +extern void __show_branch_counts(const struct super_block *sb,
10221 + const char *file, const char *fxn, int line);
10222 +extern void __show_inode_times(const struct inode *inode,
10223 + const char *file, const char *fxn, int line);
10224 +extern void __show_dinode_times(const struct dentry *dentry,
10225 + const char *file, const char *fxn, int line);
10226 +extern void __show_inode_counts(const struct inode *inode,
10227 + const char *file, const char *fxn, int line);
10228 +
10229 +#else /* not CONFIG_UNION_FS_DEBUG */
10230 +
10231 +/* we leave useful hooks for these check functions throughout the code */
10232 +#define unionfs_check_inode(i) do { } while (0)
10233 +#define unionfs_check_dentry(d) do { } while (0)
10234 +#define unionfs_check_file(f) do { } while (0)
10235 +#define unionfs_check_nd(n) do { } while (0)
10236 +#define show_branch_counts(sb) do { } while (0)
10237 +#define show_inode_times(i) do { } while (0)
10238 +#define show_dinode_times(d) do { } while (0)
10239 +#define show_inode_counts(i) do { } while (0)
10240 +
10241 +#endif /* not CONFIG_UNION_FS_DEBUG */
10242 +
10243 +#endif /* not _UNION_H_ */
10244 diff --git a/fs/unionfs/unlink.c b/fs/unionfs/unlink.c
10245 new file mode 100644
10246 index 0000000..b6d8e10
10247 --- /dev/null
10248 +++ b/fs/unionfs/unlink.c
10249 @@ -0,0 +1,282 @@
10250 +/*
10251 + * Copyright (c) 2003-2009 Erez Zadok
10252 + * Copyright (c) 2003-2006 Charles P. Wright
10253 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10254 + * Copyright (c) 2005-2006 Junjiro Okajima
10255 + * Copyright (c) 2005 Arun M. Krishnakumar
10256 + * Copyright (c) 2004-2006 David P. Quigley
10257 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10258 + * Copyright (c) 2003 Puja Gupta
10259 + * Copyright (c) 2003 Harikesavan Krishnan
10260 + * Copyright (c) 2003-2009 Stony Brook University
10261 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10262 + *
10263 + * This program is free software; you can redistribute it and/or modify
10264 + * it under the terms of the GNU General Public License version 2 as
10265 + * published by the Free Software Foundation.
10266 + */
10267 +
10268 +#include "union.h"
10269 +
10270 +/*
10271 + * Helper function for Unionfs's unlink operation.
10272 + *
10273 + * The main goal of this function is to optimize the unlinking of non-dir
10274 + * objects in unionfs by deleting all possible lower inode objects from the
10275 + * underlying branches having same dentry name as the non-dir dentry on
10276 + * which this unlink operation is called. This way we delete as many lower
10277 + * inodes as possible, and save space. Whiteouts need to be created in
10278 + * branch0 only if unlinking fails on any of the lower branch other than
10279 + * branch0, or if a lower branch is marked read-only.
10280 + *
10281 + * Also, while unlinking a file, if we encounter any dir type entry in any
10282 + * intermediate branch, then we remove the directory by calling vfs_rmdir.
10283 + * The following special cases are also handled:
10284 +
10285 + * (1) If an error occurs in branch0 during vfs_unlink, then we return
10286 + * appropriate error.
10287 + *
10288 + * (2) If we get an error during unlink in any of other lower branch other
10289 + * than branch0, then we create a whiteout in branch0.
10290 + *
10291 + * (3) If a whiteout already exists in any intermediate branch, we delete
10292 + * all possible inodes only up to that branch (this is an "opaqueness"
10293 + * as as per Documentation/filesystems/unionfs/concepts.txt).
10294 + *
10295 + */
10296 +static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry,
10297 + struct dentry *parent)
10298 +{
10299 + struct dentry *lower_dentry;
10300 + struct dentry *lower_dir_dentry;
10301 + int bindex;
10302 + int err = 0;
10303 +
10304 + err = unionfs_partial_lookup(dentry, parent);
10305 + if (err)
10306 + goto out;
10307 +
10308 + /* trying to unlink all possible valid instances */
10309 + for (bindex = dbstart(dentry); bindex <= dbend(dentry); bindex++) {
10310 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10311 + if (!lower_dentry || !lower_dentry->d_inode)
10312 + continue;
10313 +
10314 + lower_dir_dentry = lock_parent(lower_dentry);
10315 +
10316 + /* avoid destroying the lower inode if the object is in use */
10317 + dget(lower_dentry);
10318 + err = is_robranch_super(dentry->d_sb, bindex);
10319 + if (!err) {
10320 + /* see Documentation/filesystems/unionfs/issues.txt */
10321 + lockdep_off();
10322 + if (!S_ISDIR(lower_dentry->d_inode->i_mode))
10323 + err = vfs_unlink(lower_dir_dentry->d_inode,
10324 + lower_dentry);
10325 + else
10326 + err = vfs_rmdir(lower_dir_dentry->d_inode,
10327 + lower_dentry);
10328 + lockdep_on();
10329 + }
10330 +
10331 + /* if lower object deletion succeeds, update inode's times */
10332 + if (!err)
10333 + unionfs_copy_attr_times(dentry->d_inode);
10334 + dput(lower_dentry);
10335 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10336 + unlock_dir(lower_dir_dentry);
10337 +
10338 + if (err)
10339 + break;
10340 + }
10341 +
10342 + /*
10343 + * Create the whiteout in branch 0 (highest priority) only if (a)
10344 + * there was an error in any intermediate branch other than branch 0
10345 + * due to failure of vfs_unlink/vfs_rmdir or (b) a branch marked or
10346 + * mounted read-only.
10347 + */
10348 + if (err) {
10349 + if ((bindex == 0) ||
10350 + ((bindex == dbstart(dentry)) &&
10351 + (!IS_COPYUP_ERR(err))))
10352 + goto out;
10353 + else {
10354 + if (!IS_COPYUP_ERR(err))
10355 + pr_debug("unionfs: lower object deletion "
10356 + "failed in branch:%d\n", bindex);
10357 + err = create_whiteout(dentry, sbstart(dentry->d_sb));
10358 + }
10359 + }
10360 +
10361 +out:
10362 + if (!err)
10363 + inode_dec_link_count(dentry->d_inode);
10364 +
10365 + /* We don't want to leave negative leftover dentries for revalidate. */
10366 + if (!err && (dbopaque(dentry) != -1))
10367 + update_bstart(dentry);
10368 +
10369 + return err;
10370 +}
10371 +
10372 +int unionfs_unlink(struct inode *dir, struct dentry *dentry)
10373 +{
10374 + int err = 0;
10375 + struct inode *inode = dentry->d_inode;
10376 + struct dentry *parent;
10377 + int valid;
10378 +
10379 + BUG_ON(S_ISDIR(inode->i_mode));
10380 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10381 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10382 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10383 +
10384 + valid = __unionfs_d_revalidate(dentry, parent, false);
10385 + if (unlikely(!valid)) {
10386 + err = -ESTALE;
10387 + goto out;
10388 + }
10389 + unionfs_check_dentry(dentry);
10390 +
10391 + err = unionfs_unlink_whiteout(dir, dentry, parent);
10392 + /* call d_drop so the system "forgets" about us */
10393 + if (!err) {
10394 + unionfs_postcopyup_release(dentry);
10395 + unionfs_postcopyup_setmnt(parent);
10396 + if (inode->i_nlink == 0) /* drop lower inodes */
10397 + iput_lowers_all(inode, false);
10398 + d_drop(dentry);
10399 + /*
10400 + * if unlink/whiteout succeeded, parent dir mtime has
10401 + * changed
10402 + */
10403 + unionfs_copy_attr_times(dir);
10404 + }
10405 +
10406 +out:
10407 + if (!err) {
10408 + unionfs_check_dentry(dentry);
10409 + unionfs_check_inode(dir);
10410 + }
10411 + unionfs_unlock_dentry(dentry);
10412 + unionfs_unlock_parent(dentry, parent);
10413 + unionfs_read_unlock(dentry->d_sb);
10414 + return err;
10415 +}
10416 +
10417 +static int unionfs_rmdir_first(struct inode *dir, struct dentry *dentry,
10418 + struct unionfs_dir_state *namelist)
10419 +{
10420 + int err;
10421 + struct dentry *lower_dentry;
10422 + struct dentry *lower_dir_dentry = NULL;
10423 +
10424 + /* Here we need to remove whiteout entries. */
10425 + err = delete_whiteouts(dentry, dbstart(dentry), namelist);
10426 + if (err)
10427 + goto out;
10428 +
10429 + lower_dentry = unionfs_lower_dentry(dentry);
10430 +
10431 + lower_dir_dentry = lock_parent(lower_dentry);
10432 +
10433 + /* avoid destroying the lower inode if the file is in use */
10434 + dget(lower_dentry);
10435 + err = is_robranch(dentry);
10436 + if (!err) {
10437 + /* see Documentation/filesystems/unionfs/issues.txt */
10438 + lockdep_off();
10439 + err = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
10440 + lockdep_on();
10441 + }
10442 + dput(lower_dentry);
10443 +
10444 + fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
10445 + /* propagate number of hard-links */
10446 + dentry->d_inode->i_nlink = unionfs_get_nlinks(dentry->d_inode);
10447 +
10448 +out:
10449 + if (lower_dir_dentry)
10450 + unlock_dir(lower_dir_dentry);
10451 + return err;
10452 +}
10453 +
10454 +int unionfs_rmdir(struct inode *dir, struct dentry *dentry)
10455 +{
10456 + int err = 0;
10457 + struct unionfs_dir_state *namelist = NULL;
10458 + struct dentry *parent;
10459 + int dstart, dend;
10460 + bool valid;
10461 +
10462 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
10463 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
10464 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
10465 +
10466 + valid = __unionfs_d_revalidate(dentry, parent, false);
10467 + if (unlikely(!valid)) {
10468 + err = -ESTALE;
10469 + goto out;
10470 + }
10471 + unionfs_check_dentry(dentry);
10472 +
10473 + /* check if this unionfs directory is empty or not */
10474 + err = check_empty(dentry, parent, &namelist);
10475 + if (err)
10476 + goto out;
10477 +
10478 + err = unionfs_rmdir_first(dir, dentry, namelist);
10479 + dstart = dbstart(dentry);
10480 + dend = dbend(dentry);
10481 + /*
10482 + * We create a whiteout for the directory if there was an error to
10483 + * rmdir the first directory entry in the union. Otherwise, we
10484 + * create a whiteout only if there is no chance that a lower
10485 + * priority branch might also have the same named directory. IOW,
10486 + * if there is not another same-named directory at a lower priority
10487 + * branch, then we don't need to create a whiteout for it.
10488 + */
10489 + if (!err) {
10490 + if (dstart < dend)
10491 + err = create_whiteout(dentry, dstart);
10492 + } else {
10493 + int new_err;
10494 +
10495 + if (dstart == 0)
10496 + goto out;
10497 +
10498 + /* exit if the error returned was NOT -EROFS */
10499 + if (!IS_COPYUP_ERR(err))
10500 + goto out;
10501 +
10502 + new_err = create_whiteout(dentry, dstart - 1);
10503 + if (new_err != -EEXIST)
10504 + err = new_err;
10505 + }
10506 +
10507 +out:
10508 + /*
10509 + * Drop references to lower dentry/inode so storage space for them
10510 + * can be reclaimed. Then, call d_drop so the system "forgets"
10511 + * about us.
10512 + */
10513 + if (!err) {
10514 + iput_lowers_all(dentry->d_inode, false);
10515 + dput(unionfs_lower_dentry_idx(dentry, dstart));
10516 + unionfs_set_lower_dentry_idx(dentry, dstart, NULL);
10517 + d_drop(dentry);
10518 + /* update our lower vfsmnts, in case a copyup took place */
10519 + unionfs_postcopyup_setmnt(dentry);
10520 + unionfs_check_dentry(dentry);
10521 + unionfs_check_inode(dir);
10522 + }
10523 +
10524 + if (namelist)
10525 + free_rdstate(namelist);
10526 +
10527 + unionfs_unlock_dentry(dentry);
10528 + unionfs_unlock_parent(dentry, parent);
10529 + unionfs_read_unlock(dentry->d_sb);
10530 + return err;
10531 +}
10532 diff --git a/fs/unionfs/whiteout.c b/fs/unionfs/whiteout.c
10533 new file mode 100644
10534 index 0000000..626006a
10535 --- /dev/null
10536 +++ b/fs/unionfs/whiteout.c
10537 @@ -0,0 +1,584 @@
10538 +/*
10539 + * Copyright (c) 2003-2009 Erez Zadok
10540 + * Copyright (c) 2003-2006 Charles P. Wright
10541 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
10542 + * Copyright (c) 2005-2006 Junjiro Okajima
10543 + * Copyright (c) 2005 Arun M. Krishnakumar
10544 + * Copyright (c) 2004-2006 David P. Quigley
10545 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
10546 + * Copyright (c) 2003 Puja Gupta
10547 + * Copyright (c) 2003 Harikesavan Krishnan
10548 + * Copyright (c) 2003-2009 Stony Brook University
10549 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
10550 + *
10551 + * This program is free software; you can redistribute it and/or modify
10552 + * it under the terms of the GNU General Public License version 2 as
10553 + * published by the Free Software Foundation.
10554 + */
10555 +
10556 +#include "union.h"
10557 +
10558 +/*
10559 + * whiteout and opaque directory helpers
10560 + */
10561 +
10562 +/* What do we use for whiteouts. */
10563 +#define UNIONFS_WHPFX ".wh."
10564 +#define UNIONFS_WHLEN 4
10565 +/*
10566 + * If a directory contains this file, then it is opaque. We start with the
10567 + * .wh. flag so that it is blocked by lookup.
10568 + */
10569 +#define UNIONFS_DIR_OPAQUE_NAME "__dir_opaque"
10570 +#define UNIONFS_DIR_OPAQUE UNIONFS_WHPFX UNIONFS_DIR_OPAQUE_NAME
10571 +
10572 +/* construct whiteout filename */
10573 +char *alloc_whname(const char *name, int len)
10574 +{
10575 + char *buf;
10576 +
10577 + buf = kmalloc(len + UNIONFS_WHLEN + 1, GFP_KERNEL);
10578 + if (unlikely(!buf))
10579 + return ERR_PTR(-ENOMEM);
10580 +
10581 + strcpy(buf, UNIONFS_WHPFX);
10582 + strlcat(buf, name, len + UNIONFS_WHLEN + 1);
10583 +
10584 + return buf;
10585 +}
10586 +
10587 +/*
10588 + * XXX: this can be inline or CPP macro, but is here to keep all whiteout
10589 + * code in one place.
10590 + */
10591 +void unionfs_set_max_namelen(long *namelen)
10592 +{
10593 + *namelen -= UNIONFS_WHLEN;
10594 +}
10595 +
10596 +/* check if @namep is a whiteout, update @namep and @namelenp accordingly */
10597 +bool is_whiteout_name(char **namep, int *namelenp)
10598 +{
10599 + if (*namelenp > UNIONFS_WHLEN &&
10600 + !strncmp(*namep, UNIONFS_WHPFX, UNIONFS_WHLEN)) {
10601 + *namep += UNIONFS_WHLEN;
10602 + *namelenp -= UNIONFS_WHLEN;
10603 + return true;
10604 + }
10605 + return false;
10606 +}
10607 +
10608 +/* is the filename valid == !(whiteout for a file or opaque dir marker) */
10609 +bool is_validname(const char *name)
10610 +{
10611 + if (!strncmp(name, UNIONFS_WHPFX, UNIONFS_WHLEN))
10612 + return false;
10613 + if (!strncmp(name, UNIONFS_DIR_OPAQUE_NAME,
10614 + sizeof(UNIONFS_DIR_OPAQUE_NAME) - 1))
10615 + return false;
10616 + return true;
10617 +}
10618 +
10619 +/*
10620 + * Look for a whiteout @name in @lower_parent directory. If error, return
10621 + * ERR_PTR. Caller must dput() the returned dentry if not an error.
10622 + *
10623 + * XXX: some callers can reuse the whname allocated buffer to avoid repeated
10624 + * free then re-malloc calls. Need to provide a different API for those
10625 + * callers.
10626 + */
10627 +struct dentry *lookup_whiteout(const char *name, struct dentry *lower_parent)
10628 +{
10629 + char *whname = NULL;
10630 + int err = 0, namelen;
10631 + struct dentry *wh_dentry = NULL;
10632 +
10633 + namelen = strlen(name);
10634 + whname = alloc_whname(name, namelen);
10635 + if (unlikely(IS_ERR(whname))) {
10636 + err = PTR_ERR(whname);
10637 + goto out;
10638 + }
10639 +
10640 + /* check if whiteout exists in this branch: lookup .wh.foo */
10641 + wh_dentry = lookup_lck_len(whname, lower_parent, strlen(whname));
10642 + if (IS_ERR(wh_dentry)) {
10643 + err = PTR_ERR(wh_dentry);
10644 + goto out;
10645 + }
10646 +
10647 + /* check if negative dentry (ENOENT) */
10648 + if (!wh_dentry->d_inode)
10649 + goto out;
10650 +
10651 + /* whiteout found: check if valid type */
10652 + if (!S_ISREG(wh_dentry->d_inode->i_mode)) {
10653 + printk(KERN_ERR "unionfs: invalid whiteout %s entry type %d\n",
10654 + whname, wh_dentry->d_inode->i_mode);
10655 + dput(wh_dentry);
10656 + err = -EIO;
10657 + goto out;
10658 + }
10659 +
10660 +out:
10661 + kfree(whname);
10662 + if (err)
10663 + wh_dentry = ERR_PTR(err);
10664 + return wh_dentry;
10665 +}
10666 +
10667 +/* find and return first whiteout in parent directory, else ENOENT */
10668 +struct dentry *find_first_whiteout(struct dentry *dentry)
10669 +{
10670 + int bindex, bstart, bend;
10671 + struct dentry *parent, *lower_parent, *wh_dentry;
10672 +
10673 + parent = dget_parent(dentry);
10674 +
10675 + bstart = dbstart(parent);
10676 + bend = dbend(parent);
10677 + wh_dentry = ERR_PTR(-ENOENT);
10678 +
10679 + for (bindex = bstart; bindex <= bend; bindex++) {
10680 + lower_parent = unionfs_lower_dentry_idx(parent, bindex);
10681 + if (!lower_parent)
10682 + continue;
10683 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_parent);
10684 + if (IS_ERR(wh_dentry))
10685 + continue;
10686 + if (wh_dentry->d_inode)
10687 + break;
10688 + dput(wh_dentry);
10689 + wh_dentry = ERR_PTR(-ENOENT);
10690 + }
10691 +
10692 + dput(parent);
10693 +
10694 + return wh_dentry;
10695 +}
10696 +
10697 +/*
10698 + * Unlink a whiteout dentry. Returns 0 or -errno. Caller must hold and
10699 + * release dentry reference.
10700 + */
10701 +int unlink_whiteout(struct dentry *wh_dentry)
10702 +{
10703 + int err;
10704 + struct dentry *lower_dir_dentry;
10705 +
10706 + /* dget and lock parent dentry */
10707 + lower_dir_dentry = lock_parent_wh(wh_dentry);
10708 +
10709 + /* see Documentation/filesystems/unionfs/issues.txt */
10710 + lockdep_off();
10711 + err = vfs_unlink(lower_dir_dentry->d_inode, wh_dentry);
10712 + lockdep_on();
10713 + unlock_dir(lower_dir_dentry);
10714 +
10715 + /*
10716 + * Whiteouts are special files and should be deleted no matter what
10717 + * (as if they never existed), in order to allow this create
10718 + * operation to succeed. This is especially important in sticky
10719 + * directories: a whiteout may have been created by one user, but
10720 + * the newly created file may be created by another user.
10721 + * Therefore, in order to maintain Unix semantics, if the vfs_unlink
10722 + * above failed, then we have to try to directly unlink the
10723 + * whiteout. Note: in the ODF version of unionfs, whiteout are
10724 + * handled much more cleanly.
10725 + */
10726 + if (err == -EPERM) {
10727 + struct inode *inode = lower_dir_dentry->d_inode;
10728 + err = inode->i_op->unlink(inode, wh_dentry);
10729 + }
10730 + if (err)
10731 + printk(KERN_ERR "unionfs: could not unlink whiteout %s, "
10732 + "err = %d\n", wh_dentry->d_name.name, err);
10733 +
10734 + return err;
10735 +
10736 +}
10737 +
10738 +/*
10739 + * Helper function when creating new objects (create, symlink, mknod, etc.).
10740 + * Checks to see if there's a whiteout in @lower_dentry's parent directory,
10741 + * whose name is taken from @dentry. Then tries to remove that whiteout, if
10742 + * found. If <dentry,bindex> is a branch marked readonly, return -EROFS.
10743 + * If it finds both a regular file and a whiteout, return -EIO (this should
10744 + * never happen).
10745 + *
10746 + * Return 0 if no whiteout was found. Return 1 if one was found and
10747 + * successfully removed. Therefore a value >= 0 tells the caller that
10748 + * @lower_dentry belongs to a good branch to create the new object in).
10749 + * Return -ERRNO if an error occurred during whiteout lookup or in trying to
10750 + * unlink the whiteout.
10751 + */
10752 +int check_unlink_whiteout(struct dentry *dentry, struct dentry *lower_dentry,
10753 + int bindex)
10754 +{
10755 + int err;
10756 + struct dentry *wh_dentry = NULL;
10757 + struct dentry *lower_dir_dentry = NULL;
10758 +
10759 + /* look for whiteout dentry first */
10760 + lower_dir_dentry = dget_parent(lower_dentry);
10761 + wh_dentry = lookup_whiteout(dentry->d_name.name, lower_dir_dentry);
10762 + dput(lower_dir_dentry);
10763 + if (IS_ERR(wh_dentry)) {
10764 + err = PTR_ERR(wh_dentry);
10765 + goto out;
10766 + }
10767 +
10768 + if (!wh_dentry->d_inode) { /* no whiteout exists*/
10769 + err = 0;
10770 + goto out_dput;
10771 + }
10772 +
10773 + /* check if regular file and whiteout were both found */
10774 + if (unlikely(lower_dentry->d_inode)) {
10775 + err = -EIO;
10776 + printk(KERN_ERR "unionfs: found both whiteout and regular "
10777 + "file in directory %s (branch %d)\n",
10778 + lower_dir_dentry->d_name.name, bindex);
10779 + goto out_dput;
10780 + }
10781 +
10782 + /* check if branch is writeable */
10783 + err = is_robranch_super(dentry->d_sb, bindex);
10784 + if (err)
10785 + goto out_dput;
10786 +
10787 + /* .wh.foo has been found, so let's unlink it */
10788 + err = unlink_whiteout(wh_dentry);
10789 + if (!err)
10790 + err = 1; /* a whiteout was found and successfully removed */
10791 +out_dput:
10792 + dput(wh_dentry);
10793 +out:
10794 + return err;
10795 +}
10796 +
10797 +/*
10798 + * Pass an unionfs dentry and an index. It will try to create a whiteout
10799 + * for the filename in dentry, and will try in branch 'index'. On error,
10800 + * it will proceed to a branch to the left.
10801 + */
10802 +int create_whiteout(struct dentry *dentry, int start)
10803 +{
10804 + int bstart, bend, bindex;
10805 + struct dentry *lower_dir_dentry;
10806 + struct dentry *lower_dentry;
10807 + struct dentry *lower_wh_dentry;
10808 + struct nameidata nd;
10809 + char *name = NULL;
10810 + int err = -EINVAL;
10811 +
10812 + verify_locked(dentry);
10813 +
10814 + bstart = dbstart(dentry);
10815 + bend = dbend(dentry);
10816 +
10817 + /* create dentry's whiteout equivalent */
10818 + name = alloc_whname(dentry->d_name.name, dentry->d_name.len);
10819 + if (unlikely(IS_ERR(name))) {
10820 + err = PTR_ERR(name);
10821 + goto out;
10822 + }
10823 +
10824 + for (bindex = start; bindex >= 0; bindex--) {
10825 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10826 +
10827 + if (!lower_dentry) {
10828 + /*
10829 + * if lower dentry is not present, create the
10830 + * entire lower dentry directory structure and go
10831 + * ahead. Since we want to just create whiteout, we
10832 + * only want the parent dentry, and hence get rid of
10833 + * this dentry.
10834 + */
10835 + lower_dentry = create_parents(dentry->d_inode,
10836 + dentry,
10837 + dentry->d_name.name,
10838 + bindex);
10839 + if (!lower_dentry || IS_ERR(lower_dentry)) {
10840 + int ret = PTR_ERR(lower_dentry);
10841 + if (!IS_COPYUP_ERR(ret))
10842 + printk(KERN_ERR
10843 + "unionfs: create_parents for "
10844 + "whiteout failed: bindex=%d "
10845 + "err=%d\n", bindex, ret);
10846 + continue;
10847 + }
10848 + }
10849 +
10850 + lower_wh_dentry =
10851 + lookup_lck_len(name, lower_dentry->d_parent,
10852 + dentry->d_name.len + UNIONFS_WHLEN);
10853 + if (IS_ERR(lower_wh_dentry))
10854 + continue;
10855 +
10856 + /*
10857 + * The whiteout already exists. This used to be impossible,
10858 + * but now is possible because of opaqueness.
10859 + */
10860 + if (lower_wh_dentry->d_inode) {
10861 + dput(lower_wh_dentry);
10862 + err = 0;
10863 + goto out;
10864 + }
10865 +
10866 + err = init_lower_nd(&nd, LOOKUP_CREATE);
10867 + if (unlikely(err < 0))
10868 + goto out;
10869 + lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
10870 + err = is_robranch_super(dentry->d_sb, bindex);
10871 + if (!err)
10872 + err = vfs_create(lower_dir_dentry->d_inode,
10873 + lower_wh_dentry,
10874 + current_umask() & S_IRUGO,
10875 + &nd);
10876 + unlock_dir(lower_dir_dentry);
10877 + dput(lower_wh_dentry);
10878 + release_lower_nd(&nd, err);
10879 +
10880 + if (!err || !IS_COPYUP_ERR(err))
10881 + break;
10882 + }
10883 +
10884 + /* set dbopaque so that lookup will not proceed after this branch */
10885 + if (!err)
10886 + dbopaque(dentry) = bindex;
10887 +
10888 +out:
10889 + kfree(name);
10890 + return err;
10891 +}
10892 +
10893 +/*
10894 + * Delete all of the whiteouts in a given directory for rmdir.
10895 + *
10896 + * lower directory inode should be locked
10897 + */
10898 +static int do_delete_whiteouts(struct dentry *dentry, int bindex,
10899 + struct unionfs_dir_state *namelist)
10900 +{
10901 + int err = 0;
10902 + struct dentry *lower_dir_dentry = NULL;
10903 + struct dentry *lower_dentry;
10904 + char *name = NULL, *p;
10905 + struct inode *lower_dir;
10906 + int i;
10907 + struct list_head *pos;
10908 + struct filldir_node *cursor;
10909 +
10910 + /* Find out lower parent dentry */
10911 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10912 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10913 + lower_dir = lower_dir_dentry->d_inode;
10914 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10915 +
10916 + err = -ENOMEM;
10917 + name = __getname();
10918 + if (unlikely(!name))
10919 + goto out;
10920 + strcpy(name, UNIONFS_WHPFX);
10921 + p = name + UNIONFS_WHLEN;
10922 +
10923 + err = 0;
10924 + for (i = 0; !err && i < namelist->size; i++) {
10925 + list_for_each(pos, &namelist->list[i]) {
10926 + cursor =
10927 + list_entry(pos, struct filldir_node,
10928 + file_list);
10929 + /* Only operate on whiteouts in this branch. */
10930 + if (cursor->bindex != bindex)
10931 + continue;
10932 + if (!cursor->whiteout)
10933 + continue;
10934 +
10935 + strlcpy(p, cursor->name, PATH_MAX - UNIONFS_WHLEN);
10936 + lower_dentry =
10937 + lookup_lck_len(name, lower_dir_dentry,
10938 + cursor->namelen +
10939 + UNIONFS_WHLEN);
10940 + if (IS_ERR(lower_dentry)) {
10941 + err = PTR_ERR(lower_dentry);
10942 + break;
10943 + }
10944 + if (lower_dentry->d_inode)
10945 + err = vfs_unlink(lower_dir, lower_dentry);
10946 + dput(lower_dentry);
10947 + if (err)
10948 + break;
10949 + }
10950 + }
10951 +
10952 + __putname(name);
10953 +
10954 + /* After all of the removals, we should copy the attributes once. */
10955 + fsstack_copy_attr_times(dentry->d_inode, lower_dir_dentry->d_inode);
10956 +
10957 +out:
10958 + return err;
10959 +}
10960 +
10961 +
10962 +void __delete_whiteouts(struct work_struct *work)
10963 +{
10964 + struct sioq_args *args = container_of(work, struct sioq_args, work);
10965 + struct deletewh_args *d = &args->deletewh;
10966 +
10967 + args->err = do_delete_whiteouts(d->dentry, d->bindex, d->namelist);
10968 + complete(&args->comp);
10969 +}
10970 +
10971 +/* delete whiteouts in a dir (for rmdir operation) using sioq if necessary */
10972 +int delete_whiteouts(struct dentry *dentry, int bindex,
10973 + struct unionfs_dir_state *namelist)
10974 +{
10975 + int err;
10976 + struct super_block *sb;
10977 + struct dentry *lower_dir_dentry;
10978 + struct inode *lower_dir;
10979 + struct sioq_args args;
10980 +
10981 + sb = dentry->d_sb;
10982 +
10983 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode));
10984 + BUG_ON(bindex < dbstart(dentry));
10985 + BUG_ON(bindex > dbend(dentry));
10986 + err = is_robranch_super(sb, bindex);
10987 + if (err)
10988 + goto out;
10989 +
10990 + lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
10991 + BUG_ON(!S_ISDIR(lower_dir_dentry->d_inode->i_mode));
10992 + lower_dir = lower_dir_dentry->d_inode;
10993 + BUG_ON(!S_ISDIR(lower_dir->i_mode));
10994 +
10995 + if (!inode_permission(lower_dir, MAY_WRITE | MAY_EXEC)) {
10996 + err = do_delete_whiteouts(dentry, bindex, namelist);
10997 + } else {
10998 + args.deletewh.namelist = namelist;
10999 + args.deletewh.dentry = dentry;
11000 + args.deletewh.bindex = bindex;
11001 + run_sioq(__delete_whiteouts, &args);
11002 + err = args.err;
11003 + }
11004 +
11005 +out:
11006 + return err;
11007 +}
11008 +
11009 +/****************************************************************************
11010 + * Opaque directory helpers *
11011 + ****************************************************************************/
11012 +
11013 +/*
11014 + * is_opaque_dir: returns 0 if it is NOT an opaque dir, 1 if it is, and
11015 + * -errno if an error occurred trying to figure this out.
11016 + */
11017 +int is_opaque_dir(struct dentry *dentry, int bindex)
11018 +{
11019 + int err = 0;
11020 + struct dentry *lower_dentry;
11021 + struct dentry *wh_lower_dentry;
11022 + struct inode *lower_inode;
11023 + struct sioq_args args;
11024 +
11025 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11026 + lower_inode = lower_dentry->d_inode;
11027 +
11028 + BUG_ON(!S_ISDIR(lower_inode->i_mode));
11029 +
11030 + mutex_lock(&lower_inode->i_mutex);
11031 +
11032 + if (!inode_permission(lower_inode, MAY_EXEC)) {
11033 + wh_lower_dentry =
11034 + lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
11035 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11036 + } else {
11037 + args.is_opaque.dentry = lower_dentry;
11038 + run_sioq(__is_opaque_dir, &args);
11039 + wh_lower_dentry = args.ret;
11040 + }
11041 +
11042 + mutex_unlock(&lower_inode->i_mutex);
11043 +
11044 + if (IS_ERR(wh_lower_dentry)) {
11045 + err = PTR_ERR(wh_lower_dentry);
11046 + goto out;
11047 + }
11048 +
11049 + /* This is an opaque dir iff wh_lower_dentry is positive */
11050 + err = !!wh_lower_dentry->d_inode;
11051 +
11052 + dput(wh_lower_dentry);
11053 +out:
11054 + return err;
11055 +}
11056 +
11057 +void __is_opaque_dir(struct work_struct *work)
11058 +{
11059 + struct sioq_args *args = container_of(work, struct sioq_args, work);
11060 +
11061 + args->ret = lookup_one_len(UNIONFS_DIR_OPAQUE, args->is_opaque.dentry,
11062 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11063 + complete(&args->comp);
11064 +}
11065 +
11066 +int make_dir_opaque(struct dentry *dentry, int bindex)
11067 +{
11068 + int err = 0;
11069 + struct dentry *lower_dentry, *diropq;
11070 + struct inode *lower_dir;
11071 + struct nameidata nd;
11072 + const struct cred *old_creds;
11073 + struct cred *new_creds;
11074 +
11075 + /*
11076 + * Opaque directory whiteout markers are special files (like regular
11077 + * whiteouts), and should appear to the users as if they don't
11078 + * exist. They should be created/deleted regardless of directory
11079 + * search/create permissions, but only for the duration of this
11080 + * creation of the .wh.__dir_opaque: file. Note, this does not
11081 + * circumvent normal ->permission).
11082 + */
11083 + new_creds = prepare_creds();
11084 + if (unlikely(!new_creds)) {
11085 + err = -ENOMEM;
11086 + goto out_err;
11087 + }
11088 + cap_raise(new_creds->cap_effective, CAP_DAC_READ_SEARCH);
11089 + cap_raise(new_creds->cap_effective, CAP_DAC_OVERRIDE);
11090 + old_creds = override_creds(new_creds);
11091 +
11092 + lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
11093 + lower_dir = lower_dentry->d_inode;
11094 + BUG_ON(!S_ISDIR(dentry->d_inode->i_mode) ||
11095 + !S_ISDIR(lower_dir->i_mode));
11096 +
11097 + mutex_lock(&lower_dir->i_mutex);
11098 + diropq = lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
11099 + sizeof(UNIONFS_DIR_OPAQUE) - 1);
11100 + if (IS_ERR(diropq)) {
11101 + err = PTR_ERR(diropq);
11102 + goto out;
11103 + }
11104 +
11105 + err = init_lower_nd(&nd, LOOKUP_CREATE);
11106 + if (unlikely(err < 0))
11107 + goto out;
11108 + if (!diropq->d_inode)
11109 + err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
11110 + if (!err)
11111 + dbopaque(dentry) = bindex;
11112 + release_lower_nd(&nd, err);
11113 +
11114 + dput(diropq);
11115 +
11116 +out:
11117 + mutex_unlock(&lower_dir->i_mutex);
11118 + revert_creds(old_creds);
11119 +out_err:
11120 + return err;
11121 +}
11122 diff --git a/fs/unionfs/xattr.c b/fs/unionfs/xattr.c
11123 new file mode 100644
11124 index 0000000..af72cca
11125 --- /dev/null
11126 +++ b/fs/unionfs/xattr.c
11127 @@ -0,0 +1,173 @@
11128 +/*
11129 + * Copyright (c) 2003-2009 Erez Zadok
11130 + * Copyright (c) 2003-2006 Charles P. Wright
11131 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11132 + * Copyright (c) 2005-2006 Junjiro Okajima
11133 + * Copyright (c) 2005 Arun M. Krishnakumar
11134 + * Copyright (c) 2004-2006 David P. Quigley
11135 + * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
11136 + * Copyright (c) 2003 Puja Gupta
11137 + * Copyright (c) 2003 Harikesavan Krishnan
11138 + * Copyright (c) 2003-2009 Stony Brook University
11139 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11140 + *
11141 + * This program is free software; you can redistribute it and/or modify
11142 + * it under the terms of the GNU General Public License version 2 as
11143 + * published by the Free Software Foundation.
11144 + */
11145 +
11146 +#include "union.h"
11147 +
11148 +/* This is lifted from fs/xattr.c */
11149 +void *unionfs_xattr_alloc(size_t size, size_t limit)
11150 +{
11151 + void *ptr;
11152 +
11153 + if (size > limit)
11154 + return ERR_PTR(-E2BIG);
11155 +
11156 + if (!size) /* size request, no buffer is needed */
11157 + return NULL;
11158 +
11159 + ptr = kmalloc(size, GFP_KERNEL);
11160 + if (unlikely(!ptr))
11161 + return ERR_PTR(-ENOMEM);
11162 + return ptr;
11163 +}
11164 +
11165 +/*
11166 + * BKL held by caller.
11167 + * dentry->d_inode->i_mutex locked
11168 + */
11169 +ssize_t unionfs_getxattr(struct dentry *dentry, const char *name, void *value,
11170 + size_t size)
11171 +{
11172 + struct dentry *lower_dentry = NULL;
11173 + struct dentry *parent;
11174 + int err = -EOPNOTSUPP;
11175 + bool valid;
11176 +
11177 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11178 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11179 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11180 +
11181 + valid = __unionfs_d_revalidate(dentry, parent, false);
11182 + if (unlikely(!valid)) {
11183 + err = -ESTALE;
11184 + goto out;
11185 + }
11186 +
11187 + lower_dentry = unionfs_lower_dentry(dentry);
11188 +
11189 + err = vfs_getxattr(lower_dentry, (char *) name, value, size);
11190 +
11191 +out:
11192 + unionfs_check_dentry(dentry);
11193 + unionfs_unlock_dentry(dentry);
11194 + unionfs_unlock_parent(dentry, parent);
11195 + unionfs_read_unlock(dentry->d_sb);
11196 + return err;
11197 +}
11198 +
11199 +/*
11200 + * BKL held by caller.
11201 + * dentry->d_inode->i_mutex locked
11202 + */
11203 +int unionfs_setxattr(struct dentry *dentry, const char *name,
11204 + const void *value, size_t size, int flags)
11205 +{
11206 + struct dentry *lower_dentry = NULL;
11207 + struct dentry *parent;
11208 + int err = -EOPNOTSUPP;
11209 + bool valid;
11210 +
11211 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11212 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11213 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11214 +
11215 + valid = __unionfs_d_revalidate(dentry, parent, false);
11216 + if (unlikely(!valid)) {
11217 + err = -ESTALE;
11218 + goto out;
11219 + }
11220 +
11221 + lower_dentry = unionfs_lower_dentry(dentry);
11222 +
11223 + err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
11224 + size, flags);
11225 +
11226 +out:
11227 + unionfs_check_dentry(dentry);
11228 + unionfs_unlock_dentry(dentry);
11229 + unionfs_unlock_parent(dentry, parent);
11230 + unionfs_read_unlock(dentry->d_sb);
11231 + return err;
11232 +}
11233 +
11234 +/*
11235 + * BKL held by caller.
11236 + * dentry->d_inode->i_mutex locked
11237 + */
11238 +int unionfs_removexattr(struct dentry *dentry, const char *name)
11239 +{
11240 + struct dentry *lower_dentry = NULL;
11241 + struct dentry *parent;
11242 + int err = -EOPNOTSUPP;
11243 + bool valid;
11244 +
11245 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11246 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11247 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11248 +
11249 + valid = __unionfs_d_revalidate(dentry, parent, false);
11250 + if (unlikely(!valid)) {
11251 + err = -ESTALE;
11252 + goto out;
11253 + }
11254 +
11255 + lower_dentry = unionfs_lower_dentry(dentry);
11256 +
11257 + err = vfs_removexattr(lower_dentry, (char *) name);
11258 +
11259 +out:
11260 + unionfs_check_dentry(dentry);
11261 + unionfs_unlock_dentry(dentry);
11262 + unionfs_unlock_parent(dentry, parent);
11263 + unionfs_read_unlock(dentry->d_sb);
11264 + return err;
11265 +}
11266 +
11267 +/*
11268 + * BKL held by caller.
11269 + * dentry->d_inode->i_mutex locked
11270 + */
11271 +ssize_t unionfs_listxattr(struct dentry *dentry, char *list, size_t size)
11272 +{
11273 + struct dentry *lower_dentry = NULL;
11274 + struct dentry *parent;
11275 + int err = -EOPNOTSUPP;
11276 + char *encoded_list = NULL;
11277 + bool valid;
11278 +
11279 + unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
11280 + parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT);
11281 + unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
11282 +
11283 + valid = __unionfs_d_revalidate(dentry, parent, false);
11284 + if (unlikely(!valid)) {
11285 + err = -ESTALE;
11286 + goto out;
11287 + }
11288 +
11289 + lower_dentry = unionfs_lower_dentry(dentry);
11290 +
11291 + encoded_list = list;
11292 + err = vfs_listxattr(lower_dentry, encoded_list, size);
11293 +
11294 +out:
11295 + unionfs_check_dentry(dentry);
11296 + unionfs_unlock_dentry(dentry);
11297 + unionfs_unlock_parent(dentry, parent);
11298 + unionfs_read_unlock(dentry->d_sb);
11299 + return err;
11300 +}
11301 diff --git a/include/linux/fs_stack.h b/include/linux/fs_stack.h
11302 index bb516ce..64f1ced 100644
11303 --- a/include/linux/fs_stack.h
11304 +++ b/include/linux/fs_stack.h
11305 @@ -1,17 +1,27 @@
11306 +/*
11307 + * Copyright (c) 2006-2009 Erez Zadok
11308 + * Copyright (c) 2006-2007 Josef 'Jeff' Sipek
11309 + * Copyright (c) 2006-2009 Stony Brook University
11310 + * Copyright (c) 2006-2009 The Research Foundation of SUNY
11311 + *
11312 + * This program is free software; you can redistribute it and/or modify
11313 + * it under the terms of the GNU General Public License version 2 as
11314 + * published by the Free Software Foundation.
11315 + */
11316 +
11317 #ifndef _LINUX_FS_STACK_H
11318 #define _LINUX_FS_STACK_H
11319
11320 -/* This file defines generic functions used primarily by stackable
11321 +/*
11322 + * This file defines generic functions used primarily by stackable
11323 * filesystems; none of these functions require i_mutex to be held.
11324 */
11325
11326 #include <linux/fs.h>
11327
11328 /* externs for fs/stack.c */
11329 -extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
11330 - int (*get_nlinks)(struct inode *));
11331 -
11332 -extern void fsstack_copy_inode_size(struct inode *dst, const struct inode *src);
11333 +extern void fsstack_copy_attr_all(struct inode *dest, const struct inode *src);
11334 +extern void fsstack_copy_inode_size(struct inode *dst, struct inode *src);
11335
11336 /* inlines */
11337 static inline void fsstack_copy_attr_atime(struct inode *dest,
11338 diff --git a/include/linux/magic.h b/include/linux/magic.h
11339 index 1923327..c3fc308 100644
11340 --- a/include/linux/magic.h
11341 +++ b/include/linux/magic.h
11342 @@ -45,6 +45,8 @@
11343 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
11344 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
11345
11346 +#define UNIONFS_SUPER_MAGIC 0xf15f083d
11347 +
11348 #define SMB_SUPER_MAGIC 0x517B
11349 #define USBDEVICE_SUPER_MAGIC 0x9fa2
11350 #define CGROUP_SUPER_MAGIC 0x27e0eb
11351 diff --git a/include/linux/splice.h b/include/linux/splice.h
11352 index 18e7c7c..af56841 100644
11353 --- a/include/linux/splice.h
11354 +++ b/include/linux/splice.h
11355 @@ -81,5 +81,10 @@ extern ssize_t splice_to_pipe(struct pipe_inode_info *,
11356 struct splice_pipe_desc *);
11357 extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
11358 splice_direct_actor *);
11359 +extern long vfs_splice_from(struct pipe_inode_info *pipe, struct file *out,
11360 + loff_t *ppos, size_t len, unsigned int flags);
11361 +extern long vfs_splice_to(struct file *in, loff_t *ppos,
11362 + struct pipe_inode_info *pipe, size_t len,
11363 + unsigned int flags);
11364
11365 #endif
11366 diff --git a/include/linux/union_fs.h b/include/linux/union_fs.h
11367 new file mode 100644
11368 index 0000000..c84d97e
11369 --- /dev/null
11370 +++ b/include/linux/union_fs.h
11371 @@ -0,0 +1,22 @@
11372 +/*
11373 + * Copyright (c) 2003-2009 Erez Zadok
11374 + * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
11375 + * Copyright (c) 2003-2009 Stony Brook University
11376 + * Copyright (c) 2003-2009 The Research Foundation of SUNY
11377 + *
11378 + * This program is free software; you can redistribute it and/or modify
11379 + * it under the terms of the GNU General Public License version 2 as
11380 + * published by the Free Software Foundation.
11381 + */
11382 +
11383 +#ifndef _LINUX_UNION_FS_H
11384 +#define _LINUX_UNION_FS_H
11385 +
11386 +/*
11387 + * DEFINITIONS FOR USER AND KERNEL CODE:
11388 + */
11389 +# define UNIONFS_IOCTL_INCGEN _IOR(0x15, 11, int)
11390 +# define UNIONFS_IOCTL_QUERYFILE _IOR(0x15, 15, int)
11391 +
11392 +#endif /* _LINUX_UNIONFS_H */
11393 +
11394 diff --git a/security/security.c b/security/security.c
11395 index dc7674f..aa3f061 100644
11396 --- a/security/security.c
11397 +++ b/security/security.c
11398 @@ -519,6 +519,7 @@ int security_inode_permission(struct inode *inode, int mask)
11399 return 0;
11400 return security_ops->inode_permission(inode, mask);
11401 }
11402 +EXPORT_SYMBOL(security_inode_permission);
11403
11404 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
11405 {