]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- include linux/module.h so EXPORT_SYMBOL is properly defined
[packages/kernel.git] / kernel-aufs3.patch
1 aufs3.x-rcN kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index 5f4c45d..357a8a6 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -215,6 +215,7 @@ source "fs/pstore/Kconfig"
8  source "fs/sysv/Kconfig"
9  source "fs/ufs/Kconfig"
10  source "fs/exofs/Kconfig"
11 +source "fs/aufs/Kconfig"
12  
13  endif # MISC_FILESYSTEMS
14  
15 diff --git a/fs/Makefile b/fs/Makefile
16 index d2c3353..680ad8a 100644
17 --- a/fs/Makefile
18 +++ b/fs/Makefile
19 @@ -123,3 +123,4 @@ obj-$(CONFIG_GFS2_FS)           += gfs2/
20  obj-y                          += exofs/ # Multiple modules
21  obj-$(CONFIG_CEPH_FS)          += ceph/
22  obj-$(CONFIG_PSTORE)           += pstore/
23 +obj-$(CONFIG_AUFS_FS)           += aufs/
24 diff --git a/include/linux/Kbuild b/include/linux/Kbuild
25 index 619b565..29f386b 100644
26 --- a/include/linux/Kbuild
27 +++ b/include/linux/Kbuild
28 @@ -65,6 +65,7 @@ header-y += atmppp.h
29  header-y += atmsap.h
30  header-y += atmsvc.h
31  header-y += audit.h
32 +header-y += aufs_type.h
33  header-y += auto_fs.h
34  header-y += auto_fs4.h
35  header-y += auxvec.h
36 aufs3.x-rcN base patch
37
38 diff --git a/fs/namei.c b/fs/namei.c
39 index 5008f01..4cc94cf 100644
40 --- a/fs/namei.c
41 +++ b/fs/namei.c
42 @@ -1753,7 +1753,7 @@ static struct dentry *__lookup_hash(struct qstr *name,
43   * needs parent already locked. Doesn't follow mounts.
44   * SMP-safe.
45   */
46 -static struct dentry *lookup_hash(struct nameidata *nd)
47 +struct dentry *lookup_hash(struct nameidata *nd)
48  {
49         return __lookup_hash(&nd->last, nd->path.dentry, nd);
50  }
51 diff --git a/fs/splice.c b/fs/splice.c
52 index fa2defa..e3569b0 100644
53 --- a/fs/splice.c
54 +++ b/fs/splice.c
55 @@ -1085,8 +1085,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
56  /*
57   * Attempt to initiate a splice from pipe to file.
58   */
59 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
60 -                          loff_t *ppos, size_t len, unsigned int flags)
61 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
62 +                   loff_t *ppos, size_t len, unsigned int flags)
63  {
64         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
65                                 loff_t *, size_t, unsigned int);
66 @@ -1113,9 +1113,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
67  /*
68   * Attempt to initiate a splice from a file to a pipe.
69   */
70 -static long do_splice_to(struct file *in, loff_t *ppos,
71 -                        struct pipe_inode_info *pipe, size_t len,
72 -                        unsigned int flags)
73 +long do_splice_to(struct file *in, loff_t *ppos,
74 +                 struct pipe_inode_info *pipe, size_t len,
75 +                 unsigned int flags)
76  {
77         ssize_t (*splice_read)(struct file *, loff_t *,
78                                struct pipe_inode_info *, size_t, unsigned int);
79 diff --git a/include/linux/namei.h b/include/linux/namei.h
80 index ffc0213..ef35a31 100644
81 --- a/include/linux/namei.h
82 +++ b/include/linux/namei.h
83 @@ -85,6 +85,7 @@ extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
84  extern struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
85                 int (*open)(struct inode *, struct file *));
86  
87 +extern struct dentry *lookup_hash(struct nameidata *nd);
88  extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
89  
90  extern int follow_down_one(struct path *);
91 diff --git a/include/linux/splice.h b/include/linux/splice.h
92 index 26e5b61..3ffef2f 100644
93 --- a/include/linux/splice.h
94 +++ b/include/linux/splice.h
95 @@ -91,4 +91,10 @@ extern void splice_shrink_spd(struct pipe_inode_info *,
96  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
97  
98  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
99 +
100 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
101 +                          loff_t *ppos, size_t len, unsigned int flags);
102 +extern long do_splice_to(struct file *in, loff_t *ppos,
103 +                        struct pipe_inode_info *pipe, size_t len,
104 +                        unsigned int flags);
105  #endif
106 aufs3.x-rcN standalone patch
107
108 diff --git a/fs/file_table.c b/fs/file_table.c
109 index c322794..2aad244 100644
110 --- a/fs/file_table.c
111 +++ b/fs/file_table.c
112 @@ -443,6 +443,8 @@ void file_sb_list_del(struct file *file)
113         }
114  }
115  
116 +EXPORT_SYMBOL(file_sb_list_del);
117 +
118  #ifdef CONFIG_SMP
119  
120  /*
121 diff --git a/fs/inode.c b/fs/inode.c
122 index ee4e66b..728042b 100644
123 --- a/fs/inode.c
124 +++ b/fs/inode.c
125 @@ -65,6 +65,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
126  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
127  
128  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
129 +EXPORT_SYMBOL(inode_sb_list_lock);
130  
131  /*
132   * Empty aops. Can be used for the cases where the user does not
133 diff --git a/fs/namei.c b/fs/namei.c
134 index 4cc94cf..af19e30 100644
135 --- a/fs/namei.c
136 +++ b/fs/namei.c
137 @@ -1757,6 +1757,7 @@ struct dentry *lookup_hash(struct nameidata *nd)
138  {
139         return __lookup_hash(&nd->last, nd->path.dentry, nd);
140  }
141 +EXPORT_SYMBOL(lookup_hash);
142  
143  /**
144   * lookup_one_len - filesystem helper to lookup single pathname component
145 diff --git a/fs/namespace.c b/fs/namespace.c
146 index 6d3a196..d832654 100644
147 --- a/fs/namespace.c
148 +++ b/fs/namespace.c
149 @@ -1509,6 +1509,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
150         }
151         return 0;
152  }
153 +EXPORT_SYMBOL(iterate_mounts);
154  
155  static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
156  {
157 diff --git a/fs/notify/group.c b/fs/notify/group.c
158 index 63fc294..6f4adca 100644
159 --- a/fs/notify/group.c
160 +++ b/fs/notify/group.c
161 @@ -22,6 +22,7 @@
162  #include <linux/srcu.h>
163  #include <linux/rculist.h>
164  #include <linux/wait.h>
165 +#include <linux/module.h>
166  
167  #include <linux/fsnotify_backend.h>
168  #include "fsnotify.h"
169 @@ -70,6 +71,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
170         if (atomic_dec_and_test(&group->refcnt))
171                 fsnotify_destroy_group(group);
172  }
173 +EXPORT_SYMBOL(fsnotify_put_group);
174  
175  /*
176   * Create a new fsnotify_group and hold a reference for the group returned.
177 @@ -102,3 +104,4 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
178  
179         return group;
180  }
181 +EXPORT_SYMBOL(fsnotify_alloc_group);
182 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
183 index e14587d..be6533b 100644
184 --- a/fs/notify/mark.c
185 +++ b/fs/notify/mark.c
186 @@ -112,6 +112,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
187         if (atomic_dec_and_test(&mark->refcnt))
188                 mark->free_mark(mark);
189  }
190 +EXPORT_SYMBOL(fsnotify_put_mark);
191  
192  /*
193   * Any time a mark is getting freed we end up here.
194 @@ -189,6 +190,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
195         if (unlikely(atomic_dec_and_test(&group->num_marks)))
196                 fsnotify_final_destroy_group(group);
197  }
198 +EXPORT_SYMBOL(fsnotify_destroy_mark);
199  
200  void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
201  {
202 @@ -276,6 +278,7 @@ err:
203  
204         return ret;
205  }
206 +EXPORT_SYMBOL(fsnotify_add_mark);
207  
208  /*
209   * clear any marks in a group in which mark->flags & flags is true
210 @@ -331,6 +334,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
211         atomic_set(&mark->refcnt, 1);
212         mark->free_mark = free_mark;
213  }
214 +EXPORT_SYMBOL(fsnotify_init_mark);
215  
216  static int fsnotify_mark_destroy(void *ignored)
217  {
218 diff --git a/fs/open.c b/fs/open.c
219 index 22c41b5..33b4033 100644
220 --- a/fs/open.c
221 +++ b/fs/open.c
222 @@ -60,6 +60,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
223         mutex_unlock(&dentry->d_inode->i_mutex);
224         return ret;
225  }
226 +EXPORT_SYMBOL(do_truncate);
227  
228  static long do_sys_truncate(const char __user *pathname, loff_t length)
229  {
230 diff --git a/fs/splice.c b/fs/splice.c
231 index e3569b0..9dc07b7 100644
232 --- a/fs/splice.c
233 +++ b/fs/splice.c
234 @@ -1109,6 +1109,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
235  
236         return splice_write(pipe, out, ppos, len, flags);
237  }
238 +EXPORT_SYMBOL(do_splice_from);
239  
240  /*
241   * Attempt to initiate a splice from a file to a pipe.
242 @@ -1135,6 +1136,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
243  
244         return splice_read(in, ppos, pipe, len, flags);
245  }
246 +EXPORT_SYMBOL(do_splice_to);
247  
248  /**
249   * splice_direct_to_actor - splices data directly between two non-pipes
250 diff --git a/security/commoncap.c b/security/commoncap.c
251 index ee4f848..611fd70 100644
252 --- a/security/commoncap.c
253 +++ b/security/commoncap.c
254 @@ -975,3 +975,4 @@ int cap_file_mmap(struct file *file, unsigned long reqprot,
255         }
256         return ret;
257  }
258 +EXPORT_SYMBOL(cap_file_mmap);
259 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
260 index 4450fbe..2c437e5 100644
261 --- a/security/device_cgroup.c
262 +++ b/security/device_cgroup.c
263 @@ -500,6 +500,7 @@ found:
264  
265         return -EPERM;
266  }
267 +EXPORT_SYMBOL(__devcgroup_inode_permission);
268  
269  int devcgroup_inode_mknod(int mode, dev_t dev)
270  {
271 diff --git a/security/security.c b/security/security.c
272 index 0c6cc69..d58ba1b 100644
273 --- a/security/security.c
274 +++ b/security/security.c
275 @@ -411,6 +411,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
276                 return 0;
277         return security_ops->path_rmdir(dir, dentry);
278  }
279 +EXPORT_SYMBOL(security_path_rmdir);
280  
281  int security_path_unlink(struct path *dir, struct dentry *dentry)
282  {
283 @@ -427,6 +428,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
284                 return 0;
285         return security_ops->path_symlink(dir, dentry, old_name);
286  }
287 +EXPORT_SYMBOL(security_path_symlink);
288  
289  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
290                        struct dentry *new_dentry)
291 @@ -435,6 +437,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
292                 return 0;
293         return security_ops->path_link(old_dentry, new_dir, new_dentry);
294  }
295 +EXPORT_SYMBOL(security_path_link);
296  
297  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
298                          struct path *new_dir, struct dentry *new_dentry)
299 @@ -453,6 +456,7 @@ int security_path_truncate(struct path *path)
300                 return 0;
301         return security_ops->path_truncate(path);
302  }
303 +EXPORT_SYMBOL(security_path_truncate);
304  
305  int security_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
306                         mode_t mode)
307 @@ -461,6 +465,7 @@ int security_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
308                 return 0;
309         return security_ops->path_chmod(dentry, mnt, mode);
310  }
311 +EXPORT_SYMBOL(security_path_chmod);
312  
313  int security_path_chown(struct path *path, uid_t uid, gid_t gid)
314  {
315 @@ -468,6 +473,7 @@ int security_path_chown(struct path *path, uid_t uid, gid_t gid)
316                 return 0;
317         return security_ops->path_chown(path, uid, gid);
318  }
319 +EXPORT_SYMBOL(security_path_chown);
320  
321  int security_path_chroot(struct path *path)
322  {
323 @@ -544,6 +550,7 @@ int security_inode_readlink(struct dentry *dentry)
324                 return 0;
325         return security_ops->inode_readlink(dentry);
326  }
327 +EXPORT_SYMBOL(security_inode_readlink);
328  
329  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
330  {
331 @@ -558,6 +565,7 @@ int security_inode_permission(struct inode *inode, int mask)
332                 return 0;
333         return security_ops->inode_permission(inode, mask);
334  }
335 +EXPORT_SYMBOL(security_inode_permission);
336  
337  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
338  {
339 @@ -673,6 +681,7 @@ int security_file_permission(struct file *file, int mask)
340  
341         return fsnotify_perm(file, mask);
342  }
343 +EXPORT_SYMBOL(security_file_permission);
344  
345  int security_file_alloc(struct file *file)
346  {
347 @@ -700,6 +709,7 @@ int security_file_mmap(struct file *file, unsigned long reqprot,
348                 return ret;
349         return ima_file_mmap(file, prot);
350  }
351 +EXPORT_SYMBOL(security_file_mmap);
352  
353  int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
354                             unsigned long prot)
355 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
356 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
357 +++ linux/Documentation/ABI/testing/debugfs-aufs        2011-08-24 13:30:24.727980364 +0200
358 @@ -0,0 +1,37 @@
359 +What:          /debug/aufs/si_<id>/
360 +Date:          March 2009
361 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
362 +Description:
363 +               Under /debug/aufs, a directory named si_<id> is created
364 +               per aufs mount, where <id> is a unique id generated
365 +               internally.
366 +
367 +What:          /debug/aufs/si_<id>/xib
368 +Date:          March 2009
369 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
370 +Description:
371 +               It shows the consumed blocks by xib (External Inode Number
372 +               Bitmap), its block size and file size.
373 +               When the aufs mount option 'noxino' is specified, it
374 +               will be empty. About XINO files, see the aufs manual.
375 +
376 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
377 +Date:          March 2009
378 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
379 +Description:
380 +               It shows the consumed blocks by xino (External Inode Number
381 +               Translation Table), its link count, block size and file
382 +               size.
383 +               When the aufs mount option 'noxino' is specified, it
384 +               will be empty. About XINO files, see the aufs manual.
385 +
386 +What:          /debug/aufs/si_<id>/xigen
387 +Date:          March 2009
388 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
389 +Description:
390 +               It shows the consumed blocks by xigen (External Inode
391 +               Generation Table), its block size and file size.
392 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
393 +               be created.
394 +               When the aufs mount option 'noxino' is specified, it
395 +               will be empty. About XINO files, see the aufs manual.
396 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
397 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
398 +++ linux/Documentation/ABI/testing/sysfs-aufs  2011-08-24 13:30:24.727980364 +0200
399 @@ -0,0 +1,24 @@
400 +What:          /sys/fs/aufs/si_<id>/
401 +Date:          March 2009
402 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
403 +Description:
404 +               Under /sys/fs/aufs, a directory named si_<id> is created
405 +               per aufs mount, where <id> is a unique id generated
406 +               internally.
407 +
408 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
409 +Date:          March 2009
410 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
411 +Description:
412 +               It shows the abolute path of a member directory (which
413 +               is called branch) in aufs, and its permission.
414 +
415 +What:          /sys/fs/aufs/si_<id>/xi_path
416 +Date:          March 2009
417 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
418 +Description:
419 +               It shows the abolute path of XINO (External Inode Number
420 +               Bitmap, Translation Table and Generation Table) file
421 +               even if it is the default path.
422 +               When the aufs mount option 'noxino' is specified, it
423 +               will be empty. About XINO files, see the aufs manual.
424 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
425 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
426 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2011-08-24 13:30:24.727980364 +0200
427 @@ -0,0 +1,162 @@
428 +
429 +# Copyright (C) 2005-2011 Junjiro R. Okajima
430 +# 
431 +# This program is free software; you can redistribute it and/or modify
432 +# it under the terms of the GNU General Public License as published by
433 +# the Free Software Foundation; either version 2 of the License, or
434 +# (at your option) any later version.
435 +# 
436 +# This program is distributed in the hope that it will be useful,
437 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
438 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
439 +# GNU General Public License for more details.
440 +# 
441 +# You should have received a copy of the GNU General Public License
442 +# along with this program; if not, write to the Free Software
443 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
444 +
445 +Introduction
446 +----------------------------------------
447 +
448 +aufs [ei ju: ef es] | [a u f s]
449 +1. abbrev. for "advanced multi-layered unification filesystem".
450 +2. abbrev. for "another unionfs".
451 +3. abbrev. for "auf das" in German which means "on the" in English.
452 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
453 +   But "Filesystem aufs Filesystem" is hard to understand.
454 +
455 +AUFS is a filesystem with features:
456 +- multi layered stackable unification filesystem, the member directory
457 +  is called as a branch.
458 +- branch permission and attribute, 'readonly', 'real-readonly',
459 +  'readwrite', 'whiteout-able', 'link-able whiteout' and their
460 +  combination.
461 +- internal "file copy-on-write".
462 +- logical deletion, whiteout.
463 +- dynamic branch manipulation, adding, deleting and changing permission.
464 +- allow bypassing aufs, user's direct branch access.
465 +- external inode number translation table and bitmap which maintains the
466 +  persistent aufs inode number.
467 +- seekable directory, including NFS readdir.
468 +- file mapping, mmap and sharing pages.
469 +- pseudo-link, hardlink over branches.
470 +- loopback mounted filesystem as a branch.
471 +- several policies to select one among multiple writable branches.
472 +- revert a single systemcall when an error occurs in aufs.
473 +- and more...
474 +
475 +
476 +Multi Layered Stackable Unification Filesystem
477 +----------------------------------------------------------------------
478 +Most people already knows what it is.
479 +It is a filesystem which unifies several directories and provides a
480 +merged single directory. When users access a file, the access will be
481 +passed/re-directed/converted (sorry, I am not sure which English word is
482 +correct) to the real file on the member filesystem. The member
483 +filesystem is called 'lower filesystem' or 'branch' and has a mode
484 +'readonly' and 'readwrite.' And the deletion for a file on the lower
485 +readonly branch is handled by creating 'whiteout' on the upper writable
486 +branch.
487 +
488 +On LKML, there have been discussions about UnionMount (Jan Blunck,
489 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
490 +different approaches to implement the merged-view.
491 +The former tries putting it into VFS, and the latter implements as a
492 +separate filesystem.
493 +(If I misunderstand about these implementations, please let me know and
494 +I shall correct it. Because it is a long time ago when I read their
495 +source files last time).
496 +
497 +UnionMount's approach will be able to small, but may be hard to share
498 +branches between several UnionMount since the whiteout in it is
499 +implemented in the inode on branch filesystem and always
500 +shared. According to Bharata's post, readdir does not seems to be
501 +finished yet.
502 +There are several missing features known in this implementations such as
503 +- for users, the inode number may change silently. eg. copy-up.
504 +- link(2) may break by copy-up.
505 +- read(2) may get an obsoleted filedata (fstat(2) too).
506 +- fcntl(F_SETLK) may be broken by copy-up.
507 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
508 +  open(O_RDWR).
509 +
510 +Unionfs has a longer history. When I started implementing a stacking filesystem
511 +(Aug 2005), it already existed. It has virtual super_block, inode,
512 +dentry and file objects and they have an array pointing lower same kind
513 +objects. After contributing many patches for Unionfs, I re-started my
514 +project AUFS (Jun 2006).
515 +
516 +In AUFS, the structure of filesystem resembles to Unionfs, but I
517 +implemented my own ideas, approaches and enhancements and it became
518 +totally different one.
519 +
520 +Comparing DM snapshot and fs based implementation
521 +- the number of bytes to be copied between devices is much smaller.
522 +- the type of filesystem must be one and only.
523 +- the fs must be writable, no readonly fs, even for the lower original
524 +  device. so the compression fs will not be usable. but if we use
525 +  loopback mount, we may address this issue.
526 +  for instance,
527 +       mount /cdrom/squashfs.img /sq
528 +       losetup /sq/ext2.img
529 +       losetup /somewhere/cow
530 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
531 +- it will be difficult (or needs more operations) to extract the
532 +  difference between the original device and COW.
533 +- DM snapshot-merge may help a lot when users try merging. in the
534 +  fs-layer union, users will use rsync(1).
535 +
536 +
537 +Several characters/aspects of aufs
538 +----------------------------------------------------------------------
539 +
540 +Aufs has several characters or aspects.
541 +1. a filesystem, callee of VFS helper
542 +2. sub-VFS, caller of VFS helper for branches
543 +3. a virtual filesystem which maintains persistent inode number
544 +4. reader/writer of files on branches such like an application
545 +
546 +1. Callee of VFS Helper
547 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
548 +unlink(2) from an application reaches sys_unlink() kernel function and
549 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
550 +calls filesystem specific unlink operation. Actually aufs implements the
551 +unlink operation but it behaves like a redirector.
552 +
553 +2. Caller of VFS Helper for Branches
554 +aufs_unlink() passes the unlink request to the branch filesystem as if
555 +it were called from VFS. So the called unlink operation of the branch
556 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
557 +every necessary pre/post operation for the branch filesystem.
558 +- acquire the lock for the parent dir on a branch
559 +- lookup in a branch
560 +- revalidate dentry on a branch
561 +- mnt_want_write() for a branch
562 +- vfs_unlink() for a branch
563 +- mnt_drop_write() for a branch
564 +- release the lock on a branch
565 +
566 +3. Persistent Inode Number
567 +One of the most important issue for a filesystem is to maintain inode
568 +numbers. This is particularly important to support exporting a
569 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
570 +backend block device for its own. But some storage is necessary to
571 +maintain inode number. It may be a large space and may not suit to keep
572 +in memory. Aufs rents some space from its first writable branch
573 +filesystem (by default) and creates file(s) on it. These files are
574 +created by aufs internally and removed soon (currently) keeping opened.
575 +Note: Because these files are removed, they are totally gone after
576 +      unmounting aufs. It means the inode numbers are not persistent
577 +      across unmount or reboot. I have a plan to make them really
578 +      persistent which will be important for aufs on NFS server.
579 +
580 +4. Read/Write Files Internally (copy-on-write)
581 +Because a branch can be readonly, when you write a file on it, aufs will
582 +"copy-up" it to the upper writable branch internally. And then write the
583 +originally requested thing to the file. Generally kernel doesn't
584 +open/read/write file actively. In aufs, even a single write may cause a
585 +internal "file copy". This behaviour is very similar to cp(1) command.
586 +
587 +Some people may think it is better to pass such work to user space
588 +helper, instead of doing in kernel space. Actually I am still thinking
589 +about it. But currently I have implemented it in kernel space.
590 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
591 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
592 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2011-08-24 13:30:24.727980364 +0200
593 @@ -0,0 +1,226 @@
594 +
595 +# Copyright (C) 2005-2011 Junjiro R. Okajima
596 +# 
597 +# This program is free software; you can redistribute it and/or modify
598 +# it under the terms of the GNU General Public License as published by
599 +# the Free Software Foundation; either version 2 of the License, or
600 +# (at your option) any later version.
601 +# 
602 +# This program is distributed in the hope that it will be useful,
603 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
604 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
605 +# GNU General Public License for more details.
606 +# 
607 +# You should have received a copy of the GNU General Public License
608 +# along with this program; if not, write to the Free Software
609 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
610 +
611 +Basic Aufs Internal Structure
612 +
613 +Superblock/Inode/Dentry/File Objects
614 +----------------------------------------------------------------------
615 +As like an ordinary filesystem, aufs has its own
616 +superblock/inode/dentry/file objects. All these objects have a
617 +dynamically allocated array and store the same kind of pointers to the
618 +lower filesystem, branch.
619 +For example, when you build a union with one readwrite branch and one
620 +readonly, mounted /au, /rw and /ro respectively.
621 +- /au = /rw + /ro
622 +- /ro/fileA exists but /rw/fileA
623 +
624 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
625 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
626 +- [0] = NULL
627 +- [1] = /ro/fileA
628 +
629 +This style of an array is essentially same to the aufs
630 +superblock/inode/dentry/file objects.
631 +
632 +Because aufs supports manipulating branches, ie. add/delete/change
633 +dynamically, these objects has its own generation. When branches are
634 +changed, the generation in aufs superblock is incremented. And a
635 +generation in other object are compared when it is accessed.
636 +When a generation in other objects are obsoleted, aufs refreshes the
637 +internal array.
638 +
639 +
640 +Superblock
641 +----------------------------------------------------------------------
642 +Additionally aufs superblock has some data for policies to select one
643 +among multiple writable branches, XIB files, pseudo-links and kobject.
644 +See below in detail.
645 +About the policies which supports copy-down a directory, see policy.txt
646 +too.
647 +
648 +
649 +Branch and XINO(External Inode Number Translation Table)
650 +----------------------------------------------------------------------
651 +Every branch has its own xino (external inode number translation table)
652 +file. The xino file is created and unlinked by aufs internally. When two
653 +members of a union exist on the same filesystem, they share the single
654 +xino file.
655 +The struct of a xino file is simple, just a sequence of aufs inode
656 +numbers which is indexed by the lower inode number.
657 +In the above sample, assume the inode number of /ro/fileA is i111 and
658 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
659 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
660 +
661 +When the inode numbers are not contiguous, the xino file will be sparse
662 +which has a hole in it and doesn't consume as much disk space as it
663 +might appear. If your branch filesystem consumes disk space for such
664 +holes, then you should specify 'xino=' option at mounting aufs.
665 +
666 +Also a writable branch has three kinds of "whiteout bases". All these
667 +are existed when the branch is joined to aufs and the names are
668 +whiteout-ed doubly, so that users will never see their names in aufs
669 +hierarchy.
670 +1. a regular file which will be linked to all whiteouts.
671 +2. a directory to store a pseudo-link.
672 +3. a directory to store an "orphan-ed" file temporary.
673 +
674 +1. Whiteout Base
675 +   When you remove a file on a readonly branch, aufs handles it as a
676 +   logical deletion and creates a whiteout on the upper writable branch
677 +   as a hardlink of this file in order not to consume inode on the
678 +   writable branch.
679 +2. Pseudo-link Dir
680 +   See below, Pseudo-link.
681 +3. Step-Parent Dir
682 +   When "fileC" exists on the lower readonly branch only and it is
683 +   opened and removed with its parent dir, and then user writes
684 +   something into it, then aufs copies-up fileC to this
685 +   directory. Because there is no other dir to store fileC. After
686 +   creating a file under this dir, the file is unlinked.
687 +
688 +Because aufs supports manipulating branches, ie. add/delete/change
689 +dynamically, a branch has its own id. When the branch order changes, aufs
690 +finds the new index by searching the branch id.
691 +
692 +
693 +Pseudo-link
694 +----------------------------------------------------------------------
695 +Assume "fileA" exists on the lower readonly branch only and it is
696 +hardlinked to "fileB" on the branch. When you write something to fileA,
697 +aufs copies-up it to the upper writable branch. Additionally aufs
698 +creates a hardlink under the Pseudo-link Directory of the writable
699 +branch. The inode of a pseudo-link is kept in aufs super_block as a
700 +simple list. If fileB is read after unlinking fileA, aufs returns
701 +filedata from the pseudo-link instead of the lower readonly
702 +branch. Because the pseudo-link is based upon the inode, to keep the
703 +inode number by xino (see above) is important.
704 +
705 +All the hardlinks under the Pseudo-link Directory of the writable branch
706 +should be restored in a proper location later. Aufs provides a utility
707 +to do this. The userspace helpers executed at remounting and unmounting
708 +aufs by default.
709 +During this utility is running, it puts aufs into the pseudo-link
710 +maintenance mode. In this mode, only the process which began the
711 +maintenance mode (and its child processes) is allowed to operate in
712 +aufs. Some other processes which are not related to the pseudo-link will
713 +be allowed to run too, but the rest have to return an error or wait
714 +until the maintenance mode ends. If a process already acquires an inode
715 +mutex (in VFS), it has to return an error.
716 +
717 +
718 +XIB(external inode number bitmap)
719 +----------------------------------------------------------------------
720 +Addition to the xino file per a branch, aufs has an external inode number
721 +bitmap in a superblock object. It is also a file such like a xino file.
722 +It is a simple bitmap to mark whether the aufs inode number is in-use or
723 +not.
724 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
725 +
726 +Aufs implements a feature to truncate/refresh both of xino and xib to
727 +reduce the number of consumed disk blocks for these files.
728 +
729 +
730 +Virtual or Vertical Dir, and Readdir in Userspace
731 +----------------------------------------------------------------------
732 +In order to support multiple layers (branches), aufs readdir operation
733 +constructs a virtual dir block on memory. For readdir, aufs calls
734 +vfs_readdir() internally for each dir on branches, merges their entries
735 +with eliminating the whiteout-ed ones, and sets it to file (dir)
736 +object. So the file object has its entry list until it is closed. The
737 +entry list will be updated when the file position is zero and becomes
738 +old. This decision is made in aufs automatically.
739 +
740 +The dynamically allocated memory block for the name of entries has a
741 +unit of 512 bytes (by default) and stores the names contiguously (no
742 +padding). Another block for each entry is handled by kmem_cache too.
743 +During building dir blocks, aufs creates hash list and judging whether
744 +the entry is whiteouted by its upper branch or already listed.
745 +The merged result is cached in the corresponding inode object and
746 +maintained by a customizable life-time option.
747 +
748 +Some people may call it can be a security hole or invite DoS attack
749 +since the opened and once readdir-ed dir (file object) holds its entry
750 +list and becomes a pressure for system memory. But I'd say it is similar
751 +to files under /proc or /sys. The virtual files in them also holds a
752 +memory page (generally) while they are opened. When an idea to reduce
753 +memory for them is introduced, it will be applied to aufs too.
754 +For those who really hate this situation, I've developed readdir(3)
755 +library which operates this merging in userspace. You just need to set
756 +LD_PRELOAD environment variable, and aufs will not consume no memory in
757 +kernel space for readdir(3).
758 +
759 +
760 +Workqueue
761 +----------------------------------------------------------------------
762 +Aufs sometimes requires privilege access to a branch. For instance,
763 +in copy-up/down operation. When a user process is going to make changes
764 +to a file which exists in the lower readonly branch only, and the mode
765 +of one of ancestor directories may not be writable by a user
766 +process. Here aufs copy-up the file with its ancestors and they may
767 +require privilege to set its owner/group/mode/etc.
768 +This is a typical case of a application character of aufs (see
769 +Introduction).
770 +
771 +Aufs uses workqueue synchronously for this case. It creates its own
772 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
773 +passes the request to call mkdir or write (for example), and wait for
774 +its completion. This approach solves a problem of a signal handler
775 +simply.
776 +If aufs didn't adopt the workqueue and changed the privilege of the
777 +process, and if the mkdir/write call arises SIGXFSZ or other signal,
778 +then the user process might gain a privilege or the generated core file
779 +was owned by a superuser.
780 +
781 +Also aufs uses the system global workqueue ("events" kernel thread) too
782 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
783 +whiteout base and etc. This is unrelated to a privilege.
784 +Most of aufs operation tries acquiring a rw_semaphore for aufs
785 +superblock at the beginning, at the same time waits for the completion
786 +of all queued asynchronous tasks.
787 +
788 +
789 +Whiteout
790 +----------------------------------------------------------------------
791 +The whiteout in aufs is very similar to Unionfs's. That is represented
792 +by its filename. UnionMount takes an approach of a file mode, but I am
793 +afraid several utilities (find(1) or something) will have to support it.
794 +
795 +Basically the whiteout represents "logical deletion" which stops aufs to
796 +lookup further, but also it represents "dir is opaque" which also stop
797 +lookup.
798 +
799 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
800 +In order to make several functions in a single systemcall to be
801 +revertible, aufs adopts an approach to rename a directory to a temporary
802 +unique whiteouted name.
803 +For example, in rename(2) dir where the target dir already existed, aufs
804 +renames the target dir to a temporary unique whiteouted name before the
805 +actual rename on a branch and then handles other actions (make it opaque,
806 +update the attributes, etc). If an error happens in these actions, aufs
807 +simply renames the whiteouted name back and returns an error. If all are
808 +succeeded, aufs registers a function to remove the whiteouted unique
809 +temporary name completely and asynchronously to the system global
810 +workqueue.
811 +
812 +
813 +Copy-up
814 +----------------------------------------------------------------------
815 +It is a well-known feature or concept.
816 +When user modifies a file on a readonly branch, aufs operate "copy-up"
817 +internally and makes change to the new file on the upper writable branch.
818 +When the trigger systemcall does not update the timestamps of the parent
819 +dir, aufs reverts it after copy-up.
820 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
821 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
822 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2011-08-24 13:30:24.727980364 +0200
823 @@ -0,0 +1,106 @@
824 +
825 +# Copyright (C) 2005-2011 Junjiro R. Okajima
826 +# 
827 +# This program is free software; you can redistribute it and/or modify
828 +# it under the terms of the GNU General Public License as published by
829 +# the Free Software Foundation; either version 2 of the License, or
830 +# (at your option) any later version.
831 +# 
832 +# This program is distributed in the hope that it will be useful,
833 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
834 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
835 +# GNU General Public License for more details.
836 +# 
837 +# You should have received a copy of the GNU General Public License
838 +# along with this program; if not, write to the Free Software
839 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
840 +
841 +Lookup in a Branch
842 +----------------------------------------------------------------------
843 +Since aufs has a character of sub-VFS (see Introduction), it operates
844 +lookup for branches as VFS does. It may be a heavy work. Generally
845 +speaking struct nameidata is a bigger structure and includes many
846 +information. But almost all lookup operation in aufs is the simplest
847 +case, ie. lookup only an entry directly connected to its parent. Digging
848 +down the directory hierarchy is unnecessary.
849 +
850 +VFS has a function lookup_one_len() for that use, but it is not usable
851 +for a branch filesystem which requires struct nameidata. So aufs
852 +implements a simple lookup wrapper function. When a branch filesystem
853 +allows NULL as nameidata, it calls lookup_one_len(). Otherwise it builds
854 +a simplest nameidata and calls lookup_hash().
855 +Here aufs applies "a principle in NFSD", ie. if the filesystem supports
856 +NFS-export, then it has to support NULL as a nameidata parameter for
857 +->create(), ->lookup() and ->d_revalidate(). So the lookup wrapper in
858 +aufs tests if ->s_export_op in the branch is NULL or not.
859 +
860 +When a branch is a remote filesystem, aufs basically trusts its
861 +->d_revalidate(), also aufs forces the hardest revalidate tests for
862 +them.
863 +For d_revalidate, aufs implements three levels of revalidate tests. See
864 +"Revalidate Dentry and UDBA" in detail.
865 +
866 +
867 +Loopback Mount
868 +----------------------------------------------------------------------
869 +Basically aufs supports any type of filesystem and block device for a
870 +branch (actually there are some exceptions). But it is prohibited to add
871 +a loopback mounted one whose backend file exists in a filesystem which is
872 +already added to aufs. The reason is to protect aufs from a recursive
873 +lookup. If it was allowed, the aufs lookup operation might re-enter a
874 +lookup for the loopback mounted branch in the same context, and will
875 +cause a deadlock.
876 +
877 +
878 +Revalidate Dentry and UDBA (User's Direct Branch Access)
879 +----------------------------------------------------------------------
880 +Generally VFS helpers re-validate a dentry as a part of lookup.
881 +0. digging down the directory hierarchy.
882 +1. lock the parent dir by its i_mutex.
883 +2. lookup the final (child) entry.
884 +3. revalidate it.
885 +4. call the actual operation (create, unlink, etc.)
886 +5. unlock the parent dir
887 +
888 +If the filesystem implements its ->d_revalidate() (step 3), then it is
889 +called. Actually aufs implements it and checks the dentry on a branch is
890 +still valid.
891 +But it is not enough. Because aufs has to release the lock for the
892 +parent dir on a branch at the end of ->lookup() (step 2) and
893 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
894 +held by VFS.
895 +If the file on a branch is changed directly, eg. bypassing aufs, after
896 +aufs released the lock, then the subsequent operation may cause
897 +something unpleasant result.
898 +
899 +This situation is a result of VFS architecture, ->lookup() and
900 +->d_revalidate() is separated. But I never say it is wrong. It is a good
901 +design from VFS's point of view. It is just not suitable for sub-VFS
902 +character in aufs.
903 +
904 +Aufs supports such case by three level of revalidation which is
905 +selectable by user.
906 +1. Simple Revalidate
907 +   Addition to the native flow in VFS's, confirm the child-parent
908 +   relationship on the branch just after locking the parent dir on the
909 +   branch in the "actual operation" (step 4). When this validation
910 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
911 +   checks the validation of the dentry on branches.
912 +2. Monitor Changes Internally by Inotify/Fsnotify
913 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
914 +   the dentry on the branch, and returns EBUSY if it finds different
915 +   dentry.
916 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
917 +   during it is in cache. When the event is notified, aufs registers a
918 +   function to kernel 'events' thread by schedule_work(). And the
919 +   function sets some special status to the cached aufs dentry and inode
920 +   private data. If they are not cached, then aufs has nothing to
921 +   do. When the same file is accessed through aufs (step 0-3) later,
922 +   aufs will detect the status and refresh all necessary data.
923 +   In this mode, aufs has to ignore the event which is fired by aufs
924 +   itself.
925 +3. No Extra Validation
926 +   This is the simplest test and doesn't add any additional revalidation
927 +   test, and skip therevalidatin in step 4. It is useful and improves
928 +   aufs performance when system surely hide the aufs branches from user,
929 +   by over-mounting something (or another method).
930 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
931 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
932 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2011-08-24 13:30:24.727980364 +0200
933 @@ -0,0 +1,76 @@
934 +
935 +# Copyright (C) 2005-2011 Junjiro R. Okajima
936 +# 
937 +# This program is free software; you can redistribute it and/or modify
938 +# it under the terms of the GNU General Public License as published by
939 +# the Free Software Foundation; either version 2 of the License, or
940 +# (at your option) any later version.
941 +# 
942 +# This program is distributed in the hope that it will be useful,
943 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
944 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
945 +# GNU General Public License for more details.
946 +# 
947 +# You should have received a copy of the GNU General Public License
948 +# along with this program; if not, write to the Free Software
949 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
950 +
951 +Branch Manipulation
952 +
953 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
954 +and changing its permission/attribute, there are a lot of works to do.
955 +
956 +
957 +Add a Branch
958 +----------------------------------------------------------------------
959 +o Confirm the adding dir exists outside of aufs, including loopback
960 +  mount.
961 +- and other various attributes...
962 +o Initialize the xino file and whiteout bases if necessary.
963 +  See struct.txt.
964 +
965 +o Check the owner/group/mode of the directory
966 +  When the owner/group/mode of the adding directory differs from the
967 +  existing branch, aufs issues a warning because it may impose a
968 +  security risk.
969 +  For example, when a upper writable branch has a world writable empty
970 +  top directory, a malicious user can create any files on the writable
971 +  branch directly, like copy-up and modify manually. If something like
972 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
973 +  writable branch, and the writable branch is world-writable, then a
974 +  malicious guy may create /etc/passwd on the writable branch directly
975 +  and the infected file will be valid in aufs.
976 +  I am afraid it can be a security issue, but nothing to do except
977 +  producing a warning.
978 +
979 +
980 +Delete a Branch
981 +----------------------------------------------------------------------
982 +o Confirm the deleting branch is not busy
983 +  To be general, there is one merit to adopt "remount" interface to
984 +  manipulate branches. It is to discard caches. At deleting a branch,
985 +  aufs checks the still cached (and connected) dentries and inodes. If
986 +  there are any, then they are all in-use. An inode without its
987 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
988 +
989 +  For the cached one, aufs checks whether the same named entry exists on
990 +  other branches.
991 +  If the cached one is a directory, because aufs provides a merged view
992 +  to users, as long as one dir is left on any branch aufs can show the
993 +  dir to users. In this case, the branch can be removed from aufs.
994 +  Otherwise aufs rejects deleting the branch.
995 +
996 +  If any file on the deleting branch is opened by aufs, then aufs
997 +  rejects deleting.
998 +
999 +
1000 +Modify the Permission of a Branch
1001 +----------------------------------------------------------------------
1002 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1003 +  See struct.txt.
1004 +
1005 +o rw --> ro: Confirm the modifying branch is not busy
1006 +  Aufs rejects the request if any of these conditions are true.
1007 +  - a file on the branch is mmap-ed.
1008 +  - a regular file on the branch is opened for write and there is no
1009 +    same named entry on the upper branch.
1010 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1011 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1012 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2011-08-24 13:30:24.727980364 +0200
1013 @@ -0,0 +1,65 @@
1014 +
1015 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1016 +# 
1017 +# This program is free software; you can redistribute it and/or modify
1018 +# it under the terms of the GNU General Public License as published by
1019 +# the Free Software Foundation; either version 2 of the License, or
1020 +# (at your option) any later version.
1021 +# 
1022 +# This program is distributed in the hope that it will be useful,
1023 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1024 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1025 +# GNU General Public License for more details.
1026 +# 
1027 +# You should have received a copy of the GNU General Public License
1028 +# along with this program; if not, write to the Free Software
1029 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1030 +
1031 +Policies to Select One among Multiple Writable Branches
1032 +----------------------------------------------------------------------
1033 +When the number of writable branch is more than one, aufs has to decide
1034 +the target branch for file creation or copy-up. By default, the highest
1035 +writable branch which has the parent (or ancestor) dir of the target
1036 +file is chosen (top-down-parent policy).
1037 +By user's request, aufs implements some other policies to select the
1038 +writable branch, for file creation two policies, round-robin and
1039 +most-free-space policies. For copy-up three policies, top-down-parent,
1040 +bottom-up-parent and bottom-up policies.
1041 +
1042 +As expected, the round-robin policy selects the branch in circular. When
1043 +you have two writable branches and creates 10 new files, 5 files will be
1044 +created for each branch. mkdir(2) systemcall is an exception. When you
1045 +create 10 new directories, all will be created on the same branch.
1046 +And the most-free-space policy selects the one which has most free
1047 +space among the writable branches. The amount of free space will be
1048 +checked by aufs internally, and users can specify its time interval.
1049 +
1050 +The policies for copy-up is more simple,
1051 +top-down-parent is equivalent to the same named on in create policy,
1052 +bottom-up-parent selects the writable branch where the parent dir
1053 +exists and the nearest upper one from the copyup-source,
1054 +bottom-up selects the nearest upper writable branch from the
1055 +copyup-source, regardless the existence of the parent dir.
1056 +
1057 +There are some rules or exceptions to apply these policies.
1058 +- If there is a readonly branch above the policy-selected branch and
1059 +  the parent dir is marked as opaque (a variation of whiteout), or the
1060 +  target (creating) file is whiteout-ed on the upper readonly branch,
1061 +  then the result of the policy is ignored and the target file will be
1062 +  created on the nearest upper writable branch than the readonly branch.
1063 +- If there is a writable branch above the policy-selected branch and
1064 +  the parent dir is marked as opaque or the target file is whiteouted
1065 +  on the branch, then the result of the policy is ignored and the target
1066 +  file will be created on the highest one among the upper writable
1067 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1068 +  it as usual.
1069 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1070 +  They try selecting the branch where the source exists as possible
1071 +  since copyup a large file will take long time. If it can't be,
1072 +  ie. the branch where the source exists is readonly, then they will
1073 +  follow the copyup policy.
1074 +- There is an exception for rename(2) when the target exists.
1075 +  If the rename target exists, aufs compares the index of the branches
1076 +  where the source and the target exists and selects the higher
1077 +  one. If the selected branch is readonly, then aufs follows the
1078 +  copyup policy.
1079 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1080 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1081 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2011-08-24 13:30:24.727980364 +0200
1082 @@ -0,0 +1,47 @@
1083 +
1084 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1085 +# 
1086 +# This program is free software; you can redistribute it and/or modify
1087 +# it under the terms of the GNU General Public License as published by
1088 +# the Free Software Foundation; either version 2 of the License, or
1089 +# (at your option) any later version.
1090 +# 
1091 +# This program is distributed in the hope that it will be useful,
1092 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1093 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1094 +# GNU General Public License for more details.
1095 +# 
1096 +# You should have received a copy of the GNU General Public License
1097 +# along with this program; if not, write to the Free Software
1098 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1099 +
1100 +mmap(2) -- File Memory Mapping
1101 +----------------------------------------------------------------------
1102 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1103 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1104 +->mmap().
1105 +This approach is simple and good, but there is one problem.
1106 +Under /proc, several entries show the mmap-ped files by its path (with
1107 +device and inode number), and the printed path will be the path on the
1108 +branch fs's instead of virtual aufs's.
1109 +This is not a problem in most cases, but some utilities lsof(1) (and its
1110 +user) may expect the path on aufs.
1111 +
1112 +To address this issue, aufs adds a new member called vm_prfile in struct
1113 +vm_area_struct (and struct vm_region). The original vm_file points to
1114 +the file on the branch fs in order to handle everything correctly as
1115 +usual. The new vm_prfile points to a virtual file in aufs, and the
1116 +show-functions in procfs refers to vm_prfile if it is set.
1117 +Also we need to maintain several other places where touching vm_file
1118 +such like
1119 +- fork()/clone() copies vma and the reference count of vm_file is
1120 +  incremented.
1121 +- merging vma maintains the ref count too.
1122 +
1123 +This is not a good approach. It just faking the printed path. But it
1124 +leaves all behaviour around f_mapping unchanged. This is surely an
1125 +advantage.
1126 +Actually aufs had adopted another complicated approach which calls
1127 +generic_file_mmap() and handles struct vm_operations_struct. In this
1128 +approach, aufs met a hard problem and I could not solve it without
1129 +switching the approach.
1130 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
1131 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1132 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2011-08-24 13:30:24.727980364 +0200
1133 @@ -0,0 +1,59 @@
1134 +
1135 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1136 +# 
1137 +# This program is free software; you can redistribute it and/or modify
1138 +# it under the terms of the GNU General Public License as published by
1139 +# the Free Software Foundation; either version 2 of the License, or
1140 +# (at your option) any later version.
1141 +# 
1142 +# This program is distributed in the hope that it will be useful,
1143 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1144 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1145 +# GNU General Public License for more details.
1146 +# 
1147 +# You should have received a copy of the GNU General Public License
1148 +# along with this program; if not, write to the Free Software
1149 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1150 +
1151 +Export Aufs via NFS
1152 +----------------------------------------------------------------------
1153 +Here is an approach.
1154 +- like xino/xib, add a new file 'xigen' which stores aufs inode
1155 +  generation.
1156 +- iget_locked(): initialize aufs inode generation for a new inode, and
1157 +  store it in xigen file.
1158 +- destroy_inode(): increment aufs inode generation and store it in xigen
1159 +  file. it is necessary even if it is not unlinked, because any data of
1160 +  inode may be changed by UDBA.
1161 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
1162 +  build file handle by
1163 +  + branch id (4 bytes)
1164 +  + superblock generation (4 bytes)
1165 +  + inode number (4 or 8 bytes)
1166 +  + parent dir inode number (4 or 8 bytes)
1167 +  + inode generation (4 bytes))
1168 +  + return value of exportfs_encode_fh() for the parent on a branch (4
1169 +    bytes)
1170 +  + file handle for a branch (by exportfs_encode_fh())
1171 +- fh_to_dentry():
1172 +  + find the index of a branch from its id in handle, and check it is
1173 +    still exist in aufs.
1174 +  + 1st level: get the inode number from handle and search it in cache.
1175 +  + 2nd level: if not found, get the parent inode number from handle and
1176 +    search it in cache. and then open the parent dir, find the matching
1177 +    inode number by vfs_readdir() and get its name, and call
1178 +    lookup_one_len() for the target dentry.
1179 +  + 3rd level: if the parent dir is not cached, call
1180 +    exportfs_decode_fh() for a branch and get the parent on a branch,
1181 +    build a pathname of it, convert it a pathname in aufs, call
1182 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
1183 +    the 2nd level.
1184 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
1185 +    for every branch, but not itself. to get this, (currently) aufs
1186 +    searches in current->nsproxy->mnt_ns list. it may not be a good
1187 +    idea, but I didn't get other approach.
1188 +  + test the generation of the gotten inode.
1189 +- every inode operation: they may get EBUSY due to UDBA. in this case,
1190 +  convert it into ESTALE for NFSD.
1191 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
1192 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
1193 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
1194 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
1195 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2011-08-24 13:30:24.727980364 +0200
1196 @@ -0,0 +1,53 @@
1197 +
1198 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1199 +# 
1200 +# This program is free software; you can redistribute it and/or modify
1201 +# it under the terms of the GNU General Public License as published by
1202 +# the Free Software Foundation; either version 2 of the License, or
1203 +# (at your option) any later version.
1204 +# 
1205 +# This program is distributed in the hope that it will be useful,
1206 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1207 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1208 +# GNU General Public License for more details.
1209 +# 
1210 +# You should have received a copy of the GNU General Public License
1211 +# along with this program; if not, write to the Free Software
1212 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1213 +
1214 +Show Whiteout Mode (shwh)
1215 +----------------------------------------------------------------------
1216 +Generally aufs hides the name of whiteouts. But in some cases, to show
1217 +them is very useful for users. For instance, creating a new middle layer
1218 +(branch) by merging existing layers.
1219 +
1220 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
1221 +When you have three branches,
1222 +- Bottom: 'system', squashfs (underlying base system), read-only
1223 +- Middle: 'mods', squashfs, read-only
1224 +- Top: 'overlay', ram (tmpfs), read-write
1225 +
1226 +The top layer is loaded at boot time and saved at shutdown, to preserve
1227 +the changes made to the system during the session.
1228 +When larger changes have been made, or smaller changes have accumulated,
1229 +the size of the saved top layer data grows. At this point, it would be
1230 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
1231 +and rewrite the 'mods' squashfs, clearing the top layer and thus
1232 +restoring save and load speed.
1233 +
1234 +This merging is simplified by the use of another aufs mount, of just the
1235 +two overlay branches using the 'shwh' option.
1236 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
1237 +       aufs /livesys/merge_union
1238 +
1239 +A merged view of these two branches is then available at
1240 +/livesys/merge_union, and the new feature is that the whiteouts are
1241 +visible!
1242 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
1243 +writing to all branches. Also the default mode for all branches is 'ro'.
1244 +It is now possible to save the combined contents of the two overlay
1245 +branches to a new squashfs, e.g.:
1246 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
1247 +
1248 +This new squashfs archive can be stored on the boot device and the
1249 +initramfs will use it to replace the old one at the next boot.
1250 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
1251 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
1252 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2011-08-24 13:30:24.727980364 +0200
1253 @@ -0,0 +1,47 @@
1254 +
1255 +# Copyright (C) 2010-2011 Junjiro R. Okajima
1256 +#
1257 +# This program is free software; you can redistribute it and/or modify
1258 +# it under the terms of the GNU General Public License as published by
1259 +# the Free Software Foundation; either version 2 of the License, or
1260 +# (at your option) any later version.
1261 +#
1262 +# This program is distributed in the hope that it will be useful,
1263 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1264 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1265 +# GNU General Public License for more details.
1266 +#
1267 +# You should have received a copy of the GNU General Public License
1268 +# along with this program; if not, write to the Free Software
1269 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1270 +
1271 +Dynamically customizable FS operations
1272 +----------------------------------------------------------------------
1273 +Generally FS operations (struct inode_operations, struct
1274 +address_space_operations, struct file_operations, etc.) are defined as
1275 +"static const", but it never means that FS have only one set of
1276 +operation. Some FS have multiple sets of them. For instance, ext2 has
1277 +three sets, one for XIP, for NOBH, and for normal.
1278 +Since aufs overrides and redirects these operations, sometimes aufs has
1279 +to change its behaviour according to the branch FS type. More imporantly
1280 +VFS acts differently if a function (member in the struct) is set or
1281 +not. It means aufs should have several sets of operations and select one
1282 +among them according to the branch FS definition.
1283 +
1284 +In order to solve this problem and not to affect the behavour of VFS,
1285 +aufs defines these operations dynamically. For instance, aufs defines
1286 +aio_read function for struct file_operations, but it may not be set to
1287 +the file_operations. When the branch FS doesn't have it, aufs doesn't
1288 +set it to its file_operations while the function definition itself is
1289 +still alive. So the behaviour of io_submit(2) will not change, and it
1290 +will return an error when aio_read is not defined.
1291 +
1292 +The lifetime of these dynamically generated operation object is
1293 +maintained by aufs branch object. When the branch is removed from aufs,
1294 +the reference counter of the object is decremented. When it reaches
1295 +zero, the dynamically generated operation object will be freed.
1296 +
1297 +This approach is designed to support AIO (io_submit), Direcit I/O and
1298 +XIP mainly.
1299 +Currently this approach is applied to file_operations and
1300 +vm_operations_struct for regular files only.
1301 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
1302 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
1303 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2011-08-24 13:30:24.727980364 +0200
1304 @@ -0,0 +1,96 @@
1305 +
1306 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1307 +# 
1308 +# This program is free software; you can redistribute it and/or modify
1309 +# it under the terms of the GNU General Public License as published by
1310 +# the Free Software Foundation; either version 2 of the License, or
1311 +# (at your option) any later version.
1312 +# 
1313 +# This program is distributed in the hope that it will be useful,
1314 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1315 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1316 +# GNU General Public License for more details.
1317 +# 
1318 +# You should have received a copy of the GNU General Public License
1319 +# along with this program; if not, write to the Free Software
1320 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1321 +
1322 +Plan
1323 +
1324 +Restoring some features which was implemented in aufs1.
1325 +They were dropped in aufs2 in order to make source files simpler and
1326 +easier to be reviewed.
1327 +
1328 +
1329 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1330 +----------------------------------------------------------------------
1331 +Let's try case study.
1332 +- aufs has two branches, upper readwrite and lower readonly.
1333 +  /au = /rw + /ro
1334 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1335 +- user invoked "chmod a+rx /au/dirA"
1336 +- then "dirA" becomes world readable?
1337 +
1338 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1339 +or it may be a natively readonly filesystem. If aufs respects the lower
1340 +branch, it should not respond readdir request from other users. But user
1341 +allowed it by chmod. Should really aufs rejects showing the entries
1342 +under /ro/dirA?
1343 +
1344 +To be honest, I don't have a best solution for this case. So I
1345 +implemented 'dirperm1' and 'nodirperm1' option in aufs1, and leave it to
1346 +users.
1347 +When dirperm1 is specified, aufs checks only the highest one for the
1348 +directory permission, and shows the entries. Otherwise, as usual, checks
1349 +every dir existing on all branches and rejects the request.
1350 +
1351 +As a side effect, dirperm1 option improves the performance of aufs
1352 +because the number of permission check is reduced.
1353 +
1354 +
1355 +Being Another Aufs's Readonly Branch (robr)
1356 +----------------------------------------------------------------------
1357 +Aufs1 allows aufs to be another aufs's readonly branch.
1358 +This feature was developed by a user's request. But it may not be used
1359 +currecnly.
1360 +
1361 +
1362 +Copy-up on Open (coo=)
1363 +----------------------------------------------------------------------
1364 +By default the internal copy-up is executed when it is really necessary.
1365 +It is not done when a file is opened for writing, but when write(2) is
1366 +done. Users who have many (over 100) branches want to know and analyse
1367 +when and what file is copied-up. To insert a new upper branch which
1368 +contains such files only may improve the performance of aufs.
1369 +
1370 +Aufs1 implemented "coo=none | leaf | all" option.
1371 +
1372 +
1373 +Refresh the Opened File (refrof)
1374 +----------------------------------------------------------------------
1375 +This option is implemented in aufs1 but incomplete.
1376 +
1377 +When user reads from a file, he expects to get its latest filedata
1378 +generally. If the file is removed and a new same named file is created,
1379 +the content he gets is unchanged, ie. the unlinked filedata.
1380 +
1381 +Let's try case study again.
1382 +- aufs has two branches.
1383 +  /au = /rw + /ro
1384 +- "fileA" exists under /ro, but /rw.
1385 +- user opened "/au/fileA".
1386 +- he or someone else inserts a branch (/new) between /rw and /ro.
1387 +  /au = /rw + /new + /ro
1388 +- the new branch has "fileA".
1389 +- user reads from the opened "fileA"
1390 +- which filedata should aufs return, from /ro or /new?
1391 +
1392 +Some people says it has to be "from /ro" and it is a semantics of Unix.
1393 +The others say it should be "from /new" because the file is not removed
1394 +and it is equivalent to the case of someone else modifies the file.
1395 +
1396 +Here again I don't have a best and final answer. I got an idea to
1397 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
1398 +Opened File) is specified (by default), aufs returns the filedata from
1399 +/new.
1400 +Otherwise from /new.
1401 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
1402 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
1403 +++ linux/Documentation/filesystems/aufs/README 2012-01-05 11:00:59.215501779 +0100
1404 @@ -0,0 +1,328 @@
1405 +
1406 +Aufs3 -- advanced multi layered unification filesystem version 3.x
1407 +http://aufs.sf.net
1408 +Junjiro R. Okajima
1409 +
1410 +
1411 +0. Introduction
1412 +----------------------------------------
1413 +In the early days, aufs was entirely re-designed and re-implemented
1414 +Unionfs Version 1.x series. After many original ideas, approaches,
1415 +improvements and implementations, it becomes totally different from
1416 +Unionfs while keeping the basic features.
1417 +Recently, Unionfs Version 2.x series begin taking some of the same
1418 +approaches to aufs1's.
1419 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
1420 +University and his team.
1421 +
1422 +Aufs3 supports linux-3.0 and later.
1423 +If you want older kernel version support, try aufs2-2.6.git or
1424 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
1425 +
1426 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
1427 +According to Christoph Hellwig, linux rejects all union-type filesystems
1428 +but UnionMount.
1429 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
1430 +
1431 +
1432 +1. Features
1433 +----------------------------------------
1434 +- unite several directories into a single virtual filesystem. The member
1435 +  directory is called as a branch.
1436 +- you can specify the permission flags to the branch, which are 'readonly',
1437 +  'readwrite' and 'whiteout-able.'
1438 +- by upper writable branch, internal copyup and whiteout, files/dirs on
1439 +  readonly branch are modifiable logically.
1440 +- dynamic branch manipulation, add, del.
1441 +- etc...
1442 +
1443 +Also there are many enhancements in aufs1, such as:
1444 +- readdir(3) in userspace.
1445 +- keep inode number by external inode number table
1446 +- keep the timestamps of file/dir in internal copyup operation
1447 +- seekable directory, supporting NFS readdir.
1448 +- whiteout is hardlinked in order to reduce the consumption of inodes
1449 +  on branch
1450 +- do not copyup, nor create a whiteout when it is unnecessary
1451 +- revert a single systemcall when an error occurs in aufs
1452 +- remount interface instead of ioctl
1453 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
1454 +- loopback mounted filesystem as a branch
1455 +- kernel thread for removing the dir who has a plenty of whiteouts
1456 +- support copyup sparse file (a file which has a 'hole' in it)
1457 +- default permission flags for branches
1458 +- selectable permission flags for ro branch, whether whiteout can
1459 +  exist or not
1460 +- export via NFS.
1461 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
1462 +- support multiple writable branches, some policies to select one
1463 +  among multiple writable branches.
1464 +- a new semantics for link(2) and rename(2) to support multiple
1465 +  writable branches.
1466 +- no glibc changes are required.
1467 +- pseudo hardlink (hardlink over branches)
1468 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
1469 +  including NFS or remote filesystem branch.
1470 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
1471 +- and more...
1472 +
1473 +Currently these features are dropped temporary from aufs3.
1474 +See design/08plan.txt in detail.
1475 +- test only the highest one for the directory permission (dirperm1)
1476 +- copyup on open (coo=)
1477 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
1478 +  (robr)
1479 +- statistics of aufs thread (/sys/fs/aufs/stat)
1480 +- delegation mode (dlgt)
1481 +  a delegation of the internal branch access to support task I/O
1482 +  accounting, which also supports Linux Security Modules (LSM) mainly
1483 +  for Suse AppArmor.
1484 +- intent.open/create (file open in a single lookup)
1485 +
1486 +Features or just an idea in the future (see also design/*.txt),
1487 +- reorder the branch index without del/re-add.
1488 +- permanent xino files for NFSD
1489 +- an option for refreshing the opened files after add/del branches
1490 +- 'move' policy for copy-up between two writable branches, after
1491 +  checking free space.
1492 +- light version, without branch manipulation. (unnecessary?)
1493 +- copyup in userspace
1494 +- inotify in userspace
1495 +- readv/writev
1496 +- xattr, acl
1497 +
1498 +
1499 +2. Download
1500 +----------------------------------------
1501 +There were three GIT trees for aufs3, aufs3-linux.git,
1502 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
1503 +"aufs-util.git."
1504 +While the aufs-util is always necessary, you need either of aufs3-linux
1505 +or aufs3-standalone.
1506 +
1507 +The aufs3-linux tree includes the whole linux mainline GIT tree,
1508 +git://git.kernel.org/.../torvalds/linux.git.
1509 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
1510 +build aufs3 as an externel kernel module.
1511 +
1512 +On the other hand, the aufs3-standalone tree has only aufs source files
1513 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
1514 +
1515 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
1516 +represents the linux kernel version, "linux-3.x". For instance,
1517 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
1518 +"aufs3.x-rcN" branch.
1519 +
1520 +o aufs3-linux tree
1521 +$ git clone --reference /your/linux/git/tree \
1522 +       git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-linux.git \
1523 +       aufs3-linux.git
1524 +- if you don't have linux GIT tree, then remove "--reference ..."
1525 +$ cd aufs3-linux.git
1526 +$ git checkout origin/aufs3.0
1527 +
1528 +o aufs3-standalone tree
1529 +$ git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git \
1530 +       aufs3-standalone.git
1531 +$ cd aufs3-standalone.git
1532 +$ git checkout origin/aufs3.0
1533 +
1534 +o aufs-util tree
1535 +$ git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs-util.git \
1536 +       aufs-util.git
1537 +$ cd aufs-util.git
1538 +$ git checkout origin/aufs3.0
1539 +
1540 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
1541 +The minor version number, 'x' in '3.x', of aufs may not always
1542 +follow the minor version number of the kernel.
1543 +Because changes in the kernel that cause the use of a new
1544 +minor version number do not always require changes to aufs-util.
1545 +
1546 +Since aufs-util has its own minor version number, you may not be
1547 +able to find a GIT branch in aufs-util for your kernel's
1548 +exact minor version number.
1549 +In this case, you should git-checkout the branch for the
1550 +nearest lower number.
1551 +
1552 +For (an unreleased) example:
1553 +If you are using "linux-3.10" and the "aufs3.10" branch
1554 +does not exit in aufs-util repository, then "aufs3.9", "aufs3.8"
1555 +or something numerically smaller is the branch for your kernel.
1556 +
1557 +Also you can view all branches by
1558 +       $ git branch -a
1559 +
1560 +
1561 +3. Configuration and Compilation
1562 +----------------------------------------
1563 +Make sure you have git-checkout'ed the correct branch.
1564 +
1565 +For aufs3-linux tree,
1566 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS.
1567 +- set other aufs configurations if necessary.
1568 +
1569 +For aufs3-standalone tree,
1570 +There are several ways to build.
1571 +
1572 +1.
1573 +- apply ./aufs3-kbuild.patch to your kernel source files.
1574 +- apply ./aufs3-base.patch too.
1575 +- apply ./aufs3-proc_map.patch too, if you want to make /proc/PID/maps (and
1576 +  others including lsof(1)) show the file path on aufs instead of the
1577 +  path on the branch fs.
1578 +- apply ./aufs3-standalone.patch too, if you have a plan to set
1579 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
1580 +- copy ./{Documentation,fs,include/linux/aufs_type.h} files to your
1581 +  kernel source tree. Never copy ./include/linux/Kbuild.
1582 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS, you can select either
1583 +  =m or =y.
1584 +- and build your kernel as usual.
1585 +- install the built kernel.
1586 +- install the header files too by "make headers_install".
1587 +- and reboot your system.
1588 +
1589 +2.
1590 +- module only (CONFIG_AUFS_FS=m).
1591 +- apply ./aufs3-base.patch to your kernel source files.
1592 +- apply ./aufs3-proc_map.patch too to your kernel source files,
1593 +  if you want to make /proc/PID/maps (and others including lsof(1)) show
1594 +  the file path on aufs instead of the path on the branch fs.
1595 +- apply ./aufs3-standalone.patch too.
1596 +- build your kernel, don't forget "make headers_install", and reboot.
1597 +- edit ./config.mk and set other aufs configurations if necessary.
1598 +  Note: You should read ./fs/aufs/Kconfig carefully which describes
1599 +  every aufs configurations.
1600 +- build the module by simple "make".
1601 +- you can specify ${KDIR} make variable which points to your kernel
1602 +  source tree.
1603 +- install the files
1604 +  + run "make install" to install the aufs module, or copy the built
1605 +    ./aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
1606 +  + run "make headers_install" to install the aufs header file (you can
1607 +    specify DESTDIR), or copty ./usr/include/linux/aufs_type.h to
1608 +    /usr/include/linux or wherever you like.
1609 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
1610 +  kernel source tree.
1611 +
1612 +Note: The haeder file aufs_type.h is necessary to build aufs-util
1613 +      as well as "make headers_install" in the kernel source tree.
1614 +      headers_install is subject to be forgotten, but it is essentially
1615 +      necessary, not only for building aufs-util.
1616 +      You may not meet problems without headers_install in some older
1617 +      version though.
1618 +
1619 +And then,
1620 +- read README in aufs-util, build and install it
1621 +- note that your distribution may contain an obsoleted version of
1622 +  aufs_type.h in /usr/include/linux or something. When you build aufs
1623 +  utilities, make sure that your compiler refers the correct aufs header
1624 +  file which is built by "make headers_install."
1625 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
1626 +  then run "make install_ulib" too. And refer to the aufs manual in
1627 +  detail.
1628 +
1629 +
1630 +4. Usage
1631 +----------------------------------------
1632 +At first, make sure aufs-util are installed, and please read the aufs
1633 +manual, aufs.5 in aufs-util.git tree.
1634 +$ man -l aufs.5
1635 +
1636 +And then,
1637 +$ mkdir /tmp/rw /tmp/aufs
1638 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
1639 +
1640 +Here is another example. The result is equivalent.
1641 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
1642 +  Or
1643 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
1644 +# mount -o remount,append:${HOME} /tmp/aufs
1645 +
1646 +Then, you can see whole tree of your home dir through /tmp/aufs. If
1647 +you modify a file under /tmp/aufs, the one on your home directory is
1648 +not affected, instead the same named file will be newly created under
1649 +/tmp/rw. And all of your modification to a file will be applied to
1650 +the one under /tmp/rw. This is called the file based Copy on Write
1651 +(COW) method.
1652 +Aufs mount options are described in aufs.5.
1653 +If you run chroot or something and make your aufs as a root directory,
1654 +then you need to customize the shutdown script. See the aufs manual in
1655 +detail.
1656 +
1657 +Additionally, there are some sample usages of aufs which are a
1658 +diskless system with network booting, and LiveCD over NFS.
1659 +See sample dir in CVS tree on SourceForge.
1660 +
1661 +
1662 +5. Contact
1663 +----------------------------------------
1664 +When you have any problems or strange behaviour in aufs, please let me
1665 +know with:
1666 +- /proc/mounts (instead of the output of mount(8))
1667 +- /sys/module/aufs/*
1668 +- /sys/fs/aufs/* (if you have them)
1669 +- /debug/aufs/* (if you have them)
1670 +- linux kernel version
1671 +  if your kernel is not plain, for example modified by distributor,
1672 +  the url where i can download its source is necessary too.
1673 +- aufs version which was printed at loading the module or booting the
1674 +  system, instead of the date you downloaded.
1675 +- configuration (define/undefine CONFIG_AUFS_xxx)
1676 +- kernel configuration or /proc/config.gz (if you have it)
1677 +- behaviour which you think to be incorrect
1678 +- actual operation, reproducible one is better
1679 +- mailto: aufs-users at lists.sourceforge.net
1680 +
1681 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
1682 +and Feature Requests) on SourceForge. Please join and write to
1683 +aufs-users ML.
1684 +
1685 +
1686 +6. Acknowledgements
1687 +----------------------------------------
1688 +Thanks to everyone who have tried and are using aufs, whoever
1689 +have reported a bug or any feedback.
1690 +
1691 +Especially donators:
1692 +Tomas Matejicek(slax.org) made a donation (much more than once).
1693 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
1694 +       scripts) is making "doubling" donations.
1695 +       Unfortunately I cannot list all of the donators, but I really
1696 +       appriciate.
1697 +       It ends Aug 2010, but the ordinary donation URL is still available.
1698 +       <http://sourceforge.net/donate/index.php?group_id=167503>
1699 +Dai Itasaka made a donation (2007/8).
1700 +Chuck Smith made a donation (2008/4, 10 and 12).
1701 +Henk Schoneveld made a donation (2008/9).
1702 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
1703 +Francois Dupoux made a donation (2008/11).
1704 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
1705 +       aufs2 GIT tree (2009/2).
1706 +William Grant made a donation (2009/3).
1707 +Patrick Lane made a donation (2009/4).
1708 +The Mail Archive (mail-archive.com) made donations (2009/5).
1709 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
1710 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
1711 +Pavel Pronskiy made a donation (2011/2).
1712 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
1713 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
1714 +Max Lekomcev (DOM-TV project) made a donation (2011/7).
1715 +Sam Liddicott made a donation (2011/9).
1716 +
1717 +Thank you very much.
1718 +Donations are always, including future donations, very important and
1719 +helpful for me to keep on developing aufs.
1720 +
1721 +
1722 +7.
1723 +----------------------------------------
1724 +If you are an experienced user, no explanation is needed. Aufs is
1725 +just a linux filesystem.
1726 +
1727 +
1728 +Enjoy!
1729 +
1730 +# Local variables: ;
1731 +# mode: text;
1732 +# End: ;
1733 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
1734 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
1735 +++ linux/fs/aufs/aufs.h        2011-08-24 13:30:24.731313534 +0200
1736 @@ -0,0 +1,60 @@
1737 +/*
1738 + * Copyright (C) 2005-2011 Junjiro R. Okajima
1739 + *
1740 + * This program, aufs is free software; you can redistribute it and/or modify
1741 + * it under the terms of the GNU General Public License as published by
1742 + * the Free Software Foundation; either version 2 of the License, or
1743 + * (at your option) any later version.
1744 + *
1745 + * This program is distributed in the hope that it will be useful,
1746 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1747 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1748 + * GNU General Public License for more details.
1749 + *
1750 + * You should have received a copy of the GNU General Public License
1751 + * along with this program; if not, write to the Free Software
1752 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1753 + */
1754 +
1755 +/*
1756 + * all header files
1757 + */
1758 +
1759 +#ifndef __AUFS_H__
1760 +#define __AUFS_H__
1761 +
1762 +#ifdef __KERNEL__
1763 +
1764 +#define AuStub(type, name, body, ...) \
1765 +       static inline type name(__VA_ARGS__) { body; }
1766 +
1767 +#define AuStubVoid(name, ...) \
1768 +       AuStub(void, name, , __VA_ARGS__)
1769 +#define AuStubInt0(name, ...) \
1770 +       AuStub(int, name, return 0, __VA_ARGS__)
1771 +
1772 +#include "debug.h"
1773 +
1774 +#include "branch.h"
1775 +#include "cpup.h"
1776 +#include "dcsub.h"
1777 +#include "dbgaufs.h"
1778 +#include "dentry.h"
1779 +#include "dir.h"
1780 +#include "dynop.h"
1781 +#include "file.h"
1782 +#include "fstype.h"
1783 +#include "inode.h"
1784 +#include "loop.h"
1785 +#include "module.h"
1786 +#include "opts.h"
1787 +#include "rwsem.h"
1788 +#include "spl.h"
1789 +#include "super.h"
1790 +#include "sysaufs.h"
1791 +#include "vfsub.h"
1792 +#include "whout.h"
1793 +#include "wkq.h"
1794 +
1795 +#endif /* __KERNEL__ */
1796 +#endif /* __AUFS_H__ */
1797 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
1798 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
1799 +++ linux/fs/aufs/branch.c      2011-08-24 13:30:24.731313534 +0200
1800 @@ -0,0 +1,1170 @@
1801 +/*
1802 + * Copyright (C) 2005-2011 Junjiro R. Okajima
1803 + *
1804 + * This program, aufs is free software; you can redistribute it and/or modify
1805 + * it under the terms of the GNU General Public License as published by
1806 + * the Free Software Foundation; either version 2 of the License, or
1807 + * (at your option) any later version.
1808 + *
1809 + * This program is distributed in the hope that it will be useful,
1810 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1811 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1812 + * GNU General Public License for more details.
1813 + *
1814 + * You should have received a copy of the GNU General Public License
1815 + * along with this program; if not, write to the Free Software
1816 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1817 + */
1818 +
1819 +/*
1820 + * branch management
1821 + */
1822 +
1823 +#include <linux/compat.h>
1824 +#include <linux/file.h>
1825 +#include <linux/statfs.h>
1826 +#include "aufs.h"
1827 +
1828 +/*
1829 + * free a single branch
1830 + */
1831 +static void au_br_do_free(struct au_branch *br)
1832 +{
1833 +       int i;
1834 +       struct au_wbr *wbr;
1835 +       struct au_dykey **key;
1836 +
1837 +       au_hnotify_fin_br(br);
1838 +
1839 +       if (br->br_xino.xi_file)
1840 +               fput(br->br_xino.xi_file);
1841 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
1842 +
1843 +       AuDebugOn(atomic_read(&br->br_count));
1844 +
1845 +       wbr = br->br_wbr;
1846 +       if (wbr) {
1847 +               for (i = 0; i < AuBrWh_Last; i++)
1848 +                       dput(wbr->wbr_wh[i]);
1849 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
1850 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
1851 +       }
1852 +
1853 +       key = br->br_dykey;
1854 +       for (i = 0; i < AuBrDynOp; i++, key++)
1855 +               if (*key)
1856 +                       au_dy_put(*key);
1857 +               else
1858 +                       break;
1859 +
1860 +       mntput(br->br_mnt);
1861 +       kfree(wbr);
1862 +       kfree(br);
1863 +}
1864 +
1865 +/*
1866 + * frees all branches
1867 + */
1868 +void au_br_free(struct au_sbinfo *sbinfo)
1869 +{
1870 +       aufs_bindex_t bmax;
1871 +       struct au_branch **br;
1872 +
1873 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
1874 +
1875 +       bmax = sbinfo->si_bend + 1;
1876 +       br = sbinfo->si_branch;
1877 +       while (bmax--)
1878 +               au_br_do_free(*br++);
1879 +}
1880 +
1881 +/*
1882 + * find the index of a branch which is specified by @br_id.
1883 + */
1884 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
1885 +{
1886 +       aufs_bindex_t bindex, bend;
1887 +
1888 +       bend = au_sbend(sb);
1889 +       for (bindex = 0; bindex <= bend; bindex++)
1890 +               if (au_sbr_id(sb, bindex) == br_id)
1891 +                       return bindex;
1892 +       return -1;
1893 +}
1894 +
1895 +/* ---------------------------------------------------------------------- */
1896 +
1897 +/*
1898 + * add a branch
1899 + */
1900 +
1901 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
1902 +                       struct dentry *h_root)
1903 +{
1904 +       if (unlikely(h_adding == h_root
1905 +                    || au_test_loopback_overlap(sb, h_adding)))
1906 +               return 1;
1907 +       if (h_adding->d_sb != h_root->d_sb)
1908 +               return 0;
1909 +       return au_test_subdir(h_adding, h_root)
1910 +               || au_test_subdir(h_root, h_adding);
1911 +}
1912 +
1913 +/*
1914 + * returns a newly allocated branch. @new_nbranch is a number of branches
1915 + * after adding a branch.
1916 + */
1917 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
1918 +                                    int perm)
1919 +{
1920 +       struct au_branch *add_branch;
1921 +       struct dentry *root;
1922 +       int err;
1923 +
1924 +       err = -ENOMEM;
1925 +       root = sb->s_root;
1926 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
1927 +       if (unlikely(!add_branch))
1928 +               goto out;
1929 +
1930 +       err = au_hnotify_init_br(add_branch, perm);
1931 +       if (unlikely(err))
1932 +               goto out_br;
1933 +
1934 +       add_branch->br_wbr = NULL;
1935 +       if (au_br_writable(perm)) {
1936 +               /* may be freed separately at changing the branch permission */
1937 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
1938 +                                            GFP_NOFS);
1939 +               if (unlikely(!add_branch->br_wbr))
1940 +                       goto out_hnotify;
1941 +       }
1942 +
1943 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
1944 +       if (!err)
1945 +               err = au_di_realloc(au_di(root), new_nbranch);
1946 +       if (!err)
1947 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
1948 +       if (!err)
1949 +               return add_branch; /* success */
1950 +
1951 +       kfree(add_branch->br_wbr);
1952 +
1953 +out_hnotify:
1954 +       au_hnotify_fin_br(add_branch);
1955 +out_br:
1956 +       kfree(add_branch);
1957 +out:
1958 +       return ERR_PTR(err);
1959 +}
1960 +
1961 +/*
1962 + * test if the branch permission is legal or not.
1963 + */
1964 +static int test_br(struct inode *inode, int brperm, char *path)
1965 +{
1966 +       int err;
1967 +
1968 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
1969 +       if (!err)
1970 +               goto out;
1971 +
1972 +       err = -EINVAL;
1973 +       pr_err("write permission for readonly mount or inode, %s\n", path);
1974 +
1975 +out:
1976 +       return err;
1977 +}
1978 +
1979 +/*
1980 + * returns:
1981 + * 0: success, the caller will add it
1982 + * plus: success, it is already unified, the caller should ignore it
1983 + * minus: error
1984 + */
1985 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
1986 +{
1987 +       int err;
1988 +       aufs_bindex_t bend, bindex;
1989 +       struct dentry *root;
1990 +       struct inode *inode, *h_inode;
1991 +
1992 +       root = sb->s_root;
1993 +       bend = au_sbend(sb);
1994 +       if (unlikely(bend >= 0
1995 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
1996 +               err = 1;
1997 +               if (!remount) {
1998 +                       err = -EINVAL;
1999 +                       pr_err("%s duplicated\n", add->pathname);
2000 +               }
2001 +               goto out;
2002 +       }
2003 +
2004 +       err = -ENOSPC; /* -E2BIG; */
2005 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2006 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2007 +               pr_err("number of branches exceeded %s\n", add->pathname);
2008 +               goto out;
2009 +       }
2010 +
2011 +       err = -EDOM;
2012 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2013 +               pr_err("bad index %d\n", add->bindex);
2014 +               goto out;
2015 +       }
2016 +
2017 +       inode = add->path.dentry->d_inode;
2018 +       err = -ENOENT;
2019 +       if (unlikely(!inode->i_nlink)) {
2020 +               pr_err("no existence %s\n", add->pathname);
2021 +               goto out;
2022 +       }
2023 +
2024 +       err = -EINVAL;
2025 +       if (unlikely(inode->i_sb == sb)) {
2026 +               pr_err("%s must be outside\n", add->pathname);
2027 +               goto out;
2028 +       }
2029 +
2030 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2031 +               pr_err("unsupported filesystem, %s (%s)\n",
2032 +                      add->pathname, au_sbtype(inode->i_sb));
2033 +               goto out;
2034 +       }
2035 +
2036 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
2037 +       if (unlikely(err))
2038 +               goto out;
2039 +
2040 +       if (bend < 0)
2041 +               return 0; /* success */
2042 +
2043 +       err = -EINVAL;
2044 +       for (bindex = 0; bindex <= bend; bindex++)
2045 +               if (unlikely(test_overlap(sb, add->path.dentry,
2046 +                                         au_h_dptr(root, bindex)))) {
2047 +                       pr_err("%s is overlapped\n", add->pathname);
2048 +                       goto out;
2049 +               }
2050 +
2051 +       err = 0;
2052 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2053 +               h_inode = au_h_dptr(root, 0)->d_inode;
2054 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2055 +                   || h_inode->i_uid != inode->i_uid
2056 +                   || h_inode->i_gid != inode->i_gid)
2057 +                       pr_warning("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2058 +                                  add->pathname,
2059 +                                  inode->i_uid, inode->i_gid,
2060 +                                  (inode->i_mode & S_IALLUGO),
2061 +                                  h_inode->i_uid, h_inode->i_gid,
2062 +                                  (h_inode->i_mode & S_IALLUGO));
2063 +       }
2064 +
2065 +out:
2066 +       return err;
2067 +}
2068 +
2069 +/*
2070 + * initialize or clean the whiteouts for an adding branch
2071 + */
2072 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2073 +                        int new_perm, struct dentry *h_root)
2074 +{
2075 +       int err, old_perm;
2076 +       aufs_bindex_t bindex;
2077 +       struct mutex *h_mtx;
2078 +       struct au_wbr *wbr;
2079 +       struct au_hinode *hdir;
2080 +
2081 +       wbr = br->br_wbr;
2082 +       old_perm = br->br_perm;
2083 +       br->br_perm = new_perm;
2084 +       hdir = NULL;
2085 +       h_mtx = NULL;
2086 +       bindex = au_br_index(sb, br->br_id);
2087 +       if (0 <= bindex) {
2088 +               hdir = au_hi(sb->s_root->d_inode, bindex);
2089 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
2090 +       } else {
2091 +               h_mtx = &h_root->d_inode->i_mutex;
2092 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
2093 +       }
2094 +       if (!wbr)
2095 +               err = au_wh_init(h_root, br, sb);
2096 +       else {
2097 +               wbr_wh_write_lock(wbr);
2098 +               err = au_wh_init(h_root, br, sb);
2099 +               wbr_wh_write_unlock(wbr);
2100 +       }
2101 +       if (hdir)
2102 +               au_hn_imtx_unlock(hdir);
2103 +       else
2104 +               mutex_unlock(h_mtx);
2105 +       br->br_perm = old_perm;
2106 +
2107 +       if (!err && wbr && !au_br_writable(new_perm)) {
2108 +               kfree(wbr);
2109 +               br->br_wbr = NULL;
2110 +       }
2111 +
2112 +       return err;
2113 +}
2114 +
2115 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2116 +                      int perm, struct path *path)
2117 +{
2118 +       int err;
2119 +       struct kstatfs kst;
2120 +       struct au_wbr *wbr;
2121 +       struct dentry *h_dentry;
2122 +
2123 +       wbr = br->br_wbr;
2124 +       au_rw_init(&wbr->wbr_wh_rwsem);
2125 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
2126 +       atomic_set(&wbr->wbr_wh_running, 0);
2127 +       wbr->wbr_bytes = 0;
2128 +
2129 +       /*
2130 +        * a limit for rmdir/rename a dir
2131 +        * cf. AUFS_MAX_NAMELEN in include/linux/aufs_type.h
2132 +        */
2133 +       err = vfs_statfs(path, &kst);
2134 +       if (unlikely(err))
2135 +               goto out;
2136 +       err = -EINVAL;
2137 +       h_dentry = path->dentry;
2138 +       if (kst.f_namelen >= NAME_MAX)
2139 +               err = au_br_init_wh(sb, br, perm, h_dentry);
2140 +       else
2141 +               pr_err("%.*s(%s), unsupported namelen %ld\n",
2142 +                      AuDLNPair(h_dentry), au_sbtype(h_dentry->d_sb),
2143 +                      kst.f_namelen);
2144 +
2145 +out:
2146 +       return err;
2147 +}
2148 +
2149 +/* intialize a new branch */
2150 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2151 +                     struct au_opt_add *add)
2152 +{
2153 +       int err;
2154 +
2155 +       err = 0;
2156 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
2157 +       mutex_init(&br->br_xino.xi_nondir_mtx);
2158 +       br->br_perm = add->perm;
2159 +       br->br_mnt = add->path.mnt; /* set first, mntget() later */
2160 +       spin_lock_init(&br->br_dykey_lock);
2161 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
2162 +       atomic_set(&br->br_count, 0);
2163 +       br->br_xino_upper = AUFS_XINO_TRUNC_INIT;
2164 +       atomic_set(&br->br_xino_running, 0);
2165 +       br->br_id = au_new_br_id(sb);
2166 +       AuDebugOn(br->br_id < 0);
2167 +
2168 +       if (au_br_writable(add->perm)) {
2169 +               err = au_wbr_init(br, sb, add->perm, &add->path);
2170 +               if (unlikely(err))
2171 +                       goto out_err;
2172 +       }
2173 +
2174 +       if (au_opt_test(au_mntflags(sb), XINO)) {
2175 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
2176 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
2177 +               if (unlikely(err)) {
2178 +                       AuDebugOn(br->br_xino.xi_file);
2179 +                       goto out_err;
2180 +               }
2181 +       }
2182 +
2183 +       sysaufs_br_init(br);
2184 +       mntget(add->path.mnt);
2185 +       goto out; /* success */
2186 +
2187 +out_err:
2188 +       br->br_mnt = NULL;
2189 +out:
2190 +       return err;
2191 +}
2192 +
2193 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
2194 +                            struct au_branch *br, aufs_bindex_t bend,
2195 +                            aufs_bindex_t amount)
2196 +{
2197 +       struct au_branch **brp;
2198 +
2199 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2200 +
2201 +       brp = sbinfo->si_branch + bindex;
2202 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
2203 +       *brp = br;
2204 +       sbinfo->si_bend++;
2205 +       if (unlikely(bend < 0))
2206 +               sbinfo->si_bend = 0;
2207 +}
2208 +
2209 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
2210 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2211 +{
2212 +       struct au_hdentry *hdp;
2213 +
2214 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2215 +
2216 +       hdp = dinfo->di_hdentry + bindex;
2217 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
2218 +       au_h_dentry_init(hdp);
2219 +       dinfo->di_bend++;
2220 +       if (unlikely(bend < 0))
2221 +               dinfo->di_bstart = 0;
2222 +}
2223 +
2224 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
2225 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2226 +{
2227 +       struct au_hinode *hip;
2228 +
2229 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2230 +
2231 +       hip = iinfo->ii_hinode + bindex;
2232 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
2233 +       hip->hi_inode = NULL;
2234 +       au_hn_init(hip);
2235 +       iinfo->ii_bend++;
2236 +       if (unlikely(bend < 0))
2237 +               iinfo->ii_bstart = 0;
2238 +}
2239 +
2240 +static void au_br_do_add(struct super_block *sb, struct dentry *h_dentry,
2241 +                        struct au_branch *br, aufs_bindex_t bindex)
2242 +{
2243 +       struct dentry *root;
2244 +       struct inode *root_inode;
2245 +       aufs_bindex_t bend, amount;
2246 +
2247 +       root = sb->s_root;
2248 +       root_inode = root->d_inode;
2249 +       bend = au_sbend(sb);
2250 +       amount = bend + 1 - bindex;
2251 +       au_sbilist_lock();
2252 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
2253 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
2254 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
2255 +       au_set_h_dptr(root, bindex, dget(h_dentry));
2256 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
2257 +                     /*flags*/0);
2258 +       au_sbilist_unlock();
2259 +}
2260 +
2261 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
2262 +{
2263 +       int err;
2264 +       aufs_bindex_t bend, add_bindex;
2265 +       struct dentry *root, *h_dentry;
2266 +       struct inode *root_inode;
2267 +       struct au_branch *add_branch;
2268 +
2269 +       root = sb->s_root;
2270 +       root_inode = root->d_inode;
2271 +       IMustLock(root_inode);
2272 +       err = test_add(sb, add, remount);
2273 +       if (unlikely(err < 0))
2274 +               goto out;
2275 +       if (err) {
2276 +               err = 0;
2277 +               goto out; /* success */
2278 +       }
2279 +
2280 +       bend = au_sbend(sb);
2281 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
2282 +       err = PTR_ERR(add_branch);
2283 +       if (IS_ERR(add_branch))
2284 +               goto out;
2285 +
2286 +       err = au_br_init(add_branch, sb, add);
2287 +       if (unlikely(err)) {
2288 +               au_br_do_free(add_branch);
2289 +               goto out;
2290 +       }
2291 +
2292 +       add_bindex = add->bindex;
2293 +       h_dentry = add->path.dentry;
2294 +       if (!remount)
2295 +               au_br_do_add(sb, h_dentry, add_branch, add_bindex);
2296 +       else {
2297 +               sysaufs_brs_del(sb, add_bindex);
2298 +               au_br_do_add(sb, h_dentry, add_branch, add_bindex);
2299 +               sysaufs_brs_add(sb, add_bindex);
2300 +       }
2301 +
2302 +       if (!add_bindex) {
2303 +               au_cpup_attr_all(root_inode, /*force*/1);
2304 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
2305 +       } else
2306 +               au_add_nlink(root_inode, h_dentry->d_inode);
2307 +
2308 +       /*
2309 +        * this test/set prevents aufs from handling unnecesary notify events
2310 +        * of xino files, in case of re-adding a writable branch which was
2311 +        * once detached from aufs.
2312 +        */
2313 +       if (au_xino_brid(sb) < 0
2314 +           && au_br_writable(add_branch->br_perm)
2315 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
2316 +           && add_branch->br_xino.xi_file
2317 +           && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
2318 +               au_xino_brid_set(sb, add_branch->br_id);
2319 +
2320 +out:
2321 +       return err;
2322 +}
2323 +
2324 +/* ---------------------------------------------------------------------- */
2325 +
2326 +/*
2327 + * delete a branch
2328 + */
2329 +
2330 +/* to show the line number, do not make it inlined function */
2331 +#define AuVerbose(do_info, fmt, ...) do { \
2332 +       if (do_info) \
2333 +               pr_info(fmt, ##__VA_ARGS__); \
2334 +} while (0)
2335 +
2336 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
2337 +                        aufs_bindex_t bend)
2338 +{
2339 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
2340 +}
2341 +
2342 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
2343 +                        aufs_bindex_t bend)
2344 +{
2345 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
2346 +}
2347 +
2348 +/*
2349 + * test if the branch is deletable or not.
2350 + */
2351 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
2352 +                           unsigned int sigen, const unsigned int verbose)
2353 +{
2354 +       int err, i, j, ndentry;
2355 +       aufs_bindex_t bstart, bend;
2356 +       struct au_dcsub_pages dpages;
2357 +       struct au_dpage *dpage;
2358 +       struct dentry *d;
2359 +
2360 +       err = au_dpages_init(&dpages, GFP_NOFS);
2361 +       if (unlikely(err))
2362 +               goto out;
2363 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
2364 +       if (unlikely(err))
2365 +               goto out_dpages;
2366 +
2367 +       for (i = 0; !err && i < dpages.ndpage; i++) {
2368 +               dpage = dpages.dpages + i;
2369 +               ndentry = dpage->ndentry;
2370 +               for (j = 0; !err && j < ndentry; j++) {
2371 +                       d = dpage->dentries[j];
2372 +                       AuDebugOn(!d->d_count);
2373 +                       if (!au_digen_test(d, sigen)) {
2374 +                               di_read_lock_child(d, AuLock_IR);
2375 +                               if (unlikely(au_dbrange_test(d))) {
2376 +                                       di_read_unlock(d, AuLock_IR);
2377 +                                       continue;
2378 +                               }
2379 +                       } else {
2380 +                               di_write_lock_child(d);
2381 +                               if (unlikely(au_dbrange_test(d))) {
2382 +                                       di_write_unlock(d);
2383 +                                       continue;
2384 +                               }
2385 +                               err = au_reval_dpath(d, sigen);
2386 +                               if (!err)
2387 +                                       di_downgrade_lock(d, AuLock_IR);
2388 +                               else {
2389 +                                       di_write_unlock(d);
2390 +                                       break;
2391 +                               }
2392 +                       }
2393 +
2394 +                       /* AuDbgDentry(d); */
2395 +                       bstart = au_dbstart(d);
2396 +                       bend = au_dbend(d);
2397 +                       if (bstart <= bindex
2398 +                           && bindex <= bend
2399 +                           && au_h_dptr(d, bindex)
2400 +                           && au_test_dbusy(d, bstart, bend)) {
2401 +                               err = -EBUSY;
2402 +                               AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
2403 +                               AuDbgDentry(d);
2404 +                       }
2405 +                       di_read_unlock(d, AuLock_IR);
2406 +               }
2407 +       }
2408 +
2409 +out_dpages:
2410 +       au_dpages_free(&dpages);
2411 +out:
2412 +       return err;
2413 +}
2414 +
2415 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
2416 +                          unsigned int sigen, const unsigned int verbose)
2417 +{
2418 +       int err;
2419 +       unsigned long long max, ull;
2420 +       struct inode *i, **array;
2421 +       aufs_bindex_t bstart, bend;
2422 +
2423 +       array = au_iarray_alloc(sb, &max);
2424 +       err = PTR_ERR(array);
2425 +       if (IS_ERR(array))
2426 +               goto out;
2427 +
2428 +       err = 0;
2429 +       AuDbg("b%d\n", bindex);
2430 +       for (ull = 0; !err && ull < max; ull++) {
2431 +               i = array[ull];
2432 +               if (i->i_ino == AUFS_ROOT_INO)
2433 +                       continue;
2434 +
2435 +               /* AuDbgInode(i); */
2436 +               if (au_iigen(i) == sigen)
2437 +                       ii_read_lock_child(i);
2438 +               else {
2439 +                       ii_write_lock_child(i);
2440 +                       err = au_refresh_hinode_self(i);
2441 +                       au_iigen_dec(i);
2442 +                       if (!err)
2443 +                               ii_downgrade_lock(i);
2444 +                       else {
2445 +                               ii_write_unlock(i);
2446 +                               break;
2447 +                       }
2448 +               }
2449 +
2450 +               bstart = au_ibstart(i);
2451 +               bend = au_ibend(i);
2452 +               if (bstart <= bindex
2453 +                   && bindex <= bend
2454 +                   && au_h_iptr(i, bindex)
2455 +                   && au_test_ibusy(i, bstart, bend)) {
2456 +                       err = -EBUSY;
2457 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
2458 +                       AuDbgInode(i);
2459 +               }
2460 +               ii_read_unlock(i);
2461 +       }
2462 +       au_iarray_free(array, max);
2463 +
2464 +out:
2465 +       return err;
2466 +}
2467 +
2468 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
2469 +                             const unsigned int verbose)
2470 +{
2471 +       int err;
2472 +       unsigned int sigen;
2473 +
2474 +       sigen = au_sigen(root->d_sb);
2475 +       DiMustNoWaiters(root);
2476 +       IiMustNoWaiters(root->d_inode);
2477 +       di_write_unlock(root);
2478 +       err = test_dentry_busy(root, bindex, sigen, verbose);
2479 +       if (!err)
2480 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
2481 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
2482 +
2483 +       return err;
2484 +}
2485 +
2486 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
2487 +                            const aufs_bindex_t bindex,
2488 +                            const aufs_bindex_t bend)
2489 +{
2490 +       struct au_branch **brp, **p;
2491 +
2492 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2493 +
2494 +       brp = sbinfo->si_branch + bindex;
2495 +       if (bindex < bend)
2496 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
2497 +       sbinfo->si_branch[0 + bend] = NULL;
2498 +       sbinfo->si_bend--;
2499 +
2500 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
2501 +       if (p)
2502 +               sbinfo->si_branch = p;
2503 +       /* harmless error */
2504 +}
2505 +
2506 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
2507 +                            const aufs_bindex_t bend)
2508 +{
2509 +       struct au_hdentry *hdp, *p;
2510 +
2511 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2512 +
2513 +       hdp = dinfo->di_hdentry;
2514 +       if (bindex < bend)
2515 +               memmove(hdp + bindex, hdp + bindex + 1,
2516 +                       sizeof(*hdp) * (bend - bindex));
2517 +       hdp[0 + bend].hd_dentry = NULL;
2518 +       dinfo->di_bend--;
2519 +
2520 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
2521 +       if (p)
2522 +               dinfo->di_hdentry = p;
2523 +       /* harmless error */
2524 +}
2525 +
2526 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
2527 +                            const aufs_bindex_t bend)
2528 +{
2529 +       struct au_hinode *hip, *p;
2530 +
2531 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2532 +
2533 +       hip = iinfo->ii_hinode + bindex;
2534 +       if (bindex < bend)
2535 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
2536 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
2537 +       au_hn_init(iinfo->ii_hinode + bend);
2538 +       iinfo->ii_bend--;
2539 +
2540 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
2541 +       if (p)
2542 +               iinfo->ii_hinode = p;
2543 +       /* harmless error */
2544 +}
2545 +
2546 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
2547 +                        struct au_branch *br)
2548 +{
2549 +       aufs_bindex_t bend;
2550 +       struct au_sbinfo *sbinfo;
2551 +       struct dentry *root, *h_root;
2552 +       struct inode *inode, *h_inode;
2553 +       struct au_hinode *hinode;
2554 +
2555 +       SiMustWriteLock(sb);
2556 +
2557 +       root = sb->s_root;
2558 +       inode = root->d_inode;
2559 +       sbinfo = au_sbi(sb);
2560 +       bend = sbinfo->si_bend;
2561 +
2562 +       h_root = au_h_dptr(root, bindex);
2563 +       hinode = au_hi(inode, bindex);
2564 +       h_inode = au_igrab(hinode->hi_inode);
2565 +       au_hiput(hinode);
2566 +
2567 +       au_sbilist_lock();
2568 +       au_br_do_del_brp(sbinfo, bindex, bend);
2569 +       au_br_do_del_hdp(au_di(root), bindex, bend);
2570 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
2571 +       au_sbilist_unlock();
2572 +
2573 +       dput(h_root);
2574 +       iput(h_inode);
2575 +       au_br_do_free(br);
2576 +}
2577 +
2578 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
2579 +{
2580 +       int err, rerr, i;
2581 +       unsigned int mnt_flags;
2582 +       aufs_bindex_t bindex, bend, br_id;
2583 +       unsigned char do_wh, verbose;
2584 +       struct au_branch *br;
2585 +       struct au_wbr *wbr;
2586 +
2587 +       err = 0;
2588 +       bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
2589 +       if (bindex < 0) {
2590 +               if (remount)
2591 +                       goto out; /* success */
2592 +               err = -ENOENT;
2593 +               pr_err("%s no such branch\n", del->pathname);
2594 +               goto out;
2595 +       }
2596 +       AuDbg("bindex b%d\n", bindex);
2597 +
2598 +       err = -EBUSY;
2599 +       mnt_flags = au_mntflags(sb);
2600 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2601 +       bend = au_sbend(sb);
2602 +       if (unlikely(!bend)) {
2603 +               AuVerbose(verbose, "no more branches left\n");
2604 +               goto out;
2605 +       }
2606 +       br = au_sbr(sb, bindex);
2607 +       i = atomic_read(&br->br_count);
2608 +       if (unlikely(i)) {
2609 +               AuVerbose(verbose, "%d file(s) opened\n", i);
2610 +               goto out;
2611 +       }
2612 +
2613 +       wbr = br->br_wbr;
2614 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
2615 +       if (do_wh) {
2616 +               /* instead of WbrWhMustWriteLock(wbr) */
2617 +               SiMustWriteLock(sb);
2618 +               for (i = 0; i < AuBrWh_Last; i++) {
2619 +                       dput(wbr->wbr_wh[i]);
2620 +                       wbr->wbr_wh[i] = NULL;
2621 +               }
2622 +       }
2623 +
2624 +       err = test_children_busy(sb->s_root, bindex, verbose);
2625 +       if (unlikely(err)) {
2626 +               if (do_wh)
2627 +                       goto out_wh;
2628 +               goto out;
2629 +       }
2630 +
2631 +       err = 0;
2632 +       br_id = br->br_id;
2633 +       if (!remount)
2634 +               au_br_do_del(sb, bindex, br);
2635 +       else {
2636 +               sysaufs_brs_del(sb, bindex);
2637 +               au_br_do_del(sb, bindex, br);
2638 +               sysaufs_brs_add(sb, bindex);
2639 +       }
2640 +
2641 +       if (!bindex) {
2642 +               au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
2643 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
2644 +       } else
2645 +               au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
2646 +       if (au_opt_test(mnt_flags, PLINK))
2647 +               au_plink_half_refresh(sb, br_id);
2648 +
2649 +       if (au_xino_brid(sb) == br_id)
2650 +               au_xino_brid_set(sb, -1);
2651 +       goto out; /* success */
2652 +
2653 +out_wh:
2654 +       /* revert */
2655 +       rerr = au_br_init_wh(sb, br, br->br_perm, del->h_path.dentry);
2656 +       if (rerr)
2657 +               pr_warning("failed re-creating base whiteout, %s. (%d)\n",
2658 +                          del->pathname, rerr);
2659 +out:
2660 +       return err;
2661 +}
2662 +
2663 +/* ---------------------------------------------------------------------- */
2664 +
2665 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
2666 +{
2667 +       int err;
2668 +       aufs_bindex_t bstart, bend;
2669 +       struct aufs_ibusy ibusy;
2670 +       struct inode *inode, *h_inode;
2671 +
2672 +       err = -EPERM;
2673 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
2674 +               goto out;
2675 +
2676 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
2677 +       if (!err)
2678 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
2679 +       if (unlikely(err)) {
2680 +               err = -EFAULT;
2681 +               AuTraceErr(err);
2682 +               goto out;
2683 +       }
2684 +
2685 +       err = -EINVAL;
2686 +       si_read_lock(sb, AuLock_FLUSH);
2687 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
2688 +               goto out_unlock;
2689 +
2690 +       err = 0;
2691 +       ibusy.h_ino = 0; /* invalid */
2692 +       inode = ilookup(sb, ibusy.ino);
2693 +       if (!inode
2694 +           || inode->i_ino == AUFS_ROOT_INO
2695 +           || is_bad_inode(inode))
2696 +               goto out_unlock;
2697 +
2698 +       ii_read_lock_child(inode);
2699 +       bstart = au_ibstart(inode);
2700 +       bend = au_ibend(inode);
2701 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
2702 +               h_inode = au_h_iptr(inode, ibusy.bindex);
2703 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
2704 +                       ibusy.h_ino = h_inode->i_ino;
2705 +       }
2706 +       ii_read_unlock(inode);
2707 +       iput(inode);
2708 +
2709 +out_unlock:
2710 +       si_read_unlock(sb);
2711 +       if (!err) {
2712 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
2713 +               if (unlikely(err)) {
2714 +                       err = -EFAULT;
2715 +                       AuTraceErr(err);
2716 +               }
2717 +       }
2718 +out:
2719 +       return err;
2720 +}
2721 +
2722 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
2723 +{
2724 +       return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
2725 +}
2726 +
2727 +#ifdef CONFIG_COMPAT
2728 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
2729 +{
2730 +       return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
2731 +}
2732 +#endif
2733 +
2734 +/* ---------------------------------------------------------------------- */
2735 +
2736 +/*
2737 + * change a branch permission
2738 + */
2739 +
2740 +static void au_warn_ima(void)
2741 +{
2742 +#ifdef CONFIG_IMA
2743 +       /* since it doesn't support mark_files_ro() */
2744 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
2745 +#endif
2746 +}
2747 +
2748 +static int do_need_sigen_inc(int a, int b)
2749 +{
2750 +       return au_br_whable(a) && !au_br_whable(b);
2751 +}
2752 +
2753 +static int need_sigen_inc(int old, int new)
2754 +{
2755 +       return do_need_sigen_inc(old, new)
2756 +               || do_need_sigen_inc(new, old);
2757 +}
2758 +
2759 +static unsigned long long au_farray_cb(void *a,
2760 +                                      unsigned long long max __maybe_unused,
2761 +                                      void *arg)
2762 +{
2763 +       unsigned long long n;
2764 +       struct file **p, *f;
2765 +       struct super_block *sb = arg;
2766 +
2767 +       n = 0;
2768 +       p = a;
2769 +       lg_global_lock(files_lglock);
2770 +       do_file_list_for_each_entry(sb, f) {
2771 +               if (au_fi(f)
2772 +                   && file_count(f)
2773 +                   && !special_file(f->f_dentry->d_inode->i_mode)) {
2774 +                       get_file(f);
2775 +                       *p++ = f;
2776 +                       n++;
2777 +                       AuDebugOn(n > max);
2778 +               }
2779 +       } while_file_list_for_each_entry;
2780 +       lg_global_unlock(files_lglock);
2781 +
2782 +       return n;
2783 +}
2784 +
2785 +static struct file **au_farray_alloc(struct super_block *sb,
2786 +                                    unsigned long long *max)
2787 +{
2788 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
2789 +       return au_array_alloc(max, au_farray_cb, sb);
2790 +}
2791 +
2792 +static void au_farray_free(struct file **a, unsigned long long max)
2793 +{
2794 +       unsigned long long ull;
2795 +
2796 +       for (ull = 0; ull < max; ull++)
2797 +               if (a[ull])
2798 +                       fput(a[ull]);
2799 +       au_array_free(a);
2800 +}
2801 +
2802 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
2803 +{
2804 +       int err, do_warn;
2805 +       unsigned int mnt_flags;
2806 +       unsigned long long ull, max;
2807 +       aufs_bindex_t br_id;
2808 +       unsigned char verbose;
2809 +       struct file *file, *hf, **array;
2810 +       struct inode *inode;
2811 +       struct au_hfile *hfile;
2812 +
2813 +       mnt_flags = au_mntflags(sb);
2814 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2815 +
2816 +       array = au_farray_alloc(sb, &max);
2817 +       err = PTR_ERR(array);
2818 +       if (IS_ERR(array))
2819 +               goto out;
2820 +
2821 +       do_warn = 0;
2822 +       br_id = au_sbr_id(sb, bindex);
2823 +       for (ull = 0; ull < max; ull++) {
2824 +               file = array[ull];
2825 +
2826 +               /* AuDbg("%.*s\n", AuDLNPair(file->f_dentry)); */
2827 +               fi_read_lock(file);
2828 +               if (unlikely(au_test_mmapped(file))) {
2829 +                       err = -EBUSY;
2830 +                       AuVerbose(verbose, "mmapped %.*s\n",
2831 +                                 AuDLNPair(file->f_dentry));
2832 +                       AuDbgFile(file);
2833 +                       FiMustNoWaiters(file);
2834 +                       fi_read_unlock(file);
2835 +                       goto out_array;
2836 +               }
2837 +
2838 +               inode = file->f_dentry->d_inode;
2839 +               hfile = &au_fi(file)->fi_htop;
2840 +               hf = hfile->hf_file;
2841 +               if (!S_ISREG(inode->i_mode)
2842 +                   || !(file->f_mode & FMODE_WRITE)
2843 +                   || hfile->hf_br->br_id != br_id
2844 +                   || !(hf->f_mode & FMODE_WRITE))
2845 +                       array[ull] = NULL;
2846 +               else {
2847 +                       do_warn = 1;
2848 +                       get_file(file);
2849 +               }
2850 +
2851 +               FiMustNoWaiters(file);
2852 +               fi_read_unlock(file);
2853 +               fput(file);
2854 +       }
2855 +
2856 +       err = 0;
2857 +       if (do_warn)
2858 +               au_warn_ima();
2859 +
2860 +       for (ull = 0; ull < max; ull++) {
2861 +               file = array[ull];
2862 +               if (!file)
2863 +                       continue;
2864 +
2865 +               /* todo: already flushed? */
2866 +               /* cf. fs/super.c:mark_files_ro() */
2867 +               /* fi_read_lock(file); */
2868 +               hfile = &au_fi(file)->fi_htop;
2869 +               hf = hfile->hf_file;
2870 +               /* fi_read_unlock(file); */
2871 +               spin_lock(&hf->f_lock);
2872 +               hf->f_mode &= ~FMODE_WRITE;
2873 +               spin_unlock(&hf->f_lock);
2874 +               if (!file_check_writeable(hf)) {
2875 +                       file_release_write(hf);
2876 +                       mnt_drop_write(hf->f_vfsmnt);
2877 +               }
2878 +       }
2879 +
2880 +out_array:
2881 +       au_farray_free(array, max);
2882 +out:
2883 +       AuTraceErr(err);
2884 +       return err;
2885 +}
2886 +
2887 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
2888 +             int *do_refresh)
2889 +{
2890 +       int err, rerr;
2891 +       aufs_bindex_t bindex;
2892 +       struct path path;
2893 +       struct dentry *root;
2894 +       struct au_branch *br;
2895 +
2896 +       root = sb->s_root;
2897 +       bindex = au_find_dbindex(root, mod->h_root);
2898 +       if (bindex < 0) {
2899 +               if (remount)
2900 +                       return 0; /* success */
2901 +               err = -ENOENT;
2902 +               pr_err("%s no such branch\n", mod->path);
2903 +               goto out;
2904 +       }
2905 +       AuDbg("bindex b%d\n", bindex);
2906 +
2907 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
2908 +       if (unlikely(err))
2909 +               goto out;
2910 +
2911 +       br = au_sbr(sb, bindex);
2912 +       if (br->br_perm == mod->perm)
2913 +               return 0; /* success */
2914 +
2915 +       if (au_br_writable(br->br_perm)) {
2916 +               /* remove whiteout base */
2917 +               err = au_br_init_wh(sb, br, mod->perm, mod->h_root);
2918 +               if (unlikely(err))
2919 +                       goto out;
2920 +
2921 +               if (!au_br_writable(mod->perm)) {
2922 +                       /* rw --> ro, file might be mmapped */
2923 +                       DiMustNoWaiters(root);
2924 +                       IiMustNoWaiters(root->d_inode);
2925 +                       di_write_unlock(root);
2926 +                       err = au_br_mod_files_ro(sb, bindex);
2927 +                       /* aufs_write_lock() calls ..._child() */
2928 +                       di_write_lock_child(root);
2929 +
2930 +                       if (unlikely(err)) {
2931 +                               rerr = -ENOMEM;
2932 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
2933 +                                                    GFP_NOFS);
2934 +                               if (br->br_wbr) {
2935 +                                       path.mnt = br->br_mnt;
2936 +                                       path.dentry = mod->h_root;
2937 +                                       rerr = au_wbr_init(br, sb, br->br_perm,
2938 +                                                          &path);
2939 +                               }
2940 +                               if (unlikely(rerr)) {
2941 +                                       AuIOErr("nested error %d (%d)\n",
2942 +                                               rerr, err);
2943 +                                       br->br_perm = mod->perm;
2944 +                               }
2945 +                       }
2946 +               }
2947 +       } else if (au_br_writable(mod->perm)) {
2948 +               /* ro --> rw */
2949 +               err = -ENOMEM;
2950 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
2951 +               if (br->br_wbr) {
2952 +                       path.mnt = br->br_mnt;
2953 +                       path.dentry = mod->h_root;
2954 +                       err = au_wbr_init(br, sb, mod->perm, &path);
2955 +                       if (unlikely(err)) {
2956 +                               kfree(br->br_wbr);
2957 +                               br->br_wbr = NULL;
2958 +                       }
2959 +               }
2960 +       }
2961 +
2962 +       if (!err) {
2963 +               *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
2964 +               br->br_perm = mod->perm;
2965 +       }
2966 +
2967 +out:
2968 +       AuTraceErr(err);
2969 +       return err;
2970 +}
2971 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
2972 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
2973 +++ linux/fs/aufs/branch.h      2011-10-24 20:51:51.580466925 +0200
2974 @@ -0,0 +1,232 @@
2975 +/*
2976 + * Copyright (C) 2005-2011 Junjiro R. Okajima
2977 + *
2978 + * This program, aufs is free software; you can redistribute it and/or modify
2979 + * it under the terms of the GNU General Public License as published by
2980 + * the Free Software Foundation; either version 2 of the License, or
2981 + * (at your option) any later version.
2982 + *
2983 + * This program is distributed in the hope that it will be useful,
2984 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2985 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2986 + * GNU General Public License for more details.
2987 + *
2988 + * You should have received a copy of the GNU General Public License
2989 + * along with this program; if not, write to the Free Software
2990 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2991 + */
2992 +
2993 +/*
2994 + * branch filesystems and xino for them
2995 + */
2996 +
2997 +#ifndef __AUFS_BRANCH_H__
2998 +#define __AUFS_BRANCH_H__
2999 +
3000 +#ifdef __KERNEL__
3001 +
3002 +#include <linux/fs.h>
3003 +#include <linux/mount.h>
3004 +#include <linux/aufs_type.h>
3005 +#include "dynop.h"
3006 +#include "rwsem.h"
3007 +#include "super.h"
3008 +
3009 +/* ---------------------------------------------------------------------- */
3010 +
3011 +/* a xino file */
3012 +struct au_xino_file {
3013 +       struct file             *xi_file;
3014 +       struct mutex            xi_nondir_mtx;
3015 +
3016 +       /* todo: make xino files an array to support huge inode number */
3017 +
3018 +#ifdef CONFIG_DEBUG_FS
3019 +       struct dentry            *xi_dbgaufs;
3020 +#endif
3021 +};
3022 +
3023 +/* members for writable branch only */
3024 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
3025 +struct au_wbr {
3026 +       struct au_rwsem         wbr_wh_rwsem;
3027 +       struct dentry           *wbr_wh[AuBrWh_Last];
3028 +       atomic_t                wbr_wh_running;
3029 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
3030 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
3031 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
3032 +
3033 +       /* mfs mode */
3034 +       unsigned long long      wbr_bytes;
3035 +};
3036 +
3037 +/* ext2 has 3 types of operations at least, ext3 has 4 */
3038 +#define AuBrDynOp (AuDyLast * 4)
3039 +
3040 +/* protected by superblock rwsem */
3041 +struct au_branch {
3042 +       struct au_xino_file     br_xino;
3043 +
3044 +       aufs_bindex_t           br_id;
3045 +
3046 +       int                     br_perm;
3047 +       struct vfsmount         *br_mnt;
3048 +       spinlock_t              br_dykey_lock;
3049 +       struct au_dykey         *br_dykey[AuBrDynOp];
3050 +       atomic_t                br_count;
3051 +
3052 +       struct au_wbr           *br_wbr;
3053 +
3054 +       /* xino truncation */
3055 +       blkcnt_t                br_xino_upper;  /* watermark in blocks */
3056 +       atomic_t                br_xino_running;
3057 +
3058 +#ifdef CONFIG_AUFS_HFSNOTIFY
3059 +       struct fsnotify_group   *br_hfsn_group;
3060 +       struct fsnotify_ops     br_hfsn_ops;
3061 +#endif
3062 +
3063 +#ifdef CONFIG_SYSFS
3064 +       /* an entry under sysfs per mount-point */
3065 +       char                    br_name[8];
3066 +       struct attribute        br_attr;
3067 +#endif
3068 +};
3069 +
3070 +/* ---------------------------------------------------------------------- */
3071 +
3072 +/* branch permissions and attributes */
3073 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
3074 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
3075 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
3076 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
3077 +
3078 +#define AuBrRAttr_WH           (1 << 3)        /* whiteout-able */
3079 +
3080 +#define AuBrWAttr_NoLinkWH     (1 << 4)        /* un-hardlinkable whiteouts */
3081 +
3082 +static inline int au_br_writable(int brperm)
3083 +{
3084 +       return brperm & AuBrPerm_RW;
3085 +}
3086 +
3087 +static inline int au_br_whable(int brperm)
3088 +{
3089 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
3090 +}
3091 +
3092 +static inline int au_br_wh_linkable(int brperm)
3093 +{
3094 +       return !(brperm & AuBrWAttr_NoLinkWH);
3095 +}
3096 +
3097 +static inline int au_br_rdonly(struct au_branch *br)
3098 +{
3099 +       return ((br->br_mnt->mnt_sb->s_flags & MS_RDONLY)
3100 +               || !au_br_writable(br->br_perm))
3101 +               ? -EROFS : 0;
3102 +}
3103 +
3104 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
3105 +{
3106 +#ifdef CONFIG_AUFS_HNOTIFY
3107 +       return !(brperm & AuBrPerm_RR);
3108 +#else
3109 +       return 0;
3110 +#endif
3111 +}
3112 +
3113 +/* ---------------------------------------------------------------------- */
3114 +
3115 +/* branch.c */
3116 +struct au_sbinfo;
3117 +void au_br_free(struct au_sbinfo *sinfo);
3118 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
3119 +struct au_opt_add;
3120 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
3121 +struct au_opt_del;
3122 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
3123 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
3124 +#ifdef CONFIG_COMPAT
3125 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
3126 +#endif
3127 +struct au_opt_mod;
3128 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3129 +             int *do_refresh);
3130 +
3131 +/* xino.c */
3132 +static const loff_t au_loff_max = LLONG_MAX;
3133 +
3134 +int au_xib_trunc(struct super_block *sb);
3135 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
3136 +                  loff_t *pos);
3137 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
3138 +                   loff_t *pos);
3139 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
3140 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
3141 +ino_t au_xino_new_ino(struct super_block *sb);
3142 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
3143 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3144 +                 ino_t ino);
3145 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3146 +                ino_t *ino);
3147 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
3148 +              struct file *base_file, int do_test);
3149 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
3150 +
3151 +struct au_opt_xino;
3152 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
3153 +void au_xino_clr(struct super_block *sb);
3154 +struct file *au_xino_def(struct super_block *sb);
3155 +int au_xino_path(struct seq_file *seq, struct file *file);
3156 +
3157 +/* ---------------------------------------------------------------------- */
3158 +
3159 +/* Superblock to branch */
3160 +static inline
3161 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
3162 +{
3163 +       return au_sbr(sb, bindex)->br_id;
3164 +}
3165 +
3166 +static inline
3167 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
3168 +{
3169 +       return au_sbr(sb, bindex)->br_mnt;
3170 +}
3171 +
3172 +static inline
3173 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
3174 +{
3175 +       return au_sbr_mnt(sb, bindex)->mnt_sb;
3176 +}
3177 +
3178 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
3179 +{
3180 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
3181 +}
3182 +
3183 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
3184 +{
3185 +       return au_sbr(sb, bindex)->br_perm;
3186 +}
3187 +
3188 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
3189 +{
3190 +       return au_br_whable(au_sbr_perm(sb, bindex));
3191 +}
3192 +
3193 +/* ---------------------------------------------------------------------- */
3194 +
3195 +/*
3196 + * wbr_wh_read_lock, wbr_wh_write_lock
3197 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
3198 + */
3199 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
3200 +
3201 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
3202 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
3203 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
3204 +
3205 +#endif /* __KERNEL__ */
3206 +#endif /* __AUFS_BRANCH_H__ */
3207 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
3208 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
3209 +++ linux/fs/aufs/conf.mk       2011-08-24 13:30:24.731313534 +0200
3210 @@ -0,0 +1,38 @@
3211 +
3212 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
3213 +
3214 +define AuConf
3215 +ifdef ${1}
3216 +AuConfStr += ${1}=${${1}}
3217 +endif
3218 +endef
3219 +
3220 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
3221 +       SBILIST \
3222 +       HNOTIFY HFSNOTIFY \
3223 +       EXPORT INO_T_64 \
3224 +       RDU \
3225 +       PROC_MAP \
3226 +       SP_IATTR \
3227 +       SHWH \
3228 +       BR_RAMFS \
3229 +       BR_FUSE POLL \
3230 +       BR_HFSPLUS \
3231 +       BDEV_LOOP \
3232 +       DEBUG MAGIC_SYSRQ
3233 +$(foreach i, ${AuConfAll}, \
3234 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
3235 +
3236 +AuConfName = ${obj}/conf.str
3237 +${AuConfName}.tmp: FORCE
3238 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
3239 +${AuConfName}: ${AuConfName}.tmp
3240 +       @diff -q $< $@ > /dev/null 2>&1 || { \
3241 +       echo '  GEN    ' $@; \
3242 +       cp -p $< $@; \
3243 +       }
3244 +FORCE:
3245 +clean-files += ${AuConfName} ${AuConfName}.tmp
3246 +${obj}/sysfs.o: ${AuConfName}
3247 +
3248 +-include ${srctree}/${src}/conf_priv.mk
3249 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
3250 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
3251 +++ linux/fs/aufs/cpup.c        2012-01-05 11:00:59.215501779 +0100
3252 @@ -0,0 +1,1081 @@
3253 +/*
3254 + * Copyright (C) 2005-2011 Junjiro R. Okajima
3255 + *
3256 + * This program, aufs is free software; you can redistribute it and/or modify
3257 + * it under the terms of the GNU General Public License as published by
3258 + * the Free Software Foundation; either version 2 of the License, or
3259 + * (at your option) any later version.
3260 + *
3261 + * This program is distributed in the hope that it will be useful,
3262 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3263 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3264 + * GNU General Public License for more details.
3265 + *
3266 + * You should have received a copy of the GNU General Public License
3267 + * along with this program; if not, write to the Free Software
3268 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3269 + */
3270 +
3271 +/*
3272 + * copy-up functions, see wbr_policy.c for copy-down
3273 + */
3274 +
3275 +#include <linux/file.h>
3276 +#include <linux/fs_stack.h>
3277 +#include <linux/mm.h>
3278 +#include <linux/uaccess.h>
3279 +#include "aufs.h"
3280 +
3281 +void au_cpup_attr_flags(struct inode *dst, struct inode *src)
3282 +{
3283 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
3284 +               | S_NOATIME | S_NOCMTIME;
3285 +
3286 +       dst->i_flags |= src->i_flags & ~mask;
3287 +       if (au_test_fs_notime(dst->i_sb))
3288 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
3289 +}
3290 +
3291 +void au_cpup_attr_timesizes(struct inode *inode)
3292 +{
3293 +       struct inode *h_inode;
3294 +
3295 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3296 +       fsstack_copy_attr_times(inode, h_inode);
3297 +       fsstack_copy_inode_size(inode, h_inode);
3298 +}
3299 +
3300 +void au_cpup_attr_nlink(struct inode *inode, int force)
3301 +{
3302 +       struct inode *h_inode;
3303 +       struct super_block *sb;
3304 +       aufs_bindex_t bindex, bend;
3305 +
3306 +       sb = inode->i_sb;
3307 +       bindex = au_ibstart(inode);
3308 +       h_inode = au_h_iptr(inode, bindex);
3309 +       if (!force
3310 +           && !S_ISDIR(h_inode->i_mode)
3311 +           && au_opt_test(au_mntflags(sb), PLINK)
3312 +           && au_plink_test(inode))
3313 +               return;
3314 +
3315 +       set_nlink(inode, h_inode->i_nlink);
3316 +
3317 +       /*
3318 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
3319 +        * it may includes whplink directory.
3320 +        */
3321 +       if (S_ISDIR(h_inode->i_mode)) {
3322 +               bend = au_ibend(inode);
3323 +               for (bindex++; bindex <= bend; bindex++) {
3324 +                       h_inode = au_h_iptr(inode, bindex);
3325 +                       if (h_inode)
3326 +                               au_add_nlink(inode, h_inode);
3327 +               }
3328 +       }
3329 +}
3330 +
3331 +void au_cpup_attr_changeable(struct inode *inode)
3332 +{
3333 +       struct inode *h_inode;
3334 +
3335 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3336 +       inode->i_mode = h_inode->i_mode;
3337 +       inode->i_uid = h_inode->i_uid;
3338 +       inode->i_gid = h_inode->i_gid;
3339 +       au_cpup_attr_timesizes(inode);
3340 +       au_cpup_attr_flags(inode, h_inode);
3341 +}
3342 +
3343 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
3344 +{
3345 +       struct au_iinfo *iinfo = au_ii(inode);
3346 +
3347 +       IiMustWriteLock(inode);
3348 +
3349 +       iinfo->ii_higen = h_inode->i_generation;
3350 +       iinfo->ii_hsb1 = h_inode->i_sb;
3351 +}
3352 +
3353 +void au_cpup_attr_all(struct inode *inode, int force)
3354 +{
3355 +       struct inode *h_inode;
3356 +
3357 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3358 +       au_cpup_attr_changeable(inode);
3359 +       if (inode->i_nlink > 0)
3360 +               au_cpup_attr_nlink(inode, force);
3361 +       inode->i_rdev = h_inode->i_rdev;
3362 +       inode->i_blkbits = h_inode->i_blkbits;
3363 +       au_cpup_igen(inode, h_inode);
3364 +}
3365 +
3366 +/* ---------------------------------------------------------------------- */
3367 +
3368 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
3369 +
3370 +/* keep the timestamps of the parent dir when cpup */
3371 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
3372 +                   struct path *h_path)
3373 +{
3374 +       struct inode *h_inode;
3375 +
3376 +       dt->dt_dentry = dentry;
3377 +       dt->dt_h_path = *h_path;
3378 +       h_inode = h_path->dentry->d_inode;
3379 +       dt->dt_atime = h_inode->i_atime;
3380 +       dt->dt_mtime = h_inode->i_mtime;
3381 +       /* smp_mb(); */
3382 +}
3383 +
3384 +void au_dtime_revert(struct au_dtime *dt)
3385 +{
3386 +       struct iattr attr;
3387 +       int err;
3388 +
3389 +       attr.ia_atime = dt->dt_atime;
3390 +       attr.ia_mtime = dt->dt_mtime;
3391 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
3392 +               | ATTR_ATIME | ATTR_ATIME_SET;
3393 +
3394 +       err = vfsub_notify_change(&dt->dt_h_path, &attr);
3395 +       if (unlikely(err))
3396 +               pr_warning("restoring timestamps failed(%d). ignored\n", err);
3397 +}
3398 +
3399 +/* ---------------------------------------------------------------------- */
3400 +
3401 +static noinline_for_stack
3402 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src)
3403 +{
3404 +       int err, sbits;
3405 +       struct iattr ia;
3406 +       struct path h_path;
3407 +       struct inode *h_isrc, *h_idst;
3408 +
3409 +       h_path.dentry = au_h_dptr(dst, bindex);
3410 +       h_idst = h_path.dentry->d_inode;
3411 +       h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
3412 +       h_isrc = h_src->d_inode;
3413 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
3414 +               | ATTR_ATIME | ATTR_MTIME
3415 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
3416 +       ia.ia_uid = h_isrc->i_uid;
3417 +       ia.ia_gid = h_isrc->i_gid;
3418 +       ia.ia_atime = h_isrc->i_atime;
3419 +       ia.ia_mtime = h_isrc->i_mtime;
3420 +       if (h_idst->i_mode != h_isrc->i_mode
3421 +           && !S_ISLNK(h_idst->i_mode)) {
3422 +               ia.ia_valid |= ATTR_MODE;
3423 +               ia.ia_mode = h_isrc->i_mode;
3424 +       }
3425 +       sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
3426 +       au_cpup_attr_flags(h_idst, h_isrc);
3427 +       err = vfsub_notify_change(&h_path, &ia);
3428 +
3429 +       /* is this nfs only? */
3430 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
3431 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
3432 +               ia.ia_mode = h_isrc->i_mode;
3433 +               err = vfsub_notify_change(&h_path, &ia);
3434 +       }
3435 +
3436 +       return err;
3437 +}
3438 +
3439 +/* ---------------------------------------------------------------------- */
3440 +
3441 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
3442 +                          char *buf, unsigned long blksize)
3443 +{
3444 +       int err;
3445 +       size_t sz, rbytes, wbytes;
3446 +       unsigned char all_zero;
3447 +       char *p, *zp;
3448 +       struct mutex *h_mtx;
3449 +       /* reduce stack usage */
3450 +       struct iattr *ia;
3451 +
3452 +       zp = page_address(ZERO_PAGE(0));
3453 +       if (unlikely(!zp))
3454 +               return -ENOMEM; /* possible? */
3455 +
3456 +       err = 0;
3457 +       all_zero = 0;
3458 +       while (len) {
3459 +               AuDbg("len %lld\n", len);
3460 +               sz = blksize;
3461 +               if (len < blksize)
3462 +                       sz = len;
3463 +
3464 +               rbytes = 0;
3465 +               /* todo: signal_pending? */
3466 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
3467 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
3468 +                       err = rbytes;
3469 +               }
3470 +               if (unlikely(err < 0))
3471 +                       break;
3472 +
3473 +               all_zero = 0;
3474 +               if (len >= rbytes && rbytes == blksize)
3475 +                       all_zero = !memcmp(buf, zp, rbytes);
3476 +               if (!all_zero) {
3477 +                       wbytes = rbytes;
3478 +                       p = buf;
3479 +                       while (wbytes) {
3480 +                               size_t b;
3481 +
3482 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
3483 +                               err = b;
3484 +                               /* todo: signal_pending? */
3485 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
3486 +                                       continue;
3487 +                               if (unlikely(err < 0))
3488 +                                       break;
3489 +                               wbytes -= b;
3490 +                               p += b;
3491 +                       }
3492 +               } else {
3493 +                       loff_t res;
3494 +
3495 +                       AuLabel(hole);
3496 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
3497 +                       err = res;
3498 +                       if (unlikely(res < 0))
3499 +                               break;
3500 +               }
3501 +               len -= rbytes;
3502 +               err = 0;
3503 +       }
3504 +
3505 +       /* the last block may be a hole */
3506 +       if (!err && all_zero) {
3507 +               AuLabel(last hole);
3508 +
3509 +               err = 1;
3510 +               if (au_test_nfs(dst->f_dentry->d_sb)) {
3511 +                       /* nfs requires this step to make last hole */
3512 +                       /* is this only nfs? */
3513 +                       do {
3514 +                               /* todo: signal_pending? */
3515 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
3516 +                       } while (err == -EAGAIN || err == -EINTR);
3517 +                       if (err == 1)
3518 +                               dst->f_pos--;
3519 +               }
3520 +
3521 +               if (err == 1) {
3522 +                       ia = (void *)buf;
3523 +                       ia->ia_size = dst->f_pos;
3524 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
3525 +                       ia->ia_file = dst;
3526 +                       h_mtx = &dst->f_dentry->d_inode->i_mutex;
3527 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
3528 +                       err = vfsub_notify_change(&dst->f_path, ia);
3529 +                       mutex_unlock(h_mtx);
3530 +               }
3531 +       }
3532 +
3533 +       return err;
3534 +}
3535 +
3536 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
3537 +{
3538 +       int err;
3539 +       unsigned long blksize;
3540 +       unsigned char do_kfree;
3541 +       char *buf;
3542 +
3543 +       err = -ENOMEM;
3544 +       blksize = dst->f_dentry->d_sb->s_blocksize;
3545 +       if (!blksize || PAGE_SIZE < blksize)
3546 +               blksize = PAGE_SIZE;
3547 +       AuDbg("blksize %lu\n", blksize);
3548 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
3549 +       if (do_kfree)
3550 +               buf = kmalloc(blksize, GFP_NOFS);
3551 +       else
3552 +               buf = (void *)__get_free_page(GFP_NOFS);
3553 +       if (unlikely(!buf))
3554 +               goto out;
3555 +
3556 +       if (len > (1 << 22))
3557 +               AuDbg("copying a large file %lld\n", (long long)len);
3558 +
3559 +       src->f_pos = 0;
3560 +       dst->f_pos = 0;
3561 +       err = au_do_copy_file(dst, src, len, buf, blksize);
3562 +       if (do_kfree)
3563 +               kfree(buf);
3564 +       else
3565 +               free_page((unsigned long)buf);
3566 +
3567 +out:
3568 +       return err;
3569 +}
3570 +
3571 +/*
3572 + * to support a sparse file which is opened with O_APPEND,
3573 + * we need to close the file.
3574 + */
3575 +static int au_cp_regular(struct dentry *dentry, aufs_bindex_t bdst,
3576 +                        aufs_bindex_t bsrc, loff_t len)
3577 +{
3578 +       int err, i;
3579 +       enum { SRC, DST };
3580 +       struct {
3581 +               aufs_bindex_t bindex;
3582 +               unsigned int flags;
3583 +               struct dentry *dentry;
3584 +               struct file *file;
3585 +               void *label, *label_file;
3586 +       } *f, file[] = {
3587 +               {
3588 +                       .bindex = bsrc,
3589 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
3590 +                       .file = NULL,
3591 +                       .label = &&out,
3592 +                       .label_file = &&out_src
3593 +               },
3594 +               {
3595 +                       .bindex = bdst,
3596 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
3597 +                       .file = NULL,
3598 +                       .label = &&out_src,
3599 +                       .label_file = &&out_dst
3600 +               }
3601 +       };
3602 +       struct super_block *sb;
3603 +
3604 +       /* bsrc branch can be ro/rw. */
3605 +       sb = dentry->d_sb;
3606 +       f = file;
3607 +       for (i = 0; i < 2; i++, f++) {
3608 +               f->dentry = au_h_dptr(dentry, f->bindex);
3609 +               f->file = au_h_open(dentry, f->bindex, f->flags, /*file*/NULL);
3610 +               err = PTR_ERR(f->file);
3611 +               if (IS_ERR(f->file))
3612 +                       goto *f->label;
3613 +               err = -EINVAL;
3614 +               if (unlikely(!f->file->f_op))
3615 +                       goto *f->label_file;
3616 +       }
3617 +
3618 +       /* try stopping to update while we copyup */
3619 +       IMustLock(file[SRC].dentry->d_inode);
3620 +       err = au_copy_file(file[DST].file, file[SRC].file, len);
3621 +
3622 +out_dst:
3623 +       fput(file[DST].file);
3624 +       au_sbr_put(sb, file[DST].bindex);
3625 +out_src:
3626 +       fput(file[SRC].file);
3627 +       au_sbr_put(sb, file[SRC].bindex);
3628 +out:
3629 +       return err;
3630 +}
3631 +
3632 +static int au_do_cpup_regular(struct dentry *dentry, aufs_bindex_t bdst,
3633 +                             aufs_bindex_t bsrc, loff_t len,
3634 +                             struct inode *h_dir, struct path *h_path)
3635 +{
3636 +       int err, rerr;
3637 +       loff_t l;
3638 +
3639 +       err = 0;
3640 +       l = i_size_read(au_h_iptr(dentry->d_inode, bsrc));
3641 +       if (len == -1 || l < len)
3642 +               len = l;
3643 +       if (len)
3644 +               err = au_cp_regular(dentry, bdst, bsrc, len);
3645 +       if (!err)
3646 +               goto out; /* success */
3647 +
3648 +       rerr = vfsub_unlink(h_dir, h_path, /*force*/0);
3649 +       if (rerr) {
3650 +               AuIOErr("failed unlinking cpup-ed %.*s(%d, %d)\n",
3651 +                       AuDLNPair(h_path->dentry), err, rerr);
3652 +               err = -EIO;
3653 +       }
3654 +
3655 +out:
3656 +       return err;
3657 +}
3658 +
3659 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
3660 +                             struct inode *h_dir)
3661 +{
3662 +       int err, symlen;
3663 +       mm_segment_t old_fs;
3664 +       union {
3665 +               char *k;
3666 +               char __user *u;
3667 +       } sym;
3668 +
3669 +       err = -ENOSYS;
3670 +       if (unlikely(!h_src->d_inode->i_op->readlink))
3671 +               goto out;
3672 +
3673 +       err = -ENOMEM;
3674 +       sym.k = __getname_gfp(GFP_NOFS);
3675 +       if (unlikely(!sym.k))
3676 +               goto out;
3677 +
3678 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
3679 +       old_fs = get_fs();
3680 +       set_fs(KERNEL_DS);
3681 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
3682 +       err = symlen;
3683 +       set_fs(old_fs);
3684 +
3685 +       if (symlen > 0) {
3686 +               sym.k[symlen] = 0;
3687 +               err = vfsub_symlink(h_dir, h_path, sym.k);
3688 +       }
3689 +       __putname(sym.k);
3690 +
3691 +out:
3692 +       return err;
3693 +}
3694 +
3695 +/* return with the lower dst inode is locked */
3696 +static noinline_for_stack
3697 +int cpup_entry(struct dentry *dentry, aufs_bindex_t bdst,
3698 +              aufs_bindex_t bsrc, loff_t len, unsigned int flags,
3699 +              struct dentry *dst_parent)
3700 +{
3701 +       int err;
3702 +       umode_t mode;
3703 +       unsigned int mnt_flags;
3704 +       unsigned char isdir;
3705 +       const unsigned char do_dt = !!au_ftest_cpup(flags, DTIME);
3706 +       struct au_dtime dt;
3707 +       struct path h_path;
3708 +       struct dentry *h_src, *h_dst, *h_parent;
3709 +       struct inode *h_inode, *h_dir;
3710 +       struct super_block *sb;
3711 +
3712 +       /* bsrc branch can be ro/rw. */
3713 +       h_src = au_h_dptr(dentry, bsrc);
3714 +       h_inode = h_src->d_inode;
3715 +       AuDebugOn(h_inode != au_h_iptr(dentry->d_inode, bsrc));
3716 +
3717 +       /* try stopping to be referenced while we are creating */
3718 +       h_dst = au_h_dptr(dentry, bdst);
3719 +       h_parent = h_dst->d_parent; /* dir inode is locked */
3720 +       h_dir = h_parent->d_inode;
3721 +       IMustLock(h_dir);
3722 +       AuDebugOn(h_parent != h_dst->d_parent);
3723 +
3724 +       sb = dentry->d_sb;
3725 +       h_path.mnt = au_sbr_mnt(sb, bdst);
3726 +       if (do_dt) {
3727 +               h_path.dentry = h_parent;
3728 +               au_dtime_store(&dt, dst_parent, &h_path);
3729 +       }
3730 +       h_path.dentry = h_dst;
3731 +
3732 +       isdir = 0;
3733 +       mode = h_inode->i_mode;
3734 +       switch (mode & S_IFMT) {
3735 +       case S_IFREG:
3736 +               /* try stopping to update while we are referencing */
3737 +               IMustLock(h_inode);
3738 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
3739 +               if (!err)
3740 +                       err = au_do_cpup_regular
3741 +                               (dentry, bdst, bsrc, len,
3742 +                                au_h_iptr(dst_parent->d_inode, bdst), &h_path);
3743 +               break;
3744 +       case S_IFDIR:
3745 +               isdir = 1;
3746 +               err = vfsub_mkdir(h_dir, &h_path, mode);
3747 +               if (!err) {
3748 +                       /*
3749 +                        * strange behaviour from the users view,
3750 +                        * particularry setattr case
3751 +                        */
3752 +                       if (au_ibstart(dst_parent->d_inode) == bdst)
3753 +                               au_cpup_attr_nlink(dst_parent->d_inode,
3754 +                                                  /*force*/1);
3755 +                       au_cpup_attr_nlink(dentry->d_inode, /*force*/1);
3756 +               }
3757 +               break;
3758 +       case S_IFLNK:
3759 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
3760 +               break;
3761 +       case S_IFCHR:
3762 +       case S_IFBLK:
3763 +               AuDebugOn(!capable(CAP_MKNOD));
3764 +               /*FALLTHROUGH*/
3765 +       case S_IFIFO:
3766 +       case S_IFSOCK:
3767 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
3768 +               break;
3769 +       default:
3770 +               AuIOErr("Unknown inode type 0%o\n", mode);
3771 +               err = -EIO;
3772 +       }
3773 +
3774 +       mnt_flags = au_mntflags(sb);
3775 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
3776 +           && !isdir
3777 +           && au_opt_test(mnt_flags, XINO)
3778 +           && h_inode->i_nlink == 1
3779 +           /* todo: unnecessary? */
3780 +           /* && dentry->d_inode->i_nlink == 1 */
3781 +           && bdst < bsrc
3782 +           && !au_ftest_cpup(flags, KEEPLINO))
3783 +               au_xino_write(sb, bsrc, h_inode->i_ino, /*ino*/0);
3784 +               /* ignore this error */
3785 +
3786 +       if (do_dt)
3787 +               au_dtime_revert(&dt);
3788 +       return err;
3789 +}
3790 +
3791 +/*
3792 + * copyup the @dentry from @bsrc to @bdst.
3793 + * the caller must set the both of lower dentries.
3794 + * @len is for truncating when it is -1 copyup the entire file.
3795 + * in link/rename cases, @dst_parent may be different from the real one.
3796 + */
3797 +static int au_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
3798 +                         aufs_bindex_t bsrc, loff_t len, unsigned int flags,
3799 +                         struct dentry *dst_parent)
3800 +{
3801 +       int err, rerr;
3802 +       aufs_bindex_t old_ibstart;
3803 +       unsigned char isdir, plink;
3804 +       struct au_dtime dt;
3805 +       struct path h_path;
3806 +       struct dentry *h_src, *h_dst, *h_parent;
3807 +       struct inode *dst_inode, *h_dir, *inode;
3808 +       struct super_block *sb;
3809 +
3810 +       AuDebugOn(bsrc <= bdst);
3811 +
3812 +       sb = dentry->d_sb;
3813 +       h_path.mnt = au_sbr_mnt(sb, bdst);
3814 +       h_dst = au_h_dptr(dentry, bdst);
3815 +       h_parent = h_dst->d_parent; /* dir inode is locked */
3816 +       h_dir = h_parent->d_inode;
3817 +       IMustLock(h_dir);
3818 +
3819 +       h_src = au_h_dptr(dentry, bsrc);
3820 +       inode = dentry->d_inode;
3821 +
3822 +       if (!dst_parent)
3823 +               dst_parent = dget_parent(dentry);
3824 +       else
3825 +               dget(dst_parent);
3826 +
3827 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
3828 +       dst_inode = au_h_iptr(inode, bdst);
3829 +       if (dst_inode) {
3830 +               if (unlikely(!plink)) {
3831 +                       err = -EIO;
3832 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
3833 +                               "but plink is disabled\n",
3834 +                               dst_inode->i_ino, inode->i_ino, bdst);
3835 +                       goto out;
3836 +               }
3837 +
3838 +               if (dst_inode->i_nlink) {
3839 +                       const int do_dt = au_ftest_cpup(flags, DTIME);
3840 +
3841 +                       h_src = au_plink_lkup(inode, bdst);
3842 +                       err = PTR_ERR(h_src);
3843 +                       if (IS_ERR(h_src))
3844 +                               goto out;
3845 +                       if (unlikely(!h_src->d_inode)) {
3846 +                               err = -EIO;
3847 +                               AuIOErr("i%lu exists on a upper branch "
3848 +                                       "but not pseudo-linked\n",
3849 +                                       inode->i_ino);
3850 +                               dput(h_src);
3851 +                               goto out;
3852 +                       }
3853 +
3854 +                       if (do_dt) {
3855 +                               h_path.dentry = h_parent;
3856 +                               au_dtime_store(&dt, dst_parent, &h_path);
3857 +                       }
3858 +                       h_path.dentry = h_dst;
3859 +                       err = vfsub_link(h_src, h_dir, &h_path);
3860 +                       if (do_dt)
3861 +                               au_dtime_revert(&dt);
3862 +                       dput(h_src);
3863 +                       goto out;
3864 +               } else
3865 +                       /* todo: cpup_wh_file? */
3866 +                       /* udba work */
3867 +                       au_update_ibrange(inode, /*do_put_zero*/1);
3868 +       }
3869 +
3870 +       old_ibstart = au_ibstart(inode);
3871 +       err = cpup_entry(dentry, bdst, bsrc, len, flags, dst_parent);
3872 +       if (unlikely(err))
3873 +               goto out;
3874 +       dst_inode = h_dst->d_inode;
3875 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
3876 +
3877 +       err = cpup_iattr(dentry, bdst, h_src);
3878 +       isdir = S_ISDIR(dst_inode->i_mode);
3879 +       if (!err) {
3880 +               if (bdst < old_ibstart) {
3881 +                       if (S_ISREG(inode->i_mode)) {
3882 +                               err = au_dy_iaop(inode, bdst, dst_inode);
3883 +                               if (unlikely(err))
3884 +                                       goto out_rev;
3885 +                       }
3886 +                       au_set_ibstart(inode, bdst);
3887 +               }
3888 +               au_set_h_iptr(inode, bdst, au_igrab(dst_inode),
3889 +                             au_hi_flags(inode, isdir));
3890 +               mutex_unlock(&dst_inode->i_mutex);
3891 +               if (!isdir
3892 +                   && h_src->d_inode->i_nlink > 1
3893 +                   && plink)
3894 +                       au_plink_append(inode, bdst, h_dst);
3895 +               goto out; /* success */
3896 +       }
3897 +
3898 +       /* revert */
3899 +out_rev:
3900 +       h_path.dentry = h_parent;
3901 +       mutex_unlock(&dst_inode->i_mutex);
3902 +       au_dtime_store(&dt, dst_parent, &h_path);
3903 +       h_path.dentry = h_dst;
3904 +       if (!isdir)
3905 +               rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
3906 +       else
3907 +               rerr = vfsub_rmdir(h_dir, &h_path);
3908 +       au_dtime_revert(&dt);
3909 +       if (rerr) {
3910 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
3911 +               err = -EIO;
3912 +       }
3913 +
3914 +out:
3915 +       dput(dst_parent);
3916 +       return err;
3917 +}
3918 +
3919 +struct au_cpup_single_args {
3920 +       int *errp;
3921 +       struct dentry *dentry;
3922 +       aufs_bindex_t bdst, bsrc;
3923 +       loff_t len;
3924 +       unsigned int flags;
3925 +       struct dentry *dst_parent;
3926 +};
3927 +
3928 +static void au_call_cpup_single(void *args)
3929 +{
3930 +       struct au_cpup_single_args *a = args;
3931 +       *a->errp = au_cpup_single(a->dentry, a->bdst, a->bsrc, a->len,
3932 +                                 a->flags, a->dst_parent);
3933 +}
3934 +
3935 +/*
3936 + * prevent SIGXFSZ in copy-up.
3937 + * testing CAP_MKNOD is for generic fs,
3938 + * but CAP_FSETID is for xfs only, currently.
3939 + */
3940 +static int au_cpup_sio_test(struct super_block *sb, umode_t mode)
3941 +{
3942 +       int do_sio;
3943 +
3944 +       do_sio = 0;
3945 +       if (!au_wkq_test()
3946 +           && (!au_sbi(sb)->si_plink_maint_pid
3947 +               || au_plink_maint(sb, AuLock_NOPLM))) {
3948 +               switch (mode & S_IFMT) {
3949 +               case S_IFREG:
3950 +                       /* no condition about RLIMIT_FSIZE and the file size */
3951 +                       do_sio = 1;
3952 +                       break;
3953 +               case S_IFCHR:
3954 +               case S_IFBLK:
3955 +                       do_sio = !capable(CAP_MKNOD);
3956 +                       break;
3957 +               }
3958 +               if (!do_sio)
3959 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
3960 +                                 && !capable(CAP_FSETID));
3961 +       }
3962 +
3963 +       return do_sio;
3964 +}
3965 +
3966 +int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
3967 +                      aufs_bindex_t bsrc, loff_t len, unsigned int flags,
3968 +                      struct dentry *dst_parent)
3969 +{
3970 +       int err, wkq_err;
3971 +       struct dentry *h_dentry;
3972 +
3973 +       h_dentry = au_h_dptr(dentry, bsrc);
3974 +       if (!au_cpup_sio_test(dentry->d_sb, h_dentry->d_inode->i_mode))
3975 +               err = au_cpup_single(dentry, bdst, bsrc, len, flags,
3976 +                                    dst_parent);
3977 +       else {
3978 +               struct au_cpup_single_args args = {
3979 +                       .errp           = &err,
3980 +                       .dentry         = dentry,
3981 +                       .bdst           = bdst,
3982 +                       .bsrc           = bsrc,
3983 +                       .len            = len,
3984 +                       .flags          = flags,
3985 +                       .dst_parent     = dst_parent
3986 +               };
3987 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
3988 +               if (unlikely(wkq_err))
3989 +                       err = wkq_err;
3990 +       }
3991 +
3992 +       return err;
3993 +}
3994 +
3995 +/*
3996 + * copyup the @dentry from the first active lower branch to @bdst,
3997 + * using au_cpup_single().
3998 + */
3999 +static int au_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4000 +                         unsigned int flags)
4001 +{
4002 +       int err;
4003 +       aufs_bindex_t bsrc, bend;
4004 +
4005 +       bend = au_dbend(dentry);
4006 +       for (bsrc = bdst + 1; bsrc <= bend; bsrc++)
4007 +               if (au_h_dptr(dentry, bsrc))
4008 +                       break;
4009 +
4010 +       err = au_lkup_neg(dentry, bdst);
4011 +       if (!err) {
4012 +               err = au_cpup_single(dentry, bdst, bsrc, len, flags, NULL);
4013 +               if (!err)
4014 +                       return 0; /* success */
4015 +
4016 +               /* revert */
4017 +               au_set_h_dptr(dentry, bdst, NULL);
4018 +               au_set_dbstart(dentry, bsrc);
4019 +       }
4020 +
4021 +       return err;
4022 +}
4023 +
4024 +struct au_cpup_simple_args {
4025 +       int *errp;
4026 +       struct dentry *dentry;
4027 +       aufs_bindex_t bdst;
4028 +       loff_t len;
4029 +       unsigned int flags;
4030 +};
4031 +
4032 +static void au_call_cpup_simple(void *args)
4033 +{
4034 +       struct au_cpup_simple_args *a = args;
4035 +       *a->errp = au_cpup_simple(a->dentry, a->bdst, a->len, a->flags);
4036 +}
4037 +
4038 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4039 +                      unsigned int flags)
4040 +{
4041 +       int err, wkq_err;
4042 +       struct dentry *parent;
4043 +       struct inode *h_dir;
4044 +
4045 +       parent = dget_parent(dentry);
4046 +       h_dir = au_h_iptr(parent->d_inode, bdst);
4047 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
4048 +           && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
4049 +               err = au_cpup_simple(dentry, bdst, len, flags);
4050 +       else {
4051 +               struct au_cpup_simple_args args = {
4052 +                       .errp           = &err,
4053 +                       .dentry         = dentry,
4054 +                       .bdst           = bdst,
4055 +                       .len            = len,
4056 +                       .flags          = flags
4057 +               };
4058 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
4059 +               if (unlikely(wkq_err))
4060 +                       err = wkq_err;
4061 +       }
4062 +
4063 +       dput(parent);
4064 +       return err;
4065 +}
4066 +
4067 +/* ---------------------------------------------------------------------- */
4068 +
4069 +/*
4070 + * copyup the deleted file for writing.
4071 + */
4072 +static int au_do_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst,
4073 +                        struct dentry *wh_dentry, struct file *file,
4074 +                        loff_t len)
4075 +{
4076 +       int err;
4077 +       aufs_bindex_t bstart;
4078 +       struct au_dinfo *dinfo;
4079 +       struct dentry *h_d_dst, *h_d_start;
4080 +       struct au_hdentry *hdp;
4081 +
4082 +       dinfo = au_di(dentry);
4083 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4084 +
4085 +       bstart = dinfo->di_bstart;
4086 +       hdp = dinfo->di_hdentry;
4087 +       h_d_dst = hdp[0 + bdst].hd_dentry;
4088 +       dinfo->di_bstart = bdst;
4089 +       hdp[0 + bdst].hd_dentry = wh_dentry;
4090 +       if (file) {
4091 +               h_d_start = hdp[0 + bstart].hd_dentry;
4092 +               hdp[0 + bstart].hd_dentry = au_hf_top(file)->f_dentry;
4093 +       }
4094 +       err = au_cpup_single(dentry, bdst, bstart, len, !AuCpup_DTIME,
4095 +                            /*h_parent*/NULL);
4096 +       if (file) {
4097 +               if (!err)
4098 +                       err = au_reopen_nondir(file);
4099 +               hdp[0 + bstart].hd_dentry = h_d_start;
4100 +       }
4101 +       hdp[0 + bdst].hd_dentry = h_d_dst;
4102 +       dinfo->di_bstart = bstart;
4103 +
4104 +       return err;
4105 +}
4106 +
4107 +static int au_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4108 +                     struct file *file)
4109 +{
4110 +       int err;
4111 +       struct au_dtime dt;
4112 +       struct dentry *parent, *h_parent, *wh_dentry;
4113 +       struct au_branch *br;
4114 +       struct path h_path;
4115 +
4116 +       br = au_sbr(dentry->d_sb, bdst);
4117 +       parent = dget_parent(dentry);
4118 +       h_parent = au_h_dptr(parent, bdst);
4119 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
4120 +       err = PTR_ERR(wh_dentry);
4121 +       if (IS_ERR(wh_dentry))
4122 +               goto out;
4123 +
4124 +       h_path.dentry = h_parent;
4125 +       h_path.mnt = br->br_mnt;
4126 +       au_dtime_store(&dt, parent, &h_path);
4127 +       err = au_do_cpup_wh(dentry, bdst, wh_dentry, file, len);
4128 +       if (unlikely(err))
4129 +               goto out_wh;
4130 +
4131 +       dget(wh_dentry);
4132 +       h_path.dentry = wh_dentry;
4133 +       if (!S_ISDIR(wh_dentry->d_inode->i_mode))
4134 +               err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
4135 +       else
4136 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
4137 +       if (unlikely(err)) {
4138 +               AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
4139 +                       AuDLNPair(wh_dentry), err);
4140 +               err = -EIO;
4141 +       }
4142 +       au_dtime_revert(&dt);
4143 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
4144 +
4145 +out_wh:
4146 +       dput(wh_dentry);
4147 +out:
4148 +       dput(parent);
4149 +       return err;
4150 +}
4151 +
4152 +struct au_cpup_wh_args {
4153 +       int *errp;
4154 +       struct dentry *dentry;
4155 +       aufs_bindex_t bdst;
4156 +       loff_t len;
4157 +       struct file *file;
4158 +};
4159 +
4160 +static void au_call_cpup_wh(void *args)
4161 +{
4162 +       struct au_cpup_wh_args *a = args;
4163 +       *a->errp = au_cpup_wh(a->dentry, a->bdst, a->len, a->file);
4164 +}
4165 +
4166 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4167 +                  struct file *file)
4168 +{
4169 +       int err, wkq_err;
4170 +       struct dentry *parent, *h_orph, *h_parent, *h_dentry;
4171 +       struct inode *dir, *h_dir, *h_tmpdir, *h_inode;
4172 +       struct au_wbr *wbr;
4173 +
4174 +       parent = dget_parent(dentry);
4175 +       dir = parent->d_inode;
4176 +       h_orph = NULL;
4177 +       h_parent = NULL;
4178 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
4179 +       h_tmpdir = h_dir;
4180 +       if (!h_dir->i_nlink) {
4181 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
4182 +               h_orph = wbr->wbr_orph;
4183 +
4184 +               h_parent = dget(au_h_dptr(parent, bdst));
4185 +               au_set_h_dptr(parent, bdst, dget(h_orph));
4186 +               h_tmpdir = h_orph->d_inode;
4187 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
4188 +
4189 +               /* this temporary unlock is safe */
4190 +               if (file)
4191 +                       h_dentry = au_hf_top(file)->f_dentry;
4192 +               else
4193 +                       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
4194 +               h_inode = h_dentry->d_inode;
4195 +               IMustLock(h_inode);
4196 +               mutex_unlock(&h_inode->i_mutex);
4197 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
4198 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
4199 +               /* todo: au_h_open_pre()? */
4200 +       }
4201 +
4202 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
4203 +           && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
4204 +               err = au_cpup_wh(dentry, bdst, len, file);
4205 +       else {
4206 +               struct au_cpup_wh_args args = {
4207 +                       .errp   = &err,
4208 +                       .dentry = dentry,
4209 +                       .bdst   = bdst,
4210 +                       .len    = len,
4211 +                       .file   = file
4212 +               };
4213 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
4214 +               if (unlikely(wkq_err))
4215 +                       err = wkq_err;
4216 +       }
4217 +
4218 +       if (h_orph) {
4219 +               mutex_unlock(&h_tmpdir->i_mutex);
4220 +               /* todo: au_h_open_post()? */
4221 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
4222 +               au_set_h_dptr(parent, bdst, h_parent);
4223 +       }
4224 +       iput(h_dir);
4225 +       dput(parent);
4226 +
4227 +       return err;
4228 +}
4229 +
4230 +/* ---------------------------------------------------------------------- */
4231 +
4232 +/*
4233 + * generic routine for both of copy-up and copy-down.
4234 + */
4235 +/* cf. revalidate function in file.c */
4236 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4237 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4238 +                        struct dentry *h_parent, void *arg),
4239 +              void *arg)
4240 +{
4241 +       int err;
4242 +       struct au_pin pin;
4243 +       struct dentry *d, *parent, *h_parent, *real_parent;
4244 +
4245 +       err = 0;
4246 +       parent = dget_parent(dentry);
4247 +       if (IS_ROOT(parent))
4248 +               goto out;
4249 +
4250 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
4251 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
4252 +
4253 +       /* do not use au_dpage */
4254 +       real_parent = parent;
4255 +       while (1) {
4256 +               dput(parent);
4257 +               parent = dget_parent(dentry);
4258 +               h_parent = au_h_dptr(parent, bdst);
4259 +               if (h_parent)
4260 +                       goto out; /* success */
4261 +
4262 +               /* find top dir which is necessary to cpup */
4263 +               do {
4264 +                       d = parent;
4265 +                       dput(parent);
4266 +                       parent = dget_parent(d);
4267 +                       di_read_lock_parent3(parent, !AuLock_IR);
4268 +                       h_parent = au_h_dptr(parent, bdst);
4269 +                       di_read_unlock(parent, !AuLock_IR);
4270 +               } while (!h_parent);
4271 +
4272 +               if (d != real_parent)
4273 +                       di_write_lock_child3(d);
4274 +
4275 +               /* somebody else might create while we were sleeping */
4276 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
4277 +                       if (au_h_dptr(d, bdst))
4278 +                               au_update_dbstart(d);
4279 +
4280 +                       au_pin_set_dentry(&pin, d);
4281 +                       err = au_do_pin(&pin);
4282 +                       if (!err) {
4283 +                               err = cp(d, bdst, h_parent, arg);
4284 +                               au_unpin(&pin);
4285 +                       }
4286 +               }
4287 +
4288 +               if (d != real_parent)
4289 +                       di_write_unlock(d);
4290 +               if (unlikely(err))
4291 +                       break;
4292 +       }
4293 +
4294 +out:
4295 +       dput(parent);
4296 +       return err;
4297 +}
4298 +
4299 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
4300 +                      struct dentry *h_parent __maybe_unused ,
4301 +                      void *arg __maybe_unused)
4302 +{
4303 +       return au_sio_cpup_simple(dentry, bdst, -1, AuCpup_DTIME);
4304 +}
4305 +
4306 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4307 +{
4308 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
4309 +}
4310 +
4311 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4312 +{
4313 +       int err;
4314 +       struct dentry *parent;
4315 +       struct inode *dir;
4316 +
4317 +       parent = dget_parent(dentry);
4318 +       dir = parent->d_inode;
4319 +       err = 0;
4320 +       if (au_h_iptr(dir, bdst))
4321 +               goto out;
4322 +
4323 +       di_read_unlock(parent, AuLock_IR);
4324 +       di_write_lock_parent(parent);
4325 +       /* someone else might change our inode while we were sleeping */
4326 +       if (!au_h_iptr(dir, bdst))
4327 +               err = au_cpup_dirs(dentry, bdst);
4328 +       di_downgrade_lock(parent, AuLock_IR);
4329 +
4330 +out:
4331 +       dput(parent);
4332 +       return err;
4333 +}
4334 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
4335 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
4336 +++ linux/fs/aufs/cpup.h        2011-08-24 13:30:24.731313534 +0200
4337 @@ -0,0 +1,83 @@
4338 +/*
4339 + * Copyright (C) 2005-2011 Junjiro R. Okajima
4340 + *
4341 + * This program, aufs is free software; you can redistribute it and/or modify
4342 + * it under the terms of the GNU General Public License as published by
4343 + * the Free Software Foundation; either version 2 of the License, or
4344 + * (at your option) any later version.
4345 + *
4346 + * This program is distributed in the hope that it will be useful,
4347 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4348 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4349 + * GNU General Public License for more details.
4350 + *
4351 + * You should have received a copy of the GNU General Public License
4352 + * along with this program; if not, write to the Free Software
4353 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4354 + */
4355 +
4356 +/*
4357 + * copy-up/down functions
4358 + */
4359 +
4360 +#ifndef __AUFS_CPUP_H__
4361 +#define __AUFS_CPUP_H__
4362 +
4363 +#ifdef __KERNEL__
4364 +
4365 +#include <linux/path.h>
4366 +#include <linux/time.h>
4367 +#include <linux/aufs_type.h>
4368 +
4369 +struct inode;
4370 +struct file;
4371 +
4372 +void au_cpup_attr_flags(struct inode *dst, struct inode *src);
4373 +void au_cpup_attr_timesizes(struct inode *inode);
4374 +void au_cpup_attr_nlink(struct inode *inode, int force);
4375 +void au_cpup_attr_changeable(struct inode *inode);
4376 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
4377 +void au_cpup_attr_all(struct inode *inode, int force);
4378 +
4379 +/* ---------------------------------------------------------------------- */
4380 +
4381 +/* cpup flags */
4382 +#define AuCpup_DTIME   1               /* do dtime_store/revert */
4383 +#define AuCpup_KEEPLINO        (1 << 1)        /* do not clear the lower xino,
4384 +                                          for link(2) */
4385 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
4386 +#define au_fset_cpup(flags, name) \
4387 +       do { (flags) |= AuCpup_##name; } while (0)
4388 +#define au_fclr_cpup(flags, name) \
4389 +       do { (flags) &= ~AuCpup_##name; } while (0)
4390 +
4391 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
4392 +int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
4393 +                      aufs_bindex_t bsrc, loff_t len, unsigned int flags,
4394 +                      struct dentry *dst_parent);
4395 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4396 +                      unsigned int flags);
4397 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4398 +                  struct file *file);
4399 +
4400 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4401 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4402 +                        struct dentry *h_parent, void *arg),
4403 +              void *arg);
4404 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4405 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4406 +
4407 +/* ---------------------------------------------------------------------- */
4408 +
4409 +/* keep timestamps when copyup */
4410 +struct au_dtime {
4411 +       struct dentry *dt_dentry;
4412 +       struct path dt_h_path;
4413 +       struct timespec dt_atime, dt_mtime;
4414 +};
4415 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4416 +                   struct path *h_path);
4417 +void au_dtime_revert(struct au_dtime *dt);
4418 +
4419 +#endif /* __KERNEL__ */
4420 +#endif /* __AUFS_CPUP_H__ */
4421 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
4422 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
4423 +++ linux/fs/aufs/dbgaufs.c     2012-01-05 11:00:59.215501779 +0100
4424 @@ -0,0 +1,334 @@
4425 +/*
4426 + * Copyright (C) 2005-2011 Junjiro R. Okajima
4427 + *
4428 + * This program, aufs is free software; you can redistribute it and/or modify
4429 + * it under the terms of the GNU General Public License as published by
4430 + * the Free Software Foundation; either version 2 of the License, or
4431 + * (at your option) any later version.
4432 + *
4433 + * This program is distributed in the hope that it will be useful,
4434 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4435 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4436 + * GNU General Public License for more details.
4437 + *
4438 + * You should have received a copy of the GNU General Public License
4439 + * along with this program; if not, write to the Free Software
4440 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4441 + */
4442 +
4443 +/*
4444 + * debugfs interface
4445 + */
4446 +
4447 +#include <linux/debugfs.h>
4448 +#include "aufs.h"
4449 +
4450 +#ifndef CONFIG_SYSFS
4451 +#error DEBUG_FS depends upon SYSFS
4452 +#endif
4453 +
4454 +static struct dentry *dbgaufs;
4455 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
4456 +
4457 +/* 20 is max digits length of ulong 64 */
4458 +struct dbgaufs_arg {
4459 +       int n;
4460 +       char a[20 * 4];
4461 +};
4462 +
4463 +/*
4464 + * common function for all XINO files
4465 + */
4466 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
4467 +                             struct file *file)
4468 +{
4469 +       kfree(file->private_data);
4470 +       return 0;
4471 +}
4472 +
4473 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
4474 +{
4475 +       int err;
4476 +       struct kstat st;
4477 +       struct dbgaufs_arg *p;
4478 +
4479 +       err = -ENOMEM;
4480 +       p = kmalloc(sizeof(*p), GFP_NOFS);
4481 +       if (unlikely(!p))
4482 +               goto out;
4483 +
4484 +       err = 0;
4485 +       p->n = 0;
4486 +       file->private_data = p;
4487 +       if (!xf)
4488 +               goto out;
4489 +
4490 +       err = vfs_getattr(xf->f_vfsmnt, xf->f_dentry, &st);
4491 +       if (!err) {
4492 +               if (do_fcnt)
4493 +                       p->n = snprintf
4494 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
4495 +                                (long)file_count(xf), st.blocks, st.blksize,
4496 +                                (long long)st.size);
4497 +               else
4498 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
4499 +                                       st.blocks, st.blksize,
4500 +                                       (long long)st.size);
4501 +               AuDebugOn(p->n >= sizeof(p->a));
4502 +       } else {
4503 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
4504 +               err = 0;
4505 +       }
4506 +
4507 +out:
4508 +       return err;
4509 +
4510 +}
4511 +
4512 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
4513 +                              size_t count, loff_t *ppos)
4514 +{
4515 +       struct dbgaufs_arg *p;
4516 +
4517 +       p = file->private_data;
4518 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
4519 +}
4520 +
4521 +/* ---------------------------------------------------------------------- */
4522 +
4523 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
4524 +{
4525 +       int err;
4526 +       struct au_sbinfo *sbinfo;
4527 +       struct super_block *sb;
4528 +
4529 +       sbinfo = inode->i_private;
4530 +       sb = sbinfo->si_sb;
4531 +       si_noflush_read_lock(sb);
4532 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
4533 +       si_read_unlock(sb);
4534 +       return err;
4535 +}
4536 +
4537 +static const struct file_operations dbgaufs_xib_fop = {
4538 +       .owner          = THIS_MODULE,
4539 +       .open           = dbgaufs_xib_open,
4540 +       .release        = dbgaufs_xi_release,
4541 +       .read           = dbgaufs_xi_read
4542 +};
4543 +
4544 +/* ---------------------------------------------------------------------- */
4545 +
4546 +#define DbgaufsXi_PREFIX "xi"
4547 +
4548 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
4549 +{
4550 +       int err;
4551 +       long l;
4552 +       struct au_sbinfo *sbinfo;
4553 +       struct super_block *sb;
4554 +       struct file *xf;
4555 +       struct qstr *name;
4556 +
4557 +       err = -ENOENT;
4558 +       xf = NULL;
4559 +       name = &file->f_dentry->d_name;
4560 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
4561 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
4562 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
4563 +               goto out;
4564 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
4565 +       if (unlikely(err))
4566 +               goto out;
4567 +
4568 +       sbinfo = inode->i_private;
4569 +       sb = sbinfo->si_sb;
4570 +       si_noflush_read_lock(sb);
4571 +       if (l <= au_sbend(sb)) {
4572 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
4573 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
4574 +       } else
4575 +               err = -ENOENT;
4576 +       si_read_unlock(sb);
4577 +
4578 +out:
4579 +       return err;
4580 +}
4581 +
4582 +static const struct file_operations dbgaufs_xino_fop = {
4583 +       .owner          = THIS_MODULE,
4584 +       .open           = dbgaufs_xino_open,
4585 +       .release        = dbgaufs_xi_release,
4586 +       .read           = dbgaufs_xi_read
4587 +};
4588 +
4589 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
4590 +{
4591 +       aufs_bindex_t bend;
4592 +       struct au_branch *br;
4593 +       struct au_xino_file *xi;
4594 +
4595 +       if (!au_sbi(sb)->si_dbgaufs)
4596 +               return;
4597 +
4598 +       bend = au_sbend(sb);
4599 +       for (; bindex <= bend; bindex++) {
4600 +               br = au_sbr(sb, bindex);
4601 +               xi = &br->br_xino;
4602 +               if (xi->xi_dbgaufs) {
4603 +                       debugfs_remove(xi->xi_dbgaufs);
4604 +                       xi->xi_dbgaufs = NULL;
4605 +               }
4606 +       }
4607 +}
4608 +
4609 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
4610 +{
4611 +       struct au_sbinfo *sbinfo;
4612 +       struct dentry *parent;
4613 +       struct au_branch *br;
4614 +       struct au_xino_file *xi;
4615 +       aufs_bindex_t bend;
4616 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
4617 +
4618 +       sbinfo = au_sbi(sb);
4619 +       parent = sbinfo->si_dbgaufs;
4620 +       if (!parent)
4621 +               return;
4622 +
4623 +       bend = au_sbend(sb);
4624 +       for (; bindex <= bend; bindex++) {
4625 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
4626 +               br = au_sbr(sb, bindex);
4627 +               xi = &br->br_xino;
4628 +               AuDebugOn(xi->xi_dbgaufs);
4629 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
4630 +                                                    sbinfo, &dbgaufs_xino_fop);
4631 +               /* ignore an error */
4632 +               if (unlikely(!xi->xi_dbgaufs))
4633 +                       AuWarn1("failed %s under debugfs\n", name);
4634 +       }
4635 +}
4636 +
4637 +/* ---------------------------------------------------------------------- */
4638 +
4639 +#ifdef CONFIG_AUFS_EXPORT
4640 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
4641 +{
4642 +       int err;
4643 +       struct au_sbinfo *sbinfo;
4644 +       struct super_block *sb;
4645 +
4646 +       sbinfo = inode->i_private;
4647 +       sb = sbinfo->si_sb;
4648 +       si_noflush_read_lock(sb);
4649 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
4650 +       si_read_unlock(sb);
4651 +       return err;
4652 +}
4653 +
4654 +static const struct file_operations dbgaufs_xigen_fop = {
4655 +       .owner          = THIS_MODULE,
4656 +       .open           = dbgaufs_xigen_open,
4657 +       .release        = dbgaufs_xi_release,
4658 +       .read           = dbgaufs_xi_read
4659 +};
4660 +
4661 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
4662 +{
4663 +       int err;
4664 +
4665 +       /*
4666 +        * This function is a dynamic '__init' fucntion actually,
4667 +        * so the tiny check for si_rwsem is unnecessary.
4668 +        */
4669 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
4670 +
4671 +       err = -EIO;
4672 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
4673 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
4674 +                &dbgaufs_xigen_fop);
4675 +       if (sbinfo->si_dbgaufs_xigen)
4676 +               err = 0;
4677 +
4678 +       return err;
4679 +}
4680 +#else
4681 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
4682 +{
4683 +       return 0;
4684 +}
4685 +#endif /* CONFIG_AUFS_EXPORT */
4686 +
4687 +/* ---------------------------------------------------------------------- */
4688 +
4689 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
4690 +{
4691 +       /*
4692 +        * This function is a dynamic '__init' fucntion actually,
4693 +        * so the tiny check for si_rwsem is unnecessary.
4694 +        */
4695 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
4696 +
4697 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
4698 +       sbinfo->si_dbgaufs = NULL;
4699 +       kobject_put(&sbinfo->si_kobj);
4700 +}
4701 +
4702 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
4703 +{
4704 +       int err;
4705 +       char name[SysaufsSiNameLen];
4706 +
4707 +       /*
4708 +        * This function is a dynamic '__init' fucntion actually,
4709 +        * so the tiny check for si_rwsem is unnecessary.
4710 +        */
4711 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
4712 +
4713 +       err = -ENOENT;
4714 +       if (!dbgaufs) {
4715 +               AuErr1("/debug/aufs is uninitialized\n");
4716 +               goto out;
4717 +       }
4718 +
4719 +       err = -EIO;
4720 +       sysaufs_name(sbinfo, name);
4721 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
4722 +       if (unlikely(!sbinfo->si_dbgaufs))
4723 +               goto out;
4724 +       kobject_get(&sbinfo->si_kobj);
4725 +
4726 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
4727 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
4728 +                &dbgaufs_xib_fop);
4729 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
4730 +               goto out_dir;
4731 +
4732 +       err = dbgaufs_xigen_init(sbinfo);
4733 +       if (!err)
4734 +               goto out; /* success */
4735 +
4736 +out_dir:
4737 +       dbgaufs_si_fin(sbinfo);
4738 +out:
4739 +       return err;
4740 +}
4741 +
4742 +/* ---------------------------------------------------------------------- */
4743 +
4744 +void dbgaufs_fin(void)
4745 +{
4746 +       debugfs_remove(dbgaufs);
4747 +}
4748 +
4749 +int __init dbgaufs_init(void)
4750 +{
4751 +       int err;
4752 +
4753 +       err = -EIO;
4754 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
4755 +       if (dbgaufs)
4756 +               err = 0;
4757 +       return err;
4758 +}
4759 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
4760 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
4761 +++ linux/fs/aufs/dbgaufs.h     2011-08-24 13:30:24.731313534 +0200
4762 @@ -0,0 +1,52 @@
4763 +/*
4764 + * Copyright (C) 2005-2011 Junjiro R. Okajima
4765 + *
4766 + * This program, aufs is free software; you can redistribute it and/or modify
4767 + * it under the terms of the GNU General Public License as published by
4768 + * the Free Software Foundation; either version 2 of the License, or
4769 + * (at your option) any later version.
4770 + *
4771 + * This program is distributed in the hope that it will be useful,
4772 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4773 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4774 + * GNU General Public License for more details.
4775 + *
4776 + * You should have received a copy of the GNU General Public License
4777 + * along with this program; if not, write to the Free Software
4778 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4779 + */
4780 +
4781 +/*
4782 + * debugfs interface
4783 + */
4784 +
4785 +#ifndef __DBGAUFS_H__
4786 +#define __DBGAUFS_H__
4787 +
4788 +#ifdef __KERNEL__
4789 +
4790 +#include <linux/init.h>
4791 +#include <linux/aufs_type.h>
4792 +
4793 +struct super_block;
4794 +struct au_sbinfo;
4795 +
4796 +#ifdef CONFIG_DEBUG_FS
4797 +/* dbgaufs.c */
4798 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
4799 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
4800 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
4801 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
4802 +void dbgaufs_fin(void);
4803 +int __init dbgaufs_init(void);
4804 +#else
4805 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
4806 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
4807 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
4808 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
4809 +AuStubVoid(dbgaufs_fin, void)
4810 +AuStubInt0(__init dbgaufs_init, void)
4811 +#endif /* CONFIG_DEBUG_FS */
4812 +
4813 +#endif /* __KERNEL__ */
4814 +#endif /* __DBGAUFS_H__ */
4815 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
4816 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
4817 +++ linux/fs/aufs/dcsub.c       2011-08-24 13:30:24.731313534 +0200
4818 @@ -0,0 +1,243 @@
4819 +/*
4820 + * Copyright (C) 2005-2011 Junjiro R. Okajima
4821 + *
4822 + * This program, aufs is free software; you can redistribute it and/or modify
4823 + * it under the terms of the GNU General Public License as published by
4824 + * the Free Software Foundation; either version 2 of the License, or
4825 + * (at your option) any later version.
4826 + *
4827 + * This program is distributed in the hope that it will be useful,
4828 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4829 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4830 + * GNU General Public License for more details.
4831 + *
4832 + * You should have received a copy of the GNU General Public License
4833 + * along with this program; if not, write to the Free Software
4834 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4835 + */
4836 +
4837 +/*
4838 + * sub-routines for dentry cache
4839 + */
4840 +
4841 +#include "aufs.h"
4842 +
4843 +static void au_dpage_free(struct au_dpage *dpage)
4844 +{
4845 +       int i;
4846 +       struct dentry **p;
4847 +
4848 +       p = dpage->dentries;
4849 +       for (i = 0; i < dpage->ndentry; i++)
4850 +               dput(*p++);
4851 +       free_page((unsigned long)dpage->dentries);
4852 +}
4853 +
4854 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
4855 +{
4856 +       int err;
4857 +       void *p;
4858 +
4859 +       err = -ENOMEM;
4860 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
4861 +       if (unlikely(!dpages->dpages))
4862 +               goto out;
4863 +
4864 +       p = (void *)__get_free_page(gfp);
4865 +       if (unlikely(!p))
4866 +               goto out_dpages;
4867 +
4868 +       dpages->dpages[0].ndentry = 0;
4869 +       dpages->dpages[0].dentries = p;
4870 +       dpages->ndpage = 1;
4871 +       return 0; /* success */
4872 +
4873 +out_dpages:
4874 +       kfree(dpages->dpages);
4875 +out:
4876 +       return err;
4877 +}
4878 +
4879 +void au_dpages_free(struct au_dcsub_pages *dpages)
4880 +{
4881 +       int i;
4882 +       struct au_dpage *p;
4883 +
4884 +       p = dpages->dpages;
4885 +       for (i = 0; i < dpages->ndpage; i++)
4886 +               au_dpage_free(p++);
4887 +       kfree(dpages->dpages);
4888 +}
4889 +
4890 +static int au_dpages_append(struct au_dcsub_pages *dpages,
4891 +                           struct dentry *dentry, gfp_t gfp)
4892 +{
4893 +       int err, sz;
4894 +       struct au_dpage *dpage;
4895 +       void *p;
4896 +
4897 +       dpage = dpages->dpages + dpages->ndpage - 1;
4898 +       sz = PAGE_SIZE / sizeof(dentry);
4899 +       if (unlikely(dpage->ndentry >= sz)) {
4900 +               AuLabel(new dpage);
4901 +               err = -ENOMEM;
4902 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
4903 +               p = au_kzrealloc(dpages->dpages, sz,
4904 +                                sz + sizeof(*dpages->dpages), gfp);
4905 +               if (unlikely(!p))
4906 +                       goto out;
4907 +
4908 +               dpages->dpages = p;
4909 +               dpage = dpages->dpages + dpages->ndpage;
4910 +               p = (void *)__get_free_page(gfp);
4911 +               if (unlikely(!p))
4912 +                       goto out;
4913 +
4914 +               dpage->ndentry = 0;
4915 +               dpage->dentries = p;
4916 +               dpages->ndpage++;
4917 +       }
4918 +
4919 +       AuDebugOn(!dentry->d_count);
4920 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
4921 +       return 0; /* success */
4922 +
4923 +out:
4924 +       return err;
4925 +}
4926 +
4927 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
4928 +                  au_dpages_test test, void *arg)
4929 +{
4930 +       int err;
4931 +       struct dentry *this_parent;
4932 +       struct list_head *next;
4933 +       struct super_block *sb = root->d_sb;
4934 +
4935 +       err = 0;
4936 +       write_seqlock(&rename_lock);
4937 +       this_parent = root;
4938 +       spin_lock(&this_parent->d_lock);
4939 +repeat:
4940 +       next = this_parent->d_subdirs.next;
4941 +resume:
4942 +       if (this_parent->d_sb == sb
4943 +           && !IS_ROOT(this_parent)
4944 +           && au_di(this_parent)
4945 +           && this_parent->d_count
4946 +           && (!test || test(this_parent, arg))) {
4947 +               err = au_dpages_append(dpages, this_parent, GFP_ATOMIC);
4948 +               if (unlikely(err))
4949 +                       goto out;
4950 +       }
4951 +
4952 +       while (next != &this_parent->d_subdirs) {
4953 +               struct list_head *tmp = next;
4954 +               struct dentry *dentry = list_entry(tmp, struct dentry,
4955 +                                                  d_u.d_child);
4956 +
4957 +               next = tmp->next;
4958 +               spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
4959 +               if (dentry->d_count) {
4960 +                       if (!list_empty(&dentry->d_subdirs)) {
4961 +                               spin_unlock(&this_parent->d_lock);
4962 +                               spin_release(&dentry->d_lock.dep_map, 1,
4963 +                                            _RET_IP_);
4964 +                               this_parent = dentry;
4965 +                               spin_acquire(&this_parent->d_lock.dep_map, 0, 1,
4966 +                                            _RET_IP_);
4967 +                               goto repeat;
4968 +                       }
4969 +                       if (dentry->d_sb == sb
4970 +                           && au_di(dentry)
4971 +                           && (!test || test(dentry, arg)))
4972 +                               err = au_dpages_append(dpages, dentry,
4973 +                                                      GFP_ATOMIC);
4974 +               }
4975 +               spin_unlock(&dentry->d_lock);
4976 +               if (unlikely(err))
4977 +                       goto out;
4978 +       }
4979 +
4980 +       if (this_parent != root) {
4981 +               struct dentry *tmp;
4982 +               struct dentry *child;
4983 +
4984 +               tmp = this_parent->d_parent;
4985 +               rcu_read_lock();
4986 +               spin_unlock(&this_parent->d_lock);
4987 +               child = this_parent;
4988 +               this_parent = tmp;
4989 +               spin_lock(&this_parent->d_lock);
4990 +               rcu_read_unlock();
4991 +               next = child->d_u.d_child.next;
4992 +               goto resume;
4993 +       }
4994 +
4995 +out:
4996 +       spin_unlock(&this_parent->d_lock);
4997 +       write_sequnlock(&rename_lock);
4998 +       return err;
4999 +}
5000 +
5001 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5002 +                      int do_include, au_dpages_test test, void *arg)
5003 +{
5004 +       int err;
5005 +
5006 +       err = 0;
5007 +       write_seqlock(&rename_lock);
5008 +       spin_lock(&dentry->d_lock);
5009 +       if (do_include
5010 +           && dentry->d_count
5011 +           && (!test || test(dentry, arg)))
5012 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5013 +       spin_unlock(&dentry->d_lock);
5014 +       if (unlikely(err))
5015 +               goto out;
5016 +
5017 +       /*
5018 +        * vfsmount_lock is unnecessary since this is a traverse in a single
5019 +        * mount
5020 +        */
5021 +       while (!IS_ROOT(dentry)) {
5022 +               dentry = dentry->d_parent; /* rename_lock is locked */
5023 +               spin_lock(&dentry->d_lock);
5024 +               if (dentry->d_count
5025 +                   && (!test || test(dentry, arg)))
5026 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5027 +               spin_unlock(&dentry->d_lock);
5028 +               if (unlikely(err))
5029 +                       break;
5030 +       }
5031 +
5032 +out:
5033 +       write_sequnlock(&rename_lock);
5034 +       return err;
5035 +}
5036 +
5037 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
5038 +{
5039 +       return au_di(dentry) && dentry->d_sb == arg;
5040 +}
5041 +
5042 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5043 +                           struct dentry *dentry, int do_include)
5044 +{
5045 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
5046 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
5047 +}
5048 +
5049 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
5050 +{
5051 +       struct path path[2] = {
5052 +               {
5053 +                       .dentry = d1
5054 +               },
5055 +               {
5056 +                       .dentry = d2
5057 +               }
5058 +       };
5059 +
5060 +       return path_is_under(path + 0, path + 1);
5061 +}
5062 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
5063 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
5064 +++ linux/fs/aufs/dcsub.h       2011-08-24 13:30:24.731313534 +0200
5065 @@ -0,0 +1,95 @@
5066 +/*
5067 + * Copyright (C) 2005-2011 Junjiro R. Okajima
5068 + *
5069 + * This program, aufs is free software; you can redistribute it and/or modify
5070 + * it under the terms of the GNU General Public License as published by
5071 + * the Free Software Foundation; either version 2 of the License, or
5072 + * (at your option) any later version.
5073 + *
5074 + * This program is distributed in the hope that it will be useful,
5075 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5076 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5077 + * GNU General Public License for more details.
5078 + *
5079 + * You should have received a copy of the GNU General Public License
5080 + * along with this program; if not, write to the Free Software
5081 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5082 + */
5083 +
5084 +/*
5085 + * sub-routines for dentry cache
5086 + */
5087 +
5088 +#ifndef __AUFS_DCSUB_H__
5089 +#define __AUFS_DCSUB_H__
5090 +
5091 +#ifdef __KERNEL__
5092 +
5093 +#include <linux/dcache.h>
5094 +#include <linux/fs.h>
5095 +#include <linux/types.h>
5096 +
5097 +struct dentry;
5098 +
5099 +struct au_dpage {
5100 +       int ndentry;
5101 +       struct dentry **dentries;
5102 +};
5103 +
5104 +struct au_dcsub_pages {
5105 +       int ndpage;
5106 +       struct au_dpage *dpages;
5107 +};
5108 +
5109 +/* ---------------------------------------------------------------------- */
5110 +
5111 +/* dcsub.c */
5112 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
5113 +void au_dpages_free(struct au_dcsub_pages *dpages);
5114 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
5115 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5116 +                  au_dpages_test test, void *arg);
5117 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5118 +                      int do_include, au_dpages_test test, void *arg);
5119 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5120 +                           struct dentry *dentry, int do_include);
5121 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
5122 +
5123 +/* ---------------------------------------------------------------------- */
5124 +
5125 +static inline int au_d_hashed_positive(struct dentry *d)
5126 +{
5127 +       int err;
5128 +       struct inode *inode = d->d_inode;
5129 +       err = 0;
5130 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
5131 +               err = -ENOENT;
5132 +       return err;
5133 +}
5134 +
5135 +static inline int au_d_alive(struct dentry *d)
5136 +{
5137 +       int err;
5138 +       struct inode *inode;
5139 +       err = 0;
5140 +       if (!IS_ROOT(d))
5141 +               err = au_d_hashed_positive(d);
5142 +       else {
5143 +               inode = d->d_inode;
5144 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
5145 +                       err = -ENOENT;
5146 +       }
5147 +       return err;
5148 +}
5149 +
5150 +static inline int au_alive_dir(struct dentry *d)
5151 +{
5152 +       int err;
5153 +       err = au_d_alive(d);
5154 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
5155 +               err = -ENOENT;
5156 +       return err;
5157 +}
5158 +
5159 +#endif /* __KERNEL__ */
5160 +#endif /* __AUFS_DCSUB_H__ */
5161 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
5162 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
5163 +++ linux/fs/aufs/debug.c       2011-10-24 20:51:51.580466925 +0200
5164 @@ -0,0 +1,490 @@
5165 +/*
5166 + * Copyright (C) 2005-2011 Junjiro R. Okajima
5167 + *
5168 + * This program, aufs is free software; you can redistribute it and/or modify
5169 + * it under the terms of the GNU General Public License as published by
5170 + * the Free Software Foundation; either version 2 of the License, or
5171 + * (at your option) any later version.
5172 + *
5173 + * This program is distributed in the hope that it will be useful,
5174 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5175 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5176 + * GNU General Public License for more details.
5177 + *
5178 + * You should have received a copy of the GNU General Public License
5179 + * along with this program; if not, write to the Free Software
5180 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5181 + */
5182 +
5183 +/*
5184 + * debug print functions
5185 + */
5186 +
5187 +#include <linux/module.h>
5188 +#include <linux/vt_kern.h>
5189 +#include "aufs.h"
5190 +
5191 +int aufs_debug;
5192 +MODULE_PARM_DESC(debug, "debug print");
5193 +module_param_named(debug, aufs_debug, int, S_IRUGO | S_IWUSR | S_IWGRP);
5194 +
5195 +char *au_plevel = KERN_DEBUG;
5196 +#define dpri(fmt, ...) do {                                    \
5197 +       if ((au_plevel                                          \
5198 +            && strcmp(au_plevel, KERN_DEBUG))                  \
5199 +           || au_debug_test())                                 \
5200 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
5201 +} while (0)
5202 +
5203 +/* ---------------------------------------------------------------------- */
5204 +
5205 +void au_dpri_whlist(struct au_nhash *whlist)
5206 +{
5207 +       unsigned long ul, n;
5208 +       struct hlist_head *head;
5209 +       struct au_vdir_wh *tpos;
5210 +       struct hlist_node *pos;
5211 +
5212 +       n = whlist->nh_num;
5213 +       head = whlist->nh_head;
5214 +       for (ul = 0; ul < n; ul++) {
5215 +               hlist_for_each_entry(tpos, pos, head, wh_hash)
5216 +                       dpri("b%d, %.*s, %d\n",
5217 +                            tpos->wh_bindex,
5218 +                            tpos->wh_str.len, tpos->wh_str.name,
5219 +                            tpos->wh_str.len);
5220 +               head++;
5221 +       }
5222 +}
5223 +
5224 +void au_dpri_vdir(struct au_vdir *vdir)
5225 +{
5226 +       unsigned long ul;
5227 +       union au_vdir_deblk_p p;
5228 +       unsigned char *o;
5229 +
5230 +       if (!vdir || IS_ERR(vdir)) {
5231 +               dpri("err %ld\n", PTR_ERR(vdir));
5232 +               return;
5233 +       }
5234 +
5235 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
5236 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
5237 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
5238 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
5239 +               p.deblk = vdir->vd_deblk[ul];
5240 +               o = p.deblk;
5241 +               dpri("[%lu]: %p\n", ul, o);
5242 +       }
5243 +}
5244 +
5245 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
5246 +                       struct dentry *wh)
5247 +{
5248 +       char *n = NULL;
5249 +       int l = 0;
5250 +
5251 +       if (!inode || IS_ERR(inode)) {
5252 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
5253 +               return -1;
5254 +       }
5255 +
5256 +       /* the type of i_blocks depends upon CONFIG_LSF */
5257 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
5258 +                    && sizeof(inode->i_blocks) != sizeof(u64));
5259 +       if (wh) {
5260 +               n = (void *)wh->d_name.name;
5261 +               l = wh->d_name.len;
5262 +       }
5263 +
5264 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
5265 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
5266 +            bindex, inode,
5267 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
5268 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
5269 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
5270 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
5271 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
5272 +            inode->i_state, inode->i_flags, inode->i_version,
5273 +            inode->i_generation,
5274 +            l ? ", wh " : "", l, n);
5275 +       return 0;
5276 +}
5277 +
5278 +void au_dpri_inode(struct inode *inode)
5279 +{
5280 +       struct au_iinfo *iinfo;
5281 +       aufs_bindex_t bindex;
5282 +       int err, hn;
5283 +
5284 +       err = do_pri_inode(-1, inode, -1, NULL);
5285 +       if (err || !au_test_aufs(inode->i_sb))
5286 +               return;
5287 +
5288 +       iinfo = au_ii(inode);
5289 +       if (!iinfo)
5290 +               return;
5291 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
5292 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode));
5293 +       if (iinfo->ii_bstart < 0)
5294 +               return;
5295 +       hn = 0;
5296 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
5297 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
5298 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
5299 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
5300 +       }
5301 +}
5302 +
5303 +void au_dpri_dalias(struct inode *inode)
5304 +{
5305 +       struct dentry *d;
5306 +
5307 +       spin_lock(&inode->i_lock);
5308 +       list_for_each_entry(d, &inode->i_dentry, d_alias)
5309 +               au_dpri_dentry(d);
5310 +       spin_unlock(&inode->i_lock);
5311 +}
5312 +
5313 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
5314 +{
5315 +       struct dentry *wh = NULL;
5316 +       int hn;
5317 +
5318 +       if (!dentry || IS_ERR(dentry)) {
5319 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
5320 +               return -1;
5321 +       }
5322 +       /* do not call dget_parent() here */
5323 +       /* note: access d_xxx without d_lock */
5324 +       dpri("d%d: %.*s?/%.*s, %s, cnt %d, flags 0x%x\n",
5325 +            bindex,
5326 +            AuDLNPair(dentry->d_parent), AuDLNPair(dentry),
5327 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
5328 +            dentry->d_count, dentry->d_flags);
5329 +       hn = -1;
5330 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
5331 +               struct au_iinfo *iinfo = au_ii(dentry->d_inode);
5332 +               if (iinfo) {
5333 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
5334 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
5335 +               }
5336 +       }
5337 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
5338 +       return 0;
5339 +}
5340 +
5341 +void au_dpri_dentry(struct dentry *dentry)
5342 +{
5343 +       struct au_dinfo *dinfo;
5344 +       aufs_bindex_t bindex;
5345 +       int err;
5346 +       struct au_hdentry *hdp;
5347 +
5348 +       err = do_pri_dentry(-1, dentry);
5349 +       if (err || !au_test_aufs(dentry->d_sb))
5350 +               return;
5351 +
5352 +       dinfo = au_di(dentry);
5353 +       if (!dinfo)
5354 +               return;
5355 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d\n",
5356 +            dinfo->di_bstart, dinfo->di_bend,
5357 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry));
5358 +       if (dinfo->di_bstart < 0)
5359 +               return;
5360 +       hdp = dinfo->di_hdentry;
5361 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
5362 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
5363 +}
5364 +
5365 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
5366 +{
5367 +       char a[32];
5368 +
5369 +       if (!file || IS_ERR(file)) {
5370 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
5371 +               return -1;
5372 +       }
5373 +       a[0] = 0;
5374 +       if (bindex < 0
5375 +           && file->f_dentry
5376 +           && au_test_aufs(file->f_dentry->d_sb)
5377 +           && au_fi(file))
5378 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
5379 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
5380 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
5381 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
5382 +            file->f_version, file->f_pos, a);
5383 +       if (file->f_dentry)
5384 +               do_pri_dentry(bindex, file->f_dentry);
5385 +       return 0;
5386 +}
5387 +
5388 +void au_dpri_file(struct file *file)
5389 +{
5390 +       struct au_finfo *finfo;
5391 +       struct au_fidir *fidir;
5392 +       struct au_hfile *hfile;
5393 +       aufs_bindex_t bindex;
5394 +       int err;
5395 +
5396 +       err = do_pri_file(-1, file);
5397 +       if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
5398 +               return;
5399 +
5400 +       finfo = au_fi(file);
5401 +       if (!finfo)
5402 +               return;
5403 +       if (finfo->fi_btop < 0)
5404 +               return;
5405 +       fidir = finfo->fi_hdir;
5406 +       if (!fidir)
5407 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
5408 +       else
5409 +               for (bindex = finfo->fi_btop;
5410 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
5411 +                    bindex++) {
5412 +                       hfile = fidir->fd_hfile + bindex;
5413 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
5414 +               }
5415 +}
5416 +
5417 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
5418 +{
5419 +       struct vfsmount *mnt;
5420 +       struct super_block *sb;
5421 +
5422 +       if (!br || IS_ERR(br))
5423 +               goto out;
5424 +       mnt = br->br_mnt;
5425 +       if (!mnt || IS_ERR(mnt))
5426 +               goto out;
5427 +       sb = mnt->mnt_sb;
5428 +       if (!sb || IS_ERR(sb))
5429 +               goto out;
5430 +
5431 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
5432 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
5433 +            "xino %d\n",
5434 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
5435 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
5436 +            sb->s_flags, sb->s_count,
5437 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
5438 +       return 0;
5439 +
5440 +out:
5441 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
5442 +       return -1;
5443 +}
5444 +
5445 +void au_dpri_sb(struct super_block *sb)
5446 +{
5447 +       struct au_sbinfo *sbinfo;
5448 +       aufs_bindex_t bindex;
5449 +       int err;
5450 +       /* to reuduce stack size */
5451 +       struct {
5452 +               struct vfsmount mnt;
5453 +               struct au_branch fake;
5454 +       } *a;
5455 +
5456 +       /* this function can be called from magic sysrq */
5457 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
5458 +       if (unlikely(!a)) {
5459 +               dpri("no memory\n");
5460 +               return;
5461 +       }
5462 +
5463 +       a->mnt.mnt_sb = sb;
5464 +       a->fake.br_perm = 0;
5465 +       a->fake.br_mnt = &a->mnt;
5466 +       a->fake.br_xino.xi_file = NULL;
5467 +       atomic_set(&a->fake.br_count, 0);
5468 +       smp_mb(); /* atomic_set */
5469 +       err = do_pri_br(-1, &a->fake);
5470 +       kfree(a);
5471 +       dpri("dev 0x%x\n", sb->s_dev);
5472 +       if (err || !au_test_aufs(sb))
5473 +               return;
5474 +
5475 +       sbinfo = au_sbi(sb);
5476 +       if (!sbinfo)
5477 +               return;
5478 +       dpri("nw %d, gen %u, kobj %d\n",
5479 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
5480 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
5481 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
5482 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
5483 +}
5484 +
5485 +/* ---------------------------------------------------------------------- */
5486 +
5487 +void au_dbg_sleep_jiffy(int jiffy)
5488 +{
5489 +       while (jiffy)
5490 +               jiffy = schedule_timeout_uninterruptible(jiffy);
5491 +}
5492 +
5493 +void au_dbg_iattr(struct iattr *ia)
5494 +{
5495 +#define AuBit(name)    if (ia->ia_valid & ATTR_ ## name) \
5496 +                               dpri(#name "\n")
5497 +       AuBit(MODE);
5498 +       AuBit(UID);
5499 +       AuBit(GID);
5500 +       AuBit(SIZE);
5501 +       AuBit(ATIME);
5502 +       AuBit(MTIME);
5503 +       AuBit(CTIME);
5504 +       AuBit(ATIME_SET);
5505 +       AuBit(MTIME_SET);
5506 +       AuBit(FORCE);
5507 +       AuBit(ATTR_FLAG);
5508 +       AuBit(KILL_SUID);
5509 +       AuBit(KILL_SGID);
5510 +       AuBit(FILE);
5511 +       AuBit(KILL_PRIV);
5512 +       AuBit(OPEN);
5513 +       AuBit(TIMES_SET);
5514 +#undef AuBit
5515 +       dpri("ia_file %p\n", ia->ia_file);
5516 +}
5517 +
5518 +/* ---------------------------------------------------------------------- */
5519 +
5520 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
5521 +{
5522 +       struct inode *h_inode, *inode = dentry->d_inode;
5523 +       struct dentry *h_dentry;
5524 +       aufs_bindex_t bindex, bend, bi;
5525 +
5526 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
5527 +               return;
5528 +
5529 +       bend = au_dbend(dentry);
5530 +       bi = au_ibend(inode);
5531 +       if (bi < bend)
5532 +               bend = bi;
5533 +       bindex = au_dbstart(dentry);
5534 +       bi = au_ibstart(inode);
5535 +       if (bi > bindex)
5536 +               bindex = bi;
5537 +
5538 +       for (; bindex <= bend; bindex++) {
5539 +               h_dentry = au_h_dptr(dentry, bindex);
5540 +               if (!h_dentry)
5541 +                       continue;
5542 +               h_inode = au_h_iptr(inode, bindex);
5543 +               if (unlikely(h_inode != h_dentry->d_inode)) {
5544 +                       int old = au_debug_test();
5545 +                       if (!old)
5546 +                               au_debug(1);
5547 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
5548 +                       AuDbgDentry(dentry);
5549 +                       AuDbgInode(inode);
5550 +                       if (!old)
5551 +                               au_debug(0);
5552 +                       BUG();
5553 +               }
5554 +       }
5555 +}
5556 +
5557 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
5558 +{
5559 +       struct dentry *parent;
5560 +
5561 +       parent = dget_parent(dentry);
5562 +       AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
5563 +       AuDebugOn(IS_ROOT(dentry));
5564 +       AuDebugOn(au_digen_test(parent, sigen));
5565 +       dput(parent);
5566 +}
5567 +
5568 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
5569 +{
5570 +       struct dentry *parent;
5571 +       struct inode *inode;
5572 +
5573 +       parent = dget_parent(dentry);
5574 +       inode = dentry->d_inode;
5575 +       AuDebugOn(inode && S_ISDIR(dentry->d_inode->i_mode));
5576 +       AuDebugOn(au_digen_test(parent, sigen));
5577 +       dput(parent);
5578 +}
5579 +
5580 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
5581 +{
5582 +       int err, i, j;
5583 +       struct au_dcsub_pages dpages;
5584 +       struct au_dpage *dpage;
5585 +       struct dentry **dentries;
5586 +
5587 +       err = au_dpages_init(&dpages, GFP_NOFS);
5588 +       AuDebugOn(err);
5589 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
5590 +       AuDebugOn(err);
5591 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
5592 +               dpage = dpages.dpages + i;
5593 +               dentries = dpage->dentries;
5594 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
5595 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
5596 +       }
5597 +       au_dpages_free(&dpages);
5598 +}
5599 +
5600 +void au_dbg_verify_kthread(void)
5601 +{
5602 +       if (au_wkq_test()) {
5603 +               au_dbg_blocked();
5604 +               /*
5605 +                * It may be recursive, but udba=notify between two aufs mounts,
5606 +                * where a single ro branch is shared, is not a problem.
5607 +                */
5608 +               /* WARN_ON(1); */
5609 +       }
5610 +}
5611 +
5612 +/* ---------------------------------------------------------------------- */
5613 +
5614 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
5615 +{
5616 +#ifdef AuForceNoPlink
5617 +       au_opt_clr(sbinfo->si_mntflags, PLINK);
5618 +#endif
5619 +#ifdef AuForceNoXino
5620 +       au_opt_clr(sbinfo->si_mntflags, XINO);
5621 +#endif
5622 +#ifdef AuForceNoRefrof
5623 +       au_opt_clr(sbinfo->si_mntflags, REFROF);
5624 +#endif
5625 +#ifdef AuForceHnotify
5626 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_HNOTIFY);
5627 +#endif
5628 +#ifdef AuForceRd0
5629 +       sbinfo->si_rdblk = 0;
5630 +       sbinfo->si_rdhash = 0;
5631 +#endif
5632 +}
5633 +
5634 +int __init au_debug_init(void)
5635 +{
5636 +       aufs_bindex_t bindex;
5637 +       struct au_vdir_destr destr;
5638 +
5639 +       bindex = -1;
5640 +       AuDebugOn(bindex >= 0);
5641 +
5642 +       destr.len = -1;
5643 +       AuDebugOn(destr.len < NAME_MAX);
5644 +
5645 +#ifdef CONFIG_4KSTACKS
5646 +       pr_warning("CONFIG_4KSTACKS is defined.\n");
5647 +#endif
5648 +
5649 +#ifdef AuForceNoBrs
5650 +       sysaufs_brs = 0;
5651 +#endif
5652 +
5653 +       return 0;
5654 +}
5655 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
5656 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
5657 +++ linux/fs/aufs/debug.h       2011-08-24 13:30:24.731313534 +0200
5658 @@ -0,0 +1,252 @@
5659 +/*
5660 + * Copyright (C) 2005-2011 Junjiro R. Okajima
5661 + *
5662 + * This program, aufs is free software; you can redistribute it and/or modify
5663 + * it under the terms of the GNU General Public License as published by
5664 + * the Free Software Foundation; either version 2 of the License, or
5665 + * (at your option) any later version.
5666 + *
5667 + * This program is distributed in the hope that it will be useful,
5668 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5669 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5670 + * GNU General Public License for more details.
5671 + *
5672 + * You should have received a copy of the GNU General Public License
5673 + * along with this program; if not, write to the Free Software
5674 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5675 + */
5676 +
5677 +/*
5678 + * debug print functions
5679 + */
5680 +
5681 +#ifndef __AUFS_DEBUG_H__
5682 +#define __AUFS_DEBUG_H__
5683 +
5684 +#ifdef __KERNEL__
5685 +
5686 +#include <asm/system.h>
5687 +#include <linux/bug.h>
5688 +/* #include <linux/err.h> */
5689 +#include <linux/init.h>
5690 +#include <linux/module.h>
5691 +#include <linux/kallsyms.h>
5692 +/* #include <linux/kernel.h> */
5693 +#include <linux/delay.h>
5694 +/* #include <linux/kd.h> */
5695 +#include <linux/sysrq.h>
5696 +#include <linux/aufs_type.h>
5697 +
5698 +#include <asm/system.h>
5699 +
5700 +#ifdef CONFIG_AUFS_DEBUG
5701 +#define AuDebugOn(a)           BUG_ON(a)
5702 +
5703 +/* module parameter */
5704 +extern int aufs_debug;
5705 +static inline void au_debug(int n)
5706 +{
5707 +       aufs_debug = n;
5708 +       smp_mb();
5709 +}
5710 +
5711 +static inline int au_debug_test(void)
5712 +{
5713 +       return aufs_debug;
5714 +}
5715 +#else
5716 +#define AuDebugOn(a)           do {} while (0)
5717 +AuStubVoid(au_debug, int n)
5718 +AuStubInt0(au_debug_test, void)
5719 +#endif /* CONFIG_AUFS_DEBUG */
5720 +
5721 +/* ---------------------------------------------------------------------- */
5722 +
5723 +/* debug print */
5724 +
5725 +#define AuDbg(fmt, ...) do { \
5726 +       if (au_debug_test()) \
5727 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
5728 +} while (0)
5729 +#define AuLabel(l)             AuDbg(#l "\n")
5730 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
5731 +#define AuWarn1(fmt, ...) do { \
5732 +       static unsigned char _c; \
5733 +       if (!_c++) \
5734 +               pr_warning(fmt, ##__VA_ARGS__); \
5735 +} while (0)
5736 +
5737 +#define AuErr1(fmt, ...) do { \
5738 +       static unsigned char _c; \
5739 +       if (!_c++) \
5740 +               pr_err(fmt, ##__VA_ARGS__); \
5741 +} while (0)
5742 +
5743 +#define AuIOErr1(fmt, ...) do { \
5744 +       static unsigned char _c; \
5745 +       if (!_c++) \
5746 +               AuIOErr(fmt, ##__VA_ARGS__); \
5747 +} while (0)
5748 +
5749 +#define AuUnsupportMsg "This operation is not supported." \
5750 +                       " Please report this application to aufs-users ML."
5751 +#define AuUnsupport(fmt, ...) do { \
5752 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
5753 +       dump_stack(); \
5754 +} while (0)
5755 +
5756 +#define AuTraceErr(e) do { \
5757 +       if (unlikely((e) < 0)) \
5758 +               AuDbg("err %d\n", (int)(e)); \
5759 +} while (0)
5760 +
5761 +#define AuTraceErrPtr(p) do { \
5762 +       if (IS_ERR(p)) \
5763 +               AuDbg("err %ld\n", PTR_ERR(p)); \
5764 +} while (0)
5765 +
5766 +/* dirty macros for debug print, use with "%.*s" and caution */
5767 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
5768 +#define AuDLNPair(d)           AuLNPair(&(d)->d_name)
5769 +
5770 +/* ---------------------------------------------------------------------- */
5771 +
5772 +struct au_sbinfo;
5773 +struct au_finfo;
5774 +struct dentry;
5775 +#ifdef CONFIG_AUFS_DEBUG
5776 +extern char *au_plevel;
5777 +struct au_nhash;
5778 +void au_dpri_whlist(struct au_nhash *whlist);
5779 +struct au_vdir;
5780 +void au_dpri_vdir(struct au_vdir *vdir);
5781 +struct inode;
5782 +void au_dpri_inode(struct inode *inode);
5783 +void au_dpri_dalias(struct inode *inode);
5784 +void au_dpri_dentry(struct dentry *dentry);
5785 +struct file;
5786 +void au_dpri_file(struct file *filp);
5787 +struct super_block;
5788 +void au_dpri_sb(struct super_block *sb);
5789 +
5790 +void au_dbg_sleep_jiffy(int jiffy);
5791 +struct iattr;
5792 +void au_dbg_iattr(struct iattr *ia);
5793 +
5794 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
5795 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
5796 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen);
5797 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen);
5798 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
5799 +void au_dbg_verify_kthread(void);
5800 +
5801 +int __init au_debug_init(void);
5802 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
5803 +#define AuDbgWhlist(w) do { \
5804 +       AuDbg(#w "\n"); \
5805 +       au_dpri_whlist(w); \
5806 +} while (0)
5807 +
5808 +#define AuDbgVdir(v) do { \
5809 +       AuDbg(#v "\n"); \
5810 +       au_dpri_vdir(v); \
5811 +} while (0)
5812 +
5813 +#define AuDbgInode(i) do { \
5814 +       AuDbg(#i "\n"); \
5815 +       au_dpri_inode(i); \
5816 +} while (0)
5817 +
5818 +#define AuDbgDAlias(i) do { \
5819 +       AuDbg(#i "\n"); \
5820 +       au_dpri_dalias(i); \
5821 +} while (0)
5822 +
5823 +#define AuDbgDentry(d) do { \
5824 +       AuDbg(#d "\n"); \
5825 +       au_dpri_dentry(d); \
5826 +} while (0)
5827 +
5828 +#define AuDbgFile(f) do { \
5829 +       AuDbg(#f "\n"); \
5830 +       au_dpri_file(f); \
5831 +} while (0)
5832 +
5833 +#define AuDbgSb(sb) do { \
5834 +       AuDbg(#sb "\n"); \
5835 +       au_dpri_sb(sb); \
5836 +} while (0)
5837 +
5838 +#define AuDbgSleep(sec) do { \
5839 +       AuDbg("sleep %d sec\n", sec); \
5840 +       ssleep(sec); \
5841 +} while (0)
5842 +
5843 +#define AuDbgSleepJiffy(jiffy) do { \
5844 +       AuDbg("sleep %d jiffies\n", jiffy); \
5845 +       au_dbg_sleep_jiffy(jiffy); \
5846 +} while (0)
5847 +
5848 +#define AuDbgIAttr(ia) do { \
5849 +       AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
5850 +       au_dbg_iattr(ia); \
5851 +} while (0)
5852 +
5853 +#define AuDbgSym(addr) do {                            \
5854 +       char sym[KSYM_SYMBOL_LEN];                      \
5855 +       sprint_symbol(sym, (unsigned long)addr);        \
5856 +       AuDbg("%s\n", sym);                             \
5857 +} while (0)
5858 +
5859 +#define AuInfoSym(addr) do {                           \
5860 +       char sym[KSYM_SYMBOL_LEN];                      \
5861 +       sprint_symbol(sym, (unsigned long)addr);        \
5862 +       AuInfo("%s\n", sym);                            \
5863 +} while (0)
5864 +#else
5865 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
5866 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
5867 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
5868 +          unsigned int sigen)
5869 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
5870 +AuStubVoid(au_dbg_verify_kthread, void)
5871 +AuStubInt0(__init au_debug_init, void)
5872 +AuStubVoid(au_debug_sbinfo_init, struct au_sbinfo *sbinfo)
5873 +
5874 +#define AuDbgWhlist(w)         do {} while (0)
5875 +#define AuDbgVdir(v)           do {} while (0)
5876 +#define AuDbgInode(i)          do {} while (0)
5877 +#define AuDbgDAlias(i)         do {} while (0)
5878 +#define AuDbgDentry(d)         do {} while (0)
5879 +#define AuDbgFile(f)           do {} while (0)
5880 +#define AuDbgSb(sb)            do {} while (0)
5881 +#define AuDbgSleep(sec)                do {} while (0)
5882 +#define AuDbgSleepJiffy(jiffy) do {} while (0)
5883 +#define AuDbgIAttr(ia)         do {} while (0)
5884 +#define AuDbgSym(addr)         do {} while (0)
5885 +#define AuInfoSym(addr)                do {} while (0)
5886 +#endif /* CONFIG_AUFS_DEBUG */
5887 +
5888 +/* ---------------------------------------------------------------------- */
5889 +
5890 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
5891 +int __init au_sysrq_init(void);
5892 +void au_sysrq_fin(void);
5893 +
5894 +#ifdef CONFIG_HW_CONSOLE
5895 +#define au_dbg_blocked() do { \
5896 +       WARN_ON(1); \
5897 +       handle_sysrq('w'); \
5898 +} while (0)
5899 +#else
5900 +AuStubVoid(au_dbg_blocked, void)
5901 +#endif
5902 +
5903 +#else
5904 +AuStubInt0(__init au_sysrq_init, void)
5905 +AuStubVoid(au_sysrq_fin, void)
5906 +AuStubVoid(au_dbg_blocked, void)
5907 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
5908 +
5909 +#endif /* __KERNEL__ */
5910 +#endif /* __AUFS_DEBUG_H__ */
5911 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
5912 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
5913 +++ linux/fs/aufs/dentry.c      2011-08-24 13:30:24.731313534 +0200
5914 @@ -0,0 +1,1140 @@
5915 +/*
5916 + * Copyright (C) 2005-2011 Junjiro R. Okajima
5917 + *
5918 + * This program, aufs is free software; you can redistribute it and/or modify
5919 + * it under the terms of the GNU General Public License as published by
5920 + * the Free Software Foundation; either version 2 of the License, or
5921 + * (at your option) any later version.
5922 + *
5923 + * This program is distributed in the hope that it will be useful,
5924 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5925 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5926 + * GNU General Public License for more details.
5927 + *
5928 + * You should have received a copy of the GNU General Public License
5929 + * along with this program; if not, write to the Free Software
5930 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5931 + */
5932 +
5933 +/*
5934 + * lookup and dentry operations
5935 + */
5936 +
5937 +#include <linux/namei.h>
5938 +#include "aufs.h"
5939 +
5940 +static void au_h_nd(struct nameidata *h_nd, struct nameidata *nd)
5941 +{
5942 +       if (nd) {
5943 +               *h_nd = *nd;
5944 +
5945 +               /*
5946 +                * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
5947 +                * due to whiteout and branch permission.
5948 +                */
5949 +               h_nd->flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
5950 +                                | LOOKUP_FOLLOW | LOOKUP_EXCL);
5951 +               /* unnecessary? */
5952 +               h_nd->intent.open.file = NULL;
5953 +       } else
5954 +               memset(h_nd, 0, sizeof(*h_nd));
5955 +}
5956 +
5957 +struct au_lkup_one_args {
5958 +       struct dentry **errp;
5959 +       struct qstr *name;
5960 +       struct dentry *h_parent;
5961 +       struct au_branch *br;
5962 +       struct nameidata *nd;
5963 +};
5964 +
5965 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
5966 +                          struct au_branch *br, struct nameidata *nd)
5967 +{
5968 +       struct dentry *h_dentry;
5969 +       int err;
5970 +       struct nameidata h_nd;
5971 +
5972 +       if (au_test_fs_null_nd(h_parent->d_sb))
5973 +               return vfsub_lookup_one_len(name->name, h_parent, name->len);
5974 +
5975 +       au_h_nd(&h_nd, nd);
5976 +       h_nd.path.dentry = h_parent;
5977 +       h_nd.path.mnt = br->br_mnt;
5978 +
5979 +       err = vfsub_name_hash(name->name, &h_nd.last, name->len);
5980 +       h_dentry = ERR_PTR(err);
5981 +       if (!err) {
5982 +               path_get(&h_nd.path);
5983 +               h_dentry = vfsub_lookup_hash(&h_nd);
5984 +               path_put(&h_nd.path);
5985 +       }
5986 +
5987 +       AuTraceErrPtr(h_dentry);
5988 +       return h_dentry;
5989 +}
5990 +
5991 +static void au_call_lkup_one(void *args)
5992 +{
5993 +       struct au_lkup_one_args *a = args;
5994 +       *a->errp = au_lkup_one(a->name, a->h_parent, a->br, a->nd);
5995 +}
5996 +
5997 +#define AuLkup_ALLOW_NEG       1
5998 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
5999 +#define au_fset_lkup(flags, name) \
6000 +       do { (flags) |= AuLkup_##name; } while (0)
6001 +#define au_fclr_lkup(flags, name) \
6002 +       do { (flags) &= ~AuLkup_##name; } while (0)
6003 +
6004 +struct au_do_lookup_args {
6005 +       unsigned int            flags;
6006 +       mode_t                  type;
6007 +       struct nameidata        *nd;
6008 +};
6009 +
6010 +/*
6011 + * returns positive/negative dentry, NULL or an error.
6012 + * NULL means whiteout-ed or not-found.
6013 + */
6014 +static struct dentry*
6015 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
6016 +            aufs_bindex_t bindex, struct qstr *wh_name,
6017 +            struct au_do_lookup_args *args)
6018 +{
6019 +       struct dentry *h_dentry;
6020 +       struct inode *h_inode, *inode;
6021 +       struct au_branch *br;
6022 +       int wh_found, opq;
6023 +       unsigned char wh_able;
6024 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
6025 +
6026 +       wh_found = 0;
6027 +       br = au_sbr(dentry->d_sb, bindex);
6028 +       wh_able = !!au_br_whable(br->br_perm);
6029 +       if (wh_able)
6030 +               wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6031 +       h_dentry = ERR_PTR(wh_found);
6032 +       if (!wh_found)
6033 +               goto real_lookup;
6034 +       if (unlikely(wh_found < 0))
6035 +               goto out;
6036 +
6037 +       /* We found a whiteout */
6038 +       /* au_set_dbend(dentry, bindex); */
6039 +       au_set_dbwh(dentry, bindex);
6040 +       if (!allow_neg)
6041 +               return NULL; /* success */
6042 +
6043 +real_lookup:
6044 +       h_dentry = au_lkup_one(&dentry->d_name, h_parent, br, args->nd);
6045 +       if (IS_ERR(h_dentry))
6046 +               goto out;
6047 +
6048 +       h_inode = h_dentry->d_inode;
6049 +       if (!h_inode) {
6050 +               if (!allow_neg)
6051 +                       goto out_neg;
6052 +       } else if (wh_found
6053 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
6054 +               goto out_neg;
6055 +
6056 +       if (au_dbend(dentry) <= bindex)
6057 +               au_set_dbend(dentry, bindex);
6058 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
6059 +               au_set_dbstart(dentry, bindex);
6060 +       au_set_h_dptr(dentry, bindex, h_dentry);
6061 +
6062 +       inode = dentry->d_inode;
6063 +       if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
6064 +           || (inode && !S_ISDIR(inode->i_mode)))
6065 +               goto out; /* success */
6066 +
6067 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
6068 +       opq = au_diropq_test(h_dentry, br);
6069 +       mutex_unlock(&h_inode->i_mutex);
6070 +       if (opq > 0)
6071 +               au_set_dbdiropq(dentry, bindex);
6072 +       else if (unlikely(opq < 0)) {
6073 +               au_set_h_dptr(dentry, bindex, NULL);
6074 +               h_dentry = ERR_PTR(opq);
6075 +       }
6076 +       goto out;
6077 +
6078 +out_neg:
6079 +       dput(h_dentry);
6080 +       h_dentry = NULL;
6081 +out:
6082 +       return h_dentry;
6083 +}
6084 +
6085 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
6086 +{
6087 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
6088 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6089 +               return -EPERM;
6090 +       return 0;
6091 +}
6092 +
6093 +/*
6094 + * returns the number of lower positive dentries,
6095 + * otherwise an error.
6096 + * can be called at unlinking with @type is zero.
6097 + */
6098 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
6099 +                  struct nameidata *nd)
6100 +{
6101 +       int npositive, err;
6102 +       aufs_bindex_t bindex, btail, bdiropq;
6103 +       unsigned char isdir;
6104 +       struct qstr whname;
6105 +       struct au_do_lookup_args args = {
6106 +               .flags  = 0,
6107 +               .type   = type,
6108 +               .nd     = nd
6109 +       };
6110 +       const struct qstr *name = &dentry->d_name;
6111 +       struct dentry *parent;
6112 +       struct inode *inode;
6113 +
6114 +       err = au_test_shwh(dentry->d_sb, name);
6115 +       if (unlikely(err))
6116 +               goto out;
6117 +
6118 +       err = au_wh_name_alloc(&whname, name);
6119 +       if (unlikely(err))
6120 +               goto out;
6121 +
6122 +       inode = dentry->d_inode;
6123 +       isdir = !!(inode && S_ISDIR(inode->i_mode));
6124 +       if (!type)
6125 +               au_fset_lkup(args.flags, ALLOW_NEG);
6126 +
6127 +       npositive = 0;
6128 +       parent = dget_parent(dentry);
6129 +       btail = au_dbtaildir(parent);
6130 +       for (bindex = bstart; bindex <= btail; bindex++) {
6131 +               struct dentry *h_parent, *h_dentry;
6132 +               struct inode *h_inode, *h_dir;
6133 +
6134 +               h_dentry = au_h_dptr(dentry, bindex);
6135 +               if (h_dentry) {
6136 +                       if (h_dentry->d_inode)
6137 +                               npositive++;
6138 +                       if (type != S_IFDIR)
6139 +                               break;
6140 +                       continue;
6141 +               }
6142 +               h_parent = au_h_dptr(parent, bindex);
6143 +               if (!h_parent)
6144 +                       continue;
6145 +               h_dir = h_parent->d_inode;
6146 +               if (!h_dir || !S_ISDIR(h_dir->i_mode))
6147 +                       continue;
6148 +
6149 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
6150 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
6151 +                                       &args);
6152 +               mutex_unlock(&h_dir->i_mutex);
6153 +               err = PTR_ERR(h_dentry);
6154 +               if (IS_ERR(h_dentry))
6155 +                       goto out_parent;
6156 +               au_fclr_lkup(args.flags, ALLOW_NEG);
6157 +
6158 +               if (au_dbwh(dentry) >= 0)
6159 +                       break;
6160 +               if (!h_dentry)
6161 +                       continue;
6162 +               h_inode = h_dentry->d_inode;
6163 +               if (!h_inode)
6164 +                       continue;
6165 +               npositive++;
6166 +               if (!args.type)
6167 +                       args.type = h_inode->i_mode & S_IFMT;
6168 +               if (args.type != S_IFDIR)
6169 +                       break;
6170 +               else if (isdir) {
6171 +                       /* the type of lower may be different */
6172 +                       bdiropq = au_dbdiropq(dentry);
6173 +                       if (bdiropq >= 0 && bdiropq <= bindex)
6174 +                               break;
6175 +               }
6176 +       }
6177 +
6178 +       if (npositive) {
6179 +               AuLabel(positive);
6180 +               au_update_dbstart(dentry);
6181 +       }
6182 +       err = npositive;
6183 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
6184 +                    && au_dbstart(dentry) < 0)) {
6185 +               err = -EIO;
6186 +               AuIOErr("both of real entry and whiteout found, %.*s, err %d\n",
6187 +                       AuDLNPair(dentry), err);
6188 +       }
6189 +
6190 +out_parent:
6191 +       dput(parent);
6192 +       kfree(whname.name);
6193 +out:
6194 +       return err;
6195 +}
6196 +
6197 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
6198 +                              struct au_branch *br)
6199 +{
6200 +       struct dentry *dentry;
6201 +       int wkq_err;
6202 +
6203 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
6204 +               dentry = au_lkup_one(name, parent, br, /*nd*/NULL);
6205 +       else {
6206 +               struct au_lkup_one_args args = {
6207 +                       .errp           = &dentry,
6208 +                       .name           = name,
6209 +                       .h_parent       = parent,
6210 +                       .br             = br,
6211 +                       .nd             = NULL
6212 +               };
6213 +
6214 +               wkq_err = au_wkq_wait(au_call_lkup_one, &args);
6215 +               if (unlikely(wkq_err))
6216 +                       dentry = ERR_PTR(wkq_err);
6217 +       }
6218 +
6219 +       return dentry;
6220 +}
6221 +
6222 +/*
6223 + * lookup @dentry on @bindex which should be negative.
6224 + */
6225 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex)
6226 +{
6227 +       int err;
6228 +       struct dentry *parent, *h_parent, *h_dentry;
6229 +
6230 +       parent = dget_parent(dentry);
6231 +       h_parent = au_h_dptr(parent, bindex);
6232 +       h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent,
6233 +                                  au_sbr(dentry->d_sb, bindex));
6234 +       err = PTR_ERR(h_dentry);
6235 +       if (IS_ERR(h_dentry))
6236 +               goto out;
6237 +       if (unlikely(h_dentry->d_inode)) {
6238 +               err = -EIO;
6239 +               AuIOErr("%.*s should be negative on b%d.\n",
6240 +                       AuDLNPair(h_dentry), bindex);
6241 +               dput(h_dentry);
6242 +               goto out;
6243 +       }
6244 +
6245 +       err = 0;
6246 +       if (bindex < au_dbstart(dentry))
6247 +               au_set_dbstart(dentry, bindex);
6248 +       if (au_dbend(dentry) < bindex)
6249 +               au_set_dbend(dentry, bindex);
6250 +       au_set_h_dptr(dentry, bindex, h_dentry);
6251 +
6252 +out:
6253 +       dput(parent);
6254 +       return err;
6255 +}
6256 +
6257 +/* ---------------------------------------------------------------------- */
6258 +
6259 +/* subset of struct inode */
6260 +struct au_iattr {
6261 +       unsigned long           i_ino;
6262 +       /* unsigned int         i_nlink; */
6263 +       uid_t                   i_uid;
6264 +       gid_t                   i_gid;
6265 +       u64                     i_version;
6266 +/*
6267 +       loff_t                  i_size;
6268 +       blkcnt_t                i_blocks;
6269 +*/
6270 +       umode_t                 i_mode;
6271 +};
6272 +
6273 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
6274 +{
6275 +       ia->i_ino = h_inode->i_ino;
6276 +       /* ia->i_nlink = h_inode->i_nlink; */
6277 +       ia->i_uid = h_inode->i_uid;
6278 +       ia->i_gid = h_inode->i_gid;
6279 +       ia->i_version = h_inode->i_version;
6280 +/*
6281 +       ia->i_size = h_inode->i_size;
6282 +       ia->i_blocks = h_inode->i_blocks;
6283 +*/
6284 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
6285 +}
6286 +
6287 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
6288 +{
6289 +       return ia->i_ino != h_inode->i_ino
6290 +               /* || ia->i_nlink != h_inode->i_nlink */
6291 +               || ia->i_uid != h_inode->i_uid
6292 +               || ia->i_gid != h_inode->i_gid
6293 +               || ia->i_version != h_inode->i_version
6294 +/*
6295 +               || ia->i_size != h_inode->i_size
6296 +               || ia->i_blocks != h_inode->i_blocks
6297 +*/
6298 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
6299 +}
6300 +
6301 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
6302 +                             struct au_branch *br)
6303 +{
6304 +       int err;
6305 +       struct au_iattr ia;
6306 +       struct inode *h_inode;
6307 +       struct dentry *h_d;
6308 +       struct super_block *h_sb;
6309 +
6310 +       err = 0;
6311 +       memset(&ia, -1, sizeof(ia));
6312 +       h_sb = h_dentry->d_sb;
6313 +       h_inode = h_dentry->d_inode;
6314 +       if (h_inode)
6315 +               au_iattr_save(&ia, h_inode);
6316 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
6317 +               /* nfs d_revalidate may return 0 for negative dentry */
6318 +               /* fuse d_revalidate always return 0 for negative dentry */
6319 +               goto out;
6320 +
6321 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
6322 +       h_d = au_lkup_one(&h_dentry->d_name, h_parent, br, /*nd*/NULL);
6323 +       err = PTR_ERR(h_d);
6324 +       if (IS_ERR(h_d))
6325 +               goto out;
6326 +
6327 +       err = 0;
6328 +       if (unlikely(h_d != h_dentry
6329 +                    || h_d->d_inode != h_inode
6330 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
6331 +               err = au_busy_or_stale();
6332 +       dput(h_d);
6333 +
6334 +out:
6335 +       AuTraceErr(err);
6336 +       return err;
6337 +}
6338 +
6339 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
6340 +               struct dentry *h_parent, struct au_branch *br)
6341 +{
6342 +       int err;
6343 +
6344 +       err = 0;
6345 +       if (udba == AuOpt_UDBA_REVAL
6346 +           && !au_test_fs_remote(h_dentry->d_sb)) {
6347 +               IMustLock(h_dir);
6348 +               err = (h_dentry->d_parent->d_inode != h_dir);
6349 +       } else if (udba != AuOpt_UDBA_NONE)
6350 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
6351 +
6352 +       return err;
6353 +}
6354 +
6355 +/* ---------------------------------------------------------------------- */
6356 +
6357 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
6358 +{
6359 +       int err;
6360 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
6361 +       struct au_hdentry tmp, *p, *q;
6362 +       struct au_dinfo *dinfo;
6363 +       struct super_block *sb;
6364 +
6365 +       DiMustWriteLock(dentry);
6366 +
6367 +       sb = dentry->d_sb;
6368 +       dinfo = au_di(dentry);
6369 +       bend = dinfo->di_bend;
6370 +       bwh = dinfo->di_bwh;
6371 +       bdiropq = dinfo->di_bdiropq;
6372 +       p = dinfo->di_hdentry + dinfo->di_bstart;
6373 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
6374 +               if (!p->hd_dentry)
6375 +                       continue;
6376 +
6377 +               new_bindex = au_br_index(sb, p->hd_id);
6378 +               if (new_bindex == bindex)
6379 +                       continue;
6380 +
6381 +               if (dinfo->di_bwh == bindex)
6382 +                       bwh = new_bindex;
6383 +               if (dinfo->di_bdiropq == bindex)
6384 +                       bdiropq = new_bindex;
6385 +               if (new_bindex < 0) {
6386 +                       au_hdput(p);
6387 +                       p->hd_dentry = NULL;
6388 +                       continue;
6389 +               }
6390 +
6391 +               /* swap two lower dentries, and loop again */
6392 +               q = dinfo->di_hdentry + new_bindex;
6393 +               tmp = *q;
6394 +               *q = *p;
6395 +               *p = tmp;
6396 +               if (tmp.hd_dentry) {
6397 +                       bindex--;
6398 +                       p--;
6399 +               }
6400 +       }
6401 +
6402 +       dinfo->di_bwh = -1;
6403 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
6404 +               dinfo->di_bwh = bwh;
6405 +
6406 +       dinfo->di_bdiropq = -1;
6407 +       if (bdiropq >= 0
6408 +           && bdiropq <= au_sbend(sb)
6409 +           && au_sbr_whable(sb, bdiropq))
6410 +               dinfo->di_bdiropq = bdiropq;
6411 +
6412 +       err = -EIO;
6413 +       dinfo->di_bstart = -1;
6414 +       dinfo->di_bend = -1;
6415 +       bend = au_dbend(parent);
6416 +       p = dinfo->di_hdentry;
6417 +       for (bindex = 0; bindex <= bend; bindex++, p++)
6418 +               if (p->hd_dentry) {
6419 +                       dinfo->di_bstart = bindex;
6420 +                       break;
6421 +               }
6422 +
6423 +       if (dinfo->di_bstart >= 0) {
6424 +               p = dinfo->di_hdentry + bend;
6425 +               for (bindex = bend; bindex >= 0; bindex--, p--)
6426 +                       if (p->hd_dentry) {
6427 +                               dinfo->di_bend = bindex;
6428 +                               err = 0;
6429 +                               break;
6430 +                       }
6431 +       }
6432 +
6433 +       return err;
6434 +}
6435 +
6436 +static void au_do_hide(struct dentry *dentry)
6437 +{
6438 +       struct inode *inode;
6439 +
6440 +       inode = dentry->d_inode;
6441 +       if (inode) {
6442 +               if (!S_ISDIR(inode->i_mode)) {
6443 +                       if (inode->i_nlink && !d_unhashed(dentry))
6444 +                               drop_nlink(inode);
6445 +               } else {
6446 +                       clear_nlink(inode);
6447 +                       /* stop next lookup */
6448 +                       inode->i_flags |= S_DEAD;
6449 +               }
6450 +               smp_mb(); /* necessary? */
6451 +       }
6452 +       d_drop(dentry);
6453 +}
6454 +
6455 +static int au_hide_children(struct dentry *parent)
6456 +{
6457 +       int err, i, j, ndentry;
6458 +       struct au_dcsub_pages dpages;
6459 +       struct au_dpage *dpage;
6460 +       struct dentry *dentry;
6461 +
6462 +       err = au_dpages_init(&dpages, GFP_NOFS);
6463 +       if (unlikely(err))
6464 +               goto out;
6465 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
6466 +       if (unlikely(err))
6467 +               goto out_dpages;
6468 +
6469 +       /* in reverse order */
6470 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
6471 +               dpage = dpages.dpages + i;
6472 +               ndentry = dpage->ndentry;
6473 +               for (j = ndentry - 1; j >= 0; j--) {
6474 +                       dentry = dpage->dentries[j];
6475 +                       if (dentry != parent)
6476 +                               au_do_hide(dentry);
6477 +               }
6478 +       }
6479 +
6480 +out_dpages:
6481 +       au_dpages_free(&dpages);
6482 +out:
6483 +       return err;
6484 +}
6485 +
6486 +static void au_hide(struct dentry *dentry)
6487 +{
6488 +       int err;
6489 +       struct inode *inode;
6490 +
6491 +       AuDbgDentry(dentry);
6492 +       inode = dentry->d_inode;
6493 +       if (inode && S_ISDIR(inode->i_mode)) {
6494 +               /* shrink_dcache_parent(dentry); */
6495 +               err = au_hide_children(dentry);
6496 +               if (unlikely(err))
6497 +                       AuIOErr("%.*s, failed hiding children, ignored %d\n",
6498 +                               AuDLNPair(dentry), err);
6499 +       }
6500 +       au_do_hide(dentry);
6501 +}
6502 +
6503 +/*
6504 + * By adding a dirty branch, a cached dentry may be affected in various ways.
6505 + *
6506 + * a dirty branch is added
6507 + * - on the top of layers
6508 + * - in the middle of layers
6509 + * - to the bottom of layers
6510 + *
6511 + * on the added branch there exists
6512 + * - a whiteout
6513 + * - a diropq
6514 + * - a same named entry
6515 + *   + exist
6516 + *     * negative --> positive
6517 + *     * positive --> positive
6518 + *      - type is unchanged
6519 + *      - type is changed
6520 + *   + doesn't exist
6521 + *     * negative --> negative
6522 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
6523 + * - none
6524 + */
6525 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
6526 +                              struct au_dinfo *tmp)
6527 +{
6528 +       int err;
6529 +       aufs_bindex_t bindex, bend;
6530 +       struct {
6531 +               struct dentry *dentry;
6532 +               struct inode *inode;
6533 +               mode_t mode;
6534 +       } orig_h, tmp_h;
6535 +       struct au_hdentry *hd;
6536 +       struct inode *inode, *h_inode;
6537 +       struct dentry *h_dentry;
6538 +
6539 +       err = 0;
6540 +       AuDebugOn(dinfo->di_bstart < 0);
6541 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
6542 +       orig_h.inode = orig_h.dentry->d_inode;
6543 +       orig_h.mode = 0;
6544 +       if (orig_h.inode)
6545 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
6546 +       memset(&tmp_h, 0, sizeof(tmp_h));
6547 +       if (tmp->di_bstart >= 0) {
6548 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
6549 +               tmp_h.inode = tmp_h.dentry->d_inode;
6550 +               if (tmp_h.inode)
6551 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
6552 +       }
6553 +
6554 +       inode = dentry->d_inode;
6555 +       if (!orig_h.inode) {
6556 +               AuDbg("nagative originally\n");
6557 +               if (inode) {
6558 +                       au_hide(dentry);
6559 +                       goto out;
6560 +               }
6561 +               AuDebugOn(inode);
6562 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6563 +               AuDebugOn(dinfo->di_bdiropq != -1);
6564 +
6565 +               if (!tmp_h.inode) {
6566 +                       AuDbg("negative --> negative\n");
6567 +                       /* should have only one negative lower */
6568 +                       if (tmp->di_bstart >= 0
6569 +                           && tmp->di_bstart < dinfo->di_bstart) {
6570 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
6571 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6572 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
6573 +                               au_di_cp(dinfo, tmp);
6574 +                               hd = tmp->di_hdentry + tmp->di_bstart;
6575 +                               au_set_h_dptr(dentry, tmp->di_bstart,
6576 +                                             dget(hd->hd_dentry));
6577 +                       }
6578 +                       au_dbg_verify_dinode(dentry);
6579 +               } else {
6580 +                       AuDbg("negative --> positive\n");
6581 +                       /*
6582 +                        * similar to the behaviour of creating with bypassing
6583 +                        * aufs.
6584 +                        * unhash it in order to force an error in the
6585 +                        * succeeding create operation.
6586 +                        * we should not set S_DEAD here.
6587 +                        */
6588 +                       d_drop(dentry);
6589 +                       /* au_di_swap(tmp, dinfo); */
6590 +                       au_dbg_verify_dinode(dentry);
6591 +               }
6592 +       } else {
6593 +               AuDbg("positive originally\n");
6594 +               /* inode may be NULL */
6595 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
6596 +               if (!tmp_h.inode) {
6597 +                       AuDbg("positive --> negative\n");
6598 +                       /* or bypassing aufs */
6599 +                       au_hide(dentry);
6600 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
6601 +                               dinfo->di_bwh = tmp->di_bwh;
6602 +                       if (inode)
6603 +                               err = au_refresh_hinode_self(inode);
6604 +                       au_dbg_verify_dinode(dentry);
6605 +               } else if (orig_h.mode == tmp_h.mode) {
6606 +                       AuDbg("positive --> positive, same type\n");
6607 +                       if (!S_ISDIR(orig_h.mode)
6608 +                           && dinfo->di_bstart > tmp->di_bstart) {
6609 +                               /*
6610 +                                * similar to the behaviour of removing and
6611 +                                * creating.
6612 +                                */
6613 +                               au_hide(dentry);
6614 +                               if (inode)
6615 +                                       err = au_refresh_hinode_self(inode);
6616 +                               au_dbg_verify_dinode(dentry);
6617 +                       } else {
6618 +                               /* fill empty slots */
6619 +                               if (dinfo->di_bstart > tmp->di_bstart)
6620 +                                       dinfo->di_bstart = tmp->di_bstart;
6621 +                               if (dinfo->di_bend < tmp->di_bend)
6622 +                                       dinfo->di_bend = tmp->di_bend;
6623 +                               dinfo->di_bwh = tmp->di_bwh;
6624 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
6625 +                               hd = tmp->di_hdentry;
6626 +                               bend = dinfo->di_bend;
6627 +                               for (bindex = tmp->di_bstart; bindex <= bend;
6628 +                                    bindex++) {
6629 +                                       if (au_h_dptr(dentry, bindex))
6630 +                                               continue;
6631 +                                       h_dentry = hd[bindex].hd_dentry;
6632 +                                       if (!h_dentry)
6633 +                                               continue;
6634 +                                       h_inode = h_dentry->d_inode;
6635 +                                       AuDebugOn(!h_inode);
6636 +                                       AuDebugOn(orig_h.mode
6637 +                                                 != (h_inode->i_mode
6638 +                                                     & S_IFMT));
6639 +                                       au_set_h_dptr(dentry, bindex,
6640 +                                                     dget(h_dentry));
6641 +                               }
6642 +                               err = au_refresh_hinode(inode, dentry);
6643 +                               au_dbg_verify_dinode(dentry);
6644 +                       }
6645 +               } else {
6646 +                       AuDbg("positive --> positive, different type\n");
6647 +                       /* similar to the behaviour of removing and creating */
6648 +                       au_hide(dentry);
6649 +                       if (inode)
6650 +                               err = au_refresh_hinode_self(inode);
6651 +                       au_dbg_verify_dinode(dentry);
6652 +               }
6653 +       }
6654 +
6655 +out:
6656 +       return err;
6657 +}
6658 +
6659 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
6660 +{
6661 +       int err, ebrange;
6662 +       unsigned int sigen;
6663 +       struct au_dinfo *dinfo, *tmp;
6664 +       struct super_block *sb;
6665 +       struct inode *inode;
6666 +
6667 +       DiMustWriteLock(dentry);
6668 +       AuDebugOn(IS_ROOT(dentry));
6669 +       AuDebugOn(!parent->d_inode);
6670 +
6671 +       sb = dentry->d_sb;
6672 +       inode = dentry->d_inode;
6673 +       sigen = au_sigen(sb);
6674 +       err = au_digen_test(parent, sigen);
6675 +       if (unlikely(err))
6676 +               goto out;
6677 +
6678 +       dinfo = au_di(dentry);
6679 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
6680 +       if (unlikely(err))
6681 +               goto out;
6682 +       ebrange = au_dbrange_test(dentry);
6683 +       if (!ebrange)
6684 +               ebrange = au_do_refresh_hdentry(dentry, parent);
6685 +
6686 +       if (d_unhashed(dentry) || ebrange) {
6687 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
6688 +               if (inode)
6689 +                       err = au_refresh_hinode_self(inode);
6690 +               au_dbg_verify_dinode(dentry);
6691 +               if (!err)
6692 +                       goto out_dgen; /* success */
6693 +               goto out;
6694 +       }
6695 +
6696 +       /* temporary dinfo */
6697 +       AuDbgDentry(dentry);
6698 +       err = -ENOMEM;
6699 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
6700 +       if (unlikely(!tmp))
6701 +               goto out;
6702 +       au_di_swap(tmp, dinfo);
6703 +       /* returns the number of positive dentries */
6704 +       /*
6705 +        * if current working dir is removed, it returns an error.
6706 +        * but the dentry is legal.
6707 +        */
6708 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0, /*nd*/NULL);
6709 +       AuDbgDentry(dentry);
6710 +       au_di_swap(tmp, dinfo);
6711 +       if (err == -ENOENT)
6712 +               err = 0;
6713 +       if (err >= 0) {
6714 +               /* compare/refresh by dinfo */
6715 +               AuDbgDentry(dentry);
6716 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
6717 +               au_dbg_verify_dinode(dentry);
6718 +               AuTraceErr(err);
6719 +       }
6720 +       au_rw_write_unlock(&tmp->di_rwsem);
6721 +       au_di_free(tmp);
6722 +       if (unlikely(err))
6723 +               goto out;
6724 +
6725 +out_dgen:
6726 +       au_update_digen(dentry);
6727 +out:
6728 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
6729 +               AuIOErr("failed refreshing %.*s, %d\n",
6730 +                       AuDLNPair(dentry), err);
6731 +               AuDbgDentry(dentry);
6732 +       }
6733 +       AuTraceErr(err);
6734 +       return err;
6735 +}
6736 +
6737 +static noinline_for_stack
6738 +int au_do_h_d_reval(struct dentry *h_dentry, struct nameidata *nd,
6739 +                   struct dentry *dentry, aufs_bindex_t bindex)
6740 +{
6741 +       int err, valid;
6742 +       int (*reval)(struct dentry *, struct nameidata *);
6743 +
6744 +       err = 0;
6745 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
6746 +               goto out;
6747 +       reval = h_dentry->d_op->d_revalidate;
6748 +
6749 +       AuDbg("b%d\n", bindex);
6750 +       if (au_test_fs_null_nd(h_dentry->d_sb))
6751 +               /* it may return tri-state */
6752 +               valid = reval(h_dentry, NULL);
6753 +       else {
6754 +               struct nameidata h_nd;
6755 +               int locked;
6756 +               struct dentry *parent;
6757 +
6758 +               au_h_nd(&h_nd, nd);
6759 +               parent = nd->path.dentry;
6760 +               locked = (nd && nd->path.dentry != dentry);
6761 +               if (locked)
6762 +                       di_read_lock_parent(parent, AuLock_IR);
6763 +               BUG_ON(bindex > au_dbend(parent));
6764 +               h_nd.path.dentry = au_h_dptr(parent, bindex);
6765 +               BUG_ON(!h_nd.path.dentry);
6766 +               h_nd.path.mnt = au_sbr(parent->d_sb, bindex)->br_mnt;
6767 +               path_get(&h_nd.path);
6768 +               valid = reval(h_dentry, &h_nd);
6769 +               path_put(&h_nd.path);
6770 +               if (locked)
6771 +                       di_read_unlock(parent, AuLock_IR);
6772 +       }
6773 +
6774 +       if (unlikely(valid < 0))
6775 +               err = valid;
6776 +       else if (!valid)
6777 +               err = -EINVAL;
6778 +
6779 +out:
6780 +       AuTraceErr(err);
6781 +       return err;
6782 +}
6783 +
6784 +/* todo: remove this */
6785 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
6786 +                         struct nameidata *nd, int do_udba)
6787 +{
6788 +       int err;
6789 +       umode_t mode, h_mode;
6790 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
6791 +       unsigned char plus, unhashed, is_root, h_plus;
6792 +       struct inode *h_inode, *h_cached_inode;
6793 +       struct dentry *h_dentry;
6794 +       struct qstr *name, *h_name;
6795 +
6796 +       err = 0;
6797 +       plus = 0;
6798 +       mode = 0;
6799 +       ibs = -1;
6800 +       ibe = -1;
6801 +       unhashed = !!d_unhashed(dentry);
6802 +       is_root = !!IS_ROOT(dentry);
6803 +       name = &dentry->d_name;
6804 +
6805 +       /*
6806 +        * Theoretically, REVAL test should be unnecessary in case of
6807 +        * {FS,I}NOTIFY.
6808 +        * But {fs,i}notify doesn't fire some necessary events,
6809 +        *      IN_ATTRIB for atime/nlink/pageio
6810 +        *      IN_DELETE for NFS dentry
6811 +        * Let's do REVAL test too.
6812 +        */
6813 +       if (do_udba && inode) {
6814 +               mode = (inode->i_mode & S_IFMT);
6815 +               plus = (inode->i_nlink > 0);
6816 +               ibs = au_ibstart(inode);
6817 +               ibe = au_ibend(inode);
6818 +       }
6819 +
6820 +       bstart = au_dbstart(dentry);
6821 +       btail = bstart;
6822 +       if (inode && S_ISDIR(inode->i_mode))
6823 +               btail = au_dbtaildir(dentry);
6824 +       for (bindex = bstart; bindex <= btail; bindex++) {
6825 +               h_dentry = au_h_dptr(dentry, bindex);
6826 +               if (!h_dentry)
6827 +                       continue;
6828 +
6829 +               AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
6830 +               spin_lock(&h_dentry->d_lock);
6831 +               h_name = &h_dentry->d_name;
6832 +               if (unlikely(do_udba
6833 +                            && !is_root
6834 +                            && (unhashed != !!d_unhashed(h_dentry)
6835 +                                || name->len != h_name->len
6836 +                                || memcmp(name->name, h_name->name, name->len))
6837 +                           )) {
6838 +                       AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
6839 +                                 unhashed, d_unhashed(h_dentry),
6840 +                                 AuDLNPair(dentry), AuDLNPair(h_dentry));
6841 +                       spin_unlock(&h_dentry->d_lock);
6842 +                       goto err;
6843 +               }
6844 +               spin_unlock(&h_dentry->d_lock);
6845 +
6846 +               err = au_do_h_d_reval(h_dentry, nd, dentry, bindex);
6847 +               if (unlikely(err))
6848 +                       /* do not goto err, to keep the errno */
6849 +                       break;
6850 +
6851 +               /* todo: plink too? */
6852 +               if (!do_udba)
6853 +                       continue;
6854 +
6855 +               /* UDBA tests */
6856 +               h_inode = h_dentry->d_inode;
6857 +               if (unlikely(!!inode != !!h_inode))
6858 +                       goto err;
6859 +
6860 +               h_plus = plus;
6861 +               h_mode = mode;
6862 +               h_cached_inode = h_inode;
6863 +               if (h_inode) {
6864 +                       h_mode = (h_inode->i_mode & S_IFMT);
6865 +                       h_plus = (h_inode->i_nlink > 0);
6866 +               }
6867 +               if (inode && ibs <= bindex && bindex <= ibe)
6868 +                       h_cached_inode = au_h_iptr(inode, bindex);
6869 +
6870 +               if (unlikely(plus != h_plus
6871 +                            || mode != h_mode
6872 +                            || h_cached_inode != h_inode))
6873 +                       goto err;
6874 +               continue;
6875 +
6876 +       err:
6877 +               err = -EINVAL;
6878 +               break;
6879 +       }
6880 +
6881 +       return err;
6882 +}
6883 +
6884 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
6885 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
6886 +{
6887 +       int err;
6888 +       struct dentry *parent;
6889 +
6890 +       if (!au_digen_test(dentry, sigen))
6891 +               return 0;
6892 +
6893 +       parent = dget_parent(dentry);
6894 +       di_read_lock_parent(parent, AuLock_IR);
6895 +       AuDebugOn(au_digen_test(parent, sigen));
6896 +       au_dbg_verify_gen(parent, sigen);
6897 +       err = au_refresh_dentry(dentry, parent);
6898 +       di_read_unlock(parent, AuLock_IR);
6899 +       dput(parent);
6900 +       AuTraceErr(err);
6901 +       return err;
6902 +}
6903 +
6904 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
6905 +{
6906 +       int err;
6907 +       struct dentry *d, *parent;
6908 +       struct inode *inode;
6909 +
6910 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
6911 +               return simple_reval_dpath(dentry, sigen);
6912 +
6913 +       /* slow loop, keep it simple and stupid */
6914 +       /* cf: au_cpup_dirs() */
6915 +       err = 0;
6916 +       parent = NULL;
6917 +       while (au_digen_test(dentry, sigen)) {
6918 +               d = dentry;
6919 +               while (1) {
6920 +                       dput(parent);
6921 +                       parent = dget_parent(d);
6922 +                       if (!au_digen_test(parent, sigen))
6923 +                               break;
6924 +                       d = parent;
6925 +               }
6926 +
6927 +               inode = d->d_inode;
6928 +               if (d != dentry)
6929 +                       di_write_lock_child2(d);
6930 +
6931 +               /* someone might update our dentry while we were sleeping */
6932 +               if (au_digen_test(d, sigen)) {
6933 +                       /*
6934 +                        * todo: consolidate with simple_reval_dpath(),
6935 +                        * do_refresh() and au_reval_for_attr().
6936 +                        */
6937 +                       di_read_lock_parent(parent, AuLock_IR);
6938 +                       err = au_refresh_dentry(d, parent);
6939 +                       di_read_unlock(parent, AuLock_IR);
6940 +               }
6941 +
6942 +               if (d != dentry)
6943 +                       di_write_unlock(d);
6944 +               dput(parent);
6945 +               if (unlikely(err))
6946 +                       break;
6947 +       }
6948 +
6949 +       return err;
6950 +}
6951 +
6952 +/*
6953 + * if valid returns 1, otherwise 0.
6954 + */
6955 +static int aufs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
6956 +{
6957 +       int valid, err;
6958 +       unsigned int sigen;
6959 +       unsigned char do_udba;
6960 +       struct super_block *sb;
6961 +       struct inode *inode;
6962 +
6963 +       /* todo: support rcu-walk? */
6964 +       if (nd && (nd->flags & LOOKUP_RCU))
6965 +               return -ECHILD;
6966 +
6967 +       valid = 0;
6968 +       if (unlikely(!au_di(dentry)))
6969 +               goto out;
6970 +
6971 +       inode = dentry->d_inode;
6972 +       if (inode && is_bad_inode(inode))
6973 +               goto out;
6974 +
6975 +       valid = 1;
6976 +       sb = dentry->d_sb;
6977 +       /*
6978 +        * todo: very ugly
6979 +        * i_mutex of parent dir may be held,
6980 +        * but we should not return 'invalid' due to busy.
6981 +        */
6982 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
6983 +       if (unlikely(err)) {
6984 +               valid = err;
6985 +               AuTraceErr(err);
6986 +               goto out;
6987 +       }
6988 +       if (unlikely(au_dbrange_test(dentry))) {
6989 +               err = -EINVAL;
6990 +               AuTraceErr(err);
6991 +               goto out_dgrade;
6992 +       }
6993 +
6994 +       sigen = au_sigen(sb);
6995 +       if (au_digen_test(dentry, sigen)) {
6996 +               AuDebugOn(IS_ROOT(dentry));
6997 +               err = au_reval_dpath(dentry, sigen);
6998 +               if (unlikely(err)) {
6999 +                       AuTraceErr(err);
7000 +                       goto out_dgrade;
7001 +               }
7002 +       }
7003 +       di_downgrade_lock(dentry, AuLock_IR);
7004 +
7005 +       err = -EINVAL;
7006 +       if (inode && (IS_DEADDIR(inode) || !inode->i_nlink))
7007 +               goto out_inval;
7008 +
7009 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7010 +       if (do_udba && inode) {
7011 +               aufs_bindex_t bstart = au_ibstart(inode);
7012 +               struct inode *h_inode;
7013 +
7014 +               if (bstart >= 0) {
7015 +                       h_inode = au_h_iptr(inode, bstart);
7016 +                       if (h_inode && au_test_higen(inode, h_inode))
7017 +                               goto out_inval;
7018 +               }
7019 +       }
7020 +
7021 +       err = h_d_revalidate(dentry, inode, nd, do_udba);
7022 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
7023 +               err = -EIO;
7024 +               AuDbg("both of real entry and whiteout found, %.*s, err %d\n",
7025 +                     AuDLNPair(dentry), err);
7026 +       }
7027 +       goto out_inval;
7028 +
7029 +out_dgrade:
7030 +       di_downgrade_lock(dentry, AuLock_IR);
7031 +out_inval:
7032 +       aufs_read_unlock(dentry, AuLock_IR);
7033 +       AuTraceErr(err);
7034 +       valid = !err;
7035 +out:
7036 +       if (!valid) {
7037 +               AuDbg("%.*s invalid, %d\n", AuDLNPair(dentry), valid);
7038 +               d_drop(dentry);
7039 +       }
7040 +       return valid;
7041 +}
7042 +
7043 +static void aufs_d_release(struct dentry *dentry)
7044 +{
7045 +       if (au_di(dentry)) {
7046 +               au_di_fin(dentry);
7047 +               au_hn_di_reinit(dentry);
7048 +       }
7049 +}
7050 +
7051 +const struct dentry_operations aufs_dop = {
7052 +       .d_revalidate   = aufs_d_revalidate,
7053 +       .d_release      = aufs_d_release
7054 +};
7055 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
7056 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
7057 +++ linux/fs/aufs/dentry.h      2011-08-24 13:30:24.731313534 +0200
7058 @@ -0,0 +1,238 @@
7059 +/*
7060 + * Copyright (C) 2005-2011 Junjiro R. Okajima
7061 + *
7062 + * This program, aufs is free software; you can redistribute it and/or modify
7063 + * it under the terms of the GNU General Public License as published by
7064 + * the Free Software Foundation; either version 2 of the License, or
7065 + * (at your option) any later version.
7066 + *
7067 + * This program is distributed in the hope that it will be useful,
7068 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7069 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7070 + * GNU General Public License for more details.
7071 + *
7072 + * You should have received a copy of the GNU General Public License
7073 + * along with this program; if not, write to the Free Software
7074 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7075 + */
7076 +
7077 +/*
7078 + * lookup and dentry operations
7079 + */
7080 +
7081 +#ifndef __AUFS_DENTRY_H__
7082 +#define __AUFS_DENTRY_H__
7083 +
7084 +#ifdef __KERNEL__
7085 +
7086 +#include <linux/dcache.h>
7087 +#include <linux/aufs_type.h>
7088 +#include "rwsem.h"
7089 +
7090 +struct au_hdentry {
7091 +       struct dentry           *hd_dentry;
7092 +       aufs_bindex_t           hd_id;
7093 +};
7094 +
7095 +struct au_dinfo {
7096 +       atomic_t                di_generation;
7097 +
7098 +       struct au_rwsem         di_rwsem;
7099 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
7100 +       struct au_hdentry       *di_hdentry;
7101 +} ____cacheline_aligned_in_smp;
7102 +
7103 +/* ---------------------------------------------------------------------- */
7104 +
7105 +/* dentry.c */
7106 +extern const struct dentry_operations aufs_dop;
7107 +struct au_branch;
7108 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
7109 +                          struct au_branch *br, struct nameidata *nd);
7110 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
7111 +                              struct au_branch *br);
7112 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7113 +               struct dentry *h_parent, struct au_branch *br);
7114 +
7115 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
7116 +                  struct nameidata *nd);
7117 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex);
7118 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
7119 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
7120 +
7121 +/* dinfo.c */
7122 +void au_di_init_once(void *_di);
7123 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
7124 +void au_di_free(struct au_dinfo *dinfo);
7125 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
7126 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
7127 +int au_di_init(struct dentry *dentry);
7128 +void au_di_fin(struct dentry *dentry);
7129 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
7130 +
7131 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
7132 +void di_read_unlock(struct dentry *d, int flags);
7133 +void di_downgrade_lock(struct dentry *d, int flags);
7134 +void di_write_lock(struct dentry *d, unsigned int lsc);
7135 +void di_write_unlock(struct dentry *d);
7136 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
7137 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
7138 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
7139 +
7140 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
7141 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
7142 +aufs_bindex_t au_dbtail(struct dentry *dentry);
7143 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
7144 +
7145 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7146 +                  struct dentry *h_dentry);
7147 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
7148 +int au_dbrange_test(struct dentry *dentry);
7149 +void au_update_digen(struct dentry *dentry);
7150 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
7151 +void au_update_dbstart(struct dentry *dentry);
7152 +void au_update_dbend(struct dentry *dentry);
7153 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
7154 +
7155 +/* ---------------------------------------------------------------------- */
7156 +
7157 +static inline struct au_dinfo *au_di(struct dentry *dentry)
7158 +{
7159 +       return dentry->d_fsdata;
7160 +}
7161 +
7162 +/* ---------------------------------------------------------------------- */
7163 +
7164 +/* lock subclass for dinfo */
7165 +enum {
7166 +       AuLsc_DI_CHILD,         /* child first */
7167 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
7168 +       AuLsc_DI_CHILD3,        /* copyup dirs */
7169 +       AuLsc_DI_PARENT,
7170 +       AuLsc_DI_PARENT2,
7171 +       AuLsc_DI_PARENT3,
7172 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
7173 +};
7174 +
7175 +/*
7176 + * di_read_lock_child, di_write_lock_child,
7177 + * di_read_lock_child2, di_write_lock_child2,
7178 + * di_read_lock_child3, di_write_lock_child3,
7179 + * di_read_lock_parent, di_write_lock_parent,
7180 + * di_read_lock_parent2, di_write_lock_parent2,
7181 + * di_read_lock_parent3, di_write_lock_parent3,
7182 + */
7183 +#define AuReadLockFunc(name, lsc) \
7184 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
7185 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
7186 +
7187 +#define AuWriteLockFunc(name, lsc) \
7188 +static inline void di_write_lock_##name(struct dentry *d) \
7189 +{ di_write_lock(d, AuLsc_DI_##lsc); }
7190 +
7191 +#define AuRWLockFuncs(name, lsc) \
7192 +       AuReadLockFunc(name, lsc) \
7193 +       AuWriteLockFunc(name, lsc)
7194 +
7195 +AuRWLockFuncs(child, CHILD);
7196 +AuRWLockFuncs(child2, CHILD2);
7197 +AuRWLockFuncs(child3, CHILD3);
7198 +AuRWLockFuncs(parent, PARENT);
7199 +AuRWLockFuncs(parent2, PARENT2);
7200 +AuRWLockFuncs(parent3, PARENT3);
7201 +
7202 +#undef AuReadLockFunc
7203 +#undef AuWriteLockFunc
7204 +#undef AuRWLockFuncs
7205 +
7206 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
7207 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
7208 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
7209 +
7210 +/* ---------------------------------------------------------------------- */
7211 +
7212 +/* todo: memory barrier? */
7213 +static inline unsigned int au_digen(struct dentry *d)
7214 +{
7215 +       return atomic_read(&au_di(d)->di_generation);
7216 +}
7217 +
7218 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
7219 +{
7220 +       hdentry->hd_dentry = NULL;
7221 +}
7222 +
7223 +static inline void au_hdput(struct au_hdentry *hd)
7224 +{
7225 +       if (hd)
7226 +               dput(hd->hd_dentry);
7227 +}
7228 +
7229 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
7230 +{
7231 +       DiMustAnyLock(dentry);
7232 +       return au_di(dentry)->di_bstart;
7233 +}
7234 +
7235 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
7236 +{
7237 +       DiMustAnyLock(dentry);
7238 +       return au_di(dentry)->di_bend;
7239 +}
7240 +
7241 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
7242 +{
7243 +       DiMustAnyLock(dentry);
7244 +       return au_di(dentry)->di_bwh;
7245 +}
7246 +
7247 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
7248 +{
7249 +       DiMustAnyLock(dentry);
7250 +       return au_di(dentry)->di_bdiropq;
7251 +}
7252 +
7253 +/* todo: hard/soft set? */
7254 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
7255 +{
7256 +       DiMustWriteLock(dentry);
7257 +       au_di(dentry)->di_bstart = bindex;
7258 +}
7259 +
7260 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
7261 +{
7262 +       DiMustWriteLock(dentry);
7263 +       au_di(dentry)->di_bend = bindex;
7264 +}
7265 +
7266 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
7267 +{
7268 +       DiMustWriteLock(dentry);
7269 +       /* dbwh can be outside of bstart - bend range */
7270 +       au_di(dentry)->di_bwh = bindex;
7271 +}
7272 +
7273 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
7274 +{
7275 +       DiMustWriteLock(dentry);
7276 +       au_di(dentry)->di_bdiropq = bindex;
7277 +}
7278 +
7279 +/* ---------------------------------------------------------------------- */
7280 +
7281 +#ifdef CONFIG_AUFS_HNOTIFY
7282 +static inline void au_digen_dec(struct dentry *d)
7283 +{
7284 +       atomic_dec(&au_di(d)->di_generation);
7285 +}
7286 +
7287 +static inline void au_hn_di_reinit(struct dentry *dentry)
7288 +{
7289 +       dentry->d_fsdata = NULL;
7290 +}
7291 +#else
7292 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
7293 +#endif /* CONFIG_AUFS_HNOTIFY */
7294 +
7295 +#endif /* __KERNEL__ */
7296 +#endif /* __AUFS_DENTRY_H__ */
7297 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
7298 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
7299 +++ linux/fs/aufs/dinfo.c       2011-08-24 13:30:24.731313534 +0200
7300 @@ -0,0 +1,543 @@
7301 +/*
7302 + * Copyright (C) 2005-2011 Junjiro R. Okajima
7303 + *
7304 + * This program, aufs is free software; you can redistribute it and/or modify
7305 + * it under the terms of the GNU General Public License as published by
7306 + * the Free Software Foundation; either version 2 of the License, or
7307 + * (at your option) any later version.
7308 + *
7309 + * This program is distributed in the hope that it will be useful,
7310 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7311 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7312 + * GNU General Public License for more details.
7313 + *
7314 + * You should have received a copy of the GNU General Public License
7315 + * along with this program; if not, write to the Free Software
7316 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7317 + */
7318 +
7319 +/*
7320 + * dentry private data
7321 + */
7322 +
7323 +#include "aufs.h"
7324 +
7325 +void au_di_init_once(void *_dinfo)
7326 +{
7327 +       struct au_dinfo *dinfo = _dinfo;
7328 +       static struct lock_class_key aufs_di;
7329 +
7330 +       au_rw_init(&dinfo->di_rwsem);
7331 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
7332 +}
7333 +
7334 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
7335 +{
7336 +       struct au_dinfo *dinfo;
7337 +       int nbr, i;
7338 +
7339 +       dinfo = au_cache_alloc_dinfo();
7340 +       if (unlikely(!dinfo))
7341 +               goto out;
7342 +
7343 +       nbr = au_sbend(sb) + 1;
7344 +       if (nbr <= 0)
7345 +               nbr = 1;
7346 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
7347 +       if (dinfo->di_hdentry) {
7348 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
7349 +               dinfo->di_bstart = -1;
7350 +               dinfo->di_bend = -1;
7351 +               dinfo->di_bwh = -1;
7352 +               dinfo->di_bdiropq = -1;
7353 +               for (i = 0; i < nbr; i++)
7354 +                       dinfo->di_hdentry[i].hd_id = -1;
7355 +               goto out;
7356 +       }
7357 +
7358 +       au_cache_free_dinfo(dinfo);
7359 +       dinfo = NULL;
7360 +
7361 +out:
7362 +       return dinfo;
7363 +}
7364 +
7365 +void au_di_free(struct au_dinfo *dinfo)
7366 +{
7367 +       struct au_hdentry *p;
7368 +       aufs_bindex_t bend, bindex;
7369 +
7370 +       /* dentry may not be revalidated */
7371 +       bindex = dinfo->di_bstart;
7372 +       if (bindex >= 0) {
7373 +               bend = dinfo->di_bend;
7374 +               p = dinfo->di_hdentry + bindex;
7375 +               while (bindex++ <= bend)
7376 +                       au_hdput(p++);
7377 +       }
7378 +       kfree(dinfo->di_hdentry);
7379 +       au_cache_free_dinfo(dinfo);
7380 +}
7381 +
7382 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
7383 +{
7384 +       struct au_hdentry *p;
7385 +       aufs_bindex_t bi;
7386 +
7387 +       AuRwMustWriteLock(&a->di_rwsem);
7388 +       AuRwMustWriteLock(&b->di_rwsem);
7389 +
7390 +#define DiSwap(v, name)                                \
7391 +       do {                                    \
7392 +               v = a->di_##name;               \
7393 +               a->di_##name = b->di_##name;    \
7394 +               b->di_##name = v;               \
7395 +       } while (0)
7396 +
7397 +       DiSwap(p, hdentry);
7398 +       DiSwap(bi, bstart);
7399 +       DiSwap(bi, bend);
7400 +       DiSwap(bi, bwh);
7401 +       DiSwap(bi, bdiropq);
7402 +       /* smp_mb(); */
7403 +
7404 +#undef DiSwap
7405 +}
7406 +
7407 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
7408 +{
7409 +       AuRwMustWriteLock(&dst->di_rwsem);
7410 +       AuRwMustWriteLock(&src->di_rwsem);
7411 +
7412 +       dst->di_bstart = src->di_bstart;
7413 +       dst->di_bend = src->di_bend;
7414 +       dst->di_bwh = src->di_bwh;
7415 +       dst->di_bdiropq = src->di_bdiropq;
7416 +       /* smp_mb(); */
7417 +}
7418 +
7419 +int au_di_init(struct dentry *dentry)
7420 +{
7421 +       int err;
7422 +       struct super_block *sb;
7423 +       struct au_dinfo *dinfo;
7424 +
7425 +       err = 0;
7426 +       sb = dentry->d_sb;
7427 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
7428 +       if (dinfo) {
7429 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
7430 +               /* smp_mb(); */ /* atomic_set */
7431 +               dentry->d_fsdata = dinfo;
7432 +       } else
7433 +               err = -ENOMEM;
7434 +
7435 +       return err;
7436 +}
7437 +
7438 +void au_di_fin(struct dentry *dentry)
7439 +{
7440 +       struct au_dinfo *dinfo;
7441 +
7442 +       dinfo = au_di(dentry);
7443 +       AuRwDestroy(&dinfo->di_rwsem);
7444 +       au_di_free(dinfo);
7445 +}
7446 +
7447 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
7448 +{
7449 +       int err, sz;
7450 +       struct au_hdentry *hdp;
7451 +
7452 +       AuRwMustWriteLock(&dinfo->di_rwsem);
7453 +
7454 +       err = -ENOMEM;
7455 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
7456 +       if (!sz)
7457 +               sz = sizeof(*hdp);
7458 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
7459 +       if (hdp) {
7460 +               dinfo->di_hdentry = hdp;
7461 +               err = 0;
7462 +       }
7463 +
7464 +       return err;
7465 +}
7466 +
7467 +/* ---------------------------------------------------------------------- */
7468 +
7469 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
7470 +{
7471 +       switch (lsc) {
7472 +       case AuLsc_DI_CHILD:
7473 +               ii_write_lock_child(inode);
7474 +               break;
7475 +       case AuLsc_DI_CHILD2:
7476 +               ii_write_lock_child2(inode);
7477 +               break;
7478 +       case AuLsc_DI_CHILD3:
7479 +               ii_write_lock_child3(inode);
7480 +               break;
7481 +       case AuLsc_DI_PARENT:
7482 +               ii_write_lock_parent(inode);
7483 +               break;
7484 +       case AuLsc_DI_PARENT2:
7485 +               ii_write_lock_parent2(inode);
7486 +               break;
7487 +       case AuLsc_DI_PARENT3:
7488 +               ii_write_lock_parent3(inode);
7489 +               break;
7490 +       default:
7491 +               BUG();
7492 +       }
7493 +}
7494 +
7495 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
7496 +{
7497 +       switch (lsc) {
7498 +       case AuLsc_DI_CHILD:
7499 +               ii_read_lock_child(inode);
7500 +               break;
7501 +       case AuLsc_DI_CHILD2:
7502 +               ii_read_lock_child2(inode);
7503 +               break;
7504 +       case AuLsc_DI_CHILD3:
7505 +               ii_read_lock_child3(inode);
7506 +               break;
7507 +       case AuLsc_DI_PARENT:
7508 +               ii_read_lock_parent(inode);
7509 +               break;
7510 +       case AuLsc_DI_PARENT2:
7511 +               ii_read_lock_parent2(inode);
7512 +               break;
7513 +       case AuLsc_DI_PARENT3:
7514 +               ii_read_lock_parent3(inode);
7515 +               break;
7516 +       default:
7517 +               BUG();
7518 +       }
7519 +}
7520 +
7521 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
7522 +{
7523 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
7524 +       if (d->d_inode) {
7525 +               if (au_ftest_lock(flags, IW))
7526 +                       do_ii_write_lock(d->d_inode, lsc);
7527 +               else if (au_ftest_lock(flags, IR))
7528 +                       do_ii_read_lock(d->d_inode, lsc);
7529 +       }
7530 +}
7531 +
7532 +void di_read_unlock(struct dentry *d, int flags)
7533 +{
7534 +       if (d->d_inode) {
7535 +               if (au_ftest_lock(flags, IW)) {
7536 +                       au_dbg_verify_dinode(d);
7537 +                       ii_write_unlock(d->d_inode);
7538 +               } else if (au_ftest_lock(flags, IR)) {
7539 +                       au_dbg_verify_dinode(d);
7540 +                       ii_read_unlock(d->d_inode);
7541 +               }
7542 +       }
7543 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
7544 +}
7545 +
7546 +void di_downgrade_lock(struct dentry *d, int flags)
7547 +{
7548 +       if (d->d_inode && au_ftest_lock(flags, IR))
7549 +               ii_downgrade_lock(d->d_inode);
7550 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
7551 +}
7552 +
7553 +void di_write_lock(struct dentry *d, unsigned int lsc)
7554 +{
7555 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
7556 +       if (d->d_inode)
7557 +               do_ii_write_lock(d->d_inode, lsc);
7558 +}
7559 +
7560 +void di_write_unlock(struct dentry *d)
7561 +{
7562 +       au_dbg_verify_dinode(d);
7563 +       if (d->d_inode)
7564 +               ii_write_unlock(d->d_inode);
7565 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
7566 +}
7567 +
7568 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
7569 +{
7570 +       AuDebugOn(d1 == d2
7571 +                 || d1->d_inode == d2->d_inode
7572 +                 || d1->d_sb != d2->d_sb);
7573 +
7574 +       if (isdir && au_test_subdir(d1, d2)) {
7575 +               di_write_lock_child(d1);
7576 +               di_write_lock_child2(d2);
7577 +       } else {
7578 +               /* there should be no races */
7579 +               di_write_lock_child(d2);
7580 +               di_write_lock_child2(d1);
7581 +       }
7582 +}
7583 +
7584 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
7585 +{
7586 +       AuDebugOn(d1 == d2
7587 +                 || d1->d_inode == d2->d_inode
7588 +                 || d1->d_sb != d2->d_sb);
7589 +
7590 +       if (isdir && au_test_subdir(d1, d2)) {
7591 +               di_write_lock_parent(d1);
7592 +               di_write_lock_parent2(d2);
7593 +       } else {
7594 +               /* there should be no races */
7595 +               di_write_lock_parent(d2);
7596 +               di_write_lock_parent2(d1);
7597 +       }
7598 +}
7599 +
7600 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
7601 +{
7602 +       di_write_unlock(d1);
7603 +       if (d1->d_inode == d2->d_inode)
7604 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
7605 +       else
7606 +               di_write_unlock(d2);
7607 +}
7608 +
7609 +/* ---------------------------------------------------------------------- */
7610 +
7611 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
7612 +{
7613 +       struct dentry *d;
7614 +
7615 +       DiMustAnyLock(dentry);
7616 +
7617 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7618 +               return NULL;
7619 +       AuDebugOn(bindex < 0);
7620 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
7621 +       AuDebugOn(d && d->d_count <= 0);
7622 +       return d;
7623 +}
7624 +
7625 +/*
7626 + * extended version of au_h_dptr().
7627 + * returns a hashed and positive h_dentry in bindex, NULL, or error.
7628 + */
7629 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
7630 +{
7631 +       struct dentry *h_dentry;
7632 +       struct inode *inode, *h_inode;
7633 +
7634 +       inode = dentry->d_inode;
7635 +       AuDebugOn(!inode);
7636 +
7637 +       h_dentry = NULL;
7638 +       if (au_dbstart(dentry) <= bindex
7639 +           && bindex <= au_dbend(dentry))
7640 +               h_dentry = au_h_dptr(dentry, bindex);
7641 +       if (h_dentry && !au_d_hashed_positive(h_dentry)) {
7642 +               dget(h_dentry);
7643 +               goto out; /* success */
7644 +       }
7645 +
7646 +       AuDebugOn(bindex < au_ibstart(inode));
7647 +       AuDebugOn(au_ibend(inode) < bindex);
7648 +       h_inode = au_h_iptr(inode, bindex);
7649 +       h_dentry = d_find_alias(h_inode);
7650 +       if (h_dentry) {
7651 +               if (!IS_ERR(h_dentry)) {
7652 +                       if (!au_d_hashed_positive(h_dentry))
7653 +                               goto out; /* success */
7654 +                       dput(h_dentry);
7655 +               } else
7656 +                       goto out;
7657 +       }
7658 +
7659 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
7660 +               h_dentry = au_plink_lkup(inode, bindex);
7661 +               AuDebugOn(!h_dentry);
7662 +               if (!IS_ERR(h_dentry)) {
7663 +                       if (!au_d_hashed_positive(h_dentry))
7664 +                               goto out; /* success */
7665 +                       dput(h_dentry);
7666 +                       h_dentry = NULL;
7667 +               }
7668 +       }
7669 +
7670 +out:
7671 +       AuDbgDentry(h_dentry);
7672 +       return h_dentry;
7673 +}
7674 +
7675 +aufs_bindex_t au_dbtail(struct dentry *dentry)
7676 +{
7677 +       aufs_bindex_t bend, bwh;
7678 +
7679 +       bend = au_dbend(dentry);
7680 +       if (0 <= bend) {
7681 +               bwh = au_dbwh(dentry);
7682 +               if (!bwh)
7683 +                       return bwh;
7684 +               if (0 < bwh && bwh < bend)
7685 +                       return bwh - 1;
7686 +       }
7687 +       return bend;
7688 +}
7689 +
7690 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
7691 +{
7692 +       aufs_bindex_t bend, bopq;
7693 +
7694 +       bend = au_dbtail(dentry);
7695 +       if (0 <= bend) {
7696 +               bopq = au_dbdiropq(dentry);
7697 +               if (0 <= bopq && bopq < bend)
7698 +                       bend = bopq;
7699 +       }
7700 +       return bend;
7701 +}
7702 +
7703 +/* ---------------------------------------------------------------------- */
7704 +
7705 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7706 +                  struct dentry *h_dentry)
7707 +{
7708 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
7709 +       struct au_branch *br;
7710 +
7711 +       DiMustWriteLock(dentry);
7712 +
7713 +       au_hdput(hd);
7714 +       hd->hd_dentry = h_dentry;
7715 +       if (h_dentry) {
7716 +               br = au_sbr(dentry->d_sb, bindex);
7717 +               hd->hd_id = br->br_id;
7718 +       }
7719 +}
7720 +
7721 +int au_dbrange_test(struct dentry *dentry)
7722 +{
7723 +       int err;
7724 +       aufs_bindex_t bstart, bend;
7725 +
7726 +       err = 0;
7727 +       bstart = au_dbstart(dentry);
7728 +       bend = au_dbend(dentry);
7729 +       if (bstart >= 0)
7730 +               AuDebugOn(bend < 0 && bstart > bend);
7731 +       else {
7732 +               err = -EIO;
7733 +               AuDebugOn(bend >= 0);
7734 +       }
7735 +
7736 +       return err;
7737 +}
7738 +
7739 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
7740 +{
7741 +       int err;
7742 +
7743 +       err = 0;
7744 +       if (unlikely(au_digen(dentry) != sigen
7745 +                    || au_iigen_test(dentry->d_inode, sigen)))
7746 +               err = -EIO;
7747 +
7748 +       return err;
7749 +}
7750 +
7751 +void au_update_digen(struct dentry *dentry)
7752 +{
7753 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
7754 +       /* smp_mb(); */ /* atomic_set */
7755 +}
7756 +
7757 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
7758 +{
7759 +       struct au_dinfo *dinfo;
7760 +       struct dentry *h_d;
7761 +       struct au_hdentry *hdp;
7762 +
7763 +       DiMustWriteLock(dentry);
7764 +
7765 +       dinfo = au_di(dentry);
7766 +       if (!dinfo || dinfo->di_bstart < 0)
7767 +               return;
7768 +
7769 +       hdp = dinfo->di_hdentry;
7770 +       if (do_put_zero) {
7771 +               aufs_bindex_t bindex, bend;
7772 +
7773 +               bend = dinfo->di_bend;
7774 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
7775 +                       h_d = hdp[0 + bindex].hd_dentry;
7776 +                       if (h_d && !h_d->d_inode)
7777 +                               au_set_h_dptr(dentry, bindex, NULL);
7778 +               }
7779 +       }
7780 +
7781 +       dinfo->di_bstart = -1;
7782 +       while (++dinfo->di_bstart <= dinfo->di_bend)
7783 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
7784 +                       break;
7785 +       if (dinfo->di_bstart > dinfo->di_bend) {
7786 +               dinfo->di_bstart = -1;
7787 +               dinfo->di_bend = -1;
7788 +               return;
7789 +       }
7790 +
7791 +       dinfo->di_bend++;
7792 +       while (0 <= --dinfo->di_bend)
7793 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
7794 +                       break;
7795 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
7796 +}
7797 +
7798 +void au_update_dbstart(struct dentry *dentry)
7799 +{
7800 +       aufs_bindex_t bindex, bend;
7801 +       struct dentry *h_dentry;
7802 +
7803 +       bend = au_dbend(dentry);
7804 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
7805 +               h_dentry = au_h_dptr(dentry, bindex);
7806 +               if (!h_dentry)
7807 +                       continue;
7808 +               if (h_dentry->d_inode) {
7809 +                       au_set_dbstart(dentry, bindex);
7810 +                       return;
7811 +               }
7812 +               au_set_h_dptr(dentry, bindex, NULL);
7813 +       }
7814 +}
7815 +
7816 +void au_update_dbend(struct dentry *dentry)
7817 +{
7818 +       aufs_bindex_t bindex, bstart;
7819 +       struct dentry *h_dentry;
7820 +
7821 +       bstart = au_dbstart(dentry);
7822 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
7823 +               h_dentry = au_h_dptr(dentry, bindex);
7824 +               if (!h_dentry)
7825 +                       continue;
7826 +               if (h_dentry->d_inode) {
7827 +                       au_set_dbend(dentry, bindex);
7828 +                       return;
7829 +               }
7830 +               au_set_h_dptr(dentry, bindex, NULL);
7831 +       }
7832 +}
7833 +
7834 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
7835 +{
7836 +       aufs_bindex_t bindex, bend;
7837 +
7838 +       bend = au_dbend(dentry);
7839 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
7840 +               if (au_h_dptr(dentry, bindex) == h_dentry)
7841 +                       return bindex;
7842 +       return -1;
7843 +}
7844 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
7845 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
7846 +++ linux/fs/aufs/dir.c 2012-01-05 11:00:59.215501779 +0100
7847 @@ -0,0 +1,635 @@
7848 +/*
7849 + * Copyright (C) 2005-2011 Junjiro R. Okajima
7850 + *
7851 + * This program, aufs is free software; you can redistribute it and/or modify
7852 + * it under the terms of the GNU General Public License as published by
7853 + * the Free Software Foundation; either version 2 of the License, or
7854 + * (at your option) any later version.
7855 + *
7856 + * This program is distributed in the hope that it will be useful,
7857 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7858 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7859 + * GNU General Public License for more details.
7860 + *
7861 + * You should have received a copy of the GNU General Public License
7862 + * along with this program; if not, write to the Free Software
7863 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7864 + */
7865 +
7866 +/*
7867 + * directory operations
7868 + */
7869 +
7870 +#include <linux/file.h>
7871 +#include <linux/fs_stack.h>
7872 +#include "aufs.h"
7873 +
7874 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
7875 +{
7876 +       unsigned int nlink;
7877 +
7878 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
7879 +
7880 +       nlink = dir->i_nlink;
7881 +       nlink += h_dir->i_nlink - 2;
7882 +       if (h_dir->i_nlink < 2)
7883 +               nlink += 2;
7884 +       set_nlink(dir, nlink);
7885 +}
7886 +
7887 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
7888 +{
7889 +       unsigned int nlink;
7890 +
7891 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
7892 +
7893 +       nlink = dir->i_nlink;
7894 +       nlink -= h_dir->i_nlink - 2;
7895 +       if (h_dir->i_nlink < 2)
7896 +               nlink -= 2;
7897 +       set_nlink(dir, nlink);
7898 +}
7899 +
7900 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
7901 +{
7902 +       loff_t sz;
7903 +       aufs_bindex_t bindex, bend;
7904 +       struct file *h_file;
7905 +       struct dentry *h_dentry;
7906 +
7907 +       sz = 0;
7908 +       if (file) {
7909 +               AuDebugOn(!file->f_dentry);
7910 +               AuDebugOn(!file->f_dentry->d_inode);
7911 +               AuDebugOn(!S_ISDIR(file->f_dentry->d_inode->i_mode));
7912 +
7913 +               bend = au_fbend_dir(file);
7914 +               for (bindex = au_fbstart(file);
7915 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
7916 +                    bindex++) {
7917 +                       h_file = au_hf_dir(file, bindex);
7918 +                       if (h_file
7919 +                           && h_file->f_dentry
7920 +                           && h_file->f_dentry->d_inode)
7921 +                               sz += i_size_read(h_file->f_dentry->d_inode);
7922 +               }
7923 +       } else {
7924 +               AuDebugOn(!dentry);
7925 +               AuDebugOn(!dentry->d_inode);
7926 +               AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
7927 +
7928 +               bend = au_dbtaildir(dentry);
7929 +               for (bindex = au_dbstart(dentry);
7930 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
7931 +                    bindex++) {
7932 +                       h_dentry = au_h_dptr(dentry, bindex);
7933 +                       if (h_dentry && h_dentry->d_inode)
7934 +                               sz += i_size_read(h_dentry->d_inode);
7935 +               }
7936 +       }
7937 +       if (sz < KMALLOC_MAX_SIZE)
7938 +               sz = roundup_pow_of_two(sz);
7939 +       if (sz > KMALLOC_MAX_SIZE)
7940 +               sz = KMALLOC_MAX_SIZE;
7941 +       else if (sz < NAME_MAX) {
7942 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
7943 +               sz = AUFS_RDBLK_DEF;
7944 +       }
7945 +       return sz;
7946 +}
7947 +
7948 +/* ---------------------------------------------------------------------- */
7949 +
7950 +static int reopen_dir(struct file *file)
7951 +{
7952 +       int err;
7953 +       unsigned int flags;
7954 +       aufs_bindex_t bindex, btail, bstart;
7955 +       struct dentry *dentry, *h_dentry;
7956 +       struct file *h_file;
7957 +
7958 +       /* open all lower dirs */
7959 +       dentry = file->f_dentry;
7960 +       bstart = au_dbstart(dentry);
7961 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
7962 +               au_set_h_fptr(file, bindex, NULL);
7963 +       au_set_fbstart(file, bstart);
7964 +
7965 +       btail = au_dbtaildir(dentry);
7966 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
7967 +               au_set_h_fptr(file, bindex, NULL);
7968 +       au_set_fbend_dir(file, btail);
7969 +
7970 +       flags = vfsub_file_flags(file);
7971 +       for (bindex = bstart; bindex <= btail; bindex++) {
7972 +               h_dentry = au_h_dptr(dentry, bindex);
7973 +               if (!h_dentry)
7974 +                       continue;
7975 +               h_file = au_hf_dir(file, bindex);
7976 +               if (h_file)
7977 +                       continue;
7978 +
7979 +               h_file = au_h_open(dentry, bindex, flags, file);
7980 +               err = PTR_ERR(h_file);
7981 +               if (IS_ERR(h_file))
7982 +                       goto out; /* close all? */
7983 +               au_set_h_fptr(file, bindex, h_file);
7984 +       }
7985 +       au_update_figen(file);
7986 +       /* todo: necessary? */
7987 +       /* file->f_ra = h_file->f_ra; */
7988 +       err = 0;
7989 +
7990 +out:
7991 +       return err;
7992 +}
7993 +
7994 +static int do_open_dir(struct file *file, int flags)
7995 +{
7996 +       int err;
7997 +       aufs_bindex_t bindex, btail;
7998 +       struct dentry *dentry, *h_dentry;
7999 +       struct file *h_file;
8000 +
8001 +       FiMustWriteLock(file);
8002 +
8003 +       dentry = file->f_dentry;
8004 +       err = au_alive_dir(dentry);
8005 +       if (unlikely(err))
8006 +               goto out;
8007 +
8008 +       file->f_version = dentry->d_inode->i_version;
8009 +       bindex = au_dbstart(dentry);
8010 +       au_set_fbstart(file, bindex);
8011 +       btail = au_dbtaildir(dentry);
8012 +       au_set_fbend_dir(file, btail);
8013 +       for (; !err && bindex <= btail; bindex++) {
8014 +               h_dentry = au_h_dptr(dentry, bindex);
8015 +               if (!h_dentry)
8016 +                       continue;
8017 +
8018 +               h_file = au_h_open(dentry, bindex, flags, file);
8019 +               if (IS_ERR(h_file)) {
8020 +                       err = PTR_ERR(h_file);
8021 +                       break;
8022 +               }
8023 +               au_set_h_fptr(file, bindex, h_file);
8024 +       }
8025 +       au_update_figen(file);
8026 +       /* todo: necessary? */
8027 +       /* file->f_ra = h_file->f_ra; */
8028 +       if (!err)
8029 +               return 0; /* success */
8030 +
8031 +       /* close all */
8032 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
8033 +               au_set_h_fptr(file, bindex, NULL);
8034 +       au_set_fbstart(file, -1);
8035 +       au_set_fbend_dir(file, -1);
8036 +
8037 +out:
8038 +       return err;
8039 +}
8040 +
8041 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8042 +                        struct file *file)
8043 +{
8044 +       int err;
8045 +       struct super_block *sb;
8046 +       struct au_fidir *fidir;
8047 +
8048 +       err = -ENOMEM;
8049 +       sb = file->f_dentry->d_sb;
8050 +       si_read_lock(sb, AuLock_FLUSH);
8051 +       fidir = au_fidir_alloc(sb);
8052 +       if (fidir) {
8053 +               err = au_do_open(file, do_open_dir, fidir);
8054 +               if (unlikely(err))
8055 +                       kfree(fidir);
8056 +       }
8057 +       si_read_unlock(sb);
8058 +       return err;
8059 +}
8060 +
8061 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8062 +                           struct file *file)
8063 +{
8064 +       struct au_vdir *vdir_cache;
8065 +       struct au_finfo *finfo;
8066 +       struct au_fidir *fidir;
8067 +       aufs_bindex_t bindex, bend;
8068 +
8069 +       finfo = au_fi(file);
8070 +       fidir = finfo->fi_hdir;
8071 +       if (fidir) {
8072 +               /* remove me from sb->s_files */
8073 +               file_sb_list_del(file);
8074 +
8075 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
8076 +               if (vdir_cache)
8077 +                       au_vdir_free(vdir_cache);
8078 +
8079 +               bindex = finfo->fi_btop;
8080 +               if (bindex >= 0) {
8081 +                       /*
8082 +                        * calls fput() instead of filp_close(),
8083 +                        * since no dnotify or lock for the lower file.
8084 +                        */
8085 +                       bend = fidir->fd_bbot;
8086 +                       for (; bindex <= bend; bindex++)
8087 +                               au_set_h_fptr(file, bindex, NULL);
8088 +               }
8089 +               kfree(fidir);
8090 +               finfo->fi_hdir = NULL;
8091 +       }
8092 +       au_finfo_fin(file);
8093 +       return 0;
8094 +}
8095 +
8096 +/* ---------------------------------------------------------------------- */
8097 +
8098 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
8099 +{
8100 +       int err;
8101 +       aufs_bindex_t bindex, bend;
8102 +       struct file *h_file;
8103 +
8104 +       err = 0;
8105 +       bend = au_fbend_dir(file);
8106 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8107 +               h_file = au_hf_dir(file, bindex);
8108 +               if (h_file)
8109 +                       err = vfsub_flush(h_file, id);
8110 +       }
8111 +       return err;
8112 +}
8113 +
8114 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
8115 +{
8116 +       return au_do_flush(file, id, au_do_flush_dir);
8117 +}
8118 +
8119 +/* ---------------------------------------------------------------------- */
8120 +
8121 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
8122 +{
8123 +       int err;
8124 +       aufs_bindex_t bend, bindex;
8125 +       struct inode *inode;
8126 +       struct super_block *sb;
8127 +
8128 +       err = 0;
8129 +       sb = dentry->d_sb;
8130 +       inode = dentry->d_inode;
8131 +       IMustLock(inode);
8132 +       bend = au_dbend(dentry);
8133 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
8134 +               struct path h_path;
8135 +
8136 +               if (au_test_ro(sb, bindex, inode))
8137 +                       continue;
8138 +               h_path.dentry = au_h_dptr(dentry, bindex);
8139 +               if (!h_path.dentry)
8140 +                       continue;
8141 +
8142 +               h_path.mnt = au_sbr_mnt(sb, bindex);
8143 +               err = vfsub_fsync(NULL, &h_path, datasync);
8144 +       }
8145 +
8146 +       return err;
8147 +}
8148 +
8149 +static int au_do_fsync_dir(struct file *file, int datasync)
8150 +{
8151 +       int err;
8152 +       aufs_bindex_t bend, bindex;
8153 +       struct file *h_file;
8154 +       struct super_block *sb;
8155 +       struct inode *inode;
8156 +
8157 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8158 +       if (unlikely(err))
8159 +               goto out;
8160 +
8161 +       sb = file->f_dentry->d_sb;
8162 +       inode = file->f_dentry->d_inode;
8163 +       bend = au_fbend_dir(file);
8164 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8165 +               h_file = au_hf_dir(file, bindex);
8166 +               if (!h_file || au_test_ro(sb, bindex, inode))
8167 +                       continue;
8168 +
8169 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
8170 +       }
8171 +
8172 +out:
8173 +       return err;
8174 +}
8175 +
8176 +/*
8177 + * @file may be NULL
8178 + */
8179 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
8180 +                         int datasync)
8181 +{
8182 +       int err;
8183 +       struct dentry *dentry;
8184 +       struct super_block *sb;
8185 +       struct mutex *mtx;
8186 +
8187 +       err = 0;
8188 +       dentry = file->f_dentry;
8189 +       mtx = &dentry->d_inode->i_mutex;
8190 +       mutex_lock(mtx);
8191 +       sb = dentry->d_sb;
8192 +       si_noflush_read_lock(sb);
8193 +       if (file)
8194 +               err = au_do_fsync_dir(file, datasync);
8195 +       else {
8196 +               di_write_lock_child(dentry);
8197 +               err = au_do_fsync_dir_no_file(dentry, datasync);
8198 +       }
8199 +       au_cpup_attr_timesizes(dentry->d_inode);
8200 +       di_write_unlock(dentry);
8201 +       if (file)
8202 +               fi_write_unlock(file);
8203 +
8204 +       si_read_unlock(sb);
8205 +       mutex_unlock(mtx);
8206 +       return err;
8207 +}
8208 +
8209 +/* ---------------------------------------------------------------------- */
8210 +
8211 +static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
8212 +{
8213 +       int err;
8214 +       struct dentry *dentry;
8215 +       struct inode *inode, *h_inode;
8216 +       struct super_block *sb;
8217 +
8218 +       dentry = file->f_dentry;
8219 +       inode = dentry->d_inode;
8220 +       IMustLock(inode);
8221 +
8222 +       sb = dentry->d_sb;
8223 +       si_read_lock(sb, AuLock_FLUSH);
8224 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8225 +       if (unlikely(err))
8226 +               goto out;
8227 +       err = au_alive_dir(dentry);
8228 +       if (!err)
8229 +               err = au_vdir_init(file);
8230 +       di_downgrade_lock(dentry, AuLock_IR);
8231 +       if (unlikely(err))
8232 +               goto out_unlock;
8233 +
8234 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
8235 +       if (!au_test_nfsd()) {
8236 +               err = au_vdir_fill_de(file, dirent, filldir);
8237 +               fsstack_copy_attr_atime(inode, h_inode);
8238 +       } else {
8239 +               /*
8240 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
8241 +                * encode_fh() and others.
8242 +                */
8243 +               atomic_inc(&h_inode->i_count);
8244 +               di_read_unlock(dentry, AuLock_IR);
8245 +               si_read_unlock(sb);
8246 +               err = au_vdir_fill_de(file, dirent, filldir);
8247 +               fsstack_copy_attr_atime(inode, h_inode);
8248 +               fi_write_unlock(file);
8249 +               iput(h_inode);
8250 +
8251 +               AuTraceErr(err);
8252 +               return err;
8253 +       }
8254 +
8255 +out_unlock:
8256 +       di_read_unlock(dentry, AuLock_IR);
8257 +       fi_write_unlock(file);
8258 +out:
8259 +       si_read_unlock(sb);
8260 +       return err;
8261 +}
8262 +
8263 +/* ---------------------------------------------------------------------- */
8264 +
8265 +#define AuTestEmpty_WHONLY     1
8266 +#define AuTestEmpty_CALLED     (1 << 1)
8267 +#define AuTestEmpty_SHWH       (1 << 2)
8268 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
8269 +#define au_fset_testempty(flags, name) \
8270 +       do { (flags) |= AuTestEmpty_##name; } while (0)
8271 +#define au_fclr_testempty(flags, name) \
8272 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
8273 +
8274 +#ifndef CONFIG_AUFS_SHWH
8275 +#undef AuTestEmpty_SHWH
8276 +#define AuTestEmpty_SHWH       0
8277 +#endif
8278 +
8279 +struct test_empty_arg {
8280 +       struct au_nhash *whlist;
8281 +       unsigned int flags;
8282 +       int err;
8283 +       aufs_bindex_t bindex;
8284 +};
8285 +
8286 +static int test_empty_cb(void *__arg, const char *__name, int namelen,
8287 +                        loff_t offset __maybe_unused, u64 ino,
8288 +                        unsigned int d_type)
8289 +{
8290 +       struct test_empty_arg *arg = __arg;
8291 +       char *name = (void *)__name;
8292 +
8293 +       arg->err = 0;
8294 +       au_fset_testempty(arg->flags, CALLED);
8295 +       /* smp_mb(); */
8296 +       if (name[0] == '.'
8297 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
8298 +               goto out; /* success */
8299 +
8300 +       if (namelen <= AUFS_WH_PFX_LEN
8301 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
8302 +               if (au_ftest_testempty(arg->flags, WHONLY)
8303 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
8304 +                       arg->err = -ENOTEMPTY;
8305 +               goto out;
8306 +       }
8307 +
8308 +       name += AUFS_WH_PFX_LEN;
8309 +       namelen -= AUFS_WH_PFX_LEN;
8310 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
8311 +               arg->err = au_nhash_append_wh
8312 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
8313 +                        au_ftest_testempty(arg->flags, SHWH));
8314 +
8315 +out:
8316 +       /* smp_mb(); */
8317 +       AuTraceErr(arg->err);
8318 +       return arg->err;
8319 +}
8320 +
8321 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8322 +{
8323 +       int err;
8324 +       struct file *h_file;
8325 +
8326 +       h_file = au_h_open(dentry, arg->bindex,
8327 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
8328 +                          /*file*/NULL);
8329 +       err = PTR_ERR(h_file);
8330 +       if (IS_ERR(h_file))
8331 +               goto out;
8332 +
8333 +       err = 0;
8334 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
8335 +           && !h_file->f_dentry->d_inode->i_nlink)
8336 +               goto out_put;
8337 +
8338 +       do {
8339 +               arg->err = 0;
8340 +               au_fclr_testempty(arg->flags, CALLED);
8341 +               /* smp_mb(); */
8342 +               err = vfsub_readdir(h_file, test_empty_cb, arg);
8343 +               if (err >= 0)
8344 +                       err = arg->err;
8345 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
8346 +
8347 +out_put:
8348 +       fput(h_file);
8349 +       au_sbr_put(dentry->d_sb, arg->bindex);
8350 +out:
8351 +       return err;
8352 +}
8353 +
8354 +struct do_test_empty_args {
8355 +       int *errp;
8356 +       struct dentry *dentry;
8357 +       struct test_empty_arg *arg;
8358 +};
8359 +
8360 +static void call_do_test_empty(void *args)
8361 +{
8362 +       struct do_test_empty_args *a = args;
8363 +       *a->errp = do_test_empty(a->dentry, a->arg);
8364 +}
8365 +
8366 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8367 +{
8368 +       int err, wkq_err;
8369 +       struct dentry *h_dentry;
8370 +       struct inode *h_inode;
8371 +
8372 +       h_dentry = au_h_dptr(dentry, arg->bindex);
8373 +       h_inode = h_dentry->d_inode;
8374 +       /* todo: i_mode changes anytime? */
8375 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
8376 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
8377 +       mutex_unlock(&h_inode->i_mutex);
8378 +       if (!err)
8379 +               err = do_test_empty(dentry, arg);
8380 +       else {
8381 +               struct do_test_empty_args args = {
8382 +                       .errp   = &err,
8383 +                       .dentry = dentry,
8384 +                       .arg    = arg
8385 +               };
8386 +               unsigned int flags = arg->flags;
8387 +
8388 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
8389 +               if (unlikely(wkq_err))
8390 +                       err = wkq_err;
8391 +               arg->flags = flags;
8392 +       }
8393 +
8394 +       return err;
8395 +}
8396 +
8397 +int au_test_empty_lower(struct dentry *dentry)
8398 +{
8399 +       int err;
8400 +       unsigned int rdhash;
8401 +       aufs_bindex_t bindex, bstart, btail;
8402 +       struct au_nhash whlist;
8403 +       struct test_empty_arg arg;
8404 +
8405 +       SiMustAnyLock(dentry->d_sb);
8406 +
8407 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
8408 +       if (!rdhash)
8409 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
8410 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
8411 +       if (unlikely(err))
8412 +               goto out;
8413 +
8414 +       arg.flags = 0;
8415 +       arg.whlist = &whlist;
8416 +       bstart = au_dbstart(dentry);
8417 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8418 +               au_fset_testempty(arg.flags, SHWH);
8419 +       arg.bindex = bstart;
8420 +       err = do_test_empty(dentry, &arg);
8421 +       if (unlikely(err))
8422 +               goto out_whlist;
8423 +
8424 +       au_fset_testempty(arg.flags, WHONLY);
8425 +       btail = au_dbtaildir(dentry);
8426 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
8427 +               struct dentry *h_dentry;
8428 +
8429 +               h_dentry = au_h_dptr(dentry, bindex);
8430 +               if (h_dentry && h_dentry->d_inode) {
8431 +                       arg.bindex = bindex;
8432 +                       err = do_test_empty(dentry, &arg);
8433 +               }
8434 +       }
8435 +
8436 +out_whlist:
8437 +       au_nhash_wh_free(&whlist);
8438 +out:
8439 +       return err;
8440 +}
8441 +
8442 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
8443 +{
8444 +       int err;
8445 +       struct test_empty_arg arg;
8446 +       aufs_bindex_t bindex, btail;
8447 +
8448 +       err = 0;
8449 +       arg.whlist = whlist;
8450 +       arg.flags = AuTestEmpty_WHONLY;
8451 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8452 +               au_fset_testempty(arg.flags, SHWH);
8453 +       btail = au_dbtaildir(dentry);
8454 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
8455 +               struct dentry *h_dentry;
8456 +
8457 +               h_dentry = au_h_dptr(dentry, bindex);
8458 +               if (h_dentry && h_dentry->d_inode) {
8459 +                       arg.bindex = bindex;
8460 +                       err = sio_test_empty(dentry, &arg);
8461 +               }
8462 +       }
8463 +
8464 +       return err;
8465 +}
8466 +
8467 +/* ---------------------------------------------------------------------- */
8468 +
8469 +const struct file_operations aufs_dir_fop = {
8470 +       .owner          = THIS_MODULE,
8471 +       .llseek         = default_llseek,
8472 +       .read           = generic_read_dir,
8473 +       .readdir        = aufs_readdir,
8474 +       .unlocked_ioctl = aufs_ioctl_dir,
8475 +#ifdef CONFIG_COMPAT
8476 +       .compat_ioctl   = aufs_compat_ioctl_dir,
8477 +#endif
8478 +       .open           = aufs_open_dir,
8479 +       .release        = aufs_release_dir,
8480 +       .flush          = aufs_flush_dir,
8481 +       .fsync          = aufs_fsync_dir
8482 +};
8483 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
8484 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
8485 +++ linux/fs/aufs/dir.h 2011-08-24 13:30:24.731313534 +0200
8486 @@ -0,0 +1,138 @@
8487 +/*
8488 + * Copyright (C) 2005-2011 Junjiro R. Okajima
8489 + *
8490 + * This program, aufs is free software; you can redistribute it and/or modify
8491 + * it under the terms of the GNU General Public License as published by
8492 + * the Free Software Foundation; either version 2 of the License, or
8493 + * (at your option) any later version.
8494 + *
8495 + * This program is distributed in the hope that it will be useful,
8496 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8497 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8498 + * GNU General Public License for more details.
8499 + *
8500 + * You should have received a copy of the GNU General Public License
8501 + * along with this program; if not, write to the Free Software
8502 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8503 + */
8504 +
8505 +/*
8506 + * directory operations
8507 + */
8508 +
8509 +#ifndef __AUFS_DIR_H__
8510 +#define __AUFS_DIR_H__
8511 +
8512 +#ifdef __KERNEL__
8513 +
8514 +#include <linux/fs.h>
8515 +#include <linux/aufs_type.h>
8516 +
8517 +/* ---------------------------------------------------------------------- */
8518 +
8519 +/* need to be faster and smaller */
8520 +
8521 +struct au_nhash {
8522 +       unsigned int            nh_num;
8523 +       struct hlist_head       *nh_head;
8524 +};
8525 +
8526 +struct au_vdir_destr {
8527 +       unsigned char   len;
8528 +       unsigned char   name[0];
8529 +} __packed;
8530 +
8531 +struct au_vdir_dehstr {
8532 +       struct hlist_node       hash;
8533 +       struct au_vdir_destr    *str;
8534 +} ____cacheline_aligned_in_smp;
8535 +
8536 +struct au_vdir_de {
8537 +       ino_t                   de_ino;
8538 +       unsigned char           de_type;
8539 +       /* caution: packed */
8540 +       struct au_vdir_destr    de_str;
8541 +} __packed;
8542 +
8543 +struct au_vdir_wh {
8544 +       struct hlist_node       wh_hash;
8545 +#ifdef CONFIG_AUFS_SHWH
8546 +       ino_t                   wh_ino;
8547 +       aufs_bindex_t           wh_bindex;
8548 +       unsigned char           wh_type;
8549 +#else
8550 +       aufs_bindex_t           wh_bindex;
8551 +#endif
8552 +       /* caution: packed */
8553 +       struct au_vdir_destr    wh_str;
8554 +} __packed;
8555 +
8556 +union au_vdir_deblk_p {
8557 +       unsigned char           *deblk;
8558 +       struct au_vdir_de       *de;
8559 +};
8560 +
8561 +struct au_vdir {
8562 +       unsigned char   **vd_deblk;
8563 +       unsigned long   vd_nblk;
8564 +       struct {
8565 +               unsigned long           ul;
8566 +               union au_vdir_deblk_p   p;
8567 +       } vd_last;
8568 +
8569 +       unsigned long   vd_version;
8570 +       unsigned int    vd_deblk_sz;
8571 +       unsigned long   vd_jiffy;
8572 +} ____cacheline_aligned_in_smp;
8573 +
8574 +/* ---------------------------------------------------------------------- */
8575 +
8576 +/* dir.c */
8577 +extern const struct file_operations aufs_dir_fop;
8578 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
8579 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
8580 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
8581 +int au_test_empty_lower(struct dentry *dentry);
8582 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
8583 +
8584 +/* vdir.c */
8585 +unsigned int au_rdhash_est(loff_t sz);
8586 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
8587 +void au_nhash_wh_free(struct au_nhash *whlist);
8588 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
8589 +                           int limit);
8590 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
8591 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
8592 +                      unsigned int d_type, aufs_bindex_t bindex,
8593 +                      unsigned char shwh);
8594 +void au_vdir_free(struct au_vdir *vdir);
8595 +int au_vdir_init(struct file *file);
8596 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir);
8597 +
8598 +/* ioctl.c */
8599 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
8600 +
8601 +#ifdef CONFIG_AUFS_RDU
8602 +/* rdu.c */
8603 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
8604 +#ifdef CONFIG_COMPAT
8605 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8606 +                        unsigned long arg);
8607 +#endif
8608 +#else
8609 +static inline long au_rdu_ioctl(struct file *file, unsigned int cmd,
8610 +                               unsigned long arg)
8611 +{
8612 +       return -EINVAL;
8613 +}
8614 +#ifdef CONFIG_COMPAT
8615 +static inline long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8616 +                                      unsigned long arg)
8617 +{
8618 +       return -EINVAL;
8619 +}
8620 +#endif
8621 +#endif
8622 +
8623 +#endif /* __KERNEL__ */
8624 +#endif /* __AUFS_DIR_H__ */
8625 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
8626 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
8627 +++ linux/fs/aufs/dynop.c       2011-08-24 13:30:24.731313534 +0200
8628 @@ -0,0 +1,377 @@
8629 +/*
8630 + * Copyright (C) 2010-2011 Junjiro R. Okajima
8631 + *
8632 + * This program, aufs is free software; you can redistribute it and/or modify
8633 + * it under the terms of the GNU General Public License as published by
8634 + * the Free Software Foundation; either version 2 of the License, or
8635 + * (at your option) any later version.
8636 + *
8637 + * This program is distributed in the hope that it will be useful,
8638 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8639 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8640 + * GNU General Public License for more details.
8641 + *
8642 + * You should have received a copy of the GNU General Public License
8643 + * along with this program; if not, write to the Free Software
8644 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8645 + */
8646 +
8647 +/*
8648 + * dynamically customizable operations for regular files
8649 + */
8650 +
8651 +#include "aufs.h"
8652 +
8653 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
8654 +
8655 +/*
8656 + * How large will these lists be?
8657 + * Usually just a few elements, 20-30 at most for each, I guess.
8658 + */
8659 +static struct au_splhead dynop[AuDyLast];
8660 +
8661 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
8662 +{
8663 +       struct au_dykey *key, *tmp;
8664 +       struct list_head *head;
8665 +
8666 +       key = NULL;
8667 +       head = &spl->head;
8668 +       rcu_read_lock();
8669 +       list_for_each_entry_rcu(tmp, head, dk_list)
8670 +               if (tmp->dk_op.dy_hop == h_op) {
8671 +                       key = tmp;
8672 +                       kref_get(&key->dk_kref);
8673 +                       break;
8674 +               }
8675 +       rcu_read_unlock();
8676 +
8677 +       return key;
8678 +}
8679 +
8680 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
8681 +{
8682 +       struct au_dykey **k, *found;
8683 +       const void *h_op = key->dk_op.dy_hop;
8684 +       int i;
8685 +
8686 +       found = NULL;
8687 +       k = br->br_dykey;
8688 +       for (i = 0; i < AuBrDynOp; i++)
8689 +               if (k[i]) {
8690 +                       if (k[i]->dk_op.dy_hop == h_op) {
8691 +                               found = k[i];
8692 +                               break;
8693 +                       }
8694 +               } else
8695 +                       break;
8696 +       if (!found) {
8697 +               spin_lock(&br->br_dykey_lock);
8698 +               for (; i < AuBrDynOp; i++)
8699 +                       if (k[i]) {
8700 +                               if (k[i]->dk_op.dy_hop == h_op) {
8701 +                                       found = k[i];
8702 +                                       break;
8703 +                               }
8704 +                       } else {
8705 +                               k[i] = key;
8706 +                               break;
8707 +                       }
8708 +               spin_unlock(&br->br_dykey_lock);
8709 +               BUG_ON(i == AuBrDynOp); /* expand the array */
8710 +       }
8711 +
8712 +       return found;
8713 +}
8714 +
8715 +/* kref_get() if @key is already added */
8716 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
8717 +{
8718 +       struct au_dykey *tmp, *found;
8719 +       struct list_head *head;
8720 +       const void *h_op = key->dk_op.dy_hop;
8721 +
8722 +       found = NULL;
8723 +       head = &spl->head;
8724 +       spin_lock(&spl->spin);
8725 +       list_for_each_entry(tmp, head, dk_list)
8726 +               if (tmp->dk_op.dy_hop == h_op) {
8727 +                       kref_get(&tmp->dk_kref);
8728 +                       found = tmp;
8729 +                       break;
8730 +               }
8731 +       if (!found)
8732 +               list_add_rcu(&key->dk_list, head);
8733 +       spin_unlock(&spl->spin);
8734 +
8735 +       if (!found)
8736 +               DyPrSym(key);
8737 +       return found;
8738 +}
8739 +
8740 +static void dy_free_rcu(struct rcu_head *rcu)
8741 +{
8742 +       struct au_dykey *key;
8743 +
8744 +       key = container_of(rcu, struct au_dykey, dk_rcu);
8745 +       DyPrSym(key);
8746 +       kfree(key);
8747 +}
8748 +
8749 +static void dy_free(struct kref *kref)
8750 +{
8751 +       struct au_dykey *key;
8752 +       struct au_splhead *spl;
8753 +
8754 +       key = container_of(kref, struct au_dykey, dk_kref);
8755 +       spl = dynop + key->dk_op.dy_type;
8756 +       au_spl_del_rcu(&key->dk_list, spl);
8757 +       call_rcu(&key->dk_rcu, dy_free_rcu);
8758 +}
8759 +
8760 +void au_dy_put(struct au_dykey *key)
8761 +{
8762 +       kref_put(&key->dk_kref, dy_free);
8763 +}
8764 +
8765 +/* ---------------------------------------------------------------------- */
8766 +
8767 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
8768 +
8769 +#ifdef CONFIG_AUFS_DEBUG
8770 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
8771 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
8772 +#else
8773 +#define DyDbgDeclare(cnt)      do {} while (0)
8774 +#define DyDbgInc(cnt)          do {} while (0)
8775 +#endif
8776 +
8777 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
8778 +       DyDbgInc(cnt);                                                  \
8779 +       if (h_op->func) {                                               \
8780 +               if (src.func)                                           \
8781 +                       dst.func = src.func;                            \
8782 +               else                                                    \
8783 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
8784 +       }                                                               \
8785 +} while (0)
8786 +
8787 +#define DySetForce(func, dst, src) do {                \
8788 +       AuDebugOn(!src.func);                   \
8789 +       DyDbgInc(cnt);                          \
8790 +       dst.func = src.func;                    \
8791 +} while (0)
8792 +
8793 +#define DySetAop(func) \
8794 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
8795 +#define DySetAopForce(func) \
8796 +       DySetForce(func, dyaop->da_op, aufs_aop)
8797 +
8798 +static void dy_aop(struct au_dykey *key, const void *h_op,
8799 +                  struct super_block *h_sb __maybe_unused)
8800 +{
8801 +       struct au_dyaop *dyaop = (void *)key;
8802 +       const struct address_space_operations *h_aop = h_op;
8803 +       DyDbgDeclare(cnt);
8804 +
8805 +       AuDbg("%s\n", au_sbtype(h_sb));
8806 +
8807 +       DySetAop(writepage);
8808 +       DySetAopForce(readpage);        /* force */
8809 +       DySetAop(writepages);
8810 +       DySetAop(set_page_dirty);
8811 +       DySetAop(readpages);
8812 +       DySetAop(write_begin);
8813 +       DySetAop(write_end);
8814 +       DySetAop(bmap);
8815 +       DySetAop(invalidatepage);
8816 +       DySetAop(releasepage);
8817 +       DySetAop(freepage);
8818 +       /* these two will be changed according to an aufs mount option */
8819 +       DySetAop(direct_IO);
8820 +       DySetAop(get_xip_mem);
8821 +       DySetAop(migratepage);
8822 +       DySetAop(launder_page);
8823 +       DySetAop(is_partially_uptodate);
8824 +       DySetAop(error_remove_page);
8825 +
8826 +       DyDbgSize(cnt, *h_aop);
8827 +       dyaop->da_get_xip_mem = h_aop->get_xip_mem;
8828 +}
8829 +
8830 +/* ---------------------------------------------------------------------- */
8831 +
8832 +static void dy_bug(struct kref *kref)
8833 +{
8834 +       BUG();
8835 +}
8836 +
8837 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
8838 +{
8839 +       struct au_dykey *key, *old;
8840 +       struct au_splhead *spl;
8841 +       struct op {
8842 +               unsigned int sz;
8843 +               void (*set)(struct au_dykey *key, const void *h_op,
8844 +                           struct super_block *h_sb __maybe_unused);
8845 +       };
8846 +       static const struct op a[] = {
8847 +               [AuDy_AOP] = {
8848 +                       .sz     = sizeof(struct au_dyaop),
8849 +                       .set    = dy_aop
8850 +               }
8851 +       };
8852 +       const struct op *p;
8853 +
8854 +       spl = dynop + op->dy_type;
8855 +       key = dy_gfind_get(spl, op->dy_hop);
8856 +       if (key)
8857 +               goto out_add; /* success */
8858 +
8859 +       p = a + op->dy_type;
8860 +       key = kzalloc(p->sz, GFP_NOFS);
8861 +       if (unlikely(!key)) {
8862 +               key = ERR_PTR(-ENOMEM);
8863 +               goto out;
8864 +       }
8865 +
8866 +       key->dk_op.dy_hop = op->dy_hop;
8867 +       kref_init(&key->dk_kref);
8868 +       p->set(key, op->dy_hop, br->br_mnt->mnt_sb);
8869 +       old = dy_gadd(spl, key);
8870 +       if (old) {
8871 +               kfree(key);
8872 +               key = old;
8873 +       }
8874 +
8875 +out_add:
8876 +       old = dy_bradd(br, key);
8877 +       if (old)
8878 +               /* its ref-count should never be zero here */
8879 +               kref_put(&key->dk_kref, dy_bug);
8880 +out:
8881 +       return key;
8882 +}
8883 +
8884 +/* ---------------------------------------------------------------------- */
8885 +/*
8886 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
8887 + * This behaviour is neccessary to return an error from open(O_DIRECT) instead
8888 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
8889 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
8890 + * See the aufs manual in detail.
8891 + *
8892 + * To keep this behaviour, aufs has to set NULL to ->get_xip_mem too, and the
8893 + * performance of fadvise() and madvise() may be affected.
8894 + */
8895 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
8896 +{
8897 +       if (!do_dx) {
8898 +               dyaop->da_op.direct_IO = NULL;
8899 +               dyaop->da_op.get_xip_mem = NULL;
8900 +       } else {
8901 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
8902 +               dyaop->da_op.get_xip_mem = aufs_aop.get_xip_mem;
8903 +               if (!dyaop->da_get_xip_mem)
8904 +                       dyaop->da_op.get_xip_mem = NULL;
8905 +       }
8906 +}
8907 +
8908 +static struct au_dyaop *dy_aget(struct au_branch *br,
8909 +                               const struct address_space_operations *h_aop,
8910 +                               int do_dx)
8911 +{
8912 +       struct au_dyaop *dyaop;
8913 +       struct au_dynop op;
8914 +
8915 +       op.dy_type = AuDy_AOP;
8916 +       op.dy_haop = h_aop;
8917 +       dyaop = (void *)dy_get(&op, br);
8918 +       if (IS_ERR(dyaop))
8919 +               goto out;
8920 +       dy_adx(dyaop, do_dx);
8921 +
8922 +out:
8923 +       return dyaop;
8924 +}
8925 +
8926 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
8927 +               struct inode *h_inode)
8928 +{
8929 +       int err, do_dx;
8930 +       struct super_block *sb;
8931 +       struct au_branch *br;
8932 +       struct au_dyaop *dyaop;
8933 +
8934 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
8935 +       IiMustWriteLock(inode);
8936 +
8937 +       sb = inode->i_sb;
8938 +       br = au_sbr(sb, bindex);
8939 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
8940 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
8941 +       err = PTR_ERR(dyaop);
8942 +       if (IS_ERR(dyaop))
8943 +               /* unnecessary to call dy_fput() */
8944 +               goto out;
8945 +
8946 +       err = 0;
8947 +       inode->i_mapping->a_ops = &dyaop->da_op;
8948 +
8949 +out:
8950 +       return err;
8951 +}
8952 +
8953 +/*
8954 + * Is it safe to replace a_ops during the inode/file is in operation?
8955 + * Yes, I hope so.
8956 + */
8957 +int au_dy_irefresh(struct inode *inode)
8958 +{
8959 +       int err;
8960 +       aufs_bindex_t bstart;
8961 +       struct inode *h_inode;
8962 +
8963 +       err = 0;
8964 +       if (S_ISREG(inode->i_mode)) {
8965 +               bstart = au_ibstart(inode);
8966 +               h_inode = au_h_iptr(inode, bstart);
8967 +               err = au_dy_iaop(inode, bstart, h_inode);
8968 +       }
8969 +       return err;
8970 +}
8971 +
8972 +void au_dy_arefresh(int do_dx)
8973 +{
8974 +       struct au_splhead *spl;
8975 +       struct list_head *head;
8976 +       struct au_dykey *key;
8977 +
8978 +       spl = dynop + AuDy_AOP;
8979 +       head = &spl->head;
8980 +       spin_lock(&spl->spin);
8981 +       list_for_each_entry(key, head, dk_list)
8982 +               dy_adx((void *)key, do_dx);
8983 +       spin_unlock(&spl->spin);
8984 +}
8985 +
8986 +/* ---------------------------------------------------------------------- */
8987 +
8988 +void __init au_dy_init(void)
8989 +{
8990 +       int i;
8991 +
8992 +       /* make sure that 'struct au_dykey *' can be any type */
8993 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
8994 +
8995 +       for (i = 0; i < AuDyLast; i++)
8996 +               au_spl_init(dynop + i);
8997 +}
8998 +
8999 +void au_dy_fin(void)
9000 +{
9001 +       int i;
9002 +
9003 +       for (i = 0; i < AuDyLast; i++)
9004 +               WARN_ON(!list_empty(&dynop[i].head));
9005 +}
9006 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
9007 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
9008 +++ linux/fs/aufs/dynop.h       2011-08-24 13:30:24.731313534 +0200
9009 @@ -0,0 +1,80 @@
9010 +/*
9011 + * Copyright (C) 2010-2011 Junjiro R. Okajima
9012 + *
9013 + * This program, aufs is free software; you can redistribute it and/or modify
9014 + * it under the terms of the GNU General Public License as published by
9015 + * the Free Software Foundation; either version 2 of the License, or
9016 + * (at your option) any later version.
9017 + *
9018 + * This program is distributed in the hope that it will be useful,
9019 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9020 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9021 + * GNU General Public License for more details.
9022 + *
9023 + * You should have received a copy of the GNU General Public License
9024 + * along with this program; if not, write to the Free Software
9025 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9026 + */
9027 +
9028 +/*
9029 + * dynamically customizable operations (for regular files only)
9030 + */
9031 +
9032 +#ifndef __AUFS_DYNOP_H__
9033 +#define __AUFS_DYNOP_H__
9034 +
9035 +#ifdef __KERNEL__
9036 +
9037 +#include <linux/fs.h>
9038 +#include <linux/mm.h>
9039 +#include <linux/rcupdate.h>
9040 +#include <linux/aufs_type.h>
9041 +#include "inode.h"
9042 +
9043 +enum {AuDy_AOP, AuDyLast};
9044 +
9045 +struct au_dynop {
9046 +       int                                             dy_type;
9047 +       union {
9048 +               const void                              *dy_hop;
9049 +               const struct address_space_operations   *dy_haop;
9050 +       };
9051 +};
9052 +
9053 +struct au_dykey {
9054 +       union {
9055 +               struct list_head        dk_list;
9056 +               struct rcu_head         dk_rcu;
9057 +       };
9058 +       struct au_dynop         dk_op;
9059 +
9060 +       /*
9061 +        * during I am in the branch local array, kref is gotten. when the
9062 +        * branch is removed, kref is put.
9063 +        */
9064 +       struct kref             dk_kref;
9065 +};
9066 +
9067 +/* stop unioning since their sizes are very different from each other */
9068 +struct au_dyaop {
9069 +       struct au_dykey                 da_key;
9070 +       struct address_space_operations da_op; /* not const */
9071 +       int (*da_get_xip_mem)(struct address_space *, pgoff_t, int,
9072 +                             void **, unsigned long *);
9073 +};
9074 +
9075 +/* ---------------------------------------------------------------------- */
9076 +
9077 +/* dynop.c */
9078 +struct au_branch;
9079 +void au_dy_put(struct au_dykey *key);
9080 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9081 +               struct inode *h_inode);
9082 +int au_dy_irefresh(struct inode *inode);
9083 +void au_dy_arefresh(int do_dio);
9084 +
9085 +void __init au_dy_init(void);
9086 +void au_dy_fin(void);
9087 +
9088 +#endif /* __KERNEL__ */
9089 +#endif /* __AUFS_DYNOP_H__ */
9090 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
9091 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
9092 +++ linux/fs/aufs/export.c      2011-08-24 13:30:24.731313534 +0200
9093 @@ -0,0 +1,805 @@
9094 +/*
9095 + * Copyright (C) 2005-2011 Junjiro R. Okajima
9096 + *
9097 + * This program, aufs is free software; you can redistribute it and/or modify
9098 + * it under the terms of the GNU General Public License as published by
9099 + * the Free Software Foundation; either version 2 of the License, or
9100 + * (at your option) any later version.
9101 + *
9102 + * This program is distributed in the hope that it will be useful,
9103 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9104 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9105 + * GNU General Public License for more details.
9106 + *
9107 + * You should have received a copy of the GNU General Public License
9108 + * along with this program; if not, write to the Free Software
9109 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9110 + */
9111 +
9112 +/*
9113 + * export via nfs
9114 + */
9115 +
9116 +#include <linux/exportfs.h>
9117 +#include <linux/file.h>
9118 +#include <linux/mnt_namespace.h>
9119 +#include <linux/namei.h>
9120 +#include <linux/nsproxy.h>
9121 +#include <linux/random.h>
9122 +#include <linux/writeback.h>
9123 +#include "aufs.h"
9124 +
9125 +union conv {
9126 +#ifdef CONFIG_AUFS_INO_T_64
9127 +       __u32 a[2];
9128 +#else
9129 +       __u32 a[1];
9130 +#endif
9131 +       ino_t ino;
9132 +};
9133 +
9134 +static ino_t decode_ino(__u32 *a)
9135 +{
9136 +       union conv u;
9137 +
9138 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
9139 +       u.a[0] = a[0];
9140 +#ifdef CONFIG_AUFS_INO_T_64
9141 +       u.a[1] = a[1];
9142 +#endif
9143 +       return u.ino;
9144 +}
9145 +
9146 +static void encode_ino(__u32 *a, ino_t ino)
9147 +{
9148 +       union conv u;
9149 +
9150 +       u.ino = ino;
9151 +       a[0] = u.a[0];
9152 +#ifdef CONFIG_AUFS_INO_T_64
9153 +       a[1] = u.a[1];
9154 +#endif
9155 +}
9156 +
9157 +/* NFS file handle */
9158 +enum {
9159 +       Fh_br_id,
9160 +       Fh_sigen,
9161 +#ifdef CONFIG_AUFS_INO_T_64
9162 +       /* support 64bit inode number */
9163 +       Fh_ino1,
9164 +       Fh_ino2,
9165 +       Fh_dir_ino1,
9166 +       Fh_dir_ino2,
9167 +#else
9168 +       Fh_ino1,
9169 +       Fh_dir_ino1,
9170 +#endif
9171 +       Fh_igen,
9172 +       Fh_h_type,
9173 +       Fh_tail,
9174 +
9175 +       Fh_ino = Fh_ino1,
9176 +       Fh_dir_ino = Fh_dir_ino1
9177 +};
9178 +
9179 +static int au_test_anon(struct dentry *dentry)
9180 +{
9181 +       /* note: read d_flags without d_lock */
9182 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
9183 +}
9184 +
9185 +/* ---------------------------------------------------------------------- */
9186 +/* inode generation external table */
9187 +
9188 +void au_xigen_inc(struct inode *inode)
9189 +{
9190 +       loff_t pos;
9191 +       ssize_t sz;
9192 +       __u32 igen;
9193 +       struct super_block *sb;
9194 +       struct au_sbinfo *sbinfo;
9195 +
9196 +       sb = inode->i_sb;
9197 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
9198 +
9199 +       sbinfo = au_sbi(sb);
9200 +       pos = inode->i_ino;
9201 +       pos *= sizeof(igen);
9202 +       igen = inode->i_generation + 1;
9203 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
9204 +                        sizeof(igen), &pos);
9205 +       if (sz == sizeof(igen))
9206 +               return; /* success */
9207 +
9208 +       if (unlikely(sz >= 0))
9209 +               AuIOErr("xigen error (%zd)\n", sz);
9210 +}
9211 +
9212 +int au_xigen_new(struct inode *inode)
9213 +{
9214 +       int err;
9215 +       loff_t pos;
9216 +       ssize_t sz;
9217 +       struct super_block *sb;
9218 +       struct au_sbinfo *sbinfo;
9219 +       struct file *file;
9220 +
9221 +       err = 0;
9222 +       /* todo: dirty, at mount time */
9223 +       if (inode->i_ino == AUFS_ROOT_INO)
9224 +               goto out;
9225 +       sb = inode->i_sb;
9226 +       SiMustAnyLock(sb);
9227 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9228 +               goto out;
9229 +
9230 +       err = -EFBIG;
9231 +       pos = inode->i_ino;
9232 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
9233 +               AuIOErr1("too large i%lld\n", pos);
9234 +               goto out;
9235 +       }
9236 +       pos *= sizeof(inode->i_generation);
9237 +
9238 +       err = 0;
9239 +       sbinfo = au_sbi(sb);
9240 +       file = sbinfo->si_xigen;
9241 +       BUG_ON(!file);
9242 +
9243 +       if (i_size_read(file->f_dentry->d_inode)
9244 +           < pos + sizeof(inode->i_generation)) {
9245 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
9246 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
9247 +                                sizeof(inode->i_generation), &pos);
9248 +       } else
9249 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
9250 +                               sizeof(inode->i_generation), &pos);
9251 +       if (sz == sizeof(inode->i_generation))
9252 +               goto out; /* success */
9253 +
9254 +       err = sz;
9255 +       if (unlikely(sz >= 0)) {
9256 +               err = -EIO;
9257 +               AuIOErr("xigen error (%zd)\n", sz);
9258 +       }
9259 +
9260 +out:
9261 +       return err;
9262 +}
9263 +
9264 +int au_xigen_set(struct super_block *sb, struct file *base)
9265 +{
9266 +       int err;
9267 +       struct au_sbinfo *sbinfo;
9268 +       struct file *file;
9269 +
9270 +       SiMustWriteLock(sb);
9271 +
9272 +       sbinfo = au_sbi(sb);
9273 +       file = au_xino_create2(base, sbinfo->si_xigen);
9274 +       err = PTR_ERR(file);
9275 +       if (IS_ERR(file))
9276 +               goto out;
9277 +       err = 0;
9278 +       if (sbinfo->si_xigen)
9279 +               fput(sbinfo->si_xigen);
9280 +       sbinfo->si_xigen = file;
9281 +
9282 +out:
9283 +       return err;
9284 +}
9285 +
9286 +void au_xigen_clr(struct super_block *sb)
9287 +{
9288 +       struct au_sbinfo *sbinfo;
9289 +
9290 +       SiMustWriteLock(sb);
9291 +
9292 +       sbinfo = au_sbi(sb);
9293 +       if (sbinfo->si_xigen) {
9294 +               fput(sbinfo->si_xigen);
9295 +               sbinfo->si_xigen = NULL;
9296 +       }
9297 +}
9298 +
9299 +/* ---------------------------------------------------------------------- */
9300 +
9301 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
9302 +                                   ino_t dir_ino)
9303 +{
9304 +       struct dentry *dentry, *d;
9305 +       struct inode *inode;
9306 +       unsigned int sigen;
9307 +
9308 +       dentry = NULL;
9309 +       inode = ilookup(sb, ino);
9310 +       if (!inode)
9311 +               goto out;
9312 +
9313 +       dentry = ERR_PTR(-ESTALE);
9314 +       sigen = au_sigen(sb);
9315 +       if (unlikely(is_bad_inode(inode)
9316 +                    || IS_DEADDIR(inode)
9317 +                    || sigen != au_iigen(inode)))
9318 +               goto out_iput;
9319 +
9320 +       dentry = NULL;
9321 +       if (!dir_ino || S_ISDIR(inode->i_mode))
9322 +               dentry = d_find_alias(inode);
9323 +       else {
9324 +               spin_lock(&inode->i_lock);
9325 +               list_for_each_entry(d, &inode->i_dentry, d_alias) {
9326 +                       spin_lock(&d->d_lock);
9327 +                       if (!au_test_anon(d)
9328 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
9329 +                               dentry = dget_dlock(d);
9330 +                               spin_unlock(&d->d_lock);
9331 +                               break;
9332 +                       }
9333 +                       spin_unlock(&d->d_lock);
9334 +               }
9335 +               spin_unlock(&inode->i_lock);
9336 +       }
9337 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
9338 +               /* need to refresh */
9339 +               dput(dentry);
9340 +               dentry = NULL;
9341 +       }
9342 +
9343 +out_iput:
9344 +       iput(inode);
9345 +out:
9346 +       AuTraceErrPtr(dentry);
9347 +       return dentry;
9348 +}
9349 +
9350 +/* ---------------------------------------------------------------------- */
9351 +
9352 +/* todo: dirty? */
9353 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
9354 +
9355 +struct au_compare_mnt_args {
9356 +       /* input */
9357 +       struct super_block *sb;
9358 +
9359 +       /* output */
9360 +       struct vfsmount *mnt;
9361 +};
9362 +
9363 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
9364 +{
9365 +       struct au_compare_mnt_args *a = arg;
9366 +
9367 +       if (mnt->mnt_sb != a->sb)
9368 +               return 0;
9369 +       a->mnt = mntget(mnt);
9370 +       return 1;
9371 +}
9372 +
9373 +static struct vfsmount *au_mnt_get(struct super_block *sb)
9374 +{
9375 +       int err;
9376 +       struct au_compare_mnt_args args = {
9377 +               .sb = sb
9378 +       };
9379 +       struct mnt_namespace *ns;
9380 +
9381 +       br_read_lock(vfsmount_lock);
9382 +       /* no get/put ?? */
9383 +       AuDebugOn(!current->nsproxy);
9384 +       ns = current->nsproxy->mnt_ns;
9385 +       AuDebugOn(!ns);
9386 +       err = iterate_mounts(au_compare_mnt, &args, ns->root);
9387 +       br_read_unlock(vfsmount_lock);
9388 +       AuDebugOn(!err);
9389 +       AuDebugOn(!args.mnt);
9390 +       return args.mnt;
9391 +}
9392 +
9393 +struct au_nfsd_si_lock {
9394 +       unsigned int sigen;
9395 +       aufs_bindex_t bindex, br_id;
9396 +       unsigned char force_lock;
9397 +};
9398 +
9399 +static int si_nfsd_read_lock(struct super_block *sb,
9400 +                            struct au_nfsd_si_lock *nsi_lock)
9401 +{
9402 +       int err;
9403 +       aufs_bindex_t bindex;
9404 +
9405 +       si_read_lock(sb, AuLock_FLUSH);
9406 +
9407 +       /* branch id may be wrapped around */
9408 +       err = 0;
9409 +       bindex = au_br_index(sb, nsi_lock->br_id);
9410 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
9411 +               goto out; /* success */
9412 +
9413 +       err = -ESTALE;
9414 +       bindex = -1;
9415 +       if (!nsi_lock->force_lock)
9416 +               si_read_unlock(sb);
9417 +
9418 +out:
9419 +       nsi_lock->bindex = bindex;
9420 +       return err;
9421 +}
9422 +
9423 +struct find_name_by_ino {
9424 +       int called, found;
9425 +       ino_t ino;
9426 +       char *name;
9427 +       int namelen;
9428 +};
9429 +
9430 +static int
9431 +find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
9432 +                u64 ino, unsigned int d_type)
9433 +{
9434 +       struct find_name_by_ino *a = arg;
9435 +
9436 +       a->called++;
9437 +       if (a->ino != ino)
9438 +               return 0;
9439 +
9440 +       memcpy(a->name, name, namelen);
9441 +       a->namelen = namelen;
9442 +       a->found = 1;
9443 +       return 1;
9444 +}
9445 +
9446 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
9447 +                                    struct au_nfsd_si_lock *nsi_lock)
9448 +{
9449 +       struct dentry *dentry, *parent;
9450 +       struct file *file;
9451 +       struct inode *dir;
9452 +       struct find_name_by_ino arg;
9453 +       int err;
9454 +
9455 +       parent = path->dentry;
9456 +       if (nsi_lock)
9457 +               si_read_unlock(parent->d_sb);
9458 +       file = vfsub_dentry_open(path, au_dir_roflags);
9459 +       dentry = (void *)file;
9460 +       if (IS_ERR(file))
9461 +               goto out;
9462 +
9463 +       dentry = ERR_PTR(-ENOMEM);
9464 +       arg.name = __getname_gfp(GFP_NOFS);
9465 +       if (unlikely(!arg.name))
9466 +               goto out_file;
9467 +       arg.ino = ino;
9468 +       arg.found = 0;
9469 +       do {
9470 +               arg.called = 0;
9471 +               /* smp_mb(); */
9472 +               err = vfsub_readdir(file, find_name_by_ino, &arg);
9473 +       } while (!err && !arg.found && arg.called);
9474 +       dentry = ERR_PTR(err);
9475 +       if (unlikely(err))
9476 +               goto out_name;
9477 +       dentry = ERR_PTR(-ENOENT);
9478 +       if (!arg.found)
9479 +               goto out_name;
9480 +
9481 +       /* do not call au_lkup_one() */
9482 +       dir = parent->d_inode;
9483 +       mutex_lock(&dir->i_mutex);
9484 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
9485 +       mutex_unlock(&dir->i_mutex);
9486 +       AuTraceErrPtr(dentry);
9487 +       if (IS_ERR(dentry))
9488 +               goto out_name;
9489 +       AuDebugOn(au_test_anon(dentry));
9490 +       if (unlikely(!dentry->d_inode)) {
9491 +               dput(dentry);
9492 +               dentry = ERR_PTR(-ENOENT);
9493 +       }
9494 +
9495 +out_name:
9496 +       __putname(arg.name);
9497 +out_file:
9498 +       fput(file);
9499 +out:
9500 +       if (unlikely(nsi_lock
9501 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
9502 +               if (!IS_ERR(dentry)) {
9503 +                       dput(dentry);
9504 +                       dentry = ERR_PTR(-ESTALE);
9505 +               }
9506 +       AuTraceErrPtr(dentry);
9507 +       return dentry;
9508 +}
9509 +
9510 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
9511 +                                       ino_t dir_ino,
9512 +                                       struct au_nfsd_si_lock *nsi_lock)
9513 +{
9514 +       struct dentry *dentry;
9515 +       struct path path;
9516 +
9517 +       if (dir_ino != AUFS_ROOT_INO) {
9518 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
9519 +               dentry = path.dentry;
9520 +               if (!path.dentry || IS_ERR(path.dentry))
9521 +                       goto out;
9522 +               AuDebugOn(au_test_anon(path.dentry));
9523 +       } else
9524 +               path.dentry = dget(sb->s_root);
9525 +
9526 +       path.mnt = au_mnt_get(sb);
9527 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
9528 +       path_put(&path);
9529 +
9530 +out:
9531 +       AuTraceErrPtr(dentry);
9532 +       return dentry;
9533 +}
9534 +
9535 +/* ---------------------------------------------------------------------- */
9536 +
9537 +static int h_acceptable(void *expv, struct dentry *dentry)
9538 +{
9539 +       return 1;
9540 +}
9541 +
9542 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
9543 +                          char *buf, int len, struct super_block *sb)
9544 +{
9545 +       char *p;
9546 +       int n;
9547 +       struct path path;
9548 +
9549 +       p = d_path(h_rootpath, buf, len);
9550 +       if (IS_ERR(p))
9551 +               goto out;
9552 +       n = strlen(p);
9553 +
9554 +       path.mnt = h_rootpath->mnt;
9555 +       path.dentry = h_parent;
9556 +       p = d_path(&path, buf, len);
9557 +       if (IS_ERR(p))
9558 +               goto out;
9559 +       if (n != 1)
9560 +               p += n;
9561 +
9562 +       path.mnt = au_mnt_get(sb);
9563 +       path.dentry = sb->s_root;
9564 +       p = d_path(&path, buf, len - strlen(p));
9565 +       mntput(path.mnt);
9566 +       if (IS_ERR(p))
9567 +               goto out;
9568 +       if (n != 1)
9569 +               p[strlen(p)] = '/';
9570 +
9571 +out:
9572 +       AuTraceErrPtr(p);
9573 +       return p;
9574 +}
9575 +
9576 +static
9577 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
9578 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
9579 +{
9580 +       struct dentry *dentry, *h_parent, *root;
9581 +       struct super_block *h_sb;
9582 +       char *pathname, *p;
9583 +       struct vfsmount *h_mnt;
9584 +       struct au_branch *br;
9585 +       int err;
9586 +       struct path path;
9587 +
9588 +       br = au_sbr(sb, nsi_lock->bindex);
9589 +       h_mnt = br->br_mnt;
9590 +       h_sb = h_mnt->mnt_sb;
9591 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
9592 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
9593 +                                     fh_len - Fh_tail, fh[Fh_h_type],
9594 +                                     h_acceptable, /*context*/NULL);
9595 +       dentry = h_parent;
9596 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
9597 +               AuWarn1("%s decode_fh failed, %ld\n",
9598 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
9599 +               goto out;
9600 +       }
9601 +       dentry = NULL;
9602 +       if (unlikely(au_test_anon(h_parent))) {
9603 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
9604 +                       au_sbtype(h_sb));
9605 +               goto out_h_parent;
9606 +       }
9607 +
9608 +       dentry = ERR_PTR(-ENOMEM);
9609 +       pathname = (void *)__get_free_page(GFP_NOFS);
9610 +       if (unlikely(!pathname))
9611 +               goto out_h_parent;
9612 +
9613 +       root = sb->s_root;
9614 +       path.mnt = h_mnt;
9615 +       di_read_lock_parent(root, !AuLock_IR);
9616 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
9617 +       di_read_unlock(root, !AuLock_IR);
9618 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
9619 +       dentry = (void *)p;
9620 +       if (IS_ERR(p))
9621 +               goto out_pathname;
9622 +
9623 +       si_read_unlock(sb);
9624 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
9625 +       dentry = ERR_PTR(err);
9626 +       if (unlikely(err))
9627 +               goto out_relock;
9628 +
9629 +       dentry = ERR_PTR(-ENOENT);
9630 +       AuDebugOn(au_test_anon(path.dentry));
9631 +       if (unlikely(!path.dentry->d_inode))
9632 +               goto out_path;
9633 +
9634 +       if (ino != path.dentry->d_inode->i_ino)
9635 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
9636 +       else
9637 +               dentry = dget(path.dentry);
9638 +
9639 +out_path:
9640 +       path_put(&path);
9641 +out_relock:
9642 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
9643 +               if (!IS_ERR(dentry)) {
9644 +                       dput(dentry);
9645 +                       dentry = ERR_PTR(-ESTALE);
9646 +               }
9647 +out_pathname:
9648 +       free_page((unsigned long)pathname);
9649 +out_h_parent:
9650 +       dput(h_parent);
9651 +out:
9652 +       AuTraceErrPtr(dentry);
9653 +       return dentry;
9654 +}
9655 +
9656 +/* ---------------------------------------------------------------------- */
9657 +
9658 +static struct dentry *
9659 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
9660 +                 int fh_type)
9661 +{
9662 +       struct dentry *dentry;
9663 +       __u32 *fh = fid->raw;
9664 +       struct au_branch *br;
9665 +       ino_t ino, dir_ino;
9666 +       struct au_nfsd_si_lock nsi_lock = {
9667 +               .force_lock     = 0
9668 +       };
9669 +
9670 +       dentry = ERR_PTR(-ESTALE);
9671 +       /* it should never happen, but the file handle is unreliable */
9672 +       if (unlikely(fh_len < Fh_tail))
9673 +               goto out;
9674 +       nsi_lock.sigen = fh[Fh_sigen];
9675 +       nsi_lock.br_id = fh[Fh_br_id];
9676 +
9677 +       /* branch id may be wrapped around */
9678 +       br = NULL;
9679 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
9680 +               goto out;
9681 +       nsi_lock.force_lock = 1;
9682 +
9683 +       /* is this inode still cached? */
9684 +       ino = decode_ino(fh + Fh_ino);
9685 +       /* it should never happen */
9686 +       if (unlikely(ino == AUFS_ROOT_INO))
9687 +               goto out;
9688 +
9689 +       dir_ino = decode_ino(fh + Fh_dir_ino);
9690 +       dentry = decode_by_ino(sb, ino, dir_ino);
9691 +       if (IS_ERR(dentry))
9692 +               goto out_unlock;
9693 +       if (dentry)
9694 +               goto accept;
9695 +
9696 +       /* is the parent dir cached? */
9697 +       br = au_sbr(sb, nsi_lock.bindex);
9698 +       atomic_inc(&br->br_count);
9699 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
9700 +       if (IS_ERR(dentry))
9701 +               goto out_unlock;
9702 +       if (dentry)
9703 +               goto accept;
9704 +
9705 +       /* lookup path */
9706 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
9707 +       if (IS_ERR(dentry))
9708 +               goto out_unlock;
9709 +       if (unlikely(!dentry))
9710 +               /* todo?: make it ESTALE */
9711 +               goto out_unlock;
9712 +
9713 +accept:
9714 +       if (!au_digen_test(dentry, au_sigen(sb))
9715 +           && dentry->d_inode->i_generation == fh[Fh_igen])
9716 +               goto out_unlock; /* success */
9717 +
9718 +       dput(dentry);
9719 +       dentry = ERR_PTR(-ESTALE);
9720 +out_unlock:
9721 +       if (br)
9722 +               atomic_dec(&br->br_count);
9723 +       si_read_unlock(sb);
9724 +out:
9725 +       AuTraceErrPtr(dentry);
9726 +       return dentry;
9727 +}
9728 +
9729 +#if 0 /* reserved for future use */
9730 +/* support subtreecheck option */
9731 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
9732 +                                       int fh_len, int fh_type)
9733 +{
9734 +       struct dentry *parent;
9735 +       __u32 *fh = fid->raw;
9736 +       ino_t dir_ino;
9737 +
9738 +       dir_ino = decode_ino(fh + Fh_dir_ino);
9739 +       parent = decode_by_ino(sb, dir_ino, 0);
9740 +       if (IS_ERR(parent))
9741 +               goto out;
9742 +       if (!parent)
9743 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
9744 +                                       dir_ino, fh, fh_len);
9745 +
9746 +out:
9747 +       AuTraceErrPtr(parent);
9748 +       return parent;
9749 +}
9750 +#endif
9751 +
9752 +/* ---------------------------------------------------------------------- */
9753 +
9754 +static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
9755 +                         int connectable)
9756 +{
9757 +       int err;
9758 +       aufs_bindex_t bindex, bend;
9759 +       struct super_block *sb, *h_sb;
9760 +       struct inode *inode;
9761 +       struct dentry *parent, *h_parent;
9762 +       struct au_branch *br;
9763 +
9764 +       AuDebugOn(au_test_anon(dentry));
9765 +
9766 +       parent = NULL;
9767 +       err = -ENOSPC;
9768 +       if (unlikely(*max_len <= Fh_tail)) {
9769 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
9770 +               goto out;
9771 +       }
9772 +
9773 +       err = FILEID_ROOT;
9774 +       if (IS_ROOT(dentry)) {
9775 +               AuDebugOn(dentry->d_inode->i_ino != AUFS_ROOT_INO);
9776 +               goto out;
9777 +       }
9778 +
9779 +       h_parent = NULL;
9780 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_IR | AuLock_GEN);
9781 +       if (unlikely(err))
9782 +               goto out;
9783 +
9784 +       inode = dentry->d_inode;
9785 +       AuDebugOn(!inode);
9786 +       sb = dentry->d_sb;
9787 +#ifdef CONFIG_AUFS_DEBUG
9788 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9789 +               AuWarn1("NFS-exporting requires xino\n");
9790 +#endif
9791 +       err = -EIO;
9792 +       parent = dget_parent(dentry);
9793 +       di_read_lock_parent(parent, !AuLock_IR);
9794 +       bend = au_dbtaildir(parent);
9795 +       for (bindex = au_dbstart(parent); bindex <= bend; bindex++) {
9796 +               h_parent = au_h_dptr(parent, bindex);
9797 +               if (h_parent) {
9798 +                       dget(h_parent);
9799 +                       break;
9800 +               }
9801 +       }
9802 +       if (unlikely(!h_parent))
9803 +               goto out_unlock;
9804 +
9805 +       err = -EPERM;
9806 +       br = au_sbr(sb, bindex);
9807 +       h_sb = br->br_mnt->mnt_sb;
9808 +       if (unlikely(!h_sb->s_export_op)) {
9809 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
9810 +               goto out_dput;
9811 +       }
9812 +
9813 +       fh[Fh_br_id] = br->br_id;
9814 +       fh[Fh_sigen] = au_sigen(sb);
9815 +       encode_ino(fh + Fh_ino, inode->i_ino);
9816 +       encode_ino(fh + Fh_dir_ino, parent->d_inode->i_ino);
9817 +       fh[Fh_igen] = inode->i_generation;
9818 +
9819 +       *max_len -= Fh_tail;
9820 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
9821 +                                          max_len,
9822 +                                          /*connectable or subtreecheck*/0);
9823 +       err = fh[Fh_h_type];
9824 +       *max_len += Fh_tail;
9825 +       /* todo: macros? */
9826 +       if (err != 255)
9827 +               err = 99;
9828 +       else
9829 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
9830 +
9831 +out_dput:
9832 +       dput(h_parent);
9833 +out_unlock:
9834 +       di_read_unlock(parent, !AuLock_IR);
9835 +       dput(parent);
9836 +       aufs_read_unlock(dentry, AuLock_IR);
9837 +out:
9838 +       if (unlikely(err < 0))
9839 +               err = 255;
9840 +       return err;
9841 +}
9842 +
9843 +/* ---------------------------------------------------------------------- */
9844 +
9845 +static int aufs_commit_metadata(struct inode *inode)
9846 +{
9847 +       int err;
9848 +       aufs_bindex_t bindex;
9849 +       struct super_block *sb;
9850 +       struct inode *h_inode;
9851 +       int (*f)(struct inode *inode);
9852 +
9853 +       sb = inode->i_sb;
9854 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
9855 +       ii_write_lock_child(inode);
9856 +       bindex = au_ibstart(inode);
9857 +       AuDebugOn(bindex < 0);
9858 +       h_inode = au_h_iptr(inode, bindex);
9859 +
9860 +       f = h_inode->i_sb->s_export_op->commit_metadata;
9861 +       if (f)
9862 +               err = f(h_inode);
9863 +       else {
9864 +               struct writeback_control wbc = {
9865 +                       .sync_mode      = WB_SYNC_ALL,
9866 +                       .nr_to_write    = 0 /* metadata only */
9867 +               };
9868 +
9869 +               err = sync_inode(h_inode, &wbc);
9870 +       }
9871 +
9872 +       au_cpup_attr_timesizes(inode);
9873 +       ii_write_unlock(inode);
9874 +       si_read_unlock(sb);
9875 +       return err;
9876 +}
9877 +
9878 +/* ---------------------------------------------------------------------- */
9879 +
9880 +static struct export_operations aufs_export_op = {
9881 +       .fh_to_dentry           = aufs_fh_to_dentry,
9882 +       /* .fh_to_parent        = aufs_fh_to_parent, */
9883 +       .encode_fh              = aufs_encode_fh,
9884 +       .commit_metadata        = aufs_commit_metadata
9885 +};
9886 +
9887 +void au_export_init(struct super_block *sb)
9888 +{
9889 +       struct au_sbinfo *sbinfo;
9890 +       __u32 u;
9891 +
9892 +       sb->s_export_op = &aufs_export_op;
9893 +       sbinfo = au_sbi(sb);
9894 +       sbinfo->si_xigen = NULL;
9895 +       get_random_bytes(&u, sizeof(u));
9896 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
9897 +       atomic_set(&sbinfo->si_xigen_next, u);
9898 +}
9899 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
9900 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
9901 +++ linux/fs/aufs/file.c        2011-08-24 13:30:24.731313534 +0200
9902 @@ -0,0 +1,676 @@
9903 +/*
9904 + * Copyright (C) 2005-2011 Junjiro R. Okajima
9905 + *
9906 + * This program, aufs is free software; you can redistribute it and/or modify
9907 + * it under the terms of the GNU General Public License as published by
9908 + * the Free Software Foundation; either version 2 of the License, or
9909 + * (at your option) any later version.
9910 + *
9911 + * This program is distributed in the hope that it will be useful,
9912 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9913 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9914 + * GNU General Public License for more details.
9915 + *
9916 + * You should have received a copy of the GNU General Public License
9917 + * along with this program; if not, write to the Free Software
9918 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9919 + */
9920 +
9921 +/*
9922 + * handling file/dir, and address_space operation
9923 + */
9924 +
9925 +#include <linux/file.h>
9926 +#include <linux/fsnotify.h>
9927 +#include <linux/namei.h>
9928 +#include <linux/pagemap.h>
9929 +#include "aufs.h"
9930 +
9931 +/* drop flags for writing */
9932 +unsigned int au_file_roflags(unsigned int flags)
9933 +{
9934 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
9935 +       flags |= O_RDONLY | O_NOATIME;
9936 +       return flags;
9937 +}
9938 +
9939 +/* common functions to regular file and dir */
9940 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
9941 +                      struct file *file)
9942 +{
9943 +       struct file *h_file;
9944 +       struct dentry *h_dentry;
9945 +       struct inode *h_inode;
9946 +       struct super_block *sb;
9947 +       struct au_branch *br;
9948 +       struct path h_path;
9949 +       int err, exec_flag;
9950 +
9951 +       /* a race condition can happen between open and unlink/rmdir */
9952 +       h_file = ERR_PTR(-ENOENT);
9953 +       h_dentry = au_h_dptr(dentry, bindex);
9954 +       if (au_test_nfsd() && !h_dentry)
9955 +               goto out;
9956 +       h_inode = h_dentry->d_inode;
9957 +       if (au_test_nfsd() && !h_inode)
9958 +               goto out;
9959 +       spin_lock(&h_dentry->d_lock);
9960 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
9961 +               || !h_inode
9962 +               /* || !dentry->d_inode->i_nlink */
9963 +               ;
9964 +       spin_unlock(&h_dentry->d_lock);
9965 +       if (unlikely(err))
9966 +               goto out;
9967 +
9968 +       sb = dentry->d_sb;
9969 +       br = au_sbr(sb, bindex);
9970 +       h_file = ERR_PTR(-EACCES);
9971 +       exec_flag = flags & __FMODE_EXEC;
9972 +       if (exec_flag && (br->br_mnt->mnt_flags & MNT_NOEXEC))
9973 +               goto out;
9974 +
9975 +       /* drop flags for writing */
9976 +       if (au_test_ro(sb, bindex, dentry->d_inode))
9977 +               flags = au_file_roflags(flags);
9978 +       flags &= ~O_CREAT;
9979 +       atomic_inc(&br->br_count);
9980 +       h_path.dentry = h_dentry;
9981 +       h_path.mnt = br->br_mnt;
9982 +       if (!au_special_file(h_inode->i_mode))
9983 +               h_file = vfsub_dentry_open(&h_path, flags);
9984 +       else {
9985 +               /* this block depends upon the configuration */
9986 +               di_read_unlock(dentry, AuLock_IR);
9987 +               fi_write_unlock(file);
9988 +               si_read_unlock(sb);
9989 +               h_file = vfsub_dentry_open(&h_path, flags);
9990 +               si_noflush_read_lock(sb);
9991 +               fi_write_lock(file);
9992 +               di_read_lock_child(dentry, AuLock_IR);
9993 +       }
9994 +       if (IS_ERR(h_file))
9995 +               goto out_br;
9996 +
9997 +       if (exec_flag) {
9998 +               err = deny_write_access(h_file);
9999 +               if (unlikely(err)) {
10000 +                       fput(h_file);
10001 +                       h_file = ERR_PTR(err);
10002 +                       goto out_br;
10003 +               }
10004 +       }
10005 +       fsnotify_open(h_file);
10006 +       goto out; /* success */
10007 +
10008 +out_br:
10009 +       atomic_dec(&br->br_count);
10010 +out:
10011 +       return h_file;
10012 +}
10013 +
10014 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10015 +              struct au_fidir *fidir)
10016 +{
10017 +       int err;
10018 +       struct dentry *dentry;
10019 +
10020 +       err = au_finfo_init(file, fidir);
10021 +       if (unlikely(err))
10022 +               goto out;
10023 +
10024 +       dentry = file->f_dentry;
10025 +       di_read_lock_child(dentry, AuLock_IR);
10026 +       err = open(file, vfsub_file_flags(file));
10027 +       di_read_unlock(dentry, AuLock_IR);
10028 +
10029 +       fi_write_unlock(file);
10030 +       if (unlikely(err)) {
10031 +               au_fi(file)->fi_hdir = NULL;
10032 +               au_finfo_fin(file);
10033 +       }
10034 +
10035 +out:
10036 +       return err;
10037 +}
10038 +
10039 +int au_reopen_nondir(struct file *file)
10040 +{
10041 +       int err;
10042 +       aufs_bindex_t bstart;
10043 +       struct dentry *dentry;
10044 +       struct file *h_file, *h_file_tmp;
10045 +
10046 +       dentry = file->f_dentry;
10047 +       AuDebugOn(au_special_file(dentry->d_inode->i_mode));
10048 +       bstart = au_dbstart(dentry);
10049 +       h_file_tmp = NULL;
10050 +       if (au_fbstart(file) == bstart) {
10051 +               h_file = au_hf_top(file);
10052 +               if (file->f_mode == h_file->f_mode)
10053 +                       return 0; /* success */
10054 +               h_file_tmp = h_file;
10055 +               get_file(h_file_tmp);
10056 +               au_set_h_fptr(file, bstart, NULL);
10057 +       }
10058 +       AuDebugOn(au_fi(file)->fi_hdir);
10059 +       AuDebugOn(au_fbstart(file) < bstart);
10060 +
10061 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
10062 +                          file);
10063 +       err = PTR_ERR(h_file);
10064 +       if (IS_ERR(h_file))
10065 +               goto out; /* todo: close all? */
10066 +
10067 +       err = 0;
10068 +       au_set_fbstart(file, bstart);
10069 +       au_set_h_fptr(file, bstart, h_file);
10070 +       au_update_figen(file);
10071 +       /* todo: necessary? */
10072 +       /* file->f_ra = h_file->f_ra; */
10073 +
10074 +out:
10075 +       if (h_file_tmp)
10076 +               fput(h_file_tmp);
10077 +       return err;
10078 +}
10079 +
10080 +/* ---------------------------------------------------------------------- */
10081 +
10082 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
10083 +                       struct dentry *hi_wh)
10084 +{
10085 +       int err;
10086 +       aufs_bindex_t bstart;
10087 +       struct au_dinfo *dinfo;
10088 +       struct dentry *h_dentry;
10089 +       struct au_hdentry *hdp;
10090 +
10091 +       dinfo = au_di(file->f_dentry);
10092 +       AuRwMustWriteLock(&dinfo->di_rwsem);
10093 +
10094 +       bstart = dinfo->di_bstart;
10095 +       dinfo->di_bstart = btgt;
10096 +       hdp = dinfo->di_hdentry;
10097 +       h_dentry = hdp[0 + btgt].hd_dentry;
10098 +       hdp[0 + btgt].hd_dentry = hi_wh;
10099 +       err = au_reopen_nondir(file);
10100 +       hdp[0 + btgt].hd_dentry = h_dentry;
10101 +       dinfo->di_bstart = bstart;
10102 +
10103 +       return err;
10104 +}
10105 +
10106 +static int au_ready_to_write_wh(struct file *file, loff_t len,
10107 +                               aufs_bindex_t bcpup)
10108 +{
10109 +       int err;
10110 +       struct inode *inode, *h_inode;
10111 +       struct dentry *dentry, *h_dentry, *hi_wh;
10112 +
10113 +       dentry = file->f_dentry;
10114 +       au_update_dbstart(dentry);
10115 +       inode = dentry->d_inode;
10116 +       h_inode = NULL;
10117 +       if (au_dbstart(dentry) <= bcpup && au_dbend(dentry) >= bcpup) {
10118 +               h_dentry = au_h_dptr(dentry, bcpup);
10119 +               if (h_dentry)
10120 +                       h_inode = h_dentry->d_inode;
10121 +       }
10122 +       hi_wh = au_hi_wh(inode, bcpup);
10123 +       if (!hi_wh && !h_inode)
10124 +               err = au_sio_cpup_wh(dentry, bcpup, len, file);
10125 +       else
10126 +               /* already copied-up after unlink */
10127 +               err = au_reopen_wh(file, bcpup, hi_wh);
10128 +
10129 +       if (!err
10130 +           && inode->i_nlink > 1
10131 +           && au_opt_test(au_mntflags(dentry->d_sb), PLINK))
10132 +               au_plink_append(inode, bcpup, au_h_dptr(dentry, bcpup));
10133 +
10134 +       return err;
10135 +}
10136 +
10137 +/*
10138 + * prepare the @file for writing.
10139 + */
10140 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
10141 +{
10142 +       int err;
10143 +       aufs_bindex_t bstart, bcpup, dbstart;
10144 +       struct dentry *dentry, *parent, *h_dentry;
10145 +       struct inode *h_inode, *inode;
10146 +       struct super_block *sb;
10147 +       struct file *h_file;
10148 +
10149 +       dentry = file->f_dentry;
10150 +       sb = dentry->d_sb;
10151 +       inode = dentry->d_inode;
10152 +       AuDebugOn(au_special_file(inode->i_mode));
10153 +       bstart = au_fbstart(file);
10154 +       err = au_test_ro(sb, bstart, inode);
10155 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
10156 +               err = au_pin(pin, dentry, bstart, AuOpt_UDBA_NONE, /*flags*/0);
10157 +               goto out;
10158 +       }
10159 +
10160 +       /* need to cpup or reopen */
10161 +       parent = dget_parent(dentry);
10162 +       di_write_lock_parent(parent);
10163 +       err = AuWbrCopyup(au_sbi(sb), dentry);
10164 +       bcpup = err;
10165 +       if (unlikely(err < 0))
10166 +               goto out_dgrade;
10167 +       err = 0;
10168 +
10169 +       if (!d_unhashed(dentry) && !au_h_dptr(parent, bcpup)) {
10170 +               err = au_cpup_dirs(dentry, bcpup);
10171 +               if (unlikely(err))
10172 +                       goto out_dgrade;
10173 +       }
10174 +
10175 +       err = au_pin(pin, dentry, bcpup, AuOpt_UDBA_NONE,
10176 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10177 +       if (unlikely(err))
10178 +               goto out_dgrade;
10179 +
10180 +       h_dentry = au_hf_top(file)->f_dentry;
10181 +       h_inode = h_dentry->d_inode;
10182 +       dbstart = au_dbstart(dentry);
10183 +       if (dbstart <= bcpup) {
10184 +               h_dentry = au_h_dptr(dentry, bcpup);
10185 +               AuDebugOn(!h_dentry);
10186 +               h_inode = h_dentry->d_inode;
10187 +               AuDebugOn(!h_inode);
10188 +               bstart = bcpup;
10189 +       }
10190 +
10191 +       if (dbstart <= bcpup            /* just reopen */
10192 +           || !d_unhashed(dentry)      /* copyup and reopen */
10193 +               ) {
10194 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
10195 +               h_file = au_h_open_pre(dentry, bstart);
10196 +               if (IS_ERR(h_file)) {
10197 +                       err = PTR_ERR(h_file);
10198 +                       h_file = NULL;
10199 +               } else {
10200 +                       di_downgrade_lock(parent, AuLock_IR);
10201 +                       if (dbstart > bcpup)
10202 +                               err = au_sio_cpup_simple(dentry, bcpup, len,
10203 +                                                        AuCpup_DTIME);
10204 +                       if (!err)
10205 +                               err = au_reopen_nondir(file);
10206 +               }
10207 +               mutex_unlock(&h_inode->i_mutex);
10208 +               au_h_open_post(dentry, bstart, h_file);
10209 +       } else {                        /* copyup as wh and reopen */
10210 +               /*
10211 +                * since writable hfsplus branch is not supported,
10212 +                * h_open_pre/post() are unnecessary.
10213 +                */
10214 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
10215 +               err = au_ready_to_write_wh(file, len, bcpup);
10216 +               di_downgrade_lock(parent, AuLock_IR);
10217 +               mutex_unlock(&h_inode->i_mutex);
10218 +       }
10219 +
10220 +       if (!err) {
10221 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
10222 +               goto out_dput; /* success */
10223 +       }
10224 +       au_unpin(pin);
10225 +       goto out_unlock;
10226 +
10227 +out_dgrade:
10228 +       di_downgrade_lock(parent, AuLock_IR);
10229 +out_unlock:
10230 +       di_read_unlock(parent, AuLock_IR);
10231 +out_dput:
10232 +       dput(parent);
10233 +out:
10234 +       return err;
10235 +}
10236 +
10237 +/* ---------------------------------------------------------------------- */
10238 +
10239 +int au_do_flush(struct file *file, fl_owner_t id,
10240 +               int (*flush)(struct file *file, fl_owner_t id))
10241 +{
10242 +       int err;
10243 +       struct dentry *dentry;
10244 +       struct super_block *sb;
10245 +       struct inode *inode;
10246 +
10247 +       dentry = file->f_dentry;
10248 +       sb = dentry->d_sb;
10249 +       inode = dentry->d_inode;
10250 +       si_noflush_read_lock(sb);
10251 +       fi_read_lock(file);
10252 +       ii_read_lock_child(inode);
10253 +
10254 +       err = flush(file, id);
10255 +       au_cpup_attr_timesizes(inode);
10256 +
10257 +       ii_read_unlock(inode);
10258 +       fi_read_unlock(file);
10259 +       si_read_unlock(sb);
10260 +       return err;
10261 +}
10262 +
10263 +/* ---------------------------------------------------------------------- */
10264 +
10265 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
10266 +{
10267 +       int err;
10268 +       aufs_bindex_t bstart;
10269 +       struct au_pin pin;
10270 +       struct au_finfo *finfo;
10271 +       struct dentry *dentry, *parent, *hi_wh;
10272 +       struct inode *inode;
10273 +       struct super_block *sb;
10274 +
10275 +       FiMustWriteLock(file);
10276 +
10277 +       err = 0;
10278 +       finfo = au_fi(file);
10279 +       dentry = file->f_dentry;
10280 +       sb = dentry->d_sb;
10281 +       inode = dentry->d_inode;
10282 +       bstart = au_ibstart(inode);
10283 +       if (bstart == finfo->fi_btop || IS_ROOT(dentry))
10284 +               goto out;
10285 +
10286 +       parent = dget_parent(dentry);
10287 +       if (au_test_ro(sb, bstart, inode)) {
10288 +               di_read_lock_parent(parent, !AuLock_IR);
10289 +               err = AuWbrCopyup(au_sbi(sb), dentry);
10290 +               bstart = err;
10291 +               di_read_unlock(parent, !AuLock_IR);
10292 +               if (unlikely(err < 0))
10293 +                       goto out_parent;
10294 +               err = 0;
10295 +       }
10296 +
10297 +       di_read_lock_parent(parent, AuLock_IR);
10298 +       hi_wh = au_hi_wh(inode, bstart);
10299 +       if (!S_ISDIR(inode->i_mode)
10300 +           && au_opt_test(au_mntflags(sb), PLINK)
10301 +           && au_plink_test(inode)
10302 +           && !d_unhashed(dentry)) {
10303 +               err = au_test_and_cpup_dirs(dentry, bstart);
10304 +               if (unlikely(err))
10305 +                       goto out_unlock;
10306 +
10307 +               /* always superio. */
10308 +               err = au_pin(&pin, dentry, bstart, AuOpt_UDBA_NONE,
10309 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10310 +               if (!err)
10311 +                       err = au_sio_cpup_simple(dentry, bstart, -1,
10312 +                                                AuCpup_DTIME);
10313 +               au_unpin(&pin);
10314 +       } else if (hi_wh) {
10315 +               /* already copied-up after unlink */
10316 +               err = au_reopen_wh(file, bstart, hi_wh);
10317 +               *need_reopen = 0;
10318 +       }
10319 +
10320 +out_unlock:
10321 +       di_read_unlock(parent, AuLock_IR);
10322 +out_parent:
10323 +       dput(parent);
10324 +out:
10325 +       return err;
10326 +}
10327 +
10328 +static void au_do_refresh_dir(struct file *file)
10329 +{
10330 +       aufs_bindex_t bindex, bend, new_bindex, brid;
10331 +       struct au_hfile *p, tmp, *q;
10332 +       struct au_finfo *finfo;
10333 +       struct super_block *sb;
10334 +       struct au_fidir *fidir;
10335 +
10336 +       FiMustWriteLock(file);
10337 +
10338 +       sb = file->f_dentry->d_sb;
10339 +       finfo = au_fi(file);
10340 +       fidir = finfo->fi_hdir;
10341 +       AuDebugOn(!fidir);
10342 +       p = fidir->fd_hfile + finfo->fi_btop;
10343 +       brid = p->hf_br->br_id;
10344 +       bend = fidir->fd_bbot;
10345 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
10346 +               if (!p->hf_file)
10347 +                       continue;
10348 +
10349 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
10350 +               if (new_bindex == bindex)
10351 +                       continue;
10352 +               if (new_bindex < 0) {
10353 +                       au_set_h_fptr(file, bindex, NULL);
10354 +                       continue;
10355 +               }
10356 +
10357 +               /* swap two lower inode, and loop again */
10358 +               q = fidir->fd_hfile + new_bindex;
10359 +               tmp = *q;
10360 +               *q = *p;
10361 +               *p = tmp;
10362 +               if (tmp.hf_file) {
10363 +                       bindex--;
10364 +                       p--;
10365 +               }
10366 +       }
10367 +
10368 +       p = fidir->fd_hfile;
10369 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
10370 +               bend = au_sbend(sb);
10371 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
10372 +                    finfo->fi_btop++, p++)
10373 +                       if (p->hf_file) {
10374 +                               if (p->hf_file->f_dentry
10375 +                                   && p->hf_file->f_dentry->d_inode)
10376 +                                       break;
10377 +                               else
10378 +                                       au_hfput(p, file);
10379 +                       }
10380 +       } else {
10381 +               bend = au_br_index(sb, brid);
10382 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
10383 +                    finfo->fi_btop++, p++)
10384 +                       if (p->hf_file)
10385 +                               au_hfput(p, file);
10386 +               bend = au_sbend(sb);
10387 +       }
10388 +
10389 +       p = fidir->fd_hfile + bend;
10390 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
10391 +            fidir->fd_bbot--, p--)
10392 +               if (p->hf_file) {
10393 +                       if (p->hf_file->f_dentry
10394 +                           && p->hf_file->f_dentry->d_inode)
10395 +                               break;
10396 +                       else
10397 +                               au_hfput(p, file);
10398 +               }
10399 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
10400 +}
10401 +
10402 +/*
10403 + * after branch manipulating, refresh the file.
10404 + */
10405 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
10406 +{
10407 +       int err, need_reopen;
10408 +       aufs_bindex_t bend, bindex;
10409 +       struct dentry *dentry;
10410 +       struct au_finfo *finfo;
10411 +       struct au_hfile *hfile;
10412 +
10413 +       dentry = file->f_dentry;
10414 +       finfo = au_fi(file);
10415 +       if (!finfo->fi_hdir) {
10416 +               hfile = &finfo->fi_htop;
10417 +               AuDebugOn(!hfile->hf_file);
10418 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
10419 +               AuDebugOn(bindex < 0);
10420 +               if (bindex != finfo->fi_btop)
10421 +                       au_set_fbstart(file, bindex);
10422 +       } else {
10423 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
10424 +               if (unlikely(err))
10425 +                       goto out;
10426 +               au_do_refresh_dir(file);
10427 +       }
10428 +
10429 +       err = 0;
10430 +       need_reopen = 1;
10431 +       if (!au_test_mmapped(file))
10432 +               err = au_file_refresh_by_inode(file, &need_reopen);
10433 +       if (!err && need_reopen && !d_unlinked(dentry))
10434 +               err = reopen(file);
10435 +       if (!err) {
10436 +               au_update_figen(file);
10437 +               goto out; /* success */
10438 +       }
10439 +
10440 +       /* error, close all lower files */
10441 +       if (finfo->fi_hdir) {
10442 +               bend = au_fbend_dir(file);
10443 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
10444 +                       au_set_h_fptr(file, bindex, NULL);
10445 +       }
10446 +
10447 +out:
10448 +       return err;
10449 +}
10450 +
10451 +/* common function to regular file and dir */
10452 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10453 +                         int wlock)
10454 +{
10455 +       int err;
10456 +       unsigned int sigen, figen;
10457 +       aufs_bindex_t bstart;
10458 +       unsigned char pseudo_link;
10459 +       struct dentry *dentry;
10460 +       struct inode *inode;
10461 +
10462 +       err = 0;
10463 +       dentry = file->f_dentry;
10464 +       inode = dentry->d_inode;
10465 +       AuDebugOn(au_special_file(inode->i_mode));
10466 +       sigen = au_sigen(dentry->d_sb);
10467 +       fi_write_lock(file);
10468 +       figen = au_figen(file);
10469 +       di_write_lock_child(dentry);
10470 +       bstart = au_dbstart(dentry);
10471 +       pseudo_link = (bstart != au_ibstart(inode));
10472 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
10473 +               if (!wlock) {
10474 +                       di_downgrade_lock(dentry, AuLock_IR);
10475 +                       fi_downgrade_lock(file);
10476 +               }
10477 +               goto out; /* success */
10478 +       }
10479 +
10480 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
10481 +       if (au_digen_test(dentry, sigen)) {
10482 +               err = au_reval_dpath(dentry, sigen);
10483 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
10484 +       }
10485 +
10486 +       if (!err)
10487 +               err = refresh_file(file, reopen);
10488 +       if (!err) {
10489 +               if (!wlock) {
10490 +                       di_downgrade_lock(dentry, AuLock_IR);
10491 +                       fi_downgrade_lock(file);
10492 +               }
10493 +       } else {
10494 +               di_write_unlock(dentry);
10495 +               fi_write_unlock(file);
10496 +       }
10497 +
10498 +out:
10499 +       return err;
10500 +}
10501 +
10502 +/* ---------------------------------------------------------------------- */
10503 +
10504 +/* cf. aufs_nopage() */
10505 +/* for madvise(2) */
10506 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
10507 +{
10508 +       unlock_page(page);
10509 +       return 0;
10510 +}
10511 +
10512 +/* it will never be called, but necessary to support O_DIRECT */
10513 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
10514 +                             const struct iovec *iov, loff_t offset,
10515 +                             unsigned long nr_segs)
10516 +{ BUG(); return 0; }
10517 +
10518 +/*
10519 + * it will never be called, but madvise and fadvise behaves differently
10520 + * when get_xip_mem is defined
10521 + */
10522 +static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
10523 +                           int create, void **kmem, unsigned long *pfn)
10524 +{ BUG(); return 0; }
10525 +
10526 +/* they will never be called. */
10527 +#ifdef CONFIG_AUFS_DEBUG
10528 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
10529 +                           loff_t pos, unsigned len, unsigned flags,
10530 +                           struct page **pagep, void **fsdata)
10531 +{ AuUnsupport(); return 0; }
10532 +static int aufs_write_end(struct file *file, struct address_space *mapping,
10533 +                         loff_t pos, unsigned len, unsigned copied,
10534 +                         struct page *page, void *fsdata)
10535 +{ AuUnsupport(); return 0; }
10536 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
10537 +{ AuUnsupport(); return 0; }
10538 +
10539 +static int aufs_set_page_dirty(struct page *page)
10540 +{ AuUnsupport(); return 0; }
10541 +static void aufs_invalidatepage(struct page *page, unsigned long offset)
10542 +{ AuUnsupport(); }
10543 +static int aufs_releasepage(struct page *page, gfp_t gfp)
10544 +{ AuUnsupport(); return 0; }
10545 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
10546 +                           struct page *page)
10547 +{ AuUnsupport(); return 0; }
10548 +static int aufs_launder_page(struct page *page)
10549 +{ AuUnsupport(); return 0; }
10550 +static int aufs_is_partially_uptodate(struct page *page,
10551 +                                     read_descriptor_t *desc,
10552 +                                     unsigned long from)
10553 +{ AuUnsupport(); return 0; }
10554 +static int aufs_error_remove_page(struct address_space *mapping,
10555 +                                 struct page *page)
10556 +{ AuUnsupport(); return 0; }
10557 +#endif /* CONFIG_AUFS_DEBUG */
10558 +
10559 +const struct address_space_operations aufs_aop = {
10560 +       .readpage               = aufs_readpage,
10561 +       .direct_IO              = aufs_direct_IO,
10562 +       .get_xip_mem            = aufs_get_xip_mem,
10563 +#ifdef CONFIG_AUFS_DEBUG
10564 +       .writepage              = aufs_writepage,
10565 +       /* no writepages, because of writepage */
10566 +       .set_page_dirty         = aufs_set_page_dirty,
10567 +       /* no readpages, because of readpage */
10568 +       .write_begin            = aufs_write_begin,
10569 +       .write_end              = aufs_write_end,
10570 +       /* no bmap, no block device */
10571 +       .invalidatepage         = aufs_invalidatepage,
10572 +       .releasepage            = aufs_releasepage,
10573 +       .migratepage            = aufs_migratepage,
10574 +       .launder_page           = aufs_launder_page,
10575 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
10576 +       .error_remove_page      = aufs_error_remove_page
10577 +#endif /* CONFIG_AUFS_DEBUG */
10578 +};
10579 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
10580 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
10581 +++ linux/fs/aufs/file.h        2011-08-24 13:30:24.731313534 +0200
10582 @@ -0,0 +1,299 @@
10583 +/*
10584 + * Copyright (C) 2005-2011 Junjiro R. Okajima
10585 + *
10586 + * This program, aufs is free software; you can redistribute it and/or modify
10587 + * it under the terms of the GNU General Public License as published by
10588 + * the Free Software Foundation; either version 2 of the License, or
10589 + * (at your option) any later version.
10590 + *
10591 + * This program is distributed in the hope that it will be useful,
10592 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10593 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10594 + * GNU General Public License for more details.
10595 + *
10596 + * You should have received a copy of the GNU General Public License
10597 + * along with this program; if not, write to the Free Software
10598 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10599 + */
10600 +
10601 +/*
10602 + * file operations
10603 + */
10604 +
10605 +#ifndef __AUFS_FILE_H__
10606 +#define __AUFS_FILE_H__
10607 +
10608 +#ifdef __KERNEL__
10609 +
10610 +#include <linux/file.h>
10611 +#include <linux/fs.h>
10612 +#include <linux/poll.h>
10613 +#include <linux/aufs_type.h>
10614 +#include "rwsem.h"
10615 +
10616 +struct au_branch;
10617 +struct au_hfile {
10618 +       struct file             *hf_file;
10619 +       struct au_branch        *hf_br;
10620 +};
10621 +
10622 +struct au_vdir;
10623 +struct au_fidir {
10624 +       aufs_bindex_t           fd_bbot;
10625 +       aufs_bindex_t           fd_nent;
10626 +       struct au_vdir          *fd_vdir_cache;
10627 +       struct au_hfile         fd_hfile[];
10628 +};
10629 +
10630 +static inline int au_fidir_sz(int nent)
10631 +{
10632 +       AuDebugOn(nent < 0);
10633 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
10634 +}
10635 +
10636 +struct au_finfo {
10637 +       atomic_t                fi_generation;
10638 +
10639 +       struct au_rwsem         fi_rwsem;
10640 +       aufs_bindex_t           fi_btop;
10641 +
10642 +       /* do not union them */
10643 +       struct {                                /* for non-dir */
10644 +               struct au_hfile                 fi_htop;
10645 +               atomic_t                        fi_mmapped;
10646 +       };
10647 +       struct au_fidir         *fi_hdir;       /* for dir only */
10648 +} ____cacheline_aligned_in_smp;
10649 +
10650 +/* ---------------------------------------------------------------------- */
10651 +
10652 +/* file.c */
10653 +extern const struct address_space_operations aufs_aop;
10654 +unsigned int au_file_roflags(unsigned int flags);
10655 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10656 +                      struct file *file);
10657 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10658 +              struct au_fidir *fidir);
10659 +int au_reopen_nondir(struct file *file);
10660 +struct au_pin;
10661 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
10662 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10663 +                         int wlock);
10664 +int au_do_flush(struct file *file, fl_owner_t id,
10665 +               int (*flush)(struct file *file, fl_owner_t id));
10666 +
10667 +/* poll.c */
10668 +#ifdef CONFIG_AUFS_POLL
10669 +unsigned int aufs_poll(struct file *file, poll_table *wait);
10670 +#endif
10671 +
10672 +#ifdef CONFIG_AUFS_BR_HFSPLUS
10673 +/* hfsplus.c */
10674 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex);
10675 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
10676 +                   struct file *h_file);
10677 +#else
10678 +static inline
10679 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
10680 +{
10681 +       return NULL;
10682 +}
10683 +
10684 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
10685 +          struct file *h_file);
10686 +#endif
10687 +
10688 +/* f_op.c */
10689 +extern const struct file_operations aufs_file_fop;
10690 +int au_do_open_nondir(struct file *file, int flags);
10691 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
10692 +
10693 +#ifdef CONFIG_AUFS_SP_IATTR
10694 +/* f_op_sp.c */
10695 +int au_special_file(umode_t mode);
10696 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev);
10697 +#else
10698 +AuStubInt0(au_special_file, umode_t mode)
10699 +static inline void au_init_special_fop(struct inode *inode, umode_t mode,
10700 +                                      dev_t rdev)
10701 +{
10702 +       init_special_inode(inode, mode, rdev);
10703 +}
10704 +#endif
10705 +
10706 +/* finfo.c */
10707 +void au_hfput(struct au_hfile *hf, struct file *file);
10708 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
10709 +                  struct file *h_file);
10710 +
10711 +void au_update_figen(struct file *file);
10712 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
10713 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
10714 +
10715 +void au_fi_init_once(void *_fi);
10716 +void au_finfo_fin(struct file *file);
10717 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
10718 +
10719 +/* ioctl.c */
10720 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
10721 +#ifdef CONFIG_COMPAT
10722 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
10723 +                          unsigned long arg);
10724 +#endif
10725 +
10726 +/* ---------------------------------------------------------------------- */
10727 +
10728 +static inline struct au_finfo *au_fi(struct file *file)
10729 +{
10730 +       return file->private_data;
10731 +}
10732 +
10733 +/* ---------------------------------------------------------------------- */
10734 +
10735 +/*
10736 + * fi_read_lock, fi_write_lock,
10737 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
10738 + */
10739 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
10740 +
10741 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
10742 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
10743 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
10744 +
10745 +/* ---------------------------------------------------------------------- */
10746 +
10747 +/* todo: hard/soft set? */
10748 +static inline aufs_bindex_t au_fbstart(struct file *file)
10749 +{
10750 +       FiMustAnyLock(file);
10751 +       return au_fi(file)->fi_btop;
10752 +}
10753 +
10754 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
10755 +{
10756 +       FiMustAnyLock(file);
10757 +       AuDebugOn(!au_fi(file)->fi_hdir);
10758 +       return au_fi(file)->fi_hdir->fd_bbot;
10759 +}
10760 +
10761 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
10762 +{
10763 +       FiMustAnyLock(file);
10764 +       AuDebugOn(!au_fi(file)->fi_hdir);
10765 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
10766 +}
10767 +
10768 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
10769 +{
10770 +       FiMustWriteLock(file);
10771 +       au_fi(file)->fi_btop = bindex;
10772 +}
10773 +
10774 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
10775 +{
10776 +       FiMustWriteLock(file);
10777 +       AuDebugOn(!au_fi(file)->fi_hdir);
10778 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
10779 +}
10780 +
10781 +static inline void au_set_fvdir_cache(struct file *file,
10782 +                                     struct au_vdir *vdir_cache)
10783 +{
10784 +       FiMustWriteLock(file);
10785 +       AuDebugOn(!au_fi(file)->fi_hdir);
10786 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
10787 +}
10788 +
10789 +static inline struct file *au_hf_top(struct file *file)
10790 +{
10791 +       FiMustAnyLock(file);
10792 +       AuDebugOn(au_fi(file)->fi_hdir);
10793 +       return au_fi(file)->fi_htop.hf_file;
10794 +}
10795 +
10796 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
10797 +{
10798 +       FiMustAnyLock(file);
10799 +       AuDebugOn(!au_fi(file)->fi_hdir);
10800 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
10801 +}
10802 +
10803 +/* todo: memory barrier? */
10804 +static inline unsigned int au_figen(struct file *f)
10805 +{
10806 +       return atomic_read(&au_fi(f)->fi_generation);
10807 +}
10808 +
10809 +static inline void au_set_mmapped(struct file *f)
10810 +{
10811 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
10812 +               return;
10813 +       pr_warning("fi_mmapped wrapped around\n");
10814 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
10815 +               ;
10816 +}
10817 +
10818 +static inline void au_unset_mmapped(struct file *f)
10819 +{
10820 +       atomic_dec(&au_fi(f)->fi_mmapped);
10821 +}
10822 +
10823 +static inline int au_test_mmapped(struct file *f)
10824 +{
10825 +       return atomic_read(&au_fi(f)->fi_mmapped);
10826 +}
10827 +
10828 +/* customize vma->vm_file */
10829 +
10830 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
10831 +                                      struct file *file)
10832 +{
10833 +       struct file *f;
10834 +
10835 +       f = vma->vm_file;
10836 +       get_file(file);
10837 +       vma->vm_file = file;
10838 +       fput(f);
10839 +}
10840 +
10841 +#ifdef CONFIG_MMU
10842 +#define AuDbgVmRegion(file, vma) do {} while (0)
10843 +
10844 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
10845 +                                   struct file *file)
10846 +{
10847 +       au_do_vm_file_reset(vma, file);
10848 +}
10849 +#else
10850 +#define AuDbgVmRegion(file, vma) \
10851 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
10852 +
10853 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
10854 +                                   struct file *file)
10855 +{
10856 +       struct file *f;
10857 +
10858 +       au_do_vm_file_reset(vma, file);
10859 +       f = vma->vm_region->vm_file;
10860 +       get_file(file);
10861 +       vma->vm_region->vm_file = file;
10862 +       fput(f);
10863 +}
10864 +#endif /* CONFIG_MMU */
10865 +
10866 +/* handle vma->vm_prfile */
10867 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
10868 +                                   struct file *file)
10869 +{
10870 +#ifdef CONFIG_AUFS_PROC_MAP
10871 +       get_file(file);
10872 +       vma->vm_prfile = file;
10873 +#ifndef CONFIG_MMU
10874 +       get_file(file);
10875 +       vma->vm_region->vm_prfile = file;
10876 +#endif
10877 +#endif
10878 +}
10879 +
10880 +#endif /* __KERNEL__ */
10881 +#endif /* __AUFS_FILE_H__ */
10882 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
10883 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
10884 +++ linux/fs/aufs/finfo.c       2012-01-05 11:00:59.215501779 +0100
10885 @@ -0,0 +1,157 @@
10886 +/*
10887 + * Copyright (C) 2005-2011 Junjiro R. Okajima
10888 + *
10889 + * This program, aufs is free software; you can redistribute it and/or modify
10890 + * it under the terms of the GNU General Public License as published by
10891 + * the Free Software Foundation; either version 2 of the License, or
10892 + * (at your option) any later version.
10893 + *
10894 + * This program is distributed in the hope that it will be useful,
10895 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10896 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10897 + * GNU General Public License for more details.
10898 + *
10899 + * You should have received a copy of the GNU General Public License
10900 + * along with this program; if not, write to the Free Software
10901 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10902 + */
10903 +
10904 +/*
10905 + * file private data
10906 + */
10907 +
10908 +#include <linux/file.h>
10909 +#include "aufs.h"
10910 +
10911 +void au_hfput(struct au_hfile *hf, struct file *file)
10912 +{
10913 +       /* todo: direct access f_flags */
10914 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
10915 +               allow_write_access(hf->hf_file);
10916 +       fput(hf->hf_file);
10917 +       hf->hf_file = NULL;
10918 +       atomic_dec(&hf->hf_br->br_count);
10919 +       hf->hf_br = NULL;
10920 +}
10921 +
10922 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
10923 +{
10924 +       struct au_finfo *finfo = au_fi(file);
10925 +       struct au_hfile *hf;
10926 +       struct au_fidir *fidir;
10927 +
10928 +       fidir = finfo->fi_hdir;
10929 +       if (!fidir) {
10930 +               AuDebugOn(finfo->fi_btop != bindex);
10931 +               hf = &finfo->fi_htop;
10932 +       } else
10933 +               hf = fidir->fd_hfile + bindex;
10934 +
10935 +       if (hf && hf->hf_file)
10936 +               au_hfput(hf, file);
10937 +       if (val) {
10938 +               FiMustWriteLock(file);
10939 +               hf->hf_file = val;
10940 +               hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
10941 +       }
10942 +}
10943 +
10944 +void au_update_figen(struct file *file)
10945 +{
10946 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
10947 +       /* smp_mb(); */ /* atomic_set */
10948 +}
10949 +
10950 +/* ---------------------------------------------------------------------- */
10951 +
10952 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
10953 +{
10954 +       struct au_fidir *fidir;
10955 +       int nbr;
10956 +
10957 +       nbr = au_sbend(sb) + 1;
10958 +       if (nbr < 2)
10959 +               nbr = 2; /* initial allocate for 2 branches */
10960 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
10961 +       if (fidir) {
10962 +               fidir->fd_bbot = -1;
10963 +               fidir->fd_nent = nbr;
10964 +               fidir->fd_vdir_cache = NULL;
10965 +       }
10966 +
10967 +       return fidir;
10968 +}
10969 +
10970 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
10971 +{
10972 +       int err;
10973 +       struct au_fidir *fidir, *p;
10974 +
10975 +       AuRwMustWriteLock(&finfo->fi_rwsem);
10976 +       fidir = finfo->fi_hdir;
10977 +       AuDebugOn(!fidir);
10978 +
10979 +       err = -ENOMEM;
10980 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
10981 +                        GFP_NOFS);
10982 +       if (p) {
10983 +               p->fd_nent = nbr;
10984 +               finfo->fi_hdir = p;
10985 +               err = 0;
10986 +       }
10987 +
10988 +       return err;
10989 +}
10990 +
10991 +/* ---------------------------------------------------------------------- */
10992 +
10993 +void au_finfo_fin(struct file *file)
10994 +{
10995 +       struct au_finfo *finfo;
10996 +
10997 +       au_nfiles_dec(file->f_dentry->d_sb);
10998 +
10999 +       finfo = au_fi(file);
11000 +       AuDebugOn(finfo->fi_hdir);
11001 +       AuRwDestroy(&finfo->fi_rwsem);
11002 +       au_cache_free_finfo(finfo);
11003 +}
11004 +
11005 +void au_fi_init_once(void *_finfo)
11006 +{
11007 +       struct au_finfo *finfo = _finfo;
11008 +       static struct lock_class_key aufs_fi;
11009 +
11010 +       au_rw_init(&finfo->fi_rwsem);
11011 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
11012 +}
11013 +
11014 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
11015 +{
11016 +       int err, lc_idx;
11017 +       struct au_finfo *finfo;
11018 +       struct dentry *dentry;
11019 +
11020 +       err = -ENOMEM;
11021 +       dentry = file->f_dentry;
11022 +       finfo = au_cache_alloc_finfo();
11023 +       if (unlikely(!finfo))
11024 +               goto out;
11025 +
11026 +       err = 0;
11027 +       au_nfiles_inc(dentry->d_sb);
11028 +       lc_idx = AuLcNonDir_FIINFO;
11029 +       if (fidir)
11030 +               lc_idx = AuLcDir_FIINFO;
11031 +       au_rw_class(&finfo->fi_rwsem, au_lc_key + lc_idx);
11032 +       au_rw_write_lock(&finfo->fi_rwsem);
11033 +       finfo->fi_btop = -1;
11034 +       finfo->fi_hdir = fidir;
11035 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
11036 +       /* smp_mb(); */ /* atomic_set */
11037 +
11038 +       file->private_data = finfo;
11039 +
11040 +out:
11041 +       return err;
11042 +}
11043 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
11044 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
11045 +++ linux/fs/aufs/f_op.c        2012-01-05 11:00:59.215501779 +0100
11046 @@ -0,0 +1,731 @@
11047 +/*
11048 + * Copyright (C) 2005-2011 Junjiro R. Okajima
11049 + *
11050 + * This program, aufs is free software; you can redistribute it and/or modify
11051 + * it under the terms of the GNU General Public License as published by
11052 + * the Free Software Foundation; either version 2 of the License, or
11053 + * (at your option) any later version.
11054 + *
11055 + * This program is distributed in the hope that it will be useful,
11056 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11057 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11058 + * GNU General Public License for more details.
11059 + *
11060 + * You should have received a copy of the GNU General Public License
11061 + * along with this program; if not, write to the Free Software
11062 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11063 + */
11064 +
11065 +/*
11066 + * file and vm operations
11067 + */
11068 +
11069 +#include <linux/file.h>
11070 +#include <linux/fs_stack.h>
11071 +#include <linux/mman.h>
11072 +#include <linux/mm.h>
11073 +#include <linux/security.h>
11074 +#include "aufs.h"
11075 +
11076 +int au_do_open_nondir(struct file *file, int flags)
11077 +{
11078 +       int err;
11079 +       aufs_bindex_t bindex;
11080 +       struct file *h_file;
11081 +       struct dentry *dentry;
11082 +       struct au_finfo *finfo;
11083 +
11084 +       FiMustWriteLock(file);
11085 +
11086 +       dentry = file->f_dentry;
11087 +       err = au_d_alive(dentry);
11088 +       if (unlikely(err))
11089 +               goto out;
11090 +
11091 +       finfo = au_fi(file);
11092 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
11093 +       atomic_set(&finfo->fi_mmapped, 0);
11094 +       bindex = au_dbstart(dentry);
11095 +       h_file = au_h_open(dentry, bindex, flags, file);
11096 +       if (IS_ERR(h_file))
11097 +               err = PTR_ERR(h_file);
11098 +       else {
11099 +               au_set_fbstart(file, bindex);
11100 +               au_set_h_fptr(file, bindex, h_file);
11101 +               au_update_figen(file);
11102 +               /* todo: necessary? */
11103 +               /* file->f_ra = h_file->f_ra; */
11104 +       }
11105 +
11106 +out:
11107 +       return err;
11108 +}
11109 +
11110 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
11111 +                           struct file *file)
11112 +{
11113 +       int err;
11114 +       struct super_block *sb;
11115 +
11116 +       AuDbg("%.*s, f_flags 0x%x, f_mode 0x%x\n",
11117 +             AuDLNPair(file->f_dentry), vfsub_file_flags(file),
11118 +             file->f_mode);
11119 +
11120 +       sb = file->f_dentry->d_sb;
11121 +       si_read_lock(sb, AuLock_FLUSH);
11122 +       err = au_do_open(file, au_do_open_nondir, /*fidir*/NULL);
11123 +       si_read_unlock(sb);
11124 +       return err;
11125 +}
11126 +
11127 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
11128 +{
11129 +       struct au_finfo *finfo;
11130 +       aufs_bindex_t bindex;
11131 +
11132 +       finfo = au_fi(file);
11133 +       bindex = finfo->fi_btop;
11134 +       if (bindex >= 0) {
11135 +               /* remove me from sb->s_files */
11136 +               file_sb_list_del(file);
11137 +               au_set_h_fptr(file, bindex, NULL);
11138 +       }
11139 +
11140 +       au_finfo_fin(file);
11141 +       return 0;
11142 +}
11143 +
11144 +/* ---------------------------------------------------------------------- */
11145 +
11146 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
11147 +{
11148 +       int err;
11149 +       struct file *h_file;
11150 +
11151 +       err = 0;
11152 +       h_file = au_hf_top(file);
11153 +       if (h_file)
11154 +               err = vfsub_flush(h_file, id);
11155 +       return err;
11156 +}
11157 +
11158 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
11159 +{
11160 +       return au_do_flush(file, id, au_do_flush_nondir);
11161 +}
11162 +
11163 +/* ---------------------------------------------------------------------- */
11164 +/*
11165 + * read and write functions acquire [fdi]_rwsem once, but release before
11166 + * mmap_sem. This is because to stop a race condition between mmap(2).
11167 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
11168 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
11169 + * read functions after [fdi]_rwsem are released, but it should be harmless.
11170 + */
11171 +
11172 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
11173 +                        loff_t *ppos)
11174 +{
11175 +       ssize_t err;
11176 +       struct dentry *dentry;
11177 +       struct file *h_file;
11178 +       struct super_block *sb;
11179 +
11180 +       dentry = file->f_dentry;
11181 +       sb = dentry->d_sb;
11182 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11183 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11184 +       if (unlikely(err))
11185 +               goto out;
11186 +
11187 +       h_file = au_hf_top(file);
11188 +       get_file(h_file);
11189 +       di_read_unlock(dentry, AuLock_IR);
11190 +       fi_read_unlock(file);
11191 +
11192 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
11193 +       err = vfsub_read_u(h_file, buf, count, ppos);
11194 +       /* todo: necessary? */
11195 +       /* file->f_ra = h_file->f_ra; */
11196 +       /* update without lock, I don't think it a problem */
11197 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
11198 +       fput(h_file);
11199 +
11200 +out:
11201 +       si_read_unlock(sb);
11202 +       return err;
11203 +}
11204 +
11205 +/*
11206 + * todo: very ugly
11207 + * it locks both of i_mutex and si_rwsem for read in safe.
11208 + * if the plink maintenance mode continues forever (that is the problem),
11209 + * may loop forever.
11210 + */
11211 +static void au_mtx_and_read_lock(struct inode *inode)
11212 +{
11213 +       int err;
11214 +       struct super_block *sb = inode->i_sb;
11215 +
11216 +       while (1) {
11217 +               mutex_lock(&inode->i_mutex);
11218 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
11219 +               if (!err)
11220 +                       break;
11221 +               mutex_unlock(&inode->i_mutex);
11222 +               si_read_lock(sb, AuLock_NOPLMW);
11223 +               si_read_unlock(sb);
11224 +       }
11225 +}
11226 +
11227 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
11228 +                         size_t count, loff_t *ppos)
11229 +{
11230 +       ssize_t err;
11231 +       struct au_pin pin;
11232 +       struct dentry *dentry;
11233 +       struct super_block *sb;
11234 +       struct inode *inode;
11235 +       struct file *h_file;
11236 +       char __user *buf = (char __user *)ubuf;
11237 +
11238 +       dentry = file->f_dentry;
11239 +       sb = dentry->d_sb;
11240 +       inode = dentry->d_inode;
11241 +       au_mtx_and_read_lock(inode);
11242 +
11243 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11244 +       if (unlikely(err))
11245 +               goto out;
11246 +
11247 +       err = au_ready_to_write(file, -1, &pin);
11248 +       di_downgrade_lock(dentry, AuLock_IR);
11249 +       if (unlikely(err)) {
11250 +               di_read_unlock(dentry, AuLock_IR);
11251 +               fi_write_unlock(file);
11252 +               goto out;
11253 +       }
11254 +
11255 +       h_file = au_hf_top(file);
11256 +       get_file(h_file);
11257 +       au_unpin(&pin);
11258 +       di_read_unlock(dentry, AuLock_IR);
11259 +       fi_write_unlock(file);
11260 +
11261 +       err = vfsub_write_u(h_file, buf, count, ppos);
11262 +       ii_write_lock_child(inode);
11263 +       au_cpup_attr_timesizes(inode);
11264 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
11265 +       ii_write_unlock(inode);
11266 +       fput(h_file);
11267 +
11268 +out:
11269 +       si_read_unlock(sb);
11270 +       mutex_unlock(&inode->i_mutex);
11271 +       return err;
11272 +}
11273 +
11274 +static ssize_t au_do_aio(struct file *h_file, int rw, struct kiocb *kio,
11275 +                        const struct iovec *iov, unsigned long nv, loff_t pos)
11276 +{
11277 +       ssize_t err;
11278 +       struct file *file;
11279 +       ssize_t (*func)(struct kiocb *, const struct iovec *, unsigned long,
11280 +                       loff_t);
11281 +
11282 +       err = security_file_permission(h_file, rw);
11283 +       if (unlikely(err))
11284 +               goto out;
11285 +
11286 +       err = -ENOSYS;
11287 +       func = NULL;
11288 +       if (rw == MAY_READ)
11289 +               func = h_file->f_op->aio_read;
11290 +       else if (rw == MAY_WRITE)
11291 +               func = h_file->f_op->aio_write;
11292 +       if (func) {
11293 +               file = kio->ki_filp;
11294 +               kio->ki_filp = h_file;
11295 +               lockdep_off();
11296 +               err = func(kio, iov, nv, pos);
11297 +               lockdep_on();
11298 +               kio->ki_filp = file;
11299 +       } else
11300 +               /* currently there is no such fs */
11301 +               WARN_ON_ONCE(1);
11302 +
11303 +out:
11304 +       return err;
11305 +}
11306 +
11307 +static ssize_t aufs_aio_read(struct kiocb *kio, const struct iovec *iov,
11308 +                            unsigned long nv, loff_t pos)
11309 +{
11310 +       ssize_t err;
11311 +       struct file *file, *h_file;
11312 +       struct dentry *dentry;
11313 +       struct super_block *sb;
11314 +
11315 +       file = kio->ki_filp;
11316 +       dentry = file->f_dentry;
11317 +       sb = dentry->d_sb;
11318 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11319 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11320 +       if (unlikely(err))
11321 +               goto out;
11322 +
11323 +       h_file = au_hf_top(file);
11324 +       get_file(h_file);
11325 +       di_read_unlock(dentry, AuLock_IR);
11326 +       fi_read_unlock(file);
11327 +
11328 +       err = au_do_aio(h_file, MAY_READ, kio, iov, nv, pos);
11329 +       /* todo: necessary? */
11330 +       /* file->f_ra = h_file->f_ra; */
11331 +       /* update without lock, I don't think it a problem */
11332 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
11333 +       fput(h_file);
11334 +
11335 +out:
11336 +       si_read_unlock(sb);
11337 +       return err;
11338 +}
11339 +
11340 +static ssize_t aufs_aio_write(struct kiocb *kio, const struct iovec *iov,
11341 +                             unsigned long nv, loff_t pos)
11342 +{
11343 +       ssize_t err;
11344 +       struct au_pin pin;
11345 +       struct dentry *dentry;
11346 +       struct inode *inode;
11347 +       struct file *file, *h_file;
11348 +       struct super_block *sb;
11349 +
11350 +       file = kio->ki_filp;
11351 +       dentry = file->f_dentry;
11352 +       sb = dentry->d_sb;
11353 +       inode = dentry->d_inode;
11354 +       au_mtx_and_read_lock(inode);
11355 +
11356 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11357 +       if (unlikely(err))
11358 +               goto out;
11359 +
11360 +       err = au_ready_to_write(file, -1, &pin);
11361 +       di_downgrade_lock(dentry, AuLock_IR);
11362 +       if (unlikely(err)) {
11363 +               di_read_unlock(dentry, AuLock_IR);
11364 +               fi_write_unlock(file);
11365 +               goto out;
11366 +       }
11367 +
11368 +       h_file = au_hf_top(file);
11369 +       get_file(h_file);
11370 +       au_unpin(&pin);
11371 +       di_read_unlock(dentry, AuLock_IR);
11372 +       fi_write_unlock(file);
11373 +
11374 +       err = au_do_aio(h_file, MAY_WRITE, kio, iov, nv, pos);
11375 +       ii_write_lock_child(inode);
11376 +       au_cpup_attr_timesizes(inode);
11377 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
11378 +       ii_write_unlock(inode);
11379 +       fput(h_file);
11380 +
11381 +out:
11382 +       si_read_unlock(sb);
11383 +       mutex_unlock(&inode->i_mutex);
11384 +       return err;
11385 +}
11386 +
11387 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
11388 +                               struct pipe_inode_info *pipe, size_t len,
11389 +                               unsigned int flags)
11390 +{
11391 +       ssize_t err;
11392 +       struct file *h_file;
11393 +       struct dentry *dentry;
11394 +       struct super_block *sb;
11395 +
11396 +       dentry = file->f_dentry;
11397 +       sb = dentry->d_sb;
11398 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11399 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11400 +       if (unlikely(err))
11401 +               goto out;
11402 +
11403 +       err = -EINVAL;
11404 +       h_file = au_hf_top(file);
11405 +       get_file(h_file);
11406 +       if (au_test_loopback_kthread()) {
11407 +               au_warn_loopback(h_file->f_dentry->d_sb);
11408 +               if (file->f_mapping != h_file->f_mapping) {
11409 +                       file->f_mapping = h_file->f_mapping;
11410 +                       smp_mb(); /* unnecessary? */
11411 +               }
11412 +       }
11413 +       di_read_unlock(dentry, AuLock_IR);
11414 +       fi_read_unlock(file);
11415 +
11416 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
11417 +       /* todo: necessasry? */
11418 +       /* file->f_ra = h_file->f_ra; */
11419 +       /* update without lock, I don't think it a problem */
11420 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
11421 +       fput(h_file);
11422 +
11423 +out:
11424 +       si_read_unlock(sb);
11425 +       return err;
11426 +}
11427 +
11428 +static ssize_t
11429 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
11430 +                 size_t len, unsigned int flags)
11431 +{
11432 +       ssize_t err;
11433 +       struct au_pin pin;
11434 +       struct dentry *dentry;
11435 +       struct inode *inode;
11436 +       struct file *h_file;
11437 +       struct super_block *sb;
11438 +
11439 +       dentry = file->f_dentry;
11440 +       sb = dentry->d_sb;
11441 +       inode = dentry->d_inode;
11442 +       au_mtx_and_read_lock(inode);
11443 +
11444 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11445 +       if (unlikely(err))
11446 +               goto out;
11447 +
11448 +       err = au_ready_to_write(file, -1, &pin);
11449 +       di_downgrade_lock(dentry, AuLock_IR);
11450 +       if (unlikely(err)) {
11451 +               di_read_unlock(dentry, AuLock_IR);
11452 +               fi_write_unlock(file);
11453 +               goto out;
11454 +       }
11455 +
11456 +       h_file = au_hf_top(file);
11457 +       get_file(h_file);
11458 +       au_unpin(&pin);
11459 +       di_read_unlock(dentry, AuLock_IR);
11460 +       fi_write_unlock(file);
11461 +
11462 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
11463 +       ii_write_lock_child(inode);
11464 +       au_cpup_attr_timesizes(inode);
11465 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
11466 +       ii_write_unlock(inode);
11467 +       fput(h_file);
11468 +
11469 +out:
11470 +       si_read_unlock(sb);
11471 +       mutex_unlock(&inode->i_mutex);
11472 +       return err;
11473 +}
11474 +
11475 +/* ---------------------------------------------------------------------- */
11476 +
11477 +/*
11478 + * The locking order around current->mmap_sem.
11479 + * - in most and regular cases
11480 + *   file I/O syscall -- aufs_read() or something
11481 + *     -- si_rwsem for read -- mmap_sem
11482 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
11483 + * - in mmap case
11484 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
11485 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
11486 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
11487 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
11488 + * It means that when aufs acquires si_rwsem for write, the process should never
11489 + * acquire mmap_sem.
11490 + *
11491 + * Actually aufs_readdir() holds [fdi]i_rwsem before mmap_sem, but this is not a
11492 + * problem either since any directory is not able to be mmap-ed.
11493 + * The similar scenario is applied to aufs_readlink() too.
11494 + */
11495 +
11496 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
11497 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
11498 +
11499 +static unsigned long au_arch_prot_conv(unsigned long flags)
11500 +{
11501 +       /* currently ppc64 only */
11502 +#ifdef CONFIG_PPC64
11503 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
11504 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
11505 +       return AuConv_VM_PROT(flags, SAO);
11506 +#else
11507 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
11508 +       return 0;
11509 +#endif
11510 +}
11511 +
11512 +static unsigned long au_prot_conv(unsigned long flags)
11513 +{
11514 +       return AuConv_VM_PROT(flags, READ)
11515 +               | AuConv_VM_PROT(flags, WRITE)
11516 +               | AuConv_VM_PROT(flags, EXEC)
11517 +               | au_arch_prot_conv(flags);
11518 +}
11519 +
11520 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
11521 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
11522 +
11523 +static unsigned long au_flag_conv(unsigned long flags)
11524 +{
11525 +       return AuConv_VM_MAP(flags, GROWSDOWN)
11526 +               | AuConv_VM_MAP(flags, DENYWRITE)
11527 +               | AuConv_VM_MAP(flags, EXECUTABLE)
11528 +               | AuConv_VM_MAP(flags, LOCKED);
11529 +}
11530 +
11531 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
11532 +{
11533 +       int err;
11534 +       unsigned long prot;
11535 +       aufs_bindex_t bstart;
11536 +       const unsigned char wlock
11537 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
11538 +       struct dentry *dentry;
11539 +       struct super_block *sb;
11540 +       struct file *h_file;
11541 +       struct au_branch *br;
11542 +       struct au_pin pin;
11543 +
11544 +       AuDbgVmRegion(file, vma);
11545 +
11546 +       dentry = file->f_dentry;
11547 +       sb = dentry->d_sb;
11548 +       lockdep_off();
11549 +       si_read_lock(sb, AuLock_NOPLMW);
11550 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11551 +       if (unlikely(err))
11552 +               goto out;
11553 +
11554 +       if (wlock) {
11555 +               err = au_ready_to_write(file, -1, &pin);
11556 +               di_write_unlock(dentry);
11557 +               if (unlikely(err)) {
11558 +                       fi_write_unlock(file);
11559 +                       goto out;
11560 +               }
11561 +               au_unpin(&pin);
11562 +       } else
11563 +               di_write_unlock(dentry);
11564 +
11565 +       bstart = au_fbstart(file);
11566 +       br = au_sbr(sb, bstart);
11567 +       h_file = au_hf_top(file);
11568 +       get_file(h_file);
11569 +       au_set_mmapped(file);
11570 +       fi_write_unlock(file);
11571 +       lockdep_on();
11572 +
11573 +       au_vm_file_reset(vma, h_file);
11574 +       prot = au_prot_conv(vma->vm_flags);
11575 +       err = security_file_mmap(h_file, /*reqprot*/prot, prot,
11576 +                                au_flag_conv(vma->vm_flags), vma->vm_start, 0);
11577 +       if (!err)
11578 +               err = h_file->f_op->mmap(h_file, vma);
11579 +       if (unlikely(err))
11580 +               goto out_reset;
11581 +
11582 +       au_vm_prfile_set(vma, file);
11583 +       /* update without lock, I don't think it a problem */
11584 +       fsstack_copy_attr_atime(file->f_dentry->d_inode,
11585 +                               h_file->f_dentry->d_inode);
11586 +       goto out_fput; /* success */
11587 +
11588 +out_reset:
11589 +       au_unset_mmapped(file);
11590 +       au_vm_file_reset(vma, file);
11591 +out_fput:
11592 +       fput(h_file);
11593 +       lockdep_off();
11594 +out:
11595 +       si_read_unlock(sb);
11596 +       lockdep_on();
11597 +       AuTraceErr(err);
11598 +       return err;
11599 +}
11600 +
11601 +/* ---------------------------------------------------------------------- */
11602 +
11603 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
11604 +                            int datasync)
11605 +{
11606 +       int err;
11607 +       struct au_pin pin;
11608 +       struct dentry *dentry;
11609 +       struct inode *inode;
11610 +       struct file *h_file;
11611 +       struct super_block *sb;
11612 +
11613 +       dentry = file->f_dentry;
11614 +       inode = dentry->d_inode;
11615 +       sb = dentry->d_sb;
11616 +       mutex_lock(&inode->i_mutex);
11617 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
11618 +       if (unlikely(err))
11619 +               goto out;
11620 +
11621 +       err = 0; /* -EBADF; */ /* posix? */
11622 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
11623 +               goto out_si;
11624 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11625 +       if (unlikely(err))
11626 +               goto out_si;
11627 +
11628 +       err = au_ready_to_write(file, -1, &pin);
11629 +       di_downgrade_lock(dentry, AuLock_IR);
11630 +       if (unlikely(err))
11631 +               goto out_unlock;
11632 +       au_unpin(&pin);
11633 +
11634 +       err = -EINVAL;
11635 +       h_file = au_hf_top(file);
11636 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
11637 +       au_cpup_attr_timesizes(inode);
11638 +
11639 +out_unlock:
11640 +       di_read_unlock(dentry, AuLock_IR);
11641 +       fi_write_unlock(file);
11642 +out_si:
11643 +       si_read_unlock(sb);
11644 +out:
11645 +       mutex_unlock(&inode->i_mutex);
11646 +       return err;
11647 +}
11648 +
11649 +/* no one supports this operation, currently */
11650 +#if 0
11651 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
11652 +{
11653 +       int err;
11654 +       struct au_pin pin;
11655 +       struct dentry *dentry;
11656 +       struct inode *inode;
11657 +       struct file *file, *h_file;
11658 +
11659 +       file = kio->ki_filp;
11660 +       dentry = file->f_dentry;
11661 +       inode = dentry->d_inode;
11662 +       au_mtx_and_read_lock(inode);
11663 +
11664 +       err = 0; /* -EBADF; */ /* posix? */
11665 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
11666 +               goto out;
11667 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11668 +       if (unlikely(err))
11669 +               goto out;
11670 +
11671 +       err = au_ready_to_write(file, -1, &pin);
11672 +       di_downgrade_lock(dentry, AuLock_IR);
11673 +       if (unlikely(err))
11674 +               goto out_unlock;
11675 +       au_unpin(&pin);
11676 +
11677 +       err = -ENOSYS;
11678 +       h_file = au_hf_top(file);
11679 +       if (h_file->f_op && h_file->f_op->aio_fsync) {
11680 +               struct dentry *h_d;
11681 +               struct mutex *h_mtx;
11682 +
11683 +               h_d = h_file->f_dentry;
11684 +               h_mtx = &h_d->d_inode->i_mutex;
11685 +               if (!is_sync_kiocb(kio)) {
11686 +                       get_file(h_file);
11687 +                       fput(file);
11688 +               }
11689 +               kio->ki_filp = h_file;
11690 +               err = h_file->f_op->aio_fsync(kio, datasync);
11691 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
11692 +               if (!err)
11693 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
11694 +               /*ignore*/
11695 +               au_cpup_attr_timesizes(inode);
11696 +               mutex_unlock(h_mtx);
11697 +       }
11698 +
11699 +out_unlock:
11700 +       di_read_unlock(dentry, AuLock_IR);
11701 +       fi_write_unlock(file);
11702 +out:
11703 +       si_read_unlock(inode->sb);
11704 +       mutex_unlock(&inode->i_mutex);
11705 +       return err;
11706 +}
11707 +#endif
11708 +
11709 +static int aufs_fasync(int fd, struct file *file, int flag)
11710 +{
11711 +       int err;
11712 +       struct file *h_file;
11713 +       struct dentry *dentry;
11714 +       struct super_block *sb;
11715 +
11716 +       dentry = file->f_dentry;
11717 +       sb = dentry->d_sb;
11718 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11719 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11720 +       if (unlikely(err))
11721 +               goto out;
11722 +
11723 +       h_file = au_hf_top(file);
11724 +       if (h_file->f_op && h_file->f_op->fasync)
11725 +               err = h_file->f_op->fasync(fd, h_file, flag);
11726 +
11727 +       di_read_unlock(dentry, AuLock_IR);
11728 +       fi_read_unlock(file);
11729 +
11730 +out:
11731 +       si_read_unlock(sb);
11732 +       return err;
11733 +}
11734 +
11735 +/* ---------------------------------------------------------------------- */
11736 +
11737 +/* no one supports this operation, currently */
11738 +#if 0
11739 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
11740 +                            size_t len, loff_t *pos , int more)
11741 +{
11742 +}
11743 +#endif
11744 +
11745 +/* ---------------------------------------------------------------------- */
11746 +
11747 +const struct file_operations aufs_file_fop = {
11748 +       .owner          = THIS_MODULE,
11749 +
11750 +       .llseek         = default_llseek,
11751 +
11752 +       .read           = aufs_read,
11753 +       .write          = aufs_write,
11754 +       .aio_read       = aufs_aio_read,
11755 +       .aio_write      = aufs_aio_write,
11756 +#ifdef CONFIG_AUFS_POLL
11757 +       .poll           = aufs_poll,
11758 +#endif
11759 +       .unlocked_ioctl = aufs_ioctl_nondir,
11760 +#ifdef CONFIG_COMPAT
11761 +       .compat_ioctl   = aufs_ioctl_nondir, /* same */
11762 +#endif
11763 +       .mmap           = aufs_mmap,
11764 +       .open           = aufs_open_nondir,
11765 +       .flush          = aufs_flush_nondir,
11766 +       .release        = aufs_release_nondir,
11767 +       .fsync          = aufs_fsync_nondir,
11768 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
11769 +       .fasync         = aufs_fasync,
11770 +       /* .sendpage    = aufs_sendpage, */
11771 +       .splice_write   = aufs_splice_write,
11772 +       .splice_read    = aufs_splice_read,
11773 +#if 0
11774 +       .aio_splice_write = aufs_aio_splice_write,
11775 +       .aio_splice_read  = aufs_aio_splice_read
11776 +#endif
11777 +};
11778 diff -urN /usr/share/empty/fs/aufs/f_op_sp.c linux/fs/aufs/f_op_sp.c
11779 --- /usr/share/empty/fs/aufs/f_op_sp.c  1970-01-01 01:00:00.000000000 +0100
11780 +++ linux/fs/aufs/f_op_sp.c     2011-08-24 13:30:24.731313534 +0200
11781 @@ -0,0 +1,299 @@
11782 +/*
11783 + * Copyright (C) 2005-2011 Junjiro R. Okajima
11784 + *
11785 + * This program, aufs is free software; you can redistribute it and/or modify
11786 + * it under the terms of the GNU General Public License as published by
11787 + * the Free Software Foundation; either version 2 of the License, or
11788 + * (at your option) any later version.
11789 + *
11790 + * This program is distributed in the hope that it will be useful,
11791 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11792 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11793 + * GNU General Public License for more details.
11794 + *
11795 + * You should have received a copy of the GNU General Public License
11796 + * along with this program; if not, write to the Free Software
11797 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11798 + */
11799 +
11800 +/*
11801 + * file operations for special files.
11802 + * while they exist in aufs virtually,
11803 + * their file I/O is handled out of aufs.
11804 + */
11805 +
11806 +#include <linux/fs_stack.h>
11807 +#include "aufs.h"
11808 +
11809 +static ssize_t aufs_aio_read_sp(struct kiocb *kio, const struct iovec *iov,
11810 +                               unsigned long nv, loff_t pos)
11811 +{
11812 +       ssize_t err;
11813 +       aufs_bindex_t bstart;
11814 +       unsigned char wbr;
11815 +       struct file *file, *h_file;
11816 +       struct super_block *sb;
11817 +
11818 +       file = kio->ki_filp;
11819 +       sb = file->f_dentry->d_sb;
11820 +       si_read_lock(sb, AuLock_FLUSH);
11821 +       fi_read_lock(file);
11822 +       bstart = au_fbstart(file);
11823 +       h_file = au_hf_top(file);
11824 +       fi_read_unlock(file);
11825 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
11826 +       si_read_unlock(sb);
11827 +
11828 +       /* do not change the file in kio */
11829 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_read);
11830 +       err = h_file->f_op->aio_read(kio, iov, nv, pos);
11831 +       if (err > 0 && wbr)
11832 +               file_accessed(h_file);
11833 +
11834 +       return err;
11835 +}
11836 +
11837 +static ssize_t aufs_aio_write_sp(struct kiocb *kio, const struct iovec *iov,
11838 +                                unsigned long nv, loff_t pos)
11839 +{
11840 +       ssize_t err;
11841 +       aufs_bindex_t bstart;
11842 +       unsigned char wbr;
11843 +       struct super_block *sb;
11844 +       struct file *file, *h_file;
11845 +
11846 +       file = kio->ki_filp;
11847 +       sb = file->f_dentry->d_sb;
11848 +       si_read_lock(sb, AuLock_FLUSH);
11849 +       fi_read_lock(file);
11850 +       bstart = au_fbstart(file);
11851 +       h_file = au_hf_top(file);
11852 +       fi_read_unlock(file);
11853 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
11854 +       si_read_unlock(sb);
11855 +
11856 +       /* do not change the file in kio */
11857 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_write);
11858 +       err = h_file->f_op->aio_write(kio, iov, nv, pos);
11859 +       if (err > 0 && wbr)
11860 +               file_update_time(h_file);
11861 +
11862 +       return err;
11863 +}
11864 +
11865 +/* ---------------------------------------------------------------------- */
11866 +
11867 +static int aufs_release_sp(struct inode *inode, struct file *file)
11868 +{
11869 +       int err;
11870 +       struct file *h_file;
11871 +
11872 +       fi_read_lock(file);
11873 +       h_file = au_hf_top(file);
11874 +       fi_read_unlock(file);
11875 +       /* close this fifo in aufs */
11876 +       err = h_file->f_op->release(inode, file); /* ignore */
11877 +       aufs_release_nondir(inode, file); /* ignore */
11878 +       return err;
11879 +}
11880 +
11881 +/* ---------------------------------------------------------------------- */
11882 +
11883 +/* currently, support only FIFO */
11884 +enum {
11885 +       AuSp_FIFO, AuSp_FIFO_R, AuSp_FIFO_W, AuSp_FIFO_RW,
11886 +       /* AuSp_SOCK, AuSp_CHR, AuSp_BLK, */
11887 +       AuSp_Last
11888 +};
11889 +static int aufs_open_sp(struct inode *inode, struct file *file);
11890 +static struct au_sp_fop {
11891 +       int                     done;
11892 +       struct file_operations  fop;    /* not 'const' */
11893 +       spinlock_t              spin;
11894 +} au_sp_fop[AuSp_Last] = {
11895 +       [AuSp_FIFO] = {
11896 +               .fop    = {
11897 +                       .owner  = THIS_MODULE,
11898 +                       .open   = aufs_open_sp
11899 +               }
11900 +       }
11901 +};
11902 +
11903 +static void au_init_fop_sp(struct file *file)
11904 +{
11905 +       struct au_sp_fop *p;
11906 +       int i;
11907 +       struct file *h_file;
11908 +
11909 +       p = au_sp_fop;
11910 +       if (unlikely(!p->done)) {
11911 +               /* initialize first time only */
11912 +               static DEFINE_SPINLOCK(spin);
11913 +
11914 +               spin_lock(&spin);
11915 +               if (!p->done) {
11916 +                       BUILD_BUG_ON(sizeof(au_sp_fop)/sizeof(*au_sp_fop)
11917 +                                    != AuSp_Last);
11918 +                       for (i = 0; i < AuSp_Last; i++)
11919 +                               spin_lock_init(&p[i].spin);
11920 +                       p->done = 1;
11921 +               }
11922 +               spin_unlock(&spin);
11923 +       }
11924 +
11925 +       switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
11926 +       case FMODE_READ:
11927 +               i = AuSp_FIFO_R;
11928 +               break;
11929 +       case FMODE_WRITE:
11930 +               i = AuSp_FIFO_W;
11931 +               break;
11932 +       case FMODE_READ | FMODE_WRITE:
11933 +               i = AuSp_FIFO_RW;
11934 +               break;
11935 +       default:
11936 +               BUG();
11937 +       }
11938 +
11939 +       p += i;
11940 +       if (unlikely(!p->done)) {
11941 +               /* initialize first time only */
11942 +               h_file = au_hf_top(file);
11943 +               spin_lock(&p->spin);
11944 +               if (!p->done) {
11945 +                       p->fop = *h_file->f_op;
11946 +                       p->fop.owner = THIS_MODULE;
11947 +                       if (p->fop.aio_read)
11948 +                               p->fop.aio_read = aufs_aio_read_sp;
11949 +                       if (p->fop.aio_write)
11950 +                               p->fop.aio_write = aufs_aio_write_sp;
11951 +                       p->fop.release = aufs_release_sp;
11952 +                       p->done = 1;
11953 +               }
11954 +               spin_unlock(&p->spin);
11955 +       }
11956 +       file->f_op = &p->fop;
11957 +}
11958 +
11959 +static int au_cpup_sp(struct dentry *dentry)
11960 +{
11961 +       int err;
11962 +       aufs_bindex_t bcpup;
11963 +       struct au_pin pin;
11964 +       struct au_wr_dir_args wr_dir_args = {
11965 +               .force_btgt     = -1,
11966 +               .flags          = 0
11967 +       };
11968 +
11969 +       AuDbg("%.*s\n", AuDLNPair(dentry));
11970 +
11971 +       di_read_unlock(dentry, AuLock_IR);
11972 +       di_write_lock_child(dentry);
11973 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
11974 +       if (unlikely(err < 0))
11975 +               goto out;
11976 +       bcpup = err;
11977 +       err = 0;
11978 +       if (bcpup == au_dbstart(dentry))
11979 +               goto out; /* success */
11980 +
11981 +       err = au_pin(&pin, dentry, bcpup, au_opt_udba(dentry->d_sb),
11982 +                    AuPin_MNT_WRITE);
11983 +       if (!err) {
11984 +               err = au_sio_cpup_simple(dentry, bcpup, -1, AuCpup_DTIME);
11985 +               au_unpin(&pin);
11986 +       }
11987 +
11988 +out:
11989 +       di_downgrade_lock(dentry, AuLock_IR);
11990 +       return err;
11991 +}
11992 +
11993 +static int au_do_open_sp(struct file *file, int flags)
11994 +{
11995 +       int err;
11996 +       struct dentry *dentry;
11997 +       struct super_block *sb;
11998 +       struct file *h_file;
11999 +       struct inode *h_inode;
12000 +
12001 +       dentry = file->f_dentry;
12002 +       AuDbg("%.*s\n", AuDLNPair(dentry));
12003 +
12004 +       /*
12005 +        * try copying-up.
12006 +        * operate on the ro branch is not an error.
12007 +        */
12008 +       au_cpup_sp(dentry); /* ignore */
12009 +
12010 +       /* prepare h_file */
12011 +       err = au_do_open_nondir(file, vfsub_file_flags(file));
12012 +       if (unlikely(err))
12013 +               goto out;
12014 +
12015 +       sb = dentry->d_sb;
12016 +       h_file = au_hf_top(file);
12017 +       h_inode = h_file->f_dentry->d_inode;
12018 +       di_read_unlock(dentry, AuLock_IR);
12019 +       fi_write_unlock(file);
12020 +       si_read_unlock(sb);
12021 +       /* open this fifo in aufs */
12022 +       err = h_inode->i_fop->open(file->f_dentry->d_inode, file);
12023 +       si_noflush_read_lock(sb);
12024 +       fi_write_lock(file);
12025 +       di_read_lock_child(dentry, AuLock_IR);
12026 +       if (!err)
12027 +               au_init_fop_sp(file);
12028 +
12029 +out:
12030 +       return err;
12031 +}
12032 +
12033 +static int aufs_open_sp(struct inode *inode, struct file *file)
12034 +{
12035 +       int err;
12036 +       struct super_block *sb;
12037 +
12038 +       sb = file->f_dentry->d_sb;
12039 +       si_read_lock(sb, AuLock_FLUSH);
12040 +       err = au_do_open(file, au_do_open_sp, /*fidir*/NULL);
12041 +       si_read_unlock(sb);
12042 +       return err;
12043 +}
12044 +
12045 +/* ---------------------------------------------------------------------- */
12046 +
12047 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev)
12048 +{
12049 +       init_special_inode(inode, mode, rdev);
12050 +
12051 +       switch (mode & S_IFMT) {
12052 +       case S_IFIFO:
12053 +               inode->i_fop = &au_sp_fop[AuSp_FIFO].fop;
12054 +               /*FALLTHROUGH*/
12055 +       case S_IFCHR:
12056 +       case S_IFBLK:
12057 +       case S_IFSOCK:
12058 +               break;
12059 +       default:
12060 +               AuDebugOn(1);
12061 +       }
12062 +}
12063 +
12064 +int au_special_file(umode_t mode)
12065 +{
12066 +       int ret;
12067 +
12068 +       ret = 0;
12069 +       switch (mode & S_IFMT) {
12070 +       case S_IFIFO:
12071 +#if 0
12072 +       case S_IFCHR:
12073 +       case S_IFBLK:
12074 +       case S_IFSOCK:
12075 +#endif
12076 +               ret = 1;
12077 +       }
12078 +
12079 +       return ret;
12080 +}
12081 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
12082 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
12083 +++ linux/fs/aufs/fstype.h      2011-08-24 13:30:24.731313534 +0200
12084 @@ -0,0 +1,497 @@
12085 +/*
12086 + * Copyright (C) 2005-2011 Junjiro R. Okajima
12087 + *
12088 + * This program, aufs is free software; you can redistribute it and/or modify
12089 + * it under the terms of the GNU General Public License as published by
12090 + * the Free Software Foundation; either version 2 of the License, or
12091 + * (at your option) any later version.
12092 + *
12093 + * This program is distributed in the hope that it will be useful,
12094 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12095 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12096 + * GNU General Public License for more details.
12097 + *
12098 + * You should have received a copy of the GNU General Public License
12099 + * along with this program; if not, write to the Free Software
12100 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12101 + */
12102 +
12103 +/*
12104 + * judging filesystem type
12105 + */
12106 +
12107 +#ifndef __AUFS_FSTYPE_H__
12108 +#define __AUFS_FSTYPE_H__
12109 +
12110 +#ifdef __KERNEL__
12111 +
12112 +#include <linux/fs.h>
12113 +#include <linux/magic.h>
12114 +#include <linux/romfs_fs.h>
12115 +#include <linux/aufs_type.h>
12116 +
12117 +static inline int au_test_aufs(struct super_block *sb)
12118 +{
12119 +       return sb->s_magic == AUFS_SUPER_MAGIC;
12120 +}
12121 +
12122 +static inline const char *au_sbtype(struct super_block *sb)
12123 +{
12124 +       return sb->s_type->name;
12125 +}
12126 +
12127 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
12128 +{
12129 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
12130 +       return sb->s_magic == ROMFS_MAGIC;
12131 +#else
12132 +       return 0;
12133 +#endif
12134 +}
12135 +
12136 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
12137 +{
12138 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
12139 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
12140 +#else
12141 +       return 0;
12142 +#endif
12143 +}
12144 +
12145 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
12146 +{
12147 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
12148 +       return sb->s_magic == CRAMFS_MAGIC;
12149 +#endif
12150 +       return 0;
12151 +}
12152 +
12153 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
12154 +{
12155 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
12156 +       return sb->s_magic == NFS_SUPER_MAGIC;
12157 +#else
12158 +       return 0;
12159 +#endif
12160 +}
12161 +
12162 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
12163 +{
12164 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
12165 +       return sb->s_magic == FUSE_SUPER_MAGIC;
12166 +#else
12167 +       return 0;
12168 +#endif
12169 +}
12170 +
12171 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
12172 +{
12173 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
12174 +       return sb->s_magic == XFS_SB_MAGIC;
12175 +#else
12176 +       return 0;
12177 +#endif
12178 +}
12179 +
12180 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
12181 +{
12182 +#ifdef CONFIG_TMPFS
12183 +       return sb->s_magic == TMPFS_MAGIC;
12184 +#else
12185 +       return 0;
12186 +#endif
12187 +}
12188 +
12189 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
12190 +{
12191 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
12192 +       return !strcmp(au_sbtype(sb), "ecryptfs");
12193 +#else
12194 +       return 0;
12195 +#endif
12196 +}
12197 +
12198 +static inline int au_test_smbfs(struct super_block *sb __maybe_unused)
12199 +{
12200 +#if defined(CONFIG_SMB_FS) || defined(CONFIG_SMB_FS_MODULE)
12201 +       return sb->s_magic == SMB_SUPER_MAGIC;
12202 +#else
12203 +       return 0;
12204 +#endif
12205 +}
12206 +
12207 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
12208 +{
12209 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
12210 +       return sb->s_magic == OCFS2_SUPER_MAGIC;
12211 +#else
12212 +       return 0;
12213 +#endif
12214 +}
12215 +
12216 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
12217 +{
12218 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
12219 +       return sb->s_magic == DLMFS_MAGIC;
12220 +#else
12221 +       return 0;
12222 +#endif
12223 +}
12224 +
12225 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
12226 +{
12227 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
12228 +       return sb->s_magic == CODA_SUPER_MAGIC;
12229 +#else
12230 +       return 0;
12231 +#endif
12232 +}
12233 +
12234 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
12235 +{
12236 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
12237 +       return sb->s_magic == V9FS_MAGIC;
12238 +#else
12239 +       return 0;
12240 +#endif
12241 +}
12242 +
12243 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
12244 +{
12245 +#if defined(CONFIG_EXT4DEV_FS) || defined(CONFIG_EXT4DEV_FS_MODULE)
12246 +       return sb->s_magic == EXT4_SUPER_MAGIC;
12247 +#else
12248 +       return 0;
12249 +#endif
12250 +}
12251 +
12252 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
12253 +{
12254 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
12255 +       return !strcmp(au_sbtype(sb), "sysv");
12256 +#else
12257 +       return 0;
12258 +#endif
12259 +}
12260 +
12261 +static inline int au_test_ramfs(struct super_block *sb)
12262 +{
12263 +       return sb->s_magic == RAMFS_MAGIC;
12264 +}
12265 +
12266 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
12267 +{
12268 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
12269 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
12270 +#else
12271 +       return 0;
12272 +#endif
12273 +}
12274 +
12275 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
12276 +{
12277 +#ifdef CONFIG_PROC_FS
12278 +       return sb->s_magic == PROC_SUPER_MAGIC;
12279 +#else
12280 +       return 0;
12281 +#endif
12282 +}
12283 +
12284 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
12285 +{
12286 +#ifdef CONFIG_SYSFS
12287 +       return sb->s_magic == SYSFS_MAGIC;
12288 +#else
12289 +       return 0;
12290 +#endif
12291 +}
12292 +
12293 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
12294 +{
12295 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
12296 +       return sb->s_magic == CONFIGFS_MAGIC;
12297 +#else
12298 +       return 0;
12299 +#endif
12300 +}
12301 +
12302 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
12303 +{
12304 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
12305 +       return sb->s_magic == MINIX3_SUPER_MAGIC
12306 +               || sb->s_magic == MINIX2_SUPER_MAGIC
12307 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
12308 +               || sb->s_magic == MINIX_SUPER_MAGIC
12309 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
12310 +#else
12311 +       return 0;
12312 +#endif
12313 +}
12314 +
12315 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
12316 +{
12317 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
12318 +       return sb->s_magic == CIFS_MAGIC_NUMBER;
12319 +#else
12320 +       return 0;
12321 +#endif
12322 +}
12323 +
12324 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
12325 +{
12326 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
12327 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
12328 +#else
12329 +       return 0;
12330 +#endif
12331 +}
12332 +
12333 +static inline int au_test_msdos(struct super_block *sb)
12334 +{
12335 +       return au_test_fat(sb);
12336 +}
12337 +
12338 +static inline int au_test_vfat(struct super_block *sb)
12339 +{
12340 +       return au_test_fat(sb);
12341 +}
12342 +
12343 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
12344 +{
12345 +#ifdef CONFIG_SECURITYFS
12346 +       return sb->s_magic == SECURITYFS_MAGIC;
12347 +#else
12348 +       return 0;
12349 +#endif
12350 +}
12351 +
12352 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
12353 +{
12354 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
12355 +       return sb->s_magic == SQUASHFS_MAGIC;
12356 +#else
12357 +       return 0;
12358 +#endif
12359 +}
12360 +
12361 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
12362 +{
12363 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
12364 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
12365 +#else
12366 +       return 0;
12367 +#endif
12368 +}
12369 +
12370 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
12371 +{
12372 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
12373 +       return sb->s_magic == XENFS_SUPER_MAGIC;
12374 +#else
12375 +       return 0;
12376 +#endif
12377 +}
12378 +
12379 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
12380 +{
12381 +#ifdef CONFIG_DEBUG_FS
12382 +       return sb->s_magic == DEBUGFS_MAGIC;
12383 +#else
12384 +       return 0;
12385 +#endif
12386 +}
12387 +
12388 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
12389 +{
12390 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
12391 +       return sb->s_magic == NILFS_SUPER_MAGIC;
12392 +#else
12393 +       return 0;
12394 +#endif
12395 +}
12396 +
12397 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
12398 +{
12399 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
12400 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
12401 +#else
12402 +       return 0;
12403 +#endif
12404 +}
12405 +
12406 +/* ---------------------------------------------------------------------- */
12407 +/*
12408 + * they can't be an aufs branch.
12409 + */
12410 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
12411 +{
12412 +       return
12413 +#ifndef CONFIG_AUFS_BR_RAMFS
12414 +               au_test_ramfs(sb) ||
12415 +#endif
12416 +               au_test_procfs(sb)
12417 +               || au_test_sysfs(sb)
12418 +               || au_test_configfs(sb)
12419 +               || au_test_debugfs(sb)
12420 +               || au_test_securityfs(sb)
12421 +               || au_test_xenfs(sb)
12422 +               || au_test_ecryptfs(sb)
12423 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
12424 +               || au_test_aufs(sb); /* will be supported in next version */
12425 +}
12426 +
12427 +/*
12428 + * If the filesystem supports NFS-export, then it has to support NULL as
12429 + * a nameidata parameter for ->create(), ->lookup() and ->d_revalidate().
12430 + * We can apply this principle when we handle a lower filesystem.
12431 + */
12432 +static inline int au_test_fs_null_nd(struct super_block *sb)
12433 +{
12434 +       return !!sb->s_export_op;
12435 +}
12436 +
12437 +static inline int au_test_fs_remote(struct super_block *sb)
12438 +{
12439 +       return !au_test_tmpfs(sb)
12440 +#ifdef CONFIG_AUFS_BR_RAMFS
12441 +               && !au_test_ramfs(sb)
12442 +#endif
12443 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
12444 +}
12445 +
12446 +/* ---------------------------------------------------------------------- */
12447 +
12448 +/*
12449 + * Note: these functions (below) are created after reading ->getattr() in all
12450 + * filesystems under linux/fs. it means we have to do so in every update...
12451 + */
12452 +
12453 +/*
12454 + * some filesystems require getattr to refresh the inode attributes before
12455 + * referencing.
12456 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
12457 + * and leave the work for d_revalidate()
12458 + */
12459 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
12460 +{
12461 +       return au_test_nfs(sb)
12462 +               || au_test_fuse(sb)
12463 +               /* || au_test_smbfs(sb) */      /* untested */
12464 +               /* || au_test_ocfs2(sb) */      /* untested */
12465 +               /* || au_test_btrfs(sb) */      /* untested */
12466 +               /* || au_test_coda(sb) */       /* untested */
12467 +               /* || au_test_v9fs(sb) */       /* untested */
12468 +               ;
12469 +}
12470 +
12471 +/*
12472 + * filesystems which don't maintain i_size or i_blocks.
12473 + */
12474 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
12475 +{
12476 +       return au_test_xfs(sb)
12477 +               || au_test_btrfs(sb)
12478 +               || au_test_ubifs(sb)
12479 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
12480 +               /* || au_test_ext4(sb) */       /* untested */
12481 +               /* || au_test_ocfs2(sb) */      /* untested */
12482 +               /* || au_test_ocfs2_dlmfs(sb) */ /* untested */
12483 +               /* || au_test_sysv(sb) */       /* untested */
12484 +               /* || au_test_minix(sb) */      /* untested */
12485 +               ;
12486 +}
12487 +
12488 +/*
12489 + * filesystems which don't store the correct value in some of their inode
12490 + * attributes.
12491 + */
12492 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
12493 +{
12494 +       return au_test_fs_bad_iattr_size(sb)
12495 +               /* || au_test_cifs(sb) */       /* untested */
12496 +               || au_test_fat(sb)
12497 +               || au_test_msdos(sb)
12498 +               || au_test_vfat(sb);
12499 +}
12500 +
12501 +/* they don't check i_nlink in link(2) */
12502 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
12503 +{
12504 +       return au_test_tmpfs(sb)
12505 +#ifdef CONFIG_AUFS_BR_RAMFS
12506 +               || au_test_ramfs(sb)
12507 +#endif
12508 +               || au_test_ubifs(sb)
12509 +               || au_test_btrfs(sb)
12510 +               || au_test_hfsplus(sb);
12511 +}
12512 +
12513 +/*
12514 + * filesystems which sets S_NOATIME and S_NOCMTIME.
12515 + */
12516 +static inline int au_test_fs_notime(struct super_block *sb)
12517 +{
12518 +       return au_test_nfs(sb)
12519 +               || au_test_fuse(sb)
12520 +               || au_test_ubifs(sb)
12521 +               /* || au_test_cifs(sb) */       /* untested */
12522 +               ;
12523 +}
12524 +
12525 +/*
12526 + * filesystems which requires replacing i_mapping.
12527 + */
12528 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
12529 +{
12530 +       return au_test_fuse(sb)
12531 +               || au_test_ubifs(sb);
12532 +}
12533 +
12534 +/* temporary support for i#1 in cramfs */
12535 +static inline int au_test_fs_unique_ino(struct inode *inode)
12536 +{
12537 +       if (au_test_cramfs(inode->i_sb))
12538 +               return inode->i_ino != 1;
12539 +       return 1;
12540 +}
12541 +
12542 +/* ---------------------------------------------------------------------- */
12543 +
12544 +/*
12545 + * the filesystem where the xino files placed must support i/o after unlink and
12546 + * maintain i_size and i_blocks.
12547 + */
12548 +static inline int au_test_fs_bad_xino(struct super_block *sb)
12549 +{
12550 +       return au_test_fs_remote(sb)
12551 +               || au_test_fs_bad_iattr_size(sb)
12552 +#ifdef CONFIG_AUFS_BR_RAMFS
12553 +               || !(au_test_ramfs(sb) || au_test_fs_null_nd(sb))
12554 +#else
12555 +               || !au_test_fs_null_nd(sb) /* to keep xino code simple */
12556 +#endif
12557 +               /* don't want unnecessary work for xino */
12558 +               || au_test_aufs(sb)
12559 +               || au_test_ecryptfs(sb)
12560 +               || au_test_nilfs(sb);
12561 +}
12562 +
12563 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
12564 +{
12565 +       return au_test_tmpfs(sb)
12566 +               || au_test_ramfs(sb);
12567 +}
12568 +
12569 +/*
12570 + * test if the @sb is real-readonly.
12571 + */
12572 +static inline int au_test_fs_rr(struct super_block *sb)
12573 +{
12574 +       return au_test_squashfs(sb)
12575 +               || au_test_iso9660(sb)
12576 +               || au_test_cramfs(sb)
12577 +               || au_test_romfs(sb);
12578 +}
12579 +
12580 +#endif /* __KERNEL__ */
12581 +#endif /* __AUFS_FSTYPE_H__ */
12582 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
12583 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
12584 +++ linux/fs/aufs/hfsnotify.c   2011-08-24 13:30:24.731313534 +0200
12585 @@ -0,0 +1,247 @@
12586 +/*
12587 + * Copyright (C) 2005-2011 Junjiro R. Okajima
12588 + *
12589 + * This program, aufs is free software; you can redistribute it and/or modify
12590 + * it under the terms of the GNU General Public License as published by
12591 + * the Free Software Foundation; either version 2 of the License, or
12592 + * (at your option) any later version.
12593 + *
12594 + * This program is distributed in the hope that it will be useful,
12595 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12596 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12597 + * GNU General Public License for more details.
12598 + *
12599 + * You should have received a copy of the GNU General Public License
12600 + * along with this program; if not, write to the Free Software
12601 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12602 + */
12603 +
12604 +/*
12605 + * fsnotify for the lower directories
12606 + */
12607 +
12608 +#include "aufs.h"
12609 +
12610 +/* FS_IN_IGNORED is unnecessary */
12611 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
12612 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
12613 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
12614 +
12615 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
12616 +{
12617 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
12618 +                                            hn_mark);
12619 +       AuDbg("here\n");
12620 +       hn->hn_mark_dead = 1;
12621 +       smp_mb();
12622 +       wake_up_all(&au_hfsn_wq);
12623 +}
12624 +
12625 +static int au_hfsn_alloc(struct au_hinode *hinode)
12626 +{
12627 +       struct au_hnotify *hn;
12628 +       struct super_block *sb;
12629 +       struct au_branch *br;
12630 +       struct fsnotify_mark *mark;
12631 +       aufs_bindex_t bindex;
12632 +
12633 +       hn = hinode->hi_notify;
12634 +       sb = hn->hn_aufs_inode->i_sb;
12635 +       bindex = au_br_index(sb, hinode->hi_id);
12636 +       br = au_sbr(sb, bindex);
12637 +       hn->hn_mark_dead = 0;
12638 +       mark = &hn->hn_mark;
12639 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
12640 +       mark->mask = AuHfsnMask;
12641 +       /*
12642 +        * by udba rename or rmdir, aufs assign a new inode to the known
12643 +        * h_inode, so specify 1 to allow dups.
12644 +        */
12645 +       return fsnotify_add_mark(mark, br->br_hfsn_group, hinode->hi_inode,
12646 +                                /*mnt*/NULL, /*allow_dups*/1);
12647 +}
12648 +
12649 +static void au_hfsn_free(struct au_hinode *hinode)
12650 +{
12651 +       struct au_hnotify *hn;
12652 +       struct fsnotify_mark *mark;
12653 +
12654 +       hn = hinode->hi_notify;
12655 +       mark = &hn->hn_mark;
12656 +       fsnotify_destroy_mark(mark);
12657 +       fsnotify_put_mark(mark);
12658 +
12659 +       /* TODO: bad approach */
12660 +       wait_event(au_hfsn_wq, hn->hn_mark_dead);
12661 +}
12662 +
12663 +/* ---------------------------------------------------------------------- */
12664 +
12665 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
12666 +{
12667 +       struct fsnotify_mark *mark;
12668 +
12669 +       mark = &hinode->hi_notify->hn_mark;
12670 +       spin_lock(&mark->lock);
12671 +       if (do_set) {
12672 +               AuDebugOn(mark->mask & AuHfsnMask);
12673 +               mark->mask |= AuHfsnMask;
12674 +       } else {
12675 +               AuDebugOn(!(mark->mask & AuHfsnMask));
12676 +               mark->mask &= ~AuHfsnMask;
12677 +       }
12678 +       spin_unlock(&mark->lock);
12679 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
12680 +}
12681 +
12682 +/* ---------------------------------------------------------------------- */
12683 +
12684 +/* #define AuDbgHnotify */
12685 +#ifdef AuDbgHnotify
12686 +static char *au_hfsn_name(u32 mask)
12687 +{
12688 +#ifdef CONFIG_AUFS_DEBUG
12689 +#define test_ret(flag) if (mask & flag) \
12690 +                               return #flag;
12691 +       test_ret(FS_ACCESS);
12692 +       test_ret(FS_MODIFY);
12693 +       test_ret(FS_ATTRIB);
12694 +       test_ret(FS_CLOSE_WRITE);
12695 +       test_ret(FS_CLOSE_NOWRITE);
12696 +       test_ret(FS_OPEN);
12697 +       test_ret(FS_MOVED_FROM);
12698 +       test_ret(FS_MOVED_TO);
12699 +       test_ret(FS_CREATE);
12700 +       test_ret(FS_DELETE);
12701 +       test_ret(FS_DELETE_SELF);
12702 +       test_ret(FS_MOVE_SELF);
12703 +       test_ret(FS_UNMOUNT);
12704 +       test_ret(FS_Q_OVERFLOW);
12705 +       test_ret(FS_IN_IGNORED);
12706 +       test_ret(FS_IN_ISDIR);
12707 +       test_ret(FS_IN_ONESHOT);
12708 +       test_ret(FS_EVENT_ON_CHILD);
12709 +       return "";
12710 +#undef test_ret
12711 +#else
12712 +       return "??";
12713 +#endif
12714 +}
12715 +#endif
12716 +
12717 +/* ---------------------------------------------------------------------- */
12718 +
12719 +static int au_hfsn_handle_event(struct fsnotify_group *group,
12720 +                               struct fsnotify_mark *inode_mark,
12721 +                               struct fsnotify_mark *vfsmount_mark,
12722 +                               struct fsnotify_event *event)
12723 +{
12724 +       int err;
12725 +       struct au_hnotify *hnotify;
12726 +       struct inode *h_dir, *h_inode;
12727 +       __u32 mask;
12728 +       struct qstr h_child_qstr = {
12729 +               .name   = event->file_name,
12730 +               .len    = event->name_len
12731 +       };
12732 +
12733 +       AuDebugOn(event->data_type != FSNOTIFY_EVENT_INODE);
12734 +
12735 +       err = 0;
12736 +       /* if FS_UNMOUNT happens, there must be another bug */
12737 +       mask = event->mask;
12738 +       AuDebugOn(mask & FS_UNMOUNT);
12739 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
12740 +               goto out;
12741 +
12742 +       h_dir = event->to_tell;
12743 +       h_inode = event->inode;
12744 +#ifdef AuDbgHnotify
12745 +       au_debug(1);
12746 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
12747 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
12748 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
12749 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
12750 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
12751 +               /* WARN_ON(1); */
12752 +       }
12753 +       au_debug(0);
12754 +#endif
12755 +
12756 +       AuDebugOn(!inode_mark);
12757 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
12758 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
12759 +
12760 +out:
12761 +       return err;
12762 +}
12763 +
12764 +/* isn't it waste to ask every registered 'group'? */
12765 +/* copied from linux/fs/notify/inotify/inotify_fsnotiry.c */
12766 +/* it should be exported to modules */
12767 +static bool au_hfsn_should_send_event(struct fsnotify_group *group,
12768 +                                     struct inode *h_inode,
12769 +                                     struct fsnotify_mark *inode_mark,
12770 +                                     struct fsnotify_mark *vfsmount_mark,
12771 +                                     __u32 mask, void *data, int data_type)
12772 +{
12773 +       mask = (mask & ~FS_EVENT_ON_CHILD);
12774 +       return inode_mark->mask & mask;
12775 +}
12776 +
12777 +static struct fsnotify_ops au_hfsn_ops = {
12778 +       .should_send_event      = au_hfsn_should_send_event,
12779 +       .handle_event           = au_hfsn_handle_event
12780 +};
12781 +
12782 +/* ---------------------------------------------------------------------- */
12783 +
12784 +static void au_hfsn_fin_br(struct au_branch *br)
12785 +{
12786 +       if (br->br_hfsn_group)
12787 +               fsnotify_put_group(br->br_hfsn_group);
12788 +}
12789 +
12790 +static int au_hfsn_init_br(struct au_branch *br, int perm)
12791 +{
12792 +       br->br_hfsn_group = NULL;
12793 +       br->br_hfsn_ops = au_hfsn_ops;
12794 +       return 0;
12795 +}
12796 +
12797 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
12798 +{
12799 +       int err;
12800 +
12801 +       err = 0;
12802 +       if (udba != AuOpt_UDBA_HNOTIFY
12803 +           || !au_br_hnotifyable(perm)) {
12804 +               au_hfsn_fin_br(br);
12805 +               br->br_hfsn_group = NULL;
12806 +               goto out;
12807 +       }
12808 +
12809 +       if (br->br_hfsn_group)
12810 +               goto out;
12811 +
12812 +       br->br_hfsn_group = fsnotify_alloc_group(&br->br_hfsn_ops);
12813 +       if (IS_ERR(br->br_hfsn_group)) {
12814 +               err = PTR_ERR(br->br_hfsn_group);
12815 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
12816 +               br->br_hfsn_group = NULL;
12817 +       }
12818 +
12819 +out:
12820 +       AuTraceErr(err);
12821 +       return err;
12822 +}
12823 +
12824 +const struct au_hnotify_op au_hnotify_op = {
12825 +       .ctl            = au_hfsn_ctl,
12826 +       .alloc          = au_hfsn_alloc,
12827 +       .free           = au_hfsn_free,
12828 +
12829 +       .reset_br       = au_hfsn_reset_br,
12830 +       .fin_br         = au_hfsn_fin_br,
12831 +       .init_br        = au_hfsn_init_br
12832 +};
12833 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
12834 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
12835 +++ linux/fs/aufs/hfsplus.c     2011-08-24 13:30:24.731313534 +0200
12836 @@ -0,0 +1,58 @@
12837 +/*
12838 + * Copyright (C) 2010-2011 Junjiro R. Okajima
12839 + *
12840 + * This program, aufs is free software; you can redistribute it and/or modify
12841 + * it under the terms of the GNU General Public License as published by
12842 + * the Free Software Foundation; either version 2 of the License, or
12843 + * (at your option) any later version.
12844 + *
12845 + * This program is distributed in the hope that it will be useful,
12846 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12847 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12848 + * GNU General Public License for more details.
12849 + *
12850 + * You should have received a copy of the GNU General Public License
12851 + * along with this program; if not, write to the Free Software
12852 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12853 + */
12854 +
12855 +/*
12856 + * special support for filesystems which aqucires an inode mutex
12857 + * at final closing a file, eg, hfsplus.
12858 + *
12859 + * This trick is very simple and stupid, just to open the file before really
12860 + * neceeary open to tell hfsplus that this is not the final closing.
12861 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
12862 + * and au_h_open_post() after releasing it.
12863 + */
12864 +
12865 +#include <linux/file.h>
12866 +#include "aufs.h"
12867 +
12868 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
12869 +{
12870 +       struct file *h_file;
12871 +       struct dentry *h_dentry;
12872 +
12873 +       h_dentry = au_h_dptr(dentry, bindex);
12874 +       AuDebugOn(!h_dentry);
12875 +       AuDebugOn(!h_dentry->d_inode);
12876 +       IMustLock(h_dentry->d_inode);
12877 +
12878 +       h_file = NULL;
12879 +       if (au_test_hfsplus(h_dentry->d_sb)
12880 +           && S_ISREG(h_dentry->d_inode->i_mode))
12881 +               h_file = au_h_open(dentry, bindex,
12882 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
12883 +                                  /*file*/NULL);
12884 +       return h_file;
12885 +}
12886 +
12887 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
12888 +                   struct file *h_file)
12889 +{
12890 +       if (h_file) {
12891 +               fput(h_file);
12892 +               au_sbr_put(dentry->d_sb, bindex);
12893 +       }
12894 +}
12895 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
12896 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
12897 +++ linux/fs/aufs/hnotify.c     2011-08-24 13:30:24.731313534 +0200
12898 @@ -0,0 +1,712 @@
12899 +/*
12900 + * Copyright (C) 2005-2011 Junjiro R. Okajima
12901 + *
12902 + * This program, aufs is free software; you can redistribute it and/or modify
12903 + * it under the terms of the GNU General Public License as published by
12904 + * the Free Software Foundation; either version 2 of the License, or
12905 + * (at your option) any later version.
12906 + *
12907 + * This program is distributed in the hope that it will be useful,
12908 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12909 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12910 + * GNU General Public License for more details.
12911 + *
12912 + * You should have received a copy of the GNU General Public License
12913 + * along with this program; if not, write to the Free Software
12914 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12915 + */
12916 +
12917 +/*
12918 + * abstraction to notify the direct changes on lower directories
12919 + */
12920 +
12921 +#include "aufs.h"
12922 +
12923 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
12924 +{
12925 +       int err;
12926 +       struct au_hnotify *hn;
12927 +
12928 +       err = -ENOMEM;
12929 +       hn = au_cache_alloc_hnotify();
12930 +       if (hn) {
12931 +               hn->hn_aufs_inode = inode;
12932 +               hinode->hi_notify = hn;
12933 +               err = au_hnotify_op.alloc(hinode);
12934 +               AuTraceErr(err);
12935 +               if (unlikely(err)) {
12936 +                       hinode->hi_notify = NULL;
12937 +                       au_cache_free_hnotify(hn);
12938 +                       /*
12939 +                        * The upper dir was removed by udba, but the same named
12940 +                        * dir left. In this case, aufs assignes a new inode
12941 +                        * number and set the monitor again.
12942 +                        * For the lower dir, the old monitnor is still left.
12943 +                        */
12944 +                       if (err == -EEXIST)
12945 +                               err = 0;
12946 +               }
12947 +       }
12948 +
12949 +       AuTraceErr(err);
12950 +       return err;
12951 +}
12952 +
12953 +void au_hn_free(struct au_hinode *hinode)
12954 +{
12955 +       struct au_hnotify *hn;
12956 +
12957 +       hn = hinode->hi_notify;
12958 +       if (hn) {
12959 +               au_hnotify_op.free(hinode);
12960 +               au_cache_free_hnotify(hn);
12961 +               hinode->hi_notify = NULL;
12962 +       }
12963 +}
12964 +
12965 +/* ---------------------------------------------------------------------- */
12966 +
12967 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
12968 +{
12969 +       if (hinode->hi_notify)
12970 +               au_hnotify_op.ctl(hinode, do_set);
12971 +}
12972 +
12973 +void au_hn_reset(struct inode *inode, unsigned int flags)
12974 +{
12975 +       aufs_bindex_t bindex, bend;
12976 +       struct inode *hi;
12977 +       struct dentry *iwhdentry;
12978 +
12979 +       bend = au_ibend(inode);
12980 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
12981 +               hi = au_h_iptr(inode, bindex);
12982 +               if (!hi)
12983 +                       continue;
12984 +
12985 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
12986 +               iwhdentry = au_hi_wh(inode, bindex);
12987 +               if (iwhdentry)
12988 +                       dget(iwhdentry);
12989 +               au_igrab(hi);
12990 +               au_set_h_iptr(inode, bindex, NULL, 0);
12991 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
12992 +                             flags & ~AuHi_XINO);
12993 +               iput(hi);
12994 +               dput(iwhdentry);
12995 +               /* mutex_unlock(&hi->i_mutex); */
12996 +       }
12997 +}
12998 +
12999 +/* ---------------------------------------------------------------------- */
13000 +
13001 +static int hn_xino(struct inode *inode, struct inode *h_inode)
13002 +{
13003 +       int err;
13004 +       aufs_bindex_t bindex, bend, bfound, bstart;
13005 +       struct inode *h_i;
13006 +
13007 +       err = 0;
13008 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13009 +               pr_warning("branch root dir was changed\n");
13010 +               goto out;
13011 +       }
13012 +
13013 +       bfound = -1;
13014 +       bend = au_ibend(inode);
13015 +       bstart = au_ibstart(inode);
13016 +#if 0 /* reserved for future use */
13017 +       if (bindex == bend) {
13018 +               /* keep this ino in rename case */
13019 +               goto out;
13020 +       }
13021 +#endif
13022 +       for (bindex = bstart; bindex <= bend; bindex++)
13023 +               if (au_h_iptr(inode, bindex) == h_inode) {
13024 +                       bfound = bindex;
13025 +                       break;
13026 +               }
13027 +       if (bfound < 0)
13028 +               goto out;
13029 +
13030 +       for (bindex = bstart; bindex <= bend; bindex++) {
13031 +               h_i = au_h_iptr(inode, bindex);
13032 +               if (!h_i)
13033 +                       continue;
13034 +
13035 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
13036 +               /* ignore this error */
13037 +               /* bad action? */
13038 +       }
13039 +
13040 +       /* children inode number will be broken */
13041 +
13042 +out:
13043 +       AuTraceErr(err);
13044 +       return err;
13045 +}
13046 +
13047 +static int hn_gen_tree(struct dentry *dentry)
13048 +{
13049 +       int err, i, j, ndentry;
13050 +       struct au_dcsub_pages dpages;
13051 +       struct au_dpage *dpage;
13052 +       struct dentry **dentries;
13053 +
13054 +       err = au_dpages_init(&dpages, GFP_NOFS);
13055 +       if (unlikely(err))
13056 +               goto out;
13057 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
13058 +       if (unlikely(err))
13059 +               goto out_dpages;
13060 +
13061 +       for (i = 0; i < dpages.ndpage; i++) {
13062 +               dpage = dpages.dpages + i;
13063 +               dentries = dpage->dentries;
13064 +               ndentry = dpage->ndentry;
13065 +               for (j = 0; j < ndentry; j++) {
13066 +                       struct dentry *d;
13067 +
13068 +                       d = dentries[j];
13069 +                       if (IS_ROOT(d))
13070 +                               continue;
13071 +
13072 +                       au_digen_dec(d);
13073 +                       if (d->d_inode)
13074 +                               /* todo: reset children xino?
13075 +                                  cached children only? */
13076 +                               au_iigen_dec(d->d_inode);
13077 +               }
13078 +       }
13079 +
13080 +out_dpages:
13081 +       au_dpages_free(&dpages);
13082 +
13083 +#if 0
13084 +       /* discard children */
13085 +       dentry_unhash(dentry);
13086 +       dput(dentry);
13087 +#endif
13088 +out:
13089 +       return err;
13090 +}
13091 +
13092 +/*
13093 + * return 0 if processed.
13094 + */
13095 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
13096 +                          const unsigned int isdir)
13097 +{
13098 +       int err;
13099 +       struct dentry *d;
13100 +       struct qstr *dname;
13101 +
13102 +       err = 1;
13103 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13104 +               pr_warning("branch root dir was changed\n");
13105 +               err = 0;
13106 +               goto out;
13107 +       }
13108 +
13109 +       if (!isdir) {
13110 +               AuDebugOn(!name);
13111 +               au_iigen_dec(inode);
13112 +               spin_lock(&inode->i_lock);
13113 +               list_for_each_entry(d, &inode->i_dentry, d_alias) {
13114 +                       spin_lock(&d->d_lock);
13115 +                       dname = &d->d_name;
13116 +                       if (dname->len != nlen
13117 +                           && memcmp(dname->name, name, nlen)) {
13118 +                               spin_unlock(&d->d_lock);
13119 +                               continue;
13120 +                       }
13121 +                       err = 0;
13122 +                       au_digen_dec(d);
13123 +                       spin_unlock(&d->d_lock);
13124 +                       break;
13125 +               }
13126 +               spin_unlock(&inode->i_lock);
13127 +       } else {
13128 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
13129 +               d = d_find_alias(inode);
13130 +               if (!d) {
13131 +                       au_iigen_dec(inode);
13132 +                       goto out;
13133 +               }
13134 +
13135 +               spin_lock(&d->d_lock);
13136 +               dname = &d->d_name;
13137 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
13138 +                       spin_unlock(&d->d_lock);
13139 +                       err = hn_gen_tree(d);
13140 +                       spin_lock(&d->d_lock);
13141 +               }
13142 +               spin_unlock(&d->d_lock);
13143 +               dput(d);
13144 +       }
13145 +
13146 +out:
13147 +       AuTraceErr(err);
13148 +       return err;
13149 +}
13150 +
13151 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
13152 +{
13153 +       int err;
13154 +       struct inode *inode;
13155 +
13156 +       inode = dentry->d_inode;
13157 +       if (IS_ROOT(dentry)
13158 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
13159 +               ) {
13160 +               pr_warning("branch root dir was changed\n");
13161 +               return 0;
13162 +       }
13163 +
13164 +       err = 0;
13165 +       if (!isdir) {
13166 +               au_digen_dec(dentry);
13167 +               if (inode)
13168 +                       au_iigen_dec(inode);
13169 +       } else {
13170 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
13171 +               if (inode)
13172 +                       err = hn_gen_tree(dentry);
13173 +       }
13174 +
13175 +       AuTraceErr(err);
13176 +       return err;
13177 +}
13178 +
13179 +/* ---------------------------------------------------------------------- */
13180 +
13181 +/* hnotify job flags */
13182 +#define AuHnJob_XINO0          1
13183 +#define AuHnJob_GEN            (1 << 1)
13184 +#define AuHnJob_DIRENT         (1 << 2)
13185 +#define AuHnJob_ISDIR          (1 << 3)
13186 +#define AuHnJob_TRYXINO0       (1 << 4)
13187 +#define AuHnJob_MNTPNT         (1 << 5)
13188 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
13189 +#define au_fset_hnjob(flags, name) \
13190 +       do { (flags) |= AuHnJob_##name; } while (0)
13191 +#define au_fclr_hnjob(flags, name) \
13192 +       do { (flags) &= ~AuHnJob_##name; } while (0)
13193 +
13194 +enum {
13195 +       AuHn_CHILD,
13196 +       AuHn_PARENT,
13197 +       AuHnLast
13198 +};
13199 +
13200 +struct au_hnotify_args {
13201 +       struct inode *h_dir, *dir, *h_child_inode;
13202 +       u32 mask;
13203 +       unsigned int flags[AuHnLast];
13204 +       unsigned int h_child_nlen;
13205 +       char h_child_name[];
13206 +};
13207 +
13208 +struct hn_job_args {
13209 +       unsigned int flags;
13210 +       struct inode *inode, *h_inode, *dir, *h_dir;
13211 +       struct dentry *dentry;
13212 +       char *h_name;
13213 +       int h_nlen;
13214 +};
13215 +
13216 +static int hn_job(struct hn_job_args *a)
13217 +{
13218 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
13219 +
13220 +       /* reset xino */
13221 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
13222 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
13223 +
13224 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
13225 +           && a->inode
13226 +           && a->h_inode) {
13227 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
13228 +               if (!a->h_inode->i_nlink)
13229 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
13230 +               mutex_unlock(&a->h_inode->i_mutex);
13231 +       }
13232 +
13233 +       /* make the generation obsolete */
13234 +       if (au_ftest_hnjob(a->flags, GEN)) {
13235 +               int err = -1;
13236 +               if (a->inode)
13237 +                       err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
13238 +                                             isdir);
13239 +               if (err && a->dentry)
13240 +                       hn_gen_by_name(a->dentry, isdir);
13241 +               /* ignore this error */
13242 +       }
13243 +
13244 +       /* make dir entries obsolete */
13245 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
13246 +               struct au_vdir *vdir;
13247 +
13248 +               vdir = au_ivdir(a->inode);
13249 +               if (vdir)
13250 +                       vdir->vd_jiffy = 0;
13251 +               /* IMustLock(a->inode); */
13252 +               /* a->inode->i_version++; */
13253 +       }
13254 +
13255 +       /* can do nothing but warn */
13256 +       if (au_ftest_hnjob(a->flags, MNTPNT)
13257 +           && a->dentry
13258 +           && d_mountpoint(a->dentry))
13259 +               pr_warning("mount-point %.*s is removed or renamed\n",
13260 +                          AuDLNPair(a->dentry));
13261 +
13262 +       return 0;
13263 +}
13264 +
13265 +/* ---------------------------------------------------------------------- */
13266 +
13267 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
13268 +                                          struct inode *dir)
13269 +{
13270 +       struct dentry *dentry, *d, *parent;
13271 +       struct qstr *dname;
13272 +
13273 +       parent = d_find_alias(dir);
13274 +       if (!parent)
13275 +               return NULL;
13276 +
13277 +       dentry = NULL;
13278 +       spin_lock(&parent->d_lock);
13279 +       list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
13280 +               /* AuDbg("%.*s\n", AuDLNPair(d)); */
13281 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
13282 +               dname = &d->d_name;
13283 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
13284 +                       goto cont_unlock;
13285 +               if (au_di(d))
13286 +                       au_digen_dec(d);
13287 +               else
13288 +                       goto cont_unlock;
13289 +               if (d->d_count) {
13290 +                       dentry = dget_dlock(d);
13291 +                       spin_unlock(&d->d_lock);
13292 +                       break;
13293 +               }
13294 +
13295 +       cont_unlock:
13296 +               spin_unlock(&d->d_lock);
13297 +       }
13298 +       spin_unlock(&parent->d_lock);
13299 +       dput(parent);
13300 +
13301 +       if (dentry)
13302 +               di_write_lock_child(dentry);
13303 +
13304 +       return dentry;
13305 +}
13306 +
13307 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
13308 +                                        aufs_bindex_t bindex, ino_t h_ino)
13309 +{
13310 +       struct inode *inode;
13311 +       ino_t ino;
13312 +       int err;
13313 +
13314 +       inode = NULL;
13315 +       err = au_xino_read(sb, bindex, h_ino, &ino);
13316 +       if (!err && ino)
13317 +               inode = ilookup(sb, ino);
13318 +       if (!inode)
13319 +               goto out;
13320 +
13321 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13322 +               pr_warning("wrong root branch\n");
13323 +               iput(inode);
13324 +               inode = NULL;
13325 +               goto out;
13326 +       }
13327 +
13328 +       ii_write_lock_child(inode);
13329 +
13330 +out:
13331 +       return inode;
13332 +}
13333 +
13334 +static void au_hn_bh(void *_args)
13335 +{
13336 +       struct au_hnotify_args *a = _args;
13337 +       struct super_block *sb;
13338 +       aufs_bindex_t bindex, bend, bfound;
13339 +       unsigned char xino, try_iput;
13340 +       int err;
13341 +       struct inode *inode;
13342 +       ino_t h_ino;
13343 +       struct hn_job_args args;
13344 +       struct dentry *dentry;
13345 +       struct au_sbinfo *sbinfo;
13346 +
13347 +       AuDebugOn(!_args);
13348 +       AuDebugOn(!a->h_dir);
13349 +       AuDebugOn(!a->dir);
13350 +       AuDebugOn(!a->mask);
13351 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
13352 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
13353 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
13354 +
13355 +       inode = NULL;
13356 +       dentry = NULL;
13357 +       /*
13358 +        * do not lock a->dir->i_mutex here
13359 +        * because of d_revalidate() may cause a deadlock.
13360 +        */
13361 +       sb = a->dir->i_sb;
13362 +       AuDebugOn(!sb);
13363 +       sbinfo = au_sbi(sb);
13364 +       AuDebugOn(!sbinfo);
13365 +       si_write_lock(sb, AuLock_NOPLMW);
13366 +
13367 +       ii_read_lock_parent(a->dir);
13368 +       bfound = -1;
13369 +       bend = au_ibend(a->dir);
13370 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
13371 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
13372 +                       bfound = bindex;
13373 +                       break;
13374 +               }
13375 +       ii_read_unlock(a->dir);
13376 +       if (unlikely(bfound < 0))
13377 +               goto out;
13378 +
13379 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
13380 +       h_ino = 0;
13381 +       if (a->h_child_inode)
13382 +               h_ino = a->h_child_inode->i_ino;
13383 +
13384 +       if (a->h_child_nlen
13385 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
13386 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
13387 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
13388 +                                             a->dir);
13389 +       try_iput = 0;
13390 +       if (dentry)
13391 +               inode = dentry->d_inode;
13392 +       if (xino && !inode && h_ino
13393 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
13394 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
13395 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
13396 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
13397 +               try_iput = 1;
13398 +           }
13399 +
13400 +       args.flags = a->flags[AuHn_CHILD];
13401 +       args.dentry = dentry;
13402 +       args.inode = inode;
13403 +       args.h_inode = a->h_child_inode;
13404 +       args.dir = a->dir;
13405 +       args.h_dir = a->h_dir;
13406 +       args.h_name = a->h_child_name;
13407 +       args.h_nlen = a->h_child_nlen;
13408 +       err = hn_job(&args);
13409 +       if (dentry) {
13410 +               if (au_di(dentry))
13411 +                       di_write_unlock(dentry);
13412 +               dput(dentry);
13413 +       }
13414 +       if (inode && try_iput) {
13415 +               ii_write_unlock(inode);
13416 +               iput(inode);
13417 +       }
13418 +
13419 +       ii_write_lock_parent(a->dir);
13420 +       args.flags = a->flags[AuHn_PARENT];
13421 +       args.dentry = NULL;
13422 +       args.inode = a->dir;
13423 +       args.h_inode = a->h_dir;
13424 +       args.dir = NULL;
13425 +       args.h_dir = NULL;
13426 +       args.h_name = NULL;
13427 +       args.h_nlen = 0;
13428 +       err = hn_job(&args);
13429 +       ii_write_unlock(a->dir);
13430 +
13431 +out:
13432 +       iput(a->h_child_inode);
13433 +       iput(a->h_dir);
13434 +       iput(a->dir);
13435 +       si_write_unlock(sb);
13436 +       au_nwt_done(&sbinfo->si_nowait);
13437 +       kfree(a);
13438 +}
13439 +
13440 +/* ---------------------------------------------------------------------- */
13441 +
13442 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
13443 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
13444 +{
13445 +       int err, len;
13446 +       unsigned int flags[AuHnLast], f;
13447 +       unsigned char isdir, isroot, wh;
13448 +       struct inode *dir;
13449 +       struct au_hnotify_args *args;
13450 +       char *p, *h_child_name;
13451 +
13452 +       err = 0;
13453 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
13454 +       dir = igrab(hnotify->hn_aufs_inode);
13455 +       if (!dir)
13456 +               goto out;
13457 +
13458 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
13459 +       wh = 0;
13460 +       h_child_name = (void *)h_child_qstr->name;
13461 +       len = h_child_qstr->len;
13462 +       if (h_child_name) {
13463 +               if (len > AUFS_WH_PFX_LEN
13464 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
13465 +                       h_child_name += AUFS_WH_PFX_LEN;
13466 +                       len -= AUFS_WH_PFX_LEN;
13467 +                       wh = 1;
13468 +               }
13469 +       }
13470 +
13471 +       isdir = 0;
13472 +       if (h_child_inode)
13473 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
13474 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
13475 +       flags[AuHn_CHILD] = 0;
13476 +       if (isdir)
13477 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
13478 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
13479 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
13480 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
13481 +       case FS_MOVED_FROM:
13482 +       case FS_MOVED_TO:
13483 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
13484 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13485 +               /*FALLTHROUGH*/
13486 +       case FS_CREATE:
13487 +               AuDebugOn(!h_child_name || !h_child_inode);
13488 +               break;
13489 +
13490 +       case FS_DELETE:
13491 +               /*
13492 +                * aufs never be able to get this child inode.
13493 +                * revalidation should be in d_revalidate()
13494 +                * by checking i_nlink, i_generation or d_unhashed().
13495 +                */
13496 +               AuDebugOn(!h_child_name);
13497 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
13498 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13499 +               break;
13500 +
13501 +       default:
13502 +               AuDebugOn(1);
13503 +       }
13504 +
13505 +       if (wh)
13506 +               h_child_inode = NULL;
13507 +
13508 +       err = -ENOMEM;
13509 +       /* iput() and kfree() will be called in au_hnotify() */
13510 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
13511 +       if (unlikely(!args)) {
13512 +               AuErr1("no memory\n");
13513 +               iput(dir);
13514 +               goto out;
13515 +       }
13516 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
13517 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
13518 +       args->mask = mask;
13519 +       args->dir = dir;
13520 +       args->h_dir = igrab(h_dir);
13521 +       if (h_child_inode)
13522 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
13523 +       args->h_child_inode = h_child_inode;
13524 +       args->h_child_nlen = len;
13525 +       if (len) {
13526 +               p = (void *)args;
13527 +               p += sizeof(*args);
13528 +               memcpy(p, h_child_name, len);
13529 +               p[len] = 0;
13530 +       }
13531 +
13532 +       f = 0;
13533 +       if (!dir->i_nlink)
13534 +               f = AuWkq_NEST;
13535 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
13536 +       if (unlikely(err)) {
13537 +               pr_err("wkq %d\n", err);
13538 +               iput(args->h_child_inode);
13539 +               iput(args->h_dir);
13540 +               iput(args->dir);
13541 +               kfree(args);
13542 +       }
13543 +
13544 +out:
13545 +       return err;
13546 +}
13547 +
13548 +/* ---------------------------------------------------------------------- */
13549 +
13550 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
13551 +{
13552 +       int err;
13553 +
13554 +       AuDebugOn(!(udba & AuOptMask_UDBA));
13555 +
13556 +       err = 0;
13557 +       if (au_hnotify_op.reset_br)
13558 +               err = au_hnotify_op.reset_br(udba, br, perm);
13559 +
13560 +       return err;
13561 +}
13562 +
13563 +int au_hnotify_init_br(struct au_branch *br, int perm)
13564 +{
13565 +       int err;
13566 +
13567 +       err = 0;
13568 +       if (au_hnotify_op.init_br)
13569 +               err = au_hnotify_op.init_br(br, perm);
13570 +
13571 +       return err;
13572 +}
13573 +
13574 +void au_hnotify_fin_br(struct au_branch *br)
13575 +{
13576 +       if (au_hnotify_op.fin_br)
13577 +               au_hnotify_op.fin_br(br);
13578 +}
13579 +
13580 +static void au_hn_destroy_cache(void)
13581 +{
13582 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
13583 +       au_cachep[AuCache_HNOTIFY] = NULL;
13584 +}
13585 +
13586 +int __init au_hnotify_init(void)
13587 +{
13588 +       int err;
13589 +
13590 +       err = -ENOMEM;
13591 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
13592 +       if (au_cachep[AuCache_HNOTIFY]) {
13593 +               err = 0;
13594 +               if (au_hnotify_op.init)
13595 +                       err = au_hnotify_op.init();
13596 +               if (unlikely(err))
13597 +                       au_hn_destroy_cache();
13598 +       }
13599 +       AuTraceErr(err);
13600 +       return err;
13601 +}
13602 +
13603 +void au_hnotify_fin(void)
13604 +{
13605 +       if (au_hnotify_op.fin)
13606 +               au_hnotify_op.fin();
13607 +       /* cf. au_cache_fin() */
13608 +       if (au_cachep[AuCache_HNOTIFY])
13609 +               au_hn_destroy_cache();
13610 +}
13611 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
13612 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
13613 +++ linux/fs/aufs/iinfo.c       2011-08-24 13:30:24.734646739 +0200
13614 @@ -0,0 +1,264 @@
13615 +/*
13616 + * Copyright (C) 2005-2011 Junjiro R. Okajima
13617 + *
13618 + * This program, aufs is free software; you can redistribute it and/or modify
13619 + * it under the terms of the GNU General Public License as published by
13620 + * the Free Software Foundation; either version 2 of the License, or
13621 + * (at your option) any later version.
13622 + *
13623 + * This program is distributed in the hope that it will be useful,
13624 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13625 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13626 + * GNU General Public License for more details.
13627 + *
13628 + * You should have received a copy of the GNU General Public License
13629 + * along with this program; if not, write to the Free Software
13630 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13631 + */
13632 +
13633 +/*
13634 + * inode private data
13635 + */
13636 +
13637 +#include "aufs.h"
13638 +
13639 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
13640 +{
13641 +       struct inode *h_inode;
13642 +
13643 +       IiMustAnyLock(inode);
13644 +
13645 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
13646 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
13647 +       return h_inode;
13648 +}
13649 +
13650 +/* todo: hard/soft set? */
13651 +void au_hiput(struct au_hinode *hinode)
13652 +{
13653 +       au_hn_free(hinode);
13654 +       dput(hinode->hi_whdentry);
13655 +       iput(hinode->hi_inode);
13656 +}
13657 +
13658 +unsigned int au_hi_flags(struct inode *inode, int isdir)
13659 +{
13660 +       unsigned int flags;
13661 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
13662 +
13663 +       flags = 0;
13664 +       if (au_opt_test(mnt_flags, XINO))
13665 +               au_fset_hi(flags, XINO);
13666 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
13667 +               au_fset_hi(flags, HNOTIFY);
13668 +       return flags;
13669 +}
13670 +
13671 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
13672 +                  struct inode *h_inode, unsigned int flags)
13673 +{
13674 +       struct au_hinode *hinode;
13675 +       struct inode *hi;
13676 +       struct au_iinfo *iinfo = au_ii(inode);
13677 +
13678 +       IiMustWriteLock(inode);
13679 +
13680 +       hinode = iinfo->ii_hinode + bindex;
13681 +       hi = hinode->hi_inode;
13682 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
13683 +
13684 +       if (hi)
13685 +               au_hiput(hinode);
13686 +       hinode->hi_inode = h_inode;
13687 +       if (h_inode) {
13688 +               int err;
13689 +               struct super_block *sb = inode->i_sb;
13690 +               struct au_branch *br;
13691 +
13692 +               AuDebugOn(inode->i_mode
13693 +                         && (h_inode->i_mode & S_IFMT)
13694 +                         != (inode->i_mode & S_IFMT));
13695 +               if (bindex == iinfo->ii_bstart)
13696 +                       au_cpup_igen(inode, h_inode);
13697 +               br = au_sbr(sb, bindex);
13698 +               hinode->hi_id = br->br_id;
13699 +               if (au_ftest_hi(flags, XINO)) {
13700 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
13701 +                                           inode->i_ino);
13702 +                       if (unlikely(err))
13703 +                               AuIOErr1("failed au_xino_write() %d\n", err);
13704 +               }
13705 +
13706 +               if (au_ftest_hi(flags, HNOTIFY)
13707 +                   && au_br_hnotifyable(br->br_perm)) {
13708 +                       err = au_hn_alloc(hinode, inode);
13709 +                       if (unlikely(err))
13710 +                               AuIOErr1("au_hn_alloc() %d\n", err);
13711 +               }
13712 +       }
13713 +}
13714 +
13715 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
13716 +                 struct dentry *h_wh)
13717 +{
13718 +       struct au_hinode *hinode;
13719 +
13720 +       IiMustWriteLock(inode);
13721 +
13722 +       hinode = au_ii(inode)->ii_hinode + bindex;
13723 +       AuDebugOn(hinode->hi_whdentry);
13724 +       hinode->hi_whdentry = h_wh;
13725 +}
13726 +
13727 +void au_update_iigen(struct inode *inode)
13728 +{
13729 +       atomic_set(&au_ii(inode)->ii_generation, au_sigen(inode->i_sb));
13730 +       /* smp_mb(); */ /* atomic_set */
13731 +}
13732 +
13733 +/* it may be called at remount time, too */
13734 +void au_update_ibrange(struct inode *inode, int do_put_zero)
13735 +{
13736 +       struct au_iinfo *iinfo;
13737 +       aufs_bindex_t bindex, bend;
13738 +
13739 +       iinfo = au_ii(inode);
13740 +       if (!iinfo)
13741 +               return;
13742 +
13743 +       IiMustWriteLock(inode);
13744 +
13745 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
13746 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
13747 +                    bindex++) {
13748 +                       struct inode *h_i;
13749 +
13750 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
13751 +                       if (h_i && !h_i->i_nlink)
13752 +                               au_set_h_iptr(inode, bindex, NULL, 0);
13753 +               }
13754 +       }
13755 +
13756 +       iinfo->ii_bstart = -1;
13757 +       iinfo->ii_bend = -1;
13758 +       bend = au_sbend(inode->i_sb);
13759 +       for (bindex = 0; bindex <= bend; bindex++)
13760 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
13761 +                       iinfo->ii_bstart = bindex;
13762 +                       break;
13763 +               }
13764 +       if (iinfo->ii_bstart >= 0)
13765 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
13766 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
13767 +                               iinfo->ii_bend = bindex;
13768 +                               break;
13769 +                       }
13770 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
13771 +}
13772 +
13773 +/* ---------------------------------------------------------------------- */
13774 +
13775 +void au_icntnr_init_once(void *_c)
13776 +{
13777 +       struct au_icntnr *c = _c;
13778 +       struct au_iinfo *iinfo = &c->iinfo;
13779 +       static struct lock_class_key aufs_ii;
13780 +
13781 +       au_rw_init(&iinfo->ii_rwsem);
13782 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
13783 +       inode_init_once(&c->vfs_inode);
13784 +}
13785 +
13786 +int au_iinfo_init(struct inode *inode)
13787 +{
13788 +       struct au_iinfo *iinfo;
13789 +       struct super_block *sb;
13790 +       int nbr, i;
13791 +
13792 +       sb = inode->i_sb;
13793 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
13794 +       nbr = au_sbend(sb) + 1;
13795 +       if (unlikely(nbr <= 0))
13796 +               nbr = 1;
13797 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
13798 +       if (iinfo->ii_hinode) {
13799 +               au_ninodes_inc(sb);
13800 +               for (i = 0; i < nbr; i++)
13801 +                       iinfo->ii_hinode[i].hi_id = -1;
13802 +
13803 +               atomic_set(&iinfo->ii_generation, au_sigen(sb));
13804 +               /* smp_mb(); */ /* atomic_set */
13805 +               iinfo->ii_bstart = -1;
13806 +               iinfo->ii_bend = -1;
13807 +               iinfo->ii_vdir = NULL;
13808 +               return 0;
13809 +       }
13810 +       return -ENOMEM;
13811 +}
13812 +
13813 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
13814 +{
13815 +       int err, sz;
13816 +       struct au_hinode *hip;
13817 +
13818 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
13819 +
13820 +       err = -ENOMEM;
13821 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
13822 +       if (!sz)
13823 +               sz = sizeof(*hip);
13824 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
13825 +       if (hip) {
13826 +               iinfo->ii_hinode = hip;
13827 +               err = 0;
13828 +       }
13829 +
13830 +       return err;
13831 +}
13832 +
13833 +void au_iinfo_fin(struct inode *inode)
13834 +{
13835 +       struct au_iinfo *iinfo;
13836 +       struct au_hinode *hi;
13837 +       struct super_block *sb;
13838 +       aufs_bindex_t bindex, bend;
13839 +       const unsigned char unlinked = !inode->i_nlink;
13840 +
13841 +       iinfo = au_ii(inode);
13842 +       /* bad_inode case */
13843 +       if (!iinfo)
13844 +               return;
13845 +
13846 +       sb = inode->i_sb;
13847 +       au_ninodes_dec(sb);
13848 +       if (si_pid_test(sb))
13849 +               au_xino_delete_inode(inode, unlinked);
13850 +       else {
13851 +               /*
13852 +                * it is safe to hide the dependency between sbinfo and
13853 +                * sb->s_umount.
13854 +                */
13855 +               lockdep_off();
13856 +               si_noflush_read_lock(sb);
13857 +               au_xino_delete_inode(inode, unlinked);
13858 +               si_read_unlock(sb);
13859 +               lockdep_on();
13860 +       }
13861 +
13862 +       if (iinfo->ii_vdir)
13863 +               au_vdir_free(iinfo->ii_vdir);
13864 +
13865 +       bindex = iinfo->ii_bstart;
13866 +       if (bindex >= 0) {
13867 +               hi = iinfo->ii_hinode + bindex;
13868 +               bend = iinfo->ii_bend;
13869 +               while (bindex++ <= bend) {
13870 +                       if (hi->hi_inode)
13871 +                               au_hiput(hi);
13872 +                       hi++;
13873 +               }
13874 +       }
13875 +       kfree(iinfo->ii_hinode);
13876 +       iinfo->ii_hinode = NULL;
13877 +       AuRwDestroy(&iinfo->ii_rwsem);
13878 +}
13879 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
13880 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
13881 +++ linux/fs/aufs/inode.c       2011-08-24 13:30:24.734646739 +0200
13882 @@ -0,0 +1,471 @@
13883 +/*
13884 + * Copyright (C) 2005-2011 Junjiro R. Okajima
13885 + *
13886 + * This program, aufs is free software; you can redistribute it and/or modify
13887 + * it under the terms of the GNU General Public License as published by
13888 + * the Free Software Foundation; either version 2 of the License, or
13889 + * (at your option) any later version.
13890 + *
13891 + * This program is distributed in the hope that it will be useful,
13892 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13893 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13894 + * GNU General Public License for more details.
13895 + *
13896 + * You should have received a copy of the GNU General Public License
13897 + * along with this program; if not, write to the Free Software
13898 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13899 + */
13900 +
13901 +/*
13902 + * inode functions
13903 + */
13904 +
13905 +#include "aufs.h"
13906 +
13907 +struct inode *au_igrab(struct inode *inode)
13908 +{
13909 +       if (inode) {
13910 +               AuDebugOn(!atomic_read(&inode->i_count));
13911 +               ihold(inode);
13912 +       }
13913 +       return inode;
13914 +}
13915 +
13916 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
13917 +{
13918 +       au_cpup_attr_all(inode, /*force*/0);
13919 +       au_update_iigen(inode);
13920 +       if (do_version)
13921 +               inode->i_version++;
13922 +}
13923 +
13924 +static int au_ii_refresh(struct inode *inode, int *update)
13925 +{
13926 +       int err, e;
13927 +       umode_t type;
13928 +       aufs_bindex_t bindex, new_bindex;
13929 +       struct super_block *sb;
13930 +       struct au_iinfo *iinfo;
13931 +       struct au_hinode *p, *q, tmp;
13932 +
13933 +       IiMustWriteLock(inode);
13934 +
13935 +       *update = 0;
13936 +       sb = inode->i_sb;
13937 +       type = inode->i_mode & S_IFMT;
13938 +       iinfo = au_ii(inode);
13939 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
13940 +       if (unlikely(err))
13941 +               goto out;
13942 +
13943 +       AuDebugOn(iinfo->ii_bstart < 0);
13944 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
13945 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
13946 +            bindex++, p++) {
13947 +               if (!p->hi_inode)
13948 +                       continue;
13949 +
13950 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
13951 +               new_bindex = au_br_index(sb, p->hi_id);
13952 +               if (new_bindex == bindex)
13953 +                       continue;
13954 +
13955 +               if (new_bindex < 0) {
13956 +                       *update = 1;
13957 +                       au_hiput(p);
13958 +                       p->hi_inode = NULL;
13959 +                       continue;
13960 +               }
13961 +
13962 +               if (new_bindex < iinfo->ii_bstart)
13963 +                       iinfo->ii_bstart = new_bindex;
13964 +               if (iinfo->ii_bend < new_bindex)
13965 +                       iinfo->ii_bend = new_bindex;
13966 +               /* swap two lower inode, and loop again */
13967 +               q = iinfo->ii_hinode + new_bindex;
13968 +               tmp = *q;
13969 +               *q = *p;
13970 +               *p = tmp;
13971 +               if (tmp.hi_inode) {
13972 +                       bindex--;
13973 +                       p--;
13974 +               }
13975 +       }
13976 +       au_update_ibrange(inode, /*do_put_zero*/0);
13977 +       e = au_dy_irefresh(inode);
13978 +       if (unlikely(e && !err))
13979 +               err = e;
13980 +
13981 +out:
13982 +       AuTraceErr(err);
13983 +       return err;
13984 +}
13985 +
13986 +int au_refresh_hinode_self(struct inode *inode)
13987 +{
13988 +       int err, update;
13989 +
13990 +       err = au_ii_refresh(inode, &update);
13991 +       if (!err)
13992 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
13993 +
13994 +       AuTraceErr(err);
13995 +       return err;
13996 +}
13997 +
13998 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
13999 +{
14000 +       int err, e, update;
14001 +       unsigned int flags;
14002 +       umode_t mode;
14003 +       aufs_bindex_t bindex, bend;
14004 +       unsigned char isdir;
14005 +       struct au_hinode *p;
14006 +       struct au_iinfo *iinfo;
14007 +
14008 +       err = au_ii_refresh(inode, &update);
14009 +       if (unlikely(err))
14010 +               goto out;
14011 +
14012 +       update = 0;
14013 +       iinfo = au_ii(inode);
14014 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14015 +       mode = (inode->i_mode & S_IFMT);
14016 +       isdir = S_ISDIR(mode);
14017 +       flags = au_hi_flags(inode, isdir);
14018 +       bend = au_dbend(dentry);
14019 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
14020 +               struct inode *h_i;
14021 +               struct dentry *h_d;
14022 +
14023 +               h_d = au_h_dptr(dentry, bindex);
14024 +               if (!h_d || !h_d->d_inode)
14025 +                       continue;
14026 +
14027 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
14028 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
14029 +                       h_i = au_h_iptr(inode, bindex);
14030 +                       if (h_i) {
14031 +                               if (h_i == h_d->d_inode)
14032 +                                       continue;
14033 +                               err = -EIO;
14034 +                               break;
14035 +                       }
14036 +               }
14037 +               if (bindex < iinfo->ii_bstart)
14038 +                       iinfo->ii_bstart = bindex;
14039 +               if (iinfo->ii_bend < bindex)
14040 +                       iinfo->ii_bend = bindex;
14041 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
14042 +               update = 1;
14043 +       }
14044 +       au_update_ibrange(inode, /*do_put_zero*/0);
14045 +       e = au_dy_irefresh(inode);
14046 +       if (unlikely(e && !err))
14047 +               err = e;
14048 +       if (!err)
14049 +               au_refresh_hinode_attr(inode, update && isdir);
14050 +
14051 +out:
14052 +       AuTraceErr(err);
14053 +       return err;
14054 +}
14055 +
14056 +static int set_inode(struct inode *inode, struct dentry *dentry)
14057 +{
14058 +       int err;
14059 +       unsigned int flags;
14060 +       umode_t mode;
14061 +       aufs_bindex_t bindex, bstart, btail;
14062 +       unsigned char isdir;
14063 +       struct dentry *h_dentry;
14064 +       struct inode *h_inode;
14065 +       struct au_iinfo *iinfo;
14066 +
14067 +       IiMustWriteLock(inode);
14068 +
14069 +       err = 0;
14070 +       isdir = 0;
14071 +       bstart = au_dbstart(dentry);
14072 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
14073 +       mode = h_inode->i_mode;
14074 +       switch (mode & S_IFMT) {
14075 +       case S_IFREG:
14076 +               btail = au_dbtail(dentry);
14077 +               inode->i_op = &aufs_iop;
14078 +               inode->i_fop = &aufs_file_fop;
14079 +               err = au_dy_iaop(inode, bstart, h_inode);
14080 +               if (unlikely(err))
14081 +                       goto out;
14082 +               break;
14083 +       case S_IFDIR:
14084 +               isdir = 1;
14085 +               btail = au_dbtaildir(dentry);
14086 +               inode->i_op = &aufs_dir_iop;
14087 +               inode->i_fop = &aufs_dir_fop;
14088 +               break;
14089 +       case S_IFLNK:
14090 +               btail = au_dbtail(dentry);
14091 +               inode->i_op = &aufs_symlink_iop;
14092 +               break;
14093 +       case S_IFBLK:
14094 +       case S_IFCHR:
14095 +       case S_IFIFO:
14096 +       case S_IFSOCK:
14097 +               btail = au_dbtail(dentry);
14098 +               inode->i_op = &aufs_iop;
14099 +               au_init_special_fop(inode, mode, h_inode->i_rdev);
14100 +               break;
14101 +       default:
14102 +               AuIOErr("Unknown file type 0%o\n", mode);
14103 +               err = -EIO;
14104 +               goto out;
14105 +       }
14106 +
14107 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
14108 +       flags = au_hi_flags(inode, isdir);
14109 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
14110 +           && au_ftest_hi(flags, HNOTIFY)
14111 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
14112 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
14113 +               au_fclr_hi(flags, HNOTIFY);
14114 +       iinfo = au_ii(inode);
14115 +       iinfo->ii_bstart = bstart;
14116 +       iinfo->ii_bend = btail;
14117 +       for (bindex = bstart; bindex <= btail; bindex++) {
14118 +               h_dentry = au_h_dptr(dentry, bindex);
14119 +               if (h_dentry)
14120 +                       au_set_h_iptr(inode, bindex,
14121 +                                     au_igrab(h_dentry->d_inode), flags);
14122 +       }
14123 +       au_cpup_attr_all(inode, /*force*/1);
14124 +
14125 +out:
14126 +       return err;
14127 +}
14128 +
14129 +/*
14130 + * successful returns with iinfo write_locked
14131 + * minus: errno
14132 + * zero: success, matched
14133 + * plus: no error, but unmatched
14134 + */
14135 +static int reval_inode(struct inode *inode, struct dentry *dentry)
14136 +{
14137 +       int err;
14138 +       aufs_bindex_t bindex, bend;
14139 +       struct inode *h_inode, *h_dinode;
14140 +
14141 +       /*
14142 +        * before this function, if aufs got any iinfo lock, it must be only
14143 +        * one, the parent dir.
14144 +        * it can happen by UDBA and the obsoleted inode number.
14145 +        */
14146 +       err = -EIO;
14147 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
14148 +               goto out;
14149 +
14150 +       err = 1;
14151 +       ii_write_lock_new_child(inode);
14152 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
14153 +       bend = au_ibend(inode);
14154 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14155 +               h_inode = au_h_iptr(inode, bindex);
14156 +               if (h_inode && h_inode == h_dinode) {
14157 +                       err = 0;
14158 +                       if (au_iigen_test(inode, au_digen(dentry)))
14159 +                               err = au_refresh_hinode(inode, dentry);
14160 +                       break;
14161 +               }
14162 +       }
14163 +
14164 +       if (unlikely(err))
14165 +               ii_write_unlock(inode);
14166 +out:
14167 +       return err;
14168 +}
14169 +
14170 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14171 +          unsigned int d_type, ino_t *ino)
14172 +{
14173 +       int err;
14174 +       struct mutex *mtx;
14175 +
14176 +       /* prevent hardlinked inode number from race condition */
14177 +       mtx = NULL;
14178 +       if (d_type != DT_DIR) {
14179 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
14180 +               mutex_lock(mtx);
14181 +       }
14182 +       err = au_xino_read(sb, bindex, h_ino, ino);
14183 +       if (unlikely(err))
14184 +               goto out;
14185 +
14186 +       if (!*ino) {
14187 +               err = -EIO;
14188 +               *ino = au_xino_new_ino(sb);
14189 +               if (unlikely(!*ino))
14190 +                       goto out;
14191 +               err = au_xino_write(sb, bindex, h_ino, *ino);
14192 +               if (unlikely(err))
14193 +                       goto out;
14194 +       }
14195 +
14196 +out:
14197 +       if (mtx)
14198 +               mutex_unlock(mtx);
14199 +       return err;
14200 +}
14201 +
14202 +/* successful returns with iinfo write_locked */
14203 +/* todo: return with unlocked? */
14204 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
14205 +{
14206 +       struct inode *inode, *h_inode;
14207 +       struct dentry *h_dentry;
14208 +       struct super_block *sb;
14209 +       struct mutex *mtx;
14210 +       ino_t h_ino, ino;
14211 +       int err;
14212 +       aufs_bindex_t bstart;
14213 +
14214 +       sb = dentry->d_sb;
14215 +       bstart = au_dbstart(dentry);
14216 +       h_dentry = au_h_dptr(dentry, bstart);
14217 +       h_inode = h_dentry->d_inode;
14218 +       h_ino = h_inode->i_ino;
14219 +
14220 +       /*
14221 +        * stop 'race'-ing between hardlinks under different
14222 +        * parents.
14223 +        */
14224 +       mtx = NULL;
14225 +       if (!S_ISDIR(h_inode->i_mode))
14226 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
14227 +
14228 +new_ino:
14229 +       if (mtx)
14230 +               mutex_lock(mtx);
14231 +       err = au_xino_read(sb, bstart, h_ino, &ino);
14232 +       inode = ERR_PTR(err);
14233 +       if (unlikely(err))
14234 +               goto out;
14235 +
14236 +       if (!ino) {
14237 +               ino = au_xino_new_ino(sb);
14238 +               if (unlikely(!ino)) {
14239 +                       inode = ERR_PTR(-EIO);
14240 +                       goto out;
14241 +               }
14242 +       }
14243 +
14244 +       AuDbg("i%lu\n", (unsigned long)ino);
14245 +       inode = au_iget_locked(sb, ino);
14246 +       err = PTR_ERR(inode);
14247 +       if (IS_ERR(inode))
14248 +               goto out;
14249 +
14250 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
14251 +       if (inode->i_state & I_NEW) {
14252 +               ii_write_lock_new_child(inode);
14253 +               err = set_inode(inode, dentry);
14254 +               if (!err) {
14255 +                       unlock_new_inode(inode);
14256 +                       goto out; /* success */
14257 +               }
14258 +
14259 +               /*
14260 +                * iget_failed() calls iput(), but we need to call
14261 +                * ii_write_unlock() after iget_failed(). so dirty hack for
14262 +                * i_count.
14263 +                */
14264 +               atomic_inc(&inode->i_count);
14265 +               iget_failed(inode);
14266 +               ii_write_unlock(inode);
14267 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
14268 +               /* ignore this error */
14269 +               goto out_iput;
14270 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
14271 +               /*
14272 +                * horrible race condition between lookup, readdir and copyup
14273 +                * (or something).
14274 +                */
14275 +               if (mtx)
14276 +                       mutex_unlock(mtx);
14277 +               err = reval_inode(inode, dentry);
14278 +               if (unlikely(err < 0)) {
14279 +                       mtx = NULL;
14280 +                       goto out_iput;
14281 +               }
14282 +
14283 +               if (!err) {
14284 +                       mtx = NULL;
14285 +                       goto out; /* success */
14286 +               } else if (mtx)
14287 +                       mutex_lock(mtx);
14288 +       }
14289 +
14290 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
14291 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
14292 +                       " b%d, %s, %.*s, hi%lu, i%lu.\n",
14293 +                       bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
14294 +                       (unsigned long)h_ino, (unsigned long)ino);
14295 +       ino = 0;
14296 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
14297 +       if (!err) {
14298 +               iput(inode);
14299 +               if (mtx)
14300 +                       mutex_unlock(mtx);
14301 +               goto new_ino;
14302 +       }
14303 +
14304 +out_iput:
14305 +       iput(inode);
14306 +       inode = ERR_PTR(err);
14307 +out:
14308 +       if (mtx)
14309 +               mutex_unlock(mtx);
14310 +       return inode;
14311 +}
14312 +
14313 +/* ---------------------------------------------------------------------- */
14314 +
14315 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
14316 +              struct inode *inode)
14317 +{
14318 +       int err;
14319 +
14320 +       err = au_br_rdonly(au_sbr(sb, bindex));
14321 +
14322 +       /* pseudo-link after flushed may happen out of bounds */
14323 +       if (!err
14324 +           && inode
14325 +           && au_ibstart(inode) <= bindex
14326 +           && bindex <= au_ibend(inode)) {
14327 +               /*
14328 +                * permission check is unnecessary since vfsub routine
14329 +                * will be called later
14330 +                */
14331 +               struct inode *hi = au_h_iptr(inode, bindex);
14332 +               if (hi)
14333 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
14334 +       }
14335 +
14336 +       return err;
14337 +}
14338 +
14339 +int au_test_h_perm(struct inode *h_inode, int mask)
14340 +{
14341 +       if (!current_fsuid())
14342 +               return 0;
14343 +       return inode_permission(h_inode, mask);
14344 +}
14345 +
14346 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
14347 +{
14348 +       if (au_test_nfs(h_inode->i_sb)
14349 +           && (mask & MAY_WRITE)
14350 +           && S_ISDIR(h_inode->i_mode))
14351 +               mask |= MAY_READ; /* force permission check */
14352 +       return au_test_h_perm(h_inode, mask);
14353 +}
14354 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
14355 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
14356 +++ linux/fs/aufs/inode.h       2011-08-24 13:30:24.734646739 +0200
14357 @@ -0,0 +1,556 @@
14358 +/*
14359 + * Copyright (C) 2005-2011 Junjiro R. Okajima
14360 + *
14361 + * This program, aufs is free software; you can redistribute it and/or modify
14362 + * it under the terms of the GNU General Public License as published by
14363 + * the Free Software Foundation; either version 2 of the License, or
14364 + * (at your option) any later version.
14365 + *
14366 + * This program is distributed in the hope that it will be useful,
14367 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14368 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14369 + * GNU General Public License for more details.
14370 + *
14371 + * You should have received a copy of the GNU General Public License
14372 + * along with this program; if not, write to the Free Software
14373 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14374 + */
14375 +
14376 +/*
14377 + * inode operations
14378 + */
14379 +
14380 +#ifndef __AUFS_INODE_H__
14381 +#define __AUFS_INODE_H__
14382 +
14383 +#ifdef __KERNEL__
14384 +
14385 +#include <linux/fs.h>
14386 +#include <linux/fsnotify.h>
14387 +#include <linux/aufs_type.h>
14388 +#include "rwsem.h"
14389 +
14390 +struct vfsmount;
14391 +
14392 +struct au_hnotify {
14393 +#ifdef CONFIG_AUFS_HNOTIFY
14394 +#ifdef CONFIG_AUFS_HFSNOTIFY
14395 +       /* never use fsnotify_add_vfsmount_mark() */
14396 +       struct fsnotify_mark            hn_mark;
14397 +       int                             hn_mark_dead;
14398 +#endif
14399 +       struct inode                    *hn_aufs_inode; /* no get/put */
14400 +#endif
14401 +} ____cacheline_aligned_in_smp;
14402 +
14403 +struct au_hinode {
14404 +       struct inode            *hi_inode;
14405 +       aufs_bindex_t           hi_id;
14406 +#ifdef CONFIG_AUFS_HNOTIFY
14407 +       struct au_hnotify       *hi_notify;
14408 +#endif
14409 +
14410 +       /* reference to the copied-up whiteout with get/put */
14411 +       struct dentry           *hi_whdentry;
14412 +};
14413 +
14414 +struct au_vdir;
14415 +struct au_iinfo {
14416 +       atomic_t                ii_generation;
14417 +       struct super_block      *ii_hsb1;       /* no get/put */
14418 +
14419 +       struct au_rwsem         ii_rwsem;
14420 +       aufs_bindex_t           ii_bstart, ii_bend;
14421 +       __u32                   ii_higen;
14422 +       struct au_hinode        *ii_hinode;
14423 +       struct au_vdir          *ii_vdir;
14424 +};
14425 +
14426 +struct au_icntnr {
14427 +       struct au_iinfo iinfo;
14428 +       struct inode vfs_inode;
14429 +} ____cacheline_aligned_in_smp;
14430 +
14431 +/* au_pin flags */
14432 +#define AuPin_DI_LOCKED                1
14433 +#define AuPin_MNT_WRITE                (1 << 1)
14434 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
14435 +#define au_fset_pin(flags, name) \
14436 +       do { (flags) |= AuPin_##name; } while (0)
14437 +#define au_fclr_pin(flags, name) \
14438 +       do { (flags) &= ~AuPin_##name; } while (0)
14439 +
14440 +struct au_pin {
14441 +       /* input */
14442 +       struct dentry *dentry;
14443 +       unsigned int udba;
14444 +       unsigned char lsc_di, lsc_hi, flags;
14445 +       aufs_bindex_t bindex;
14446 +
14447 +       /* output */
14448 +       struct dentry *parent;
14449 +       struct au_hinode *hdir;
14450 +       struct vfsmount *h_mnt;
14451 +};
14452 +
14453 +/* ---------------------------------------------------------------------- */
14454 +
14455 +static inline struct au_iinfo *au_ii(struct inode *inode)
14456 +{
14457 +       struct au_iinfo *iinfo;
14458 +
14459 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
14460 +       if (iinfo->ii_hinode)
14461 +               return iinfo;
14462 +       return NULL; /* debugging bad_inode case */
14463 +}
14464 +
14465 +/* ---------------------------------------------------------------------- */
14466 +
14467 +/* inode.c */
14468 +struct inode *au_igrab(struct inode *inode);
14469 +int au_refresh_hinode_self(struct inode *inode);
14470 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
14471 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14472 +          unsigned int d_type, ino_t *ino);
14473 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
14474 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
14475 +              struct inode *inode);
14476 +int au_test_h_perm(struct inode *h_inode, int mask);
14477 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
14478 +
14479 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
14480 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
14481 +{
14482 +#ifdef CONFIG_AUFS_SHWH
14483 +       return au_ino(sb, bindex, h_ino, d_type, ino);
14484 +#else
14485 +       return 0;
14486 +#endif
14487 +}
14488 +
14489 +/* i_op.c */
14490 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
14491 +
14492 +/* au_wr_dir flags */
14493 +#define AuWrDir_ADD_ENTRY      1
14494 +#define AuWrDir_ISDIR          (1 << 1)
14495 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
14496 +#define au_fset_wrdir(flags, name) \
14497 +       do { (flags) |= AuWrDir_##name; } while (0)
14498 +#define au_fclr_wrdir(flags, name) \
14499 +       do { (flags) &= ~AuWrDir_##name; } while (0)
14500 +
14501 +struct au_wr_dir_args {
14502 +       aufs_bindex_t force_btgt;
14503 +       unsigned char flags;
14504 +};
14505 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
14506 +             struct au_wr_dir_args *args);
14507 +
14508 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
14509 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
14510 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
14511 +                unsigned int udba, unsigned char flags);
14512 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
14513 +          unsigned int udba, unsigned char flags) __must_check;
14514 +int au_do_pin(struct au_pin *pin) __must_check;
14515 +void au_unpin(struct au_pin *pin);
14516 +
14517 +/* i_op_add.c */
14518 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
14519 +              struct dentry *h_parent, int isdir);
14520 +int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);
14521 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
14522 +int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
14523 +               struct nameidata *nd);
14524 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
14525 +             struct dentry *dentry);
14526 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
14527 +
14528 +/* i_op_del.c */
14529 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
14530 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
14531 +              struct dentry *h_parent, int isdir);
14532 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
14533 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
14534 +
14535 +/* i_op_ren.c */
14536 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
14537 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
14538 +               struct inode *dir, struct dentry *dentry);
14539 +
14540 +/* iinfo.c */
14541 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
14542 +void au_hiput(struct au_hinode *hinode);
14543 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
14544 +                 struct dentry *h_wh);
14545 +unsigned int au_hi_flags(struct inode *inode, int isdir);
14546 +
14547 +/* hinode flags */
14548 +#define AuHi_XINO      1
14549 +#define AuHi_HNOTIFY   (1 << 1)
14550 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
14551 +#define au_fset_hi(flags, name) \
14552 +       do { (flags) |= AuHi_##name; } while (0)
14553 +#define au_fclr_hi(flags, name) \
14554 +       do { (flags) &= ~AuHi_##name; } while (0)
14555 +
14556 +#ifndef CONFIG_AUFS_HNOTIFY
14557 +#undef AuHi_HNOTIFY
14558 +#define AuHi_HNOTIFY   0
14559 +#endif
14560 +
14561 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
14562 +                  struct inode *h_inode, unsigned int flags);
14563 +
14564 +void au_update_iigen(struct inode *inode);
14565 +void au_update_ibrange(struct inode *inode, int do_put_zero);
14566 +
14567 +void au_icntnr_init_once(void *_c);
14568 +int au_iinfo_init(struct inode *inode);
14569 +void au_iinfo_fin(struct inode *inode);
14570 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
14571 +
14572 +#ifdef CONFIG_PROC_FS
14573 +/* plink.c */
14574 +int au_plink_maint(struct super_block *sb, int flags);
14575 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
14576 +int au_plink_maint_enter(struct super_block *sb);
14577 +#ifdef CONFIG_AUFS_DEBUG
14578 +void au_plink_list(struct super_block *sb);
14579 +#else
14580 +AuStubVoid(au_plink_list, struct super_block *sb)
14581 +#endif
14582 +int au_plink_test(struct inode *inode);
14583 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
14584 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
14585 +                    struct dentry *h_dentry);
14586 +void au_plink_put(struct super_block *sb, int verbose);
14587 +void au_plink_clean(struct super_block *sb, int verbose);
14588 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
14589 +#else
14590 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
14591 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
14592 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
14593 +AuStubVoid(au_plink_list, struct super_block *sb);
14594 +AuStubInt0(au_plink_test, struct inode *inode);
14595 +AuStub(struct dentry *, au_plink_lkup, return NULL,
14596 +       struct inode *inode, aufs_bindex_t bindex);
14597 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
14598 +          struct dentry *h_dentry);
14599 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
14600 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
14601 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
14602 +#endif /* CONFIG_PROC_FS */
14603 +
14604 +/* ---------------------------------------------------------------------- */
14605 +
14606 +/* lock subclass for iinfo */
14607 +enum {
14608 +       AuLsc_II_CHILD,         /* child first */
14609 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
14610 +       AuLsc_II_CHILD3,        /* copyup dirs */
14611 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
14612 +       AuLsc_II_PARENT2,
14613 +       AuLsc_II_PARENT3,       /* copyup dirs */
14614 +       AuLsc_II_NEW_CHILD
14615 +};
14616 +
14617 +/*
14618 + * ii_read_lock_child, ii_write_lock_child,
14619 + * ii_read_lock_child2, ii_write_lock_child2,
14620 + * ii_read_lock_child3, ii_write_lock_child3,
14621 + * ii_read_lock_parent, ii_write_lock_parent,
14622 + * ii_read_lock_parent2, ii_write_lock_parent2,
14623 + * ii_read_lock_parent3, ii_write_lock_parent3,
14624 + * ii_read_lock_new_child, ii_write_lock_new_child,
14625 + */
14626 +#define AuReadLockFunc(name, lsc) \
14627 +static inline void ii_read_lock_##name(struct inode *i) \
14628 +{ \
14629 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
14630 +}
14631 +
14632 +#define AuWriteLockFunc(name, lsc) \
14633 +static inline void ii_write_lock_##name(struct inode *i) \
14634 +{ \
14635 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
14636 +}
14637 +
14638 +#define AuRWLockFuncs(name, lsc) \
14639 +       AuReadLockFunc(name, lsc) \
14640 +       AuWriteLockFunc(name, lsc)
14641 +
14642 +AuRWLockFuncs(child, CHILD);
14643 +AuRWLockFuncs(child2, CHILD2);
14644 +AuRWLockFuncs(child3, CHILD3);
14645 +AuRWLockFuncs(parent, PARENT);
14646 +AuRWLockFuncs(parent2, PARENT2);
14647 +AuRWLockFuncs(parent3, PARENT3);
14648 +AuRWLockFuncs(new_child, NEW_CHILD);
14649 +
14650 +#undef AuReadLockFunc
14651 +#undef AuWriteLockFunc
14652 +#undef AuRWLockFuncs
14653 +
14654 +/*
14655 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
14656 + */
14657 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
14658 +
14659 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
14660 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
14661 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
14662 +
14663 +/* ---------------------------------------------------------------------- */
14664 +
14665 +static inline void au_icntnr_init(struct au_icntnr *c)
14666 +{
14667 +#ifdef CONFIG_AUFS_DEBUG
14668 +       c->vfs_inode.i_mode = 0;
14669 +#endif
14670 +}
14671 +
14672 +static inline unsigned int au_iigen(struct inode *inode)
14673 +{
14674 +       return atomic_read(&au_ii(inode)->ii_generation);
14675 +}
14676 +
14677 +/* tiny test for inode number */
14678 +/* tmpfs generation is too rough */
14679 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
14680 +{
14681 +       struct au_iinfo *iinfo;
14682 +
14683 +       iinfo = au_ii(inode);
14684 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
14685 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
14686 +                && iinfo->ii_higen == h_inode->i_generation);
14687 +}
14688 +
14689 +static inline void au_iigen_dec(struct inode *inode)
14690 +{
14691 +       atomic_dec(&au_ii(inode)->ii_generation);
14692 +}
14693 +
14694 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
14695 +{
14696 +       int err;
14697 +
14698 +       err = 0;
14699 +       if (unlikely(inode && au_iigen(inode) != sigen))
14700 +               err = -EIO;
14701 +
14702 +       return err;
14703 +}
14704 +
14705 +/* ---------------------------------------------------------------------- */
14706 +
14707 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
14708 +                                       aufs_bindex_t bindex)
14709 +{
14710 +       IiMustAnyLock(inode);
14711 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
14712 +}
14713 +
14714 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
14715 +{
14716 +       IiMustAnyLock(inode);
14717 +       return au_ii(inode)->ii_bstart;
14718 +}
14719 +
14720 +static inline aufs_bindex_t au_ibend(struct inode *inode)
14721 +{
14722 +       IiMustAnyLock(inode);
14723 +       return au_ii(inode)->ii_bend;
14724 +}
14725 +
14726 +static inline struct au_vdir *au_ivdir(struct inode *inode)
14727 +{
14728 +       IiMustAnyLock(inode);
14729 +       return au_ii(inode)->ii_vdir;
14730 +}
14731 +
14732 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
14733 +{
14734 +       IiMustAnyLock(inode);
14735 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
14736 +}
14737 +
14738 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
14739 +{
14740 +       IiMustWriteLock(inode);
14741 +       au_ii(inode)->ii_bstart = bindex;
14742 +}
14743 +
14744 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
14745 +{
14746 +       IiMustWriteLock(inode);
14747 +       au_ii(inode)->ii_bend = bindex;
14748 +}
14749 +
14750 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
14751 +{
14752 +       IiMustWriteLock(inode);
14753 +       au_ii(inode)->ii_vdir = vdir;
14754 +}
14755 +
14756 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
14757 +{
14758 +       IiMustAnyLock(inode);
14759 +       return au_ii(inode)->ii_hinode + bindex;
14760 +}
14761 +
14762 +/* ---------------------------------------------------------------------- */
14763 +
14764 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
14765 +{
14766 +       if (pin)
14767 +               return pin->parent;
14768 +       return NULL;
14769 +}
14770 +
14771 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
14772 +{
14773 +       if (pin && pin->hdir)
14774 +               return pin->hdir->hi_inode;
14775 +       return NULL;
14776 +}
14777 +
14778 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
14779 +{
14780 +       if (pin)
14781 +               return pin->hdir;
14782 +       return NULL;
14783 +}
14784 +
14785 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
14786 +{
14787 +       if (pin)
14788 +               pin->dentry = dentry;
14789 +}
14790 +
14791 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
14792 +                                          unsigned char lflag)
14793 +{
14794 +       if (pin) {
14795 +               if (lflag)
14796 +                       au_fset_pin(pin->flags, DI_LOCKED);
14797 +               else
14798 +                       au_fclr_pin(pin->flags, DI_LOCKED);
14799 +       }
14800 +}
14801 +
14802 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
14803 +{
14804 +       if (pin) {
14805 +               dput(pin->parent);
14806 +               pin->parent = dget(parent);
14807 +       }
14808 +}
14809 +
14810 +/* ---------------------------------------------------------------------- */
14811 +
14812 +struct au_branch;
14813 +#ifdef CONFIG_AUFS_HNOTIFY
14814 +struct au_hnotify_op {
14815 +       void (*ctl)(struct au_hinode *hinode, int do_set);
14816 +       int (*alloc)(struct au_hinode *hinode);
14817 +       void (*free)(struct au_hinode *hinode);
14818 +
14819 +       void (*fin)(void);
14820 +       int (*init)(void);
14821 +
14822 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
14823 +       void (*fin_br)(struct au_branch *br);
14824 +       int (*init_br)(struct au_branch *br, int perm);
14825 +};
14826 +
14827 +/* hnotify.c */
14828 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
14829 +void au_hn_free(struct au_hinode *hinode);
14830 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
14831 +void au_hn_reset(struct inode *inode, unsigned int flags);
14832 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
14833 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
14834 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
14835 +int au_hnotify_init_br(struct au_branch *br, int perm);
14836 +void au_hnotify_fin_br(struct au_branch *br);
14837 +int __init au_hnotify_init(void);
14838 +void au_hnotify_fin(void);
14839 +
14840 +/* hfsnotify.c */
14841 +extern const struct au_hnotify_op au_hnotify_op;
14842 +
14843 +static inline
14844 +void au_hn_init(struct au_hinode *hinode)
14845 +{
14846 +       hinode->hi_notify = NULL;
14847 +}
14848 +
14849 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
14850 +{
14851 +       return hinode->hi_notify;
14852 +}
14853 +
14854 +#else
14855 +static inline
14856 +int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
14857 +               struct inode *inode __maybe_unused)
14858 +{
14859 +       return -EOPNOTSUPP;
14860 +}
14861 +
14862 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
14863 +{
14864 +       return NULL;
14865 +}
14866 +
14867 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
14868 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
14869 +          int do_set __maybe_unused)
14870 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
14871 +          unsigned int flags __maybe_unused)
14872 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
14873 +          struct au_branch *br __maybe_unused,
14874 +          int perm __maybe_unused)
14875 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
14876 +          int perm __maybe_unused)
14877 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
14878 +AuStubInt0(__init au_hnotify_init, void)
14879 +AuStubVoid(au_hnotify_fin, void)
14880 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
14881 +#endif /* CONFIG_AUFS_HNOTIFY */
14882 +
14883 +static inline void au_hn_suspend(struct au_hinode *hdir)
14884 +{
14885 +       au_hn_ctl(hdir, /*do_set*/0);
14886 +}
14887 +
14888 +static inline void au_hn_resume(struct au_hinode *hdir)
14889 +{
14890 +       au_hn_ctl(hdir, /*do_set*/1);
14891 +}
14892 +
14893 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
14894 +{
14895 +       mutex_lock(&hdir->hi_inode->i_mutex);
14896 +       au_hn_suspend(hdir);
14897 +}
14898 +
14899 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
14900 +                                         unsigned int sc __maybe_unused)
14901 +{
14902 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
14903 +       au_hn_suspend(hdir);
14904 +}
14905 +
14906 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
14907 +{
14908 +       au_hn_resume(hdir);
14909 +       mutex_unlock(&hdir->hi_inode->i_mutex);
14910 +}
14911 +
14912 +#endif /* __KERNEL__ */
14913 +#endif /* __AUFS_INODE_H__ */
14914 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
14915 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
14916 +++ linux/fs/aufs/ioctl.c       2011-10-24 20:51:51.580466925 +0200
14917 @@ -0,0 +1,197 @@
14918 +/*
14919 + * Copyright (C) 2005-2011 Junjiro R. Okajima
14920 + *
14921 + * This program, aufs is free software; you can redistribute it and/or modify
14922 + * it under the terms of the GNU General Public License as published by
14923 + * the Free Software Foundation; either version 2 of the License, or
14924 + * (at your option) any later version.
14925 + *
14926 + * This program is distributed in the hope that it will be useful,
14927 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14928 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14929 + * GNU General Public License for more details.
14930 + *
14931 + * You should have received a copy of the GNU General Public License
14932 + * along with this program; if not, write to the Free Software
14933 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14934 + */
14935 +
14936 +/*
14937 + * ioctl
14938 + * plink-management and readdir in userspace.
14939 + * assist the pathconf(3) wrapper library.
14940 + */
14941 +
14942 +#include <linux/file.h>
14943 +#include "aufs.h"
14944 +
14945 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
14946 +{
14947 +       int err, fd;
14948 +       aufs_bindex_t wbi, bindex, bend;
14949 +       struct file *h_file;
14950 +       struct super_block *sb;
14951 +       struct dentry *root;
14952 +       struct au_branch *br;
14953 +       struct aufs_wbr_fd wbrfd = {
14954 +               .oflags = au_dir_roflags,
14955 +               .brid   = -1
14956 +       };
14957 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
14958 +               | O_NOATIME | O_CLOEXEC;
14959 +
14960 +       AuDebugOn(wbrfd.oflags & ~valid);
14961 +
14962 +       if (arg) {
14963 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
14964 +               if (unlikely(err)) {
14965 +                       err = -EFAULT;
14966 +                       goto out;
14967 +               }
14968 +
14969 +               err = -EINVAL;
14970 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
14971 +               wbrfd.oflags |= au_dir_roflags;
14972 +               AuDbg("0%o\n", wbrfd.oflags);
14973 +               if (unlikely(wbrfd.oflags & ~valid))
14974 +                       goto out;
14975 +       }
14976 +
14977 +       fd = get_unused_fd();
14978 +       err = fd;
14979 +       if (unlikely(fd < 0))
14980 +               goto out;
14981 +
14982 +       h_file = ERR_PTR(-EINVAL);
14983 +       wbi = 0;
14984 +       br = NULL;
14985 +       sb = path->dentry->d_sb;
14986 +       root = sb->s_root;
14987 +       aufs_read_lock(root, AuLock_IR);
14988 +       bend = au_sbend(sb);
14989 +       if (wbrfd.brid >= 0) {
14990 +               wbi = au_br_index(sb, wbrfd.brid);
14991 +               if (unlikely(wbi < 0 || wbi > bend))
14992 +                       goto out_unlock;
14993 +       }
14994 +
14995 +       h_file = ERR_PTR(-ENOENT);
14996 +       br = au_sbr(sb, wbi);
14997 +       if (!au_br_writable(br->br_perm)) {
14998 +               if (arg)
14999 +                       goto out_unlock;
15000 +
15001 +               bindex = wbi + 1;
15002 +               wbi = -1;
15003 +               for (; bindex <= bend; bindex++) {
15004 +                       br = au_sbr(sb, bindex);
15005 +                       if (au_br_writable(br->br_perm)) {
15006 +                               wbi = bindex;
15007 +                               br = au_sbr(sb, wbi);
15008 +                               break;
15009 +                       }
15010 +               }
15011 +       }
15012 +       AuDbg("wbi %d\n", wbi);
15013 +       if (wbi >= 0)
15014 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL);
15015 +
15016 +out_unlock:
15017 +       aufs_read_unlock(root, AuLock_IR);
15018 +       err = PTR_ERR(h_file);
15019 +       if (IS_ERR(h_file))
15020 +               goto out_fd;
15021 +
15022 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
15023 +       fd_install(fd, h_file);
15024 +       err = fd;
15025 +       goto out; /* success */
15026 +
15027 +out_fd:
15028 +       put_unused_fd(fd);
15029 +out:
15030 +       AuTraceErr(err);
15031 +       return err;
15032 +}
15033 +
15034 +/* ---------------------------------------------------------------------- */
15035 +
15036 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
15037 +{
15038 +       long err;
15039 +
15040 +       switch (cmd) {
15041 +       case AUFS_CTL_RDU:
15042 +       case AUFS_CTL_RDU_INO:
15043 +               err = au_rdu_ioctl(file, cmd, arg);
15044 +               break;
15045 +
15046 +       case AUFS_CTL_WBR_FD:
15047 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15048 +               break;
15049 +
15050 +       case AUFS_CTL_IBUSY:
15051 +               err = au_ibusy_ioctl(file, arg);
15052 +               break;
15053 +
15054 +       default:
15055 +               /* do not call the lower */
15056 +               AuDbg("0x%x\n", cmd);
15057 +               err = -ENOTTY;
15058 +       }
15059 +
15060 +       AuTraceErr(err);
15061 +       return err;
15062 +}
15063 +
15064 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
15065 +{
15066 +       long err;
15067 +
15068 +       switch (cmd) {
15069 +       case AUFS_CTL_WBR_FD:
15070 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15071 +               break;
15072 +
15073 +       default:
15074 +               /* do not call the lower */
15075 +               AuDbg("0x%x\n", cmd);
15076 +               err = -ENOTTY;
15077 +       }
15078 +
15079 +       AuTraceErr(err);
15080 +       return err;
15081 +}
15082 +
15083 +#ifdef CONFIG_COMPAT
15084 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15085 +                          unsigned long arg)
15086 +{
15087 +       long err;
15088 +
15089 +       switch (cmd) {
15090 +       case AUFS_CTL_RDU:
15091 +       case AUFS_CTL_RDU_INO:
15092 +               err = au_rdu_compat_ioctl(file, cmd, arg);
15093 +               break;
15094 +
15095 +       case AUFS_CTL_IBUSY:
15096 +               err = au_ibusy_compat_ioctl(file, arg);
15097 +               break;
15098 +
15099 +       default:
15100 +               err = aufs_ioctl_dir(file, cmd, arg);
15101 +       }
15102 +
15103 +       AuTraceErr(err);
15104 +       return err;
15105 +}
15106 +
15107 +#if 0 /* unused yet */
15108 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15109 +                             unsigned long arg)
15110 +{
15111 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
15112 +}
15113 +#endif
15114 +#endif
15115 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
15116 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
15117 +++ linux/fs/aufs/i_op_add.c    2011-10-24 20:51:51.580466925 +0200
15118 @@ -0,0 +1,711 @@
15119 +/*
15120 + * Copyright (C) 2005-2011 Junjiro R. Okajima
15121 + *
15122 + * This program, aufs is free software; you can redistribute it and/or modify
15123 + * it under the terms of the GNU General Public License as published by
15124 + * the Free Software Foundation; either version 2 of the License, or
15125 + * (at your option) any later version.
15126 + *
15127 + * This program is distributed in the hope that it will be useful,
15128 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15129 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130 + * GNU General Public License for more details.
15131 + *
15132 + * You should have received a copy of the GNU General Public License
15133 + * along with this program; if not, write to the Free Software
15134 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15135 + */
15136 +
15137 +/*
15138 + * inode operations (add entry)
15139 + */
15140 +
15141 +#include "aufs.h"
15142 +
15143 +/*
15144 + * final procedure of adding a new entry, except link(2).
15145 + * remove whiteout, instantiate, copyup the parent dir's times and size
15146 + * and update version.
15147 + * if it failed, re-create the removed whiteout.
15148 + */
15149 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
15150 +                 struct dentry *wh_dentry, struct dentry *dentry)
15151 +{
15152 +       int err, rerr;
15153 +       aufs_bindex_t bwh;
15154 +       struct path h_path;
15155 +       struct inode *inode, *h_dir;
15156 +       struct dentry *wh;
15157 +
15158 +       bwh = -1;
15159 +       if (wh_dentry) {
15160 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
15161 +               IMustLock(h_dir);
15162 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
15163 +               bwh = au_dbwh(dentry);
15164 +               h_path.dentry = wh_dentry;
15165 +               h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
15166 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
15167 +                                         dentry);
15168 +               if (unlikely(err))
15169 +                       goto out;
15170 +       }
15171 +
15172 +       inode = au_new_inode(dentry, /*must_new*/1);
15173 +       if (!IS_ERR(inode)) {
15174 +               d_instantiate(dentry, inode);
15175 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
15176 +               IMustLock(dir);
15177 +               if (au_ibstart(dir) == au_dbstart(dentry))
15178 +                       au_cpup_attr_timesizes(dir);
15179 +               dir->i_version++;
15180 +               return 0; /* success */
15181 +       }
15182 +
15183 +       err = PTR_ERR(inode);
15184 +       if (!wh_dentry)
15185 +               goto out;
15186 +
15187 +       /* revert */
15188 +       /* dir inode is locked */
15189 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
15190 +       rerr = PTR_ERR(wh);
15191 +       if (IS_ERR(wh)) {
15192 +               AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
15193 +                       AuDLNPair(dentry), err, rerr);
15194 +               err = -EIO;
15195 +       } else
15196 +               dput(wh);
15197 +
15198 +out:
15199 +       return err;
15200 +}
15201 +
15202 +static int au_d_may_add(struct dentry *dentry)
15203 +{
15204 +       int err;
15205 +
15206 +       err = 0;
15207 +       if (unlikely(d_unhashed(dentry)))
15208 +               err = -ENOENT;
15209 +       if (unlikely(dentry->d_inode))
15210 +               err = -EEXIST;
15211 +       return err;
15212 +}
15213 +
15214 +/*
15215 + * simple tests for the adding inode operations.
15216 + * following the checks in vfs, plus the parent-child relationship.
15217 + */
15218 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15219 +              struct dentry *h_parent, int isdir)
15220 +{
15221 +       int err;
15222 +       umode_t h_mode;
15223 +       struct dentry *h_dentry;
15224 +       struct inode *h_inode;
15225 +
15226 +       err = -ENAMETOOLONG;
15227 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15228 +               goto out;
15229 +
15230 +       h_dentry = au_h_dptr(dentry, bindex);
15231 +       h_inode = h_dentry->d_inode;
15232 +       if (!dentry->d_inode) {
15233 +               err = -EEXIST;
15234 +               if (unlikely(h_inode))
15235 +                       goto out;
15236 +       } else {
15237 +               /* rename(2) case */
15238 +               err = -EIO;
15239 +               if (unlikely(!h_inode || !h_inode->i_nlink))
15240 +                       goto out;
15241 +
15242 +               h_mode = h_inode->i_mode;
15243 +               if (!isdir) {
15244 +                       err = -EISDIR;
15245 +                       if (unlikely(S_ISDIR(h_mode)))
15246 +                               goto out;
15247 +               } else if (unlikely(!S_ISDIR(h_mode))) {
15248 +                       err = -ENOTDIR;
15249 +                       goto out;
15250 +               }
15251 +       }
15252 +
15253 +       err = 0;
15254 +       /* expected parent dir is locked */
15255 +       if (unlikely(h_parent != h_dentry->d_parent))
15256 +               err = -EIO;
15257 +
15258 +out:
15259 +       AuTraceErr(err);
15260 +       return err;
15261 +}
15262 +
15263 +/*
15264 + * initial procedure of adding a new entry.
15265 + * prepare writable branch and the parent dir, lock it,
15266 + * and lookup whiteout for the new entry.
15267 + */
15268 +static struct dentry*
15269 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
15270 +                 struct dentry *src_dentry, struct au_pin *pin,
15271 +                 struct au_wr_dir_args *wr_dir_args)
15272 +{
15273 +       struct dentry *wh_dentry, *h_parent;
15274 +       struct super_block *sb;
15275 +       struct au_branch *br;
15276 +       int err;
15277 +       unsigned int udba;
15278 +       aufs_bindex_t bcpup;
15279 +
15280 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15281 +
15282 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
15283 +       bcpup = err;
15284 +       wh_dentry = ERR_PTR(err);
15285 +       if (unlikely(err < 0))
15286 +               goto out;
15287 +
15288 +       sb = dentry->d_sb;
15289 +       udba = au_opt_udba(sb);
15290 +       err = au_pin(pin, dentry, bcpup, udba,
15291 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15292 +       wh_dentry = ERR_PTR(err);
15293 +       if (unlikely(err))
15294 +               goto out;
15295 +
15296 +       h_parent = au_pinned_h_parent(pin);
15297 +       if (udba != AuOpt_UDBA_NONE
15298 +           && au_dbstart(dentry) == bcpup)
15299 +               err = au_may_add(dentry, bcpup, h_parent,
15300 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
15301 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15302 +               err = -ENAMETOOLONG;
15303 +       wh_dentry = ERR_PTR(err);
15304 +       if (unlikely(err))
15305 +               goto out_unpin;
15306 +
15307 +       br = au_sbr(sb, bcpup);
15308 +       if (dt) {
15309 +               struct path tmp = {
15310 +                       .dentry = h_parent,
15311 +                       .mnt    = br->br_mnt
15312 +               };
15313 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
15314 +       }
15315 +
15316 +       wh_dentry = NULL;
15317 +       if (bcpup != au_dbwh(dentry))
15318 +               goto out; /* success */
15319 +
15320 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
15321 +
15322 +out_unpin:
15323 +       if (IS_ERR(wh_dentry))
15324 +               au_unpin(pin);
15325 +out:
15326 +       return wh_dentry;
15327 +}
15328 +
15329 +/* ---------------------------------------------------------------------- */
15330 +
15331 +enum { Mknod, Symlink, Creat };
15332 +struct simple_arg {
15333 +       int type;
15334 +       union {
15335 +               struct {
15336 +                       int mode;
15337 +                       struct nameidata *nd;
15338 +               } c;
15339 +               struct {
15340 +                       const char *symname;
15341 +               } s;
15342 +               struct {
15343 +                       int mode;
15344 +                       dev_t dev;
15345 +               } m;
15346 +       } u;
15347 +};
15348 +
15349 +static int add_simple(struct inode *dir, struct dentry *dentry,
15350 +                     struct simple_arg *arg)
15351 +{
15352 +       int err;
15353 +       aufs_bindex_t bstart;
15354 +       unsigned char created;
15355 +       struct au_dtime dt;
15356 +       struct au_pin pin;
15357 +       struct path h_path;
15358 +       struct dentry *wh_dentry, *parent;
15359 +       struct inode *h_dir;
15360 +       struct au_wr_dir_args wr_dir_args = {
15361 +               .force_btgt     = -1,
15362 +               .flags          = AuWrDir_ADD_ENTRY
15363 +       };
15364 +
15365 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15366 +       IMustLock(dir);
15367 +
15368 +       parent = dentry->d_parent; /* dir inode is locked */
15369 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
15370 +       if (unlikely(err))
15371 +               goto out;
15372 +       err = au_d_may_add(dentry);
15373 +       if (unlikely(err))
15374 +               goto out_unlock;
15375 +       di_write_lock_parent(parent);
15376 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL, &pin,
15377 +                                     &wr_dir_args);
15378 +       err = PTR_ERR(wh_dentry);
15379 +       if (IS_ERR(wh_dentry))
15380 +               goto out_parent;
15381 +
15382 +       bstart = au_dbstart(dentry);
15383 +       h_path.dentry = au_h_dptr(dentry, bstart);
15384 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
15385 +       h_dir = au_pinned_h_dir(&pin);
15386 +       switch (arg->type) {
15387 +       case Creat:
15388 +               err = vfsub_create(h_dir, &h_path, arg->u.c.mode);
15389 +               break;
15390 +       case Symlink:
15391 +               err = vfsub_symlink(h_dir, &h_path, arg->u.s.symname);
15392 +               break;
15393 +       case Mknod:
15394 +               err = vfsub_mknod(h_dir, &h_path, arg->u.m.mode, arg->u.m.dev);
15395 +               break;
15396 +       default:
15397 +               BUG();
15398 +       }
15399 +       created = !err;
15400 +       if (!err)
15401 +               err = epilog(dir, bstart, wh_dentry, dentry);
15402 +
15403 +       /* revert */
15404 +       if (unlikely(created && err && h_path.dentry->d_inode)) {
15405 +               int rerr;
15406 +               rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
15407 +               if (rerr) {
15408 +                       AuIOErr("%.*s revert failure(%d, %d)\n",
15409 +                               AuDLNPair(dentry), err, rerr);
15410 +                       err = -EIO;
15411 +               }
15412 +               au_dtime_revert(&dt);
15413 +       }
15414 +
15415 +       au_unpin(&pin);
15416 +       dput(wh_dentry);
15417 +
15418 +out_parent:
15419 +       di_write_unlock(parent);
15420 +out_unlock:
15421 +       if (unlikely(err)) {
15422 +               au_update_dbstart(dentry);
15423 +               d_drop(dentry);
15424 +       }
15425 +       aufs_read_unlock(dentry, AuLock_DW);
15426 +out:
15427 +       return err;
15428 +}
15429 +
15430 +int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
15431 +{
15432 +       struct simple_arg arg = {
15433 +               .type = Mknod,
15434 +               .u.m = {
15435 +                       .mode   = mode,
15436 +                       .dev    = dev
15437 +               }
15438 +       };
15439 +       return add_simple(dir, dentry, &arg);
15440 +}
15441 +
15442 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
15443 +{
15444 +       struct simple_arg arg = {
15445 +               .type = Symlink,
15446 +               .u.s.symname = symname
15447 +       };
15448 +       return add_simple(dir, dentry, &arg);
15449 +}
15450 +
15451 +int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
15452 +               struct nameidata *nd)
15453 +{
15454 +       struct simple_arg arg = {
15455 +               .type = Creat,
15456 +               .u.c = {
15457 +                       .mode   = mode,
15458 +                       .nd     = nd
15459 +               }
15460 +       };
15461 +       return add_simple(dir, dentry, &arg);
15462 +}
15463 +
15464 +/* ---------------------------------------------------------------------- */
15465 +
15466 +struct au_link_args {
15467 +       aufs_bindex_t bdst, bsrc;
15468 +       struct au_pin pin;
15469 +       struct path h_path;
15470 +       struct dentry *src_parent, *parent;
15471 +};
15472 +
15473 +static int au_cpup_before_link(struct dentry *src_dentry,
15474 +                              struct au_link_args *a)
15475 +{
15476 +       int err;
15477 +       struct dentry *h_src_dentry;
15478 +       struct mutex *h_mtx;
15479 +       struct file *h_file;
15480 +
15481 +       di_read_lock_parent(a->src_parent, AuLock_IR);
15482 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
15483 +       if (unlikely(err))
15484 +               goto out;
15485 +
15486 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
15487 +       h_mtx = &h_src_dentry->d_inode->i_mutex;
15488 +       err = au_pin(&a->pin, src_dentry, a->bdst,
15489 +                    au_opt_udba(src_dentry->d_sb),
15490 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15491 +       if (unlikely(err))
15492 +               goto out;
15493 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
15494 +       h_file = au_h_open_pre(src_dentry, a->bsrc);
15495 +       if (IS_ERR(h_file)) {
15496 +               err = PTR_ERR(h_file);
15497 +               h_file = NULL;
15498 +       } else
15499 +               err = au_sio_cpup_simple(src_dentry, a->bdst, -1,
15500 +                                        AuCpup_DTIME /* | AuCpup_KEEPLINO */);
15501 +       mutex_unlock(h_mtx);
15502 +       au_h_open_post(src_dentry, a->bsrc, h_file);
15503 +       au_unpin(&a->pin);
15504 +
15505 +out:
15506 +       di_read_unlock(a->src_parent, AuLock_IR);
15507 +       return err;
15508 +}
15509 +
15510 +static int au_cpup_or_link(struct dentry *src_dentry, struct au_link_args *a)
15511 +{
15512 +       int err;
15513 +       unsigned char plink;
15514 +       struct inode *h_inode, *inode;
15515 +       struct dentry *h_src_dentry;
15516 +       struct super_block *sb;
15517 +       struct file *h_file;
15518 +
15519 +       plink = 0;
15520 +       h_inode = NULL;
15521 +       sb = src_dentry->d_sb;
15522 +       inode = src_dentry->d_inode;
15523 +       if (au_ibstart(inode) <= a->bdst)
15524 +               h_inode = au_h_iptr(inode, a->bdst);
15525 +       if (!h_inode || !h_inode->i_nlink) {
15526 +               /* copyup src_dentry as the name of dentry. */
15527 +               au_set_dbstart(src_dentry, a->bdst);
15528 +               au_set_h_dptr(src_dentry, a->bdst, dget(a->h_path.dentry));
15529 +               h_inode = au_h_dptr(src_dentry, a->bsrc)->d_inode;
15530 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
15531 +               h_file = au_h_open_pre(src_dentry, a->bsrc);
15532 +               if (IS_ERR(h_file)) {
15533 +                       err = PTR_ERR(h_file);
15534 +                       h_file = NULL;
15535 +               } else
15536 +                       err = au_sio_cpup_single(src_dentry, a->bdst, a->bsrc,
15537 +                                                -1, AuCpup_KEEPLINO,
15538 +                                                a->parent);
15539 +               mutex_unlock(&h_inode->i_mutex);
15540 +               au_h_open_post(src_dentry, a->bsrc, h_file);
15541 +               au_set_h_dptr(src_dentry, a->bdst, NULL);
15542 +               au_set_dbstart(src_dentry, a->bsrc);
15543 +       } else {
15544 +               /* the inode of src_dentry already exists on a.bdst branch */
15545 +               h_src_dentry = d_find_alias(h_inode);
15546 +               if (!h_src_dentry && au_plink_test(inode)) {
15547 +                       plink = 1;
15548 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
15549 +                       err = PTR_ERR(h_src_dentry);
15550 +                       if (IS_ERR(h_src_dentry))
15551 +                               goto out;
15552 +
15553 +                       if (unlikely(!h_src_dentry->d_inode)) {
15554 +                               dput(h_src_dentry);
15555 +                               h_src_dentry = NULL;
15556 +                       }
15557 +
15558 +               }
15559 +               if (h_src_dentry) {
15560 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
15561 +                                        &a->h_path);
15562 +                       dput(h_src_dentry);
15563 +               } else {
15564 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
15565 +                               h_inode->i_ino, a->bdst);
15566 +                       err = -EIO;
15567 +               }
15568 +       }
15569 +
15570 +       if (!err && !plink)
15571 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
15572 +
15573 +out:
15574 +       AuTraceErr(err);
15575 +       return err;
15576 +}
15577 +
15578 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
15579 +             struct dentry *dentry)
15580 +{
15581 +       int err, rerr;
15582 +       struct au_dtime dt;
15583 +       struct au_link_args *a;
15584 +       struct dentry *wh_dentry, *h_src_dentry;
15585 +       struct inode *inode;
15586 +       struct super_block *sb;
15587 +       struct au_wr_dir_args wr_dir_args = {
15588 +               /* .force_btgt  = -1, */
15589 +               .flags          = AuWrDir_ADD_ENTRY
15590 +       };
15591 +
15592 +       IMustLock(dir);
15593 +       inode = src_dentry->d_inode;
15594 +       IMustLock(inode);
15595 +
15596 +       err = -ENOMEM;
15597 +       a = kzalloc(sizeof(*a), GFP_NOFS);
15598 +       if (unlikely(!a))
15599 +               goto out;
15600 +
15601 +       a->parent = dentry->d_parent; /* dir inode is locked */
15602 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
15603 +                                       AuLock_NOPLM | AuLock_GEN);
15604 +       if (unlikely(err))
15605 +               goto out_kfree;
15606 +       err = au_d_hashed_positive(src_dentry);
15607 +       if (unlikely(err))
15608 +               goto out_unlock;
15609 +       err = au_d_may_add(dentry);
15610 +       if (unlikely(err))
15611 +               goto out_unlock;
15612 +
15613 +       a->src_parent = dget_parent(src_dentry);
15614 +       wr_dir_args.force_btgt = au_ibstart(inode);
15615 +
15616 +       di_write_lock_parent(a->parent);
15617 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
15618 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
15619 +                                     &wr_dir_args);
15620 +       err = PTR_ERR(wh_dentry);
15621 +       if (IS_ERR(wh_dentry))
15622 +               goto out_parent;
15623 +
15624 +       err = 0;
15625 +       sb = dentry->d_sb;
15626 +       a->bdst = au_dbstart(dentry);
15627 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
15628 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
15629 +       a->bsrc = au_ibstart(inode);
15630 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
15631 +       if (!h_src_dentry) {
15632 +               a->bsrc = au_dbstart(src_dentry);
15633 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
15634 +               AuDebugOn(!h_src_dentry);
15635 +       } else if (IS_ERR(h_src_dentry))
15636 +               goto out_parent;
15637 +
15638 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
15639 +               if (a->bdst < a->bsrc
15640 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
15641 +                       err = au_cpup_or_link(src_dentry, a);
15642 +               else
15643 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
15644 +                                        &a->h_path);
15645 +               dput(h_src_dentry);
15646 +       } else {
15647 +               /*
15648 +                * copyup src_dentry to the branch we process,
15649 +                * and then link(2) to it.
15650 +                */
15651 +               dput(h_src_dentry);
15652 +               if (a->bdst < a->bsrc
15653 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
15654 +                       au_unpin(&a->pin);
15655 +                       di_write_unlock(a->parent);
15656 +                       err = au_cpup_before_link(src_dentry, a);
15657 +                       di_write_lock_parent(a->parent);
15658 +                       if (!err)
15659 +                               err = au_pin(&a->pin, dentry, a->bdst,
15660 +                                            au_opt_udba(sb),
15661 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15662 +                       if (unlikely(err))
15663 +                               goto out_wh;
15664 +               }
15665 +               if (!err) {
15666 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
15667 +                       err = -ENOENT;
15668 +                       if (h_src_dentry && h_src_dentry->d_inode)
15669 +                               err = vfsub_link(h_src_dentry,
15670 +                                                au_pinned_h_dir(&a->pin),
15671 +                                                &a->h_path);
15672 +               }
15673 +       }
15674 +       if (unlikely(err))
15675 +               goto out_unpin;
15676 +
15677 +       if (wh_dentry) {
15678 +               a->h_path.dentry = wh_dentry;
15679 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
15680 +                                         dentry);
15681 +               if (unlikely(err))
15682 +                       goto out_revert;
15683 +       }
15684 +
15685 +       dir->i_version++;
15686 +       if (au_ibstart(dir) == au_dbstart(dentry))
15687 +               au_cpup_attr_timesizes(dir);
15688 +       inc_nlink(inode);
15689 +       inode->i_ctime = dir->i_ctime;
15690 +       d_instantiate(dentry, au_igrab(inode));
15691 +       if (d_unhashed(a->h_path.dentry))
15692 +               /* some filesystem calls d_drop() */
15693 +               d_drop(dentry);
15694 +       goto out_unpin; /* success */
15695 +
15696 +out_revert:
15697 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
15698 +       if (unlikely(rerr)) {
15699 +               AuIOErr("%.*s reverting failed(%d, %d)\n",
15700 +                       AuDLNPair(dentry), err, rerr);
15701 +               err = -EIO;
15702 +       }
15703 +       au_dtime_revert(&dt);
15704 +out_unpin:
15705 +       au_unpin(&a->pin);
15706 +out_wh:
15707 +       dput(wh_dentry);
15708 +out_parent:
15709 +       di_write_unlock(a->parent);
15710 +       dput(a->src_parent);
15711 +out_unlock:
15712 +       if (unlikely(err)) {
15713 +               au_update_dbstart(dentry);
15714 +               d_drop(dentry);
15715 +       }
15716 +       aufs_read_and_write_unlock2(dentry, src_dentry);
15717 +out_kfree:
15718 +       kfree(a);
15719 +out:
15720 +       return err;
15721 +}
15722 +
15723 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
15724 +{
15725 +       int err, rerr;
15726 +       aufs_bindex_t bindex;
15727 +       unsigned char diropq;
15728 +       struct path h_path;
15729 +       struct dentry *wh_dentry, *parent, *opq_dentry;
15730 +       struct mutex *h_mtx;
15731 +       struct super_block *sb;
15732 +       struct {
15733 +               struct au_pin pin;
15734 +               struct au_dtime dt;
15735 +       } *a; /* reduce the stack usage */
15736 +       struct au_wr_dir_args wr_dir_args = {
15737 +               .force_btgt     = -1,
15738 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
15739 +       };
15740 +
15741 +       IMustLock(dir);
15742 +
15743 +       err = -ENOMEM;
15744 +       a = kmalloc(sizeof(*a), GFP_NOFS);
15745 +       if (unlikely(!a))
15746 +               goto out;
15747 +
15748 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
15749 +       if (unlikely(err))
15750 +               goto out_free;
15751 +       err = au_d_may_add(dentry);
15752 +       if (unlikely(err))
15753 +               goto out_unlock;
15754 +
15755 +       parent = dentry->d_parent; /* dir inode is locked */
15756 +       di_write_lock_parent(parent);
15757 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
15758 +                                     &a->pin, &wr_dir_args);
15759 +       err = PTR_ERR(wh_dentry);
15760 +       if (IS_ERR(wh_dentry))
15761 +               goto out_parent;
15762 +
15763 +       sb = dentry->d_sb;
15764 +       bindex = au_dbstart(dentry);
15765 +       h_path.dentry = au_h_dptr(dentry, bindex);
15766 +       h_path.mnt = au_sbr_mnt(sb, bindex);
15767 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
15768 +       if (unlikely(err))
15769 +               goto out_unpin;
15770 +
15771 +       /* make the dir opaque */
15772 +       diropq = 0;
15773 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
15774 +       if (wh_dentry
15775 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
15776 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
15777 +               opq_dentry = au_diropq_create(dentry, bindex);
15778 +               mutex_unlock(h_mtx);
15779 +               err = PTR_ERR(opq_dentry);
15780 +               if (IS_ERR(opq_dentry))
15781 +                       goto out_dir;
15782 +               dput(opq_dentry);
15783 +               diropq = 1;
15784 +       }
15785 +
15786 +       err = epilog(dir, bindex, wh_dentry, dentry);
15787 +       if (!err) {
15788 +               inc_nlink(dir);
15789 +               goto out_unpin; /* success */
15790 +       }
15791 +
15792 +       /* revert */
15793 +       if (diropq) {
15794 +               AuLabel(revert opq);
15795 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
15796 +               rerr = au_diropq_remove(dentry, bindex);
15797 +               mutex_unlock(h_mtx);
15798 +               if (rerr) {
15799 +                       AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
15800 +                               AuDLNPair(dentry), err, rerr);
15801 +                       err = -EIO;
15802 +               }
15803 +       }
15804 +
15805 +out_dir:
15806 +       AuLabel(revert dir);
15807 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
15808 +       if (rerr) {
15809 +               AuIOErr("%.*s reverting dir failed(%d, %d)\n",
15810 +                       AuDLNPair(dentry), err, rerr);
15811 +               err = -EIO;
15812 +       }
15813 +       au_dtime_revert(&a->dt);
15814 +out_unpin:
15815 +       au_unpin(&a->pin);
15816 +       dput(wh_dentry);
15817 +out_parent:
15818 +       di_write_unlock(parent);
15819 +out_unlock:
15820 +       if (unlikely(err)) {
15821 +               au_update_dbstart(dentry);
15822 +               d_drop(dentry);
15823 +       }
15824 +       aufs_read_unlock(dentry, AuLock_DW);
15825 +out_free:
15826 +       kfree(a);
15827 +out:
15828 +       return err;
15829 +}
15830 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
15831 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
15832 +++ linux/fs/aufs/i_op.c        2012-01-05 11:00:59.215501779 +0100
15833 @@ -0,0 +1,994 @@
15834 +/*
15835 + * Copyright (C) 2005-2011 Junjiro R. Okajima
15836 + *
15837 + * This program, aufs is free software; you can redistribute it and/or modify
15838 + * it under the terms of the GNU General Public License as published by
15839 + * the Free Software Foundation; either version 2 of the License, or
15840 + * (at your option) any later version.
15841 + *
15842 + * This program is distributed in the hope that it will be useful,
15843 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15844 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15845 + * GNU General Public License for more details.
15846 + *
15847 + * You should have received a copy of the GNU General Public License
15848 + * along with this program; if not, write to the Free Software
15849 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15850 + */
15851 +
15852 +/*
15853 + * inode operations (except add/del/rename)
15854 + */
15855 +
15856 +#include <linux/device_cgroup.h>
15857 +#include <linux/fs_stack.h>
15858 +#include <linux/mm.h>
15859 +#include <linux/namei.h>
15860 +#include <linux/security.h>
15861 +#include <linux/uaccess.h>
15862 +#include "aufs.h"
15863 +
15864 +static int h_permission(struct inode *h_inode, int mask,
15865 +                       struct vfsmount *h_mnt, int brperm)
15866 +{
15867 +       int err;
15868 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
15869 +
15870 +       err = -EACCES;
15871 +       if ((write_mask && IS_IMMUTABLE(h_inode))
15872 +           || ((mask & MAY_EXEC)
15873 +               && S_ISREG(h_inode->i_mode)
15874 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
15875 +                   || !(h_inode->i_mode & S_IXUGO))))
15876 +               goto out;
15877 +
15878 +       /*
15879 +        * - skip the lower fs test in the case of write to ro branch.
15880 +        * - nfs dir permission write check is optimized, but a policy for
15881 +        *   link/rename requires a real check.
15882 +        */
15883 +       if ((write_mask && !au_br_writable(brperm))
15884 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
15885 +               && write_mask && !(mask & MAY_READ))
15886 +           || !h_inode->i_op->permission) {
15887 +               /* AuLabel(generic_permission); */
15888 +               err = generic_permission(h_inode, mask);
15889 +       } else {
15890 +               /* AuLabel(h_inode->permission); */
15891 +               err = h_inode->i_op->permission(h_inode, mask);
15892 +               AuTraceErr(err);
15893 +       }
15894 +
15895 +       if (!err)
15896 +               err = devcgroup_inode_permission(h_inode, mask);
15897 +       if (!err)
15898 +               err = security_inode_permission(h_inode, mask);
15899 +
15900 +#if 0
15901 +       if (!err) {
15902 +               /* todo: do we need to call ima_path_check()? */
15903 +               struct path h_path = {
15904 +                       .dentry =
15905 +                       .mnt    = h_mnt
15906 +               };
15907 +               err = ima_path_check(&h_path,
15908 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
15909 +                                    IMA_COUNT_LEAVE);
15910 +       }
15911 +#endif
15912 +
15913 +out:
15914 +       return err;
15915 +}
15916 +
15917 +static int aufs_permission(struct inode *inode, int mask)
15918 +{
15919 +       int err;
15920 +       aufs_bindex_t bindex, bend;
15921 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
15922 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
15923 +       struct inode *h_inode;
15924 +       struct super_block *sb;
15925 +       struct au_branch *br;
15926 +
15927 +       /* todo: support rcu-walk? */
15928 +       if (mask & MAY_NOT_BLOCK)
15929 +               return -ECHILD;
15930 +
15931 +       sb = inode->i_sb;
15932 +       si_read_lock(sb, AuLock_FLUSH);
15933 +       ii_read_lock_child(inode);
15934 +#if 0
15935 +       err = au_iigen_test(inode, au_sigen(sb));
15936 +       if (unlikely(err))
15937 +               goto out;
15938 +#endif
15939 +
15940 +       if (!isdir || write_mask) {
15941 +               err = au_busy_or_stale();
15942 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
15943 +               if (unlikely(!h_inode
15944 +                            || (h_inode->i_mode & S_IFMT)
15945 +                            != (inode->i_mode & S_IFMT)))
15946 +                       goto out;
15947 +
15948 +               err = 0;
15949 +               bindex = au_ibstart(inode);
15950 +               br = au_sbr(sb, bindex);
15951 +               err = h_permission(h_inode, mask, br->br_mnt, br->br_perm);
15952 +               if (write_mask
15953 +                   && !err
15954 +                   && !special_file(h_inode->i_mode)) {
15955 +                       /* test whether the upper writable branch exists */
15956 +                       err = -EROFS;
15957 +                       for (; bindex >= 0; bindex--)
15958 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
15959 +                                       err = 0;
15960 +                                       break;
15961 +                               }
15962 +               }
15963 +               goto out;
15964 +       }
15965 +
15966 +       /* non-write to dir */
15967 +       err = 0;
15968 +       bend = au_ibend(inode);
15969 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
15970 +               h_inode = au_h_iptr(inode, bindex);
15971 +               if (h_inode) {
15972 +                       err = au_busy_or_stale();
15973 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
15974 +                               break;
15975 +
15976 +                       br = au_sbr(sb, bindex);
15977 +                       err = h_permission(h_inode, mask, br->br_mnt,
15978 +                                          br->br_perm);
15979 +               }
15980 +       }
15981 +
15982 +out:
15983 +       ii_read_unlock(inode);
15984 +       si_read_unlock(sb);
15985 +       return err;
15986 +}
15987 +
15988 +/* ---------------------------------------------------------------------- */
15989 +
15990 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
15991 +                                 struct nameidata *nd)
15992 +{
15993 +       struct dentry *ret, *parent;
15994 +       struct inode *inode;
15995 +       struct super_block *sb;
15996 +       int err, npositive, lc_idx;
15997 +
15998 +       IMustLock(dir);
15999 +
16000 +       sb = dir->i_sb;
16001 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16002 +       ret = ERR_PTR(err);
16003 +       if (unlikely(err))
16004 +               goto out;
16005 +
16006 +       ret = ERR_PTR(-ENAMETOOLONG);
16007 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16008 +               goto out_si;
16009 +       err = au_di_init(dentry);
16010 +       ret = ERR_PTR(err);
16011 +       if (unlikely(err))
16012 +               goto out_si;
16013 +
16014 +       inode = NULL;
16015 +       npositive = 0; /* suppress a warning */
16016 +       parent = dentry->d_parent; /* dir inode is locked */
16017 +       di_read_lock_parent(parent, AuLock_IR);
16018 +       err = au_alive_dir(parent);
16019 +       if (!err)
16020 +               err = au_digen_test(parent, au_sigen(sb));
16021 +       if (!err) {
16022 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
16023 +                                          /*type*/0, nd);
16024 +               err = npositive;
16025 +       }
16026 +       di_read_unlock(parent, AuLock_IR);
16027 +       ret = ERR_PTR(err);
16028 +       if (unlikely(err < 0))
16029 +               goto out_unlock;
16030 +
16031 +       if (npositive) {
16032 +               inode = au_new_inode(dentry, /*must_new*/0);
16033 +               ret = (void *)inode;
16034 +       }
16035 +       if (IS_ERR(inode)) {
16036 +               inode = NULL;
16037 +               goto out_unlock;
16038 +       }
16039 +
16040 +       ret = d_splice_alias(inode, dentry);
16041 +       if (unlikely(IS_ERR(ret) && inode)) {
16042 +               ii_write_unlock(inode);
16043 +               lc_idx = AuLcNonDir_IIINFO;
16044 +               if (S_ISLNK(inode->i_mode))
16045 +                       lc_idx = AuLcSymlink_IIINFO;
16046 +               else if (S_ISDIR(inode->i_mode))
16047 +                       lc_idx = AuLcDir_IIINFO;
16048 +               au_rw_class(&au_ii(inode)->ii_rwsem, au_lc_key + lc_idx);
16049 +               iput(inode);
16050 +       }
16051 +
16052 +out_unlock:
16053 +       di_write_unlock(dentry);
16054 +       if (unlikely(IS_ERR(ret) && inode)) {
16055 +               lc_idx = AuLcNonDir_DIINFO;
16056 +               if (S_ISLNK(inode->i_mode))
16057 +                       lc_idx = AuLcSymlink_DIINFO;
16058 +               else if (S_ISDIR(inode->i_mode))
16059 +                       lc_idx = AuLcDir_DIINFO;
16060 +               au_rw_class(&au_di(dentry)->di_rwsem, au_lc_key + lc_idx);
16061 +       }
16062 +out_si:
16063 +       si_read_unlock(sb);
16064 +out:
16065 +       return ret;
16066 +}
16067 +
16068 +/* ---------------------------------------------------------------------- */
16069 +
16070 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
16071 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
16072 +                         aufs_bindex_t bstart)
16073 +{
16074 +       int err;
16075 +       struct dentry *h_parent;
16076 +       struct inode *h_dir;
16077 +
16078 +       if (add_entry)
16079 +               IMustLock(parent->d_inode);
16080 +       else
16081 +               di_write_lock_parent(parent);
16082 +
16083 +       err = 0;
16084 +       if (!au_h_dptr(parent, bcpup)) {
16085 +               if (bstart < bcpup)
16086 +                       err = au_cpdown_dirs(dentry, bcpup);
16087 +               else
16088 +                       err = au_cpup_dirs(dentry, bcpup);
16089 +       }
16090 +       if (!err && add_entry) {
16091 +               h_parent = au_h_dptr(parent, bcpup);
16092 +               h_dir = h_parent->d_inode;
16093 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
16094 +               err = au_lkup_neg(dentry, bcpup);
16095 +               /* todo: no unlock here */
16096 +               mutex_unlock(&h_dir->i_mutex);
16097 +
16098 +               AuDbg("bcpup %d\n", bcpup);
16099 +               if (!err) {
16100 +                       if (!dentry->d_inode)
16101 +                               au_set_h_dptr(dentry, bstart, NULL);
16102 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
16103 +               }
16104 +       }
16105 +
16106 +       if (!add_entry)
16107 +               di_write_unlock(parent);
16108 +       if (!err)
16109 +               err = bcpup; /* success */
16110 +
16111 +       AuTraceErr(err);
16112 +       return err;
16113 +}
16114 +
16115 +/*
16116 + * decide the branch and the parent dir where we will create a new entry.
16117 + * returns new bindex or an error.
16118 + * copyup the parent dir if needed.
16119 + */
16120 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16121 +             struct au_wr_dir_args *args)
16122 +{
16123 +       int err;
16124 +       aufs_bindex_t bcpup, bstart, src_bstart;
16125 +       const unsigned char add_entry = !!au_ftest_wrdir(args->flags,
16126 +                                                        ADD_ENTRY);
16127 +       struct super_block *sb;
16128 +       struct dentry *parent;
16129 +       struct au_sbinfo *sbinfo;
16130 +
16131 +       sb = dentry->d_sb;
16132 +       sbinfo = au_sbi(sb);
16133 +       parent = dget_parent(dentry);
16134 +       bstart = au_dbstart(dentry);
16135 +       bcpup = bstart;
16136 +       if (args->force_btgt < 0) {
16137 +               if (src_dentry) {
16138 +                       src_bstart = au_dbstart(src_dentry);
16139 +                       if (src_bstart < bstart)
16140 +                               bcpup = src_bstart;
16141 +               } else if (add_entry) {
16142 +                       err = AuWbrCreate(sbinfo, dentry,
16143 +                                         au_ftest_wrdir(args->flags, ISDIR));
16144 +                       bcpup = err;
16145 +               }
16146 +
16147 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
16148 +                       if (add_entry)
16149 +                               err = AuWbrCopyup(sbinfo, dentry);
16150 +                       else {
16151 +                               if (!IS_ROOT(dentry)) {
16152 +                                       di_read_lock_parent(parent, !AuLock_IR);
16153 +                                       err = AuWbrCopyup(sbinfo, dentry);
16154 +                                       di_read_unlock(parent, !AuLock_IR);
16155 +                               } else
16156 +                                       err = AuWbrCopyup(sbinfo, dentry);
16157 +                       }
16158 +                       bcpup = err;
16159 +                       if (unlikely(err < 0))
16160 +                               goto out;
16161 +               }
16162 +       } else {
16163 +               bcpup = args->force_btgt;
16164 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
16165 +       }
16166 +
16167 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
16168 +       err = bcpup;
16169 +       if (bcpup == bstart)
16170 +               goto out; /* success */
16171 +
16172 +       /* copyup the new parent into the branch we process */
16173 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
16174 +       if (err >= 0) {
16175 +               if (!dentry->d_inode) {
16176 +                       au_set_h_dptr(dentry, bstart, NULL);
16177 +                       au_set_dbstart(dentry, bcpup);
16178 +                       au_set_dbend(dentry, bcpup);
16179 +               }
16180 +               AuDebugOn(add_entry && !au_h_dptr(dentry, bcpup));
16181 +       }
16182 +
16183 +out:
16184 +       dput(parent);
16185 +       return err;
16186 +}
16187 +
16188 +/* ---------------------------------------------------------------------- */
16189 +
16190 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
16191 +{
16192 +       if (pin && pin->parent)
16193 +               return au_h_dptr(pin->parent, pin->bindex);
16194 +       return NULL;
16195 +}
16196 +
16197 +void au_unpin(struct au_pin *p)
16198 +{
16199 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
16200 +               mnt_drop_write(p->h_mnt);
16201 +       if (!p->hdir)
16202 +               return;
16203 +
16204 +       au_hn_imtx_unlock(p->hdir);
16205 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16206 +               di_read_unlock(p->parent, AuLock_IR);
16207 +       iput(p->hdir->hi_inode);
16208 +       dput(p->parent);
16209 +       p->parent = NULL;
16210 +       p->hdir = NULL;
16211 +       p->h_mnt = NULL;
16212 +}
16213 +
16214 +int au_do_pin(struct au_pin *p)
16215 +{
16216 +       int err;
16217 +       struct super_block *sb;
16218 +       struct dentry *h_dentry, *h_parent;
16219 +       struct au_branch *br;
16220 +       struct inode *h_dir;
16221 +
16222 +       err = 0;
16223 +       sb = p->dentry->d_sb;
16224 +       br = au_sbr(sb, p->bindex);
16225 +       if (IS_ROOT(p->dentry)) {
16226 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
16227 +                       p->h_mnt = br->br_mnt;
16228 +                       err = mnt_want_write(p->h_mnt);
16229 +                       if (unlikely(err)) {
16230 +                               au_fclr_pin(p->flags, MNT_WRITE);
16231 +                               goto out_err;
16232 +                       }
16233 +               }
16234 +               goto out;
16235 +       }
16236 +
16237 +       h_dentry = NULL;
16238 +       if (p->bindex <= au_dbend(p->dentry))
16239 +               h_dentry = au_h_dptr(p->dentry, p->bindex);
16240 +
16241 +       p->parent = dget_parent(p->dentry);
16242 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16243 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
16244 +
16245 +       h_dir = NULL;
16246 +       h_parent = au_h_dptr(p->parent, p->bindex);
16247 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
16248 +       if (p->hdir)
16249 +               h_dir = p->hdir->hi_inode;
16250 +
16251 +       /*
16252 +        * udba case, or
16253 +        * if DI_LOCKED is not set, then p->parent may be different
16254 +        * and h_parent can be NULL.
16255 +        */
16256 +       if (unlikely(!p->hdir || !h_dir || !h_parent)) {
16257 +               err = -EBUSY;
16258 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
16259 +                       di_read_unlock(p->parent, AuLock_IR);
16260 +               dput(p->parent);
16261 +               p->parent = NULL;
16262 +               goto out_err;
16263 +       }
16264 +
16265 +       au_igrab(h_dir);
16266 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
16267 +
16268 +       if (unlikely(p->hdir->hi_inode != h_parent->d_inode)) {
16269 +               err = -EBUSY;
16270 +               goto out_unpin;
16271 +       }
16272 +       if (h_dentry) {
16273 +               err = au_h_verify(h_dentry, p->udba, h_dir, h_parent, br);
16274 +               if (unlikely(err)) {
16275 +                       au_fclr_pin(p->flags, MNT_WRITE);
16276 +                       goto out_unpin;
16277 +               }
16278 +       }
16279 +
16280 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
16281 +               p->h_mnt = br->br_mnt;
16282 +               err = mnt_want_write(p->h_mnt);
16283 +               if (unlikely(err)) {
16284 +                       au_fclr_pin(p->flags, MNT_WRITE);
16285 +                       goto out_unpin;
16286 +               }
16287 +       }
16288 +       goto out; /* success */
16289 +
16290 +out_unpin:
16291 +       au_unpin(p);
16292 +out_err:
16293 +       pr_err("err %d\n", err);
16294 +       err = au_busy_or_stale();
16295 +out:
16296 +       return err;
16297 +}
16298 +
16299 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
16300 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16301 +                unsigned int udba, unsigned char flags)
16302 +{
16303 +       p->dentry = dentry;
16304 +       p->udba = udba;
16305 +       p->lsc_di = lsc_di;
16306 +       p->lsc_hi = lsc_hi;
16307 +       p->flags = flags;
16308 +       p->bindex = bindex;
16309 +
16310 +       p->parent = NULL;
16311 +       p->hdir = NULL;
16312 +       p->h_mnt = NULL;
16313 +}
16314 +
16315 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16316 +          unsigned int udba, unsigned char flags)
16317 +{
16318 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
16319 +                   udba, flags);
16320 +       return au_do_pin(pin);
16321 +}
16322 +
16323 +/* ---------------------------------------------------------------------- */
16324 +
16325 +/*
16326 + * ->setattr() and ->getattr() are called in various cases.
16327 + * chmod, stat: dentry is revalidated.
16328 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
16329 + *               unhashed.
16330 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
16331 + */
16332 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
16333 +static int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
16334 +{
16335 +       int err;
16336 +       struct inode *inode;
16337 +       struct dentry *parent;
16338 +
16339 +       err = 0;
16340 +       inode = dentry->d_inode;
16341 +       if (au_digen_test(dentry, sigen)) {
16342 +               parent = dget_parent(dentry);
16343 +               di_read_lock_parent(parent, AuLock_IR);
16344 +               err = au_refresh_dentry(dentry, parent);
16345 +               di_read_unlock(parent, AuLock_IR);
16346 +               dput(parent);
16347 +       }
16348 +
16349 +       AuTraceErr(err);
16350 +       return err;
16351 +}
16352 +
16353 +#define AuIcpup_DID_CPUP       1
16354 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16355 +#define au_fset_icpup(flags, name) \
16356 +       do { (flags) |= AuIcpup_##name; } while (0)
16357 +#define au_fclr_icpup(flags, name) \
16358 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16359 +
16360 +struct au_icpup_args {
16361 +       unsigned char flags;
16362 +       unsigned char pin_flags;
16363 +       aufs_bindex_t btgt;
16364 +       unsigned int udba;
16365 +       struct au_pin pin;
16366 +       struct path h_path;
16367 +       struct inode *h_inode;
16368 +};
16369 +
16370 +static int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16371 +                           struct au_icpup_args *a)
16372 +{
16373 +       int err;
16374 +       loff_t sz;
16375 +       aufs_bindex_t bstart, ibstart;
16376 +       struct dentry *hi_wh, *parent;
16377 +       struct inode *inode;
16378 +       struct file *h_file;
16379 +       struct au_wr_dir_args wr_dir_args = {
16380 +               .force_btgt     = -1,
16381 +               .flags          = 0
16382 +       };
16383 +
16384 +       bstart = au_dbstart(dentry);
16385 +       inode = dentry->d_inode;
16386 +       if (S_ISDIR(inode->i_mode))
16387 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
16388 +       /* plink or hi_wh() case */
16389 +       ibstart = au_ibstart(inode);
16390 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
16391 +               wr_dir_args.force_btgt = ibstart;
16392 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
16393 +       if (unlikely(err < 0))
16394 +               goto out;
16395 +       a->btgt = err;
16396 +       if (err != bstart)
16397 +               au_fset_icpup(a->flags, DID_CPUP);
16398 +
16399 +       err = 0;
16400 +       a->pin_flags = AuPin_MNT_WRITE;
16401 +       parent = NULL;
16402 +       if (!IS_ROOT(dentry)) {
16403 +               au_fset_pin(a->pin_flags, DI_LOCKED);
16404 +               parent = dget_parent(dentry);
16405 +               di_write_lock_parent(parent);
16406 +       }
16407 +
16408 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
16409 +       if (unlikely(err))
16410 +               goto out_parent;
16411 +
16412 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
16413 +       a->h_inode = a->h_path.dentry->d_inode;
16414 +       mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
16415 +       sz = -1;
16416 +       if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
16417 +               sz = ia->ia_size;
16418 +
16419 +       h_file = NULL;
16420 +       hi_wh = NULL;
16421 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
16422 +               hi_wh = au_hi_wh(inode, a->btgt);
16423 +               if (!hi_wh) {
16424 +                       err = au_sio_cpup_wh(dentry, a->btgt, sz, /*file*/NULL);
16425 +                       if (unlikely(err))
16426 +                               goto out_unlock;
16427 +                       hi_wh = au_hi_wh(inode, a->btgt);
16428 +                       /* todo: revalidate hi_wh? */
16429 +               }
16430 +       }
16431 +
16432 +       if (parent) {
16433 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
16434 +               di_downgrade_lock(parent, AuLock_IR);
16435 +               dput(parent);
16436 +               parent = NULL;
16437 +       }
16438 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
16439 +               goto out; /* success */
16440 +
16441 +       if (!d_unhashed(dentry)) {
16442 +               h_file = au_h_open_pre(dentry, bstart);
16443 +               if (IS_ERR(h_file)) {
16444 +                       err = PTR_ERR(h_file);
16445 +                       h_file = NULL;
16446 +               } else
16447 +                       err = au_sio_cpup_simple(dentry, a->btgt, sz,
16448 +                                                AuCpup_DTIME);
16449 +               if (!err)
16450 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
16451 +       } else if (!hi_wh)
16452 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
16453 +       else
16454 +               a->h_path.dentry = hi_wh; /* do not dget here */
16455 +
16456 +out_unlock:
16457 +       mutex_unlock(&a->h_inode->i_mutex);
16458 +       au_h_open_post(dentry, bstart, h_file);
16459 +       a->h_inode = a->h_path.dentry->d_inode;
16460 +       if (!err) {
16461 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
16462 +               goto out; /* success */
16463 +       }
16464 +
16465 +       au_unpin(&a->pin);
16466 +out_parent:
16467 +       if (parent) {
16468 +               di_write_unlock(parent);
16469 +               dput(parent);
16470 +       }
16471 +out:
16472 +       return err;
16473 +}
16474 +
16475 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
16476 +{
16477 +       int err;
16478 +       struct inode *inode;
16479 +       struct super_block *sb;
16480 +       struct file *file;
16481 +       struct au_icpup_args *a;
16482 +
16483 +       inode = dentry->d_inode;
16484 +       IMustLock(inode);
16485 +
16486 +       err = -ENOMEM;
16487 +       a = kzalloc(sizeof(*a), GFP_NOFS);
16488 +       if (unlikely(!a))
16489 +               goto out;
16490 +
16491 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
16492 +               ia->ia_valid &= ~ATTR_MODE;
16493 +
16494 +       file = NULL;
16495 +       sb = dentry->d_sb;
16496 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16497 +       if (unlikely(err))
16498 +               goto out_kfree;
16499 +
16500 +       if (ia->ia_valid & ATTR_FILE) {
16501 +               /* currently ftruncate(2) only */
16502 +               AuDebugOn(!S_ISREG(inode->i_mode));
16503 +               file = ia->ia_file;
16504 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
16505 +               if (unlikely(err))
16506 +                       goto out_si;
16507 +               ia->ia_file = au_hf_top(file);
16508 +               a->udba = AuOpt_UDBA_NONE;
16509 +       } else {
16510 +               /* fchmod() doesn't pass ia_file */
16511 +               a->udba = au_opt_udba(sb);
16512 +               di_write_lock_child(dentry);
16513 +               /* no d_unlinked(), to set UDBA_NONE for root */
16514 +               if (d_unhashed(dentry))
16515 +                       a->udba = AuOpt_UDBA_NONE;
16516 +               if (a->udba != AuOpt_UDBA_NONE) {
16517 +                       AuDebugOn(IS_ROOT(dentry));
16518 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
16519 +                       if (unlikely(err))
16520 +                               goto out_dentry;
16521 +               }
16522 +       }
16523 +
16524 +       err = au_pin_and_icpup(dentry, ia, a);
16525 +       if (unlikely(err < 0))
16526 +               goto out_dentry;
16527 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
16528 +               ia->ia_file = NULL;
16529 +               ia->ia_valid &= ~ATTR_FILE;
16530 +       }
16531 +
16532 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
16533 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
16534 +           == (ATTR_MODE | ATTR_CTIME)) {
16535 +               err = security_path_chmod(a->h_path.dentry, a->h_path.mnt,
16536 +                                         ia->ia_mode);
16537 +               if (unlikely(err))
16538 +                       goto out_unlock;
16539 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
16540 +                  && (ia->ia_valid & ATTR_CTIME)) {
16541 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
16542 +               if (unlikely(err))
16543 +                       goto out_unlock;
16544 +       }
16545 +
16546 +       if (ia->ia_valid & ATTR_SIZE) {
16547 +               struct file *f;
16548 +
16549 +               if (ia->ia_size < i_size_read(inode))
16550 +                       /* unmap only */
16551 +                       truncate_setsize(inode, ia->ia_size);
16552 +
16553 +               f = NULL;
16554 +               if (ia->ia_valid & ATTR_FILE)
16555 +                       f = ia->ia_file;
16556 +               mutex_unlock(&a->h_inode->i_mutex);
16557 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
16558 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
16559 +       } else
16560 +               err = vfsub_notify_change(&a->h_path, ia);
16561 +       if (!err)
16562 +               au_cpup_attr_changeable(inode);
16563 +
16564 +out_unlock:
16565 +       mutex_unlock(&a->h_inode->i_mutex);
16566 +       au_unpin(&a->pin);
16567 +       if (unlikely(err))
16568 +               au_update_dbstart(dentry);
16569 +out_dentry:
16570 +       di_write_unlock(dentry);
16571 +       if (file) {
16572 +               fi_write_unlock(file);
16573 +               ia->ia_file = file;
16574 +               ia->ia_valid |= ATTR_FILE;
16575 +       }
16576 +out_si:
16577 +       si_read_unlock(sb);
16578 +out_kfree:
16579 +       kfree(a);
16580 +out:
16581 +       AuTraceErr(err);
16582 +       return err;
16583 +}
16584 +
16585 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
16586 +                            unsigned int nlink)
16587 +{
16588 +       unsigned int n;
16589 +
16590 +       inode->i_mode = st->mode;
16591 +       inode->i_uid = st->uid;
16592 +       inode->i_gid = st->gid;
16593 +       inode->i_atime = st->atime;
16594 +       inode->i_mtime = st->mtime;
16595 +       inode->i_ctime = st->ctime;
16596 +
16597 +       au_cpup_attr_nlink(inode, /*force*/0);
16598 +       if (S_ISDIR(inode->i_mode)) {
16599 +               n = inode->i_nlink;
16600 +               n -= nlink;
16601 +               n += st->nlink;
16602 +               set_nlink(inode, n);
16603 +       }
16604 +
16605 +       spin_lock(&inode->i_lock);
16606 +       inode->i_blocks = st->blocks;
16607 +       i_size_write(inode, st->size);
16608 +       spin_unlock(&inode->i_lock);
16609 +}
16610 +
16611 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
16612 +                       struct dentry *dentry, struct kstat *st)
16613 +{
16614 +       int err;
16615 +       unsigned int mnt_flags;
16616 +       aufs_bindex_t bindex;
16617 +       unsigned char udba_none, positive;
16618 +       struct super_block *sb, *h_sb;
16619 +       struct inode *inode;
16620 +       struct vfsmount *h_mnt;
16621 +       struct dentry *h_dentry;
16622 +
16623 +       sb = dentry->d_sb;
16624 +       inode = dentry->d_inode;
16625 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16626 +       if (unlikely(err))
16627 +               goto out;
16628 +       mnt_flags = au_mntflags(sb);
16629 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
16630 +
16631 +       /* support fstat(2) */
16632 +       if (!d_unlinked(dentry) && !udba_none) {
16633 +               unsigned int sigen = au_sigen(sb);
16634 +               err = au_digen_test(dentry, sigen);
16635 +               if (!err) {
16636 +                       di_read_lock_child(dentry, AuLock_IR);
16637 +                       err = au_dbrange_test(dentry);
16638 +                       if (unlikely(err))
16639 +                               goto out_unlock;
16640 +               } else {
16641 +                       AuDebugOn(IS_ROOT(dentry));
16642 +                       di_write_lock_child(dentry);
16643 +                       err = au_dbrange_test(dentry);
16644 +                       if (!err)
16645 +                               err = au_reval_for_attr(dentry, sigen);
16646 +                       di_downgrade_lock(dentry, AuLock_IR);
16647 +                       if (unlikely(err))
16648 +                               goto out_unlock;
16649 +               }
16650 +       } else
16651 +               di_read_lock_child(dentry, AuLock_IR);
16652 +
16653 +       bindex = au_ibstart(inode);
16654 +       h_mnt = au_sbr_mnt(sb, bindex);
16655 +       h_sb = h_mnt->mnt_sb;
16656 +       if (!au_test_fs_bad_iattr(h_sb) && udba_none)
16657 +               goto out_fill; /* success */
16658 +
16659 +       h_dentry = NULL;
16660 +       if (au_dbstart(dentry) == bindex)
16661 +               h_dentry = dget(au_h_dptr(dentry, bindex));
16662 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
16663 +               h_dentry = au_plink_lkup(inode, bindex);
16664 +               if (IS_ERR(h_dentry))
16665 +                       goto out_fill; /* pretending success */
16666 +       }
16667 +       /* illegally overlapped or something */
16668 +       if (unlikely(!h_dentry))
16669 +               goto out_fill; /* pretending success */
16670 +
16671 +       positive = !!h_dentry->d_inode;
16672 +       if (positive)
16673 +               err = vfs_getattr(h_mnt, h_dentry, st);
16674 +       dput(h_dentry);
16675 +       if (!err) {
16676 +               if (positive)
16677 +                       au_refresh_iattr(inode, st, h_dentry->d_inode->i_nlink);
16678 +               goto out_fill; /* success */
16679 +       }
16680 +       AuTraceErr(err);
16681 +       goto out_unlock;
16682 +
16683 +out_fill:
16684 +       generic_fillattr(inode, st);
16685 +out_unlock:
16686 +       di_read_unlock(dentry, AuLock_IR);
16687 +       si_read_unlock(sb);
16688 +out:
16689 +       AuTraceErr(err);
16690 +       return err;
16691 +}
16692 +
16693 +/* ---------------------------------------------------------------------- */
16694 +
16695 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
16696 +                     int bufsiz)
16697 +{
16698 +       int err;
16699 +       struct super_block *sb;
16700 +       struct dentry *h_dentry;
16701 +
16702 +       err = -EINVAL;
16703 +       h_dentry = au_h_dptr(dentry, bindex);
16704 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
16705 +               goto out;
16706 +
16707 +       err = security_inode_readlink(h_dentry);
16708 +       if (unlikely(err))
16709 +               goto out;
16710 +
16711 +       sb = dentry->d_sb;
16712 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
16713 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
16714 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
16715 +       }
16716 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
16717 +
16718 +out:
16719 +       return err;
16720 +}
16721 +
16722 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
16723 +{
16724 +       int err;
16725 +
16726 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
16727 +       if (unlikely(err))
16728 +               goto out;
16729 +       err = au_d_hashed_positive(dentry);
16730 +       if (!err)
16731 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
16732 +       aufs_read_unlock(dentry, AuLock_IR);
16733 +
16734 +out:
16735 +       return err;
16736 +}
16737 +
16738 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
16739 +{
16740 +       int err;
16741 +       mm_segment_t old_fs;
16742 +       union {
16743 +               char *k;
16744 +               char __user *u;
16745 +       } buf;
16746 +
16747 +       err = -ENOMEM;
16748 +       buf.k = __getname_gfp(GFP_NOFS);
16749 +       if (unlikely(!buf.k))
16750 +               goto out;
16751 +
16752 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
16753 +       if (unlikely(err))
16754 +               goto out_name;
16755 +
16756 +       err = au_d_hashed_positive(dentry);
16757 +       if (!err) {
16758 +               old_fs = get_fs();
16759 +               set_fs(KERNEL_DS);
16760 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
16761 +               set_fs(old_fs);
16762 +       }
16763 +       aufs_read_unlock(dentry, AuLock_IR);
16764 +
16765 +       if (err >= 0) {
16766 +               buf.k[err] = 0;
16767 +               /* will be freed by put_link */
16768 +               nd_set_link(nd, buf.k);
16769 +               return NULL; /* success */
16770 +       }
16771 +
16772 +out_name:
16773 +       __putname(buf.k);
16774 +out:
16775 +       path_put(&nd->path);
16776 +       AuTraceErr(err);
16777 +       return ERR_PTR(err);
16778 +}
16779 +
16780 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
16781 +                         struct nameidata *nd, void *cookie __maybe_unused)
16782 +{
16783 +       __putname(nd_get_link(nd));
16784 +}
16785 +
16786 +/* ---------------------------------------------------------------------- */
16787 +
16788 +static void aufs_truncate_range(struct inode *inode __maybe_unused,
16789 +                               loff_t start __maybe_unused,
16790 +                               loff_t end __maybe_unused)
16791 +{
16792 +       AuUnsupport();
16793 +}
16794 +
16795 +/* ---------------------------------------------------------------------- */
16796 +
16797 +struct inode_operations aufs_symlink_iop = {
16798 +       .permission     = aufs_permission,
16799 +       .setattr        = aufs_setattr,
16800 +       .getattr        = aufs_getattr,
16801 +       .readlink       = aufs_readlink,
16802 +       .follow_link    = aufs_follow_link,
16803 +       .put_link       = aufs_put_link
16804 +};
16805 +
16806 +struct inode_operations aufs_dir_iop = {
16807 +       .create         = aufs_create,
16808 +       .lookup         = aufs_lookup,
16809 +       .link           = aufs_link,
16810 +       .unlink         = aufs_unlink,
16811 +       .symlink        = aufs_symlink,
16812 +       .mkdir          = aufs_mkdir,
16813 +       .rmdir          = aufs_rmdir,
16814 +       .mknod          = aufs_mknod,
16815 +       .rename         = aufs_rename,
16816 +
16817 +       .permission     = aufs_permission,
16818 +       .setattr        = aufs_setattr,
16819 +       .getattr        = aufs_getattr
16820 +};
16821 +
16822 +struct inode_operations aufs_iop = {
16823 +       .permission     = aufs_permission,
16824 +       .setattr        = aufs_setattr,
16825 +       .getattr        = aufs_getattr,
16826 +       .truncate_range = aufs_truncate_range
16827 +};
16828 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
16829 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
16830 +++ linux/fs/aufs/i_op_del.c    2011-08-24 13:30:24.731313534 +0200
16831 @@ -0,0 +1,478 @@
16832 +/*
16833 + * Copyright (C) 2005-2011 Junjiro R. Okajima
16834 + *
16835 + * This program, aufs is free software; you can redistribute it and/or modify
16836 + * it under the terms of the GNU General Public License as published by
16837 + * the Free Software Foundation; either version 2 of the License, or
16838 + * (at your option) any later version.
16839 + *
16840 + * This program is distributed in the hope that it will be useful,
16841 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16842 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16843 + * GNU General Public License for more details.
16844 + *
16845 + * You should have received a copy of the GNU General Public License
16846 + * along with this program; if not, write to the Free Software
16847 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16848 + */
16849 +
16850 +/*
16851 + * inode operations (del entry)
16852 + */
16853 +
16854 +#include "aufs.h"
16855 +
16856 +/*
16857 + * decide if a new whiteout for @dentry is necessary or not.
16858 + * when it is necessary, prepare the parent dir for the upper branch whose
16859 + * branch index is @bcpup for creation. the actual creation of the whiteout will
16860 + * be done by caller.
16861 + * return value:
16862 + * 0: wh is unnecessary
16863 + * plus: wh is necessary
16864 + * minus: error
16865 + */
16866 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
16867 +{
16868 +       int need_wh, err;
16869 +       aufs_bindex_t bstart;
16870 +       struct super_block *sb;
16871 +
16872 +       sb = dentry->d_sb;
16873 +       bstart = au_dbstart(dentry);
16874 +       if (*bcpup < 0) {
16875 +               *bcpup = bstart;
16876 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
16877 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
16878 +                       *bcpup = err;
16879 +                       if (unlikely(err < 0))
16880 +                               goto out;
16881 +               }
16882 +       } else
16883 +               AuDebugOn(bstart < *bcpup
16884 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
16885 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
16886 +
16887 +       if (*bcpup != bstart) {
16888 +               err = au_cpup_dirs(dentry, *bcpup);
16889 +               if (unlikely(err))
16890 +                       goto out;
16891 +               need_wh = 1;
16892 +       } else {
16893 +               struct au_dinfo *dinfo, *tmp;
16894 +
16895 +               need_wh = -ENOMEM;
16896 +               dinfo = au_di(dentry);
16897 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
16898 +               if (tmp) {
16899 +                       au_di_cp(tmp, dinfo);
16900 +                       au_di_swap(tmp, dinfo);
16901 +                       /* returns the number of positive dentries */
16902 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0,
16903 +                                                /*nd*/NULL);
16904 +                       au_di_swap(tmp, dinfo);
16905 +                       au_rw_write_unlock(&tmp->di_rwsem);
16906 +                       au_di_free(tmp);
16907 +               }
16908 +       }
16909 +       AuDbg("need_wh %d\n", need_wh);
16910 +       err = need_wh;
16911 +
16912 +out:
16913 +       return err;
16914 +}
16915 +
16916 +/*
16917 + * simple tests for the del-entry operations.
16918 + * following the checks in vfs, plus the parent-child relationship.
16919 + */
16920 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16921 +              struct dentry *h_parent, int isdir)
16922 +{
16923 +       int err;
16924 +       umode_t h_mode;
16925 +       struct dentry *h_dentry, *h_latest;
16926 +       struct inode *h_inode;
16927 +
16928 +       h_dentry = au_h_dptr(dentry, bindex);
16929 +       h_inode = h_dentry->d_inode;
16930 +       if (dentry->d_inode) {
16931 +               err = -ENOENT;
16932 +               if (unlikely(!h_inode || !h_inode->i_nlink))
16933 +                       goto out;
16934 +
16935 +               h_mode = h_inode->i_mode;
16936 +               if (!isdir) {
16937 +                       err = -EISDIR;
16938 +                       if (unlikely(S_ISDIR(h_mode)))
16939 +                               goto out;
16940 +               } else if (unlikely(!S_ISDIR(h_mode))) {
16941 +                       err = -ENOTDIR;
16942 +                       goto out;
16943 +               }
16944 +       } else {
16945 +               /* rename(2) case */
16946 +               err = -EIO;
16947 +               if (unlikely(h_inode))
16948 +                       goto out;
16949 +       }
16950 +
16951 +       err = -ENOENT;
16952 +       /* expected parent dir is locked */
16953 +       if (unlikely(h_parent != h_dentry->d_parent))
16954 +               goto out;
16955 +       err = 0;
16956 +
16957 +       /*
16958 +        * rmdir a dir may break the consistency on some filesystem.
16959 +        * let's try heavy test.
16960 +        */
16961 +       err = -EACCES;
16962 +       if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
16963 +               goto out;
16964 +
16965 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
16966 +                                  au_sbr(dentry->d_sb, bindex));
16967 +       err = -EIO;
16968 +       if (IS_ERR(h_latest))
16969 +               goto out;
16970 +       if (h_latest == h_dentry)
16971 +               err = 0;
16972 +       dput(h_latest);
16973 +
16974 +out:
16975 +       return err;
16976 +}
16977 +
16978 +/*
16979 + * decide the branch where we operate for @dentry. the branch index will be set
16980 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
16981 + * dir for reverting.
16982 + * when a new whiteout is necessary, create it.
16983 + */
16984 +static struct dentry*
16985 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
16986 +                   struct au_dtime *dt, struct au_pin *pin)
16987 +{
16988 +       struct dentry *wh_dentry;
16989 +       struct super_block *sb;
16990 +       struct path h_path;
16991 +       int err, need_wh;
16992 +       unsigned int udba;
16993 +       aufs_bindex_t bcpup;
16994 +
16995 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
16996 +       wh_dentry = ERR_PTR(need_wh);
16997 +       if (unlikely(need_wh < 0))
16998 +               goto out;
16999 +
17000 +       sb = dentry->d_sb;
17001 +       udba = au_opt_udba(sb);
17002 +       bcpup = *rbcpup;
17003 +       err = au_pin(pin, dentry, bcpup, udba,
17004 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17005 +       wh_dentry = ERR_PTR(err);
17006 +       if (unlikely(err))
17007 +               goto out;
17008 +
17009 +       h_path.dentry = au_pinned_h_parent(pin);
17010 +       if (udba != AuOpt_UDBA_NONE
17011 +           && au_dbstart(dentry) == bcpup) {
17012 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
17013 +               wh_dentry = ERR_PTR(err);
17014 +               if (unlikely(err))
17015 +                       goto out_unpin;
17016 +       }
17017 +
17018 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
17019 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
17020 +       wh_dentry = NULL;
17021 +       if (!need_wh)
17022 +               goto out; /* success, no need to create whiteout */
17023 +
17024 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
17025 +       if (IS_ERR(wh_dentry))
17026 +               goto out_unpin;
17027 +
17028 +       /* returns with the parent is locked and wh_dentry is dget-ed */
17029 +       goto out; /* success */
17030 +
17031 +out_unpin:
17032 +       au_unpin(pin);
17033 +out:
17034 +       return wh_dentry;
17035 +}
17036 +
17037 +/*
17038 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
17039 + * in order to be revertible and save time for removing many child whiteouts
17040 + * under the dir.
17041 + * returns 1 when there are too many child whiteout and caller should remove
17042 + * them asynchronously. returns 0 when the number of children is enough small to
17043 + * remove now or the branch fs is a remote fs.
17044 + * otherwise return an error.
17045 + */
17046 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
17047 +                          struct au_nhash *whlist, struct inode *dir)
17048 +{
17049 +       int rmdir_later, err, dirwh;
17050 +       struct dentry *h_dentry;
17051 +       struct super_block *sb;
17052 +
17053 +       sb = dentry->d_sb;
17054 +       SiMustAnyLock(sb);
17055 +       h_dentry = au_h_dptr(dentry, bindex);
17056 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
17057 +       if (unlikely(err))
17058 +               goto out;
17059 +
17060 +       /* stop monitoring */
17061 +       au_hn_free(au_hi(dentry->d_inode, bindex));
17062 +
17063 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
17064 +               dirwh = au_sbi(sb)->si_dirwh;
17065 +               rmdir_later = (dirwh <= 1);
17066 +               if (!rmdir_later)
17067 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
17068 +                                                             dirwh);
17069 +               if (rmdir_later)
17070 +                       return rmdir_later;
17071 +       }
17072 +
17073 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
17074 +       if (unlikely(err)) {
17075 +               AuIOErr("rmdir %.*s, b%d failed, %d. ignored\n",
17076 +                       AuDLNPair(h_dentry), bindex, err);
17077 +               err = 0;
17078 +       }
17079 +
17080 +out:
17081 +       AuTraceErr(err);
17082 +       return err;
17083 +}
17084 +
17085 +/*
17086 + * final procedure for deleting a entry.
17087 + * maintain dentry and iattr.
17088 + */
17089 +static void epilog(struct inode *dir, struct dentry *dentry,
17090 +                  aufs_bindex_t bindex)
17091 +{
17092 +       struct inode *inode;
17093 +
17094 +       inode = dentry->d_inode;
17095 +       d_drop(dentry);
17096 +       inode->i_ctime = dir->i_ctime;
17097 +
17098 +       if (au_ibstart(dir) == bindex)
17099 +               au_cpup_attr_timesizes(dir);
17100 +       dir->i_version++;
17101 +}
17102 +
17103 +/*
17104 + * when an error happened, remove the created whiteout and revert everything.
17105 + */
17106 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
17107 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
17108 +                    struct dentry *dentry, struct au_dtime *dt)
17109 +{
17110 +       int rerr;
17111 +       struct path h_path = {
17112 +               .dentry = wh_dentry,
17113 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
17114 +       };
17115 +
17116 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
17117 +       if (!rerr) {
17118 +               au_set_dbwh(dentry, bwh);
17119 +               au_dtime_revert(dt);
17120 +               return 0;
17121 +       }
17122 +
17123 +       AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
17124 +               AuDLNPair(dentry), err, rerr);
17125 +       return -EIO;
17126 +}
17127 +
17128 +/* ---------------------------------------------------------------------- */
17129 +
17130 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
17131 +{
17132 +       int err;
17133 +       aufs_bindex_t bwh, bindex, bstart;
17134 +       struct au_dtime dt;
17135 +       struct au_pin pin;
17136 +       struct path h_path;
17137 +       struct inode *inode, *h_dir;
17138 +       struct dentry *parent, *wh_dentry;
17139 +
17140 +       IMustLock(dir);
17141 +
17142 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17143 +       if (unlikely(err))
17144 +               goto out;
17145 +       err = au_d_hashed_positive(dentry);
17146 +       if (unlikely(err))
17147 +               goto out_unlock;
17148 +       inode = dentry->d_inode;
17149 +       IMustLock(inode);
17150 +       err = -EISDIR;
17151 +       if (unlikely(S_ISDIR(inode->i_mode)))
17152 +               goto out_unlock; /* possible? */
17153 +
17154 +       bstart = au_dbstart(dentry);
17155 +       bwh = au_dbwh(dentry);
17156 +       bindex = -1;
17157 +       parent = dentry->d_parent; /* dir inode is locked */
17158 +       di_write_lock_parent(parent);
17159 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &dt, &pin);
17160 +       err = PTR_ERR(wh_dentry);
17161 +       if (IS_ERR(wh_dentry))
17162 +               goto out_parent;
17163 +
17164 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
17165 +       h_path.dentry = au_h_dptr(dentry, bstart);
17166 +       dget(h_path.dentry);
17167 +       if (bindex == bstart) {
17168 +               h_dir = au_pinned_h_dir(&pin);
17169 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
17170 +       } else {
17171 +               /* dir inode is locked */
17172 +               h_dir = wh_dentry->d_parent->d_inode;
17173 +               IMustLock(h_dir);
17174 +               err = 0;
17175 +       }
17176 +
17177 +       if (!err) {
17178 +               vfsub_drop_nlink(inode);
17179 +               epilog(dir, dentry, bindex);
17180 +
17181 +               /* update target timestamps */
17182 +               if (bindex == bstart) {
17183 +                       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
17184 +                       inode->i_ctime = h_path.dentry->d_inode->i_ctime;
17185 +               } else
17186 +                       /* todo: this timestamp may be reverted later */
17187 +                       inode->i_ctime = h_dir->i_ctime;
17188 +               goto out_unpin; /* success */
17189 +       }
17190 +
17191 +       /* revert */
17192 +       if (wh_dentry) {
17193 +               int rerr;
17194 +
17195 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry, &dt);
17196 +               if (rerr)
17197 +                       err = rerr;
17198 +       }
17199 +
17200 +out_unpin:
17201 +       au_unpin(&pin);
17202 +       dput(wh_dentry);
17203 +       dput(h_path.dentry);
17204 +out_parent:
17205 +       di_write_unlock(parent);
17206 +out_unlock:
17207 +       aufs_read_unlock(dentry, AuLock_DW);
17208 +out:
17209 +       return err;
17210 +}
17211 +
17212 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
17213 +{
17214 +       int err, rmdir_later;
17215 +       aufs_bindex_t bwh, bindex, bstart;
17216 +       struct au_dtime dt;
17217 +       struct au_pin pin;
17218 +       struct inode *inode;
17219 +       struct dentry *parent, *wh_dentry, *h_dentry;
17220 +       struct au_whtmp_rmdir *args;
17221 +
17222 +       IMustLock(dir);
17223 +
17224 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
17225 +       if (unlikely(err))
17226 +               goto out;
17227 +       err = au_alive_dir(dentry);
17228 +       if (unlikely(err))
17229 +               goto out_unlock;
17230 +       inode = dentry->d_inode;
17231 +       IMustLock(inode);
17232 +       err = -ENOTDIR;
17233 +       if (unlikely(!S_ISDIR(inode->i_mode)))
17234 +               goto out_unlock; /* possible? */
17235 +
17236 +       err = -ENOMEM;
17237 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
17238 +       if (unlikely(!args))
17239 +               goto out_unlock;
17240 +
17241 +       parent = dentry->d_parent; /* dir inode is locked */
17242 +       di_write_lock_parent(parent);
17243 +       err = au_test_empty(dentry, &args->whlist);
17244 +       if (unlikely(err))
17245 +               goto out_parent;
17246 +
17247 +       bstart = au_dbstart(dentry);
17248 +       bwh = au_dbwh(dentry);
17249 +       bindex = -1;
17250 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &dt, &pin);
17251 +       err = PTR_ERR(wh_dentry);
17252 +       if (IS_ERR(wh_dentry))
17253 +               goto out_parent;
17254 +
17255 +       h_dentry = au_h_dptr(dentry, bstart);
17256 +       dget(h_dentry);
17257 +       rmdir_later = 0;
17258 +       if (bindex == bstart) {
17259 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
17260 +               if (err > 0) {
17261 +                       rmdir_later = err;
17262 +                       err = 0;
17263 +               }
17264 +       } else {
17265 +               /* stop monitoring */
17266 +               au_hn_free(au_hi(inode, bstart));
17267 +
17268 +               /* dir inode is locked */
17269 +               IMustLock(wh_dentry->d_parent->d_inode);
17270 +               err = 0;
17271 +       }
17272 +
17273 +       if (!err) {
17274 +               vfsub_dead_dir(inode);
17275 +               au_set_dbdiropq(dentry, -1);
17276 +               epilog(dir, dentry, bindex);
17277 +
17278 +               if (rmdir_later) {
17279 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
17280 +                       args = NULL;
17281 +               }
17282 +
17283 +               goto out_unpin; /* success */
17284 +       }
17285 +
17286 +       /* revert */
17287 +       AuLabel(revert);
17288 +       if (wh_dentry) {
17289 +               int rerr;
17290 +
17291 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry, &dt);
17292 +               if (rerr)
17293 +                       err = rerr;
17294 +       }
17295 +
17296 +out_unpin:
17297 +       au_unpin(&pin);
17298 +       dput(wh_dentry);
17299 +       dput(h_dentry);
17300 +out_parent:
17301 +       di_write_unlock(parent);
17302 +       if (args)
17303 +               au_whtmp_rmdir_free(args);
17304 +out_unlock:
17305 +       aufs_read_unlock(dentry, AuLock_DW);
17306 +out:
17307 +       AuTraceErr(err);
17308 +       return err;
17309 +}
17310 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
17311 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
17312 +++ linux/fs/aufs/i_op_ren.c    2011-08-24 13:30:24.734646739 +0200
17313 @@ -0,0 +1,1017 @@
17314 +/*
17315 + * Copyright (C) 2005-2011 Junjiro R. Okajima
17316 + *
17317 + * This program, aufs is free software; you can redistribute it and/or modify
17318 + * it under the terms of the GNU General Public License as published by
17319 + * the Free Software Foundation; either version 2 of the License, or
17320 + * (at your option) any later version.
17321 + *
17322 + * This program is distributed in the hope that it will be useful,
17323 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17324 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17325 + * GNU General Public License for more details.
17326 + *
17327 + * You should have received a copy of the GNU General Public License
17328 + * along with this program; if not, write to the Free Software
17329 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17330 + */
17331 +
17332 +/*
17333 + * inode operation (rename entry)
17334 + * todo: this is crazy monster
17335 + */
17336 +
17337 +#include "aufs.h"
17338 +
17339 +enum { AuSRC, AuDST, AuSrcDst };
17340 +enum { AuPARENT, AuCHILD, AuParentChild };
17341 +
17342 +#define AuRen_ISDIR    1
17343 +#define AuRen_ISSAMEDIR        (1 << 1)
17344 +#define AuRen_WHSRC    (1 << 2)
17345 +#define AuRen_WHDST    (1 << 3)
17346 +#define AuRen_MNT_WRITE        (1 << 4)
17347 +#define AuRen_DT_DSTDIR        (1 << 5)
17348 +#define AuRen_DIROPQ   (1 << 6)
17349 +#define AuRen_CPUP     (1 << 7)
17350 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
17351 +#define au_fset_ren(flags, name) \
17352 +       do { (flags) |= AuRen_##name; } while (0)
17353 +#define au_fclr_ren(flags, name) \
17354 +       do { (flags) &= ~AuRen_##name; } while (0)
17355 +
17356 +struct au_ren_args {
17357 +       struct {
17358 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
17359 +                       *wh_dentry;
17360 +               struct inode *dir, *inode;
17361 +               struct au_hinode *hdir;
17362 +               struct au_dtime dt[AuParentChild];
17363 +               aufs_bindex_t bstart;
17364 +       } sd[AuSrcDst];
17365 +
17366 +#define src_dentry     sd[AuSRC].dentry
17367 +#define src_dir                sd[AuSRC].dir
17368 +#define src_inode      sd[AuSRC].inode
17369 +#define src_h_dentry   sd[AuSRC].h_dentry
17370 +#define src_parent     sd[AuSRC].parent
17371 +#define src_h_parent   sd[AuSRC].h_parent
17372 +#define src_wh_dentry  sd[AuSRC].wh_dentry
17373 +#define src_hdir       sd[AuSRC].hdir
17374 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
17375 +#define src_dt         sd[AuSRC].dt
17376 +#define src_bstart     sd[AuSRC].bstart
17377 +
17378 +#define dst_dentry     sd[AuDST].dentry
17379 +#define dst_dir                sd[AuDST].dir
17380 +#define dst_inode      sd[AuDST].inode
17381 +#define dst_h_dentry   sd[AuDST].h_dentry
17382 +#define dst_parent     sd[AuDST].parent
17383 +#define dst_h_parent   sd[AuDST].h_parent
17384 +#define dst_wh_dentry  sd[AuDST].wh_dentry
17385 +#define dst_hdir       sd[AuDST].hdir
17386 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
17387 +#define dst_dt         sd[AuDST].dt
17388 +#define dst_bstart     sd[AuDST].bstart
17389 +
17390 +       struct dentry *h_trap;
17391 +       struct au_branch *br;
17392 +       struct au_hinode *src_hinode;
17393 +       struct path h_path;
17394 +       struct au_nhash whlist;
17395 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
17396 +
17397 +       unsigned int flags;
17398 +
17399 +       struct au_whtmp_rmdir *thargs;
17400 +       struct dentry *h_dst;
17401 +};
17402 +
17403 +/* ---------------------------------------------------------------------- */
17404 +
17405 +/*
17406 + * functions for reverting.
17407 + * when an error happened in a single rename systemcall, we should revert
17408 + * everything as if nothing happend.
17409 + * we don't need to revert the copied-up/down the parent dir since they are
17410 + * harmless.
17411 + */
17412 +
17413 +#define RevertFailure(fmt, ...) do { \
17414 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
17415 +               ##__VA_ARGS__, err, rerr); \
17416 +       err = -EIO; \
17417 +} while (0)
17418 +
17419 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
17420 +{
17421 +       int rerr;
17422 +
17423 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
17424 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
17425 +       au_hn_imtx_unlock(a->src_hinode);
17426 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
17427 +       if (rerr)
17428 +               RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
17429 +}
17430 +
17431 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
17432 +{
17433 +       int rerr;
17434 +
17435 +       a->h_path.dentry = au_lkup_one(&a->src_dentry->d_name, a->src_h_parent,
17436 +                                      a->br, /*nd*/NULL);
17437 +       rerr = PTR_ERR(a->h_path.dentry);
17438 +       if (IS_ERR(a->h_path.dentry)) {
17439 +               RevertFailure("au_lkup_one %.*s", AuDLNPair(a->src_dentry));
17440 +               return;
17441 +       }
17442 +
17443 +       rerr = vfsub_rename(a->dst_h_dir,
17444 +                           au_h_dptr(a->src_dentry, a->btgt),
17445 +                           a->src_h_dir, &a->h_path);
17446 +       d_drop(a->h_path.dentry);
17447 +       dput(a->h_path.dentry);
17448 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
17449 +       if (rerr)
17450 +               RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
17451 +}
17452 +
17453 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
17454 +{
17455 +       int rerr;
17456 +
17457 +       a->h_path.dentry = a->dst_h_dentry;
17458 +       rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*force*/0);
17459 +       au_set_h_dptr(a->src_dentry, a->btgt, NULL);
17460 +       au_set_dbstart(a->src_dentry, a->src_bstart);
17461 +       if (rerr)
17462 +               RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
17463 +}
17464 +
17465 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
17466 +{
17467 +       int rerr;
17468 +
17469 +       a->h_path.dentry = au_lkup_one(&a->dst_dentry->d_name, a->dst_h_parent,
17470 +                                      a->br, /*nd*/NULL);
17471 +       rerr = PTR_ERR(a->h_path.dentry);
17472 +       if (IS_ERR(a->h_path.dentry)) {
17473 +               RevertFailure("lookup %.*s", AuDLNPair(a->dst_dentry));
17474 +               return;
17475 +       }
17476 +       if (a->h_path.dentry->d_inode) {
17477 +               d_drop(a->h_path.dentry);
17478 +               dput(a->h_path.dentry);
17479 +               return;
17480 +       }
17481 +
17482 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path);
17483 +       d_drop(a->h_path.dentry);
17484 +       dput(a->h_path.dentry);
17485 +       if (!rerr)
17486 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
17487 +       else
17488 +               RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
17489 +}
17490 +
17491 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
17492 +{
17493 +       int rerr;
17494 +
17495 +       a->h_path.dentry = a->src_wh_dentry;
17496 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
17497 +       au_set_dbwh(a->src_dentry, a->src_bwh);
17498 +       if (rerr)
17499 +               RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
17500 +}
17501 +#undef RevertFailure
17502 +
17503 +/* ---------------------------------------------------------------------- */
17504 +
17505 +/*
17506 + * when we have to copyup the renaming entry, do it with the rename-target name
17507 + * in order to minimize the cost (the later actual rename is unnecessary).
17508 + * otherwise rename it on the target branch.
17509 + */
17510 +static int au_ren_or_cpup(struct au_ren_args *a)
17511 +{
17512 +       int err;
17513 +       struct dentry *d;
17514 +
17515 +       d = a->src_dentry;
17516 +       if (au_dbstart(d) == a->btgt) {
17517 +               a->h_path.dentry = a->dst_h_dentry;
17518 +               if (au_ftest_ren(a->flags, DIROPQ)
17519 +                   && au_dbdiropq(d) == a->btgt)
17520 +                       au_fclr_ren(a->flags, DIROPQ);
17521 +               AuDebugOn(au_dbstart(d) != a->btgt);
17522 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
17523 +                                  a->dst_h_dir, &a->h_path);
17524 +       } else {
17525 +               struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
17526 +               struct file *h_file;
17527 +
17528 +               au_fset_ren(a->flags, CPUP);
17529 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
17530 +               au_set_dbstart(d, a->btgt);
17531 +               au_set_h_dptr(d, a->btgt, dget(a->dst_h_dentry));
17532 +               h_file = au_h_open_pre(d, a->src_bstart);
17533 +               if (IS_ERR(h_file)) {
17534 +                       err = PTR_ERR(h_file);
17535 +                       h_file = NULL;
17536 +               } else
17537 +                       err = au_sio_cpup_single(d, a->btgt, a->src_bstart, -1,
17538 +                                                !AuCpup_DTIME, a->dst_parent);
17539 +               mutex_unlock(h_mtx);
17540 +               au_h_open_post(d, a->src_bstart, h_file);
17541 +               if (!err) {
17542 +                       d = a->dst_dentry;
17543 +                       au_set_h_dptr(d, a->btgt, NULL);
17544 +                       au_update_dbstart(d);
17545 +               } else {
17546 +                       au_set_h_dptr(d, a->btgt, NULL);
17547 +                       au_set_dbstart(d, a->src_bstart);
17548 +               }
17549 +       }
17550 +       if (!err && a->h_dst)
17551 +               /* it will be set to dinfo later */
17552 +               dget(a->h_dst);
17553 +
17554 +       return err;
17555 +}
17556 +
17557 +/* cf. aufs_rmdir() */
17558 +static int au_ren_del_whtmp(struct au_ren_args *a)
17559 +{
17560 +       int err;
17561 +       struct inode *dir;
17562 +
17563 +       dir = a->dst_dir;
17564 +       SiMustAnyLock(dir->i_sb);
17565 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
17566 +                                    au_sbi(dir->i_sb)->si_dirwh)
17567 +           || au_test_fs_remote(a->h_dst->d_sb)) {
17568 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
17569 +               if (unlikely(err))
17570 +                       pr_warning("failed removing whtmp dir %.*s (%d), "
17571 +                                  "ignored.\n", AuDLNPair(a->h_dst), err);
17572 +       } else {
17573 +               au_nhash_wh_free(&a->thargs->whlist);
17574 +               a->thargs->whlist = a->whlist;
17575 +               a->whlist.nh_num = 0;
17576 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
17577 +               dput(a->h_dst);
17578 +               a->thargs = NULL;
17579 +       }
17580 +
17581 +       return 0;
17582 +}
17583 +
17584 +/* make it 'opaque' dir. */
17585 +static int au_ren_diropq(struct au_ren_args *a)
17586 +{
17587 +       int err;
17588 +       struct dentry *diropq;
17589 +
17590 +       err = 0;
17591 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
17592 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
17593 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
17594 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
17595 +       au_hn_imtx_unlock(a->src_hinode);
17596 +       if (IS_ERR(diropq))
17597 +               err = PTR_ERR(diropq);
17598 +       dput(diropq);
17599 +
17600 +       return err;
17601 +}
17602 +
17603 +static int do_rename(struct au_ren_args *a)
17604 +{
17605 +       int err;
17606 +       struct dentry *d, *h_d;
17607 +
17608 +       /* prepare workqueue args for asynchronous rmdir */
17609 +       h_d = a->dst_h_dentry;
17610 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
17611 +               err = -ENOMEM;
17612 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
17613 +               if (unlikely(!a->thargs))
17614 +                       goto out;
17615 +               a->h_dst = dget(h_d);
17616 +       }
17617 +
17618 +       /* create whiteout for src_dentry */
17619 +       if (au_ftest_ren(a->flags, WHSRC)) {
17620 +               a->src_bwh = au_dbwh(a->src_dentry);
17621 +               AuDebugOn(a->src_bwh >= 0);
17622 +               a->src_wh_dentry
17623 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
17624 +               err = PTR_ERR(a->src_wh_dentry);
17625 +               if (IS_ERR(a->src_wh_dentry))
17626 +                       goto out_thargs;
17627 +       }
17628 +
17629 +       /* lookup whiteout for dentry */
17630 +       if (au_ftest_ren(a->flags, WHDST)) {
17631 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
17632 +                                a->br);
17633 +               err = PTR_ERR(h_d);
17634 +               if (IS_ERR(h_d))
17635 +                       goto out_whsrc;
17636 +               if (!h_d->d_inode)
17637 +                       dput(h_d);
17638 +               else
17639 +                       a->dst_wh_dentry = h_d;
17640 +       }
17641 +
17642 +       /* rename dentry to tmpwh */
17643 +       if (a->thargs) {
17644 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
17645 +               if (unlikely(err))
17646 +                       goto out_whdst;
17647 +
17648 +               d = a->dst_dentry;
17649 +               au_set_h_dptr(d, a->btgt, NULL);
17650 +               err = au_lkup_neg(d, a->btgt);
17651 +               if (unlikely(err))
17652 +                       goto out_whtmp;
17653 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
17654 +       }
17655 +
17656 +       /* cpup src */
17657 +       if (a->dst_h_dentry->d_inode && a->src_bstart != a->btgt) {
17658 +               struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
17659 +               struct file *h_file;
17660 +
17661 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
17662 +               AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
17663 +               h_file = au_h_open_pre(a->src_dentry, a->src_bstart);
17664 +               if (IS_ERR(h_file)) {
17665 +                       err = PTR_ERR(h_file);
17666 +                       h_file = NULL;
17667 +               } else
17668 +                       err = au_sio_cpup_simple(a->src_dentry, a->btgt, -1,
17669 +                                                !AuCpup_DTIME);
17670 +               mutex_unlock(h_mtx);
17671 +               au_h_open_post(a->src_dentry, a->src_bstart, h_file);
17672 +               if (unlikely(err))
17673 +                       goto out_whtmp;
17674 +       }
17675 +
17676 +       /* rename by vfs_rename or cpup */
17677 +       d = a->dst_dentry;
17678 +       if (au_ftest_ren(a->flags, ISDIR)
17679 +           && (a->dst_wh_dentry
17680 +               || au_dbdiropq(d) == a->btgt
17681 +               /* hide the lower to keep xino */
17682 +               || a->btgt < au_dbend(d)
17683 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
17684 +               au_fset_ren(a->flags, DIROPQ);
17685 +       err = au_ren_or_cpup(a);
17686 +       if (unlikely(err))
17687 +               /* leave the copied-up one */
17688 +               goto out_whtmp;
17689 +
17690 +       /* make dir opaque */
17691 +       if (au_ftest_ren(a->flags, DIROPQ)) {
17692 +               err = au_ren_diropq(a);
17693 +               if (unlikely(err))
17694 +                       goto out_rename;
17695 +       }
17696 +
17697 +       /* update target timestamps */
17698 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
17699 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
17700 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
17701 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
17702 +
17703 +       /* remove whiteout for dentry */
17704 +       if (a->dst_wh_dentry) {
17705 +               a->h_path.dentry = a->dst_wh_dentry;
17706 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
17707 +                                         a->dst_dentry);
17708 +               if (unlikely(err))
17709 +                       goto out_diropq;
17710 +       }
17711 +
17712 +       /* remove whtmp */
17713 +       if (a->thargs)
17714 +               au_ren_del_whtmp(a); /* ignore this error */
17715 +
17716 +       err = 0;
17717 +       goto out_success;
17718 +
17719 +out_diropq:
17720 +       if (au_ftest_ren(a->flags, DIROPQ))
17721 +               au_ren_rev_diropq(err, a);
17722 +out_rename:
17723 +       if (!au_ftest_ren(a->flags, CPUP))
17724 +               au_ren_rev_rename(err, a);
17725 +       else
17726 +               au_ren_rev_cpup(err, a);
17727 +       dput(a->h_dst);
17728 +out_whtmp:
17729 +       if (a->thargs)
17730 +               au_ren_rev_whtmp(err, a);
17731 +out_whdst:
17732 +       dput(a->dst_wh_dentry);
17733 +       a->dst_wh_dentry = NULL;
17734 +out_whsrc:
17735 +       if (a->src_wh_dentry)
17736 +               au_ren_rev_whsrc(err, a);
17737 +out_success:
17738 +       dput(a->src_wh_dentry);
17739 +       dput(a->dst_wh_dentry);
17740 +out_thargs:
17741 +       if (a->thargs) {
17742 +               dput(a->h_dst);
17743 +               au_whtmp_rmdir_free(a->thargs);
17744 +               a->thargs = NULL;
17745 +       }
17746 +out:
17747 +       return err;
17748 +}
17749 +
17750 +/* ---------------------------------------------------------------------- */
17751 +
17752 +/*
17753 + * test if @dentry dir can be rename destination or not.
17754 + * success means, it is a logically empty dir.
17755 + */
17756 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
17757 +{
17758 +       return au_test_empty(dentry, whlist);
17759 +}
17760 +
17761 +/*
17762 + * test if @dentry dir can be rename source or not.
17763 + * if it can, return 0 and @children is filled.
17764 + * success means,
17765 + * - it is a logically empty dir.
17766 + * - or, it exists on writable branch and has no children including whiteouts
17767 + *       on the lower branch.
17768 + */
17769 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
17770 +{
17771 +       int err;
17772 +       unsigned int rdhash;
17773 +       aufs_bindex_t bstart;
17774 +
17775 +       bstart = au_dbstart(dentry);
17776 +       if (bstart != btgt) {
17777 +               struct au_nhash whlist;
17778 +
17779 +               SiMustAnyLock(dentry->d_sb);
17780 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
17781 +               if (!rdhash)
17782 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
17783 +                                                          dentry));
17784 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
17785 +               if (unlikely(err))
17786 +                       goto out;
17787 +               err = au_test_empty(dentry, &whlist);
17788 +               au_nhash_wh_free(&whlist);
17789 +               goto out;
17790 +       }
17791 +
17792 +       if (bstart == au_dbtaildir(dentry))
17793 +               return 0; /* success */
17794 +
17795 +       err = au_test_empty_lower(dentry);
17796 +
17797 +out:
17798 +       if (err == -ENOTEMPTY) {
17799 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
17800 +                       " is not supported\n");
17801 +               err = -EXDEV;
17802 +       }
17803 +       return err;
17804 +}
17805 +
17806 +/* side effect: sets whlist and h_dentry */
17807 +static int au_ren_may_dir(struct au_ren_args *a)
17808 +{
17809 +       int err;
17810 +       unsigned int rdhash;
17811 +       struct dentry *d;
17812 +
17813 +       d = a->dst_dentry;
17814 +       SiMustAnyLock(d->d_sb);
17815 +
17816 +       err = 0;
17817 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
17818 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
17819 +               if (!rdhash)
17820 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
17821 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
17822 +               if (unlikely(err))
17823 +                       goto out;
17824 +
17825 +               au_set_dbstart(d, a->dst_bstart);
17826 +               err = may_rename_dstdir(d, &a->whlist);
17827 +               au_set_dbstart(d, a->btgt);
17828 +       }
17829 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
17830 +       if (unlikely(err))
17831 +               goto out;
17832 +
17833 +       d = a->src_dentry;
17834 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
17835 +       if (au_ftest_ren(a->flags, ISDIR)) {
17836 +               err = may_rename_srcdir(d, a->btgt);
17837 +               if (unlikely(err)) {
17838 +                       au_nhash_wh_free(&a->whlist);
17839 +                       a->whlist.nh_num = 0;
17840 +               }
17841 +       }
17842 +out:
17843 +       return err;
17844 +}
17845 +
17846 +/* ---------------------------------------------------------------------- */
17847 +
17848 +/*
17849 + * simple tests for rename.
17850 + * following the checks in vfs, plus the parent-child relationship.
17851 + */
17852 +static int au_may_ren(struct au_ren_args *a)
17853 +{
17854 +       int err, isdir;
17855 +       struct inode *h_inode;
17856 +
17857 +       if (a->src_bstart == a->btgt) {
17858 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
17859 +                                au_ftest_ren(a->flags, ISDIR));
17860 +               if (unlikely(err))
17861 +                       goto out;
17862 +               err = -EINVAL;
17863 +               if (unlikely(a->src_h_dentry == a->h_trap))
17864 +                       goto out;
17865 +       }
17866 +
17867 +       err = 0;
17868 +       if (a->dst_bstart != a->btgt)
17869 +               goto out;
17870 +
17871 +       err = -ENOTEMPTY;
17872 +       if (unlikely(a->dst_h_dentry == a->h_trap))
17873 +               goto out;
17874 +
17875 +       err = -EIO;
17876 +       h_inode = a->dst_h_dentry->d_inode;
17877 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
17878 +       if (!a->dst_dentry->d_inode) {
17879 +               if (unlikely(h_inode))
17880 +                       goto out;
17881 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
17882 +                                isdir);
17883 +       } else {
17884 +               if (unlikely(!h_inode || !h_inode->i_nlink))
17885 +                       goto out;
17886 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
17887 +                                isdir);
17888 +               if (unlikely(err))
17889 +                       goto out;
17890 +       }
17891 +
17892 +out:
17893 +       if (unlikely(err == -ENOENT || err == -EEXIST))
17894 +               err = -EIO;
17895 +       AuTraceErr(err);
17896 +       return err;
17897 +}
17898 +
17899 +/* ---------------------------------------------------------------------- */
17900 +
17901 +/*
17902 + * locking order
17903 + * (VFS)
17904 + * - src_dir and dir by lock_rename()
17905 + * - inode if exitsts
17906 + * (aufs)
17907 + * - lock all
17908 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
17909 + *     + si_read_lock
17910 + *     + di_write_lock2_child()
17911 + *       + di_write_lock_child()
17912 + *        + ii_write_lock_child()
17913 + *       + di_write_lock_child2()
17914 + *        + ii_write_lock_child2()
17915 + *     + src_parent and parent
17916 + *       + di_write_lock_parent()
17917 + *        + ii_write_lock_parent()
17918 + *       + di_write_lock_parent2()
17919 + *        + ii_write_lock_parent2()
17920 + *   + lower src_dir and dir by vfsub_lock_rename()
17921 + *   + verify the every relationships between child and parent. if any
17922 + *     of them failed, unlock all and return -EBUSY.
17923 + */
17924 +static void au_ren_unlock(struct au_ren_args *a)
17925 +{
17926 +       struct super_block *sb;
17927 +
17928 +       sb = a->dst_dentry->d_sb;
17929 +       if (au_ftest_ren(a->flags, MNT_WRITE))
17930 +               mnt_drop_write(a->br->br_mnt);
17931 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
17932 +                           a->dst_h_parent, a->dst_hdir);
17933 +}
17934 +
17935 +static int au_ren_lock(struct au_ren_args *a)
17936 +{
17937 +       int err;
17938 +       unsigned int udba;
17939 +
17940 +       err = 0;
17941 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
17942 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
17943 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
17944 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
17945 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
17946 +                                     a->dst_h_parent, a->dst_hdir);
17947 +       udba = au_opt_udba(a->src_dentry->d_sb);
17948 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
17949 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
17950 +               err = au_busy_or_stale();
17951 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
17952 +               err = au_h_verify(a->src_h_dentry, udba,
17953 +                                 a->src_h_parent->d_inode, a->src_h_parent,
17954 +                                 a->br);
17955 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
17956 +               err = au_h_verify(a->dst_h_dentry, udba,
17957 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
17958 +                                 a->br);
17959 +       if (!err) {
17960 +               err = mnt_want_write(a->br->br_mnt);
17961 +               if (unlikely(err))
17962 +                       goto out_unlock;
17963 +               au_fset_ren(a->flags, MNT_WRITE);
17964 +               goto out; /* success */
17965 +       }
17966 +
17967 +       err = au_busy_or_stale();
17968 +
17969 +out_unlock:
17970 +       au_ren_unlock(a);
17971 +out:
17972 +       return err;
17973 +}
17974 +
17975 +/* ---------------------------------------------------------------------- */
17976 +
17977 +static void au_ren_refresh_dir(struct au_ren_args *a)
17978 +{
17979 +       struct inode *dir;
17980 +
17981 +       dir = a->dst_dir;
17982 +       dir->i_version++;
17983 +       if (au_ftest_ren(a->flags, ISDIR)) {
17984 +               /* is this updating defined in POSIX? */
17985 +               au_cpup_attr_timesizes(a->src_inode);
17986 +               au_cpup_attr_nlink(dir, /*force*/1);
17987 +       }
17988 +
17989 +       if (au_ibstart(dir) == a->btgt)
17990 +               au_cpup_attr_timesizes(dir);
17991 +
17992 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
17993 +               return;
17994 +
17995 +       dir = a->src_dir;
17996 +       dir->i_version++;
17997 +       if (au_ftest_ren(a->flags, ISDIR))
17998 +               au_cpup_attr_nlink(dir, /*force*/1);
17999 +       if (au_ibstart(dir) == a->btgt)
18000 +               au_cpup_attr_timesizes(dir);
18001 +}
18002 +
18003 +static void au_ren_refresh(struct au_ren_args *a)
18004 +{
18005 +       aufs_bindex_t bend, bindex;
18006 +       struct dentry *d, *h_d;
18007 +       struct inode *i, *h_i;
18008 +       struct super_block *sb;
18009 +
18010 +       d = a->dst_dentry;
18011 +       d_drop(d);
18012 +       if (a->h_dst)
18013 +               /* already dget-ed by au_ren_or_cpup() */
18014 +               au_set_h_dptr(d, a->btgt, a->h_dst);
18015 +
18016 +       i = a->dst_inode;
18017 +       if (i) {
18018 +               if (!au_ftest_ren(a->flags, ISDIR))
18019 +                       vfsub_drop_nlink(i);
18020 +               else {
18021 +                       vfsub_dead_dir(i);
18022 +                       au_cpup_attr_timesizes(i);
18023 +               }
18024 +               au_update_dbrange(d, /*do_put_zero*/1);
18025 +       } else {
18026 +               bend = a->btgt;
18027 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
18028 +                       au_set_h_dptr(d, bindex, NULL);
18029 +               bend = au_dbend(d);
18030 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
18031 +                       au_set_h_dptr(d, bindex, NULL);
18032 +               au_update_dbrange(d, /*do_put_zero*/0);
18033 +       }
18034 +
18035 +       d = a->src_dentry;
18036 +       au_set_dbwh(d, -1);
18037 +       bend = au_dbend(d);
18038 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18039 +               h_d = au_h_dptr(d, bindex);
18040 +               if (h_d)
18041 +                       au_set_h_dptr(d, bindex, NULL);
18042 +       }
18043 +       au_set_dbend(d, a->btgt);
18044 +
18045 +       sb = d->d_sb;
18046 +       i = a->src_inode;
18047 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
18048 +               return; /* success */
18049 +
18050 +       bend = au_ibend(i);
18051 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18052 +               h_i = au_h_iptr(i, bindex);
18053 +               if (h_i) {
18054 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
18055 +                       /* ignore this error */
18056 +                       au_set_h_iptr(i, bindex, NULL, 0);
18057 +               }
18058 +       }
18059 +       au_set_ibend(i, a->btgt);
18060 +}
18061 +
18062 +/* ---------------------------------------------------------------------- */
18063 +
18064 +/* mainly for link(2) and rename(2) */
18065 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
18066 +{
18067 +       aufs_bindex_t bdiropq, bwh;
18068 +       struct dentry *parent;
18069 +       struct au_branch *br;
18070 +
18071 +       parent = dentry->d_parent;
18072 +       IMustLock(parent->d_inode); /* dir is locked */
18073 +
18074 +       bdiropq = au_dbdiropq(parent);
18075 +       bwh = au_dbwh(dentry);
18076 +       br = au_sbr(dentry->d_sb, btgt);
18077 +       if (au_br_rdonly(br)
18078 +           || (0 <= bdiropq && bdiropq < btgt)
18079 +           || (0 <= bwh && bwh < btgt))
18080 +               btgt = -1;
18081 +
18082 +       AuDbg("btgt %d\n", btgt);
18083 +       return btgt;
18084 +}
18085 +
18086 +/* sets src_bstart, dst_bstart and btgt */
18087 +static int au_ren_wbr(struct au_ren_args *a)
18088 +{
18089 +       int err;
18090 +       struct au_wr_dir_args wr_dir_args = {
18091 +               /* .force_btgt  = -1, */
18092 +               .flags          = AuWrDir_ADD_ENTRY
18093 +       };
18094 +
18095 +       a->src_bstart = au_dbstart(a->src_dentry);
18096 +       a->dst_bstart = au_dbstart(a->dst_dentry);
18097 +       if (au_ftest_ren(a->flags, ISDIR))
18098 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18099 +       wr_dir_args.force_btgt = a->src_bstart;
18100 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
18101 +               wr_dir_args.force_btgt = a->dst_bstart;
18102 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
18103 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
18104 +       a->btgt = err;
18105 +
18106 +       return err;
18107 +}
18108 +
18109 +static void au_ren_dt(struct au_ren_args *a)
18110 +{
18111 +       a->h_path.dentry = a->src_h_parent;
18112 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
18113 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
18114 +               a->h_path.dentry = a->dst_h_parent;
18115 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
18116 +       }
18117 +
18118 +       au_fclr_ren(a->flags, DT_DSTDIR);
18119 +       if (!au_ftest_ren(a->flags, ISDIR))
18120 +               return;
18121 +
18122 +       a->h_path.dentry = a->src_h_dentry;
18123 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
18124 +       if (a->dst_h_dentry->d_inode) {
18125 +               au_fset_ren(a->flags, DT_DSTDIR);
18126 +               a->h_path.dentry = a->dst_h_dentry;
18127 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
18128 +       }
18129 +}
18130 +
18131 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
18132 +{
18133 +       struct dentry *h_d;
18134 +       struct mutex *h_mtx;
18135 +
18136 +       au_dtime_revert(a->src_dt + AuPARENT);
18137 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
18138 +               au_dtime_revert(a->dst_dt + AuPARENT);
18139 +
18140 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
18141 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
18142 +               h_mtx = &h_d->d_inode->i_mutex;
18143 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18144 +               au_dtime_revert(a->src_dt + AuCHILD);
18145 +               mutex_unlock(h_mtx);
18146 +
18147 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
18148 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
18149 +                       h_mtx = &h_d->d_inode->i_mutex;
18150 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18151 +                       au_dtime_revert(a->dst_dt + AuCHILD);
18152 +                       mutex_unlock(h_mtx);
18153 +               }
18154 +       }
18155 +}
18156 +
18157 +/* ---------------------------------------------------------------------- */
18158 +
18159 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
18160 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
18161 +{
18162 +       int err, flags;
18163 +       /* reduce stack space */
18164 +       struct au_ren_args *a;
18165 +
18166 +       AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
18167 +       IMustLock(_src_dir);
18168 +       IMustLock(_dst_dir);
18169 +
18170 +       err = -ENOMEM;
18171 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
18172 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18173 +       if (unlikely(!a))
18174 +               goto out;
18175 +
18176 +       a->src_dir = _src_dir;
18177 +       a->src_dentry = _src_dentry;
18178 +       a->src_inode = a->src_dentry->d_inode;
18179 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
18180 +       a->dst_dir = _dst_dir;
18181 +       a->dst_dentry = _dst_dentry;
18182 +       a->dst_inode = a->dst_dentry->d_inode;
18183 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
18184 +       if (a->dst_inode) {
18185 +               IMustLock(a->dst_inode);
18186 +               au_igrab(a->dst_inode);
18187 +       }
18188 +
18189 +       err = -ENOTDIR;
18190 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
18191 +       if (S_ISDIR(a->src_inode->i_mode)) {
18192 +               au_fset_ren(a->flags, ISDIR);
18193 +               if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
18194 +                       goto out_free;
18195 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18196 +                                               AuLock_DIR | flags);
18197 +       } else
18198 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18199 +                                               flags);
18200 +       if (unlikely(err))
18201 +               goto out_free;
18202 +
18203 +       err = au_d_hashed_positive(a->src_dentry);
18204 +       if (unlikely(err))
18205 +               goto out_unlock;
18206 +       err = -ENOENT;
18207 +       if (a->dst_inode) {
18208 +               /*
18209 +                * If it is a dir, VFS unhash dst_dentry before this
18210 +                * function. It means we cannot rely upon d_unhashed().
18211 +                */
18212 +               if (unlikely(!a->dst_inode->i_nlink))
18213 +                       goto out_unlock;
18214 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
18215 +                       err = au_d_hashed_positive(a->dst_dentry);
18216 +                       if (unlikely(err))
18217 +                               goto out_unlock;
18218 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
18219 +                       goto out_unlock;
18220 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
18221 +               goto out_unlock;
18222 +
18223 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
18224 +       di_write_lock_parent(a->dst_parent);
18225 +
18226 +       /* which branch we process */
18227 +       err = au_ren_wbr(a);
18228 +       if (unlikely(err < 0))
18229 +               goto out_parent;
18230 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
18231 +       a->h_path.mnt = a->br->br_mnt;
18232 +
18233 +       /* are they available to be renamed */
18234 +       err = au_ren_may_dir(a);
18235 +       if (unlikely(err))
18236 +               goto out_children;
18237 +
18238 +       /* prepare the writable parent dir on the same branch */
18239 +       if (a->dst_bstart == a->btgt) {
18240 +               au_fset_ren(a->flags, WHDST);
18241 +       } else {
18242 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
18243 +               if (unlikely(err))
18244 +                       goto out_children;
18245 +       }
18246 +
18247 +       if (a->src_dir != a->dst_dir) {
18248 +               /*
18249 +                * this temporary unlock is safe,
18250 +                * because both dir->i_mutex are locked.
18251 +                */
18252 +               di_write_unlock(a->dst_parent);
18253 +               di_write_lock_parent(a->src_parent);
18254 +               err = au_wr_dir_need_wh(a->src_dentry,
18255 +                                       au_ftest_ren(a->flags, ISDIR),
18256 +                                       &a->btgt);
18257 +               di_write_unlock(a->src_parent);
18258 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
18259 +               au_fclr_ren(a->flags, ISSAMEDIR);
18260 +       } else
18261 +               err = au_wr_dir_need_wh(a->src_dentry,
18262 +                                       au_ftest_ren(a->flags, ISDIR),
18263 +                                       &a->btgt);
18264 +       if (unlikely(err < 0))
18265 +               goto out_children;
18266 +       if (err)
18267 +               au_fset_ren(a->flags, WHSRC);
18268 +
18269 +       /* lock them all */
18270 +       err = au_ren_lock(a);
18271 +       if (unlikely(err))
18272 +               goto out_children;
18273 +
18274 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
18275 +               err = au_may_ren(a);
18276 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
18277 +               err = -ENAMETOOLONG;
18278 +       if (unlikely(err))
18279 +               goto out_hdir;
18280 +
18281 +       /* store timestamps to be revertible */
18282 +       au_ren_dt(a);
18283 +
18284 +       /* here we go */
18285 +       err = do_rename(a);
18286 +       if (unlikely(err))
18287 +               goto out_dt;
18288 +
18289 +       /* update dir attributes */
18290 +       au_ren_refresh_dir(a);
18291 +
18292 +       /* dput/iput all lower dentries */
18293 +       au_ren_refresh(a);
18294 +
18295 +       goto out_hdir; /* success */
18296 +
18297 +out_dt:
18298 +       au_ren_rev_dt(err, a);
18299 +out_hdir:
18300 +       au_ren_unlock(a);
18301 +out_children:
18302 +       au_nhash_wh_free(&a->whlist);
18303 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
18304 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
18305 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
18306 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
18307 +       }
18308 +out_parent:
18309 +       if (!err)
18310 +               d_move(a->src_dentry, a->dst_dentry);
18311 +       else {
18312 +               au_update_dbstart(a->dst_dentry);
18313 +               if (!a->dst_inode)
18314 +                       d_drop(a->dst_dentry);
18315 +       }
18316 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
18317 +               di_write_unlock(a->dst_parent);
18318 +       else
18319 +               di_write_unlock2(a->src_parent, a->dst_parent);
18320 +out_unlock:
18321 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
18322 +out_free:
18323 +       iput(a->dst_inode);
18324 +       if (a->thargs)
18325 +               au_whtmp_rmdir_free(a->thargs);
18326 +       kfree(a);
18327 +out:
18328 +       AuTraceErr(err);
18329 +       return err;
18330 +}
18331 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
18332 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
18333 +++ linux/fs/aufs/Kconfig       2011-08-24 13:30:24.727980364 +0200
18334 @@ -0,0 +1,203 @@
18335 +config AUFS_FS
18336 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
18337 +       depends on EXPERIMENTAL
18338 +       help
18339 +       Aufs is a stackable unification filesystem such as Unionfs,
18340 +       which unifies several directories and provides a merged single
18341 +       directory.
18342 +       In the early days, aufs was entirely re-designed and
18343 +       re-implemented Unionfs Version 1.x series. Introducing many
18344 +       original ideas, approaches and improvements, it becomes totally
18345 +       different from Unionfs while keeping the basic features.
18346 +
18347 +if AUFS_FS
18348 +choice
18349 +       prompt "Maximum number of branches"
18350 +       default AUFS_BRANCH_MAX_127
18351 +       help
18352 +       Specifies the maximum number of branches (or member directories)
18353 +       in a single aufs. The larger value consumes more system
18354 +       resources and has a minor impact to performance.
18355 +config AUFS_BRANCH_MAX_127
18356 +       bool "127"
18357 +       help
18358 +       Specifies the maximum number of branches (or member directories)
18359 +       in a single aufs. The larger value consumes more system
18360 +       resources and has a minor impact to performance.
18361 +config AUFS_BRANCH_MAX_511
18362 +       bool "511"
18363 +       help
18364 +       Specifies the maximum number of branches (or member directories)
18365 +       in a single aufs. The larger value consumes more system
18366 +       resources and has a minor impact to performance.
18367 +config AUFS_BRANCH_MAX_1023
18368 +       bool "1023"
18369 +       help
18370 +       Specifies the maximum number of branches (or member directories)
18371 +       in a single aufs. The larger value consumes more system
18372 +       resources and has a minor impact to performance.
18373 +config AUFS_BRANCH_MAX_32767
18374 +       bool "32767"
18375 +       help
18376 +       Specifies the maximum number of branches (or member directories)
18377 +       in a single aufs. The larger value consumes more system
18378 +       resources and has a minor impact to performance.
18379 +endchoice
18380 +
18381 +config AUFS_SBILIST
18382 +       bool
18383 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
18384 +       default y
18385 +       help
18386 +       Automatic configuration for internal use.
18387 +       When aufs supports Magic SysRq or /proc, enabled automatically.
18388 +
18389 +config AUFS_HNOTIFY
18390 +       bool "Detect direct branch access (bypassing aufs)"
18391 +       help
18392 +       If you want to modify files on branches directly, eg. bypassing aufs,
18393 +       and want aufs to detect the changes of them fully, then enable this
18394 +       option and use 'udba=notify' mount option.
18395 +       Currently there is only one available configuration, "fsnotify".
18396 +       It will have a negative impact to the performance.
18397 +       See detail in aufs.5.
18398 +
18399 +choice
18400 +       prompt "method" if AUFS_HNOTIFY
18401 +       default AUFS_HFSNOTIFY
18402 +config AUFS_HFSNOTIFY
18403 +       bool "fsnotify"
18404 +       select FSNOTIFY
18405 +endchoice
18406 +
18407 +config AUFS_EXPORT
18408 +       bool "NFS-exportable aufs"
18409 +       depends on EXPORTFS
18410 +       help
18411 +       If you want to export your mounted aufs via NFS, then enable this
18412 +       option. There are several requirements for this configuration.
18413 +       See detail in aufs.5.
18414 +
18415 +config AUFS_INO_T_64
18416 +       bool
18417 +       depends on AUFS_EXPORT
18418 +       depends on 64BIT && !(ALPHA || S390)
18419 +       default y
18420 +       help
18421 +       Automatic configuration for internal use.
18422 +       /* typedef unsigned long/int __kernel_ino_t */
18423 +       /* alpha and s390x are int */
18424 +
18425 +config AUFS_RDU
18426 +       bool "Readdir in userspace"
18427 +       help
18428 +       Aufs has two methods to provide a merged view for a directory,
18429 +       by a user-space library and by kernel-space natively. The latter
18430 +       is always enabled but sometimes large and slow.
18431 +       If you enable this option, install the library in aufs2-util
18432 +       package, and set some environment variables for your readdir(3),
18433 +       then the work will be handled in user-space which generally
18434 +       shows better performance in most cases.
18435 +       See detail in aufs.5.
18436 +
18437 +config AUFS_PROC_MAP
18438 +       bool "support for /proc/maps and lsof(1)"
18439 +       depends on PROC_FS
18440 +       help
18441 +       When you issue mmap(2) in aufs, it is actually a direct mmap(2)
18442 +       call to the file on the branch fs since the file in aufs is
18443 +       purely virtual. And the file path printed in /proc/maps (and
18444 +       others) will be the path on the branch fs. In most cases, it
18445 +       does no harm. But some utilities like lsof(1) may confuse since
18446 +       the utility or user may expect the file path in aufs to be
18447 +       printed.
18448 +       To address this issue, aufs provides a patch which introduces a
18449 +       new member called vm_prfile into struct vm_are_struct. The patch
18450 +       is meaningless without enabling this configuration since nobody
18451 +       sets the new vm_prfile member.
18452 +       If you don't apply the patch, then enabling this configuration
18453 +       will cause a compile error.
18454 +       This approach is fragile since if someone else make some changes
18455 +       around vm_file, then vm_prfile may not work anymore. As a
18456 +       workaround such case, aufs provides this configuration. If you
18457 +       disable it, then lsof(1) may produce incorrect result but the
18458 +       problem will be gone even if the aufs patch is applied (I hope).
18459 +
18460 +config AUFS_SP_IATTR
18461 +       bool "Respect the attributes (mtime/ctime mainly) of special files"
18462 +       help
18463 +       When you write something to a special file, some attributes of it
18464 +       (mtime/ctime mainly) may be updated. Generally such updates are
18465 +       less important (actually some device drivers and NFS ignore
18466 +       it). But some applications (such like test program) requires
18467 +       such updates. If you need these updates, then enable this
18468 +       configuration which introduces some overhead.
18469 +       Currently this configuration handles FIFO only.
18470 +
18471 +config AUFS_SHWH
18472 +       bool "Show whiteouts"
18473 +       help
18474 +       If you want to make the whiteouts in aufs visible, then enable
18475 +       this option and specify 'shwh' mount option. Although it may
18476 +       sounds like philosophy or something, but in technically it
18477 +       simply shows the name of whiteout with keeping its behaviour.
18478 +
18479 +config AUFS_BR_RAMFS
18480 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
18481 +       help
18482 +       If you want to use ramfs as an aufs branch fs, then enable this
18483 +       option. Generally tmpfs is recommended.
18484 +       Aufs prohibited them to be a branch fs by default, because
18485 +       initramfs becomes unusable after switch_root or something
18486 +       generally. If you sets initramfs as an aufs branch and boot your
18487 +       system by switch_root, you will meet a problem easily since the
18488 +       files in initramfs may be inaccessible.
18489 +       Unless you are going to use ramfs as an aufs branch fs without
18490 +       switch_root or something, leave it N.
18491 +
18492 +config AUFS_BR_FUSE
18493 +       bool "Fuse fs as an aufs branch"
18494 +       depends on FUSE_FS
18495 +       select AUFS_POLL
18496 +       help
18497 +       If you want to use fuse-based userspace filesystem as an aufs
18498 +       branch fs, then enable this option.
18499 +       It implements the internal poll(2) operation which is
18500 +       implemented by fuse only (curretnly).
18501 +
18502 +config AUFS_POLL
18503 +       bool
18504 +       help
18505 +       Automatic configuration for internal use.
18506 +
18507 +config AUFS_BR_HFSPLUS
18508 +       bool "Hfsplus as an aufs branch"
18509 +       depends on HFSPLUS_FS
18510 +       default y
18511 +       help
18512 +       If you want to use hfsplus fs as an aufs branch fs, then enable
18513 +       this option. This option introduces a small overhead at
18514 +       copying-up a file on hfsplus.
18515 +
18516 +config AUFS_BDEV_LOOP
18517 +       bool
18518 +       depends on BLK_DEV_LOOP
18519 +       default y
18520 +       help
18521 +       Automatic configuration for internal use.
18522 +       Convert =[ym] into =y.
18523 +
18524 +config AUFS_DEBUG
18525 +       bool "Debug aufs"
18526 +       help
18527 +       Enable this to compile aufs internal debug code.
18528 +       It will have a negative impact to the performance.
18529 +
18530 +config AUFS_MAGIC_SYSRQ
18531 +       bool
18532 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
18533 +       default y
18534 +       help
18535 +       Automatic configuration for internal use.
18536 +       When aufs supports Magic SysRq, enabled automatically.
18537 +endif
18538 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
18539 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
18540 +++ linux/fs/aufs/loop.c        2011-08-24 13:30:24.734646739 +0200
18541 @@ -0,0 +1,133 @@
18542 +/*
18543 + * Copyright (C) 2005-2011 Junjiro R. Okajima
18544 + *
18545 + * This program, aufs is free software; you can redistribute it and/or modify
18546 + * it under the terms of the GNU General Public License as published by
18547 + * the Free Software Foundation; either version 2 of the License, or
18548 + * (at your option) any later version.
18549 + *
18550 + * This program is distributed in the hope that it will be useful,
18551 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18552 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18553 + * GNU General Public License for more details.
18554 + *
18555 + * You should have received a copy of the GNU General Public License
18556 + * along with this program; if not, write to the Free Software
18557 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18558 + */
18559 +
18560 +/*
18561 + * support for loopback block device as a branch
18562 + */
18563 +
18564 +#include <linux/loop.h>
18565 +#include "aufs.h"
18566 +
18567 +/*
18568 + * test if two lower dentries have overlapping branches.
18569 + */
18570 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
18571 +{
18572 +       struct super_block *h_sb;
18573 +       struct loop_device *l;
18574 +
18575 +       h_sb = h_adding->d_sb;
18576 +       if (MAJOR(h_sb->s_dev) != LOOP_MAJOR)
18577 +               return 0;
18578 +
18579 +       l = h_sb->s_bdev->bd_disk->private_data;
18580 +       h_adding = l->lo_backing_file->f_dentry;
18581 +       /*
18582 +        * h_adding can be local NFS.
18583 +        * in this case aufs cannot detect the loop.
18584 +        */
18585 +       if (unlikely(h_adding->d_sb == sb))
18586 +               return 1;
18587 +       return !!au_test_subdir(h_adding, sb->s_root);
18588 +}
18589 +
18590 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
18591 +int au_test_loopback_kthread(void)
18592 +{
18593 +       int ret;
18594 +       struct task_struct *tsk = current;
18595 +
18596 +       ret = 0;
18597 +       if (tsk->flags & PF_KTHREAD) {
18598 +               const char c = tsk->comm[4];
18599 +               ret = ('0' <= c && c <= '9'
18600 +                      && !strncmp(tsk->comm, "loop", 4));
18601 +       }
18602 +
18603 +       return ret;
18604 +}
18605 +
18606 +/* ---------------------------------------------------------------------- */
18607 +
18608 +#define au_warn_loopback_step  16
18609 +static int au_warn_loopback_nelem = au_warn_loopback_step;
18610 +static unsigned long *au_warn_loopback_array;
18611 +
18612 +void au_warn_loopback(struct super_block *h_sb)
18613 +{
18614 +       int i, new_nelem;
18615 +       unsigned long *a, magic;
18616 +       static DEFINE_SPINLOCK(spin);
18617 +
18618 +       magic = h_sb->s_magic;
18619 +       spin_lock(&spin);
18620 +       a = au_warn_loopback_array;
18621 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
18622 +               if (a[i] == magic) {
18623 +                       spin_unlock(&spin);
18624 +                       return;
18625 +               }
18626 +
18627 +       /* h_sb is new to us, print it */
18628 +       if (i < au_warn_loopback_nelem) {
18629 +               a[i] = magic;
18630 +               goto pr;
18631 +       }
18632 +
18633 +       /* expand the array */
18634 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
18635 +       a = au_kzrealloc(au_warn_loopback_array,
18636 +                        au_warn_loopback_nelem * sizeof(unsigned long),
18637 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
18638 +       if (a) {
18639 +               au_warn_loopback_nelem = new_nelem;
18640 +               au_warn_loopback_array = a;
18641 +               a[i] = magic;
18642 +               goto pr;
18643 +       }
18644 +
18645 +       spin_unlock(&spin);
18646 +       AuWarn1("realloc failed, ignored\n");
18647 +       return;
18648 +
18649 +pr:
18650 +       spin_unlock(&spin);
18651 +       pr_warning("you may want to try another patch for loopback file "
18652 +                  "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
18653 +}
18654 +
18655 +int au_loopback_init(void)
18656 +{
18657 +       int err;
18658 +       struct super_block *sb __maybe_unused;
18659 +
18660 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
18661 +
18662 +       err = 0;
18663 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
18664 +                                        sizeof(unsigned long), GFP_NOFS);
18665 +       if (unlikely(!au_warn_loopback_array))
18666 +               err = -ENOMEM;
18667 +
18668 +       return err;
18669 +}
18670 +
18671 +void au_loopback_fin(void)
18672 +{
18673 +       kfree(au_warn_loopback_array);
18674 +}
18675 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
18676 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
18677 +++ linux/fs/aufs/loop.h        2011-08-24 13:30:24.734646739 +0200
18678 @@ -0,0 +1,50 @@
18679 +/*
18680 + * Copyright (C) 2005-2011 Junjiro R. Okajima
18681 + *
18682 + * This program, aufs is free software; you can redistribute it and/or modify
18683 + * it under the terms of the GNU General Public License as published by
18684 + * the Free Software Foundation; either version 2 of the License, or
18685 + * (at your option) any later version.
18686 + *
18687 + * This program is distributed in the hope that it will be useful,
18688 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18689 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18690 + * GNU General Public License for more details.
18691 + *
18692 + * You should have received a copy of the GNU General Public License
18693 + * along with this program; if not, write to the Free Software
18694 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18695 + */
18696 +
18697 +/*
18698 + * support for loopback mount as a branch
18699 + */
18700 +
18701 +#ifndef __AUFS_LOOP_H__
18702 +#define __AUFS_LOOP_H__
18703 +
18704 +#ifdef __KERNEL__
18705 +
18706 +struct dentry;
18707 +struct super_block;
18708 +
18709 +#ifdef CONFIG_AUFS_BDEV_LOOP
18710 +/* loop.c */
18711 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
18712 +int au_test_loopback_kthread(void);
18713 +void au_warn_loopback(struct super_block *h_sb);
18714 +
18715 +int au_loopback_init(void);
18716 +void au_loopback_fin(void);
18717 +#else
18718 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
18719 +          struct dentry *h_adding)
18720 +AuStubInt0(au_test_loopback_kthread, void)
18721 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
18722 +
18723 +AuStubInt0(au_loopback_init, void)
18724 +AuStubVoid(au_loopback_fin, void)
18725 +#endif /* BLK_DEV_LOOP */
18726 +
18727 +#endif /* __KERNEL__ */
18728 +#endif /* __AUFS_LOOP_H__ */
18729 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
18730 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
18731 +++ linux/fs/aufs/magic.mk      2011-08-24 13:30:24.734646739 +0200
18732 @@ -0,0 +1,54 @@
18733 +
18734 +# defined in ${srctree}/fs/fuse/inode.c
18735 +# tristate
18736 +ifdef CONFIG_FUSE_FS
18737 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
18738 +endif
18739 +
18740 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
18741 +# tristate
18742 +ifdef CONFIG_OCFS2_FS
18743 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
18744 +endif
18745 +
18746 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
18747 +# tristate
18748 +ifdef CONFIG_OCFS2_FS_O2CB
18749 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
18750 +endif
18751 +
18752 +# defined in ${srctree}/fs/cifs/cifsfs.c
18753 +# tristate
18754 +ifdef CONFIG_CIFS_FS
18755 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
18756 +endif
18757 +
18758 +# defined in ${srctree}/fs/xfs/xfs_sb.h
18759 +# tristate
18760 +ifdef CONFIG_XFS_FS
18761 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
18762 +endif
18763 +
18764 +# defined in ${srctree}/fs/configfs/mount.c
18765 +# tristate
18766 +ifdef CONFIG_CONFIGFS_FS
18767 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
18768 +endif
18769 +
18770 +# defined in ${srctree}/fs/9p/v9fs.h
18771 +# tristate
18772 +ifdef CONFIG_9P_FS
18773 +ccflags-y += -DV9FS_MAGIC=0x01021997
18774 +endif
18775 +
18776 +# defined in ${srctree}/fs/ubifs/ubifs.h
18777 +# tristate
18778 +ifdef CONFIG_UBIFS_FS
18779 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
18780 +endif
18781 +
18782 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
18783 +# tristate
18784 +ifdef CONFIG_HFSPLUS_FS
18785 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
18786 +endif
18787 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
18788 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
18789 +++ linux/fs/aufs/Makefile      2011-08-24 13:30:24.727980364 +0200
18790 @@ -0,0 +1,38 @@
18791 +
18792 +include ${src}/magic.mk
18793 +ifeq (${CONFIG_AUFS_FS},m)
18794 +include ${src}/conf.mk
18795 +endif
18796 +-include ${src}/priv_def.mk
18797 +
18798 +# cf. include/linux/kernel.h
18799 +# enable pr_debug
18800 +ccflags-y += -DDEBUG
18801 +# sparse doesn't allow spaces
18802 +ccflags-y += -D'pr_fmt(fmt)=AUFS_NAME"\040%s:%d:%s[%d]:\040"fmt,__func__,__LINE__,current->comm,current->pid'
18803 +
18804 +obj-$(CONFIG_AUFS_FS) += aufs.o
18805 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
18806 +       wkq.o vfsub.o dcsub.o \
18807 +       cpup.o whout.o wbr_policy.o \
18808 +       dinfo.o dentry.o \
18809 +       dynop.o \
18810 +       finfo.o file.o f_op.o \
18811 +       dir.o vdir.o \
18812 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
18813 +       ioctl.o
18814 +
18815 +# all are boolean
18816 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
18817 +aufs-$(CONFIG_SYSFS) += sysfs.o
18818 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
18819 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
18820 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
18821 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
18822 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
18823 +aufs-$(CONFIG_AUFS_POLL) += poll.o
18824 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
18825 +aufs-$(CONFIG_AUFS_SP_IATTR) += f_op_sp.o
18826 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
18827 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
18828 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
18829 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
18830 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
18831 +++ linux/fs/aufs/module.c      2012-01-05 11:00:59.218835185 +0100
18832 @@ -0,0 +1,195 @@
18833 +/*
18834 + * Copyright (C) 2005-2011 Junjiro R. Okajima
18835 + *
18836 + * This program, aufs is free software; you can redistribute it and/or modify
18837 + * it under the terms of the GNU General Public License as published by
18838 + * the Free Software Foundation; either version 2 of the License, or
18839 + * (at your option) any later version.
18840 + *
18841 + * This program is distributed in the hope that it will be useful,
18842 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18843 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18844 + * GNU General Public License for more details.
18845 + *
18846 + * You should have received a copy of the GNU General Public License
18847 + * along with this program; if not, write to the Free Software
18848 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18849 + */
18850 +
18851 +/*
18852 + * module global variables and operations
18853 + */
18854 +
18855 +#include <linux/module.h>
18856 +#include <linux/seq_file.h>
18857 +#include "aufs.h"
18858 +
18859 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
18860 +{
18861 +       if (new_sz <= nused)
18862 +               return p;
18863 +
18864 +       p = krealloc(p, new_sz, gfp);
18865 +       if (p)
18866 +               memset(p + nused, 0, new_sz - nused);
18867 +       return p;
18868 +}
18869 +
18870 +/* ---------------------------------------------------------------------- */
18871 +
18872 +/*
18873 + * aufs caches
18874 + */
18875 +struct kmem_cache *au_cachep[AuCache_Last];
18876 +static int __init au_cache_init(void)
18877 +{
18878 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
18879 +       if (au_cachep[AuCache_DINFO])
18880 +               /* SLAB_DESTROY_BY_RCU */
18881 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
18882 +                                                       au_icntnr_init_once);
18883 +       if (au_cachep[AuCache_ICNTNR])
18884 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
18885 +                                                      au_fi_init_once);
18886 +       if (au_cachep[AuCache_FINFO])
18887 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
18888 +       if (au_cachep[AuCache_VDIR])
18889 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
18890 +       if (au_cachep[AuCache_DEHSTR])
18891 +               return 0;
18892 +
18893 +       return -ENOMEM;
18894 +}
18895 +
18896 +static void au_cache_fin(void)
18897 +{
18898 +       int i;
18899 +
18900 +       /* including AuCache_HNOTIFY */
18901 +       for (i = 0; i < AuCache_Last; i++)
18902 +               if (au_cachep[i]) {
18903 +                       kmem_cache_destroy(au_cachep[i]);
18904 +                       au_cachep[i] = NULL;
18905 +               }
18906 +}
18907 +
18908 +/* ---------------------------------------------------------------------- */
18909 +
18910 +int au_dir_roflags;
18911 +
18912 +#ifdef CONFIG_AUFS_SBILIST
18913 +/*
18914 + * iterate_supers_type() doesn't protect us from
18915 + * remounting (branch management)
18916 + */
18917 +struct au_splhead au_sbilist;
18918 +#endif
18919 +
18920 +struct lock_class_key au_lc_key[AuLcKey_Last];
18921 +
18922 +/*
18923 + * functions for module interface.
18924 + */
18925 +MODULE_LICENSE("GPL");
18926 +/* MODULE_LICENSE("GPL v2"); */
18927 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
18928 +MODULE_DESCRIPTION(AUFS_NAME
18929 +       " -- Advanced multi layered unification filesystem");
18930 +MODULE_VERSION(AUFS_VERSION);
18931 +
18932 +/* this module parameter has no meaning when SYSFS is disabled */
18933 +int sysaufs_brs = 1;
18934 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
18935 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
18936 +
18937 +/* ---------------------------------------------------------------------- */
18938 +
18939 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
18940 +
18941 +int au_seq_path(struct seq_file *seq, struct path *path)
18942 +{
18943 +       return seq_path(seq, path, au_esc_chars);
18944 +}
18945 +
18946 +/* ---------------------------------------------------------------------- */
18947 +
18948 +static int __init aufs_init(void)
18949 +{
18950 +       int err, i;
18951 +       char *p;
18952 +
18953 +       p = au_esc_chars;
18954 +       for (i = 1; i <= ' '; i++)
18955 +               *p++ = i;
18956 +       *p++ = '\\';
18957 +       *p++ = '\x7f';
18958 +       *p = 0;
18959 +
18960 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
18961 +
18962 +       au_sbilist_init();
18963 +       sysaufs_brs_init();
18964 +       au_debug_init();
18965 +       au_dy_init();
18966 +       err = sysaufs_init();
18967 +       if (unlikely(err))
18968 +               goto out;
18969 +       err = au_procfs_init();
18970 +       if (unlikely(err))
18971 +               goto out_sysaufs;
18972 +       err = au_wkq_init();
18973 +       if (unlikely(err))
18974 +               goto out_procfs;
18975 +       err = au_loopback_init();
18976 +       if (unlikely(err))
18977 +               goto out_wkq;
18978 +       err = au_hnotify_init();
18979 +       if (unlikely(err))
18980 +               goto out_loopback;
18981 +       err = au_sysrq_init();
18982 +       if (unlikely(err))
18983 +               goto out_hin;
18984 +       err = au_cache_init();
18985 +       if (unlikely(err))
18986 +               goto out_sysrq;
18987 +       err = register_filesystem(&aufs_fs_type);
18988 +       if (unlikely(err))
18989 +               goto out_cache;
18990 +       /* since we define pr_fmt, call printk directly */
18991 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
18992 +       goto out; /* success */
18993 +
18994 +out_cache:
18995 +       au_cache_fin();
18996 +out_sysrq:
18997 +       au_sysrq_fin();
18998 +out_hin:
18999 +       au_hnotify_fin();
19000 +out_loopback:
19001 +       au_loopback_fin();
19002 +out_wkq:
19003 +       au_wkq_fin();
19004 +out_procfs:
19005 +       au_procfs_fin();
19006 +out_sysaufs:
19007 +       sysaufs_fin();
19008 +       au_dy_fin();
19009 +out:
19010 +       return err;
19011 +}
19012 +
19013 +static void __exit aufs_exit(void)
19014 +{
19015 +       unregister_filesystem(&aufs_fs_type);
19016 +       au_cache_fin();
19017 +       au_sysrq_fin();
19018 +       au_hnotify_fin();
19019 +       au_loopback_fin();
19020 +       au_wkq_fin();
19021 +       au_procfs_fin();
19022 +       sysaufs_fin();
19023 +       au_dy_fin();
19024 +}
19025 +
19026 +module_init(aufs_init);
19027 +module_exit(aufs_exit);
19028 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
19029 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
19030 +++ linux/fs/aufs/module.h      2012-01-05 11:00:59.218835185 +0100
19031 @@ -0,0 +1,107 @@
19032 +/*
19033 + * Copyright (C) 2005-2011 Junjiro R. Okajima
19034 + *
19035 + * This program, aufs is free software; you can redistribute it and/or modify
19036 + * it under the terms of the GNU General Public License as published by
19037 + * the Free Software Foundation; either version 2 of the License, or
19038 + * (at your option) any later version.
19039 + *
19040 + * This program is distributed in the hope that it will be useful,
19041 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19042 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19043 + * GNU General Public License for more details.
19044 + *
19045 + * You should have received a copy of the GNU General Public License
19046 + * along with this program; if not, write to the Free Software
19047 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19048 + */
19049 +
19050 +/*
19051 + * module initialization and module-global
19052 + */
19053 +
19054 +#ifndef __AUFS_MODULE_H__
19055 +#define __AUFS_MODULE_H__
19056 +
19057 +#ifdef __KERNEL__
19058 +
19059 +#include <linux/slab.h>
19060 +
19061 +struct path;
19062 +struct seq_file;
19063 +
19064 +/* module parameters */
19065 +extern int sysaufs_brs;
19066 +
19067 +/* ---------------------------------------------------------------------- */
19068 +
19069 +extern int au_dir_roflags;
19070 +
19071 +enum {
19072 +       AuLcNonDir_FIINFO,
19073 +       AuLcNonDir_DIINFO,
19074 +       AuLcNonDir_IIINFO,
19075 +
19076 +       AuLcDir_FIINFO,
19077 +       AuLcDir_DIINFO,
19078 +       AuLcDir_IIINFO,
19079 +
19080 +       AuLcSymlink_DIINFO,
19081 +       AuLcSymlink_IIINFO,
19082 +
19083 +       AuLcKey_Last
19084 +};
19085 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
19086 +
19087 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
19088 +int au_seq_path(struct seq_file *seq, struct path *path);
19089 +
19090 +#ifdef CONFIG_PROC_FS
19091 +/* procfs.c */
19092 +int __init au_procfs_init(void);
19093 +void au_procfs_fin(void);
19094 +#else
19095 +AuStubInt0(au_procfs_init, void);
19096 +AuStubVoid(au_procfs_fin, void);
19097 +#endif
19098 +
19099 +/* ---------------------------------------------------------------------- */
19100 +
19101 +/* kmem cache */
19102 +enum {
19103 +       AuCache_DINFO,
19104 +       AuCache_ICNTNR,
19105 +       AuCache_FINFO,
19106 +       AuCache_VDIR,
19107 +       AuCache_DEHSTR,
19108 +#ifdef CONFIG_AUFS_HNOTIFY
19109 +       AuCache_HNOTIFY,
19110 +#endif
19111 +       AuCache_Last
19112 +};
19113 +
19114 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
19115 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
19116 +#define AuCacheCtor(type, ctor)        \
19117 +       kmem_cache_create(#type, sizeof(struct type), \
19118 +                         __alignof__(struct type), AuCacheFlags, ctor)
19119 +
19120 +extern struct kmem_cache *au_cachep[];
19121 +
19122 +#define AuCacheFuncs(name, index) \
19123 +static inline struct au_##name *au_cache_alloc_##name(void) \
19124 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
19125 +static inline void au_cache_free_##name(struct au_##name *p) \
19126 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
19127 +
19128 +AuCacheFuncs(dinfo, DINFO);
19129 +AuCacheFuncs(icntnr, ICNTNR);
19130 +AuCacheFuncs(finfo, FINFO);
19131 +AuCacheFuncs(vdir, VDIR);
19132 +AuCacheFuncs(vdir_dehstr, DEHSTR);
19133 +#ifdef CONFIG_AUFS_HNOTIFY
19134 +AuCacheFuncs(hnotify, HNOTIFY);
19135 +#endif
19136 +
19137 +#endif /* __KERNEL__ */
19138 +#endif /* __AUFS_MODULE_H__ */
19139 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
19140 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
19141 +++ linux/fs/aufs/opts.c        2012-01-05 11:00:59.218835185 +0100
19142 @@ -0,0 +1,1679 @@
19143 +/*
19144 + * Copyright (C) 2005-2011 Junjiro R. Okajima
19145 + *
19146 + * This program, aufs is free software; you can redistribute it and/or modify
19147 + * it under the terms of the GNU General Public License as published by
19148 + * the Free Software Foundation; either version 2 of the License, or
19149 + * (at your option) any later version.
19150 + *
19151 + * This program is distributed in the hope that it will be useful,
19152 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19153 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19154 + * GNU General Public License for more details.
19155 + *
19156 + * You should have received a copy of the GNU General Public License
19157 + * along with this program; if not, write to the Free Software
19158 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19159 + */
19160 +
19161 +/*
19162 + * mount options/flags
19163 + */
19164 +
19165 +#include <linux/file.h>
19166 +#include <linux/jiffies.h>
19167 +#include <linux/namei.h>
19168 +#include <linux/types.h> /* a distribution requires */
19169 +#include <linux/parser.h>
19170 +#include "aufs.h"
19171 +
19172 +/* ---------------------------------------------------------------------- */
19173 +
19174 +enum {
19175 +       Opt_br,
19176 +       Opt_add, Opt_del, Opt_mod, Opt_reorder, Opt_append, Opt_prepend,
19177 +       Opt_idel, Opt_imod, Opt_ireorder,
19178 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash, Opt_rendir,
19179 +       Opt_rdblk_def, Opt_rdhash_def,
19180 +       Opt_xino, Opt_zxino, Opt_noxino,
19181 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
19182 +       Opt_trunc_xino_path, Opt_itrunc_xino,
19183 +       Opt_trunc_xib, Opt_notrunc_xib,
19184 +       Opt_shwh, Opt_noshwh,
19185 +       Opt_plink, Opt_noplink, Opt_list_plink,
19186 +       Opt_udba,
19187 +       Opt_dio, Opt_nodio,
19188 +       /* Opt_lock, Opt_unlock, */
19189 +       Opt_cmd, Opt_cmd_args,
19190 +       Opt_diropq_a, Opt_diropq_w,
19191 +       Opt_warn_perm, Opt_nowarn_perm,
19192 +       Opt_wbr_copyup, Opt_wbr_create,
19193 +       Opt_refrof, Opt_norefrof,
19194 +       Opt_verbose, Opt_noverbose,
19195 +       Opt_sum, Opt_nosum, Opt_wsum,
19196 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
19197 +};
19198 +
19199 +static match_table_t options = {
19200 +       {Opt_br, "br=%s"},
19201 +       {Opt_br, "br:%s"},
19202 +
19203 +       {Opt_add, "add=%d:%s"},
19204 +       {Opt_add, "add:%d:%s"},
19205 +       {Opt_add, "ins=%d:%s"},
19206 +       {Opt_add, "ins:%d:%s"},
19207 +       {Opt_append, "append=%s"},
19208 +       {Opt_append, "append:%s"},
19209 +       {Opt_prepend, "prepend=%s"},
19210 +       {Opt_prepend, "prepend:%s"},
19211 +
19212 +       {Opt_del, "del=%s"},
19213 +       {Opt_del, "del:%s"},
19214 +       /* {Opt_idel, "idel:%d"}, */
19215 +       {Opt_mod, "mod=%s"},
19216 +       {Opt_mod, "mod:%s"},
19217 +       /* {Opt_imod, "imod:%d:%s"}, */
19218 +
19219 +       {Opt_dirwh, "dirwh=%d"},
19220 +
19221 +       {Opt_xino, "xino=%s"},
19222 +       {Opt_noxino, "noxino"},
19223 +       {Opt_trunc_xino, "trunc_xino"},
19224 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
19225 +       {Opt_notrunc_xino, "notrunc_xino"},
19226 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
19227 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
19228 +       /* {Opt_zxino, "zxino=%s"}, */
19229 +       {Opt_trunc_xib, "trunc_xib"},
19230 +       {Opt_notrunc_xib, "notrunc_xib"},
19231 +
19232 +#ifdef CONFIG_PROC_FS
19233 +       {Opt_plink, "plink"},
19234 +#else
19235 +       {Opt_ignore_silent, "plink"},
19236 +#endif
19237 +
19238 +       {Opt_noplink, "noplink"},
19239 +
19240 +#ifdef CONFIG_AUFS_DEBUG
19241 +       {Opt_list_plink, "list_plink"},
19242 +#endif
19243 +
19244 +       {Opt_udba, "udba=%s"},
19245 +
19246 +       {Opt_dio, "dio"},
19247 +       {Opt_nodio, "nodio"},
19248 +
19249 +       {Opt_diropq_a, "diropq=always"},
19250 +       {Opt_diropq_a, "diropq=a"},
19251 +       {Opt_diropq_w, "diropq=whiteouted"},
19252 +       {Opt_diropq_w, "diropq=w"},
19253 +
19254 +       {Opt_warn_perm, "warn_perm"},
19255 +       {Opt_nowarn_perm, "nowarn_perm"},
19256 +
19257 +       /* keep them temporary */
19258 +       {Opt_ignore_silent, "coo=%s"},
19259 +       {Opt_ignore_silent, "nodlgt"},
19260 +       {Opt_ignore_silent, "nodirperm1"},
19261 +       {Opt_ignore_silent, "clean_plink"},
19262 +
19263 +#ifdef CONFIG_AUFS_SHWH
19264 +       {Opt_shwh, "shwh"},
19265 +#endif
19266 +       {Opt_noshwh, "noshwh"},
19267 +
19268 +       {Opt_rendir, "rendir=%d"},
19269 +
19270 +       {Opt_refrof, "refrof"},
19271 +       {Opt_norefrof, "norefrof"},
19272 +
19273 +       {Opt_verbose, "verbose"},
19274 +       {Opt_verbose, "v"},
19275 +       {Opt_noverbose, "noverbose"},
19276 +       {Opt_noverbose, "quiet"},
19277 +       {Opt_noverbose, "q"},
19278 +       {Opt_noverbose, "silent"},
19279 +
19280 +       {Opt_sum, "sum"},
19281 +       {Opt_nosum, "nosum"},
19282 +       {Opt_wsum, "wsum"},
19283 +
19284 +       {Opt_rdcache, "rdcache=%d"},
19285 +       {Opt_rdblk, "rdblk=%d"},
19286 +       {Opt_rdblk_def, "rdblk=def"},
19287 +       {Opt_rdhash, "rdhash=%d"},
19288 +       {Opt_rdhash_def, "rdhash=def"},
19289 +
19290 +       {Opt_wbr_create, "create=%s"},
19291 +       {Opt_wbr_create, "create_policy=%s"},
19292 +       {Opt_wbr_copyup, "cpup=%s"},
19293 +       {Opt_wbr_copyup, "copyup=%s"},
19294 +       {Opt_wbr_copyup, "copyup_policy=%s"},
19295 +
19296 +       /* internal use for the scripts */
19297 +       {Opt_ignore_silent, "si=%s"},
19298 +
19299 +       {Opt_br, "dirs=%s"},
19300 +       {Opt_ignore, "debug=%d"},
19301 +       {Opt_ignore, "delete=whiteout"},
19302 +       {Opt_ignore, "delete=all"},
19303 +       {Opt_ignore, "imap=%s"},
19304 +
19305 +       /* temporary workaround, due to old mount(8)? */
19306 +       {Opt_ignore_silent, "relatime"},
19307 +
19308 +       {Opt_err, NULL}
19309 +};
19310 +
19311 +/* ---------------------------------------------------------------------- */
19312 +
19313 +static const char *au_parser_pattern(int val, struct match_token *token)
19314 +{
19315 +       while (token->pattern) {
19316 +               if (token->token == val)
19317 +                       return token->pattern;
19318 +               token++;
19319 +       }
19320 +       BUG();
19321 +       return "??";
19322 +}
19323 +
19324 +/* ---------------------------------------------------------------------- */
19325 +
19326 +static match_table_t brperm = {
19327 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
19328 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
19329 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
19330 +       {0, NULL}
19331 +};
19332 +
19333 +static match_table_t brrattr = {
19334 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
19335 +       {0, NULL}
19336 +};
19337 +
19338 +static match_table_t brwattr = {
19339 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
19340 +       {0, NULL}
19341 +};
19342 +
19343 +#define AuBrStr_LONGEST        AUFS_BRPERM_RW "+" AUFS_BRWATTR_NLWH
19344 +
19345 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
19346 +{
19347 +       int attr, v;
19348 +       char *p;
19349 +
19350 +       attr = 0;
19351 +       do {
19352 +               p = strchr(str, '+');
19353 +               if (p)
19354 +                       *p = 0;
19355 +               v = match_token(str, table, args);
19356 +               if (v)
19357 +                       attr |= v;
19358 +               else {
19359 +                       if (p)
19360 +                               *p = '+';
19361 +                       pr_warning("ignored branch attribute %s\n", str);
19362 +                       break;
19363 +               }
19364 +               if (p)
19365 +                       str = p + 1;
19366 +       } while (p);
19367 +
19368 +       return attr;
19369 +}
19370 +
19371 +static int noinline_for_stack br_perm_val(char *perm)
19372 +{
19373 +       int val;
19374 +       char *p;
19375 +       substring_t args[MAX_OPT_ARGS];
19376 +
19377 +       p = strchr(perm, '+');
19378 +       if (p)
19379 +               *p = 0;
19380 +       val = match_token(perm, brperm, args);
19381 +       if (!val) {
19382 +               if (p)
19383 +                       *p = '+';
19384 +               pr_warning("ignored branch permission %s\n", perm);
19385 +               val = AuBrPerm_RO;
19386 +               goto out;
19387 +       }
19388 +       if (!p)
19389 +               goto out;
19390 +
19391 +       switch (val) {
19392 +       case AuBrPerm_RO:
19393 +       case AuBrPerm_RR:
19394 +               val |= br_attr_val(p + 1, brrattr, args);
19395 +               break;
19396 +       case AuBrPerm_RW:
19397 +               val |= br_attr_val(p + 1, brwattr, args);
19398 +               break;
19399 +       }
19400 +
19401 +out:
19402 +       return val;
19403 +}
19404 +
19405 +/* Caller should free the return value */
19406 +char *au_optstr_br_perm(int brperm)
19407 +{
19408 +       char *p, a[sizeof(AuBrStr_LONGEST)];
19409 +       int sz;
19410 +
19411 +#define SetPerm(str) do {                      \
19412 +               sz = sizeof(str);               \
19413 +               memcpy(a, str, sz);             \
19414 +               p = a + sz - 1;                 \
19415 +       } while (0)
19416 +
19417 +#define AppendAttr(flag, str) do {                     \
19418 +               if (brperm & flag) {            \
19419 +                       sz = sizeof(str);       \
19420 +                       *p++ = '+';             \
19421 +                       memcpy(p, str, sz);     \
19422 +                       p += sz - 1;            \
19423 +               }                               \
19424 +       } while (0)
19425 +
19426 +       switch (brperm & AuBrPerm_Mask) {
19427 +       case AuBrPerm_RO:
19428 +               SetPerm(AUFS_BRPERM_RO);
19429 +               break;
19430 +       case AuBrPerm_RR:
19431 +               SetPerm(AUFS_BRPERM_RR);
19432 +               break;
19433 +       case AuBrPerm_RW:
19434 +               SetPerm(AUFS_BRPERM_RW);
19435 +               break;
19436 +       default:
19437 +               AuDebugOn(1);
19438 +       }
19439 +
19440 +       AppendAttr(AuBrRAttr_WH, AUFS_BRRATTR_WH);
19441 +       AppendAttr(AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH);
19442 +
19443 +       AuDebugOn(strlen(a) >= sizeof(a));
19444 +       return kstrdup(a, GFP_NOFS);
19445 +#undef SetPerm
19446 +#undef AppendAttr
19447 +}
19448 +
19449 +/* ---------------------------------------------------------------------- */
19450 +
19451 +static match_table_t udbalevel = {
19452 +       {AuOpt_UDBA_REVAL, "reval"},
19453 +       {AuOpt_UDBA_NONE, "none"},
19454 +#ifdef CONFIG_AUFS_HNOTIFY
19455 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
19456 +#ifdef CONFIG_AUFS_HFSNOTIFY
19457 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
19458 +#endif
19459 +#endif
19460 +       {-1, NULL}
19461 +};
19462 +
19463 +static int noinline_for_stack udba_val(char *str)
19464 +{
19465 +       substring_t args[MAX_OPT_ARGS];
19466 +
19467 +       return match_token(str, udbalevel, args);
19468 +}
19469 +
19470 +const char *au_optstr_udba(int udba)
19471 +{
19472 +       return au_parser_pattern(udba, (void *)udbalevel);
19473 +}
19474 +
19475 +/* ---------------------------------------------------------------------- */
19476 +
19477 +static match_table_t au_wbr_create_policy = {
19478 +       {AuWbrCreate_TDP, "tdp"},
19479 +       {AuWbrCreate_TDP, "top-down-parent"},
19480 +       {AuWbrCreate_RR, "rr"},
19481 +       {AuWbrCreate_RR, "round-robin"},
19482 +       {AuWbrCreate_MFS, "mfs"},
19483 +       {AuWbrCreate_MFS, "most-free-space"},
19484 +       {AuWbrCreate_MFSV, "mfs:%d"},
19485 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
19486 +
19487 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
19488 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
19489 +       {AuWbrCreate_PMFS, "pmfs"},
19490 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
19491 +
19492 +       {-1, NULL}
19493 +};
19494 +
19495 +/*
19496 + * cf. linux/lib/parser.c and cmdline.c
19497 + * gave up calling memparse() since it uses simple_strtoull() instead of
19498 + * kstrto...().
19499 + */
19500 +static int noinline_for_stack
19501 +au_match_ull(substring_t *s, unsigned long long *result)
19502 +{
19503 +       int err;
19504 +       unsigned int len;
19505 +       char a[32];
19506 +
19507 +       err = -ERANGE;
19508 +       len = s->to - s->from;
19509 +       if (len + 1 <= sizeof(a)) {
19510 +               memcpy(a, s->from, len);
19511 +               a[len] = '\0';
19512 +               err = kstrtoull(a, 0, result);
19513 +       }
19514 +       return err;
19515 +}
19516 +
19517 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
19518 +                           struct au_opt_wbr_create *create)
19519 +{
19520 +       int err;
19521 +       unsigned long long ull;
19522 +
19523 +       err = 0;
19524 +       if (!au_match_ull(arg, &ull))
19525 +               create->mfsrr_watermark = ull;
19526 +       else {
19527 +               pr_err("bad integer in %s\n", str);
19528 +               err = -EINVAL;
19529 +       }
19530 +
19531 +       return err;
19532 +}
19533 +
19534 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
19535 +                         struct au_opt_wbr_create *create)
19536 +{
19537 +       int n, err;
19538 +
19539 +       err = 0;
19540 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
19541 +               create->mfs_second = n;
19542 +       else {
19543 +               pr_err("bad integer in %s\n", str);
19544 +               err = -EINVAL;
19545 +       }
19546 +
19547 +       return err;
19548 +}
19549 +
19550 +static int noinline_for_stack
19551 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
19552 +{
19553 +       int err, e;
19554 +       substring_t args[MAX_OPT_ARGS];
19555 +
19556 +       err = match_token(str, au_wbr_create_policy, args);
19557 +       create->wbr_create = err;
19558 +       switch (err) {
19559 +       case AuWbrCreate_MFSRRV:
19560 +               e = au_wbr_mfs_wmark(&args[0], str, create);
19561 +               if (!e)
19562 +                       e = au_wbr_mfs_sec(&args[1], str, create);
19563 +               if (unlikely(e))
19564 +                       err = e;
19565 +               break;
19566 +       case AuWbrCreate_MFSRR:
19567 +               e = au_wbr_mfs_wmark(&args[0], str, create);
19568 +               if (unlikely(e)) {
19569 +                       err = e;
19570 +                       break;
19571 +               }
19572 +               /*FALLTHROUGH*/
19573 +       case AuWbrCreate_MFS:
19574 +       case AuWbrCreate_PMFS:
19575 +               create->mfs_second = AUFS_MFS_DEF_SEC;
19576 +               break;
19577 +       case AuWbrCreate_MFSV:
19578 +       case AuWbrCreate_PMFSV:
19579 +               e = au_wbr_mfs_sec(&args[0], str, create);
19580 +               if (unlikely(e))
19581 +                       err = e;
19582 +               break;
19583 +       }
19584 +
19585 +       return err;
19586 +}
19587 +
19588 +const char *au_optstr_wbr_create(int wbr_create)
19589 +{
19590 +       return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
19591 +}
19592 +
19593 +static match_table_t au_wbr_copyup_policy = {
19594 +       {AuWbrCopyup_TDP, "tdp"},
19595 +       {AuWbrCopyup_TDP, "top-down-parent"},
19596 +       {AuWbrCopyup_BUP, "bup"},
19597 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
19598 +       {AuWbrCopyup_BU, "bu"},
19599 +       {AuWbrCopyup_BU, "bottom-up"},
19600 +       {-1, NULL}
19601 +};
19602 +
19603 +static int noinline_for_stack au_wbr_copyup_val(char *str)
19604 +{
19605 +       substring_t args[MAX_OPT_ARGS];
19606 +
19607 +       return match_token(str, au_wbr_copyup_policy, args);
19608 +}
19609 +
19610 +const char *au_optstr_wbr_copyup(int wbr_copyup)
19611 +{
19612 +       return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
19613 +}
19614 +
19615 +/* ---------------------------------------------------------------------- */
19616 +
19617 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
19618 +
19619 +static void dump_opts(struct au_opts *opts)
19620 +{
19621 +#ifdef CONFIG_AUFS_DEBUG
19622 +       /* reduce stack space */
19623 +       union {
19624 +               struct au_opt_add *add;
19625 +               struct au_opt_del *del;
19626 +               struct au_opt_mod *mod;
19627 +               struct au_opt_xino *xino;
19628 +               struct au_opt_xino_itrunc *xino_itrunc;
19629 +               struct au_opt_wbr_create *create;
19630 +       } u;
19631 +       struct au_opt *opt;
19632 +
19633 +       opt = opts->opt;
19634 +       while (opt->type != Opt_tail) {
19635 +               switch (opt->type) {
19636 +               case Opt_add:
19637 +                       u.add = &opt->add;
19638 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
19639 +                                 u.add->bindex, u.add->pathname, u.add->perm,
19640 +                                 u.add->path.dentry);
19641 +                       break;
19642 +               case Opt_del:
19643 +               case Opt_idel:
19644 +                       u.del = &opt->del;
19645 +                       AuDbg("del {%s, %p}\n",
19646 +                             u.del->pathname, u.del->h_path.dentry);
19647 +                       break;
19648 +               case Opt_mod:
19649 +               case Opt_imod:
19650 +                       u.mod = &opt->mod;
19651 +                       AuDbg("mod {%s, 0x%x, %p}\n",
19652 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
19653 +                       break;
19654 +               case Opt_append:
19655 +                       u.add = &opt->add;
19656 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
19657 +                                 u.add->bindex, u.add->pathname, u.add->perm,
19658 +                                 u.add->path.dentry);
19659 +                       break;
19660 +               case Opt_prepend:
19661 +                       u.add = &opt->add;
19662 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
19663 +                                 u.add->bindex, u.add->pathname, u.add->perm,
19664 +                                 u.add->path.dentry);
19665 +                       break;
19666 +               case Opt_dirwh:
19667 +                       AuDbg("dirwh %d\n", opt->dirwh);
19668 +                       break;
19669 +               case Opt_rdcache:
19670 +                       AuDbg("rdcache %d\n", opt->rdcache);
19671 +                       break;
19672 +               case Opt_rdblk:
19673 +                       AuDbg("rdblk %u\n", opt->rdblk);
19674 +                       break;
19675 +               case Opt_rdblk_def:
19676 +                       AuDbg("rdblk_def\n");
19677 +                       break;
19678 +               case Opt_rdhash:
19679 +                       AuDbg("rdhash %u\n", opt->rdhash);
19680 +                       break;
19681 +               case Opt_rdhash_def:
19682 +                       AuDbg("rdhash_def\n");
19683 +                       break;
19684 +               case Opt_xino:
19685 +                       u.xino = &opt->xino;
19686 +                       AuDbg("xino {%s %.*s}\n",
19687 +                                 u.xino->path,
19688 +                                 AuDLNPair(u.xino->file->f_dentry));
19689 +                       break;
19690 +               case Opt_trunc_xino:
19691 +                       AuLabel(trunc_xino);
19692 +                       break;
19693 +               case Opt_notrunc_xino:
19694 +                       AuLabel(notrunc_xino);
19695 +                       break;
19696 +               case Opt_trunc_xino_path:
19697 +               case Opt_itrunc_xino:
19698 +                       u.xino_itrunc = &opt->xino_itrunc;
19699 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
19700 +                       break;
19701 +
19702 +               case Opt_noxino:
19703 +                       AuLabel(noxino);
19704 +                       break;
19705 +               case Opt_trunc_xib:
19706 +                       AuLabel(trunc_xib);
19707 +                       break;
19708 +               case Opt_notrunc_xib:
19709 +                       AuLabel(notrunc_xib);
19710 +                       break;
19711 +               case Opt_shwh:
19712 +                       AuLabel(shwh);
19713 +                       break;
19714 +               case Opt_noshwh:
19715 +                       AuLabel(noshwh);
19716 +                       break;
19717 +               case Opt_plink:
19718 +                       AuLabel(plink);
19719 +                       break;
19720 +               case Opt_noplink:
19721 +                       AuLabel(noplink);
19722 +                       break;
19723 +               case Opt_list_plink:
19724 +                       AuLabel(list_plink);
19725 +                       break;
19726 +               case Opt_udba:
19727 +                       AuDbg("udba %d, %s\n",
19728 +                                 opt->udba, au_optstr_udba(opt->udba));
19729 +                       break;
19730 +               case Opt_dio:
19731 +                       AuLabel(dio);
19732 +                       break;
19733 +               case Opt_nodio:
19734 +                       AuLabel(nodio);
19735 +                       break;
19736 +               case Opt_diropq_a:
19737 +                       AuLabel(diropq_a);
19738 +                       break;
19739 +               case Opt_diropq_w:
19740 +                       AuLabel(diropq_w);
19741 +                       break;
19742 +               case Opt_warn_perm:
19743 +                       AuLabel(warn_perm);
19744 +                       break;
19745 +               case Opt_nowarn_perm:
19746 +                       AuLabel(nowarn_perm);
19747 +                       break;
19748 +               case Opt_refrof:
19749 +                       AuLabel(refrof);
19750 +                       break;
19751 +               case Opt_norefrof:
19752 +                       AuLabel(norefrof);
19753 +                       break;
19754 +               case Opt_verbose:
19755 +                       AuLabel(verbose);
19756 +                       break;
19757 +               case Opt_noverbose:
19758 +                       AuLabel(noverbose);
19759 +                       break;
19760 +               case Opt_sum:
19761 +                       AuLabel(sum);
19762 +                       break;
19763 +               case Opt_nosum:
19764 +                       AuLabel(nosum);
19765 +                       break;
19766 +               case Opt_wsum:
19767 +                       AuLabel(wsum);
19768 +                       break;
19769 +               case Opt_wbr_create:
19770 +                       u.create = &opt->wbr_create;
19771 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
19772 +                                 au_optstr_wbr_create(u.create->wbr_create));
19773 +                       switch (u.create->wbr_create) {
19774 +                       case AuWbrCreate_MFSV:
19775 +                       case AuWbrCreate_PMFSV:
19776 +                               AuDbg("%d sec\n", u.create->mfs_second);
19777 +                               break;
19778 +                       case AuWbrCreate_MFSRR:
19779 +                               AuDbg("%llu watermark\n",
19780 +                                         u.create->mfsrr_watermark);
19781 +                               break;
19782 +                       case AuWbrCreate_MFSRRV:
19783 +                               AuDbg("%llu watermark, %d sec\n",
19784 +                                         u.create->mfsrr_watermark,
19785 +                                         u.create->mfs_second);
19786 +                               break;
19787 +                       }
19788 +                       break;
19789 +               case Opt_wbr_copyup:
19790 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
19791 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
19792 +                       break;
19793 +               default:
19794 +                       BUG();
19795 +               }
19796 +               opt++;
19797 +       }
19798 +#endif
19799 +}
19800 +
19801 +void au_opts_free(struct au_opts *opts)
19802 +{
19803 +       struct au_opt *opt;
19804 +
19805 +       opt = opts->opt;
19806 +       while (opt->type != Opt_tail) {
19807 +               switch (opt->type) {
19808 +               case Opt_add:
19809 +               case Opt_append:
19810 +               case Opt_prepend:
19811 +                       path_put(&opt->add.path);
19812 +                       break;
19813 +               case Opt_del:
19814 +               case Opt_idel:
19815 +                       path_put(&opt->del.h_path);
19816 +                       break;
19817 +               case Opt_mod:
19818 +               case Opt_imod:
19819 +                       dput(opt->mod.h_root);
19820 +                       break;
19821 +               case Opt_xino:
19822 +                       fput(opt->xino.file);
19823 +                       break;
19824 +               }
19825 +               opt++;
19826 +       }
19827 +}
19828 +
19829 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
19830 +                  aufs_bindex_t bindex)
19831 +{
19832 +       int err;
19833 +       struct au_opt_add *add = &opt->add;
19834 +       char *p;
19835 +
19836 +       add->bindex = bindex;
19837 +       add->perm = AuBrPerm_RO;
19838 +       add->pathname = opt_str;
19839 +       p = strchr(opt_str, '=');
19840 +       if (p) {
19841 +               *p++ = 0;
19842 +               if (*p)
19843 +                       add->perm = br_perm_val(p);
19844 +       }
19845 +
19846 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
19847 +       if (!err) {
19848 +               if (!p) {
19849 +                       add->perm = AuBrPerm_RO;
19850 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
19851 +                               add->perm = AuBrPerm_RR;
19852 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
19853 +                               add->perm = AuBrPerm_RW;
19854 +               }
19855 +               opt->type = Opt_add;
19856 +               goto out;
19857 +       }
19858 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
19859 +       err = -EINVAL;
19860 +
19861 +out:
19862 +       return err;
19863 +}
19864 +
19865 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
19866 +{
19867 +       int err;
19868 +
19869 +       del->pathname = args[0].from;
19870 +       AuDbg("del path %s\n", del->pathname);
19871 +
19872 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
19873 +       if (unlikely(err))
19874 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
19875 +
19876 +       return err;
19877 +}
19878 +
19879 +#if 0 /* reserved for future use */
19880 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
19881 +                             struct au_opt_del *del, substring_t args[])
19882 +{
19883 +       int err;
19884 +       struct dentry *root;
19885 +
19886 +       err = -EINVAL;
19887 +       root = sb->s_root;
19888 +       aufs_read_lock(root, AuLock_FLUSH);
19889 +       if (bindex < 0 || au_sbend(sb) < bindex) {
19890 +               pr_err("out of bounds, %d\n", bindex);
19891 +               goto out;
19892 +       }
19893 +
19894 +       err = 0;
19895 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
19896 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
19897 +
19898 +out:
19899 +       aufs_read_unlock(root, !AuLock_IR);
19900 +       return err;
19901 +}
19902 +#endif
19903 +
19904 +static int noinline_for_stack
19905 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
19906 +{
19907 +       int err;
19908 +       struct path path;
19909 +       char *p;
19910 +
19911 +       err = -EINVAL;
19912 +       mod->path = args[0].from;
19913 +       p = strchr(mod->path, '=');
19914 +       if (unlikely(!p)) {
19915 +               pr_err("no permssion %s\n", args[0].from);
19916 +               goto out;
19917 +       }
19918 +
19919 +       *p++ = 0;
19920 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
19921 +       if (unlikely(err)) {
19922 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
19923 +               goto out;
19924 +       }
19925 +
19926 +       mod->perm = br_perm_val(p);
19927 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
19928 +       mod->h_root = dget(path.dentry);
19929 +       path_put(&path);
19930 +
19931 +out:
19932 +       return err;
19933 +}
19934 +
19935 +#if 0 /* reserved for future use */
19936 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
19937 +                             struct au_opt_mod *mod, substring_t args[])
19938 +{
19939 +       int err;
19940 +       struct dentry *root;
19941 +
19942 +       err = -EINVAL;
19943 +       root = sb->s_root;
19944 +       aufs_read_lock(root, AuLock_FLUSH);
19945 +       if (bindex < 0 || au_sbend(sb) < bindex) {
19946 +               pr_err("out of bounds, %d\n", bindex);
19947 +               goto out;
19948 +       }
19949 +
19950 +       err = 0;
19951 +       mod->perm = br_perm_val(args[1].from);
19952 +       AuDbg("mod path %s, perm 0x%x, %s\n",
19953 +             mod->path, mod->perm, args[1].from);
19954 +       mod->h_root = dget(au_h_dptr(root, bindex));
19955 +
19956 +out:
19957 +       aufs_read_unlock(root, !AuLock_IR);
19958 +       return err;
19959 +}
19960 +#endif
19961 +
19962 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
19963 +                             substring_t args[])
19964 +{
19965 +       int err;
19966 +       struct file *file;
19967 +
19968 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
19969 +       err = PTR_ERR(file);
19970 +       if (IS_ERR(file))
19971 +               goto out;
19972 +
19973 +       err = -EINVAL;
19974 +       if (unlikely(file->f_dentry->d_sb == sb)) {
19975 +               fput(file);
19976 +               pr_err("%s must be outside\n", args[0].from);
19977 +               goto out;
19978 +       }
19979 +
19980 +       err = 0;
19981 +       xino->file = file;
19982 +       xino->path = args[0].from;
19983 +
19984 +out:
19985 +       return err;
19986 +}
19987 +
19988 +static int noinline_for_stack
19989 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
19990 +                              struct au_opt_xino_itrunc *xino_itrunc,
19991 +                              substring_t args[])
19992 +{
19993 +       int err;
19994 +       aufs_bindex_t bend, bindex;
19995 +       struct path path;
19996 +       struct dentry *root;
19997 +
19998 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
19999 +       if (unlikely(err)) {
20000 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
20001 +               goto out;
20002 +       }
20003 +
20004 +       xino_itrunc->bindex = -1;
20005 +       root = sb->s_root;
20006 +       aufs_read_lock(root, AuLock_FLUSH);
20007 +       bend = au_sbend(sb);
20008 +       for (bindex = 0; bindex <= bend; bindex++) {
20009 +               if (au_h_dptr(root, bindex) == path.dentry) {
20010 +                       xino_itrunc->bindex = bindex;
20011 +                       break;
20012 +               }
20013 +       }
20014 +       aufs_read_unlock(root, !AuLock_IR);
20015 +       path_put(&path);
20016 +
20017 +       if (unlikely(xino_itrunc->bindex < 0)) {
20018 +               pr_err("no such branch %s\n", args[0].from);
20019 +               err = -EINVAL;
20020 +       }
20021 +
20022 +out:
20023 +       return err;
20024 +}
20025 +
20026 +/* called without aufs lock */
20027 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
20028 +{
20029 +       int err, n, token;
20030 +       aufs_bindex_t bindex;
20031 +       unsigned char skipped;
20032 +       struct dentry *root;
20033 +       struct au_opt *opt, *opt_tail;
20034 +       char *opt_str;
20035 +       /* reduce the stack space */
20036 +       union {
20037 +               struct au_opt_xino_itrunc *xino_itrunc;
20038 +               struct au_opt_wbr_create *create;
20039 +       } u;
20040 +       struct {
20041 +               substring_t args[MAX_OPT_ARGS];
20042 +       } *a;
20043 +
20044 +       err = -ENOMEM;
20045 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20046 +       if (unlikely(!a))
20047 +               goto out;
20048 +
20049 +       root = sb->s_root;
20050 +       err = 0;
20051 +       bindex = 0;
20052 +       opt = opts->opt;
20053 +       opt_tail = opt + opts->max_opt - 1;
20054 +       opt->type = Opt_tail;
20055 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
20056 +               err = -EINVAL;
20057 +               skipped = 0;
20058 +               token = match_token(opt_str, options, a->args);
20059 +               switch (token) {
20060 +               case Opt_br:
20061 +                       err = 0;
20062 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
20063 +                              && *opt_str) {
20064 +                               err = opt_add(opt, opt_str, opts->sb_flags,
20065 +                                             bindex++);
20066 +                               if (unlikely(!err && ++opt > opt_tail)) {
20067 +                                       err = -E2BIG;
20068 +                                       break;
20069 +                               }
20070 +                               opt->type = Opt_tail;
20071 +                               skipped = 1;
20072 +                       }
20073 +                       break;
20074 +               case Opt_add:
20075 +                       if (unlikely(match_int(&a->args[0], &n))) {
20076 +                               pr_err("bad integer in %s\n", opt_str);
20077 +                               break;
20078 +                       }
20079 +                       bindex = n;
20080 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
20081 +                                     bindex);
20082 +                       if (!err)
20083 +                               opt->type = token;
20084 +                       break;
20085 +               case Opt_append:
20086 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
20087 +                                     /*dummy bindex*/1);
20088 +                       if (!err)
20089 +                               opt->type = token;
20090 +                       break;
20091 +               case Opt_prepend:
20092 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
20093 +                                     /*bindex*/0);
20094 +                       if (!err)
20095 +                               opt->type = token;
20096 +                       break;
20097 +               case Opt_del:
20098 +                       err = au_opts_parse_del(&opt->del, a->args);
20099 +                       if (!err)
20100 +                               opt->type = token;
20101 +                       break;
20102 +#if 0 /* reserved for future use */
20103 +               case Opt_idel:
20104 +                       del->pathname = "(indexed)";
20105 +                       if (unlikely(match_int(&args[0], &n))) {
20106 +                               pr_err("bad integer in %s\n", opt_str);
20107 +                               break;
20108 +                       }
20109 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
20110 +                       if (!err)
20111 +                               opt->type = token;
20112 +                       break;
20113 +#endif
20114 +               case Opt_mod:
20115 +                       err = au_opts_parse_mod(&opt->mod, a->args);
20116 +                       if (!err)
20117 +                               opt->type = token;
20118 +                       break;
20119 +#ifdef IMOD /* reserved for future use */
20120 +               case Opt_imod:
20121 +                       u.mod->path = "(indexed)";
20122 +                       if (unlikely(match_int(&a->args[0], &n))) {
20123 +                               pr_err("bad integer in %s\n", opt_str);
20124 +                               break;
20125 +                       }
20126 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
20127 +                       if (!err)
20128 +                               opt->type = token;
20129 +                       break;
20130 +#endif
20131 +               case Opt_xino:
20132 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
20133 +                       if (!err)
20134 +                               opt->type = token;
20135 +                       break;
20136 +
20137 +               case Opt_trunc_xino_path:
20138 +                       err = au_opts_parse_xino_itrunc_path
20139 +                               (sb, &opt->xino_itrunc, a->args);
20140 +                       if (!err)
20141 +                               opt->type = token;
20142 +                       break;
20143 +
20144 +               case Opt_itrunc_xino:
20145 +                       u.xino_itrunc = &opt->xino_itrunc;
20146 +                       if (unlikely(match_int(&a->args[0], &n))) {
20147 +                               pr_err("bad integer in %s\n", opt_str);
20148 +                               break;
20149 +                       }
20150 +                       u.xino_itrunc->bindex = n;
20151 +                       aufs_read_lock(root, AuLock_FLUSH);
20152 +                       if (n < 0 || au_sbend(sb) < n) {
20153 +                               pr_err("out of bounds, %d\n", n);
20154 +                               aufs_read_unlock(root, !AuLock_IR);
20155 +                               break;
20156 +                       }
20157 +                       aufs_read_unlock(root, !AuLock_IR);
20158 +                       err = 0;
20159 +                       opt->type = token;
20160 +                       break;
20161 +
20162 +               case Opt_dirwh:
20163 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
20164 +                               break;
20165 +                       err = 0;
20166 +                       opt->type = token;
20167 +                       break;
20168 +
20169 +               case Opt_rdcache:
20170 +                       if (unlikely(match_int(&a->args[0], &n))) {
20171 +                               pr_err("bad integer in %s\n", opt_str);
20172 +                               break;
20173 +                       }
20174 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
20175 +                               pr_err("rdcache must be smaller than %d\n",
20176 +                                      AUFS_RDCACHE_MAX);
20177 +                               break;
20178 +                       }
20179 +                       opt->rdcache = n;
20180 +                       err = 0;
20181 +                       opt->type = token;
20182 +                       break;
20183 +               case Opt_rdblk:
20184 +                       if (unlikely(match_int(&a->args[0], &n)
20185 +                                    || n < 0
20186 +                                    || n > KMALLOC_MAX_SIZE)) {
20187 +                               pr_err("bad integer in %s\n", opt_str);
20188 +                               break;
20189 +                       }
20190 +                       if (unlikely(n && n < NAME_MAX)) {
20191 +                               pr_err("rdblk must be larger than %d\n",
20192 +                                      NAME_MAX);
20193 +                               break;
20194 +                       }
20195 +                       opt->rdblk = n;
20196 +                       err = 0;
20197 +                       opt->type = token;
20198 +                       break;
20199 +               case Opt_rdhash:
20200 +                       if (unlikely(match_int(&a->args[0], &n)
20201 +                                    || n < 0
20202 +                                    || n * sizeof(struct hlist_head)
20203 +                                    > KMALLOC_MAX_SIZE)) {
20204 +                               pr_err("bad integer in %s\n", opt_str);
20205 +                               break;
20206 +                       }
20207 +                       opt->rdhash = n;
20208 +                       err = 0;
20209 +                       opt->type = token;
20210 +                       break;
20211 +
20212 +               case Opt_trunc_xino:
20213 +               case Opt_notrunc_xino:
20214 +               case Opt_noxino:
20215 +               case Opt_trunc_xib:
20216 +               case Opt_notrunc_xib:
20217 +               case Opt_shwh:
20218 +               case Opt_noshwh:
20219 +               case Opt_plink:
20220 +               case Opt_noplink:
20221 +               case Opt_list_plink:
20222 +               case Opt_dio:
20223 +               case Opt_nodio:
20224 +               case Opt_diropq_a:
20225 +               case Opt_diropq_w:
20226 +               case Opt_warn_perm:
20227 +               case Opt_nowarn_perm:
20228 +               case Opt_refrof:
20229 +               case Opt_norefrof:
20230 +               case Opt_verbose:
20231 +               case Opt_noverbose:
20232 +               case Opt_sum:
20233 +               case Opt_nosum:
20234 +               case Opt_wsum:
20235 +               case Opt_rdblk_def:
20236 +               case Opt_rdhash_def:
20237 +                       err = 0;
20238 +                       opt->type = token;
20239 +                       break;
20240 +
20241 +               case Opt_udba:
20242 +                       opt->udba = udba_val(a->args[0].from);
20243 +                       if (opt->udba >= 0) {
20244 +                               err = 0;
20245 +                               opt->type = token;
20246 +                       } else
20247 +                               pr_err("wrong value, %s\n", opt_str);
20248 +                       break;
20249 +
20250 +               case Opt_wbr_create:
20251 +                       u.create = &opt->wbr_create;
20252 +                       u.create->wbr_create
20253 +                               = au_wbr_create_val(a->args[0].from, u.create);
20254 +                       if (u.create->wbr_create >= 0) {
20255 +                               err = 0;
20256 +                               opt->type = token;
20257 +                       } else
20258 +                               pr_err("wrong value, %s\n", opt_str);
20259 +                       break;
20260 +               case Opt_wbr_copyup:
20261 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
20262 +                       if (opt->wbr_copyup >= 0) {
20263 +                               err = 0;
20264 +                               opt->type = token;
20265 +                       } else
20266 +                               pr_err("wrong value, %s\n", opt_str);
20267 +                       break;
20268 +
20269 +               case Opt_ignore:
20270 +                       pr_warning("ignored %s\n", opt_str);
20271 +                       /*FALLTHROUGH*/
20272 +               case Opt_ignore_silent:
20273 +                       skipped = 1;
20274 +                       err = 0;
20275 +                       break;
20276 +               case Opt_err:
20277 +                       pr_err("unknown option %s\n", opt_str);
20278 +                       break;
20279 +               }
20280 +
20281 +               if (!err && !skipped) {
20282 +                       if (unlikely(++opt > opt_tail)) {
20283 +                               err = -E2BIG;
20284 +                               opt--;
20285 +                               opt->type = Opt_tail;
20286 +                               break;
20287 +                       }
20288 +                       opt->type = Opt_tail;
20289 +               }
20290 +       }
20291 +
20292 +       kfree(a);
20293 +       dump_opts(opts);
20294 +       if (unlikely(err))
20295 +               au_opts_free(opts);
20296 +
20297 +out:
20298 +       return err;
20299 +}
20300 +
20301 +static int au_opt_wbr_create(struct super_block *sb,
20302 +                            struct au_opt_wbr_create *create)
20303 +{
20304 +       int err;
20305 +       struct au_sbinfo *sbinfo;
20306 +
20307 +       SiMustWriteLock(sb);
20308 +
20309 +       err = 1; /* handled */
20310 +       sbinfo = au_sbi(sb);
20311 +       if (sbinfo->si_wbr_create_ops->fin) {
20312 +               err = sbinfo->si_wbr_create_ops->fin(sb);
20313 +               if (!err)
20314 +                       err = 1;
20315 +       }
20316 +
20317 +       sbinfo->si_wbr_create = create->wbr_create;
20318 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
20319 +       switch (create->wbr_create) {
20320 +       case AuWbrCreate_MFSRRV:
20321 +       case AuWbrCreate_MFSRR:
20322 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
20323 +               /*FALLTHROUGH*/
20324 +       case AuWbrCreate_MFS:
20325 +       case AuWbrCreate_MFSV:
20326 +       case AuWbrCreate_PMFS:
20327 +       case AuWbrCreate_PMFSV:
20328 +               sbinfo->si_wbr_mfs.mfs_expire
20329 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
20330 +               break;
20331 +       }
20332 +
20333 +       if (sbinfo->si_wbr_create_ops->init)
20334 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
20335 +
20336 +       return err;
20337 +}
20338 +
20339 +/*
20340 + * returns,
20341 + * plus: processed without an error
20342 + * zero: unprocessed
20343 + */
20344 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
20345 +                        struct au_opts *opts)
20346 +{
20347 +       int err;
20348 +       struct au_sbinfo *sbinfo;
20349 +
20350 +       SiMustWriteLock(sb);
20351 +
20352 +       err = 1; /* handled */
20353 +       sbinfo = au_sbi(sb);
20354 +       switch (opt->type) {
20355 +       case Opt_udba:
20356 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
20357 +               sbinfo->si_mntflags |= opt->udba;
20358 +               opts->given_udba |= opt->udba;
20359 +               break;
20360 +
20361 +       case Opt_plink:
20362 +               au_opt_set(sbinfo->si_mntflags, PLINK);
20363 +               break;
20364 +       case Opt_noplink:
20365 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
20366 +                       au_plink_put(sb, /*verbose*/1);
20367 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
20368 +               break;
20369 +       case Opt_list_plink:
20370 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
20371 +                       au_plink_list(sb);
20372 +               break;
20373 +
20374 +       case Opt_dio:
20375 +               au_opt_set(sbinfo->si_mntflags, DIO);
20376 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
20377 +               break;
20378 +       case Opt_nodio:
20379 +               au_opt_clr(sbinfo->si_mntflags, DIO);
20380 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
20381 +               break;
20382 +
20383 +       case Opt_diropq_a:
20384 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
20385 +               break;
20386 +       case Opt_diropq_w:
20387 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
20388 +               break;
20389 +
20390 +       case Opt_warn_perm:
20391 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
20392 +               break;
20393 +       case Opt_nowarn_perm:
20394 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
20395 +               break;
20396 +
20397 +       case Opt_refrof:
20398 +               au_opt_set(sbinfo->si_mntflags, REFROF);
20399 +               break;
20400 +       case Opt_norefrof:
20401 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
20402 +               break;
20403 +
20404 +       case Opt_verbose:
20405 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
20406 +               break;
20407 +       case Opt_noverbose:
20408 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
20409 +               break;
20410 +
20411 +       case Opt_sum:
20412 +               au_opt_set(sbinfo->si_mntflags, SUM);
20413 +               break;
20414 +       case Opt_wsum:
20415 +               au_opt_clr(sbinfo->si_mntflags, SUM);
20416 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
20417 +       case Opt_nosum:
20418 +               au_opt_clr(sbinfo->si_mntflags, SUM);
20419 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
20420 +               break;
20421 +
20422 +       case Opt_wbr_create:
20423 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
20424 +               break;
20425 +       case Opt_wbr_copyup:
20426 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
20427 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
20428 +               break;
20429 +
20430 +       case Opt_dirwh:
20431 +               sbinfo->si_dirwh = opt->dirwh;
20432 +               break;
20433 +
20434 +       case Opt_rdcache:
20435 +               sbinfo->si_rdcache
20436 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
20437 +               break;
20438 +       case Opt_rdblk:
20439 +               sbinfo->si_rdblk = opt->rdblk;
20440 +               break;
20441 +       case Opt_rdblk_def:
20442 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
20443 +               break;
20444 +       case Opt_rdhash:
20445 +               sbinfo->si_rdhash = opt->rdhash;
20446 +               break;
20447 +       case Opt_rdhash_def:
20448 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
20449 +               break;
20450 +
20451 +       case Opt_shwh:
20452 +               au_opt_set(sbinfo->si_mntflags, SHWH);
20453 +               break;
20454 +       case Opt_noshwh:
20455 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
20456 +               break;
20457 +
20458 +       case Opt_trunc_xino:
20459 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
20460 +               break;
20461 +       case Opt_notrunc_xino:
20462 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
20463 +               break;
20464 +
20465 +       case Opt_trunc_xino_path:
20466 +       case Opt_itrunc_xino:
20467 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
20468 +               if (!err)
20469 +                       err = 1;
20470 +               break;
20471 +
20472 +       case Opt_trunc_xib:
20473 +               au_fset_opts(opts->flags, TRUNC_XIB);
20474 +               break;
20475 +       case Opt_notrunc_xib:
20476 +               au_fclr_opts(opts->flags, TRUNC_XIB);
20477 +               break;
20478 +
20479 +       default:
20480 +               err = 0;
20481 +               break;
20482 +       }
20483 +
20484 +       return err;
20485 +}
20486 +
20487 +/*
20488 + * returns tri-state.
20489 + * plus: processed without an error
20490 + * zero: unprocessed
20491 + * minus: error
20492 + */
20493 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
20494 +                    struct au_opts *opts)
20495 +{
20496 +       int err, do_refresh;
20497 +
20498 +       err = 0;
20499 +       switch (opt->type) {
20500 +       case Opt_append:
20501 +               opt->add.bindex = au_sbend(sb) + 1;
20502 +               if (opt->add.bindex < 0)
20503 +                       opt->add.bindex = 0;
20504 +               goto add;
20505 +       case Opt_prepend:
20506 +               opt->add.bindex = 0;
20507 +       add:
20508 +       case Opt_add:
20509 +               err = au_br_add(sb, &opt->add,
20510 +                               au_ftest_opts(opts->flags, REMOUNT));
20511 +               if (!err) {
20512 +                       err = 1;
20513 +                       au_fset_opts(opts->flags, REFRESH);
20514 +               }
20515 +               break;
20516 +
20517 +       case Opt_del:
20518 +       case Opt_idel:
20519 +               err = au_br_del(sb, &opt->del,
20520 +                               au_ftest_opts(opts->flags, REMOUNT));
20521 +               if (!err) {
20522 +                       err = 1;
20523 +                       au_fset_opts(opts->flags, TRUNC_XIB);
20524 +                       au_fset_opts(opts->flags, REFRESH);
20525 +               }
20526 +               break;
20527 +
20528 +       case Opt_mod:
20529 +       case Opt_imod:
20530 +               err = au_br_mod(sb, &opt->mod,
20531 +                               au_ftest_opts(opts->flags, REMOUNT),
20532 +                               &do_refresh);
20533 +               if (!err) {
20534 +                       err = 1;
20535 +                       if (do_refresh)
20536 +                               au_fset_opts(opts->flags, REFRESH);
20537 +               }
20538 +               break;
20539 +       }
20540 +
20541 +       return err;
20542 +}
20543 +
20544 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
20545 +                      struct au_opt_xino **opt_xino,
20546 +                      struct au_opts *opts)
20547 +{
20548 +       int err;
20549 +       aufs_bindex_t bend, bindex;
20550 +       struct dentry *root, *parent, *h_root;
20551 +
20552 +       err = 0;
20553 +       switch (opt->type) {
20554 +       case Opt_xino:
20555 +               err = au_xino_set(sb, &opt->xino,
20556 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
20557 +               if (unlikely(err))
20558 +                       break;
20559 +
20560 +               *opt_xino = &opt->xino;
20561 +               au_xino_brid_set(sb, -1);
20562 +
20563 +               /* safe d_parent access */
20564 +               parent = opt->xino.file->f_dentry->d_parent;
20565 +               root = sb->s_root;
20566 +               bend = au_sbend(sb);
20567 +               for (bindex = 0; bindex <= bend; bindex++) {
20568 +                       h_root = au_h_dptr(root, bindex);
20569 +                       if (h_root == parent) {
20570 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
20571 +                               break;
20572 +                       }
20573 +               }
20574 +               break;
20575 +
20576 +       case Opt_noxino:
20577 +               au_xino_clr(sb);
20578 +               au_xino_brid_set(sb, -1);
20579 +               *opt_xino = (void *)-1;
20580 +               break;
20581 +       }
20582 +
20583 +       return err;
20584 +}
20585 +
20586 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
20587 +                  unsigned int pending)
20588 +{
20589 +       int err;
20590 +       aufs_bindex_t bindex, bend;
20591 +       unsigned char do_plink, skip, do_free;
20592 +       struct au_branch *br;
20593 +       struct au_wbr *wbr;
20594 +       struct dentry *root;
20595 +       struct inode *dir, *h_dir;
20596 +       struct au_sbinfo *sbinfo;
20597 +       struct au_hinode *hdir;
20598 +
20599 +       SiMustAnyLock(sb);
20600 +
20601 +       sbinfo = au_sbi(sb);
20602 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
20603 +
20604 +       if (!(sb_flags & MS_RDONLY)) {
20605 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
20606 +                       pr_warning("first branch should be rw\n");
20607 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
20608 +                       pr_warning("shwh should be used with ro\n");
20609 +       }
20610 +
20611 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
20612 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
20613 +               pr_warning("udba=*notify requires xino\n");
20614 +
20615 +       err = 0;
20616 +       root = sb->s_root;
20617 +       dir = root->d_inode;
20618 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
20619 +       bend = au_sbend(sb);
20620 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
20621 +               skip = 0;
20622 +               h_dir = au_h_iptr(dir, bindex);
20623 +               br = au_sbr(sb, bindex);
20624 +               do_free = 0;
20625 +
20626 +               wbr = br->br_wbr;
20627 +               if (wbr)
20628 +                       wbr_wh_read_lock(wbr);
20629 +
20630 +               if (!au_br_writable(br->br_perm)) {
20631 +                       do_free = !!wbr;
20632 +                       skip = (!wbr
20633 +                               || (!wbr->wbr_whbase
20634 +                                   && !wbr->wbr_plink
20635 +                                   && !wbr->wbr_orph));
20636 +               } else if (!au_br_wh_linkable(br->br_perm)) {
20637 +                       /* skip = (!br->br_whbase && !br->br_orph); */
20638 +                       skip = (!wbr || !wbr->wbr_whbase);
20639 +                       if (skip && wbr) {
20640 +                               if (do_plink)
20641 +                                       skip = !!wbr->wbr_plink;
20642 +                               else
20643 +                                       skip = !wbr->wbr_plink;
20644 +                       }
20645 +               } else {
20646 +                       /* skip = (br->br_whbase && br->br_ohph); */
20647 +                       skip = (wbr && wbr->wbr_whbase);
20648 +                       if (skip) {
20649 +                               if (do_plink)
20650 +                                       skip = !!wbr->wbr_plink;
20651 +                               else
20652 +                                       skip = !wbr->wbr_plink;
20653 +                       }
20654 +               }
20655 +               if (wbr)
20656 +                       wbr_wh_read_unlock(wbr);
20657 +
20658 +               if (skip)
20659 +                       continue;
20660 +
20661 +               hdir = au_hi(dir, bindex);
20662 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
20663 +               if (wbr)
20664 +                       wbr_wh_write_lock(wbr);
20665 +               err = au_wh_init(au_h_dptr(root, bindex), br, sb);
20666 +               if (wbr)
20667 +                       wbr_wh_write_unlock(wbr);
20668 +               au_hn_imtx_unlock(hdir);
20669 +
20670 +               if (!err && do_free) {
20671 +                       kfree(wbr);
20672 +                       br->br_wbr = NULL;
20673 +               }
20674 +       }
20675 +
20676 +       return err;
20677 +}
20678 +
20679 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
20680 +{
20681 +       int err;
20682 +       unsigned int tmp;
20683 +       aufs_bindex_t bindex, bend;
20684 +       struct au_opt *opt;
20685 +       struct au_opt_xino *opt_xino, xino;
20686 +       struct au_sbinfo *sbinfo;
20687 +       struct au_branch *br;
20688 +
20689 +       SiMustWriteLock(sb);
20690 +
20691 +       err = 0;
20692 +       opt_xino = NULL;
20693 +       opt = opts->opt;
20694 +       while (err >= 0 && opt->type != Opt_tail)
20695 +               err = au_opt_simple(sb, opt++, opts);
20696 +       if (err > 0)
20697 +               err = 0;
20698 +       else if (unlikely(err < 0))
20699 +               goto out;
20700 +
20701 +       /* disable xino and udba temporary */
20702 +       sbinfo = au_sbi(sb);
20703 +       tmp = sbinfo->si_mntflags;
20704 +       au_opt_clr(sbinfo->si_mntflags, XINO);
20705 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
20706 +
20707 +       opt = opts->opt;
20708 +       while (err >= 0 && opt->type != Opt_tail)
20709 +               err = au_opt_br(sb, opt++, opts);
20710 +       if (err > 0)
20711 +               err = 0;
20712 +       else if (unlikely(err < 0))
20713 +               goto out;
20714 +
20715 +       bend = au_sbend(sb);
20716 +       if (unlikely(bend < 0)) {
20717 +               err = -EINVAL;
20718 +               pr_err("no branches\n");
20719 +               goto out;
20720 +       }
20721 +
20722 +       if (au_opt_test(tmp, XINO))
20723 +               au_opt_set(sbinfo->si_mntflags, XINO);
20724 +       opt = opts->opt;
20725 +       while (!err && opt->type != Opt_tail)
20726 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
20727 +       if (unlikely(err))
20728 +               goto out;
20729 +
20730 +       err = au_opts_verify(sb, sb->s_flags, tmp);
20731 +       if (unlikely(err))
20732 +               goto out;
20733 +
20734 +       /* restore xino */
20735 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
20736 +               xino.file = au_xino_def(sb);
20737 +               err = PTR_ERR(xino.file);
20738 +               if (IS_ERR(xino.file))
20739 +                       goto out;
20740 +
20741 +               err = au_xino_set(sb, &xino, /*remount*/0);
20742 +               fput(xino.file);
20743 +               if (unlikely(err))
20744 +                       goto out;
20745 +       }
20746 +
20747 +       /* restore udba */
20748 +       tmp &= AuOptMask_UDBA;
20749 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
20750 +       sbinfo->si_mntflags |= tmp;
20751 +       bend = au_sbend(sb);
20752 +       for (bindex = 0; bindex <= bend; bindex++) {
20753 +               br = au_sbr(sb, bindex);
20754 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
20755 +               if (unlikely(err))
20756 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
20757 +                               bindex, err);
20758 +               /* go on even if err */
20759 +       }
20760 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
20761 +               struct inode *dir = sb->s_root->d_inode;
20762 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
20763 +       }
20764 +
20765 +out:
20766 +       return err;
20767 +}
20768 +
20769 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
20770 +{
20771 +       int err, rerr;
20772 +       struct inode *dir;
20773 +       struct au_opt_xino *opt_xino;
20774 +       struct au_opt *opt;
20775 +       struct au_sbinfo *sbinfo;
20776 +
20777 +       SiMustWriteLock(sb);
20778 +
20779 +       dir = sb->s_root->d_inode;
20780 +       sbinfo = au_sbi(sb);
20781 +       err = 0;
20782 +       opt_xino = NULL;
20783 +       opt = opts->opt;
20784 +       while (err >= 0 && opt->type != Opt_tail) {
20785 +               err = au_opt_simple(sb, opt, opts);
20786 +               if (!err)
20787 +                       err = au_opt_br(sb, opt, opts);
20788 +               if (!err)
20789 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
20790 +               opt++;
20791 +       }
20792 +       if (err > 0)
20793 +               err = 0;
20794 +       AuTraceErr(err);
20795 +       /* go on even err */
20796 +
20797 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
20798 +       if (unlikely(rerr && !err))
20799 +               err = rerr;
20800 +
20801 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
20802 +               rerr = au_xib_trunc(sb);
20803 +               if (unlikely(rerr && !err))
20804 +                       err = rerr;
20805 +       }
20806 +
20807 +       /* will be handled by the caller */
20808 +       if (!au_ftest_opts(opts->flags, REFRESH)
20809 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
20810 +               au_fset_opts(opts->flags, REFRESH);
20811 +
20812 +       AuDbg("status 0x%x\n", opts->flags);
20813 +       return err;
20814 +}
20815 +
20816 +/* ---------------------------------------------------------------------- */
20817 +
20818 +unsigned int au_opt_udba(struct super_block *sb)
20819 +{
20820 +       return au_mntflags(sb) & AuOptMask_UDBA;
20821 +}
20822 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
20823 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
20824 +++ linux/fs/aufs/opts.h        2011-10-24 20:51:51.580466925 +0200
20825 @@ -0,0 +1,210 @@
20826 +/*
20827 + * Copyright (C) 2005-2011 Junjiro R. Okajima
20828 + *
20829 + * This program, aufs is free software; you can redistribute it and/or modify
20830 + * it under the terms of the GNU General Public License as published by
20831 + * the Free Software Foundation; either version 2 of the License, or
20832 + * (at your option) any later version.
20833 + *
20834 + * This program is distributed in the hope that it will be useful,
20835 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20836 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20837 + * GNU General Public License for more details.
20838 + *
20839 + * You should have received a copy of the GNU General Public License
20840 + * along with this program; if not, write to the Free Software
20841 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20842 + */
20843 +
20844 +/*
20845 + * mount options/flags
20846 + */
20847 +
20848 +#ifndef __AUFS_OPTS_H__
20849 +#define __AUFS_OPTS_H__
20850 +
20851 +#ifdef __KERNEL__
20852 +
20853 +#include <linux/path.h>
20854 +#include <linux/aufs_type.h>
20855 +
20856 +struct file;
20857 +struct super_block;
20858 +
20859 +/* ---------------------------------------------------------------------- */
20860 +
20861 +/* mount flags */
20862 +#define AuOpt_XINO             1               /* external inode number bitmap
20863 +                                                  and translation table */
20864 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
20865 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
20866 +#define AuOpt_UDBA_REVAL       (1 << 3)
20867 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
20868 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
20869 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
20870 +#define AuOpt_DIRPERM1         (1 << 7)        /* unimplemented */
20871 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
20872 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
20873 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
20874 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
20875 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
20876 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
20877 +#define AuOpt_DIO              (1 << 14)       /* direct io */
20878 +
20879 +#ifndef CONFIG_AUFS_HNOTIFY
20880 +#undef AuOpt_UDBA_HNOTIFY
20881 +#define AuOpt_UDBA_HNOTIFY     0
20882 +#endif
20883 +#ifndef CONFIG_AUFS_SHWH
20884 +#undef AuOpt_SHWH
20885 +#define AuOpt_SHWH             0
20886 +#endif
20887 +
20888 +#define AuOpt_Def      (AuOpt_XINO \
20889 +                        | AuOpt_UDBA_REVAL \
20890 +                        | AuOpt_PLINK \
20891 +                        /* | AuOpt_DIRPERM1 */ \
20892 +                        | AuOpt_WARN_PERM)
20893 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
20894 +                        | AuOpt_UDBA_REVAL \
20895 +                        | AuOpt_UDBA_HNOTIFY)
20896 +
20897 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
20898 +#define au_opt_set(flags, name) do { \
20899 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
20900 +       ((flags) |= AuOpt_##name); \
20901 +} while (0)
20902 +#define au_opt_set_udba(flags, name) do { \
20903 +       (flags) &= ~AuOptMask_UDBA; \
20904 +       ((flags) |= AuOpt_##name); \
20905 +} while (0)
20906 +#define au_opt_clr(flags, name) do { \
20907 +       ((flags) &= ~AuOpt_##name); \
20908 +} while (0)
20909 +
20910 +static inline unsigned int au_opts_plink(unsigned int mntflags)
20911 +{
20912 +#ifdef CONFIG_PROC_FS
20913 +       return mntflags;
20914 +#else
20915 +       return mntflags & ~AuOpt_PLINK;
20916 +#endif
20917 +}
20918 +
20919 +/* ---------------------------------------------------------------------- */
20920 +
20921 +/* policies to select one among multiple writable branches */
20922 +enum {
20923 +       AuWbrCreate_TDP,        /* top down parent */
20924 +       AuWbrCreate_RR,         /* round robin */
20925 +       AuWbrCreate_MFS,        /* most free space */
20926 +       AuWbrCreate_MFSV,       /* mfs with seconds */
20927 +       AuWbrCreate_MFSRR,      /* mfs then rr */
20928 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
20929 +       AuWbrCreate_PMFS,       /* parent and mfs */
20930 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
20931 +
20932 +       AuWbrCreate_Def = AuWbrCreate_TDP
20933 +};
20934 +
20935 +enum {
20936 +       AuWbrCopyup_TDP,        /* top down parent */
20937 +       AuWbrCopyup_BUP,        /* bottom up parent */
20938 +       AuWbrCopyup_BU,         /* bottom up */
20939 +
20940 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
20941 +};
20942 +
20943 +/* ---------------------------------------------------------------------- */
20944 +
20945 +struct au_opt_add {
20946 +       aufs_bindex_t   bindex;
20947 +       char            *pathname;
20948 +       int             perm;
20949 +       struct path     path;
20950 +};
20951 +
20952 +struct au_opt_del {
20953 +       char            *pathname;
20954 +       struct path     h_path;
20955 +};
20956 +
20957 +struct au_opt_mod {
20958 +       char            *path;
20959 +       int             perm;
20960 +       struct dentry   *h_root;
20961 +};
20962 +
20963 +struct au_opt_xino {
20964 +       char            *path;
20965 +       struct file     *file;
20966 +};
20967 +
20968 +struct au_opt_xino_itrunc {
20969 +       aufs_bindex_t   bindex;
20970 +};
20971 +
20972 +struct au_opt_wbr_create {
20973 +       int                     wbr_create;
20974 +       int                     mfs_second;
20975 +       unsigned long long      mfsrr_watermark;
20976 +};
20977 +
20978 +struct au_opt {
20979 +       int type;
20980 +       union {
20981 +               struct au_opt_xino      xino;
20982 +               struct au_opt_xino_itrunc xino_itrunc;
20983 +               struct au_opt_add       add;
20984 +               struct au_opt_del       del;
20985 +               struct au_opt_mod       mod;
20986 +               int                     dirwh;
20987 +               int                     rdcache;
20988 +               unsigned int            rdblk;
20989 +               unsigned int            rdhash;
20990 +               int                     udba;
20991 +               struct au_opt_wbr_create wbr_create;
20992 +               int                     wbr_copyup;
20993 +       };
20994 +};
20995 +
20996 +/* opts flags */
20997 +#define AuOpts_REMOUNT         1
20998 +#define AuOpts_REFRESH         (1 << 1)
20999 +#define AuOpts_TRUNC_XIB       (1 << 2)
21000 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
21001 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
21002 +#define au_fset_opts(flags, name) \
21003 +       do { (flags) |= AuOpts_##name; } while (0)
21004 +#define au_fclr_opts(flags, name) \
21005 +       do { (flags) &= ~AuOpts_##name; } while (0)
21006 +
21007 +struct au_opts {
21008 +       struct au_opt   *opt;
21009 +       int             max_opt;
21010 +
21011 +       unsigned int    given_udba;
21012 +       unsigned int    flags;
21013 +       unsigned long   sb_flags;
21014 +};
21015 +
21016 +/* ---------------------------------------------------------------------- */
21017 +
21018 +char *au_optstr_br_perm(int brperm);
21019 +const char *au_optstr_udba(int udba);
21020 +const char *au_optstr_wbr_copyup(int wbr_copyup);
21021 +const char *au_optstr_wbr_create(int wbr_create);
21022 +
21023 +void au_opts_free(struct au_opts *opts);
21024 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
21025 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
21026 +                  unsigned int pending);
21027 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
21028 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
21029 +
21030 +unsigned int au_opt_udba(struct super_block *sb);
21031 +
21032 +/* ---------------------------------------------------------------------- */
21033 +
21034 +#endif /* __KERNEL__ */
21035 +#endif /* __AUFS_OPTS_H__ */
21036 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
21037 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
21038 +++ linux/fs/aufs/plink.c       2011-08-24 13:30:24.734646739 +0200
21039 @@ -0,0 +1,515 @@
21040 +/*
21041 + * Copyright (C) 2005-2011 Junjiro R. Okajima
21042 + *
21043 + * This program, aufs is free software; you can redistribute it and/or modify
21044 + * it under the terms of the GNU General Public License as published by
21045 + * the Free Software Foundation; either version 2 of the License, or
21046 + * (at your option) any later version.
21047 + *
21048 + * This program is distributed in the hope that it will be useful,
21049 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21050 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21051 + * GNU General Public License for more details.
21052 + *
21053 + * You should have received a copy of the GNU General Public License
21054 + * along with this program; if not, write to the Free Software
21055 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21056 + */
21057 +
21058 +/*
21059 + * pseudo-link
21060 + */
21061 +
21062 +#include "aufs.h"
21063 +
21064 +/*
21065 + * the pseudo-link maintenance mode.
21066 + * during a user process maintains the pseudo-links,
21067 + * prohibit adding a new plink and branch manipulation.
21068 + *
21069 + * Flags
21070 + * NOPLM:
21071 + *     For entry functions which will handle plink, and i_mutex is already held
21072 + *     in VFS.
21073 + *     They cannot wait and should return an error at once.
21074 + *     Callers has to check the error.
21075 + * NOPLMW:
21076 + *     For entry functions which will handle plink, but i_mutex is not held
21077 + *     in VFS.
21078 + *     They can wait the plink maintenance mode to finish.
21079 + *
21080 + * They behave like F_SETLK and F_SETLKW.
21081 + * If the caller never handle plink, then both flags are unnecessary.
21082 + */
21083 +
21084 +int au_plink_maint(struct super_block *sb, int flags)
21085 +{
21086 +       int err;
21087 +       pid_t pid, ppid;
21088 +       struct au_sbinfo *sbi;
21089 +
21090 +       SiMustAnyLock(sb);
21091 +
21092 +       err = 0;
21093 +       if (!au_opt_test(au_mntflags(sb), PLINK))
21094 +               goto out;
21095 +
21096 +       sbi = au_sbi(sb);
21097 +       pid = sbi->si_plink_maint_pid;
21098 +       if (!pid || pid == current->pid)
21099 +               goto out;
21100 +
21101 +       /* todo: it highly depends upon /sbin/mount.aufs */
21102 +       rcu_read_lock();
21103 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
21104 +       rcu_read_unlock();
21105 +       if (pid == ppid)
21106 +               goto out;
21107 +
21108 +       if (au_ftest_lock(flags, NOPLMW)) {
21109 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
21110 +               /* AuDebugOn(!lockdep_depth(current)); */
21111 +               while (sbi->si_plink_maint_pid) {
21112 +                       si_read_unlock(sb);
21113 +                       /* gave up wake_up_bit() */
21114 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
21115 +
21116 +                       if (au_ftest_lock(flags, FLUSH))
21117 +                               au_nwt_flush(&sbi->si_nowait);
21118 +                       si_noflush_read_lock(sb);
21119 +               }
21120 +       } else if (au_ftest_lock(flags, NOPLM)) {
21121 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
21122 +               err = -EAGAIN;
21123 +       }
21124 +
21125 +out:
21126 +       return err;
21127 +}
21128 +
21129 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
21130 +{
21131 +       spin_lock(&sbinfo->si_plink_maint_lock);
21132 +       sbinfo->si_plink_maint_pid = 0;
21133 +       spin_unlock(&sbinfo->si_plink_maint_lock);
21134 +       wake_up_all(&sbinfo->si_plink_wq);
21135 +}
21136 +
21137 +int au_plink_maint_enter(struct super_block *sb)
21138 +{
21139 +       int err;
21140 +       struct au_sbinfo *sbinfo;
21141 +
21142 +       err = 0;
21143 +       sbinfo = au_sbi(sb);
21144 +       /* make sure i am the only one in this fs */
21145 +       si_write_lock(sb, AuLock_FLUSH);
21146 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21147 +               spin_lock(&sbinfo->si_plink_maint_lock);
21148 +               if (!sbinfo->si_plink_maint_pid)
21149 +                       sbinfo->si_plink_maint_pid = current->pid;
21150 +               else
21151 +                       err = -EBUSY;
21152 +               spin_unlock(&sbinfo->si_plink_maint_lock);
21153 +       }
21154 +       si_write_unlock(sb);
21155 +
21156 +       return err;
21157 +}
21158 +
21159 +/* ---------------------------------------------------------------------- */
21160 +
21161 +struct pseudo_link {
21162 +       union {
21163 +               struct list_head list;
21164 +               struct rcu_head rcu;
21165 +       };
21166 +       struct inode *inode;
21167 +};
21168 +
21169 +#ifdef CONFIG_AUFS_DEBUG
21170 +void au_plink_list(struct super_block *sb)
21171 +{
21172 +       struct au_sbinfo *sbinfo;
21173 +       struct list_head *plink_list;
21174 +       struct pseudo_link *plink;
21175 +
21176 +       SiMustAnyLock(sb);
21177 +
21178 +       sbinfo = au_sbi(sb);
21179 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21180 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21181 +
21182 +       plink_list = &sbinfo->si_plink.head;
21183 +       rcu_read_lock();
21184 +       list_for_each_entry_rcu(plink, plink_list, list)
21185 +               AuDbg("%lu\n", plink->inode->i_ino);
21186 +       rcu_read_unlock();
21187 +}
21188 +#endif
21189 +
21190 +/* is the inode pseudo-linked? */
21191 +int au_plink_test(struct inode *inode)
21192 +{
21193 +       int found;
21194 +       struct au_sbinfo *sbinfo;
21195 +       struct list_head *plink_list;
21196 +       struct pseudo_link *plink;
21197 +
21198 +       sbinfo = au_sbi(inode->i_sb);
21199 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
21200 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
21201 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
21202 +
21203 +       found = 0;
21204 +       plink_list = &sbinfo->si_plink.head;
21205 +       rcu_read_lock();
21206 +       list_for_each_entry_rcu(plink, plink_list, list)
21207 +               if (plink->inode == inode) {
21208 +                       found = 1;
21209 +                       break;
21210 +               }
21211 +       rcu_read_unlock();
21212 +       return found;
21213 +}
21214 +
21215 +/* ---------------------------------------------------------------------- */
21216 +
21217 +/*
21218 + * generate a name for plink.
21219 + * the file will be stored under AUFS_WH_PLINKDIR.
21220 + */
21221 +/* 20 is max digits length of ulong 64 */
21222 +#define PLINK_NAME_LEN ((20 + 1) * 2)
21223 +
21224 +static int plink_name(char *name, int len, struct inode *inode,
21225 +                     aufs_bindex_t bindex)
21226 +{
21227 +       int rlen;
21228 +       struct inode *h_inode;
21229 +
21230 +       h_inode = au_h_iptr(inode, bindex);
21231 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
21232 +       return rlen;
21233 +}
21234 +
21235 +struct au_do_plink_lkup_args {
21236 +       struct dentry **errp;
21237 +       struct qstr *tgtname;
21238 +       struct dentry *h_parent;
21239 +       struct au_branch *br;
21240 +};
21241 +
21242 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
21243 +                                      struct dentry *h_parent,
21244 +                                      struct au_branch *br)
21245 +{
21246 +       struct dentry *h_dentry;
21247 +       struct mutex *h_mtx;
21248 +
21249 +       h_mtx = &h_parent->d_inode->i_mutex;
21250 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
21251 +       h_dentry = au_lkup_one(tgtname, h_parent, br, /*nd*/NULL);
21252 +       mutex_unlock(h_mtx);
21253 +       return h_dentry;
21254 +}
21255 +
21256 +static void au_call_do_plink_lkup(void *args)
21257 +{
21258 +       struct au_do_plink_lkup_args *a = args;
21259 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
21260 +}
21261 +
21262 +/* lookup the plink-ed @inode under the branch at @bindex */
21263 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
21264 +{
21265 +       struct dentry *h_dentry, *h_parent;
21266 +       struct au_branch *br;
21267 +       struct inode *h_dir;
21268 +       int wkq_err;
21269 +       char a[PLINK_NAME_LEN];
21270 +       struct qstr tgtname = {
21271 +               .name   = a
21272 +       };
21273 +
21274 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
21275 +
21276 +       br = au_sbr(inode->i_sb, bindex);
21277 +       h_parent = br->br_wbr->wbr_plink;
21278 +       h_dir = h_parent->d_inode;
21279 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
21280 +
21281 +       if (current_fsuid()) {
21282 +               struct au_do_plink_lkup_args args = {
21283 +                       .errp           = &h_dentry,
21284 +                       .tgtname        = &tgtname,
21285 +                       .h_parent       = h_parent,
21286 +                       .br             = br
21287 +               };
21288 +
21289 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
21290 +               if (unlikely(wkq_err))
21291 +                       h_dentry = ERR_PTR(wkq_err);
21292 +       } else
21293 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
21294 +
21295 +       return h_dentry;
21296 +}
21297 +
21298 +/* create a pseudo-link */
21299 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
21300 +                     struct dentry *h_dentry, struct au_branch *br)
21301 +{
21302 +       int err;
21303 +       struct path h_path = {
21304 +               .mnt = br->br_mnt
21305 +       };
21306 +       struct inode *h_dir;
21307 +
21308 +       h_dir = h_parent->d_inode;
21309 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
21310 +again:
21311 +       h_path.dentry = au_lkup_one(tgt, h_parent, br, /*nd*/NULL);
21312 +       err = PTR_ERR(h_path.dentry);
21313 +       if (IS_ERR(h_path.dentry))
21314 +               goto out;
21315 +
21316 +       err = 0;
21317 +       /* wh.plink dir is not monitored */
21318 +       /* todo: is it really safe? */
21319 +       if (h_path.dentry->d_inode
21320 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
21321 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
21322 +               dput(h_path.dentry);
21323 +               h_path.dentry = NULL;
21324 +               if (!err)
21325 +                       goto again;
21326 +       }
21327 +       if (!err && !h_path.dentry->d_inode)
21328 +               err = vfsub_link(h_dentry, h_dir, &h_path);
21329 +       dput(h_path.dentry);
21330 +
21331 +out:
21332 +       mutex_unlock(&h_dir->i_mutex);
21333 +       return err;
21334 +}
21335 +
21336 +struct do_whplink_args {
21337 +       int *errp;
21338 +       struct qstr *tgt;
21339 +       struct dentry *h_parent;
21340 +       struct dentry *h_dentry;
21341 +       struct au_branch *br;
21342 +};
21343 +
21344 +static void call_do_whplink(void *args)
21345 +{
21346 +       struct do_whplink_args *a = args;
21347 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
21348 +}
21349 +
21350 +static int whplink(struct dentry *h_dentry, struct inode *inode,
21351 +                  aufs_bindex_t bindex, struct au_branch *br)
21352 +{
21353 +       int err, wkq_err;
21354 +       struct au_wbr *wbr;
21355 +       struct dentry *h_parent;
21356 +       struct inode *h_dir;
21357 +       char a[PLINK_NAME_LEN];
21358 +       struct qstr tgtname = {
21359 +               .name = a
21360 +       };
21361 +
21362 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
21363 +       h_parent = wbr->wbr_plink;
21364 +       h_dir = h_parent->d_inode;
21365 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
21366 +
21367 +       /* always superio. */
21368 +       if (current_fsuid()) {
21369 +               struct do_whplink_args args = {
21370 +                       .errp           = &err,
21371 +                       .tgt            = &tgtname,
21372 +                       .h_parent       = h_parent,
21373 +                       .h_dentry       = h_dentry,
21374 +                       .br             = br
21375 +               };
21376 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
21377 +               if (unlikely(wkq_err))
21378 +                       err = wkq_err;
21379 +       } else
21380 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
21381 +
21382 +       return err;
21383 +}
21384 +
21385 +/* free a single plink */
21386 +static void do_put_plink(struct pseudo_link *plink, int do_del)
21387 +{
21388 +       if (do_del)
21389 +               list_del(&plink->list);
21390 +       iput(plink->inode);
21391 +       kfree(plink);
21392 +}
21393 +
21394 +static void do_put_plink_rcu(struct rcu_head *rcu)
21395 +{
21396 +       struct pseudo_link *plink;
21397 +
21398 +       plink = container_of(rcu, struct pseudo_link, rcu);
21399 +       iput(plink->inode);
21400 +       kfree(plink);
21401 +}
21402 +
21403 +/*
21404 + * create a new pseudo-link for @h_dentry on @bindex.
21405 + * the linked inode is held in aufs @inode.
21406 + */
21407 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
21408 +                    struct dentry *h_dentry)
21409 +{
21410 +       struct super_block *sb;
21411 +       struct au_sbinfo *sbinfo;
21412 +       struct list_head *plink_list;
21413 +       struct pseudo_link *plink, *tmp;
21414 +       int found, err, cnt;
21415 +
21416 +       sb = inode->i_sb;
21417 +       sbinfo = au_sbi(sb);
21418 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21419 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21420 +
21421 +       cnt = 0;
21422 +       found = 0;
21423 +       plink_list = &sbinfo->si_plink.head;
21424 +       rcu_read_lock();
21425 +       list_for_each_entry_rcu(plink, plink_list, list) {
21426 +               cnt++;
21427 +               if (plink->inode == inode) {
21428 +                       found = 1;
21429 +                       break;
21430 +               }
21431 +       }
21432 +       rcu_read_unlock();
21433 +       if (found)
21434 +               return;
21435 +
21436 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
21437 +       if (tmp)
21438 +               tmp->inode = au_igrab(inode);
21439 +       else {
21440 +               err = -ENOMEM;
21441 +               goto out;
21442 +       }
21443 +
21444 +       spin_lock(&sbinfo->si_plink.spin);
21445 +       list_for_each_entry(plink, plink_list, list) {
21446 +               if (plink->inode == inode) {
21447 +                       found = 1;
21448 +                       break;
21449 +               }
21450 +       }
21451 +       if (!found)
21452 +               list_add_rcu(&tmp->list, plink_list);
21453 +       spin_unlock(&sbinfo->si_plink.spin);
21454 +       if (!found) {
21455 +               cnt++;
21456 +               WARN_ONCE(cnt > AUFS_PLINK_WARN,
21457 +                         "unexpectedly many pseudo links, %d\n", cnt);
21458 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
21459 +       } else {
21460 +               do_put_plink(tmp, 0);
21461 +               return;
21462 +       }
21463 +
21464 +out:
21465 +       if (unlikely(err)) {
21466 +               pr_warning("err %d, damaged pseudo link.\n", err);
21467 +               if (tmp) {
21468 +                       au_spl_del_rcu(&tmp->list, &sbinfo->si_plink);
21469 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
21470 +               }
21471 +       }
21472 +}
21473 +
21474 +/* free all plinks */
21475 +void au_plink_put(struct super_block *sb, int verbose)
21476 +{
21477 +       struct au_sbinfo *sbinfo;
21478 +       struct list_head *plink_list;
21479 +       struct pseudo_link *plink, *tmp;
21480 +
21481 +       SiMustWriteLock(sb);
21482 +
21483 +       sbinfo = au_sbi(sb);
21484 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21485 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21486 +
21487 +       plink_list = &sbinfo->si_plink.head;
21488 +       /* no spin_lock since sbinfo is write-locked */
21489 +       WARN(verbose && !list_empty(plink_list), "pseudo-link is not flushed");
21490 +       list_for_each_entry_safe(plink, tmp, plink_list, list)
21491 +               do_put_plink(plink, 0);
21492 +       INIT_LIST_HEAD(plink_list);
21493 +}
21494 +
21495 +void au_plink_clean(struct super_block *sb, int verbose)
21496 +{
21497 +       struct dentry *root;
21498 +
21499 +       root = sb->s_root;
21500 +       aufs_write_lock(root);
21501 +       if (au_opt_test(au_mntflags(sb), PLINK))
21502 +               au_plink_put(sb, verbose);
21503 +       aufs_write_unlock(root);
21504 +}
21505 +
21506 +/* free the plinks on a branch specified by @br_id */
21507 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
21508 +{
21509 +       struct au_sbinfo *sbinfo;
21510 +       struct list_head *plink_list;
21511 +       struct pseudo_link *plink, *tmp;
21512 +       struct inode *inode;
21513 +       aufs_bindex_t bstart, bend, bindex;
21514 +       unsigned char do_put;
21515 +
21516 +       SiMustWriteLock(sb);
21517 +
21518 +       sbinfo = au_sbi(sb);
21519 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21520 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21521 +
21522 +       plink_list = &sbinfo->si_plink.head;
21523 +       /* no spin_lock since sbinfo is write-locked */
21524 +       list_for_each_entry_safe(plink, tmp, plink_list, list) {
21525 +               do_put = 0;
21526 +               inode = au_igrab(plink->inode);
21527 +               ii_write_lock_child(inode);
21528 +               bstart = au_ibstart(inode);
21529 +               bend = au_ibend(inode);
21530 +               if (bstart >= 0) {
21531 +                       for (bindex = bstart; bindex <= bend; bindex++) {
21532 +                               if (!au_h_iptr(inode, bindex)
21533 +                                   || au_ii_br_id(inode, bindex) != br_id)
21534 +                                       continue;
21535 +                               au_set_h_iptr(inode, bindex, NULL, 0);
21536 +                               do_put = 1;
21537 +                               break;
21538 +                       }
21539 +               } else
21540 +                       do_put_plink(plink, 1);
21541 +
21542 +               if (do_put) {
21543 +                       for (bindex = bstart; bindex <= bend; bindex++)
21544 +                               if (au_h_iptr(inode, bindex)) {
21545 +                                       do_put = 0;
21546 +                                       break;
21547 +                               }
21548 +                       if (do_put)
21549 +                               do_put_plink(plink, 1);
21550 +               }
21551 +               ii_write_unlock(inode);
21552 +               iput(inode);
21553 +       }
21554 +}
21555 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
21556 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
21557 +++ linux/fs/aufs/poll.c        2011-08-24 13:30:24.734646739 +0200
21558 @@ -0,0 +1,56 @@
21559 +/*
21560 + * Copyright (C) 2005-2011 Junjiro R. Okajima
21561 + *
21562 + * This program, aufs is free software; you can redistribute it and/or modify
21563 + * it under the terms of the GNU General Public License as published by
21564 + * the Free Software Foundation; either version 2 of the License, or
21565 + * (at your option) any later version.
21566 + *
21567 + * This program is distributed in the hope that it will be useful,
21568 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21569 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21570 + * GNU General Public License for more details.
21571 + *
21572 + * You should have received a copy of the GNU General Public License
21573 + * along with this program; if not, write to the Free Software
21574 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21575 + */
21576 +
21577 +/*
21578 + * poll operation
21579 + * There is only one filesystem which implements ->poll operation, currently.
21580 + */
21581 +
21582 +#include "aufs.h"
21583 +
21584 +unsigned int aufs_poll(struct file *file, poll_table *wait)
21585 +{
21586 +       unsigned int mask;
21587 +       int err;
21588 +       struct file *h_file;
21589 +       struct dentry *dentry;
21590 +       struct super_block *sb;
21591 +
21592 +       /* We should pretend an error happened. */
21593 +       mask = POLLERR /* | POLLIN | POLLOUT */;
21594 +       dentry = file->f_dentry;
21595 +       sb = dentry->d_sb;
21596 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
21597 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
21598 +       if (unlikely(err))
21599 +               goto out;
21600 +
21601 +       /* it is not an error if h_file has no operation */
21602 +       mask = DEFAULT_POLLMASK;
21603 +       h_file = au_hf_top(file);
21604 +       if (h_file->f_op && h_file->f_op->poll)
21605 +               mask = h_file->f_op->poll(h_file, wait);
21606 +
21607 +       di_read_unlock(dentry, AuLock_IR);
21608 +       fi_read_unlock(file);
21609 +
21610 +out:
21611 +       si_read_unlock(sb);
21612 +       AuTraceErr((int)mask);
21613 +       return mask;
21614 +}
21615 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
21616 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
21617 +++ linux/fs/aufs/procfs.c      2012-01-05 11:00:59.218835185 +0100
21618 @@ -0,0 +1,170 @@
21619 +/*
21620 + * Copyright (C) 2010-2011 Junjiro R. Okajima
21621 + *
21622 + * This program, aufs is free software; you can redistribute it and/or modify
21623 + * it under the terms of the GNU General Public License as published by
21624 + * the Free Software Foundation; either version 2 of the License, or
21625 + * (at your option) any later version.
21626 + *
21627 + * This program is distributed in the hope that it will be useful,
21628 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21629 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21630 + * GNU General Public License for more details.
21631 + *
21632 + * You should have received a copy of the GNU General Public License
21633 + * along with this program; if not, write to the Free Software
21634 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21635 + */
21636 +
21637 +/*
21638 + * procfs interfaces
21639 + */
21640 +
21641 +#include <linux/proc_fs.h>
21642 +#include "aufs.h"
21643 +
21644 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
21645 +{
21646 +       struct au_sbinfo *sbinfo;
21647 +
21648 +       sbinfo = file->private_data;
21649 +       if (sbinfo) {
21650 +               au_plink_maint_leave(sbinfo);
21651 +               kobject_put(&sbinfo->si_kobj);
21652 +       }
21653 +
21654 +       return 0;
21655 +}
21656 +
21657 +static void au_procfs_plm_write_clean(struct file *file)
21658 +{
21659 +       struct au_sbinfo *sbinfo;
21660 +
21661 +       sbinfo = file->private_data;
21662 +       if (sbinfo)
21663 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
21664 +}
21665 +
21666 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
21667 +{
21668 +       int err;
21669 +       struct super_block *sb;
21670 +       struct au_sbinfo *sbinfo;
21671 +
21672 +       err = -EBUSY;
21673 +       if (unlikely(file->private_data))
21674 +               goto out;
21675 +
21676 +       sb = NULL;
21677 +       /* don't use au_sbilist_lock() here */
21678 +       spin_lock(&au_sbilist.spin);
21679 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
21680 +               if (id == sysaufs_si_id(sbinfo)) {
21681 +                       kobject_get(&sbinfo->si_kobj);
21682 +                       sb = sbinfo->si_sb;
21683 +                       break;
21684 +               }
21685 +       spin_unlock(&au_sbilist.spin);
21686 +
21687 +       err = -EINVAL;
21688 +       if (unlikely(!sb))
21689 +               goto out;
21690 +
21691 +       err = au_plink_maint_enter(sb);
21692 +       if (!err)
21693 +               /* keep kobject_get() */
21694 +               file->private_data = sbinfo;
21695 +       else
21696 +               kobject_put(&sbinfo->si_kobj);
21697 +out:
21698 +       return err;
21699 +}
21700 +
21701 +/*
21702 + * Accept a valid "si=xxxx" only.
21703 + * Once it is accepted successfully, accept "clean" too.
21704 + */
21705 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
21706 +                                  size_t count, loff_t *ppos)
21707 +{
21708 +       ssize_t err;
21709 +       unsigned long id;
21710 +       /* last newline is allowed */
21711 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
21712 +
21713 +       err = -EACCES;
21714 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
21715 +               goto out;
21716 +
21717 +       err = -EINVAL;
21718 +       if (unlikely(count > sizeof(buf)))
21719 +               goto out;
21720 +
21721 +       err = copy_from_user(buf, ubuf, count);
21722 +       if (unlikely(err)) {
21723 +               err = -EFAULT;
21724 +               goto out;
21725 +       }
21726 +       buf[count] = 0;
21727 +
21728 +       err = -EINVAL;
21729 +       if (!strcmp("clean", buf)) {
21730 +               au_procfs_plm_write_clean(file);
21731 +               goto out_success;
21732 +       } else if (unlikely(strncmp("si=", buf, 3)))
21733 +               goto out;
21734 +
21735 +       err = kstrtoul(buf + 3, 16, &id);
21736 +       if (unlikely(err))
21737 +               goto out;
21738 +
21739 +       err = au_procfs_plm_write_si(file, id);
21740 +       if (unlikely(err))
21741 +               goto out;
21742 +
21743 +out_success:
21744 +       err = count; /* success */
21745 +out:
21746 +       return err;
21747 +}
21748 +
21749 +static const struct file_operations au_procfs_plm_fop = {
21750 +       .write          = au_procfs_plm_write,
21751 +       .release        = au_procfs_plm_release,
21752 +       .owner          = THIS_MODULE
21753 +};
21754 +
21755 +/* ---------------------------------------------------------------------- */
21756 +
21757 +static struct proc_dir_entry *au_procfs_dir;
21758 +
21759 +void au_procfs_fin(void)
21760 +{
21761 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
21762 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
21763 +}
21764 +
21765 +int __init au_procfs_init(void)
21766 +{
21767 +       int err;
21768 +       struct proc_dir_entry *entry;
21769 +
21770 +       err = -ENOMEM;
21771 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
21772 +       if (unlikely(!au_procfs_dir))
21773 +               goto out;
21774 +
21775 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
21776 +                           au_procfs_dir, &au_procfs_plm_fop);
21777 +       if (unlikely(!entry))
21778 +               goto out_dir;
21779 +
21780 +       err = 0;
21781 +       goto out; /* success */
21782 +
21783 +
21784 +out_dir:
21785 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
21786 +out:
21787 +       return err;
21788 +}
21789 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
21790 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
21791 +++ linux/fs/aufs/rdu.c 2012-01-05 11:00:59.218835185 +0100
21792 @@ -0,0 +1,385 @@
21793 +/*
21794 + * Copyright (C) 2005-2011 Junjiro R. Okajima
21795 + *
21796 + * This program, aufs is free software; you can redistribute it and/or modify
21797 + * it under the terms of the GNU General Public License as published by
21798 + * the Free Software Foundation; either version 2 of the License, or
21799 + * (at your option) any later version.
21800 + *
21801 + * This program is distributed in the hope that it will be useful,
21802 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21803 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21804 + * GNU General Public License for more details.
21805 + *
21806 + * You should have received a copy of the GNU General Public License
21807 + * along with this program; if not, write to the Free Software
21808 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21809 + */
21810 +
21811 +/*
21812 + * readdir in userspace.
21813 + */
21814 +
21815 +#include <linux/compat.h>
21816 +#include <linux/fs_stack.h>
21817 +#include <linux/security.h>
21818 +#include <linux/uaccess.h>
21819 +#include <linux/aufs_type.h>
21820 +#include "aufs.h"
21821 +
21822 +/* bits for struct aufs_rdu.flags */
21823 +#define        AuRdu_CALLED    1
21824 +#define        AuRdu_CONT      (1 << 1)
21825 +#define        AuRdu_FULL      (1 << 2)
21826 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
21827 +#define au_fset_rdu(flags, name) \
21828 +       do { (flags) |= AuRdu_##name; } while (0)
21829 +#define au_fclr_rdu(flags, name) \
21830 +       do { (flags) &= ~AuRdu_##name; } while (0)
21831 +
21832 +struct au_rdu_arg {
21833 +       struct aufs_rdu                 *rdu;
21834 +       union au_rdu_ent_ul             ent;
21835 +       unsigned long                   end;
21836 +
21837 +       struct super_block              *sb;
21838 +       int                             err;
21839 +};
21840 +
21841 +static int au_rdu_fill(void *__arg, const char *name, int nlen,
21842 +                      loff_t offset, u64 h_ino, unsigned int d_type)
21843 +{
21844 +       int err, len;
21845 +       struct au_rdu_arg *arg = __arg;
21846 +       struct aufs_rdu *rdu = arg->rdu;
21847 +       struct au_rdu_ent ent;
21848 +
21849 +       err = 0;
21850 +       arg->err = 0;
21851 +       au_fset_rdu(rdu->cookie.flags, CALLED);
21852 +       len = au_rdu_len(nlen);
21853 +       if (arg->ent.ul + len  < arg->end) {
21854 +               ent.ino = h_ino;
21855 +               ent.bindex = rdu->cookie.bindex;
21856 +               ent.type = d_type;
21857 +               ent.nlen = nlen;
21858 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
21859 +                       ent.type = DT_UNKNOWN;
21860 +
21861 +               /* unnecessary to support mmap_sem since this is a dir */
21862 +               err = -EFAULT;
21863 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
21864 +                       goto out;
21865 +               if (copy_to_user(arg->ent.e->name, name, nlen))
21866 +                       goto out;
21867 +               /* the terminating NULL */
21868 +               if (__put_user(0, arg->ent.e->name + nlen))
21869 +                       goto out;
21870 +               err = 0;
21871 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
21872 +               arg->ent.ul += len;
21873 +               rdu->rent++;
21874 +       } else {
21875 +               err = -EFAULT;
21876 +               au_fset_rdu(rdu->cookie.flags, FULL);
21877 +               rdu->full = 1;
21878 +               rdu->tail = arg->ent;
21879 +       }
21880 +
21881 +out:
21882 +       /* AuTraceErr(err); */
21883 +       return err;
21884 +}
21885 +
21886 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
21887 +{
21888 +       int err;
21889 +       loff_t offset;
21890 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
21891 +
21892 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
21893 +       err = offset;
21894 +       if (unlikely(offset != cookie->h_pos))
21895 +               goto out;
21896 +
21897 +       err = 0;
21898 +       do {
21899 +               arg->err = 0;
21900 +               au_fclr_rdu(cookie->flags, CALLED);
21901 +               /* smp_mb(); */
21902 +               err = vfsub_readdir(h_file, au_rdu_fill, arg);
21903 +               if (err >= 0)
21904 +                       err = arg->err;
21905 +       } while (!err
21906 +                && au_ftest_rdu(cookie->flags, CALLED)
21907 +                && !au_ftest_rdu(cookie->flags, FULL));
21908 +       cookie->h_pos = h_file->f_pos;
21909 +
21910 +out:
21911 +       AuTraceErr(err);
21912 +       return err;
21913 +}
21914 +
21915 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
21916 +{
21917 +       int err;
21918 +       aufs_bindex_t bend;
21919 +       struct au_rdu_arg arg;
21920 +       struct dentry *dentry;
21921 +       struct inode *inode;
21922 +       struct file *h_file;
21923 +       struct au_rdu_cookie *cookie = &rdu->cookie;
21924 +
21925 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
21926 +       if (unlikely(err)) {
21927 +               err = -EFAULT;
21928 +               AuTraceErr(err);
21929 +               goto out;
21930 +       }
21931 +       rdu->rent = 0;
21932 +       rdu->tail = rdu->ent;
21933 +       rdu->full = 0;
21934 +       arg.rdu = rdu;
21935 +       arg.ent = rdu->ent;
21936 +       arg.end = arg.ent.ul;
21937 +       arg.end += rdu->sz;
21938 +
21939 +       err = -ENOTDIR;
21940 +       if (unlikely(!file->f_op || !file->f_op->readdir))
21941 +               goto out;
21942 +
21943 +       err = security_file_permission(file, MAY_READ);
21944 +       AuTraceErr(err);
21945 +       if (unlikely(err))
21946 +               goto out;
21947 +
21948 +       dentry = file->f_dentry;
21949 +       inode = dentry->d_inode;
21950 +#if 1
21951 +       mutex_lock(&inode->i_mutex);
21952 +#else
21953 +       err = mutex_lock_killable(&inode->i_mutex);
21954 +       AuTraceErr(err);
21955 +       if (unlikely(err))
21956 +               goto out;
21957 +#endif
21958 +
21959 +       arg.sb = inode->i_sb;
21960 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
21961 +       if (unlikely(err))
21962 +               goto out_mtx;
21963 +       err = au_alive_dir(dentry);
21964 +       if (unlikely(err))
21965 +               goto out_si;
21966 +       /* todo: reval? */
21967 +       fi_read_lock(file);
21968 +
21969 +       err = -EAGAIN;
21970 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
21971 +                    && cookie->generation != au_figen(file)))
21972 +               goto out_unlock;
21973 +
21974 +       err = 0;
21975 +       if (!rdu->blk) {
21976 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
21977 +               if (!rdu->blk)
21978 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
21979 +       }
21980 +       bend = au_fbstart(file);
21981 +       if (cookie->bindex < bend)
21982 +               cookie->bindex = bend;
21983 +       bend = au_fbend_dir(file);
21984 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
21985 +       for (; !err && cookie->bindex <= bend;
21986 +            cookie->bindex++, cookie->h_pos = 0) {
21987 +               h_file = au_hf_dir(file, cookie->bindex);
21988 +               if (!h_file)
21989 +                       continue;
21990 +
21991 +               au_fclr_rdu(cookie->flags, FULL);
21992 +               err = au_rdu_do(h_file, &arg);
21993 +               AuTraceErr(err);
21994 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
21995 +                       break;
21996 +       }
21997 +       AuDbg("rent %llu\n", rdu->rent);
21998 +
21999 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
22000 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
22001 +               au_fset_rdu(cookie->flags, CONT);
22002 +               cookie->generation = au_figen(file);
22003 +       }
22004 +
22005 +       ii_read_lock_child(inode);
22006 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
22007 +       ii_read_unlock(inode);
22008 +
22009 +out_unlock:
22010 +       fi_read_unlock(file);
22011 +out_si:
22012 +       si_read_unlock(arg.sb);
22013 +out_mtx:
22014 +       mutex_unlock(&inode->i_mutex);
22015 +out:
22016 +       AuTraceErr(err);
22017 +       return err;
22018 +}
22019 +
22020 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
22021 +{
22022 +       int err;
22023 +       ino_t ino;
22024 +       unsigned long long nent;
22025 +       union au_rdu_ent_ul *u;
22026 +       struct au_rdu_ent ent;
22027 +       struct super_block *sb;
22028 +
22029 +       err = 0;
22030 +       nent = rdu->nent;
22031 +       u = &rdu->ent;
22032 +       sb = file->f_dentry->d_sb;
22033 +       si_read_lock(sb, AuLock_FLUSH);
22034 +       while (nent-- > 0) {
22035 +               /* unnecessary to support mmap_sem since this is a dir */
22036 +               err = copy_from_user(&ent, u->e, sizeof(ent));
22037 +               if (!err)
22038 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
22039 +               if (unlikely(err)) {
22040 +                       err = -EFAULT;
22041 +                       AuTraceErr(err);
22042 +                       break;
22043 +               }
22044 +
22045 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
22046 +               if (!ent.wh)
22047 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
22048 +               else
22049 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
22050 +                                       &ino);
22051 +               if (unlikely(err)) {
22052 +                       AuTraceErr(err);
22053 +                       break;
22054 +               }
22055 +
22056 +               err = __put_user(ino, &u->e->ino);
22057 +               if (unlikely(err)) {
22058 +                       err = -EFAULT;
22059 +                       AuTraceErr(err);
22060 +                       break;
22061 +               }
22062 +               u->ul += au_rdu_len(ent.nlen);
22063 +       }
22064 +       si_read_unlock(sb);
22065 +
22066 +       return err;
22067 +}
22068 +
22069 +/* ---------------------------------------------------------------------- */
22070 +
22071 +static int au_rdu_verify(struct aufs_rdu *rdu)
22072 +{
22073 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
22074 +             "%llu, b%d, 0x%x, g%u}\n",
22075 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
22076 +             rdu->blk,
22077 +             rdu->rent, rdu->shwh, rdu->full,
22078 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
22079 +             rdu->cookie.generation);
22080 +
22081 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
22082 +               return 0;
22083 +
22084 +       AuDbg("%u:%u\n",
22085 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
22086 +       return -EINVAL;
22087 +}
22088 +
22089 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22090 +{
22091 +       long err, e;
22092 +       struct aufs_rdu rdu;
22093 +       void __user *p = (void __user *)arg;
22094 +
22095 +       err = copy_from_user(&rdu, p, sizeof(rdu));
22096 +       if (unlikely(err)) {
22097 +               err = -EFAULT;
22098 +               AuTraceErr(err);
22099 +               goto out;
22100 +       }
22101 +       err = au_rdu_verify(&rdu);
22102 +       if (unlikely(err))
22103 +               goto out;
22104 +
22105 +       switch (cmd) {
22106 +       case AUFS_CTL_RDU:
22107 +               err = au_rdu(file, &rdu);
22108 +               if (unlikely(err))
22109 +                       break;
22110 +
22111 +               e = copy_to_user(p, &rdu, sizeof(rdu));
22112 +               if (unlikely(e)) {
22113 +                       err = -EFAULT;
22114 +                       AuTraceErr(err);
22115 +               }
22116 +               break;
22117 +       case AUFS_CTL_RDU_INO:
22118 +               err = au_rdu_ino(file, &rdu);
22119 +               break;
22120 +
22121 +       default:
22122 +               /* err = -ENOTTY; */
22123 +               err = -EINVAL;
22124 +       }
22125 +
22126 +out:
22127 +       AuTraceErr(err);
22128 +       return err;
22129 +}
22130 +
22131 +#ifdef CONFIG_COMPAT
22132 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22133 +{
22134 +       long err, e;
22135 +       struct aufs_rdu rdu;
22136 +       void __user *p = compat_ptr(arg);
22137 +
22138 +       /* todo: get_user()? */
22139 +       err = copy_from_user(&rdu, p, sizeof(rdu));
22140 +       if (unlikely(err)) {
22141 +               err = -EFAULT;
22142 +               AuTraceErr(err);
22143 +               goto out;
22144 +       }
22145 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
22146 +       err = au_rdu_verify(&rdu);
22147 +       if (unlikely(err))
22148 +               goto out;
22149 +
22150 +       switch (cmd) {
22151 +       case AUFS_CTL_RDU:
22152 +               err = au_rdu(file, &rdu);
22153 +               if (unlikely(err))
22154 +                       break;
22155 +
22156 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
22157 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
22158 +               e = copy_to_user(p, &rdu, sizeof(rdu));
22159 +               if (unlikely(e)) {
22160 +                       err = -EFAULT;
22161 +                       AuTraceErr(err);
22162 +               }
22163 +               break;
22164 +       case AUFS_CTL_RDU_INO:
22165 +               err = au_rdu_ino(file, &rdu);
22166 +               break;
22167 +
22168 +       default:
22169 +               /* err = -ENOTTY; */
22170 +               err = -EINVAL;
22171 +       }
22172 +
22173 +out:
22174 +       AuTraceErr(err);
22175 +       return err;
22176 +}
22177 +#endif
22178 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
22179 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
22180 +++ linux/fs/aufs/rwsem.h       2011-08-24 13:30:24.734646739 +0200
22181 @@ -0,0 +1,189 @@
22182 +/*
22183 + * Copyright (C) 2005-2011 Junjiro R. Okajima
22184 + *
22185 + * This program, aufs is free software; you can redistribute it and/or modify
22186 + * it under the terms of the GNU General Public License as published by
22187 + * the Free Software Foundation; either version 2 of the License, or
22188 + * (at your option) any later version.
22189 + *
22190 + * This program is distributed in the hope that it will be useful,
22191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22193 + * GNU General Public License for more details.
22194 + *
22195 + * You should have received a copy of the GNU General Public License
22196 + * along with this program; if not, write to the Free Software
22197 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22198 + */
22199 +
22200 +/*
22201 + * simple read-write semaphore wrappers
22202 + */
22203 +
22204 +#ifndef __AUFS_RWSEM_H__
22205 +#define __AUFS_RWSEM_H__
22206 +
22207 +#ifdef __KERNEL__
22208 +
22209 +#include <linux/rwsem.h>
22210 +#include "debug.h"
22211 +
22212 +struct au_rwsem {
22213 +       struct rw_semaphore     rwsem;
22214 +#ifdef CONFIG_AUFS_DEBUG
22215 +       /* just for debugging, not almighty counter */
22216 +       atomic_t                rcnt, wcnt;
22217 +#endif
22218 +};
22219 +
22220 +#ifdef CONFIG_AUFS_DEBUG
22221 +#define AuDbgCntInit(rw) do { \
22222 +       atomic_set(&(rw)->rcnt, 0); \
22223 +       atomic_set(&(rw)->wcnt, 0); \
22224 +       smp_mb(); /* atomic set */ \
22225 +} while (0)
22226 +
22227 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
22228 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
22229 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
22230 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
22231 +#else
22232 +#define AuDbgCntInit(rw)       do {} while (0)
22233 +#define AuDbgRcntInc(rw)       do {} while (0)
22234 +#define AuDbgRcntDec(rw)       do {} while (0)
22235 +#define AuDbgWcntInc(rw)       do {} while (0)
22236 +#define AuDbgWcntDec(rw)       do {} while (0)
22237 +#endif /* CONFIG_AUFS_DEBUG */
22238 +
22239 +/* to debug easier, do not make them inlined functions */
22240 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
22241 +/* rwsem_is_locked() is unusable */
22242 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
22243 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
22244 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
22245 +                                       && atomic_read(&(rw)->wcnt) <= 0)
22246 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
22247 +                                       || atomic_read(&(rw)->wcnt))
22248 +
22249 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
22250 +
22251 +static inline void au_rw_init(struct au_rwsem *rw)
22252 +{
22253 +       AuDbgCntInit(rw);
22254 +       init_rwsem(&rw->rwsem);
22255 +}
22256 +
22257 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
22258 +{
22259 +       au_rw_init(rw);
22260 +       down_write(&rw->rwsem);
22261 +       AuDbgWcntInc(rw);
22262 +}
22263 +
22264 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
22265 +                                          unsigned int lsc)
22266 +{
22267 +       au_rw_init(rw);
22268 +       down_write_nested(&rw->rwsem, lsc);
22269 +       AuDbgWcntInc(rw);
22270 +}
22271 +
22272 +static inline void au_rw_read_lock(struct au_rwsem *rw)
22273 +{
22274 +       down_read(&rw->rwsem);
22275 +       AuDbgRcntInc(rw);
22276 +}
22277 +
22278 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
22279 +{
22280 +       down_read_nested(&rw->rwsem, lsc);
22281 +       AuDbgRcntInc(rw);
22282 +}
22283 +
22284 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
22285 +{
22286 +       AuRwMustReadLock(rw);
22287 +       AuDbgRcntDec(rw);
22288 +       up_read(&rw->rwsem);
22289 +}
22290 +
22291 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
22292 +{
22293 +       AuRwMustWriteLock(rw);
22294 +       AuDbgRcntInc(rw);
22295 +       AuDbgWcntDec(rw);
22296 +       downgrade_write(&rw->rwsem);
22297 +}
22298 +
22299 +static inline void au_rw_write_lock(struct au_rwsem *rw)
22300 +{
22301 +       down_write(&rw->rwsem);
22302 +       AuDbgWcntInc(rw);
22303 +}
22304 +
22305 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
22306 +                                          unsigned int lsc)
22307 +{
22308 +       down_write_nested(&rw->rwsem, lsc);
22309 +       AuDbgWcntInc(rw);
22310 +}
22311 +
22312 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
22313 +{
22314 +       AuRwMustWriteLock(rw);
22315 +       AuDbgWcntDec(rw);
22316 +       up_write(&rw->rwsem);
22317 +}
22318 +
22319 +/* why is not _nested version defined */
22320 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
22321 +{
22322 +       int ret = down_read_trylock(&rw->rwsem);
22323 +       if (ret)
22324 +               AuDbgRcntInc(rw);
22325 +       return ret;
22326 +}
22327 +
22328 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
22329 +{
22330 +       int ret = down_write_trylock(&rw->rwsem);
22331 +       if (ret)
22332 +               AuDbgWcntInc(rw);
22333 +       return ret;
22334 +}
22335 +
22336 +#undef AuDbgCntInit
22337 +#undef AuDbgRcntInc
22338 +#undef AuDbgRcntDec
22339 +#undef AuDbgWcntInc
22340 +#undef AuDbgWcntDec
22341 +
22342 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
22343 +static inline void prefix##_read_lock(param) \
22344 +{ au_rw_read_lock(rwsem); } \
22345 +static inline void prefix##_write_lock(param) \
22346 +{ au_rw_write_lock(rwsem); } \
22347 +static inline int prefix##_read_trylock(param) \
22348 +{ return au_rw_read_trylock(rwsem); } \
22349 +static inline int prefix##_write_trylock(param) \
22350 +{ return au_rw_write_trylock(rwsem); }
22351 +/* why is not _nested version defined */
22352 +/* static inline void prefix##_read_trylock_nested(param, lsc)
22353 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
22354 +static inline void prefix##_write_trylock_nestd(param, lsc)
22355 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
22356 +
22357 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
22358 +static inline void prefix##_read_unlock(param) \
22359 +{ au_rw_read_unlock(rwsem); } \
22360 +static inline void prefix##_write_unlock(param) \
22361 +{ au_rw_write_unlock(rwsem); } \
22362 +static inline void prefix##_downgrade_lock(param) \
22363 +{ au_rw_dgrade_lock(rwsem); }
22364 +
22365 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
22366 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
22367 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
22368 +
22369 +#endif /* __KERNEL__ */
22370 +#endif /* __AUFS_RWSEM_H__ */
22371 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
22372 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
22373 +++ linux/fs/aufs/sbinfo.c      2011-08-24 13:30:24.734646739 +0200
22374 @@ -0,0 +1,344 @@
22375 +/*
22376 + * Copyright (C) 2005-2011 Junjiro R. Okajima
22377 + *
22378 + * This program, aufs is free software; you can redistribute it and/or modify
22379 + * it under the terms of the GNU General Public License as published by
22380 + * the Free Software Foundation; either version 2 of the License, or
22381 + * (at your option) any later version.
22382 + *
22383 + * This program is distributed in the hope that it will be useful,
22384 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22385 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22386 + * GNU General Public License for more details.
22387 + *
22388 + * You should have received a copy of the GNU General Public License
22389 + * along with this program; if not, write to the Free Software
22390 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22391 + */
22392 +
22393 +/*
22394 + * superblock private data
22395 + */
22396 +
22397 +#include <linux/jiffies.h>
22398 +#include "aufs.h"
22399 +
22400 +/*
22401 + * they are necessary regardless sysfs is disabled.
22402 + */
22403 +void au_si_free(struct kobject *kobj)
22404 +{
22405 +       struct au_sbinfo *sbinfo;
22406 +       char *locked __maybe_unused; /* debug only */
22407 +
22408 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
22409 +       AuDebugOn(!list_empty(&sbinfo->si_plink.head));
22410 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
22411 +
22412 +       au_rw_write_lock(&sbinfo->si_rwsem);
22413 +       au_br_free(sbinfo);
22414 +       au_rw_write_unlock(&sbinfo->si_rwsem);
22415 +
22416 +       AuDebugOn(radix_tree_gang_lookup
22417 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
22418 +                  /*first_index*/PID_MAX_DEFAULT - 1,
22419 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
22420 +
22421 +       kfree(sbinfo->si_branch);
22422 +       kfree(sbinfo->au_si_pid.bitmap);
22423 +       mutex_destroy(&sbinfo->si_xib_mtx);
22424 +       AuRwDestroy(&sbinfo->si_rwsem);
22425 +
22426 +       kfree(sbinfo);
22427 +}
22428 +
22429 +int au_si_alloc(struct super_block *sb)
22430 +{
22431 +       int err;
22432 +       struct au_sbinfo *sbinfo;
22433 +       static struct lock_class_key aufs_si;
22434 +
22435 +       err = -ENOMEM;
22436 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
22437 +       if (unlikely(!sbinfo))
22438 +               goto out;
22439 +
22440 +       BUILD_BUG_ON(sizeof(unsigned long) !=
22441 +                    sizeof(*sbinfo->au_si_pid.bitmap));
22442 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
22443 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
22444 +                                       GFP_NOFS);
22445 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
22446 +               goto out_sbinfo;
22447 +
22448 +       /* will be reallocated separately */
22449 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
22450 +       if (unlikely(!sbinfo->si_branch))
22451 +               goto out_pidmap;
22452 +
22453 +       err = sysaufs_si_init(sbinfo);
22454 +       if (unlikely(err))
22455 +               goto out_br;
22456 +
22457 +       au_nwt_init(&sbinfo->si_nowait);
22458 +       au_rw_init_wlock(&sbinfo->si_rwsem);
22459 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
22460 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
22461 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
22462 +
22463 +       atomic_long_set(&sbinfo->si_ninodes, 0);
22464 +       atomic_long_set(&sbinfo->si_nfiles, 0);
22465 +
22466 +       sbinfo->si_bend = -1;
22467 +
22468 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
22469 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
22470 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
22471 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
22472 +
22473 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
22474 +
22475 +       mutex_init(&sbinfo->si_xib_mtx);
22476 +       sbinfo->si_xino_brid = -1;
22477 +       /* leave si_xib_last_pindex and si_xib_next_bit */
22478 +
22479 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
22480 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
22481 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
22482 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
22483 +
22484 +       au_spl_init(&sbinfo->si_plink);
22485 +       init_waitqueue_head(&sbinfo->si_plink_wq);
22486 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
22487 +
22488 +       /* leave other members for sysaufs and si_mnt. */
22489 +       sbinfo->si_sb = sb;
22490 +       sb->s_fs_info = sbinfo;
22491 +       si_pid_set(sb);
22492 +       au_debug_sbinfo_init(sbinfo);
22493 +       return 0; /* success */
22494 +
22495 +out_br:
22496 +       kfree(sbinfo->si_branch);
22497 +out_pidmap:
22498 +       kfree(sbinfo->au_si_pid.bitmap);
22499 +out_sbinfo:
22500 +       kfree(sbinfo);
22501 +out:
22502 +       return err;
22503 +}
22504 +
22505 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
22506 +{
22507 +       int err, sz;
22508 +       struct au_branch **brp;
22509 +
22510 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
22511 +
22512 +       err = -ENOMEM;
22513 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
22514 +       if (unlikely(!sz))
22515 +               sz = sizeof(*brp);
22516 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
22517 +       if (brp) {
22518 +               sbinfo->si_branch = brp;
22519 +               err = 0;
22520 +       }
22521 +
22522 +       return err;
22523 +}
22524 +
22525 +/* ---------------------------------------------------------------------- */
22526 +
22527 +unsigned int au_sigen_inc(struct super_block *sb)
22528 +{
22529 +       unsigned int gen;
22530 +
22531 +       SiMustWriteLock(sb);
22532 +
22533 +       gen = ++au_sbi(sb)->si_generation;
22534 +       au_update_digen(sb->s_root);
22535 +       au_update_iigen(sb->s_root->d_inode);
22536 +       sb->s_root->d_inode->i_version++;
22537 +       return gen;
22538 +}
22539 +
22540 +aufs_bindex_t au_new_br_id(struct super_block *sb)
22541 +{
22542 +       aufs_bindex_t br_id;
22543 +       int i;
22544 +       struct au_sbinfo *sbinfo;
22545 +
22546 +       SiMustWriteLock(sb);
22547 +
22548 +       sbinfo = au_sbi(sb);
22549 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
22550 +               br_id = ++sbinfo->si_last_br_id;
22551 +               AuDebugOn(br_id < 0);
22552 +               if (br_id && au_br_index(sb, br_id) < 0)
22553 +                       return br_id;
22554 +       }
22555 +
22556 +       return -1;
22557 +}
22558 +
22559 +/* ---------------------------------------------------------------------- */
22560 +
22561 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
22562 +int si_read_lock(struct super_block *sb, int flags)
22563 +{
22564 +       int err;
22565 +
22566 +       err = 0;
22567 +       if (au_ftest_lock(flags, FLUSH))
22568 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
22569 +
22570 +       si_noflush_read_lock(sb);
22571 +       err = au_plink_maint(sb, flags);
22572 +       if (unlikely(err))
22573 +               si_read_unlock(sb);
22574 +
22575 +       return err;
22576 +}
22577 +
22578 +int si_write_lock(struct super_block *sb, int flags)
22579 +{
22580 +       int err;
22581 +
22582 +       if (au_ftest_lock(flags, FLUSH))
22583 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
22584 +
22585 +       si_noflush_write_lock(sb);
22586 +       err = au_plink_maint(sb, flags);
22587 +       if (unlikely(err))
22588 +               si_write_unlock(sb);
22589 +
22590 +       return err;
22591 +}
22592 +
22593 +/* dentry and super_block lock. call at entry point */
22594 +int aufs_read_lock(struct dentry *dentry, int flags)
22595 +{
22596 +       int err;
22597 +       struct super_block *sb;
22598 +
22599 +       sb = dentry->d_sb;
22600 +       err = si_read_lock(sb, flags);
22601 +       if (unlikely(err))
22602 +               goto out;
22603 +
22604 +       if (au_ftest_lock(flags, DW))
22605 +               di_write_lock_child(dentry);
22606 +       else
22607 +               di_read_lock_child(dentry, flags);
22608 +
22609 +       if (au_ftest_lock(flags, GEN)) {
22610 +               err = au_digen_test(dentry, au_sigen(sb));
22611 +               AuDebugOn(!err && au_dbrange_test(dentry));
22612 +               if (unlikely(err))
22613 +                       aufs_read_unlock(dentry, flags);
22614 +       }
22615 +
22616 +out:
22617 +       return err;
22618 +}
22619 +
22620 +void aufs_read_unlock(struct dentry *dentry, int flags)
22621 +{
22622 +       if (au_ftest_lock(flags, DW))
22623 +               di_write_unlock(dentry);
22624 +       else
22625 +               di_read_unlock(dentry, flags);
22626 +       si_read_unlock(dentry->d_sb);
22627 +}
22628 +
22629 +void aufs_write_lock(struct dentry *dentry)
22630 +{
22631 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
22632 +       di_write_lock_child(dentry);
22633 +}
22634 +
22635 +void aufs_write_unlock(struct dentry *dentry)
22636 +{
22637 +       di_write_unlock(dentry);
22638 +       si_write_unlock(dentry->d_sb);
22639 +}
22640 +
22641 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
22642 +{
22643 +       int err;
22644 +       unsigned int sigen;
22645 +       struct super_block *sb;
22646 +
22647 +       sb = d1->d_sb;
22648 +       err = si_read_lock(sb, flags);
22649 +       if (unlikely(err))
22650 +               goto out;
22651 +
22652 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
22653 +
22654 +       if (au_ftest_lock(flags, GEN)) {
22655 +               sigen = au_sigen(sb);
22656 +               err = au_digen_test(d1, sigen);
22657 +               AuDebugOn(!err && au_dbrange_test(d1));
22658 +               if (!err) {
22659 +                       err = au_digen_test(d2, sigen);
22660 +                       AuDebugOn(!err && au_dbrange_test(d2));
22661 +               }
22662 +               if (unlikely(err))
22663 +                       aufs_read_and_write_unlock2(d1, d2);
22664 +       }
22665 +
22666 +out:
22667 +       return err;
22668 +}
22669 +
22670 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
22671 +{
22672 +       di_write_unlock2(d1, d2);
22673 +       si_read_unlock(d1->d_sb);
22674 +}
22675 +
22676 +/* ---------------------------------------------------------------------- */
22677 +
22678 +int si_pid_test_slow(struct super_block *sb)
22679 +{
22680 +       void *p;
22681 +
22682 +       rcu_read_lock();
22683 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
22684 +       rcu_read_unlock();
22685 +
22686 +       return (long)!!p;
22687 +}
22688 +
22689 +void si_pid_set_slow(struct super_block *sb)
22690 +{
22691 +       int err;
22692 +       struct au_sbinfo *sbinfo;
22693 +
22694 +       AuDebugOn(si_pid_test_slow(sb));
22695 +
22696 +       sbinfo = au_sbi(sb);
22697 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
22698 +       AuDebugOn(err);
22699 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
22700 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
22701 +                               /*any valid ptr*/sb);
22702 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
22703 +       AuDebugOn(err);
22704 +       radix_tree_preload_end();
22705 +}
22706 +
22707 +void si_pid_clr_slow(struct super_block *sb)
22708 +{
22709 +       void *p;
22710 +       struct au_sbinfo *sbinfo;
22711 +
22712 +       AuDebugOn(!si_pid_test_slow(sb));
22713 +
22714 +       sbinfo = au_sbi(sb);
22715 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
22716 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
22717 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
22718 +}
22719 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
22720 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
22721 +++ linux/fs/aufs/spl.h 2011-08-24 13:30:24.734646739 +0200
22722 @@ -0,0 +1,66 @@
22723 +/*
22724 + * Copyright (C) 2005-2011 Junjiro R. Okajima
22725 + *
22726 + * This program, aufs is free software; you can redistribute it and/or modify
22727 + * it under the terms of the GNU General Public License as published by
22728 + * the Free Software Foundation; either version 2 of the License, or
22729 + * (at your option) any later version.
22730 + *
22731 + * This program is distributed in the hope that it will be useful,
22732 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22733 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22734 + * GNU General Public License for more details.
22735 + *
22736 + * You should have received a copy of the GNU General Public License
22737 + * along with this program; if not, write to the Free Software
22738 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22739 + */
22740 +
22741 +/*
22742 + * simple list protected by a spinlock
22743 + */
22744 +
22745 +#ifndef __AUFS_SPL_H__
22746 +#define __AUFS_SPL_H__
22747 +
22748 +#ifdef __KERNEL__
22749 +
22750 +#include <linux/spinlock.h>
22751 +#include <linux/list.h>
22752 +#include <linux/rculist.h>
22753 +
22754 +struct au_splhead {
22755 +       spinlock_t              spin;
22756 +       struct list_head        head;
22757 +};
22758 +
22759 +static inline void au_spl_init(struct au_splhead *spl)
22760 +{
22761 +       spin_lock_init(&spl->spin);
22762 +       INIT_LIST_HEAD(&spl->head);
22763 +}
22764 +
22765 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
22766 +{
22767 +       spin_lock(&spl->spin);
22768 +       list_add(list, &spl->head);
22769 +       spin_unlock(&spl->spin);
22770 +}
22771 +
22772 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
22773 +{
22774 +       spin_lock(&spl->spin);
22775 +       list_del(list);
22776 +       spin_unlock(&spl->spin);
22777 +}
22778 +
22779 +static inline void au_spl_del_rcu(struct list_head *list,
22780 +                                 struct au_splhead *spl)
22781 +{
22782 +       spin_lock(&spl->spin);
22783 +       list_del_rcu(list);
22784 +       spin_unlock(&spl->spin);
22785 +}
22786 +
22787 +#endif /* __KERNEL__ */
22788 +#endif /* __AUFS_SPL_H__ */
22789 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
22790 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
22791 +++ linux/fs/aufs/super.c       2012-01-05 11:00:59.218835185 +0100
22792 @@ -0,0 +1,939 @@
22793 +/*
22794 + * Copyright (C) 2005-2011 Junjiro R. Okajima
22795 + *
22796 + * This program, aufs is free software; you can redistribute it and/or modify
22797 + * it under the terms of the GNU General Public License as published by
22798 + * the Free Software Foundation; either version 2 of the License, or
22799 + * (at your option) any later version.
22800 + *
22801 + * This program is distributed in the hope that it will be useful,
22802 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22803 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22804 + * GNU General Public License for more details.
22805 + *
22806 + * You should have received a copy of the GNU General Public License
22807 + * along with this program; if not, write to the Free Software
22808 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22809 + */
22810 +
22811 +/*
22812 + * mount and super_block operations
22813 + */
22814 +
22815 +#include <linux/buffer_head.h>
22816 +#include <linux/jiffies.h>
22817 +#include <linux/module.h>
22818 +#include <linux/seq_file.h>
22819 +#include <linux/statfs.h>
22820 +#include <linux/vmalloc.h>
22821 +#include <linux/writeback.h>
22822 +#include "aufs.h"
22823 +
22824 +/*
22825 + * super_operations
22826 + */
22827 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
22828 +{
22829 +       struct au_icntnr *c;
22830 +
22831 +       c = au_cache_alloc_icntnr();
22832 +       if (c) {
22833 +               au_icntnr_init(c);
22834 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
22835 +               c->iinfo.ii_hinode = NULL;
22836 +               return &c->vfs_inode;
22837 +       }
22838 +       return NULL;
22839 +}
22840 +
22841 +static void aufs_destroy_inode_cb(struct rcu_head *head)
22842 +{
22843 +       struct inode *inode = container_of(head, struct inode, i_rcu);
22844 +
22845 +       INIT_LIST_HEAD(&inode->i_dentry);
22846 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
22847 +}
22848 +
22849 +static void aufs_destroy_inode(struct inode *inode)
22850 +{
22851 +       au_iinfo_fin(inode);
22852 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
22853 +}
22854 +
22855 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
22856 +{
22857 +       struct inode *inode;
22858 +       int err;
22859 +
22860 +       inode = iget_locked(sb, ino);
22861 +       if (unlikely(!inode)) {
22862 +               inode = ERR_PTR(-ENOMEM);
22863 +               goto out;
22864 +       }
22865 +       if (!(inode->i_state & I_NEW))
22866 +               goto out;
22867 +
22868 +       err = au_xigen_new(inode);
22869 +       if (!err)
22870 +               err = au_iinfo_init(inode);
22871 +       if (!err)
22872 +               inode->i_version++;
22873 +       else {
22874 +               iget_failed(inode);
22875 +               inode = ERR_PTR(err);
22876 +       }
22877 +
22878 +out:
22879 +       /* never return NULL */
22880 +       AuDebugOn(!inode);
22881 +       AuTraceErrPtr(inode);
22882 +       return inode;
22883 +}
22884 +
22885 +/* lock free root dinfo */
22886 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
22887 +{
22888 +       int err;
22889 +       aufs_bindex_t bindex, bend;
22890 +       struct path path;
22891 +       struct au_hdentry *hdp;
22892 +       struct au_branch *br;
22893 +       char *perm;
22894 +
22895 +       err = 0;
22896 +       bend = au_sbend(sb);
22897 +       hdp = au_di(sb->s_root)->di_hdentry;
22898 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
22899 +               br = au_sbr(sb, bindex);
22900 +               path.mnt = br->br_mnt;
22901 +               path.dentry = hdp[bindex].hd_dentry;
22902 +               err = au_seq_path(seq, &path);
22903 +               if (err > 0) {
22904 +                       perm = au_optstr_br_perm(br->br_perm);
22905 +                       if (perm) {
22906 +                               err = seq_printf(seq, "=%s", perm);
22907 +                               kfree(perm);
22908 +                               if (err == -1)
22909 +                                       err = -E2BIG;
22910 +                       } else
22911 +                               err = -ENOMEM;
22912 +               }
22913 +               if (!err && bindex != bend)
22914 +                       err = seq_putc(seq, ':');
22915 +       }
22916 +
22917 +       return err;
22918 +}
22919 +
22920 +static void au_show_wbr_create(struct seq_file *m, int v,
22921 +                              struct au_sbinfo *sbinfo)
22922 +{
22923 +       const char *pat;
22924 +
22925 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22926 +
22927 +       seq_printf(m, ",create=");
22928 +       pat = au_optstr_wbr_create(v);
22929 +       switch (v) {
22930 +       case AuWbrCreate_TDP:
22931 +       case AuWbrCreate_RR:
22932 +       case AuWbrCreate_MFS:
22933 +       case AuWbrCreate_PMFS:
22934 +               seq_printf(m, pat);
22935 +               break;
22936 +       case AuWbrCreate_MFSV:
22937 +               seq_printf(m, /*pat*/"mfs:%lu",
22938 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
22939 +                          / MSEC_PER_SEC);
22940 +               break;
22941 +       case AuWbrCreate_PMFSV:
22942 +               seq_printf(m, /*pat*/"pmfs:%lu",
22943 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
22944 +                          / MSEC_PER_SEC);
22945 +               break;
22946 +       case AuWbrCreate_MFSRR:
22947 +               seq_printf(m, /*pat*/"mfsrr:%llu",
22948 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
22949 +               break;
22950 +       case AuWbrCreate_MFSRRV:
22951 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
22952 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
22953 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
22954 +                          / MSEC_PER_SEC);
22955 +               break;
22956 +       }
22957 +}
22958 +
22959 +static int au_show_xino(struct seq_file *seq, struct vfsmount *mnt)
22960 +{
22961 +#ifdef CONFIG_SYSFS
22962 +       return 0;
22963 +#else
22964 +       int err;
22965 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
22966 +       aufs_bindex_t bindex, brid;
22967 +       struct super_block *sb;
22968 +       struct qstr *name;
22969 +       struct file *f;
22970 +       struct dentry *d, *h_root;
22971 +       struct au_hdentry *hdp;
22972 +
22973 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22974 +
22975 +       err = 0;
22976 +       sb = mnt->mnt_sb;
22977 +       f = au_sbi(sb)->si_xib;
22978 +       if (!f)
22979 +               goto out;
22980 +
22981 +       /* stop printing the default xino path on the first writable branch */
22982 +       h_root = NULL;
22983 +       brid = au_xino_brid(sb);
22984 +       if (brid >= 0) {
22985 +               bindex = au_br_index(sb, brid);
22986 +               hdp = au_di(sb->s_root)->di_hdentry;
22987 +               h_root = hdp[0 + bindex].hd_dentry;
22988 +       }
22989 +       d = f->f_dentry;
22990 +       name = &d->d_name;
22991 +       /* safe ->d_parent because the file is unlinked */
22992 +       if (d->d_parent == h_root
22993 +           && name->len == len
22994 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
22995 +               goto out;
22996 +
22997 +       seq_puts(seq, ",xino=");
22998 +       err = au_xino_path(seq, f);
22999 +
23000 +out:
23001 +       return err;
23002 +#endif
23003 +}
23004 +
23005 +/* seq_file will re-call me in case of too long string */
23006 +static int aufs_show_options(struct seq_file *m, struct vfsmount *mnt)
23007 +{
23008 +       int err;
23009 +       unsigned int mnt_flags, v;
23010 +       struct super_block *sb;
23011 +       struct au_sbinfo *sbinfo;
23012 +
23013 +#define AuBool(name, str) do { \
23014 +       v = au_opt_test(mnt_flags, name); \
23015 +       if (v != au_opt_test(AuOpt_Def, name)) \
23016 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
23017 +} while (0)
23018 +
23019 +#define AuStr(name, str) do { \
23020 +       v = mnt_flags & AuOptMask_##name; \
23021 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
23022 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
23023 +} while (0)
23024 +
23025 +#define AuUInt(name, str, val) do { \
23026 +       if (val != AUFS_##name##_DEF) \
23027 +               seq_printf(m, "," #str "=%u", val); \
23028 +} while (0)
23029 +
23030 +       /* lock free root dinfo */
23031 +       sb = mnt->mnt_sb;
23032 +       si_noflush_read_lock(sb);
23033 +       sbinfo = au_sbi(sb);
23034 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
23035 +
23036 +       mnt_flags = au_mntflags(sb);
23037 +       if (au_opt_test(mnt_flags, XINO)) {
23038 +               err = au_show_xino(m, mnt);
23039 +               if (unlikely(err))
23040 +                       goto out;
23041 +       } else
23042 +               seq_puts(m, ",noxino");
23043 +
23044 +       AuBool(TRUNC_XINO, trunc_xino);
23045 +       AuStr(UDBA, udba);
23046 +       AuBool(SHWH, shwh);
23047 +       AuBool(PLINK, plink);
23048 +       AuBool(DIO, dio);
23049 +       /* AuBool(DIRPERM1, dirperm1); */
23050 +       /* AuBool(REFROF, refrof); */
23051 +
23052 +       v = sbinfo->si_wbr_create;
23053 +       if (v != AuWbrCreate_Def)
23054 +               au_show_wbr_create(m, v, sbinfo);
23055 +
23056 +       v = sbinfo->si_wbr_copyup;
23057 +       if (v != AuWbrCopyup_Def)
23058 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
23059 +
23060 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
23061 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
23062 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
23063 +
23064 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
23065 +
23066 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
23067 +       AuUInt(RDCACHE, rdcache, v);
23068 +
23069 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
23070 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
23071 +
23072 +       AuBool(SUM, sum);
23073 +       /* AuBool(SUM_W, wsum); */
23074 +       AuBool(WARN_PERM, warn_perm);
23075 +       AuBool(VERBOSE, verbose);
23076 +
23077 +out:
23078 +       /* be sure to print "br:" last */
23079 +       if (!sysaufs_brs) {
23080 +               seq_puts(m, ",br:");
23081 +               au_show_brs(m, sb);
23082 +       }
23083 +       si_read_unlock(sb);
23084 +       return 0;
23085 +
23086 +#undef AuBool
23087 +#undef AuStr
23088 +#undef AuUInt
23089 +}
23090 +
23091 +/* ---------------------------------------------------------------------- */
23092 +
23093 +/* sum mode which returns the summation for statfs(2) */
23094 +
23095 +static u64 au_add_till_max(u64 a, u64 b)
23096 +{
23097 +       u64 old;
23098 +
23099 +       old = a;
23100 +       a += b;
23101 +       if (old < a)
23102 +               return a;
23103 +       return ULLONG_MAX;
23104 +}
23105 +
23106 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
23107 +{
23108 +       int err;
23109 +       u64 blocks, bfree, bavail, files, ffree;
23110 +       aufs_bindex_t bend, bindex, i;
23111 +       unsigned char shared;
23112 +       struct path h_path;
23113 +       struct super_block *h_sb;
23114 +
23115 +       blocks = 0;
23116 +       bfree = 0;
23117 +       bavail = 0;
23118 +       files = 0;
23119 +       ffree = 0;
23120 +
23121 +       err = 0;
23122 +       bend = au_sbend(sb);
23123 +       for (bindex = bend; bindex >= 0; bindex--) {
23124 +               h_path.mnt = au_sbr_mnt(sb, bindex);
23125 +               h_sb = h_path.mnt->mnt_sb;
23126 +               shared = 0;
23127 +               for (i = bindex + 1; !shared && i <= bend; i++)
23128 +                       shared = (au_sbr_sb(sb, i) == h_sb);
23129 +               if (shared)
23130 +                       continue;
23131 +
23132 +               /* sb->s_root for NFS is unreliable */
23133 +               h_path.dentry = h_path.mnt->mnt_root;
23134 +               err = vfs_statfs(&h_path, buf);
23135 +               if (unlikely(err))
23136 +                       goto out;
23137 +
23138 +               blocks = au_add_till_max(blocks, buf->f_blocks);
23139 +               bfree = au_add_till_max(bfree, buf->f_bfree);
23140 +               bavail = au_add_till_max(bavail, buf->f_bavail);
23141 +               files = au_add_till_max(files, buf->f_files);
23142 +               ffree = au_add_till_max(ffree, buf->f_ffree);
23143 +       }
23144 +
23145 +       buf->f_blocks = blocks;
23146 +       buf->f_bfree = bfree;
23147 +       buf->f_bavail = bavail;
23148 +       buf->f_files = files;
23149 +       buf->f_ffree = ffree;
23150 +
23151 +out:
23152 +       return err;
23153 +}
23154 +
23155 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
23156 +{
23157 +       int err;
23158 +       struct path h_path;
23159 +       struct super_block *sb;
23160 +
23161 +       /* lock free root dinfo */
23162 +       sb = dentry->d_sb;
23163 +       si_noflush_read_lock(sb);
23164 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
23165 +               /* sb->s_root for NFS is unreliable */
23166 +               h_path.mnt = au_sbr_mnt(sb, 0);
23167 +               h_path.dentry = h_path.mnt->mnt_root;
23168 +               err = vfs_statfs(&h_path, buf);
23169 +       } else
23170 +               err = au_statfs_sum(sb, buf);
23171 +       si_read_unlock(sb);
23172 +
23173 +       if (!err) {
23174 +               buf->f_type = AUFS_SUPER_MAGIC;
23175 +               buf->f_namelen = AUFS_MAX_NAMELEN;
23176 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
23177 +       }
23178 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
23179 +
23180 +       return err;
23181 +}
23182 +
23183 +/* ---------------------------------------------------------------------- */
23184 +
23185 +/* final actions when unmounting a file system */
23186 +static void aufs_put_super(struct super_block *sb)
23187 +{
23188 +       struct au_sbinfo *sbinfo;
23189 +
23190 +       sbinfo = au_sbi(sb);
23191 +       if (!sbinfo)
23192 +               return;
23193 +
23194 +       dbgaufs_si_fin(sbinfo);
23195 +       kobject_put(&sbinfo->si_kobj);
23196 +}
23197 +
23198 +/* ---------------------------------------------------------------------- */
23199 +
23200 +void au_array_free(void *array)
23201 +{
23202 +       if (array) {
23203 +               if (!is_vmalloc_addr(array))
23204 +                       kfree(array);
23205 +               else
23206 +                       vfree(array);
23207 +       }
23208 +}
23209 +
23210 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
23211 +{
23212 +       void *array;
23213 +       unsigned long long n;
23214 +
23215 +       array = NULL;
23216 +       n = 0;
23217 +       if (!*hint)
23218 +               goto out;
23219 +
23220 +       if (*hint > ULLONG_MAX / sizeof(array)) {
23221 +               array = ERR_PTR(-EMFILE);
23222 +               pr_err("hint %llu\n", *hint);
23223 +               goto out;
23224 +       }
23225 +
23226 +       array = kmalloc(sizeof(array) * *hint, GFP_NOFS);
23227 +       if (unlikely(!array))
23228 +               array = vmalloc(sizeof(array) * *hint);
23229 +       if (unlikely(!array)) {
23230 +               array = ERR_PTR(-ENOMEM);
23231 +               goto out;
23232 +       }
23233 +
23234 +       n = cb(array, *hint, arg);
23235 +       AuDebugOn(n > *hint);
23236 +
23237 +out:
23238 +       *hint = n;
23239 +       return array;
23240 +}
23241 +
23242 +static unsigned long long au_iarray_cb(void *a,
23243 +                                      unsigned long long max __maybe_unused,
23244 +                                      void *arg)
23245 +{
23246 +       unsigned long long n;
23247 +       struct inode **p, *inode;
23248 +       struct list_head *head;
23249 +
23250 +       n = 0;
23251 +       p = a;
23252 +       head = arg;
23253 +       spin_lock(&inode_sb_list_lock);
23254 +       list_for_each_entry(inode, head, i_sb_list) {
23255 +               if (!is_bad_inode(inode)
23256 +                   && au_ii(inode)->ii_bstart >= 0) {
23257 +                       spin_lock(&inode->i_lock);
23258 +                       if (atomic_read(&inode->i_count)) {
23259 +                               au_igrab(inode);
23260 +                               *p++ = inode;
23261 +                               n++;
23262 +                               AuDebugOn(n > max);
23263 +                       }
23264 +                       spin_unlock(&inode->i_lock);
23265 +               }
23266 +       }
23267 +       spin_unlock(&inode_sb_list_lock);
23268 +
23269 +       return n;
23270 +}
23271 +
23272 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
23273 +{
23274 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
23275 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
23276 +}
23277 +
23278 +void au_iarray_free(struct inode **a, unsigned long long max)
23279 +{
23280 +       unsigned long long ull;
23281 +
23282 +       for (ull = 0; ull < max; ull++)
23283 +               iput(a[ull]);
23284 +       au_array_free(a);
23285 +}
23286 +
23287 +/* ---------------------------------------------------------------------- */
23288 +
23289 +/*
23290 + * refresh dentry and inode at remount time.
23291 + */
23292 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
23293 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
23294 +                     struct dentry *parent)
23295 +{
23296 +       int err;
23297 +
23298 +       di_write_lock_child(dentry);
23299 +       di_read_lock_parent(parent, AuLock_IR);
23300 +       err = au_refresh_dentry(dentry, parent);
23301 +       if (!err && dir_flags)
23302 +               au_hn_reset(dentry->d_inode, dir_flags);
23303 +       di_read_unlock(parent, AuLock_IR);
23304 +       di_write_unlock(dentry);
23305 +
23306 +       return err;
23307 +}
23308 +
23309 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
23310 +                          struct au_sbinfo *sbinfo,
23311 +                          const unsigned int dir_flags)
23312 +{
23313 +       int err;
23314 +       struct dentry *parent;
23315 +       struct inode *inode;
23316 +
23317 +       err = 0;
23318 +       parent = dget_parent(dentry);
23319 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
23320 +               inode = dentry->d_inode;
23321 +               if (inode) {
23322 +                       if (!S_ISDIR(inode->i_mode))
23323 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
23324 +                                                parent);
23325 +                       else {
23326 +                               err = au_do_refresh(dentry, dir_flags, parent);
23327 +                               if (unlikely(err))
23328 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
23329 +                       }
23330 +               } else
23331 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
23332 +               AuDbgDentry(dentry);
23333 +       }
23334 +       dput(parent);
23335 +
23336 +       AuTraceErr(err);
23337 +       return err;
23338 +}
23339 +
23340 +static int au_refresh_d(struct super_block *sb)
23341 +{
23342 +       int err, i, j, ndentry, e;
23343 +       unsigned int sigen;
23344 +       struct au_dcsub_pages dpages;
23345 +       struct au_dpage *dpage;
23346 +       struct dentry **dentries, *d;
23347 +       struct au_sbinfo *sbinfo;
23348 +       struct dentry *root = sb->s_root;
23349 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
23350 +
23351 +       err = au_dpages_init(&dpages, GFP_NOFS);
23352 +       if (unlikely(err))
23353 +               goto out;
23354 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
23355 +       if (unlikely(err))
23356 +               goto out_dpages;
23357 +
23358 +       sigen = au_sigen(sb);
23359 +       sbinfo = au_sbi(sb);
23360 +       for (i = 0; i < dpages.ndpage; i++) {
23361 +               dpage = dpages.dpages + i;
23362 +               dentries = dpage->dentries;
23363 +               ndentry = dpage->ndentry;
23364 +               for (j = 0; j < ndentry; j++) {
23365 +                       d = dentries[j];
23366 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
23367 +                       if (unlikely(e && !err))
23368 +                               err = e;
23369 +                       /* go on even err */
23370 +               }
23371 +       }
23372 +
23373 +out_dpages:
23374 +       au_dpages_free(&dpages);
23375 +out:
23376 +       return err;
23377 +}
23378 +
23379 +static int au_refresh_i(struct super_block *sb)
23380 +{
23381 +       int err, e;
23382 +       unsigned int sigen;
23383 +       unsigned long long max, ull;
23384 +       struct inode *inode, **array;
23385 +
23386 +       array = au_iarray_alloc(sb, &max);
23387 +       err = PTR_ERR(array);
23388 +       if (IS_ERR(array))
23389 +               goto out;
23390 +
23391 +       err = 0;
23392 +       sigen = au_sigen(sb);
23393 +       for (ull = 0; ull < max; ull++) {
23394 +               inode = array[ull];
23395 +               if (au_iigen(inode) != sigen) {
23396 +                       ii_write_lock_child(inode);
23397 +                       e = au_refresh_hinode_self(inode);
23398 +                       ii_write_unlock(inode);
23399 +                       if (unlikely(e)) {
23400 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
23401 +                               if (!err)
23402 +                                       err = e;
23403 +                               /* go on even if err */
23404 +                       }
23405 +               }
23406 +       }
23407 +
23408 +       au_iarray_free(array, max);
23409 +
23410 +out:
23411 +       return err;
23412 +}
23413 +
23414 +static void au_remount_refresh(struct super_block *sb)
23415 +{
23416 +       int err, e;
23417 +       unsigned int udba;
23418 +       aufs_bindex_t bindex, bend;
23419 +       struct dentry *root;
23420 +       struct inode *inode;
23421 +       struct au_branch *br;
23422 +
23423 +       au_sigen_inc(sb);
23424 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
23425 +
23426 +       root = sb->s_root;
23427 +       DiMustNoWaiters(root);
23428 +       inode = root->d_inode;
23429 +       IiMustNoWaiters(inode);
23430 +
23431 +       udba = au_opt_udba(sb);
23432 +       bend = au_sbend(sb);
23433 +       for (bindex = 0; bindex <= bend; bindex++) {
23434 +               br = au_sbr(sb, bindex);
23435 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
23436 +               if (unlikely(err))
23437 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
23438 +                               bindex, err);
23439 +               /* go on even if err */
23440 +       }
23441 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
23442 +
23443 +       di_write_unlock(root);
23444 +       err = au_refresh_d(sb);
23445 +       e = au_refresh_i(sb);
23446 +       if (unlikely(e && !err))
23447 +               err = e;
23448 +       /* aufs_write_lock() calls ..._child() */
23449 +       di_write_lock_child(root);
23450 +
23451 +       au_cpup_attr_all(inode, /*force*/1);
23452 +
23453 +       if (unlikely(err))
23454 +               AuIOErr("refresh failed, ignored, %d\n", err);
23455 +}
23456 +
23457 +/* stop extra interpretation of errno in mount(8), and strange error messages */
23458 +static int cvt_err(int err)
23459 +{
23460 +       AuTraceErr(err);
23461 +
23462 +       switch (err) {
23463 +       case -ENOENT:
23464 +       case -ENOTDIR:
23465 +       case -EEXIST:
23466 +       case -EIO:
23467 +               err = -EINVAL;
23468 +       }
23469 +       return err;
23470 +}
23471 +
23472 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
23473 +{
23474 +       int err, do_dx;
23475 +       unsigned int mntflags;
23476 +       struct au_opts opts;
23477 +       struct dentry *root;
23478 +       struct inode *inode;
23479 +       struct au_sbinfo *sbinfo;
23480 +
23481 +       err = 0;
23482 +       root = sb->s_root;
23483 +       if (!data || !*data) {
23484 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23485 +               if (!err) {
23486 +                       di_write_lock_child(root);
23487 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
23488 +                       aufs_write_unlock(root);
23489 +               }
23490 +               goto out;
23491 +       }
23492 +
23493 +       err = -ENOMEM;
23494 +       memset(&opts, 0, sizeof(opts));
23495 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
23496 +       if (unlikely(!opts.opt))
23497 +               goto out;
23498 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
23499 +       opts.flags = AuOpts_REMOUNT;
23500 +       opts.sb_flags = *flags;
23501 +
23502 +       /* parse it before aufs lock */
23503 +       err = au_opts_parse(sb, data, &opts);
23504 +       if (unlikely(err))
23505 +               goto out_opts;
23506 +
23507 +       sbinfo = au_sbi(sb);
23508 +       inode = root->d_inode;
23509 +       mutex_lock(&inode->i_mutex);
23510 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23511 +       if (unlikely(err))
23512 +               goto out_mtx;
23513 +       di_write_lock_child(root);
23514 +
23515 +       /* au_opts_remount() may return an error */
23516 +       err = au_opts_remount(sb, &opts);
23517 +       au_opts_free(&opts);
23518 +
23519 +       if (au_ftest_opts(opts.flags, REFRESH))
23520 +               au_remount_refresh(sb);
23521 +
23522 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
23523 +               mntflags = au_mntflags(sb);
23524 +               do_dx = !!au_opt_test(mntflags, DIO);
23525 +               au_dy_arefresh(do_dx);
23526 +       }
23527 +
23528 +       aufs_write_unlock(root);
23529 +
23530 +out_mtx:
23531 +       mutex_unlock(&inode->i_mutex);
23532 +out_opts:
23533 +       free_page((unsigned long)opts.opt);
23534 +out:
23535 +       err = cvt_err(err);
23536 +       AuTraceErr(err);
23537 +       return err;
23538 +}
23539 +
23540 +static const struct super_operations aufs_sop = {
23541 +       .alloc_inode    = aufs_alloc_inode,
23542 +       .destroy_inode  = aufs_destroy_inode,
23543 +       /* always deleting, no clearing */
23544 +       .drop_inode     = generic_delete_inode,
23545 +       .show_options   = aufs_show_options,
23546 +       .statfs         = aufs_statfs,
23547 +       .put_super      = aufs_put_super,
23548 +       .remount_fs     = aufs_remount_fs
23549 +};
23550 +
23551 +/* ---------------------------------------------------------------------- */
23552 +
23553 +static int alloc_root(struct super_block *sb)
23554 +{
23555 +       int err;
23556 +       struct inode *inode;
23557 +       struct dentry *root;
23558 +
23559 +       err = -ENOMEM;
23560 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
23561 +       err = PTR_ERR(inode);
23562 +       if (IS_ERR(inode))
23563 +               goto out;
23564 +
23565 +       inode->i_op = &aufs_dir_iop;
23566 +       inode->i_fop = &aufs_dir_fop;
23567 +       inode->i_mode = S_IFDIR;
23568 +       set_nlink(inode, 2);
23569 +       unlock_new_inode(inode);
23570 +
23571 +       root = d_alloc_root(inode);
23572 +       if (unlikely(!root))
23573 +               goto out_iput;
23574 +       err = PTR_ERR(root);
23575 +       if (IS_ERR(root))
23576 +               goto out_iput;
23577 +
23578 +       err = au_di_init(root);
23579 +       if (!err) {
23580 +               sb->s_root = root;
23581 +               return 0; /* success */
23582 +       }
23583 +       dput(root);
23584 +       goto out; /* do not iput */
23585 +
23586 +out_iput:
23587 +       iget_failed(inode);
23588 +out:
23589 +       return err;
23590 +
23591 +}
23592 +
23593 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
23594 +                          int silent __maybe_unused)
23595 +{
23596 +       int err;
23597 +       struct au_opts opts;
23598 +       struct dentry *root;
23599 +       struct inode *inode;
23600 +       char *arg = raw_data;
23601 +
23602 +       if (unlikely(!arg || !*arg)) {
23603 +               err = -EINVAL;
23604 +               pr_err("no arg\n");
23605 +               goto out;
23606 +       }
23607 +
23608 +       err = -ENOMEM;
23609 +       memset(&opts, 0, sizeof(opts));
23610 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
23611 +       if (unlikely(!opts.opt))
23612 +               goto out;
23613 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
23614 +       opts.sb_flags = sb->s_flags;
23615 +
23616 +       err = au_si_alloc(sb);
23617 +       if (unlikely(err))
23618 +               goto out_opts;
23619 +
23620 +       /* all timestamps always follow the ones on the branch */
23621 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
23622 +       sb->s_op = &aufs_sop;
23623 +       sb->s_d_op = &aufs_dop;
23624 +       sb->s_magic = AUFS_SUPER_MAGIC;
23625 +       sb->s_maxbytes = 0;
23626 +       au_export_init(sb);
23627 +
23628 +       err = alloc_root(sb);
23629 +       if (unlikely(err)) {
23630 +               si_write_unlock(sb);
23631 +               goto out_info;
23632 +       }
23633 +       root = sb->s_root;
23634 +       inode = root->d_inode;
23635 +
23636 +       /*
23637 +        * actually we can parse options regardless aufs lock here.
23638 +        * but at remount time, parsing must be done before aufs lock.
23639 +        * so we follow the same rule.
23640 +        */
23641 +       ii_write_lock_parent(inode);
23642 +       aufs_write_unlock(root);
23643 +       err = au_opts_parse(sb, arg, &opts);
23644 +       if (unlikely(err))
23645 +               goto out_root;
23646 +
23647 +       /* lock vfs_inode first, then aufs. */
23648 +       mutex_lock(&inode->i_mutex);
23649 +       aufs_write_lock(root);
23650 +       err = au_opts_mount(sb, &opts);
23651 +       au_opts_free(&opts);
23652 +       aufs_write_unlock(root);
23653 +       mutex_unlock(&inode->i_mutex);
23654 +       if (!err)
23655 +               goto out_opts; /* success */
23656 +
23657 +out_root:
23658 +       dput(root);
23659 +       sb->s_root = NULL;
23660 +out_info:
23661 +       dbgaufs_si_fin(au_sbi(sb));
23662 +       kobject_put(&au_sbi(sb)->si_kobj);
23663 +       sb->s_fs_info = NULL;
23664 +out_opts:
23665 +       free_page((unsigned long)opts.opt);
23666 +out:
23667 +       AuTraceErr(err);
23668 +       err = cvt_err(err);
23669 +       AuTraceErr(err);
23670 +       return err;
23671 +}
23672 +
23673 +/* ---------------------------------------------------------------------- */
23674 +
23675 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
23676 +                                const char *dev_name __maybe_unused,
23677 +                                void *raw_data)
23678 +{
23679 +       struct dentry *root;
23680 +       struct super_block *sb;
23681 +
23682 +       /* all timestamps always follow the ones on the branch */
23683 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
23684 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
23685 +       if (IS_ERR(root))
23686 +               goto out;
23687 +
23688 +       sb = root->d_sb;
23689 +       si_write_lock(sb, !AuLock_FLUSH);
23690 +       sysaufs_brs_add(sb, 0);
23691 +       si_write_unlock(sb);
23692 +       au_sbilist_add(sb);
23693 +
23694 +out:
23695 +       return root;
23696 +}
23697 +
23698 +static void aufs_kill_sb(struct super_block *sb)
23699 +{
23700 +       struct au_sbinfo *sbinfo;
23701 +
23702 +       sbinfo = au_sbi(sb);
23703 +       if (sbinfo) {
23704 +               au_sbilist_del(sb);
23705 +               aufs_write_lock(sb->s_root);
23706 +               if (sbinfo->si_wbr_create_ops->fin)
23707 +                       sbinfo->si_wbr_create_ops->fin(sb);
23708 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
23709 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
23710 +                       au_remount_refresh(sb);
23711 +               }
23712 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23713 +                       au_plink_put(sb, /*verbose*/1);
23714 +               au_xino_clr(sb);
23715 +               sbinfo->si_sb = NULL;
23716 +               aufs_write_unlock(sb->s_root);
23717 +               au_nwt_flush(&sbinfo->si_nowait);
23718 +       }
23719 +       generic_shutdown_super(sb);
23720 +}
23721 +
23722 +struct file_system_type aufs_fs_type = {
23723 +       .name           = AUFS_FSTYPE,
23724 +       .fs_flags       =
23725 +               FS_RENAME_DOES_D_MOVE   /* a race between rename and others */
23726 +               | FS_REVAL_DOT,         /* for NFS branch and udba */
23727 +       .mount          = aufs_mount,
23728 +       .kill_sb        = aufs_kill_sb,
23729 +       /* no need to __module_get() and module_put(). */
23730 +       .owner          = THIS_MODULE,
23731 +};
23732 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
23733 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
23734 +++ linux/fs/aufs/super.h       2011-08-24 13:30:24.734646739 +0200
23735 @@ -0,0 +1,547 @@
23736 +/*
23737 + * Copyright (C) 2005-2011 Junjiro R. Okajima
23738 + *
23739 + * This program, aufs is free software; you can redistribute it and/or modify
23740 + * it under the terms of the GNU General Public License as published by
23741 + * the Free Software Foundation; either version 2 of the License, or
23742 + * (at your option) any later version.
23743 + *
23744 + * This program is distributed in the hope that it will be useful,
23745 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23746 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23747 + * GNU General Public License for more details.
23748 + *
23749 + * You should have received a copy of the GNU General Public License
23750 + * along with this program; if not, write to the Free Software
23751 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23752 + */
23753 +
23754 +/*
23755 + * super_block operations
23756 + */
23757 +
23758 +#ifndef __AUFS_SUPER_H__
23759 +#define __AUFS_SUPER_H__
23760 +
23761 +#ifdef __KERNEL__
23762 +
23763 +#include <linux/fs.h>
23764 +#include <linux/aufs_type.h>
23765 +#include "rwsem.h"
23766 +#include "spl.h"
23767 +#include "wkq.h"
23768 +
23769 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
23770 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
23771 +                              loff_t *);
23772 +
23773 +/* policies to select one among multiple writable branches */
23774 +struct au_wbr_copyup_operations {
23775 +       int (*copyup)(struct dentry *dentry);
23776 +};
23777 +
23778 +struct au_wbr_create_operations {
23779 +       int (*create)(struct dentry *dentry, int isdir);
23780 +       int (*init)(struct super_block *sb);
23781 +       int (*fin)(struct super_block *sb);
23782 +};
23783 +
23784 +struct au_wbr_mfs {
23785 +       struct mutex    mfs_lock; /* protect this structure */
23786 +       unsigned long   mfs_jiffy;
23787 +       unsigned long   mfs_expire;
23788 +       aufs_bindex_t   mfs_bindex;
23789 +
23790 +       unsigned long long      mfsrr_bytes;
23791 +       unsigned long long      mfsrr_watermark;
23792 +};
23793 +
23794 +struct au_branch;
23795 +struct au_sbinfo {
23796 +       /* nowait tasks in the system-wide workqueue */
23797 +       struct au_nowait_tasks  si_nowait;
23798 +
23799 +       /*
23800 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
23801 +        * rwsem for au_sbinfo is necessary.
23802 +        */
23803 +       struct au_rwsem         si_rwsem;
23804 +
23805 +       /* prevent recursive locking in deleting inode */
23806 +       struct {
23807 +               unsigned long           *bitmap;
23808 +               spinlock_t              tree_lock;
23809 +               struct radix_tree_root  tree;
23810 +       } au_si_pid;
23811 +
23812 +       /*
23813 +        * dirty approach to protect sb->sb_inodes and ->s_files from remount.
23814 +        */
23815 +       atomic_long_t           si_ninodes, si_nfiles;
23816 +
23817 +       /* branch management */
23818 +       unsigned int            si_generation;
23819 +
23820 +       /* see above flags */
23821 +       unsigned char           au_si_status;
23822 +
23823 +       aufs_bindex_t           si_bend;
23824 +
23825 +       /* dirty trick to keep br_id plus */
23826 +       unsigned int            si_last_br_id :
23827 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
23828 +       struct au_branch        **si_branch;
23829 +
23830 +       /* policy to select a writable branch */
23831 +       unsigned char           si_wbr_copyup;
23832 +       unsigned char           si_wbr_create;
23833 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
23834 +       struct au_wbr_create_operations *si_wbr_create_ops;
23835 +
23836 +       /* round robin */
23837 +       atomic_t                si_wbr_rr_next;
23838 +
23839 +       /* most free space */
23840 +       struct au_wbr_mfs       si_wbr_mfs;
23841 +
23842 +       /* mount flags */
23843 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
23844 +       unsigned int            si_mntflags;
23845 +
23846 +       /* external inode number (bitmap and translation table) */
23847 +       au_readf_t              si_xread;
23848 +       au_writef_t             si_xwrite;
23849 +       struct file             *si_xib;
23850 +       struct mutex            si_xib_mtx; /* protect xib members */
23851 +       unsigned long           *si_xib_buf;
23852 +       unsigned long           si_xib_last_pindex;
23853 +       int                     si_xib_next_bit;
23854 +       aufs_bindex_t           si_xino_brid;
23855 +       /* reserved for future use */
23856 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
23857 +
23858 +#ifdef CONFIG_AUFS_EXPORT
23859 +       /* i_generation */
23860 +       struct file             *si_xigen;
23861 +       atomic_t                si_xigen_next;
23862 +#endif
23863 +
23864 +       /* vdir parameters */
23865 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
23866 +       unsigned int            si_rdblk;       /* deblk size */
23867 +       unsigned int            si_rdhash;      /* hash size */
23868 +
23869 +       /*
23870 +        * If the number of whiteouts are larger than si_dirwh, leave all of
23871 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
23872 +        * future fsck.aufs or kernel thread will remove them later.
23873 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
23874 +        */
23875 +       unsigned int            si_dirwh;
23876 +
23877 +       /*
23878 +        * rename(2) a directory with all children.
23879 +        */
23880 +       /* reserved for future use */
23881 +       /* int                  si_rendir; */
23882 +
23883 +       /* pseudo_link list */
23884 +       struct au_splhead       si_plink;
23885 +       wait_queue_head_t       si_plink_wq;
23886 +       spinlock_t              si_plink_maint_lock;
23887 +       pid_t                   si_plink_maint_pid;
23888 +
23889 +       /*
23890 +        * sysfs and lifetime management.
23891 +        * this is not a small structure and it may be a waste of memory in case
23892 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
23893 +        * but using sysfs is majority.
23894 +        */
23895 +       struct kobject          si_kobj;
23896 +#ifdef CONFIG_DEBUG_FS
23897 +       struct dentry            *si_dbgaufs, *si_dbgaufs_xib;
23898 +#ifdef CONFIG_AUFS_EXPORT
23899 +       struct dentry            *si_dbgaufs_xigen;
23900 +#endif
23901 +#endif
23902 +
23903 +#ifdef CONFIG_AUFS_SBILIST
23904 +       struct list_head        si_list;
23905 +#endif
23906 +
23907 +       /* dirty, necessary for unmounting, sysfs and sysrq */
23908 +       struct super_block      *si_sb;
23909 +};
23910 +
23911 +/* sbinfo status flags */
23912 +/*
23913 + * set true when refresh_dirs() failed at remount time.
23914 + * then try refreshing dirs at access time again.
23915 + * if it is false, refreshing dirs at access time is unnecesary
23916 + */
23917 +#define AuSi_FAILED_REFRESH_DIR        1
23918 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
23919 +                                          unsigned int flag)
23920 +{
23921 +       AuRwMustAnyLock(&sbi->si_rwsem);
23922 +       return sbi->au_si_status & flag;
23923 +}
23924 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
23925 +#define au_fset_si(sbinfo, name) do { \
23926 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
23927 +       (sbinfo)->au_si_status |= AuSi_##name; \
23928 +} while (0)
23929 +#define au_fclr_si(sbinfo, name) do { \
23930 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
23931 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
23932 +} while (0)
23933 +
23934 +/* ---------------------------------------------------------------------- */
23935 +
23936 +/* policy to select one among writable branches */
23937 +#define AuWbrCopyup(sbinfo, ...) \
23938 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
23939 +#define AuWbrCreate(sbinfo, ...) \
23940 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
23941 +
23942 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
23943 +#define AuLock_DW              1               /* write-lock dentry */
23944 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
23945 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
23946 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
23947 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
23948 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
23949 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
23950 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
23951 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
23952 +#define au_fset_lock(flags, name) \
23953 +       do { (flags) |= AuLock_##name; } while (0)
23954 +#define au_fclr_lock(flags, name) \
23955 +       do { (flags) &= ~AuLock_##name; } while (0)
23956 +
23957 +/* ---------------------------------------------------------------------- */
23958 +
23959 +/* super.c */
23960 +extern struct file_system_type aufs_fs_type;
23961 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
23962 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
23963 +                                          void *arg);
23964 +void au_array_free(void *array);
23965 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
23966 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
23967 +void au_iarray_free(struct inode **a, unsigned long long max);
23968 +
23969 +/* sbinfo.c */
23970 +void au_si_free(struct kobject *kobj);
23971 +int au_si_alloc(struct super_block *sb);
23972 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
23973 +
23974 +unsigned int au_sigen_inc(struct super_block *sb);
23975 +aufs_bindex_t au_new_br_id(struct super_block *sb);
23976 +
23977 +int si_read_lock(struct super_block *sb, int flags);
23978 +int si_write_lock(struct super_block *sb, int flags);
23979 +int aufs_read_lock(struct dentry *dentry, int flags);
23980 +void aufs_read_unlock(struct dentry *dentry, int flags);
23981 +void aufs_write_lock(struct dentry *dentry);
23982 +void aufs_write_unlock(struct dentry *dentry);
23983 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
23984 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
23985 +
23986 +int si_pid_test_slow(struct super_block *sb);
23987 +void si_pid_set_slow(struct super_block *sb);
23988 +void si_pid_clr_slow(struct super_block *sb);
23989 +
23990 +/* wbr_policy.c */
23991 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
23992 +extern struct au_wbr_create_operations au_wbr_create_ops[];
23993 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
23994 +
23995 +/* ---------------------------------------------------------------------- */
23996 +
23997 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
23998 +{
23999 +       return sb->s_fs_info;
24000 +}
24001 +
24002 +/* ---------------------------------------------------------------------- */
24003 +
24004 +#ifdef CONFIG_AUFS_EXPORT
24005 +void au_export_init(struct super_block *sb);
24006 +
24007 +static inline int au_test_nfsd(void)
24008 +{
24009 +       struct task_struct *tsk = current;
24010 +
24011 +       return (tsk->flags & PF_KTHREAD)
24012 +               && !strcmp(tsk->comm, "nfsd");
24013 +}
24014 +
24015 +void au_xigen_inc(struct inode *inode);
24016 +int au_xigen_new(struct inode *inode);
24017 +int au_xigen_set(struct super_block *sb, struct file *base);
24018 +void au_xigen_clr(struct super_block *sb);
24019 +
24020 +static inline int au_busy_or_stale(void)
24021 +{
24022 +       if (!au_test_nfsd())
24023 +               return -EBUSY;
24024 +       return -ESTALE;
24025 +}
24026 +#else
24027 +AuStubVoid(au_export_init, struct super_block *sb)
24028 +AuStubInt0(au_test_nfsd, void)
24029 +AuStubVoid(au_xigen_inc, struct inode *inode)
24030 +AuStubInt0(au_xigen_new, struct inode *inode)
24031 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
24032 +AuStubVoid(au_xigen_clr, struct super_block *sb)
24033 +static inline int au_busy_or_stale(void)
24034 +{
24035 +       return -EBUSY;
24036 +}
24037 +#endif /* CONFIG_AUFS_EXPORT */
24038 +
24039 +/* ---------------------------------------------------------------------- */
24040 +
24041 +#ifdef CONFIG_AUFS_SBILIST
24042 +/* module.c */
24043 +extern struct au_splhead au_sbilist;
24044 +
24045 +static inline void au_sbilist_init(void)
24046 +{
24047 +       au_spl_init(&au_sbilist);
24048 +}
24049 +
24050 +static inline void au_sbilist_add(struct super_block *sb)
24051 +{
24052 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
24053 +}
24054 +
24055 +static inline void au_sbilist_del(struct super_block *sb)
24056 +{
24057 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
24058 +}
24059 +
24060 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
24061 +static inline void au_sbilist_lock(void)
24062 +{
24063 +       spin_lock(&au_sbilist.spin);
24064 +}
24065 +
24066 +static inline void au_sbilist_unlock(void)
24067 +{
24068 +       spin_unlock(&au_sbilist.spin);
24069 +}
24070 +#define AuGFP_SBILIST  GFP_ATOMIC
24071 +#else
24072 +AuStubVoid(au_sbilist_lock, void)
24073 +AuStubVoid(au_sbilist_unlock, void)
24074 +#define AuGFP_SBILIST  GFP_NOFS
24075 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
24076 +#else
24077 +AuStubVoid(au_sbilist_init, void)
24078 +AuStubVoid(au_sbilist_add, struct super_block*)
24079 +AuStubVoid(au_sbilist_del, struct super_block*)
24080 +AuStubVoid(au_sbilist_lock, void)
24081 +AuStubVoid(au_sbilist_unlock, void)
24082 +#define AuGFP_SBILIST  GFP_NOFS
24083 +#endif
24084 +
24085 +/* ---------------------------------------------------------------------- */
24086 +
24087 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
24088 +{
24089 +       /*
24090 +        * This function is a dynamic '__init' fucntion actually,
24091 +        * so the tiny check for si_rwsem is unnecessary.
24092 +        */
24093 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
24094 +#ifdef CONFIG_DEBUG_FS
24095 +       sbinfo->si_dbgaufs = NULL;
24096 +       sbinfo->si_dbgaufs_xib = NULL;
24097 +#ifdef CONFIG_AUFS_EXPORT
24098 +       sbinfo->si_dbgaufs_xigen = NULL;
24099 +#endif
24100 +#endif
24101 +}
24102 +
24103 +/* ---------------------------------------------------------------------- */
24104 +
24105 +static inline pid_t si_pid_bit(void)
24106 +{
24107 +       /* the origin of pid is 1, but the bitmap's is 0 */
24108 +       return current->pid - 1;
24109 +}
24110 +
24111 +static inline int si_pid_test(struct super_block *sb)
24112 +{
24113 +       pid_t bit = si_pid_bit();
24114 +       if (bit < PID_MAX_DEFAULT)
24115 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
24116 +       else
24117 +               return si_pid_test_slow(sb);
24118 +}
24119 +
24120 +static inline void si_pid_set(struct super_block *sb)
24121 +{
24122 +       pid_t bit = si_pid_bit();
24123 +       if (bit < PID_MAX_DEFAULT) {
24124 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
24125 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
24126 +               /* smp_mb(); */
24127 +       } else
24128 +               si_pid_set_slow(sb);
24129 +}
24130 +
24131 +static inline void si_pid_clr(struct super_block *sb)
24132 +{
24133 +       pid_t bit = si_pid_bit();
24134 +       if (bit < PID_MAX_DEFAULT) {
24135 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
24136 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
24137 +               /* smp_mb(); */
24138 +       } else
24139 +               si_pid_clr_slow(sb);
24140 +}
24141 +
24142 +/* ---------------------------------------------------------------------- */
24143 +
24144 +/* lock superblock. mainly for entry point functions */
24145 +/*
24146 + * __si_read_lock, __si_write_lock,
24147 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
24148 + */
24149 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
24150 +
24151 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
24152 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
24153 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
24154 +
24155 +static inline void si_noflush_read_lock(struct super_block *sb)
24156 +{
24157 +       __si_read_lock(sb);
24158 +       si_pid_set(sb);
24159 +}
24160 +
24161 +static inline int si_noflush_read_trylock(struct super_block *sb)
24162 +{
24163 +       int locked = __si_read_trylock(sb);
24164 +       if (locked)
24165 +               si_pid_set(sb);
24166 +       return locked;
24167 +}
24168 +
24169 +static inline void si_noflush_write_lock(struct super_block *sb)
24170 +{
24171 +       __si_write_lock(sb);
24172 +       si_pid_set(sb);
24173 +}
24174 +
24175 +static inline int si_noflush_write_trylock(struct super_block *sb)
24176 +{
24177 +       int locked = __si_write_trylock(sb);
24178 +       if (locked)
24179 +               si_pid_set(sb);
24180 +       return locked;
24181 +}
24182 +
24183 +#if 0 /* unused */
24184 +static inline int si_read_trylock(struct super_block *sb, int flags)
24185 +{
24186 +       if (au_ftest_lock(flags, FLUSH))
24187 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
24188 +       return si_noflush_read_trylock(sb);
24189 +}
24190 +#endif
24191 +
24192 +static inline void si_read_unlock(struct super_block *sb)
24193 +{
24194 +       si_pid_clr(sb);
24195 +       __si_read_unlock(sb);
24196 +}
24197 +
24198 +#if 0 /* unused */
24199 +static inline int si_write_trylock(struct super_block *sb, int flags)
24200 +{
24201 +       if (au_ftest_lock(flags, FLUSH))
24202 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
24203 +       return si_noflush_write_trylock(sb);
24204 +}
24205 +#endif
24206 +
24207 +static inline void si_write_unlock(struct super_block *sb)
24208 +{
24209 +       si_pid_clr(sb);
24210 +       __si_write_unlock(sb);
24211 +}
24212 +
24213 +#if 0 /* unused */
24214 +static inline void si_downgrade_lock(struct super_block *sb)
24215 +{
24216 +       __si_downgrade_lock(sb);
24217 +}
24218 +#endif
24219 +
24220 +/* ---------------------------------------------------------------------- */
24221 +
24222 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
24223 +{
24224 +       SiMustAnyLock(sb);
24225 +       return au_sbi(sb)->si_bend;
24226 +}
24227 +
24228 +static inline unsigned int au_mntflags(struct super_block *sb)
24229 +{
24230 +       SiMustAnyLock(sb);
24231 +       return au_sbi(sb)->si_mntflags;
24232 +}
24233 +
24234 +static inline unsigned int au_sigen(struct super_block *sb)
24235 +{
24236 +       SiMustAnyLock(sb);
24237 +       return au_sbi(sb)->si_generation;
24238 +}
24239 +
24240 +static inline void au_ninodes_inc(struct super_block *sb)
24241 +{
24242 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
24243 +}
24244 +
24245 +static inline void au_ninodes_dec(struct super_block *sb)
24246 +{
24247 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
24248 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
24249 +}
24250 +
24251 +static inline void au_nfiles_inc(struct super_block *sb)
24252 +{
24253 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
24254 +}
24255 +
24256 +static inline void au_nfiles_dec(struct super_block *sb)
24257 +{
24258 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
24259 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
24260 +}
24261 +
24262 +static inline struct au_branch *au_sbr(struct super_block *sb,
24263 +                                      aufs_bindex_t bindex)
24264 +{
24265 +       SiMustAnyLock(sb);
24266 +       return au_sbi(sb)->si_branch[0 + bindex];
24267 +}
24268 +
24269 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
24270 +{
24271 +       SiMustWriteLock(sb);
24272 +       au_sbi(sb)->si_xino_brid = brid;
24273 +}
24274 +
24275 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
24276 +{
24277 +       SiMustAnyLock(sb);
24278 +       return au_sbi(sb)->si_xino_brid;
24279 +}
24280 +
24281 +#endif /* __KERNEL__ */
24282 +#endif /* __AUFS_SUPER_H__ */
24283 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
24284 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
24285 +++ linux/fs/aufs/sysaufs.c     2011-08-24 13:30:24.734646739 +0200
24286 @@ -0,0 +1,107 @@
24287 +/*
24288 + * Copyright (C) 2005-2011 Junjiro R. Okajima
24289 + *
24290 + * This program, aufs is free software; you can redistribute it and/or modify
24291 + * it under the terms of the GNU General Public License as published by
24292 + * the Free Software Foundation; either version 2 of the License, or
24293 + * (at your option) any later version.
24294 + *
24295 + * This program is distributed in the hope that it will be useful,
24296 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24297 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24298 + * GNU General Public License for more details.
24299 + *
24300 + * You should have received a copy of the GNU General Public License
24301 + * along with this program; if not, write to the Free Software
24302 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24303 + */
24304 +
24305 +/*
24306 + * sysfs interface and lifetime management
24307 + * they are necessary regardless sysfs is disabled.
24308 + */
24309 +
24310 +#include <linux/fs.h>
24311 +#include <linux/random.h>
24312 +#include <linux/sysfs.h>
24313 +#include "aufs.h"
24314 +
24315 +unsigned long sysaufs_si_mask;
24316 +struct kset *sysaufs_kset;
24317 +
24318 +#define AuSiAttr(_name) { \
24319 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
24320 +       .show   = sysaufs_si_##_name,                           \
24321 +}
24322 +
24323 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
24324 +struct attribute *sysaufs_si_attrs[] = {
24325 +       &sysaufs_si_attr_xi_path.attr,
24326 +       NULL,
24327 +};
24328 +
24329 +static const struct sysfs_ops au_sbi_ops = {
24330 +       .show   = sysaufs_si_show
24331 +};
24332 +
24333 +static struct kobj_type au_sbi_ktype = {
24334 +       .release        = au_si_free,
24335 +       .sysfs_ops      = &au_sbi_ops,
24336 +       .default_attrs  = sysaufs_si_attrs
24337 +};
24338 +
24339 +/* ---------------------------------------------------------------------- */
24340 +
24341 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
24342 +{
24343 +       int err;
24344 +
24345 +       sbinfo->si_kobj.kset = sysaufs_kset;
24346 +       /* cf. sysaufs_name() */
24347 +       err = kobject_init_and_add
24348 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
24349 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
24350 +
24351 +       dbgaufs_si_null(sbinfo);
24352 +       if (!err) {
24353 +               err = dbgaufs_si_init(sbinfo);
24354 +               if (unlikely(err))
24355 +                       kobject_put(&sbinfo->si_kobj);
24356 +       }
24357 +       return err;
24358 +}
24359 +
24360 +void sysaufs_fin(void)
24361 +{
24362 +       dbgaufs_fin();
24363 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
24364 +       kset_unregister(sysaufs_kset);
24365 +}
24366 +
24367 +int __init sysaufs_init(void)
24368 +{
24369 +       int err;
24370 +
24371 +       do {
24372 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
24373 +       } while (!sysaufs_si_mask);
24374 +
24375 +       err = -EINVAL;
24376 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
24377 +       if (unlikely(!sysaufs_kset))
24378 +               goto out;
24379 +       err = PTR_ERR(sysaufs_kset);
24380 +       if (IS_ERR(sysaufs_kset))
24381 +               goto out;
24382 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
24383 +       if (unlikely(err)) {
24384 +               kset_unregister(sysaufs_kset);
24385 +               goto out;
24386 +       }
24387 +
24388 +       err = dbgaufs_init();
24389 +       if (unlikely(err))
24390 +               sysaufs_fin();
24391 +out:
24392 +       return err;
24393 +}
24394 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
24395 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
24396 +++ linux/fs/aufs/sysaufs.h     2011-08-24 13:30:24.734646739 +0200
24397 @@ -0,0 +1,105 @@
24398 +/*
24399 + * Copyright (C) 2005-2011 Junjiro R. Okajima
24400 + *
24401 + * This program, aufs is free software; you can redistribute it and/or modify
24402 + * it under the terms of the GNU General Public License as published by
24403 + * the Free Software Foundation; either version 2 of the License, or
24404 + * (at your option) any later version.
24405 + *
24406 + * This program is distributed in the hope that it will be useful,
24407 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24408 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24409 + * GNU General Public License for more details.
24410 + *
24411 + * You should have received a copy of the GNU General Public License
24412 + * along with this program; if not, write to the Free Software
24413 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24414 + */
24415 +
24416 +/*
24417 + * sysfs interface and mount lifetime management
24418 + */
24419 +
24420 +#ifndef __SYSAUFS_H__
24421 +#define __SYSAUFS_H__
24422 +
24423 +#ifdef __KERNEL__
24424 +
24425 +#include <linux/sysfs.h>
24426 +#include <linux/aufs_type.h>
24427 +#include "module.h"
24428 +
24429 +struct super_block;
24430 +struct au_sbinfo;
24431 +
24432 +struct sysaufs_si_attr {
24433 +       struct attribute attr;
24434 +       int (*show)(struct seq_file *seq, struct super_block *sb);
24435 +};
24436 +
24437 +/* ---------------------------------------------------------------------- */
24438 +
24439 +/* sysaufs.c */
24440 +extern unsigned long sysaufs_si_mask;
24441 +extern struct kset *sysaufs_kset;
24442 +extern struct attribute *sysaufs_si_attrs[];
24443 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
24444 +int __init sysaufs_init(void);
24445 +void sysaufs_fin(void);
24446 +
24447 +/* ---------------------------------------------------------------------- */
24448 +
24449 +/* some people doesn't like to show a pointer in kernel */
24450 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
24451 +{
24452 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
24453 +}
24454 +
24455 +#define SysaufsSiNamePrefix    "si_"
24456 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
24457 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
24458 +{
24459 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
24460 +                sysaufs_si_id(sbinfo));
24461 +}
24462 +
24463 +struct au_branch;
24464 +#ifdef CONFIG_SYSFS
24465 +/* sysfs.c */
24466 +extern struct attribute_group *sysaufs_attr_group;
24467 +
24468 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
24469 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
24470 +                        char *buf);
24471 +
24472 +void sysaufs_br_init(struct au_branch *br);
24473 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
24474 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
24475 +
24476 +#define sysaufs_brs_init()     do {} while (0)
24477 +
24478 +#else
24479 +#define sysaufs_attr_group     NULL
24480 +
24481 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
24482 +
24483 +static inline
24484 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
24485 +                        char *buf)
24486 +{
24487 +       return 0;
24488 +}
24489 +
24490 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
24491 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
24492 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
24493 +
24494 +static inline void sysaufs_brs_init(void)
24495 +{
24496 +       sysaufs_brs = 0;
24497 +}
24498 +
24499 +#endif /* CONFIG_SYSFS */
24500 +
24501 +#endif /* __KERNEL__ */
24502 +#endif /* __SYSAUFS_H__ */
24503 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
24504 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
24505 +++ linux/fs/aufs/sysfs.c       2012-01-05 11:00:59.218835185 +0100
24506 @@ -0,0 +1,260 @@
24507 +/*
24508 + * Copyright (C) 2005-2011 Junjiro R. Okajima
24509 + *
24510 + * This program, aufs is free software; you can redistribute it and/or modify
24511 + * it under the terms of the GNU General Public License as published by
24512 + * the Free Software Foundation; either version 2 of the License, or
24513 + * (at your option) any later version.
24514 + *
24515 + * This program is distributed in the hope that it will be useful,
24516 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24517 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24518 + * GNU General Public License for more details.
24519 + *
24520 + * You should have received a copy of the GNU General Public License
24521 + * along with this program; if not, write to the Free Software
24522 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24523 + */
24524 +
24525 +/*
24526 + * sysfs interface
24527 + */
24528 +
24529 +#include <linux/fs.h>
24530 +#include <linux/module.h>
24531 +#include <linux/seq_file.h>
24532 +#include <linux/sysfs.h>
24533 +#include "aufs.h"
24534 +
24535 +#ifdef CONFIG_AUFS_FS_MODULE
24536 +/* this entry violates the "one line per file" policy of sysfs */
24537 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
24538 +                          char *buf)
24539 +{
24540 +       ssize_t err;
24541 +       static char *conf =
24542 +/* this file is generated at compiling */
24543 +#include "conf.str"
24544 +               ;
24545 +
24546 +       err = snprintf(buf, PAGE_SIZE, conf);
24547 +       if (unlikely(err >= PAGE_SIZE))
24548 +               err = -EFBIG;
24549 +       return err;
24550 +}
24551 +
24552 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
24553 +#endif
24554 +
24555 +static struct attribute *au_attr[] = {
24556 +#ifdef CONFIG_AUFS_FS_MODULE
24557 +       &au_config_attr.attr,
24558 +#endif
24559 +       NULL,   /* need to NULL terminate the list of attributes */
24560 +};
24561 +
24562 +static struct attribute_group sysaufs_attr_group_body = {
24563 +       .attrs = au_attr
24564 +};
24565 +
24566 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
24567 +
24568 +/* ---------------------------------------------------------------------- */
24569 +
24570 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
24571 +{
24572 +       int err;
24573 +
24574 +       SiMustAnyLock(sb);
24575 +
24576 +       err = 0;
24577 +       if (au_opt_test(au_mntflags(sb), XINO)) {
24578 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
24579 +               seq_putc(seq, '\n');
24580 +       }
24581 +       return err;
24582 +}
24583 +
24584 +/*
24585 + * the lifetime of branch is independent from the entry under sysfs.
24586 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
24587 + * unlinked.
24588 + */
24589 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
24590 +                        aufs_bindex_t bindex)
24591 +{
24592 +       int err;
24593 +       struct path path;
24594 +       struct dentry *root;
24595 +       struct au_branch *br;
24596 +       char *perm;
24597 +
24598 +       AuDbg("b%d\n", bindex);
24599 +
24600 +       err = 0;
24601 +       root = sb->s_root;
24602 +       di_read_lock_parent(root, !AuLock_IR);
24603 +       br = au_sbr(sb, bindex);
24604 +       path.mnt = br->br_mnt;
24605 +       path.dentry = au_h_dptr(root, bindex);
24606 +       au_seq_path(seq, &path);
24607 +       di_read_unlock(root, !AuLock_IR);
24608 +       perm = au_optstr_br_perm(br->br_perm);
24609 +       if (perm) {
24610 +               err = seq_printf(seq, "=%s\n", perm);
24611 +               kfree(perm);
24612 +               if (err == -1)
24613 +                       err = -E2BIG;
24614 +       } else
24615 +               err = -ENOMEM;
24616 +       return err;
24617 +}
24618 +
24619 +/* ---------------------------------------------------------------------- */
24620 +
24621 +static struct seq_file *au_seq(char *p, ssize_t len)
24622 +{
24623 +       struct seq_file *seq;
24624 +
24625 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
24626 +       if (seq) {
24627 +               /* mutex_init(&seq.lock); */
24628 +               seq->buf = p;
24629 +               seq->size = len;
24630 +               return seq; /* success */
24631 +       }
24632 +
24633 +       seq = ERR_PTR(-ENOMEM);
24634 +       return seq;
24635 +}
24636 +
24637 +#define SysaufsBr_PREFIX "br"
24638 +
24639 +/* todo: file size may exceed PAGE_SIZE */
24640 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
24641 +                       char *buf)
24642 +{
24643 +       ssize_t err;
24644 +       long l;
24645 +       aufs_bindex_t bend;
24646 +       struct au_sbinfo *sbinfo;
24647 +       struct super_block *sb;
24648 +       struct seq_file *seq;
24649 +       char *name;
24650 +       struct attribute **cattr;
24651 +
24652 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
24653 +       sb = sbinfo->si_sb;
24654 +
24655 +       /*
24656 +        * prevent a race condition between sysfs and aufs.
24657 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
24658 +        * prohibits maintaining the sysfs entries.
24659 +        * hew we acquire read lock after sysfs_get_active_two().
24660 +        * on the other hand, the remount process may maintain the sysfs/aufs
24661 +        * entries after acquiring write lock.
24662 +        * it can cause a deadlock.
24663 +        * simply we gave up processing read here.
24664 +        */
24665 +       err = -EBUSY;
24666 +       if (unlikely(!si_noflush_read_trylock(sb)))
24667 +               goto out;
24668 +
24669 +       seq = au_seq(buf, PAGE_SIZE);
24670 +       err = PTR_ERR(seq);
24671 +       if (IS_ERR(seq))
24672 +               goto out_unlock;
24673 +
24674 +       name = (void *)attr->name;
24675 +       cattr = sysaufs_si_attrs;
24676 +       while (*cattr) {
24677 +               if (!strcmp(name, (*cattr)->name)) {
24678 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
24679 +                               ->show(seq, sb);
24680 +                       goto out_seq;
24681 +               }
24682 +               cattr++;
24683 +       }
24684 +
24685 +       bend = au_sbend(sb);
24686 +       if (!strncmp(name, SysaufsBr_PREFIX, sizeof(SysaufsBr_PREFIX) - 1)) {
24687 +               name += sizeof(SysaufsBr_PREFIX) - 1;
24688 +               err = kstrtol(name, 10, &l);
24689 +               if (!err) {
24690 +                       if (l <= bend)
24691 +                               err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l);
24692 +                       else
24693 +                               err = -ENOENT;
24694 +               }
24695 +               goto out_seq;
24696 +       }
24697 +       BUG();
24698 +
24699 +out_seq:
24700 +       if (!err) {
24701 +               err = seq->count;
24702 +               /* sysfs limit */
24703 +               if (unlikely(err == PAGE_SIZE))
24704 +                       err = -EFBIG;
24705 +       }
24706 +       kfree(seq);
24707 +out_unlock:
24708 +       si_read_unlock(sb);
24709 +out:
24710 +       return err;
24711 +}
24712 +
24713 +/* ---------------------------------------------------------------------- */
24714 +
24715 +void sysaufs_br_init(struct au_branch *br)
24716 +{
24717 +       struct attribute *attr = &br->br_attr;
24718 +
24719 +       sysfs_attr_init(attr);
24720 +       attr->name = br->br_name;
24721 +       attr->mode = S_IRUGO;
24722 +}
24723 +
24724 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
24725 +{
24726 +       struct au_branch *br;
24727 +       struct kobject *kobj;
24728 +       aufs_bindex_t bend;
24729 +
24730 +       dbgaufs_brs_del(sb, bindex);
24731 +
24732 +       if (!sysaufs_brs)
24733 +               return;
24734 +
24735 +       kobj = &au_sbi(sb)->si_kobj;
24736 +       bend = au_sbend(sb);
24737 +       for (; bindex <= bend; bindex++) {
24738 +               br = au_sbr(sb, bindex);
24739 +               sysfs_remove_file(kobj, &br->br_attr);
24740 +       }
24741 +}
24742 +
24743 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
24744 +{
24745 +       int err;
24746 +       aufs_bindex_t bend;
24747 +       struct kobject *kobj;
24748 +       struct au_branch *br;
24749 +
24750 +       dbgaufs_brs_add(sb, bindex);
24751 +
24752 +       if (!sysaufs_brs)
24753 +               return;
24754 +
24755 +       kobj = &au_sbi(sb)->si_kobj;
24756 +       bend = au_sbend(sb);
24757 +       for (; bindex <= bend; bindex++) {
24758 +               br = au_sbr(sb, bindex);
24759 +               snprintf(br->br_name, sizeof(br->br_name), SysaufsBr_PREFIX
24760 +                        "%d", bindex);
24761 +               err = sysfs_create_file(kobj, &br->br_attr);
24762 +               if (unlikely(err))
24763 +                       pr_warning("failed %s under sysfs(%d)\n",
24764 +                                  br->br_name, err);
24765 +       }
24766 +}
24767 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
24768 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
24769 +++ linux/fs/aufs/sysrq.c       2011-08-24 13:30:24.734646739 +0200
24770 @@ -0,0 +1,151 @@
24771 +/*
24772 + * Copyright (C) 2005-2011 Junjiro R. Okajima
24773 + *
24774 + * This program, aufs is free software; you can redistribute it and/or modify
24775 + * it under the terms of the GNU General Public License as published by
24776 + * the Free Software Foundation; either version 2 of the License, or
24777 + * (at your option) any later version.
24778 + *
24779 + * This program is distributed in the hope that it will be useful,
24780 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24781 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24782 + * GNU General Public License for more details.
24783 + *
24784 + * You should have received a copy of the GNU General Public License
24785 + * along with this program; if not, write to the Free Software
24786 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24787 + */
24788 +
24789 +/*
24790 + * magic sysrq hanlder
24791 + */
24792 +
24793 +#include <linux/fs.h>
24794 +#include <linux/module.h>
24795 +#include <linux/moduleparam.h>
24796 +/* #include <linux/sysrq.h> */
24797 +#include <linux/writeback.h>
24798 +#include "aufs.h"
24799 +
24800 +/* ---------------------------------------------------------------------- */
24801 +
24802 +static void sysrq_sb(struct super_block *sb)
24803 +{
24804 +       char *plevel;
24805 +       struct au_sbinfo *sbinfo;
24806 +       struct file *file;
24807 +
24808 +       plevel = au_plevel;
24809 +       au_plevel = KERN_WARNING;
24810 +
24811 +       sbinfo = au_sbi(sb);
24812 +       /* since we define pr_fmt, call printk directly */
24813 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
24814 +       printk(KERN_WARNING AUFS_NAME ": superblock\n");
24815 +       au_dpri_sb(sb);
24816 +
24817 +#if 0
24818 +       printk(KERN_WARNING AUFS_NAME ": root dentry\n");
24819 +       au_dpri_dentry(sb->s_root);
24820 +       printk(KERN_WARNING AUFS_NAME ": root inode\n");
24821 +       au_dpri_inode(sb->s_root->d_inode);
24822 +#endif
24823 +
24824 +#if 0
24825 +       do {
24826 +               int err, i, j, ndentry;
24827 +               struct au_dcsub_pages dpages;
24828 +               struct au_dpage *dpage;
24829 +
24830 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
24831 +               if (unlikely(err))
24832 +                       break;
24833 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
24834 +               if (!err)
24835 +                       for (i = 0; i < dpages.ndpage; i++) {
24836 +                               dpage = dpages.dpages + i;
24837 +                               ndentry = dpage->ndentry;
24838 +                               for (j = 0; j < ndentry; j++)
24839 +                                       au_dpri_dentry(dpage->dentries[j]);
24840 +                       }
24841 +               au_dpages_free(&dpages);
24842 +       } while (0);
24843 +#endif
24844 +
24845 +#if 1
24846 +       {
24847 +               struct inode *i;
24848 +               printk(KERN_WARNING AUFS_NAME ": isolated inode\n");
24849 +               spin_lock(&inode_sb_list_lock);
24850 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
24851 +                       spin_lock(&i->i_lock);
24852 +                       if (1 || list_empty(&i->i_dentry))
24853 +                               au_dpri_inode(i);
24854 +                       spin_unlock(&i->i_lock);
24855 +               }
24856 +               spin_unlock(&inode_sb_list_lock);
24857 +       }
24858 +#endif
24859 +       printk(KERN_WARNING AUFS_NAME ": files\n");
24860 +       lg_global_lock(files_lglock);
24861 +       do_file_list_for_each_entry(sb, file) {
24862 +               umode_t mode;
24863 +               mode = file->f_dentry->d_inode->i_mode;
24864 +               if (!special_file(mode) || au_special_file(mode))
24865 +                       au_dpri_file(file);
24866 +       } while_file_list_for_each_entry;
24867 +       lg_global_unlock(files_lglock);
24868 +       printk(KERN_WARNING AUFS_NAME ": done\n");
24869 +
24870 +       au_plevel = plevel;
24871 +}
24872 +
24873 +/* ---------------------------------------------------------------------- */
24874 +
24875 +/* module parameter */
24876 +static char *aufs_sysrq_key = "a";
24877 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
24878 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
24879 +
24880 +static void au_sysrq(int key __maybe_unused)
24881 +{
24882 +       struct au_sbinfo *sbinfo;
24883 +
24884 +       lockdep_off();
24885 +       au_sbilist_lock();
24886 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
24887 +               sysrq_sb(sbinfo->si_sb);
24888 +       au_sbilist_unlock();
24889 +       lockdep_on();
24890 +}
24891 +
24892 +static struct sysrq_key_op au_sysrq_op = {
24893 +       .handler        = au_sysrq,
24894 +       .help_msg       = "Aufs",
24895 +       .action_msg     = "Aufs",
24896 +       .enable_mask    = SYSRQ_ENABLE_DUMP
24897 +};
24898 +
24899 +/* ---------------------------------------------------------------------- */
24900 +
24901 +int __init au_sysrq_init(void)
24902 +{
24903 +       int err;
24904 +       char key;
24905 +
24906 +       err = -1;
24907 +       key = *aufs_sysrq_key;
24908 +       if ('a' <= key && key <= 'z')
24909 +               err = register_sysrq_key(key, &au_sysrq_op);
24910 +       if (unlikely(err))
24911 +               pr_err("err %d, sysrq=%c\n", err, key);
24912 +       return err;
24913 +}
24914 +
24915 +void au_sysrq_fin(void)
24916 +{
24917 +       int err;
24918 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
24919 +       if (unlikely(err))
24920 +               pr_err("err %d (ignored)\n", err);
24921 +}
24922 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
24923 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
24924 +++ linux/fs/aufs/vdir.c        2011-08-24 13:30:24.734646739 +0200
24925 @@ -0,0 +1,886 @@
24926 +/*
24927 + * Copyright (C) 2005-2011 Junjiro R. Okajima
24928 + *
24929 + * This program, aufs is free software; you can redistribute it and/or modify
24930 + * it under the terms of the GNU General Public License as published by
24931 + * the Free Software Foundation; either version 2 of the License, or
24932 + * (at your option) any later version.
24933 + *
24934 + * This program is distributed in the hope that it will be useful,
24935 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24936 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24937 + * GNU General Public License for more details.
24938 + *
24939 + * You should have received a copy of the GNU General Public License
24940 + * along with this program; if not, write to the Free Software
24941 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24942 + */
24943 +
24944 +/*
24945 + * virtual or vertical directory
24946 + */
24947 +
24948 +#include <linux/hash.h>
24949 +#include "aufs.h"
24950 +
24951 +static unsigned int calc_size(int nlen)
24952 +{
24953 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
24954 +}
24955 +
24956 +static int set_deblk_end(union au_vdir_deblk_p *p,
24957 +                        union au_vdir_deblk_p *deblk_end)
24958 +{
24959 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
24960 +               p->de->de_str.len = 0;
24961 +               /* smp_mb(); */
24962 +               return 0;
24963 +       }
24964 +       return -1; /* error */
24965 +}
24966 +
24967 +/* returns true or false */
24968 +static int is_deblk_end(union au_vdir_deblk_p *p,
24969 +                       union au_vdir_deblk_p *deblk_end)
24970 +{
24971 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
24972 +               return !p->de->de_str.len;
24973 +       return 1;
24974 +}
24975 +
24976 +static unsigned char *last_deblk(struct au_vdir *vdir)
24977 +{
24978 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
24979 +}
24980 +
24981 +/* ---------------------------------------------------------------------- */
24982 +
24983 +/* estimate the apropriate size for name hash table */
24984 +unsigned int au_rdhash_est(loff_t sz)
24985 +{
24986 +       unsigned int n;
24987 +
24988 +       n = UINT_MAX;
24989 +       sz >>= 10;
24990 +       if (sz < n)
24991 +               n = sz;
24992 +       if (sz < AUFS_RDHASH_DEF)
24993 +               n = AUFS_RDHASH_DEF;
24994 +       /* pr_info("n %u\n", n); */
24995 +       return n;
24996 +}
24997 +
24998 +/*
24999 + * the allocated memory has to be freed by
25000 + * au_nhash_wh_free() or au_nhash_de_free().
25001 + */
25002 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
25003 +{
25004 +       struct hlist_head *head;
25005 +       unsigned int u;
25006 +
25007 +       head = kmalloc(sizeof(*nhash->nh_head) * num_hash, gfp);
25008 +       if (head) {
25009 +               nhash->nh_num = num_hash;
25010 +               nhash->nh_head = head;
25011 +               for (u = 0; u < num_hash; u++)
25012 +                       INIT_HLIST_HEAD(head++);
25013 +               return 0; /* success */
25014 +       }
25015 +
25016 +       return -ENOMEM;
25017 +}
25018 +
25019 +static void nhash_count(struct hlist_head *head)
25020 +{
25021 +#if 0
25022 +       unsigned long n;
25023 +       struct hlist_node *pos;
25024 +
25025 +       n = 0;
25026 +       hlist_for_each(pos, head)
25027 +               n++;
25028 +       pr_info("%lu\n", n);
25029 +#endif
25030 +}
25031 +
25032 +static void au_nhash_wh_do_free(struct hlist_head *head)
25033 +{
25034 +       struct au_vdir_wh *tpos;
25035 +       struct hlist_node *pos, *node;
25036 +
25037 +       hlist_for_each_entry_safe(tpos, pos, node, head, wh_hash) {
25038 +               /* hlist_del(pos); */
25039 +               kfree(tpos);
25040 +       }
25041 +}
25042 +
25043 +static void au_nhash_de_do_free(struct hlist_head *head)
25044 +{
25045 +       struct au_vdir_dehstr *tpos;
25046 +       struct hlist_node *pos, *node;
25047 +
25048 +       hlist_for_each_entry_safe(tpos, pos, node, head, hash) {
25049 +               /* hlist_del(pos); */
25050 +               au_cache_free_vdir_dehstr(tpos);
25051 +       }
25052 +}
25053 +
25054 +static void au_nhash_do_free(struct au_nhash *nhash,
25055 +                            void (*free)(struct hlist_head *head))
25056 +{
25057 +       unsigned int n;
25058 +       struct hlist_head *head;
25059 +
25060 +       n = nhash->nh_num;
25061 +       if (!n)
25062 +               return;
25063 +
25064 +       head = nhash->nh_head;
25065 +       while (n-- > 0) {
25066 +               nhash_count(head);
25067 +               free(head++);
25068 +       }
25069 +       kfree(nhash->nh_head);
25070 +}
25071 +
25072 +void au_nhash_wh_free(struct au_nhash *whlist)
25073 +{
25074 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
25075 +}
25076 +
25077 +static void au_nhash_de_free(struct au_nhash *delist)
25078 +{
25079 +       au_nhash_do_free(delist, au_nhash_de_do_free);
25080 +}
25081 +
25082 +/* ---------------------------------------------------------------------- */
25083 +
25084 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
25085 +                           int limit)
25086 +{
25087 +       int num;
25088 +       unsigned int u, n;
25089 +       struct hlist_head *head;
25090 +       struct au_vdir_wh *tpos;
25091 +       struct hlist_node *pos;
25092 +
25093 +       num = 0;
25094 +       n = whlist->nh_num;
25095 +       head = whlist->nh_head;
25096 +       for (u = 0; u < n; u++, head++)
25097 +               hlist_for_each_entry(tpos, pos, head, wh_hash)
25098 +                       if (tpos->wh_bindex == btgt && ++num > limit)
25099 +                               return 1;
25100 +       return 0;
25101 +}
25102 +
25103 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
25104 +                                      unsigned char *name,
25105 +                                      unsigned int len)
25106 +{
25107 +       unsigned int v;
25108 +       /* const unsigned int magic_bit = 12; */
25109 +
25110 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
25111 +
25112 +       v = 0;
25113 +       while (len--)
25114 +               v += *name++;
25115 +       /* v = hash_long(v, magic_bit); */
25116 +       v %= nhash->nh_num;
25117 +       return nhash->nh_head + v;
25118 +}
25119 +
25120 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
25121 +                             int nlen)
25122 +{
25123 +       return str->len == nlen && !memcmp(str->name, name, nlen);
25124 +}
25125 +
25126 +/* returns found or not */
25127 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
25128 +{
25129 +       struct hlist_head *head;
25130 +       struct au_vdir_wh *tpos;
25131 +       struct hlist_node *pos;
25132 +       struct au_vdir_destr *str;
25133 +
25134 +       head = au_name_hash(whlist, name, nlen);
25135 +       hlist_for_each_entry(tpos, pos, head, wh_hash) {
25136 +               str = &tpos->wh_str;
25137 +               AuDbg("%.*s\n", str->len, str->name);
25138 +               if (au_nhash_test_name(str, name, nlen))
25139 +                       return 1;
25140 +       }
25141 +       return 0;
25142 +}
25143 +
25144 +/* returns found(true) or not */
25145 +static int test_known(struct au_nhash *delist, char *name, int nlen)
25146 +{
25147 +       struct hlist_head *head;
25148 +       struct au_vdir_dehstr *tpos;
25149 +       struct hlist_node *pos;
25150 +       struct au_vdir_destr *str;
25151 +
25152 +       head = au_name_hash(delist, name, nlen);
25153 +       hlist_for_each_entry(tpos, pos, head, hash) {
25154 +               str = tpos->str;
25155 +               AuDbg("%.*s\n", str->len, str->name);
25156 +               if (au_nhash_test_name(str, name, nlen))
25157 +                       return 1;
25158 +       }
25159 +       return 0;
25160 +}
25161 +
25162 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
25163 +                           unsigned char d_type)
25164 +{
25165 +#ifdef CONFIG_AUFS_SHWH
25166 +       wh->wh_ino = ino;
25167 +       wh->wh_type = d_type;
25168 +#endif
25169 +}
25170 +
25171 +/* ---------------------------------------------------------------------- */
25172 +
25173 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
25174 +                      unsigned int d_type, aufs_bindex_t bindex,
25175 +                      unsigned char shwh)
25176 +{
25177 +       int err;
25178 +       struct au_vdir_destr *str;
25179 +       struct au_vdir_wh *wh;
25180 +
25181 +       AuDbg("%.*s\n", nlen, name);
25182 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
25183 +
25184 +       err = -ENOMEM;
25185 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
25186 +       if (unlikely(!wh))
25187 +               goto out;
25188 +
25189 +       err = 0;
25190 +       wh->wh_bindex = bindex;
25191 +       if (shwh)
25192 +               au_shwh_init_wh(wh, ino, d_type);
25193 +       str = &wh->wh_str;
25194 +       str->len = nlen;
25195 +       memcpy(str->name, name, nlen);
25196 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
25197 +       /* smp_mb(); */
25198 +
25199 +out:
25200 +       return err;
25201 +}
25202 +
25203 +static int append_deblk(struct au_vdir *vdir)
25204 +{
25205 +       int err;
25206 +       unsigned long ul;
25207 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
25208 +       union au_vdir_deblk_p p, deblk_end;
25209 +       unsigned char **o;
25210 +
25211 +       err = -ENOMEM;
25212 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
25213 +                    GFP_NOFS);
25214 +       if (unlikely(!o))
25215 +               goto out;
25216 +
25217 +       vdir->vd_deblk = o;
25218 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
25219 +       if (p.deblk) {
25220 +               ul = vdir->vd_nblk++;
25221 +               vdir->vd_deblk[ul] = p.deblk;
25222 +               vdir->vd_last.ul = ul;
25223 +               vdir->vd_last.p.deblk = p.deblk;
25224 +               deblk_end.deblk = p.deblk + deblk_sz;
25225 +               err = set_deblk_end(&p, &deblk_end);
25226 +       }
25227 +
25228 +out:
25229 +       return err;
25230 +}
25231 +
25232 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
25233 +                    unsigned int d_type, struct au_nhash *delist)
25234 +{
25235 +       int err;
25236 +       unsigned int sz;
25237 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
25238 +       union au_vdir_deblk_p p, *room, deblk_end;
25239 +       struct au_vdir_dehstr *dehstr;
25240 +
25241 +       p.deblk = last_deblk(vdir);
25242 +       deblk_end.deblk = p.deblk + deblk_sz;
25243 +       room = &vdir->vd_last.p;
25244 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
25245 +                 || !is_deblk_end(room, &deblk_end));
25246 +
25247 +       sz = calc_size(nlen);
25248 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
25249 +               err = append_deblk(vdir);
25250 +               if (unlikely(err))
25251 +                       goto out;
25252 +
25253 +               p.deblk = last_deblk(vdir);
25254 +               deblk_end.deblk = p.deblk + deblk_sz;
25255 +               /* smp_mb(); */
25256 +               AuDebugOn(room->deblk != p.deblk);
25257 +       }
25258 +
25259 +       err = -ENOMEM;
25260 +       dehstr = au_cache_alloc_vdir_dehstr();
25261 +       if (unlikely(!dehstr))
25262 +               goto out;
25263 +
25264 +       dehstr->str = &room->de->de_str;
25265 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
25266 +       room->de->de_ino = ino;
25267 +       room->de->de_type = d_type;
25268 +       room->de->de_str.len = nlen;
25269 +       memcpy(room->de->de_str.name, name, nlen);
25270 +
25271 +       err = 0;
25272 +       room->deblk += sz;
25273 +       if (unlikely(set_deblk_end(room, &deblk_end)))
25274 +               err = append_deblk(vdir);
25275 +       /* smp_mb(); */
25276 +
25277 +out:
25278 +       return err;
25279 +}
25280 +
25281 +/* ---------------------------------------------------------------------- */
25282 +
25283 +void au_vdir_free(struct au_vdir *vdir)
25284 +{
25285 +       unsigned char **deblk;
25286 +
25287 +       deblk = vdir->vd_deblk;
25288 +       while (vdir->vd_nblk--)
25289 +               kfree(*deblk++);
25290 +       kfree(vdir->vd_deblk);
25291 +       au_cache_free_vdir(vdir);
25292 +}
25293 +
25294 +static struct au_vdir *alloc_vdir(struct file *file)
25295 +{
25296 +       struct au_vdir *vdir;
25297 +       struct super_block *sb;
25298 +       int err;
25299 +
25300 +       sb = file->f_dentry->d_sb;
25301 +       SiMustAnyLock(sb);
25302 +
25303 +       err = -ENOMEM;
25304 +       vdir = au_cache_alloc_vdir();
25305 +       if (unlikely(!vdir))
25306 +               goto out;
25307 +
25308 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
25309 +       if (unlikely(!vdir->vd_deblk))
25310 +               goto out_free;
25311 +
25312 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
25313 +       if (!vdir->vd_deblk_sz) {
25314 +               /* estimate the apropriate size for deblk */
25315 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
25316 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
25317 +       }
25318 +       vdir->vd_nblk = 0;
25319 +       vdir->vd_version = 0;
25320 +       vdir->vd_jiffy = 0;
25321 +       err = append_deblk(vdir);
25322 +       if (!err)
25323 +               return vdir; /* success */
25324 +
25325 +       kfree(vdir->vd_deblk);
25326 +
25327 +out_free:
25328 +       au_cache_free_vdir(vdir);
25329 +out:
25330 +       vdir = ERR_PTR(err);
25331 +       return vdir;
25332 +}
25333 +
25334 +static int reinit_vdir(struct au_vdir *vdir)
25335 +{
25336 +       int err;
25337 +       union au_vdir_deblk_p p, deblk_end;
25338 +
25339 +       while (vdir->vd_nblk > 1) {
25340 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
25341 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
25342 +               vdir->vd_nblk--;
25343 +       }
25344 +       p.deblk = vdir->vd_deblk[0];
25345 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
25346 +       err = set_deblk_end(&p, &deblk_end);
25347 +       /* keep vd_dblk_sz */
25348 +       vdir->vd_last.ul = 0;
25349 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
25350 +       vdir->vd_version = 0;
25351 +       vdir->vd_jiffy = 0;
25352 +       /* smp_mb(); */
25353 +       return err;
25354 +}
25355 +
25356 +/* ---------------------------------------------------------------------- */
25357 +
25358 +#define AuFillVdir_CALLED      1
25359 +#define AuFillVdir_WHABLE      (1 << 1)
25360 +#define AuFillVdir_SHWH                (1 << 2)
25361 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
25362 +#define au_fset_fillvdir(flags, name) \
25363 +       do { (flags) |= AuFillVdir_##name; } while (0)
25364 +#define au_fclr_fillvdir(flags, name) \
25365 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
25366 +
25367 +#ifndef CONFIG_AUFS_SHWH
25368 +#undef AuFillVdir_SHWH
25369 +#define AuFillVdir_SHWH                0
25370 +#endif
25371 +
25372 +struct fillvdir_arg {
25373 +       struct file             *file;
25374 +       struct au_vdir          *vdir;
25375 +       struct au_nhash         delist;
25376 +       struct au_nhash         whlist;
25377 +       aufs_bindex_t           bindex;
25378 +       unsigned int            flags;
25379 +       int                     err;
25380 +};
25381 +
25382 +static int fillvdir(void *__arg, const char *__name, int nlen,
25383 +                   loff_t offset __maybe_unused, u64 h_ino,
25384 +                   unsigned int d_type)
25385 +{
25386 +       struct fillvdir_arg *arg = __arg;
25387 +       char *name = (void *)__name;
25388 +       struct super_block *sb;
25389 +       ino_t ino;
25390 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
25391 +
25392 +       arg->err = 0;
25393 +       sb = arg->file->f_dentry->d_sb;
25394 +       au_fset_fillvdir(arg->flags, CALLED);
25395 +       /* smp_mb(); */
25396 +       if (nlen <= AUFS_WH_PFX_LEN
25397 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
25398 +               if (test_known(&arg->delist, name, nlen)
25399 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
25400 +                       goto out; /* already exists or whiteouted */
25401 +
25402 +               sb = arg->file->f_dentry->d_sb;
25403 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
25404 +               if (!arg->err) {
25405 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
25406 +                               d_type = DT_UNKNOWN;
25407 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
25408 +                                            d_type, &arg->delist);
25409 +               }
25410 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
25411 +               name += AUFS_WH_PFX_LEN;
25412 +               nlen -= AUFS_WH_PFX_LEN;
25413 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
25414 +                       goto out; /* already whiteouted */
25415 +
25416 +               if (shwh)
25417 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
25418 +                                            &ino);
25419 +               if (!arg->err) {
25420 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
25421 +                               d_type = DT_UNKNOWN;
25422 +                       arg->err = au_nhash_append_wh
25423 +                               (&arg->whlist, name, nlen, ino, d_type,
25424 +                                arg->bindex, shwh);
25425 +               }
25426 +       }
25427 +
25428 +out:
25429 +       if (!arg->err)
25430 +               arg->vdir->vd_jiffy = jiffies;
25431 +       /* smp_mb(); */
25432 +       AuTraceErr(arg->err);
25433 +       return arg->err;
25434 +}
25435 +
25436 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
25437 +                         struct au_nhash *whlist, struct au_nhash *delist)
25438 +{
25439 +#ifdef CONFIG_AUFS_SHWH
25440 +       int err;
25441 +       unsigned int nh, u;
25442 +       struct hlist_head *head;
25443 +       struct au_vdir_wh *tpos;
25444 +       struct hlist_node *pos, *n;
25445 +       char *p, *o;
25446 +       struct au_vdir_destr *destr;
25447 +
25448 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
25449 +
25450 +       err = -ENOMEM;
25451 +       o = p = __getname_gfp(GFP_NOFS);
25452 +       if (unlikely(!p))
25453 +               goto out;
25454 +
25455 +       err = 0;
25456 +       nh = whlist->nh_num;
25457 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
25458 +       p += AUFS_WH_PFX_LEN;
25459 +       for (u = 0; u < nh; u++) {
25460 +               head = whlist->nh_head + u;
25461 +               hlist_for_each_entry_safe(tpos, pos, n, head, wh_hash) {
25462 +                       destr = &tpos->wh_str;
25463 +                       memcpy(p, destr->name, destr->len);
25464 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
25465 +                                       tpos->wh_ino, tpos->wh_type, delist);
25466 +                       if (unlikely(err))
25467 +                               break;
25468 +               }
25469 +       }
25470 +
25471 +       __putname(o);
25472 +
25473 +out:
25474 +       AuTraceErr(err);
25475 +       return err;
25476 +#else
25477 +       return 0;
25478 +#endif
25479 +}
25480 +
25481 +static int au_do_read_vdir(struct fillvdir_arg *arg)
25482 +{
25483 +       int err;
25484 +       unsigned int rdhash;
25485 +       loff_t offset;
25486 +       aufs_bindex_t bend, bindex, bstart;
25487 +       unsigned char shwh;
25488 +       struct file *hf, *file;
25489 +       struct super_block *sb;
25490 +
25491 +       file = arg->file;
25492 +       sb = file->f_dentry->d_sb;
25493 +       SiMustAnyLock(sb);
25494 +
25495 +       rdhash = au_sbi(sb)->si_rdhash;
25496 +       if (!rdhash)
25497 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
25498 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
25499 +       if (unlikely(err))
25500 +               goto out;
25501 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
25502 +       if (unlikely(err))
25503 +               goto out_delist;
25504 +
25505 +       err = 0;
25506 +       arg->flags = 0;
25507 +       shwh = 0;
25508 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
25509 +               shwh = 1;
25510 +               au_fset_fillvdir(arg->flags, SHWH);
25511 +       }
25512 +       bstart = au_fbstart(file);
25513 +       bend = au_fbend_dir(file);
25514 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
25515 +               hf = au_hf_dir(file, bindex);
25516 +               if (!hf)
25517 +                       continue;
25518 +
25519 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
25520 +               err = offset;
25521 +               if (unlikely(offset))
25522 +                       break;
25523 +
25524 +               arg->bindex = bindex;
25525 +               au_fclr_fillvdir(arg->flags, WHABLE);
25526 +               if (shwh
25527 +                   || (bindex != bend
25528 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
25529 +                       au_fset_fillvdir(arg->flags, WHABLE);
25530 +               do {
25531 +                       arg->err = 0;
25532 +                       au_fclr_fillvdir(arg->flags, CALLED);
25533 +                       /* smp_mb(); */
25534 +                       err = vfsub_readdir(hf, fillvdir, arg);
25535 +                       if (err >= 0)
25536 +                               err = arg->err;
25537 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
25538 +       }
25539 +
25540 +       if (!err && shwh)
25541 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
25542 +
25543 +       au_nhash_wh_free(&arg->whlist);
25544 +
25545 +out_delist:
25546 +       au_nhash_de_free(&arg->delist);
25547 +out:
25548 +       return err;
25549 +}
25550 +
25551 +static int read_vdir(struct file *file, int may_read)
25552 +{
25553 +       int err;
25554 +       unsigned long expire;
25555 +       unsigned char do_read;
25556 +       struct fillvdir_arg arg;
25557 +       struct inode *inode;
25558 +       struct au_vdir *vdir, *allocated;
25559 +
25560 +       err = 0;
25561 +       inode = file->f_dentry->d_inode;
25562 +       IMustLock(inode);
25563 +       SiMustAnyLock(inode->i_sb);
25564 +
25565 +       allocated = NULL;
25566 +       do_read = 0;
25567 +       expire = au_sbi(inode->i_sb)->si_rdcache;
25568 +       vdir = au_ivdir(inode);
25569 +       if (!vdir) {
25570 +               do_read = 1;
25571 +               vdir = alloc_vdir(file);
25572 +               err = PTR_ERR(vdir);
25573 +               if (IS_ERR(vdir))
25574 +                       goto out;
25575 +               err = 0;
25576 +               allocated = vdir;
25577 +       } else if (may_read
25578 +                  && (inode->i_version != vdir->vd_version
25579 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
25580 +               do_read = 1;
25581 +               err = reinit_vdir(vdir);
25582 +               if (unlikely(err))
25583 +                       goto out;
25584 +       }
25585 +
25586 +       if (!do_read)
25587 +               return 0; /* success */
25588 +
25589 +       arg.file = file;
25590 +       arg.vdir = vdir;
25591 +       err = au_do_read_vdir(&arg);
25592 +       if (!err) {
25593 +               /* file->f_pos = 0; */
25594 +               vdir->vd_version = inode->i_version;
25595 +               vdir->vd_last.ul = 0;
25596 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
25597 +               if (allocated)
25598 +                       au_set_ivdir(inode, allocated);
25599 +       } else if (allocated)
25600 +               au_vdir_free(allocated);
25601 +
25602 +out:
25603 +       return err;
25604 +}
25605 +
25606 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
25607 +{
25608 +       int err, rerr;
25609 +       unsigned long ul, n;
25610 +       const unsigned int deblk_sz = src->vd_deblk_sz;
25611 +
25612 +       AuDebugOn(tgt->vd_nblk != 1);
25613 +
25614 +       err = -ENOMEM;
25615 +       if (tgt->vd_nblk < src->vd_nblk) {
25616 +               unsigned char **p;
25617 +
25618 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
25619 +                            GFP_NOFS);
25620 +               if (unlikely(!p))
25621 +                       goto out;
25622 +               tgt->vd_deblk = p;
25623 +       }
25624 +
25625 +       if (tgt->vd_deblk_sz != deblk_sz) {
25626 +               unsigned char *p;
25627 +
25628 +               tgt->vd_deblk_sz = deblk_sz;
25629 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
25630 +               if (unlikely(!p))
25631 +                       goto out;
25632 +               tgt->vd_deblk[0] = p;
25633 +       }
25634 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
25635 +       tgt->vd_version = src->vd_version;
25636 +       tgt->vd_jiffy = src->vd_jiffy;
25637 +
25638 +       n = src->vd_nblk;
25639 +       for (ul = 1; ul < n; ul++) {
25640 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
25641 +                                           GFP_NOFS);
25642 +               if (unlikely(!tgt->vd_deblk[ul]))
25643 +                       goto out;
25644 +               tgt->vd_nblk++;
25645 +       }
25646 +       tgt->vd_nblk = n;
25647 +       tgt->vd_last.ul = tgt->vd_last.ul;
25648 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
25649 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
25650 +               - src->vd_deblk[src->vd_last.ul];
25651 +       /* smp_mb(); */
25652 +       return 0; /* success */
25653 +
25654 +out:
25655 +       rerr = reinit_vdir(tgt);
25656 +       BUG_ON(rerr);
25657 +       return err;
25658 +}
25659 +
25660 +int au_vdir_init(struct file *file)
25661 +{
25662 +       int err;
25663 +       struct inode *inode;
25664 +       struct au_vdir *vdir_cache, *allocated;
25665 +
25666 +       err = read_vdir(file, !file->f_pos);
25667 +       if (unlikely(err))
25668 +               goto out;
25669 +
25670 +       allocated = NULL;
25671 +       vdir_cache = au_fvdir_cache(file);
25672 +       if (!vdir_cache) {
25673 +               vdir_cache = alloc_vdir(file);
25674 +               err = PTR_ERR(vdir_cache);
25675 +               if (IS_ERR(vdir_cache))
25676 +                       goto out;
25677 +               allocated = vdir_cache;
25678 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
25679 +               err = reinit_vdir(vdir_cache);
25680 +               if (unlikely(err))
25681 +                       goto out;
25682 +       } else
25683 +               return 0; /* success */
25684 +
25685 +       inode = file->f_dentry->d_inode;
25686 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
25687 +       if (!err) {
25688 +               file->f_version = inode->i_version;
25689 +               if (allocated)
25690 +                       au_set_fvdir_cache(file, allocated);
25691 +       } else if (allocated)
25692 +               au_vdir_free(allocated);
25693 +
25694 +out:
25695 +       return err;
25696 +}
25697 +
25698 +static loff_t calc_offset(struct au_vdir *vdir)
25699 +{
25700 +       loff_t offset;
25701 +       union au_vdir_deblk_p p;
25702 +
25703 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
25704 +       offset = vdir->vd_last.p.deblk - p.deblk;
25705 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
25706 +       return offset;
25707 +}
25708 +
25709 +/* returns true or false */
25710 +static int seek_vdir(struct file *file)
25711 +{
25712 +       int valid;
25713 +       unsigned int deblk_sz;
25714 +       unsigned long ul, n;
25715 +       loff_t offset;
25716 +       union au_vdir_deblk_p p, deblk_end;
25717 +       struct au_vdir *vdir_cache;
25718 +
25719 +       valid = 1;
25720 +       vdir_cache = au_fvdir_cache(file);
25721 +       offset = calc_offset(vdir_cache);
25722 +       AuDbg("offset %lld\n", offset);
25723 +       if (file->f_pos == offset)
25724 +               goto out;
25725 +
25726 +       vdir_cache->vd_last.ul = 0;
25727 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
25728 +       if (!file->f_pos)
25729 +               goto out;
25730 +
25731 +       valid = 0;
25732 +       deblk_sz = vdir_cache->vd_deblk_sz;
25733 +       ul = div64_u64(file->f_pos, deblk_sz);
25734 +       AuDbg("ul %lu\n", ul);
25735 +       if (ul >= vdir_cache->vd_nblk)
25736 +               goto out;
25737 +
25738 +       n = vdir_cache->vd_nblk;
25739 +       for (; ul < n; ul++) {
25740 +               p.deblk = vdir_cache->vd_deblk[ul];
25741 +               deblk_end.deblk = p.deblk + deblk_sz;
25742 +               offset = ul;
25743 +               offset *= deblk_sz;
25744 +               while (!is_deblk_end(&p, &deblk_end) && offset < file->f_pos) {
25745 +                       unsigned int l;
25746 +
25747 +                       l = calc_size(p.de->de_str.len);
25748 +                       offset += l;
25749 +                       p.deblk += l;
25750 +               }
25751 +               if (!is_deblk_end(&p, &deblk_end)) {
25752 +                       valid = 1;
25753 +                       vdir_cache->vd_last.ul = ul;
25754 +                       vdir_cache->vd_last.p = p;
25755 +                       break;
25756 +               }
25757 +       }
25758 +
25759 +out:
25760 +       /* smp_mb(); */
25761 +       AuTraceErr(!valid);
25762 +       return valid;
25763 +}
25764 +
25765 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir)
25766 +{
25767 +       int err;
25768 +       unsigned int l, deblk_sz;
25769 +       union au_vdir_deblk_p deblk_end;
25770 +       struct au_vdir *vdir_cache;
25771 +       struct au_vdir_de *de;
25772 +
25773 +       vdir_cache = au_fvdir_cache(file);
25774 +       if (!seek_vdir(file))
25775 +               return 0;
25776 +
25777 +       deblk_sz = vdir_cache->vd_deblk_sz;
25778 +       while (1) {
25779 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
25780 +               deblk_end.deblk += deblk_sz;
25781 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
25782 +                       de = vdir_cache->vd_last.p.de;
25783 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
25784 +                             de->de_str.len, de->de_str.name, file->f_pos,
25785 +                             (unsigned long)de->de_ino, de->de_type);
25786 +                       err = filldir(dirent, de->de_str.name, de->de_str.len,
25787 +                                     file->f_pos, de->de_ino, de->de_type);
25788 +                       if (unlikely(err)) {
25789 +                               AuTraceErr(err);
25790 +                               /* todo: ignore the error caused by udba? */
25791 +                               /* return err; */
25792 +                               return 0;
25793 +                       }
25794 +
25795 +                       l = calc_size(de->de_str.len);
25796 +                       vdir_cache->vd_last.p.deblk += l;
25797 +                       file->f_pos += l;
25798 +               }
25799 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
25800 +                       vdir_cache->vd_last.ul++;
25801 +                       vdir_cache->vd_last.p.deblk
25802 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
25803 +                       file->f_pos = deblk_sz * vdir_cache->vd_last.ul;
25804 +                       continue;
25805 +               }
25806 +               break;
25807 +       }
25808 +
25809 +       /* smp_mb(); */
25810 +       return 0;
25811 +}
25812 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
25813 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
25814 +++ linux/fs/aufs/vfsub.c       2012-01-05 11:00:59.218835185 +0100
25815 @@ -0,0 +1,837 @@
25816 +/*
25817 + * Copyright (C) 2005-2011 Junjiro R. Okajima
25818 + *
25819 + * This program, aufs is free software; you can redistribute it and/or modify
25820 + * it under the terms of the GNU General Public License as published by
25821 + * the Free Software Foundation; either version 2 of the License, or
25822 + * (at your option) any later version.
25823 + *
25824 + * This program is distributed in the hope that it will be useful,
25825 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25826 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25827 + * GNU General Public License for more details.
25828 + *
25829 + * You should have received a copy of the GNU General Public License
25830 + * along with this program; if not, write to the Free Software
25831 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25832 + */
25833 +
25834 +/*
25835 + * sub-routines for VFS
25836 + */
25837 +
25838 +#include <linux/file.h>
25839 +#include <linux/ima.h>
25840 +#include <linux/namei.h>
25841 +#include <linux/security.h>
25842 +#include <linux/splice.h>
25843 +#include <linux/uaccess.h>
25844 +#include "aufs.h"
25845 +
25846 +int vfsub_update_h_iattr(struct path *h_path, int *did)
25847 +{
25848 +       int err;
25849 +       struct kstat st;
25850 +       struct super_block *h_sb;
25851 +
25852 +       /* for remote fs, leave work for its getattr or d_revalidate */
25853 +       /* for bad i_attr fs, handle them in aufs_getattr() */
25854 +       /* still some fs may acquire i_mutex. we need to skip them */
25855 +       err = 0;
25856 +       if (!did)
25857 +               did = &err;
25858 +       h_sb = h_path->dentry->d_sb;
25859 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
25860 +       if (*did)
25861 +               err = vfs_getattr(h_path->mnt, h_path->dentry, &st);
25862 +
25863 +       return err;
25864 +}
25865 +
25866 +/* ---------------------------------------------------------------------- */
25867 +
25868 +struct file *vfsub_dentry_open(struct path *path, int flags)
25869 +{
25870 +       struct file *file;
25871 +
25872 +       path_get(path);
25873 +       file = dentry_open(path->dentry, path->mnt,
25874 +                          flags /* | __FMODE_NONOTIFY */,
25875 +                          current_cred());
25876 +       if (!IS_ERR_OR_NULL(file)
25877 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
25878 +               i_readcount_inc(path->dentry->d_inode);
25879 +
25880 +       return file;
25881 +}
25882 +
25883 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
25884 +{
25885 +       struct file *file;
25886 +
25887 +       lockdep_off();
25888 +       file = filp_open(path,
25889 +                        oflags /* | __FMODE_NONOTIFY */,
25890 +                        mode);
25891 +       lockdep_on();
25892 +       if (IS_ERR(file))
25893 +               goto out;
25894 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
25895 +
25896 +out:
25897 +       return file;
25898 +}
25899 +
25900 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
25901 +{
25902 +       int err;
25903 +
25904 +       err = kern_path(name, flags, path);
25905 +       if (!err && path->dentry->d_inode)
25906 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
25907 +       return err;
25908 +}
25909 +
25910 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
25911 +                                   int len)
25912 +{
25913 +       struct path path = {
25914 +               .mnt = NULL
25915 +       };
25916 +
25917 +       /* VFS checks it too, but by WARN_ON_ONCE() */
25918 +       IMustLock(parent->d_inode);
25919 +
25920 +       path.dentry = lookup_one_len(name, parent, len);
25921 +       if (IS_ERR(path.dentry))
25922 +               goto out;
25923 +       if (path.dentry->d_inode)
25924 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
25925 +
25926 +out:
25927 +       AuTraceErrPtr(path.dentry);
25928 +       return path.dentry;
25929 +}
25930 +
25931 +struct dentry *vfsub_lookup_hash(struct nameidata *nd)
25932 +{
25933 +       struct path path = {
25934 +               .mnt = nd->path.mnt
25935 +       };
25936 +
25937 +       IMustLock(nd->path.dentry->d_inode);
25938 +
25939 +       path.dentry = lookup_hash(nd);
25940 +       if (IS_ERR(path.dentry))
25941 +               goto out;
25942 +       if (path.dentry->d_inode)
25943 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
25944 +
25945 +out:
25946 +       AuTraceErrPtr(path.dentry);
25947 +       return path.dentry;
25948 +}
25949 +
25950 +/*
25951 + * this is "VFS:__lookup_one_len()" which was removed and merged into
25952 + * VFS:lookup_one_len() by the commit.
25953 + *     6a96ba5 2011-03-14 kill __lookup_one_len()
25954 + * this function should always be equivalent to the corresponding part in
25955 + * VFS:lookup_one_len().
25956 + */
25957 +int vfsub_name_hash(const char *name, struct qstr *this, int len)
25958 +{
25959 +       unsigned long hash;
25960 +       unsigned int c;
25961 +
25962 +       this->name = name;
25963 +       this->len = len;
25964 +       if (!len)
25965 +               return -EACCES;
25966 +
25967 +       hash = init_name_hash();
25968 +       while (len--) {
25969 +               c = *(const unsigned char *)name++;
25970 +               if (c == '/' || c == '\0')
25971 +                       return -EACCES;
25972 +               hash = partial_name_hash(c, hash);
25973 +       }
25974 +       this->hash = end_name_hash(hash);
25975 +       return 0;
25976 +}
25977 +
25978 +/* ---------------------------------------------------------------------- */
25979 +
25980 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
25981 +                                struct dentry *d2, struct au_hinode *hdir2)
25982 +{
25983 +       struct dentry *d;
25984 +
25985 +       lockdep_off();
25986 +       d = lock_rename(d1, d2);
25987 +       lockdep_on();
25988 +       au_hn_suspend(hdir1);
25989 +       if (hdir1 != hdir2)
25990 +               au_hn_suspend(hdir2);
25991 +
25992 +       return d;
25993 +}
25994 +
25995 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
25996 +                        struct dentry *d2, struct au_hinode *hdir2)
25997 +{
25998 +       au_hn_resume(hdir1);
25999 +       if (hdir1 != hdir2)
26000 +               au_hn_resume(hdir2);
26001 +       lockdep_off();
26002 +       unlock_rename(d1, d2);
26003 +       lockdep_on();
26004 +}
26005 +
26006 +/* ---------------------------------------------------------------------- */
26007 +
26008 +int vfsub_create(struct inode *dir, struct path *path, int mode)
26009 +{
26010 +       int err;
26011 +       struct dentry *d;
26012 +
26013 +       IMustLock(dir);
26014 +
26015 +       d = path->dentry;
26016 +       path->dentry = d->d_parent;
26017 +       err = security_path_mknod(path, d, mode, 0);
26018 +       path->dentry = d;
26019 +       if (unlikely(err))
26020 +               goto out;
26021 +
26022 +       if (au_test_fs_null_nd(dir->i_sb))
26023 +               err = vfs_create(dir, path->dentry, mode, NULL);
26024 +       else {
26025 +               struct nameidata h_nd;
26026 +
26027 +               memset(&h_nd, 0, sizeof(h_nd));
26028 +               h_nd.flags = LOOKUP_CREATE;
26029 +               h_nd.intent.open.flags = O_CREAT
26030 +                       | vfsub_fmode_to_uint(FMODE_READ);
26031 +               h_nd.intent.open.create_mode = mode;
26032 +               h_nd.path.dentry = path->dentry->d_parent;
26033 +               h_nd.path.mnt = path->mnt;
26034 +               path_get(&h_nd.path);
26035 +               err = vfs_create(dir, path->dentry, mode, &h_nd);
26036 +               path_put(&h_nd.path);
26037 +       }
26038 +
26039 +       if (!err) {
26040 +               struct path tmp = *path;
26041 +               int did;
26042 +
26043 +               vfsub_update_h_iattr(&tmp, &did);
26044 +               if (did) {
26045 +                       tmp.dentry = path->dentry->d_parent;
26046 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26047 +               }
26048 +               /*ignore*/
26049 +       }
26050 +
26051 +out:
26052 +       return err;
26053 +}
26054 +
26055 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
26056 +{
26057 +       int err;
26058 +       struct dentry *d;
26059 +
26060 +       IMustLock(dir);
26061 +
26062 +       d = path->dentry;
26063 +       path->dentry = d->d_parent;
26064 +       err = security_path_symlink(path, d, symname);
26065 +       path->dentry = d;
26066 +       if (unlikely(err))
26067 +               goto out;
26068 +
26069 +       err = vfs_symlink(dir, path->dentry, symname);
26070 +       if (!err) {
26071 +               struct path tmp = *path;
26072 +               int did;
26073 +
26074 +               vfsub_update_h_iattr(&tmp, &did);
26075 +               if (did) {
26076 +                       tmp.dentry = path->dentry->d_parent;
26077 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26078 +               }
26079 +               /*ignore*/
26080 +       }
26081 +
26082 +out:
26083 +       return err;
26084 +}
26085 +
26086 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
26087 +{
26088 +       int err;
26089 +       struct dentry *d;
26090 +
26091 +       IMustLock(dir);
26092 +
26093 +       d = path->dentry;
26094 +       path->dentry = d->d_parent;
26095 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
26096 +       path->dentry = d;
26097 +       if (unlikely(err))
26098 +               goto out;
26099 +
26100 +       err = vfs_mknod(dir, path->dentry, mode, dev);
26101 +       if (!err) {
26102 +               struct path tmp = *path;
26103 +               int did;
26104 +
26105 +               vfsub_update_h_iattr(&tmp, &did);
26106 +               if (did) {
26107 +                       tmp.dentry = path->dentry->d_parent;
26108 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26109 +               }
26110 +               /*ignore*/
26111 +       }
26112 +
26113 +out:
26114 +       return err;
26115 +}
26116 +
26117 +static int au_test_nlink(struct inode *inode)
26118 +{
26119 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
26120 +
26121 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
26122 +           || inode->i_nlink < link_max)
26123 +               return 0;
26124 +       return -EMLINK;
26125 +}
26126 +
26127 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
26128 +{
26129 +       int err;
26130 +       struct dentry *d;
26131 +
26132 +       IMustLock(dir);
26133 +
26134 +       err = au_test_nlink(src_dentry->d_inode);
26135 +       if (unlikely(err))
26136 +               return err;
26137 +
26138 +       d = path->dentry;
26139 +       path->dentry = d->d_parent;
26140 +       err = security_path_link(src_dentry, path, d);
26141 +       path->dentry = d;
26142 +       if (unlikely(err))
26143 +               goto out;
26144 +
26145 +       lockdep_off();
26146 +       err = vfs_link(src_dentry, dir, path->dentry);
26147 +       lockdep_on();
26148 +       if (!err) {
26149 +               struct path tmp = *path;
26150 +               int did;
26151 +
26152 +               /* fuse has different memory inode for the same inumber */
26153 +               vfsub_update_h_iattr(&tmp, &did);
26154 +               if (did) {
26155 +                       tmp.dentry = path->dentry->d_parent;
26156 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26157 +                       tmp.dentry = src_dentry;
26158 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26159 +               }
26160 +               /*ignore*/
26161 +       }
26162 +
26163 +out:
26164 +       return err;
26165 +}
26166 +
26167 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
26168 +                struct inode *dir, struct path *path)
26169 +{
26170 +       int err;
26171 +       struct path tmp = {
26172 +               .mnt    = path->mnt
26173 +       };
26174 +       struct dentry *d;
26175 +
26176 +       IMustLock(dir);
26177 +       IMustLock(src_dir);
26178 +
26179 +       d = path->dentry;
26180 +       path->dentry = d->d_parent;
26181 +       tmp.dentry = src_dentry->d_parent;
26182 +       err = security_path_rename(&tmp, src_dentry, path, d);
26183 +       path->dentry = d;
26184 +       if (unlikely(err))
26185 +               goto out;
26186 +
26187 +       lockdep_off();
26188 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
26189 +       lockdep_on();
26190 +       if (!err) {
26191 +               int did;
26192 +
26193 +               tmp.dentry = d->d_parent;
26194 +               vfsub_update_h_iattr(&tmp, &did);
26195 +               if (did) {
26196 +                       tmp.dentry = src_dentry;
26197 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26198 +                       tmp.dentry = src_dentry->d_parent;
26199 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26200 +               }
26201 +               /*ignore*/
26202 +       }
26203 +
26204 +out:
26205 +       return err;
26206 +}
26207 +
26208 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
26209 +{
26210 +       int err;
26211 +       struct dentry *d;
26212 +
26213 +       IMustLock(dir);
26214 +
26215 +       d = path->dentry;
26216 +       path->dentry = d->d_parent;
26217 +       err = security_path_mkdir(path, d, mode);
26218 +       path->dentry = d;
26219 +       if (unlikely(err))
26220 +               goto out;
26221 +
26222 +       err = vfs_mkdir(dir, path->dentry, mode);
26223 +       if (!err) {
26224 +               struct path tmp = *path;
26225 +               int did;
26226 +
26227 +               vfsub_update_h_iattr(&tmp, &did);
26228 +               if (did) {
26229 +                       tmp.dentry = path->dentry->d_parent;
26230 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26231 +               }
26232 +               /*ignore*/
26233 +       }
26234 +
26235 +out:
26236 +       return err;
26237 +}
26238 +
26239 +int vfsub_rmdir(struct inode *dir, struct path *path)
26240 +{
26241 +       int err;
26242 +       struct dentry *d;
26243 +
26244 +       IMustLock(dir);
26245 +
26246 +       d = path->dentry;
26247 +       path->dentry = d->d_parent;
26248 +       err = security_path_rmdir(path, d);
26249 +       path->dentry = d;
26250 +       if (unlikely(err))
26251 +               goto out;
26252 +
26253 +       lockdep_off();
26254 +       err = vfs_rmdir(dir, path->dentry);
26255 +       lockdep_on();
26256 +       if (!err) {
26257 +               struct path tmp = {
26258 +                       .dentry = path->dentry->d_parent,
26259 +                       .mnt    = path->mnt
26260 +               };
26261 +
26262 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
26263 +       }
26264 +
26265 +out:
26266 +       return err;
26267 +}
26268 +
26269 +/* ---------------------------------------------------------------------- */
26270 +
26271 +/* todo: support mmap_sem? */
26272 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
26273 +                    loff_t *ppos)
26274 +{
26275 +       ssize_t err;
26276 +
26277 +       lockdep_off();
26278 +       err = vfs_read(file, ubuf, count, ppos);
26279 +       lockdep_on();
26280 +       if (err >= 0)
26281 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
26282 +       return err;
26283 +}
26284 +
26285 +/* todo: kernel_read()? */
26286 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
26287 +                    loff_t *ppos)
26288 +{
26289 +       ssize_t err;
26290 +       mm_segment_t oldfs;
26291 +       union {
26292 +               void *k;
26293 +               char __user *u;
26294 +       } buf;
26295 +
26296 +       buf.k = kbuf;
26297 +       oldfs = get_fs();
26298 +       set_fs(KERNEL_DS);
26299 +       err = vfsub_read_u(file, buf.u, count, ppos);
26300 +       set_fs(oldfs);
26301 +       return err;
26302 +}
26303 +
26304 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
26305 +                     loff_t *ppos)
26306 +{
26307 +       ssize_t err;
26308 +
26309 +       lockdep_off();
26310 +       err = vfs_write(file, ubuf, count, ppos);
26311 +       lockdep_on();
26312 +       if (err >= 0)
26313 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
26314 +       return err;
26315 +}
26316 +
26317 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
26318 +{
26319 +       ssize_t err;
26320 +       mm_segment_t oldfs;
26321 +       union {
26322 +               void *k;
26323 +               const char __user *u;
26324 +       } buf;
26325 +
26326 +       buf.k = kbuf;
26327 +       oldfs = get_fs();
26328 +       set_fs(KERNEL_DS);
26329 +       err = vfsub_write_u(file, buf.u, count, ppos);
26330 +       set_fs(oldfs);
26331 +       return err;
26332 +}
26333 +
26334 +int vfsub_flush(struct file *file, fl_owner_t id)
26335 +{
26336 +       int err;
26337 +
26338 +       err = 0;
26339 +       if (file->f_op && file->f_op->flush) {
26340 +               if (!au_test_nfs(file->f_dentry->d_sb))
26341 +                       err = file->f_op->flush(file, id);
26342 +               else {
26343 +                       lockdep_off();
26344 +                       err = file->f_op->flush(file, id);
26345 +                       lockdep_on();
26346 +               }
26347 +               if (!err)
26348 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
26349 +               /*ignore*/
26350 +       }
26351 +       return err;
26352 +}
26353 +
26354 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
26355 +{
26356 +       int err;
26357 +
26358 +       lockdep_off();
26359 +       err = vfs_readdir(file, filldir, arg);
26360 +       lockdep_on();
26361 +       if (err >= 0)
26362 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
26363 +       return err;
26364 +}
26365 +
26366 +long vfsub_splice_to(struct file *in, loff_t *ppos,
26367 +                    struct pipe_inode_info *pipe, size_t len,
26368 +                    unsigned int flags)
26369 +{
26370 +       long err;
26371 +
26372 +       lockdep_off();
26373 +       err = do_splice_to(in, ppos, pipe, len, flags);
26374 +       lockdep_on();
26375 +       file_accessed(in);
26376 +       if (err >= 0)
26377 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
26378 +       return err;
26379 +}
26380 +
26381 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
26382 +                      loff_t *ppos, size_t len, unsigned int flags)
26383 +{
26384 +       long err;
26385 +
26386 +       lockdep_off();
26387 +       err = do_splice_from(pipe, out, ppos, len, flags);
26388 +       lockdep_on();
26389 +       if (err >= 0)
26390 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
26391 +       return err;
26392 +}
26393 +
26394 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
26395 +{
26396 +       int err;
26397 +
26398 +       /* file can be NULL */
26399 +       lockdep_off();
26400 +       err = vfs_fsync(file, datasync);
26401 +       lockdep_on();
26402 +       if (!err) {
26403 +               if (!path) {
26404 +                       AuDebugOn(!file);
26405 +                       path = &file->f_path;
26406 +               }
26407 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
26408 +       }
26409 +       return err;
26410 +}
26411 +
26412 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
26413 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
26414 +               struct file *h_file)
26415 +{
26416 +       int err;
26417 +       struct inode *h_inode;
26418 +
26419 +       h_inode = h_path->dentry->d_inode;
26420 +       if (!h_file) {
26421 +               err = mnt_want_write(h_path->mnt);
26422 +               if (err)
26423 +                       goto out;
26424 +               err = inode_permission(h_inode, MAY_WRITE);
26425 +               if (err)
26426 +                       goto out_mnt;
26427 +               err = get_write_access(h_inode);
26428 +               if (err)
26429 +                       goto out_mnt;
26430 +               err = break_lease(h_inode, O_WRONLY);
26431 +               if (err)
26432 +                       goto out_inode;
26433 +       }
26434 +
26435 +       err = locks_verify_truncate(h_inode, h_file, length);
26436 +       if (!err)
26437 +               err = security_path_truncate(h_path);
26438 +       if (!err) {
26439 +               lockdep_off();
26440 +               err = do_truncate(h_path->dentry, length, attr, h_file);
26441 +               lockdep_on();
26442 +       }
26443 +
26444 +out_inode:
26445 +       if (!h_file)
26446 +               put_write_access(h_inode);
26447 +out_mnt:
26448 +       if (!h_file)
26449 +               mnt_drop_write(h_path->mnt);
26450 +out:
26451 +       return err;
26452 +}
26453 +
26454 +/* ---------------------------------------------------------------------- */
26455 +
26456 +struct au_vfsub_mkdir_args {
26457 +       int *errp;
26458 +       struct inode *dir;
26459 +       struct path *path;
26460 +       int mode;
26461 +};
26462 +
26463 +static void au_call_vfsub_mkdir(void *args)
26464 +{
26465 +       struct au_vfsub_mkdir_args *a = args;
26466 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
26467 +}
26468 +
26469 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
26470 +{
26471 +       int err, do_sio, wkq_err;
26472 +
26473 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
26474 +       if (!do_sio)
26475 +               err = vfsub_mkdir(dir, path, mode);
26476 +       else {
26477 +               struct au_vfsub_mkdir_args args = {
26478 +                       .errp   = &err,
26479 +                       .dir    = dir,
26480 +                       .path   = path,
26481 +                       .mode   = mode
26482 +               };
26483 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
26484 +               if (unlikely(wkq_err))
26485 +                       err = wkq_err;
26486 +       }
26487 +
26488 +       return err;
26489 +}
26490 +
26491 +struct au_vfsub_rmdir_args {
26492 +       int *errp;
26493 +       struct inode *dir;
26494 +       struct path *path;
26495 +};
26496 +
26497 +static void au_call_vfsub_rmdir(void *args)
26498 +{
26499 +       struct au_vfsub_rmdir_args *a = args;
26500 +       *a->errp = vfsub_rmdir(a->dir, a->path);
26501 +}
26502 +
26503 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
26504 +{
26505 +       int err, do_sio, wkq_err;
26506 +
26507 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
26508 +       if (!do_sio)
26509 +               err = vfsub_rmdir(dir, path);
26510 +       else {
26511 +               struct au_vfsub_rmdir_args args = {
26512 +                       .errp   = &err,
26513 +                       .dir    = dir,
26514 +                       .path   = path
26515 +               };
26516 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
26517 +               if (unlikely(wkq_err))
26518 +                       err = wkq_err;
26519 +       }
26520 +
26521 +       return err;
26522 +}
26523 +
26524 +/* ---------------------------------------------------------------------- */
26525 +
26526 +struct notify_change_args {
26527 +       int *errp;
26528 +       struct path *path;
26529 +       struct iattr *ia;
26530 +};
26531 +
26532 +static void call_notify_change(void *args)
26533 +{
26534 +       struct notify_change_args *a = args;
26535 +       struct inode *h_inode;
26536 +
26537 +       h_inode = a->path->dentry->d_inode;
26538 +       IMustLock(h_inode);
26539 +
26540 +       *a->errp = -EPERM;
26541 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
26542 +               *a->errp = notify_change(a->path->dentry, a->ia);
26543 +               if (!*a->errp)
26544 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
26545 +       }
26546 +       AuTraceErr(*a->errp);
26547 +}
26548 +
26549 +int vfsub_notify_change(struct path *path, struct iattr *ia)
26550 +{
26551 +       int err;
26552 +       struct notify_change_args args = {
26553 +               .errp   = &err,
26554 +               .path   = path,
26555 +               .ia     = ia
26556 +       };
26557 +
26558 +       call_notify_change(&args);
26559 +
26560 +       return err;
26561 +}
26562 +
26563 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
26564 +{
26565 +       int err, wkq_err;
26566 +       struct notify_change_args args = {
26567 +               .errp   = &err,
26568 +               .path   = path,
26569 +               .ia     = ia
26570 +       };
26571 +
26572 +       wkq_err = au_wkq_wait(call_notify_change, &args);
26573 +       if (unlikely(wkq_err))
26574 +               err = wkq_err;
26575 +
26576 +       return err;
26577 +}
26578 +
26579 +/* ---------------------------------------------------------------------- */
26580 +
26581 +struct unlink_args {
26582 +       int *errp;
26583 +       struct inode *dir;
26584 +       struct path *path;
26585 +};
26586 +
26587 +static void call_unlink(void *args)
26588 +{
26589 +       struct unlink_args *a = args;
26590 +       struct dentry *d = a->path->dentry;
26591 +       struct inode *h_inode;
26592 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
26593 +                                     && d->d_count == 1);
26594 +
26595 +       IMustLock(a->dir);
26596 +
26597 +       a->path->dentry = d->d_parent;
26598 +       *a->errp = security_path_unlink(a->path, d);
26599 +       a->path->dentry = d;
26600 +       if (unlikely(*a->errp))
26601 +               return;
26602 +
26603 +       if (!stop_sillyrename)
26604 +               dget(d);
26605 +       h_inode = d->d_inode;
26606 +       if (h_inode)
26607 +               ihold(h_inode);
26608 +
26609 +       lockdep_off();
26610 +       *a->errp = vfs_unlink(a->dir, d);
26611 +       lockdep_on();
26612 +       if (!*a->errp) {
26613 +               struct path tmp = {
26614 +                       .dentry = d->d_parent,
26615 +                       .mnt    = a->path->mnt
26616 +               };
26617 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
26618 +       }
26619 +
26620 +       if (!stop_sillyrename)
26621 +               dput(d);
26622 +       if (h_inode)
26623 +               iput(h_inode);
26624 +
26625 +       AuTraceErr(*a->errp);
26626 +}
26627 +
26628 +/*
26629 + * @dir: must be locked.
26630 + * @dentry: target dentry.
26631 + */
26632 +int vfsub_unlink(struct inode *dir, struct path *path, int force)
26633 +{
26634 +       int err;
26635 +       struct unlink_args args = {
26636 +               .errp   = &err,
26637 +               .dir    = dir,
26638 +               .path   = path
26639 +       };
26640 +
26641 +       if (!force)
26642 +               call_unlink(&args);
26643 +       else {
26644 +               int wkq_err;
26645 +
26646 +               wkq_err = au_wkq_wait(call_unlink, &args);
26647 +               if (unlikely(wkq_err))
26648 +                       err = wkq_err;
26649 +       }
26650 +
26651 +       return err;
26652 +}
26653 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
26654 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
26655 +++ linux/fs/aufs/vfsub.h       2011-08-24 13:30:24.734646739 +0200
26656 @@ -0,0 +1,232 @@
26657 +/*
26658 + * Copyright (C) 2005-2011 Junjiro R. Okajima
26659 + *
26660 + * This program, aufs is free software; you can redistribute it and/or modify
26661 + * it under the terms of the GNU General Public License as published by
26662 + * the Free Software Foundation; either version 2 of the License, or
26663 + * (at your option) any later version.
26664 + *
26665 + * This program is distributed in the hope that it will be useful,
26666 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26667 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26668 + * GNU General Public License for more details.
26669 + *
26670 + * You should have received a copy of the GNU General Public License
26671 + * along with this program; if not, write to the Free Software
26672 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26673 + */
26674 +
26675 +/*
26676 + * sub-routines for VFS
26677 + */
26678 +
26679 +#ifndef __AUFS_VFSUB_H__
26680 +#define __AUFS_VFSUB_H__
26681 +
26682 +#ifdef __KERNEL__
26683 +
26684 +#include <linux/fs.h>
26685 +#include <linux/lglock.h>
26686 +#include "debug.h"
26687 +
26688 +/* copied from linux/fs/internal.h */
26689 +/* todo: BAD approach!! */
26690 +DECLARE_BRLOCK(vfsmount_lock);
26691 +extern void file_sb_list_del(struct file *f);
26692 +extern spinlock_t inode_sb_list_lock;
26693 +
26694 +/* copied from linux/fs/file_table.c */
26695 +DECLARE_LGLOCK(files_lglock);
26696 +#ifdef CONFIG_SMP
26697 +/*
26698 + * These macros iterate all files on all CPUs for a given superblock.
26699 + * files_lglock must be held globally.
26700 + */
26701 +#define do_file_list_for_each_entry(__sb, __file)              \
26702 +{                                                              \
26703 +       int i;                                                  \
26704 +       for_each_possible_cpu(i) {                              \
26705 +               struct list_head *list;                         \
26706 +               list = per_cpu_ptr((__sb)->s_files, i);         \
26707 +               list_for_each_entry((__file), list, f_u.fu_list)
26708 +
26709 +#define while_file_list_for_each_entry                         \
26710 +       }                                                       \
26711 +}
26712 +
26713 +#else
26714 +
26715 +#define do_file_list_for_each_entry(__sb, __file)              \
26716 +{                                                              \
26717 +       struct list_head *list;                                 \
26718 +       list = &(sb)->s_files;                                  \
26719 +       list_for_each_entry((__file), list, f_u.fu_list)
26720 +
26721 +#define while_file_list_for_each_entry                         \
26722 +}
26723 +#endif
26724 +
26725 +/* ---------------------------------------------------------------------- */
26726 +
26727 +/* lock subclass for lower inode */
26728 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
26729 +/* reduce? gave up. */
26730 +enum {
26731 +       AuLsc_I_Begin = I_MUTEX_QUOTA, /* 4 */
26732 +       AuLsc_I_PARENT,         /* lower inode, parent first */
26733 +       AuLsc_I_PARENT2,        /* copyup dirs */
26734 +       AuLsc_I_PARENT3,        /* copyup wh */
26735 +       AuLsc_I_CHILD,
26736 +       AuLsc_I_CHILD2,
26737 +       AuLsc_I_End
26738 +};
26739 +
26740 +/* to debug easier, do not make them inlined functions */
26741 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
26742 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
26743 +
26744 +/* ---------------------------------------------------------------------- */
26745 +
26746 +static inline void vfsub_drop_nlink(struct inode *inode)
26747 +{
26748 +       AuDebugOn(!inode->i_nlink);
26749 +       drop_nlink(inode);
26750 +}
26751 +
26752 +static inline void vfsub_dead_dir(struct inode *inode)
26753 +{
26754 +       AuDebugOn(!S_ISDIR(inode->i_mode));
26755 +       inode->i_flags |= S_DEAD;
26756 +       clear_nlink(inode);
26757 +}
26758 +
26759 +/* ---------------------------------------------------------------------- */
26760 +
26761 +int vfsub_update_h_iattr(struct path *h_path, int *did);
26762 +struct file *vfsub_dentry_open(struct path *path, int flags);
26763 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
26764 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
26765 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
26766 +                                   int len);
26767 +struct dentry *vfsub_lookup_hash(struct nameidata *nd);
26768 +int vfsub_name_hash(const char *name, struct qstr *this, int len);
26769 +
26770 +/* ---------------------------------------------------------------------- */
26771 +
26772 +struct au_hinode;
26773 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
26774 +                                struct dentry *d2, struct au_hinode *hdir2);
26775 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
26776 +                        struct dentry *d2, struct au_hinode *hdir2);
26777 +
26778 +int vfsub_create(struct inode *dir, struct path *path, int mode);
26779 +int vfsub_symlink(struct inode *dir, struct path *path,
26780 +                 const char *symname);
26781 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
26782 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
26783 +              struct path *path);
26784 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
26785 +                struct inode *hdir, struct path *path);
26786 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
26787 +int vfsub_rmdir(struct inode *dir, struct path *path);
26788 +
26789 +/* ---------------------------------------------------------------------- */
26790 +
26791 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
26792 +                    loff_t *ppos);
26793 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
26794 +                       loff_t *ppos);
26795 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
26796 +                     loff_t *ppos);
26797 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
26798 +                     loff_t *ppos);
26799 +int vfsub_flush(struct file *file, fl_owner_t id);
26800 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg);
26801 +
26802 +static inline unsigned int vfsub_file_flags(struct file *file)
26803 +{
26804 +       unsigned int flags;
26805 +
26806 +       spin_lock(&file->f_lock);
26807 +       flags = file->f_flags;
26808 +       spin_unlock(&file->f_lock);
26809 +
26810 +       return flags;
26811 +}
26812 +
26813 +static inline void vfsub_file_accessed(struct file *h_file)
26814 +{
26815 +       file_accessed(h_file);
26816 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
26817 +}
26818 +
26819 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
26820 +                                    struct dentry *h_dentry)
26821 +{
26822 +       struct path h_path = {
26823 +               .dentry = h_dentry,
26824 +               .mnt    = h_mnt
26825 +       };
26826 +       touch_atime(h_mnt, h_dentry);
26827 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
26828 +}
26829 +
26830 +long vfsub_splice_to(struct file *in, loff_t *ppos,
26831 +                    struct pipe_inode_info *pipe, size_t len,
26832 +                    unsigned int flags);
26833 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
26834 +                      loff_t *ppos, size_t len, unsigned int flags);
26835 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
26836 +               struct file *h_file);
26837 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
26838 +
26839 +/* ---------------------------------------------------------------------- */
26840 +
26841 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
26842 +{
26843 +       loff_t err;
26844 +
26845 +       lockdep_off();
26846 +       err = vfs_llseek(file, offset, origin);
26847 +       lockdep_on();
26848 +       return err;
26849 +}
26850 +
26851 +/* ---------------------------------------------------------------------- */
26852 +
26853 +/* dirty workaround for strict type of fmode_t */
26854 +union vfsub_fmu {
26855 +       fmode_t fm;
26856 +       unsigned int ui;
26857 +};
26858 +
26859 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
26860 +{
26861 +       union vfsub_fmu u = {
26862 +               .fm = fm
26863 +       };
26864 +
26865 +       BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
26866 +
26867 +       return u.ui;
26868 +}
26869 +
26870 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
26871 +{
26872 +       union vfsub_fmu u = {
26873 +               .ui = ui
26874 +       };
26875 +
26876 +       return u.fm;
26877 +}
26878 +
26879 +/* ---------------------------------------------------------------------- */
26880 +
26881 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
26882 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
26883 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia);
26884 +int vfsub_notify_change(struct path *path, struct iattr *ia);
26885 +int vfsub_unlink(struct inode *dir, struct path *path, int force);
26886 +
26887 +#endif /* __KERNEL__ */
26888 +#endif /* __AUFS_VFSUB_H__ */
26889 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
26890 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
26891 +++ linux/fs/aufs/wbr_policy.c  2011-08-24 13:30:24.734646739 +0200
26892 @@ -0,0 +1,700 @@
26893 +/*
26894 + * Copyright (C) 2005-2011 Junjiro R. Okajima
26895 + *
26896 + * This program, aufs is free software; you can redistribute it and/or modify
26897 + * it under the terms of the GNU General Public License as published by
26898 + * the Free Software Foundation; either version 2 of the License, or
26899 + * (at your option) any later version.
26900 + *
26901 + * This program is distributed in the hope that it will be useful,
26902 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26903 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26904 + * GNU General Public License for more details.
26905 + *
26906 + * You should have received a copy of the GNU General Public License
26907 + * along with this program; if not, write to the Free Software
26908 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26909 + */
26910 +
26911 +/*
26912 + * policies for selecting one among multiple writable branches
26913 + */
26914 +
26915 +#include <linux/statfs.h>
26916 +#include "aufs.h"
26917 +
26918 +/* subset of cpup_attr() */
26919 +static noinline_for_stack
26920 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
26921 +{
26922 +       int err, sbits;
26923 +       struct iattr ia;
26924 +       struct inode *h_isrc;
26925 +
26926 +       h_isrc = h_src->d_inode;
26927 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
26928 +       ia.ia_mode = h_isrc->i_mode;
26929 +       ia.ia_uid = h_isrc->i_uid;
26930 +       ia.ia_gid = h_isrc->i_gid;
26931 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
26932 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc);
26933 +       err = vfsub_sio_notify_change(h_path, &ia);
26934 +
26935 +       /* is this nfs only? */
26936 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
26937 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
26938 +               ia.ia_mode = h_isrc->i_mode;
26939 +               err = vfsub_sio_notify_change(h_path, &ia);
26940 +       }
26941 +
26942 +       return err;
26943 +}
26944 +
26945 +#define AuCpdown_PARENT_OPQ    1
26946 +#define AuCpdown_WHED          (1 << 1)
26947 +#define AuCpdown_MADE_DIR      (1 << 2)
26948 +#define AuCpdown_DIROPQ                (1 << 3)
26949 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
26950 +#define au_fset_cpdown(flags, name) \
26951 +       do { (flags) |= AuCpdown_##name; } while (0)
26952 +#define au_fclr_cpdown(flags, name) \
26953 +       do { (flags) &= ~AuCpdown_##name; } while (0)
26954 +
26955 +struct au_cpdown_dir_args {
26956 +       struct dentry *parent;
26957 +       unsigned int flags;
26958 +};
26959 +
26960 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
26961 +                            struct au_cpdown_dir_args *a)
26962 +{
26963 +       int err;
26964 +       struct dentry *opq_dentry;
26965 +
26966 +       opq_dentry = au_diropq_create(dentry, bdst);
26967 +       err = PTR_ERR(opq_dentry);
26968 +       if (IS_ERR(opq_dentry))
26969 +               goto out;
26970 +       dput(opq_dentry);
26971 +       au_fset_cpdown(a->flags, DIROPQ);
26972 +
26973 +out:
26974 +       return err;
26975 +}
26976 +
26977 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
26978 +                           struct inode *dir, aufs_bindex_t bdst)
26979 +{
26980 +       int err;
26981 +       struct path h_path;
26982 +       struct au_branch *br;
26983 +
26984 +       br = au_sbr(dentry->d_sb, bdst);
26985 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
26986 +       err = PTR_ERR(h_path.dentry);
26987 +       if (IS_ERR(h_path.dentry))
26988 +               goto out;
26989 +
26990 +       err = 0;
26991 +       if (h_path.dentry->d_inode) {
26992 +               h_path.mnt = br->br_mnt;
26993 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
26994 +                                         dentry);
26995 +       }
26996 +       dput(h_path.dentry);
26997 +
26998 +out:
26999 +       return err;
27000 +}
27001 +
27002 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
27003 +                        struct dentry *h_parent, void *arg)
27004 +{
27005 +       int err, rerr;
27006 +       aufs_bindex_t bopq, bstart;
27007 +       struct path h_path;
27008 +       struct dentry *parent;
27009 +       struct inode *h_dir, *h_inode, *inode, *dir;
27010 +       struct au_cpdown_dir_args *args = arg;
27011 +
27012 +       bstart = au_dbstart(dentry);
27013 +       /* dentry is di-locked */
27014 +       parent = dget_parent(dentry);
27015 +       dir = parent->d_inode;
27016 +       h_dir = h_parent->d_inode;
27017 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
27018 +       IMustLock(h_dir);
27019 +
27020 +       err = au_lkup_neg(dentry, bdst);
27021 +       if (unlikely(err < 0))
27022 +               goto out;
27023 +       h_path.dentry = au_h_dptr(dentry, bdst);
27024 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
27025 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
27026 +                             S_IRWXU | S_IRUGO | S_IXUGO);
27027 +       if (unlikely(err))
27028 +               goto out_put;
27029 +       au_fset_cpdown(args->flags, MADE_DIR);
27030 +
27031 +       bopq = au_dbdiropq(dentry);
27032 +       au_fclr_cpdown(args->flags, WHED);
27033 +       au_fclr_cpdown(args->flags, DIROPQ);
27034 +       if (au_dbwh(dentry) == bdst)
27035 +               au_fset_cpdown(args->flags, WHED);
27036 +       if (!au_ftest_cpdown(args->flags, PARENT_OPQ) && bopq <= bdst)
27037 +               au_fset_cpdown(args->flags, PARENT_OPQ);
27038 +       h_inode = h_path.dentry->d_inode;
27039 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
27040 +       if (au_ftest_cpdown(args->flags, WHED)) {
27041 +               err = au_cpdown_dir_opq(dentry, bdst, args);
27042 +               if (unlikely(err)) {
27043 +                       mutex_unlock(&h_inode->i_mutex);
27044 +                       goto out_dir;
27045 +               }
27046 +       }
27047 +
27048 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
27049 +       mutex_unlock(&h_inode->i_mutex);
27050 +       if (unlikely(err))
27051 +               goto out_opq;
27052 +
27053 +       if (au_ftest_cpdown(args->flags, WHED)) {
27054 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
27055 +               if (unlikely(err))
27056 +                       goto out_opq;
27057 +       }
27058 +
27059 +       inode = dentry->d_inode;
27060 +       if (au_ibend(inode) < bdst)
27061 +               au_set_ibend(inode, bdst);
27062 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
27063 +                     au_hi_flags(inode, /*isdir*/1));
27064 +       goto out; /* success */
27065 +
27066 +       /* revert */
27067 +out_opq:
27068 +       if (au_ftest_cpdown(args->flags, DIROPQ)) {
27069 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
27070 +               rerr = au_diropq_remove(dentry, bdst);
27071 +               mutex_unlock(&h_inode->i_mutex);
27072 +               if (unlikely(rerr)) {
27073 +                       AuIOErr("failed removing diropq for %.*s b%d (%d)\n",
27074 +                               AuDLNPair(dentry), bdst, rerr);
27075 +                       err = -EIO;
27076 +                       goto out;
27077 +               }
27078 +       }
27079 +out_dir:
27080 +       if (au_ftest_cpdown(args->flags, MADE_DIR)) {
27081 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
27082 +               if (unlikely(rerr)) {
27083 +                       AuIOErr("failed removing %.*s b%d (%d)\n",
27084 +                               AuDLNPair(dentry), bdst, rerr);
27085 +                       err = -EIO;
27086 +               }
27087 +       }
27088 +out_put:
27089 +       au_set_h_dptr(dentry, bdst, NULL);
27090 +       if (au_dbend(dentry) == bdst)
27091 +               au_update_dbend(dentry);
27092 +out:
27093 +       dput(parent);
27094 +       return err;
27095 +}
27096 +
27097 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
27098 +{
27099 +       int err;
27100 +       struct au_cpdown_dir_args args = {
27101 +               .parent = dget_parent(dentry),
27102 +               .flags  = 0
27103 +       };
27104 +
27105 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &args);
27106 +       dput(args.parent);
27107 +
27108 +       return err;
27109 +}
27110 +
27111 +/* ---------------------------------------------------------------------- */
27112 +
27113 +/* policies for create */
27114 +
27115 +static int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
27116 +{
27117 +       int err, i, j, ndentry;
27118 +       aufs_bindex_t bopq;
27119 +       struct au_dcsub_pages dpages;
27120 +       struct au_dpage *dpage;
27121 +       struct dentry **dentries, *parent, *d;
27122 +
27123 +       err = au_dpages_init(&dpages, GFP_NOFS);
27124 +       if (unlikely(err))
27125 +               goto out;
27126 +       parent = dget_parent(dentry);
27127 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
27128 +       if (unlikely(err))
27129 +               goto out_free;
27130 +
27131 +       err = bindex;
27132 +       for (i = 0; i < dpages.ndpage; i++) {
27133 +               dpage = dpages.dpages + i;
27134 +               dentries = dpage->dentries;
27135 +               ndentry = dpage->ndentry;
27136 +               for (j = 0; j < ndentry; j++) {
27137 +                       d = dentries[j];
27138 +                       di_read_lock_parent2(d, !AuLock_IR);
27139 +                       bopq = au_dbdiropq(d);
27140 +                       di_read_unlock(d, !AuLock_IR);
27141 +                       if (bopq >= 0 && bopq < err)
27142 +                               err = bopq;
27143 +               }
27144 +       }
27145 +
27146 +out_free:
27147 +       dput(parent);
27148 +       au_dpages_free(&dpages);
27149 +out:
27150 +       return err;
27151 +}
27152 +
27153 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
27154 +{
27155 +       for (; bindex >= 0; bindex--)
27156 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
27157 +                       return bindex;
27158 +       return -EROFS;
27159 +}
27160 +
27161 +/* top down parent */
27162 +static int au_wbr_create_tdp(struct dentry *dentry, int isdir __maybe_unused)
27163 +{
27164 +       int err;
27165 +       aufs_bindex_t bstart, bindex;
27166 +       struct super_block *sb;
27167 +       struct dentry *parent, *h_parent;
27168 +
27169 +       sb = dentry->d_sb;
27170 +       bstart = au_dbstart(dentry);
27171 +       err = bstart;
27172 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
27173 +               goto out;
27174 +
27175 +       err = -EROFS;
27176 +       parent = dget_parent(dentry);
27177 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
27178 +               h_parent = au_h_dptr(parent, bindex);
27179 +               if (!h_parent || !h_parent->d_inode)
27180 +                       continue;
27181 +
27182 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
27183 +                       err = bindex;
27184 +                       break;
27185 +               }
27186 +       }
27187 +       dput(parent);
27188 +
27189 +       /* bottom up here */
27190 +       if (unlikely(err < 0)) {
27191 +               err = au_wbr_bu(sb, bstart - 1);
27192 +               if (err >= 0)
27193 +                       err = au_wbr_nonopq(dentry, err);
27194 +       }
27195 +
27196 +out:
27197 +       AuDbg("b%d\n", err);
27198 +       return err;
27199 +}
27200 +
27201 +/* ---------------------------------------------------------------------- */
27202 +
27203 +/* an exception for the policy other than tdp */
27204 +static int au_wbr_create_exp(struct dentry *dentry)
27205 +{
27206 +       int err;
27207 +       aufs_bindex_t bwh, bdiropq;
27208 +       struct dentry *parent;
27209 +
27210 +       err = -1;
27211 +       bwh = au_dbwh(dentry);
27212 +       parent = dget_parent(dentry);
27213 +       bdiropq = au_dbdiropq(parent);
27214 +       if (bwh >= 0) {
27215 +               if (bdiropq >= 0)
27216 +                       err = min(bdiropq, bwh);
27217 +               else
27218 +                       err = bwh;
27219 +               AuDbg("%d\n", err);
27220 +       } else if (bdiropq >= 0) {
27221 +               err = bdiropq;
27222 +               AuDbg("%d\n", err);
27223 +       }
27224 +       dput(parent);
27225 +
27226 +       if (err >= 0)
27227 +               err = au_wbr_nonopq(dentry, err);
27228 +
27229 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
27230 +               err = -1;
27231 +
27232 +       AuDbg("%d\n", err);
27233 +       return err;
27234 +}
27235 +
27236 +/* ---------------------------------------------------------------------- */
27237 +
27238 +/* round robin */
27239 +static int au_wbr_create_init_rr(struct super_block *sb)
27240 +{
27241 +       int err;
27242 +
27243 +       err = au_wbr_bu(sb, au_sbend(sb));
27244 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
27245 +       /* smp_mb(); */
27246 +
27247 +       AuDbg("b%d\n", err);
27248 +       return err;
27249 +}
27250 +
27251 +static int au_wbr_create_rr(struct dentry *dentry, int isdir)
27252 +{
27253 +       int err, nbr;
27254 +       unsigned int u;
27255 +       aufs_bindex_t bindex, bend;
27256 +       struct super_block *sb;
27257 +       atomic_t *next;
27258 +
27259 +       err = au_wbr_create_exp(dentry);
27260 +       if (err >= 0)
27261 +               goto out;
27262 +
27263 +       sb = dentry->d_sb;
27264 +       next = &au_sbi(sb)->si_wbr_rr_next;
27265 +       bend = au_sbend(sb);
27266 +       nbr = bend + 1;
27267 +       for (bindex = 0; bindex <= bend; bindex++) {
27268 +               if (!isdir) {
27269 +                       err = atomic_dec_return(next) + 1;
27270 +                       /* modulo for 0 is meaningless */
27271 +                       if (unlikely(!err))
27272 +                               err = atomic_dec_return(next) + 1;
27273 +               } else
27274 +                       err = atomic_read(next);
27275 +               AuDbg("%d\n", err);
27276 +               u = err;
27277 +               err = u % nbr;
27278 +               AuDbg("%d\n", err);
27279 +               if (!au_br_rdonly(au_sbr(sb, err)))
27280 +                       break;
27281 +               err = -EROFS;
27282 +       }
27283 +
27284 +       if (err >= 0)
27285 +               err = au_wbr_nonopq(dentry, err);
27286 +
27287 +out:
27288 +       AuDbg("%d\n", err);
27289 +       return err;
27290 +}
27291 +
27292 +/* ---------------------------------------------------------------------- */
27293 +
27294 +/* most free space */
27295 +static void au_mfs(struct dentry *dentry)
27296 +{
27297 +       struct super_block *sb;
27298 +       struct au_branch *br;
27299 +       struct au_wbr_mfs *mfs;
27300 +       aufs_bindex_t bindex, bend;
27301 +       int err;
27302 +       unsigned long long b, bavail;
27303 +       struct path h_path;
27304 +       /* reduce the stack usage */
27305 +       struct kstatfs *st;
27306 +
27307 +       st = kmalloc(sizeof(*st), GFP_NOFS);
27308 +       if (unlikely(!st)) {
27309 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
27310 +               return;
27311 +       }
27312 +
27313 +       bavail = 0;
27314 +       sb = dentry->d_sb;
27315 +       mfs = &au_sbi(sb)->si_wbr_mfs;
27316 +       MtxMustLock(&mfs->mfs_lock);
27317 +       mfs->mfs_bindex = -EROFS;
27318 +       mfs->mfsrr_bytes = 0;
27319 +       bend = au_sbend(sb);
27320 +       for (bindex = 0; bindex <= bend; bindex++) {
27321 +               br = au_sbr(sb, bindex);
27322 +               if (au_br_rdonly(br))
27323 +                       continue;
27324 +
27325 +               /* sb->s_root for NFS is unreliable */
27326 +               h_path.mnt = br->br_mnt;
27327 +               h_path.dentry = h_path.mnt->mnt_root;
27328 +               err = vfs_statfs(&h_path, st);
27329 +               if (unlikely(err)) {
27330 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
27331 +                       continue;
27332 +               }
27333 +
27334 +               /* when the available size is equal, select the lower one */
27335 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
27336 +                            || sizeof(b) < sizeof(st->f_bsize));
27337 +               b = st->f_bavail * st->f_bsize;
27338 +               br->br_wbr->wbr_bytes = b;
27339 +               if (b >= bavail) {
27340 +                       bavail = b;
27341 +                       mfs->mfs_bindex = bindex;
27342 +                       mfs->mfs_jiffy = jiffies;
27343 +               }
27344 +       }
27345 +
27346 +       mfs->mfsrr_bytes = bavail;
27347 +       AuDbg("b%d\n", mfs->mfs_bindex);
27348 +       kfree(st);
27349 +}
27350 +
27351 +static int au_wbr_create_mfs(struct dentry *dentry, int isdir __maybe_unused)
27352 +{
27353 +       int err;
27354 +       struct super_block *sb;
27355 +       struct au_wbr_mfs *mfs;
27356 +
27357 +       err = au_wbr_create_exp(dentry);
27358 +       if (err >= 0)
27359 +               goto out;
27360 +
27361 +       sb = dentry->d_sb;
27362 +       mfs = &au_sbi(sb)->si_wbr_mfs;
27363 +       mutex_lock(&mfs->mfs_lock);
27364 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
27365 +           || mfs->mfs_bindex < 0
27366 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
27367 +               au_mfs(dentry);
27368 +       mutex_unlock(&mfs->mfs_lock);
27369 +       err = mfs->mfs_bindex;
27370 +
27371 +       if (err >= 0)
27372 +               err = au_wbr_nonopq(dentry, err);
27373 +
27374 +out:
27375 +       AuDbg("b%d\n", err);
27376 +       return err;
27377 +}
27378 +
27379 +static int au_wbr_create_init_mfs(struct super_block *sb)
27380 +{
27381 +       struct au_wbr_mfs *mfs;
27382 +
27383 +       mfs = &au_sbi(sb)->si_wbr_mfs;
27384 +       mutex_init(&mfs->mfs_lock);
27385 +       mfs->mfs_jiffy = 0;
27386 +       mfs->mfs_bindex = -EROFS;
27387 +
27388 +       return 0;
27389 +}
27390 +
27391 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
27392 +{
27393 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
27394 +       return 0;
27395 +}
27396 +
27397 +/* ---------------------------------------------------------------------- */
27398 +
27399 +/* most free space and then round robin */
27400 +static int au_wbr_create_mfsrr(struct dentry *dentry, int isdir)
27401 +{
27402 +       int err;
27403 +       struct au_wbr_mfs *mfs;
27404 +
27405 +       err = au_wbr_create_mfs(dentry, isdir);
27406 +       if (err >= 0) {
27407 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
27408 +               mutex_lock(&mfs->mfs_lock);
27409 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
27410 +                       err = au_wbr_create_rr(dentry, isdir);
27411 +               mutex_unlock(&mfs->mfs_lock);
27412 +       }
27413 +
27414 +       AuDbg("b%d\n", err);
27415 +       return err;
27416 +}
27417 +
27418 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
27419 +{
27420 +       int err;
27421 +
27422 +       au_wbr_create_init_mfs(sb); /* ignore */
27423 +       err = au_wbr_create_init_rr(sb);
27424 +
27425 +       return err;
27426 +}
27427 +
27428 +/* ---------------------------------------------------------------------- */
27429 +
27430 +/* top down parent and most free space */
27431 +static int au_wbr_create_pmfs(struct dentry *dentry, int isdir)
27432 +{
27433 +       int err, e2;
27434 +       unsigned long long b;
27435 +       aufs_bindex_t bindex, bstart, bend;
27436 +       struct super_block *sb;
27437 +       struct dentry *parent, *h_parent;
27438 +       struct au_branch *br;
27439 +
27440 +       err = au_wbr_create_tdp(dentry, isdir);
27441 +       if (unlikely(err < 0))
27442 +               goto out;
27443 +       parent = dget_parent(dentry);
27444 +       bstart = au_dbstart(parent);
27445 +       bend = au_dbtaildir(parent);
27446 +       if (bstart == bend)
27447 +               goto out_parent; /* success */
27448 +
27449 +       e2 = au_wbr_create_mfs(dentry, isdir);
27450 +       if (e2 < 0)
27451 +               goto out_parent; /* success */
27452 +
27453 +       /* when the available size is equal, select upper one */
27454 +       sb = dentry->d_sb;
27455 +       br = au_sbr(sb, err);
27456 +       b = br->br_wbr->wbr_bytes;
27457 +       AuDbg("b%d, %llu\n", err, b);
27458 +
27459 +       for (bindex = bstart; bindex <= bend; bindex++) {
27460 +               h_parent = au_h_dptr(parent, bindex);
27461 +               if (!h_parent || !h_parent->d_inode)
27462 +                       continue;
27463 +
27464 +               br = au_sbr(sb, bindex);
27465 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
27466 +                       b = br->br_wbr->wbr_bytes;
27467 +                       err = bindex;
27468 +                       AuDbg("b%d, %llu\n", err, b);
27469 +               }
27470 +       }
27471 +
27472 +       if (err >= 0)
27473 +               err = au_wbr_nonopq(dentry, err);
27474 +
27475 +out_parent:
27476 +       dput(parent);
27477 +out:
27478 +       AuDbg("b%d\n", err);
27479 +       return err;
27480 +}
27481 +
27482 +/* ---------------------------------------------------------------------- */
27483 +
27484 +/* policies for copyup */
27485 +
27486 +/* top down parent */
27487 +static int au_wbr_copyup_tdp(struct dentry *dentry)
27488 +{
27489 +       return au_wbr_create_tdp(dentry, /*isdir, anything is ok*/0);
27490 +}
27491 +
27492 +/* bottom up parent */
27493 +static int au_wbr_copyup_bup(struct dentry *dentry)
27494 +{
27495 +       int err;
27496 +       aufs_bindex_t bindex, bstart;
27497 +       struct dentry *parent, *h_parent;
27498 +       struct super_block *sb;
27499 +
27500 +       err = -EROFS;
27501 +       sb = dentry->d_sb;
27502 +       parent = dget_parent(dentry);
27503 +       bstart = au_dbstart(parent);
27504 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
27505 +               h_parent = au_h_dptr(parent, bindex);
27506 +               if (!h_parent || !h_parent->d_inode)
27507 +                       continue;
27508 +
27509 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
27510 +                       err = bindex;
27511 +                       break;
27512 +               }
27513 +       }
27514 +       dput(parent);
27515 +
27516 +       /* bottom up here */
27517 +       if (unlikely(err < 0))
27518 +               err = au_wbr_bu(sb, bstart - 1);
27519 +
27520 +       AuDbg("b%d\n", err);
27521 +       return err;
27522 +}
27523 +
27524 +/* bottom up */
27525 +static int au_wbr_copyup_bu(struct dentry *dentry)
27526 +{
27527 +       int err;
27528 +       aufs_bindex_t bstart;
27529 +
27530 +       bstart = au_dbstart(dentry);
27531 +       err = au_wbr_bu(dentry->d_sb, bstart);
27532 +       AuDbg("b%d\n", err);
27533 +       if (err > bstart)
27534 +               err = au_wbr_nonopq(dentry, err);
27535 +
27536 +       AuDbg("b%d\n", err);
27537 +       return err;
27538 +}
27539 +
27540 +/* ---------------------------------------------------------------------- */
27541 +
27542 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
27543 +       [AuWbrCopyup_TDP] = {
27544 +               .copyup = au_wbr_copyup_tdp
27545 +       },
27546 +       [AuWbrCopyup_BUP] = {
27547 +               .copyup = au_wbr_copyup_bup
27548 +       },
27549 +       [AuWbrCopyup_BU] = {
27550 +               .copyup = au_wbr_copyup_bu
27551 +       }
27552 +};
27553 +
27554 +struct au_wbr_create_operations au_wbr_create_ops[] = {
27555 +       [AuWbrCreate_TDP] = {
27556 +               .create = au_wbr_create_tdp
27557 +       },
27558 +       [AuWbrCreate_RR] = {
27559 +               .create = au_wbr_create_rr,
27560 +               .init   = au_wbr_create_init_rr
27561 +       },
27562 +       [AuWbrCreate_MFS] = {
27563 +               .create = au_wbr_create_mfs,
27564 +               .init   = au_wbr_create_init_mfs,
27565 +               .fin    = au_wbr_create_fin_mfs
27566 +       },
27567 +       [AuWbrCreate_MFSV] = {
27568 +               .create = au_wbr_create_mfs,
27569 +               .init   = au_wbr_create_init_mfs,
27570 +               .fin    = au_wbr_create_fin_mfs
27571 +       },
27572 +       [AuWbrCreate_MFSRR] = {
27573 +               .create = au_wbr_create_mfsrr,
27574 +               .init   = au_wbr_create_init_mfsrr,
27575 +               .fin    = au_wbr_create_fin_mfs
27576 +       },
27577 +       [AuWbrCreate_MFSRRV] = {
27578 +               .create = au_wbr_create_mfsrr,
27579 +               .init   = au_wbr_create_init_mfsrr,
27580 +               .fin    = au_wbr_create_fin_mfs
27581 +       },
27582 +       [AuWbrCreate_PMFS] = {
27583 +               .create = au_wbr_create_pmfs,
27584 +               .init   = au_wbr_create_init_mfs,
27585 +               .fin    = au_wbr_create_fin_mfs
27586 +       },
27587 +       [AuWbrCreate_PMFSV] = {
27588 +               .create = au_wbr_create_pmfs,
27589 +               .init   = au_wbr_create_init_mfs,
27590 +               .fin    = au_wbr_create_fin_mfs
27591 +       }
27592 +};
27593 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
27594 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
27595 +++ linux/fs/aufs/whout.c       2011-10-24 20:51:51.583800333 +0200
27596 @@ -0,0 +1,1050 @@
27597 +/*
27598 + * Copyright (C) 2005-2011 Junjiro R. Okajima
27599 + *
27600 + * This program, aufs is free software; you can redistribute it and/or modify
27601 + * it under the terms of the GNU General Public License as published by
27602 + * the Free Software Foundation; either version 2 of the License, or
27603 + * (at your option) any later version.
27604 + *
27605 + * This program is distributed in the hope that it will be useful,
27606 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27607 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27608 + * GNU General Public License for more details.
27609 + *
27610 + * You should have received a copy of the GNU General Public License
27611 + * along with this program; if not, write to the Free Software
27612 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27613 + */
27614 +
27615 +/*
27616 + * whiteout for logical deletion and opaque directory
27617 + */
27618 +
27619 +#include <linux/fs.h>
27620 +#include "aufs.h"
27621 +
27622 +#define WH_MASK                        S_IRUGO
27623 +
27624 +/*
27625 + * If a directory contains this file, then it is opaque.  We start with the
27626 + * .wh. flag so that it is blocked by lookup.
27627 + */
27628 +static struct qstr diropq_name = {
27629 +       .name = AUFS_WH_DIROPQ,
27630 +       .len = sizeof(AUFS_WH_DIROPQ) - 1
27631 +};
27632 +
27633 +/*
27634 + * generate whiteout name, which is NOT terminated by NULL.
27635 + * @name: original d_name.name
27636 + * @len: original d_name.len
27637 + * @wh: whiteout qstr
27638 + * returns zero when succeeds, otherwise error.
27639 + * succeeded value as wh->name should be freed by kfree().
27640 + */
27641 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
27642 +{
27643 +       char *p;
27644 +
27645 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
27646 +               return -ENAMETOOLONG;
27647 +
27648 +       wh->len = name->len + AUFS_WH_PFX_LEN;
27649 +       p = kmalloc(wh->len, GFP_NOFS);
27650 +       wh->name = p;
27651 +       if (p) {
27652 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
27653 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
27654 +               /* smp_mb(); */
27655 +               return 0;
27656 +       }
27657 +       return -ENOMEM;
27658 +}
27659 +
27660 +/* ---------------------------------------------------------------------- */
27661 +
27662 +/*
27663 + * test if the @wh_name exists under @h_parent.
27664 + * @try_sio specifies the necessary of super-io.
27665 + */
27666 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
27667 +              struct au_branch *br, int try_sio)
27668 +{
27669 +       int err;
27670 +       struct dentry *wh_dentry;
27671 +
27672 +       if (!try_sio)
27673 +               wh_dentry = au_lkup_one(wh_name, h_parent, br, /*nd*/NULL);
27674 +       else
27675 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
27676 +       err = PTR_ERR(wh_dentry);
27677 +       if (IS_ERR(wh_dentry))
27678 +               goto out;
27679 +
27680 +       err = 0;
27681 +       if (!wh_dentry->d_inode)
27682 +               goto out_wh; /* success */
27683 +
27684 +       err = 1;
27685 +       if (S_ISREG(wh_dentry->d_inode->i_mode))
27686 +               goto out_wh; /* success */
27687 +
27688 +       err = -EIO;
27689 +       AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
27690 +               AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
27691 +
27692 +out_wh:
27693 +       dput(wh_dentry);
27694 +out:
27695 +       return err;
27696 +}
27697 +
27698 +/*
27699 + * test if the @h_dentry sets opaque or not.
27700 + */
27701 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
27702 +{
27703 +       int err;
27704 +       struct inode *h_dir;
27705 +
27706 +       h_dir = h_dentry->d_inode;
27707 +       err = au_wh_test(h_dentry, &diropq_name, br,
27708 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
27709 +       return err;
27710 +}
27711 +
27712 +/*
27713 + * returns a negative dentry whose name is unique and temporary.
27714 + */
27715 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
27716 +                            struct qstr *prefix)
27717 +{
27718 +       struct dentry *dentry;
27719 +       int i;
27720 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
27721 +               *name, *p;
27722 +       /* strict atomic_t is unnecessary here */
27723 +       static unsigned short cnt;
27724 +       struct qstr qs;
27725 +
27726 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
27727 +
27728 +       name = defname;
27729 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
27730 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
27731 +               dentry = ERR_PTR(-ENAMETOOLONG);
27732 +               if (unlikely(qs.len > NAME_MAX))
27733 +                       goto out;
27734 +               dentry = ERR_PTR(-ENOMEM);
27735 +               name = kmalloc(qs.len + 1, GFP_NOFS);
27736 +               if (unlikely(!name))
27737 +                       goto out;
27738 +       }
27739 +
27740 +       /* doubly whiteout-ed */
27741 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
27742 +       p = name + AUFS_WH_PFX_LEN * 2;
27743 +       memcpy(p, prefix->name, prefix->len);
27744 +       p += prefix->len;
27745 +       *p++ = '.';
27746 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
27747 +
27748 +       qs.name = name;
27749 +       for (i = 0; i < 3; i++) {
27750 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
27751 +               dentry = au_sio_lkup_one(&qs, h_parent, br);
27752 +               if (IS_ERR(dentry) || !dentry->d_inode)
27753 +                       goto out_name;
27754 +               dput(dentry);
27755 +       }
27756 +       /* pr_warning("could not get random name\n"); */
27757 +       dentry = ERR_PTR(-EEXIST);
27758 +       AuDbg("%.*s\n", AuLNPair(&qs));
27759 +       BUG();
27760 +
27761 +out_name:
27762 +       if (name != defname)
27763 +               kfree(name);
27764 +out:
27765 +       AuTraceErrPtr(dentry);
27766 +       return dentry;
27767 +}
27768 +
27769 +/*
27770 + * rename the @h_dentry on @br to the whiteouted temporary name.
27771 + */
27772 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
27773 +{
27774 +       int err;
27775 +       struct path h_path = {
27776 +               .mnt = br->br_mnt
27777 +       };
27778 +       struct inode *h_dir;
27779 +       struct dentry *h_parent;
27780 +
27781 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
27782 +       h_dir = h_parent->d_inode;
27783 +       IMustLock(h_dir);
27784 +
27785 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
27786 +       err = PTR_ERR(h_path.dentry);
27787 +       if (IS_ERR(h_path.dentry))
27788 +               goto out;
27789 +
27790 +       /* under the same dir, no need to lock_rename() */
27791 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
27792 +       AuTraceErr(err);
27793 +       dput(h_path.dentry);
27794 +
27795 +out:
27796 +       AuTraceErr(err);
27797 +       return err;
27798 +}
27799 +
27800 +/* ---------------------------------------------------------------------- */
27801 +/*
27802 + * functions for removing a whiteout
27803 + */
27804 +
27805 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
27806 +{
27807 +       int force;
27808 +
27809 +       /*
27810 +        * forces superio when the dir has a sticky bit.
27811 +        * this may be a violation of unix fs semantics.
27812 +        */
27813 +       force = (h_dir->i_mode & S_ISVTX)
27814 +               && h_path->dentry->d_inode->i_uid != current_fsuid();
27815 +       return vfsub_unlink(h_dir, h_path, force);
27816 +}
27817 +
27818 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
27819 +                       struct dentry *dentry)
27820 +{
27821 +       int err;
27822 +
27823 +       err = do_unlink_wh(h_dir, h_path);
27824 +       if (!err && dentry)
27825 +               au_set_dbwh(dentry, -1);
27826 +
27827 +       return err;
27828 +}
27829 +
27830 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
27831 +                         struct au_branch *br)
27832 +{
27833 +       int err;
27834 +       struct path h_path = {
27835 +               .mnt = br->br_mnt
27836 +       };
27837 +
27838 +       err = 0;
27839 +       h_path.dentry = au_lkup_one(wh, h_parent, br, /*nd*/NULL);
27840 +       if (IS_ERR(h_path.dentry))
27841 +               err = PTR_ERR(h_path.dentry);
27842 +       else {
27843 +               if (h_path.dentry->d_inode
27844 +                   && S_ISREG(h_path.dentry->d_inode->i_mode))
27845 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
27846 +               dput(h_path.dentry);
27847 +       }
27848 +
27849 +       return err;
27850 +}
27851 +
27852 +/* ---------------------------------------------------------------------- */
27853 +/*
27854 + * initialize/clean whiteout for a branch
27855 + */
27856 +
27857 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
27858 +                       const int isdir)
27859 +{
27860 +       int err;
27861 +
27862 +       if (!whpath->dentry->d_inode)
27863 +               return;
27864 +
27865 +       err = mnt_want_write(whpath->mnt);
27866 +       if (!err) {
27867 +               if (isdir)
27868 +                       err = vfsub_rmdir(h_dir, whpath);
27869 +               else
27870 +                       err = vfsub_unlink(h_dir, whpath, /*force*/0);
27871 +               mnt_drop_write(whpath->mnt);
27872 +       }
27873 +       if (unlikely(err))
27874 +               pr_warning("failed removing %.*s (%d), ignored.\n",
27875 +                          AuDLNPair(whpath->dentry), err);
27876 +}
27877 +
27878 +static int test_linkable(struct dentry *h_root)
27879 +{
27880 +       struct inode *h_dir = h_root->d_inode;
27881 +
27882 +       if (h_dir->i_op->link)
27883 +               return 0;
27884 +
27885 +       pr_err("%.*s (%s) doesn't support link(2), use noplink and rw+nolwh\n",
27886 +              AuDLNPair(h_root), au_sbtype(h_root->d_sb));
27887 +       return -ENOSYS;
27888 +}
27889 +
27890 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
27891 +static int au_whdir(struct inode *h_dir, struct path *path)
27892 +{
27893 +       int err;
27894 +
27895 +       err = -EEXIST;
27896 +       if (!path->dentry->d_inode) {
27897 +               int mode = S_IRWXU;
27898 +
27899 +               if (au_test_nfs(path->dentry->d_sb))
27900 +                       mode |= S_IXUGO;
27901 +               err = mnt_want_write(path->mnt);
27902 +               if (!err) {
27903 +                       err = vfsub_mkdir(h_dir, path, mode);
27904 +                       mnt_drop_write(path->mnt);
27905 +               }
27906 +       } else if (S_ISDIR(path->dentry->d_inode->i_mode))
27907 +               err = 0;
27908 +       else
27909 +               pr_err("unknown %.*s exists\n", AuDLNPair(path->dentry));
27910 +
27911 +       return err;
27912 +}
27913 +
27914 +struct au_wh_base {
27915 +       const struct qstr *name;
27916 +       struct dentry *dentry;
27917 +};
27918 +
27919 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
27920 +                         struct path *h_path)
27921 +{
27922 +       h_path->dentry = base[AuBrWh_BASE].dentry;
27923 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
27924 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
27925 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
27926 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
27927 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
27928 +}
27929 +
27930 +/*
27931 + * returns tri-state,
27932 + * minus: error, caller should print the mesage
27933 + * zero: succuess
27934 + * plus: error, caller should NOT print the mesage
27935 + */
27936 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
27937 +                               int do_plink, struct au_wh_base base[],
27938 +                               struct path *h_path)
27939 +{
27940 +       int err;
27941 +       struct inode *h_dir;
27942 +
27943 +       h_dir = h_root->d_inode;
27944 +       h_path->dentry = base[AuBrWh_BASE].dentry;
27945 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
27946 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
27947 +       if (do_plink) {
27948 +               err = test_linkable(h_root);
27949 +               if (unlikely(err)) {
27950 +                       err = 1;
27951 +                       goto out;
27952 +               }
27953 +
27954 +               err = au_whdir(h_dir, h_path);
27955 +               if (unlikely(err))
27956 +                       goto out;
27957 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
27958 +       } else
27959 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
27960 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
27961 +       err = au_whdir(h_dir, h_path);
27962 +       if (unlikely(err))
27963 +               goto out;
27964 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
27965 +
27966 +out:
27967 +       return err;
27968 +}
27969 +
27970 +/*
27971 + * for the moment, aufs supports the branch filesystem which does not support
27972 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
27973 + * copyup failed. finally, such filesystem will not be used as the writable
27974 + * branch.
27975 + *
27976 + * returns tri-state, see above.
27977 + */
27978 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
27979 +                        int do_plink, struct au_wh_base base[],
27980 +                        struct path *h_path)
27981 +{
27982 +       int err;
27983 +       struct inode *h_dir;
27984 +
27985 +       WbrWhMustWriteLock(wbr);
27986 +
27987 +       err = test_linkable(h_root);
27988 +       if (unlikely(err)) {
27989 +               err = 1;
27990 +               goto out;
27991 +       }
27992 +
27993 +       /*
27994 +        * todo: should this create be done in /sbin/mount.aufs helper?
27995 +        */
27996 +       err = -EEXIST;
27997 +       h_dir = h_root->d_inode;
27998 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
27999 +               err = mnt_want_write(h_path->mnt);
28000 +               if (!err) {
28001 +                       h_path->dentry = base[AuBrWh_BASE].dentry;
28002 +                       err = vfsub_create(h_dir, h_path, WH_MASK);
28003 +                       mnt_drop_write(h_path->mnt);
28004 +               }
28005 +       } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
28006 +               err = 0;
28007 +       else
28008 +               pr_err("unknown %.*s/%.*s exists\n",
28009 +                      AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
28010 +       if (unlikely(err))
28011 +               goto out;
28012 +
28013 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
28014 +       if (do_plink) {
28015 +               err = au_whdir(h_dir, h_path);
28016 +               if (unlikely(err))
28017 +                       goto out;
28018 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
28019 +       } else
28020 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
28021 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
28022 +
28023 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
28024 +       err = au_whdir(h_dir, h_path);
28025 +       if (unlikely(err))
28026 +               goto out;
28027 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
28028 +
28029 +out:
28030 +       return err;
28031 +}
28032 +
28033 +/*
28034 + * initialize the whiteout base file/dir for @br.
28035 + */
28036 +int au_wh_init(struct dentry *h_root, struct au_branch *br,
28037 +              struct super_block *sb)
28038 +{
28039 +       int err, i;
28040 +       const unsigned char do_plink
28041 +               = !!au_opt_test(au_mntflags(sb), PLINK);
28042 +       struct path path = {
28043 +               .mnt = br->br_mnt
28044 +       };
28045 +       struct inode *h_dir;
28046 +       struct au_wbr *wbr = br->br_wbr;
28047 +       static const struct qstr base_name[] = {
28048 +               [AuBrWh_BASE] = {
28049 +                       .name   = AUFS_BASE_NAME,
28050 +                       .len    = sizeof(AUFS_BASE_NAME) - 1
28051 +               },
28052 +               [AuBrWh_PLINK] = {
28053 +                       .name   = AUFS_PLINKDIR_NAME,
28054 +                       .len    = sizeof(AUFS_PLINKDIR_NAME) - 1
28055 +               },
28056 +               [AuBrWh_ORPH] = {
28057 +                       .name   = AUFS_ORPHDIR_NAME,
28058 +                       .len    = sizeof(AUFS_ORPHDIR_NAME) - 1
28059 +               }
28060 +       };
28061 +       struct au_wh_base base[] = {
28062 +               [AuBrWh_BASE] = {
28063 +                       .name   = base_name + AuBrWh_BASE,
28064 +                       .dentry = NULL
28065 +               },
28066 +               [AuBrWh_PLINK] = {
28067 +                       .name   = base_name + AuBrWh_PLINK,
28068 +                       .dentry = NULL
28069 +               },
28070 +               [AuBrWh_ORPH] = {
28071 +                       .name   = base_name + AuBrWh_ORPH,
28072 +                       .dentry = NULL
28073 +               }
28074 +       };
28075 +
28076 +       if (wbr)
28077 +               WbrWhMustWriteLock(wbr);
28078 +
28079 +       for (i = 0; i < AuBrWh_Last; i++) {
28080 +               /* doubly whiteouted */
28081 +               struct dentry *d;
28082 +
28083 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
28084 +               err = PTR_ERR(d);
28085 +               if (IS_ERR(d))
28086 +                       goto out;
28087 +
28088 +               base[i].dentry = d;
28089 +               AuDebugOn(wbr
28090 +                         && wbr->wbr_wh[i]
28091 +                         && wbr->wbr_wh[i] != base[i].dentry);
28092 +       }
28093 +
28094 +       if (wbr)
28095 +               for (i = 0; i < AuBrWh_Last; i++) {
28096 +                       dput(wbr->wbr_wh[i]);
28097 +                       wbr->wbr_wh[i] = NULL;
28098 +               }
28099 +
28100 +       err = 0;
28101 +       if (!au_br_writable(br->br_perm)) {
28102 +               h_dir = h_root->d_inode;
28103 +               au_wh_init_ro(h_dir, base, &path);
28104 +       } else if (!au_br_wh_linkable(br->br_perm)) {
28105 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
28106 +               if (err > 0)
28107 +                       goto out;
28108 +               else if (err)
28109 +                       goto out_err;
28110 +       } else {
28111 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
28112 +               if (err > 0)
28113 +                       goto out;
28114 +               else if (err)
28115 +                       goto out_err;
28116 +       }
28117 +       goto out; /* success */
28118 +
28119 +out_err:
28120 +       pr_err("an error(%d) on the writable branch %.*s(%s)\n",
28121 +              err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
28122 +out:
28123 +       for (i = 0; i < AuBrWh_Last; i++)
28124 +               dput(base[i].dentry);
28125 +       return err;
28126 +}
28127 +
28128 +/* ---------------------------------------------------------------------- */
28129 +/*
28130 + * whiteouts are all hard-linked usually.
28131 + * when its link count reaches a ceiling, we create a new whiteout base
28132 + * asynchronously.
28133 + */
28134 +
28135 +struct reinit_br_wh {
28136 +       struct super_block *sb;
28137 +       struct au_branch *br;
28138 +};
28139 +
28140 +static void reinit_br_wh(void *arg)
28141 +{
28142 +       int err;
28143 +       aufs_bindex_t bindex;
28144 +       struct path h_path;
28145 +       struct reinit_br_wh *a = arg;
28146 +       struct au_wbr *wbr;
28147 +       struct inode *dir;
28148 +       struct dentry *h_root;
28149 +       struct au_hinode *hdir;
28150 +
28151 +       err = 0;
28152 +       wbr = a->br->br_wbr;
28153 +       /* big aufs lock */
28154 +       si_noflush_write_lock(a->sb);
28155 +       if (!au_br_writable(a->br->br_perm))
28156 +               goto out;
28157 +       bindex = au_br_index(a->sb, a->br->br_id);
28158 +       if (unlikely(bindex < 0))
28159 +               goto out;
28160 +
28161 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
28162 +       dir = a->sb->s_root->d_inode;
28163 +       hdir = au_hi(dir, bindex);
28164 +       h_root = au_h_dptr(a->sb->s_root, bindex);
28165 +
28166 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
28167 +       wbr_wh_write_lock(wbr);
28168 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
28169 +                         h_root, a->br);
28170 +       if (!err) {
28171 +               err = mnt_want_write(a->br->br_mnt);
28172 +               if (!err) {
28173 +                       h_path.dentry = wbr->wbr_whbase;
28174 +                       h_path.mnt = a->br->br_mnt;
28175 +                       err = vfsub_unlink(hdir->hi_inode, &h_path, /*force*/0);
28176 +                       mnt_drop_write(a->br->br_mnt);
28177 +               }
28178 +       } else {
28179 +               pr_warning("%.*s is moved, ignored\n",
28180 +                          AuDLNPair(wbr->wbr_whbase));
28181 +               err = 0;
28182 +       }
28183 +       dput(wbr->wbr_whbase);
28184 +       wbr->wbr_whbase = NULL;
28185 +       if (!err)
28186 +               err = au_wh_init(h_root, a->br, a->sb);
28187 +       wbr_wh_write_unlock(wbr);
28188 +       au_hn_imtx_unlock(hdir);
28189 +       di_read_unlock(a->sb->s_root, AuLock_IR);
28190 +
28191 +out:
28192 +       if (wbr)
28193 +               atomic_dec(&wbr->wbr_wh_running);
28194 +       atomic_dec(&a->br->br_count);
28195 +       si_write_unlock(a->sb);
28196 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
28197 +       kfree(arg);
28198 +       if (unlikely(err))
28199 +               AuIOErr("err %d\n", err);
28200 +}
28201 +
28202 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
28203 +{
28204 +       int do_dec, wkq_err;
28205 +       struct reinit_br_wh *arg;
28206 +
28207 +       do_dec = 1;
28208 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
28209 +               goto out;
28210 +
28211 +       /* ignore ENOMEM */
28212 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
28213 +       if (arg) {
28214 +               /*
28215 +                * dec(wh_running), kfree(arg) and dec(br_count)
28216 +                * in reinit function
28217 +                */
28218 +               arg->sb = sb;
28219 +               arg->br = br;
28220 +               atomic_inc(&br->br_count);
28221 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
28222 +               if (unlikely(wkq_err)) {
28223 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
28224 +                       atomic_dec(&br->br_count);
28225 +                       kfree(arg);
28226 +               }
28227 +               do_dec = 0;
28228 +       }
28229 +
28230 +out:
28231 +       if (do_dec)
28232 +               atomic_dec(&br->br_wbr->wbr_wh_running);
28233 +}
28234 +
28235 +/* ---------------------------------------------------------------------- */
28236 +
28237 +/*
28238 + * create the whiteout @wh.
28239 + */
28240 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
28241 +                            struct dentry *wh)
28242 +{
28243 +       int err;
28244 +       struct path h_path = {
28245 +               .dentry = wh
28246 +       };
28247 +       struct au_branch *br;
28248 +       struct au_wbr *wbr;
28249 +       struct dentry *h_parent;
28250 +       struct inode *h_dir;
28251 +
28252 +       h_parent = wh->d_parent; /* dir inode is locked */
28253 +       h_dir = h_parent->d_inode;
28254 +       IMustLock(h_dir);
28255 +
28256 +       br = au_sbr(sb, bindex);
28257 +       h_path.mnt = br->br_mnt;
28258 +       wbr = br->br_wbr;
28259 +       wbr_wh_read_lock(wbr);
28260 +       if (wbr->wbr_whbase) {
28261 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path);
28262 +               if (!err || err != -EMLINK)
28263 +                       goto out;
28264 +
28265 +               /* link count full. re-initialize br_whbase. */
28266 +               kick_reinit_br_wh(sb, br);
28267 +       }
28268 +
28269 +       /* return this error in this context */
28270 +       err = vfsub_create(h_dir, &h_path, WH_MASK);
28271 +
28272 +out:
28273 +       wbr_wh_read_unlock(wbr);
28274 +       return err;
28275 +}
28276 +
28277 +/* ---------------------------------------------------------------------- */
28278 +
28279 +/*
28280 + * create or remove the diropq.
28281 + */
28282 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
28283 +                               unsigned int flags)
28284 +{
28285 +       struct dentry *opq_dentry, *h_dentry;
28286 +       struct super_block *sb;
28287 +       struct au_branch *br;
28288 +       int err;
28289 +
28290 +       sb = dentry->d_sb;
28291 +       br = au_sbr(sb, bindex);
28292 +       h_dentry = au_h_dptr(dentry, bindex);
28293 +       opq_dentry = au_lkup_one(&diropq_name, h_dentry, br, /*nd*/NULL);
28294 +       if (IS_ERR(opq_dentry))
28295 +               goto out;
28296 +
28297 +       if (au_ftest_diropq(flags, CREATE)) {
28298 +               err = link_or_create_wh(sb, bindex, opq_dentry);
28299 +               if (!err) {
28300 +                       au_set_dbdiropq(dentry, bindex);
28301 +                       goto out; /* success */
28302 +               }
28303 +       } else {
28304 +               struct path tmp = {
28305 +                       .dentry = opq_dentry,
28306 +                       .mnt    = br->br_mnt
28307 +               };
28308 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
28309 +               if (!err)
28310 +                       au_set_dbdiropq(dentry, -1);
28311 +       }
28312 +       dput(opq_dentry);
28313 +       opq_dentry = ERR_PTR(err);
28314 +
28315 +out:
28316 +       return opq_dentry;
28317 +}
28318 +
28319 +struct do_diropq_args {
28320 +       struct dentry **errp;
28321 +       struct dentry *dentry;
28322 +       aufs_bindex_t bindex;
28323 +       unsigned int flags;
28324 +};
28325 +
28326 +static void call_do_diropq(void *args)
28327 +{
28328 +       struct do_diropq_args *a = args;
28329 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
28330 +}
28331 +
28332 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
28333 +                            unsigned int flags)
28334 +{
28335 +       struct dentry *diropq, *h_dentry;
28336 +
28337 +       h_dentry = au_h_dptr(dentry, bindex);
28338 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
28339 +               diropq = do_diropq(dentry, bindex, flags);
28340 +       else {
28341 +               int wkq_err;
28342 +               struct do_diropq_args args = {
28343 +                       .errp           = &diropq,
28344 +                       .dentry         = dentry,
28345 +                       .bindex         = bindex,
28346 +                       .flags          = flags
28347 +               };
28348 +
28349 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
28350 +               if (unlikely(wkq_err))
28351 +                       diropq = ERR_PTR(wkq_err);
28352 +       }
28353 +
28354 +       return diropq;
28355 +}
28356 +
28357 +/* ---------------------------------------------------------------------- */
28358 +
28359 +/*
28360 + * lookup whiteout dentry.
28361 + * @h_parent: lower parent dentry which must exist and be locked
28362 + * @base_name: name of dentry which will be whiteouted
28363 + * returns dentry for whiteout.
28364 + */
28365 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
28366 +                         struct au_branch *br)
28367 +{
28368 +       int err;
28369 +       struct qstr wh_name;
28370 +       struct dentry *wh_dentry;
28371 +
28372 +       err = au_wh_name_alloc(&wh_name, base_name);
28373 +       wh_dentry = ERR_PTR(err);
28374 +       if (!err) {
28375 +               wh_dentry = au_lkup_one(&wh_name, h_parent, br, /*nd*/NULL);
28376 +               kfree(wh_name.name);
28377 +       }
28378 +       return wh_dentry;
28379 +}
28380 +
28381 +/*
28382 + * link/create a whiteout for @dentry on @bindex.
28383 + */
28384 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
28385 +                           struct dentry *h_parent)
28386 +{
28387 +       struct dentry *wh_dentry;
28388 +       struct super_block *sb;
28389 +       int err;
28390 +
28391 +       sb = dentry->d_sb;
28392 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
28393 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
28394 +               err = link_or_create_wh(sb, bindex, wh_dentry);
28395 +               if (!err)
28396 +                       au_set_dbwh(dentry, bindex);
28397 +               else {
28398 +                       dput(wh_dentry);
28399 +                       wh_dentry = ERR_PTR(err);
28400 +               }
28401 +       }
28402 +
28403 +       return wh_dentry;
28404 +}
28405 +
28406 +/* ---------------------------------------------------------------------- */
28407 +
28408 +/* Delete all whiteouts in this directory on branch bindex. */
28409 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
28410 +                          aufs_bindex_t bindex, struct au_branch *br)
28411 +{
28412 +       int err;
28413 +       unsigned long ul, n;
28414 +       struct qstr wh_name;
28415 +       char *p;
28416 +       struct hlist_head *head;
28417 +       struct au_vdir_wh *tpos;
28418 +       struct hlist_node *pos;
28419 +       struct au_vdir_destr *str;
28420 +
28421 +       err = -ENOMEM;
28422 +       p = __getname_gfp(GFP_NOFS);
28423 +       wh_name.name = p;
28424 +       if (unlikely(!wh_name.name))
28425 +               goto out;
28426 +
28427 +       err = 0;
28428 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
28429 +       p += AUFS_WH_PFX_LEN;
28430 +       n = whlist->nh_num;
28431 +       head = whlist->nh_head;
28432 +       for (ul = 0; !err && ul < n; ul++, head++) {
28433 +               hlist_for_each_entry(tpos, pos, head, wh_hash) {
28434 +                       if (tpos->wh_bindex != bindex)
28435 +                               continue;
28436 +
28437 +                       str = &tpos->wh_str;
28438 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
28439 +                               memcpy(p, str->name, str->len);
28440 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
28441 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
28442 +                               if (!err)
28443 +                                       continue;
28444 +                               break;
28445 +                       }
28446 +                       AuIOErr("whiteout name too long %.*s\n",
28447 +                               str->len, str->name);
28448 +                       err = -EIO;
28449 +                       break;
28450 +               }
28451 +       }
28452 +       __putname(wh_name.name);
28453 +
28454 +out:
28455 +       return err;
28456 +}
28457 +
28458 +struct del_wh_children_args {
28459 +       int *errp;
28460 +       struct dentry *h_dentry;
28461 +       struct au_nhash *whlist;
28462 +       aufs_bindex_t bindex;
28463 +       struct au_branch *br;
28464 +};
28465 +
28466 +static void call_del_wh_children(void *args)
28467 +{
28468 +       struct del_wh_children_args *a = args;
28469 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
28470 +}
28471 +
28472 +/* ---------------------------------------------------------------------- */
28473 +
28474 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
28475 +{
28476 +       struct au_whtmp_rmdir *whtmp;
28477 +       int err;
28478 +       unsigned int rdhash;
28479 +
28480 +       SiMustAnyLock(sb);
28481 +
28482 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
28483 +       if (unlikely(!whtmp)) {
28484 +               whtmp = ERR_PTR(-ENOMEM);
28485 +               goto out;
28486 +       }
28487 +
28488 +       whtmp->dir = NULL;
28489 +       whtmp->br = NULL;
28490 +       whtmp->wh_dentry = NULL;
28491 +       /* no estimation for dir size */
28492 +       rdhash = au_sbi(sb)->si_rdhash;
28493 +       if (!rdhash)
28494 +               rdhash = AUFS_RDHASH_DEF;
28495 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
28496 +       if (unlikely(err)) {
28497 +               kfree(whtmp);
28498 +               whtmp = ERR_PTR(err);
28499 +       }
28500 +
28501 +out:
28502 +       return whtmp;
28503 +}
28504 +
28505 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
28506 +{
28507 +       if (whtmp->br)
28508 +               atomic_dec(&whtmp->br->br_count);
28509 +       dput(whtmp->wh_dentry);
28510 +       iput(whtmp->dir);
28511 +       au_nhash_wh_free(&whtmp->whlist);
28512 +       kfree(whtmp);
28513 +}
28514 +
28515 +/*
28516 + * rmdir the whiteouted temporary named dir @h_dentry.
28517 + * @whlist: whiteouted children.
28518 + */
28519 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
28520 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
28521 +{
28522 +       int err;
28523 +       struct path h_tmp;
28524 +       struct inode *wh_inode, *h_dir;
28525 +       struct au_branch *br;
28526 +
28527 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
28528 +       IMustLock(h_dir);
28529 +
28530 +       br = au_sbr(dir->i_sb, bindex);
28531 +       wh_inode = wh_dentry->d_inode;
28532 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
28533 +
28534 +       /*
28535 +        * someone else might change some whiteouts while we were sleeping.
28536 +        * it means this whlist may have an obsoleted entry.
28537 +        */
28538 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
28539 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
28540 +       else {
28541 +               int wkq_err;
28542 +               struct del_wh_children_args args = {
28543 +                       .errp           = &err,
28544 +                       .h_dentry       = wh_dentry,
28545 +                       .whlist         = whlist,
28546 +                       .bindex         = bindex,
28547 +                       .br             = br
28548 +               };
28549 +
28550 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
28551 +               if (unlikely(wkq_err))
28552 +                       err = wkq_err;
28553 +       }
28554 +       mutex_unlock(&wh_inode->i_mutex);
28555 +
28556 +       if (!err) {
28557 +               h_tmp.dentry = wh_dentry;
28558 +               h_tmp.mnt = br->br_mnt;
28559 +               err = vfsub_rmdir(h_dir, &h_tmp);
28560 +       }
28561 +
28562 +       if (!err) {
28563 +               if (au_ibstart(dir) == bindex) {
28564 +                       /* todo: dir->i_mutex is necessary */
28565 +                       au_cpup_attr_timesizes(dir);
28566 +                       vfsub_drop_nlink(dir);
28567 +               }
28568 +               return 0; /* success */
28569 +       }
28570 +
28571 +       pr_warning("failed removing %.*s(%d), ignored\n",
28572 +                  AuDLNPair(wh_dentry), err);
28573 +       return err;
28574 +}
28575 +
28576 +static void call_rmdir_whtmp(void *args)
28577 +{
28578 +       int err;
28579 +       aufs_bindex_t bindex;
28580 +       struct au_whtmp_rmdir *a = args;
28581 +       struct super_block *sb;
28582 +       struct dentry *h_parent;
28583 +       struct inode *h_dir;
28584 +       struct au_hinode *hdir;
28585 +
28586 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
28587 +       /* mutex_lock(&a->dir->i_mutex); */
28588 +       err = -EROFS;
28589 +       sb = a->dir->i_sb;
28590 +       si_read_lock(sb, !AuLock_FLUSH);
28591 +       if (!au_br_writable(a->br->br_perm))
28592 +               goto out;
28593 +       bindex = au_br_index(sb, a->br->br_id);
28594 +       if (unlikely(bindex < 0))
28595 +               goto out;
28596 +
28597 +       err = -EIO;
28598 +       ii_write_lock_parent(a->dir);
28599 +       h_parent = dget_parent(a->wh_dentry);
28600 +       h_dir = h_parent->d_inode;
28601 +       hdir = au_hi(a->dir, bindex);
28602 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
28603 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
28604 +                         a->br);
28605 +       if (!err) {
28606 +               err = mnt_want_write(a->br->br_mnt);
28607 +               if (!err) {
28608 +                       err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry,
28609 +                                            &a->whlist);
28610 +                       mnt_drop_write(a->br->br_mnt);
28611 +               }
28612 +       }
28613 +       au_hn_imtx_unlock(hdir);
28614 +       dput(h_parent);
28615 +       ii_write_unlock(a->dir);
28616 +
28617 +out:
28618 +       /* mutex_unlock(&a->dir->i_mutex); */
28619 +       au_whtmp_rmdir_free(a);
28620 +       si_read_unlock(sb);
28621 +       au_nwt_done(&au_sbi(sb)->si_nowait);
28622 +       if (unlikely(err))
28623 +               AuIOErr("err %d\n", err);
28624 +}
28625 +
28626 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
28627 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
28628 +{
28629 +       int wkq_err;
28630 +       struct super_block *sb;
28631 +
28632 +       IMustLock(dir);
28633 +
28634 +       /* all post-process will be done in do_rmdir_whtmp(). */
28635 +       sb = dir->i_sb;
28636 +       args->dir = au_igrab(dir);
28637 +       args->br = au_sbr(sb, bindex);
28638 +       atomic_inc(&args->br->br_count);
28639 +       args->wh_dentry = dget(wh_dentry);
28640 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
28641 +       if (unlikely(wkq_err)) {
28642 +               pr_warning("rmdir error %.*s (%d), ignored\n",
28643 +                          AuDLNPair(wh_dentry), wkq_err);
28644 +               au_whtmp_rmdir_free(args);
28645 +       }
28646 +}
28647 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
28648 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
28649 +++ linux/fs/aufs/whout.h       2011-08-24 13:30:24.734646739 +0200
28650 @@ -0,0 +1,89 @@
28651 +/*
28652 + * Copyright (C) 2005-2011 Junjiro R. Okajima
28653 + *
28654 + * This program, aufs is free software; you can redistribute it and/or modify
28655 + * it under the terms of the GNU General Public License as published by
28656 + * the Free Software Foundation; either version 2 of the License, or
28657 + * (at your option) any later version.
28658 + *
28659 + * This program is distributed in the hope that it will be useful,
28660 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28661 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28662 + * GNU General Public License for more details.
28663 + *
28664 + * You should have received a copy of the GNU General Public License
28665 + * along with this program; if not, write to the Free Software
28666 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28667 + */
28668 +
28669 +/*
28670 + * whiteout for logical deletion and opaque directory
28671 + */
28672 +
28673 +#ifndef __AUFS_WHOUT_H__
28674 +#define __AUFS_WHOUT_H__
28675 +
28676 +#ifdef __KERNEL__
28677 +
28678 +#include <linux/aufs_type.h>
28679 +#include "dir.h"
28680 +
28681 +/* whout.c */
28682 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
28683 +struct au_branch;
28684 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
28685 +              struct au_branch *br, int try_sio);
28686 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br);
28687 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
28688 +                            struct qstr *prefix);
28689 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
28690 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
28691 +                       struct dentry *dentry);
28692 +int au_wh_init(struct dentry *h_parent, struct au_branch *br,
28693 +              struct super_block *sb);
28694 +
28695 +/* diropq flags */
28696 +#define AuDiropq_CREATE        1
28697 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
28698 +#define au_fset_diropq(flags, name) \
28699 +       do { (flags) |= AuDiropq_##name; } while (0)
28700 +#define au_fclr_diropq(flags, name) \
28701 +       do { (flags) &= ~AuDiropq_##name; } while (0)
28702 +
28703 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
28704 +                            unsigned int flags);
28705 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
28706 +                         struct au_branch *br);
28707 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
28708 +                           struct dentry *h_parent);
28709 +
28710 +/* real rmdir for the whiteout-ed dir */
28711 +struct au_whtmp_rmdir {
28712 +       struct inode *dir;
28713 +       struct au_branch *br;
28714 +       struct dentry *wh_dentry;
28715 +       struct au_nhash whlist;
28716 +};
28717 +
28718 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
28719 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
28720 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
28721 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
28722 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
28723 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
28724 +
28725 +/* ---------------------------------------------------------------------- */
28726 +
28727 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
28728 +                                             aufs_bindex_t bindex)
28729 +{
28730 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
28731 +}
28732 +
28733 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
28734 +{
28735 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
28736 +}
28737 +
28738 +#endif /* __KERNEL__ */
28739 +#endif /* __AUFS_WHOUT_H__ */
28740 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
28741 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
28742 +++ linux/fs/aufs/wkq.c 2012-01-05 11:00:59.218835185 +0100
28743 @@ -0,0 +1,214 @@
28744 +/*
28745 + * Copyright (C) 2005-2011 Junjiro R. Okajima
28746 + *
28747 + * This program, aufs is free software; you can redistribute it and/or modify
28748 + * it under the terms of the GNU General Public License as published by
28749 + * the Free Software Foundation; either version 2 of the License, or
28750 + * (at your option) any later version.
28751 + *
28752 + * This program is distributed in the hope that it will be useful,
28753 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28754 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28755 + * GNU General Public License for more details.
28756 + *
28757 + * You should have received a copy of the GNU General Public License
28758 + * along with this program; if not, write to the Free Software
28759 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28760 + */
28761 +
28762 +/*
28763 + * workqueue for asynchronous/super-io operations
28764 + * todo: try new dredential scheme
28765 + */
28766 +
28767 +#include <linux/module.h>
28768 +#include "aufs.h"
28769 +
28770 +/* internal workqueue named AUFS_WKQ_NAME */
28771 +
28772 +static struct workqueue_struct *au_wkq;
28773 +
28774 +struct au_wkinfo {
28775 +       struct work_struct wk;
28776 +       struct kobject *kobj;
28777 +
28778 +       unsigned int flags; /* see wkq.h */
28779 +
28780 +       au_wkq_func_t func;
28781 +       void *args;
28782 +
28783 +       struct completion *comp;
28784 +};
28785 +
28786 +/* ---------------------------------------------------------------------- */
28787 +
28788 +static void wkq_func(struct work_struct *wk)
28789 +{
28790 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
28791 +
28792 +       AuDebugOn(current_fsuid());
28793 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
28794 +
28795 +       wkinfo->func(wkinfo->args);
28796 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
28797 +               complete(wkinfo->comp);
28798 +       else {
28799 +               kobject_put(wkinfo->kobj);
28800 +               module_put(THIS_MODULE); /* todo: ?? */
28801 +               kfree(wkinfo);
28802 +       }
28803 +}
28804 +
28805 +/*
28806 + * Since struct completion is large, try allocating it dynamically.
28807 + */
28808 +#if defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS)
28809 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
28810 +
28811 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
28812 +{
28813 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
28814 +       if (*comp) {
28815 +               init_completion(*comp);
28816 +               wkinfo->comp = *comp;
28817 +               return 0;
28818 +       }
28819 +       return -ENOMEM;
28820 +}
28821 +
28822 +static void au_wkq_comp_free(struct completion *comp)
28823 +{
28824 +       kfree(comp);
28825 +}
28826 +
28827 +#else
28828 +
28829 +/* no braces */
28830 +#define AuWkqCompDeclare(name) \
28831 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
28832 +       struct completion *comp = &_ ## name
28833 +
28834 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
28835 +{
28836 +       wkinfo->comp = *comp;
28837 +       return 0;
28838 +}
28839 +
28840 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
28841 +{
28842 +       /* empty */
28843 +}
28844 +#endif /* 4KSTACKS */
28845 +
28846 +static void au_wkq_run(struct au_wkinfo *wkinfo)
28847 +{
28848 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
28849 +               if (au_wkq_test()) {
28850 +                       AuWarn1("wkq from wkq, due to a dead dir by UDBA?\n");
28851 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
28852 +               }
28853 +       } else
28854 +               au_dbg_verify_kthread();
28855 +
28856 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
28857 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
28858 +               queue_work(au_wkq, &wkinfo->wk);
28859 +       } else {
28860 +               INIT_WORK(&wkinfo->wk, wkq_func);
28861 +               schedule_work(&wkinfo->wk);
28862 +       }
28863 +}
28864 +
28865 +/*
28866 + * Be careful. It is easy to make deadlock happen.
28867 + * processA: lock, wkq and wait
28868 + * processB: wkq and wait, lock in wkq
28869 + * --> deadlock
28870 + */
28871 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
28872 +{
28873 +       int err;
28874 +       AuWkqCompDeclare(comp);
28875 +       struct au_wkinfo wkinfo = {
28876 +               .flags  = flags,
28877 +               .func   = func,
28878 +               .args   = args
28879 +       };
28880 +
28881 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
28882 +       if (!err) {
28883 +               au_wkq_run(&wkinfo);
28884 +               /* no timeout, no interrupt */
28885 +               wait_for_completion(wkinfo.comp);
28886 +               au_wkq_comp_free(comp);
28887 +               destroy_work_on_stack(&wkinfo.wk);
28888 +       }
28889 +
28890 +       return err;
28891 +
28892 +}
28893 +
28894 +/*
28895 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
28896 + * problem in a concurrent umounting.
28897 + */
28898 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
28899 +                 unsigned int flags)
28900 +{
28901 +       int err;
28902 +       struct au_wkinfo *wkinfo;
28903 +
28904 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
28905 +
28906 +       /*
28907 +        * wkq_func() must free this wkinfo.
28908 +        * it highly depends upon the implementation of workqueue.
28909 +        */
28910 +       err = 0;
28911 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
28912 +       if (wkinfo) {
28913 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
28914 +               wkinfo->flags = flags & ~AuWkq_WAIT;
28915 +               wkinfo->func = func;
28916 +               wkinfo->args = args;
28917 +               wkinfo->comp = NULL;
28918 +               kobject_get(wkinfo->kobj);
28919 +               __module_get(THIS_MODULE); /* todo: ?? */
28920 +
28921 +               au_wkq_run(wkinfo);
28922 +       } else {
28923 +               err = -ENOMEM;
28924 +               au_nwt_done(&au_sbi(sb)->si_nowait);
28925 +       }
28926 +
28927 +       return err;
28928 +}
28929 +
28930 +/* ---------------------------------------------------------------------- */
28931 +
28932 +void au_nwt_init(struct au_nowait_tasks *nwt)
28933 +{
28934 +       atomic_set(&nwt->nw_len, 0);
28935 +       /* smp_mb(); */ /* atomic_set */
28936 +       init_waitqueue_head(&nwt->nw_wq);
28937 +}
28938 +
28939 +void au_wkq_fin(void)
28940 +{
28941 +       destroy_workqueue(au_wkq);
28942 +}
28943 +
28944 +int __init au_wkq_init(void)
28945 +{
28946 +       int err;
28947 +
28948 +       err = 0;
28949 +       BUILD_BUG_ON(!WQ_RESCUER);
28950 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, !WQ_RESCUER, WQ_DFL_ACTIVE);
28951 +       if (IS_ERR(au_wkq))
28952 +               err = PTR_ERR(au_wkq);
28953 +       else if (!au_wkq)
28954 +               err = -ENOMEM;
28955 +
28956 +       return err;
28957 +}
28958 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
28959 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
28960 +++ linux/fs/aufs/wkq.h 2012-01-05 11:00:59.218835185 +0100
28961 @@ -0,0 +1,96 @@
28962 +/*
28963 + * Copyright (C) 2005-2011 Junjiro R. Okajima
28964 + *
28965 + * This program, aufs is free software; you can redistribute it and/or modify
28966 + * it under the terms of the GNU General Public License as published by
28967 + * the Free Software Foundation; either version 2 of the License, or
28968 + * (at your option) any later version.
28969 + *
28970 + * This program is distributed in the hope that it will be useful,
28971 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28972 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28973 + * GNU General Public License for more details.
28974 + *
28975 + * You should have received a copy of the GNU General Public License
28976 + * along with this program; if not, write to the Free Software
28977 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28978 + */
28979 +
28980 +/*
28981 + * workqueue for asynchronous/super-io operations
28982 + * todo: try new credentials management scheme
28983 + */
28984 +
28985 +#ifndef __AUFS_WKQ_H__
28986 +#define __AUFS_WKQ_H__
28987 +
28988 +#ifdef __KERNEL__
28989 +
28990 +#include <linux/sched.h>
28991 +#include <linux/wait.h>
28992 +#include <linux/aufs_type.h>
28993 +
28994 +struct super_block;
28995 +
28996 +/* ---------------------------------------------------------------------- */
28997 +
28998 +/*
28999 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
29000 + */
29001 +struct au_nowait_tasks {
29002 +       atomic_t                nw_len;
29003 +       wait_queue_head_t       nw_wq;
29004 +};
29005 +
29006 +/* ---------------------------------------------------------------------- */
29007 +
29008 +typedef void (*au_wkq_func_t)(void *args);
29009 +
29010 +/* wkq flags */
29011 +#define AuWkq_WAIT     1
29012 +#define AuWkq_NEST     (1 << 1)
29013 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
29014 +#define au_fset_wkq(flags, name) \
29015 +       do { (flags) |= AuWkq_##name; } while (0)
29016 +#define au_fclr_wkq(flags, name) \
29017 +       do { (flags) &= ~AuWkq_##name; } while (0)
29018 +
29019 +#ifndef CONFIG_AUFS_HNOTIFY
29020 +#undef AuWkq_NEST
29021 +#define AuWkq_NEST     0
29022 +#endif
29023 +
29024 +/* wkq.c */
29025 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
29026 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
29027 +                 unsigned int flags);
29028 +void au_nwt_init(struct au_nowait_tasks *nwt);
29029 +int __init au_wkq_init(void);
29030 +void au_wkq_fin(void);
29031 +
29032 +/* ---------------------------------------------------------------------- */
29033 +
29034 +static inline int au_wkq_test(void)
29035 +{
29036 +       return current->flags & PF_WQ_WORKER;
29037 +}
29038 +
29039 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
29040 +{
29041 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
29042 +}
29043 +
29044 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
29045 +{
29046 +       if (atomic_dec_and_test(&nwt->nw_len))
29047 +               wake_up_all(&nwt->nw_wq);
29048 +}
29049 +
29050 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
29051 +{
29052 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
29053 +       return 0;
29054 +}
29055 +
29056 +#endif /* __KERNEL__ */
29057 +#endif /* __AUFS_WKQ_H__ */
29058 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
29059 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
29060 +++ linux/fs/aufs/xino.c        2012-01-05 11:00:59.218835185 +0100
29061 @@ -0,0 +1,1266 @@
29062 +/*
29063 + * Copyright (C) 2005-2011 Junjiro R. Okajima
29064 + *
29065 + * This program, aufs is free software; you can redistribute it and/or modify
29066 + * it under the terms of the GNU General Public License as published by
29067 + * the Free Software Foundation; either version 2 of the License, or
29068 + * (at your option) any later version.
29069 + *
29070 + * This program is distributed in the hope that it will be useful,
29071 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29072 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29073 + * GNU General Public License for more details.
29074 + *
29075 + * You should have received a copy of the GNU General Public License
29076 + * along with this program; if not, write to the Free Software
29077 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29078 + */
29079 +
29080 +/*
29081 + * external inode number translation table and bitmap
29082 + */
29083 +
29084 +#include <linux/file.h>
29085 +#include <linux/seq_file.h>
29086 +#include <linux/uaccess.h>
29087 +#include "aufs.h"
29088 +
29089 +/* todo: unnecessary to support mmap_sem since kernel-space? */
29090 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
29091 +                  loff_t *pos)
29092 +{
29093 +       ssize_t err;
29094 +       mm_segment_t oldfs;
29095 +       union {
29096 +               void *k;
29097 +               char __user *u;
29098 +       } buf;
29099 +
29100 +       buf.k = kbuf;
29101 +       oldfs = get_fs();
29102 +       set_fs(KERNEL_DS);
29103 +       do {
29104 +               /* todo: signal_pending? */
29105 +               err = func(file, buf.u, size, pos);
29106 +       } while (err == -EAGAIN || err == -EINTR);
29107 +       set_fs(oldfs);
29108 +
29109 +#if 0 /* reserved for future use */
29110 +       if (err > 0)
29111 +               fsnotify_access(file->f_dentry);
29112 +#endif
29113 +
29114 +       return err;
29115 +}
29116 +
29117 +/* ---------------------------------------------------------------------- */
29118 +
29119 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
29120 +                             size_t size, loff_t *pos)
29121 +{
29122 +       ssize_t err;
29123 +       mm_segment_t oldfs;
29124 +       union {
29125 +               void *k;
29126 +               const char __user *u;
29127 +       } buf;
29128 +
29129 +       buf.k = kbuf;
29130 +       oldfs = get_fs();
29131 +       set_fs(KERNEL_DS);
29132 +       do {
29133 +               /* todo: signal_pending? */
29134 +               err = func(file, buf.u, size, pos);
29135 +       } while (err == -EAGAIN || err == -EINTR);
29136 +       set_fs(oldfs);
29137 +
29138 +#if 0 /* reserved for future use */
29139 +       if (err > 0)
29140 +               fsnotify_modify(file->f_dentry);
29141 +#endif
29142 +
29143 +       return err;
29144 +}
29145 +
29146 +struct do_xino_fwrite_args {
29147 +       ssize_t *errp;
29148 +       au_writef_t func;
29149 +       struct file *file;
29150 +       void *buf;
29151 +       size_t size;
29152 +       loff_t *pos;
29153 +};
29154 +
29155 +static void call_do_xino_fwrite(void *args)
29156 +{
29157 +       struct do_xino_fwrite_args *a = args;
29158 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
29159 +}
29160 +
29161 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
29162 +                   loff_t *pos)
29163 +{
29164 +       ssize_t err;
29165 +
29166 +       /* todo: signal block and no wkq? */
29167 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
29168 +               lockdep_off();
29169 +               err = do_xino_fwrite(func, file, buf, size, pos);
29170 +               lockdep_on();
29171 +       } else {
29172 +               /*
29173 +                * it breaks RLIMIT_FSIZE and normal user's limit,
29174 +                * users should care about quota and real 'filesystem full.'
29175 +                */
29176 +               int wkq_err;
29177 +               struct do_xino_fwrite_args args = {
29178 +                       .errp   = &err,
29179 +                       .func   = func,
29180 +                       .file   = file,
29181 +                       .buf    = buf,
29182 +                       .size   = size,
29183 +                       .pos    = pos
29184 +               };
29185 +
29186 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
29187 +               if (unlikely(wkq_err))
29188 +                       err = wkq_err;
29189 +       }
29190 +
29191 +       return err;
29192 +}
29193 +
29194 +/* ---------------------------------------------------------------------- */
29195 +
29196 +/*
29197 + * create a new xinofile at the same place/path as @base_file.
29198 + */
29199 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
29200 +{
29201 +       struct file *file;
29202 +       struct dentry *base, *parent;
29203 +       struct inode *dir;
29204 +       struct qstr *name;
29205 +       struct path path;
29206 +       int err;
29207 +
29208 +       base = base_file->f_dentry;
29209 +       parent = base->d_parent; /* dir inode is locked */
29210 +       dir = parent->d_inode;
29211 +       IMustLock(dir);
29212 +
29213 +       file = ERR_PTR(-EINVAL);
29214 +       name = &base->d_name;
29215 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
29216 +       if (IS_ERR(path.dentry)) {
29217 +               file = (void *)path.dentry;
29218 +               pr_err("%.*s lookup err %ld\n",
29219 +                      AuLNPair(name), PTR_ERR(path.dentry));
29220 +               goto out;
29221 +       }
29222 +
29223 +       /* no need to mnt_want_write() since we call dentry_open() later */
29224 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
29225 +       if (unlikely(err)) {
29226 +               file = ERR_PTR(err);
29227 +               pr_err("%.*s create err %d\n", AuLNPair(name), err);
29228 +               goto out_dput;
29229 +       }
29230 +
29231 +       path.mnt = base_file->f_vfsmnt;
29232 +       file = vfsub_dentry_open(&path,
29233 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
29234 +                                /* | __FMODE_NONOTIFY */);
29235 +       if (IS_ERR(file)) {
29236 +               pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
29237 +               goto out_dput;
29238 +       }
29239 +
29240 +       err = vfsub_unlink(dir, &file->f_path, /*force*/0);
29241 +       if (unlikely(err)) {
29242 +               pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
29243 +               goto out_fput;
29244 +       }
29245 +
29246 +       if (copy_src) {
29247 +               /* no one can touch copy_src xino */
29248 +               err = au_copy_file(file, copy_src,
29249 +                                  i_size_read(copy_src->f_dentry->d_inode));
29250 +               if (unlikely(err)) {
29251 +                       pr_err("%.*s copy err %d\n", AuLNPair(name), err);
29252 +                       goto out_fput;
29253 +               }
29254 +       }
29255 +       goto out_dput; /* success */
29256 +
29257 +out_fput:
29258 +       fput(file);
29259 +       file = ERR_PTR(err);
29260 +out_dput:
29261 +       dput(path.dentry);
29262 +out:
29263 +       return file;
29264 +}
29265 +
29266 +struct au_xino_lock_dir {
29267 +       struct au_hinode *hdir;
29268 +       struct dentry *parent;
29269 +       struct mutex *mtx;
29270 +};
29271 +
29272 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
29273 +                            struct au_xino_lock_dir *ldir)
29274 +{
29275 +       aufs_bindex_t brid, bindex;
29276 +
29277 +       ldir->hdir = NULL;
29278 +       bindex = -1;
29279 +       brid = au_xino_brid(sb);
29280 +       if (brid >= 0)
29281 +               bindex = au_br_index(sb, brid);
29282 +       if (bindex >= 0) {
29283 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
29284 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
29285 +       } else {
29286 +               ldir->parent = dget_parent(xino->f_dentry);
29287 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
29288 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
29289 +       }
29290 +}
29291 +
29292 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
29293 +{
29294 +       if (ldir->hdir)
29295 +               au_hn_imtx_unlock(ldir->hdir);
29296 +       else {
29297 +               mutex_unlock(ldir->mtx);
29298 +               dput(ldir->parent);
29299 +       }
29300 +}
29301 +
29302 +/* ---------------------------------------------------------------------- */
29303 +
29304 +/* trucate xino files asynchronously */
29305 +
29306 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
29307 +{
29308 +       int err;
29309 +       aufs_bindex_t bi, bend;
29310 +       struct au_branch *br;
29311 +       struct file *new_xino, *file;
29312 +       struct super_block *h_sb;
29313 +       struct au_xino_lock_dir ldir;
29314 +
29315 +       err = -EINVAL;
29316 +       bend = au_sbend(sb);
29317 +       if (unlikely(bindex < 0 || bend < bindex))
29318 +               goto out;
29319 +       br = au_sbr(sb, bindex);
29320 +       file = br->br_xino.xi_file;
29321 +       if (!file)
29322 +               goto out;
29323 +
29324 +       au_xino_lock_dir(sb, file, &ldir);
29325 +       /* mnt_want_write() is unnecessary here */
29326 +       new_xino = au_xino_create2(file, file);
29327 +       au_xino_unlock_dir(&ldir);
29328 +       err = PTR_ERR(new_xino);
29329 +       if (IS_ERR(new_xino))
29330 +               goto out;
29331 +       err = 0;
29332 +       fput(file);
29333 +       br->br_xino.xi_file = new_xino;
29334 +
29335 +       h_sb = br->br_mnt->mnt_sb;
29336 +       for (bi = 0; bi <= bend; bi++) {
29337 +               if (unlikely(bi == bindex))
29338 +                       continue;
29339 +               br = au_sbr(sb, bi);
29340 +               if (br->br_mnt->mnt_sb != h_sb)
29341 +                       continue;
29342 +
29343 +               fput(br->br_xino.xi_file);
29344 +               br->br_xino.xi_file = new_xino;
29345 +               get_file(new_xino);
29346 +       }
29347 +
29348 +out:
29349 +       return err;
29350 +}
29351 +
29352 +struct xino_do_trunc_args {
29353 +       struct super_block *sb;
29354 +       struct au_branch *br;
29355 +};
29356 +
29357 +static void xino_do_trunc(void *_args)
29358 +{
29359 +       struct xino_do_trunc_args *args = _args;
29360 +       struct super_block *sb;
29361 +       struct au_branch *br;
29362 +       struct inode *dir;
29363 +       int err;
29364 +       aufs_bindex_t bindex;
29365 +
29366 +       err = 0;
29367 +       sb = args->sb;
29368 +       dir = sb->s_root->d_inode;
29369 +       br = args->br;
29370 +
29371 +       si_noflush_write_lock(sb);
29372 +       ii_read_lock_parent(dir);
29373 +       bindex = au_br_index(sb, br->br_id);
29374 +       err = au_xino_trunc(sb, bindex);
29375 +       if (!err
29376 +           && br->br_xino.xi_file->f_dentry->d_inode->i_blocks
29377 +           >= br->br_xino_upper)
29378 +               br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
29379 +
29380 +       ii_read_unlock(dir);
29381 +       if (unlikely(err))
29382 +               pr_warning("err b%d, (%d)\n", bindex, err);
29383 +       atomic_dec(&br->br_xino_running);
29384 +       atomic_dec(&br->br_count);
29385 +       si_write_unlock(sb);
29386 +       au_nwt_done(&au_sbi(sb)->si_nowait);
29387 +       kfree(args);
29388 +}
29389 +
29390 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
29391 +{
29392 +       struct xino_do_trunc_args *args;
29393 +       int wkq_err;
29394 +
29395 +       if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
29396 +           < br->br_xino_upper)
29397 +               return;
29398 +
29399 +       if (atomic_inc_return(&br->br_xino_running) > 1)
29400 +               goto out;
29401 +
29402 +       /* lock and kfree() will be called in trunc_xino() */
29403 +       args = kmalloc(sizeof(*args), GFP_NOFS);
29404 +       if (unlikely(!args)) {
29405 +               AuErr1("no memory\n");
29406 +               goto out_args;
29407 +       }
29408 +
29409 +       atomic_inc(&br->br_count);
29410 +       args->sb = sb;
29411 +       args->br = br;
29412 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
29413 +       if (!wkq_err)
29414 +               return; /* success */
29415 +
29416 +       pr_err("wkq %d\n", wkq_err);
29417 +       atomic_dec(&br->br_count);
29418 +
29419 +out_args:
29420 +       kfree(args);
29421 +out:
29422 +       atomic_dec(&br->br_xino_running);
29423 +}
29424 +
29425 +/* ---------------------------------------------------------------------- */
29426 +
29427 +static int au_xino_do_write(au_writef_t write, struct file *file,
29428 +                           ino_t h_ino, ino_t ino)
29429 +{
29430 +       loff_t pos;
29431 +       ssize_t sz;
29432 +
29433 +       pos = h_ino;
29434 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
29435 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
29436 +               return -EFBIG;
29437 +       }
29438 +       pos *= sizeof(ino);
29439 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
29440 +       if (sz == sizeof(ino))
29441 +               return 0; /* success */
29442 +
29443 +       AuIOErr("write failed (%zd)\n", sz);
29444 +       return -EIO;
29445 +}
29446 +
29447 +/*
29448 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
29449 + * at the position of @h_ino.
29450 + * even if @ino is zero, it is written to the xinofile and means no entry.
29451 + * if the size of the xino file on a specific filesystem exceeds the watermark,
29452 + * try truncating it.
29453 + */
29454 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
29455 +                 ino_t ino)
29456 +{
29457 +       int err;
29458 +       unsigned int mnt_flags;
29459 +       struct au_branch *br;
29460 +
29461 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
29462 +                    || ((loff_t)-1) > 0);
29463 +       SiMustAnyLock(sb);
29464 +
29465 +       mnt_flags = au_mntflags(sb);
29466 +       if (!au_opt_test(mnt_flags, XINO))
29467 +               return 0;
29468 +
29469 +       br = au_sbr(sb, bindex);
29470 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
29471 +                              h_ino, ino);
29472 +       if (!err) {
29473 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
29474 +                   && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
29475 +                       xino_try_trunc(sb, br);
29476 +               return 0; /* success */
29477 +       }
29478 +
29479 +       AuIOErr("write failed (%d)\n", err);
29480 +       return -EIO;
29481 +}
29482 +
29483 +/* ---------------------------------------------------------------------- */
29484 +
29485 +/* aufs inode number bitmap */
29486 +
29487 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
29488 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
29489 +{
29490 +       ino_t ino;
29491 +
29492 +       AuDebugOn(bit < 0 || page_bits <= bit);
29493 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
29494 +       return ino;
29495 +}
29496 +
29497 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
29498 +{
29499 +       AuDebugOn(ino < AUFS_FIRST_INO);
29500 +       ino -= AUFS_FIRST_INO;
29501 +       *pindex = ino / page_bits;
29502 +       *bit = ino % page_bits;
29503 +}
29504 +
29505 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
29506 +{
29507 +       int err;
29508 +       loff_t pos;
29509 +       ssize_t sz;
29510 +       struct au_sbinfo *sbinfo;
29511 +       struct file *xib;
29512 +       unsigned long *p;
29513 +
29514 +       sbinfo = au_sbi(sb);
29515 +       MtxMustLock(&sbinfo->si_xib_mtx);
29516 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
29517 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
29518 +
29519 +       if (pindex == sbinfo->si_xib_last_pindex)
29520 +               return 0;
29521 +
29522 +       xib = sbinfo->si_xib;
29523 +       p = sbinfo->si_xib_buf;
29524 +       pos = sbinfo->si_xib_last_pindex;
29525 +       pos *= PAGE_SIZE;
29526 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
29527 +       if (unlikely(sz != PAGE_SIZE))
29528 +               goto out;
29529 +
29530 +       pos = pindex;
29531 +       pos *= PAGE_SIZE;
29532 +       if (i_size_read(xib->f_dentry->d_inode) >= pos + PAGE_SIZE)
29533 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
29534 +       else {
29535 +               memset(p, 0, PAGE_SIZE);
29536 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
29537 +       }
29538 +       if (sz == PAGE_SIZE) {
29539 +               sbinfo->si_xib_last_pindex = pindex;
29540 +               return 0; /* success */
29541 +       }
29542 +
29543 +out:
29544 +       AuIOErr1("write failed (%zd)\n", sz);
29545 +       err = sz;
29546 +       if (sz >= 0)
29547 +               err = -EIO;
29548 +       return err;
29549 +}
29550 +
29551 +/* ---------------------------------------------------------------------- */
29552 +
29553 +static void au_xib_clear_bit(struct inode *inode)
29554 +{
29555 +       int err, bit;
29556 +       unsigned long pindex;
29557 +       struct super_block *sb;
29558 +       struct au_sbinfo *sbinfo;
29559 +
29560 +       AuDebugOn(inode->i_nlink);
29561 +
29562 +       sb = inode->i_sb;
29563 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
29564 +       AuDebugOn(page_bits <= bit);
29565 +       sbinfo = au_sbi(sb);
29566 +       mutex_lock(&sbinfo->si_xib_mtx);
29567 +       err = xib_pindex(sb, pindex);
29568 +       if (!err) {
29569 +               clear_bit(bit, sbinfo->si_xib_buf);
29570 +               sbinfo->si_xib_next_bit = bit;
29571 +       }
29572 +       mutex_unlock(&sbinfo->si_xib_mtx);
29573 +}
29574 +
29575 +/* for s_op->delete_inode() */
29576 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
29577 +{
29578 +       int err;
29579 +       unsigned int mnt_flags;
29580 +       aufs_bindex_t bindex, bend, bi;
29581 +       unsigned char try_trunc;
29582 +       struct au_iinfo *iinfo;
29583 +       struct super_block *sb;
29584 +       struct au_hinode *hi;
29585 +       struct inode *h_inode;
29586 +       struct au_branch *br;
29587 +       au_writef_t xwrite;
29588 +
29589 +       sb = inode->i_sb;
29590 +       mnt_flags = au_mntflags(sb);
29591 +       if (!au_opt_test(mnt_flags, XINO)
29592 +           || inode->i_ino == AUFS_ROOT_INO)
29593 +               return;
29594 +
29595 +       if (unlinked) {
29596 +               au_xigen_inc(inode);
29597 +               au_xib_clear_bit(inode);
29598 +       }
29599 +
29600 +       iinfo = au_ii(inode);
29601 +       if (!iinfo)
29602 +               return;
29603 +
29604 +       bindex = iinfo->ii_bstart;
29605 +       if (bindex < 0)
29606 +               return;
29607 +
29608 +       xwrite = au_sbi(sb)->si_xwrite;
29609 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
29610 +       hi = iinfo->ii_hinode + bindex;
29611 +       bend = iinfo->ii_bend;
29612 +       for (; bindex <= bend; bindex++, hi++) {
29613 +               h_inode = hi->hi_inode;
29614 +               if (!h_inode
29615 +                   || (!unlinked && h_inode->i_nlink))
29616 +                       continue;
29617 +
29618 +               /* inode may not be revalidated */
29619 +               bi = au_br_index(sb, hi->hi_id);
29620 +               if (bi < 0)
29621 +                       continue;
29622 +
29623 +               br = au_sbr(sb, bi);
29624 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
29625 +                                      h_inode->i_ino, /*ino*/0);
29626 +               if (!err && try_trunc
29627 +                   && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
29628 +                       xino_try_trunc(sb, br);
29629 +       }
29630 +}
29631 +
29632 +/* get an unused inode number from bitmap */
29633 +ino_t au_xino_new_ino(struct super_block *sb)
29634 +{
29635 +       ino_t ino;
29636 +       unsigned long *p, pindex, ul, pend;
29637 +       struct au_sbinfo *sbinfo;
29638 +       struct file *file;
29639 +       int free_bit, err;
29640 +
29641 +       if (!au_opt_test(au_mntflags(sb), XINO))
29642 +               return iunique(sb, AUFS_FIRST_INO);
29643 +
29644 +       sbinfo = au_sbi(sb);
29645 +       mutex_lock(&sbinfo->si_xib_mtx);
29646 +       p = sbinfo->si_xib_buf;
29647 +       free_bit = sbinfo->si_xib_next_bit;
29648 +       if (free_bit < page_bits && !test_bit(free_bit, p))
29649 +               goto out; /* success */
29650 +       free_bit = find_first_zero_bit(p, page_bits);
29651 +       if (free_bit < page_bits)
29652 +               goto out; /* success */
29653 +
29654 +       pindex = sbinfo->si_xib_last_pindex;
29655 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
29656 +               err = xib_pindex(sb, ul);
29657 +               if (unlikely(err))
29658 +                       goto out_err;
29659 +               free_bit = find_first_zero_bit(p, page_bits);
29660 +               if (free_bit < page_bits)
29661 +                       goto out; /* success */
29662 +       }
29663 +
29664 +       file = sbinfo->si_xib;
29665 +       pend = i_size_read(file->f_dentry->d_inode) / PAGE_SIZE;
29666 +       for (ul = pindex + 1; ul <= pend; ul++) {
29667 +               err = xib_pindex(sb, ul);
29668 +               if (unlikely(err))
29669 +                       goto out_err;
29670 +               free_bit = find_first_zero_bit(p, page_bits);
29671 +               if (free_bit < page_bits)
29672 +                       goto out; /* success */
29673 +       }
29674 +       BUG();
29675 +
29676 +out:
29677 +       set_bit(free_bit, p);
29678 +       sbinfo->si_xib_next_bit = free_bit + 1;
29679 +       pindex = sbinfo->si_xib_last_pindex;
29680 +       mutex_unlock(&sbinfo->si_xib_mtx);
29681 +       ino = xib_calc_ino(pindex, free_bit);
29682 +       AuDbg("i%lu\n", (unsigned long)ino);
29683 +       return ino;
29684 +out_err:
29685 +       mutex_unlock(&sbinfo->si_xib_mtx);
29686 +       AuDbg("i0\n");
29687 +       return 0;
29688 +}
29689 +
29690 +/*
29691 + * read @ino from xinofile for the specified branch{@sb, @bindex}
29692 + * at the position of @h_ino.
29693 + * if @ino does not exist and @do_new is true, get new one.
29694 + */
29695 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
29696 +                ino_t *ino)
29697 +{
29698 +       int err;
29699 +       ssize_t sz;
29700 +       loff_t pos;
29701 +       struct file *file;
29702 +       struct au_sbinfo *sbinfo;
29703 +
29704 +       *ino = 0;
29705 +       if (!au_opt_test(au_mntflags(sb), XINO))
29706 +               return 0; /* no xino */
29707 +
29708 +       err = 0;
29709 +       sbinfo = au_sbi(sb);
29710 +       pos = h_ino;
29711 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
29712 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
29713 +               return -EFBIG;
29714 +       }
29715 +       pos *= sizeof(*ino);
29716 +
29717 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
29718 +       if (i_size_read(file->f_dentry->d_inode) < pos + sizeof(*ino))
29719 +               return 0; /* no ino */
29720 +
29721 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
29722 +       if (sz == sizeof(*ino))
29723 +               return 0; /* success */
29724 +
29725 +       err = sz;
29726 +       if (unlikely(sz >= 0)) {
29727 +               err = -EIO;
29728 +               AuIOErr("xino read error (%zd)\n", sz);
29729 +       }
29730 +
29731 +       return err;
29732 +}
29733 +
29734 +/* ---------------------------------------------------------------------- */
29735 +
29736 +/* create and set a new xino file */
29737 +
29738 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
29739 +{
29740 +       struct file *file;
29741 +       struct dentry *h_parent, *d;
29742 +       struct inode *h_dir;
29743 +       int err;
29744 +
29745 +       /*
29746 +        * at mount-time, and the xino file is the default path,
29747 +        * hnotify is disabled so we have no notify events to ignore.
29748 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
29749 +        */
29750 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
29751 +                              /* | __FMODE_NONOTIFY */,
29752 +                              S_IRUGO | S_IWUGO);
29753 +       if (IS_ERR(file)) {
29754 +               if (!silent)
29755 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
29756 +               return file;
29757 +       }
29758 +
29759 +       /* keep file count */
29760 +       h_parent = dget_parent(file->f_dentry);
29761 +       h_dir = h_parent->d_inode;
29762 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
29763 +       /* mnt_want_write() is unnecessary here */
29764 +       err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
29765 +       mutex_unlock(&h_dir->i_mutex);
29766 +       dput(h_parent);
29767 +       if (unlikely(err)) {
29768 +               if (!silent)
29769 +                       pr_err("unlink %s(%d)\n", fname, err);
29770 +               goto out;
29771 +       }
29772 +
29773 +       err = -EINVAL;
29774 +       d = file->f_dentry;
29775 +       if (unlikely(sb == d->d_sb)) {
29776 +               if (!silent)
29777 +                       pr_err("%s must be outside\n", fname);
29778 +               goto out;
29779 +       }
29780 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
29781 +               if (!silent)
29782 +                       pr_err("xino doesn't support %s(%s)\n",
29783 +                              fname, au_sbtype(d->d_sb));
29784 +               goto out;
29785 +       }
29786 +       return file; /* success */
29787 +
29788 +out:
29789 +       fput(file);
29790 +       file = ERR_PTR(err);
29791 +       return file;
29792 +}
29793 +
29794 +/*
29795 + * find another branch who is on the same filesystem of the specified
29796 + * branch{@btgt}. search until @bend.
29797 + */
29798 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
29799 +                       aufs_bindex_t bend)
29800 +{
29801 +       aufs_bindex_t bindex;
29802 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
29803 +
29804 +       for (bindex = 0; bindex < btgt; bindex++)
29805 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
29806 +                       return bindex;
29807 +       for (bindex++; bindex <= bend; bindex++)
29808 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
29809 +                       return bindex;
29810 +       return -1;
29811 +}
29812 +
29813 +/* ---------------------------------------------------------------------- */
29814 +
29815 +/*
29816 + * initialize the xinofile for the specified branch @br
29817 + * at the place/path where @base_file indicates.
29818 + * test whether another branch is on the same filesystem or not,
29819 + * if @do_test is true.
29820 + */
29821 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
29822 +              struct file *base_file, int do_test)
29823 +{
29824 +       int err;
29825 +       ino_t ino;
29826 +       aufs_bindex_t bend, bindex;
29827 +       struct au_branch *shared_br, *b;
29828 +       struct file *file;
29829 +       struct super_block *tgt_sb;
29830 +
29831 +       shared_br = NULL;
29832 +       bend = au_sbend(sb);
29833 +       if (do_test) {
29834 +               tgt_sb = br->br_mnt->mnt_sb;
29835 +               for (bindex = 0; bindex <= bend; bindex++) {
29836 +                       b = au_sbr(sb, bindex);
29837 +                       if (tgt_sb == b->br_mnt->mnt_sb) {
29838 +                               shared_br = b;
29839 +                               break;
29840 +                       }
29841 +               }
29842 +       }
29843 +
29844 +       if (!shared_br || !shared_br->br_xino.xi_file) {
29845 +               struct au_xino_lock_dir ldir;
29846 +
29847 +               au_xino_lock_dir(sb, base_file, &ldir);
29848 +               /* mnt_want_write() is unnecessary here */
29849 +               file = au_xino_create2(base_file, NULL);
29850 +               au_xino_unlock_dir(&ldir);
29851 +               err = PTR_ERR(file);
29852 +               if (IS_ERR(file))
29853 +                       goto out;
29854 +               br->br_xino.xi_file = file;
29855 +       } else {
29856 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
29857 +               get_file(br->br_xino.xi_file);
29858 +       }
29859 +
29860 +       ino = AUFS_ROOT_INO;
29861 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
29862 +                              h_ino, ino);
29863 +       if (unlikely(err)) {
29864 +               fput(br->br_xino.xi_file);
29865 +               br->br_xino.xi_file = NULL;
29866 +       }
29867 +
29868 +out:
29869 +       return err;
29870 +}
29871 +
29872 +/* ---------------------------------------------------------------------- */
29873 +
29874 +/* trucate a xino bitmap file */
29875 +
29876 +/* todo: slow */
29877 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
29878 +{
29879 +       int err, bit;
29880 +       ssize_t sz;
29881 +       unsigned long pindex;
29882 +       loff_t pos, pend;
29883 +       struct au_sbinfo *sbinfo;
29884 +       au_readf_t func;
29885 +       ino_t *ino;
29886 +       unsigned long *p;
29887 +
29888 +       err = 0;
29889 +       sbinfo = au_sbi(sb);
29890 +       MtxMustLock(&sbinfo->si_xib_mtx);
29891 +       p = sbinfo->si_xib_buf;
29892 +       func = sbinfo->si_xread;
29893 +       pend = i_size_read(file->f_dentry->d_inode);
29894 +       pos = 0;
29895 +       while (pos < pend) {
29896 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
29897 +               err = sz;
29898 +               if (unlikely(sz <= 0))
29899 +                       goto out;
29900 +
29901 +               err = 0;
29902 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
29903 +                       if (unlikely(*ino < AUFS_FIRST_INO))
29904 +                               continue;
29905 +
29906 +                       xib_calc_bit(*ino, &pindex, &bit);
29907 +                       AuDebugOn(page_bits <= bit);
29908 +                       err = xib_pindex(sb, pindex);
29909 +                       if (!err)
29910 +                               set_bit(bit, p);
29911 +                       else
29912 +                               goto out;
29913 +               }
29914 +       }
29915 +
29916 +out:
29917 +       return err;
29918 +}
29919 +
29920 +static int xib_restore(struct super_block *sb)
29921 +{
29922 +       int err;
29923 +       aufs_bindex_t bindex, bend;
29924 +       void *page;
29925 +
29926 +       err = -ENOMEM;
29927 +       page = (void *)__get_free_page(GFP_NOFS);
29928 +       if (unlikely(!page))
29929 +               goto out;
29930 +
29931 +       err = 0;
29932 +       bend = au_sbend(sb);
29933 +       for (bindex = 0; !err && bindex <= bend; bindex++)
29934 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
29935 +                       err = do_xib_restore
29936 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
29937 +               else
29938 +                       AuDbg("b%d\n", bindex);
29939 +       free_page((unsigned long)page);
29940 +
29941 +out:
29942 +       return err;
29943 +}
29944 +
29945 +int au_xib_trunc(struct super_block *sb)
29946 +{
29947 +       int err;
29948 +       ssize_t sz;
29949 +       loff_t pos;
29950 +       struct au_xino_lock_dir ldir;
29951 +       struct au_sbinfo *sbinfo;
29952 +       unsigned long *p;
29953 +       struct file *file;
29954 +
29955 +       SiMustWriteLock(sb);
29956 +
29957 +       err = 0;
29958 +       sbinfo = au_sbi(sb);
29959 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
29960 +               goto out;
29961 +
29962 +       file = sbinfo->si_xib;
29963 +       if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
29964 +               goto out;
29965 +
29966 +       au_xino_lock_dir(sb, file, &ldir);
29967 +       /* mnt_want_write() is unnecessary here */
29968 +       file = au_xino_create2(sbinfo->si_xib, NULL);
29969 +       au_xino_unlock_dir(&ldir);
29970 +       err = PTR_ERR(file);
29971 +       if (IS_ERR(file))
29972 +               goto out;
29973 +       fput(sbinfo->si_xib);
29974 +       sbinfo->si_xib = file;
29975 +
29976 +       p = sbinfo->si_xib_buf;
29977 +       memset(p, 0, PAGE_SIZE);
29978 +       pos = 0;
29979 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
29980 +       if (unlikely(sz != PAGE_SIZE)) {
29981 +               err = sz;
29982 +               AuIOErr("err %d\n", err);
29983 +               if (sz >= 0)
29984 +                       err = -EIO;
29985 +               goto out;
29986 +       }
29987 +
29988 +       mutex_lock(&sbinfo->si_xib_mtx);
29989 +       /* mnt_want_write() is unnecessary here */
29990 +       err = xib_restore(sb);
29991 +       mutex_unlock(&sbinfo->si_xib_mtx);
29992 +
29993 +out:
29994 +       return err;
29995 +}
29996 +
29997 +/* ---------------------------------------------------------------------- */
29998 +
29999 +/*
30000 + * xino mount option handlers
30001 + */
30002 +static au_readf_t find_readf(struct file *h_file)
30003 +{
30004 +       const struct file_operations *fop = h_file->f_op;
30005 +
30006 +       if (fop) {
30007 +               if (fop->read)
30008 +                       return fop->read;
30009 +               if (fop->aio_read)
30010 +                       return do_sync_read;
30011 +       }
30012 +       return ERR_PTR(-ENOSYS);
30013 +}
30014 +
30015 +static au_writef_t find_writef(struct file *h_file)
30016 +{
30017 +       const struct file_operations *fop = h_file->f_op;
30018 +
30019 +       if (fop) {
30020 +               if (fop->write)
30021 +                       return fop->write;
30022 +               if (fop->aio_write)
30023 +                       return do_sync_write;
30024 +       }
30025 +       return ERR_PTR(-ENOSYS);
30026 +}
30027 +
30028 +/* xino bitmap */
30029 +static void xino_clear_xib(struct super_block *sb)
30030 +{
30031 +       struct au_sbinfo *sbinfo;
30032 +
30033 +       SiMustWriteLock(sb);
30034 +
30035 +       sbinfo = au_sbi(sb);
30036 +       sbinfo->si_xread = NULL;
30037 +       sbinfo->si_xwrite = NULL;
30038 +       if (sbinfo->si_xib)
30039 +               fput(sbinfo->si_xib);
30040 +       sbinfo->si_xib = NULL;
30041 +       free_page((unsigned long)sbinfo->si_xib_buf);
30042 +       sbinfo->si_xib_buf = NULL;
30043 +}
30044 +
30045 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
30046 +{
30047 +       int err;
30048 +       loff_t pos;
30049 +       struct au_sbinfo *sbinfo;
30050 +       struct file *file;
30051 +
30052 +       SiMustWriteLock(sb);
30053 +
30054 +       sbinfo = au_sbi(sb);
30055 +       file = au_xino_create2(base, sbinfo->si_xib);
30056 +       err = PTR_ERR(file);
30057 +       if (IS_ERR(file))
30058 +               goto out;
30059 +       if (sbinfo->si_xib)
30060 +               fput(sbinfo->si_xib);
30061 +       sbinfo->si_xib = file;
30062 +       sbinfo->si_xread = find_readf(file);
30063 +       sbinfo->si_xwrite = find_writef(file);
30064 +
30065 +       err = -ENOMEM;
30066 +       if (!sbinfo->si_xib_buf)
30067 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
30068 +       if (unlikely(!sbinfo->si_xib_buf))
30069 +               goto out_unset;
30070 +
30071 +       sbinfo->si_xib_last_pindex = 0;
30072 +       sbinfo->si_xib_next_bit = 0;
30073 +       if (i_size_read(file->f_dentry->d_inode) < PAGE_SIZE) {
30074 +               pos = 0;
30075 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
30076 +                                 PAGE_SIZE, &pos);
30077 +               if (unlikely(err != PAGE_SIZE))
30078 +                       goto out_free;
30079 +       }
30080 +       err = 0;
30081 +       goto out; /* success */
30082 +
30083 +out_free:
30084 +       free_page((unsigned long)sbinfo->si_xib_buf);
30085 +       sbinfo->si_xib_buf = NULL;
30086 +       if (err >= 0)
30087 +               err = -EIO;
30088 +out_unset:
30089 +       fput(sbinfo->si_xib);
30090 +       sbinfo->si_xib = NULL;
30091 +       sbinfo->si_xread = NULL;
30092 +       sbinfo->si_xwrite = NULL;
30093 +out:
30094 +       return err;
30095 +}
30096 +
30097 +/* xino for each branch */
30098 +static void xino_clear_br(struct super_block *sb)
30099 +{
30100 +       aufs_bindex_t bindex, bend;
30101 +       struct au_branch *br;
30102 +
30103 +       bend = au_sbend(sb);
30104 +       for (bindex = 0; bindex <= bend; bindex++) {
30105 +               br = au_sbr(sb, bindex);
30106 +               if (!br || !br->br_xino.xi_file)
30107 +                       continue;
30108 +
30109 +               fput(br->br_xino.xi_file);
30110 +               br->br_xino.xi_file = NULL;
30111 +       }
30112 +}
30113 +
30114 +static int au_xino_set_br(struct super_block *sb, struct file *base)
30115 +{
30116 +       int err;
30117 +       ino_t ino;
30118 +       aufs_bindex_t bindex, bend, bshared;
30119 +       struct {
30120 +               struct file *old, *new;
30121 +       } *fpair, *p;
30122 +       struct au_branch *br;
30123 +       struct inode *inode;
30124 +       au_writef_t writef;
30125 +
30126 +       SiMustWriteLock(sb);
30127 +
30128 +       err = -ENOMEM;
30129 +       bend = au_sbend(sb);
30130 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
30131 +       if (unlikely(!fpair))
30132 +               goto out;
30133 +
30134 +       inode = sb->s_root->d_inode;
30135 +       ino = AUFS_ROOT_INO;
30136 +       writef = au_sbi(sb)->si_xwrite;
30137 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
30138 +               br = au_sbr(sb, bindex);
30139 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
30140 +               if (bshared >= 0) {
30141 +                       /* shared xino */
30142 +                       *p = fpair[bshared];
30143 +                       get_file(p->new);
30144 +               }
30145 +
30146 +               if (!p->new) {
30147 +                       /* new xino */
30148 +                       p->old = br->br_xino.xi_file;
30149 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
30150 +                       err = PTR_ERR(p->new);
30151 +                       if (IS_ERR(p->new)) {
30152 +                               p->new = NULL;
30153 +                               goto out_pair;
30154 +                       }
30155 +               }
30156 +
30157 +               err = au_xino_do_write(writef, p->new,
30158 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
30159 +               if (unlikely(err))
30160 +                       goto out_pair;
30161 +       }
30162 +
30163 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
30164 +               br = au_sbr(sb, bindex);
30165 +               if (br->br_xino.xi_file)
30166 +                       fput(br->br_xino.xi_file);
30167 +               get_file(p->new);
30168 +               br->br_xino.xi_file = p->new;
30169 +       }
30170 +
30171 +out_pair:
30172 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
30173 +               if (p->new)
30174 +                       fput(p->new);
30175 +               else
30176 +                       break;
30177 +       kfree(fpair);
30178 +out:
30179 +       return err;
30180 +}
30181 +
30182 +void au_xino_clr(struct super_block *sb)
30183 +{
30184 +       struct au_sbinfo *sbinfo;
30185 +
30186 +       au_xigen_clr(sb);
30187 +       xino_clear_xib(sb);
30188 +       xino_clear_br(sb);
30189 +       sbinfo = au_sbi(sb);
30190 +       /* lvalue, do not call au_mntflags() */
30191 +       au_opt_clr(sbinfo->si_mntflags, XINO);
30192 +}
30193 +
30194 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
30195 +{
30196 +       int err, skip;
30197 +       struct dentry *parent, *cur_parent;
30198 +       struct qstr *dname, *cur_name;
30199 +       struct file *cur_xino;
30200 +       struct inode *dir;
30201 +       struct au_sbinfo *sbinfo;
30202 +
30203 +       SiMustWriteLock(sb);
30204 +
30205 +       err = 0;
30206 +       sbinfo = au_sbi(sb);
30207 +       parent = dget_parent(xino->file->f_dentry);
30208 +       if (remount) {
30209 +               skip = 0;
30210 +               dname = &xino->file->f_dentry->d_name;
30211 +               cur_xino = sbinfo->si_xib;
30212 +               if (cur_xino) {
30213 +                       cur_parent = dget_parent(cur_xino->f_dentry);
30214 +                       cur_name = &cur_xino->f_dentry->d_name;
30215 +                       skip = (cur_parent == parent
30216 +                               && dname->len == cur_name->len
30217 +                               && !memcmp(dname->name, cur_name->name,
30218 +                                          dname->len));
30219 +                       dput(cur_parent);
30220 +               }
30221 +               if (skip)
30222 +                       goto out;
30223 +       }
30224 +
30225 +       au_opt_set(sbinfo->si_mntflags, XINO);
30226 +       dir = parent->d_inode;
30227 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
30228 +       /* mnt_want_write() is unnecessary here */
30229 +       err = au_xino_set_xib(sb, xino->file);
30230 +       if (!err)
30231 +               err = au_xigen_set(sb, xino->file);
30232 +       if (!err)
30233 +               err = au_xino_set_br(sb, xino->file);
30234 +       mutex_unlock(&dir->i_mutex);
30235 +       if (!err)
30236 +               goto out; /* success */
30237 +
30238 +       /* reset all */
30239 +       AuIOErr("failed creating xino(%d).\n", err);
30240 +
30241 +out:
30242 +       dput(parent);
30243 +       return err;
30244 +}
30245 +
30246 +/* ---------------------------------------------------------------------- */
30247 +
30248 +/*
30249 + * create a xinofile at the default place/path.
30250 + */
30251 +struct file *au_xino_def(struct super_block *sb)
30252 +{
30253 +       struct file *file;
30254 +       char *page, *p;
30255 +       struct au_branch *br;
30256 +       struct super_block *h_sb;
30257 +       struct path path;
30258 +       aufs_bindex_t bend, bindex, bwr;
30259 +
30260 +       br = NULL;
30261 +       bend = au_sbend(sb);
30262 +       bwr = -1;
30263 +       for (bindex = 0; bindex <= bend; bindex++) {
30264 +               br = au_sbr(sb, bindex);
30265 +               if (au_br_writable(br->br_perm)
30266 +                   && !au_test_fs_bad_xino(br->br_mnt->mnt_sb)) {
30267 +                       bwr = bindex;
30268 +                       break;
30269 +               }
30270 +       }
30271 +
30272 +       if (bwr >= 0) {
30273 +               file = ERR_PTR(-ENOMEM);
30274 +               page = __getname_gfp(GFP_NOFS);
30275 +               if (unlikely(!page))
30276 +                       goto out;
30277 +               path.mnt = br->br_mnt;
30278 +               path.dentry = au_h_dptr(sb->s_root, bwr);
30279 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
30280 +               file = (void *)p;
30281 +               if (!IS_ERR(p)) {
30282 +                       strcat(p, "/" AUFS_XINO_FNAME);
30283 +                       AuDbg("%s\n", p);
30284 +                       file = au_xino_create(sb, p, /*silent*/0);
30285 +                       if (!IS_ERR(file))
30286 +                               au_xino_brid_set(sb, br->br_id);
30287 +               }
30288 +               __putname(page);
30289 +       } else {
30290 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
30291 +               if (IS_ERR(file))
30292 +                       goto out;
30293 +               h_sb = file->f_dentry->d_sb;
30294 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
30295 +                       pr_err("xino doesn't support %s(%s)\n",
30296 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
30297 +                       fput(file);
30298 +                       file = ERR_PTR(-EINVAL);
30299 +               }
30300 +               if (!IS_ERR(file))
30301 +                       au_xino_brid_set(sb, -1);
30302 +       }
30303 +
30304 +out:
30305 +       return file;
30306 +}
30307 +
30308 +/* ---------------------------------------------------------------------- */
30309 +
30310 +int au_xino_path(struct seq_file *seq, struct file *file)
30311 +{
30312 +       int err;
30313 +
30314 +       err = au_seq_path(seq, &file->f_path);
30315 +       if (unlikely(err < 0))
30316 +               goto out;
30317 +
30318 +       err = 0;
30319 +#define Deleted "\\040(deleted)"
30320 +       seq->count -= sizeof(Deleted) - 1;
30321 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
30322 +                        sizeof(Deleted) - 1));
30323 +#undef Deleted
30324 +
30325 +out:
30326 +       return err;
30327 +}
30328 diff -urN /usr/share/empty/include/linux/aufs_type.h linux/include/linux/aufs_type.h
30329 --- /usr/share/empty/include/linux/aufs_type.h  1970-01-01 01:00:00.000000000 +0100
30330 +++ linux/include/linux/aufs_type.h     2012-01-05 11:00:59.218835185 +0100
30331 @@ -0,0 +1,224 @@
30332 +/*
30333 + * Copyright (C) 2005-2011 Junjiro R. Okajima
30334 + *
30335 + * This program, aufs is free software; you can redistribute it and/or modify
30336 + * it under the terms of the GNU General Public License as published by
30337 + * the Free Software Foundation; either version 2 of the License, or
30338 + * (at your option) any later version.
30339 + *
30340 + * This program is distributed in the hope that it will be useful,
30341 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30342 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30343 + * GNU General Public License for more details.
30344 + *
30345 + * You should have received a copy of the GNU General Public License
30346 + * along with this program; if not, write to the Free Software
30347 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30348 + */
30349 +
30350 +#ifndef __AUFS_TYPE_H__
30351 +#define __AUFS_TYPE_H__
30352 +
30353 +#include <linux/ioctl.h>
30354 +#include <linux/kernel.h>
30355 +#include <linux/limits.h>
30356 +#ifdef __KERNEL__
30357 +#include <linux/types.h>
30358 +#else
30359 +#include <stdint.h>
30360 +#include <sys/types.h>
30361 +#endif
30362 +
30363 +#define AUFS_VERSION   "3.x-rcN-20111205"
30364 +
30365 +/* todo? move this to linux-2.6.19/include/magic.h */
30366 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
30367 +
30368 +/* ---------------------------------------------------------------------- */
30369 +
30370 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
30371 +typedef int8_t aufs_bindex_t;
30372 +#define AUFS_BRANCH_MAX 127
30373 +#else
30374 +typedef int16_t aufs_bindex_t;
30375 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
30376 +#define AUFS_BRANCH_MAX 511
30377 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
30378 +#define AUFS_BRANCH_MAX 1023
30379 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
30380 +#define AUFS_BRANCH_MAX 32767
30381 +#endif
30382 +#endif
30383 +
30384 +#ifdef __KERNEL__
30385 +#ifndef AUFS_BRANCH_MAX
30386 +#error unknown CONFIG_AUFS_BRANCH_MAX value
30387 +#endif
30388 +#endif /* __KERNEL__ */
30389 +
30390 +/* ---------------------------------------------------------------------- */
30391 +
30392 +#define AUFS_NAME              "aufs"
30393 +#define AUFS_FSTYPE            AUFS_NAME
30394 +
30395 +#define AUFS_ROOT_INO          2
30396 +#define AUFS_FIRST_INO         11
30397 +
30398 +#define AUFS_WH_PFX            ".wh."
30399 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
30400 +#define AUFS_WH_TMP_LEN                4
30401 +/* a limit for rmdir/rename a dir */
30402 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
30403 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
30404 +                               - 1                     /* dot */\
30405 +                               - AUFS_WH_TMP_LEN)      /* hex */
30406 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
30407 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
30408 +#define AUFS_XINO_TRUNC_INIT   64 /* blocks */
30409 +#define AUFS_XINO_TRUNC_STEP   4  /* blocks */
30410 +#define AUFS_DIRWH_DEF         3
30411 +#define AUFS_RDCACHE_DEF       10 /* seconds */
30412 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
30413 +#define AUFS_RDBLK_DEF         512 /* bytes */
30414 +#define AUFS_RDHASH_DEF                32
30415 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
30416 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
30417 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
30418 +#define AUFS_PLINK_WARN                100 /* number of plinks */
30419 +
30420 +/* pseudo-link maintenace under /proc */
30421 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
30422 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
30423 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
30424 +
30425 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
30426 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
30427 +
30428 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
30429 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
30430 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
30431 +
30432 +/* doubly whiteouted */
30433 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
30434 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
30435 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
30436 +
30437 +/* branch permissions and attributes */
30438 +#define AUFS_BRPERM_RW         "rw"
30439 +#define AUFS_BRPERM_RO         "ro"
30440 +#define AUFS_BRPERM_RR         "rr"
30441 +#define AUFS_BRRATTR_WH                "wh"
30442 +#define AUFS_BRWATTR_NLWH      "nolwh"
30443 +
30444 +/* ---------------------------------------------------------------------- */
30445 +
30446 +/* ioctl */
30447 +enum {
30448 +       /* readdir in userspace */
30449 +       AuCtl_RDU,
30450 +       AuCtl_RDU_INO,
30451 +
30452 +       /* pathconf wrapper */
30453 +       AuCtl_WBR_FD,
30454 +
30455 +       /* busy inode */
30456 +       AuCtl_IBUSY
30457 +};
30458 +
30459 +/* borrowed from linux/include/linux/kernel.h */
30460 +#ifndef ALIGN
30461 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
30462 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
30463 +#endif
30464 +
30465 +/* borrowed from linux/include/linux/compiler-gcc3.h */
30466 +#ifndef __aligned
30467 +#define __aligned(x)                   __attribute__((aligned(x)))
30468 +#endif
30469 +
30470 +#ifdef __KERNEL__
30471 +#ifndef __packed
30472 +#define __packed                       __attribute__((packed))
30473 +#endif
30474 +#endif
30475 +
30476 +struct au_rdu_cookie {
30477 +       uint64_t        h_pos;
30478 +       int16_t         bindex;
30479 +       uint8_t         flags;
30480 +       uint8_t         pad;
30481 +       uint32_t        generation;
30482 +} __aligned(8);
30483 +
30484 +struct au_rdu_ent {
30485 +       uint64_t        ino;
30486 +       int16_t         bindex;
30487 +       uint8_t         type;
30488 +       uint8_t         nlen;
30489 +       uint8_t         wh;
30490 +       char            name[0];
30491 +} __aligned(8);
30492 +
30493 +static inline int au_rdu_len(int nlen)
30494 +{
30495 +       /* include the terminating NULL */
30496 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
30497 +                    sizeof(uint64_t));
30498 +}
30499 +
30500 +union au_rdu_ent_ul {
30501 +       struct au_rdu_ent __user        *e;
30502 +       uint64_t                        ul;
30503 +};
30504 +
30505 +enum {
30506 +       AufsCtlRduV_SZ,
30507 +       AufsCtlRduV_End
30508 +};
30509 +
30510 +struct aufs_rdu {
30511 +       /* input */
30512 +       union {
30513 +               uint64_t        sz;     /* AuCtl_RDU */
30514 +               uint64_t        nent;   /* AuCtl_RDU_INO */
30515 +       };
30516 +       union au_rdu_ent_ul     ent;
30517 +       uint16_t                verify[AufsCtlRduV_End];
30518 +
30519 +       /* input/output */
30520 +       uint32_t                blk;
30521 +
30522 +       /* output */
30523 +       union au_rdu_ent_ul     tail;
30524 +       /* number of entries which were added in a single call */
30525 +       uint64_t                rent;
30526 +       uint8_t                 full;
30527 +       uint8_t                 shwh;
30528 +
30529 +       struct au_rdu_cookie    cookie;
30530 +} __aligned(8);
30531 +
30532 +/* ---------------------------------------------------------------------- */
30533 +
30534 +struct aufs_wbr_fd {
30535 +       uint32_t        oflags;
30536 +       int16_t         brid;
30537 +} __aligned(8);
30538 +
30539 +/* ---------------------------------------------------------------------- */
30540 +
30541 +struct aufs_ibusy {
30542 +       uint64_t        ino, h_ino;
30543 +       int16_t         bindex;
30544 +} __aligned(8);
30545 +
30546 +/* ---------------------------------------------------------------------- */
30547 +
30548 +#define AuCtlType              'A'
30549 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
30550 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
30551 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
30552 +                                    struct aufs_wbr_fd)
30553 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
30554 +
30555 +#endif /* __AUFS_TYPE_H__ */
30556
30557 --- linux-3.2/security/device_cgroup.c~ 2012-01-05 13:48:24.000000000 +0100
30558 +++ linux-3.2/security/device_cgroup.c  2012-01-05 14:02:48.910843473 +0100
30559 @@ -13,6 +13,7 @@
30560  #include <linux/slab.h>
30561  #include <linux/rcupdate.h>
30562  #include <linux/mutex.h>
30563 +#include <linux/module.h>
30564  
30565  #define ACC_MKNOD 1
30566  #define ACC_READ  2
This page took 2.246139 seconds and 4 git commands to generate.