]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- kernel-nf_rtsp.patch dropped (upstream provides module that can be built without...
[packages/kernel.git] / kernel-aufs3.patch
1 aufs3.x-rcN kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index d621f02..9b9694c 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 93804d4..cf3dcb9 100644
17 --- a/fs/Makefile
18 +++ b/fs/Makefile
19 @@ -124,3 +124,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 c94e717..fccb9df 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 e2ba628..fde8ea2 100644
40 --- a/fs/namei.c
41 +++ b/fs/namei.c
42 @@ -1781,7 +1781,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 1ec0493..c599f73 100644
53 --- a/fs/splice.c
54 +++ b/fs/splice.c
55 @@ -1084,8 +1084,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 @@ -1112,9 +1112,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 20002e3..6d792ad 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 d3ebdbe..6db6251 100644
123 --- a/fs/inode.c
124 +++ b/fs/inode.c
125 @@ -66,6 +66,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 fde8ea2..62f2302 100644
135 --- a/fs/namei.c
136 +++ b/fs/namei.c
137 @@ -1785,6 +1785,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 e608199..38fcc2e 100644
147 --- a/fs/namespace.c
148 +++ b/fs/namespace.c
149 @@ -1339,6 +1339,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 mount *mnt, struct mount *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 f104d56..54f36db 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 @@ -191,6 +192,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 @@ -278,6 +280,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 @@ -333,6 +336,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 77becc0..f634f02 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 c599f73..00303ba 100644
232 --- a/fs/splice.c
233 +++ b/fs/splice.c
234 @@ -1108,6 +1108,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 @@ -1134,6 +1135,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 7ce191e..19a95be 100644
252 --- a/security/commoncap.c
253 +++ b/security/commoncap.c
254 @@ -965,3 +965,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 8b5b5d8..911850c 100644
261 --- a/security/device_cgroup.c
262 +++ b/security/device_cgroup.c
263 @@ -7,6 +7,7 @@
264  #include <linux/device_cgroup.h>
265  #include <linux/cgroup.h>
266  #include <linux/ctype.h>
267 +#include <linux/export.h>
268  #include <linux/list.h>
269  #include <linux/uaccess.h>
270  #include <linux/seq_file.h>
271 @@ -501,6 +502,7 @@ found:
272  
273         return -EPERM;
274  }
275 +EXPORT_SYMBOL(__devcgroup_inode_permission);
276  
277  int devcgroup_inode_mknod(int mode, dev_t dev)
278  {
279 diff --git a/security/security.c b/security/security.c
280 index d754249..1aa6154 100644
281 --- a/security/security.c
282 +++ b/security/security.c
283 @@ -392,6 +392,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
284                 return 0;
285         return security_ops->path_rmdir(dir, dentry);
286  }
287 +EXPORT_SYMBOL(security_path_rmdir);
288  
289  int security_path_unlink(struct path *dir, struct dentry *dentry)
290  {
291 @@ -408,6 +409,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
292                 return 0;
293         return security_ops->path_symlink(dir, dentry, old_name);
294  }
295 +EXPORT_SYMBOL(security_path_symlink);
296  
297  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
298                        struct dentry *new_dentry)
299 @@ -416,6 +418,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
300                 return 0;
301         return security_ops->path_link(old_dentry, new_dir, new_dentry);
302  }
303 +EXPORT_SYMBOL(security_path_link);
304  
305  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
306                          struct path *new_dir, struct dentry *new_dentry)
307 @@ -434,6 +437,7 @@ int security_path_truncate(struct path *path)
308                 return 0;
309         return security_ops->path_truncate(path);
310  }
311 +EXPORT_SYMBOL(security_path_truncate);
312  
313  int security_path_chmod(struct path *path, umode_t mode)
314  {
315 @@ -441,6 +445,7 @@ int security_path_chmod(struct path *path, umode_t mode)
316                 return 0;
317         return security_ops->path_chmod(path, mode);
318  }
319 +EXPORT_SYMBOL(security_path_chmod);
320  
321  int security_path_chown(struct path *path, uid_t uid, gid_t gid)
322  {
323 @@ -448,6 +453,7 @@ int security_path_chown(struct path *path, uid_t uid, gid_t gid)
324                 return 0;
325         return security_ops->path_chown(path, uid, gid);
326  }
327 +EXPORT_SYMBOL(security_path_chown);
328  
329  int security_path_chroot(struct path *path)
330  {
331 @@ -524,6 +530,7 @@ int security_inode_readlink(struct dentry *dentry)
332                 return 0;
333         return security_ops->inode_readlink(dentry);
334  }
335 +EXPORT_SYMBOL(security_inode_readlink);
336  
337  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
338  {
339 @@ -538,6 +545,7 @@ int security_inode_permission(struct inode *inode, int mask)
340                 return 0;
341         return security_ops->inode_permission(inode, mask);
342  }
343 +EXPORT_SYMBOL(security_inode_permission);
344  
345  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
346  {
347 @@ -653,6 +661,7 @@ int security_file_permission(struct file *file, int mask)
348  
349         return fsnotify_perm(file, mask);
350  }
351 +EXPORT_SYMBOL(security_file_permission);
352  
353  int security_file_alloc(struct file *file)
354  {
355 @@ -680,6 +689,7 @@ int security_file_mmap(struct file *file, unsigned long reqprot,
356                 return ret;
357         return ima_file_mmap(file, prot);
358  }
359 +EXPORT_SYMBOL(security_file_mmap);
360  
361  int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
362                             unsigned long prot)
363 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
364 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
365 +++ linux/Documentation/ABI/testing/debugfs-aufs        2012-03-19 12:24:13.676467669 +0100
366 @@ -0,0 +1,37 @@
367 +What:          /debug/aufs/si_<id>/
368 +Date:          March 2009
369 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
370 +Description:
371 +               Under /debug/aufs, a directory named si_<id> is created
372 +               per aufs mount, where <id> is a unique id generated
373 +               internally.
374 +
375 +What:          /debug/aufs/si_<id>/xib
376 +Date:          March 2009
377 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
378 +Description:
379 +               It shows the consumed blocks by xib (External Inode Number
380 +               Bitmap), its block size and file size.
381 +               When the aufs mount option 'noxino' is specified, it
382 +               will be empty. About XINO files, see the aufs manual.
383 +
384 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
385 +Date:          March 2009
386 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
387 +Description:
388 +               It shows the consumed blocks by xino (External Inode Number
389 +               Translation Table), its link count, block size and file
390 +               size.
391 +               When the aufs mount option 'noxino' is specified, it
392 +               will be empty. About XINO files, see the aufs manual.
393 +
394 +What:          /debug/aufs/si_<id>/xigen
395 +Date:          March 2009
396 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
397 +Description:
398 +               It shows the consumed blocks by xigen (External Inode
399 +               Generation Table), its block size and file size.
400 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
401 +               be created.
402 +               When the aufs mount option 'noxino' is specified, it
403 +               will be empty. About XINO files, see the aufs manual.
404 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
405 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
406 +++ linux/Documentation/ABI/testing/sysfs-aufs  2012-03-19 12:24:13.676467669 +0100
407 @@ -0,0 +1,24 @@
408 +What:          /sys/fs/aufs/si_<id>/
409 +Date:          March 2009
410 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
411 +Description:
412 +               Under /sys/fs/aufs, a directory named si_<id> is created
413 +               per aufs mount, where <id> is a unique id generated
414 +               internally.
415 +
416 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
417 +Date:          March 2009
418 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
419 +Description:
420 +               It shows the abolute path of a member directory (which
421 +               is called branch) in aufs, and its permission.
422 +
423 +What:          /sys/fs/aufs/si_<id>/xi_path
424 +Date:          March 2009
425 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
426 +Description:
427 +               It shows the abolute path of XINO (External Inode Number
428 +               Bitmap, Translation Table and Generation Table) file
429 +               even if it is the default path.
430 +               When the aufs mount option 'noxino' is specified, it
431 +               will be empty. About XINO files, see the aufs manual.
432 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
433 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
434 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2012-03-19 12:24:13.676467669 +0100
435 @@ -0,0 +1,162 @@
436 +
437 +# Copyright (C) 2005-2011 Junjiro R. Okajima
438 +# 
439 +# This program is free software; you can redistribute it and/or modify
440 +# it under the terms of the GNU General Public License as published by
441 +# the Free Software Foundation; either version 2 of the License, or
442 +# (at your option) any later version.
443 +# 
444 +# This program is distributed in the hope that it will be useful,
445 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
446 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
447 +# GNU General Public License for more details.
448 +# 
449 +# You should have received a copy of the GNU General Public License
450 +# along with this program; if not, write to the Free Software
451 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
452 +
453 +Introduction
454 +----------------------------------------
455 +
456 +aufs [ei ju: ef es] | [a u f s]
457 +1. abbrev. for "advanced multi-layered unification filesystem".
458 +2. abbrev. for "another unionfs".
459 +3. abbrev. for "auf das" in German which means "on the" in English.
460 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
461 +   But "Filesystem aufs Filesystem" is hard to understand.
462 +
463 +AUFS is a filesystem with features:
464 +- multi layered stackable unification filesystem, the member directory
465 +  is called as a branch.
466 +- branch permission and attribute, 'readonly', 'real-readonly',
467 +  'readwrite', 'whiteout-able', 'link-able whiteout' and their
468 +  combination.
469 +- internal "file copy-on-write".
470 +- logical deletion, whiteout.
471 +- dynamic branch manipulation, adding, deleting and changing permission.
472 +- allow bypassing aufs, user's direct branch access.
473 +- external inode number translation table and bitmap which maintains the
474 +  persistent aufs inode number.
475 +- seekable directory, including NFS readdir.
476 +- file mapping, mmap and sharing pages.
477 +- pseudo-link, hardlink over branches.
478 +- loopback mounted filesystem as a branch.
479 +- several policies to select one among multiple writable branches.
480 +- revert a single systemcall when an error occurs in aufs.
481 +- and more...
482 +
483 +
484 +Multi Layered Stackable Unification Filesystem
485 +----------------------------------------------------------------------
486 +Most people already knows what it is.
487 +It is a filesystem which unifies several directories and provides a
488 +merged single directory. When users access a file, the access will be
489 +passed/re-directed/converted (sorry, I am not sure which English word is
490 +correct) to the real file on the member filesystem. The member
491 +filesystem is called 'lower filesystem' or 'branch' and has a mode
492 +'readonly' and 'readwrite.' And the deletion for a file on the lower
493 +readonly branch is handled by creating 'whiteout' on the upper writable
494 +branch.
495 +
496 +On LKML, there have been discussions about UnionMount (Jan Blunck,
497 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
498 +different approaches to implement the merged-view.
499 +The former tries putting it into VFS, and the latter implements as a
500 +separate filesystem.
501 +(If I misunderstand about these implementations, please let me know and
502 +I shall correct it. Because it is a long time ago when I read their
503 +source files last time).
504 +
505 +UnionMount's approach will be able to small, but may be hard to share
506 +branches between several UnionMount since the whiteout in it is
507 +implemented in the inode on branch filesystem and always
508 +shared. According to Bharata's post, readdir does not seems to be
509 +finished yet.
510 +There are several missing features known in this implementations such as
511 +- for users, the inode number may change silently. eg. copy-up.
512 +- link(2) may break by copy-up.
513 +- read(2) may get an obsoleted filedata (fstat(2) too).
514 +- fcntl(F_SETLK) may be broken by copy-up.
515 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
516 +  open(O_RDWR).
517 +
518 +Unionfs has a longer history. When I started implementing a stacking filesystem
519 +(Aug 2005), it already existed. It has virtual super_block, inode,
520 +dentry and file objects and they have an array pointing lower same kind
521 +objects. After contributing many patches for Unionfs, I re-started my
522 +project AUFS (Jun 2006).
523 +
524 +In AUFS, the structure of filesystem resembles to Unionfs, but I
525 +implemented my own ideas, approaches and enhancements and it became
526 +totally different one.
527 +
528 +Comparing DM snapshot and fs based implementation
529 +- the number of bytes to be copied between devices is much smaller.
530 +- the type of filesystem must be one and only.
531 +- the fs must be writable, no readonly fs, even for the lower original
532 +  device. so the compression fs will not be usable. but if we use
533 +  loopback mount, we may address this issue.
534 +  for instance,
535 +       mount /cdrom/squashfs.img /sq
536 +       losetup /sq/ext2.img
537 +       losetup /somewhere/cow
538 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
539 +- it will be difficult (or needs more operations) to extract the
540 +  difference between the original device and COW.
541 +- DM snapshot-merge may help a lot when users try merging. in the
542 +  fs-layer union, users will use rsync(1).
543 +
544 +
545 +Several characters/aspects of aufs
546 +----------------------------------------------------------------------
547 +
548 +Aufs has several characters or aspects.
549 +1. a filesystem, callee of VFS helper
550 +2. sub-VFS, caller of VFS helper for branches
551 +3. a virtual filesystem which maintains persistent inode number
552 +4. reader/writer of files on branches such like an application
553 +
554 +1. Callee of VFS Helper
555 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
556 +unlink(2) from an application reaches sys_unlink() kernel function and
557 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
558 +calls filesystem specific unlink operation. Actually aufs implements the
559 +unlink operation but it behaves like a redirector.
560 +
561 +2. Caller of VFS Helper for Branches
562 +aufs_unlink() passes the unlink request to the branch filesystem as if
563 +it were called from VFS. So the called unlink operation of the branch
564 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
565 +every necessary pre/post operation for the branch filesystem.
566 +- acquire the lock for the parent dir on a branch
567 +- lookup in a branch
568 +- revalidate dentry on a branch
569 +- mnt_want_write() for a branch
570 +- vfs_unlink() for a branch
571 +- mnt_drop_write() for a branch
572 +- release the lock on a branch
573 +
574 +3. Persistent Inode Number
575 +One of the most important issue for a filesystem is to maintain inode
576 +numbers. This is particularly important to support exporting a
577 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
578 +backend block device for its own. But some storage is necessary to
579 +maintain inode number. It may be a large space and may not suit to keep
580 +in memory. Aufs rents some space from its first writable branch
581 +filesystem (by default) and creates file(s) on it. These files are
582 +created by aufs internally and removed soon (currently) keeping opened.
583 +Note: Because these files are removed, they are totally gone after
584 +      unmounting aufs. It means the inode numbers are not persistent
585 +      across unmount or reboot. I have a plan to make them really
586 +      persistent which will be important for aufs on NFS server.
587 +
588 +4. Read/Write Files Internally (copy-on-write)
589 +Because a branch can be readonly, when you write a file on it, aufs will
590 +"copy-up" it to the upper writable branch internally. And then write the
591 +originally requested thing to the file. Generally kernel doesn't
592 +open/read/write file actively. In aufs, even a single write may cause a
593 +internal "file copy". This behaviour is very similar to cp(1) command.
594 +
595 +Some people may think it is better to pass such work to user space
596 +helper, instead of doing in kernel space. Actually I am still thinking
597 +about it. But currently I have implemented it in kernel space.
598 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
599 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
600 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2012-03-19 12:24:13.676467669 +0100
601 @@ -0,0 +1,226 @@
602 +
603 +# Copyright (C) 2005-2011 Junjiro R. Okajima
604 +# 
605 +# This program is free software; you can redistribute it and/or modify
606 +# it under the terms of the GNU General Public License as published by
607 +# the Free Software Foundation; either version 2 of the License, or
608 +# (at your option) any later version.
609 +# 
610 +# This program is distributed in the hope that it will be useful,
611 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
612 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
613 +# GNU General Public License for more details.
614 +# 
615 +# You should have received a copy of the GNU General Public License
616 +# along with this program; if not, write to the Free Software
617 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
618 +
619 +Basic Aufs Internal Structure
620 +
621 +Superblock/Inode/Dentry/File Objects
622 +----------------------------------------------------------------------
623 +As like an ordinary filesystem, aufs has its own
624 +superblock/inode/dentry/file objects. All these objects have a
625 +dynamically allocated array and store the same kind of pointers to the
626 +lower filesystem, branch.
627 +For example, when you build a union with one readwrite branch and one
628 +readonly, mounted /au, /rw and /ro respectively.
629 +- /au = /rw + /ro
630 +- /ro/fileA exists but /rw/fileA
631 +
632 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
633 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
634 +- [0] = NULL
635 +- [1] = /ro/fileA
636 +
637 +This style of an array is essentially same to the aufs
638 +superblock/inode/dentry/file objects.
639 +
640 +Because aufs supports manipulating branches, ie. add/delete/change
641 +dynamically, these objects has its own generation. When branches are
642 +changed, the generation in aufs superblock is incremented. And a
643 +generation in other object are compared when it is accessed.
644 +When a generation in other objects are obsoleted, aufs refreshes the
645 +internal array.
646 +
647 +
648 +Superblock
649 +----------------------------------------------------------------------
650 +Additionally aufs superblock has some data for policies to select one
651 +among multiple writable branches, XIB files, pseudo-links and kobject.
652 +See below in detail.
653 +About the policies which supports copy-down a directory, see policy.txt
654 +too.
655 +
656 +
657 +Branch and XINO(External Inode Number Translation Table)
658 +----------------------------------------------------------------------
659 +Every branch has its own xino (external inode number translation table)
660 +file. The xino file is created and unlinked by aufs internally. When two
661 +members of a union exist on the same filesystem, they share the single
662 +xino file.
663 +The struct of a xino file is simple, just a sequence of aufs inode
664 +numbers which is indexed by the lower inode number.
665 +In the above sample, assume the inode number of /ro/fileA is i111 and
666 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
667 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
668 +
669 +When the inode numbers are not contiguous, the xino file will be sparse
670 +which has a hole in it and doesn't consume as much disk space as it
671 +might appear. If your branch filesystem consumes disk space for such
672 +holes, then you should specify 'xino=' option at mounting aufs.
673 +
674 +Also a writable branch has three kinds of "whiteout bases". All these
675 +are existed when the branch is joined to aufs and the names are
676 +whiteout-ed doubly, so that users will never see their names in aufs
677 +hierarchy.
678 +1. a regular file which will be linked to all whiteouts.
679 +2. a directory to store a pseudo-link.
680 +3. a directory to store an "orphan-ed" file temporary.
681 +
682 +1. Whiteout Base
683 +   When you remove a file on a readonly branch, aufs handles it as a
684 +   logical deletion and creates a whiteout on the upper writable branch
685 +   as a hardlink of this file in order not to consume inode on the
686 +   writable branch.
687 +2. Pseudo-link Dir
688 +   See below, Pseudo-link.
689 +3. Step-Parent Dir
690 +   When "fileC" exists on the lower readonly branch only and it is
691 +   opened and removed with its parent dir, and then user writes
692 +   something into it, then aufs copies-up fileC to this
693 +   directory. Because there is no other dir to store fileC. After
694 +   creating a file under this dir, the file is unlinked.
695 +
696 +Because aufs supports manipulating branches, ie. add/delete/change
697 +dynamically, a branch has its own id. When the branch order changes, aufs
698 +finds the new index by searching the branch id.
699 +
700 +
701 +Pseudo-link
702 +----------------------------------------------------------------------
703 +Assume "fileA" exists on the lower readonly branch only and it is
704 +hardlinked to "fileB" on the branch. When you write something to fileA,
705 +aufs copies-up it to the upper writable branch. Additionally aufs
706 +creates a hardlink under the Pseudo-link Directory of the writable
707 +branch. The inode of a pseudo-link is kept in aufs super_block as a
708 +simple list. If fileB is read after unlinking fileA, aufs returns
709 +filedata from the pseudo-link instead of the lower readonly
710 +branch. Because the pseudo-link is based upon the inode, to keep the
711 +inode number by xino (see above) is important.
712 +
713 +All the hardlinks under the Pseudo-link Directory of the writable branch
714 +should be restored in a proper location later. Aufs provides a utility
715 +to do this. The userspace helpers executed at remounting and unmounting
716 +aufs by default.
717 +During this utility is running, it puts aufs into the pseudo-link
718 +maintenance mode. In this mode, only the process which began the
719 +maintenance mode (and its child processes) is allowed to operate in
720 +aufs. Some other processes which are not related to the pseudo-link will
721 +be allowed to run too, but the rest have to return an error or wait
722 +until the maintenance mode ends. If a process already acquires an inode
723 +mutex (in VFS), it has to return an error.
724 +
725 +
726 +XIB(external inode number bitmap)
727 +----------------------------------------------------------------------
728 +Addition to the xino file per a branch, aufs has an external inode number
729 +bitmap in a superblock object. It is also a file such like a xino file.
730 +It is a simple bitmap to mark whether the aufs inode number is in-use or
731 +not.
732 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
733 +
734 +Aufs implements a feature to truncate/refresh both of xino and xib to
735 +reduce the number of consumed disk blocks for these files.
736 +
737 +
738 +Virtual or Vertical Dir, and Readdir in Userspace
739 +----------------------------------------------------------------------
740 +In order to support multiple layers (branches), aufs readdir operation
741 +constructs a virtual dir block on memory. For readdir, aufs calls
742 +vfs_readdir() internally for each dir on branches, merges their entries
743 +with eliminating the whiteout-ed ones, and sets it to file (dir)
744 +object. So the file object has its entry list until it is closed. The
745 +entry list will be updated when the file position is zero and becomes
746 +old. This decision is made in aufs automatically.
747 +
748 +The dynamically allocated memory block for the name of entries has a
749 +unit of 512 bytes (by default) and stores the names contiguously (no
750 +padding). Another block for each entry is handled by kmem_cache too.
751 +During building dir blocks, aufs creates hash list and judging whether
752 +the entry is whiteouted by its upper branch or already listed.
753 +The merged result is cached in the corresponding inode object and
754 +maintained by a customizable life-time option.
755 +
756 +Some people may call it can be a security hole or invite DoS attack
757 +since the opened and once readdir-ed dir (file object) holds its entry
758 +list and becomes a pressure for system memory. But I'd say it is similar
759 +to files under /proc or /sys. The virtual files in them also holds a
760 +memory page (generally) while they are opened. When an idea to reduce
761 +memory for them is introduced, it will be applied to aufs too.
762 +For those who really hate this situation, I've developed readdir(3)
763 +library which operates this merging in userspace. You just need to set
764 +LD_PRELOAD environment variable, and aufs will not consume no memory in
765 +kernel space for readdir(3).
766 +
767 +
768 +Workqueue
769 +----------------------------------------------------------------------
770 +Aufs sometimes requires privilege access to a branch. For instance,
771 +in copy-up/down operation. When a user process is going to make changes
772 +to a file which exists in the lower readonly branch only, and the mode
773 +of one of ancestor directories may not be writable by a user
774 +process. Here aufs copy-up the file with its ancestors and they may
775 +require privilege to set its owner/group/mode/etc.
776 +This is a typical case of a application character of aufs (see
777 +Introduction).
778 +
779 +Aufs uses workqueue synchronously for this case. It creates its own
780 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
781 +passes the request to call mkdir or write (for example), and wait for
782 +its completion. This approach solves a problem of a signal handler
783 +simply.
784 +If aufs didn't adopt the workqueue and changed the privilege of the
785 +process, and if the mkdir/write call arises SIGXFSZ or other signal,
786 +then the user process might gain a privilege or the generated core file
787 +was owned by a superuser.
788 +
789 +Also aufs uses the system global workqueue ("events" kernel thread) too
790 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
791 +whiteout base and etc. This is unrelated to a privilege.
792 +Most of aufs operation tries acquiring a rw_semaphore for aufs
793 +superblock at the beginning, at the same time waits for the completion
794 +of all queued asynchronous tasks.
795 +
796 +
797 +Whiteout
798 +----------------------------------------------------------------------
799 +The whiteout in aufs is very similar to Unionfs's. That is represented
800 +by its filename. UnionMount takes an approach of a file mode, but I am
801 +afraid several utilities (find(1) or something) will have to support it.
802 +
803 +Basically the whiteout represents "logical deletion" which stops aufs to
804 +lookup further, but also it represents "dir is opaque" which also stop
805 +lookup.
806 +
807 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
808 +In order to make several functions in a single systemcall to be
809 +revertible, aufs adopts an approach to rename a directory to a temporary
810 +unique whiteouted name.
811 +For example, in rename(2) dir where the target dir already existed, aufs
812 +renames the target dir to a temporary unique whiteouted name before the
813 +actual rename on a branch and then handles other actions (make it opaque,
814 +update the attributes, etc). If an error happens in these actions, aufs
815 +simply renames the whiteouted name back and returns an error. If all are
816 +succeeded, aufs registers a function to remove the whiteouted unique
817 +temporary name completely and asynchronously to the system global
818 +workqueue.
819 +
820 +
821 +Copy-up
822 +----------------------------------------------------------------------
823 +It is a well-known feature or concept.
824 +When user modifies a file on a readonly branch, aufs operate "copy-up"
825 +internally and makes change to the new file on the upper writable branch.
826 +When the trigger systemcall does not update the timestamps of the parent
827 +dir, aufs reverts it after copy-up.
828 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
829 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
830 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2012-03-19 12:24:13.676467669 +0100
831 @@ -0,0 +1,106 @@
832 +
833 +# Copyright (C) 2005-2011 Junjiro R. Okajima
834 +# 
835 +# This program is free software; you can redistribute it and/or modify
836 +# it under the terms of the GNU General Public License as published by
837 +# the Free Software Foundation; either version 2 of the License, or
838 +# (at your option) any later version.
839 +# 
840 +# This program is distributed in the hope that it will be useful,
841 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
842 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
843 +# GNU General Public License for more details.
844 +# 
845 +# You should have received a copy of the GNU General Public License
846 +# along with this program; if not, write to the Free Software
847 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
848 +
849 +Lookup in a Branch
850 +----------------------------------------------------------------------
851 +Since aufs has a character of sub-VFS (see Introduction), it operates
852 +lookup for branches as VFS does. It may be a heavy work. Generally
853 +speaking struct nameidata is a bigger structure and includes many
854 +information. But almost all lookup operation in aufs is the simplest
855 +case, ie. lookup only an entry directly connected to its parent. Digging
856 +down the directory hierarchy is unnecessary.
857 +
858 +VFS has a function lookup_one_len() for that use, but it is not usable
859 +for a branch filesystem which requires struct nameidata. So aufs
860 +implements a simple lookup wrapper function. When a branch filesystem
861 +allows NULL as nameidata, it calls lookup_one_len(). Otherwise it builds
862 +a simplest nameidata and calls lookup_hash().
863 +Here aufs applies "a principle in NFSD", ie. if the filesystem supports
864 +NFS-export, then it has to support NULL as a nameidata parameter for
865 +->create(), ->lookup() and ->d_revalidate(). So the lookup wrapper in
866 +aufs tests if ->s_export_op in the branch is NULL or not.
867 +
868 +When a branch is a remote filesystem, aufs basically trusts its
869 +->d_revalidate(), also aufs forces the hardest revalidate tests for
870 +them.
871 +For d_revalidate, aufs implements three levels of revalidate tests. See
872 +"Revalidate Dentry and UDBA" in detail.
873 +
874 +
875 +Loopback Mount
876 +----------------------------------------------------------------------
877 +Basically aufs supports any type of filesystem and block device for a
878 +branch (actually there are some exceptions). But it is prohibited to add
879 +a loopback mounted one whose backend file exists in a filesystem which is
880 +already added to aufs. The reason is to protect aufs from a recursive
881 +lookup. If it was allowed, the aufs lookup operation might re-enter a
882 +lookup for the loopback mounted branch in the same context, and will
883 +cause a deadlock.
884 +
885 +
886 +Revalidate Dentry and UDBA (User's Direct Branch Access)
887 +----------------------------------------------------------------------
888 +Generally VFS helpers re-validate a dentry as a part of lookup.
889 +0. digging down the directory hierarchy.
890 +1. lock the parent dir by its i_mutex.
891 +2. lookup the final (child) entry.
892 +3. revalidate it.
893 +4. call the actual operation (create, unlink, etc.)
894 +5. unlock the parent dir
895 +
896 +If the filesystem implements its ->d_revalidate() (step 3), then it is
897 +called. Actually aufs implements it and checks the dentry on a branch is
898 +still valid.
899 +But it is not enough. Because aufs has to release the lock for the
900 +parent dir on a branch at the end of ->lookup() (step 2) and
901 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
902 +held by VFS.
903 +If the file on a branch is changed directly, eg. bypassing aufs, after
904 +aufs released the lock, then the subsequent operation may cause
905 +something unpleasant result.
906 +
907 +This situation is a result of VFS architecture, ->lookup() and
908 +->d_revalidate() is separated. But I never say it is wrong. It is a good
909 +design from VFS's point of view. It is just not suitable for sub-VFS
910 +character in aufs.
911 +
912 +Aufs supports such case by three level of revalidation which is
913 +selectable by user.
914 +1. Simple Revalidate
915 +   Addition to the native flow in VFS's, confirm the child-parent
916 +   relationship on the branch just after locking the parent dir on the
917 +   branch in the "actual operation" (step 4). When this validation
918 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
919 +   checks the validation of the dentry on branches.
920 +2. Monitor Changes Internally by Inotify/Fsnotify
921 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
922 +   the dentry on the branch, and returns EBUSY if it finds different
923 +   dentry.
924 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
925 +   during it is in cache. When the event is notified, aufs registers a
926 +   function to kernel 'events' thread by schedule_work(). And the
927 +   function sets some special status to the cached aufs dentry and inode
928 +   private data. If they are not cached, then aufs has nothing to
929 +   do. When the same file is accessed through aufs (step 0-3) later,
930 +   aufs will detect the status and refresh all necessary data.
931 +   In this mode, aufs has to ignore the event which is fired by aufs
932 +   itself.
933 +3. No Extra Validation
934 +   This is the simplest test and doesn't add any additional revalidation
935 +   test, and skip therevalidatin in step 4. It is useful and improves
936 +   aufs performance when system surely hide the aufs branches from user,
937 +   by over-mounting something (or another method).
938 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
939 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
940 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2012-03-19 12:24:13.676467669 +0100
941 @@ -0,0 +1,76 @@
942 +
943 +# Copyright (C) 2005-2011 Junjiro R. Okajima
944 +# 
945 +# This program is free software; you can redistribute it and/or modify
946 +# it under the terms of the GNU General Public License as published by
947 +# the Free Software Foundation; either version 2 of the License, or
948 +# (at your option) any later version.
949 +# 
950 +# This program is distributed in the hope that it will be useful,
951 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
952 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
953 +# GNU General Public License for more details.
954 +# 
955 +# You should have received a copy of the GNU General Public License
956 +# along with this program; if not, write to the Free Software
957 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
958 +
959 +Branch Manipulation
960 +
961 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
962 +and changing its permission/attribute, there are a lot of works to do.
963 +
964 +
965 +Add a Branch
966 +----------------------------------------------------------------------
967 +o Confirm the adding dir exists outside of aufs, including loopback
968 +  mount.
969 +- and other various attributes...
970 +o Initialize the xino file and whiteout bases if necessary.
971 +  See struct.txt.
972 +
973 +o Check the owner/group/mode of the directory
974 +  When the owner/group/mode of the adding directory differs from the
975 +  existing branch, aufs issues a warning because it may impose a
976 +  security risk.
977 +  For example, when a upper writable branch has a world writable empty
978 +  top directory, a malicious user can create any files on the writable
979 +  branch directly, like copy-up and modify manually. If something like
980 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
981 +  writable branch, and the writable branch is world-writable, then a
982 +  malicious guy may create /etc/passwd on the writable branch directly
983 +  and the infected file will be valid in aufs.
984 +  I am afraid it can be a security issue, but nothing to do except
985 +  producing a warning.
986 +
987 +
988 +Delete a Branch
989 +----------------------------------------------------------------------
990 +o Confirm the deleting branch is not busy
991 +  To be general, there is one merit to adopt "remount" interface to
992 +  manipulate branches. It is to discard caches. At deleting a branch,
993 +  aufs checks the still cached (and connected) dentries and inodes. If
994 +  there are any, then they are all in-use. An inode without its
995 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
996 +
997 +  For the cached one, aufs checks whether the same named entry exists on
998 +  other branches.
999 +  If the cached one is a directory, because aufs provides a merged view
1000 +  to users, as long as one dir is left on any branch aufs can show the
1001 +  dir to users. In this case, the branch can be removed from aufs.
1002 +  Otherwise aufs rejects deleting the branch.
1003 +
1004 +  If any file on the deleting branch is opened by aufs, then aufs
1005 +  rejects deleting.
1006 +
1007 +
1008 +Modify the Permission of a Branch
1009 +----------------------------------------------------------------------
1010 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1011 +  See struct.txt.
1012 +
1013 +o rw --> ro: Confirm the modifying branch is not busy
1014 +  Aufs rejects the request if any of these conditions are true.
1015 +  - a file on the branch is mmap-ed.
1016 +  - a regular file on the branch is opened for write and there is no
1017 +    same named entry on the upper branch.
1018 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1019 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1020 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2012-03-19 12:24:13.676467669 +0100
1021 @@ -0,0 +1,65 @@
1022 +
1023 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1024 +# 
1025 +# This program is free software; you can redistribute it and/or modify
1026 +# it under the terms of the GNU General Public License as published by
1027 +# the Free Software Foundation; either version 2 of the License, or
1028 +# (at your option) any later version.
1029 +# 
1030 +# This program is distributed in the hope that it will be useful,
1031 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1032 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1033 +# GNU General Public License for more details.
1034 +# 
1035 +# You should have received a copy of the GNU General Public License
1036 +# along with this program; if not, write to the Free Software
1037 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1038 +
1039 +Policies to Select One among Multiple Writable Branches
1040 +----------------------------------------------------------------------
1041 +When the number of writable branch is more than one, aufs has to decide
1042 +the target branch for file creation or copy-up. By default, the highest
1043 +writable branch which has the parent (or ancestor) dir of the target
1044 +file is chosen (top-down-parent policy).
1045 +By user's request, aufs implements some other policies to select the
1046 +writable branch, for file creation two policies, round-robin and
1047 +most-free-space policies. For copy-up three policies, top-down-parent,
1048 +bottom-up-parent and bottom-up policies.
1049 +
1050 +As expected, the round-robin policy selects the branch in circular. When
1051 +you have two writable branches and creates 10 new files, 5 files will be
1052 +created for each branch. mkdir(2) systemcall is an exception. When you
1053 +create 10 new directories, all will be created on the same branch.
1054 +And the most-free-space policy selects the one which has most free
1055 +space among the writable branches. The amount of free space will be
1056 +checked by aufs internally, and users can specify its time interval.
1057 +
1058 +The policies for copy-up is more simple,
1059 +top-down-parent is equivalent to the same named on in create policy,
1060 +bottom-up-parent selects the writable branch where the parent dir
1061 +exists and the nearest upper one from the copyup-source,
1062 +bottom-up selects the nearest upper writable branch from the
1063 +copyup-source, regardless the existence of the parent dir.
1064 +
1065 +There are some rules or exceptions to apply these policies.
1066 +- If there is a readonly branch above the policy-selected branch and
1067 +  the parent dir is marked as opaque (a variation of whiteout), or the
1068 +  target (creating) file is whiteout-ed on the upper readonly branch,
1069 +  then the result of the policy is ignored and the target file will be
1070 +  created on the nearest upper writable branch than the readonly branch.
1071 +- If there is a writable branch above the policy-selected branch and
1072 +  the parent dir is marked as opaque or the target file is whiteouted
1073 +  on the branch, then the result of the policy is ignored and the target
1074 +  file will be created on the highest one among the upper writable
1075 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1076 +  it as usual.
1077 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1078 +  They try selecting the branch where the source exists as possible
1079 +  since copyup a large file will take long time. If it can't be,
1080 +  ie. the branch where the source exists is readonly, then they will
1081 +  follow the copyup policy.
1082 +- There is an exception for rename(2) when the target exists.
1083 +  If the rename target exists, aufs compares the index of the branches
1084 +  where the source and the target exists and selects the higher
1085 +  one. If the selected branch is readonly, then aufs follows the
1086 +  copyup policy.
1087 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1088 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1089 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2012-03-19 12:24:13.676467669 +0100
1090 @@ -0,0 +1,47 @@
1091 +
1092 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1093 +# 
1094 +# This program is free software; you can redistribute it and/or modify
1095 +# it under the terms of the GNU General Public License as published by
1096 +# the Free Software Foundation; either version 2 of the License, or
1097 +# (at your option) any later version.
1098 +# 
1099 +# This program is distributed in the hope that it will be useful,
1100 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1101 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1102 +# GNU General Public License for more details.
1103 +# 
1104 +# You should have received a copy of the GNU General Public License
1105 +# along with this program; if not, write to the Free Software
1106 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1107 +
1108 +mmap(2) -- File Memory Mapping
1109 +----------------------------------------------------------------------
1110 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1111 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1112 +->mmap().
1113 +This approach is simple and good, but there is one problem.
1114 +Under /proc, several entries show the mmap-ped files by its path (with
1115 +device and inode number), and the printed path will be the path on the
1116 +branch fs's instead of virtual aufs's.
1117 +This is not a problem in most cases, but some utilities lsof(1) (and its
1118 +user) may expect the path on aufs.
1119 +
1120 +To address this issue, aufs adds a new member called vm_prfile in struct
1121 +vm_area_struct (and struct vm_region). The original vm_file points to
1122 +the file on the branch fs in order to handle everything correctly as
1123 +usual. The new vm_prfile points to a virtual file in aufs, and the
1124 +show-functions in procfs refers to vm_prfile if it is set.
1125 +Also we need to maintain several other places where touching vm_file
1126 +such like
1127 +- fork()/clone() copies vma and the reference count of vm_file is
1128 +  incremented.
1129 +- merging vma maintains the ref count too.
1130 +
1131 +This is not a good approach. It just faking the printed path. But it
1132 +leaves all behaviour around f_mapping unchanged. This is surely an
1133 +advantage.
1134 +Actually aufs had adopted another complicated approach which calls
1135 +generic_file_mmap() and handles struct vm_operations_struct. In this
1136 +approach, aufs met a hard problem and I could not solve it without
1137 +switching the approach.
1138 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
1139 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1140 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2012-03-19 12:24:13.676467669 +0100
1141 @@ -0,0 +1,59 @@
1142 +
1143 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1144 +# 
1145 +# This program is free software; you can redistribute it and/or modify
1146 +# it under the terms of the GNU General Public License as published by
1147 +# the Free Software Foundation; either version 2 of the License, or
1148 +# (at your option) any later version.
1149 +# 
1150 +# This program is distributed in the hope that it will be useful,
1151 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1152 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1153 +# GNU General Public License for more details.
1154 +# 
1155 +# You should have received a copy of the GNU General Public License
1156 +# along with this program; if not, write to the Free Software
1157 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1158 +
1159 +Export Aufs via NFS
1160 +----------------------------------------------------------------------
1161 +Here is an approach.
1162 +- like xino/xib, add a new file 'xigen' which stores aufs inode
1163 +  generation.
1164 +- iget_locked(): initialize aufs inode generation for a new inode, and
1165 +  store it in xigen file.
1166 +- destroy_inode(): increment aufs inode generation and store it in xigen
1167 +  file. it is necessary even if it is not unlinked, because any data of
1168 +  inode may be changed by UDBA.
1169 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
1170 +  build file handle by
1171 +  + branch id (4 bytes)
1172 +  + superblock generation (4 bytes)
1173 +  + inode number (4 or 8 bytes)
1174 +  + parent dir inode number (4 or 8 bytes)
1175 +  + inode generation (4 bytes))
1176 +  + return value of exportfs_encode_fh() for the parent on a branch (4
1177 +    bytes)
1178 +  + file handle for a branch (by exportfs_encode_fh())
1179 +- fh_to_dentry():
1180 +  + find the index of a branch from its id in handle, and check it is
1181 +    still exist in aufs.
1182 +  + 1st level: get the inode number from handle and search it in cache.
1183 +  + 2nd level: if not found, get the parent inode number from handle and
1184 +    search it in cache. and then open the parent dir, find the matching
1185 +    inode number by vfs_readdir() and get its name, and call
1186 +    lookup_one_len() for the target dentry.
1187 +  + 3rd level: if the parent dir is not cached, call
1188 +    exportfs_decode_fh() for a branch and get the parent on a branch,
1189 +    build a pathname of it, convert it a pathname in aufs, call
1190 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
1191 +    the 2nd level.
1192 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
1193 +    for every branch, but not itself. to get this, (currently) aufs
1194 +    searches in current->nsproxy->mnt_ns list. it may not be a good
1195 +    idea, but I didn't get other approach.
1196 +  + test the generation of the gotten inode.
1197 +- every inode operation: they may get EBUSY due to UDBA. in this case,
1198 +  convert it into ESTALE for NFSD.
1199 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
1200 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
1201 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
1202 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
1203 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2012-03-19 12:24:13.676467669 +0100
1204 @@ -0,0 +1,53 @@
1205 +
1206 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1207 +# 
1208 +# This program is free software; you can redistribute it and/or modify
1209 +# it under the terms of the GNU General Public License as published by
1210 +# the Free Software Foundation; either version 2 of the License, or
1211 +# (at your option) any later version.
1212 +# 
1213 +# This program is distributed in the hope that it will be useful,
1214 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1215 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1216 +# GNU General Public License for more details.
1217 +# 
1218 +# You should have received a copy of the GNU General Public License
1219 +# along with this program; if not, write to the Free Software
1220 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1221 +
1222 +Show Whiteout Mode (shwh)
1223 +----------------------------------------------------------------------
1224 +Generally aufs hides the name of whiteouts. But in some cases, to show
1225 +them is very useful for users. For instance, creating a new middle layer
1226 +(branch) by merging existing layers.
1227 +
1228 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
1229 +When you have three branches,
1230 +- Bottom: 'system', squashfs (underlying base system), read-only
1231 +- Middle: 'mods', squashfs, read-only
1232 +- Top: 'overlay', ram (tmpfs), read-write
1233 +
1234 +The top layer is loaded at boot time and saved at shutdown, to preserve
1235 +the changes made to the system during the session.
1236 +When larger changes have been made, or smaller changes have accumulated,
1237 +the size of the saved top layer data grows. At this point, it would be
1238 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
1239 +and rewrite the 'mods' squashfs, clearing the top layer and thus
1240 +restoring save and load speed.
1241 +
1242 +This merging is simplified by the use of another aufs mount, of just the
1243 +two overlay branches using the 'shwh' option.
1244 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
1245 +       aufs /livesys/merge_union
1246 +
1247 +A merged view of these two branches is then available at
1248 +/livesys/merge_union, and the new feature is that the whiteouts are
1249 +visible!
1250 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
1251 +writing to all branches. Also the default mode for all branches is 'ro'.
1252 +It is now possible to save the combined contents of the two overlay
1253 +branches to a new squashfs, e.g.:
1254 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
1255 +
1256 +This new squashfs archive can be stored on the boot device and the
1257 +initramfs will use it to replace the old one at the next boot.
1258 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
1259 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
1260 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2012-03-19 12:24:13.676467669 +0100
1261 @@ -0,0 +1,47 @@
1262 +
1263 +# Copyright (C) 2010-2011 Junjiro R. Okajima
1264 +#
1265 +# This program is free software; you can redistribute it and/or modify
1266 +# it under the terms of the GNU General Public License as published by
1267 +# the Free Software Foundation; either version 2 of the License, or
1268 +# (at your option) any later version.
1269 +#
1270 +# This program is distributed in the hope that it will be useful,
1271 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1272 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1273 +# GNU General Public License for more details.
1274 +#
1275 +# You should have received a copy of the GNU General Public License
1276 +# along with this program; if not, write to the Free Software
1277 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1278 +
1279 +Dynamically customizable FS operations
1280 +----------------------------------------------------------------------
1281 +Generally FS operations (struct inode_operations, struct
1282 +address_space_operations, struct file_operations, etc.) are defined as
1283 +"static const", but it never means that FS have only one set of
1284 +operation. Some FS have multiple sets of them. For instance, ext2 has
1285 +three sets, one for XIP, for NOBH, and for normal.
1286 +Since aufs overrides and redirects these operations, sometimes aufs has
1287 +to change its behaviour according to the branch FS type. More imporantly
1288 +VFS acts differently if a function (member in the struct) is set or
1289 +not. It means aufs should have several sets of operations and select one
1290 +among them according to the branch FS definition.
1291 +
1292 +In order to solve this problem and not to affect the behavour of VFS,
1293 +aufs defines these operations dynamically. For instance, aufs defines
1294 +aio_read function for struct file_operations, but it may not be set to
1295 +the file_operations. When the branch FS doesn't have it, aufs doesn't
1296 +set it to its file_operations while the function definition itself is
1297 +still alive. So the behaviour of io_submit(2) will not change, and it
1298 +will return an error when aio_read is not defined.
1299 +
1300 +The lifetime of these dynamically generated operation object is
1301 +maintained by aufs branch object. When the branch is removed from aufs,
1302 +the reference counter of the object is decremented. When it reaches
1303 +zero, the dynamically generated operation object will be freed.
1304 +
1305 +This approach is designed to support AIO (io_submit), Direcit I/O and
1306 +XIP mainly.
1307 +Currently this approach is applied to file_operations and
1308 +vm_operations_struct for regular files only.
1309 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
1310 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
1311 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2012-03-19 12:24:13.676467669 +0100
1312 @@ -0,0 +1,96 @@
1313 +
1314 +# Copyright (C) 2005-2011 Junjiro R. Okajima
1315 +# 
1316 +# This program is free software; you can redistribute it and/or modify
1317 +# it under the terms of the GNU General Public License as published by
1318 +# the Free Software Foundation; either version 2 of the License, or
1319 +# (at your option) any later version.
1320 +# 
1321 +# This program is distributed in the hope that it will be useful,
1322 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1323 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1324 +# GNU General Public License for more details.
1325 +# 
1326 +# You should have received a copy of the GNU General Public License
1327 +# along with this program; if not, write to the Free Software
1328 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1329 +
1330 +Plan
1331 +
1332 +Restoring some features which was implemented in aufs1.
1333 +They were dropped in aufs2 in order to make source files simpler and
1334 +easier to be reviewed.
1335 +
1336 +
1337 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1338 +----------------------------------------------------------------------
1339 +Let's try case study.
1340 +- aufs has two branches, upper readwrite and lower readonly.
1341 +  /au = /rw + /ro
1342 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1343 +- user invoked "chmod a+rx /au/dirA"
1344 +- then "dirA" becomes world readable?
1345 +
1346 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1347 +or it may be a natively readonly filesystem. If aufs respects the lower
1348 +branch, it should not respond readdir request from other users. But user
1349 +allowed it by chmod. Should really aufs rejects showing the entries
1350 +under /ro/dirA?
1351 +
1352 +To be honest, I don't have a best solution for this case. So I
1353 +implemented 'dirperm1' and 'nodirperm1' option in aufs1, and leave it to
1354 +users.
1355 +When dirperm1 is specified, aufs checks only the highest one for the
1356 +directory permission, and shows the entries. Otherwise, as usual, checks
1357 +every dir existing on all branches and rejects the request.
1358 +
1359 +As a side effect, dirperm1 option improves the performance of aufs
1360 +because the number of permission check is reduced.
1361 +
1362 +
1363 +Being Another Aufs's Readonly Branch (robr)
1364 +----------------------------------------------------------------------
1365 +Aufs1 allows aufs to be another aufs's readonly branch.
1366 +This feature was developed by a user's request. But it may not be used
1367 +currecnly.
1368 +
1369 +
1370 +Copy-up on Open (coo=)
1371 +----------------------------------------------------------------------
1372 +By default the internal copy-up is executed when it is really necessary.
1373 +It is not done when a file is opened for writing, but when write(2) is
1374 +done. Users who have many (over 100) branches want to know and analyse
1375 +when and what file is copied-up. To insert a new upper branch which
1376 +contains such files only may improve the performance of aufs.
1377 +
1378 +Aufs1 implemented "coo=none | leaf | all" option.
1379 +
1380 +
1381 +Refresh the Opened File (refrof)
1382 +----------------------------------------------------------------------
1383 +This option is implemented in aufs1 but incomplete.
1384 +
1385 +When user reads from a file, he expects to get its latest filedata
1386 +generally. If the file is removed and a new same named file is created,
1387 +the content he gets is unchanged, ie. the unlinked filedata.
1388 +
1389 +Let's try case study again.
1390 +- aufs has two branches.
1391 +  /au = /rw + /ro
1392 +- "fileA" exists under /ro, but /rw.
1393 +- user opened "/au/fileA".
1394 +- he or someone else inserts a branch (/new) between /rw and /ro.
1395 +  /au = /rw + /new + /ro
1396 +- the new branch has "fileA".
1397 +- user reads from the opened "fileA"
1398 +- which filedata should aufs return, from /ro or /new?
1399 +
1400 +Some people says it has to be "from /ro" and it is a semantics of Unix.
1401 +The others say it should be "from /new" because the file is not removed
1402 +and it is equivalent to the case of someone else modifies the file.
1403 +
1404 +Here again I don't have a best and final answer. I got an idea to
1405 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
1406 +Opened File) is specified (by default), aufs returns the filedata from
1407 +/new.
1408 +Otherwise from /new.
1409 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
1410 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
1411 +++ linux/Documentation/filesystems/aufs/README 2012-03-19 12:24:13.676467669 +0100
1412 @@ -0,0 +1,330 @@
1413 +
1414 +Aufs3 -- advanced multi layered unification filesystem version 3.x
1415 +http://aufs.sf.net
1416 +Junjiro R. Okajima
1417 +
1418 +
1419 +0. Introduction
1420 +----------------------------------------
1421 +In the early days, aufs was entirely re-designed and re-implemented
1422 +Unionfs Version 1.x series. After many original ideas, approaches,
1423 +improvements and implementations, it becomes totally different from
1424 +Unionfs while keeping the basic features.
1425 +Recently, Unionfs Version 2.x series begin taking some of the same
1426 +approaches to aufs1's.
1427 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
1428 +University and his team.
1429 +
1430 +Aufs3 supports linux-3.0 and later.
1431 +If you want older kernel version support, try aufs2-2.6.git or
1432 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
1433 +
1434 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
1435 +According to Christoph Hellwig, linux rejects all union-type filesystems
1436 +but UnionMount.
1437 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
1438 +
1439 +
1440 +1. Features
1441 +----------------------------------------
1442 +- unite several directories into a single virtual filesystem. The member
1443 +  directory is called as a branch.
1444 +- you can specify the permission flags to the branch, which are 'readonly',
1445 +  'readwrite' and 'whiteout-able.'
1446 +- by upper writable branch, internal copyup and whiteout, files/dirs on
1447 +  readonly branch are modifiable logically.
1448 +- dynamic branch manipulation, add, del.
1449 +- etc...
1450 +
1451 +Also there are many enhancements in aufs1, such as:
1452 +- readdir(3) in userspace.
1453 +- keep inode number by external inode number table
1454 +- keep the timestamps of file/dir in internal copyup operation
1455 +- seekable directory, supporting NFS readdir.
1456 +- whiteout is hardlinked in order to reduce the consumption of inodes
1457 +  on branch
1458 +- do not copyup, nor create a whiteout when it is unnecessary
1459 +- revert a single systemcall when an error occurs in aufs
1460 +- remount interface instead of ioctl
1461 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
1462 +- loopback mounted filesystem as a branch
1463 +- kernel thread for removing the dir who has a plenty of whiteouts
1464 +- support copyup sparse file (a file which has a 'hole' in it)
1465 +- default permission flags for branches
1466 +- selectable permission flags for ro branch, whether whiteout can
1467 +  exist or not
1468 +- export via NFS.
1469 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
1470 +- support multiple writable branches, some policies to select one
1471 +  among multiple writable branches.
1472 +- a new semantics for link(2) and rename(2) to support multiple
1473 +  writable branches.
1474 +- no glibc changes are required.
1475 +- pseudo hardlink (hardlink over branches)
1476 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
1477 +  including NFS or remote filesystem branch.
1478 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
1479 +- and more...
1480 +
1481 +Currently these features are dropped temporary from aufs3.
1482 +See design/08plan.txt in detail.
1483 +- test only the highest one for the directory permission (dirperm1)
1484 +- copyup on open (coo=)
1485 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
1486 +  (robr)
1487 +- statistics of aufs thread (/sys/fs/aufs/stat)
1488 +- delegation mode (dlgt)
1489 +  a delegation of the internal branch access to support task I/O
1490 +  accounting, which also supports Linux Security Modules (LSM) mainly
1491 +  for Suse AppArmor.
1492 +- intent.open/create (file open in a single lookup)
1493 +
1494 +Features or just an idea in the future (see also design/*.txt),
1495 +- reorder the branch index without del/re-add.
1496 +- permanent xino files for NFSD
1497 +- an option for refreshing the opened files after add/del branches
1498 +- 'move' policy for copy-up between two writable branches, after
1499 +  checking free space.
1500 +- light version, without branch manipulation. (unnecessary?)
1501 +- copyup in userspace
1502 +- inotify in userspace
1503 +- readv/writev
1504 +- xattr, acl
1505 +
1506 +
1507 +2. Download
1508 +----------------------------------------
1509 +There were three GIT trees for aufs3, aufs3-linux.git,
1510 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
1511 +"aufs-util.git."
1512 +While the aufs-util is always necessary, you need either of aufs3-linux
1513 +or aufs3-standalone.
1514 +
1515 +The aufs3-linux tree includes the whole linux mainline GIT tree,
1516 +git://git.kernel.org/.../torvalds/linux.git.
1517 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
1518 +build aufs3 as an externel kernel module.
1519 +
1520 +On the other hand, the aufs3-standalone tree has only aufs source files
1521 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
1522 +
1523 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
1524 +represents the linux kernel version, "linux-3.x". For instance,
1525 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
1526 +"aufs3.x-rcN" branch.
1527 +
1528 +o aufs3-linux tree
1529 +$ git clone --reference /your/linux/git/tree \
1530 +       git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-linux.git \
1531 +       aufs3-linux.git
1532 +- if you don't have linux GIT tree, then remove "--reference ..."
1533 +$ cd aufs3-linux.git
1534 +$ git checkout origin/aufs3.0
1535 +
1536 +o aufs3-standalone tree
1537 +$ git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git \
1538 +       aufs3-standalone.git
1539 +$ cd aufs3-standalone.git
1540 +$ git checkout origin/aufs3.0
1541 +
1542 +o aufs-util tree
1543 +$ git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs-util.git \
1544 +       aufs-util.git
1545 +$ cd aufs-util.git
1546 +$ git checkout origin/aufs3.0
1547 +
1548 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
1549 +The minor version number, 'x' in '3.x', of aufs may not always
1550 +follow the minor version number of the kernel.
1551 +Because changes in the kernel that cause the use of a new
1552 +minor version number do not always require changes to aufs-util.
1553 +
1554 +Since aufs-util has its own minor version number, you may not be
1555 +able to find a GIT branch in aufs-util for your kernel's
1556 +exact minor version number.
1557 +In this case, you should git-checkout the branch for the
1558 +nearest lower number.
1559 +
1560 +For (an unreleased) example:
1561 +If you are using "linux-3.10" and the "aufs3.10" branch
1562 +does not exist in aufs-util repository, then "aufs3.9", "aufs3.8"
1563 +or something numerically smaller is the branch for your kernel.
1564 +
1565 +Also you can view all branches by
1566 +       $ git branch -a
1567 +
1568 +
1569 +3. Configuration and Compilation
1570 +----------------------------------------
1571 +Make sure you have git-checkout'ed the correct branch.
1572 +
1573 +For aufs3-linux tree,
1574 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS.
1575 +- set other aufs configurations if necessary.
1576 +
1577 +For aufs3-standalone tree,
1578 +There are several ways to build.
1579 +
1580 +1.
1581 +- apply ./aufs3-kbuild.patch to your kernel source files.
1582 +- apply ./aufs3-base.patch too.
1583 +- apply ./aufs3-proc_map.patch too, if you want to make /proc/PID/maps (and
1584 +  others including lsof(1)) show the file path on aufs instead of the
1585 +  path on the branch fs.
1586 +- apply ./aufs3-standalone.patch too, if you have a plan to set
1587 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
1588 +- copy ./{Documentation,fs,include/linux/aufs_type.h} files to your
1589 +  kernel source tree. Never copy ./include/linux/Kbuild.
1590 +- enable CONFIG_EXPERIMENTAL and CONFIG_AUFS_FS, you can select either
1591 +  =m or =y.
1592 +- and build your kernel as usual.
1593 +- install the built kernel.
1594 +- install the header files too by "make headers_install" to the
1595 +  directory where you specify. By default, it is $PWD/usr.
1596 +- and reboot your system.
1597 +
1598 +2.
1599 +- module only (CONFIG_AUFS_FS=m).
1600 +- apply ./aufs3-base.patch to your kernel source files.
1601 +- apply ./aufs3-proc_map.patch too to your kernel source files,
1602 +  if you want to make /proc/PID/maps (and others including lsof(1)) show
1603 +  the file path on aufs instead of the path on the branch fs.
1604 +- apply ./aufs3-standalone.patch too.
1605 +- build your kernel, don't forget "make headers_install", and reboot.
1606 +- edit ./config.mk and set other aufs configurations if necessary.
1607 +  Note: You should read ./fs/aufs/Kconfig carefully which describes
1608 +  every aufs configurations.
1609 +- build the module by simple "make".
1610 +- you can specify ${KDIR} make variable which points to your kernel
1611 +  source tree.
1612 +- install the files
1613 +  + run "make install" to install the aufs module, or copy the built
1614 +    ./aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
1615 +  + run "make headers_install" to install the aufs header file (you can
1616 +    specify DESTDIR), or copty ./usr/include/linux/aufs_type.h to
1617 +    /usr/include/linux or wherever you like. By default, the target
1618 +    directory is $PWD/usr.
1619 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
1620 +  kernel source tree.
1621 +
1622 +Note: The haeder file aufs_type.h is necessary to build aufs-util
1623 +      as well as "make headers_install" in the kernel source tree.
1624 +      headers_install is subject to be forgotten, but it is essentially
1625 +      necessary, not only for building aufs-util.
1626 +      You may not meet problems without headers_install in some older
1627 +      version though.
1628 +
1629 +And then,
1630 +- read README in aufs-util, build and install it
1631 +- note that your distribution may contain an obsoleted version of
1632 +  aufs_type.h in /usr/include/linux or something. When you build aufs
1633 +  utilities, make sure that your compiler refers the correct aufs header
1634 +  file which is built by "make headers_install."
1635 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
1636 +  then run "make install_ulib" too. And refer to the aufs manual in
1637 +  detail.
1638 +
1639 +
1640 +4. Usage
1641 +----------------------------------------
1642 +At first, make sure aufs-util are installed, and please read the aufs
1643 +manual, aufs.5 in aufs-util.git tree.
1644 +$ man -l aufs.5
1645 +
1646 +And then,
1647 +$ mkdir /tmp/rw /tmp/aufs
1648 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
1649 +
1650 +Here is another example. The result is equivalent.
1651 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
1652 +  Or
1653 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
1654 +# mount -o remount,append:${HOME} /tmp/aufs
1655 +
1656 +Then, you can see whole tree of your home dir through /tmp/aufs. If
1657 +you modify a file under /tmp/aufs, the one on your home directory is
1658 +not affected, instead the same named file will be newly created under
1659 +/tmp/rw. And all of your modification to a file will be applied to
1660 +the one under /tmp/rw. This is called the file based Copy on Write
1661 +(COW) method.
1662 +Aufs mount options are described in aufs.5.
1663 +If you run chroot or something and make your aufs as a root directory,
1664 +then you need to customize the shutdown script. See the aufs manual in
1665 +detail.
1666 +
1667 +Additionally, there are some sample usages of aufs which are a
1668 +diskless system with network booting, and LiveCD over NFS.
1669 +See sample dir in CVS tree on SourceForge.
1670 +
1671 +
1672 +5. Contact
1673 +----------------------------------------
1674 +When you have any problems or strange behaviour in aufs, please let me
1675 +know with:
1676 +- /proc/mounts (instead of the output of mount(8))
1677 +- /sys/module/aufs/*
1678 +- /sys/fs/aufs/* (if you have them)
1679 +- /debug/aufs/* (if you have them)
1680 +- linux kernel version
1681 +  if your kernel is not plain, for example modified by distributor,
1682 +  the url where i can download its source is necessary too.
1683 +- aufs version which was printed at loading the module or booting the
1684 +  system, instead of the date you downloaded.
1685 +- configuration (define/undefine CONFIG_AUFS_xxx)
1686 +- kernel configuration or /proc/config.gz (if you have it)
1687 +- behaviour which you think to be incorrect
1688 +- actual operation, reproducible one is better
1689 +- mailto: aufs-users at lists.sourceforge.net
1690 +
1691 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
1692 +and Feature Requests) on SourceForge. Please join and write to
1693 +aufs-users ML.
1694 +
1695 +
1696 +6. Acknowledgements
1697 +----------------------------------------
1698 +Thanks to everyone who have tried and are using aufs, whoever
1699 +have reported a bug or any feedback.
1700 +
1701 +Especially donators:
1702 +Tomas Matejicek(slax.org) made a donation (much more than once).
1703 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
1704 +       scripts) is making "doubling" donations.
1705 +       Unfortunately I cannot list all of the donators, but I really
1706 +       appriciate.
1707 +       It ends Aug 2010, but the ordinary donation URL is still available.
1708 +       <http://sourceforge.net/donate/index.php?group_id=167503>
1709 +Dai Itasaka made a donation (2007/8).
1710 +Chuck Smith made a donation (2008/4, 10 and 12).
1711 +Henk Schoneveld made a donation (2008/9).
1712 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
1713 +Francois Dupoux made a donation (2008/11).
1714 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
1715 +       aufs2 GIT tree (2009/2).
1716 +William Grant made a donation (2009/3).
1717 +Patrick Lane made a donation (2009/4).
1718 +The Mail Archive (mail-archive.com) made donations (2009/5).
1719 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
1720 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
1721 +Pavel Pronskiy made a donation (2011/2).
1722 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
1723 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
1724 +Max Lekomcev (DOM-TV project) made a donation (2011/7 and 12).
1725 +Sam Liddicott made a donation (2011/9).
1726 +
1727 +Thank you very much.
1728 +Donations are always, including future donations, very important and
1729 +helpful for me to keep on developing aufs.
1730 +
1731 +
1732 +7.
1733 +----------------------------------------
1734 +If you are an experienced user, no explanation is needed. Aufs is
1735 +just a linux filesystem.
1736 +
1737 +
1738 +Enjoy!
1739 +
1740 +# Local variables: ;
1741 +# mode: text;
1742 +# End: ;
1743 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
1744 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
1745 +++ linux/fs/aufs/aufs.h        2012-03-19 12:24:13.679801090 +0100
1746 @@ -0,0 +1,60 @@
1747 +/*
1748 + * Copyright (C) 2005-2012 Junjiro R. Okajima
1749 + *
1750 + * This program, aufs is free software; you can redistribute it and/or modify
1751 + * it under the terms of the GNU General Public License as published by
1752 + * the Free Software Foundation; either version 2 of the License, or
1753 + * (at your option) any later version.
1754 + *
1755 + * This program is distributed in the hope that it will be useful,
1756 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1757 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1758 + * GNU General Public License for more details.
1759 + *
1760 + * You should have received a copy of the GNU General Public License
1761 + * along with this program; if not, write to the Free Software
1762 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1763 + */
1764 +
1765 +/*
1766 + * all header files
1767 + */
1768 +
1769 +#ifndef __AUFS_H__
1770 +#define __AUFS_H__
1771 +
1772 +#ifdef __KERNEL__
1773 +
1774 +#define AuStub(type, name, body, ...) \
1775 +       static inline type name(__VA_ARGS__) { body; }
1776 +
1777 +#define AuStubVoid(name, ...) \
1778 +       AuStub(void, name, , __VA_ARGS__)
1779 +#define AuStubInt0(name, ...) \
1780 +       AuStub(int, name, return 0, __VA_ARGS__)
1781 +
1782 +#include "debug.h"
1783 +
1784 +#include "branch.h"
1785 +#include "cpup.h"
1786 +#include "dcsub.h"
1787 +#include "dbgaufs.h"
1788 +#include "dentry.h"
1789 +#include "dir.h"
1790 +#include "dynop.h"
1791 +#include "file.h"
1792 +#include "fstype.h"
1793 +#include "inode.h"
1794 +#include "loop.h"
1795 +#include "module.h"
1796 +#include "opts.h"
1797 +#include "rwsem.h"
1798 +#include "spl.h"
1799 +#include "super.h"
1800 +#include "sysaufs.h"
1801 +#include "vfsub.h"
1802 +#include "whout.h"
1803 +#include "wkq.h"
1804 +
1805 +#endif /* __KERNEL__ */
1806 +#endif /* __AUFS_H__ */
1807 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
1808 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
1809 +++ linux/fs/aufs/branch.c      2012-03-19 12:24:13.679801090 +0100
1810 @@ -0,0 +1,1169 @@
1811 +/*
1812 + * Copyright (C) 2005-2012 Junjiro R. Okajima
1813 + *
1814 + * This program, aufs is free software; you can redistribute it and/or modify
1815 + * it under the terms of the GNU General Public License as published by
1816 + * the Free Software Foundation; either version 2 of the License, or
1817 + * (at your option) any later version.
1818 + *
1819 + * This program is distributed in the hope that it will be useful,
1820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1822 + * GNU General Public License for more details.
1823 + *
1824 + * You should have received a copy of the GNU General Public License
1825 + * along with this program; if not, write to the Free Software
1826 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1827 + */
1828 +
1829 +/*
1830 + * branch management
1831 + */
1832 +
1833 +#include <linux/compat.h>
1834 +#include <linux/statfs.h>
1835 +#include "aufs.h"
1836 +
1837 +/*
1838 + * free a single branch
1839 + */
1840 +static void au_br_do_free(struct au_branch *br)
1841 +{
1842 +       int i;
1843 +       struct au_wbr *wbr;
1844 +       struct au_dykey **key;
1845 +
1846 +       au_hnotify_fin_br(br);
1847 +
1848 +       if (br->br_xino.xi_file)
1849 +               fput(br->br_xino.xi_file);
1850 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
1851 +
1852 +       AuDebugOn(atomic_read(&br->br_count));
1853 +
1854 +       wbr = br->br_wbr;
1855 +       if (wbr) {
1856 +               for (i = 0; i < AuBrWh_Last; i++)
1857 +                       dput(wbr->wbr_wh[i]);
1858 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
1859 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
1860 +       }
1861 +
1862 +       key = br->br_dykey;
1863 +       for (i = 0; i < AuBrDynOp; i++, key++)
1864 +               if (*key)
1865 +                       au_dy_put(*key);
1866 +               else
1867 +                       break;
1868 +
1869 +       mntput(br->br_mnt);
1870 +       kfree(wbr);
1871 +       kfree(br);
1872 +}
1873 +
1874 +/*
1875 + * frees all branches
1876 + */
1877 +void au_br_free(struct au_sbinfo *sbinfo)
1878 +{
1879 +       aufs_bindex_t bmax;
1880 +       struct au_branch **br;
1881 +
1882 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
1883 +
1884 +       bmax = sbinfo->si_bend + 1;
1885 +       br = sbinfo->si_branch;
1886 +       while (bmax--)
1887 +               au_br_do_free(*br++);
1888 +}
1889 +
1890 +/*
1891 + * find the index of a branch which is specified by @br_id.
1892 + */
1893 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
1894 +{
1895 +       aufs_bindex_t bindex, bend;
1896 +
1897 +       bend = au_sbend(sb);
1898 +       for (bindex = 0; bindex <= bend; bindex++)
1899 +               if (au_sbr_id(sb, bindex) == br_id)
1900 +                       return bindex;
1901 +       return -1;
1902 +}
1903 +
1904 +/* ---------------------------------------------------------------------- */
1905 +
1906 +/*
1907 + * add a branch
1908 + */
1909 +
1910 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
1911 +                       struct dentry *h_root)
1912 +{
1913 +       if (unlikely(h_adding == h_root
1914 +                    || au_test_loopback_overlap(sb, h_adding)))
1915 +               return 1;
1916 +       if (h_adding->d_sb != h_root->d_sb)
1917 +               return 0;
1918 +       return au_test_subdir(h_adding, h_root)
1919 +               || au_test_subdir(h_root, h_adding);
1920 +}
1921 +
1922 +/*
1923 + * returns a newly allocated branch. @new_nbranch is a number of branches
1924 + * after adding a branch.
1925 + */
1926 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
1927 +                                    int perm)
1928 +{
1929 +       struct au_branch *add_branch;
1930 +       struct dentry *root;
1931 +       int err;
1932 +
1933 +       err = -ENOMEM;
1934 +       root = sb->s_root;
1935 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
1936 +       if (unlikely(!add_branch))
1937 +               goto out;
1938 +
1939 +       err = au_hnotify_init_br(add_branch, perm);
1940 +       if (unlikely(err))
1941 +               goto out_br;
1942 +
1943 +       add_branch->br_wbr = NULL;
1944 +       if (au_br_writable(perm)) {
1945 +               /* may be freed separately at changing the branch permission */
1946 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
1947 +                                            GFP_NOFS);
1948 +               if (unlikely(!add_branch->br_wbr))
1949 +                       goto out_hnotify;
1950 +       }
1951 +
1952 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
1953 +       if (!err)
1954 +               err = au_di_realloc(au_di(root), new_nbranch);
1955 +       if (!err)
1956 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
1957 +       if (!err)
1958 +               return add_branch; /* success */
1959 +
1960 +       kfree(add_branch->br_wbr);
1961 +
1962 +out_hnotify:
1963 +       au_hnotify_fin_br(add_branch);
1964 +out_br:
1965 +       kfree(add_branch);
1966 +out:
1967 +       return ERR_PTR(err);
1968 +}
1969 +
1970 +/*
1971 + * test if the branch permission is legal or not.
1972 + */
1973 +static int test_br(struct inode *inode, int brperm, char *path)
1974 +{
1975 +       int err;
1976 +
1977 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
1978 +       if (!err)
1979 +               goto out;
1980 +
1981 +       err = -EINVAL;
1982 +       pr_err("write permission for readonly mount or inode, %s\n", path);
1983 +
1984 +out:
1985 +       return err;
1986 +}
1987 +
1988 +/*
1989 + * returns:
1990 + * 0: success, the caller will add it
1991 + * plus: success, it is already unified, the caller should ignore it
1992 + * minus: error
1993 + */
1994 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
1995 +{
1996 +       int err;
1997 +       aufs_bindex_t bend, bindex;
1998 +       struct dentry *root;
1999 +       struct inode *inode, *h_inode;
2000 +
2001 +       root = sb->s_root;
2002 +       bend = au_sbend(sb);
2003 +       if (unlikely(bend >= 0
2004 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2005 +               err = 1;
2006 +               if (!remount) {
2007 +                       err = -EINVAL;
2008 +                       pr_err("%s duplicated\n", add->pathname);
2009 +               }
2010 +               goto out;
2011 +       }
2012 +
2013 +       err = -ENOSPC; /* -E2BIG; */
2014 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2015 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2016 +               pr_err("number of branches exceeded %s\n", add->pathname);
2017 +               goto out;
2018 +       }
2019 +
2020 +       err = -EDOM;
2021 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2022 +               pr_err("bad index %d\n", add->bindex);
2023 +               goto out;
2024 +       }
2025 +
2026 +       inode = add->path.dentry->d_inode;
2027 +       err = -ENOENT;
2028 +       if (unlikely(!inode->i_nlink)) {
2029 +               pr_err("no existence %s\n", add->pathname);
2030 +               goto out;
2031 +       }
2032 +
2033 +       err = -EINVAL;
2034 +       if (unlikely(inode->i_sb == sb)) {
2035 +               pr_err("%s must be outside\n", add->pathname);
2036 +               goto out;
2037 +       }
2038 +
2039 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2040 +               pr_err("unsupported filesystem, %s (%s)\n",
2041 +                      add->pathname, au_sbtype(inode->i_sb));
2042 +               goto out;
2043 +       }
2044 +
2045 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
2046 +       if (unlikely(err))
2047 +               goto out;
2048 +
2049 +       if (bend < 0)
2050 +               return 0; /* success */
2051 +
2052 +       err = -EINVAL;
2053 +       for (bindex = 0; bindex <= bend; bindex++)
2054 +               if (unlikely(test_overlap(sb, add->path.dentry,
2055 +                                         au_h_dptr(root, bindex)))) {
2056 +                       pr_err("%s is overlapped\n", add->pathname);
2057 +                       goto out;
2058 +               }
2059 +
2060 +       err = 0;
2061 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2062 +               h_inode = au_h_dptr(root, 0)->d_inode;
2063 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2064 +                   || h_inode->i_uid != inode->i_uid
2065 +                   || h_inode->i_gid != inode->i_gid)
2066 +                       pr_warning("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2067 +                                  add->pathname,
2068 +                                  inode->i_uid, inode->i_gid,
2069 +                                  (inode->i_mode & S_IALLUGO),
2070 +                                  h_inode->i_uid, h_inode->i_gid,
2071 +                                  (h_inode->i_mode & S_IALLUGO));
2072 +       }
2073 +
2074 +out:
2075 +       return err;
2076 +}
2077 +
2078 +/*
2079 + * initialize or clean the whiteouts for an adding branch
2080 + */
2081 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2082 +                        int new_perm, struct dentry *h_root)
2083 +{
2084 +       int err, old_perm;
2085 +       aufs_bindex_t bindex;
2086 +       struct mutex *h_mtx;
2087 +       struct au_wbr *wbr;
2088 +       struct au_hinode *hdir;
2089 +
2090 +       wbr = br->br_wbr;
2091 +       old_perm = br->br_perm;
2092 +       br->br_perm = new_perm;
2093 +       hdir = NULL;
2094 +       h_mtx = NULL;
2095 +       bindex = au_br_index(sb, br->br_id);
2096 +       if (0 <= bindex) {
2097 +               hdir = au_hi(sb->s_root->d_inode, bindex);
2098 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
2099 +       } else {
2100 +               h_mtx = &h_root->d_inode->i_mutex;
2101 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
2102 +       }
2103 +       if (!wbr)
2104 +               err = au_wh_init(h_root, br, sb);
2105 +       else {
2106 +               wbr_wh_write_lock(wbr);
2107 +               err = au_wh_init(h_root, br, sb);
2108 +               wbr_wh_write_unlock(wbr);
2109 +       }
2110 +       if (hdir)
2111 +               au_hn_imtx_unlock(hdir);
2112 +       else
2113 +               mutex_unlock(h_mtx);
2114 +       br->br_perm = old_perm;
2115 +
2116 +       if (!err && wbr && !au_br_writable(new_perm)) {
2117 +               kfree(wbr);
2118 +               br->br_wbr = NULL;
2119 +       }
2120 +
2121 +       return err;
2122 +}
2123 +
2124 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2125 +                      int perm, struct path *path)
2126 +{
2127 +       int err;
2128 +       struct kstatfs kst;
2129 +       struct au_wbr *wbr;
2130 +       struct dentry *h_dentry;
2131 +
2132 +       wbr = br->br_wbr;
2133 +       au_rw_init(&wbr->wbr_wh_rwsem);
2134 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
2135 +       atomic_set(&wbr->wbr_wh_running, 0);
2136 +       wbr->wbr_bytes = 0;
2137 +
2138 +       /*
2139 +        * a limit for rmdir/rename a dir
2140 +        * cf. AUFS_MAX_NAMELEN in include/linux/aufs_type.h
2141 +        */
2142 +       err = vfs_statfs(path, &kst);
2143 +       if (unlikely(err))
2144 +               goto out;
2145 +       err = -EINVAL;
2146 +       h_dentry = path->dentry;
2147 +       if (kst.f_namelen >= NAME_MAX)
2148 +               err = au_br_init_wh(sb, br, perm, h_dentry);
2149 +       else
2150 +               pr_err("%.*s(%s), unsupported namelen %ld\n",
2151 +                      AuDLNPair(h_dentry), au_sbtype(h_dentry->d_sb),
2152 +                      kst.f_namelen);
2153 +
2154 +out:
2155 +       return err;
2156 +}
2157 +
2158 +/* intialize a new branch */
2159 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2160 +                     struct au_opt_add *add)
2161 +{
2162 +       int err;
2163 +
2164 +       err = 0;
2165 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
2166 +       mutex_init(&br->br_xino.xi_nondir_mtx);
2167 +       br->br_perm = add->perm;
2168 +       br->br_mnt = add->path.mnt; /* set first, mntget() later */
2169 +       spin_lock_init(&br->br_dykey_lock);
2170 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
2171 +       atomic_set(&br->br_count, 0);
2172 +       br->br_xino_upper = AUFS_XINO_TRUNC_INIT;
2173 +       atomic_set(&br->br_xino_running, 0);
2174 +       br->br_id = au_new_br_id(sb);
2175 +       AuDebugOn(br->br_id < 0);
2176 +
2177 +       if (au_br_writable(add->perm)) {
2178 +               err = au_wbr_init(br, sb, add->perm, &add->path);
2179 +               if (unlikely(err))
2180 +                       goto out_err;
2181 +       }
2182 +
2183 +       if (au_opt_test(au_mntflags(sb), XINO)) {
2184 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
2185 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
2186 +               if (unlikely(err)) {
2187 +                       AuDebugOn(br->br_xino.xi_file);
2188 +                       goto out_err;
2189 +               }
2190 +       }
2191 +
2192 +       sysaufs_br_init(br);
2193 +       mntget(add->path.mnt);
2194 +       goto out; /* success */
2195 +
2196 +out_err:
2197 +       br->br_mnt = NULL;
2198 +out:
2199 +       return err;
2200 +}
2201 +
2202 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
2203 +                            struct au_branch *br, aufs_bindex_t bend,
2204 +                            aufs_bindex_t amount)
2205 +{
2206 +       struct au_branch **brp;
2207 +
2208 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2209 +
2210 +       brp = sbinfo->si_branch + bindex;
2211 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
2212 +       *brp = br;
2213 +       sbinfo->si_bend++;
2214 +       if (unlikely(bend < 0))
2215 +               sbinfo->si_bend = 0;
2216 +}
2217 +
2218 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
2219 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2220 +{
2221 +       struct au_hdentry *hdp;
2222 +
2223 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2224 +
2225 +       hdp = dinfo->di_hdentry + bindex;
2226 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
2227 +       au_h_dentry_init(hdp);
2228 +       dinfo->di_bend++;
2229 +       if (unlikely(bend < 0))
2230 +               dinfo->di_bstart = 0;
2231 +}
2232 +
2233 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
2234 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2235 +{
2236 +       struct au_hinode *hip;
2237 +
2238 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2239 +
2240 +       hip = iinfo->ii_hinode + bindex;
2241 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
2242 +       hip->hi_inode = NULL;
2243 +       au_hn_init(hip);
2244 +       iinfo->ii_bend++;
2245 +       if (unlikely(bend < 0))
2246 +               iinfo->ii_bstart = 0;
2247 +}
2248 +
2249 +static void au_br_do_add(struct super_block *sb, struct dentry *h_dentry,
2250 +                        struct au_branch *br, aufs_bindex_t bindex)
2251 +{
2252 +       struct dentry *root;
2253 +       struct inode *root_inode;
2254 +       aufs_bindex_t bend, amount;
2255 +
2256 +       root = sb->s_root;
2257 +       root_inode = root->d_inode;
2258 +       bend = au_sbend(sb);
2259 +       amount = bend + 1 - bindex;
2260 +       au_sbilist_lock();
2261 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
2262 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
2263 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
2264 +       au_set_h_dptr(root, bindex, dget(h_dentry));
2265 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
2266 +                     /*flags*/0);
2267 +       au_sbilist_unlock();
2268 +}
2269 +
2270 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
2271 +{
2272 +       int err;
2273 +       aufs_bindex_t bend, add_bindex;
2274 +       struct dentry *root, *h_dentry;
2275 +       struct inode *root_inode;
2276 +       struct au_branch *add_branch;
2277 +
2278 +       root = sb->s_root;
2279 +       root_inode = root->d_inode;
2280 +       IMustLock(root_inode);
2281 +       err = test_add(sb, add, remount);
2282 +       if (unlikely(err < 0))
2283 +               goto out;
2284 +       if (err) {
2285 +               err = 0;
2286 +               goto out; /* success */
2287 +       }
2288 +
2289 +       bend = au_sbend(sb);
2290 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
2291 +       err = PTR_ERR(add_branch);
2292 +       if (IS_ERR(add_branch))
2293 +               goto out;
2294 +
2295 +       err = au_br_init(add_branch, sb, add);
2296 +       if (unlikely(err)) {
2297 +               au_br_do_free(add_branch);
2298 +               goto out;
2299 +       }
2300 +
2301 +       add_bindex = add->bindex;
2302 +       h_dentry = add->path.dentry;
2303 +       if (!remount)
2304 +               au_br_do_add(sb, h_dentry, add_branch, add_bindex);
2305 +       else {
2306 +               sysaufs_brs_del(sb, add_bindex);
2307 +               au_br_do_add(sb, h_dentry, add_branch, add_bindex);
2308 +               sysaufs_brs_add(sb, add_bindex);
2309 +       }
2310 +
2311 +       if (!add_bindex) {
2312 +               au_cpup_attr_all(root_inode, /*force*/1);
2313 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
2314 +       } else
2315 +               au_add_nlink(root_inode, h_dentry->d_inode);
2316 +
2317 +       /*
2318 +        * this test/set prevents aufs from handling unnecesary notify events
2319 +        * of xino files, in case of re-adding a writable branch which was
2320 +        * once detached from aufs.
2321 +        */
2322 +       if (au_xino_brid(sb) < 0
2323 +           && au_br_writable(add_branch->br_perm)
2324 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
2325 +           && add_branch->br_xino.xi_file
2326 +           && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
2327 +               au_xino_brid_set(sb, add_branch->br_id);
2328 +
2329 +out:
2330 +       return err;
2331 +}
2332 +
2333 +/* ---------------------------------------------------------------------- */
2334 +
2335 +/*
2336 + * delete a branch
2337 + */
2338 +
2339 +/* to show the line number, do not make it inlined function */
2340 +#define AuVerbose(do_info, fmt, ...) do { \
2341 +       if (do_info) \
2342 +               pr_info(fmt, ##__VA_ARGS__); \
2343 +} while (0)
2344 +
2345 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
2346 +                        aufs_bindex_t bend)
2347 +{
2348 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
2349 +}
2350 +
2351 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
2352 +                        aufs_bindex_t bend)
2353 +{
2354 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
2355 +}
2356 +
2357 +/*
2358 + * test if the branch is deletable or not.
2359 + */
2360 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
2361 +                           unsigned int sigen, const unsigned int verbose)
2362 +{
2363 +       int err, i, j, ndentry;
2364 +       aufs_bindex_t bstart, bend;
2365 +       struct au_dcsub_pages dpages;
2366 +       struct au_dpage *dpage;
2367 +       struct dentry *d;
2368 +
2369 +       err = au_dpages_init(&dpages, GFP_NOFS);
2370 +       if (unlikely(err))
2371 +               goto out;
2372 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
2373 +       if (unlikely(err))
2374 +               goto out_dpages;
2375 +
2376 +       for (i = 0; !err && i < dpages.ndpage; i++) {
2377 +               dpage = dpages.dpages + i;
2378 +               ndentry = dpage->ndentry;
2379 +               for (j = 0; !err && j < ndentry; j++) {
2380 +                       d = dpage->dentries[j];
2381 +                       AuDebugOn(!d->d_count);
2382 +                       if (!au_digen_test(d, sigen)) {
2383 +                               di_read_lock_child(d, AuLock_IR);
2384 +                               if (unlikely(au_dbrange_test(d))) {
2385 +                                       di_read_unlock(d, AuLock_IR);
2386 +                                       continue;
2387 +                               }
2388 +                       } else {
2389 +                               di_write_lock_child(d);
2390 +                               if (unlikely(au_dbrange_test(d))) {
2391 +                                       di_write_unlock(d);
2392 +                                       continue;
2393 +                               }
2394 +                               err = au_reval_dpath(d, sigen);
2395 +                               if (!err)
2396 +                                       di_downgrade_lock(d, AuLock_IR);
2397 +                               else {
2398 +                                       di_write_unlock(d);
2399 +                                       break;
2400 +                               }
2401 +                       }
2402 +
2403 +                       /* AuDbgDentry(d); */
2404 +                       bstart = au_dbstart(d);
2405 +                       bend = au_dbend(d);
2406 +                       if (bstart <= bindex
2407 +                           && bindex <= bend
2408 +                           && au_h_dptr(d, bindex)
2409 +                           && au_test_dbusy(d, bstart, bend)) {
2410 +                               err = -EBUSY;
2411 +                               AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
2412 +                               AuDbgDentry(d);
2413 +                       }
2414 +                       di_read_unlock(d, AuLock_IR);
2415 +               }
2416 +       }
2417 +
2418 +out_dpages:
2419 +       au_dpages_free(&dpages);
2420 +out:
2421 +       return err;
2422 +}
2423 +
2424 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
2425 +                          unsigned int sigen, const unsigned int verbose)
2426 +{
2427 +       int err;
2428 +       unsigned long long max, ull;
2429 +       struct inode *i, **array;
2430 +       aufs_bindex_t bstart, bend;
2431 +
2432 +       array = au_iarray_alloc(sb, &max);
2433 +       err = PTR_ERR(array);
2434 +       if (IS_ERR(array))
2435 +               goto out;
2436 +
2437 +       err = 0;
2438 +       AuDbg("b%d\n", bindex);
2439 +       for (ull = 0; !err && ull < max; ull++) {
2440 +               i = array[ull];
2441 +               if (i->i_ino == AUFS_ROOT_INO)
2442 +                       continue;
2443 +
2444 +               /* AuDbgInode(i); */
2445 +               if (au_iigen(i) == sigen)
2446 +                       ii_read_lock_child(i);
2447 +               else {
2448 +                       ii_write_lock_child(i);
2449 +                       err = au_refresh_hinode_self(i);
2450 +                       au_iigen_dec(i);
2451 +                       if (!err)
2452 +                               ii_downgrade_lock(i);
2453 +                       else {
2454 +                               ii_write_unlock(i);
2455 +                               break;
2456 +                       }
2457 +               }
2458 +
2459 +               bstart = au_ibstart(i);
2460 +               bend = au_ibend(i);
2461 +               if (bstart <= bindex
2462 +                   && bindex <= bend
2463 +                   && au_h_iptr(i, bindex)
2464 +                   && au_test_ibusy(i, bstart, bend)) {
2465 +                       err = -EBUSY;
2466 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
2467 +                       AuDbgInode(i);
2468 +               }
2469 +               ii_read_unlock(i);
2470 +       }
2471 +       au_iarray_free(array, max);
2472 +
2473 +out:
2474 +       return err;
2475 +}
2476 +
2477 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
2478 +                             const unsigned int verbose)
2479 +{
2480 +       int err;
2481 +       unsigned int sigen;
2482 +
2483 +       sigen = au_sigen(root->d_sb);
2484 +       DiMustNoWaiters(root);
2485 +       IiMustNoWaiters(root->d_inode);
2486 +       di_write_unlock(root);
2487 +       err = test_dentry_busy(root, bindex, sigen, verbose);
2488 +       if (!err)
2489 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
2490 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
2491 +
2492 +       return err;
2493 +}
2494 +
2495 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
2496 +                            const aufs_bindex_t bindex,
2497 +                            const aufs_bindex_t bend)
2498 +{
2499 +       struct au_branch **brp, **p;
2500 +
2501 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2502 +
2503 +       brp = sbinfo->si_branch + bindex;
2504 +       if (bindex < bend)
2505 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
2506 +       sbinfo->si_branch[0 + bend] = NULL;
2507 +       sbinfo->si_bend--;
2508 +
2509 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
2510 +       if (p)
2511 +               sbinfo->si_branch = p;
2512 +       /* harmless error */
2513 +}
2514 +
2515 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
2516 +                            const aufs_bindex_t bend)
2517 +{
2518 +       struct au_hdentry *hdp, *p;
2519 +
2520 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2521 +
2522 +       hdp = dinfo->di_hdentry;
2523 +       if (bindex < bend)
2524 +               memmove(hdp + bindex, hdp + bindex + 1,
2525 +                       sizeof(*hdp) * (bend - bindex));
2526 +       hdp[0 + bend].hd_dentry = NULL;
2527 +       dinfo->di_bend--;
2528 +
2529 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
2530 +       if (p)
2531 +               dinfo->di_hdentry = p;
2532 +       /* harmless error */
2533 +}
2534 +
2535 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
2536 +                            const aufs_bindex_t bend)
2537 +{
2538 +       struct au_hinode *hip, *p;
2539 +
2540 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2541 +
2542 +       hip = iinfo->ii_hinode + bindex;
2543 +       if (bindex < bend)
2544 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
2545 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
2546 +       au_hn_init(iinfo->ii_hinode + bend);
2547 +       iinfo->ii_bend--;
2548 +
2549 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
2550 +       if (p)
2551 +               iinfo->ii_hinode = p;
2552 +       /* harmless error */
2553 +}
2554 +
2555 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
2556 +                        struct au_branch *br)
2557 +{
2558 +       aufs_bindex_t bend;
2559 +       struct au_sbinfo *sbinfo;
2560 +       struct dentry *root, *h_root;
2561 +       struct inode *inode, *h_inode;
2562 +       struct au_hinode *hinode;
2563 +
2564 +       SiMustWriteLock(sb);
2565 +
2566 +       root = sb->s_root;
2567 +       inode = root->d_inode;
2568 +       sbinfo = au_sbi(sb);
2569 +       bend = sbinfo->si_bend;
2570 +
2571 +       h_root = au_h_dptr(root, bindex);
2572 +       hinode = au_hi(inode, bindex);
2573 +       h_inode = au_igrab(hinode->hi_inode);
2574 +       au_hiput(hinode);
2575 +
2576 +       au_sbilist_lock();
2577 +       au_br_do_del_brp(sbinfo, bindex, bend);
2578 +       au_br_do_del_hdp(au_di(root), bindex, bend);
2579 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
2580 +       au_sbilist_unlock();
2581 +
2582 +       dput(h_root);
2583 +       iput(h_inode);
2584 +       au_br_do_free(br);
2585 +}
2586 +
2587 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
2588 +{
2589 +       int err, rerr, i;
2590 +       unsigned int mnt_flags;
2591 +       aufs_bindex_t bindex, bend, br_id;
2592 +       unsigned char do_wh, verbose;
2593 +       struct au_branch *br;
2594 +       struct au_wbr *wbr;
2595 +
2596 +       err = 0;
2597 +       bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
2598 +       if (bindex < 0) {
2599 +               if (remount)
2600 +                       goto out; /* success */
2601 +               err = -ENOENT;
2602 +               pr_err("%s no such branch\n", del->pathname);
2603 +               goto out;
2604 +       }
2605 +       AuDbg("bindex b%d\n", bindex);
2606 +
2607 +       err = -EBUSY;
2608 +       mnt_flags = au_mntflags(sb);
2609 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2610 +       bend = au_sbend(sb);
2611 +       if (unlikely(!bend)) {
2612 +               AuVerbose(verbose, "no more branches left\n");
2613 +               goto out;
2614 +       }
2615 +       br = au_sbr(sb, bindex);
2616 +       i = atomic_read(&br->br_count);
2617 +       if (unlikely(i)) {
2618 +               AuVerbose(verbose, "%d file(s) opened\n", i);
2619 +               goto out;
2620 +       }
2621 +
2622 +       wbr = br->br_wbr;
2623 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
2624 +       if (do_wh) {
2625 +               /* instead of WbrWhMustWriteLock(wbr) */
2626 +               SiMustWriteLock(sb);
2627 +               for (i = 0; i < AuBrWh_Last; i++) {
2628 +                       dput(wbr->wbr_wh[i]);
2629 +                       wbr->wbr_wh[i] = NULL;
2630 +               }
2631 +       }
2632 +
2633 +       err = test_children_busy(sb->s_root, bindex, verbose);
2634 +       if (unlikely(err)) {
2635 +               if (do_wh)
2636 +                       goto out_wh;
2637 +               goto out;
2638 +       }
2639 +
2640 +       err = 0;
2641 +       br_id = br->br_id;
2642 +       if (!remount)
2643 +               au_br_do_del(sb, bindex, br);
2644 +       else {
2645 +               sysaufs_brs_del(sb, bindex);
2646 +               au_br_do_del(sb, bindex, br);
2647 +               sysaufs_brs_add(sb, bindex);
2648 +       }
2649 +
2650 +       if (!bindex) {
2651 +               au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
2652 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
2653 +       } else
2654 +               au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
2655 +       if (au_opt_test(mnt_flags, PLINK))
2656 +               au_plink_half_refresh(sb, br_id);
2657 +
2658 +       if (au_xino_brid(sb) == br_id)
2659 +               au_xino_brid_set(sb, -1);
2660 +       goto out; /* success */
2661 +
2662 +out_wh:
2663 +       /* revert */
2664 +       rerr = au_br_init_wh(sb, br, br->br_perm, del->h_path.dentry);
2665 +       if (rerr)
2666 +               pr_warning("failed re-creating base whiteout, %s. (%d)\n",
2667 +                          del->pathname, rerr);
2668 +out:
2669 +       return err;
2670 +}
2671 +
2672 +/* ---------------------------------------------------------------------- */
2673 +
2674 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
2675 +{
2676 +       int err;
2677 +       aufs_bindex_t bstart, bend;
2678 +       struct aufs_ibusy ibusy;
2679 +       struct inode *inode, *h_inode;
2680 +
2681 +       err = -EPERM;
2682 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
2683 +               goto out;
2684 +
2685 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
2686 +       if (!err)
2687 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
2688 +       if (unlikely(err)) {
2689 +               err = -EFAULT;
2690 +               AuTraceErr(err);
2691 +               goto out;
2692 +       }
2693 +
2694 +       err = -EINVAL;
2695 +       si_read_lock(sb, AuLock_FLUSH);
2696 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
2697 +               goto out_unlock;
2698 +
2699 +       err = 0;
2700 +       ibusy.h_ino = 0; /* invalid */
2701 +       inode = ilookup(sb, ibusy.ino);
2702 +       if (!inode
2703 +           || inode->i_ino == AUFS_ROOT_INO
2704 +           || is_bad_inode(inode))
2705 +               goto out_unlock;
2706 +
2707 +       ii_read_lock_child(inode);
2708 +       bstart = au_ibstart(inode);
2709 +       bend = au_ibend(inode);
2710 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
2711 +               h_inode = au_h_iptr(inode, ibusy.bindex);
2712 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
2713 +                       ibusy.h_ino = h_inode->i_ino;
2714 +       }
2715 +       ii_read_unlock(inode);
2716 +       iput(inode);
2717 +
2718 +out_unlock:
2719 +       si_read_unlock(sb);
2720 +       if (!err) {
2721 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
2722 +               if (unlikely(err)) {
2723 +                       err = -EFAULT;
2724 +                       AuTraceErr(err);
2725 +               }
2726 +       }
2727 +out:
2728 +       return err;
2729 +}
2730 +
2731 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
2732 +{
2733 +       return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
2734 +}
2735 +
2736 +#ifdef CONFIG_COMPAT
2737 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
2738 +{
2739 +       return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
2740 +}
2741 +#endif
2742 +
2743 +/* ---------------------------------------------------------------------- */
2744 +
2745 +/*
2746 + * change a branch permission
2747 + */
2748 +
2749 +static void au_warn_ima(void)
2750 +{
2751 +#ifdef CONFIG_IMA
2752 +       /* since it doesn't support mark_files_ro() */
2753 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
2754 +#endif
2755 +}
2756 +
2757 +static int do_need_sigen_inc(int a, int b)
2758 +{
2759 +       return au_br_whable(a) && !au_br_whable(b);
2760 +}
2761 +
2762 +static int need_sigen_inc(int old, int new)
2763 +{
2764 +       return do_need_sigen_inc(old, new)
2765 +               || do_need_sigen_inc(new, old);
2766 +}
2767 +
2768 +static unsigned long long au_farray_cb(void *a,
2769 +                                      unsigned long long max __maybe_unused,
2770 +                                      void *arg)
2771 +{
2772 +       unsigned long long n;
2773 +       struct file **p, *f;
2774 +       struct super_block *sb = arg;
2775 +
2776 +       n = 0;
2777 +       p = a;
2778 +       lg_global_lock(files_lglock);
2779 +       do_file_list_for_each_entry(sb, f) {
2780 +               if (au_fi(f)
2781 +                   && file_count(f)
2782 +                   && !special_file(f->f_dentry->d_inode->i_mode)) {
2783 +                       get_file(f);
2784 +                       *p++ = f;
2785 +                       n++;
2786 +                       AuDebugOn(n > max);
2787 +               }
2788 +       } while_file_list_for_each_entry;
2789 +       lg_global_unlock(files_lglock);
2790 +
2791 +       return n;
2792 +}
2793 +
2794 +static struct file **au_farray_alloc(struct super_block *sb,
2795 +                                    unsigned long long *max)
2796 +{
2797 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
2798 +       return au_array_alloc(max, au_farray_cb, sb);
2799 +}
2800 +
2801 +static void au_farray_free(struct file **a, unsigned long long max)
2802 +{
2803 +       unsigned long long ull;
2804 +
2805 +       for (ull = 0; ull < max; ull++)
2806 +               if (a[ull])
2807 +                       fput(a[ull]);
2808 +       au_array_free(a);
2809 +}
2810 +
2811 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
2812 +{
2813 +       int err, do_warn;
2814 +       unsigned int mnt_flags;
2815 +       unsigned long long ull, max;
2816 +       aufs_bindex_t br_id;
2817 +       unsigned char verbose;
2818 +       struct file *file, *hf, **array;
2819 +       struct inode *inode;
2820 +       struct au_hfile *hfile;
2821 +
2822 +       mnt_flags = au_mntflags(sb);
2823 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2824 +
2825 +       array = au_farray_alloc(sb, &max);
2826 +       err = PTR_ERR(array);
2827 +       if (IS_ERR(array))
2828 +               goto out;
2829 +
2830 +       do_warn = 0;
2831 +       br_id = au_sbr_id(sb, bindex);
2832 +       for (ull = 0; ull < max; ull++) {
2833 +               file = array[ull];
2834 +
2835 +               /* AuDbg("%.*s\n", AuDLNPair(file->f_dentry)); */
2836 +               fi_read_lock(file);
2837 +               if (unlikely(au_test_mmapped(file))) {
2838 +                       err = -EBUSY;
2839 +                       AuVerbose(verbose, "mmapped %.*s\n",
2840 +                                 AuDLNPair(file->f_dentry));
2841 +                       AuDbgFile(file);
2842 +                       FiMustNoWaiters(file);
2843 +                       fi_read_unlock(file);
2844 +                       goto out_array;
2845 +               }
2846 +
2847 +               inode = file->f_dentry->d_inode;
2848 +               hfile = &au_fi(file)->fi_htop;
2849 +               hf = hfile->hf_file;
2850 +               if (!S_ISREG(inode->i_mode)
2851 +                   || !(file->f_mode & FMODE_WRITE)
2852 +                   || hfile->hf_br->br_id != br_id
2853 +                   || !(hf->f_mode & FMODE_WRITE))
2854 +                       array[ull] = NULL;
2855 +               else {
2856 +                       do_warn = 1;
2857 +                       get_file(file);
2858 +               }
2859 +
2860 +               FiMustNoWaiters(file);
2861 +               fi_read_unlock(file);
2862 +               fput(file);
2863 +       }
2864 +
2865 +       err = 0;
2866 +       if (do_warn)
2867 +               au_warn_ima();
2868 +
2869 +       for (ull = 0; ull < max; ull++) {
2870 +               file = array[ull];
2871 +               if (!file)
2872 +                       continue;
2873 +
2874 +               /* todo: already flushed? */
2875 +               /* cf. fs/super.c:mark_files_ro() */
2876 +               /* fi_read_lock(file); */
2877 +               hfile = &au_fi(file)->fi_htop;
2878 +               hf = hfile->hf_file;
2879 +               /* fi_read_unlock(file); */
2880 +               spin_lock(&hf->f_lock);
2881 +               hf->f_mode &= ~FMODE_WRITE;
2882 +               spin_unlock(&hf->f_lock);
2883 +               if (!file_check_writeable(hf)) {
2884 +                       file_release_write(hf);
2885 +                       mnt_drop_write(hf->f_vfsmnt);
2886 +               }
2887 +       }
2888 +
2889 +out_array:
2890 +       au_farray_free(array, max);
2891 +out:
2892 +       AuTraceErr(err);
2893 +       return err;
2894 +}
2895 +
2896 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
2897 +             int *do_refresh)
2898 +{
2899 +       int err, rerr;
2900 +       aufs_bindex_t bindex;
2901 +       struct path path;
2902 +       struct dentry *root;
2903 +       struct au_branch *br;
2904 +
2905 +       root = sb->s_root;
2906 +       bindex = au_find_dbindex(root, mod->h_root);
2907 +       if (bindex < 0) {
2908 +               if (remount)
2909 +                       return 0; /* success */
2910 +               err = -ENOENT;
2911 +               pr_err("%s no such branch\n", mod->path);
2912 +               goto out;
2913 +       }
2914 +       AuDbg("bindex b%d\n", bindex);
2915 +
2916 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
2917 +       if (unlikely(err))
2918 +               goto out;
2919 +
2920 +       br = au_sbr(sb, bindex);
2921 +       if (br->br_perm == mod->perm)
2922 +               return 0; /* success */
2923 +
2924 +       if (au_br_writable(br->br_perm)) {
2925 +               /* remove whiteout base */
2926 +               err = au_br_init_wh(sb, br, mod->perm, mod->h_root);
2927 +               if (unlikely(err))
2928 +                       goto out;
2929 +
2930 +               if (!au_br_writable(mod->perm)) {
2931 +                       /* rw --> ro, file might be mmapped */
2932 +                       DiMustNoWaiters(root);
2933 +                       IiMustNoWaiters(root->d_inode);
2934 +                       di_write_unlock(root);
2935 +                       err = au_br_mod_files_ro(sb, bindex);
2936 +                       /* aufs_write_lock() calls ..._child() */
2937 +                       di_write_lock_child(root);
2938 +
2939 +                       if (unlikely(err)) {
2940 +                               rerr = -ENOMEM;
2941 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
2942 +                                                    GFP_NOFS);
2943 +                               if (br->br_wbr) {
2944 +                                       path.mnt = br->br_mnt;
2945 +                                       path.dentry = mod->h_root;
2946 +                                       rerr = au_wbr_init(br, sb, br->br_perm,
2947 +                                                          &path);
2948 +                               }
2949 +                               if (unlikely(rerr)) {
2950 +                                       AuIOErr("nested error %d (%d)\n",
2951 +                                               rerr, err);
2952 +                                       br->br_perm = mod->perm;
2953 +                               }
2954 +                       }
2955 +               }
2956 +       } else if (au_br_writable(mod->perm)) {
2957 +               /* ro --> rw */
2958 +               err = -ENOMEM;
2959 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
2960 +               if (br->br_wbr) {
2961 +                       path.mnt = br->br_mnt;
2962 +                       path.dentry = mod->h_root;
2963 +                       err = au_wbr_init(br, sb, mod->perm, &path);
2964 +                       if (unlikely(err)) {
2965 +                               kfree(br->br_wbr);
2966 +                               br->br_wbr = NULL;
2967 +                       }
2968 +               }
2969 +       }
2970 +
2971 +       if (!err) {
2972 +               *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
2973 +               br->br_perm = mod->perm;
2974 +       }
2975 +
2976 +out:
2977 +       AuTraceErr(err);
2978 +       return err;
2979 +}
2980 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
2981 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
2982 +++ linux/fs/aufs/branch.h      2012-03-19 12:24:13.679801090 +0100
2983 @@ -0,0 +1,230 @@
2984 +/*
2985 + * Copyright (C) 2005-2012 Junjiro R. Okajima
2986 + *
2987 + * This program, aufs is free software; you can redistribute it and/or modify
2988 + * it under the terms of the GNU General Public License as published by
2989 + * the Free Software Foundation; either version 2 of the License, or
2990 + * (at your option) any later version.
2991 + *
2992 + * This program is distributed in the hope that it will be useful,
2993 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2994 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2995 + * GNU General Public License for more details.
2996 + *
2997 + * You should have received a copy of the GNU General Public License
2998 + * along with this program; if not, write to the Free Software
2999 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3000 + */
3001 +
3002 +/*
3003 + * branch filesystems and xino for them
3004 + */
3005 +
3006 +#ifndef __AUFS_BRANCH_H__
3007 +#define __AUFS_BRANCH_H__
3008 +
3009 +#ifdef __KERNEL__
3010 +
3011 +#include <linux/mount.h>
3012 +#include "dynop.h"
3013 +#include "rwsem.h"
3014 +#include "super.h"
3015 +
3016 +/* ---------------------------------------------------------------------- */
3017 +
3018 +/* a xino file */
3019 +struct au_xino_file {
3020 +       struct file             *xi_file;
3021 +       struct mutex            xi_nondir_mtx;
3022 +
3023 +       /* todo: make xino files an array to support huge inode number */
3024 +
3025 +#ifdef CONFIG_DEBUG_FS
3026 +       struct dentry            *xi_dbgaufs;
3027 +#endif
3028 +};
3029 +
3030 +/* members for writable branch only */
3031 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
3032 +struct au_wbr {
3033 +       struct au_rwsem         wbr_wh_rwsem;
3034 +       struct dentry           *wbr_wh[AuBrWh_Last];
3035 +       atomic_t                wbr_wh_running;
3036 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
3037 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
3038 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
3039 +
3040 +       /* mfs mode */
3041 +       unsigned long long      wbr_bytes;
3042 +};
3043 +
3044 +/* ext2 has 3 types of operations at least, ext3 has 4 */
3045 +#define AuBrDynOp (AuDyLast * 4)
3046 +
3047 +/* protected by superblock rwsem */
3048 +struct au_branch {
3049 +       struct au_xino_file     br_xino;
3050 +
3051 +       aufs_bindex_t           br_id;
3052 +
3053 +       int                     br_perm;
3054 +       struct vfsmount         *br_mnt;
3055 +       spinlock_t              br_dykey_lock;
3056 +       struct au_dykey         *br_dykey[AuBrDynOp];
3057 +       atomic_t                br_count;
3058 +
3059 +       struct au_wbr           *br_wbr;
3060 +
3061 +       /* xino truncation */
3062 +       blkcnt_t                br_xino_upper;  /* watermark in blocks */
3063 +       atomic_t                br_xino_running;
3064 +
3065 +#ifdef CONFIG_AUFS_HFSNOTIFY
3066 +       struct fsnotify_group   *br_hfsn_group;
3067 +       struct fsnotify_ops     br_hfsn_ops;
3068 +#endif
3069 +
3070 +#ifdef CONFIG_SYSFS
3071 +       /* an entry under sysfs per mount-point */
3072 +       char                    br_name[8];
3073 +       struct attribute        br_attr;
3074 +#endif
3075 +};
3076 +
3077 +/* ---------------------------------------------------------------------- */
3078 +
3079 +/* branch permissions and attributes */
3080 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
3081 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
3082 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
3083 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
3084 +
3085 +#define AuBrRAttr_WH           (1 << 3)        /* whiteout-able */
3086 +
3087 +#define AuBrWAttr_NoLinkWH     (1 << 4)        /* un-hardlinkable whiteouts */
3088 +
3089 +static inline int au_br_writable(int brperm)
3090 +{
3091 +       return brperm & AuBrPerm_RW;
3092 +}
3093 +
3094 +static inline int au_br_whable(int brperm)
3095 +{
3096 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
3097 +}
3098 +
3099 +static inline int au_br_wh_linkable(int brperm)
3100 +{
3101 +       return !(brperm & AuBrWAttr_NoLinkWH);
3102 +}
3103 +
3104 +static inline int au_br_rdonly(struct au_branch *br)
3105 +{
3106 +       return ((br->br_mnt->mnt_sb->s_flags & MS_RDONLY)
3107 +               || !au_br_writable(br->br_perm))
3108 +               ? -EROFS : 0;
3109 +}
3110 +
3111 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
3112 +{
3113 +#ifdef CONFIG_AUFS_HNOTIFY
3114 +       return !(brperm & AuBrPerm_RR);
3115 +#else
3116 +       return 0;
3117 +#endif
3118 +}
3119 +
3120 +/* ---------------------------------------------------------------------- */
3121 +
3122 +/* branch.c */
3123 +struct au_sbinfo;
3124 +void au_br_free(struct au_sbinfo *sinfo);
3125 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
3126 +struct au_opt_add;
3127 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
3128 +struct au_opt_del;
3129 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
3130 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
3131 +#ifdef CONFIG_COMPAT
3132 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
3133 +#endif
3134 +struct au_opt_mod;
3135 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3136 +             int *do_refresh);
3137 +
3138 +/* xino.c */
3139 +static const loff_t au_loff_max = LLONG_MAX;
3140 +
3141 +int au_xib_trunc(struct super_block *sb);
3142 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
3143 +                  loff_t *pos);
3144 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
3145 +                   loff_t *pos);
3146 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
3147 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
3148 +ino_t au_xino_new_ino(struct super_block *sb);
3149 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
3150 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3151 +                 ino_t ino);
3152 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3153 +                ino_t *ino);
3154 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
3155 +              struct file *base_file, int do_test);
3156 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
3157 +
3158 +struct au_opt_xino;
3159 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
3160 +void au_xino_clr(struct super_block *sb);
3161 +struct file *au_xino_def(struct super_block *sb);
3162 +int au_xino_path(struct seq_file *seq, struct file *file);
3163 +
3164 +/* ---------------------------------------------------------------------- */
3165 +
3166 +/* Superblock to branch */
3167 +static inline
3168 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
3169 +{
3170 +       return au_sbr(sb, bindex)->br_id;
3171 +}
3172 +
3173 +static inline
3174 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
3175 +{
3176 +       return au_sbr(sb, bindex)->br_mnt;
3177 +}
3178 +
3179 +static inline
3180 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
3181 +{
3182 +       return au_sbr_mnt(sb, bindex)->mnt_sb;
3183 +}
3184 +
3185 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
3186 +{
3187 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
3188 +}
3189 +
3190 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
3191 +{
3192 +       return au_sbr(sb, bindex)->br_perm;
3193 +}
3194 +
3195 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
3196 +{
3197 +       return au_br_whable(au_sbr_perm(sb, bindex));
3198 +}
3199 +
3200 +/* ---------------------------------------------------------------------- */
3201 +
3202 +/*
3203 + * wbr_wh_read_lock, wbr_wh_write_lock
3204 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
3205 + */
3206 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
3207 +
3208 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
3209 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
3210 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
3211 +
3212 +#endif /* __KERNEL__ */
3213 +#endif /* __AUFS_BRANCH_H__ */
3214 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
3215 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
3216 +++ linux/fs/aufs/conf.mk       2012-03-19 12:24:13.679801090 +0100
3217 @@ -0,0 +1,38 @@
3218 +
3219 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
3220 +
3221 +define AuConf
3222 +ifdef ${1}
3223 +AuConfStr += ${1}=${${1}}
3224 +endif
3225 +endef
3226 +
3227 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
3228 +       SBILIST \
3229 +       HNOTIFY HFSNOTIFY \
3230 +       EXPORT INO_T_64 \
3231 +       RDU \
3232 +       PROC_MAP \
3233 +       SP_IATTR \
3234 +       SHWH \
3235 +       BR_RAMFS \
3236 +       BR_FUSE POLL \
3237 +       BR_HFSPLUS \
3238 +       BDEV_LOOP \
3239 +       DEBUG MAGIC_SYSRQ
3240 +$(foreach i, ${AuConfAll}, \
3241 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
3242 +
3243 +AuConfName = ${obj}/conf.str
3244 +${AuConfName}.tmp: FORCE
3245 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
3246 +${AuConfName}: ${AuConfName}.tmp
3247 +       @diff -q $< $@ > /dev/null 2>&1 || { \
3248 +       echo '  GEN    ' $@; \
3249 +       cp -p $< $@; \
3250 +       }
3251 +FORCE:
3252 +clean-files += ${AuConfName} ${AuConfName}.tmp
3253 +${obj}/sysfs.o: ${AuConfName}
3254 +
3255 +-include ${srctree}/${src}/conf_priv.mk
3256 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
3257 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
3258 +++ linux/fs/aufs/cpup.c        2012-03-19 12:24:13.679801090 +0100
3259 @@ -0,0 +1,1084 @@
3260 +/*
3261 + * Copyright (C) 2005-2012 Junjiro R. Okajima
3262 + *
3263 + * This program, aufs is free software; you can redistribute it and/or modify
3264 + * it under the terms of the GNU General Public License as published by
3265 + * the Free Software Foundation; either version 2 of the License, or
3266 + * (at your option) any later version.
3267 + *
3268 + * This program is distributed in the hope that it will be useful,
3269 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3270 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3271 + * GNU General Public License for more details.
3272 + *
3273 + * You should have received a copy of the GNU General Public License
3274 + * along with this program; if not, write to the Free Software
3275 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3276 + */
3277 +
3278 +/*
3279 + * copy-up functions, see wbr_policy.c for copy-down
3280 + */
3281 +
3282 +#include <linux/fs_stack.h>
3283 +#include <linux/mm.h>
3284 +#include "aufs.h"
3285 +
3286 +void au_cpup_attr_flags(struct inode *dst, struct inode *src)
3287 +{
3288 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
3289 +               | S_NOATIME | S_NOCMTIME;
3290 +
3291 +       dst->i_flags |= src->i_flags & ~mask;
3292 +       if (au_test_fs_notime(dst->i_sb))
3293 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
3294 +}
3295 +
3296 +void au_cpup_attr_timesizes(struct inode *inode)
3297 +{
3298 +       struct inode *h_inode;
3299 +
3300 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3301 +       fsstack_copy_attr_times(inode, h_inode);
3302 +       fsstack_copy_inode_size(inode, h_inode);
3303 +}
3304 +
3305 +void au_cpup_attr_nlink(struct inode *inode, int force)
3306 +{
3307 +       struct inode *h_inode;
3308 +       struct super_block *sb;
3309 +       aufs_bindex_t bindex, bend;
3310 +
3311 +       sb = inode->i_sb;
3312 +       bindex = au_ibstart(inode);
3313 +       h_inode = au_h_iptr(inode, bindex);
3314 +       if (!force
3315 +           && !S_ISDIR(h_inode->i_mode)
3316 +           && au_opt_test(au_mntflags(sb), PLINK)
3317 +           && au_plink_test(inode))
3318 +               return;
3319 +
3320 +       /*
3321 +        * 0 can happen in revalidating.
3322 +        * h_inode->i_mutex is not held, but it is harmless since once i_nlink
3323 +        * reaches 0, it will never become positive.
3324 +        */
3325 +       vfsub_set_nlink(inode, h_inode->i_nlink);
3326 +
3327 +       /*
3328 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
3329 +        * it may includes whplink directory.
3330 +        */
3331 +       if (S_ISDIR(h_inode->i_mode)) {
3332 +               bend = au_ibend(inode);
3333 +               for (bindex++; bindex <= bend; bindex++) {
3334 +                       h_inode = au_h_iptr(inode, bindex);
3335 +                       if (h_inode)
3336 +                               au_add_nlink(inode, h_inode);
3337 +               }
3338 +       }
3339 +}
3340 +
3341 +void au_cpup_attr_changeable(struct inode *inode)
3342 +{
3343 +       struct inode *h_inode;
3344 +
3345 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3346 +       inode->i_mode = h_inode->i_mode;
3347 +       inode->i_uid = h_inode->i_uid;
3348 +       inode->i_gid = h_inode->i_gid;
3349 +       au_cpup_attr_timesizes(inode);
3350 +       au_cpup_attr_flags(inode, h_inode);
3351 +}
3352 +
3353 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
3354 +{
3355 +       struct au_iinfo *iinfo = au_ii(inode);
3356 +
3357 +       IiMustWriteLock(inode);
3358 +
3359 +       iinfo->ii_higen = h_inode->i_generation;
3360 +       iinfo->ii_hsb1 = h_inode->i_sb;
3361 +}
3362 +
3363 +void au_cpup_attr_all(struct inode *inode, int force)
3364 +{
3365 +       struct inode *h_inode;
3366 +
3367 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3368 +       au_cpup_attr_changeable(inode);
3369 +       if (inode->i_nlink > 0)
3370 +               au_cpup_attr_nlink(inode, force);
3371 +       inode->i_rdev = h_inode->i_rdev;
3372 +       inode->i_blkbits = h_inode->i_blkbits;
3373 +       au_cpup_igen(inode, h_inode);
3374 +}
3375 +
3376 +/* ---------------------------------------------------------------------- */
3377 +
3378 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
3379 +
3380 +/* keep the timestamps of the parent dir when cpup */
3381 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
3382 +                   struct path *h_path)
3383 +{
3384 +       struct inode *h_inode;
3385 +
3386 +       dt->dt_dentry = dentry;
3387 +       dt->dt_h_path = *h_path;
3388 +       h_inode = h_path->dentry->d_inode;
3389 +       dt->dt_atime = h_inode->i_atime;
3390 +       dt->dt_mtime = h_inode->i_mtime;
3391 +       /* smp_mb(); */
3392 +}
3393 +
3394 +void au_dtime_revert(struct au_dtime *dt)
3395 +{
3396 +       struct iattr attr;
3397 +       int err;
3398 +
3399 +       attr.ia_atime = dt->dt_atime;
3400 +       attr.ia_mtime = dt->dt_mtime;
3401 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
3402 +               | ATTR_ATIME | ATTR_ATIME_SET;
3403 +
3404 +       err = vfsub_notify_change(&dt->dt_h_path, &attr);
3405 +       if (unlikely(err))
3406 +               pr_warning("restoring timestamps failed(%d). ignored\n", err);
3407 +}
3408 +
3409 +/* ---------------------------------------------------------------------- */
3410 +
3411 +static noinline_for_stack
3412 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src)
3413 +{
3414 +       int err, sbits;
3415 +       struct iattr ia;
3416 +       struct path h_path;
3417 +       struct inode *h_isrc, *h_idst;
3418 +
3419 +       h_path.dentry = au_h_dptr(dst, bindex);
3420 +       h_idst = h_path.dentry->d_inode;
3421 +       h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
3422 +       h_isrc = h_src->d_inode;
3423 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
3424 +               | ATTR_ATIME | ATTR_MTIME
3425 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
3426 +       ia.ia_uid = h_isrc->i_uid;
3427 +       ia.ia_gid = h_isrc->i_gid;
3428 +       ia.ia_atime = h_isrc->i_atime;
3429 +       ia.ia_mtime = h_isrc->i_mtime;
3430 +       if (h_idst->i_mode != h_isrc->i_mode
3431 +           && !S_ISLNK(h_idst->i_mode)) {
3432 +               ia.ia_valid |= ATTR_MODE;
3433 +               ia.ia_mode = h_isrc->i_mode;
3434 +       }
3435 +       sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
3436 +       au_cpup_attr_flags(h_idst, h_isrc);
3437 +       err = vfsub_notify_change(&h_path, &ia);
3438 +
3439 +       /* is this nfs only? */
3440 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
3441 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
3442 +               ia.ia_mode = h_isrc->i_mode;
3443 +               err = vfsub_notify_change(&h_path, &ia);
3444 +       }
3445 +
3446 +       return err;
3447 +}
3448 +
3449 +/* ---------------------------------------------------------------------- */
3450 +
3451 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
3452 +                          char *buf, unsigned long blksize)
3453 +{
3454 +       int err;
3455 +       size_t sz, rbytes, wbytes;
3456 +       unsigned char all_zero;
3457 +       char *p, *zp;
3458 +       struct mutex *h_mtx;
3459 +       /* reduce stack usage */
3460 +       struct iattr *ia;
3461 +
3462 +       zp = page_address(ZERO_PAGE(0));
3463 +       if (unlikely(!zp))
3464 +               return -ENOMEM; /* possible? */
3465 +
3466 +       err = 0;
3467 +       all_zero = 0;
3468 +       while (len) {
3469 +               AuDbg("len %lld\n", len);
3470 +               sz = blksize;
3471 +               if (len < blksize)
3472 +                       sz = len;
3473 +
3474 +               rbytes = 0;
3475 +               /* todo: signal_pending? */
3476 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
3477 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
3478 +                       err = rbytes;
3479 +               }
3480 +               if (unlikely(err < 0))
3481 +                       break;
3482 +
3483 +               all_zero = 0;
3484 +               if (len >= rbytes && rbytes == blksize)
3485 +                       all_zero = !memcmp(buf, zp, rbytes);
3486 +               if (!all_zero) {
3487 +                       wbytes = rbytes;
3488 +                       p = buf;
3489 +                       while (wbytes) {
3490 +                               size_t b;
3491 +
3492 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
3493 +                               err = b;
3494 +                               /* todo: signal_pending? */
3495 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
3496 +                                       continue;
3497 +                               if (unlikely(err < 0))
3498 +                                       break;
3499 +                               wbytes -= b;
3500 +                               p += b;
3501 +                       }
3502 +               } else {
3503 +                       loff_t res;
3504 +
3505 +                       AuLabel(hole);
3506 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
3507 +                       err = res;
3508 +                       if (unlikely(res < 0))
3509 +                               break;
3510 +               }
3511 +               len -= rbytes;
3512 +               err = 0;
3513 +       }
3514 +
3515 +       /* the last block may be a hole */
3516 +       if (!err && all_zero) {
3517 +               AuLabel(last hole);
3518 +
3519 +               err = 1;
3520 +               if (au_test_nfs(dst->f_dentry->d_sb)) {
3521 +                       /* nfs requires this step to make last hole */
3522 +                       /* is this only nfs? */
3523 +                       do {
3524 +                               /* todo: signal_pending? */
3525 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
3526 +                       } while (err == -EAGAIN || err == -EINTR);
3527 +                       if (err == 1)
3528 +                               dst->f_pos--;
3529 +               }
3530 +
3531 +               if (err == 1) {
3532 +                       ia = (void *)buf;
3533 +                       ia->ia_size = dst->f_pos;
3534 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
3535 +                       ia->ia_file = dst;
3536 +                       h_mtx = &dst->f_dentry->d_inode->i_mutex;
3537 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
3538 +                       err = vfsub_notify_change(&dst->f_path, ia);
3539 +                       mutex_unlock(h_mtx);
3540 +               }
3541 +       }
3542 +
3543 +       return err;
3544 +}
3545 +
3546 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
3547 +{
3548 +       int err;
3549 +       unsigned long blksize;
3550 +       unsigned char do_kfree;
3551 +       char *buf;
3552 +
3553 +       err = -ENOMEM;
3554 +       blksize = dst->f_dentry->d_sb->s_blocksize;
3555 +       if (!blksize || PAGE_SIZE < blksize)
3556 +               blksize = PAGE_SIZE;
3557 +       AuDbg("blksize %lu\n", blksize);
3558 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
3559 +       if (do_kfree)
3560 +               buf = kmalloc(blksize, GFP_NOFS);
3561 +       else
3562 +               buf = (void *)__get_free_page(GFP_NOFS);
3563 +       if (unlikely(!buf))
3564 +               goto out;
3565 +
3566 +       if (len > (1 << 22))
3567 +               AuDbg("copying a large file %lld\n", (long long)len);
3568 +
3569 +       src->f_pos = 0;
3570 +       dst->f_pos = 0;
3571 +       err = au_do_copy_file(dst, src, len, buf, blksize);
3572 +       if (do_kfree)
3573 +               kfree(buf);
3574 +       else
3575 +               free_page((unsigned long)buf);
3576 +
3577 +out:
3578 +       return err;
3579 +}
3580 +
3581 +/*
3582 + * to support a sparse file which is opened with O_APPEND,
3583 + * we need to close the file.
3584 + */
3585 +static int au_cp_regular(struct dentry *dentry, aufs_bindex_t bdst,
3586 +                        aufs_bindex_t bsrc, loff_t len)
3587 +{
3588 +       int err, i;
3589 +       enum { SRC, DST };
3590 +       struct {
3591 +               aufs_bindex_t bindex;
3592 +               unsigned int flags;
3593 +               struct dentry *dentry;
3594 +               struct file *file;
3595 +               void *label, *label_file;
3596 +       } *f, file[] = {
3597 +               {
3598 +                       .bindex = bsrc,
3599 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
3600 +                       .file = NULL,
3601 +                       .label = &&out,
3602 +                       .label_file = &&out_src
3603 +               },
3604 +               {
3605 +                       .bindex = bdst,
3606 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
3607 +                       .file = NULL,
3608 +                       .label = &&out_src,
3609 +                       .label_file = &&out_dst
3610 +               }
3611 +       };
3612 +       struct super_block *sb;
3613 +
3614 +       /* bsrc branch can be ro/rw. */
3615 +       sb = dentry->d_sb;
3616 +       f = file;
3617 +       for (i = 0; i < 2; i++, f++) {
3618 +               f->dentry = au_h_dptr(dentry, f->bindex);
3619 +               f->file = au_h_open(dentry, f->bindex, f->flags, /*file*/NULL);
3620 +               err = PTR_ERR(f->file);
3621 +               if (IS_ERR(f->file))
3622 +                       goto *f->label;
3623 +               err = -EINVAL;
3624 +               if (unlikely(!f->file->f_op))
3625 +                       goto *f->label_file;
3626 +       }
3627 +
3628 +       /* try stopping to update while we copyup */
3629 +       IMustLock(file[SRC].dentry->d_inode);
3630 +       err = au_copy_file(file[DST].file, file[SRC].file, len);
3631 +
3632 +out_dst:
3633 +       fput(file[DST].file);
3634 +       au_sbr_put(sb, file[DST].bindex);
3635 +out_src:
3636 +       fput(file[SRC].file);
3637 +       au_sbr_put(sb, file[SRC].bindex);
3638 +out:
3639 +       return err;
3640 +}
3641 +
3642 +static int au_do_cpup_regular(struct dentry *dentry, aufs_bindex_t bdst,
3643 +                             aufs_bindex_t bsrc, loff_t len,
3644 +                             struct inode *h_dir, struct path *h_path)
3645 +{
3646 +       int err, rerr;
3647 +       loff_t l;
3648 +
3649 +       err = 0;
3650 +       l = i_size_read(au_h_iptr(dentry->d_inode, bsrc));
3651 +       if (len == -1 || l < len)
3652 +               len = l;
3653 +       if (len)
3654 +               err = au_cp_regular(dentry, bdst, bsrc, len);
3655 +       if (!err)
3656 +               goto out; /* success */
3657 +
3658 +       rerr = vfsub_unlink(h_dir, h_path, /*force*/0);
3659 +       if (rerr) {
3660 +               AuIOErr("failed unlinking cpup-ed %.*s(%d, %d)\n",
3661 +                       AuDLNPair(h_path->dentry), err, rerr);
3662 +               err = -EIO;
3663 +       }
3664 +
3665 +out:
3666 +       return err;
3667 +}
3668 +
3669 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
3670 +                             struct inode *h_dir)
3671 +{
3672 +       int err, symlen;
3673 +       mm_segment_t old_fs;
3674 +       union {
3675 +               char *k;
3676 +               char __user *u;
3677 +       } sym;
3678 +
3679 +       err = -ENOSYS;
3680 +       if (unlikely(!h_src->d_inode->i_op->readlink))
3681 +               goto out;
3682 +
3683 +       err = -ENOMEM;
3684 +       sym.k = __getname_gfp(GFP_NOFS);
3685 +       if (unlikely(!sym.k))
3686 +               goto out;
3687 +
3688 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
3689 +       old_fs = get_fs();
3690 +       set_fs(KERNEL_DS);
3691 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
3692 +       err = symlen;
3693 +       set_fs(old_fs);
3694 +
3695 +       if (symlen > 0) {
3696 +               sym.k[symlen] = 0;
3697 +               err = vfsub_symlink(h_dir, h_path, sym.k);
3698 +       }
3699 +       __putname(sym.k);
3700 +
3701 +out:
3702 +       return err;
3703 +}
3704 +
3705 +/* return with the lower dst inode is locked */
3706 +static noinline_for_stack
3707 +int cpup_entry(struct dentry *dentry, aufs_bindex_t bdst,
3708 +              aufs_bindex_t bsrc, loff_t len, unsigned int flags,
3709 +              struct dentry *dst_parent)
3710 +{
3711 +       int err;
3712 +       umode_t mode;
3713 +       unsigned int mnt_flags;
3714 +       unsigned char isdir;
3715 +       const unsigned char do_dt = !!au_ftest_cpup(flags, DTIME);
3716 +       struct au_dtime dt;
3717 +       struct path h_path;
3718 +       struct dentry *h_src, *h_dst, *h_parent;
3719 +       struct inode *h_inode, *h_dir;
3720 +       struct super_block *sb;
3721 +
3722 +       /* bsrc branch can be ro/rw. */
3723 +       h_src = au_h_dptr(dentry, bsrc);
3724 +       h_inode = h_src->d_inode;
3725 +       AuDebugOn(h_inode != au_h_iptr(dentry->d_inode, bsrc));
3726 +
3727 +       /* try stopping to be referenced while we are creating */
3728 +       h_dst = au_h_dptr(dentry, bdst);
3729 +       h_parent = h_dst->d_parent; /* dir inode is locked */
3730 +       h_dir = h_parent->d_inode;
3731 +       IMustLock(h_dir);
3732 +       AuDebugOn(h_parent != h_dst->d_parent);
3733 +
3734 +       sb = dentry->d_sb;
3735 +       h_path.mnt = au_sbr_mnt(sb, bdst);
3736 +       if (do_dt) {
3737 +               h_path.dentry = h_parent;
3738 +               au_dtime_store(&dt, dst_parent, &h_path);
3739 +       }
3740 +       h_path.dentry = h_dst;
3741 +
3742 +       isdir = 0;
3743 +       mode = h_inode->i_mode;
3744 +       switch (mode & S_IFMT) {
3745 +       case S_IFREG:
3746 +               /* try stopping to update while we are referencing */
3747 +               IMustLock(h_inode);
3748 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
3749 +               if (!err)
3750 +                       err = au_do_cpup_regular
3751 +                               (dentry, bdst, bsrc, len,
3752 +                                au_h_iptr(dst_parent->d_inode, bdst), &h_path);
3753 +               break;
3754 +       case S_IFDIR:
3755 +               isdir = 1;
3756 +               err = vfsub_mkdir(h_dir, &h_path, mode);
3757 +               if (!err) {
3758 +                       /*
3759 +                        * strange behaviour from the users view,
3760 +                        * particularry setattr case
3761 +                        */
3762 +                       if (au_ibstart(dst_parent->d_inode) == bdst)
3763 +                               au_cpup_attr_nlink(dst_parent->d_inode,
3764 +                                                  /*force*/1);
3765 +                       au_cpup_attr_nlink(dentry->d_inode, /*force*/1);
3766 +               }
3767 +               break;
3768 +       case S_IFLNK:
3769 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
3770 +               break;
3771 +       case S_IFCHR:
3772 +       case S_IFBLK:
3773 +               AuDebugOn(!capable(CAP_MKNOD));
3774 +               /*FALLTHROUGH*/
3775 +       case S_IFIFO:
3776 +       case S_IFSOCK:
3777 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
3778 +               break;
3779 +       default:
3780 +               AuIOErr("Unknown inode type 0%o\n", mode);
3781 +               err = -EIO;
3782 +       }
3783 +
3784 +       mnt_flags = au_mntflags(sb);
3785 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
3786 +           && !isdir
3787 +           && au_opt_test(mnt_flags, XINO)
3788 +           && h_inode->i_nlink == 1
3789 +           /* todo: unnecessary? */
3790 +           /* && dentry->d_inode->i_nlink == 1 */
3791 +           && bdst < bsrc
3792 +           && !au_ftest_cpup(flags, KEEPLINO))
3793 +               au_xino_write(sb, bsrc, h_inode->i_ino, /*ino*/0);
3794 +               /* ignore this error */
3795 +
3796 +       if (do_dt)
3797 +               au_dtime_revert(&dt);
3798 +       return err;
3799 +}
3800 +
3801 +/*
3802 + * copyup the @dentry from @bsrc to @bdst.
3803 + * the caller must set the both of lower dentries.
3804 + * @len is for truncating when it is -1 copyup the entire file.
3805 + * in link/rename cases, @dst_parent may be different from the real one.
3806 + */
3807 +static int au_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
3808 +                         aufs_bindex_t bsrc, loff_t len, unsigned int flags,
3809 +                         struct dentry *dst_parent)
3810 +{
3811 +       int err, rerr;
3812 +       aufs_bindex_t old_ibstart;
3813 +       unsigned char isdir, plink;
3814 +       struct au_dtime dt;
3815 +       struct path h_path;
3816 +       struct dentry *h_src, *h_dst, *h_parent;
3817 +       struct inode *dst_inode, *h_dir, *inode;
3818 +       struct super_block *sb;
3819 +
3820 +       AuDebugOn(bsrc <= bdst);
3821 +
3822 +       sb = dentry->d_sb;
3823 +       h_path.mnt = au_sbr_mnt(sb, bdst);
3824 +       h_dst = au_h_dptr(dentry, bdst);
3825 +       h_parent = h_dst->d_parent; /* dir inode is locked */
3826 +       h_dir = h_parent->d_inode;
3827 +       IMustLock(h_dir);
3828 +
3829 +       h_src = au_h_dptr(dentry, bsrc);
3830 +       inode = dentry->d_inode;
3831 +
3832 +       if (!dst_parent)
3833 +               dst_parent = dget_parent(dentry);
3834 +       else
3835 +               dget(dst_parent);
3836 +
3837 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
3838 +       dst_inode = au_h_iptr(inode, bdst);
3839 +       if (dst_inode) {
3840 +               if (unlikely(!plink)) {
3841 +                       err = -EIO;
3842 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
3843 +                               "but plink is disabled\n",
3844 +                               dst_inode->i_ino, inode->i_ino, bdst);
3845 +                       goto out;
3846 +               }
3847 +
3848 +               if (dst_inode->i_nlink) {
3849 +                       const int do_dt = au_ftest_cpup(flags, DTIME);
3850 +
3851 +                       h_src = au_plink_lkup(inode, bdst);
3852 +                       err = PTR_ERR(h_src);
3853 +                       if (IS_ERR(h_src))
3854 +                               goto out;
3855 +                       if (unlikely(!h_src->d_inode)) {
3856 +                               err = -EIO;
3857 +                               AuIOErr("i%lu exists on a upper branch "
3858 +                                       "but not pseudo-linked\n",
3859 +                                       inode->i_ino);
3860 +                               dput(h_src);
3861 +                               goto out;
3862 +                       }
3863 +
3864 +                       if (do_dt) {
3865 +                               h_path.dentry = h_parent;
3866 +                               au_dtime_store(&dt, dst_parent, &h_path);
3867 +                       }
3868 +                       h_path.dentry = h_dst;
3869 +                       err = vfsub_link(h_src, h_dir, &h_path);
3870 +                       if (do_dt)
3871 +                               au_dtime_revert(&dt);
3872 +                       dput(h_src);
3873 +                       goto out;
3874 +               } else
3875 +                       /* todo: cpup_wh_file? */
3876 +                       /* udba work */
3877 +                       au_update_ibrange(inode, /*do_put_zero*/1);
3878 +       }
3879 +
3880 +       old_ibstart = au_ibstart(inode);
3881 +       err = cpup_entry(dentry, bdst, bsrc, len, flags, dst_parent);
3882 +       if (unlikely(err))
3883 +               goto out;
3884 +       dst_inode = h_dst->d_inode;
3885 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
3886 +
3887 +       err = cpup_iattr(dentry, bdst, h_src);
3888 +       isdir = S_ISDIR(dst_inode->i_mode);
3889 +       if (!err) {
3890 +               if (bdst < old_ibstart) {
3891 +                       if (S_ISREG(inode->i_mode)) {
3892 +                               err = au_dy_iaop(inode, bdst, dst_inode);
3893 +                               if (unlikely(err))
3894 +                                       goto out_rev;
3895 +                       }
3896 +                       au_set_ibstart(inode, bdst);
3897 +               }
3898 +               au_set_h_iptr(inode, bdst, au_igrab(dst_inode),
3899 +                             au_hi_flags(inode, isdir));
3900 +               mutex_unlock(&dst_inode->i_mutex);
3901 +               if (!isdir
3902 +                   && h_src->d_inode->i_nlink > 1
3903 +                   && plink)
3904 +                       au_plink_append(inode, bdst, h_dst);
3905 +               goto out; /* success */
3906 +       }
3907 +
3908 +       /* revert */
3909 +out_rev:
3910 +       h_path.dentry = h_parent;
3911 +       mutex_unlock(&dst_inode->i_mutex);
3912 +       au_dtime_store(&dt, dst_parent, &h_path);
3913 +       h_path.dentry = h_dst;
3914 +       if (!isdir)
3915 +               rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
3916 +       else
3917 +               rerr = vfsub_rmdir(h_dir, &h_path);
3918 +       au_dtime_revert(&dt);
3919 +       if (rerr) {
3920 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
3921 +               err = -EIO;
3922 +       }
3923 +
3924 +out:
3925 +       dput(dst_parent);
3926 +       return err;
3927 +}
3928 +
3929 +struct au_cpup_single_args {
3930 +       int *errp;
3931 +       struct dentry *dentry;
3932 +       aufs_bindex_t bdst, bsrc;
3933 +       loff_t len;
3934 +       unsigned int flags;
3935 +       struct dentry *dst_parent;
3936 +};
3937 +
3938 +static void au_call_cpup_single(void *args)
3939 +{
3940 +       struct au_cpup_single_args *a = args;
3941 +       *a->errp = au_cpup_single(a->dentry, a->bdst, a->bsrc, a->len,
3942 +                                 a->flags, a->dst_parent);
3943 +}
3944 +
3945 +/*
3946 + * prevent SIGXFSZ in copy-up.
3947 + * testing CAP_MKNOD is for generic fs,
3948 + * but CAP_FSETID is for xfs only, currently.
3949 + */
3950 +static int au_cpup_sio_test(struct super_block *sb, umode_t mode)
3951 +{
3952 +       int do_sio;
3953 +
3954 +       do_sio = 0;
3955 +       if (!au_wkq_test()
3956 +           && (!au_sbi(sb)->si_plink_maint_pid
3957 +               || au_plink_maint(sb, AuLock_NOPLM))) {
3958 +               switch (mode & S_IFMT) {
3959 +               case S_IFREG:
3960 +                       /* no condition about RLIMIT_FSIZE and the file size */
3961 +                       do_sio = 1;
3962 +                       break;
3963 +               case S_IFCHR:
3964 +               case S_IFBLK:
3965 +                       do_sio = !capable(CAP_MKNOD);
3966 +                       break;
3967 +               }
3968 +               if (!do_sio)
3969 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
3970 +                                 && !capable(CAP_FSETID));
3971 +       }
3972 +
3973 +       return do_sio;
3974 +}
3975 +
3976 +int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
3977 +                      aufs_bindex_t bsrc, loff_t len, unsigned int flags,
3978 +                      struct dentry *dst_parent)
3979 +{
3980 +       int err, wkq_err;
3981 +       struct dentry *h_dentry;
3982 +
3983 +       h_dentry = au_h_dptr(dentry, bsrc);
3984 +       if (!au_cpup_sio_test(dentry->d_sb, h_dentry->d_inode->i_mode))
3985 +               err = au_cpup_single(dentry, bdst, bsrc, len, flags,
3986 +                                    dst_parent);
3987 +       else {
3988 +               struct au_cpup_single_args args = {
3989 +                       .errp           = &err,
3990 +                       .dentry         = dentry,
3991 +                       .bdst           = bdst,
3992 +                       .bsrc           = bsrc,
3993 +                       .len            = len,
3994 +                       .flags          = flags,
3995 +                       .dst_parent     = dst_parent
3996 +               };
3997 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
3998 +               if (unlikely(wkq_err))
3999 +                       err = wkq_err;
4000 +       }
4001 +
4002 +       return err;
4003 +}
4004 +
4005 +/*
4006 + * copyup the @dentry from the first active lower branch to @bdst,
4007 + * using au_cpup_single().
4008 + */
4009 +static int au_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4010 +                         unsigned int flags)
4011 +{
4012 +       int err;
4013 +       aufs_bindex_t bsrc, bend;
4014 +
4015 +       bend = au_dbend(dentry);
4016 +       for (bsrc = bdst + 1; bsrc <= bend; bsrc++)
4017 +               if (au_h_dptr(dentry, bsrc))
4018 +                       break;
4019 +
4020 +       err = au_lkup_neg(dentry, bdst);
4021 +       if (!err) {
4022 +               err = au_cpup_single(dentry, bdst, bsrc, len, flags, NULL);
4023 +               if (!err)
4024 +                       return 0; /* success */
4025 +
4026 +               /* revert */
4027 +               au_set_h_dptr(dentry, bdst, NULL);
4028 +               au_set_dbstart(dentry, bsrc);
4029 +       }
4030 +
4031 +       return err;
4032 +}
4033 +
4034 +struct au_cpup_simple_args {
4035 +       int *errp;
4036 +       struct dentry *dentry;
4037 +       aufs_bindex_t bdst;
4038 +       loff_t len;
4039 +       unsigned int flags;
4040 +};
4041 +
4042 +static void au_call_cpup_simple(void *args)
4043 +{
4044 +       struct au_cpup_simple_args *a = args;
4045 +       *a->errp = au_cpup_simple(a->dentry, a->bdst, a->len, a->flags);
4046 +}
4047 +
4048 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4049 +                      unsigned int flags)
4050 +{
4051 +       int err, wkq_err;
4052 +       struct dentry *parent;
4053 +       struct inode *h_dir;
4054 +
4055 +       parent = dget_parent(dentry);
4056 +       h_dir = au_h_iptr(parent->d_inode, bdst);
4057 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
4058 +           && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
4059 +               err = au_cpup_simple(dentry, bdst, len, flags);
4060 +       else {
4061 +               struct au_cpup_simple_args args = {
4062 +                       .errp           = &err,
4063 +                       .dentry         = dentry,
4064 +                       .bdst           = bdst,
4065 +                       .len            = len,
4066 +                       .flags          = flags
4067 +               };
4068 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
4069 +               if (unlikely(wkq_err))
4070 +                       err = wkq_err;
4071 +       }
4072 +
4073 +       dput(parent);
4074 +       return err;
4075 +}
4076 +
4077 +/* ---------------------------------------------------------------------- */
4078 +
4079 +/*
4080 + * copyup the deleted file for writing.
4081 + */
4082 +static int au_do_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst,
4083 +                        struct dentry *wh_dentry, struct file *file,
4084 +                        loff_t len)
4085 +{
4086 +       int err;
4087 +       aufs_bindex_t bstart;
4088 +       struct au_dinfo *dinfo;
4089 +       struct dentry *h_d_dst, *h_d_start;
4090 +       struct au_hdentry *hdp;
4091 +
4092 +       dinfo = au_di(dentry);
4093 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4094 +
4095 +       bstart = dinfo->di_bstart;
4096 +       hdp = dinfo->di_hdentry;
4097 +       h_d_dst = hdp[0 + bdst].hd_dentry;
4098 +       dinfo->di_bstart = bdst;
4099 +       hdp[0 + bdst].hd_dentry = wh_dentry;
4100 +       if (file) {
4101 +               h_d_start = hdp[0 + bstart].hd_dentry;
4102 +               hdp[0 + bstart].hd_dentry = au_hf_top(file)->f_dentry;
4103 +       }
4104 +       err = au_cpup_single(dentry, bdst, bstart, len, !AuCpup_DTIME,
4105 +                            /*h_parent*/NULL);
4106 +       if (file) {
4107 +               if (!err)
4108 +                       err = au_reopen_nondir(file);
4109 +               hdp[0 + bstart].hd_dentry = h_d_start;
4110 +       }
4111 +       hdp[0 + bdst].hd_dentry = h_d_dst;
4112 +       dinfo->di_bstart = bstart;
4113 +
4114 +       return err;
4115 +}
4116 +
4117 +static int au_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4118 +                     struct file *file)
4119 +{
4120 +       int err;
4121 +       struct au_dtime dt;
4122 +       struct dentry *parent, *h_parent, *wh_dentry;
4123 +       struct au_branch *br;
4124 +       struct path h_path;
4125 +
4126 +       br = au_sbr(dentry->d_sb, bdst);
4127 +       parent = dget_parent(dentry);
4128 +       h_parent = au_h_dptr(parent, bdst);
4129 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
4130 +       err = PTR_ERR(wh_dentry);
4131 +       if (IS_ERR(wh_dentry))
4132 +               goto out;
4133 +
4134 +       h_path.dentry = h_parent;
4135 +       h_path.mnt = br->br_mnt;
4136 +       au_dtime_store(&dt, parent, &h_path);
4137 +       err = au_do_cpup_wh(dentry, bdst, wh_dentry, file, len);
4138 +       if (unlikely(err))
4139 +               goto out_wh;
4140 +
4141 +       dget(wh_dentry);
4142 +       h_path.dentry = wh_dentry;
4143 +       if (!S_ISDIR(wh_dentry->d_inode->i_mode))
4144 +               err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
4145 +       else
4146 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
4147 +       if (unlikely(err)) {
4148 +               AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
4149 +                       AuDLNPair(wh_dentry), err);
4150 +               err = -EIO;
4151 +       }
4152 +       au_dtime_revert(&dt);
4153 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
4154 +
4155 +out_wh:
4156 +       dput(wh_dentry);
4157 +out:
4158 +       dput(parent);
4159 +       return err;
4160 +}
4161 +
4162 +struct au_cpup_wh_args {
4163 +       int *errp;
4164 +       struct dentry *dentry;
4165 +       aufs_bindex_t bdst;
4166 +       loff_t len;
4167 +       struct file *file;
4168 +};
4169 +
4170 +static void au_call_cpup_wh(void *args)
4171 +{
4172 +       struct au_cpup_wh_args *a = args;
4173 +       *a->errp = au_cpup_wh(a->dentry, a->bdst, a->len, a->file);
4174 +}
4175 +
4176 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4177 +                  struct file *file)
4178 +{
4179 +       int err, wkq_err;
4180 +       struct dentry *parent, *h_orph, *h_parent, *h_dentry;
4181 +       struct inode *dir, *h_dir, *h_tmpdir, *h_inode;
4182 +       struct au_wbr *wbr;
4183 +
4184 +       parent = dget_parent(dentry);
4185 +       dir = parent->d_inode;
4186 +       h_orph = NULL;
4187 +       h_parent = NULL;
4188 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
4189 +       h_tmpdir = h_dir;
4190 +       if (!h_dir->i_nlink) {
4191 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
4192 +               h_orph = wbr->wbr_orph;
4193 +
4194 +               h_parent = dget(au_h_dptr(parent, bdst));
4195 +               au_set_h_dptr(parent, bdst, dget(h_orph));
4196 +               h_tmpdir = h_orph->d_inode;
4197 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
4198 +
4199 +               /* this temporary unlock is safe */
4200 +               if (file)
4201 +                       h_dentry = au_hf_top(file)->f_dentry;
4202 +               else
4203 +                       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
4204 +               h_inode = h_dentry->d_inode;
4205 +               IMustLock(h_inode);
4206 +               mutex_unlock(&h_inode->i_mutex);
4207 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
4208 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
4209 +               /* todo: au_h_open_pre()? */
4210 +       }
4211 +
4212 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
4213 +           && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
4214 +               err = au_cpup_wh(dentry, bdst, len, file);
4215 +       else {
4216 +               struct au_cpup_wh_args args = {
4217 +                       .errp   = &err,
4218 +                       .dentry = dentry,
4219 +                       .bdst   = bdst,
4220 +                       .len    = len,
4221 +                       .file   = file
4222 +               };
4223 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
4224 +               if (unlikely(wkq_err))
4225 +                       err = wkq_err;
4226 +       }
4227 +
4228 +       if (h_orph) {
4229 +               mutex_unlock(&h_tmpdir->i_mutex);
4230 +               /* todo: au_h_open_post()? */
4231 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
4232 +               au_set_h_dptr(parent, bdst, h_parent);
4233 +       }
4234 +       iput(h_dir);
4235 +       dput(parent);
4236 +
4237 +       return err;
4238 +}
4239 +
4240 +/* ---------------------------------------------------------------------- */
4241 +
4242 +/*
4243 + * generic routine for both of copy-up and copy-down.
4244 + */
4245 +/* cf. revalidate function in file.c */
4246 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4247 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4248 +                        struct dentry *h_parent, void *arg),
4249 +              void *arg)
4250 +{
4251 +       int err;
4252 +       struct au_pin pin;
4253 +       struct dentry *d, *parent, *h_parent, *real_parent;
4254 +
4255 +       err = 0;
4256 +       parent = dget_parent(dentry);
4257 +       if (IS_ROOT(parent))
4258 +               goto out;
4259 +
4260 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
4261 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
4262 +
4263 +       /* do not use au_dpage */
4264 +       real_parent = parent;
4265 +       while (1) {
4266 +               dput(parent);
4267 +               parent = dget_parent(dentry);
4268 +               h_parent = au_h_dptr(parent, bdst);
4269 +               if (h_parent)
4270 +                       goto out; /* success */
4271 +
4272 +               /* find top dir which is necessary to cpup */
4273 +               do {
4274 +                       d = parent;
4275 +                       dput(parent);
4276 +                       parent = dget_parent(d);
4277 +                       di_read_lock_parent3(parent, !AuLock_IR);
4278 +                       h_parent = au_h_dptr(parent, bdst);
4279 +                       di_read_unlock(parent, !AuLock_IR);
4280 +               } while (!h_parent);
4281 +
4282 +               if (d != real_parent)
4283 +                       di_write_lock_child3(d);
4284 +
4285 +               /* somebody else might create while we were sleeping */
4286 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
4287 +                       if (au_h_dptr(d, bdst))
4288 +                               au_update_dbstart(d);
4289 +
4290 +                       au_pin_set_dentry(&pin, d);
4291 +                       err = au_do_pin(&pin);
4292 +                       if (!err) {
4293 +                               err = cp(d, bdst, h_parent, arg);
4294 +                               au_unpin(&pin);
4295 +                       }
4296 +               }
4297 +
4298 +               if (d != real_parent)
4299 +                       di_write_unlock(d);
4300 +               if (unlikely(err))
4301 +                       break;
4302 +       }
4303 +
4304 +out:
4305 +       dput(parent);
4306 +       return err;
4307 +}
4308 +
4309 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
4310 +                      struct dentry *h_parent __maybe_unused ,
4311 +                      void *arg __maybe_unused)
4312 +{
4313 +       return au_sio_cpup_simple(dentry, bdst, -1, AuCpup_DTIME);
4314 +}
4315 +
4316 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4317 +{
4318 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
4319 +}
4320 +
4321 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4322 +{
4323 +       int err;
4324 +       struct dentry *parent;
4325 +       struct inode *dir;
4326 +
4327 +       parent = dget_parent(dentry);
4328 +       dir = parent->d_inode;
4329 +       err = 0;
4330 +       if (au_h_iptr(dir, bdst))
4331 +               goto out;
4332 +
4333 +       di_read_unlock(parent, AuLock_IR);
4334 +       di_write_lock_parent(parent);
4335 +       /* someone else might change our inode while we were sleeping */
4336 +       if (!au_h_iptr(dir, bdst))
4337 +               err = au_cpup_dirs(dentry, bdst);
4338 +       di_downgrade_lock(parent, AuLock_IR);
4339 +
4340 +out:
4341 +       dput(parent);
4342 +       return err;
4343 +}
4344 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
4345 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
4346 +++ linux/fs/aufs/cpup.h        2012-03-19 12:24:13.679801090 +0100
4347 @@ -0,0 +1,81 @@
4348 +/*
4349 + * Copyright (C) 2005-2012 Junjiro R. Okajima
4350 + *
4351 + * This program, aufs is free software; you can redistribute it and/or modify
4352 + * it under the terms of the GNU General Public License as published by
4353 + * the Free Software Foundation; either version 2 of the License, or
4354 + * (at your option) any later version.
4355 + *
4356 + * This program is distributed in the hope that it will be useful,
4357 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4358 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4359 + * GNU General Public License for more details.
4360 + *
4361 + * You should have received a copy of the GNU General Public License
4362 + * along with this program; if not, write to the Free Software
4363 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4364 + */
4365 +
4366 +/*
4367 + * copy-up/down functions
4368 + */
4369 +
4370 +#ifndef __AUFS_CPUP_H__
4371 +#define __AUFS_CPUP_H__
4372 +
4373 +#ifdef __KERNEL__
4374 +
4375 +#include <linux/path.h>
4376 +
4377 +struct inode;
4378 +struct file;
4379 +
4380 +void au_cpup_attr_flags(struct inode *dst, struct inode *src);
4381 +void au_cpup_attr_timesizes(struct inode *inode);
4382 +void au_cpup_attr_nlink(struct inode *inode, int force);
4383 +void au_cpup_attr_changeable(struct inode *inode);
4384 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
4385 +void au_cpup_attr_all(struct inode *inode, int force);
4386 +
4387 +/* ---------------------------------------------------------------------- */
4388 +
4389 +/* cpup flags */
4390 +#define AuCpup_DTIME   1               /* do dtime_store/revert */
4391 +#define AuCpup_KEEPLINO        (1 << 1)        /* do not clear the lower xino,
4392 +                                          for link(2) */
4393 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
4394 +#define au_fset_cpup(flags, name) \
4395 +       do { (flags) |= AuCpup_##name; } while (0)
4396 +#define au_fclr_cpup(flags, name) \
4397 +       do { (flags) &= ~AuCpup_##name; } while (0)
4398 +
4399 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
4400 +int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
4401 +                      aufs_bindex_t bsrc, loff_t len, unsigned int flags,
4402 +                      struct dentry *dst_parent);
4403 +int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4404 +                      unsigned int flags);
4405 +int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
4406 +                  struct file *file);
4407 +
4408 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4409 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4410 +                        struct dentry *h_parent, void *arg),
4411 +              void *arg);
4412 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4413 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4414 +
4415 +/* ---------------------------------------------------------------------- */
4416 +
4417 +/* keep timestamps when copyup */
4418 +struct au_dtime {
4419 +       struct dentry *dt_dentry;
4420 +       struct path dt_h_path;
4421 +       struct timespec dt_atime, dt_mtime;
4422 +};
4423 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4424 +                   struct path *h_path);
4425 +void au_dtime_revert(struct au_dtime *dt);
4426 +
4427 +#endif /* __KERNEL__ */
4428 +#endif /* __AUFS_CPUP_H__ */
4429 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
4430 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
4431 +++ linux/fs/aufs/dbgaufs.c     2012-03-19 12:24:13.679801090 +0100
4432 @@ -0,0 +1,334 @@
4433 +/*
4434 + * Copyright (C) 2005-2012 Junjiro R. Okajima
4435 + *
4436 + * This program, aufs is free software; you can redistribute it and/or modify
4437 + * it under the terms of the GNU General Public License as published by
4438 + * the Free Software Foundation; either version 2 of the License, or
4439 + * (at your option) any later version.
4440 + *
4441 + * This program is distributed in the hope that it will be useful,
4442 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4443 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4444 + * GNU General Public License for more details.
4445 + *
4446 + * You should have received a copy of the GNU General Public License
4447 + * along with this program; if not, write to the Free Software
4448 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4449 + */
4450 +
4451 +/*
4452 + * debugfs interface
4453 + */
4454 +
4455 +#include <linux/debugfs.h>
4456 +#include "aufs.h"
4457 +
4458 +#ifndef CONFIG_SYSFS
4459 +#error DEBUG_FS depends upon SYSFS
4460 +#endif
4461 +
4462 +static struct dentry *dbgaufs;
4463 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
4464 +
4465 +/* 20 is max digits length of ulong 64 */
4466 +struct dbgaufs_arg {
4467 +       int n;
4468 +       char a[20 * 4];
4469 +};
4470 +
4471 +/*
4472 + * common function for all XINO files
4473 + */
4474 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
4475 +                             struct file *file)
4476 +{
4477 +       kfree(file->private_data);
4478 +       return 0;
4479 +}
4480 +
4481 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
4482 +{
4483 +       int err;
4484 +       struct kstat st;
4485 +       struct dbgaufs_arg *p;
4486 +
4487 +       err = -ENOMEM;
4488 +       p = kmalloc(sizeof(*p), GFP_NOFS);
4489 +       if (unlikely(!p))
4490 +               goto out;
4491 +
4492 +       err = 0;
4493 +       p->n = 0;
4494 +       file->private_data = p;
4495 +       if (!xf)
4496 +               goto out;
4497 +
4498 +       err = vfs_getattr(xf->f_vfsmnt, xf->f_dentry, &st);
4499 +       if (!err) {
4500 +               if (do_fcnt)
4501 +                       p->n = snprintf
4502 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
4503 +                                (long)file_count(xf), st.blocks, st.blksize,
4504 +                                (long long)st.size);
4505 +               else
4506 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
4507 +                                       st.blocks, st.blksize,
4508 +                                       (long long)st.size);
4509 +               AuDebugOn(p->n >= sizeof(p->a));
4510 +       } else {
4511 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
4512 +               err = 0;
4513 +       }
4514 +
4515 +out:
4516 +       return err;
4517 +
4518 +}
4519 +
4520 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
4521 +                              size_t count, loff_t *ppos)
4522 +{
4523 +       struct dbgaufs_arg *p;
4524 +
4525 +       p = file->private_data;
4526 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
4527 +}
4528 +
4529 +/* ---------------------------------------------------------------------- */
4530 +
4531 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
4532 +{
4533 +       int err;
4534 +       struct au_sbinfo *sbinfo;
4535 +       struct super_block *sb;
4536 +
4537 +       sbinfo = inode->i_private;
4538 +       sb = sbinfo->si_sb;
4539 +       si_noflush_read_lock(sb);
4540 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
4541 +       si_read_unlock(sb);
4542 +       return err;
4543 +}
4544 +
4545 +static const struct file_operations dbgaufs_xib_fop = {
4546 +       .owner          = THIS_MODULE,
4547 +       .open           = dbgaufs_xib_open,
4548 +       .release        = dbgaufs_xi_release,
4549 +       .read           = dbgaufs_xi_read
4550 +};
4551 +
4552 +/* ---------------------------------------------------------------------- */
4553 +
4554 +#define DbgaufsXi_PREFIX "xi"
4555 +
4556 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
4557 +{
4558 +       int err;
4559 +       long l;
4560 +       struct au_sbinfo *sbinfo;
4561 +       struct super_block *sb;
4562 +       struct file *xf;
4563 +       struct qstr *name;
4564 +
4565 +       err = -ENOENT;
4566 +       xf = NULL;
4567 +       name = &file->f_dentry->d_name;
4568 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
4569 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
4570 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
4571 +               goto out;
4572 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
4573 +       if (unlikely(err))
4574 +               goto out;
4575 +
4576 +       sbinfo = inode->i_private;
4577 +       sb = sbinfo->si_sb;
4578 +       si_noflush_read_lock(sb);
4579 +       if (l <= au_sbend(sb)) {
4580 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
4581 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
4582 +       } else
4583 +               err = -ENOENT;
4584 +       si_read_unlock(sb);
4585 +
4586 +out:
4587 +       return err;
4588 +}
4589 +
4590 +static const struct file_operations dbgaufs_xino_fop = {
4591 +       .owner          = THIS_MODULE,
4592 +       .open           = dbgaufs_xino_open,
4593 +       .release        = dbgaufs_xi_release,
4594 +       .read           = dbgaufs_xi_read
4595 +};
4596 +
4597 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
4598 +{
4599 +       aufs_bindex_t bend;
4600 +       struct au_branch *br;
4601 +       struct au_xino_file *xi;
4602 +
4603 +       if (!au_sbi(sb)->si_dbgaufs)
4604 +               return;
4605 +
4606 +       bend = au_sbend(sb);
4607 +       for (; bindex <= bend; bindex++) {
4608 +               br = au_sbr(sb, bindex);
4609 +               xi = &br->br_xino;
4610 +               if (xi->xi_dbgaufs) {
4611 +                       debugfs_remove(xi->xi_dbgaufs);
4612 +                       xi->xi_dbgaufs = NULL;
4613 +               }
4614 +       }
4615 +}
4616 +
4617 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
4618 +{
4619 +       struct au_sbinfo *sbinfo;
4620 +       struct dentry *parent;
4621 +       struct au_branch *br;
4622 +       struct au_xino_file *xi;
4623 +       aufs_bindex_t bend;
4624 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
4625 +
4626 +       sbinfo = au_sbi(sb);
4627 +       parent = sbinfo->si_dbgaufs;
4628 +       if (!parent)
4629 +               return;
4630 +
4631 +       bend = au_sbend(sb);
4632 +       for (; bindex <= bend; bindex++) {
4633 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
4634 +               br = au_sbr(sb, bindex);
4635 +               xi = &br->br_xino;
4636 +               AuDebugOn(xi->xi_dbgaufs);
4637 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
4638 +                                                    sbinfo, &dbgaufs_xino_fop);
4639 +               /* ignore an error */
4640 +               if (unlikely(!xi->xi_dbgaufs))
4641 +                       AuWarn1("failed %s under debugfs\n", name);
4642 +       }
4643 +}
4644 +
4645 +/* ---------------------------------------------------------------------- */
4646 +
4647 +#ifdef CONFIG_AUFS_EXPORT
4648 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
4649 +{
4650 +       int err;
4651 +       struct au_sbinfo *sbinfo;
4652 +       struct super_block *sb;
4653 +
4654 +       sbinfo = inode->i_private;
4655 +       sb = sbinfo->si_sb;
4656 +       si_noflush_read_lock(sb);
4657 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
4658 +       si_read_unlock(sb);
4659 +       return err;
4660 +}
4661 +
4662 +static const struct file_operations dbgaufs_xigen_fop = {
4663 +       .owner          = THIS_MODULE,
4664 +       .open           = dbgaufs_xigen_open,
4665 +       .release        = dbgaufs_xi_release,
4666 +       .read           = dbgaufs_xi_read
4667 +};
4668 +
4669 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
4670 +{
4671 +       int err;
4672 +
4673 +       /*
4674 +        * This function is a dynamic '__init' fucntion actually,
4675 +        * so the tiny check for si_rwsem is unnecessary.
4676 +        */
4677 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
4678 +
4679 +       err = -EIO;
4680 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
4681 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
4682 +                &dbgaufs_xigen_fop);
4683 +       if (sbinfo->si_dbgaufs_xigen)
4684 +               err = 0;
4685 +
4686 +       return err;
4687 +}
4688 +#else
4689 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
4690 +{
4691 +       return 0;
4692 +}
4693 +#endif /* CONFIG_AUFS_EXPORT */
4694 +
4695 +/* ---------------------------------------------------------------------- */
4696 +
4697 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
4698 +{
4699 +       /*
4700 +        * This function is a dynamic '__init' fucntion actually,
4701 +        * so the tiny check for si_rwsem is unnecessary.
4702 +        */
4703 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
4704 +
4705 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
4706 +       sbinfo->si_dbgaufs = NULL;
4707 +       kobject_put(&sbinfo->si_kobj);
4708 +}
4709 +
4710 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
4711 +{
4712 +       int err;
4713 +       char name[SysaufsSiNameLen];
4714 +
4715 +       /*
4716 +        * This function is a dynamic '__init' fucntion actually,
4717 +        * so the tiny check for si_rwsem is unnecessary.
4718 +        */
4719 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
4720 +
4721 +       err = -ENOENT;
4722 +       if (!dbgaufs) {
4723 +               AuErr1("/debug/aufs is uninitialized\n");
4724 +               goto out;
4725 +       }
4726 +
4727 +       err = -EIO;
4728 +       sysaufs_name(sbinfo, name);
4729 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
4730 +       if (unlikely(!sbinfo->si_dbgaufs))
4731 +               goto out;
4732 +       kobject_get(&sbinfo->si_kobj);
4733 +
4734 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
4735 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
4736 +                &dbgaufs_xib_fop);
4737 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
4738 +               goto out_dir;
4739 +
4740 +       err = dbgaufs_xigen_init(sbinfo);
4741 +       if (!err)
4742 +               goto out; /* success */
4743 +
4744 +out_dir:
4745 +       dbgaufs_si_fin(sbinfo);
4746 +out:
4747 +       return err;
4748 +}
4749 +
4750 +/* ---------------------------------------------------------------------- */
4751 +
4752 +void dbgaufs_fin(void)
4753 +{
4754 +       debugfs_remove(dbgaufs);
4755 +}
4756 +
4757 +int __init dbgaufs_init(void)
4758 +{
4759 +       int err;
4760 +
4761 +       err = -EIO;
4762 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
4763 +       if (dbgaufs)
4764 +               err = 0;
4765 +       return err;
4766 +}
4767 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
4768 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
4769 +++ linux/fs/aufs/dbgaufs.h     2012-03-19 12:24:13.679801090 +0100
4770 @@ -0,0 +1,49 @@
4771 +/*
4772 + * Copyright (C) 2005-2012 Junjiro R. Okajima
4773 + *
4774 + * This program, aufs is free software; you can redistribute it and/or modify
4775 + * it under the terms of the GNU General Public License as published by
4776 + * the Free Software Foundation; either version 2 of the License, or
4777 + * (at your option) any later version.
4778 + *
4779 + * This program is distributed in the hope that it will be useful,
4780 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4781 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4782 + * GNU General Public License for more details.
4783 + *
4784 + * You should have received a copy of the GNU General Public License
4785 + * along with this program; if not, write to the Free Software
4786 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4787 + */
4788 +
4789 +/*
4790 + * debugfs interface
4791 + */
4792 +
4793 +#ifndef __DBGAUFS_H__
4794 +#define __DBGAUFS_H__
4795 +
4796 +#ifdef __KERNEL__
4797 +
4798 +struct super_block;
4799 +struct au_sbinfo;
4800 +
4801 +#ifdef CONFIG_DEBUG_FS
4802 +/* dbgaufs.c */
4803 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
4804 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
4805 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
4806 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
4807 +void dbgaufs_fin(void);
4808 +int __init dbgaufs_init(void);
4809 +#else
4810 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
4811 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
4812 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
4813 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
4814 +AuStubVoid(dbgaufs_fin, void)
4815 +AuStubInt0(__init dbgaufs_init, void)
4816 +#endif /* CONFIG_DEBUG_FS */
4817 +
4818 +#endif /* __KERNEL__ */
4819 +#endif /* __DBGAUFS_H__ */
4820 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
4821 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
4822 +++ linux/fs/aufs/dcsub.c       2012-03-19 12:24:13.679801090 +0100
4823 @@ -0,0 +1,243 @@
4824 +/*
4825 + * Copyright (C) 2005-2012 Junjiro R. Okajima
4826 + *
4827 + * This program, aufs is free software; you can redistribute it and/or modify
4828 + * it under the terms of the GNU General Public License as published by
4829 + * the Free Software Foundation; either version 2 of the License, or
4830 + * (at your option) any later version.
4831 + *
4832 + * This program is distributed in the hope that it will be useful,
4833 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4834 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4835 + * GNU General Public License for more details.
4836 + *
4837 + * You should have received a copy of the GNU General Public License
4838 + * along with this program; if not, write to the Free Software
4839 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4840 + */
4841 +
4842 +/*
4843 + * sub-routines for dentry cache
4844 + */
4845 +
4846 +#include "aufs.h"
4847 +
4848 +static void au_dpage_free(struct au_dpage *dpage)
4849 +{
4850 +       int i;
4851 +       struct dentry **p;
4852 +
4853 +       p = dpage->dentries;
4854 +       for (i = 0; i < dpage->ndentry; i++)
4855 +               dput(*p++);
4856 +       free_page((unsigned long)dpage->dentries);
4857 +}
4858 +
4859 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
4860 +{
4861 +       int err;
4862 +       void *p;
4863 +
4864 +       err = -ENOMEM;
4865 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
4866 +       if (unlikely(!dpages->dpages))
4867 +               goto out;
4868 +
4869 +       p = (void *)__get_free_page(gfp);
4870 +       if (unlikely(!p))
4871 +               goto out_dpages;
4872 +
4873 +       dpages->dpages[0].ndentry = 0;
4874 +       dpages->dpages[0].dentries = p;
4875 +       dpages->ndpage = 1;
4876 +       return 0; /* success */
4877 +
4878 +out_dpages:
4879 +       kfree(dpages->dpages);
4880 +out:
4881 +       return err;
4882 +}
4883 +
4884 +void au_dpages_free(struct au_dcsub_pages *dpages)
4885 +{
4886 +       int i;
4887 +       struct au_dpage *p;
4888 +
4889 +       p = dpages->dpages;
4890 +       for (i = 0; i < dpages->ndpage; i++)
4891 +               au_dpage_free(p++);
4892 +       kfree(dpages->dpages);
4893 +}
4894 +
4895 +static int au_dpages_append(struct au_dcsub_pages *dpages,
4896 +                           struct dentry *dentry, gfp_t gfp)
4897 +{
4898 +       int err, sz;
4899 +       struct au_dpage *dpage;
4900 +       void *p;
4901 +
4902 +       dpage = dpages->dpages + dpages->ndpage - 1;
4903 +       sz = PAGE_SIZE / sizeof(dentry);
4904 +       if (unlikely(dpage->ndentry >= sz)) {
4905 +               AuLabel(new dpage);
4906 +               err = -ENOMEM;
4907 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
4908 +               p = au_kzrealloc(dpages->dpages, sz,
4909 +                                sz + sizeof(*dpages->dpages), gfp);
4910 +               if (unlikely(!p))
4911 +                       goto out;
4912 +
4913 +               dpages->dpages = p;
4914 +               dpage = dpages->dpages + dpages->ndpage;
4915 +               p = (void *)__get_free_page(gfp);
4916 +               if (unlikely(!p))
4917 +                       goto out;
4918 +
4919 +               dpage->ndentry = 0;
4920 +               dpage->dentries = p;
4921 +               dpages->ndpage++;
4922 +       }
4923 +
4924 +       AuDebugOn(!dentry->d_count);
4925 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
4926 +       return 0; /* success */
4927 +
4928 +out:
4929 +       return err;
4930 +}
4931 +
4932 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
4933 +                  au_dpages_test test, void *arg)
4934 +{
4935 +       int err;
4936 +       struct dentry *this_parent;
4937 +       struct list_head *next;
4938 +       struct super_block *sb = root->d_sb;
4939 +
4940 +       err = 0;
4941 +       write_seqlock(&rename_lock);
4942 +       this_parent = root;
4943 +       spin_lock(&this_parent->d_lock);
4944 +repeat:
4945 +       next = this_parent->d_subdirs.next;
4946 +resume:
4947 +       if (this_parent->d_sb == sb
4948 +           && !IS_ROOT(this_parent)
4949 +           && au_di(this_parent)
4950 +           && this_parent->d_count
4951 +           && (!test || test(this_parent, arg))) {
4952 +               err = au_dpages_append(dpages, this_parent, GFP_ATOMIC);
4953 +               if (unlikely(err))
4954 +                       goto out;
4955 +       }
4956 +
4957 +       while (next != &this_parent->d_subdirs) {
4958 +               struct list_head *tmp = next;
4959 +               struct dentry *dentry = list_entry(tmp, struct dentry,
4960 +                                                  d_u.d_child);
4961 +
4962 +               next = tmp->next;
4963 +               spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
4964 +               if (dentry->d_count) {
4965 +                       if (!list_empty(&dentry->d_subdirs)) {
4966 +                               spin_unlock(&this_parent->d_lock);
4967 +                               spin_release(&dentry->d_lock.dep_map, 1,
4968 +                                            _RET_IP_);
4969 +                               this_parent = dentry;
4970 +                               spin_acquire(&this_parent->d_lock.dep_map, 0, 1,
4971 +                                            _RET_IP_);
4972 +                               goto repeat;
4973 +                       }
4974 +                       if (dentry->d_sb == sb
4975 +                           && au_di(dentry)
4976 +                           && (!test || test(dentry, arg)))
4977 +                               err = au_dpages_append(dpages, dentry,
4978 +                                                      GFP_ATOMIC);
4979 +               }
4980 +               spin_unlock(&dentry->d_lock);
4981 +               if (unlikely(err))
4982 +                       goto out;
4983 +       }
4984 +
4985 +       if (this_parent != root) {
4986 +               struct dentry *tmp;
4987 +               struct dentry *child;
4988 +
4989 +               tmp = this_parent->d_parent;
4990 +               rcu_read_lock();
4991 +               spin_unlock(&this_parent->d_lock);
4992 +               child = this_parent;
4993 +               this_parent = tmp;
4994 +               spin_lock(&this_parent->d_lock);
4995 +               rcu_read_unlock();
4996 +               next = child->d_u.d_child.next;
4997 +               goto resume;
4998 +       }
4999 +
5000 +out:
5001 +       spin_unlock(&this_parent->d_lock);
5002 +       write_sequnlock(&rename_lock);
5003 +       return err;
5004 +}
5005 +
5006 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5007 +                      int do_include, au_dpages_test test, void *arg)
5008 +{
5009 +       int err;
5010 +
5011 +       err = 0;
5012 +       write_seqlock(&rename_lock);
5013 +       spin_lock(&dentry->d_lock);
5014 +       if (do_include
5015 +           && dentry->d_count
5016 +           && (!test || test(dentry, arg)))
5017 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5018 +       spin_unlock(&dentry->d_lock);
5019 +       if (unlikely(err))
5020 +               goto out;
5021 +
5022 +       /*
5023 +        * vfsmount_lock is unnecessary since this is a traverse in a single
5024 +        * mount
5025 +        */
5026 +       while (!IS_ROOT(dentry)) {
5027 +               dentry = dentry->d_parent; /* rename_lock is locked */
5028 +               spin_lock(&dentry->d_lock);
5029 +               if (dentry->d_count
5030 +                   && (!test || test(dentry, arg)))
5031 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5032 +               spin_unlock(&dentry->d_lock);
5033 +               if (unlikely(err))
5034 +                       break;
5035 +       }
5036 +
5037 +out:
5038 +       write_sequnlock(&rename_lock);
5039 +       return err;
5040 +}
5041 +
5042 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
5043 +{
5044 +       return au_di(dentry) && dentry->d_sb == arg;
5045 +}
5046 +
5047 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5048 +                           struct dentry *dentry, int do_include)
5049 +{
5050 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
5051 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
5052 +}
5053 +
5054 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
5055 +{
5056 +       struct path path[2] = {
5057 +               {
5058 +                       .dentry = d1
5059 +               },
5060 +               {
5061 +                       .dentry = d2
5062 +               }
5063 +       };
5064 +
5065 +       return path_is_under(path + 0, path + 1);
5066 +}
5067 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
5068 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
5069 +++ linux/fs/aufs/dcsub.h       2012-03-19 12:24:13.679801090 +0100
5070 @@ -0,0 +1,94 @@
5071 +/*
5072 + * Copyright (C) 2005-2012 Junjiro R. Okajima
5073 + *
5074 + * This program, aufs is free software; you can redistribute it and/or modify
5075 + * it under the terms of the GNU General Public License as published by
5076 + * the Free Software Foundation; either version 2 of the License, or
5077 + * (at your option) any later version.
5078 + *
5079 + * This program is distributed in the hope that it will be useful,
5080 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5081 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5082 + * GNU General Public License for more details.
5083 + *
5084 + * You should have received a copy of the GNU General Public License
5085 + * along with this program; if not, write to the Free Software
5086 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5087 + */
5088 +
5089 +/*
5090 + * sub-routines for dentry cache
5091 + */
5092 +
5093 +#ifndef __AUFS_DCSUB_H__
5094 +#define __AUFS_DCSUB_H__
5095 +
5096 +#ifdef __KERNEL__
5097 +
5098 +#include <linux/dcache.h>
5099 +#include <linux/fs.h>
5100 +
5101 +struct dentry;
5102 +
5103 +struct au_dpage {
5104 +       int ndentry;
5105 +       struct dentry **dentries;
5106 +};
5107 +
5108 +struct au_dcsub_pages {
5109 +       int ndpage;
5110 +       struct au_dpage *dpages;
5111 +};
5112 +
5113 +/* ---------------------------------------------------------------------- */
5114 +
5115 +/* dcsub.c */
5116 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
5117 +void au_dpages_free(struct au_dcsub_pages *dpages);
5118 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
5119 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5120 +                  au_dpages_test test, void *arg);
5121 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5122 +                      int do_include, au_dpages_test test, void *arg);
5123 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5124 +                           struct dentry *dentry, int do_include);
5125 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
5126 +
5127 +/* ---------------------------------------------------------------------- */
5128 +
5129 +static inline int au_d_hashed_positive(struct dentry *d)
5130 +{
5131 +       int err;
5132 +       struct inode *inode = d->d_inode;
5133 +       err = 0;
5134 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
5135 +               err = -ENOENT;
5136 +       return err;
5137 +}
5138 +
5139 +static inline int au_d_alive(struct dentry *d)
5140 +{
5141 +       int err;
5142 +       struct inode *inode;
5143 +       err = 0;
5144 +       if (!IS_ROOT(d))
5145 +               err = au_d_hashed_positive(d);
5146 +       else {
5147 +               inode = d->d_inode;
5148 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
5149 +                       err = -ENOENT;
5150 +       }
5151 +       return err;
5152 +}
5153 +
5154 +static inline int au_alive_dir(struct dentry *d)
5155 +{
5156 +       int err;
5157 +       err = au_d_alive(d);
5158 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
5159 +               err = -ENOENT;
5160 +       return err;
5161 +}
5162 +
5163 +#endif /* __KERNEL__ */
5164 +#endif /* __AUFS_DCSUB_H__ */
5165 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
5166 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
5167 +++ linux/fs/aufs/debug.c       2012-03-19 12:24:13.679801090 +0100
5168 @@ -0,0 +1,489 @@
5169 +/*
5170 + * Copyright (C) 2005-2012 Junjiro R. Okajima
5171 + *
5172 + * This program, aufs is free software; you can redistribute it and/or modify
5173 + * it under the terms of the GNU General Public License as published by
5174 + * the Free Software Foundation; either version 2 of the License, or
5175 + * (at your option) any later version.
5176 + *
5177 + * This program is distributed in the hope that it will be useful,
5178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5180 + * GNU General Public License for more details.
5181 + *
5182 + * You should have received a copy of the GNU General Public License
5183 + * along with this program; if not, write to the Free Software
5184 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5185 + */
5186 +
5187 +/*
5188 + * debug print functions
5189 + */
5190 +
5191 +#include <linux/vt_kern.h>
5192 +#include "aufs.h"
5193 +
5194 +int aufs_debug;
5195 +MODULE_PARM_DESC(debug, "debug print");
5196 +module_param_named(debug, aufs_debug, int, S_IRUGO | S_IWUSR | S_IWGRP);
5197 +
5198 +char *au_plevel = KERN_DEBUG;
5199 +#define dpri(fmt, ...) do {                                    \
5200 +       if ((au_plevel                                          \
5201 +            && strcmp(au_plevel, KERN_DEBUG))                  \
5202 +           || au_debug_test())                                 \
5203 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
5204 +} while (0)
5205 +
5206 +/* ---------------------------------------------------------------------- */
5207 +
5208 +void au_dpri_whlist(struct au_nhash *whlist)
5209 +{
5210 +       unsigned long ul, n;
5211 +       struct hlist_head *head;
5212 +       struct au_vdir_wh *tpos;
5213 +       struct hlist_node *pos;
5214 +
5215 +       n = whlist->nh_num;
5216 +       head = whlist->nh_head;
5217 +       for (ul = 0; ul < n; ul++) {
5218 +               hlist_for_each_entry(tpos, pos, head, wh_hash)
5219 +                       dpri("b%d, %.*s, %d\n",
5220 +                            tpos->wh_bindex,
5221 +                            tpos->wh_str.len, tpos->wh_str.name,
5222 +                            tpos->wh_str.len);
5223 +               head++;
5224 +       }
5225 +}
5226 +
5227 +void au_dpri_vdir(struct au_vdir *vdir)
5228 +{
5229 +       unsigned long ul;
5230 +       union au_vdir_deblk_p p;
5231 +       unsigned char *o;
5232 +
5233 +       if (!vdir || IS_ERR(vdir)) {
5234 +               dpri("err %ld\n", PTR_ERR(vdir));
5235 +               return;
5236 +       }
5237 +
5238 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
5239 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
5240 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
5241 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
5242 +               p.deblk = vdir->vd_deblk[ul];
5243 +               o = p.deblk;
5244 +               dpri("[%lu]: %p\n", ul, o);
5245 +       }
5246 +}
5247 +
5248 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
5249 +                       struct dentry *wh)
5250 +{
5251 +       char *n = NULL;
5252 +       int l = 0;
5253 +
5254 +       if (!inode || IS_ERR(inode)) {
5255 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
5256 +               return -1;
5257 +       }
5258 +
5259 +       /* the type of i_blocks depends upon CONFIG_LSF */
5260 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
5261 +                    && sizeof(inode->i_blocks) != sizeof(u64));
5262 +       if (wh) {
5263 +               n = (void *)wh->d_name.name;
5264 +               l = wh->d_name.len;
5265 +       }
5266 +
5267 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
5268 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
5269 +            bindex, inode,
5270 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
5271 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
5272 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
5273 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
5274 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
5275 +            inode->i_state, inode->i_flags, inode->i_version,
5276 +            inode->i_generation,
5277 +            l ? ", wh " : "", l, n);
5278 +       return 0;
5279 +}
5280 +
5281 +void au_dpri_inode(struct inode *inode)
5282 +{
5283 +       struct au_iinfo *iinfo;
5284 +       aufs_bindex_t bindex;
5285 +       int err, hn;
5286 +
5287 +       err = do_pri_inode(-1, inode, -1, NULL);
5288 +       if (err || !au_test_aufs(inode->i_sb))
5289 +               return;
5290 +
5291 +       iinfo = au_ii(inode);
5292 +       if (!iinfo)
5293 +               return;
5294 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
5295 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode));
5296 +       if (iinfo->ii_bstart < 0)
5297 +               return;
5298 +       hn = 0;
5299 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
5300 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
5301 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
5302 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
5303 +       }
5304 +}
5305 +
5306 +void au_dpri_dalias(struct inode *inode)
5307 +{
5308 +       struct dentry *d;
5309 +
5310 +       spin_lock(&inode->i_lock);
5311 +       list_for_each_entry(d, &inode->i_dentry, d_alias)
5312 +               au_dpri_dentry(d);
5313 +       spin_unlock(&inode->i_lock);
5314 +}
5315 +
5316 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
5317 +{
5318 +       struct dentry *wh = NULL;
5319 +       int hn;
5320 +
5321 +       if (!dentry || IS_ERR(dentry)) {
5322 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
5323 +               return -1;
5324 +       }
5325 +       /* do not call dget_parent() here */
5326 +       /* note: access d_xxx without d_lock */
5327 +       dpri("d%d: %.*s?/%.*s, %s, cnt %d, flags 0x%x\n",
5328 +            bindex,
5329 +            AuDLNPair(dentry->d_parent), AuDLNPair(dentry),
5330 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
5331 +            dentry->d_count, dentry->d_flags);
5332 +       hn = -1;
5333 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
5334 +               struct au_iinfo *iinfo = au_ii(dentry->d_inode);
5335 +               if (iinfo) {
5336 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
5337 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
5338 +               }
5339 +       }
5340 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
5341 +       return 0;
5342 +}
5343 +
5344 +void au_dpri_dentry(struct dentry *dentry)
5345 +{
5346 +       struct au_dinfo *dinfo;
5347 +       aufs_bindex_t bindex;
5348 +       int err;
5349 +       struct au_hdentry *hdp;
5350 +
5351 +       err = do_pri_dentry(-1, dentry);
5352 +       if (err || !au_test_aufs(dentry->d_sb))
5353 +               return;
5354 +
5355 +       dinfo = au_di(dentry);
5356 +       if (!dinfo)
5357 +               return;
5358 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d\n",
5359 +            dinfo->di_bstart, dinfo->di_bend,
5360 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry));
5361 +       if (dinfo->di_bstart < 0)
5362 +               return;
5363 +       hdp = dinfo->di_hdentry;
5364 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
5365 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
5366 +}
5367 +
5368 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
5369 +{
5370 +       char a[32];
5371 +
5372 +       if (!file || IS_ERR(file)) {
5373 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
5374 +               return -1;
5375 +       }
5376 +       a[0] = 0;
5377 +       if (bindex < 0
5378 +           && file->f_dentry
5379 +           && au_test_aufs(file->f_dentry->d_sb)
5380 +           && au_fi(file))
5381 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
5382 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
5383 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
5384 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
5385 +            file->f_version, file->f_pos, a);
5386 +       if (file->f_dentry)
5387 +               do_pri_dentry(bindex, file->f_dentry);
5388 +       return 0;
5389 +}
5390 +
5391 +void au_dpri_file(struct file *file)
5392 +{
5393 +       struct au_finfo *finfo;
5394 +       struct au_fidir *fidir;
5395 +       struct au_hfile *hfile;
5396 +       aufs_bindex_t bindex;
5397 +       int err;
5398 +
5399 +       err = do_pri_file(-1, file);
5400 +       if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
5401 +               return;
5402 +
5403 +       finfo = au_fi(file);
5404 +       if (!finfo)
5405 +               return;
5406 +       if (finfo->fi_btop < 0)
5407 +               return;
5408 +       fidir = finfo->fi_hdir;
5409 +       if (!fidir)
5410 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
5411 +       else
5412 +               for (bindex = finfo->fi_btop;
5413 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
5414 +                    bindex++) {
5415 +                       hfile = fidir->fd_hfile + bindex;
5416 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
5417 +               }
5418 +}
5419 +
5420 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
5421 +{
5422 +       struct vfsmount *mnt;
5423 +       struct super_block *sb;
5424 +
5425 +       if (!br || IS_ERR(br))
5426 +               goto out;
5427 +       mnt = br->br_mnt;
5428 +       if (!mnt || IS_ERR(mnt))
5429 +               goto out;
5430 +       sb = mnt->mnt_sb;
5431 +       if (!sb || IS_ERR(sb))
5432 +               goto out;
5433 +
5434 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
5435 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
5436 +            "xino %d\n",
5437 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
5438 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
5439 +            sb->s_flags, sb->s_count,
5440 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
5441 +       return 0;
5442 +
5443 +out:
5444 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
5445 +       return -1;
5446 +}
5447 +
5448 +void au_dpri_sb(struct super_block *sb)
5449 +{
5450 +       struct au_sbinfo *sbinfo;
5451 +       aufs_bindex_t bindex;
5452 +       int err;
5453 +       /* to reuduce stack size */
5454 +       struct {
5455 +               struct vfsmount mnt;
5456 +               struct au_branch fake;
5457 +       } *a;
5458 +
5459 +       /* this function can be called from magic sysrq */
5460 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
5461 +       if (unlikely(!a)) {
5462 +               dpri("no memory\n");
5463 +               return;
5464 +       }
5465 +
5466 +       a->mnt.mnt_sb = sb;
5467 +       a->fake.br_perm = 0;
5468 +       a->fake.br_mnt = &a->mnt;
5469 +       a->fake.br_xino.xi_file = NULL;
5470 +       atomic_set(&a->fake.br_count, 0);
5471 +       smp_mb(); /* atomic_set */
5472 +       err = do_pri_br(-1, &a->fake);
5473 +       kfree(a);
5474 +       dpri("dev 0x%x\n", sb->s_dev);
5475 +       if (err || !au_test_aufs(sb))
5476 +               return;
5477 +
5478 +       sbinfo = au_sbi(sb);
5479 +       if (!sbinfo)
5480 +               return;
5481 +       dpri("nw %d, gen %u, kobj %d\n",
5482 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
5483 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
5484 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
5485 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
5486 +}
5487 +
5488 +/* ---------------------------------------------------------------------- */
5489 +
5490 +void au_dbg_sleep_jiffy(int jiffy)
5491 +{
5492 +       while (jiffy)
5493 +               jiffy = schedule_timeout_uninterruptible(jiffy);
5494 +}
5495 +
5496 +void au_dbg_iattr(struct iattr *ia)
5497 +{
5498 +#define AuBit(name)    if (ia->ia_valid & ATTR_ ## name) \
5499 +                               dpri(#name "\n")
5500 +       AuBit(MODE);
5501 +       AuBit(UID);
5502 +       AuBit(GID);
5503 +       AuBit(SIZE);
5504 +       AuBit(ATIME);
5505 +       AuBit(MTIME);
5506 +       AuBit(CTIME);
5507 +       AuBit(ATIME_SET);
5508 +       AuBit(MTIME_SET);
5509 +       AuBit(FORCE);
5510 +       AuBit(ATTR_FLAG);
5511 +       AuBit(KILL_SUID);
5512 +       AuBit(KILL_SGID);
5513 +       AuBit(FILE);
5514 +       AuBit(KILL_PRIV);
5515 +       AuBit(OPEN);
5516 +       AuBit(TIMES_SET);
5517 +#undef AuBit
5518 +       dpri("ia_file %p\n", ia->ia_file);
5519 +}
5520 +
5521 +/* ---------------------------------------------------------------------- */
5522 +
5523 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
5524 +{
5525 +       struct inode *h_inode, *inode = dentry->d_inode;
5526 +       struct dentry *h_dentry;
5527 +       aufs_bindex_t bindex, bend, bi;
5528 +
5529 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
5530 +               return;
5531 +
5532 +       bend = au_dbend(dentry);
5533 +       bi = au_ibend(inode);
5534 +       if (bi < bend)
5535 +               bend = bi;
5536 +       bindex = au_dbstart(dentry);
5537 +       bi = au_ibstart(inode);
5538 +       if (bi > bindex)
5539 +               bindex = bi;
5540 +
5541 +       for (; bindex <= bend; bindex++) {
5542 +               h_dentry = au_h_dptr(dentry, bindex);
5543 +               if (!h_dentry)
5544 +                       continue;
5545 +               h_inode = au_h_iptr(inode, bindex);
5546 +               if (unlikely(h_inode != h_dentry->d_inode)) {
5547 +                       int old = au_debug_test();
5548 +                       if (!old)
5549 +                               au_debug(1);
5550 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
5551 +                       AuDbgDentry(dentry);
5552 +                       AuDbgInode(inode);
5553 +                       if (!old)
5554 +                               au_debug(0);
5555 +                       BUG();
5556 +               }
5557 +       }
5558 +}
5559 +
5560 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
5561 +{
5562 +       struct dentry *parent;
5563 +
5564 +       parent = dget_parent(dentry);
5565 +       AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
5566 +       AuDebugOn(IS_ROOT(dentry));
5567 +       AuDebugOn(au_digen_test(parent, sigen));
5568 +       dput(parent);
5569 +}
5570 +
5571 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
5572 +{
5573 +       struct dentry *parent;
5574 +       struct inode *inode;
5575 +
5576 +       parent = dget_parent(dentry);
5577 +       inode = dentry->d_inode;
5578 +       AuDebugOn(inode && S_ISDIR(dentry->d_inode->i_mode));
5579 +       AuDebugOn(au_digen_test(parent, sigen));
5580 +       dput(parent);
5581 +}
5582 +
5583 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
5584 +{
5585 +       int err, i, j;
5586 +       struct au_dcsub_pages dpages;
5587 +       struct au_dpage *dpage;
5588 +       struct dentry **dentries;
5589 +
5590 +       err = au_dpages_init(&dpages, GFP_NOFS);
5591 +       AuDebugOn(err);
5592 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
5593 +       AuDebugOn(err);
5594 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
5595 +               dpage = dpages.dpages + i;
5596 +               dentries = dpage->dentries;
5597 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
5598 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
5599 +       }
5600 +       au_dpages_free(&dpages);
5601 +}
5602 +
5603 +void au_dbg_verify_kthread(void)
5604 +{
5605 +       if (au_wkq_test()) {
5606 +               au_dbg_blocked();
5607 +               /*
5608 +                * It may be recursive, but udba=notify between two aufs mounts,
5609 +                * where a single ro branch is shared, is not a problem.
5610 +                */
5611 +               /* WARN_ON(1); */
5612 +       }
5613 +}
5614 +
5615 +/* ---------------------------------------------------------------------- */
5616 +
5617 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
5618 +{
5619 +#ifdef AuForceNoPlink
5620 +       au_opt_clr(sbinfo->si_mntflags, PLINK);
5621 +#endif
5622 +#ifdef AuForceNoXino
5623 +       au_opt_clr(sbinfo->si_mntflags, XINO);
5624 +#endif
5625 +#ifdef AuForceNoRefrof
5626 +       au_opt_clr(sbinfo->si_mntflags, REFROF);
5627 +#endif
5628 +#ifdef AuForceHnotify
5629 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_HNOTIFY);
5630 +#endif
5631 +#ifdef AuForceRd0
5632 +       sbinfo->si_rdblk = 0;
5633 +       sbinfo->si_rdhash = 0;
5634 +#endif
5635 +}
5636 +
5637 +int __init au_debug_init(void)
5638 +{
5639 +       aufs_bindex_t bindex;
5640 +       struct au_vdir_destr destr;
5641 +
5642 +       bindex = -1;
5643 +       AuDebugOn(bindex >= 0);
5644 +
5645 +       destr.len = -1;
5646 +       AuDebugOn(destr.len < NAME_MAX);
5647 +
5648 +#ifdef CONFIG_4KSTACKS
5649 +       pr_warning("CONFIG_4KSTACKS is defined.\n");
5650 +#endif
5651 +
5652 +#ifdef AuForceNoBrs
5653 +       sysaufs_brs = 0;
5654 +#endif
5655 +
5656 +       return 0;
5657 +}
5658 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
5659 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
5660 +++ linux/fs/aufs/debug.h       2012-03-19 12:24:13.679801090 +0100
5661 @@ -0,0 +1,243 @@
5662 +/*
5663 + * Copyright (C) 2005-2012 Junjiro R. Okajima
5664 + *
5665 + * This program, aufs is free software; you can redistribute it and/or modify
5666 + * it under the terms of the GNU General Public License as published by
5667 + * the Free Software Foundation; either version 2 of the License, or
5668 + * (at your option) any later version.
5669 + *
5670 + * This program is distributed in the hope that it will be useful,
5671 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5672 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5673 + * GNU General Public License for more details.
5674 + *
5675 + * You should have received a copy of the GNU General Public License
5676 + * along with this program; if not, write to the Free Software
5677 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5678 + */
5679 +
5680 +/*
5681 + * debug print functions
5682 + */
5683 +
5684 +#ifndef __AUFS_DEBUG_H__
5685 +#define __AUFS_DEBUG_H__
5686 +
5687 +#ifdef __KERNEL__
5688 +
5689 +#include <asm/system.h>
5690 +#include <linux/module.h>
5691 +#include <linux/kallsyms.h>
5692 +#include <linux/sysrq.h>
5693 +
5694 +#ifdef CONFIG_AUFS_DEBUG
5695 +#define AuDebugOn(a)           BUG_ON(a)
5696 +
5697 +/* module parameter */
5698 +extern int aufs_debug;
5699 +static inline void au_debug(int n)
5700 +{
5701 +       aufs_debug = n;
5702 +       smp_mb();
5703 +}
5704 +
5705 +static inline int au_debug_test(void)
5706 +{
5707 +       return aufs_debug;
5708 +}
5709 +#else
5710 +#define AuDebugOn(a)           do {} while (0)
5711 +AuStubVoid(au_debug, int n)
5712 +AuStubInt0(au_debug_test, void)
5713 +#endif /* CONFIG_AUFS_DEBUG */
5714 +
5715 +/* ---------------------------------------------------------------------- */
5716 +
5717 +/* debug print */
5718 +
5719 +#define AuDbg(fmt, ...) do { \
5720 +       if (au_debug_test()) \
5721 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
5722 +} while (0)
5723 +#define AuLabel(l)             AuDbg(#l "\n")
5724 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
5725 +#define AuWarn1(fmt, ...) do { \
5726 +       static unsigned char _c; \
5727 +       if (!_c++) \
5728 +               pr_warning(fmt, ##__VA_ARGS__); \
5729 +} while (0)
5730 +
5731 +#define AuErr1(fmt, ...) do { \
5732 +       static unsigned char _c; \
5733 +       if (!_c++) \
5734 +               pr_err(fmt, ##__VA_ARGS__); \
5735 +} while (0)
5736 +
5737 +#define AuIOErr1(fmt, ...) do { \
5738 +       static unsigned char _c; \
5739 +       if (!_c++) \
5740 +               AuIOErr(fmt, ##__VA_ARGS__); \
5741 +} while (0)
5742 +
5743 +#define AuUnsupportMsg "This operation is not supported." \
5744 +                       " Please report this application to aufs-users ML."
5745 +#define AuUnsupport(fmt, ...) do { \
5746 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
5747 +       dump_stack(); \
5748 +} while (0)
5749 +
5750 +#define AuTraceErr(e) do { \
5751 +       if (unlikely((e) < 0)) \
5752 +               AuDbg("err %d\n", (int)(e)); \
5753 +} while (0)
5754 +
5755 +#define AuTraceErrPtr(p) do { \
5756 +       if (IS_ERR(p)) \
5757 +               AuDbg("err %ld\n", PTR_ERR(p)); \
5758 +} while (0)
5759 +
5760 +/* dirty macros for debug print, use with "%.*s" and caution */
5761 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
5762 +#define AuDLNPair(d)           AuLNPair(&(d)->d_name)
5763 +
5764 +/* ---------------------------------------------------------------------- */
5765 +
5766 +struct au_sbinfo;
5767 +struct au_finfo;
5768 +struct dentry;
5769 +#ifdef CONFIG_AUFS_DEBUG
5770 +extern char *au_plevel;
5771 +struct au_nhash;
5772 +void au_dpri_whlist(struct au_nhash *whlist);
5773 +struct au_vdir;
5774 +void au_dpri_vdir(struct au_vdir *vdir);
5775 +struct inode;
5776 +void au_dpri_inode(struct inode *inode);
5777 +void au_dpri_dalias(struct inode *inode);
5778 +void au_dpri_dentry(struct dentry *dentry);
5779 +struct file;
5780 +void au_dpri_file(struct file *filp);
5781 +struct super_block;
5782 +void au_dpri_sb(struct super_block *sb);
5783 +
5784 +void au_dbg_sleep_jiffy(int jiffy);
5785 +struct iattr;
5786 +void au_dbg_iattr(struct iattr *ia);
5787 +
5788 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
5789 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
5790 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen);
5791 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen);
5792 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
5793 +void au_dbg_verify_kthread(void);
5794 +
5795 +int __init au_debug_init(void);
5796 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
5797 +#define AuDbgWhlist(w) do { \
5798 +       AuDbg(#w "\n"); \
5799 +       au_dpri_whlist(w); \
5800 +} while (0)
5801 +
5802 +#define AuDbgVdir(v) do { \
5803 +       AuDbg(#v "\n"); \
5804 +       au_dpri_vdir(v); \
5805 +} while (0)
5806 +
5807 +#define AuDbgInode(i) do { \
5808 +       AuDbg(#i "\n"); \
5809 +       au_dpri_inode(i); \
5810 +} while (0)
5811 +
5812 +#define AuDbgDAlias(i) do { \
5813 +       AuDbg(#i "\n"); \
5814 +       au_dpri_dalias(i); \
5815 +} while (0)
5816 +
5817 +#define AuDbgDentry(d) do { \
5818 +       AuDbg(#d "\n"); \
5819 +       au_dpri_dentry(d); \
5820 +} while (0)
5821 +
5822 +#define AuDbgFile(f) do { \
5823 +       AuDbg(#f "\n"); \
5824 +       au_dpri_file(f); \
5825 +} while (0)
5826 +
5827 +#define AuDbgSb(sb) do { \
5828 +       AuDbg(#sb "\n"); \
5829 +       au_dpri_sb(sb); \
5830 +} while (0)
5831 +
5832 +#define AuDbgSleep(sec) do { \
5833 +       AuDbg("sleep %d sec\n", sec); \
5834 +       ssleep(sec); \
5835 +} while (0)
5836 +
5837 +#define AuDbgSleepJiffy(jiffy) do { \
5838 +       AuDbg("sleep %d jiffies\n", jiffy); \
5839 +       au_dbg_sleep_jiffy(jiffy); \
5840 +} while (0)
5841 +
5842 +#define AuDbgIAttr(ia) do { \
5843 +       AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
5844 +       au_dbg_iattr(ia); \
5845 +} while (0)
5846 +
5847 +#define AuDbgSym(addr) do {                            \
5848 +       char sym[KSYM_SYMBOL_LEN];                      \
5849 +       sprint_symbol(sym, (unsigned long)addr);        \
5850 +       AuDbg("%s\n", sym);                             \
5851 +} while (0)
5852 +
5853 +#define AuInfoSym(addr) do {                           \
5854 +       char sym[KSYM_SYMBOL_LEN];                      \
5855 +       sprint_symbol(sym, (unsigned long)addr);        \
5856 +       AuInfo("%s\n", sym);                            \
5857 +} while (0)
5858 +#else
5859 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
5860 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
5861 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
5862 +          unsigned int sigen)
5863 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
5864 +AuStubVoid(au_dbg_verify_kthread, void)
5865 +AuStubInt0(__init au_debug_init, void)
5866 +AuStubVoid(au_debug_sbinfo_init, struct au_sbinfo *sbinfo)
5867 +
5868 +#define AuDbgWhlist(w)         do {} while (0)
5869 +#define AuDbgVdir(v)           do {} while (0)
5870 +#define AuDbgInode(i)          do {} while (0)
5871 +#define AuDbgDAlias(i)         do {} while (0)
5872 +#define AuDbgDentry(d)         do {} while (0)
5873 +#define AuDbgFile(f)           do {} while (0)
5874 +#define AuDbgSb(sb)            do {} while (0)
5875 +#define AuDbgSleep(sec)                do {} while (0)
5876 +#define AuDbgSleepJiffy(jiffy) do {} while (0)
5877 +#define AuDbgIAttr(ia)         do {} while (0)
5878 +#define AuDbgSym(addr)         do {} while (0)
5879 +#define AuInfoSym(addr)                do {} while (0)
5880 +#endif /* CONFIG_AUFS_DEBUG */
5881 +
5882 +/* ---------------------------------------------------------------------- */
5883 +
5884 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
5885 +int __init au_sysrq_init(void);
5886 +void au_sysrq_fin(void);
5887 +
5888 +#ifdef CONFIG_HW_CONSOLE
5889 +#define au_dbg_blocked() do { \
5890 +       WARN_ON(1); \
5891 +       handle_sysrq('w'); \
5892 +} while (0)
5893 +#else
5894 +AuStubVoid(au_dbg_blocked, void)
5895 +#endif
5896 +
5897 +#else
5898 +AuStubInt0(__init au_sysrq_init, void)
5899 +AuStubVoid(au_sysrq_fin, void)
5900 +AuStubVoid(au_dbg_blocked, void)
5901 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
5902 +
5903 +#endif /* __KERNEL__ */
5904 +#endif /* __AUFS_DEBUG_H__ */
5905 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
5906 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
5907 +++ linux/fs/aufs/dentry.c      2012-03-19 12:24:13.679801090 +0100
5908 @@ -0,0 +1,1140 @@
5909 +/*
5910 + * Copyright (C) 2005-2012 Junjiro R. Okajima
5911 + *
5912 + * This program, aufs is free software; you can redistribute it and/or modify
5913 + * it under the terms of the GNU General Public License as published by
5914 + * the Free Software Foundation; either version 2 of the License, or
5915 + * (at your option) any later version.
5916 + *
5917 + * This program is distributed in the hope that it will be useful,
5918 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5919 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5920 + * GNU General Public License for more details.
5921 + *
5922 + * You should have received a copy of the GNU General Public License
5923 + * along with this program; if not, write to the Free Software
5924 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5925 + */
5926 +
5927 +/*
5928 + * lookup and dentry operations
5929 + */
5930 +
5931 +#include <linux/namei.h>
5932 +#include "aufs.h"
5933 +
5934 +static void au_h_nd(struct nameidata *h_nd, struct nameidata *nd)
5935 +{
5936 +       if (nd) {
5937 +               *h_nd = *nd;
5938 +
5939 +               /*
5940 +                * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
5941 +                * due to whiteout and branch permission.
5942 +                */
5943 +               h_nd->flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
5944 +                                | LOOKUP_FOLLOW | LOOKUP_EXCL);
5945 +               /* unnecessary? */
5946 +               h_nd->intent.open.file = NULL;
5947 +       } else
5948 +               memset(h_nd, 0, sizeof(*h_nd));
5949 +}
5950 +
5951 +struct au_lkup_one_args {
5952 +       struct dentry **errp;
5953 +       struct qstr *name;
5954 +       struct dentry *h_parent;
5955 +       struct au_branch *br;
5956 +       struct nameidata *nd;
5957 +};
5958 +
5959 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
5960 +                          struct au_branch *br, struct nameidata *nd)
5961 +{
5962 +       struct dentry *h_dentry;
5963 +       int err;
5964 +       struct nameidata h_nd;
5965 +
5966 +       if (au_test_fs_null_nd(h_parent->d_sb))
5967 +               return vfsub_lookup_one_len(name->name, h_parent, name->len);
5968 +
5969 +       au_h_nd(&h_nd, nd);
5970 +       h_nd.path.dentry = h_parent;
5971 +       h_nd.path.mnt = br->br_mnt;
5972 +
5973 +       err = vfsub_name_hash(name->name, &h_nd.last, name->len);
5974 +       h_dentry = ERR_PTR(err);
5975 +       if (!err) {
5976 +               path_get(&h_nd.path);
5977 +               h_dentry = vfsub_lookup_hash(&h_nd);
5978 +               path_put(&h_nd.path);
5979 +       }
5980 +
5981 +       AuTraceErrPtr(h_dentry);
5982 +       return h_dentry;
5983 +}
5984 +
5985 +static void au_call_lkup_one(void *args)
5986 +{
5987 +       struct au_lkup_one_args *a = args;
5988 +       *a->errp = au_lkup_one(a->name, a->h_parent, a->br, a->nd);
5989 +}
5990 +
5991 +#define AuLkup_ALLOW_NEG       1
5992 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
5993 +#define au_fset_lkup(flags, name) \
5994 +       do { (flags) |= AuLkup_##name; } while (0)
5995 +#define au_fclr_lkup(flags, name) \
5996 +       do { (flags) &= ~AuLkup_##name; } while (0)
5997 +
5998 +struct au_do_lookup_args {
5999 +       unsigned int            flags;
6000 +       mode_t                  type;
6001 +       struct nameidata        *nd;
6002 +};
6003 +
6004 +/*
6005 + * returns positive/negative dentry, NULL or an error.
6006 + * NULL means whiteout-ed or not-found.
6007 + */
6008 +static struct dentry*
6009 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
6010 +            aufs_bindex_t bindex, struct qstr *wh_name,
6011 +            struct au_do_lookup_args *args)
6012 +{
6013 +       struct dentry *h_dentry;
6014 +       struct inode *h_inode, *inode;
6015 +       struct au_branch *br;
6016 +       int wh_found, opq;
6017 +       unsigned char wh_able;
6018 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
6019 +
6020 +       wh_found = 0;
6021 +       br = au_sbr(dentry->d_sb, bindex);
6022 +       wh_able = !!au_br_whable(br->br_perm);
6023 +       if (wh_able)
6024 +               wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6025 +       h_dentry = ERR_PTR(wh_found);
6026 +       if (!wh_found)
6027 +               goto real_lookup;
6028 +       if (unlikely(wh_found < 0))
6029 +               goto out;
6030 +
6031 +       /* We found a whiteout */
6032 +       /* au_set_dbend(dentry, bindex); */
6033 +       au_set_dbwh(dentry, bindex);
6034 +       if (!allow_neg)
6035 +               return NULL; /* success */
6036 +
6037 +real_lookup:
6038 +       h_dentry = au_lkup_one(&dentry->d_name, h_parent, br, args->nd);
6039 +       if (IS_ERR(h_dentry))
6040 +               goto out;
6041 +
6042 +       h_inode = h_dentry->d_inode;
6043 +       if (!h_inode) {
6044 +               if (!allow_neg)
6045 +                       goto out_neg;
6046 +       } else if (wh_found
6047 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
6048 +               goto out_neg;
6049 +
6050 +       if (au_dbend(dentry) <= bindex)
6051 +               au_set_dbend(dentry, bindex);
6052 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
6053 +               au_set_dbstart(dentry, bindex);
6054 +       au_set_h_dptr(dentry, bindex, h_dentry);
6055 +
6056 +       inode = dentry->d_inode;
6057 +       if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
6058 +           || (inode && !S_ISDIR(inode->i_mode)))
6059 +               goto out; /* success */
6060 +
6061 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
6062 +       opq = au_diropq_test(h_dentry, br);
6063 +       mutex_unlock(&h_inode->i_mutex);
6064 +       if (opq > 0)
6065 +               au_set_dbdiropq(dentry, bindex);
6066 +       else if (unlikely(opq < 0)) {
6067 +               au_set_h_dptr(dentry, bindex, NULL);
6068 +               h_dentry = ERR_PTR(opq);
6069 +       }
6070 +       goto out;
6071 +
6072 +out_neg:
6073 +       dput(h_dentry);
6074 +       h_dentry = NULL;
6075 +out:
6076 +       return h_dentry;
6077 +}
6078 +
6079 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
6080 +{
6081 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
6082 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6083 +               return -EPERM;
6084 +       return 0;
6085 +}
6086 +
6087 +/*
6088 + * returns the number of lower positive dentries,
6089 + * otherwise an error.
6090 + * can be called at unlinking with @type is zero.
6091 + */
6092 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
6093 +                  struct nameidata *nd)
6094 +{
6095 +       int npositive, err;
6096 +       aufs_bindex_t bindex, btail, bdiropq;
6097 +       unsigned char isdir;
6098 +       struct qstr whname;
6099 +       struct au_do_lookup_args args = {
6100 +               .flags  = 0,
6101 +               .type   = type,
6102 +               .nd     = nd
6103 +       };
6104 +       const struct qstr *name = &dentry->d_name;
6105 +       struct dentry *parent;
6106 +       struct inode *inode;
6107 +
6108 +       err = au_test_shwh(dentry->d_sb, name);
6109 +       if (unlikely(err))
6110 +               goto out;
6111 +
6112 +       err = au_wh_name_alloc(&whname, name);
6113 +       if (unlikely(err))
6114 +               goto out;
6115 +
6116 +       inode = dentry->d_inode;
6117 +       isdir = !!(inode && S_ISDIR(inode->i_mode));
6118 +       if (!type)
6119 +               au_fset_lkup(args.flags, ALLOW_NEG);
6120 +
6121 +       npositive = 0;
6122 +       parent = dget_parent(dentry);
6123 +       btail = au_dbtaildir(parent);
6124 +       for (bindex = bstart; bindex <= btail; bindex++) {
6125 +               struct dentry *h_parent, *h_dentry;
6126 +               struct inode *h_inode, *h_dir;
6127 +
6128 +               h_dentry = au_h_dptr(dentry, bindex);
6129 +               if (h_dentry) {
6130 +                       if (h_dentry->d_inode)
6131 +                               npositive++;
6132 +                       if (type != S_IFDIR)
6133 +                               break;
6134 +                       continue;
6135 +               }
6136 +               h_parent = au_h_dptr(parent, bindex);
6137 +               if (!h_parent)
6138 +                       continue;
6139 +               h_dir = h_parent->d_inode;
6140 +               if (!h_dir || !S_ISDIR(h_dir->i_mode))
6141 +                       continue;
6142 +
6143 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
6144 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
6145 +                                       &args);
6146 +               mutex_unlock(&h_dir->i_mutex);
6147 +               err = PTR_ERR(h_dentry);
6148 +               if (IS_ERR(h_dentry))
6149 +                       goto out_parent;
6150 +               au_fclr_lkup(args.flags, ALLOW_NEG);
6151 +
6152 +               if (au_dbwh(dentry) >= 0)
6153 +                       break;
6154 +               if (!h_dentry)
6155 +                       continue;
6156 +               h_inode = h_dentry->d_inode;
6157 +               if (!h_inode)
6158 +                       continue;
6159 +               npositive++;
6160 +               if (!args.type)
6161 +                       args.type = h_inode->i_mode & S_IFMT;
6162 +               if (args.type != S_IFDIR)
6163 +                       break;
6164 +               else if (isdir) {
6165 +                       /* the type of lower may be different */
6166 +                       bdiropq = au_dbdiropq(dentry);
6167 +                       if (bdiropq >= 0 && bdiropq <= bindex)
6168 +                               break;
6169 +               }
6170 +       }
6171 +
6172 +       if (npositive) {
6173 +               AuLabel(positive);
6174 +               au_update_dbstart(dentry);
6175 +       }
6176 +       err = npositive;
6177 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
6178 +                    && au_dbstart(dentry) < 0)) {
6179 +               err = -EIO;
6180 +               AuIOErr("both of real entry and whiteout found, %.*s, err %d\n",
6181 +                       AuDLNPair(dentry), err);
6182 +       }
6183 +
6184 +out_parent:
6185 +       dput(parent);
6186 +       kfree(whname.name);
6187 +out:
6188 +       return err;
6189 +}
6190 +
6191 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
6192 +                              struct au_branch *br)
6193 +{
6194 +       struct dentry *dentry;
6195 +       int wkq_err;
6196 +
6197 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
6198 +               dentry = au_lkup_one(name, parent, br, /*nd*/NULL);
6199 +       else {
6200 +               struct au_lkup_one_args args = {
6201 +                       .errp           = &dentry,
6202 +                       .name           = name,
6203 +                       .h_parent       = parent,
6204 +                       .br             = br,
6205 +                       .nd             = NULL
6206 +               };
6207 +
6208 +               wkq_err = au_wkq_wait(au_call_lkup_one, &args);
6209 +               if (unlikely(wkq_err))
6210 +                       dentry = ERR_PTR(wkq_err);
6211 +       }
6212 +
6213 +       return dentry;
6214 +}
6215 +
6216 +/*
6217 + * lookup @dentry on @bindex which should be negative.
6218 + */
6219 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex)
6220 +{
6221 +       int err;
6222 +       struct dentry *parent, *h_parent, *h_dentry;
6223 +
6224 +       parent = dget_parent(dentry);
6225 +       h_parent = au_h_dptr(parent, bindex);
6226 +       h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent,
6227 +                                  au_sbr(dentry->d_sb, bindex));
6228 +       err = PTR_ERR(h_dentry);
6229 +       if (IS_ERR(h_dentry))
6230 +               goto out;
6231 +       if (unlikely(h_dentry->d_inode)) {
6232 +               err = -EIO;
6233 +               AuIOErr("%.*s should be negative on b%d.\n",
6234 +                       AuDLNPair(h_dentry), bindex);
6235 +               dput(h_dentry);
6236 +               goto out;
6237 +       }
6238 +
6239 +       err = 0;
6240 +       if (bindex < au_dbstart(dentry))
6241 +               au_set_dbstart(dentry, bindex);
6242 +       if (au_dbend(dentry) < bindex)
6243 +               au_set_dbend(dentry, bindex);
6244 +       au_set_h_dptr(dentry, bindex, h_dentry);
6245 +
6246 +out:
6247 +       dput(parent);
6248 +       return err;
6249 +}
6250 +
6251 +/* ---------------------------------------------------------------------- */
6252 +
6253 +/* subset of struct inode */
6254 +struct au_iattr {
6255 +       unsigned long           i_ino;
6256 +       /* unsigned int         i_nlink; */
6257 +       uid_t                   i_uid;
6258 +       gid_t                   i_gid;
6259 +       u64                     i_version;
6260 +/*
6261 +       loff_t                  i_size;
6262 +       blkcnt_t                i_blocks;
6263 +*/
6264 +       umode_t                 i_mode;
6265 +};
6266 +
6267 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
6268 +{
6269 +       ia->i_ino = h_inode->i_ino;
6270 +       /* ia->i_nlink = h_inode->i_nlink; */
6271 +       ia->i_uid = h_inode->i_uid;
6272 +       ia->i_gid = h_inode->i_gid;
6273 +       ia->i_version = h_inode->i_version;
6274 +/*
6275 +       ia->i_size = h_inode->i_size;
6276 +       ia->i_blocks = h_inode->i_blocks;
6277 +*/
6278 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
6279 +}
6280 +
6281 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
6282 +{
6283 +       return ia->i_ino != h_inode->i_ino
6284 +               /* || ia->i_nlink != h_inode->i_nlink */
6285 +               || ia->i_uid != h_inode->i_uid
6286 +               || ia->i_gid != h_inode->i_gid
6287 +               || ia->i_version != h_inode->i_version
6288 +/*
6289 +               || ia->i_size != h_inode->i_size
6290 +               || ia->i_blocks != h_inode->i_blocks
6291 +*/
6292 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
6293 +}
6294 +
6295 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
6296 +                             struct au_branch *br)
6297 +{
6298 +       int err;
6299 +       struct au_iattr ia;
6300 +       struct inode *h_inode;
6301 +       struct dentry *h_d;
6302 +       struct super_block *h_sb;
6303 +
6304 +       err = 0;
6305 +       memset(&ia, -1, sizeof(ia));
6306 +       h_sb = h_dentry->d_sb;
6307 +       h_inode = h_dentry->d_inode;
6308 +       if (h_inode)
6309 +               au_iattr_save(&ia, h_inode);
6310 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
6311 +               /* nfs d_revalidate may return 0 for negative dentry */
6312 +               /* fuse d_revalidate always return 0 for negative dentry */
6313 +               goto out;
6314 +
6315 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
6316 +       h_d = au_lkup_one(&h_dentry->d_name, h_parent, br, /*nd*/NULL);
6317 +       err = PTR_ERR(h_d);
6318 +       if (IS_ERR(h_d))
6319 +               goto out;
6320 +
6321 +       err = 0;
6322 +       if (unlikely(h_d != h_dentry
6323 +                    || h_d->d_inode != h_inode
6324 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
6325 +               err = au_busy_or_stale();
6326 +       dput(h_d);
6327 +
6328 +out:
6329 +       AuTraceErr(err);
6330 +       return err;
6331 +}
6332 +
6333 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
6334 +               struct dentry *h_parent, struct au_branch *br)
6335 +{
6336 +       int err;
6337 +
6338 +       err = 0;
6339 +       if (udba == AuOpt_UDBA_REVAL
6340 +           && !au_test_fs_remote(h_dentry->d_sb)) {
6341 +               IMustLock(h_dir);
6342 +               err = (h_dentry->d_parent->d_inode != h_dir);
6343 +       } else if (udba != AuOpt_UDBA_NONE)
6344 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
6345 +
6346 +       return err;
6347 +}
6348 +
6349 +/* ---------------------------------------------------------------------- */
6350 +
6351 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
6352 +{
6353 +       int err;
6354 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
6355 +       struct au_hdentry tmp, *p, *q;
6356 +       struct au_dinfo *dinfo;
6357 +       struct super_block *sb;
6358 +
6359 +       DiMustWriteLock(dentry);
6360 +
6361 +       sb = dentry->d_sb;
6362 +       dinfo = au_di(dentry);
6363 +       bend = dinfo->di_bend;
6364 +       bwh = dinfo->di_bwh;
6365 +       bdiropq = dinfo->di_bdiropq;
6366 +       p = dinfo->di_hdentry + dinfo->di_bstart;
6367 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
6368 +               if (!p->hd_dentry)
6369 +                       continue;
6370 +
6371 +               new_bindex = au_br_index(sb, p->hd_id);
6372 +               if (new_bindex == bindex)
6373 +                       continue;
6374 +
6375 +               if (dinfo->di_bwh == bindex)
6376 +                       bwh = new_bindex;
6377 +               if (dinfo->di_bdiropq == bindex)
6378 +                       bdiropq = new_bindex;
6379 +               if (new_bindex < 0) {
6380 +                       au_hdput(p);
6381 +                       p->hd_dentry = NULL;
6382 +                       continue;
6383 +               }
6384 +
6385 +               /* swap two lower dentries, and loop again */
6386 +               q = dinfo->di_hdentry + new_bindex;
6387 +               tmp = *q;
6388 +               *q = *p;
6389 +               *p = tmp;
6390 +               if (tmp.hd_dentry) {
6391 +                       bindex--;
6392 +                       p--;
6393 +               }
6394 +       }
6395 +
6396 +       dinfo->di_bwh = -1;
6397 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
6398 +               dinfo->di_bwh = bwh;
6399 +
6400 +       dinfo->di_bdiropq = -1;
6401 +       if (bdiropq >= 0
6402 +           && bdiropq <= au_sbend(sb)
6403 +           && au_sbr_whable(sb, bdiropq))
6404 +               dinfo->di_bdiropq = bdiropq;
6405 +
6406 +       err = -EIO;
6407 +       dinfo->di_bstart = -1;
6408 +       dinfo->di_bend = -1;
6409 +       bend = au_dbend(parent);
6410 +       p = dinfo->di_hdentry;
6411 +       for (bindex = 0; bindex <= bend; bindex++, p++)
6412 +               if (p->hd_dentry) {
6413 +                       dinfo->di_bstart = bindex;
6414 +                       break;
6415 +               }
6416 +
6417 +       if (dinfo->di_bstart >= 0) {
6418 +               p = dinfo->di_hdentry + bend;
6419 +               for (bindex = bend; bindex >= 0; bindex--, p--)
6420 +                       if (p->hd_dentry) {
6421 +                               dinfo->di_bend = bindex;
6422 +                               err = 0;
6423 +                               break;
6424 +                       }
6425 +       }
6426 +
6427 +       return err;
6428 +}
6429 +
6430 +static void au_do_hide(struct dentry *dentry)
6431 +{
6432 +       struct inode *inode;
6433 +
6434 +       inode = dentry->d_inode;
6435 +       if (inode) {
6436 +               if (!S_ISDIR(inode->i_mode)) {
6437 +                       if (inode->i_nlink && !d_unhashed(dentry))
6438 +                               drop_nlink(inode);
6439 +               } else {
6440 +                       clear_nlink(inode);
6441 +                       /* stop next lookup */
6442 +                       inode->i_flags |= S_DEAD;
6443 +               }
6444 +               smp_mb(); /* necessary? */
6445 +       }
6446 +       d_drop(dentry);
6447 +}
6448 +
6449 +static int au_hide_children(struct dentry *parent)
6450 +{
6451 +       int err, i, j, ndentry;
6452 +       struct au_dcsub_pages dpages;
6453 +       struct au_dpage *dpage;
6454 +       struct dentry *dentry;
6455 +
6456 +       err = au_dpages_init(&dpages, GFP_NOFS);
6457 +       if (unlikely(err))
6458 +               goto out;
6459 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
6460 +       if (unlikely(err))
6461 +               goto out_dpages;
6462 +
6463 +       /* in reverse order */
6464 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
6465 +               dpage = dpages.dpages + i;
6466 +               ndentry = dpage->ndentry;
6467 +               for (j = ndentry - 1; j >= 0; j--) {
6468 +                       dentry = dpage->dentries[j];
6469 +                       if (dentry != parent)
6470 +                               au_do_hide(dentry);
6471 +               }
6472 +       }
6473 +
6474 +out_dpages:
6475 +       au_dpages_free(&dpages);
6476 +out:
6477 +       return err;
6478 +}
6479 +
6480 +static void au_hide(struct dentry *dentry)
6481 +{
6482 +       int err;
6483 +       struct inode *inode;
6484 +
6485 +       AuDbgDentry(dentry);
6486 +       inode = dentry->d_inode;
6487 +       if (inode && S_ISDIR(inode->i_mode)) {
6488 +               /* shrink_dcache_parent(dentry); */
6489 +               err = au_hide_children(dentry);
6490 +               if (unlikely(err))
6491 +                       AuIOErr("%.*s, failed hiding children, ignored %d\n",
6492 +                               AuDLNPair(dentry), err);
6493 +       }
6494 +       au_do_hide(dentry);
6495 +}
6496 +
6497 +/*
6498 + * By adding a dirty branch, a cached dentry may be affected in various ways.
6499 + *
6500 + * a dirty branch is added
6501 + * - on the top of layers
6502 + * - in the middle of layers
6503 + * - to the bottom of layers
6504 + *
6505 + * on the added branch there exists
6506 + * - a whiteout
6507 + * - a diropq
6508 + * - a same named entry
6509 + *   + exist
6510 + *     * negative --> positive
6511 + *     * positive --> positive
6512 + *      - type is unchanged
6513 + *      - type is changed
6514 + *   + doesn't exist
6515 + *     * negative --> negative
6516 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
6517 + * - none
6518 + */
6519 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
6520 +                              struct au_dinfo *tmp)
6521 +{
6522 +       int err;
6523 +       aufs_bindex_t bindex, bend;
6524 +       struct {
6525 +               struct dentry *dentry;
6526 +               struct inode *inode;
6527 +               mode_t mode;
6528 +       } orig_h, tmp_h;
6529 +       struct au_hdentry *hd;
6530 +       struct inode *inode, *h_inode;
6531 +       struct dentry *h_dentry;
6532 +
6533 +       err = 0;
6534 +       AuDebugOn(dinfo->di_bstart < 0);
6535 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
6536 +       orig_h.inode = orig_h.dentry->d_inode;
6537 +       orig_h.mode = 0;
6538 +       if (orig_h.inode)
6539 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
6540 +       memset(&tmp_h, 0, sizeof(tmp_h));
6541 +       if (tmp->di_bstart >= 0) {
6542 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
6543 +               tmp_h.inode = tmp_h.dentry->d_inode;
6544 +               if (tmp_h.inode)
6545 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
6546 +       }
6547 +
6548 +       inode = dentry->d_inode;
6549 +       if (!orig_h.inode) {
6550 +               AuDbg("nagative originally\n");
6551 +               if (inode) {
6552 +                       au_hide(dentry);
6553 +                       goto out;
6554 +               }
6555 +               AuDebugOn(inode);
6556 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6557 +               AuDebugOn(dinfo->di_bdiropq != -1);
6558 +
6559 +               if (!tmp_h.inode) {
6560 +                       AuDbg("negative --> negative\n");
6561 +                       /* should have only one negative lower */
6562 +                       if (tmp->di_bstart >= 0
6563 +                           && tmp->di_bstart < dinfo->di_bstart) {
6564 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
6565 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6566 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
6567 +                               au_di_cp(dinfo, tmp);
6568 +                               hd = tmp->di_hdentry + tmp->di_bstart;
6569 +                               au_set_h_dptr(dentry, tmp->di_bstart,
6570 +                                             dget(hd->hd_dentry));
6571 +                       }
6572 +                       au_dbg_verify_dinode(dentry);
6573 +               } else {
6574 +                       AuDbg("negative --> positive\n");
6575 +                       /*
6576 +                        * similar to the behaviour of creating with bypassing
6577 +                        * aufs.
6578 +                        * unhash it in order to force an error in the
6579 +                        * succeeding create operation.
6580 +                        * we should not set S_DEAD here.
6581 +                        */
6582 +                       d_drop(dentry);
6583 +                       /* au_di_swap(tmp, dinfo); */
6584 +                       au_dbg_verify_dinode(dentry);
6585 +               }
6586 +       } else {
6587 +               AuDbg("positive originally\n");
6588 +               /* inode may be NULL */
6589 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
6590 +               if (!tmp_h.inode) {
6591 +                       AuDbg("positive --> negative\n");
6592 +                       /* or bypassing aufs */
6593 +                       au_hide(dentry);
6594 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
6595 +                               dinfo->di_bwh = tmp->di_bwh;
6596 +                       if (inode)
6597 +                               err = au_refresh_hinode_self(inode);
6598 +                       au_dbg_verify_dinode(dentry);
6599 +               } else if (orig_h.mode == tmp_h.mode) {
6600 +                       AuDbg("positive --> positive, same type\n");
6601 +                       if (!S_ISDIR(orig_h.mode)
6602 +                           && dinfo->di_bstart > tmp->di_bstart) {
6603 +                               /*
6604 +                                * similar to the behaviour of removing and
6605 +                                * creating.
6606 +                                */
6607 +                               au_hide(dentry);
6608 +                               if (inode)
6609 +                                       err = au_refresh_hinode_self(inode);
6610 +                               au_dbg_verify_dinode(dentry);
6611 +                       } else {
6612 +                               /* fill empty slots */
6613 +                               if (dinfo->di_bstart > tmp->di_bstart)
6614 +                                       dinfo->di_bstart = tmp->di_bstart;
6615 +                               if (dinfo->di_bend < tmp->di_bend)
6616 +                                       dinfo->di_bend = tmp->di_bend;
6617 +                               dinfo->di_bwh = tmp->di_bwh;
6618 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
6619 +                               hd = tmp->di_hdentry;
6620 +                               bend = dinfo->di_bend;
6621 +                               for (bindex = tmp->di_bstart; bindex <= bend;
6622 +                                    bindex++) {
6623 +                                       if (au_h_dptr(dentry, bindex))
6624 +                                               continue;
6625 +                                       h_dentry = hd[bindex].hd_dentry;
6626 +                                       if (!h_dentry)
6627 +                                               continue;
6628 +                                       h_inode = h_dentry->d_inode;
6629 +                                       AuDebugOn(!h_inode);
6630 +                                       AuDebugOn(orig_h.mode
6631 +                                                 != (h_inode->i_mode
6632 +                                                     & S_IFMT));
6633 +                                       au_set_h_dptr(dentry, bindex,
6634 +                                                     dget(h_dentry));
6635 +                               }
6636 +                               err = au_refresh_hinode(inode, dentry);
6637 +                               au_dbg_verify_dinode(dentry);
6638 +                       }
6639 +               } else {
6640 +                       AuDbg("positive --> positive, different type\n");
6641 +                       /* similar to the behaviour of removing and creating */
6642 +                       au_hide(dentry);
6643 +                       if (inode)
6644 +                               err = au_refresh_hinode_self(inode);
6645 +                       au_dbg_verify_dinode(dentry);
6646 +               }
6647 +       }
6648 +
6649 +out:
6650 +       return err;
6651 +}
6652 +
6653 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
6654 +{
6655 +       int err, ebrange;
6656 +       unsigned int sigen;
6657 +       struct au_dinfo *dinfo, *tmp;
6658 +       struct super_block *sb;
6659 +       struct inode *inode;
6660 +
6661 +       DiMustWriteLock(dentry);
6662 +       AuDebugOn(IS_ROOT(dentry));
6663 +       AuDebugOn(!parent->d_inode);
6664 +
6665 +       sb = dentry->d_sb;
6666 +       inode = dentry->d_inode;
6667 +       sigen = au_sigen(sb);
6668 +       err = au_digen_test(parent, sigen);
6669 +       if (unlikely(err))
6670 +               goto out;
6671 +
6672 +       dinfo = au_di(dentry);
6673 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
6674 +       if (unlikely(err))
6675 +               goto out;
6676 +       ebrange = au_dbrange_test(dentry);
6677 +       if (!ebrange)
6678 +               ebrange = au_do_refresh_hdentry(dentry, parent);
6679 +
6680 +       if (d_unhashed(dentry) || ebrange) {
6681 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
6682 +               if (inode)
6683 +                       err = au_refresh_hinode_self(inode);
6684 +               au_dbg_verify_dinode(dentry);
6685 +               if (!err)
6686 +                       goto out_dgen; /* success */
6687 +               goto out;
6688 +       }
6689 +
6690 +       /* temporary dinfo */
6691 +       AuDbgDentry(dentry);
6692 +       err = -ENOMEM;
6693 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
6694 +       if (unlikely(!tmp))
6695 +               goto out;
6696 +       au_di_swap(tmp, dinfo);
6697 +       /* returns the number of positive dentries */
6698 +       /*
6699 +        * if current working dir is removed, it returns an error.
6700 +        * but the dentry is legal.
6701 +        */
6702 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0, /*nd*/NULL);
6703 +       AuDbgDentry(dentry);
6704 +       au_di_swap(tmp, dinfo);
6705 +       if (err == -ENOENT)
6706 +               err = 0;
6707 +       if (err >= 0) {
6708 +               /* compare/refresh by dinfo */
6709 +               AuDbgDentry(dentry);
6710 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
6711 +               au_dbg_verify_dinode(dentry);
6712 +               AuTraceErr(err);
6713 +       }
6714 +       au_rw_write_unlock(&tmp->di_rwsem);
6715 +       au_di_free(tmp);
6716 +       if (unlikely(err))
6717 +               goto out;
6718 +
6719 +out_dgen:
6720 +       au_update_digen(dentry);
6721 +out:
6722 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
6723 +               AuIOErr("failed refreshing %.*s, %d\n",
6724 +                       AuDLNPair(dentry), err);
6725 +               AuDbgDentry(dentry);
6726 +       }
6727 +       AuTraceErr(err);
6728 +       return err;
6729 +}
6730 +
6731 +static noinline_for_stack
6732 +int au_do_h_d_reval(struct dentry *h_dentry, struct nameidata *nd,
6733 +                   struct dentry *dentry, aufs_bindex_t bindex)
6734 +{
6735 +       int err, valid;
6736 +       int (*reval)(struct dentry *, struct nameidata *);
6737 +
6738 +       err = 0;
6739 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
6740 +               goto out;
6741 +       reval = h_dentry->d_op->d_revalidate;
6742 +
6743 +       AuDbg("b%d\n", bindex);
6744 +       if (au_test_fs_null_nd(h_dentry->d_sb))
6745 +               /* it may return tri-state */
6746 +               valid = reval(h_dentry, NULL);
6747 +       else {
6748 +               struct nameidata h_nd;
6749 +               int locked;
6750 +               struct dentry *parent;
6751 +
6752 +               au_h_nd(&h_nd, nd);
6753 +               parent = nd->path.dentry;
6754 +               locked = (nd && nd->path.dentry != dentry);
6755 +               if (locked)
6756 +                       di_read_lock_parent(parent, AuLock_IR);
6757 +               BUG_ON(bindex > au_dbend(parent));
6758 +               h_nd.path.dentry = au_h_dptr(parent, bindex);
6759 +               BUG_ON(!h_nd.path.dentry);
6760 +               h_nd.path.mnt = au_sbr(parent->d_sb, bindex)->br_mnt;
6761 +               path_get(&h_nd.path);
6762 +               valid = reval(h_dentry, &h_nd);
6763 +               path_put(&h_nd.path);
6764 +               if (locked)
6765 +                       di_read_unlock(parent, AuLock_IR);
6766 +       }
6767 +
6768 +       if (unlikely(valid < 0))
6769 +               err = valid;
6770 +       else if (!valid)
6771 +               err = -EINVAL;
6772 +
6773 +out:
6774 +       AuTraceErr(err);
6775 +       return err;
6776 +}
6777 +
6778 +/* todo: remove this */
6779 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
6780 +                         struct nameidata *nd, int do_udba)
6781 +{
6782 +       int err;
6783 +       umode_t mode, h_mode;
6784 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
6785 +       unsigned char plus, unhashed, is_root, h_plus;
6786 +       struct inode *h_inode, *h_cached_inode;
6787 +       struct dentry *h_dentry;
6788 +       struct qstr *name, *h_name;
6789 +
6790 +       err = 0;
6791 +       plus = 0;
6792 +       mode = 0;
6793 +       ibs = -1;
6794 +       ibe = -1;
6795 +       unhashed = !!d_unhashed(dentry);
6796 +       is_root = !!IS_ROOT(dentry);
6797 +       name = &dentry->d_name;
6798 +
6799 +       /*
6800 +        * Theoretically, REVAL test should be unnecessary in case of
6801 +        * {FS,I}NOTIFY.
6802 +        * But {fs,i}notify doesn't fire some necessary events,
6803 +        *      IN_ATTRIB for atime/nlink/pageio
6804 +        *      IN_DELETE for NFS dentry
6805 +        * Let's do REVAL test too.
6806 +        */
6807 +       if (do_udba && inode) {
6808 +               mode = (inode->i_mode & S_IFMT);
6809 +               plus = (inode->i_nlink > 0);
6810 +               ibs = au_ibstart(inode);
6811 +               ibe = au_ibend(inode);
6812 +       }
6813 +
6814 +       bstart = au_dbstart(dentry);
6815 +       btail = bstart;
6816 +       if (inode && S_ISDIR(inode->i_mode))
6817 +               btail = au_dbtaildir(dentry);
6818 +       for (bindex = bstart; bindex <= btail; bindex++) {
6819 +               h_dentry = au_h_dptr(dentry, bindex);
6820 +               if (!h_dentry)
6821 +                       continue;
6822 +
6823 +               AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
6824 +               spin_lock(&h_dentry->d_lock);
6825 +               h_name = &h_dentry->d_name;
6826 +               if (unlikely(do_udba
6827 +                            && !is_root
6828 +                            && (unhashed != !!d_unhashed(h_dentry)
6829 +                                || name->len != h_name->len
6830 +                                || memcmp(name->name, h_name->name, name->len))
6831 +                           )) {
6832 +                       AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
6833 +                                 unhashed, d_unhashed(h_dentry),
6834 +                                 AuDLNPair(dentry), AuDLNPair(h_dentry));
6835 +                       spin_unlock(&h_dentry->d_lock);
6836 +                       goto err;
6837 +               }
6838 +               spin_unlock(&h_dentry->d_lock);
6839 +
6840 +               err = au_do_h_d_reval(h_dentry, nd, dentry, bindex);
6841 +               if (unlikely(err))
6842 +                       /* do not goto err, to keep the errno */
6843 +                       break;
6844 +
6845 +               /* todo: plink too? */
6846 +               if (!do_udba)
6847 +                       continue;
6848 +
6849 +               /* UDBA tests */
6850 +               h_inode = h_dentry->d_inode;
6851 +               if (unlikely(!!inode != !!h_inode))
6852 +                       goto err;
6853 +
6854 +               h_plus = plus;
6855 +               h_mode = mode;
6856 +               h_cached_inode = h_inode;
6857 +               if (h_inode) {
6858 +                       h_mode = (h_inode->i_mode & S_IFMT);
6859 +                       h_plus = (h_inode->i_nlink > 0);
6860 +               }
6861 +               if (inode && ibs <= bindex && bindex <= ibe)
6862 +                       h_cached_inode = au_h_iptr(inode, bindex);
6863 +
6864 +               if (unlikely(plus != h_plus
6865 +                            || mode != h_mode
6866 +                            || h_cached_inode != h_inode))
6867 +                       goto err;
6868 +               continue;
6869 +
6870 +       err:
6871 +               err = -EINVAL;
6872 +               break;
6873 +       }
6874 +
6875 +       return err;
6876 +}
6877 +
6878 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
6879 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
6880 +{
6881 +       int err;
6882 +       struct dentry *parent;
6883 +
6884 +       if (!au_digen_test(dentry, sigen))
6885 +               return 0;
6886 +
6887 +       parent = dget_parent(dentry);
6888 +       di_read_lock_parent(parent, AuLock_IR);
6889 +       AuDebugOn(au_digen_test(parent, sigen));
6890 +       au_dbg_verify_gen(parent, sigen);
6891 +       err = au_refresh_dentry(dentry, parent);
6892 +       di_read_unlock(parent, AuLock_IR);
6893 +       dput(parent);
6894 +       AuTraceErr(err);
6895 +       return err;
6896 +}
6897 +
6898 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
6899 +{
6900 +       int err;
6901 +       struct dentry *d, *parent;
6902 +       struct inode *inode;
6903 +
6904 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
6905 +               return simple_reval_dpath(dentry, sigen);
6906 +
6907 +       /* slow loop, keep it simple and stupid */
6908 +       /* cf: au_cpup_dirs() */
6909 +       err = 0;
6910 +       parent = NULL;
6911 +       while (au_digen_test(dentry, sigen)) {
6912 +               d = dentry;
6913 +               while (1) {
6914 +                       dput(parent);
6915 +                       parent = dget_parent(d);
6916 +                       if (!au_digen_test(parent, sigen))
6917 +                               break;
6918 +                       d = parent;
6919 +               }
6920 +
6921 +               inode = d->d_inode;
6922 +               if (d != dentry)
6923 +                       di_write_lock_child2(d);
6924 +
6925 +               /* someone might update our dentry while we were sleeping */
6926 +               if (au_digen_test(d, sigen)) {
6927 +                       /*
6928 +                        * todo: consolidate with simple_reval_dpath(),
6929 +                        * do_refresh() and au_reval_for_attr().
6930 +                        */
6931 +                       di_read_lock_parent(parent, AuLock_IR);
6932 +                       err = au_refresh_dentry(d, parent);
6933 +                       di_read_unlock(parent, AuLock_IR);
6934 +               }
6935 +
6936 +               if (d != dentry)
6937 +                       di_write_unlock(d);
6938 +               dput(parent);
6939 +               if (unlikely(err))
6940 +                       break;
6941 +       }
6942 +
6943 +       return err;
6944 +}
6945 +
6946 +/*
6947 + * if valid returns 1, otherwise 0.
6948 + */
6949 +static int aufs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
6950 +{
6951 +       int valid, err;
6952 +       unsigned int sigen;
6953 +       unsigned char do_udba;
6954 +       struct super_block *sb;
6955 +       struct inode *inode;
6956 +
6957 +       /* todo: support rcu-walk? */
6958 +       if (nd && (nd->flags & LOOKUP_RCU))
6959 +               return -ECHILD;
6960 +
6961 +       valid = 0;
6962 +       if (unlikely(!au_di(dentry)))
6963 +               goto out;
6964 +
6965 +       inode = dentry->d_inode;
6966 +       if (inode && is_bad_inode(inode))
6967 +               goto out;
6968 +
6969 +       valid = 1;
6970 +       sb = dentry->d_sb;
6971 +       /*
6972 +        * todo: very ugly
6973 +        * i_mutex of parent dir may be held,
6974 +        * but we should not return 'invalid' due to busy.
6975 +        */
6976 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
6977 +       if (unlikely(err)) {
6978 +               valid = err;
6979 +               AuTraceErr(err);
6980 +               goto out;
6981 +       }
6982 +       if (unlikely(au_dbrange_test(dentry))) {
6983 +               err = -EINVAL;
6984 +               AuTraceErr(err);
6985 +               goto out_dgrade;
6986 +       }
6987 +
6988 +       sigen = au_sigen(sb);
6989 +       if (au_digen_test(dentry, sigen)) {
6990 +               AuDebugOn(IS_ROOT(dentry));
6991 +               err = au_reval_dpath(dentry, sigen);
6992 +               if (unlikely(err)) {
6993 +                       AuTraceErr(err);
6994 +                       goto out_dgrade;
6995 +               }
6996 +       }
6997 +       di_downgrade_lock(dentry, AuLock_IR);
6998 +
6999 +       err = -EINVAL;
7000 +       if (inode && (IS_DEADDIR(inode) || !inode->i_nlink))
7001 +               goto out_inval;
7002 +
7003 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7004 +       if (do_udba && inode) {
7005 +               aufs_bindex_t bstart = au_ibstart(inode);
7006 +               struct inode *h_inode;
7007 +
7008 +               if (bstart >= 0) {
7009 +                       h_inode = au_h_iptr(inode, bstart);
7010 +                       if (h_inode && au_test_higen(inode, h_inode))
7011 +                               goto out_inval;
7012 +               }
7013 +       }
7014 +
7015 +       err = h_d_revalidate(dentry, inode, nd, do_udba);
7016 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
7017 +               err = -EIO;
7018 +               AuDbg("both of real entry and whiteout found, %.*s, err %d\n",
7019 +                     AuDLNPair(dentry), err);
7020 +       }
7021 +       goto out_inval;
7022 +
7023 +out_dgrade:
7024 +       di_downgrade_lock(dentry, AuLock_IR);
7025 +out_inval:
7026 +       aufs_read_unlock(dentry, AuLock_IR);
7027 +       AuTraceErr(err);
7028 +       valid = !err;
7029 +out:
7030 +       if (!valid) {
7031 +               AuDbg("%.*s invalid, %d\n", AuDLNPair(dentry), valid);
7032 +               d_drop(dentry);
7033 +       }
7034 +       return valid;
7035 +}
7036 +
7037 +static void aufs_d_release(struct dentry *dentry)
7038 +{
7039 +       if (au_di(dentry)) {
7040 +               au_di_fin(dentry);
7041 +               au_hn_di_reinit(dentry);
7042 +       }
7043 +}
7044 +
7045 +const struct dentry_operations aufs_dop = {
7046 +       .d_revalidate   = aufs_d_revalidate,
7047 +       .d_release      = aufs_d_release
7048 +};
7049 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
7050 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
7051 +++ linux/fs/aufs/dentry.h      2012-03-19 12:24:13.679801090 +0100
7052 @@ -0,0 +1,237 @@
7053 +/*
7054 + * Copyright (C) 2005-2012 Junjiro R. Okajima
7055 + *
7056 + * This program, aufs is free software; you can redistribute it and/or modify
7057 + * it under the terms of the GNU General Public License as published by
7058 + * the Free Software Foundation; either version 2 of the License, or
7059 + * (at your option) any later version.
7060 + *
7061 + * This program is distributed in the hope that it will be useful,
7062 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7063 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7064 + * GNU General Public License for more details.
7065 + *
7066 + * You should have received a copy of the GNU General Public License
7067 + * along with this program; if not, write to the Free Software
7068 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7069 + */
7070 +
7071 +/*
7072 + * lookup and dentry operations
7073 + */
7074 +
7075 +#ifndef __AUFS_DENTRY_H__
7076 +#define __AUFS_DENTRY_H__
7077 +
7078 +#ifdef __KERNEL__
7079 +
7080 +#include <linux/dcache.h>
7081 +#include "rwsem.h"
7082 +
7083 +struct au_hdentry {
7084 +       struct dentry           *hd_dentry;
7085 +       aufs_bindex_t           hd_id;
7086 +};
7087 +
7088 +struct au_dinfo {
7089 +       atomic_t                di_generation;
7090 +
7091 +       struct au_rwsem         di_rwsem;
7092 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
7093 +       struct au_hdentry       *di_hdentry;
7094 +} ____cacheline_aligned_in_smp;
7095 +
7096 +/* ---------------------------------------------------------------------- */
7097 +
7098 +/* dentry.c */
7099 +extern const struct dentry_operations aufs_dop;
7100 +struct au_branch;
7101 +struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
7102 +                          struct au_branch *br, struct nameidata *nd);
7103 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
7104 +                              struct au_branch *br);
7105 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7106 +               struct dentry *h_parent, struct au_branch *br);
7107 +
7108 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
7109 +                  struct nameidata *nd);
7110 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex);
7111 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
7112 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
7113 +
7114 +/* dinfo.c */
7115 +void au_di_init_once(void *_di);
7116 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
7117 +void au_di_free(struct au_dinfo *dinfo);
7118 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
7119 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
7120 +int au_di_init(struct dentry *dentry);
7121 +void au_di_fin(struct dentry *dentry);
7122 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
7123 +
7124 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
7125 +void di_read_unlock(struct dentry *d, int flags);
7126 +void di_downgrade_lock(struct dentry *d, int flags);
7127 +void di_write_lock(struct dentry *d, unsigned int lsc);
7128 +void di_write_unlock(struct dentry *d);
7129 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
7130 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
7131 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
7132 +
7133 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
7134 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
7135 +aufs_bindex_t au_dbtail(struct dentry *dentry);
7136 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
7137 +
7138 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7139 +                  struct dentry *h_dentry);
7140 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
7141 +int au_dbrange_test(struct dentry *dentry);
7142 +void au_update_digen(struct dentry *dentry);
7143 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
7144 +void au_update_dbstart(struct dentry *dentry);
7145 +void au_update_dbend(struct dentry *dentry);
7146 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
7147 +
7148 +/* ---------------------------------------------------------------------- */
7149 +
7150 +static inline struct au_dinfo *au_di(struct dentry *dentry)
7151 +{
7152 +       return dentry->d_fsdata;
7153 +}
7154 +
7155 +/* ---------------------------------------------------------------------- */
7156 +
7157 +/* lock subclass for dinfo */
7158 +enum {
7159 +       AuLsc_DI_CHILD,         /* child first */
7160 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
7161 +       AuLsc_DI_CHILD3,        /* copyup dirs */
7162 +       AuLsc_DI_PARENT,
7163 +       AuLsc_DI_PARENT2,
7164 +       AuLsc_DI_PARENT3,
7165 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
7166 +};
7167 +
7168 +/*
7169 + * di_read_lock_child, di_write_lock_child,
7170 + * di_read_lock_child2, di_write_lock_child2,
7171 + * di_read_lock_child3, di_write_lock_child3,
7172 + * di_read_lock_parent, di_write_lock_parent,
7173 + * di_read_lock_parent2, di_write_lock_parent2,
7174 + * di_read_lock_parent3, di_write_lock_parent3,
7175 + */
7176 +#define AuReadLockFunc(name, lsc) \
7177 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
7178 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
7179 +
7180 +#define AuWriteLockFunc(name, lsc) \
7181 +static inline void di_write_lock_##name(struct dentry *d) \
7182 +{ di_write_lock(d, AuLsc_DI_##lsc); }
7183 +
7184 +#define AuRWLockFuncs(name, lsc) \
7185 +       AuReadLockFunc(name, lsc) \
7186 +       AuWriteLockFunc(name, lsc)
7187 +
7188 +AuRWLockFuncs(child, CHILD);
7189 +AuRWLockFuncs(child2, CHILD2);
7190 +AuRWLockFuncs(child3, CHILD3);
7191 +AuRWLockFuncs(parent, PARENT);
7192 +AuRWLockFuncs(parent2, PARENT2);
7193 +AuRWLockFuncs(parent3, PARENT3);
7194 +
7195 +#undef AuReadLockFunc
7196 +#undef AuWriteLockFunc
7197 +#undef AuRWLockFuncs
7198 +
7199 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
7200 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
7201 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
7202 +
7203 +/* ---------------------------------------------------------------------- */
7204 +
7205 +/* todo: memory barrier? */
7206 +static inline unsigned int au_digen(struct dentry *d)
7207 +{
7208 +       return atomic_read(&au_di(d)->di_generation);
7209 +}
7210 +
7211 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
7212 +{
7213 +       hdentry->hd_dentry = NULL;
7214 +}
7215 +
7216 +static inline void au_hdput(struct au_hdentry *hd)
7217 +{
7218 +       if (hd)
7219 +               dput(hd->hd_dentry);
7220 +}
7221 +
7222 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
7223 +{
7224 +       DiMustAnyLock(dentry);
7225 +       return au_di(dentry)->di_bstart;
7226 +}
7227 +
7228 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
7229 +{
7230 +       DiMustAnyLock(dentry);
7231 +       return au_di(dentry)->di_bend;
7232 +}
7233 +
7234 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
7235 +{
7236 +       DiMustAnyLock(dentry);
7237 +       return au_di(dentry)->di_bwh;
7238 +}
7239 +
7240 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
7241 +{
7242 +       DiMustAnyLock(dentry);
7243 +       return au_di(dentry)->di_bdiropq;
7244 +}
7245 +
7246 +/* todo: hard/soft set? */
7247 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
7248 +{
7249 +       DiMustWriteLock(dentry);
7250 +       au_di(dentry)->di_bstart = bindex;
7251 +}
7252 +
7253 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
7254 +{
7255 +       DiMustWriteLock(dentry);
7256 +       au_di(dentry)->di_bend = bindex;
7257 +}
7258 +
7259 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
7260 +{
7261 +       DiMustWriteLock(dentry);
7262 +       /* dbwh can be outside of bstart - bend range */
7263 +       au_di(dentry)->di_bwh = bindex;
7264 +}
7265 +
7266 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
7267 +{
7268 +       DiMustWriteLock(dentry);
7269 +       au_di(dentry)->di_bdiropq = bindex;
7270 +}
7271 +
7272 +/* ---------------------------------------------------------------------- */
7273 +
7274 +#ifdef CONFIG_AUFS_HNOTIFY
7275 +static inline void au_digen_dec(struct dentry *d)
7276 +{
7277 +       atomic_dec(&au_di(d)->di_generation);
7278 +}
7279 +
7280 +static inline void au_hn_di_reinit(struct dentry *dentry)
7281 +{
7282 +       dentry->d_fsdata = NULL;
7283 +}
7284 +#else
7285 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
7286 +#endif /* CONFIG_AUFS_HNOTIFY */
7287 +
7288 +#endif /* __KERNEL__ */
7289 +#endif /* __AUFS_DENTRY_H__ */
7290 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
7291 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
7292 +++ linux/fs/aufs/dinfo.c       2012-03-19 12:24:13.679801090 +0100
7293 @@ -0,0 +1,543 @@
7294 +/*
7295 + * Copyright (C) 2005-2012 Junjiro R. Okajima
7296 + *
7297 + * This program, aufs is free software; you can redistribute it and/or modify
7298 + * it under the terms of the GNU General Public License as published by
7299 + * the Free Software Foundation; either version 2 of the License, or
7300 + * (at your option) any later version.
7301 + *
7302 + * This program is distributed in the hope that it will be useful,
7303 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7304 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7305 + * GNU General Public License for more details.
7306 + *
7307 + * You should have received a copy of the GNU General Public License
7308 + * along with this program; if not, write to the Free Software
7309 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7310 + */
7311 +
7312 +/*
7313 + * dentry private data
7314 + */
7315 +
7316 +#include "aufs.h"
7317 +
7318 +void au_di_init_once(void *_dinfo)
7319 +{
7320 +       struct au_dinfo *dinfo = _dinfo;
7321 +       static struct lock_class_key aufs_di;
7322 +
7323 +       au_rw_init(&dinfo->di_rwsem);
7324 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
7325 +}
7326 +
7327 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
7328 +{
7329 +       struct au_dinfo *dinfo;
7330 +       int nbr, i;
7331 +
7332 +       dinfo = au_cache_alloc_dinfo();
7333 +       if (unlikely(!dinfo))
7334 +               goto out;
7335 +
7336 +       nbr = au_sbend(sb) + 1;
7337 +       if (nbr <= 0)
7338 +               nbr = 1;
7339 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
7340 +       if (dinfo->di_hdentry) {
7341 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
7342 +               dinfo->di_bstart = -1;
7343 +               dinfo->di_bend = -1;
7344 +               dinfo->di_bwh = -1;
7345 +               dinfo->di_bdiropq = -1;
7346 +               for (i = 0; i < nbr; i++)
7347 +                       dinfo->di_hdentry[i].hd_id = -1;
7348 +               goto out;
7349 +       }
7350 +
7351 +       au_cache_free_dinfo(dinfo);
7352 +       dinfo = NULL;
7353 +
7354 +out:
7355 +       return dinfo;
7356 +}
7357 +
7358 +void au_di_free(struct au_dinfo *dinfo)
7359 +{
7360 +       struct au_hdentry *p;
7361 +       aufs_bindex_t bend, bindex;
7362 +
7363 +       /* dentry may not be revalidated */
7364 +       bindex = dinfo->di_bstart;
7365 +       if (bindex >= 0) {
7366 +               bend = dinfo->di_bend;
7367 +               p = dinfo->di_hdentry + bindex;
7368 +               while (bindex++ <= bend)
7369 +                       au_hdput(p++);
7370 +       }
7371 +       kfree(dinfo->di_hdentry);
7372 +       au_cache_free_dinfo(dinfo);
7373 +}
7374 +
7375 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
7376 +{
7377 +       struct au_hdentry *p;
7378 +       aufs_bindex_t bi;
7379 +
7380 +       AuRwMustWriteLock(&a->di_rwsem);
7381 +       AuRwMustWriteLock(&b->di_rwsem);
7382 +
7383 +#define DiSwap(v, name)                                \
7384 +       do {                                    \
7385 +               v = a->di_##name;               \
7386 +               a->di_##name = b->di_##name;    \
7387 +               b->di_##name = v;               \
7388 +       } while (0)
7389 +
7390 +       DiSwap(p, hdentry);
7391 +       DiSwap(bi, bstart);
7392 +       DiSwap(bi, bend);
7393 +       DiSwap(bi, bwh);
7394 +       DiSwap(bi, bdiropq);
7395 +       /* smp_mb(); */
7396 +
7397 +#undef DiSwap
7398 +}
7399 +
7400 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
7401 +{
7402 +       AuRwMustWriteLock(&dst->di_rwsem);
7403 +       AuRwMustWriteLock(&src->di_rwsem);
7404 +
7405 +       dst->di_bstart = src->di_bstart;
7406 +       dst->di_bend = src->di_bend;
7407 +       dst->di_bwh = src->di_bwh;
7408 +       dst->di_bdiropq = src->di_bdiropq;
7409 +       /* smp_mb(); */
7410 +}
7411 +
7412 +int au_di_init(struct dentry *dentry)
7413 +{
7414 +       int err;
7415 +       struct super_block *sb;
7416 +       struct au_dinfo *dinfo;
7417 +
7418 +       err = 0;
7419 +       sb = dentry->d_sb;
7420 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
7421 +       if (dinfo) {
7422 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
7423 +               /* smp_mb(); */ /* atomic_set */
7424 +               dentry->d_fsdata = dinfo;
7425 +       } else
7426 +               err = -ENOMEM;
7427 +
7428 +       return err;
7429 +}
7430 +
7431 +void au_di_fin(struct dentry *dentry)
7432 +{
7433 +       struct au_dinfo *dinfo;
7434 +
7435 +       dinfo = au_di(dentry);
7436 +       AuRwDestroy(&dinfo->di_rwsem);
7437 +       au_di_free(dinfo);
7438 +}
7439 +
7440 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
7441 +{
7442 +       int err, sz;
7443 +       struct au_hdentry *hdp;
7444 +
7445 +       AuRwMustWriteLock(&dinfo->di_rwsem);
7446 +
7447 +       err = -ENOMEM;
7448 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
7449 +       if (!sz)
7450 +               sz = sizeof(*hdp);
7451 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
7452 +       if (hdp) {
7453 +               dinfo->di_hdentry = hdp;
7454 +               err = 0;
7455 +       }
7456 +
7457 +       return err;
7458 +}
7459 +
7460 +/* ---------------------------------------------------------------------- */
7461 +
7462 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
7463 +{
7464 +       switch (lsc) {
7465 +       case AuLsc_DI_CHILD:
7466 +               ii_write_lock_child(inode);
7467 +               break;
7468 +       case AuLsc_DI_CHILD2:
7469 +               ii_write_lock_child2(inode);
7470 +               break;
7471 +       case AuLsc_DI_CHILD3:
7472 +               ii_write_lock_child3(inode);
7473 +               break;
7474 +       case AuLsc_DI_PARENT:
7475 +               ii_write_lock_parent(inode);
7476 +               break;
7477 +       case AuLsc_DI_PARENT2:
7478 +               ii_write_lock_parent2(inode);
7479 +               break;
7480 +       case AuLsc_DI_PARENT3:
7481 +               ii_write_lock_parent3(inode);
7482 +               break;
7483 +       default:
7484 +               BUG();
7485 +       }
7486 +}
7487 +
7488 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
7489 +{
7490 +       switch (lsc) {
7491 +       case AuLsc_DI_CHILD:
7492 +               ii_read_lock_child(inode);
7493 +               break;
7494 +       case AuLsc_DI_CHILD2:
7495 +               ii_read_lock_child2(inode);
7496 +               break;
7497 +       case AuLsc_DI_CHILD3:
7498 +               ii_read_lock_child3(inode);
7499 +               break;
7500 +       case AuLsc_DI_PARENT:
7501 +               ii_read_lock_parent(inode);
7502 +               break;
7503 +       case AuLsc_DI_PARENT2:
7504 +               ii_read_lock_parent2(inode);
7505 +               break;
7506 +       case AuLsc_DI_PARENT3:
7507 +               ii_read_lock_parent3(inode);
7508 +               break;
7509 +       default:
7510 +               BUG();
7511 +       }
7512 +}
7513 +
7514 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
7515 +{
7516 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
7517 +       if (d->d_inode) {
7518 +               if (au_ftest_lock(flags, IW))
7519 +                       do_ii_write_lock(d->d_inode, lsc);
7520 +               else if (au_ftest_lock(flags, IR))
7521 +                       do_ii_read_lock(d->d_inode, lsc);
7522 +       }
7523 +}
7524 +
7525 +void di_read_unlock(struct dentry *d, int flags)
7526 +{
7527 +       if (d->d_inode) {
7528 +               if (au_ftest_lock(flags, IW)) {
7529 +                       au_dbg_verify_dinode(d);
7530 +                       ii_write_unlock(d->d_inode);
7531 +               } else if (au_ftest_lock(flags, IR)) {
7532 +                       au_dbg_verify_dinode(d);
7533 +                       ii_read_unlock(d->d_inode);
7534 +               }
7535 +       }
7536 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
7537 +}
7538 +
7539 +void di_downgrade_lock(struct dentry *d, int flags)
7540 +{
7541 +       if (d->d_inode && au_ftest_lock(flags, IR))
7542 +               ii_downgrade_lock(d->d_inode);
7543 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
7544 +}
7545 +
7546 +void di_write_lock(struct dentry *d, unsigned int lsc)
7547 +{
7548 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
7549 +       if (d->d_inode)
7550 +               do_ii_write_lock(d->d_inode, lsc);
7551 +}
7552 +
7553 +void di_write_unlock(struct dentry *d)
7554 +{
7555 +       au_dbg_verify_dinode(d);
7556 +       if (d->d_inode)
7557 +               ii_write_unlock(d->d_inode);
7558 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
7559 +}
7560 +
7561 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
7562 +{
7563 +       AuDebugOn(d1 == d2
7564 +                 || d1->d_inode == d2->d_inode
7565 +                 || d1->d_sb != d2->d_sb);
7566 +
7567 +       if (isdir && au_test_subdir(d1, d2)) {
7568 +               di_write_lock_child(d1);
7569 +               di_write_lock_child2(d2);
7570 +       } else {
7571 +               /* there should be no races */
7572 +               di_write_lock_child(d2);
7573 +               di_write_lock_child2(d1);
7574 +       }
7575 +}
7576 +
7577 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
7578 +{
7579 +       AuDebugOn(d1 == d2
7580 +                 || d1->d_inode == d2->d_inode
7581 +                 || d1->d_sb != d2->d_sb);
7582 +
7583 +       if (isdir && au_test_subdir(d1, d2)) {
7584 +               di_write_lock_parent(d1);
7585 +               di_write_lock_parent2(d2);
7586 +       } else {
7587 +               /* there should be no races */
7588 +               di_write_lock_parent(d2);
7589 +               di_write_lock_parent2(d1);
7590 +       }
7591 +}
7592 +
7593 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
7594 +{
7595 +       di_write_unlock(d1);
7596 +       if (d1->d_inode == d2->d_inode)
7597 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
7598 +       else
7599 +               di_write_unlock(d2);
7600 +}
7601 +
7602 +/* ---------------------------------------------------------------------- */
7603 +
7604 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
7605 +{
7606 +       struct dentry *d;
7607 +
7608 +       DiMustAnyLock(dentry);
7609 +
7610 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7611 +               return NULL;
7612 +       AuDebugOn(bindex < 0);
7613 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
7614 +       AuDebugOn(d && d->d_count <= 0);
7615 +       return d;
7616 +}
7617 +
7618 +/*
7619 + * extended version of au_h_dptr().
7620 + * returns a hashed and positive h_dentry in bindex, NULL, or error.
7621 + */
7622 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
7623 +{
7624 +       struct dentry *h_dentry;
7625 +       struct inode *inode, *h_inode;
7626 +
7627 +       inode = dentry->d_inode;
7628 +       AuDebugOn(!inode);
7629 +
7630 +       h_dentry = NULL;
7631 +       if (au_dbstart(dentry) <= bindex
7632 +           && bindex <= au_dbend(dentry))
7633 +               h_dentry = au_h_dptr(dentry, bindex);
7634 +       if (h_dentry && !au_d_hashed_positive(h_dentry)) {
7635 +               dget(h_dentry);
7636 +               goto out; /* success */
7637 +       }
7638 +
7639 +       AuDebugOn(bindex < au_ibstart(inode));
7640 +       AuDebugOn(au_ibend(inode) < bindex);
7641 +       h_inode = au_h_iptr(inode, bindex);
7642 +       h_dentry = d_find_alias(h_inode);
7643 +       if (h_dentry) {
7644 +               if (!IS_ERR(h_dentry)) {
7645 +                       if (!au_d_hashed_positive(h_dentry))
7646 +                               goto out; /* success */
7647 +                       dput(h_dentry);
7648 +               } else
7649 +                       goto out;
7650 +       }
7651 +
7652 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
7653 +               h_dentry = au_plink_lkup(inode, bindex);
7654 +               AuDebugOn(!h_dentry);
7655 +               if (!IS_ERR(h_dentry)) {
7656 +                       if (!au_d_hashed_positive(h_dentry))
7657 +                               goto out; /* success */
7658 +                       dput(h_dentry);
7659 +                       h_dentry = NULL;
7660 +               }
7661 +       }
7662 +
7663 +out:
7664 +       AuDbgDentry(h_dentry);
7665 +       return h_dentry;
7666 +}
7667 +
7668 +aufs_bindex_t au_dbtail(struct dentry *dentry)
7669 +{
7670 +       aufs_bindex_t bend, bwh;
7671 +
7672 +       bend = au_dbend(dentry);
7673 +       if (0 <= bend) {
7674 +               bwh = au_dbwh(dentry);
7675 +               if (!bwh)
7676 +                       return bwh;
7677 +               if (0 < bwh && bwh < bend)
7678 +                       return bwh - 1;
7679 +       }
7680 +       return bend;
7681 +}
7682 +
7683 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
7684 +{
7685 +       aufs_bindex_t bend, bopq;
7686 +
7687 +       bend = au_dbtail(dentry);
7688 +       if (0 <= bend) {
7689 +               bopq = au_dbdiropq(dentry);
7690 +               if (0 <= bopq && bopq < bend)
7691 +                       bend = bopq;
7692 +       }
7693 +       return bend;
7694 +}
7695 +
7696 +/* ---------------------------------------------------------------------- */
7697 +
7698 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7699 +                  struct dentry *h_dentry)
7700 +{
7701 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
7702 +       struct au_branch *br;
7703 +
7704 +       DiMustWriteLock(dentry);
7705 +
7706 +       au_hdput(hd);
7707 +       hd->hd_dentry = h_dentry;
7708 +       if (h_dentry) {
7709 +               br = au_sbr(dentry->d_sb, bindex);
7710 +               hd->hd_id = br->br_id;
7711 +       }
7712 +}
7713 +
7714 +int au_dbrange_test(struct dentry *dentry)
7715 +{
7716 +       int err;
7717 +       aufs_bindex_t bstart, bend;
7718 +
7719 +       err = 0;
7720 +       bstart = au_dbstart(dentry);
7721 +       bend = au_dbend(dentry);
7722 +       if (bstart >= 0)
7723 +               AuDebugOn(bend < 0 && bstart > bend);
7724 +       else {
7725 +               err = -EIO;
7726 +               AuDebugOn(bend >= 0);
7727 +       }
7728 +
7729 +       return err;
7730 +}
7731 +
7732 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
7733 +{
7734 +       int err;
7735 +
7736 +       err = 0;
7737 +       if (unlikely(au_digen(dentry) != sigen
7738 +                    || au_iigen_test(dentry->d_inode, sigen)))
7739 +               err = -EIO;
7740 +
7741 +       return err;
7742 +}
7743 +
7744 +void au_update_digen(struct dentry *dentry)
7745 +{
7746 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
7747 +       /* smp_mb(); */ /* atomic_set */
7748 +}
7749 +
7750 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
7751 +{
7752 +       struct au_dinfo *dinfo;
7753 +       struct dentry *h_d;
7754 +       struct au_hdentry *hdp;
7755 +
7756 +       DiMustWriteLock(dentry);
7757 +
7758 +       dinfo = au_di(dentry);
7759 +       if (!dinfo || dinfo->di_bstart < 0)
7760 +               return;
7761 +
7762 +       hdp = dinfo->di_hdentry;
7763 +       if (do_put_zero) {
7764 +               aufs_bindex_t bindex, bend;
7765 +
7766 +               bend = dinfo->di_bend;
7767 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
7768 +                       h_d = hdp[0 + bindex].hd_dentry;
7769 +                       if (h_d && !h_d->d_inode)
7770 +                               au_set_h_dptr(dentry, bindex, NULL);
7771 +               }
7772 +       }
7773 +
7774 +       dinfo->di_bstart = -1;
7775 +       while (++dinfo->di_bstart <= dinfo->di_bend)
7776 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
7777 +                       break;
7778 +       if (dinfo->di_bstart > dinfo->di_bend) {
7779 +               dinfo->di_bstart = -1;
7780 +               dinfo->di_bend = -1;
7781 +               return;
7782 +       }
7783 +
7784 +       dinfo->di_bend++;
7785 +       while (0 <= --dinfo->di_bend)
7786 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
7787 +                       break;
7788 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
7789 +}
7790 +
7791 +void au_update_dbstart(struct dentry *dentry)
7792 +{
7793 +       aufs_bindex_t bindex, bend;
7794 +       struct dentry *h_dentry;
7795 +
7796 +       bend = au_dbend(dentry);
7797 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
7798 +               h_dentry = au_h_dptr(dentry, bindex);
7799 +               if (!h_dentry)
7800 +                       continue;
7801 +               if (h_dentry->d_inode) {
7802 +                       au_set_dbstart(dentry, bindex);
7803 +                       return;
7804 +               }
7805 +               au_set_h_dptr(dentry, bindex, NULL);
7806 +       }
7807 +}
7808 +
7809 +void au_update_dbend(struct dentry *dentry)
7810 +{
7811 +       aufs_bindex_t bindex, bstart;
7812 +       struct dentry *h_dentry;
7813 +
7814 +       bstart = au_dbstart(dentry);
7815 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
7816 +               h_dentry = au_h_dptr(dentry, bindex);
7817 +               if (!h_dentry)
7818 +                       continue;
7819 +               if (h_dentry->d_inode) {
7820 +                       au_set_dbend(dentry, bindex);
7821 +                       return;
7822 +               }
7823 +               au_set_h_dptr(dentry, bindex, NULL);
7824 +       }
7825 +}
7826 +
7827 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
7828 +{
7829 +       aufs_bindex_t bindex, bend;
7830 +
7831 +       bend = au_dbend(dentry);
7832 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
7833 +               if (au_h_dptr(dentry, bindex) == h_dentry)
7834 +                       return bindex;
7835 +       return -1;
7836 +}
7837 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
7838 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
7839 +++ linux/fs/aufs/dir.c 2012-03-19 12:24:13.679801090 +0100
7840 @@ -0,0 +1,636 @@
7841 +/*
7842 + * Copyright (C) 2005-2012 Junjiro R. Okajima
7843 + *
7844 + * This program, aufs is free software; you can redistribute it and/or modify
7845 + * it under the terms of the GNU General Public License as published by
7846 + * the Free Software Foundation; either version 2 of the License, or
7847 + * (at your option) any later version.
7848 + *
7849 + * This program is distributed in the hope that it will be useful,
7850 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7851 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7852 + * GNU General Public License for more details.
7853 + *
7854 + * You should have received a copy of the GNU General Public License
7855 + * along with this program; if not, write to the Free Software
7856 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7857 + */
7858 +
7859 +/*
7860 + * directory operations
7861 + */
7862 +
7863 +#include <linux/fs_stack.h>
7864 +#include "aufs.h"
7865 +
7866 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
7867 +{
7868 +       unsigned int nlink;
7869 +
7870 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
7871 +
7872 +       nlink = dir->i_nlink;
7873 +       nlink += h_dir->i_nlink - 2;
7874 +       if (h_dir->i_nlink < 2)
7875 +               nlink += 2;
7876 +       /* 0 can happen in revaliding */
7877 +       vfsub_set_nlink(dir, nlink);
7878 +}
7879 +
7880 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
7881 +{
7882 +       unsigned int nlink;
7883 +
7884 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
7885 +
7886 +       nlink = dir->i_nlink;
7887 +       nlink -= h_dir->i_nlink - 2;
7888 +       if (h_dir->i_nlink < 2)
7889 +               nlink -= 2;
7890 +       /* no vfsub version. nlink == 0 means the branch-fs is broken */
7891 +       set_nlink(dir, nlink);
7892 +}
7893 +
7894 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
7895 +{
7896 +       loff_t sz;
7897 +       aufs_bindex_t bindex, bend;
7898 +       struct file *h_file;
7899 +       struct dentry *h_dentry;
7900 +
7901 +       sz = 0;
7902 +       if (file) {
7903 +               AuDebugOn(!file->f_dentry);
7904 +               AuDebugOn(!file->f_dentry->d_inode);
7905 +               AuDebugOn(!S_ISDIR(file->f_dentry->d_inode->i_mode));
7906 +
7907 +               bend = au_fbend_dir(file);
7908 +               for (bindex = au_fbstart(file);
7909 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
7910 +                    bindex++) {
7911 +                       h_file = au_hf_dir(file, bindex);
7912 +                       if (h_file
7913 +                           && h_file->f_dentry
7914 +                           && h_file->f_dentry->d_inode)
7915 +                               sz += i_size_read(h_file->f_dentry->d_inode);
7916 +               }
7917 +       } else {
7918 +               AuDebugOn(!dentry);
7919 +               AuDebugOn(!dentry->d_inode);
7920 +               AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
7921 +
7922 +               bend = au_dbtaildir(dentry);
7923 +               for (bindex = au_dbstart(dentry);
7924 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
7925 +                    bindex++) {
7926 +                       h_dentry = au_h_dptr(dentry, bindex);
7927 +                       if (h_dentry && h_dentry->d_inode)
7928 +                               sz += i_size_read(h_dentry->d_inode);
7929 +               }
7930 +       }
7931 +       if (sz < KMALLOC_MAX_SIZE)
7932 +               sz = roundup_pow_of_two(sz);
7933 +       if (sz > KMALLOC_MAX_SIZE)
7934 +               sz = KMALLOC_MAX_SIZE;
7935 +       else if (sz < NAME_MAX) {
7936 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
7937 +               sz = AUFS_RDBLK_DEF;
7938 +       }
7939 +       return sz;
7940 +}
7941 +
7942 +/* ---------------------------------------------------------------------- */
7943 +
7944 +static int reopen_dir(struct file *file)
7945 +{
7946 +       int err;
7947 +       unsigned int flags;
7948 +       aufs_bindex_t bindex, btail, bstart;
7949 +       struct dentry *dentry, *h_dentry;
7950 +       struct file *h_file;
7951 +
7952 +       /* open all lower dirs */
7953 +       dentry = file->f_dentry;
7954 +       bstart = au_dbstart(dentry);
7955 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
7956 +               au_set_h_fptr(file, bindex, NULL);
7957 +       au_set_fbstart(file, bstart);
7958 +
7959 +       btail = au_dbtaildir(dentry);
7960 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
7961 +               au_set_h_fptr(file, bindex, NULL);
7962 +       au_set_fbend_dir(file, btail);
7963 +
7964 +       flags = vfsub_file_flags(file);
7965 +       for (bindex = bstart; bindex <= btail; bindex++) {
7966 +               h_dentry = au_h_dptr(dentry, bindex);
7967 +               if (!h_dentry)
7968 +                       continue;
7969 +               h_file = au_hf_dir(file, bindex);
7970 +               if (h_file)
7971 +                       continue;
7972 +
7973 +               h_file = au_h_open(dentry, bindex, flags, file);
7974 +               err = PTR_ERR(h_file);
7975 +               if (IS_ERR(h_file))
7976 +                       goto out; /* close all? */
7977 +               au_set_h_fptr(file, bindex, h_file);
7978 +       }
7979 +       au_update_figen(file);
7980 +       /* todo: necessary? */
7981 +       /* file->f_ra = h_file->f_ra; */
7982 +       err = 0;
7983 +
7984 +out:
7985 +       return err;
7986 +}
7987 +
7988 +static int do_open_dir(struct file *file, int flags)
7989 +{
7990 +       int err;
7991 +       aufs_bindex_t bindex, btail;
7992 +       struct dentry *dentry, *h_dentry;
7993 +       struct file *h_file;
7994 +
7995 +       FiMustWriteLock(file);
7996 +
7997 +       dentry = file->f_dentry;
7998 +       err = au_alive_dir(dentry);
7999 +       if (unlikely(err))
8000 +               goto out;
8001 +
8002 +       file->f_version = dentry->d_inode->i_version;
8003 +       bindex = au_dbstart(dentry);
8004 +       au_set_fbstart(file, bindex);
8005 +       btail = au_dbtaildir(dentry);
8006 +       au_set_fbend_dir(file, btail);
8007 +       for (; !err && bindex <= btail; bindex++) {
8008 +               h_dentry = au_h_dptr(dentry, bindex);
8009 +               if (!h_dentry)
8010 +                       continue;
8011 +
8012 +               h_file = au_h_open(dentry, bindex, flags, file);
8013 +               if (IS_ERR(h_file)) {
8014 +                       err = PTR_ERR(h_file);
8015 +                       break;
8016 +               }
8017 +               au_set_h_fptr(file, bindex, h_file);
8018 +       }
8019 +       au_update_figen(file);
8020 +       /* todo: necessary? */
8021 +       /* file->f_ra = h_file->f_ra; */
8022 +       if (!err)
8023 +               return 0; /* success */
8024 +
8025 +       /* close all */
8026 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
8027 +               au_set_h_fptr(file, bindex, NULL);
8028 +       au_set_fbstart(file, -1);
8029 +       au_set_fbend_dir(file, -1);
8030 +
8031 +out:
8032 +       return err;
8033 +}
8034 +
8035 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8036 +                        struct file *file)
8037 +{
8038 +       int err;
8039 +       struct super_block *sb;
8040 +       struct au_fidir *fidir;
8041 +
8042 +       err = -ENOMEM;
8043 +       sb = file->f_dentry->d_sb;
8044 +       si_read_lock(sb, AuLock_FLUSH);
8045 +       fidir = au_fidir_alloc(sb);
8046 +       if (fidir) {
8047 +               err = au_do_open(file, do_open_dir, fidir);
8048 +               if (unlikely(err))
8049 +                       kfree(fidir);
8050 +       }
8051 +       si_read_unlock(sb);
8052 +       return err;
8053 +}
8054 +
8055 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8056 +                           struct file *file)
8057 +{
8058 +       struct au_vdir *vdir_cache;
8059 +       struct au_finfo *finfo;
8060 +       struct au_fidir *fidir;
8061 +       aufs_bindex_t bindex, bend;
8062 +
8063 +       finfo = au_fi(file);
8064 +       fidir = finfo->fi_hdir;
8065 +       if (fidir) {
8066 +               /* remove me from sb->s_files */
8067 +               file_sb_list_del(file);
8068 +
8069 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
8070 +               if (vdir_cache)
8071 +                       au_vdir_free(vdir_cache);
8072 +
8073 +               bindex = finfo->fi_btop;
8074 +               if (bindex >= 0) {
8075 +                       /*
8076 +                        * calls fput() instead of filp_close(),
8077 +                        * since no dnotify or lock for the lower file.
8078 +                        */
8079 +                       bend = fidir->fd_bbot;
8080 +                       for (; bindex <= bend; bindex++)
8081 +                               au_set_h_fptr(file, bindex, NULL);
8082 +               }
8083 +               kfree(fidir);
8084 +               finfo->fi_hdir = NULL;
8085 +       }
8086 +       au_finfo_fin(file);
8087 +       return 0;
8088 +}
8089 +
8090 +/* ---------------------------------------------------------------------- */
8091 +
8092 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
8093 +{
8094 +       int err;
8095 +       aufs_bindex_t bindex, bend;
8096 +       struct file *h_file;
8097 +
8098 +       err = 0;
8099 +       bend = au_fbend_dir(file);
8100 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8101 +               h_file = au_hf_dir(file, bindex);
8102 +               if (h_file)
8103 +                       err = vfsub_flush(h_file, id);
8104 +       }
8105 +       return err;
8106 +}
8107 +
8108 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
8109 +{
8110 +       return au_do_flush(file, id, au_do_flush_dir);
8111 +}
8112 +
8113 +/* ---------------------------------------------------------------------- */
8114 +
8115 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
8116 +{
8117 +       int err;
8118 +       aufs_bindex_t bend, bindex;
8119 +       struct inode *inode;
8120 +       struct super_block *sb;
8121 +
8122 +       err = 0;
8123 +       sb = dentry->d_sb;
8124 +       inode = dentry->d_inode;
8125 +       IMustLock(inode);
8126 +       bend = au_dbend(dentry);
8127 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
8128 +               struct path h_path;
8129 +
8130 +               if (au_test_ro(sb, bindex, inode))
8131 +                       continue;
8132 +               h_path.dentry = au_h_dptr(dentry, bindex);
8133 +               if (!h_path.dentry)
8134 +                       continue;
8135 +
8136 +               h_path.mnt = au_sbr_mnt(sb, bindex);
8137 +               err = vfsub_fsync(NULL, &h_path, datasync);
8138 +       }
8139 +
8140 +       return err;
8141 +}
8142 +
8143 +static int au_do_fsync_dir(struct file *file, int datasync)
8144 +{
8145 +       int err;
8146 +       aufs_bindex_t bend, bindex;
8147 +       struct file *h_file;
8148 +       struct super_block *sb;
8149 +       struct inode *inode;
8150 +
8151 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8152 +       if (unlikely(err))
8153 +               goto out;
8154 +
8155 +       sb = file->f_dentry->d_sb;
8156 +       inode = file->f_dentry->d_inode;
8157 +       bend = au_fbend_dir(file);
8158 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8159 +               h_file = au_hf_dir(file, bindex);
8160 +               if (!h_file || au_test_ro(sb, bindex, inode))
8161 +                       continue;
8162 +
8163 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
8164 +       }
8165 +
8166 +out:
8167 +       return err;
8168 +}
8169 +
8170 +/*
8171 + * @file may be NULL
8172 + */
8173 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
8174 +                         int datasync)
8175 +{
8176 +       int err;
8177 +       struct dentry *dentry;
8178 +       struct super_block *sb;
8179 +       struct mutex *mtx;
8180 +
8181 +       err = 0;
8182 +       dentry = file->f_dentry;
8183 +       mtx = &dentry->d_inode->i_mutex;
8184 +       mutex_lock(mtx);
8185 +       sb = dentry->d_sb;
8186 +       si_noflush_read_lock(sb);
8187 +       if (file)
8188 +               err = au_do_fsync_dir(file, datasync);
8189 +       else {
8190 +               di_write_lock_child(dentry);
8191 +               err = au_do_fsync_dir_no_file(dentry, datasync);
8192 +       }
8193 +       au_cpup_attr_timesizes(dentry->d_inode);
8194 +       di_write_unlock(dentry);
8195 +       if (file)
8196 +               fi_write_unlock(file);
8197 +
8198 +       si_read_unlock(sb);
8199 +       mutex_unlock(mtx);
8200 +       return err;
8201 +}
8202 +
8203 +/* ---------------------------------------------------------------------- */
8204 +
8205 +static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
8206 +{
8207 +       int err;
8208 +       struct dentry *dentry;
8209 +       struct inode *inode, *h_inode;
8210 +       struct super_block *sb;
8211 +
8212 +       dentry = file->f_dentry;
8213 +       inode = dentry->d_inode;
8214 +       IMustLock(inode);
8215 +
8216 +       sb = dentry->d_sb;
8217 +       si_read_lock(sb, AuLock_FLUSH);
8218 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8219 +       if (unlikely(err))
8220 +               goto out;
8221 +       err = au_alive_dir(dentry);
8222 +       if (!err)
8223 +               err = au_vdir_init(file);
8224 +       di_downgrade_lock(dentry, AuLock_IR);
8225 +       if (unlikely(err))
8226 +               goto out_unlock;
8227 +
8228 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
8229 +       if (!au_test_nfsd()) {
8230 +               err = au_vdir_fill_de(file, dirent, filldir);
8231 +               fsstack_copy_attr_atime(inode, h_inode);
8232 +       } else {
8233 +               /*
8234 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
8235 +                * encode_fh() and others.
8236 +                */
8237 +               atomic_inc(&h_inode->i_count);
8238 +               di_read_unlock(dentry, AuLock_IR);
8239 +               si_read_unlock(sb);
8240 +               err = au_vdir_fill_de(file, dirent, filldir);
8241 +               fsstack_copy_attr_atime(inode, h_inode);
8242 +               fi_write_unlock(file);
8243 +               iput(h_inode);
8244 +
8245 +               AuTraceErr(err);
8246 +               return err;
8247 +       }
8248 +
8249 +out_unlock:
8250 +       di_read_unlock(dentry, AuLock_IR);
8251 +       fi_write_unlock(file);
8252 +out:
8253 +       si_read_unlock(sb);
8254 +       return err;
8255 +}
8256 +
8257 +/* ---------------------------------------------------------------------- */
8258 +
8259 +#define AuTestEmpty_WHONLY     1
8260 +#define AuTestEmpty_CALLED     (1 << 1)
8261 +#define AuTestEmpty_SHWH       (1 << 2)
8262 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
8263 +#define au_fset_testempty(flags, name) \
8264 +       do { (flags) |= AuTestEmpty_##name; } while (0)
8265 +#define au_fclr_testempty(flags, name) \
8266 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
8267 +
8268 +#ifndef CONFIG_AUFS_SHWH
8269 +#undef AuTestEmpty_SHWH
8270 +#define AuTestEmpty_SHWH       0
8271 +#endif
8272 +
8273 +struct test_empty_arg {
8274 +       struct au_nhash *whlist;
8275 +       unsigned int flags;
8276 +       int err;
8277 +       aufs_bindex_t bindex;
8278 +};
8279 +
8280 +static int test_empty_cb(void *__arg, const char *__name, int namelen,
8281 +                        loff_t offset __maybe_unused, u64 ino,
8282 +                        unsigned int d_type)
8283 +{
8284 +       struct test_empty_arg *arg = __arg;
8285 +       char *name = (void *)__name;
8286 +
8287 +       arg->err = 0;
8288 +       au_fset_testempty(arg->flags, CALLED);
8289 +       /* smp_mb(); */
8290 +       if (name[0] == '.'
8291 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
8292 +               goto out; /* success */
8293 +
8294 +       if (namelen <= AUFS_WH_PFX_LEN
8295 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
8296 +               if (au_ftest_testempty(arg->flags, WHONLY)
8297 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
8298 +                       arg->err = -ENOTEMPTY;
8299 +               goto out;
8300 +       }
8301 +
8302 +       name += AUFS_WH_PFX_LEN;
8303 +       namelen -= AUFS_WH_PFX_LEN;
8304 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
8305 +               arg->err = au_nhash_append_wh
8306 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
8307 +                        au_ftest_testempty(arg->flags, SHWH));
8308 +
8309 +out:
8310 +       /* smp_mb(); */
8311 +       AuTraceErr(arg->err);
8312 +       return arg->err;
8313 +}
8314 +
8315 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8316 +{
8317 +       int err;
8318 +       struct file *h_file;
8319 +
8320 +       h_file = au_h_open(dentry, arg->bindex,
8321 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
8322 +                          /*file*/NULL);
8323 +       err = PTR_ERR(h_file);
8324 +       if (IS_ERR(h_file))
8325 +               goto out;
8326 +
8327 +       err = 0;
8328 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
8329 +           && !h_file->f_dentry->d_inode->i_nlink)
8330 +               goto out_put;
8331 +
8332 +       do {
8333 +               arg->err = 0;
8334 +               au_fclr_testempty(arg->flags, CALLED);
8335 +               /* smp_mb(); */
8336 +               err = vfsub_readdir(h_file, test_empty_cb, arg);
8337 +               if (err >= 0)
8338 +                       err = arg->err;
8339 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
8340 +
8341 +out_put:
8342 +       fput(h_file);
8343 +       au_sbr_put(dentry->d_sb, arg->bindex);
8344 +out:
8345 +       return err;
8346 +}
8347 +
8348 +struct do_test_empty_args {
8349 +       int *errp;
8350 +       struct dentry *dentry;
8351 +       struct test_empty_arg *arg;
8352 +};
8353 +
8354 +static void call_do_test_empty(void *args)
8355 +{
8356 +       struct do_test_empty_args *a = args;
8357 +       *a->errp = do_test_empty(a->dentry, a->arg);
8358 +}
8359 +
8360 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8361 +{
8362 +       int err, wkq_err;
8363 +       struct dentry *h_dentry;
8364 +       struct inode *h_inode;
8365 +
8366 +       h_dentry = au_h_dptr(dentry, arg->bindex);
8367 +       h_inode = h_dentry->d_inode;
8368 +       /* todo: i_mode changes anytime? */
8369 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
8370 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
8371 +       mutex_unlock(&h_inode->i_mutex);
8372 +       if (!err)
8373 +               err = do_test_empty(dentry, arg);
8374 +       else {
8375 +               struct do_test_empty_args args = {
8376 +                       .errp   = &err,
8377 +                       .dentry = dentry,
8378 +                       .arg    = arg
8379 +               };
8380 +               unsigned int flags = arg->flags;
8381 +
8382 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
8383 +               if (unlikely(wkq_err))
8384 +                       err = wkq_err;
8385 +               arg->flags = flags;
8386 +       }
8387 +
8388 +       return err;
8389 +}
8390 +
8391 +int au_test_empty_lower(struct dentry *dentry)
8392 +{
8393 +       int err;
8394 +       unsigned int rdhash;
8395 +       aufs_bindex_t bindex, bstart, btail;
8396 +       struct au_nhash whlist;
8397 +       struct test_empty_arg arg;
8398 +
8399 +       SiMustAnyLock(dentry->d_sb);
8400 +
8401 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
8402 +       if (!rdhash)
8403 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
8404 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
8405 +       if (unlikely(err))
8406 +               goto out;
8407 +
8408 +       arg.flags = 0;
8409 +       arg.whlist = &whlist;
8410 +       bstart = au_dbstart(dentry);
8411 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8412 +               au_fset_testempty(arg.flags, SHWH);
8413 +       arg.bindex = bstart;
8414 +       err = do_test_empty(dentry, &arg);
8415 +       if (unlikely(err))
8416 +               goto out_whlist;
8417 +
8418 +       au_fset_testempty(arg.flags, WHONLY);
8419 +       btail = au_dbtaildir(dentry);
8420 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
8421 +               struct dentry *h_dentry;
8422 +
8423 +               h_dentry = au_h_dptr(dentry, bindex);
8424 +               if (h_dentry && h_dentry->d_inode) {
8425 +                       arg.bindex = bindex;
8426 +                       err = do_test_empty(dentry, &arg);
8427 +               }
8428 +       }
8429 +
8430 +out_whlist:
8431 +       au_nhash_wh_free(&whlist);
8432 +out:
8433 +       return err;
8434 +}
8435 +
8436 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
8437 +{
8438 +       int err;
8439 +       struct test_empty_arg arg;
8440 +       aufs_bindex_t bindex, btail;
8441 +
8442 +       err = 0;
8443 +       arg.whlist = whlist;
8444 +       arg.flags = AuTestEmpty_WHONLY;
8445 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8446 +               au_fset_testempty(arg.flags, SHWH);
8447 +       btail = au_dbtaildir(dentry);
8448 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
8449 +               struct dentry *h_dentry;
8450 +
8451 +               h_dentry = au_h_dptr(dentry, bindex);
8452 +               if (h_dentry && h_dentry->d_inode) {
8453 +                       arg.bindex = bindex;
8454 +                       err = sio_test_empty(dentry, &arg);
8455 +               }
8456 +       }
8457 +
8458 +       return err;
8459 +}
8460 +
8461 +/* ---------------------------------------------------------------------- */
8462 +
8463 +const struct file_operations aufs_dir_fop = {
8464 +       .owner          = THIS_MODULE,
8465 +       .llseek         = default_llseek,
8466 +       .read           = generic_read_dir,
8467 +       .readdir        = aufs_readdir,
8468 +       .unlocked_ioctl = aufs_ioctl_dir,
8469 +#ifdef CONFIG_COMPAT
8470 +       .compat_ioctl   = aufs_compat_ioctl_dir,
8471 +#endif
8472 +       .open           = aufs_open_dir,
8473 +       .release        = aufs_release_dir,
8474 +       .flush          = aufs_flush_dir,
8475 +       .fsync          = aufs_fsync_dir
8476 +};
8477 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
8478 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
8479 +++ linux/fs/aufs/dir.h 2012-03-19 12:24:13.679801090 +0100
8480 @@ -0,0 +1,137 @@
8481 +/*
8482 + * Copyright (C) 2005-2012 Junjiro R. Okajima
8483 + *
8484 + * This program, aufs is free software; you can redistribute it and/or modify
8485 + * it under the terms of the GNU General Public License as published by
8486 + * the Free Software Foundation; either version 2 of the License, or
8487 + * (at your option) any later version.
8488 + *
8489 + * This program is distributed in the hope that it will be useful,
8490 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8491 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8492 + * GNU General Public License for more details.
8493 + *
8494 + * You should have received a copy of the GNU General Public License
8495 + * along with this program; if not, write to the Free Software
8496 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8497 + */
8498 +
8499 +/*
8500 + * directory operations
8501 + */
8502 +
8503 +#ifndef __AUFS_DIR_H__
8504 +#define __AUFS_DIR_H__
8505 +
8506 +#ifdef __KERNEL__
8507 +
8508 +#include <linux/fs.h>
8509 +
8510 +/* ---------------------------------------------------------------------- */
8511 +
8512 +/* need to be faster and smaller */
8513 +
8514 +struct au_nhash {
8515 +       unsigned int            nh_num;
8516 +       struct hlist_head       *nh_head;
8517 +};
8518 +
8519 +struct au_vdir_destr {
8520 +       unsigned char   len;
8521 +       unsigned char   name[0];
8522 +} __packed;
8523 +
8524 +struct au_vdir_dehstr {
8525 +       struct hlist_node       hash;
8526 +       struct au_vdir_destr    *str;
8527 +} ____cacheline_aligned_in_smp;
8528 +
8529 +struct au_vdir_de {
8530 +       ino_t                   de_ino;
8531 +       unsigned char           de_type;
8532 +       /* caution: packed */
8533 +       struct au_vdir_destr    de_str;
8534 +} __packed;
8535 +
8536 +struct au_vdir_wh {
8537 +       struct hlist_node       wh_hash;
8538 +#ifdef CONFIG_AUFS_SHWH
8539 +       ino_t                   wh_ino;
8540 +       aufs_bindex_t           wh_bindex;
8541 +       unsigned char           wh_type;
8542 +#else
8543 +       aufs_bindex_t           wh_bindex;
8544 +#endif
8545 +       /* caution: packed */
8546 +       struct au_vdir_destr    wh_str;
8547 +} __packed;
8548 +
8549 +union au_vdir_deblk_p {
8550 +       unsigned char           *deblk;
8551 +       struct au_vdir_de       *de;
8552 +};
8553 +
8554 +struct au_vdir {
8555 +       unsigned char   **vd_deblk;
8556 +       unsigned long   vd_nblk;
8557 +       struct {
8558 +               unsigned long           ul;
8559 +               union au_vdir_deblk_p   p;
8560 +       } vd_last;
8561 +
8562 +       unsigned long   vd_version;
8563 +       unsigned int    vd_deblk_sz;
8564 +       unsigned long   vd_jiffy;
8565 +} ____cacheline_aligned_in_smp;
8566 +
8567 +/* ---------------------------------------------------------------------- */
8568 +
8569 +/* dir.c */
8570 +extern const struct file_operations aufs_dir_fop;
8571 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
8572 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
8573 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
8574 +int au_test_empty_lower(struct dentry *dentry);
8575 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
8576 +
8577 +/* vdir.c */
8578 +unsigned int au_rdhash_est(loff_t sz);
8579 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
8580 +void au_nhash_wh_free(struct au_nhash *whlist);
8581 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
8582 +                           int limit);
8583 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
8584 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
8585 +                      unsigned int d_type, aufs_bindex_t bindex,
8586 +                      unsigned char shwh);
8587 +void au_vdir_free(struct au_vdir *vdir);
8588 +int au_vdir_init(struct file *file);
8589 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir);
8590 +
8591 +/* ioctl.c */
8592 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
8593 +
8594 +#ifdef CONFIG_AUFS_RDU
8595 +/* rdu.c */
8596 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
8597 +#ifdef CONFIG_COMPAT
8598 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8599 +                        unsigned long arg);
8600 +#endif
8601 +#else
8602 +static inline long au_rdu_ioctl(struct file *file, unsigned int cmd,
8603 +                               unsigned long arg)
8604 +{
8605 +       return -EINVAL;
8606 +}
8607 +#ifdef CONFIG_COMPAT
8608 +static inline long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8609 +                                      unsigned long arg)
8610 +{
8611 +       return -EINVAL;
8612 +}
8613 +#endif
8614 +#endif
8615 +
8616 +#endif /* __KERNEL__ */
8617 +#endif /* __AUFS_DIR_H__ */
8618 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
8619 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
8620 +++ linux/fs/aufs/dynop.c       2012-03-19 12:24:13.679801090 +0100
8621 @@ -0,0 +1,377 @@
8622 +/*
8623 + * Copyright (C) 2010-2012 Junjiro R. Okajima
8624 + *
8625 + * This program, aufs is free software; you can redistribute it and/or modify
8626 + * it under the terms of the GNU General Public License as published by
8627 + * the Free Software Foundation; either version 2 of the License, or
8628 + * (at your option) any later version.
8629 + *
8630 + * This program is distributed in the hope that it will be useful,
8631 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8632 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8633 + * GNU General Public License for more details.
8634 + *
8635 + * You should have received a copy of the GNU General Public License
8636 + * along with this program; if not, write to the Free Software
8637 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8638 + */
8639 +
8640 +/*
8641 + * dynamically customizable operations for regular files
8642 + */
8643 +
8644 +#include "aufs.h"
8645 +
8646 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
8647 +
8648 +/*
8649 + * How large will these lists be?
8650 + * Usually just a few elements, 20-30 at most for each, I guess.
8651 + */
8652 +static struct au_splhead dynop[AuDyLast];
8653 +
8654 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
8655 +{
8656 +       struct au_dykey *key, *tmp;
8657 +       struct list_head *head;
8658 +
8659 +       key = NULL;
8660 +       head = &spl->head;
8661 +       rcu_read_lock();
8662 +       list_for_each_entry_rcu(tmp, head, dk_list)
8663 +               if (tmp->dk_op.dy_hop == h_op) {
8664 +                       key = tmp;
8665 +                       kref_get(&key->dk_kref);
8666 +                       break;
8667 +               }
8668 +       rcu_read_unlock();
8669 +
8670 +       return key;
8671 +}
8672 +
8673 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
8674 +{
8675 +       struct au_dykey **k, *found;
8676 +       const void *h_op = key->dk_op.dy_hop;
8677 +       int i;
8678 +
8679 +       found = NULL;
8680 +       k = br->br_dykey;
8681 +       for (i = 0; i < AuBrDynOp; i++)
8682 +               if (k[i]) {
8683 +                       if (k[i]->dk_op.dy_hop == h_op) {
8684 +                               found = k[i];
8685 +                               break;
8686 +                       }
8687 +               } else
8688 +                       break;
8689 +       if (!found) {
8690 +               spin_lock(&br->br_dykey_lock);
8691 +               for (; i < AuBrDynOp; i++)
8692 +                       if (k[i]) {
8693 +                               if (k[i]->dk_op.dy_hop == h_op) {
8694 +                                       found = k[i];
8695 +                                       break;
8696 +                               }
8697 +                       } else {
8698 +                               k[i] = key;
8699 +                               break;
8700 +                       }
8701 +               spin_unlock(&br->br_dykey_lock);
8702 +               BUG_ON(i == AuBrDynOp); /* expand the array */
8703 +       }
8704 +
8705 +       return found;
8706 +}
8707 +
8708 +/* kref_get() if @key is already added */
8709 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
8710 +{
8711 +       struct au_dykey *tmp, *found;
8712 +       struct list_head *head;
8713 +       const void *h_op = key->dk_op.dy_hop;
8714 +
8715 +       found = NULL;
8716 +       head = &spl->head;
8717 +       spin_lock(&spl->spin);
8718 +       list_for_each_entry(tmp, head, dk_list)
8719 +               if (tmp->dk_op.dy_hop == h_op) {
8720 +                       kref_get(&tmp->dk_kref);
8721 +                       found = tmp;
8722 +                       break;
8723 +               }
8724 +       if (!found)
8725 +               list_add_rcu(&key->dk_list, head);
8726 +       spin_unlock(&spl->spin);
8727 +
8728 +       if (!found)
8729 +               DyPrSym(key);
8730 +       return found;
8731 +}
8732 +
8733 +static void dy_free_rcu(struct rcu_head *rcu)
8734 +{
8735 +       struct au_dykey *key;
8736 +
8737 +       key = container_of(rcu, struct au_dykey, dk_rcu);
8738 +       DyPrSym(key);
8739 +       kfree(key);
8740 +}
8741 +
8742 +static void dy_free(struct kref *kref)
8743 +{
8744 +       struct au_dykey *key;
8745 +       struct au_splhead *spl;
8746 +
8747 +       key = container_of(kref, struct au_dykey, dk_kref);
8748 +       spl = dynop + key->dk_op.dy_type;
8749 +       au_spl_del_rcu(&key->dk_list, spl);
8750 +       call_rcu(&key->dk_rcu, dy_free_rcu);
8751 +}
8752 +
8753 +void au_dy_put(struct au_dykey *key)
8754 +{
8755 +       kref_put(&key->dk_kref, dy_free);
8756 +}
8757 +
8758 +/* ---------------------------------------------------------------------- */
8759 +
8760 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
8761 +
8762 +#ifdef CONFIG_AUFS_DEBUG
8763 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
8764 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
8765 +#else
8766 +#define DyDbgDeclare(cnt)      do {} while (0)
8767 +#define DyDbgInc(cnt)          do {} while (0)
8768 +#endif
8769 +
8770 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
8771 +       DyDbgInc(cnt);                                                  \
8772 +       if (h_op->func) {                                               \
8773 +               if (src.func)                                           \
8774 +                       dst.func = src.func;                            \
8775 +               else                                                    \
8776 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
8777 +       }                                                               \
8778 +} while (0)
8779 +
8780 +#define DySetForce(func, dst, src) do {                \
8781 +       AuDebugOn(!src.func);                   \
8782 +       DyDbgInc(cnt);                          \
8783 +       dst.func = src.func;                    \
8784 +} while (0)
8785 +
8786 +#define DySetAop(func) \
8787 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
8788 +#define DySetAopForce(func) \
8789 +       DySetForce(func, dyaop->da_op, aufs_aop)
8790 +
8791 +static void dy_aop(struct au_dykey *key, const void *h_op,
8792 +                  struct super_block *h_sb __maybe_unused)
8793 +{
8794 +       struct au_dyaop *dyaop = (void *)key;
8795 +       const struct address_space_operations *h_aop = h_op;
8796 +       DyDbgDeclare(cnt);
8797 +
8798 +       AuDbg("%s\n", au_sbtype(h_sb));
8799 +
8800 +       DySetAop(writepage);
8801 +       DySetAopForce(readpage);        /* force */
8802 +       DySetAop(writepages);
8803 +       DySetAop(set_page_dirty);
8804 +       DySetAop(readpages);
8805 +       DySetAop(write_begin);
8806 +       DySetAop(write_end);
8807 +       DySetAop(bmap);
8808 +       DySetAop(invalidatepage);
8809 +       DySetAop(releasepage);
8810 +       DySetAop(freepage);
8811 +       /* these two will be changed according to an aufs mount option */
8812 +       DySetAop(direct_IO);
8813 +       DySetAop(get_xip_mem);
8814 +       DySetAop(migratepage);
8815 +       DySetAop(launder_page);
8816 +       DySetAop(is_partially_uptodate);
8817 +       DySetAop(error_remove_page);
8818 +
8819 +       DyDbgSize(cnt, *h_aop);
8820 +       dyaop->da_get_xip_mem = h_aop->get_xip_mem;
8821 +}
8822 +
8823 +/* ---------------------------------------------------------------------- */
8824 +
8825 +static void dy_bug(struct kref *kref)
8826 +{
8827 +       BUG();
8828 +}
8829 +
8830 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
8831 +{
8832 +       struct au_dykey *key, *old;
8833 +       struct au_splhead *spl;
8834 +       struct op {
8835 +               unsigned int sz;
8836 +               void (*set)(struct au_dykey *key, const void *h_op,
8837 +                           struct super_block *h_sb __maybe_unused);
8838 +       };
8839 +       static const struct op a[] = {
8840 +               [AuDy_AOP] = {
8841 +                       .sz     = sizeof(struct au_dyaop),
8842 +                       .set    = dy_aop
8843 +               }
8844 +       };
8845 +       const struct op *p;
8846 +
8847 +       spl = dynop + op->dy_type;
8848 +       key = dy_gfind_get(spl, op->dy_hop);
8849 +       if (key)
8850 +               goto out_add; /* success */
8851 +
8852 +       p = a + op->dy_type;
8853 +       key = kzalloc(p->sz, GFP_NOFS);
8854 +       if (unlikely(!key)) {
8855 +               key = ERR_PTR(-ENOMEM);
8856 +               goto out;
8857 +       }
8858 +
8859 +       key->dk_op.dy_hop = op->dy_hop;
8860 +       kref_init(&key->dk_kref);
8861 +       p->set(key, op->dy_hop, br->br_mnt->mnt_sb);
8862 +       old = dy_gadd(spl, key);
8863 +       if (old) {
8864 +               kfree(key);
8865 +               key = old;
8866 +       }
8867 +
8868 +out_add:
8869 +       old = dy_bradd(br, key);
8870 +       if (old)
8871 +               /* its ref-count should never be zero here */
8872 +               kref_put(&key->dk_kref, dy_bug);
8873 +out:
8874 +       return key;
8875 +}
8876 +
8877 +/* ---------------------------------------------------------------------- */
8878 +/*
8879 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
8880 + * This behaviour is neccessary to return an error from open(O_DIRECT) instead
8881 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
8882 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
8883 + * See the aufs manual in detail.
8884 + *
8885 + * To keep this behaviour, aufs has to set NULL to ->get_xip_mem too, and the
8886 + * performance of fadvise() and madvise() may be affected.
8887 + */
8888 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
8889 +{
8890 +       if (!do_dx) {
8891 +               dyaop->da_op.direct_IO = NULL;
8892 +               dyaop->da_op.get_xip_mem = NULL;
8893 +       } else {
8894 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
8895 +               dyaop->da_op.get_xip_mem = aufs_aop.get_xip_mem;
8896 +               if (!dyaop->da_get_xip_mem)
8897 +                       dyaop->da_op.get_xip_mem = NULL;
8898 +       }
8899 +}
8900 +
8901 +static struct au_dyaop *dy_aget(struct au_branch *br,
8902 +                               const struct address_space_operations *h_aop,
8903 +                               int do_dx)
8904 +{
8905 +       struct au_dyaop *dyaop;
8906 +       struct au_dynop op;
8907 +
8908 +       op.dy_type = AuDy_AOP;
8909 +       op.dy_haop = h_aop;
8910 +       dyaop = (void *)dy_get(&op, br);
8911 +       if (IS_ERR(dyaop))
8912 +               goto out;
8913 +       dy_adx(dyaop, do_dx);
8914 +
8915 +out:
8916 +       return dyaop;
8917 +}
8918 +
8919 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
8920 +               struct inode *h_inode)
8921 +{
8922 +       int err, do_dx;
8923 +       struct super_block *sb;
8924 +       struct au_branch *br;
8925 +       struct au_dyaop *dyaop;
8926 +
8927 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
8928 +       IiMustWriteLock(inode);
8929 +
8930 +       sb = inode->i_sb;
8931 +       br = au_sbr(sb, bindex);
8932 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
8933 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
8934 +       err = PTR_ERR(dyaop);
8935 +       if (IS_ERR(dyaop))
8936 +               /* unnecessary to call dy_fput() */
8937 +               goto out;
8938 +
8939 +       err = 0;
8940 +       inode->i_mapping->a_ops = &dyaop->da_op;
8941 +
8942 +out:
8943 +       return err;
8944 +}
8945 +
8946 +/*
8947 + * Is it safe to replace a_ops during the inode/file is in operation?
8948 + * Yes, I hope so.
8949 + */
8950 +int au_dy_irefresh(struct inode *inode)
8951 +{
8952 +       int err;
8953 +       aufs_bindex_t bstart;
8954 +       struct inode *h_inode;
8955 +
8956 +       err = 0;
8957 +       if (S_ISREG(inode->i_mode)) {
8958 +               bstart = au_ibstart(inode);
8959 +               h_inode = au_h_iptr(inode, bstart);
8960 +               err = au_dy_iaop(inode, bstart, h_inode);
8961 +       }
8962 +       return err;
8963 +}
8964 +
8965 +void au_dy_arefresh(int do_dx)
8966 +{
8967 +       struct au_splhead *spl;
8968 +       struct list_head *head;
8969 +       struct au_dykey *key;
8970 +
8971 +       spl = dynop + AuDy_AOP;
8972 +       head = &spl->head;
8973 +       spin_lock(&spl->spin);
8974 +       list_for_each_entry(key, head, dk_list)
8975 +               dy_adx((void *)key, do_dx);
8976 +       spin_unlock(&spl->spin);
8977 +}
8978 +
8979 +/* ---------------------------------------------------------------------- */
8980 +
8981 +void __init au_dy_init(void)
8982 +{
8983 +       int i;
8984 +
8985 +       /* make sure that 'struct au_dykey *' can be any type */
8986 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
8987 +
8988 +       for (i = 0; i < AuDyLast; i++)
8989 +               au_spl_init(dynop + i);
8990 +}
8991 +
8992 +void au_dy_fin(void)
8993 +{
8994 +       int i;
8995 +
8996 +       for (i = 0; i < AuDyLast; i++)
8997 +               WARN_ON(!list_empty(&dynop[i].head));
8998 +}
8999 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
9000 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
9001 +++ linux/fs/aufs/dynop.h       2012-03-19 12:24:13.679801090 +0100
9002 @@ -0,0 +1,76 @@
9003 +/*
9004 + * Copyright (C) 2010-2012 Junjiro R. Okajima
9005 + *
9006 + * This program, aufs is free software; you can redistribute it and/or modify
9007 + * it under the terms of the GNU General Public License as published by
9008 + * the Free Software Foundation; either version 2 of the License, or
9009 + * (at your option) any later version.
9010 + *
9011 + * This program is distributed in the hope that it will be useful,
9012 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9013 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9014 + * GNU General Public License for more details.
9015 + *
9016 + * You should have received a copy of the GNU General Public License
9017 + * along with this program; if not, write to the Free Software
9018 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9019 + */
9020 +
9021 +/*
9022 + * dynamically customizable operations (for regular files only)
9023 + */
9024 +
9025 +#ifndef __AUFS_DYNOP_H__
9026 +#define __AUFS_DYNOP_H__
9027 +
9028 +#ifdef __KERNEL__
9029 +
9030 +#include "inode.h"
9031 +
9032 +enum {AuDy_AOP, AuDyLast};
9033 +
9034 +struct au_dynop {
9035 +       int                                             dy_type;
9036 +       union {
9037 +               const void                              *dy_hop;
9038 +               const struct address_space_operations   *dy_haop;
9039 +       };
9040 +};
9041 +
9042 +struct au_dykey {
9043 +       union {
9044 +               struct list_head        dk_list;
9045 +               struct rcu_head         dk_rcu;
9046 +       };
9047 +       struct au_dynop         dk_op;
9048 +
9049 +       /*
9050 +        * during I am in the branch local array, kref is gotten. when the
9051 +        * branch is removed, kref is put.
9052 +        */
9053 +       struct kref             dk_kref;
9054 +};
9055 +
9056 +/* stop unioning since their sizes are very different from each other */
9057 +struct au_dyaop {
9058 +       struct au_dykey                 da_key;
9059 +       struct address_space_operations da_op; /* not const */
9060 +       int (*da_get_xip_mem)(struct address_space *, pgoff_t, int,
9061 +                             void **, unsigned long *);
9062 +};
9063 +
9064 +/* ---------------------------------------------------------------------- */
9065 +
9066 +/* dynop.c */
9067 +struct au_branch;
9068 +void au_dy_put(struct au_dykey *key);
9069 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9070 +               struct inode *h_inode);
9071 +int au_dy_irefresh(struct inode *inode);
9072 +void au_dy_arefresh(int do_dio);
9073 +
9074 +void __init au_dy_init(void);
9075 +void au_dy_fin(void);
9076 +
9077 +#endif /* __KERNEL__ */
9078 +#endif /* __AUFS_DYNOP_H__ */
9079 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
9080 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
9081 +++ linux/fs/aufs/export.c      2012-03-19 12:24:13.679801090 +0100
9082 @@ -0,0 +1,803 @@
9083 +/*
9084 + * Copyright (C) 2005-2012 Junjiro R. Okajima
9085 + *
9086 + * This program, aufs is free software; you can redistribute it and/or modify
9087 + * it under the terms of the GNU General Public License as published by
9088 + * the Free Software Foundation; either version 2 of the License, or
9089 + * (at your option) any later version.
9090 + *
9091 + * This program is distributed in the hope that it will be useful,
9092 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9093 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9094 + * GNU General Public License for more details.
9095 + *
9096 + * You should have received a copy of the GNU General Public License
9097 + * along with this program; if not, write to the Free Software
9098 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9099 + */
9100 +
9101 +/*
9102 + * export via nfs
9103 + */
9104 +
9105 +#include <linux/exportfs.h>
9106 +#include <linux/fs_struct.h>
9107 +#include <linux/namei.h>
9108 +#include <linux/nsproxy.h>
9109 +#include <linux/random.h>
9110 +#include <linux/writeback.h>
9111 +#include "../fs/mount.h"
9112 +#include "aufs.h"
9113 +
9114 +union conv {
9115 +#ifdef CONFIG_AUFS_INO_T_64
9116 +       __u32 a[2];
9117 +#else
9118 +       __u32 a[1];
9119 +#endif
9120 +       ino_t ino;
9121 +};
9122 +
9123 +static ino_t decode_ino(__u32 *a)
9124 +{
9125 +       union conv u;
9126 +
9127 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
9128 +       u.a[0] = a[0];
9129 +#ifdef CONFIG_AUFS_INO_T_64
9130 +       u.a[1] = a[1];
9131 +#endif
9132 +       return u.ino;
9133 +}
9134 +
9135 +static void encode_ino(__u32 *a, ino_t ino)
9136 +{
9137 +       union conv u;
9138 +
9139 +       u.ino = ino;
9140 +       a[0] = u.a[0];
9141 +#ifdef CONFIG_AUFS_INO_T_64
9142 +       a[1] = u.a[1];
9143 +#endif
9144 +}
9145 +
9146 +/* NFS file handle */
9147 +enum {
9148 +       Fh_br_id,
9149 +       Fh_sigen,
9150 +#ifdef CONFIG_AUFS_INO_T_64
9151 +       /* support 64bit inode number */
9152 +       Fh_ino1,
9153 +       Fh_ino2,
9154 +       Fh_dir_ino1,
9155 +       Fh_dir_ino2,
9156 +#else
9157 +       Fh_ino1,
9158 +       Fh_dir_ino1,
9159 +#endif
9160 +       Fh_igen,
9161 +       Fh_h_type,
9162 +       Fh_tail,
9163 +
9164 +       Fh_ino = Fh_ino1,
9165 +       Fh_dir_ino = Fh_dir_ino1
9166 +};
9167 +
9168 +static int au_test_anon(struct dentry *dentry)
9169 +{
9170 +       /* note: read d_flags without d_lock */
9171 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
9172 +}
9173 +
9174 +/* ---------------------------------------------------------------------- */
9175 +/* inode generation external table */
9176 +
9177 +void au_xigen_inc(struct inode *inode)
9178 +{
9179 +       loff_t pos;
9180 +       ssize_t sz;
9181 +       __u32 igen;
9182 +       struct super_block *sb;
9183 +       struct au_sbinfo *sbinfo;
9184 +
9185 +       sb = inode->i_sb;
9186 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
9187 +
9188 +       sbinfo = au_sbi(sb);
9189 +       pos = inode->i_ino;
9190 +       pos *= sizeof(igen);
9191 +       igen = inode->i_generation + 1;
9192 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
9193 +                        sizeof(igen), &pos);
9194 +       if (sz == sizeof(igen))
9195 +               return; /* success */
9196 +
9197 +       if (unlikely(sz >= 0))
9198 +               AuIOErr("xigen error (%zd)\n", sz);
9199 +}
9200 +
9201 +int au_xigen_new(struct inode *inode)
9202 +{
9203 +       int err;
9204 +       loff_t pos;
9205 +       ssize_t sz;
9206 +       struct super_block *sb;
9207 +       struct au_sbinfo *sbinfo;
9208 +       struct file *file;
9209 +
9210 +       err = 0;
9211 +       /* todo: dirty, at mount time */
9212 +       if (inode->i_ino == AUFS_ROOT_INO)
9213 +               goto out;
9214 +       sb = inode->i_sb;
9215 +       SiMustAnyLock(sb);
9216 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9217 +               goto out;
9218 +
9219 +       err = -EFBIG;
9220 +       pos = inode->i_ino;
9221 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
9222 +               AuIOErr1("too large i%lld\n", pos);
9223 +               goto out;
9224 +       }
9225 +       pos *= sizeof(inode->i_generation);
9226 +
9227 +       err = 0;
9228 +       sbinfo = au_sbi(sb);
9229 +       file = sbinfo->si_xigen;
9230 +       BUG_ON(!file);
9231 +
9232 +       if (i_size_read(file->f_dentry->d_inode)
9233 +           < pos + sizeof(inode->i_generation)) {
9234 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
9235 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
9236 +                                sizeof(inode->i_generation), &pos);
9237 +       } else
9238 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
9239 +                               sizeof(inode->i_generation), &pos);
9240 +       if (sz == sizeof(inode->i_generation))
9241 +               goto out; /* success */
9242 +
9243 +       err = sz;
9244 +       if (unlikely(sz >= 0)) {
9245 +               err = -EIO;
9246 +               AuIOErr("xigen error (%zd)\n", sz);
9247 +       }
9248 +
9249 +out:
9250 +       return err;
9251 +}
9252 +
9253 +int au_xigen_set(struct super_block *sb, struct file *base)
9254 +{
9255 +       int err;
9256 +       struct au_sbinfo *sbinfo;
9257 +       struct file *file;
9258 +
9259 +       SiMustWriteLock(sb);
9260 +
9261 +       sbinfo = au_sbi(sb);
9262 +       file = au_xino_create2(base, sbinfo->si_xigen);
9263 +       err = PTR_ERR(file);
9264 +       if (IS_ERR(file))
9265 +               goto out;
9266 +       err = 0;
9267 +       if (sbinfo->si_xigen)
9268 +               fput(sbinfo->si_xigen);
9269 +       sbinfo->si_xigen = file;
9270 +
9271 +out:
9272 +       return err;
9273 +}
9274 +
9275 +void au_xigen_clr(struct super_block *sb)
9276 +{
9277 +       struct au_sbinfo *sbinfo;
9278 +
9279 +       SiMustWriteLock(sb);
9280 +
9281 +       sbinfo = au_sbi(sb);
9282 +       if (sbinfo->si_xigen) {
9283 +               fput(sbinfo->si_xigen);
9284 +               sbinfo->si_xigen = NULL;
9285 +       }
9286 +}
9287 +
9288 +/* ---------------------------------------------------------------------- */
9289 +
9290 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
9291 +                                   ino_t dir_ino)
9292 +{
9293 +       struct dentry *dentry, *d;
9294 +       struct inode *inode;
9295 +       unsigned int sigen;
9296 +
9297 +       dentry = NULL;
9298 +       inode = ilookup(sb, ino);
9299 +       if (!inode)
9300 +               goto out;
9301 +
9302 +       dentry = ERR_PTR(-ESTALE);
9303 +       sigen = au_sigen(sb);
9304 +       if (unlikely(is_bad_inode(inode)
9305 +                    || IS_DEADDIR(inode)
9306 +                    || sigen != au_iigen(inode)))
9307 +               goto out_iput;
9308 +
9309 +       dentry = NULL;
9310 +       if (!dir_ino || S_ISDIR(inode->i_mode))
9311 +               dentry = d_find_alias(inode);
9312 +       else {
9313 +               spin_lock(&inode->i_lock);
9314 +               list_for_each_entry(d, &inode->i_dentry, d_alias) {
9315 +                       spin_lock(&d->d_lock);
9316 +                       if (!au_test_anon(d)
9317 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
9318 +                               dentry = dget_dlock(d);
9319 +                               spin_unlock(&d->d_lock);
9320 +                               break;
9321 +                       }
9322 +                       spin_unlock(&d->d_lock);
9323 +               }
9324 +               spin_unlock(&inode->i_lock);
9325 +       }
9326 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
9327 +               /* need to refresh */
9328 +               dput(dentry);
9329 +               dentry = NULL;
9330 +       }
9331 +
9332 +out_iput:
9333 +       iput(inode);
9334 +out:
9335 +       AuTraceErrPtr(dentry);
9336 +       return dentry;
9337 +}
9338 +
9339 +/* ---------------------------------------------------------------------- */
9340 +
9341 +/* todo: dirty? */
9342 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
9343 +
9344 +struct au_compare_mnt_args {
9345 +       /* input */
9346 +       struct super_block *sb;
9347 +
9348 +       /* output */
9349 +       struct vfsmount *mnt;
9350 +};
9351 +
9352 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
9353 +{
9354 +       struct au_compare_mnt_args *a = arg;
9355 +
9356 +       if (mnt->mnt_sb != a->sb)
9357 +               return 0;
9358 +       a->mnt = mntget(mnt);
9359 +       return 1;
9360 +}
9361 +
9362 +static struct vfsmount *au_mnt_get(struct super_block *sb)
9363 +{
9364 +       int err;
9365 +       struct path root;
9366 +       struct au_compare_mnt_args args = {
9367 +               .sb = sb
9368 +       };
9369 +
9370 +       get_fs_root(current->fs, &root);
9371 +       br_read_lock(vfsmount_lock);
9372 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
9373 +       br_read_unlock(vfsmount_lock);
9374 +       path_put(&root);
9375 +       AuDebugOn(!err);
9376 +       AuDebugOn(!args.mnt);
9377 +       return args.mnt;
9378 +}
9379 +
9380 +struct au_nfsd_si_lock {
9381 +       unsigned int sigen;
9382 +       aufs_bindex_t bindex, br_id;
9383 +       unsigned char force_lock;
9384 +};
9385 +
9386 +static int si_nfsd_read_lock(struct super_block *sb,
9387 +                            struct au_nfsd_si_lock *nsi_lock)
9388 +{
9389 +       int err;
9390 +       aufs_bindex_t bindex;
9391 +
9392 +       si_read_lock(sb, AuLock_FLUSH);
9393 +
9394 +       /* branch id may be wrapped around */
9395 +       err = 0;
9396 +       bindex = au_br_index(sb, nsi_lock->br_id);
9397 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
9398 +               goto out; /* success */
9399 +
9400 +       err = -ESTALE;
9401 +       bindex = -1;
9402 +       if (!nsi_lock->force_lock)
9403 +               si_read_unlock(sb);
9404 +
9405 +out:
9406 +       nsi_lock->bindex = bindex;
9407 +       return err;
9408 +}
9409 +
9410 +struct find_name_by_ino {
9411 +       int called, found;
9412 +       ino_t ino;
9413 +       char *name;
9414 +       int namelen;
9415 +};
9416 +
9417 +static int
9418 +find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
9419 +                u64 ino, unsigned int d_type)
9420 +{
9421 +       struct find_name_by_ino *a = arg;
9422 +
9423 +       a->called++;
9424 +       if (a->ino != ino)
9425 +               return 0;
9426 +
9427 +       memcpy(a->name, name, namelen);
9428 +       a->namelen = namelen;
9429 +       a->found = 1;
9430 +       return 1;
9431 +}
9432 +
9433 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
9434 +                                    struct au_nfsd_si_lock *nsi_lock)
9435 +{
9436 +       struct dentry *dentry, *parent;
9437 +       struct file *file;
9438 +       struct inode *dir;
9439 +       struct find_name_by_ino arg;
9440 +       int err;
9441 +
9442 +       parent = path->dentry;
9443 +       if (nsi_lock)
9444 +               si_read_unlock(parent->d_sb);
9445 +       file = vfsub_dentry_open(path, au_dir_roflags);
9446 +       dentry = (void *)file;
9447 +       if (IS_ERR(file))
9448 +               goto out;
9449 +
9450 +       dentry = ERR_PTR(-ENOMEM);
9451 +       arg.name = __getname_gfp(GFP_NOFS);
9452 +       if (unlikely(!arg.name))
9453 +               goto out_file;
9454 +       arg.ino = ino;
9455 +       arg.found = 0;
9456 +       do {
9457 +               arg.called = 0;
9458 +               /* smp_mb(); */
9459 +               err = vfsub_readdir(file, find_name_by_ino, &arg);
9460 +       } while (!err && !arg.found && arg.called);
9461 +       dentry = ERR_PTR(err);
9462 +       if (unlikely(err))
9463 +               goto out_name;
9464 +       dentry = ERR_PTR(-ENOENT);
9465 +       if (!arg.found)
9466 +               goto out_name;
9467 +
9468 +       /* do not call au_lkup_one() */
9469 +       dir = parent->d_inode;
9470 +       mutex_lock(&dir->i_mutex);
9471 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
9472 +       mutex_unlock(&dir->i_mutex);
9473 +       AuTraceErrPtr(dentry);
9474 +       if (IS_ERR(dentry))
9475 +               goto out_name;
9476 +       AuDebugOn(au_test_anon(dentry));
9477 +       if (unlikely(!dentry->d_inode)) {
9478 +               dput(dentry);
9479 +               dentry = ERR_PTR(-ENOENT);
9480 +       }
9481 +
9482 +out_name:
9483 +       __putname(arg.name);
9484 +out_file:
9485 +       fput(file);
9486 +out:
9487 +       if (unlikely(nsi_lock
9488 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
9489 +               if (!IS_ERR(dentry)) {
9490 +                       dput(dentry);
9491 +                       dentry = ERR_PTR(-ESTALE);
9492 +               }
9493 +       AuTraceErrPtr(dentry);
9494 +       return dentry;
9495 +}
9496 +
9497 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
9498 +                                       ino_t dir_ino,
9499 +                                       struct au_nfsd_si_lock *nsi_lock)
9500 +{
9501 +       struct dentry *dentry;
9502 +       struct path path;
9503 +
9504 +       if (dir_ino != AUFS_ROOT_INO) {
9505 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
9506 +               dentry = path.dentry;
9507 +               if (!path.dentry || IS_ERR(path.dentry))
9508 +                       goto out;
9509 +               AuDebugOn(au_test_anon(path.dentry));
9510 +       } else
9511 +               path.dentry = dget(sb->s_root);
9512 +
9513 +       path.mnt = au_mnt_get(sb);
9514 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
9515 +       path_put(&path);
9516 +
9517 +out:
9518 +       AuTraceErrPtr(dentry);
9519 +       return dentry;
9520 +}
9521 +
9522 +/* ---------------------------------------------------------------------- */
9523 +
9524 +static int h_acceptable(void *expv, struct dentry *dentry)
9525 +{
9526 +       return 1;
9527 +}
9528 +
9529 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
9530 +                          char *buf, int len, struct super_block *sb)
9531 +{
9532 +       char *p;
9533 +       int n;
9534 +       struct path path;
9535 +
9536 +       p = d_path(h_rootpath, buf, len);
9537 +       if (IS_ERR(p))
9538 +               goto out;
9539 +       n = strlen(p);
9540 +
9541 +       path.mnt = h_rootpath->mnt;
9542 +       path.dentry = h_parent;
9543 +       p = d_path(&path, buf, len);
9544 +       if (IS_ERR(p))
9545 +               goto out;
9546 +       if (n != 1)
9547 +               p += n;
9548 +
9549 +       path.mnt = au_mnt_get(sb);
9550 +       path.dentry = sb->s_root;
9551 +       p = d_path(&path, buf, len - strlen(p));
9552 +       mntput(path.mnt);
9553 +       if (IS_ERR(p))
9554 +               goto out;
9555 +       if (n != 1)
9556 +               p[strlen(p)] = '/';
9557 +
9558 +out:
9559 +       AuTraceErrPtr(p);
9560 +       return p;
9561 +}
9562 +
9563 +static
9564 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
9565 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
9566 +{
9567 +       struct dentry *dentry, *h_parent, *root;
9568 +       struct super_block *h_sb;
9569 +       char *pathname, *p;
9570 +       struct vfsmount *h_mnt;
9571 +       struct au_branch *br;
9572 +       int err;
9573 +       struct path path;
9574 +
9575 +       br = au_sbr(sb, nsi_lock->bindex);
9576 +       h_mnt = br->br_mnt;
9577 +       h_sb = h_mnt->mnt_sb;
9578 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
9579 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
9580 +                                     fh_len - Fh_tail, fh[Fh_h_type],
9581 +                                     h_acceptable, /*context*/NULL);
9582 +       dentry = h_parent;
9583 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
9584 +               AuWarn1("%s decode_fh failed, %ld\n",
9585 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
9586 +               goto out;
9587 +       }
9588 +       dentry = NULL;
9589 +       if (unlikely(au_test_anon(h_parent))) {
9590 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
9591 +                       au_sbtype(h_sb));
9592 +               goto out_h_parent;
9593 +       }
9594 +
9595 +       dentry = ERR_PTR(-ENOMEM);
9596 +       pathname = (void *)__get_free_page(GFP_NOFS);
9597 +       if (unlikely(!pathname))
9598 +               goto out_h_parent;
9599 +
9600 +       root = sb->s_root;
9601 +       path.mnt = h_mnt;
9602 +       di_read_lock_parent(root, !AuLock_IR);
9603 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
9604 +       di_read_unlock(root, !AuLock_IR);
9605 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
9606 +       dentry = (void *)p;
9607 +       if (IS_ERR(p))
9608 +               goto out_pathname;
9609 +
9610 +       si_read_unlock(sb);
9611 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
9612 +       dentry = ERR_PTR(err);
9613 +       if (unlikely(err))
9614 +               goto out_relock;
9615 +
9616 +       dentry = ERR_PTR(-ENOENT);
9617 +       AuDebugOn(au_test_anon(path.dentry));
9618 +       if (unlikely(!path.dentry->d_inode))
9619 +               goto out_path;
9620 +
9621 +       if (ino != path.dentry->d_inode->i_ino)
9622 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
9623 +       else
9624 +               dentry = dget(path.dentry);
9625 +
9626 +out_path:
9627 +       path_put(&path);
9628 +out_relock:
9629 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
9630 +               if (!IS_ERR(dentry)) {
9631 +                       dput(dentry);
9632 +                       dentry = ERR_PTR(-ESTALE);
9633 +               }
9634 +out_pathname:
9635 +       free_page((unsigned long)pathname);
9636 +out_h_parent:
9637 +       dput(h_parent);
9638 +out:
9639 +       AuTraceErrPtr(dentry);
9640 +       return dentry;
9641 +}
9642 +
9643 +/* ---------------------------------------------------------------------- */
9644 +
9645 +static struct dentry *
9646 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
9647 +                 int fh_type)
9648 +{
9649 +       struct dentry *dentry;
9650 +       __u32 *fh = fid->raw;
9651 +       struct au_branch *br;
9652 +       ino_t ino, dir_ino;
9653 +       struct au_nfsd_si_lock nsi_lock = {
9654 +               .force_lock     = 0
9655 +       };
9656 +
9657 +       dentry = ERR_PTR(-ESTALE);
9658 +       /* it should never happen, but the file handle is unreliable */
9659 +       if (unlikely(fh_len < Fh_tail))
9660 +               goto out;
9661 +       nsi_lock.sigen = fh[Fh_sigen];
9662 +       nsi_lock.br_id = fh[Fh_br_id];
9663 +
9664 +       /* branch id may be wrapped around */
9665 +       br = NULL;
9666 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
9667 +               goto out;
9668 +       nsi_lock.force_lock = 1;
9669 +
9670 +       /* is this inode still cached? */
9671 +       ino = decode_ino(fh + Fh_ino);
9672 +       /* it should never happen */
9673 +       if (unlikely(ino == AUFS_ROOT_INO))
9674 +               goto out;
9675 +
9676 +       dir_ino = decode_ino(fh + Fh_dir_ino);
9677 +       dentry = decode_by_ino(sb, ino, dir_ino);
9678 +       if (IS_ERR(dentry))
9679 +               goto out_unlock;
9680 +       if (dentry)
9681 +               goto accept;
9682 +
9683 +       /* is the parent dir cached? */
9684 +       br = au_sbr(sb, nsi_lock.bindex);
9685 +       atomic_inc(&br->br_count);
9686 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
9687 +       if (IS_ERR(dentry))
9688 +               goto out_unlock;
9689 +       if (dentry)
9690 +               goto accept;
9691 +
9692 +       /* lookup path */
9693 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
9694 +       if (IS_ERR(dentry))
9695 +               goto out_unlock;
9696 +       if (unlikely(!dentry))
9697 +               /* todo?: make it ESTALE */
9698 +               goto out_unlock;
9699 +
9700 +accept:
9701 +       if (!au_digen_test(dentry, au_sigen(sb))
9702 +           && dentry->d_inode->i_generation == fh[Fh_igen])
9703 +               goto out_unlock; /* success */
9704 +
9705 +       dput(dentry);
9706 +       dentry = ERR_PTR(-ESTALE);
9707 +out_unlock:
9708 +       if (br)
9709 +               atomic_dec(&br->br_count);
9710 +       si_read_unlock(sb);
9711 +out:
9712 +       AuTraceErrPtr(dentry);
9713 +       return dentry;
9714 +}
9715 +
9716 +#if 0 /* reserved for future use */
9717 +/* support subtreecheck option */
9718 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
9719 +                                       int fh_len, int fh_type)
9720 +{
9721 +       struct dentry *parent;
9722 +       __u32 *fh = fid->raw;
9723 +       ino_t dir_ino;
9724 +
9725 +       dir_ino = decode_ino(fh + Fh_dir_ino);
9726 +       parent = decode_by_ino(sb, dir_ino, 0);
9727 +       if (IS_ERR(parent))
9728 +               goto out;
9729 +       if (!parent)
9730 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
9731 +                                       dir_ino, fh, fh_len);
9732 +
9733 +out:
9734 +       AuTraceErrPtr(parent);
9735 +       return parent;
9736 +}
9737 +#endif
9738 +
9739 +/* ---------------------------------------------------------------------- */
9740 +
9741 +static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
9742 +                         int connectable)
9743 +{
9744 +       int err;
9745 +       aufs_bindex_t bindex, bend;
9746 +       struct super_block *sb, *h_sb;
9747 +       struct inode *inode;
9748 +       struct dentry *parent, *h_parent;
9749 +       struct au_branch *br;
9750 +
9751 +       AuDebugOn(au_test_anon(dentry));
9752 +
9753 +       parent = NULL;
9754 +       err = -ENOSPC;
9755 +       if (unlikely(*max_len <= Fh_tail)) {
9756 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
9757 +               goto out;
9758 +       }
9759 +
9760 +       err = FILEID_ROOT;
9761 +       if (IS_ROOT(dentry)) {
9762 +               AuDebugOn(dentry->d_inode->i_ino != AUFS_ROOT_INO);
9763 +               goto out;
9764 +       }
9765 +
9766 +       h_parent = NULL;
9767 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_IR | AuLock_GEN);
9768 +       if (unlikely(err))
9769 +               goto out;
9770 +
9771 +       inode = dentry->d_inode;
9772 +       AuDebugOn(!inode);
9773 +       sb = dentry->d_sb;
9774 +#ifdef CONFIG_AUFS_DEBUG
9775 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9776 +               AuWarn1("NFS-exporting requires xino\n");
9777 +#endif
9778 +       err = -EIO;
9779 +       parent = dget_parent(dentry);
9780 +       di_read_lock_parent(parent, !AuLock_IR);
9781 +       bend = au_dbtaildir(parent);
9782 +       for (bindex = au_dbstart(parent); bindex <= bend; bindex++) {
9783 +               h_parent = au_h_dptr(parent, bindex);
9784 +               if (h_parent) {
9785 +                       dget(h_parent);
9786 +                       break;
9787 +               }
9788 +       }
9789 +       if (unlikely(!h_parent))
9790 +               goto out_unlock;
9791 +
9792 +       err = -EPERM;
9793 +       br = au_sbr(sb, bindex);
9794 +       h_sb = br->br_mnt->mnt_sb;
9795 +       if (unlikely(!h_sb->s_export_op)) {
9796 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
9797 +               goto out_dput;
9798 +       }
9799 +
9800 +       fh[Fh_br_id] = br->br_id;
9801 +       fh[Fh_sigen] = au_sigen(sb);
9802 +       encode_ino(fh + Fh_ino, inode->i_ino);
9803 +       encode_ino(fh + Fh_dir_ino, parent->d_inode->i_ino);
9804 +       fh[Fh_igen] = inode->i_generation;
9805 +
9806 +       *max_len -= Fh_tail;
9807 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
9808 +                                          max_len,
9809 +                                          /*connectable or subtreecheck*/0);
9810 +       err = fh[Fh_h_type];
9811 +       *max_len += Fh_tail;
9812 +       /* todo: macros? */
9813 +       if (err != 255)
9814 +               err = 99;
9815 +       else
9816 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
9817 +
9818 +out_dput:
9819 +       dput(h_parent);
9820 +out_unlock:
9821 +       di_read_unlock(parent, !AuLock_IR);
9822 +       dput(parent);
9823 +       aufs_read_unlock(dentry, AuLock_IR);
9824 +out:
9825 +       if (unlikely(err < 0))
9826 +               err = 255;
9827 +       return err;
9828 +}
9829 +
9830 +/* ---------------------------------------------------------------------- */
9831 +
9832 +static int aufs_commit_metadata(struct inode *inode)
9833 +{
9834 +       int err;
9835 +       aufs_bindex_t bindex;
9836 +       struct super_block *sb;
9837 +       struct inode *h_inode;
9838 +       int (*f)(struct inode *inode);
9839 +
9840 +       sb = inode->i_sb;
9841 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
9842 +       ii_write_lock_child(inode);
9843 +       bindex = au_ibstart(inode);
9844 +       AuDebugOn(bindex < 0);
9845 +       h_inode = au_h_iptr(inode, bindex);
9846 +
9847 +       f = h_inode->i_sb->s_export_op->commit_metadata;
9848 +       if (f)
9849 +               err = f(h_inode);
9850 +       else {
9851 +               struct writeback_control wbc = {
9852 +                       .sync_mode      = WB_SYNC_ALL,
9853 +                       .nr_to_write    = 0 /* metadata only */
9854 +               };
9855 +
9856 +               err = sync_inode(h_inode, &wbc);
9857 +       }
9858 +
9859 +       au_cpup_attr_timesizes(inode);
9860 +       ii_write_unlock(inode);
9861 +       si_read_unlock(sb);
9862 +       return err;
9863 +}
9864 +
9865 +/* ---------------------------------------------------------------------- */
9866 +
9867 +static struct export_operations aufs_export_op = {
9868 +       .fh_to_dentry           = aufs_fh_to_dentry,
9869 +       /* .fh_to_parent        = aufs_fh_to_parent, */
9870 +       .encode_fh              = aufs_encode_fh,
9871 +       .commit_metadata        = aufs_commit_metadata
9872 +};
9873 +
9874 +void au_export_init(struct super_block *sb)
9875 +{
9876 +       struct au_sbinfo *sbinfo;
9877 +       __u32 u;
9878 +
9879 +       sb->s_export_op = &aufs_export_op;
9880 +       sbinfo = au_sbi(sb);
9881 +       sbinfo->si_xigen = NULL;
9882 +       get_random_bytes(&u, sizeof(u));
9883 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
9884 +       atomic_set(&sbinfo->si_xigen_next, u);
9885 +}
9886 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
9887 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
9888 +++ linux/fs/aufs/file.c        2012-03-19 12:24:13.679801090 +0100
9889 @@ -0,0 +1,676 @@
9890 +/*
9891 + * Copyright (C) 2005-2012 Junjiro R. Okajima
9892 + *
9893 + * This program, aufs is free software; you can redistribute it and/or modify
9894 + * it under the terms of the GNU General Public License as published by
9895 + * the Free Software Foundation; either version 2 of the License, or
9896 + * (at your option) any later version.
9897 + *
9898 + * This program is distributed in the hope that it will be useful,
9899 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9900 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9901 + * GNU General Public License for more details.
9902 + *
9903 + * You should have received a copy of the GNU General Public License
9904 + * along with this program; if not, write to the Free Software
9905 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9906 + */
9907 +
9908 +/*
9909 + * handling file/dir, and address_space operation
9910 + */
9911 +
9912 +#ifdef CONFIG_AUFS_DEBUG
9913 +#include <linux/migrate.h>
9914 +#endif
9915 +#include <linux/pagemap.h>
9916 +#include "aufs.h"
9917 +
9918 +/* drop flags for writing */
9919 +unsigned int au_file_roflags(unsigned int flags)
9920 +{
9921 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
9922 +       flags |= O_RDONLY | O_NOATIME;
9923 +       return flags;
9924 +}
9925 +
9926 +/* common functions to regular file and dir */
9927 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
9928 +                      struct file *file)
9929 +{
9930 +       struct file *h_file;
9931 +       struct dentry *h_dentry;
9932 +       struct inode *h_inode;
9933 +       struct super_block *sb;
9934 +       struct au_branch *br;
9935 +       struct path h_path;
9936 +       int err, exec_flag;
9937 +
9938 +       /* a race condition can happen between open and unlink/rmdir */
9939 +       h_file = ERR_PTR(-ENOENT);
9940 +       h_dentry = au_h_dptr(dentry, bindex);
9941 +       if (au_test_nfsd() && !h_dentry)
9942 +               goto out;
9943 +       h_inode = h_dentry->d_inode;
9944 +       if (au_test_nfsd() && !h_inode)
9945 +               goto out;
9946 +       spin_lock(&h_dentry->d_lock);
9947 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
9948 +               || !h_inode
9949 +               /* || !dentry->d_inode->i_nlink */
9950 +               ;
9951 +       spin_unlock(&h_dentry->d_lock);
9952 +       if (unlikely(err))
9953 +               goto out;
9954 +
9955 +       sb = dentry->d_sb;
9956 +       br = au_sbr(sb, bindex);
9957 +       h_file = ERR_PTR(-EACCES);
9958 +       exec_flag = flags & __FMODE_EXEC;
9959 +       if (exec_flag && (br->br_mnt->mnt_flags & MNT_NOEXEC))
9960 +               goto out;
9961 +
9962 +       /* drop flags for writing */
9963 +       if (au_test_ro(sb, bindex, dentry->d_inode))
9964 +               flags = au_file_roflags(flags);
9965 +       flags &= ~O_CREAT;
9966 +       atomic_inc(&br->br_count);
9967 +       h_path.dentry = h_dentry;
9968 +       h_path.mnt = br->br_mnt;
9969 +       if (!au_special_file(h_inode->i_mode))
9970 +               h_file = vfsub_dentry_open(&h_path, flags);
9971 +       else {
9972 +               /* this block depends upon the configuration */
9973 +               di_read_unlock(dentry, AuLock_IR);
9974 +               fi_write_unlock(file);
9975 +               si_read_unlock(sb);
9976 +               h_file = vfsub_dentry_open(&h_path, flags);
9977 +               si_noflush_read_lock(sb);
9978 +               fi_write_lock(file);
9979 +               di_read_lock_child(dentry, AuLock_IR);
9980 +       }
9981 +       if (IS_ERR(h_file))
9982 +               goto out_br;
9983 +
9984 +       if (exec_flag) {
9985 +               err = deny_write_access(h_file);
9986 +               if (unlikely(err)) {
9987 +                       fput(h_file);
9988 +                       h_file = ERR_PTR(err);
9989 +                       goto out_br;
9990 +               }
9991 +       }
9992 +       fsnotify_open(h_file);
9993 +       goto out; /* success */
9994 +
9995 +out_br:
9996 +       atomic_dec(&br->br_count);
9997 +out:
9998 +       return h_file;
9999 +}
10000 +
10001 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10002 +              struct au_fidir *fidir)
10003 +{
10004 +       int err;
10005 +       struct dentry *dentry;
10006 +
10007 +       err = au_finfo_init(file, fidir);
10008 +       if (unlikely(err))
10009 +               goto out;
10010 +
10011 +       dentry = file->f_dentry;
10012 +       di_read_lock_child(dentry, AuLock_IR);
10013 +       err = open(file, vfsub_file_flags(file));
10014 +       di_read_unlock(dentry, AuLock_IR);
10015 +
10016 +       fi_write_unlock(file);
10017 +       if (unlikely(err)) {
10018 +               au_fi(file)->fi_hdir = NULL;
10019 +               au_finfo_fin(file);
10020 +       }
10021 +
10022 +out:
10023 +       return err;
10024 +}
10025 +
10026 +int au_reopen_nondir(struct file *file)
10027 +{
10028 +       int err;
10029 +       aufs_bindex_t bstart;
10030 +       struct dentry *dentry;
10031 +       struct file *h_file, *h_file_tmp;
10032 +
10033 +       dentry = file->f_dentry;
10034 +       AuDebugOn(au_special_file(dentry->d_inode->i_mode));
10035 +       bstart = au_dbstart(dentry);
10036 +       h_file_tmp = NULL;
10037 +       if (au_fbstart(file) == bstart) {
10038 +               h_file = au_hf_top(file);
10039 +               if (file->f_mode == h_file->f_mode)
10040 +                       return 0; /* success */
10041 +               h_file_tmp = h_file;
10042 +               get_file(h_file_tmp);
10043 +               au_set_h_fptr(file, bstart, NULL);
10044 +       }
10045 +       AuDebugOn(au_fi(file)->fi_hdir);
10046 +       AuDebugOn(au_fbstart(file) < bstart);
10047 +
10048 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
10049 +                          file);
10050 +       err = PTR_ERR(h_file);
10051 +       if (IS_ERR(h_file))
10052 +               goto out; /* todo: close all? */
10053 +
10054 +       err = 0;
10055 +       au_set_fbstart(file, bstart);
10056 +       au_set_h_fptr(file, bstart, h_file);
10057 +       au_update_figen(file);
10058 +       /* todo: necessary? */
10059 +       /* file->f_ra = h_file->f_ra; */
10060 +
10061 +out:
10062 +       if (h_file_tmp)
10063 +               fput(h_file_tmp);
10064 +       return err;
10065 +}
10066 +
10067 +/* ---------------------------------------------------------------------- */
10068 +
10069 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
10070 +                       struct dentry *hi_wh)
10071 +{
10072 +       int err;
10073 +       aufs_bindex_t bstart;
10074 +       struct au_dinfo *dinfo;
10075 +       struct dentry *h_dentry;
10076 +       struct au_hdentry *hdp;
10077 +
10078 +       dinfo = au_di(file->f_dentry);
10079 +       AuRwMustWriteLock(&dinfo->di_rwsem);
10080 +
10081 +       bstart = dinfo->di_bstart;
10082 +       dinfo->di_bstart = btgt;
10083 +       hdp = dinfo->di_hdentry;
10084 +       h_dentry = hdp[0 + btgt].hd_dentry;
10085 +       hdp[0 + btgt].hd_dentry = hi_wh;
10086 +       err = au_reopen_nondir(file);
10087 +       hdp[0 + btgt].hd_dentry = h_dentry;
10088 +       dinfo->di_bstart = bstart;
10089 +
10090 +       return err;
10091 +}
10092 +
10093 +static int au_ready_to_write_wh(struct file *file, loff_t len,
10094 +                               aufs_bindex_t bcpup)
10095 +{
10096 +       int err;
10097 +       struct inode *inode, *h_inode;
10098 +       struct dentry *dentry, *h_dentry, *hi_wh;
10099 +
10100 +       dentry = file->f_dentry;
10101 +       au_update_dbstart(dentry);
10102 +       inode = dentry->d_inode;
10103 +       h_inode = NULL;
10104 +       if (au_dbstart(dentry) <= bcpup && au_dbend(dentry) >= bcpup) {
10105 +               h_dentry = au_h_dptr(dentry, bcpup);
10106 +               if (h_dentry)
10107 +                       h_inode = h_dentry->d_inode;
10108 +       }
10109 +       hi_wh = au_hi_wh(inode, bcpup);
10110 +       if (!hi_wh && !h_inode)
10111 +               err = au_sio_cpup_wh(dentry, bcpup, len, file);
10112 +       else
10113 +               /* already copied-up after unlink */
10114 +               err = au_reopen_wh(file, bcpup, hi_wh);
10115 +
10116 +       if (!err
10117 +           && inode->i_nlink > 1
10118 +           && au_opt_test(au_mntflags(dentry->d_sb), PLINK))
10119 +               au_plink_append(inode, bcpup, au_h_dptr(dentry, bcpup));
10120 +
10121 +       return err;
10122 +}
10123 +
10124 +/*
10125 + * prepare the @file for writing.
10126 + */
10127 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
10128 +{
10129 +       int err;
10130 +       aufs_bindex_t bstart, bcpup, dbstart;
10131 +       struct dentry *dentry, *parent, *h_dentry;
10132 +       struct inode *h_inode, *inode;
10133 +       struct super_block *sb;
10134 +       struct file *h_file;
10135 +
10136 +       dentry = file->f_dentry;
10137 +       sb = dentry->d_sb;
10138 +       inode = dentry->d_inode;
10139 +       AuDebugOn(au_special_file(inode->i_mode));
10140 +       bstart = au_fbstart(file);
10141 +       err = au_test_ro(sb, bstart, inode);
10142 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
10143 +               err = au_pin(pin, dentry, bstart, AuOpt_UDBA_NONE, /*flags*/0);
10144 +               goto out;
10145 +       }
10146 +
10147 +       /* need to cpup or reopen */
10148 +       parent = dget_parent(dentry);
10149 +       di_write_lock_parent(parent);
10150 +       err = AuWbrCopyup(au_sbi(sb), dentry);
10151 +       bcpup = err;
10152 +       if (unlikely(err < 0))
10153 +               goto out_dgrade;
10154 +       err = 0;
10155 +
10156 +       if (!d_unhashed(dentry) && !au_h_dptr(parent, bcpup)) {
10157 +               err = au_cpup_dirs(dentry, bcpup);
10158 +               if (unlikely(err))
10159 +                       goto out_dgrade;
10160 +       }
10161 +
10162 +       err = au_pin(pin, dentry, bcpup, AuOpt_UDBA_NONE,
10163 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10164 +       if (unlikely(err))
10165 +               goto out_dgrade;
10166 +
10167 +       h_dentry = au_hf_top(file)->f_dentry;
10168 +       h_inode = h_dentry->d_inode;
10169 +       dbstart = au_dbstart(dentry);
10170 +       if (dbstart <= bcpup) {
10171 +               h_dentry = au_h_dptr(dentry, bcpup);
10172 +               AuDebugOn(!h_dentry);
10173 +               h_inode = h_dentry->d_inode;
10174 +               AuDebugOn(!h_inode);
10175 +               bstart = bcpup;
10176 +       }
10177 +
10178 +       if (dbstart <= bcpup            /* just reopen */
10179 +           || !d_unhashed(dentry)      /* copyup and reopen */
10180 +               ) {
10181 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
10182 +               h_file = au_h_open_pre(dentry, bstart);
10183 +               if (IS_ERR(h_file)) {
10184 +                       err = PTR_ERR(h_file);
10185 +                       h_file = NULL;
10186 +               } else {
10187 +                       di_downgrade_lock(parent, AuLock_IR);
10188 +                       if (dbstart > bcpup)
10189 +                               err = au_sio_cpup_simple(dentry, bcpup, len,
10190 +                                                        AuCpup_DTIME);
10191 +                       if (!err)
10192 +                               err = au_reopen_nondir(file);
10193 +               }
10194 +               mutex_unlock(&h_inode->i_mutex);
10195 +               au_h_open_post(dentry, bstart, h_file);
10196 +       } else {                        /* copyup as wh and reopen */
10197 +               /*
10198 +                * since writable hfsplus branch is not supported,
10199 +                * h_open_pre/post() are unnecessary.
10200 +                */
10201 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
10202 +               err = au_ready_to_write_wh(file, len, bcpup);
10203 +               di_downgrade_lock(parent, AuLock_IR);
10204 +               mutex_unlock(&h_inode->i_mutex);
10205 +       }
10206 +
10207 +       if (!err) {
10208 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
10209 +               goto out_dput; /* success */
10210 +       }
10211 +       au_unpin(pin);
10212 +       goto out_unlock;
10213 +
10214 +out_dgrade:
10215 +       di_downgrade_lock(parent, AuLock_IR);
10216 +out_unlock:
10217 +       di_read_unlock(parent, AuLock_IR);
10218 +out_dput:
10219 +       dput(parent);
10220 +out:
10221 +       return err;
10222 +}
10223 +
10224 +/* ---------------------------------------------------------------------- */
10225 +
10226 +int au_do_flush(struct file *file, fl_owner_t id,
10227 +               int (*flush)(struct file *file, fl_owner_t id))
10228 +{
10229 +       int err;
10230 +       struct dentry *dentry;
10231 +       struct super_block *sb;
10232 +       struct inode *inode;
10233 +
10234 +       dentry = file->f_dentry;
10235 +       sb = dentry->d_sb;
10236 +       inode = dentry->d_inode;
10237 +       si_noflush_read_lock(sb);
10238 +       fi_read_lock(file);
10239 +       ii_read_lock_child(inode);
10240 +
10241 +       err = flush(file, id);
10242 +       au_cpup_attr_timesizes(inode);
10243 +
10244 +       ii_read_unlock(inode);
10245 +       fi_read_unlock(file);
10246 +       si_read_unlock(sb);
10247 +       return err;
10248 +}
10249 +
10250 +/* ---------------------------------------------------------------------- */
10251 +
10252 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
10253 +{
10254 +       int err;
10255 +       aufs_bindex_t bstart;
10256 +       struct au_pin pin;
10257 +       struct au_finfo *finfo;
10258 +       struct dentry *dentry, *parent, *hi_wh;
10259 +       struct inode *inode;
10260 +       struct super_block *sb;
10261 +
10262 +       FiMustWriteLock(file);
10263 +
10264 +       err = 0;
10265 +       finfo = au_fi(file);
10266 +       dentry = file->f_dentry;
10267 +       sb = dentry->d_sb;
10268 +       inode = dentry->d_inode;
10269 +       bstart = au_ibstart(inode);
10270 +       if (bstart == finfo->fi_btop || IS_ROOT(dentry))
10271 +               goto out;
10272 +
10273 +       parent = dget_parent(dentry);
10274 +       if (au_test_ro(sb, bstart, inode)) {
10275 +               di_read_lock_parent(parent, !AuLock_IR);
10276 +               err = AuWbrCopyup(au_sbi(sb), dentry);
10277 +               bstart = err;
10278 +               di_read_unlock(parent, !AuLock_IR);
10279 +               if (unlikely(err < 0))
10280 +                       goto out_parent;
10281 +               err = 0;
10282 +       }
10283 +
10284 +       di_read_lock_parent(parent, AuLock_IR);
10285 +       hi_wh = au_hi_wh(inode, bstart);
10286 +       if (!S_ISDIR(inode->i_mode)
10287 +           && au_opt_test(au_mntflags(sb), PLINK)
10288 +           && au_plink_test(inode)
10289 +           && !d_unhashed(dentry)) {
10290 +               err = au_test_and_cpup_dirs(dentry, bstart);
10291 +               if (unlikely(err))
10292 +                       goto out_unlock;
10293 +
10294 +               /* always superio. */
10295 +               err = au_pin(&pin, dentry, bstart, AuOpt_UDBA_NONE,
10296 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10297 +               if (!err)
10298 +                       err = au_sio_cpup_simple(dentry, bstart, -1,
10299 +                                                AuCpup_DTIME);
10300 +               au_unpin(&pin);
10301 +       } else if (hi_wh) {
10302 +               /* already copied-up after unlink */
10303 +               err = au_reopen_wh(file, bstart, hi_wh);
10304 +               *need_reopen = 0;
10305 +       }
10306 +
10307 +out_unlock:
10308 +       di_read_unlock(parent, AuLock_IR);
10309 +out_parent:
10310 +       dput(parent);
10311 +out:
10312 +       return err;
10313 +}
10314 +
10315 +static void au_do_refresh_dir(struct file *file)
10316 +{
10317 +       aufs_bindex_t bindex, bend, new_bindex, brid;
10318 +       struct au_hfile *p, tmp, *q;
10319 +       struct au_finfo *finfo;
10320 +       struct super_block *sb;
10321 +       struct au_fidir *fidir;
10322 +
10323 +       FiMustWriteLock(file);
10324 +
10325 +       sb = file->f_dentry->d_sb;
10326 +       finfo = au_fi(file);
10327 +       fidir = finfo->fi_hdir;
10328 +       AuDebugOn(!fidir);
10329 +       p = fidir->fd_hfile + finfo->fi_btop;
10330 +       brid = p->hf_br->br_id;
10331 +       bend = fidir->fd_bbot;
10332 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
10333 +               if (!p->hf_file)
10334 +                       continue;
10335 +
10336 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
10337 +               if (new_bindex == bindex)
10338 +                       continue;
10339 +               if (new_bindex < 0) {
10340 +                       au_set_h_fptr(file, bindex, NULL);
10341 +                       continue;
10342 +               }
10343 +
10344 +               /* swap two lower inode, and loop again */
10345 +               q = fidir->fd_hfile + new_bindex;
10346 +               tmp = *q;
10347 +               *q = *p;
10348 +               *p = tmp;
10349 +               if (tmp.hf_file) {
10350 +                       bindex--;
10351 +                       p--;
10352 +               }
10353 +       }
10354 +
10355 +       p = fidir->fd_hfile;
10356 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
10357 +               bend = au_sbend(sb);
10358 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
10359 +                    finfo->fi_btop++, p++)
10360 +                       if (p->hf_file) {
10361 +                               if (p->hf_file->f_dentry
10362 +                                   && p->hf_file->f_dentry->d_inode)
10363 +                                       break;
10364 +                               else
10365 +                                       au_hfput(p, file);
10366 +                       }
10367 +       } else {
10368 +               bend = au_br_index(sb, brid);
10369 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
10370 +                    finfo->fi_btop++, p++)
10371 +                       if (p->hf_file)
10372 +                               au_hfput(p, file);
10373 +               bend = au_sbend(sb);
10374 +       }
10375 +
10376 +       p = fidir->fd_hfile + bend;
10377 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
10378 +            fidir->fd_bbot--, p--)
10379 +               if (p->hf_file) {
10380 +                       if (p->hf_file->f_dentry
10381 +                           && p->hf_file->f_dentry->d_inode)
10382 +                               break;
10383 +                       else
10384 +                               au_hfput(p, file);
10385 +               }
10386 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
10387 +}
10388 +
10389 +/*
10390 + * after branch manipulating, refresh the file.
10391 + */
10392 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
10393 +{
10394 +       int err, need_reopen;
10395 +       aufs_bindex_t bend, bindex;
10396 +       struct dentry *dentry;
10397 +       struct au_finfo *finfo;
10398 +       struct au_hfile *hfile;
10399 +
10400 +       dentry = file->f_dentry;
10401 +       finfo = au_fi(file);
10402 +       if (!finfo->fi_hdir) {
10403 +               hfile = &finfo->fi_htop;
10404 +               AuDebugOn(!hfile->hf_file);
10405 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
10406 +               AuDebugOn(bindex < 0);
10407 +               if (bindex != finfo->fi_btop)
10408 +                       au_set_fbstart(file, bindex);
10409 +       } else {
10410 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
10411 +               if (unlikely(err))
10412 +                       goto out;
10413 +               au_do_refresh_dir(file);
10414 +       }
10415 +
10416 +       err = 0;
10417 +       need_reopen = 1;
10418 +       if (!au_test_mmapped(file))
10419 +               err = au_file_refresh_by_inode(file, &need_reopen);
10420 +       if (!err && need_reopen && !d_unlinked(dentry))
10421 +               err = reopen(file);
10422 +       if (!err) {
10423 +               au_update_figen(file);
10424 +               goto out; /* success */
10425 +       }
10426 +
10427 +       /* error, close all lower files */
10428 +       if (finfo->fi_hdir) {
10429 +               bend = au_fbend_dir(file);
10430 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
10431 +                       au_set_h_fptr(file, bindex, NULL);
10432 +       }
10433 +
10434 +out:
10435 +       return err;
10436 +}
10437 +
10438 +/* common function to regular file and dir */
10439 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10440 +                         int wlock)
10441 +{
10442 +       int err;
10443 +       unsigned int sigen, figen;
10444 +       aufs_bindex_t bstart;
10445 +       unsigned char pseudo_link;
10446 +       struct dentry *dentry;
10447 +       struct inode *inode;
10448 +
10449 +       err = 0;
10450 +       dentry = file->f_dentry;
10451 +       inode = dentry->d_inode;
10452 +       AuDebugOn(au_special_file(inode->i_mode));
10453 +       sigen = au_sigen(dentry->d_sb);
10454 +       fi_write_lock(file);
10455 +       figen = au_figen(file);
10456 +       di_write_lock_child(dentry);
10457 +       bstart = au_dbstart(dentry);
10458 +       pseudo_link = (bstart != au_ibstart(inode));
10459 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
10460 +               if (!wlock) {
10461 +                       di_downgrade_lock(dentry, AuLock_IR);
10462 +                       fi_downgrade_lock(file);
10463 +               }
10464 +               goto out; /* success */
10465 +       }
10466 +
10467 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
10468 +       if (au_digen_test(dentry, sigen)) {
10469 +               err = au_reval_dpath(dentry, sigen);
10470 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
10471 +       }
10472 +
10473 +       if (!err)
10474 +               err = refresh_file(file, reopen);
10475 +       if (!err) {
10476 +               if (!wlock) {
10477 +                       di_downgrade_lock(dentry, AuLock_IR);
10478 +                       fi_downgrade_lock(file);
10479 +               }
10480 +       } else {
10481 +               di_write_unlock(dentry);
10482 +               fi_write_unlock(file);
10483 +       }
10484 +
10485 +out:
10486 +       return err;
10487 +}
10488 +
10489 +/* ---------------------------------------------------------------------- */
10490 +
10491 +/* cf. aufs_nopage() */
10492 +/* for madvise(2) */
10493 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
10494 +{
10495 +       unlock_page(page);
10496 +       return 0;
10497 +}
10498 +
10499 +/* it will never be called, but necessary to support O_DIRECT */
10500 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
10501 +                             const struct iovec *iov, loff_t offset,
10502 +                             unsigned long nr_segs)
10503 +{ BUG(); return 0; }
10504 +
10505 +/*
10506 + * it will never be called, but madvise and fadvise behaves differently
10507 + * when get_xip_mem is defined
10508 + */
10509 +static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
10510 +                           int create, void **kmem, unsigned long *pfn)
10511 +{ BUG(); return 0; }
10512 +
10513 +/* they will never be called. */
10514 +#ifdef CONFIG_AUFS_DEBUG
10515 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
10516 +                           loff_t pos, unsigned len, unsigned flags,
10517 +                           struct page **pagep, void **fsdata)
10518 +{ AuUnsupport(); return 0; }
10519 +static int aufs_write_end(struct file *file, struct address_space *mapping,
10520 +                         loff_t pos, unsigned len, unsigned copied,
10521 +                         struct page *page, void *fsdata)
10522 +{ AuUnsupport(); return 0; }
10523 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
10524 +{ AuUnsupport(); return 0; }
10525 +
10526 +static int aufs_set_page_dirty(struct page *page)
10527 +{ AuUnsupport(); return 0; }
10528 +static void aufs_invalidatepage(struct page *page, unsigned long offset)
10529 +{ AuUnsupport(); }
10530 +static int aufs_releasepage(struct page *page, gfp_t gfp)
10531 +{ AuUnsupport(); return 0; }
10532 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
10533 +                           struct page *page, enum migrate_mode mode)
10534 +{ AuUnsupport(); return 0; }
10535 +static int aufs_launder_page(struct page *page)
10536 +{ AuUnsupport(); return 0; }
10537 +static int aufs_is_partially_uptodate(struct page *page,
10538 +                                     read_descriptor_t *desc,
10539 +                                     unsigned long from)
10540 +{ AuUnsupport(); return 0; }
10541 +static int aufs_error_remove_page(struct address_space *mapping,
10542 +                                 struct page *page)
10543 +{ AuUnsupport(); return 0; }
10544 +#endif /* CONFIG_AUFS_DEBUG */
10545 +
10546 +const struct address_space_operations aufs_aop = {
10547 +       .readpage               = aufs_readpage,
10548 +       .direct_IO              = aufs_direct_IO,
10549 +       .get_xip_mem            = aufs_get_xip_mem,
10550 +#ifdef CONFIG_AUFS_DEBUG
10551 +       .writepage              = aufs_writepage,
10552 +       /* no writepages, because of writepage */
10553 +       .set_page_dirty         = aufs_set_page_dirty,
10554 +       /* no readpages, because of readpage */
10555 +       .write_begin            = aufs_write_begin,
10556 +       .write_end              = aufs_write_end,
10557 +       /* no bmap, no block device */
10558 +       .invalidatepage         = aufs_invalidatepage,
10559 +       .releasepage            = aufs_releasepage,
10560 +       .migratepage            = aufs_migratepage,
10561 +       .launder_page           = aufs_launder_page,
10562 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
10563 +       .error_remove_page      = aufs_error_remove_page
10564 +#endif /* CONFIG_AUFS_DEBUG */
10565 +};
10566 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
10567 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
10568 +++ linux/fs/aufs/file.h        2012-03-19 12:24:13.683134508 +0100
10569 @@ -0,0 +1,298 @@
10570 +/*
10571 + * Copyright (C) 2005-2012 Junjiro R. Okajima
10572 + *
10573 + * This program, aufs is free software; you can redistribute it and/or modify
10574 + * it under the terms of the GNU General Public License as published by
10575 + * the Free Software Foundation; either version 2 of the License, or
10576 + * (at your option) any later version.
10577 + *
10578 + * This program is distributed in the hope that it will be useful,
10579 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10580 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10581 + * GNU General Public License for more details.
10582 + *
10583 + * You should have received a copy of the GNU General Public License
10584 + * along with this program; if not, write to the Free Software
10585 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10586 + */
10587 +
10588 +/*
10589 + * file operations
10590 + */
10591 +
10592 +#ifndef __AUFS_FILE_H__
10593 +#define __AUFS_FILE_H__
10594 +
10595 +#ifdef __KERNEL__
10596 +
10597 +#include <linux/file.h>
10598 +#include <linux/fs.h>
10599 +#include <linux/poll.h>
10600 +#include "rwsem.h"
10601 +
10602 +struct au_branch;
10603 +struct au_hfile {
10604 +       struct file             *hf_file;
10605 +       struct au_branch        *hf_br;
10606 +};
10607 +
10608 +struct au_vdir;
10609 +struct au_fidir {
10610 +       aufs_bindex_t           fd_bbot;
10611 +       aufs_bindex_t           fd_nent;
10612 +       struct au_vdir          *fd_vdir_cache;
10613 +       struct au_hfile         fd_hfile[];
10614 +};
10615 +
10616 +static inline int au_fidir_sz(int nent)
10617 +{
10618 +       AuDebugOn(nent < 0);
10619 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
10620 +}
10621 +
10622 +struct au_finfo {
10623 +       atomic_t                fi_generation;
10624 +
10625 +       struct au_rwsem         fi_rwsem;
10626 +       aufs_bindex_t           fi_btop;
10627 +
10628 +       /* do not union them */
10629 +       struct {                                /* for non-dir */
10630 +               struct au_hfile                 fi_htop;
10631 +               atomic_t                        fi_mmapped;
10632 +       };
10633 +       struct au_fidir         *fi_hdir;       /* for dir only */
10634 +} ____cacheline_aligned_in_smp;
10635 +
10636 +/* ---------------------------------------------------------------------- */
10637 +
10638 +/* file.c */
10639 +extern const struct address_space_operations aufs_aop;
10640 +unsigned int au_file_roflags(unsigned int flags);
10641 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10642 +                      struct file *file);
10643 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10644 +              struct au_fidir *fidir);
10645 +int au_reopen_nondir(struct file *file);
10646 +struct au_pin;
10647 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
10648 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10649 +                         int wlock);
10650 +int au_do_flush(struct file *file, fl_owner_t id,
10651 +               int (*flush)(struct file *file, fl_owner_t id));
10652 +
10653 +/* poll.c */
10654 +#ifdef CONFIG_AUFS_POLL
10655 +unsigned int aufs_poll(struct file *file, poll_table *wait);
10656 +#endif
10657 +
10658 +#ifdef CONFIG_AUFS_BR_HFSPLUS
10659 +/* hfsplus.c */
10660 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex);
10661 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
10662 +                   struct file *h_file);
10663 +#else
10664 +static inline
10665 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
10666 +{
10667 +       return NULL;
10668 +}
10669 +
10670 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
10671 +          struct file *h_file);
10672 +#endif
10673 +
10674 +/* f_op.c */
10675 +extern const struct file_operations aufs_file_fop;
10676 +int au_do_open_nondir(struct file *file, int flags);
10677 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
10678 +
10679 +#ifdef CONFIG_AUFS_SP_IATTR
10680 +/* f_op_sp.c */
10681 +int au_special_file(umode_t mode);
10682 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev);
10683 +#else
10684 +AuStubInt0(au_special_file, umode_t mode)
10685 +static inline void au_init_special_fop(struct inode *inode, umode_t mode,
10686 +                                      dev_t rdev)
10687 +{
10688 +       init_special_inode(inode, mode, rdev);
10689 +}
10690 +#endif
10691 +
10692 +/* finfo.c */
10693 +void au_hfput(struct au_hfile *hf, struct file *file);
10694 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
10695 +                  struct file *h_file);
10696 +
10697 +void au_update_figen(struct file *file);
10698 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
10699 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
10700 +
10701 +void au_fi_init_once(void *_fi);
10702 +void au_finfo_fin(struct file *file);
10703 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
10704 +
10705 +/* ioctl.c */
10706 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
10707 +#ifdef CONFIG_COMPAT
10708 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
10709 +                          unsigned long arg);
10710 +#endif
10711 +
10712 +/* ---------------------------------------------------------------------- */
10713 +
10714 +static inline struct au_finfo *au_fi(struct file *file)
10715 +{
10716 +       return file->private_data;
10717 +}
10718 +
10719 +/* ---------------------------------------------------------------------- */
10720 +
10721 +/*
10722 + * fi_read_lock, fi_write_lock,
10723 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
10724 + */
10725 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
10726 +
10727 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
10728 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
10729 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
10730 +
10731 +/* ---------------------------------------------------------------------- */
10732 +
10733 +/* todo: hard/soft set? */
10734 +static inline aufs_bindex_t au_fbstart(struct file *file)
10735 +{
10736 +       FiMustAnyLock(file);
10737 +       return au_fi(file)->fi_btop;
10738 +}
10739 +
10740 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
10741 +{
10742 +       FiMustAnyLock(file);
10743 +       AuDebugOn(!au_fi(file)->fi_hdir);
10744 +       return au_fi(file)->fi_hdir->fd_bbot;
10745 +}
10746 +
10747 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
10748 +{
10749 +       FiMustAnyLock(file);
10750 +       AuDebugOn(!au_fi(file)->fi_hdir);
10751 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
10752 +}
10753 +
10754 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
10755 +{
10756 +       FiMustWriteLock(file);
10757 +       au_fi(file)->fi_btop = bindex;
10758 +}
10759 +
10760 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
10761 +{
10762 +       FiMustWriteLock(file);
10763 +       AuDebugOn(!au_fi(file)->fi_hdir);
10764 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
10765 +}
10766 +
10767 +static inline void au_set_fvdir_cache(struct file *file,
10768 +                                     struct au_vdir *vdir_cache)
10769 +{
10770 +       FiMustWriteLock(file);
10771 +       AuDebugOn(!au_fi(file)->fi_hdir);
10772 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
10773 +}
10774 +
10775 +static inline struct file *au_hf_top(struct file *file)
10776 +{
10777 +       FiMustAnyLock(file);
10778 +       AuDebugOn(au_fi(file)->fi_hdir);
10779 +       return au_fi(file)->fi_htop.hf_file;
10780 +}
10781 +
10782 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
10783 +{
10784 +       FiMustAnyLock(file);
10785 +       AuDebugOn(!au_fi(file)->fi_hdir);
10786 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
10787 +}
10788 +
10789 +/* todo: memory barrier? */
10790 +static inline unsigned int au_figen(struct file *f)
10791 +{
10792 +       return atomic_read(&au_fi(f)->fi_generation);
10793 +}
10794 +
10795 +static inline void au_set_mmapped(struct file *f)
10796 +{
10797 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
10798 +               return;
10799 +       pr_warning("fi_mmapped wrapped around\n");
10800 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
10801 +               ;
10802 +}
10803 +
10804 +static inline void au_unset_mmapped(struct file *f)
10805 +{
10806 +       atomic_dec(&au_fi(f)->fi_mmapped);
10807 +}
10808 +
10809 +static inline int au_test_mmapped(struct file *f)
10810 +{
10811 +       return atomic_read(&au_fi(f)->fi_mmapped);
10812 +}
10813 +
10814 +/* customize vma->vm_file */
10815 +
10816 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
10817 +                                      struct file *file)
10818 +{
10819 +       struct file *f;
10820 +
10821 +       f = vma->vm_file;
10822 +       get_file(file);
10823 +       vma->vm_file = file;
10824 +       fput(f);
10825 +}
10826 +
10827 +#ifdef CONFIG_MMU
10828 +#define AuDbgVmRegion(file, vma) do {} while (0)
10829 +
10830 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
10831 +                                   struct file *file)
10832 +{
10833 +       au_do_vm_file_reset(vma, file);
10834 +}
10835 +#else
10836 +#define AuDbgVmRegion(file, vma) \
10837 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
10838 +
10839 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
10840 +                                   struct file *file)
10841 +{
10842 +       struct file *f;
10843 +
10844 +       au_do_vm_file_reset(vma, file);
10845 +       f = vma->vm_region->vm_file;
10846 +       get_file(file);
10847 +       vma->vm_region->vm_file = file;
10848 +       fput(f);
10849 +}
10850 +#endif /* CONFIG_MMU */
10851 +
10852 +/* handle vma->vm_prfile */
10853 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
10854 +                                   struct file *file)
10855 +{
10856 +#ifdef CONFIG_AUFS_PROC_MAP
10857 +       get_file(file);
10858 +       vma->vm_prfile = file;
10859 +#ifndef CONFIG_MMU
10860 +       get_file(file);
10861 +       vma->vm_region->vm_prfile = file;
10862 +#endif
10863 +#endif
10864 +}
10865 +
10866 +#endif /* __KERNEL__ */
10867 +#endif /* __AUFS_FILE_H__ */
10868 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
10869 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
10870 +++ linux/fs/aufs/finfo.c       2012-03-19 12:24:13.683134508 +0100
10871 @@ -0,0 +1,156 @@
10872 +/*
10873 + * Copyright (C) 2005-2012 Junjiro R. Okajima
10874 + *
10875 + * This program, aufs is free software; you can redistribute it and/or modify
10876 + * it under the terms of the GNU General Public License as published by
10877 + * the Free Software Foundation; either version 2 of the License, or
10878 + * (at your option) any later version.
10879 + *
10880 + * This program is distributed in the hope that it will be useful,
10881 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10882 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10883 + * GNU General Public License for more details.
10884 + *
10885 + * You should have received a copy of the GNU General Public License
10886 + * along with this program; if not, write to the Free Software
10887 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10888 + */
10889 +
10890 +/*
10891 + * file private data
10892 + */
10893 +
10894 +#include "aufs.h"
10895 +
10896 +void au_hfput(struct au_hfile *hf, struct file *file)
10897 +{
10898 +       /* todo: direct access f_flags */
10899 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
10900 +               allow_write_access(hf->hf_file);
10901 +       fput(hf->hf_file);
10902 +       hf->hf_file = NULL;
10903 +       atomic_dec(&hf->hf_br->br_count);
10904 +       hf->hf_br = NULL;
10905 +}
10906 +
10907 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
10908 +{
10909 +       struct au_finfo *finfo = au_fi(file);
10910 +       struct au_hfile *hf;
10911 +       struct au_fidir *fidir;
10912 +
10913 +       fidir = finfo->fi_hdir;
10914 +       if (!fidir) {
10915 +               AuDebugOn(finfo->fi_btop != bindex);
10916 +               hf = &finfo->fi_htop;
10917 +       } else
10918 +               hf = fidir->fd_hfile + bindex;
10919 +
10920 +       if (hf && hf->hf_file)
10921 +               au_hfput(hf, file);
10922 +       if (val) {
10923 +               FiMustWriteLock(file);
10924 +               hf->hf_file = val;
10925 +               hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
10926 +       }
10927 +}
10928 +
10929 +void au_update_figen(struct file *file)
10930 +{
10931 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
10932 +       /* smp_mb(); */ /* atomic_set */
10933 +}
10934 +
10935 +/* ---------------------------------------------------------------------- */
10936 +
10937 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
10938 +{
10939 +       struct au_fidir *fidir;
10940 +       int nbr;
10941 +
10942 +       nbr = au_sbend(sb) + 1;
10943 +       if (nbr < 2)
10944 +               nbr = 2; /* initial allocate for 2 branches */
10945 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
10946 +       if (fidir) {
10947 +               fidir->fd_bbot = -1;
10948 +               fidir->fd_nent = nbr;
10949 +               fidir->fd_vdir_cache = NULL;
10950 +       }
10951 +
10952 +       return fidir;
10953 +}
10954 +
10955 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
10956 +{
10957 +       int err;
10958 +       struct au_fidir *fidir, *p;
10959 +
10960 +       AuRwMustWriteLock(&finfo->fi_rwsem);
10961 +       fidir = finfo->fi_hdir;
10962 +       AuDebugOn(!fidir);
10963 +
10964 +       err = -ENOMEM;
10965 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
10966 +                        GFP_NOFS);
10967 +       if (p) {
10968 +               p->fd_nent = nbr;
10969 +               finfo->fi_hdir = p;
10970 +               err = 0;
10971 +       }
10972 +
10973 +       return err;
10974 +}
10975 +
10976 +/* ---------------------------------------------------------------------- */
10977 +
10978 +void au_finfo_fin(struct file *file)
10979 +{
10980 +       struct au_finfo *finfo;
10981 +
10982 +       au_nfiles_dec(file->f_dentry->d_sb);
10983 +
10984 +       finfo = au_fi(file);
10985 +       AuDebugOn(finfo->fi_hdir);
10986 +       AuRwDestroy(&finfo->fi_rwsem);
10987 +       au_cache_free_finfo(finfo);
10988 +}
10989 +
10990 +void au_fi_init_once(void *_finfo)
10991 +{
10992 +       struct au_finfo *finfo = _finfo;
10993 +       static struct lock_class_key aufs_fi;
10994 +
10995 +       au_rw_init(&finfo->fi_rwsem);
10996 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
10997 +}
10998 +
10999 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
11000 +{
11001 +       int err, lc_idx;
11002 +       struct au_finfo *finfo;
11003 +       struct dentry *dentry;
11004 +
11005 +       err = -ENOMEM;
11006 +       dentry = file->f_dentry;
11007 +       finfo = au_cache_alloc_finfo();
11008 +       if (unlikely(!finfo))
11009 +               goto out;
11010 +
11011 +       err = 0;
11012 +       au_nfiles_inc(dentry->d_sb);
11013 +       lc_idx = AuLcNonDir_FIINFO;
11014 +       if (fidir)
11015 +               lc_idx = AuLcDir_FIINFO;
11016 +       au_rw_class(&finfo->fi_rwsem, au_lc_key + lc_idx);
11017 +       au_rw_write_lock(&finfo->fi_rwsem);
11018 +       finfo->fi_btop = -1;
11019 +       finfo->fi_hdir = fidir;
11020 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
11021 +       /* smp_mb(); */ /* atomic_set */
11022 +
11023 +       file->private_data = finfo;
11024 +
11025 +out:
11026 +       return err;
11027 +}
11028 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
11029 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
11030 +++ linux/fs/aufs/f_op.c        2012-03-19 12:24:13.679801090 +0100
11031 @@ -0,0 +1,729 @@
11032 +/*
11033 + * Copyright (C) 2005-2012 Junjiro R. Okajima
11034 + *
11035 + * This program, aufs is free software; you can redistribute it and/or modify
11036 + * it under the terms of the GNU General Public License as published by
11037 + * the Free Software Foundation; either version 2 of the License, or
11038 + * (at your option) any later version.
11039 + *
11040 + * This program is distributed in the hope that it will be useful,
11041 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11042 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11043 + * GNU General Public License for more details.
11044 + *
11045 + * You should have received a copy of the GNU General Public License
11046 + * along with this program; if not, write to the Free Software
11047 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11048 + */
11049 +
11050 +/*
11051 + * file and vm operations
11052 + */
11053 +
11054 +#include <linux/fs_stack.h>
11055 +#include <linux/mman.h>
11056 +#include <linux/security.h>
11057 +#include "aufs.h"
11058 +
11059 +int au_do_open_nondir(struct file *file, int flags)
11060 +{
11061 +       int err;
11062 +       aufs_bindex_t bindex;
11063 +       struct file *h_file;
11064 +       struct dentry *dentry;
11065 +       struct au_finfo *finfo;
11066 +
11067 +       FiMustWriteLock(file);
11068 +
11069 +       dentry = file->f_dentry;
11070 +       err = au_d_alive(dentry);
11071 +       if (unlikely(err))
11072 +               goto out;
11073 +
11074 +       finfo = au_fi(file);
11075 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
11076 +       atomic_set(&finfo->fi_mmapped, 0);
11077 +       bindex = au_dbstart(dentry);
11078 +       h_file = au_h_open(dentry, bindex, flags, file);
11079 +       if (IS_ERR(h_file))
11080 +               err = PTR_ERR(h_file);
11081 +       else {
11082 +               au_set_fbstart(file, bindex);
11083 +               au_set_h_fptr(file, bindex, h_file);
11084 +               au_update_figen(file);
11085 +               /* todo: necessary? */
11086 +               /* file->f_ra = h_file->f_ra; */
11087 +       }
11088 +
11089 +out:
11090 +       return err;
11091 +}
11092 +
11093 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
11094 +                           struct file *file)
11095 +{
11096 +       int err;
11097 +       struct super_block *sb;
11098 +
11099 +       AuDbg("%.*s, f_flags 0x%x, f_mode 0x%x\n",
11100 +             AuDLNPair(file->f_dentry), vfsub_file_flags(file),
11101 +             file->f_mode);
11102 +
11103 +       sb = file->f_dentry->d_sb;
11104 +       si_read_lock(sb, AuLock_FLUSH);
11105 +       err = au_do_open(file, au_do_open_nondir, /*fidir*/NULL);
11106 +       si_read_unlock(sb);
11107 +       return err;
11108 +}
11109 +
11110 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
11111 +{
11112 +       struct au_finfo *finfo;
11113 +       aufs_bindex_t bindex;
11114 +
11115 +       finfo = au_fi(file);
11116 +       bindex = finfo->fi_btop;
11117 +       if (bindex >= 0) {
11118 +               /* remove me from sb->s_files */
11119 +               file_sb_list_del(file);
11120 +               au_set_h_fptr(file, bindex, NULL);
11121 +       }
11122 +
11123 +       au_finfo_fin(file);
11124 +       return 0;
11125 +}
11126 +
11127 +/* ---------------------------------------------------------------------- */
11128 +
11129 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
11130 +{
11131 +       int err;
11132 +       struct file *h_file;
11133 +
11134 +       err = 0;
11135 +       h_file = au_hf_top(file);
11136 +       if (h_file)
11137 +               err = vfsub_flush(h_file, id);
11138 +       return err;
11139 +}
11140 +
11141 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
11142 +{
11143 +       return au_do_flush(file, id, au_do_flush_nondir);
11144 +}
11145 +
11146 +/* ---------------------------------------------------------------------- */
11147 +/*
11148 + * read and write functions acquire [fdi]_rwsem once, but release before
11149 + * mmap_sem. This is because to stop a race condition between mmap(2).
11150 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
11151 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
11152 + * read functions after [fdi]_rwsem are released, but it should be harmless.
11153 + */
11154 +
11155 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
11156 +                        loff_t *ppos)
11157 +{
11158 +       ssize_t err;
11159 +       struct dentry *dentry;
11160 +       struct file *h_file;
11161 +       struct super_block *sb;
11162 +
11163 +       dentry = file->f_dentry;
11164 +       sb = dentry->d_sb;
11165 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11166 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11167 +       if (unlikely(err))
11168 +               goto out;
11169 +
11170 +       h_file = au_hf_top(file);
11171 +       get_file(h_file);
11172 +       di_read_unlock(dentry, AuLock_IR);
11173 +       fi_read_unlock(file);
11174 +
11175 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
11176 +       err = vfsub_read_u(h_file, buf, count, ppos);
11177 +       /* todo: necessary? */
11178 +       /* file->f_ra = h_file->f_ra; */
11179 +       /* update without lock, I don't think it a problem */
11180 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
11181 +       fput(h_file);
11182 +
11183 +out:
11184 +       si_read_unlock(sb);
11185 +       return err;
11186 +}
11187 +
11188 +/*
11189 + * todo: very ugly
11190 + * it locks both of i_mutex and si_rwsem for read in safe.
11191 + * if the plink maintenance mode continues forever (that is the problem),
11192 + * may loop forever.
11193 + */
11194 +static void au_mtx_and_read_lock(struct inode *inode)
11195 +{
11196 +       int err;
11197 +       struct super_block *sb = inode->i_sb;
11198 +
11199 +       while (1) {
11200 +               mutex_lock(&inode->i_mutex);
11201 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
11202 +               if (!err)
11203 +                       break;
11204 +               mutex_unlock(&inode->i_mutex);
11205 +               si_read_lock(sb, AuLock_NOPLMW);
11206 +               si_read_unlock(sb);
11207 +       }
11208 +}
11209 +
11210 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
11211 +                         size_t count, loff_t *ppos)
11212 +{
11213 +       ssize_t err;
11214 +       struct au_pin pin;
11215 +       struct dentry *dentry;
11216 +       struct super_block *sb;
11217 +       struct inode *inode;
11218 +       struct file *h_file;
11219 +       char __user *buf = (char __user *)ubuf;
11220 +
11221 +       dentry = file->f_dentry;
11222 +       sb = dentry->d_sb;
11223 +       inode = dentry->d_inode;
11224 +       au_mtx_and_read_lock(inode);
11225 +
11226 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11227 +       if (unlikely(err))
11228 +               goto out;
11229 +
11230 +       err = au_ready_to_write(file, -1, &pin);
11231 +       di_downgrade_lock(dentry, AuLock_IR);
11232 +       if (unlikely(err)) {
11233 +               di_read_unlock(dentry, AuLock_IR);
11234 +               fi_write_unlock(file);
11235 +               goto out;
11236 +       }
11237 +
11238 +       h_file = au_hf_top(file);
11239 +       get_file(h_file);
11240 +       au_unpin(&pin);
11241 +       di_read_unlock(dentry, AuLock_IR);
11242 +       fi_write_unlock(file);
11243 +
11244 +       err = vfsub_write_u(h_file, buf, count, ppos);
11245 +       ii_write_lock_child(inode);
11246 +       au_cpup_attr_timesizes(inode);
11247 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
11248 +       ii_write_unlock(inode);
11249 +       fput(h_file);
11250 +
11251 +out:
11252 +       si_read_unlock(sb);
11253 +       mutex_unlock(&inode->i_mutex);
11254 +       return err;
11255 +}
11256 +
11257 +static ssize_t au_do_aio(struct file *h_file, int rw, struct kiocb *kio,
11258 +                        const struct iovec *iov, unsigned long nv, loff_t pos)
11259 +{
11260 +       ssize_t err;
11261 +       struct file *file;
11262 +       ssize_t (*func)(struct kiocb *, const struct iovec *, unsigned long,
11263 +                       loff_t);
11264 +
11265 +       err = security_file_permission(h_file, rw);
11266 +       if (unlikely(err))
11267 +               goto out;
11268 +
11269 +       err = -ENOSYS;
11270 +       func = NULL;
11271 +       if (rw == MAY_READ)
11272 +               func = h_file->f_op->aio_read;
11273 +       else if (rw == MAY_WRITE)
11274 +               func = h_file->f_op->aio_write;
11275 +       if (func) {
11276 +               file = kio->ki_filp;
11277 +               kio->ki_filp = h_file;
11278 +               lockdep_off();
11279 +               err = func(kio, iov, nv, pos);
11280 +               lockdep_on();
11281 +               kio->ki_filp = file;
11282 +       } else
11283 +               /* currently there is no such fs */
11284 +               WARN_ON_ONCE(1);
11285 +
11286 +out:
11287 +       return err;
11288 +}
11289 +
11290 +static ssize_t aufs_aio_read(struct kiocb *kio, const struct iovec *iov,
11291 +                            unsigned long nv, loff_t pos)
11292 +{
11293 +       ssize_t err;
11294 +       struct file *file, *h_file;
11295 +       struct dentry *dentry;
11296 +       struct super_block *sb;
11297 +
11298 +       file = kio->ki_filp;
11299 +       dentry = file->f_dentry;
11300 +       sb = dentry->d_sb;
11301 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11302 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11303 +       if (unlikely(err))
11304 +               goto out;
11305 +
11306 +       h_file = au_hf_top(file);
11307 +       get_file(h_file);
11308 +       di_read_unlock(dentry, AuLock_IR);
11309 +       fi_read_unlock(file);
11310 +
11311 +       err = au_do_aio(h_file, MAY_READ, kio, iov, nv, pos);
11312 +       /* todo: necessary? */
11313 +       /* file->f_ra = h_file->f_ra; */
11314 +       /* update without lock, I don't think it a problem */
11315 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
11316 +       fput(h_file);
11317 +
11318 +out:
11319 +       si_read_unlock(sb);
11320 +       return err;
11321 +}
11322 +
11323 +static ssize_t aufs_aio_write(struct kiocb *kio, const struct iovec *iov,
11324 +                             unsigned long nv, loff_t pos)
11325 +{
11326 +       ssize_t err;
11327 +       struct au_pin pin;
11328 +       struct dentry *dentry;
11329 +       struct inode *inode;
11330 +       struct file *file, *h_file;
11331 +       struct super_block *sb;
11332 +
11333 +       file = kio->ki_filp;
11334 +       dentry = file->f_dentry;
11335 +       sb = dentry->d_sb;
11336 +       inode = dentry->d_inode;
11337 +       au_mtx_and_read_lock(inode);
11338 +
11339 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11340 +       if (unlikely(err))
11341 +               goto out;
11342 +
11343 +       err = au_ready_to_write(file, -1, &pin);
11344 +       di_downgrade_lock(dentry, AuLock_IR);
11345 +       if (unlikely(err)) {
11346 +               di_read_unlock(dentry, AuLock_IR);
11347 +               fi_write_unlock(file);
11348 +               goto out;
11349 +       }
11350 +
11351 +       h_file = au_hf_top(file);
11352 +       get_file(h_file);
11353 +       au_unpin(&pin);
11354 +       di_read_unlock(dentry, AuLock_IR);
11355 +       fi_write_unlock(file);
11356 +
11357 +       err = au_do_aio(h_file, MAY_WRITE, kio, iov, nv, pos);
11358 +       ii_write_lock_child(inode);
11359 +       au_cpup_attr_timesizes(inode);
11360 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
11361 +       ii_write_unlock(inode);
11362 +       fput(h_file);
11363 +
11364 +out:
11365 +       si_read_unlock(sb);
11366 +       mutex_unlock(&inode->i_mutex);
11367 +       return err;
11368 +}
11369 +
11370 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
11371 +                               struct pipe_inode_info *pipe, size_t len,
11372 +                               unsigned int flags)
11373 +{
11374 +       ssize_t err;
11375 +       struct file *h_file;
11376 +       struct dentry *dentry;
11377 +       struct super_block *sb;
11378 +
11379 +       dentry = file->f_dentry;
11380 +       sb = dentry->d_sb;
11381 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11382 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11383 +       if (unlikely(err))
11384 +               goto out;
11385 +
11386 +       err = -EINVAL;
11387 +       h_file = au_hf_top(file);
11388 +       get_file(h_file);
11389 +       if (au_test_loopback_kthread()) {
11390 +               au_warn_loopback(h_file->f_dentry->d_sb);
11391 +               if (file->f_mapping != h_file->f_mapping) {
11392 +                       file->f_mapping = h_file->f_mapping;
11393 +                       smp_mb(); /* unnecessary? */
11394 +               }
11395 +       }
11396 +       di_read_unlock(dentry, AuLock_IR);
11397 +       fi_read_unlock(file);
11398 +
11399 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
11400 +       /* todo: necessasry? */
11401 +       /* file->f_ra = h_file->f_ra; */
11402 +       /* update without lock, I don't think it a problem */
11403 +       fsstack_copy_attr_atime(dentry->d_inode, h_file->f_dentry->d_inode);
11404 +       fput(h_file);
11405 +
11406 +out:
11407 +       si_read_unlock(sb);
11408 +       return err;
11409 +}
11410 +
11411 +static ssize_t
11412 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
11413 +                 size_t len, unsigned int flags)
11414 +{
11415 +       ssize_t err;
11416 +       struct au_pin pin;
11417 +       struct dentry *dentry;
11418 +       struct inode *inode;
11419 +       struct file *h_file;
11420 +       struct super_block *sb;
11421 +
11422 +       dentry = file->f_dentry;
11423 +       sb = dentry->d_sb;
11424 +       inode = dentry->d_inode;
11425 +       au_mtx_and_read_lock(inode);
11426 +
11427 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11428 +       if (unlikely(err))
11429 +               goto out;
11430 +
11431 +       err = au_ready_to_write(file, -1, &pin);
11432 +       di_downgrade_lock(dentry, AuLock_IR);
11433 +       if (unlikely(err)) {
11434 +               di_read_unlock(dentry, AuLock_IR);
11435 +               fi_write_unlock(file);
11436 +               goto out;
11437 +       }
11438 +
11439 +       h_file = au_hf_top(file);
11440 +       get_file(h_file);
11441 +       au_unpin(&pin);
11442 +       di_read_unlock(dentry, AuLock_IR);
11443 +       fi_write_unlock(file);
11444 +
11445 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
11446 +       ii_write_lock_child(inode);
11447 +       au_cpup_attr_timesizes(inode);
11448 +       inode->i_mode = h_file->f_dentry->d_inode->i_mode;
11449 +       ii_write_unlock(inode);
11450 +       fput(h_file);
11451 +
11452 +out:
11453 +       si_read_unlock(sb);
11454 +       mutex_unlock(&inode->i_mutex);
11455 +       return err;
11456 +}
11457 +
11458 +/* ---------------------------------------------------------------------- */
11459 +
11460 +/*
11461 + * The locking order around current->mmap_sem.
11462 + * - in most and regular cases
11463 + *   file I/O syscall -- aufs_read() or something
11464 + *     -- si_rwsem for read -- mmap_sem
11465 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
11466 + * - in mmap case
11467 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
11468 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
11469 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
11470 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
11471 + * It means that when aufs acquires si_rwsem for write, the process should never
11472 + * acquire mmap_sem.
11473 + *
11474 + * Actually aufs_readdir() holds [fdi]i_rwsem before mmap_sem, but this is not a
11475 + * problem either since any directory is not able to be mmap-ed.
11476 + * The similar scenario is applied to aufs_readlink() too.
11477 + */
11478 +
11479 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
11480 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
11481 +
11482 +static unsigned long au_arch_prot_conv(unsigned long flags)
11483 +{
11484 +       /* currently ppc64 only */
11485 +#ifdef CONFIG_PPC64
11486 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
11487 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
11488 +       return AuConv_VM_PROT(flags, SAO);
11489 +#else
11490 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
11491 +       return 0;
11492 +#endif
11493 +}
11494 +
11495 +static unsigned long au_prot_conv(unsigned long flags)
11496 +{
11497 +       return AuConv_VM_PROT(flags, READ)
11498 +               | AuConv_VM_PROT(flags, WRITE)
11499 +               | AuConv_VM_PROT(flags, EXEC)
11500 +               | au_arch_prot_conv(flags);
11501 +}
11502 +
11503 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
11504 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
11505 +
11506 +static unsigned long au_flag_conv(unsigned long flags)
11507 +{
11508 +       return AuConv_VM_MAP(flags, GROWSDOWN)
11509 +               | AuConv_VM_MAP(flags, DENYWRITE)
11510 +               | AuConv_VM_MAP(flags, EXECUTABLE)
11511 +               | AuConv_VM_MAP(flags, LOCKED);
11512 +}
11513 +
11514 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
11515 +{
11516 +       int err;
11517 +       unsigned long prot;
11518 +       aufs_bindex_t bstart;
11519 +       const unsigned char wlock
11520 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
11521 +       struct dentry *dentry;
11522 +       struct super_block *sb;
11523 +       struct file *h_file;
11524 +       struct au_branch *br;
11525 +       struct au_pin pin;
11526 +
11527 +       AuDbgVmRegion(file, vma);
11528 +
11529 +       dentry = file->f_dentry;
11530 +       sb = dentry->d_sb;
11531 +       lockdep_off();
11532 +       si_read_lock(sb, AuLock_NOPLMW);
11533 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11534 +       if (unlikely(err))
11535 +               goto out;
11536 +
11537 +       if (wlock) {
11538 +               err = au_ready_to_write(file, -1, &pin);
11539 +               di_write_unlock(dentry);
11540 +               if (unlikely(err)) {
11541 +                       fi_write_unlock(file);
11542 +                       goto out;
11543 +               }
11544 +               au_unpin(&pin);
11545 +       } else
11546 +               di_write_unlock(dentry);
11547 +
11548 +       bstart = au_fbstart(file);
11549 +       br = au_sbr(sb, bstart);
11550 +       h_file = au_hf_top(file);
11551 +       get_file(h_file);
11552 +       au_set_mmapped(file);
11553 +       fi_write_unlock(file);
11554 +       lockdep_on();
11555 +
11556 +       au_vm_file_reset(vma, h_file);
11557 +       prot = au_prot_conv(vma->vm_flags);
11558 +       err = security_file_mmap(h_file, /*reqprot*/prot, prot,
11559 +                                au_flag_conv(vma->vm_flags), vma->vm_start, 0);
11560 +       if (!err)
11561 +               err = h_file->f_op->mmap(h_file, vma);
11562 +       if (unlikely(err))
11563 +               goto out_reset;
11564 +
11565 +       au_vm_prfile_set(vma, file);
11566 +       /* update without lock, I don't think it a problem */
11567 +       fsstack_copy_attr_atime(file->f_dentry->d_inode,
11568 +                               h_file->f_dentry->d_inode);
11569 +       goto out_fput; /* success */
11570 +
11571 +out_reset:
11572 +       au_unset_mmapped(file);
11573 +       au_vm_file_reset(vma, file);
11574 +out_fput:
11575 +       fput(h_file);
11576 +       lockdep_off();
11577 +out:
11578 +       si_read_unlock(sb);
11579 +       lockdep_on();
11580 +       AuTraceErr(err);
11581 +       return err;
11582 +}
11583 +
11584 +/* ---------------------------------------------------------------------- */
11585 +
11586 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
11587 +                            int datasync)
11588 +{
11589 +       int err;
11590 +       struct au_pin pin;
11591 +       struct dentry *dentry;
11592 +       struct inode *inode;
11593 +       struct file *h_file;
11594 +       struct super_block *sb;
11595 +
11596 +       dentry = file->f_dentry;
11597 +       inode = dentry->d_inode;
11598 +       sb = dentry->d_sb;
11599 +       mutex_lock(&inode->i_mutex);
11600 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
11601 +       if (unlikely(err))
11602 +               goto out;
11603 +
11604 +       err = 0; /* -EBADF; */ /* posix? */
11605 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
11606 +               goto out_si;
11607 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11608 +       if (unlikely(err))
11609 +               goto out_si;
11610 +
11611 +       err = au_ready_to_write(file, -1, &pin);
11612 +       di_downgrade_lock(dentry, AuLock_IR);
11613 +       if (unlikely(err))
11614 +               goto out_unlock;
11615 +       au_unpin(&pin);
11616 +
11617 +       err = -EINVAL;
11618 +       h_file = au_hf_top(file);
11619 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
11620 +       au_cpup_attr_timesizes(inode);
11621 +
11622 +out_unlock:
11623 +       di_read_unlock(dentry, AuLock_IR);
11624 +       fi_write_unlock(file);
11625 +out_si:
11626 +       si_read_unlock(sb);
11627 +out:
11628 +       mutex_unlock(&inode->i_mutex);
11629 +       return err;
11630 +}
11631 +
11632 +/* no one supports this operation, currently */
11633 +#if 0
11634 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
11635 +{
11636 +       int err;
11637 +       struct au_pin pin;
11638 +       struct dentry *dentry;
11639 +       struct inode *inode;
11640 +       struct file *file, *h_file;
11641 +
11642 +       file = kio->ki_filp;
11643 +       dentry = file->f_dentry;
11644 +       inode = dentry->d_inode;
11645 +       au_mtx_and_read_lock(inode);
11646 +
11647 +       err = 0; /* -EBADF; */ /* posix? */
11648 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
11649 +               goto out;
11650 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11651 +       if (unlikely(err))
11652 +               goto out;
11653 +
11654 +       err = au_ready_to_write(file, -1, &pin);
11655 +       di_downgrade_lock(dentry, AuLock_IR);
11656 +       if (unlikely(err))
11657 +               goto out_unlock;
11658 +       au_unpin(&pin);
11659 +
11660 +       err = -ENOSYS;
11661 +       h_file = au_hf_top(file);
11662 +       if (h_file->f_op && h_file->f_op->aio_fsync) {
11663 +               struct dentry *h_d;
11664 +               struct mutex *h_mtx;
11665 +
11666 +               h_d = h_file->f_dentry;
11667 +               h_mtx = &h_d->d_inode->i_mutex;
11668 +               if (!is_sync_kiocb(kio)) {
11669 +                       get_file(h_file);
11670 +                       fput(file);
11671 +               }
11672 +               kio->ki_filp = h_file;
11673 +               err = h_file->f_op->aio_fsync(kio, datasync);
11674 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
11675 +               if (!err)
11676 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
11677 +               /*ignore*/
11678 +               au_cpup_attr_timesizes(inode);
11679 +               mutex_unlock(h_mtx);
11680 +       }
11681 +
11682 +out_unlock:
11683 +       di_read_unlock(dentry, AuLock_IR);
11684 +       fi_write_unlock(file);
11685 +out:
11686 +       si_read_unlock(inode->sb);
11687 +       mutex_unlock(&inode->i_mutex);
11688 +       return err;
11689 +}
11690 +#endif
11691 +
11692 +static int aufs_fasync(int fd, struct file *file, int flag)
11693 +{
11694 +       int err;
11695 +       struct file *h_file;
11696 +       struct dentry *dentry;
11697 +       struct super_block *sb;
11698 +
11699 +       dentry = file->f_dentry;
11700 +       sb = dentry->d_sb;
11701 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11702 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11703 +       if (unlikely(err))
11704 +               goto out;
11705 +
11706 +       h_file = au_hf_top(file);
11707 +       if (h_file->f_op && h_file->f_op->fasync)
11708 +               err = h_file->f_op->fasync(fd, h_file, flag);
11709 +
11710 +       di_read_unlock(dentry, AuLock_IR);
11711 +       fi_read_unlock(file);
11712 +
11713 +out:
11714 +       si_read_unlock(sb);
11715 +       return err;
11716 +}
11717 +
11718 +/* ---------------------------------------------------------------------- */
11719 +
11720 +/* no one supports this operation, currently */
11721 +#if 0
11722 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
11723 +                            size_t len, loff_t *pos , int more)
11724 +{
11725 +}
11726 +#endif
11727 +
11728 +/* ---------------------------------------------------------------------- */
11729 +
11730 +const struct file_operations aufs_file_fop = {
11731 +       .owner          = THIS_MODULE,
11732 +
11733 +       .llseek         = default_llseek,
11734 +
11735 +       .read           = aufs_read,
11736 +       .write          = aufs_write,
11737 +       .aio_read       = aufs_aio_read,
11738 +       .aio_write      = aufs_aio_write,
11739 +#ifdef CONFIG_AUFS_POLL
11740 +       .poll           = aufs_poll,
11741 +#endif
11742 +       .unlocked_ioctl = aufs_ioctl_nondir,
11743 +#ifdef CONFIG_COMPAT
11744 +       .compat_ioctl   = aufs_ioctl_nondir, /* same */
11745 +#endif
11746 +       .mmap           = aufs_mmap,
11747 +       .open           = aufs_open_nondir,
11748 +       .flush          = aufs_flush_nondir,
11749 +       .release        = aufs_release_nondir,
11750 +       .fsync          = aufs_fsync_nondir,
11751 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
11752 +       .fasync         = aufs_fasync,
11753 +       /* .sendpage    = aufs_sendpage, */
11754 +       .splice_write   = aufs_splice_write,
11755 +       .splice_read    = aufs_splice_read,
11756 +#if 0
11757 +       .aio_splice_write = aufs_aio_splice_write,
11758 +       .aio_splice_read  = aufs_aio_splice_read
11759 +#endif
11760 +};
11761 diff -urN /usr/share/empty/fs/aufs/f_op_sp.c linux/fs/aufs/f_op_sp.c
11762 --- /usr/share/empty/fs/aufs/f_op_sp.c  1970-01-01 01:00:00.000000000 +0100
11763 +++ linux/fs/aufs/f_op_sp.c     2012-03-19 12:24:13.679801090 +0100
11764 @@ -0,0 +1,298 @@
11765 +/*
11766 + * Copyright (C) 2005-2012 Junjiro R. Okajima
11767 + *
11768 + * This program, aufs is free software; you can redistribute it and/or modify
11769 + * it under the terms of the GNU General Public License as published by
11770 + * the Free Software Foundation; either version 2 of the License, or
11771 + * (at your option) any later version.
11772 + *
11773 + * This program is distributed in the hope that it will be useful,
11774 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11775 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11776 + * GNU General Public License for more details.
11777 + *
11778 + * You should have received a copy of the GNU General Public License
11779 + * along with this program; if not, write to the Free Software
11780 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11781 + */
11782 +
11783 +/*
11784 + * file operations for special files.
11785 + * while they exist in aufs virtually,
11786 + * their file I/O is handled out of aufs.
11787 + */
11788 +
11789 +#include "aufs.h"
11790 +
11791 +static ssize_t aufs_aio_read_sp(struct kiocb *kio, const struct iovec *iov,
11792 +                               unsigned long nv, loff_t pos)
11793 +{
11794 +       ssize_t err;
11795 +       aufs_bindex_t bstart;
11796 +       unsigned char wbr;
11797 +       struct file *file, *h_file;
11798 +       struct super_block *sb;
11799 +
11800 +       file = kio->ki_filp;
11801 +       sb = file->f_dentry->d_sb;
11802 +       si_read_lock(sb, AuLock_FLUSH);
11803 +       fi_read_lock(file);
11804 +       bstart = au_fbstart(file);
11805 +       h_file = au_hf_top(file);
11806 +       fi_read_unlock(file);
11807 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
11808 +       si_read_unlock(sb);
11809 +
11810 +       /* do not change the file in kio */
11811 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_read);
11812 +       err = h_file->f_op->aio_read(kio, iov, nv, pos);
11813 +       if (err > 0 && wbr)
11814 +               file_accessed(h_file);
11815 +
11816 +       return err;
11817 +}
11818 +
11819 +static ssize_t aufs_aio_write_sp(struct kiocb *kio, const struct iovec *iov,
11820 +                                unsigned long nv, loff_t pos)
11821 +{
11822 +       ssize_t err;
11823 +       aufs_bindex_t bstart;
11824 +       unsigned char wbr;
11825 +       struct super_block *sb;
11826 +       struct file *file, *h_file;
11827 +
11828 +       file = kio->ki_filp;
11829 +       sb = file->f_dentry->d_sb;
11830 +       si_read_lock(sb, AuLock_FLUSH);
11831 +       fi_read_lock(file);
11832 +       bstart = au_fbstart(file);
11833 +       h_file = au_hf_top(file);
11834 +       fi_read_unlock(file);
11835 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
11836 +       si_read_unlock(sb);
11837 +
11838 +       /* do not change the file in kio */
11839 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_write);
11840 +       err = h_file->f_op->aio_write(kio, iov, nv, pos);
11841 +       if (err > 0 && wbr)
11842 +               file_update_time(h_file);
11843 +
11844 +       return err;
11845 +}
11846 +
11847 +/* ---------------------------------------------------------------------- */
11848 +
11849 +static int aufs_release_sp(struct inode *inode, struct file *file)
11850 +{
11851 +       int err;
11852 +       struct file *h_file;
11853 +
11854 +       fi_read_lock(file);
11855 +       h_file = au_hf_top(file);
11856 +       fi_read_unlock(file);
11857 +       /* close this fifo in aufs */
11858 +       err = h_file->f_op->release(inode, file); /* ignore */
11859 +       aufs_release_nondir(inode, file); /* ignore */
11860 +       return err;
11861 +}
11862 +
11863 +/* ---------------------------------------------------------------------- */
11864 +
11865 +/* currently, support only FIFO */
11866 +enum {
11867 +       AuSp_FIFO, AuSp_FIFO_R, AuSp_FIFO_W, AuSp_FIFO_RW,
11868 +       /* AuSp_SOCK, AuSp_CHR, AuSp_BLK, */
11869 +       AuSp_Last
11870 +};
11871 +static int aufs_open_sp(struct inode *inode, struct file *file);
11872 +static struct au_sp_fop {
11873 +       int                     done;
11874 +       struct file_operations  fop;    /* not 'const' */
11875 +       spinlock_t              spin;
11876 +} au_sp_fop[AuSp_Last] = {
11877 +       [AuSp_FIFO] = {
11878 +               .fop    = {
11879 +                       .owner  = THIS_MODULE,
11880 +                       .open   = aufs_open_sp
11881 +               }
11882 +       }
11883 +};
11884 +
11885 +static void au_init_fop_sp(struct file *file)
11886 +{
11887 +       struct au_sp_fop *p;
11888 +       int i;
11889 +       struct file *h_file;
11890 +
11891 +       p = au_sp_fop;
11892 +       if (unlikely(!p->done)) {
11893 +               /* initialize first time only */
11894 +               static DEFINE_SPINLOCK(spin);
11895 +
11896 +               spin_lock(&spin);
11897 +               if (!p->done) {
11898 +                       BUILD_BUG_ON(sizeof(au_sp_fop)/sizeof(*au_sp_fop)
11899 +                                    != AuSp_Last);
11900 +                       for (i = 0; i < AuSp_Last; i++)
11901 +                               spin_lock_init(&p[i].spin);
11902 +                       p->done = 1;
11903 +               }
11904 +               spin_unlock(&spin);
11905 +       }
11906 +
11907 +       switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
11908 +       case FMODE_READ:
11909 +               i = AuSp_FIFO_R;
11910 +               break;
11911 +       case FMODE_WRITE:
11912 +               i = AuSp_FIFO_W;
11913 +               break;
11914 +       case FMODE_READ | FMODE_WRITE:
11915 +               i = AuSp_FIFO_RW;
11916 +               break;
11917 +       default:
11918 +               BUG();
11919 +       }
11920 +
11921 +       p += i;
11922 +       if (unlikely(!p->done)) {
11923 +               /* initialize first time only */
11924 +               h_file = au_hf_top(file);
11925 +               spin_lock(&p->spin);
11926 +               if (!p->done) {
11927 +                       p->fop = *h_file->f_op;
11928 +                       p->fop.owner = THIS_MODULE;
11929 +                       if (p->fop.aio_read)
11930 +                               p->fop.aio_read = aufs_aio_read_sp;
11931 +                       if (p->fop.aio_write)
11932 +                               p->fop.aio_write = aufs_aio_write_sp;
11933 +                       p->fop.release = aufs_release_sp;
11934 +                       p->done = 1;
11935 +               }
11936 +               spin_unlock(&p->spin);
11937 +       }
11938 +       file->f_op = &p->fop;
11939 +}
11940 +
11941 +static int au_cpup_sp(struct dentry *dentry)
11942 +{
11943 +       int err;
11944 +       aufs_bindex_t bcpup;
11945 +       struct au_pin pin;
11946 +       struct au_wr_dir_args wr_dir_args = {
11947 +               .force_btgt     = -1,
11948 +               .flags          = 0
11949 +       };
11950 +
11951 +       AuDbg("%.*s\n", AuDLNPair(dentry));
11952 +
11953 +       di_read_unlock(dentry, AuLock_IR);
11954 +       di_write_lock_child(dentry);
11955 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
11956 +       if (unlikely(err < 0))
11957 +               goto out;
11958 +       bcpup = err;
11959 +       err = 0;
11960 +       if (bcpup == au_dbstart(dentry))
11961 +               goto out; /* success */
11962 +
11963 +       err = au_pin(&pin, dentry, bcpup, au_opt_udba(dentry->d_sb),
11964 +                    AuPin_MNT_WRITE);
11965 +       if (!err) {
11966 +               err = au_sio_cpup_simple(dentry, bcpup, -1, AuCpup_DTIME);
11967 +               au_unpin(&pin);
11968 +       }
11969 +
11970 +out:
11971 +       di_downgrade_lock(dentry, AuLock_IR);
11972 +       return err;
11973 +}
11974 +
11975 +static int au_do_open_sp(struct file *file, int flags)
11976 +{
11977 +       int err;
11978 +       struct dentry *dentry;
11979 +       struct super_block *sb;
11980 +       struct file *h_file;
11981 +       struct inode *h_inode;
11982 +
11983 +       dentry = file->f_dentry;
11984 +       AuDbg("%.*s\n", AuDLNPair(dentry));
11985 +
11986 +       /*
11987 +        * try copying-up.
11988 +        * operate on the ro branch is not an error.
11989 +        */
11990 +       au_cpup_sp(dentry); /* ignore */
11991 +
11992 +       /* prepare h_file */
11993 +       err = au_do_open_nondir(file, vfsub_file_flags(file));
11994 +       if (unlikely(err))
11995 +               goto out;
11996 +
11997 +       sb = dentry->d_sb;
11998 +       h_file = au_hf_top(file);
11999 +       h_inode = h_file->f_dentry->d_inode;
12000 +       di_read_unlock(dentry, AuLock_IR);
12001 +       fi_write_unlock(file);
12002 +       si_read_unlock(sb);
12003 +       /* open this fifo in aufs */
12004 +       err = h_inode->i_fop->open(file->f_dentry->d_inode, file);
12005 +       si_noflush_read_lock(sb);
12006 +       fi_write_lock(file);
12007 +       di_read_lock_child(dentry, AuLock_IR);
12008 +       if (!err)
12009 +               au_init_fop_sp(file);
12010 +
12011 +out:
12012 +       return err;
12013 +}
12014 +
12015 +static int aufs_open_sp(struct inode *inode, struct file *file)
12016 +{
12017 +       int err;
12018 +       struct super_block *sb;
12019 +
12020 +       sb = file->f_dentry->d_sb;
12021 +       si_read_lock(sb, AuLock_FLUSH);
12022 +       err = au_do_open(file, au_do_open_sp, /*fidir*/NULL);
12023 +       si_read_unlock(sb);
12024 +       return err;
12025 +}
12026 +
12027 +/* ---------------------------------------------------------------------- */
12028 +
12029 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev)
12030 +{
12031 +       init_special_inode(inode, mode, rdev);
12032 +
12033 +       switch (mode & S_IFMT) {
12034 +       case S_IFIFO:
12035 +               inode->i_fop = &au_sp_fop[AuSp_FIFO].fop;
12036 +               /*FALLTHROUGH*/
12037 +       case S_IFCHR:
12038 +       case S_IFBLK:
12039 +       case S_IFSOCK:
12040 +               break;
12041 +       default:
12042 +               AuDebugOn(1);
12043 +       }
12044 +}
12045 +
12046 +int au_special_file(umode_t mode)
12047 +{
12048 +       int ret;
12049 +
12050 +       ret = 0;
12051 +       switch (mode & S_IFMT) {
12052 +       case S_IFIFO:
12053 +#if 0
12054 +       case S_IFCHR:
12055 +       case S_IFBLK:
12056 +       case S_IFSOCK:
12057 +#endif
12058 +               ret = 1;
12059 +       }
12060 +
12061 +       return ret;
12062 +}
12063 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
12064 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
12065 +++ linux/fs/aufs/fstype.h      2012-03-19 12:24:13.683134508 +0100
12066 @@ -0,0 +1,496 @@
12067 +/*
12068 + * Copyright (C) 2005-2012 Junjiro R. Okajima
12069 + *
12070 + * This program, aufs is free software; you can redistribute it and/or modify
12071 + * it under the terms of the GNU General Public License as published by
12072 + * the Free Software Foundation; either version 2 of the License, or
12073 + * (at your option) any later version.
12074 + *
12075 + * This program is distributed in the hope that it will be useful,
12076 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12077 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12078 + * GNU General Public License for more details.
12079 + *
12080 + * You should have received a copy of the GNU General Public License
12081 + * along with this program; if not, write to the Free Software
12082 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12083 + */
12084 +
12085 +/*
12086 + * judging filesystem type
12087 + */
12088 +
12089 +#ifndef __AUFS_FSTYPE_H__
12090 +#define __AUFS_FSTYPE_H__
12091 +
12092 +#ifdef __KERNEL__
12093 +
12094 +#include <linux/fs.h>
12095 +#include <linux/magic.h>
12096 +#include <linux/romfs_fs.h>
12097 +
12098 +static inline int au_test_aufs(struct super_block *sb)
12099 +{
12100 +       return sb->s_magic == AUFS_SUPER_MAGIC;
12101 +}
12102 +
12103 +static inline const char *au_sbtype(struct super_block *sb)
12104 +{
12105 +       return sb->s_type->name;
12106 +}
12107 +
12108 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
12109 +{
12110 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
12111 +       return sb->s_magic == ROMFS_MAGIC;
12112 +#else
12113 +       return 0;
12114 +#endif
12115 +}
12116 +
12117 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
12118 +{
12119 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
12120 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
12121 +#else
12122 +       return 0;
12123 +#endif
12124 +}
12125 +
12126 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
12127 +{
12128 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
12129 +       return sb->s_magic == CRAMFS_MAGIC;
12130 +#endif
12131 +       return 0;
12132 +}
12133 +
12134 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
12135 +{
12136 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
12137 +       return sb->s_magic == NFS_SUPER_MAGIC;
12138 +#else
12139 +       return 0;
12140 +#endif
12141 +}
12142 +
12143 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
12144 +{
12145 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
12146 +       return sb->s_magic == FUSE_SUPER_MAGIC;
12147 +#else
12148 +       return 0;
12149 +#endif
12150 +}
12151 +
12152 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
12153 +{
12154 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
12155 +       return sb->s_magic == XFS_SB_MAGIC;
12156 +#else
12157 +       return 0;
12158 +#endif
12159 +}
12160 +
12161 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
12162 +{
12163 +#ifdef CONFIG_TMPFS
12164 +       return sb->s_magic == TMPFS_MAGIC;
12165 +#else
12166 +       return 0;
12167 +#endif
12168 +}
12169 +
12170 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
12171 +{
12172 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
12173 +       return !strcmp(au_sbtype(sb), "ecryptfs");
12174 +#else
12175 +       return 0;
12176 +#endif
12177 +}
12178 +
12179 +static inline int au_test_smbfs(struct super_block *sb __maybe_unused)
12180 +{
12181 +#if defined(CONFIG_SMB_FS) || defined(CONFIG_SMB_FS_MODULE)
12182 +       return sb->s_magic == SMB_SUPER_MAGIC;
12183 +#else
12184 +       return 0;
12185 +#endif
12186 +}
12187 +
12188 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
12189 +{
12190 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
12191 +       return sb->s_magic == OCFS2_SUPER_MAGIC;
12192 +#else
12193 +       return 0;
12194 +#endif
12195 +}
12196 +
12197 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
12198 +{
12199 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
12200 +       return sb->s_magic == DLMFS_MAGIC;
12201 +#else
12202 +       return 0;
12203 +#endif
12204 +}
12205 +
12206 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
12207 +{
12208 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
12209 +       return sb->s_magic == CODA_SUPER_MAGIC;
12210 +#else
12211 +       return 0;
12212 +#endif
12213 +}
12214 +
12215 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
12216 +{
12217 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
12218 +       return sb->s_magic == V9FS_MAGIC;
12219 +#else
12220 +       return 0;
12221 +#endif
12222 +}
12223 +
12224 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
12225 +{
12226 +#if defined(CONFIG_EXT4DEV_FS) || defined(CONFIG_EXT4DEV_FS_MODULE)
12227 +       return sb->s_magic == EXT4_SUPER_MAGIC;
12228 +#else
12229 +       return 0;
12230 +#endif
12231 +}
12232 +
12233 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
12234 +{
12235 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
12236 +       return !strcmp(au_sbtype(sb), "sysv");
12237 +#else
12238 +       return 0;
12239 +#endif
12240 +}
12241 +
12242 +static inline int au_test_ramfs(struct super_block *sb)
12243 +{
12244 +       return sb->s_magic == RAMFS_MAGIC;
12245 +}
12246 +
12247 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
12248 +{
12249 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
12250 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
12251 +#else
12252 +       return 0;
12253 +#endif
12254 +}
12255 +
12256 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
12257 +{
12258 +#ifdef CONFIG_PROC_FS
12259 +       return sb->s_magic == PROC_SUPER_MAGIC;
12260 +#else
12261 +       return 0;
12262 +#endif
12263 +}
12264 +
12265 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
12266 +{
12267 +#ifdef CONFIG_SYSFS
12268 +       return sb->s_magic == SYSFS_MAGIC;
12269 +#else
12270 +       return 0;
12271 +#endif
12272 +}
12273 +
12274 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
12275 +{
12276 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
12277 +       return sb->s_magic == CONFIGFS_MAGIC;
12278 +#else
12279 +       return 0;
12280 +#endif
12281 +}
12282 +
12283 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
12284 +{
12285 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
12286 +       return sb->s_magic == MINIX3_SUPER_MAGIC
12287 +               || sb->s_magic == MINIX2_SUPER_MAGIC
12288 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
12289 +               || sb->s_magic == MINIX_SUPER_MAGIC
12290 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
12291 +#else
12292 +       return 0;
12293 +#endif
12294 +}
12295 +
12296 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
12297 +{
12298 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
12299 +       return sb->s_magic == CIFS_MAGIC_NUMBER;
12300 +#else
12301 +       return 0;
12302 +#endif
12303 +}
12304 +
12305 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
12306 +{
12307 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
12308 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
12309 +#else
12310 +       return 0;
12311 +#endif
12312 +}
12313 +
12314 +static inline int au_test_msdos(struct super_block *sb)
12315 +{
12316 +       return au_test_fat(sb);
12317 +}
12318 +
12319 +static inline int au_test_vfat(struct super_block *sb)
12320 +{
12321 +       return au_test_fat(sb);
12322 +}
12323 +
12324 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
12325 +{
12326 +#ifdef CONFIG_SECURITYFS
12327 +       return sb->s_magic == SECURITYFS_MAGIC;
12328 +#else
12329 +       return 0;
12330 +#endif
12331 +}
12332 +
12333 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
12334 +{
12335 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
12336 +       return sb->s_magic == SQUASHFS_MAGIC;
12337 +#else
12338 +       return 0;
12339 +#endif
12340 +}
12341 +
12342 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
12343 +{
12344 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
12345 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
12346 +#else
12347 +       return 0;
12348 +#endif
12349 +}
12350 +
12351 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
12352 +{
12353 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
12354 +       return sb->s_magic == XENFS_SUPER_MAGIC;
12355 +#else
12356 +       return 0;
12357 +#endif
12358 +}
12359 +
12360 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
12361 +{
12362 +#ifdef CONFIG_DEBUG_FS
12363 +       return sb->s_magic == DEBUGFS_MAGIC;
12364 +#else
12365 +       return 0;
12366 +#endif
12367 +}
12368 +
12369 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
12370 +{
12371 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
12372 +       return sb->s_magic == NILFS_SUPER_MAGIC;
12373 +#else
12374 +       return 0;
12375 +#endif
12376 +}
12377 +
12378 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
12379 +{
12380 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
12381 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
12382 +#else
12383 +       return 0;
12384 +#endif
12385 +}
12386 +
12387 +/* ---------------------------------------------------------------------- */
12388 +/*
12389 + * they can't be an aufs branch.
12390 + */
12391 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
12392 +{
12393 +       return
12394 +#ifndef CONFIG_AUFS_BR_RAMFS
12395 +               au_test_ramfs(sb) ||
12396 +#endif
12397 +               au_test_procfs(sb)
12398 +               || au_test_sysfs(sb)
12399 +               || au_test_configfs(sb)
12400 +               || au_test_debugfs(sb)
12401 +               || au_test_securityfs(sb)
12402 +               || au_test_xenfs(sb)
12403 +               || au_test_ecryptfs(sb)
12404 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
12405 +               || au_test_aufs(sb); /* will be supported in next version */
12406 +}
12407 +
12408 +/*
12409 + * If the filesystem supports NFS-export, then it has to support NULL as
12410 + * a nameidata parameter for ->create(), ->lookup() and ->d_revalidate().
12411 + * We can apply this principle when we handle a lower filesystem.
12412 + */
12413 +static inline int au_test_fs_null_nd(struct super_block *sb)
12414 +{
12415 +       return !!sb->s_export_op;
12416 +}
12417 +
12418 +static inline int au_test_fs_remote(struct super_block *sb)
12419 +{
12420 +       return !au_test_tmpfs(sb)
12421 +#ifdef CONFIG_AUFS_BR_RAMFS
12422 +               && !au_test_ramfs(sb)
12423 +#endif
12424 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
12425 +}
12426 +
12427 +/* ---------------------------------------------------------------------- */
12428 +
12429 +/*
12430 + * Note: these functions (below) are created after reading ->getattr() in all
12431 + * filesystems under linux/fs. it means we have to do so in every update...
12432 + */
12433 +
12434 +/*
12435 + * some filesystems require getattr to refresh the inode attributes before
12436 + * referencing.
12437 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
12438 + * and leave the work for d_revalidate()
12439 + */
12440 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
12441 +{
12442 +       return au_test_nfs(sb)
12443 +               || au_test_fuse(sb)
12444 +               /* || au_test_smbfs(sb) */      /* untested */
12445 +               /* || au_test_ocfs2(sb) */      /* untested */
12446 +               /* || au_test_btrfs(sb) */      /* untested */
12447 +               /* || au_test_coda(sb) */       /* untested */
12448 +               /* || au_test_v9fs(sb) */       /* untested */
12449 +               ;
12450 +}
12451 +
12452 +/*
12453 + * filesystems which don't maintain i_size or i_blocks.
12454 + */
12455 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
12456 +{
12457 +       return au_test_xfs(sb)
12458 +               || au_test_btrfs(sb)
12459 +               || au_test_ubifs(sb)
12460 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
12461 +               /* || au_test_ext4(sb) */       /* untested */
12462 +               /* || au_test_ocfs2(sb) */      /* untested */
12463 +               /* || au_test_ocfs2_dlmfs(sb) */ /* untested */
12464 +               /* || au_test_sysv(sb) */       /* untested */
12465 +               /* || au_test_minix(sb) */      /* untested */
12466 +               ;
12467 +}
12468 +
12469 +/*
12470 + * filesystems which don't store the correct value in some of their inode
12471 + * attributes.
12472 + */
12473 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
12474 +{
12475 +       return au_test_fs_bad_iattr_size(sb)
12476 +               /* || au_test_cifs(sb) */       /* untested */
12477 +               || au_test_fat(sb)
12478 +               || au_test_msdos(sb)
12479 +               || au_test_vfat(sb);
12480 +}
12481 +
12482 +/* they don't check i_nlink in link(2) */
12483 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
12484 +{
12485 +       return au_test_tmpfs(sb)
12486 +#ifdef CONFIG_AUFS_BR_RAMFS
12487 +               || au_test_ramfs(sb)
12488 +#endif
12489 +               || au_test_ubifs(sb)
12490 +               || au_test_btrfs(sb)
12491 +               || au_test_hfsplus(sb);
12492 +}
12493 +
12494 +/*
12495 + * filesystems which sets S_NOATIME and S_NOCMTIME.
12496 + */
12497 +static inline int au_test_fs_notime(struct super_block *sb)
12498 +{
12499 +       return au_test_nfs(sb)
12500 +               || au_test_fuse(sb)
12501 +               || au_test_ubifs(sb)
12502 +               /* || au_test_cifs(sb) */       /* untested */
12503 +               ;
12504 +}
12505 +
12506 +/*
12507 + * filesystems which requires replacing i_mapping.
12508 + */
12509 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
12510 +{
12511 +       return au_test_fuse(sb)
12512 +               || au_test_ubifs(sb);
12513 +}
12514 +
12515 +/* temporary support for i#1 in cramfs */
12516 +static inline int au_test_fs_unique_ino(struct inode *inode)
12517 +{
12518 +       if (au_test_cramfs(inode->i_sb))
12519 +               return inode->i_ino != 1;
12520 +       return 1;
12521 +}
12522 +
12523 +/* ---------------------------------------------------------------------- */
12524 +
12525 +/*
12526 + * the filesystem where the xino files placed must support i/o after unlink and
12527 + * maintain i_size and i_blocks.
12528 + */
12529 +static inline int au_test_fs_bad_xino(struct super_block *sb)
12530 +{
12531 +       return au_test_fs_remote(sb)
12532 +               || au_test_fs_bad_iattr_size(sb)
12533 +#ifdef CONFIG_AUFS_BR_RAMFS
12534 +               || !(au_test_ramfs(sb) || au_test_fs_null_nd(sb))
12535 +#else
12536 +               || !au_test_fs_null_nd(sb) /* to keep xino code simple */
12537 +#endif
12538 +               /* don't want unnecessary work for xino */
12539 +               || au_test_aufs(sb)
12540 +               || au_test_ecryptfs(sb)
12541 +               || au_test_nilfs(sb);
12542 +}
12543 +
12544 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
12545 +{
12546 +       return au_test_tmpfs(sb)
12547 +               || au_test_ramfs(sb);
12548 +}
12549 +
12550 +/*
12551 + * test if the @sb is real-readonly.
12552 + */
12553 +static inline int au_test_fs_rr(struct super_block *sb)
12554 +{
12555 +       return au_test_squashfs(sb)
12556 +               || au_test_iso9660(sb)
12557 +               || au_test_cramfs(sb)
12558 +               || au_test_romfs(sb);
12559 +}
12560 +
12561 +#endif /* __KERNEL__ */
12562 +#endif /* __AUFS_FSTYPE_H__ */
12563 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
12564 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
12565 +++ linux/fs/aufs/hfsnotify.c   2012-03-19 12:24:13.683134508 +0100
12566 @@ -0,0 +1,260 @@
12567 +/*
12568 + * Copyright (C) 2005-2012 Junjiro R. Okajima
12569 + *
12570 + * This program, aufs is free software; you can redistribute it and/or modify
12571 + * it under the terms of the GNU General Public License as published by
12572 + * the Free Software Foundation; either version 2 of the License, or
12573 + * (at your option) any later version.
12574 + *
12575 + * This program is distributed in the hope that it will be useful,
12576 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12577 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12578 + * GNU General Public License for more details.
12579 + *
12580 + * You should have received a copy of the GNU General Public License
12581 + * along with this program; if not, write to the Free Software
12582 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12583 + */
12584 +
12585 +/*
12586 + * fsnotify for the lower directories
12587 + */
12588 +
12589 +#include "aufs.h"
12590 +
12591 +/* FS_IN_IGNORED is unnecessary */
12592 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
12593 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
12594 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
12595 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
12596 +
12597 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
12598 +{
12599 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
12600 +                                            hn_mark);
12601 +       AuDbg("here\n");
12602 +       au_cache_free_hnotify(hn);
12603 +       smp_mb__before_atomic_dec();
12604 +       atomic64_dec(&au_hfsn_ifree);
12605 +       wake_up(&au_hfsn_wq);
12606 +}
12607 +
12608 +static int au_hfsn_alloc(struct au_hinode *hinode)
12609 +{
12610 +       struct au_hnotify *hn;
12611 +       struct super_block *sb;
12612 +       struct au_branch *br;
12613 +       struct fsnotify_mark *mark;
12614 +       aufs_bindex_t bindex;
12615 +
12616 +       hn = hinode->hi_notify;
12617 +       sb = hn->hn_aufs_inode->i_sb;
12618 +       bindex = au_br_index(sb, hinode->hi_id);
12619 +       br = au_sbr(sb, bindex);
12620 +       mark = &hn->hn_mark;
12621 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
12622 +       mark->mask = AuHfsnMask;
12623 +       /*
12624 +        * by udba rename or rmdir, aufs assign a new inode to the known
12625 +        * h_inode, so specify 1 to allow dups.
12626 +        */
12627 +       return fsnotify_add_mark(mark, br->br_hfsn_group, hinode->hi_inode,
12628 +                                /*mnt*/NULL, /*allow_dups*/1);
12629 +}
12630 +
12631 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
12632 +{
12633 +       struct fsnotify_mark *mark;
12634 +       unsigned long long ull;
12635 +
12636 +       ull = atomic64_inc_return(&au_hfsn_ifree);
12637 +       BUG_ON(!ull);
12638 +
12639 +       mark = &hn->hn_mark;
12640 +       fsnotify_destroy_mark(mark);
12641 +       fsnotify_put_mark(mark);
12642 +
12643 +       /* free hn by myself */
12644 +       return 0;
12645 +}
12646 +
12647 +/* ---------------------------------------------------------------------- */
12648 +
12649 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
12650 +{
12651 +       struct fsnotify_mark *mark;
12652 +
12653 +       mark = &hinode->hi_notify->hn_mark;
12654 +       spin_lock(&mark->lock);
12655 +       if (do_set) {
12656 +               AuDebugOn(mark->mask & AuHfsnMask);
12657 +               mark->mask |= AuHfsnMask;
12658 +       } else {
12659 +               AuDebugOn(!(mark->mask & AuHfsnMask));
12660 +               mark->mask &= ~AuHfsnMask;
12661 +       }
12662 +       spin_unlock(&mark->lock);
12663 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
12664 +}
12665 +
12666 +/* ---------------------------------------------------------------------- */
12667 +
12668 +/* #define AuDbgHnotify */
12669 +#ifdef AuDbgHnotify
12670 +static char *au_hfsn_name(u32 mask)
12671 +{
12672 +#ifdef CONFIG_AUFS_DEBUG
12673 +#define test_ret(flag) if (mask & flag) \
12674 +                               return #flag;
12675 +       test_ret(FS_ACCESS);
12676 +       test_ret(FS_MODIFY);
12677 +       test_ret(FS_ATTRIB);
12678 +       test_ret(FS_CLOSE_WRITE);
12679 +       test_ret(FS_CLOSE_NOWRITE);
12680 +       test_ret(FS_OPEN);
12681 +       test_ret(FS_MOVED_FROM);
12682 +       test_ret(FS_MOVED_TO);
12683 +       test_ret(FS_CREATE);
12684 +       test_ret(FS_DELETE);
12685 +       test_ret(FS_DELETE_SELF);
12686 +       test_ret(FS_MOVE_SELF);
12687 +       test_ret(FS_UNMOUNT);
12688 +       test_ret(FS_Q_OVERFLOW);
12689 +       test_ret(FS_IN_IGNORED);
12690 +       test_ret(FS_IN_ISDIR);
12691 +       test_ret(FS_IN_ONESHOT);
12692 +       test_ret(FS_EVENT_ON_CHILD);
12693 +       return "";
12694 +#undef test_ret
12695 +#else
12696 +       return "??";
12697 +#endif
12698 +}
12699 +#endif
12700 +
12701 +/* ---------------------------------------------------------------------- */
12702 +
12703 +static int au_hfsn_handle_event(struct fsnotify_group *group,
12704 +                               struct fsnotify_mark *inode_mark,
12705 +                               struct fsnotify_mark *vfsmount_mark,
12706 +                               struct fsnotify_event *event)
12707 +{
12708 +       int err;
12709 +       struct au_hnotify *hnotify;
12710 +       struct inode *h_dir, *h_inode;
12711 +       __u32 mask;
12712 +       struct qstr h_child_qstr = {
12713 +               .name   = event->file_name,
12714 +               .len    = event->name_len
12715 +       };
12716 +
12717 +       AuDebugOn(event->data_type != FSNOTIFY_EVENT_INODE);
12718 +
12719 +       err = 0;
12720 +       /* if FS_UNMOUNT happens, there must be another bug */
12721 +       mask = event->mask;
12722 +       AuDebugOn(mask & FS_UNMOUNT);
12723 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
12724 +               goto out;
12725 +
12726 +       h_dir = event->to_tell;
12727 +       h_inode = event->inode;
12728 +#ifdef AuDbgHnotify
12729 +       au_debug(1);
12730 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
12731 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
12732 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
12733 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
12734 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
12735 +               /* WARN_ON(1); */
12736 +       }
12737 +       au_debug(0);
12738 +#endif
12739 +
12740 +       AuDebugOn(!inode_mark);
12741 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
12742 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
12743 +
12744 +out:
12745 +       return err;
12746 +}
12747 +
12748 +/* isn't it waste to ask every registered 'group'? */
12749 +/* copied from linux/fs/notify/inotify/inotify_fsnotiry.c */
12750 +/* it should be exported to modules */
12751 +static bool au_hfsn_should_send_event(struct fsnotify_group *group,
12752 +                                     struct inode *h_inode,
12753 +                                     struct fsnotify_mark *inode_mark,
12754 +                                     struct fsnotify_mark *vfsmount_mark,
12755 +                                     __u32 mask, void *data, int data_type)
12756 +{
12757 +       mask = (mask & ~FS_EVENT_ON_CHILD);
12758 +       return inode_mark->mask & mask;
12759 +}
12760 +
12761 +static struct fsnotify_ops au_hfsn_ops = {
12762 +       .should_send_event      = au_hfsn_should_send_event,
12763 +       .handle_event           = au_hfsn_handle_event
12764 +};
12765 +
12766 +/* ---------------------------------------------------------------------- */
12767 +
12768 +static void au_hfsn_fin_br(struct au_branch *br)
12769 +{
12770 +       if (br->br_hfsn_group)
12771 +               fsnotify_put_group(br->br_hfsn_group);
12772 +}
12773 +
12774 +static int au_hfsn_init_br(struct au_branch *br, int perm)
12775 +{
12776 +       br->br_hfsn_group = NULL;
12777 +       br->br_hfsn_ops = au_hfsn_ops;
12778 +       return 0;
12779 +}
12780 +
12781 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
12782 +{
12783 +       int err;
12784 +
12785 +       err = 0;
12786 +       if (udba != AuOpt_UDBA_HNOTIFY
12787 +           || !au_br_hnotifyable(perm)) {
12788 +               au_hfsn_fin_br(br);
12789 +               br->br_hfsn_group = NULL;
12790 +               goto out;
12791 +       }
12792 +
12793 +       if (br->br_hfsn_group)
12794 +               goto out;
12795 +
12796 +       br->br_hfsn_group = fsnotify_alloc_group(&br->br_hfsn_ops);
12797 +       if (IS_ERR(br->br_hfsn_group)) {
12798 +               err = PTR_ERR(br->br_hfsn_group);
12799 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
12800 +               br->br_hfsn_group = NULL;
12801 +       }
12802 +
12803 +out:
12804 +       AuTraceErr(err);
12805 +       return err;
12806 +}
12807 +
12808 +/* ---------------------------------------------------------------------- */
12809 +
12810 +static void au_hfsn_fin(void)
12811 +{
12812 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
12813 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
12814 +}
12815 +
12816 +const struct au_hnotify_op au_hnotify_op = {
12817 +       .ctl            = au_hfsn_ctl,
12818 +       .alloc          = au_hfsn_alloc,
12819 +       .free           = au_hfsn_free,
12820 +
12821 +       .fin            = au_hfsn_fin,
12822 +
12823 +       .reset_br       = au_hfsn_reset_br,
12824 +       .fin_br         = au_hfsn_fin_br,
12825 +       .init_br        = au_hfsn_init_br
12826 +};
12827 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
12828 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
12829 +++ linux/fs/aufs/hfsplus.c     2012-03-19 12:24:13.683134508 +0100
12830 @@ -0,0 +1,57 @@
12831 +/*
12832 + * Copyright (C) 2010-2012 Junjiro R. Okajima
12833 + *
12834 + * This program, aufs is free software; you can redistribute it and/or modify
12835 + * it under the terms of the GNU General Public License as published by
12836 + * the Free Software Foundation; either version 2 of the License, or
12837 + * (at your option) any later version.
12838 + *
12839 + * This program is distributed in the hope that it will be useful,
12840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12842 + * GNU General Public License for more details.
12843 + *
12844 + * You should have received a copy of the GNU General Public License
12845 + * along with this program; if not, write to the Free Software
12846 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12847 + */
12848 +
12849 +/*
12850 + * special support for filesystems which aqucires an inode mutex
12851 + * at final closing a file, eg, hfsplus.
12852 + *
12853 + * This trick is very simple and stupid, just to open the file before really
12854 + * neceeary open to tell hfsplus that this is not the final closing.
12855 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
12856 + * and au_h_open_post() after releasing it.
12857 + */
12858 +
12859 +#include "aufs.h"
12860 +
12861 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
12862 +{
12863 +       struct file *h_file;
12864 +       struct dentry *h_dentry;
12865 +
12866 +       h_dentry = au_h_dptr(dentry, bindex);
12867 +       AuDebugOn(!h_dentry);
12868 +       AuDebugOn(!h_dentry->d_inode);
12869 +       IMustLock(h_dentry->d_inode);
12870 +
12871 +       h_file = NULL;
12872 +       if (au_test_hfsplus(h_dentry->d_sb)
12873 +           && S_ISREG(h_dentry->d_inode->i_mode))
12874 +               h_file = au_h_open(dentry, bindex,
12875 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
12876 +                                  /*file*/NULL);
12877 +       return h_file;
12878 +}
12879 +
12880 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
12881 +                   struct file *h_file)
12882 +{
12883 +       if (h_file) {
12884 +               fput(h_file);
12885 +               au_sbr_put(dentry->d_sb, bindex);
12886 +       }
12887 +}
12888 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
12889 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
12890 +++ linux/fs/aufs/hnotify.c     2012-03-19 12:24:13.683134508 +0100
12891 @@ -0,0 +1,712 @@
12892 +/*
12893 + * Copyright (C) 2005-2012 Junjiro R. Okajima
12894 + *
12895 + * This program, aufs is free software; you can redistribute it and/or modify
12896 + * it under the terms of the GNU General Public License as published by
12897 + * the Free Software Foundation; either version 2 of the License, or
12898 + * (at your option) any later version.
12899 + *
12900 + * This program is distributed in the hope that it will be useful,
12901 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12902 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12903 + * GNU General Public License for more details.
12904 + *
12905 + * You should have received a copy of the GNU General Public License
12906 + * along with this program; if not, write to the Free Software
12907 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12908 + */
12909 +
12910 +/*
12911 + * abstraction to notify the direct changes on lower directories
12912 + */
12913 +
12914 +#include "aufs.h"
12915 +
12916 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
12917 +{
12918 +       int err;
12919 +       struct au_hnotify *hn;
12920 +
12921 +       err = -ENOMEM;
12922 +       hn = au_cache_alloc_hnotify();
12923 +       if (hn) {
12924 +               hn->hn_aufs_inode = inode;
12925 +               hinode->hi_notify = hn;
12926 +               err = au_hnotify_op.alloc(hinode);
12927 +               AuTraceErr(err);
12928 +               if (unlikely(err)) {
12929 +                       hinode->hi_notify = NULL;
12930 +                       au_cache_free_hnotify(hn);
12931 +                       /*
12932 +                        * The upper dir was removed by udba, but the same named
12933 +                        * dir left. In this case, aufs assignes a new inode
12934 +                        * number and set the monitor again.
12935 +                        * For the lower dir, the old monitnor is still left.
12936 +                        */
12937 +                       if (err == -EEXIST)
12938 +                               err = 0;
12939 +               }
12940 +       }
12941 +
12942 +       AuTraceErr(err);
12943 +       return err;
12944 +}
12945 +
12946 +void au_hn_free(struct au_hinode *hinode)
12947 +{
12948 +       struct au_hnotify *hn;
12949 +
12950 +       hn = hinode->hi_notify;
12951 +       if (hn) {
12952 +               hinode->hi_notify = NULL;
12953 +               if (au_hnotify_op.free(hinode, hn))
12954 +                       au_cache_free_hnotify(hn);
12955 +       }
12956 +}
12957 +
12958 +/* ---------------------------------------------------------------------- */
12959 +
12960 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
12961 +{
12962 +       if (hinode->hi_notify)
12963 +               au_hnotify_op.ctl(hinode, do_set);
12964 +}
12965 +
12966 +void au_hn_reset(struct inode *inode, unsigned int flags)
12967 +{
12968 +       aufs_bindex_t bindex, bend;
12969 +       struct inode *hi;
12970 +       struct dentry *iwhdentry;
12971 +
12972 +       bend = au_ibend(inode);
12973 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
12974 +               hi = au_h_iptr(inode, bindex);
12975 +               if (!hi)
12976 +                       continue;
12977 +
12978 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
12979 +               iwhdentry = au_hi_wh(inode, bindex);
12980 +               if (iwhdentry)
12981 +                       dget(iwhdentry);
12982 +               au_igrab(hi);
12983 +               au_set_h_iptr(inode, bindex, NULL, 0);
12984 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
12985 +                             flags & ~AuHi_XINO);
12986 +               iput(hi);
12987 +               dput(iwhdentry);
12988 +               /* mutex_unlock(&hi->i_mutex); */
12989 +       }
12990 +}
12991 +
12992 +/* ---------------------------------------------------------------------- */
12993 +
12994 +static int hn_xino(struct inode *inode, struct inode *h_inode)
12995 +{
12996 +       int err;
12997 +       aufs_bindex_t bindex, bend, bfound, bstart;
12998 +       struct inode *h_i;
12999 +
13000 +       err = 0;
13001 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13002 +               pr_warning("branch root dir was changed\n");
13003 +               goto out;
13004 +       }
13005 +
13006 +       bfound = -1;
13007 +       bend = au_ibend(inode);
13008 +       bstart = au_ibstart(inode);
13009 +#if 0 /* reserved for future use */
13010 +       if (bindex == bend) {
13011 +               /* keep this ino in rename case */
13012 +               goto out;
13013 +       }
13014 +#endif
13015 +       for (bindex = bstart; bindex <= bend; bindex++)
13016 +               if (au_h_iptr(inode, bindex) == h_inode) {
13017 +                       bfound = bindex;
13018 +                       break;
13019 +               }
13020 +       if (bfound < 0)
13021 +               goto out;
13022 +
13023 +       for (bindex = bstart; bindex <= bend; bindex++) {
13024 +               h_i = au_h_iptr(inode, bindex);
13025 +               if (!h_i)
13026 +                       continue;
13027 +
13028 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
13029 +               /* ignore this error */
13030 +               /* bad action? */
13031 +       }
13032 +
13033 +       /* children inode number will be broken */
13034 +
13035 +out:
13036 +       AuTraceErr(err);
13037 +       return err;
13038 +}
13039 +
13040 +static int hn_gen_tree(struct dentry *dentry)
13041 +{
13042 +       int err, i, j, ndentry;
13043 +       struct au_dcsub_pages dpages;
13044 +       struct au_dpage *dpage;
13045 +       struct dentry **dentries;
13046 +
13047 +       err = au_dpages_init(&dpages, GFP_NOFS);
13048 +       if (unlikely(err))
13049 +               goto out;
13050 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
13051 +       if (unlikely(err))
13052 +               goto out_dpages;
13053 +
13054 +       for (i = 0; i < dpages.ndpage; i++) {
13055 +               dpage = dpages.dpages + i;
13056 +               dentries = dpage->dentries;
13057 +               ndentry = dpage->ndentry;
13058 +               for (j = 0; j < ndentry; j++) {
13059 +                       struct dentry *d;
13060 +
13061 +                       d = dentries[j];
13062 +                       if (IS_ROOT(d))
13063 +                               continue;
13064 +
13065 +                       au_digen_dec(d);
13066 +                       if (d->d_inode)
13067 +                               /* todo: reset children xino?
13068 +                                  cached children only? */
13069 +                               au_iigen_dec(d->d_inode);
13070 +               }
13071 +       }
13072 +
13073 +out_dpages:
13074 +       au_dpages_free(&dpages);
13075 +
13076 +#if 0
13077 +       /* discard children */
13078 +       dentry_unhash(dentry);
13079 +       dput(dentry);
13080 +#endif
13081 +out:
13082 +       return err;
13083 +}
13084 +
13085 +/*
13086 + * return 0 if processed.
13087 + */
13088 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
13089 +                          const unsigned int isdir)
13090 +{
13091 +       int err;
13092 +       struct dentry *d;
13093 +       struct qstr *dname;
13094 +
13095 +       err = 1;
13096 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13097 +               pr_warning("branch root dir was changed\n");
13098 +               err = 0;
13099 +               goto out;
13100 +       }
13101 +
13102 +       if (!isdir) {
13103 +               AuDebugOn(!name);
13104 +               au_iigen_dec(inode);
13105 +               spin_lock(&inode->i_lock);
13106 +               list_for_each_entry(d, &inode->i_dentry, d_alias) {
13107 +                       spin_lock(&d->d_lock);
13108 +                       dname = &d->d_name;
13109 +                       if (dname->len != nlen
13110 +                           && memcmp(dname->name, name, nlen)) {
13111 +                               spin_unlock(&d->d_lock);
13112 +                               continue;
13113 +                       }
13114 +                       err = 0;
13115 +                       au_digen_dec(d);
13116 +                       spin_unlock(&d->d_lock);
13117 +                       break;
13118 +               }
13119 +               spin_unlock(&inode->i_lock);
13120 +       } else {
13121 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
13122 +               d = d_find_alias(inode);
13123 +               if (!d) {
13124 +                       au_iigen_dec(inode);
13125 +                       goto out;
13126 +               }
13127 +
13128 +               spin_lock(&d->d_lock);
13129 +               dname = &d->d_name;
13130 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
13131 +                       spin_unlock(&d->d_lock);
13132 +                       err = hn_gen_tree(d);
13133 +                       spin_lock(&d->d_lock);
13134 +               }
13135 +               spin_unlock(&d->d_lock);
13136 +               dput(d);
13137 +       }
13138 +
13139 +out:
13140 +       AuTraceErr(err);
13141 +       return err;
13142 +}
13143 +
13144 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
13145 +{
13146 +       int err;
13147 +       struct inode *inode;
13148 +
13149 +       inode = dentry->d_inode;
13150 +       if (IS_ROOT(dentry)
13151 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
13152 +               ) {
13153 +               pr_warning("branch root dir was changed\n");
13154 +               return 0;
13155 +       }
13156 +
13157 +       err = 0;
13158 +       if (!isdir) {
13159 +               au_digen_dec(dentry);
13160 +               if (inode)
13161 +                       au_iigen_dec(inode);
13162 +       } else {
13163 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
13164 +               if (inode)
13165 +                       err = hn_gen_tree(dentry);
13166 +       }
13167 +
13168 +       AuTraceErr(err);
13169 +       return err;
13170 +}
13171 +
13172 +/* ---------------------------------------------------------------------- */
13173 +
13174 +/* hnotify job flags */
13175 +#define AuHnJob_XINO0          1
13176 +#define AuHnJob_GEN            (1 << 1)
13177 +#define AuHnJob_DIRENT         (1 << 2)
13178 +#define AuHnJob_ISDIR          (1 << 3)
13179 +#define AuHnJob_TRYXINO0       (1 << 4)
13180 +#define AuHnJob_MNTPNT         (1 << 5)
13181 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
13182 +#define au_fset_hnjob(flags, name) \
13183 +       do { (flags) |= AuHnJob_##name; } while (0)
13184 +#define au_fclr_hnjob(flags, name) \
13185 +       do { (flags) &= ~AuHnJob_##name; } while (0)
13186 +
13187 +enum {
13188 +       AuHn_CHILD,
13189 +       AuHn_PARENT,
13190 +       AuHnLast
13191 +};
13192 +
13193 +struct au_hnotify_args {
13194 +       struct inode *h_dir, *dir, *h_child_inode;
13195 +       u32 mask;
13196 +       unsigned int flags[AuHnLast];
13197 +       unsigned int h_child_nlen;
13198 +       char h_child_name[];
13199 +};
13200 +
13201 +struct hn_job_args {
13202 +       unsigned int flags;
13203 +       struct inode *inode, *h_inode, *dir, *h_dir;
13204 +       struct dentry *dentry;
13205 +       char *h_name;
13206 +       int h_nlen;
13207 +};
13208 +
13209 +static int hn_job(struct hn_job_args *a)
13210 +{
13211 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
13212 +
13213 +       /* reset xino */
13214 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
13215 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
13216 +
13217 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
13218 +           && a->inode
13219 +           && a->h_inode) {
13220 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
13221 +               if (!a->h_inode->i_nlink)
13222 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
13223 +               mutex_unlock(&a->h_inode->i_mutex);
13224 +       }
13225 +
13226 +       /* make the generation obsolete */
13227 +       if (au_ftest_hnjob(a->flags, GEN)) {
13228 +               int err = -1;
13229 +               if (a->inode)
13230 +                       err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
13231 +                                             isdir);
13232 +               if (err && a->dentry)
13233 +                       hn_gen_by_name(a->dentry, isdir);
13234 +               /* ignore this error */
13235 +       }
13236 +
13237 +       /* make dir entries obsolete */
13238 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
13239 +               struct au_vdir *vdir;
13240 +
13241 +               vdir = au_ivdir(a->inode);
13242 +               if (vdir)
13243 +                       vdir->vd_jiffy = 0;
13244 +               /* IMustLock(a->inode); */
13245 +               /* a->inode->i_version++; */
13246 +       }
13247 +
13248 +       /* can do nothing but warn */
13249 +       if (au_ftest_hnjob(a->flags, MNTPNT)
13250 +           && a->dentry
13251 +           && d_mountpoint(a->dentry))
13252 +               pr_warning("mount-point %.*s is removed or renamed\n",
13253 +                          AuDLNPair(a->dentry));
13254 +
13255 +       return 0;
13256 +}
13257 +
13258 +/* ---------------------------------------------------------------------- */
13259 +
13260 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
13261 +                                          struct inode *dir)
13262 +{
13263 +       struct dentry *dentry, *d, *parent;
13264 +       struct qstr *dname;
13265 +
13266 +       parent = d_find_alias(dir);
13267 +       if (!parent)
13268 +               return NULL;
13269 +
13270 +       dentry = NULL;
13271 +       spin_lock(&parent->d_lock);
13272 +       list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
13273 +               /* AuDbg("%.*s\n", AuDLNPair(d)); */
13274 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
13275 +               dname = &d->d_name;
13276 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
13277 +                       goto cont_unlock;
13278 +               if (au_di(d))
13279 +                       au_digen_dec(d);
13280 +               else
13281 +                       goto cont_unlock;
13282 +               if (d->d_count) {
13283 +                       dentry = dget_dlock(d);
13284 +                       spin_unlock(&d->d_lock);
13285 +                       break;
13286 +               }
13287 +
13288 +       cont_unlock:
13289 +               spin_unlock(&d->d_lock);
13290 +       }
13291 +       spin_unlock(&parent->d_lock);
13292 +       dput(parent);
13293 +
13294 +       if (dentry)
13295 +               di_write_lock_child(dentry);
13296 +
13297 +       return dentry;
13298 +}
13299 +
13300 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
13301 +                                        aufs_bindex_t bindex, ino_t h_ino)
13302 +{
13303 +       struct inode *inode;
13304 +       ino_t ino;
13305 +       int err;
13306 +
13307 +       inode = NULL;
13308 +       err = au_xino_read(sb, bindex, h_ino, &ino);
13309 +       if (!err && ino)
13310 +               inode = ilookup(sb, ino);
13311 +       if (!inode)
13312 +               goto out;
13313 +
13314 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13315 +               pr_warning("wrong root branch\n");
13316 +               iput(inode);
13317 +               inode = NULL;
13318 +               goto out;
13319 +       }
13320 +
13321 +       ii_write_lock_child(inode);
13322 +
13323 +out:
13324 +       return inode;
13325 +}
13326 +
13327 +static void au_hn_bh(void *_args)
13328 +{
13329 +       struct au_hnotify_args *a = _args;
13330 +       struct super_block *sb;
13331 +       aufs_bindex_t bindex, bend, bfound;
13332 +       unsigned char xino, try_iput;
13333 +       int err;
13334 +       struct inode *inode;
13335 +       ino_t h_ino;
13336 +       struct hn_job_args args;
13337 +       struct dentry *dentry;
13338 +       struct au_sbinfo *sbinfo;
13339 +
13340 +       AuDebugOn(!_args);
13341 +       AuDebugOn(!a->h_dir);
13342 +       AuDebugOn(!a->dir);
13343 +       AuDebugOn(!a->mask);
13344 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
13345 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
13346 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
13347 +
13348 +       inode = NULL;
13349 +       dentry = NULL;
13350 +       /*
13351 +        * do not lock a->dir->i_mutex here
13352 +        * because of d_revalidate() may cause a deadlock.
13353 +        */
13354 +       sb = a->dir->i_sb;
13355 +       AuDebugOn(!sb);
13356 +       sbinfo = au_sbi(sb);
13357 +       AuDebugOn(!sbinfo);
13358 +       si_write_lock(sb, AuLock_NOPLMW);
13359 +
13360 +       ii_read_lock_parent(a->dir);
13361 +       bfound = -1;
13362 +       bend = au_ibend(a->dir);
13363 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
13364 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
13365 +                       bfound = bindex;
13366 +                       break;
13367 +               }
13368 +       ii_read_unlock(a->dir);
13369 +       if (unlikely(bfound < 0))
13370 +               goto out;
13371 +
13372 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
13373 +       h_ino = 0;
13374 +       if (a->h_child_inode)
13375 +               h_ino = a->h_child_inode->i_ino;
13376 +
13377 +       if (a->h_child_nlen
13378 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
13379 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
13380 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
13381 +                                             a->dir);
13382 +       try_iput = 0;
13383 +       if (dentry)
13384 +               inode = dentry->d_inode;
13385 +       if (xino && !inode && h_ino
13386 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
13387 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
13388 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
13389 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
13390 +               try_iput = 1;
13391 +           }
13392 +
13393 +       args.flags = a->flags[AuHn_CHILD];
13394 +       args.dentry = dentry;
13395 +       args.inode = inode;
13396 +       args.h_inode = a->h_child_inode;
13397 +       args.dir = a->dir;
13398 +       args.h_dir = a->h_dir;
13399 +       args.h_name = a->h_child_name;
13400 +       args.h_nlen = a->h_child_nlen;
13401 +       err = hn_job(&args);
13402 +       if (dentry) {
13403 +               if (au_di(dentry))
13404 +                       di_write_unlock(dentry);
13405 +               dput(dentry);
13406 +       }
13407 +       if (inode && try_iput) {
13408 +               ii_write_unlock(inode);
13409 +               iput(inode);
13410 +       }
13411 +
13412 +       ii_write_lock_parent(a->dir);
13413 +       args.flags = a->flags[AuHn_PARENT];
13414 +       args.dentry = NULL;
13415 +       args.inode = a->dir;
13416 +       args.h_inode = a->h_dir;
13417 +       args.dir = NULL;
13418 +       args.h_dir = NULL;
13419 +       args.h_name = NULL;
13420 +       args.h_nlen = 0;
13421 +       err = hn_job(&args);
13422 +       ii_write_unlock(a->dir);
13423 +
13424 +out:
13425 +       iput(a->h_child_inode);
13426 +       iput(a->h_dir);
13427 +       iput(a->dir);
13428 +       si_write_unlock(sb);
13429 +       au_nwt_done(&sbinfo->si_nowait);
13430 +       kfree(a);
13431 +}
13432 +
13433 +/* ---------------------------------------------------------------------- */
13434 +
13435 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
13436 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
13437 +{
13438 +       int err, len;
13439 +       unsigned int flags[AuHnLast], f;
13440 +       unsigned char isdir, isroot, wh;
13441 +       struct inode *dir;
13442 +       struct au_hnotify_args *args;
13443 +       char *p, *h_child_name;
13444 +
13445 +       err = 0;
13446 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
13447 +       dir = igrab(hnotify->hn_aufs_inode);
13448 +       if (!dir)
13449 +               goto out;
13450 +
13451 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
13452 +       wh = 0;
13453 +       h_child_name = (void *)h_child_qstr->name;
13454 +       len = h_child_qstr->len;
13455 +       if (h_child_name) {
13456 +               if (len > AUFS_WH_PFX_LEN
13457 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
13458 +                       h_child_name += AUFS_WH_PFX_LEN;
13459 +                       len -= AUFS_WH_PFX_LEN;
13460 +                       wh = 1;
13461 +               }
13462 +       }
13463 +
13464 +       isdir = 0;
13465 +       if (h_child_inode)
13466 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
13467 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
13468 +       flags[AuHn_CHILD] = 0;
13469 +       if (isdir)
13470 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
13471 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
13472 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
13473 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
13474 +       case FS_MOVED_FROM:
13475 +       case FS_MOVED_TO:
13476 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
13477 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13478 +               /*FALLTHROUGH*/
13479 +       case FS_CREATE:
13480 +               AuDebugOn(!h_child_name || !h_child_inode);
13481 +               break;
13482 +
13483 +       case FS_DELETE:
13484 +               /*
13485 +                * aufs never be able to get this child inode.
13486 +                * revalidation should be in d_revalidate()
13487 +                * by checking i_nlink, i_generation or d_unhashed().
13488 +                */
13489 +               AuDebugOn(!h_child_name);
13490 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
13491 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13492 +               break;
13493 +
13494 +       default:
13495 +               AuDebugOn(1);
13496 +       }
13497 +
13498 +       if (wh)
13499 +               h_child_inode = NULL;
13500 +
13501 +       err = -ENOMEM;
13502 +       /* iput() and kfree() will be called in au_hnotify() */
13503 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
13504 +       if (unlikely(!args)) {
13505 +               AuErr1("no memory\n");
13506 +               iput(dir);
13507 +               goto out;
13508 +       }
13509 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
13510 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
13511 +       args->mask = mask;
13512 +       args->dir = dir;
13513 +       args->h_dir = igrab(h_dir);
13514 +       if (h_child_inode)
13515 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
13516 +       args->h_child_inode = h_child_inode;
13517 +       args->h_child_nlen = len;
13518 +       if (len) {
13519 +               p = (void *)args;
13520 +               p += sizeof(*args);
13521 +               memcpy(p, h_child_name, len);
13522 +               p[len] = 0;
13523 +       }
13524 +
13525 +       f = 0;
13526 +       if (!dir->i_nlink)
13527 +               f = AuWkq_NEST;
13528 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
13529 +       if (unlikely(err)) {
13530 +               pr_err("wkq %d\n", err);
13531 +               iput(args->h_child_inode);
13532 +               iput(args->h_dir);
13533 +               iput(args->dir);
13534 +               kfree(args);
13535 +       }
13536 +
13537 +out:
13538 +       return err;
13539 +}
13540 +
13541 +/* ---------------------------------------------------------------------- */
13542 +
13543 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
13544 +{
13545 +       int err;
13546 +
13547 +       AuDebugOn(!(udba & AuOptMask_UDBA));
13548 +
13549 +       err = 0;
13550 +       if (au_hnotify_op.reset_br)
13551 +               err = au_hnotify_op.reset_br(udba, br, perm);
13552 +
13553 +       return err;
13554 +}
13555 +
13556 +int au_hnotify_init_br(struct au_branch *br, int perm)
13557 +{
13558 +       int err;
13559 +
13560 +       err = 0;
13561 +       if (au_hnotify_op.init_br)
13562 +               err = au_hnotify_op.init_br(br, perm);
13563 +
13564 +       return err;
13565 +}
13566 +
13567 +void au_hnotify_fin_br(struct au_branch *br)
13568 +{
13569 +       if (au_hnotify_op.fin_br)
13570 +               au_hnotify_op.fin_br(br);
13571 +}
13572 +
13573 +static void au_hn_destroy_cache(void)
13574 +{
13575 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
13576 +       au_cachep[AuCache_HNOTIFY] = NULL;
13577 +}
13578 +
13579 +int __init au_hnotify_init(void)
13580 +{
13581 +       int err;
13582 +
13583 +       err = -ENOMEM;
13584 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
13585 +       if (au_cachep[AuCache_HNOTIFY]) {
13586 +               err = 0;
13587 +               if (au_hnotify_op.init)
13588 +                       err = au_hnotify_op.init();
13589 +               if (unlikely(err))
13590 +                       au_hn_destroy_cache();
13591 +       }
13592 +       AuTraceErr(err);
13593 +       return err;
13594 +}
13595 +
13596 +void au_hnotify_fin(void)
13597 +{
13598 +       if (au_hnotify_op.fin)
13599 +               au_hnotify_op.fin();
13600 +       /* cf. au_cache_fin() */
13601 +       if (au_cachep[AuCache_HNOTIFY])
13602 +               au_hn_destroy_cache();
13603 +}
13604 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
13605 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
13606 +++ linux/fs/aufs/iinfo.c       2012-03-19 12:24:13.683134508 +0100
13607 @@ -0,0 +1,264 @@
13608 +/*
13609 + * Copyright (C) 2005-2012 Junjiro R. Okajima
13610 + *
13611 + * This program, aufs is free software; you can redistribute it and/or modify
13612 + * it under the terms of the GNU General Public License as published by
13613 + * the Free Software Foundation; either version 2 of the License, or
13614 + * (at your option) any later version.
13615 + *
13616 + * This program is distributed in the hope that it will be useful,
13617 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13618 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13619 + * GNU General Public License for more details.
13620 + *
13621 + * You should have received a copy of the GNU General Public License
13622 + * along with this program; if not, write to the Free Software
13623 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13624 + */
13625 +
13626 +/*
13627 + * inode private data
13628 + */
13629 +
13630 +#include "aufs.h"
13631 +
13632 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
13633 +{
13634 +       struct inode *h_inode;
13635 +
13636 +       IiMustAnyLock(inode);
13637 +
13638 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
13639 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
13640 +       return h_inode;
13641 +}
13642 +
13643 +/* todo: hard/soft set? */
13644 +void au_hiput(struct au_hinode *hinode)
13645 +{
13646 +       au_hn_free(hinode);
13647 +       dput(hinode->hi_whdentry);
13648 +       iput(hinode->hi_inode);
13649 +}
13650 +
13651 +unsigned int au_hi_flags(struct inode *inode, int isdir)
13652 +{
13653 +       unsigned int flags;
13654 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
13655 +
13656 +       flags = 0;
13657 +       if (au_opt_test(mnt_flags, XINO))
13658 +               au_fset_hi(flags, XINO);
13659 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
13660 +               au_fset_hi(flags, HNOTIFY);
13661 +       return flags;
13662 +}
13663 +
13664 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
13665 +                  struct inode *h_inode, unsigned int flags)
13666 +{
13667 +       struct au_hinode *hinode;
13668 +       struct inode *hi;
13669 +       struct au_iinfo *iinfo = au_ii(inode);
13670 +
13671 +       IiMustWriteLock(inode);
13672 +
13673 +       hinode = iinfo->ii_hinode + bindex;
13674 +       hi = hinode->hi_inode;
13675 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
13676 +
13677 +       if (hi)
13678 +               au_hiput(hinode);
13679 +       hinode->hi_inode = h_inode;
13680 +       if (h_inode) {
13681 +               int err;
13682 +               struct super_block *sb = inode->i_sb;
13683 +               struct au_branch *br;
13684 +
13685 +               AuDebugOn(inode->i_mode
13686 +                         && (h_inode->i_mode & S_IFMT)
13687 +                         != (inode->i_mode & S_IFMT));
13688 +               if (bindex == iinfo->ii_bstart)
13689 +                       au_cpup_igen(inode, h_inode);
13690 +               br = au_sbr(sb, bindex);
13691 +               hinode->hi_id = br->br_id;
13692 +               if (au_ftest_hi(flags, XINO)) {
13693 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
13694 +                                           inode->i_ino);
13695 +                       if (unlikely(err))
13696 +                               AuIOErr1("failed au_xino_write() %d\n", err);
13697 +               }
13698 +
13699 +               if (au_ftest_hi(flags, HNOTIFY)
13700 +                   && au_br_hnotifyable(br->br_perm)) {
13701 +                       err = au_hn_alloc(hinode, inode);
13702 +                       if (unlikely(err))
13703 +                               AuIOErr1("au_hn_alloc() %d\n", err);
13704 +               }
13705 +       }
13706 +}
13707 +
13708 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
13709 +                 struct dentry *h_wh)
13710 +{
13711 +       struct au_hinode *hinode;
13712 +
13713 +       IiMustWriteLock(inode);
13714 +
13715 +       hinode = au_ii(inode)->ii_hinode + bindex;
13716 +       AuDebugOn(hinode->hi_whdentry);
13717 +       hinode->hi_whdentry = h_wh;
13718 +}
13719 +
13720 +void au_update_iigen(struct inode *inode)
13721 +{
13722 +       atomic_set(&au_ii(inode)->ii_generation, au_sigen(inode->i_sb));
13723 +       /* smp_mb(); */ /* atomic_set */
13724 +}
13725 +
13726 +/* it may be called at remount time, too */
13727 +void au_update_ibrange(struct inode *inode, int do_put_zero)
13728 +{
13729 +       struct au_iinfo *iinfo;
13730 +       aufs_bindex_t bindex, bend;
13731 +
13732 +       iinfo = au_ii(inode);
13733 +       if (!iinfo)
13734 +               return;
13735 +
13736 +       IiMustWriteLock(inode);
13737 +
13738 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
13739 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
13740 +                    bindex++) {
13741 +                       struct inode *h_i;
13742 +
13743 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
13744 +                       if (h_i && !h_i->i_nlink)
13745 +                               au_set_h_iptr(inode, bindex, NULL, 0);
13746 +               }
13747 +       }
13748 +
13749 +       iinfo->ii_bstart = -1;
13750 +       iinfo->ii_bend = -1;
13751 +       bend = au_sbend(inode->i_sb);
13752 +       for (bindex = 0; bindex <= bend; bindex++)
13753 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
13754 +                       iinfo->ii_bstart = bindex;
13755 +                       break;
13756 +               }
13757 +       if (iinfo->ii_bstart >= 0)
13758 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
13759 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
13760 +                               iinfo->ii_bend = bindex;
13761 +                               break;
13762 +                       }
13763 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
13764 +}
13765 +
13766 +/* ---------------------------------------------------------------------- */
13767 +
13768 +void au_icntnr_init_once(void *_c)
13769 +{
13770 +       struct au_icntnr *c = _c;
13771 +       struct au_iinfo *iinfo = &c->iinfo;
13772 +       static struct lock_class_key aufs_ii;
13773 +
13774 +       au_rw_init(&iinfo->ii_rwsem);
13775 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
13776 +       inode_init_once(&c->vfs_inode);
13777 +}
13778 +
13779 +int au_iinfo_init(struct inode *inode)
13780 +{
13781 +       struct au_iinfo *iinfo;
13782 +       struct super_block *sb;
13783 +       int nbr, i;
13784 +
13785 +       sb = inode->i_sb;
13786 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
13787 +       nbr = au_sbend(sb) + 1;
13788 +       if (unlikely(nbr <= 0))
13789 +               nbr = 1;
13790 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
13791 +       if (iinfo->ii_hinode) {
13792 +               au_ninodes_inc(sb);
13793 +               for (i = 0; i < nbr; i++)
13794 +                       iinfo->ii_hinode[i].hi_id = -1;
13795 +
13796 +               atomic_set(&iinfo->ii_generation, au_sigen(sb));
13797 +               /* smp_mb(); */ /* atomic_set */
13798 +               iinfo->ii_bstart = -1;
13799 +               iinfo->ii_bend = -1;
13800 +               iinfo->ii_vdir = NULL;
13801 +               return 0;
13802 +       }
13803 +       return -ENOMEM;
13804 +}
13805 +
13806 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
13807 +{
13808 +       int err, sz;
13809 +       struct au_hinode *hip;
13810 +
13811 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
13812 +
13813 +       err = -ENOMEM;
13814 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
13815 +       if (!sz)
13816 +               sz = sizeof(*hip);
13817 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
13818 +       if (hip) {
13819 +               iinfo->ii_hinode = hip;
13820 +               err = 0;
13821 +       }
13822 +
13823 +       return err;
13824 +}
13825 +
13826 +void au_iinfo_fin(struct inode *inode)
13827 +{
13828 +       struct au_iinfo *iinfo;
13829 +       struct au_hinode *hi;
13830 +       struct super_block *sb;
13831 +       aufs_bindex_t bindex, bend;
13832 +       const unsigned char unlinked = !inode->i_nlink;
13833 +
13834 +       iinfo = au_ii(inode);
13835 +       /* bad_inode case */
13836 +       if (!iinfo)
13837 +               return;
13838 +
13839 +       sb = inode->i_sb;
13840 +       au_ninodes_dec(sb);
13841 +       if (si_pid_test(sb))
13842 +               au_xino_delete_inode(inode, unlinked);
13843 +       else {
13844 +               /*
13845 +                * it is safe to hide the dependency between sbinfo and
13846 +                * sb->s_umount.
13847 +                */
13848 +               lockdep_off();
13849 +               si_noflush_read_lock(sb);
13850 +               au_xino_delete_inode(inode, unlinked);
13851 +               si_read_unlock(sb);
13852 +               lockdep_on();
13853 +       }
13854 +
13855 +       if (iinfo->ii_vdir)
13856 +               au_vdir_free(iinfo->ii_vdir);
13857 +
13858 +       bindex = iinfo->ii_bstart;
13859 +       if (bindex >= 0) {
13860 +               hi = iinfo->ii_hinode + bindex;
13861 +               bend = iinfo->ii_bend;
13862 +               while (bindex++ <= bend) {
13863 +                       if (hi->hi_inode)
13864 +                               au_hiput(hi);
13865 +                       hi++;
13866 +               }
13867 +       }
13868 +       kfree(iinfo->ii_hinode);
13869 +       iinfo->ii_hinode = NULL;
13870 +       AuRwDestroy(&iinfo->ii_rwsem);
13871 +}
13872 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
13873 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
13874 +++ linux/fs/aufs/inode.c       2012-03-19 12:24:13.683134508 +0100
13875 @@ -0,0 +1,471 @@
13876 +/*
13877 + * Copyright (C) 2005-2012 Junjiro R. Okajima
13878 + *
13879 + * This program, aufs is free software; you can redistribute it and/or modify
13880 + * it under the terms of the GNU General Public License as published by
13881 + * the Free Software Foundation; either version 2 of the License, or
13882 + * (at your option) any later version.
13883 + *
13884 + * This program is distributed in the hope that it will be useful,
13885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13887 + * GNU General Public License for more details.
13888 + *
13889 + * You should have received a copy of the GNU General Public License
13890 + * along with this program; if not, write to the Free Software
13891 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13892 + */
13893 +
13894 +/*
13895 + * inode functions
13896 + */
13897 +
13898 +#include "aufs.h"
13899 +
13900 +struct inode *au_igrab(struct inode *inode)
13901 +{
13902 +       if (inode) {
13903 +               AuDebugOn(!atomic_read(&inode->i_count));
13904 +               ihold(inode);
13905 +       }
13906 +       return inode;
13907 +}
13908 +
13909 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
13910 +{
13911 +       au_cpup_attr_all(inode, /*force*/0);
13912 +       au_update_iigen(inode);
13913 +       if (do_version)
13914 +               inode->i_version++;
13915 +}
13916 +
13917 +static int au_ii_refresh(struct inode *inode, int *update)
13918 +{
13919 +       int err, e;
13920 +       umode_t type;
13921 +       aufs_bindex_t bindex, new_bindex;
13922 +       struct super_block *sb;
13923 +       struct au_iinfo *iinfo;
13924 +       struct au_hinode *p, *q, tmp;
13925 +
13926 +       IiMustWriteLock(inode);
13927 +
13928 +       *update = 0;
13929 +       sb = inode->i_sb;
13930 +       type = inode->i_mode & S_IFMT;
13931 +       iinfo = au_ii(inode);
13932 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
13933 +       if (unlikely(err))
13934 +               goto out;
13935 +
13936 +       AuDebugOn(iinfo->ii_bstart < 0);
13937 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
13938 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
13939 +            bindex++, p++) {
13940 +               if (!p->hi_inode)
13941 +                       continue;
13942 +
13943 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
13944 +               new_bindex = au_br_index(sb, p->hi_id);
13945 +               if (new_bindex == bindex)
13946 +                       continue;
13947 +
13948 +               if (new_bindex < 0) {
13949 +                       *update = 1;
13950 +                       au_hiput(p);
13951 +                       p->hi_inode = NULL;
13952 +                       continue;
13953 +               }
13954 +
13955 +               if (new_bindex < iinfo->ii_bstart)
13956 +                       iinfo->ii_bstart = new_bindex;
13957 +               if (iinfo->ii_bend < new_bindex)
13958 +                       iinfo->ii_bend = new_bindex;
13959 +               /* swap two lower inode, and loop again */
13960 +               q = iinfo->ii_hinode + new_bindex;
13961 +               tmp = *q;
13962 +               *q = *p;
13963 +               *p = tmp;
13964 +               if (tmp.hi_inode) {
13965 +                       bindex--;
13966 +                       p--;
13967 +               }
13968 +       }
13969 +       au_update_ibrange(inode, /*do_put_zero*/0);
13970 +       e = au_dy_irefresh(inode);
13971 +       if (unlikely(e && !err))
13972 +               err = e;
13973 +
13974 +out:
13975 +       AuTraceErr(err);
13976 +       return err;
13977 +}
13978 +
13979 +int au_refresh_hinode_self(struct inode *inode)
13980 +{
13981 +       int err, update;
13982 +
13983 +       err = au_ii_refresh(inode, &update);
13984 +       if (!err)
13985 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
13986 +
13987 +       AuTraceErr(err);
13988 +       return err;
13989 +}
13990 +
13991 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
13992 +{
13993 +       int err, e, update;
13994 +       unsigned int flags;
13995 +       umode_t mode;
13996 +       aufs_bindex_t bindex, bend;
13997 +       unsigned char isdir;
13998 +       struct au_hinode *p;
13999 +       struct au_iinfo *iinfo;
14000 +
14001 +       err = au_ii_refresh(inode, &update);
14002 +       if (unlikely(err))
14003 +               goto out;
14004 +
14005 +       update = 0;
14006 +       iinfo = au_ii(inode);
14007 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14008 +       mode = (inode->i_mode & S_IFMT);
14009 +       isdir = S_ISDIR(mode);
14010 +       flags = au_hi_flags(inode, isdir);
14011 +       bend = au_dbend(dentry);
14012 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
14013 +               struct inode *h_i;
14014 +               struct dentry *h_d;
14015 +
14016 +               h_d = au_h_dptr(dentry, bindex);
14017 +               if (!h_d || !h_d->d_inode)
14018 +                       continue;
14019 +
14020 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
14021 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
14022 +                       h_i = au_h_iptr(inode, bindex);
14023 +                       if (h_i) {
14024 +                               if (h_i == h_d->d_inode)
14025 +                                       continue;
14026 +                               err = -EIO;
14027 +                               break;
14028 +                       }
14029 +               }
14030 +               if (bindex < iinfo->ii_bstart)
14031 +                       iinfo->ii_bstart = bindex;
14032 +               if (iinfo->ii_bend < bindex)
14033 +                       iinfo->ii_bend = bindex;
14034 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
14035 +               update = 1;
14036 +       }
14037 +       au_update_ibrange(inode, /*do_put_zero*/0);
14038 +       e = au_dy_irefresh(inode);
14039 +       if (unlikely(e && !err))
14040 +               err = e;
14041 +       if (!err)
14042 +               au_refresh_hinode_attr(inode, update && isdir);
14043 +
14044 +out:
14045 +       AuTraceErr(err);
14046 +       return err;
14047 +}
14048 +
14049 +static int set_inode(struct inode *inode, struct dentry *dentry)
14050 +{
14051 +       int err;
14052 +       unsigned int flags;
14053 +       umode_t mode;
14054 +       aufs_bindex_t bindex, bstart, btail;
14055 +       unsigned char isdir;
14056 +       struct dentry *h_dentry;
14057 +       struct inode *h_inode;
14058 +       struct au_iinfo *iinfo;
14059 +
14060 +       IiMustWriteLock(inode);
14061 +
14062 +       err = 0;
14063 +       isdir = 0;
14064 +       bstart = au_dbstart(dentry);
14065 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
14066 +       mode = h_inode->i_mode;
14067 +       switch (mode & S_IFMT) {
14068 +       case S_IFREG:
14069 +               btail = au_dbtail(dentry);
14070 +               inode->i_op = &aufs_iop;
14071 +               inode->i_fop = &aufs_file_fop;
14072 +               err = au_dy_iaop(inode, bstart, h_inode);
14073 +               if (unlikely(err))
14074 +                       goto out;
14075 +               break;
14076 +       case S_IFDIR:
14077 +               isdir = 1;
14078 +               btail = au_dbtaildir(dentry);
14079 +               inode->i_op = &aufs_dir_iop;
14080 +               inode->i_fop = &aufs_dir_fop;
14081 +               break;
14082 +       case S_IFLNK:
14083 +               btail = au_dbtail(dentry);
14084 +               inode->i_op = &aufs_symlink_iop;
14085 +               break;
14086 +       case S_IFBLK:
14087 +       case S_IFCHR:
14088 +       case S_IFIFO:
14089 +       case S_IFSOCK:
14090 +               btail = au_dbtail(dentry);
14091 +               inode->i_op = &aufs_iop;
14092 +               au_init_special_fop(inode, mode, h_inode->i_rdev);
14093 +               break;
14094 +       default:
14095 +               AuIOErr("Unknown file type 0%o\n", mode);
14096 +               err = -EIO;
14097 +               goto out;
14098 +       }
14099 +
14100 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
14101 +       flags = au_hi_flags(inode, isdir);
14102 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
14103 +           && au_ftest_hi(flags, HNOTIFY)
14104 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
14105 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
14106 +               au_fclr_hi(flags, HNOTIFY);
14107 +       iinfo = au_ii(inode);
14108 +       iinfo->ii_bstart = bstart;
14109 +       iinfo->ii_bend = btail;
14110 +       for (bindex = bstart; bindex <= btail; bindex++) {
14111 +               h_dentry = au_h_dptr(dentry, bindex);
14112 +               if (h_dentry)
14113 +                       au_set_h_iptr(inode, bindex,
14114 +                                     au_igrab(h_dentry->d_inode), flags);
14115 +       }
14116 +       au_cpup_attr_all(inode, /*force*/1);
14117 +
14118 +out:
14119 +       return err;
14120 +}
14121 +
14122 +/*
14123 + * successful returns with iinfo write_locked
14124 + * minus: errno
14125 + * zero: success, matched
14126 + * plus: no error, but unmatched
14127 + */
14128 +static int reval_inode(struct inode *inode, struct dentry *dentry)
14129 +{
14130 +       int err;
14131 +       aufs_bindex_t bindex, bend;
14132 +       struct inode *h_inode, *h_dinode;
14133 +
14134 +       /*
14135 +        * before this function, if aufs got any iinfo lock, it must be only
14136 +        * one, the parent dir.
14137 +        * it can happen by UDBA and the obsoleted inode number.
14138 +        */
14139 +       err = -EIO;
14140 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
14141 +               goto out;
14142 +
14143 +       err = 1;
14144 +       ii_write_lock_new_child(inode);
14145 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
14146 +       bend = au_ibend(inode);
14147 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14148 +               h_inode = au_h_iptr(inode, bindex);
14149 +               if (h_inode && h_inode == h_dinode) {
14150 +                       err = 0;
14151 +                       if (au_iigen_test(inode, au_digen(dentry)))
14152 +                               err = au_refresh_hinode(inode, dentry);
14153 +                       break;
14154 +               }
14155 +       }
14156 +
14157 +       if (unlikely(err))
14158 +               ii_write_unlock(inode);
14159 +out:
14160 +       return err;
14161 +}
14162 +
14163 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14164 +          unsigned int d_type, ino_t *ino)
14165 +{
14166 +       int err;
14167 +       struct mutex *mtx;
14168 +
14169 +       /* prevent hardlinked inode number from race condition */
14170 +       mtx = NULL;
14171 +       if (d_type != DT_DIR) {
14172 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
14173 +               mutex_lock(mtx);
14174 +       }
14175 +       err = au_xino_read(sb, bindex, h_ino, ino);
14176 +       if (unlikely(err))
14177 +               goto out;
14178 +
14179 +       if (!*ino) {
14180 +               err = -EIO;
14181 +               *ino = au_xino_new_ino(sb);
14182 +               if (unlikely(!*ino))
14183 +                       goto out;
14184 +               err = au_xino_write(sb, bindex, h_ino, *ino);
14185 +               if (unlikely(err))
14186 +                       goto out;
14187 +       }
14188 +
14189 +out:
14190 +       if (mtx)
14191 +               mutex_unlock(mtx);
14192 +       return err;
14193 +}
14194 +
14195 +/* successful returns with iinfo write_locked */
14196 +/* todo: return with unlocked? */
14197 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
14198 +{
14199 +       struct inode *inode, *h_inode;
14200 +       struct dentry *h_dentry;
14201 +       struct super_block *sb;
14202 +       struct mutex *mtx;
14203 +       ino_t h_ino, ino;
14204 +       int err;
14205 +       aufs_bindex_t bstart;
14206 +
14207 +       sb = dentry->d_sb;
14208 +       bstart = au_dbstart(dentry);
14209 +       h_dentry = au_h_dptr(dentry, bstart);
14210 +       h_inode = h_dentry->d_inode;
14211 +       h_ino = h_inode->i_ino;
14212 +
14213 +       /*
14214 +        * stop 'race'-ing between hardlinks under different
14215 +        * parents.
14216 +        */
14217 +       mtx = NULL;
14218 +       if (!S_ISDIR(h_inode->i_mode))
14219 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
14220 +
14221 +new_ino:
14222 +       if (mtx)
14223 +               mutex_lock(mtx);
14224 +       err = au_xino_read(sb, bstart, h_ino, &ino);
14225 +       inode = ERR_PTR(err);
14226 +       if (unlikely(err))
14227 +               goto out;
14228 +
14229 +       if (!ino) {
14230 +               ino = au_xino_new_ino(sb);
14231 +               if (unlikely(!ino)) {
14232 +                       inode = ERR_PTR(-EIO);
14233 +                       goto out;
14234 +               }
14235 +       }
14236 +
14237 +       AuDbg("i%lu\n", (unsigned long)ino);
14238 +       inode = au_iget_locked(sb, ino);
14239 +       err = PTR_ERR(inode);
14240 +       if (IS_ERR(inode))
14241 +               goto out;
14242 +
14243 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
14244 +       if (inode->i_state & I_NEW) {
14245 +               ii_write_lock_new_child(inode);
14246 +               err = set_inode(inode, dentry);
14247 +               if (!err) {
14248 +                       unlock_new_inode(inode);
14249 +                       goto out; /* success */
14250 +               }
14251 +
14252 +               /*
14253 +                * iget_failed() calls iput(), but we need to call
14254 +                * ii_write_unlock() after iget_failed(). so dirty hack for
14255 +                * i_count.
14256 +                */
14257 +               atomic_inc(&inode->i_count);
14258 +               iget_failed(inode);
14259 +               ii_write_unlock(inode);
14260 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
14261 +               /* ignore this error */
14262 +               goto out_iput;
14263 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
14264 +               /*
14265 +                * horrible race condition between lookup, readdir and copyup
14266 +                * (or something).
14267 +                */
14268 +               if (mtx)
14269 +                       mutex_unlock(mtx);
14270 +               err = reval_inode(inode, dentry);
14271 +               if (unlikely(err < 0)) {
14272 +                       mtx = NULL;
14273 +                       goto out_iput;
14274 +               }
14275 +
14276 +               if (!err) {
14277 +                       mtx = NULL;
14278 +                       goto out; /* success */
14279 +               } else if (mtx)
14280 +                       mutex_lock(mtx);
14281 +       }
14282 +
14283 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
14284 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
14285 +                       " b%d, %s, %.*s, hi%lu, i%lu.\n",
14286 +                       bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
14287 +                       (unsigned long)h_ino, (unsigned long)ino);
14288 +       ino = 0;
14289 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
14290 +       if (!err) {
14291 +               iput(inode);
14292 +               if (mtx)
14293 +                       mutex_unlock(mtx);
14294 +               goto new_ino;
14295 +       }
14296 +
14297 +out_iput:
14298 +       iput(inode);
14299 +       inode = ERR_PTR(err);
14300 +out:
14301 +       if (mtx)
14302 +               mutex_unlock(mtx);
14303 +       return inode;
14304 +}
14305 +
14306 +/* ---------------------------------------------------------------------- */
14307 +
14308 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
14309 +              struct inode *inode)
14310 +{
14311 +       int err;
14312 +
14313 +       err = au_br_rdonly(au_sbr(sb, bindex));
14314 +
14315 +       /* pseudo-link after flushed may happen out of bounds */
14316 +       if (!err
14317 +           && inode
14318 +           && au_ibstart(inode) <= bindex
14319 +           && bindex <= au_ibend(inode)) {
14320 +               /*
14321 +                * permission check is unnecessary since vfsub routine
14322 +                * will be called later
14323 +                */
14324 +               struct inode *hi = au_h_iptr(inode, bindex);
14325 +               if (hi)
14326 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
14327 +       }
14328 +
14329 +       return err;
14330 +}
14331 +
14332 +int au_test_h_perm(struct inode *h_inode, int mask)
14333 +{
14334 +       if (!current_fsuid())
14335 +               return 0;
14336 +       return inode_permission(h_inode, mask);
14337 +}
14338 +
14339 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
14340 +{
14341 +       if (au_test_nfs(h_inode->i_sb)
14342 +           && (mask & MAY_WRITE)
14343 +           && S_ISDIR(h_inode->i_mode))
14344 +               mask |= MAY_READ; /* force permission check */
14345 +       return au_test_h_perm(h_inode, mask);
14346 +}
14347 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
14348 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
14349 +++ linux/fs/aufs/inode.h       2012-03-19 12:24:13.683134508 +0100
14350 @@ -0,0 +1,560 @@
14351 +/*
14352 + * Copyright (C) 2005-2012 Junjiro R. Okajima
14353 + *
14354 + * This program, aufs is free software; you can redistribute it and/or modify
14355 + * it under the terms of the GNU General Public License as published by
14356 + * the Free Software Foundation; either version 2 of the License, or
14357 + * (at your option) any later version.
14358 + *
14359 + * This program is distributed in the hope that it will be useful,
14360 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14361 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14362 + * GNU General Public License for more details.
14363 + *
14364 + * You should have received a copy of the GNU General Public License
14365 + * along with this program; if not, write to the Free Software
14366 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14367 + */
14368 +
14369 +/*
14370 + * inode operations
14371 + */
14372 +
14373 +#ifndef __AUFS_INODE_H__
14374 +#define __AUFS_INODE_H__
14375 +
14376 +#ifdef __KERNEL__
14377 +
14378 +#include <linux/fsnotify.h>
14379 +#include "rwsem.h"
14380 +
14381 +struct vfsmount;
14382 +
14383 +struct au_hnotify {
14384 +#ifdef CONFIG_AUFS_HNOTIFY
14385 +#ifdef CONFIG_AUFS_HFSNOTIFY
14386 +       /* never use fsnotify_add_vfsmount_mark() */
14387 +       struct fsnotify_mark            hn_mark;
14388 +#endif
14389 +       struct inode                    *hn_aufs_inode; /* no get/put */
14390 +#endif
14391 +} ____cacheline_aligned_in_smp;
14392 +
14393 +struct au_hinode {
14394 +       struct inode            *hi_inode;
14395 +       aufs_bindex_t           hi_id;
14396 +#ifdef CONFIG_AUFS_HNOTIFY
14397 +       struct au_hnotify       *hi_notify;
14398 +#endif
14399 +
14400 +       /* reference to the copied-up whiteout with get/put */
14401 +       struct dentry           *hi_whdentry;
14402 +};
14403 +
14404 +struct au_vdir;
14405 +struct au_iinfo {
14406 +       atomic_t                ii_generation;
14407 +       struct super_block      *ii_hsb1;       /* no get/put */
14408 +
14409 +       struct au_rwsem         ii_rwsem;
14410 +       aufs_bindex_t           ii_bstart, ii_bend;
14411 +       __u32                   ii_higen;
14412 +       struct au_hinode        *ii_hinode;
14413 +       struct au_vdir          *ii_vdir;
14414 +};
14415 +
14416 +struct au_icntnr {
14417 +       struct au_iinfo iinfo;
14418 +       struct inode vfs_inode;
14419 +} ____cacheline_aligned_in_smp;
14420 +
14421 +/* au_pin flags */
14422 +#define AuPin_DI_LOCKED                1
14423 +#define AuPin_MNT_WRITE                (1 << 1)
14424 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
14425 +#define au_fset_pin(flags, name) \
14426 +       do { (flags) |= AuPin_##name; } while (0)
14427 +#define au_fclr_pin(flags, name) \
14428 +       do { (flags) &= ~AuPin_##name; } while (0)
14429 +
14430 +struct au_pin {
14431 +       /* input */
14432 +       struct dentry *dentry;
14433 +       unsigned int udba;
14434 +       unsigned char lsc_di, lsc_hi, flags;
14435 +       aufs_bindex_t bindex;
14436 +
14437 +       /* output */
14438 +       struct dentry *parent;
14439 +       struct au_hinode *hdir;
14440 +       struct vfsmount *h_mnt;
14441 +};
14442 +
14443 +/* ---------------------------------------------------------------------- */
14444 +
14445 +static inline struct au_iinfo *au_ii(struct inode *inode)
14446 +{
14447 +       struct au_iinfo *iinfo;
14448 +
14449 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
14450 +       if (iinfo->ii_hinode)
14451 +               return iinfo;
14452 +       return NULL; /* debugging bad_inode case */
14453 +}
14454 +
14455 +/* ---------------------------------------------------------------------- */
14456 +
14457 +/* inode.c */
14458 +struct inode *au_igrab(struct inode *inode);
14459 +int au_refresh_hinode_self(struct inode *inode);
14460 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
14461 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14462 +          unsigned int d_type, ino_t *ino);
14463 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
14464 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
14465 +              struct inode *inode);
14466 +int au_test_h_perm(struct inode *h_inode, int mask);
14467 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
14468 +
14469 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
14470 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
14471 +{
14472 +#ifdef CONFIG_AUFS_SHWH
14473 +       return au_ino(sb, bindex, h_ino, d_type, ino);
14474 +#else
14475 +       return 0;
14476 +#endif
14477 +}
14478 +
14479 +/* i_op.c */
14480 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
14481 +
14482 +/* au_wr_dir flags */
14483 +#define AuWrDir_ADD_ENTRY      1
14484 +#define AuWrDir_ISDIR          (1 << 1)
14485 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
14486 +#define au_fset_wrdir(flags, name) \
14487 +       do { (flags) |= AuWrDir_##name; } while (0)
14488 +#define au_fclr_wrdir(flags, name) \
14489 +       do { (flags) &= ~AuWrDir_##name; } while (0)
14490 +
14491 +struct au_wr_dir_args {
14492 +       aufs_bindex_t force_btgt;
14493 +       unsigned char flags;
14494 +};
14495 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
14496 +             struct au_wr_dir_args *args);
14497 +
14498 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
14499 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
14500 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
14501 +                unsigned int udba, unsigned char flags);
14502 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
14503 +          unsigned int udba, unsigned char flags) __must_check;
14504 +int au_do_pin(struct au_pin *pin) __must_check;
14505 +void au_unpin(struct au_pin *pin);
14506 +
14507 +/* i_op_add.c */
14508 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
14509 +              struct dentry *h_parent, int isdir);
14510 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
14511 +              dev_t dev);
14512 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
14513 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
14514 +               struct nameidata *nd);
14515 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
14516 +             struct dentry *dentry);
14517 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
14518 +
14519 +/* i_op_del.c */
14520 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
14521 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
14522 +              struct dentry *h_parent, int isdir);
14523 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
14524 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
14525 +
14526 +/* i_op_ren.c */
14527 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
14528 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
14529 +               struct inode *dir, struct dentry *dentry);
14530 +
14531 +/* iinfo.c */
14532 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
14533 +void au_hiput(struct au_hinode *hinode);
14534 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
14535 +                 struct dentry *h_wh);
14536 +unsigned int au_hi_flags(struct inode *inode, int isdir);
14537 +
14538 +/* hinode flags */
14539 +#define AuHi_XINO      1
14540 +#define AuHi_HNOTIFY   (1 << 1)
14541 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
14542 +#define au_fset_hi(flags, name) \
14543 +       do { (flags) |= AuHi_##name; } while (0)
14544 +#define au_fclr_hi(flags, name) \
14545 +       do { (flags) &= ~AuHi_##name; } while (0)
14546 +
14547 +#ifndef CONFIG_AUFS_HNOTIFY
14548 +#undef AuHi_HNOTIFY
14549 +#define AuHi_HNOTIFY   0
14550 +#endif
14551 +
14552 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
14553 +                  struct inode *h_inode, unsigned int flags);
14554 +
14555 +void au_update_iigen(struct inode *inode);
14556 +void au_update_ibrange(struct inode *inode, int do_put_zero);
14557 +
14558 +void au_icntnr_init_once(void *_c);
14559 +int au_iinfo_init(struct inode *inode);
14560 +void au_iinfo_fin(struct inode *inode);
14561 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
14562 +
14563 +#ifdef CONFIG_PROC_FS
14564 +/* plink.c */
14565 +int au_plink_maint(struct super_block *sb, int flags);
14566 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
14567 +int au_plink_maint_enter(struct super_block *sb);
14568 +#ifdef CONFIG_AUFS_DEBUG
14569 +void au_plink_list(struct super_block *sb);
14570 +#else
14571 +AuStubVoid(au_plink_list, struct super_block *sb)
14572 +#endif
14573 +int au_plink_test(struct inode *inode);
14574 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
14575 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
14576 +                    struct dentry *h_dentry);
14577 +void au_plink_put(struct super_block *sb, int verbose);
14578 +void au_plink_clean(struct super_block *sb, int verbose);
14579 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
14580 +#else
14581 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
14582 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
14583 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
14584 +AuStubVoid(au_plink_list, struct super_block *sb);
14585 +AuStubInt0(au_plink_test, struct inode *inode);
14586 +AuStub(struct dentry *, au_plink_lkup, return NULL,
14587 +       struct inode *inode, aufs_bindex_t bindex);
14588 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
14589 +          struct dentry *h_dentry);
14590 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
14591 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
14592 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
14593 +#endif /* CONFIG_PROC_FS */
14594 +
14595 +/* ---------------------------------------------------------------------- */
14596 +
14597 +/* lock subclass for iinfo */
14598 +enum {
14599 +       AuLsc_II_CHILD,         /* child first */
14600 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
14601 +       AuLsc_II_CHILD3,        /* copyup dirs */
14602 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
14603 +       AuLsc_II_PARENT2,
14604 +       AuLsc_II_PARENT3,       /* copyup dirs */
14605 +       AuLsc_II_NEW_CHILD
14606 +};
14607 +
14608 +/*
14609 + * ii_read_lock_child, ii_write_lock_child,
14610 + * ii_read_lock_child2, ii_write_lock_child2,
14611 + * ii_read_lock_child3, ii_write_lock_child3,
14612 + * ii_read_lock_parent, ii_write_lock_parent,
14613 + * ii_read_lock_parent2, ii_write_lock_parent2,
14614 + * ii_read_lock_parent3, ii_write_lock_parent3,
14615 + * ii_read_lock_new_child, ii_write_lock_new_child,
14616 + */
14617 +#define AuReadLockFunc(name, lsc) \
14618 +static inline void ii_read_lock_##name(struct inode *i) \
14619 +{ \
14620 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
14621 +}
14622 +
14623 +#define AuWriteLockFunc(name, lsc) \
14624 +static inline void ii_write_lock_##name(struct inode *i) \
14625 +{ \
14626 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
14627 +}
14628 +
14629 +#define AuRWLockFuncs(name, lsc) \
14630 +       AuReadLockFunc(name, lsc) \
14631 +       AuWriteLockFunc(name, lsc)
14632 +
14633 +AuRWLockFuncs(child, CHILD);
14634 +AuRWLockFuncs(child2, CHILD2);
14635 +AuRWLockFuncs(child3, CHILD3);
14636 +AuRWLockFuncs(parent, PARENT);
14637 +AuRWLockFuncs(parent2, PARENT2);
14638 +AuRWLockFuncs(parent3, PARENT3);
14639 +AuRWLockFuncs(new_child, NEW_CHILD);
14640 +
14641 +#undef AuReadLockFunc
14642 +#undef AuWriteLockFunc
14643 +#undef AuRWLockFuncs
14644 +
14645 +/*
14646 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
14647 + */
14648 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
14649 +
14650 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
14651 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
14652 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
14653 +
14654 +/* ---------------------------------------------------------------------- */
14655 +
14656 +static inline void au_icntnr_init(struct au_icntnr *c)
14657 +{
14658 +#ifdef CONFIG_AUFS_DEBUG
14659 +       c->vfs_inode.i_mode = 0;
14660 +#endif
14661 +}
14662 +
14663 +static inline unsigned int au_iigen(struct inode *inode)
14664 +{
14665 +       return atomic_read(&au_ii(inode)->ii_generation);
14666 +}
14667 +
14668 +/* tiny test for inode number */
14669 +/* tmpfs generation is too rough */
14670 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
14671 +{
14672 +       struct au_iinfo *iinfo;
14673 +
14674 +       iinfo = au_ii(inode);
14675 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
14676 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
14677 +                && iinfo->ii_higen == h_inode->i_generation);
14678 +}
14679 +
14680 +static inline void au_iigen_dec(struct inode *inode)
14681 +{
14682 +       atomic_dec(&au_ii(inode)->ii_generation);
14683 +}
14684 +
14685 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
14686 +{
14687 +       int err;
14688 +
14689 +       err = 0;
14690 +       if (unlikely(inode && au_iigen(inode) != sigen))
14691 +               err = -EIO;
14692 +
14693 +       return err;
14694 +}
14695 +
14696 +/* ---------------------------------------------------------------------- */
14697 +
14698 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
14699 +                                       aufs_bindex_t bindex)
14700 +{
14701 +       IiMustAnyLock(inode);
14702 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
14703 +}
14704 +
14705 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
14706 +{
14707 +       IiMustAnyLock(inode);
14708 +       return au_ii(inode)->ii_bstart;
14709 +}
14710 +
14711 +static inline aufs_bindex_t au_ibend(struct inode *inode)
14712 +{
14713 +       IiMustAnyLock(inode);
14714 +       return au_ii(inode)->ii_bend;
14715 +}
14716 +
14717 +static inline struct au_vdir *au_ivdir(struct inode *inode)
14718 +{
14719 +       IiMustAnyLock(inode);
14720 +       return au_ii(inode)->ii_vdir;
14721 +}
14722 +
14723 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
14724 +{
14725 +       IiMustAnyLock(inode);
14726 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
14727 +}
14728 +
14729 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
14730 +{
14731 +       IiMustWriteLock(inode);
14732 +       au_ii(inode)->ii_bstart = bindex;
14733 +}
14734 +
14735 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
14736 +{
14737 +       IiMustWriteLock(inode);
14738 +       au_ii(inode)->ii_bend = bindex;
14739 +}
14740 +
14741 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
14742 +{
14743 +       IiMustWriteLock(inode);
14744 +       au_ii(inode)->ii_vdir = vdir;
14745 +}
14746 +
14747 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
14748 +{
14749 +       IiMustAnyLock(inode);
14750 +       return au_ii(inode)->ii_hinode + bindex;
14751 +}
14752 +
14753 +/* ---------------------------------------------------------------------- */
14754 +
14755 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
14756 +{
14757 +       if (pin)
14758 +               return pin->parent;
14759 +       return NULL;
14760 +}
14761 +
14762 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
14763 +{
14764 +       if (pin && pin->hdir)
14765 +               return pin->hdir->hi_inode;
14766 +       return NULL;
14767 +}
14768 +
14769 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
14770 +{
14771 +       if (pin)
14772 +               return pin->hdir;
14773 +       return NULL;
14774 +}
14775 +
14776 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
14777 +{
14778 +       if (pin)
14779 +               pin->dentry = dentry;
14780 +}
14781 +
14782 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
14783 +                                          unsigned char lflag)
14784 +{
14785 +       if (pin) {
14786 +               if (lflag)
14787 +                       au_fset_pin(pin->flags, DI_LOCKED);
14788 +               else
14789 +                       au_fclr_pin(pin->flags, DI_LOCKED);
14790 +       }
14791 +}
14792 +
14793 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
14794 +{
14795 +       if (pin) {
14796 +               dput(pin->parent);
14797 +               pin->parent = dget(parent);
14798 +       }
14799 +}
14800 +
14801 +/* ---------------------------------------------------------------------- */
14802 +
14803 +struct au_branch;
14804 +#ifdef CONFIG_AUFS_HNOTIFY
14805 +struct au_hnotify_op {
14806 +       void (*ctl)(struct au_hinode *hinode, int do_set);
14807 +       int (*alloc)(struct au_hinode *hinode);
14808 +
14809 +       /*
14810 +        * if it returns true, the the caller should free hinode->hi_notify,
14811 +        * otherwise ->free() frees it.
14812 +        */
14813 +       int (*free)(struct au_hinode *hinode,
14814 +                   struct au_hnotify *hn) __must_check;
14815 +
14816 +       void (*fin)(void);
14817 +       int (*init)(void);
14818 +
14819 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
14820 +       void (*fin_br)(struct au_branch *br);
14821 +       int (*init_br)(struct au_branch *br, int perm);
14822 +};
14823 +
14824 +/* hnotify.c */
14825 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
14826 +void au_hn_free(struct au_hinode *hinode);
14827 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
14828 +void au_hn_reset(struct inode *inode, unsigned int flags);
14829 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
14830 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
14831 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
14832 +int au_hnotify_init_br(struct au_branch *br, int perm);
14833 +void au_hnotify_fin_br(struct au_branch *br);
14834 +int __init au_hnotify_init(void);
14835 +void au_hnotify_fin(void);
14836 +
14837 +/* hfsnotify.c */
14838 +extern const struct au_hnotify_op au_hnotify_op;
14839 +
14840 +static inline
14841 +void au_hn_init(struct au_hinode *hinode)
14842 +{
14843 +       hinode->hi_notify = NULL;
14844 +}
14845 +
14846 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
14847 +{
14848 +       return hinode->hi_notify;
14849 +}
14850 +
14851 +#else
14852 +static inline
14853 +int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
14854 +               struct inode *inode __maybe_unused)
14855 +{
14856 +       return -EOPNOTSUPP;
14857 +}
14858 +
14859 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
14860 +{
14861 +       return NULL;
14862 +}
14863 +
14864 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
14865 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
14866 +          int do_set __maybe_unused)
14867 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
14868 +          unsigned int flags __maybe_unused)
14869 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
14870 +          struct au_branch *br __maybe_unused,
14871 +          int perm __maybe_unused)
14872 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
14873 +          int perm __maybe_unused)
14874 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
14875 +AuStubInt0(__init au_hnotify_init, void)
14876 +AuStubVoid(au_hnotify_fin, void)
14877 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
14878 +#endif /* CONFIG_AUFS_HNOTIFY */
14879 +
14880 +static inline void au_hn_suspend(struct au_hinode *hdir)
14881 +{
14882 +       au_hn_ctl(hdir, /*do_set*/0);
14883 +}
14884 +
14885 +static inline void au_hn_resume(struct au_hinode *hdir)
14886 +{
14887 +       au_hn_ctl(hdir, /*do_set*/1);
14888 +}
14889 +
14890 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
14891 +{
14892 +       mutex_lock(&hdir->hi_inode->i_mutex);
14893 +       au_hn_suspend(hdir);
14894 +}
14895 +
14896 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
14897 +                                         unsigned int sc __maybe_unused)
14898 +{
14899 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
14900 +       au_hn_suspend(hdir);
14901 +}
14902 +
14903 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
14904 +{
14905 +       au_hn_resume(hdir);
14906 +       mutex_unlock(&hdir->hi_inode->i_mutex);
14907 +}
14908 +
14909 +#endif /* __KERNEL__ */
14910 +#endif /* __AUFS_INODE_H__ */
14911 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
14912 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
14913 +++ linux/fs/aufs/ioctl.c       2012-03-19 12:24:13.683134508 +0100
14914 @@ -0,0 +1,196 @@
14915 +/*
14916 + * Copyright (C) 2005-2012 Junjiro R. Okajima
14917 + *
14918 + * This program, aufs is free software; you can redistribute it and/or modify
14919 + * it under the terms of the GNU General Public License as published by
14920 + * the Free Software Foundation; either version 2 of the License, or
14921 + * (at your option) any later version.
14922 + *
14923 + * This program is distributed in the hope that it will be useful,
14924 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14925 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14926 + * GNU General Public License for more details.
14927 + *
14928 + * You should have received a copy of the GNU General Public License
14929 + * along with this program; if not, write to the Free Software
14930 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14931 + */
14932 +
14933 +/*
14934 + * ioctl
14935 + * plink-management and readdir in userspace.
14936 + * assist the pathconf(3) wrapper library.
14937 + */
14938 +
14939 +#include "aufs.h"
14940 +
14941 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
14942 +{
14943 +       int err, fd;
14944 +       aufs_bindex_t wbi, bindex, bend;
14945 +       struct file *h_file;
14946 +       struct super_block *sb;
14947 +       struct dentry *root;
14948 +       struct au_branch *br;
14949 +       struct aufs_wbr_fd wbrfd = {
14950 +               .oflags = au_dir_roflags,
14951 +               .brid   = -1
14952 +       };
14953 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
14954 +               | O_NOATIME | O_CLOEXEC;
14955 +
14956 +       AuDebugOn(wbrfd.oflags & ~valid);
14957 +
14958 +       if (arg) {
14959 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
14960 +               if (unlikely(err)) {
14961 +                       err = -EFAULT;
14962 +                       goto out;
14963 +               }
14964 +
14965 +               err = -EINVAL;
14966 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
14967 +               wbrfd.oflags |= au_dir_roflags;
14968 +               AuDbg("0%o\n", wbrfd.oflags);
14969 +               if (unlikely(wbrfd.oflags & ~valid))
14970 +                       goto out;
14971 +       }
14972 +
14973 +       fd = get_unused_fd();
14974 +       err = fd;
14975 +       if (unlikely(fd < 0))
14976 +               goto out;
14977 +
14978 +       h_file = ERR_PTR(-EINVAL);
14979 +       wbi = 0;
14980 +       br = NULL;
14981 +       sb = path->dentry->d_sb;
14982 +       root = sb->s_root;
14983 +       aufs_read_lock(root, AuLock_IR);
14984 +       bend = au_sbend(sb);
14985 +       if (wbrfd.brid >= 0) {
14986 +               wbi = au_br_index(sb, wbrfd.brid);
14987 +               if (unlikely(wbi < 0 || wbi > bend))
14988 +                       goto out_unlock;
14989 +       }
14990 +
14991 +       h_file = ERR_PTR(-ENOENT);
14992 +       br = au_sbr(sb, wbi);
14993 +       if (!au_br_writable(br->br_perm)) {
14994 +               if (arg)
14995 +                       goto out_unlock;
14996 +
14997 +               bindex = wbi + 1;
14998 +               wbi = -1;
14999 +               for (; bindex <= bend; bindex++) {
15000 +                       br = au_sbr(sb, bindex);
15001 +                       if (au_br_writable(br->br_perm)) {
15002 +                               wbi = bindex;
15003 +                               br = au_sbr(sb, wbi);
15004 +                               break;
15005 +                       }
15006 +               }
15007 +       }
15008 +       AuDbg("wbi %d\n", wbi);
15009 +       if (wbi >= 0)
15010 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL);
15011 +
15012 +out_unlock:
15013 +       aufs_read_unlock(root, AuLock_IR);
15014 +       err = PTR_ERR(h_file);
15015 +       if (IS_ERR(h_file))
15016 +               goto out_fd;
15017 +
15018 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
15019 +       fd_install(fd, h_file);
15020 +       err = fd;
15021 +       goto out; /* success */
15022 +
15023 +out_fd:
15024 +       put_unused_fd(fd);
15025 +out:
15026 +       AuTraceErr(err);
15027 +       return err;
15028 +}
15029 +
15030 +/* ---------------------------------------------------------------------- */
15031 +
15032 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
15033 +{
15034 +       long err;
15035 +
15036 +       switch (cmd) {
15037 +       case AUFS_CTL_RDU:
15038 +       case AUFS_CTL_RDU_INO:
15039 +               err = au_rdu_ioctl(file, cmd, arg);
15040 +               break;
15041 +
15042 +       case AUFS_CTL_WBR_FD:
15043 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15044 +               break;
15045 +
15046 +       case AUFS_CTL_IBUSY:
15047 +               err = au_ibusy_ioctl(file, arg);
15048 +               break;
15049 +
15050 +       default:
15051 +               /* do not call the lower */
15052 +               AuDbg("0x%x\n", cmd);
15053 +               err = -ENOTTY;
15054 +       }
15055 +
15056 +       AuTraceErr(err);
15057 +       return err;
15058 +}
15059 +
15060 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
15061 +{
15062 +       long err;
15063 +
15064 +       switch (cmd) {
15065 +       case AUFS_CTL_WBR_FD:
15066 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15067 +               break;
15068 +
15069 +       default:
15070 +               /* do not call the lower */
15071 +               AuDbg("0x%x\n", cmd);
15072 +               err = -ENOTTY;
15073 +       }
15074 +
15075 +       AuTraceErr(err);
15076 +       return err;
15077 +}
15078 +
15079 +#ifdef CONFIG_COMPAT
15080 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15081 +                          unsigned long arg)
15082 +{
15083 +       long err;
15084 +
15085 +       switch (cmd) {
15086 +       case AUFS_CTL_RDU:
15087 +       case AUFS_CTL_RDU_INO:
15088 +               err = au_rdu_compat_ioctl(file, cmd, arg);
15089 +               break;
15090 +
15091 +       case AUFS_CTL_IBUSY:
15092 +               err = au_ibusy_compat_ioctl(file, arg);
15093 +               break;
15094 +
15095 +       default:
15096 +               err = aufs_ioctl_dir(file, cmd, arg);
15097 +       }
15098 +
15099 +       AuTraceErr(err);
15100 +       return err;
15101 +}
15102 +
15103 +#if 0 /* unused yet */
15104 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15105 +                             unsigned long arg)
15106 +{
15107 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
15108 +}
15109 +#endif
15110 +#endif
15111 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
15112 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
15113 +++ linux/fs/aufs/i_op_add.c    2012-03-19 12:24:13.683134508 +0100
15114 @@ -0,0 +1,712 @@
15115 +/*
15116 + * Copyright (C) 2005-2012 Junjiro R. Okajima
15117 + *
15118 + * This program, aufs is free software; you can redistribute it and/or modify
15119 + * it under the terms of the GNU General Public License as published by
15120 + * the Free Software Foundation; either version 2 of the License, or
15121 + * (at your option) any later version.
15122 + *
15123 + * This program is distributed in the hope that it will be useful,
15124 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15125 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15126 + * GNU General Public License for more details.
15127 + *
15128 + * You should have received a copy of the GNU General Public License
15129 + * along with this program; if not, write to the Free Software
15130 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15131 + */
15132 +
15133 +/*
15134 + * inode operations (add entry)
15135 + */
15136 +
15137 +#include "aufs.h"
15138 +
15139 +/*
15140 + * final procedure of adding a new entry, except link(2).
15141 + * remove whiteout, instantiate, copyup the parent dir's times and size
15142 + * and update version.
15143 + * if it failed, re-create the removed whiteout.
15144 + */
15145 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
15146 +                 struct dentry *wh_dentry, struct dentry *dentry)
15147 +{
15148 +       int err, rerr;
15149 +       aufs_bindex_t bwh;
15150 +       struct path h_path;
15151 +       struct inode *inode, *h_dir;
15152 +       struct dentry *wh;
15153 +
15154 +       bwh = -1;
15155 +       if (wh_dentry) {
15156 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
15157 +               IMustLock(h_dir);
15158 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
15159 +               bwh = au_dbwh(dentry);
15160 +               h_path.dentry = wh_dentry;
15161 +               h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
15162 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
15163 +                                         dentry);
15164 +               if (unlikely(err))
15165 +                       goto out;
15166 +       }
15167 +
15168 +       inode = au_new_inode(dentry, /*must_new*/1);
15169 +       if (!IS_ERR(inode)) {
15170 +               d_instantiate(dentry, inode);
15171 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
15172 +               IMustLock(dir);
15173 +               if (au_ibstart(dir) == au_dbstart(dentry))
15174 +                       au_cpup_attr_timesizes(dir);
15175 +               dir->i_version++;
15176 +               return 0; /* success */
15177 +       }
15178 +
15179 +       err = PTR_ERR(inode);
15180 +       if (!wh_dentry)
15181 +               goto out;
15182 +
15183 +       /* revert */
15184 +       /* dir inode is locked */
15185 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
15186 +       rerr = PTR_ERR(wh);
15187 +       if (IS_ERR(wh)) {
15188 +               AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
15189 +                       AuDLNPair(dentry), err, rerr);
15190 +               err = -EIO;
15191 +       } else
15192 +               dput(wh);
15193 +
15194 +out:
15195 +       return err;
15196 +}
15197 +
15198 +static int au_d_may_add(struct dentry *dentry)
15199 +{
15200 +       int err;
15201 +
15202 +       err = 0;
15203 +       if (unlikely(d_unhashed(dentry)))
15204 +               err = -ENOENT;
15205 +       if (unlikely(dentry->d_inode))
15206 +               err = -EEXIST;
15207 +       return err;
15208 +}
15209 +
15210 +/*
15211 + * simple tests for the adding inode operations.
15212 + * following the checks in vfs, plus the parent-child relationship.
15213 + */
15214 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15215 +              struct dentry *h_parent, int isdir)
15216 +{
15217 +       int err;
15218 +       umode_t h_mode;
15219 +       struct dentry *h_dentry;
15220 +       struct inode *h_inode;
15221 +
15222 +       err = -ENAMETOOLONG;
15223 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15224 +               goto out;
15225 +
15226 +       h_dentry = au_h_dptr(dentry, bindex);
15227 +       h_inode = h_dentry->d_inode;
15228 +       if (!dentry->d_inode) {
15229 +               err = -EEXIST;
15230 +               if (unlikely(h_inode))
15231 +                       goto out;
15232 +       } else {
15233 +               /* rename(2) case */
15234 +               err = -EIO;
15235 +               if (unlikely(!h_inode || !h_inode->i_nlink))
15236 +                       goto out;
15237 +
15238 +               h_mode = h_inode->i_mode;
15239 +               if (!isdir) {
15240 +                       err = -EISDIR;
15241 +                       if (unlikely(S_ISDIR(h_mode)))
15242 +                               goto out;
15243 +               } else if (unlikely(!S_ISDIR(h_mode))) {
15244 +                       err = -ENOTDIR;
15245 +                       goto out;
15246 +               }
15247 +       }
15248 +
15249 +       err = 0;
15250 +       /* expected parent dir is locked */
15251 +       if (unlikely(h_parent != h_dentry->d_parent))
15252 +               err = -EIO;
15253 +
15254 +out:
15255 +       AuTraceErr(err);
15256 +       return err;
15257 +}
15258 +
15259 +/*
15260 + * initial procedure of adding a new entry.
15261 + * prepare writable branch and the parent dir, lock it,
15262 + * and lookup whiteout for the new entry.
15263 + */
15264 +static struct dentry*
15265 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
15266 +                 struct dentry *src_dentry, struct au_pin *pin,
15267 +                 struct au_wr_dir_args *wr_dir_args)
15268 +{
15269 +       struct dentry *wh_dentry, *h_parent;
15270 +       struct super_block *sb;
15271 +       struct au_branch *br;
15272 +       int err;
15273 +       unsigned int udba;
15274 +       aufs_bindex_t bcpup;
15275 +
15276 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15277 +
15278 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
15279 +       bcpup = err;
15280 +       wh_dentry = ERR_PTR(err);
15281 +       if (unlikely(err < 0))
15282 +               goto out;
15283 +
15284 +       sb = dentry->d_sb;
15285 +       udba = au_opt_udba(sb);
15286 +       err = au_pin(pin, dentry, bcpup, udba,
15287 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15288 +       wh_dentry = ERR_PTR(err);
15289 +       if (unlikely(err))
15290 +               goto out;
15291 +
15292 +       h_parent = au_pinned_h_parent(pin);
15293 +       if (udba != AuOpt_UDBA_NONE
15294 +           && au_dbstart(dentry) == bcpup)
15295 +               err = au_may_add(dentry, bcpup, h_parent,
15296 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
15297 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15298 +               err = -ENAMETOOLONG;
15299 +       wh_dentry = ERR_PTR(err);
15300 +       if (unlikely(err))
15301 +               goto out_unpin;
15302 +
15303 +       br = au_sbr(sb, bcpup);
15304 +       if (dt) {
15305 +               struct path tmp = {
15306 +                       .dentry = h_parent,
15307 +                       .mnt    = br->br_mnt
15308 +               };
15309 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
15310 +       }
15311 +
15312 +       wh_dentry = NULL;
15313 +       if (bcpup != au_dbwh(dentry))
15314 +               goto out; /* success */
15315 +
15316 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
15317 +
15318 +out_unpin:
15319 +       if (IS_ERR(wh_dentry))
15320 +               au_unpin(pin);
15321 +out:
15322 +       return wh_dentry;
15323 +}
15324 +
15325 +/* ---------------------------------------------------------------------- */
15326 +
15327 +enum { Mknod, Symlink, Creat };
15328 +struct simple_arg {
15329 +       int type;
15330 +       union {
15331 +               struct {
15332 +                       umode_t mode;
15333 +                       struct nameidata *nd;
15334 +               } c;
15335 +               struct {
15336 +                       const char *symname;
15337 +               } s;
15338 +               struct {
15339 +                       umode_t mode;
15340 +                       dev_t dev;
15341 +               } m;
15342 +       } u;
15343 +};
15344 +
15345 +static int add_simple(struct inode *dir, struct dentry *dentry,
15346 +                     struct simple_arg *arg)
15347 +{
15348 +       int err;
15349 +       aufs_bindex_t bstart;
15350 +       unsigned char created;
15351 +       struct au_dtime dt;
15352 +       struct au_pin pin;
15353 +       struct path h_path;
15354 +       struct dentry *wh_dentry, *parent;
15355 +       struct inode *h_dir;
15356 +       struct au_wr_dir_args wr_dir_args = {
15357 +               .force_btgt     = -1,
15358 +               .flags          = AuWrDir_ADD_ENTRY
15359 +       };
15360 +
15361 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15362 +       IMustLock(dir);
15363 +
15364 +       parent = dentry->d_parent; /* dir inode is locked */
15365 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
15366 +       if (unlikely(err))
15367 +               goto out;
15368 +       err = au_d_may_add(dentry);
15369 +       if (unlikely(err))
15370 +               goto out_unlock;
15371 +       di_write_lock_parent(parent);
15372 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL, &pin,
15373 +                                     &wr_dir_args);
15374 +       err = PTR_ERR(wh_dentry);
15375 +       if (IS_ERR(wh_dentry))
15376 +               goto out_parent;
15377 +
15378 +       bstart = au_dbstart(dentry);
15379 +       h_path.dentry = au_h_dptr(dentry, bstart);
15380 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
15381 +       h_dir = au_pinned_h_dir(&pin);
15382 +       switch (arg->type) {
15383 +       case Creat:
15384 +               err = vfsub_create(h_dir, &h_path, arg->u.c.mode);
15385 +               break;
15386 +       case Symlink:
15387 +               err = vfsub_symlink(h_dir, &h_path, arg->u.s.symname);
15388 +               break;
15389 +       case Mknod:
15390 +               err = vfsub_mknod(h_dir, &h_path, arg->u.m.mode, arg->u.m.dev);
15391 +               break;
15392 +       default:
15393 +               BUG();
15394 +       }
15395 +       created = !err;
15396 +       if (!err)
15397 +               err = epilog(dir, bstart, wh_dentry, dentry);
15398 +
15399 +       /* revert */
15400 +       if (unlikely(created && err && h_path.dentry->d_inode)) {
15401 +               int rerr;
15402 +               rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
15403 +               if (rerr) {
15404 +                       AuIOErr("%.*s revert failure(%d, %d)\n",
15405 +                               AuDLNPair(dentry), err, rerr);
15406 +                       err = -EIO;
15407 +               }
15408 +               au_dtime_revert(&dt);
15409 +       }
15410 +
15411 +       au_unpin(&pin);
15412 +       dput(wh_dentry);
15413 +
15414 +out_parent:
15415 +       di_write_unlock(parent);
15416 +out_unlock:
15417 +       if (unlikely(err)) {
15418 +               au_update_dbstart(dentry);
15419 +               d_drop(dentry);
15420 +       }
15421 +       aufs_read_unlock(dentry, AuLock_DW);
15422 +out:
15423 +       return err;
15424 +}
15425 +
15426 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
15427 +              dev_t dev)
15428 +{
15429 +       struct simple_arg arg = {
15430 +               .type = Mknod,
15431 +               .u.m = {
15432 +                       .mode   = mode,
15433 +                       .dev    = dev
15434 +               }
15435 +       };
15436 +       return add_simple(dir, dentry, &arg);
15437 +}
15438 +
15439 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
15440 +{
15441 +       struct simple_arg arg = {
15442 +               .type = Symlink,
15443 +               .u.s.symname = symname
15444 +       };
15445 +       return add_simple(dir, dentry, &arg);
15446 +}
15447 +
15448 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
15449 +               struct nameidata *nd)
15450 +{
15451 +       struct simple_arg arg = {
15452 +               .type = Creat,
15453 +               .u.c = {
15454 +                       .mode   = mode,
15455 +                       .nd     = nd
15456 +               }
15457 +       };
15458 +       return add_simple(dir, dentry, &arg);
15459 +}
15460 +
15461 +/* ---------------------------------------------------------------------- */
15462 +
15463 +struct au_link_args {
15464 +       aufs_bindex_t bdst, bsrc;
15465 +       struct au_pin pin;
15466 +       struct path h_path;
15467 +       struct dentry *src_parent, *parent;
15468 +};
15469 +
15470 +static int au_cpup_before_link(struct dentry *src_dentry,
15471 +                              struct au_link_args *a)
15472 +{
15473 +       int err;
15474 +       struct dentry *h_src_dentry;
15475 +       struct mutex *h_mtx;
15476 +       struct file *h_file;
15477 +
15478 +       di_read_lock_parent(a->src_parent, AuLock_IR);
15479 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
15480 +       if (unlikely(err))
15481 +               goto out;
15482 +
15483 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
15484 +       h_mtx = &h_src_dentry->d_inode->i_mutex;
15485 +       err = au_pin(&a->pin, src_dentry, a->bdst,
15486 +                    au_opt_udba(src_dentry->d_sb),
15487 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15488 +       if (unlikely(err))
15489 +               goto out;
15490 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
15491 +       h_file = au_h_open_pre(src_dentry, a->bsrc);
15492 +       if (IS_ERR(h_file)) {
15493 +               err = PTR_ERR(h_file);
15494 +               h_file = NULL;
15495 +       } else
15496 +               err = au_sio_cpup_simple(src_dentry, a->bdst, -1,
15497 +                                        AuCpup_DTIME /* | AuCpup_KEEPLINO */);
15498 +       mutex_unlock(h_mtx);
15499 +       au_h_open_post(src_dentry, a->bsrc, h_file);
15500 +       au_unpin(&a->pin);
15501 +
15502 +out:
15503 +       di_read_unlock(a->src_parent, AuLock_IR);
15504 +       return err;
15505 +}
15506 +
15507 +static int au_cpup_or_link(struct dentry *src_dentry, struct au_link_args *a)
15508 +{
15509 +       int err;
15510 +       unsigned char plink;
15511 +       struct inode *h_inode, *inode;
15512 +       struct dentry *h_src_dentry;
15513 +       struct super_block *sb;
15514 +       struct file *h_file;
15515 +
15516 +       plink = 0;
15517 +       h_inode = NULL;
15518 +       sb = src_dentry->d_sb;
15519 +       inode = src_dentry->d_inode;
15520 +       if (au_ibstart(inode) <= a->bdst)
15521 +               h_inode = au_h_iptr(inode, a->bdst);
15522 +       if (!h_inode || !h_inode->i_nlink) {
15523 +               /* copyup src_dentry as the name of dentry. */
15524 +               au_set_dbstart(src_dentry, a->bdst);
15525 +               au_set_h_dptr(src_dentry, a->bdst, dget(a->h_path.dentry));
15526 +               h_inode = au_h_dptr(src_dentry, a->bsrc)->d_inode;
15527 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
15528 +               h_file = au_h_open_pre(src_dentry, a->bsrc);
15529 +               if (IS_ERR(h_file)) {
15530 +                       err = PTR_ERR(h_file);
15531 +                       h_file = NULL;
15532 +               } else
15533 +                       err = au_sio_cpup_single(src_dentry, a->bdst, a->bsrc,
15534 +                                                -1, AuCpup_KEEPLINO,
15535 +                                                a->parent);
15536 +               mutex_unlock(&h_inode->i_mutex);
15537 +               au_h_open_post(src_dentry, a->bsrc, h_file);
15538 +               au_set_h_dptr(src_dentry, a->bdst, NULL);
15539 +               au_set_dbstart(src_dentry, a->bsrc);
15540 +       } else {
15541 +               /* the inode of src_dentry already exists on a.bdst branch */
15542 +               h_src_dentry = d_find_alias(h_inode);
15543 +               if (!h_src_dentry && au_plink_test(inode)) {
15544 +                       plink = 1;
15545 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
15546 +                       err = PTR_ERR(h_src_dentry);
15547 +                       if (IS_ERR(h_src_dentry))
15548 +                               goto out;
15549 +
15550 +                       if (unlikely(!h_src_dentry->d_inode)) {
15551 +                               dput(h_src_dentry);
15552 +                               h_src_dentry = NULL;
15553 +                       }
15554 +
15555 +               }
15556 +               if (h_src_dentry) {
15557 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
15558 +                                        &a->h_path);
15559 +                       dput(h_src_dentry);
15560 +               } else {
15561 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
15562 +                               h_inode->i_ino, a->bdst);
15563 +                       err = -EIO;
15564 +               }
15565 +       }
15566 +
15567 +       if (!err && !plink)
15568 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
15569 +
15570 +out:
15571 +       AuTraceErr(err);
15572 +       return err;
15573 +}
15574 +
15575 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
15576 +             struct dentry *dentry)
15577 +{
15578 +       int err, rerr;
15579 +       struct au_dtime dt;
15580 +       struct au_link_args *a;
15581 +       struct dentry *wh_dentry, *h_src_dentry;
15582 +       struct inode *inode;
15583 +       struct super_block *sb;
15584 +       struct au_wr_dir_args wr_dir_args = {
15585 +               /* .force_btgt  = -1, */
15586 +               .flags          = AuWrDir_ADD_ENTRY
15587 +       };
15588 +
15589 +       IMustLock(dir);
15590 +       inode = src_dentry->d_inode;
15591 +       IMustLock(inode);
15592 +
15593 +       err = -ENOMEM;
15594 +       a = kzalloc(sizeof(*a), GFP_NOFS);
15595 +       if (unlikely(!a))
15596 +               goto out;
15597 +
15598 +       a->parent = dentry->d_parent; /* dir inode is locked */
15599 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
15600 +                                       AuLock_NOPLM | AuLock_GEN);
15601 +       if (unlikely(err))
15602 +               goto out_kfree;
15603 +       err = au_d_hashed_positive(src_dentry);
15604 +       if (unlikely(err))
15605 +               goto out_unlock;
15606 +       err = au_d_may_add(dentry);
15607 +       if (unlikely(err))
15608 +               goto out_unlock;
15609 +
15610 +       a->src_parent = dget_parent(src_dentry);
15611 +       wr_dir_args.force_btgt = au_ibstart(inode);
15612 +
15613 +       di_write_lock_parent(a->parent);
15614 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
15615 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
15616 +                                     &wr_dir_args);
15617 +       err = PTR_ERR(wh_dentry);
15618 +       if (IS_ERR(wh_dentry))
15619 +               goto out_parent;
15620 +
15621 +       err = 0;
15622 +       sb = dentry->d_sb;
15623 +       a->bdst = au_dbstart(dentry);
15624 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
15625 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
15626 +       a->bsrc = au_ibstart(inode);
15627 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
15628 +       if (!h_src_dentry) {
15629 +               a->bsrc = au_dbstart(src_dentry);
15630 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
15631 +               AuDebugOn(!h_src_dentry);
15632 +       } else if (IS_ERR(h_src_dentry))
15633 +               goto out_parent;
15634 +
15635 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
15636 +               if (a->bdst < a->bsrc
15637 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
15638 +                       err = au_cpup_or_link(src_dentry, a);
15639 +               else
15640 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
15641 +                                        &a->h_path);
15642 +               dput(h_src_dentry);
15643 +       } else {
15644 +               /*
15645 +                * copyup src_dentry to the branch we process,
15646 +                * and then link(2) to it.
15647 +                */
15648 +               dput(h_src_dentry);
15649 +               if (a->bdst < a->bsrc
15650 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
15651 +                       au_unpin(&a->pin);
15652 +                       di_write_unlock(a->parent);
15653 +                       err = au_cpup_before_link(src_dentry, a);
15654 +                       di_write_lock_parent(a->parent);
15655 +                       if (!err)
15656 +                               err = au_pin(&a->pin, dentry, a->bdst,
15657 +                                            au_opt_udba(sb),
15658 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15659 +                       if (unlikely(err))
15660 +                               goto out_wh;
15661 +               }
15662 +               if (!err) {
15663 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
15664 +                       err = -ENOENT;
15665 +                       if (h_src_dentry && h_src_dentry->d_inode)
15666 +                               err = vfsub_link(h_src_dentry,
15667 +                                                au_pinned_h_dir(&a->pin),
15668 +                                                &a->h_path);
15669 +               }
15670 +       }
15671 +       if (unlikely(err))
15672 +               goto out_unpin;
15673 +
15674 +       if (wh_dentry) {
15675 +               a->h_path.dentry = wh_dentry;
15676 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
15677 +                                         dentry);
15678 +               if (unlikely(err))
15679 +                       goto out_revert;
15680 +       }
15681 +
15682 +       dir->i_version++;
15683 +       if (au_ibstart(dir) == au_dbstart(dentry))
15684 +               au_cpup_attr_timesizes(dir);
15685 +       inc_nlink(inode);
15686 +       inode->i_ctime = dir->i_ctime;
15687 +       d_instantiate(dentry, au_igrab(inode));
15688 +       if (d_unhashed(a->h_path.dentry))
15689 +               /* some filesystem calls d_drop() */
15690 +               d_drop(dentry);
15691 +       goto out_unpin; /* success */
15692 +
15693 +out_revert:
15694 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
15695 +       if (unlikely(rerr)) {
15696 +               AuIOErr("%.*s reverting failed(%d, %d)\n",
15697 +                       AuDLNPair(dentry), err, rerr);
15698 +               err = -EIO;
15699 +       }
15700 +       au_dtime_revert(&dt);
15701 +out_unpin:
15702 +       au_unpin(&a->pin);
15703 +out_wh:
15704 +       dput(wh_dentry);
15705 +out_parent:
15706 +       di_write_unlock(a->parent);
15707 +       dput(a->src_parent);
15708 +out_unlock:
15709 +       if (unlikely(err)) {
15710 +               au_update_dbstart(dentry);
15711 +               d_drop(dentry);
15712 +       }
15713 +       aufs_read_and_write_unlock2(dentry, src_dentry);
15714 +out_kfree:
15715 +       kfree(a);
15716 +out:
15717 +       return err;
15718 +}
15719 +
15720 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
15721 +{
15722 +       int err, rerr;
15723 +       aufs_bindex_t bindex;
15724 +       unsigned char diropq;
15725 +       struct path h_path;
15726 +       struct dentry *wh_dentry, *parent, *opq_dentry;
15727 +       struct mutex *h_mtx;
15728 +       struct super_block *sb;
15729 +       struct {
15730 +               struct au_pin pin;
15731 +               struct au_dtime dt;
15732 +       } *a; /* reduce the stack usage */
15733 +       struct au_wr_dir_args wr_dir_args = {
15734 +               .force_btgt     = -1,
15735 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
15736 +       };
15737 +
15738 +       IMustLock(dir);
15739 +
15740 +       err = -ENOMEM;
15741 +       a = kmalloc(sizeof(*a), GFP_NOFS);
15742 +       if (unlikely(!a))
15743 +               goto out;
15744 +
15745 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
15746 +       if (unlikely(err))
15747 +               goto out_free;
15748 +       err = au_d_may_add(dentry);
15749 +       if (unlikely(err))
15750 +               goto out_unlock;
15751 +
15752 +       parent = dentry->d_parent; /* dir inode is locked */
15753 +       di_write_lock_parent(parent);
15754 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
15755 +                                     &a->pin, &wr_dir_args);
15756 +       err = PTR_ERR(wh_dentry);
15757 +       if (IS_ERR(wh_dentry))
15758 +               goto out_parent;
15759 +
15760 +       sb = dentry->d_sb;
15761 +       bindex = au_dbstart(dentry);
15762 +       h_path.dentry = au_h_dptr(dentry, bindex);
15763 +       h_path.mnt = au_sbr_mnt(sb, bindex);
15764 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
15765 +       if (unlikely(err))
15766 +               goto out_unpin;
15767 +
15768 +       /* make the dir opaque */
15769 +       diropq = 0;
15770 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
15771 +       if (wh_dentry
15772 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
15773 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
15774 +               opq_dentry = au_diropq_create(dentry, bindex);
15775 +               mutex_unlock(h_mtx);
15776 +               err = PTR_ERR(opq_dentry);
15777 +               if (IS_ERR(opq_dentry))
15778 +                       goto out_dir;
15779 +               dput(opq_dentry);
15780 +               diropq = 1;
15781 +       }
15782 +
15783 +       err = epilog(dir, bindex, wh_dentry, dentry);
15784 +       if (!err) {
15785 +               inc_nlink(dir);
15786 +               goto out_unpin; /* success */
15787 +       }
15788 +
15789 +       /* revert */
15790 +       if (diropq) {
15791 +               AuLabel(revert opq);
15792 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
15793 +               rerr = au_diropq_remove(dentry, bindex);
15794 +               mutex_unlock(h_mtx);
15795 +               if (rerr) {
15796 +                       AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
15797 +                               AuDLNPair(dentry), err, rerr);
15798 +                       err = -EIO;
15799 +               }
15800 +       }
15801 +
15802 +out_dir:
15803 +       AuLabel(revert dir);
15804 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
15805 +       if (rerr) {
15806 +               AuIOErr("%.*s reverting dir failed(%d, %d)\n",
15807 +                       AuDLNPair(dentry), err, rerr);
15808 +               err = -EIO;
15809 +       }
15810 +       au_dtime_revert(&a->dt);
15811 +out_unpin:
15812 +       au_unpin(&a->pin);
15813 +       dput(wh_dentry);
15814 +out_parent:
15815 +       di_write_unlock(parent);
15816 +out_unlock:
15817 +       if (unlikely(err)) {
15818 +               au_update_dbstart(dentry);
15819 +               d_drop(dentry);
15820 +       }
15821 +       aufs_read_unlock(dentry, AuLock_DW);
15822 +out_free:
15823 +       kfree(a);
15824 +out:
15825 +       return err;
15826 +}
15827 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
15828 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
15829 +++ linux/fs/aufs/i_op.c        2012-03-19 12:24:13.683134508 +0100
15830 @@ -0,0 +1,992 @@
15831 +/*
15832 + * Copyright (C) 2005-2012 Junjiro R. Okajima
15833 + *
15834 + * This program, aufs is free software; you can redistribute it and/or modify
15835 + * it under the terms of the GNU General Public License as published by
15836 + * the Free Software Foundation; either version 2 of the License, or
15837 + * (at your option) any later version.
15838 + *
15839 + * This program is distributed in the hope that it will be useful,
15840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15842 + * GNU General Public License for more details.
15843 + *
15844 + * You should have received a copy of the GNU General Public License
15845 + * along with this program; if not, write to the Free Software
15846 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15847 + */
15848 +
15849 +/*
15850 + * inode operations (except add/del/rename)
15851 + */
15852 +
15853 +#include <linux/device_cgroup.h>
15854 +#include <linux/fs_stack.h>
15855 +#include <linux/namei.h>
15856 +#include <linux/security.h>
15857 +#include "aufs.h"
15858 +
15859 +static int h_permission(struct inode *h_inode, int mask,
15860 +                       struct vfsmount *h_mnt, int brperm)
15861 +{
15862 +       int err;
15863 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
15864 +
15865 +       err = -EACCES;
15866 +       if ((write_mask && IS_IMMUTABLE(h_inode))
15867 +           || ((mask & MAY_EXEC)
15868 +               && S_ISREG(h_inode->i_mode)
15869 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
15870 +                   || !(h_inode->i_mode & S_IXUGO))))
15871 +               goto out;
15872 +
15873 +       /*
15874 +        * - skip the lower fs test in the case of write to ro branch.
15875 +        * - nfs dir permission write check is optimized, but a policy for
15876 +        *   link/rename requires a real check.
15877 +        */
15878 +       if ((write_mask && !au_br_writable(brperm))
15879 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
15880 +               && write_mask && !(mask & MAY_READ))
15881 +           || !h_inode->i_op->permission) {
15882 +               /* AuLabel(generic_permission); */
15883 +               err = generic_permission(h_inode, mask);
15884 +       } else {
15885 +               /* AuLabel(h_inode->permission); */
15886 +               err = h_inode->i_op->permission(h_inode, mask);
15887 +               AuTraceErr(err);
15888 +       }
15889 +
15890 +       if (!err)
15891 +               err = devcgroup_inode_permission(h_inode, mask);
15892 +       if (!err)
15893 +               err = security_inode_permission(h_inode, mask);
15894 +
15895 +#if 0
15896 +       if (!err) {
15897 +               /* todo: do we need to call ima_path_check()? */
15898 +               struct path h_path = {
15899 +                       .dentry =
15900 +                       .mnt    = h_mnt
15901 +               };
15902 +               err = ima_path_check(&h_path,
15903 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
15904 +                                    IMA_COUNT_LEAVE);
15905 +       }
15906 +#endif
15907 +
15908 +out:
15909 +       return err;
15910 +}
15911 +
15912 +static int aufs_permission(struct inode *inode, int mask)
15913 +{
15914 +       int err;
15915 +       aufs_bindex_t bindex, bend;
15916 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
15917 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
15918 +       struct inode *h_inode;
15919 +       struct super_block *sb;
15920 +       struct au_branch *br;
15921 +
15922 +       /* todo: support rcu-walk? */
15923 +       if (mask & MAY_NOT_BLOCK)
15924 +               return -ECHILD;
15925 +
15926 +       sb = inode->i_sb;
15927 +       si_read_lock(sb, AuLock_FLUSH);
15928 +       ii_read_lock_child(inode);
15929 +#if 0
15930 +       err = au_iigen_test(inode, au_sigen(sb));
15931 +       if (unlikely(err))
15932 +               goto out;
15933 +#endif
15934 +
15935 +       if (!isdir || write_mask) {
15936 +               err = au_busy_or_stale();
15937 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
15938 +               if (unlikely(!h_inode
15939 +                            || (h_inode->i_mode & S_IFMT)
15940 +                            != (inode->i_mode & S_IFMT)))
15941 +                       goto out;
15942 +
15943 +               err = 0;
15944 +               bindex = au_ibstart(inode);
15945 +               br = au_sbr(sb, bindex);
15946 +               err = h_permission(h_inode, mask, br->br_mnt, br->br_perm);
15947 +               if (write_mask
15948 +                   && !err
15949 +                   && !special_file(h_inode->i_mode)) {
15950 +                       /* test whether the upper writable branch exists */
15951 +                       err = -EROFS;
15952 +                       for (; bindex >= 0; bindex--)
15953 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
15954 +                                       err = 0;
15955 +                                       break;
15956 +                               }
15957 +               }
15958 +               goto out;
15959 +       }
15960 +
15961 +       /* non-write to dir */
15962 +       err = 0;
15963 +       bend = au_ibend(inode);
15964 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
15965 +               h_inode = au_h_iptr(inode, bindex);
15966 +               if (h_inode) {
15967 +                       err = au_busy_or_stale();
15968 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
15969 +                               break;
15970 +
15971 +                       br = au_sbr(sb, bindex);
15972 +                       err = h_permission(h_inode, mask, br->br_mnt,
15973 +                                          br->br_perm);
15974 +               }
15975 +       }
15976 +
15977 +out:
15978 +       ii_read_unlock(inode);
15979 +       si_read_unlock(sb);
15980 +       return err;
15981 +}
15982 +
15983 +/* ---------------------------------------------------------------------- */
15984 +
15985 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
15986 +                                 struct nameidata *nd)
15987 +{
15988 +       struct dentry *ret, *parent;
15989 +       struct inode *inode;
15990 +       struct super_block *sb;
15991 +       int err, npositive, lc_idx;
15992 +
15993 +       IMustLock(dir);
15994 +
15995 +       sb = dir->i_sb;
15996 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15997 +       ret = ERR_PTR(err);
15998 +       if (unlikely(err))
15999 +               goto out;
16000 +
16001 +       ret = ERR_PTR(-ENAMETOOLONG);
16002 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16003 +               goto out_si;
16004 +       err = au_di_init(dentry);
16005 +       ret = ERR_PTR(err);
16006 +       if (unlikely(err))
16007 +               goto out_si;
16008 +
16009 +       inode = NULL;
16010 +       npositive = 0; /* suppress a warning */
16011 +       parent = dentry->d_parent; /* dir inode is locked */
16012 +       di_read_lock_parent(parent, AuLock_IR);
16013 +       err = au_alive_dir(parent);
16014 +       if (!err)
16015 +               err = au_digen_test(parent, au_sigen(sb));
16016 +       if (!err) {
16017 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
16018 +                                          /*type*/0, nd);
16019 +               err = npositive;
16020 +       }
16021 +       di_read_unlock(parent, AuLock_IR);
16022 +       ret = ERR_PTR(err);
16023 +       if (unlikely(err < 0))
16024 +               goto out_unlock;
16025 +
16026 +       if (npositive) {
16027 +               inode = au_new_inode(dentry, /*must_new*/0);
16028 +               ret = (void *)inode;
16029 +       }
16030 +       if (IS_ERR(inode)) {
16031 +               inode = NULL;
16032 +               goto out_unlock;
16033 +       }
16034 +
16035 +       ret = d_splice_alias(inode, dentry);
16036 +       if (unlikely(IS_ERR(ret) && inode)) {
16037 +               ii_write_unlock(inode);
16038 +               lc_idx = AuLcNonDir_IIINFO;
16039 +               if (S_ISLNK(inode->i_mode))
16040 +                       lc_idx = AuLcSymlink_IIINFO;
16041 +               else if (S_ISDIR(inode->i_mode))
16042 +                       lc_idx = AuLcDir_IIINFO;
16043 +               au_rw_class(&au_ii(inode)->ii_rwsem, au_lc_key + lc_idx);
16044 +               iput(inode);
16045 +       }
16046 +
16047 +out_unlock:
16048 +       di_write_unlock(dentry);
16049 +       if (unlikely(IS_ERR(ret) && inode)) {
16050 +               lc_idx = AuLcNonDir_DIINFO;
16051 +               if (S_ISLNK(inode->i_mode))
16052 +                       lc_idx = AuLcSymlink_DIINFO;
16053 +               else if (S_ISDIR(inode->i_mode))
16054 +                       lc_idx = AuLcDir_DIINFO;
16055 +               au_rw_class(&au_di(dentry)->di_rwsem, au_lc_key + lc_idx);
16056 +       }
16057 +out_si:
16058 +       si_read_unlock(sb);
16059 +out:
16060 +       return ret;
16061 +}
16062 +
16063 +/* ---------------------------------------------------------------------- */
16064 +
16065 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
16066 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
16067 +                         aufs_bindex_t bstart)
16068 +{
16069 +       int err;
16070 +       struct dentry *h_parent;
16071 +       struct inode *h_dir;
16072 +
16073 +       if (add_entry)
16074 +               IMustLock(parent->d_inode);
16075 +       else
16076 +               di_write_lock_parent(parent);
16077 +
16078 +       err = 0;
16079 +       if (!au_h_dptr(parent, bcpup)) {
16080 +               if (bstart < bcpup)
16081 +                       err = au_cpdown_dirs(dentry, bcpup);
16082 +               else
16083 +                       err = au_cpup_dirs(dentry, bcpup);
16084 +       }
16085 +       if (!err && add_entry) {
16086 +               h_parent = au_h_dptr(parent, bcpup);
16087 +               h_dir = h_parent->d_inode;
16088 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
16089 +               err = au_lkup_neg(dentry, bcpup);
16090 +               /* todo: no unlock here */
16091 +               mutex_unlock(&h_dir->i_mutex);
16092 +
16093 +               AuDbg("bcpup %d\n", bcpup);
16094 +               if (!err) {
16095 +                       if (!dentry->d_inode)
16096 +                               au_set_h_dptr(dentry, bstart, NULL);
16097 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
16098 +               }
16099 +       }
16100 +
16101 +       if (!add_entry)
16102 +               di_write_unlock(parent);
16103 +       if (!err)
16104 +               err = bcpup; /* success */
16105 +
16106 +       AuTraceErr(err);
16107 +       return err;
16108 +}
16109 +
16110 +/*
16111 + * decide the branch and the parent dir where we will create a new entry.
16112 + * returns new bindex or an error.
16113 + * copyup the parent dir if needed.
16114 + */
16115 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16116 +             struct au_wr_dir_args *args)
16117 +{
16118 +       int err;
16119 +       aufs_bindex_t bcpup, bstart, src_bstart;
16120 +       const unsigned char add_entry = !!au_ftest_wrdir(args->flags,
16121 +                                                        ADD_ENTRY);
16122 +       struct super_block *sb;
16123 +       struct dentry *parent;
16124 +       struct au_sbinfo *sbinfo;
16125 +
16126 +       sb = dentry->d_sb;
16127 +       sbinfo = au_sbi(sb);
16128 +       parent = dget_parent(dentry);
16129 +       bstart = au_dbstart(dentry);
16130 +       bcpup = bstart;
16131 +       if (args->force_btgt < 0) {
16132 +               if (src_dentry) {
16133 +                       src_bstart = au_dbstart(src_dentry);
16134 +                       if (src_bstart < bstart)
16135 +                               bcpup = src_bstart;
16136 +               } else if (add_entry) {
16137 +                       err = AuWbrCreate(sbinfo, dentry,
16138 +                                         au_ftest_wrdir(args->flags, ISDIR));
16139 +                       bcpup = err;
16140 +               }
16141 +
16142 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
16143 +                       if (add_entry)
16144 +                               err = AuWbrCopyup(sbinfo, dentry);
16145 +                       else {
16146 +                               if (!IS_ROOT(dentry)) {
16147 +                                       di_read_lock_parent(parent, !AuLock_IR);
16148 +                                       err = AuWbrCopyup(sbinfo, dentry);
16149 +                                       di_read_unlock(parent, !AuLock_IR);
16150 +                               } else
16151 +                                       err = AuWbrCopyup(sbinfo, dentry);
16152 +                       }
16153 +                       bcpup = err;
16154 +                       if (unlikely(err < 0))
16155 +                               goto out;
16156 +               }
16157 +       } else {
16158 +               bcpup = args->force_btgt;
16159 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
16160 +       }
16161 +
16162 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
16163 +       err = bcpup;
16164 +       if (bcpup == bstart)
16165 +               goto out; /* success */
16166 +
16167 +       /* copyup the new parent into the branch we process */
16168 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
16169 +       if (err >= 0) {
16170 +               if (!dentry->d_inode) {
16171 +                       au_set_h_dptr(dentry, bstart, NULL);
16172 +                       au_set_dbstart(dentry, bcpup);
16173 +                       au_set_dbend(dentry, bcpup);
16174 +               }
16175 +               AuDebugOn(add_entry && !au_h_dptr(dentry, bcpup));
16176 +       }
16177 +
16178 +out:
16179 +       dput(parent);
16180 +       return err;
16181 +}
16182 +
16183 +/* ---------------------------------------------------------------------- */
16184 +
16185 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
16186 +{
16187 +       if (pin && pin->parent)
16188 +               return au_h_dptr(pin->parent, pin->bindex);
16189 +       return NULL;
16190 +}
16191 +
16192 +void au_unpin(struct au_pin *p)
16193 +{
16194 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
16195 +               mnt_drop_write(p->h_mnt);
16196 +       if (!p->hdir)
16197 +               return;
16198 +
16199 +       au_hn_imtx_unlock(p->hdir);
16200 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16201 +               di_read_unlock(p->parent, AuLock_IR);
16202 +       iput(p->hdir->hi_inode);
16203 +       dput(p->parent);
16204 +       p->parent = NULL;
16205 +       p->hdir = NULL;
16206 +       p->h_mnt = NULL;
16207 +}
16208 +
16209 +int au_do_pin(struct au_pin *p)
16210 +{
16211 +       int err;
16212 +       struct super_block *sb;
16213 +       struct dentry *h_dentry, *h_parent;
16214 +       struct au_branch *br;
16215 +       struct inode *h_dir;
16216 +
16217 +       err = 0;
16218 +       sb = p->dentry->d_sb;
16219 +       br = au_sbr(sb, p->bindex);
16220 +       if (IS_ROOT(p->dentry)) {
16221 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
16222 +                       p->h_mnt = br->br_mnt;
16223 +                       err = mnt_want_write(p->h_mnt);
16224 +                       if (unlikely(err)) {
16225 +                               au_fclr_pin(p->flags, MNT_WRITE);
16226 +                               goto out_err;
16227 +                       }
16228 +               }
16229 +               goto out;
16230 +       }
16231 +
16232 +       h_dentry = NULL;
16233 +       if (p->bindex <= au_dbend(p->dentry))
16234 +               h_dentry = au_h_dptr(p->dentry, p->bindex);
16235 +
16236 +       p->parent = dget_parent(p->dentry);
16237 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16238 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
16239 +
16240 +       h_dir = NULL;
16241 +       h_parent = au_h_dptr(p->parent, p->bindex);
16242 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
16243 +       if (p->hdir)
16244 +               h_dir = p->hdir->hi_inode;
16245 +
16246 +       /*
16247 +        * udba case, or
16248 +        * if DI_LOCKED is not set, then p->parent may be different
16249 +        * and h_parent can be NULL.
16250 +        */
16251 +       if (unlikely(!p->hdir || !h_dir || !h_parent)) {
16252 +               err = -EBUSY;
16253 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
16254 +                       di_read_unlock(p->parent, AuLock_IR);
16255 +               dput(p->parent);
16256 +               p->parent = NULL;
16257 +               goto out_err;
16258 +       }
16259 +
16260 +       au_igrab(h_dir);
16261 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
16262 +
16263 +       if (unlikely(p->hdir->hi_inode != h_parent->d_inode)) {
16264 +               err = -EBUSY;
16265 +               goto out_unpin;
16266 +       }
16267 +       if (h_dentry) {
16268 +               err = au_h_verify(h_dentry, p->udba, h_dir, h_parent, br);
16269 +               if (unlikely(err)) {
16270 +                       au_fclr_pin(p->flags, MNT_WRITE);
16271 +                       goto out_unpin;
16272 +               }
16273 +       }
16274 +
16275 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
16276 +               p->h_mnt = br->br_mnt;
16277 +               err = mnt_want_write(p->h_mnt);
16278 +               if (unlikely(err)) {
16279 +                       au_fclr_pin(p->flags, MNT_WRITE);
16280 +                       goto out_unpin;
16281 +               }
16282 +       }
16283 +       goto out; /* success */
16284 +
16285 +out_unpin:
16286 +       au_unpin(p);
16287 +out_err:
16288 +       pr_err("err %d\n", err);
16289 +       err = au_busy_or_stale();
16290 +out:
16291 +       return err;
16292 +}
16293 +
16294 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
16295 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16296 +                unsigned int udba, unsigned char flags)
16297 +{
16298 +       p->dentry = dentry;
16299 +       p->udba = udba;
16300 +       p->lsc_di = lsc_di;
16301 +       p->lsc_hi = lsc_hi;
16302 +       p->flags = flags;
16303 +       p->bindex = bindex;
16304 +
16305 +       p->parent = NULL;
16306 +       p->hdir = NULL;
16307 +       p->h_mnt = NULL;
16308 +}
16309 +
16310 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16311 +          unsigned int udba, unsigned char flags)
16312 +{
16313 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
16314 +                   udba, flags);
16315 +       return au_do_pin(pin);
16316 +}
16317 +
16318 +/* ---------------------------------------------------------------------- */
16319 +
16320 +/*
16321 + * ->setattr() and ->getattr() are called in various cases.
16322 + * chmod, stat: dentry is revalidated.
16323 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
16324 + *               unhashed.
16325 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
16326 + */
16327 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
16328 +static int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
16329 +{
16330 +       int err;
16331 +       struct inode *inode;
16332 +       struct dentry *parent;
16333 +
16334 +       err = 0;
16335 +       inode = dentry->d_inode;
16336 +       if (au_digen_test(dentry, sigen)) {
16337 +               parent = dget_parent(dentry);
16338 +               di_read_lock_parent(parent, AuLock_IR);
16339 +               err = au_refresh_dentry(dentry, parent);
16340 +               di_read_unlock(parent, AuLock_IR);
16341 +               dput(parent);
16342 +       }
16343 +
16344 +       AuTraceErr(err);
16345 +       return err;
16346 +}
16347 +
16348 +#define AuIcpup_DID_CPUP       1
16349 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16350 +#define au_fset_icpup(flags, name) \
16351 +       do { (flags) |= AuIcpup_##name; } while (0)
16352 +#define au_fclr_icpup(flags, name) \
16353 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16354 +
16355 +struct au_icpup_args {
16356 +       unsigned char flags;
16357 +       unsigned char pin_flags;
16358 +       aufs_bindex_t btgt;
16359 +       unsigned int udba;
16360 +       struct au_pin pin;
16361 +       struct path h_path;
16362 +       struct inode *h_inode;
16363 +};
16364 +
16365 +static int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16366 +                           struct au_icpup_args *a)
16367 +{
16368 +       int err;
16369 +       loff_t sz;
16370 +       aufs_bindex_t bstart, ibstart;
16371 +       struct dentry *hi_wh, *parent;
16372 +       struct inode *inode;
16373 +       struct file *h_file;
16374 +       struct au_wr_dir_args wr_dir_args = {
16375 +               .force_btgt     = -1,
16376 +               .flags          = 0
16377 +       };
16378 +
16379 +       bstart = au_dbstart(dentry);
16380 +       inode = dentry->d_inode;
16381 +       if (S_ISDIR(inode->i_mode))
16382 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
16383 +       /* plink or hi_wh() case */
16384 +       ibstart = au_ibstart(inode);
16385 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
16386 +               wr_dir_args.force_btgt = ibstart;
16387 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
16388 +       if (unlikely(err < 0))
16389 +               goto out;
16390 +       a->btgt = err;
16391 +       if (err != bstart)
16392 +               au_fset_icpup(a->flags, DID_CPUP);
16393 +
16394 +       err = 0;
16395 +       a->pin_flags = AuPin_MNT_WRITE;
16396 +       parent = NULL;
16397 +       if (!IS_ROOT(dentry)) {
16398 +               au_fset_pin(a->pin_flags, DI_LOCKED);
16399 +               parent = dget_parent(dentry);
16400 +               di_write_lock_parent(parent);
16401 +       }
16402 +
16403 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
16404 +       if (unlikely(err))
16405 +               goto out_parent;
16406 +
16407 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
16408 +       a->h_inode = a->h_path.dentry->d_inode;
16409 +       mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
16410 +       sz = -1;
16411 +       if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
16412 +               sz = ia->ia_size;
16413 +
16414 +       h_file = NULL;
16415 +       hi_wh = NULL;
16416 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
16417 +               hi_wh = au_hi_wh(inode, a->btgt);
16418 +               if (!hi_wh) {
16419 +                       err = au_sio_cpup_wh(dentry, a->btgt, sz, /*file*/NULL);
16420 +                       if (unlikely(err))
16421 +                               goto out_unlock;
16422 +                       hi_wh = au_hi_wh(inode, a->btgt);
16423 +                       /* todo: revalidate hi_wh? */
16424 +               }
16425 +       }
16426 +
16427 +       if (parent) {
16428 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
16429 +               di_downgrade_lock(parent, AuLock_IR);
16430 +               dput(parent);
16431 +               parent = NULL;
16432 +       }
16433 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
16434 +               goto out; /* success */
16435 +
16436 +       if (!d_unhashed(dentry)) {
16437 +               h_file = au_h_open_pre(dentry, bstart);
16438 +               if (IS_ERR(h_file)) {
16439 +                       err = PTR_ERR(h_file);
16440 +                       h_file = NULL;
16441 +               } else
16442 +                       err = au_sio_cpup_simple(dentry, a->btgt, sz,
16443 +                                                AuCpup_DTIME);
16444 +               if (!err)
16445 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
16446 +       } else if (!hi_wh)
16447 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
16448 +       else
16449 +               a->h_path.dentry = hi_wh; /* do not dget here */
16450 +
16451 +out_unlock:
16452 +       mutex_unlock(&a->h_inode->i_mutex);
16453 +       au_h_open_post(dentry, bstart, h_file);
16454 +       a->h_inode = a->h_path.dentry->d_inode;
16455 +       if (!err) {
16456 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
16457 +               goto out; /* success */
16458 +       }
16459 +
16460 +       au_unpin(&a->pin);
16461 +out_parent:
16462 +       if (parent) {
16463 +               di_write_unlock(parent);
16464 +               dput(parent);
16465 +       }
16466 +out:
16467 +       return err;
16468 +}
16469 +
16470 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
16471 +{
16472 +       int err;
16473 +       struct inode *inode;
16474 +       struct super_block *sb;
16475 +       struct file *file;
16476 +       struct au_icpup_args *a;
16477 +
16478 +       inode = dentry->d_inode;
16479 +       IMustLock(inode);
16480 +
16481 +       err = -ENOMEM;
16482 +       a = kzalloc(sizeof(*a), GFP_NOFS);
16483 +       if (unlikely(!a))
16484 +               goto out;
16485 +
16486 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
16487 +               ia->ia_valid &= ~ATTR_MODE;
16488 +
16489 +       file = NULL;
16490 +       sb = dentry->d_sb;
16491 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16492 +       if (unlikely(err))
16493 +               goto out_kfree;
16494 +
16495 +       if (ia->ia_valid & ATTR_FILE) {
16496 +               /* currently ftruncate(2) only */
16497 +               AuDebugOn(!S_ISREG(inode->i_mode));
16498 +               file = ia->ia_file;
16499 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
16500 +               if (unlikely(err))
16501 +                       goto out_si;
16502 +               ia->ia_file = au_hf_top(file);
16503 +               a->udba = AuOpt_UDBA_NONE;
16504 +       } else {
16505 +               /* fchmod() doesn't pass ia_file */
16506 +               a->udba = au_opt_udba(sb);
16507 +               di_write_lock_child(dentry);
16508 +               /* no d_unlinked(), to set UDBA_NONE for root */
16509 +               if (d_unhashed(dentry))
16510 +                       a->udba = AuOpt_UDBA_NONE;
16511 +               if (a->udba != AuOpt_UDBA_NONE) {
16512 +                       AuDebugOn(IS_ROOT(dentry));
16513 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
16514 +                       if (unlikely(err))
16515 +                               goto out_dentry;
16516 +               }
16517 +       }
16518 +
16519 +       err = au_pin_and_icpup(dentry, ia, a);
16520 +       if (unlikely(err < 0))
16521 +               goto out_dentry;
16522 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
16523 +               ia->ia_file = NULL;
16524 +               ia->ia_valid &= ~ATTR_FILE;
16525 +       }
16526 +
16527 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
16528 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
16529 +           == (ATTR_MODE | ATTR_CTIME)) {
16530 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
16531 +               if (unlikely(err))
16532 +                       goto out_unlock;
16533 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
16534 +                  && (ia->ia_valid & ATTR_CTIME)) {
16535 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
16536 +               if (unlikely(err))
16537 +                       goto out_unlock;
16538 +       }
16539 +
16540 +       if (ia->ia_valid & ATTR_SIZE) {
16541 +               struct file *f;
16542 +
16543 +               if (ia->ia_size < i_size_read(inode))
16544 +                       /* unmap only */
16545 +                       truncate_setsize(inode, ia->ia_size);
16546 +
16547 +               f = NULL;
16548 +               if (ia->ia_valid & ATTR_FILE)
16549 +                       f = ia->ia_file;
16550 +               mutex_unlock(&a->h_inode->i_mutex);
16551 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
16552 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
16553 +       } else
16554 +               err = vfsub_notify_change(&a->h_path, ia);
16555 +       if (!err)
16556 +               au_cpup_attr_changeable(inode);
16557 +
16558 +out_unlock:
16559 +       mutex_unlock(&a->h_inode->i_mutex);
16560 +       au_unpin(&a->pin);
16561 +       if (unlikely(err))
16562 +               au_update_dbstart(dentry);
16563 +out_dentry:
16564 +       di_write_unlock(dentry);
16565 +       if (file) {
16566 +               fi_write_unlock(file);
16567 +               ia->ia_file = file;
16568 +               ia->ia_valid |= ATTR_FILE;
16569 +       }
16570 +out_si:
16571 +       si_read_unlock(sb);
16572 +out_kfree:
16573 +       kfree(a);
16574 +out:
16575 +       AuTraceErr(err);
16576 +       return err;
16577 +}
16578 +
16579 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
16580 +                            unsigned int nlink)
16581 +{
16582 +       unsigned int n;
16583 +
16584 +       inode->i_mode = st->mode;
16585 +       inode->i_uid = st->uid;
16586 +       inode->i_gid = st->gid;
16587 +       inode->i_atime = st->atime;
16588 +       inode->i_mtime = st->mtime;
16589 +       inode->i_ctime = st->ctime;
16590 +
16591 +       au_cpup_attr_nlink(inode, /*force*/0);
16592 +       if (S_ISDIR(inode->i_mode)) {
16593 +               n = inode->i_nlink;
16594 +               n -= nlink;
16595 +               n += st->nlink;
16596 +               /* 0 can happen */
16597 +               vfsub_set_nlink(inode, n);
16598 +       }
16599 +
16600 +       spin_lock(&inode->i_lock);
16601 +       inode->i_blocks = st->blocks;
16602 +       i_size_write(inode, st->size);
16603 +       spin_unlock(&inode->i_lock);
16604 +}
16605 +
16606 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
16607 +                       struct dentry *dentry, struct kstat *st)
16608 +{
16609 +       int err;
16610 +       unsigned int mnt_flags;
16611 +       aufs_bindex_t bindex;
16612 +       unsigned char udba_none, positive;
16613 +       struct super_block *sb, *h_sb;
16614 +       struct inode *inode;
16615 +       struct vfsmount *h_mnt;
16616 +       struct dentry *h_dentry;
16617 +
16618 +       sb = dentry->d_sb;
16619 +       inode = dentry->d_inode;
16620 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16621 +       if (unlikely(err))
16622 +               goto out;
16623 +       mnt_flags = au_mntflags(sb);
16624 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
16625 +
16626 +       /* support fstat(2) */
16627 +       if (!d_unlinked(dentry) && !udba_none) {
16628 +               unsigned int sigen = au_sigen(sb);
16629 +               err = au_digen_test(dentry, sigen);
16630 +               if (!err) {
16631 +                       di_read_lock_child(dentry, AuLock_IR);
16632 +                       err = au_dbrange_test(dentry);
16633 +                       if (unlikely(err))
16634 +                               goto out_unlock;
16635 +               } else {
16636 +                       AuDebugOn(IS_ROOT(dentry));
16637 +                       di_write_lock_child(dentry);
16638 +                       err = au_dbrange_test(dentry);
16639 +                       if (!err)
16640 +                               err = au_reval_for_attr(dentry, sigen);
16641 +                       di_downgrade_lock(dentry, AuLock_IR);
16642 +                       if (unlikely(err))
16643 +                               goto out_unlock;
16644 +               }
16645 +       } else
16646 +               di_read_lock_child(dentry, AuLock_IR);
16647 +
16648 +       bindex = au_ibstart(inode);
16649 +       h_mnt = au_sbr_mnt(sb, bindex);
16650 +       h_sb = h_mnt->mnt_sb;
16651 +       if (!au_test_fs_bad_iattr(h_sb) && udba_none)
16652 +               goto out_fill; /* success */
16653 +
16654 +       h_dentry = NULL;
16655 +       if (au_dbstart(dentry) == bindex)
16656 +               h_dentry = dget(au_h_dptr(dentry, bindex));
16657 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
16658 +               h_dentry = au_plink_lkup(inode, bindex);
16659 +               if (IS_ERR(h_dentry))
16660 +                       goto out_fill; /* pretending success */
16661 +       }
16662 +       /* illegally overlapped or something */
16663 +       if (unlikely(!h_dentry))
16664 +               goto out_fill; /* pretending success */
16665 +
16666 +       positive = !!h_dentry->d_inode;
16667 +       if (positive)
16668 +               err = vfs_getattr(h_mnt, h_dentry, st);
16669 +       dput(h_dentry);
16670 +       if (!err) {
16671 +               if (positive)
16672 +                       au_refresh_iattr(inode, st, h_dentry->d_inode->i_nlink);
16673 +               goto out_fill; /* success */
16674 +       }
16675 +       AuTraceErr(err);
16676 +       goto out_unlock;
16677 +
16678 +out_fill:
16679 +       generic_fillattr(inode, st);
16680 +out_unlock:
16681 +       di_read_unlock(dentry, AuLock_IR);
16682 +       si_read_unlock(sb);
16683 +out:
16684 +       AuTraceErr(err);
16685 +       return err;
16686 +}
16687 +
16688 +/* ---------------------------------------------------------------------- */
16689 +
16690 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
16691 +                     int bufsiz)
16692 +{
16693 +       int err;
16694 +       struct super_block *sb;
16695 +       struct dentry *h_dentry;
16696 +
16697 +       err = -EINVAL;
16698 +       h_dentry = au_h_dptr(dentry, bindex);
16699 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
16700 +               goto out;
16701 +
16702 +       err = security_inode_readlink(h_dentry);
16703 +       if (unlikely(err))
16704 +               goto out;
16705 +
16706 +       sb = dentry->d_sb;
16707 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
16708 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
16709 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
16710 +       }
16711 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
16712 +
16713 +out:
16714 +       return err;
16715 +}
16716 +
16717 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
16718 +{
16719 +       int err;
16720 +
16721 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
16722 +       if (unlikely(err))
16723 +               goto out;
16724 +       err = au_d_hashed_positive(dentry);
16725 +       if (!err)
16726 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
16727 +       aufs_read_unlock(dentry, AuLock_IR);
16728 +
16729 +out:
16730 +       return err;
16731 +}
16732 +
16733 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
16734 +{
16735 +       int err;
16736 +       mm_segment_t old_fs;
16737 +       union {
16738 +               char *k;
16739 +               char __user *u;
16740 +       } buf;
16741 +
16742 +       err = -ENOMEM;
16743 +       buf.k = __getname_gfp(GFP_NOFS);
16744 +       if (unlikely(!buf.k))
16745 +               goto out;
16746 +
16747 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
16748 +       if (unlikely(err))
16749 +               goto out_name;
16750 +
16751 +       err = au_d_hashed_positive(dentry);
16752 +       if (!err) {
16753 +               old_fs = get_fs();
16754 +               set_fs(KERNEL_DS);
16755 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
16756 +               set_fs(old_fs);
16757 +       }
16758 +       aufs_read_unlock(dentry, AuLock_IR);
16759 +
16760 +       if (err >= 0) {
16761 +               buf.k[err] = 0;
16762 +               /* will be freed by put_link */
16763 +               nd_set_link(nd, buf.k);
16764 +               return NULL; /* success */
16765 +       }
16766 +
16767 +out_name:
16768 +       __putname(buf.k);
16769 +out:
16770 +       path_put(&nd->path);
16771 +       AuTraceErr(err);
16772 +       return ERR_PTR(err);
16773 +}
16774 +
16775 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
16776 +                         struct nameidata *nd, void *cookie __maybe_unused)
16777 +{
16778 +       __putname(nd_get_link(nd));
16779 +}
16780 +
16781 +/* ---------------------------------------------------------------------- */
16782 +
16783 +static void aufs_truncate_range(struct inode *inode __maybe_unused,
16784 +                               loff_t start __maybe_unused,
16785 +                               loff_t end __maybe_unused)
16786 +{
16787 +       AuUnsupport();
16788 +}
16789 +
16790 +/* ---------------------------------------------------------------------- */
16791 +
16792 +struct inode_operations aufs_symlink_iop = {
16793 +       .permission     = aufs_permission,
16794 +       .setattr        = aufs_setattr,
16795 +       .getattr        = aufs_getattr,
16796 +       .readlink       = aufs_readlink,
16797 +       .follow_link    = aufs_follow_link,
16798 +       .put_link       = aufs_put_link
16799 +};
16800 +
16801 +struct inode_operations aufs_dir_iop = {
16802 +       .create         = aufs_create,
16803 +       .lookup         = aufs_lookup,
16804 +       .link           = aufs_link,
16805 +       .unlink         = aufs_unlink,
16806 +       .symlink        = aufs_symlink,
16807 +       .mkdir          = aufs_mkdir,
16808 +       .rmdir          = aufs_rmdir,
16809 +       .mknod          = aufs_mknod,
16810 +       .rename         = aufs_rename,
16811 +
16812 +       .permission     = aufs_permission,
16813 +       .setattr        = aufs_setattr,
16814 +       .getattr        = aufs_getattr
16815 +};
16816 +
16817 +struct inode_operations aufs_iop = {
16818 +       .permission     = aufs_permission,
16819 +       .setattr        = aufs_setattr,
16820 +       .getattr        = aufs_getattr,
16821 +       .truncate_range = aufs_truncate_range
16822 +};
16823 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
16824 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
16825 +++ linux/fs/aufs/i_op_del.c    2012-03-19 12:24:13.683134508 +0100
16826 @@ -0,0 +1,478 @@
16827 +/*
16828 + * Copyright (C) 2005-2012 Junjiro R. Okajima
16829 + *
16830 + * This program, aufs is free software; you can redistribute it and/or modify
16831 + * it under the terms of the GNU General Public License as published by
16832 + * the Free Software Foundation; either version 2 of the License, or
16833 + * (at your option) any later version.
16834 + *
16835 + * This program is distributed in the hope that it will be useful,
16836 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16837 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16838 + * GNU General Public License for more details.
16839 + *
16840 + * You should have received a copy of the GNU General Public License
16841 + * along with this program; if not, write to the Free Software
16842 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16843 + */
16844 +
16845 +/*
16846 + * inode operations (del entry)
16847 + */
16848 +
16849 +#include "aufs.h"
16850 +
16851 +/*
16852 + * decide if a new whiteout for @dentry is necessary or not.
16853 + * when it is necessary, prepare the parent dir for the upper branch whose
16854 + * branch index is @bcpup for creation. the actual creation of the whiteout will
16855 + * be done by caller.
16856 + * return value:
16857 + * 0: wh is unnecessary
16858 + * plus: wh is necessary
16859 + * minus: error
16860 + */
16861 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
16862 +{
16863 +       int need_wh, err;
16864 +       aufs_bindex_t bstart;
16865 +       struct super_block *sb;
16866 +
16867 +       sb = dentry->d_sb;
16868 +       bstart = au_dbstart(dentry);
16869 +       if (*bcpup < 0) {
16870 +               *bcpup = bstart;
16871 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
16872 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
16873 +                       *bcpup = err;
16874 +                       if (unlikely(err < 0))
16875 +                               goto out;
16876 +               }
16877 +       } else
16878 +               AuDebugOn(bstart < *bcpup
16879 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
16880 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
16881 +
16882 +       if (*bcpup != bstart) {
16883 +               err = au_cpup_dirs(dentry, *bcpup);
16884 +               if (unlikely(err))
16885 +                       goto out;
16886 +               need_wh = 1;
16887 +       } else {
16888 +               struct au_dinfo *dinfo, *tmp;
16889 +
16890 +               need_wh = -ENOMEM;
16891 +               dinfo = au_di(dentry);
16892 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
16893 +               if (tmp) {
16894 +                       au_di_cp(tmp, dinfo);
16895 +                       au_di_swap(tmp, dinfo);
16896 +                       /* returns the number of positive dentries */
16897 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0,
16898 +                                                /*nd*/NULL);
16899 +                       au_di_swap(tmp, dinfo);
16900 +                       au_rw_write_unlock(&tmp->di_rwsem);
16901 +                       au_di_free(tmp);
16902 +               }
16903 +       }
16904 +       AuDbg("need_wh %d\n", need_wh);
16905 +       err = need_wh;
16906 +
16907 +out:
16908 +       return err;
16909 +}
16910 +
16911 +/*
16912 + * simple tests for the del-entry operations.
16913 + * following the checks in vfs, plus the parent-child relationship.
16914 + */
16915 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16916 +              struct dentry *h_parent, int isdir)
16917 +{
16918 +       int err;
16919 +       umode_t h_mode;
16920 +       struct dentry *h_dentry, *h_latest;
16921 +       struct inode *h_inode;
16922 +
16923 +       h_dentry = au_h_dptr(dentry, bindex);
16924 +       h_inode = h_dentry->d_inode;
16925 +       if (dentry->d_inode) {
16926 +               err = -ENOENT;
16927 +               if (unlikely(!h_inode || !h_inode->i_nlink))
16928 +                       goto out;
16929 +
16930 +               h_mode = h_inode->i_mode;
16931 +               if (!isdir) {
16932 +                       err = -EISDIR;
16933 +                       if (unlikely(S_ISDIR(h_mode)))
16934 +                               goto out;
16935 +               } else if (unlikely(!S_ISDIR(h_mode))) {
16936 +                       err = -ENOTDIR;
16937 +                       goto out;
16938 +               }
16939 +       } else {
16940 +               /* rename(2) case */
16941 +               err = -EIO;
16942 +               if (unlikely(h_inode))
16943 +                       goto out;
16944 +       }
16945 +
16946 +       err = -ENOENT;
16947 +       /* expected parent dir is locked */
16948 +       if (unlikely(h_parent != h_dentry->d_parent))
16949 +               goto out;
16950 +       err = 0;
16951 +
16952 +       /*
16953 +        * rmdir a dir may break the consistency on some filesystem.
16954 +        * let's try heavy test.
16955 +        */
16956 +       err = -EACCES;
16957 +       if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
16958 +               goto out;
16959 +
16960 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
16961 +                                  au_sbr(dentry->d_sb, bindex));
16962 +       err = -EIO;
16963 +       if (IS_ERR(h_latest))
16964 +               goto out;
16965 +       if (h_latest == h_dentry)
16966 +               err = 0;
16967 +       dput(h_latest);
16968 +
16969 +out:
16970 +       return err;
16971 +}
16972 +
16973 +/*
16974 + * decide the branch where we operate for @dentry. the branch index will be set
16975 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
16976 + * dir for reverting.
16977 + * when a new whiteout is necessary, create it.
16978 + */
16979 +static struct dentry*
16980 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
16981 +                   struct au_dtime *dt, struct au_pin *pin)
16982 +{
16983 +       struct dentry *wh_dentry;
16984 +       struct super_block *sb;
16985 +       struct path h_path;
16986 +       int err, need_wh;
16987 +       unsigned int udba;
16988 +       aufs_bindex_t bcpup;
16989 +
16990 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
16991 +       wh_dentry = ERR_PTR(need_wh);
16992 +       if (unlikely(need_wh < 0))
16993 +               goto out;
16994 +
16995 +       sb = dentry->d_sb;
16996 +       udba = au_opt_udba(sb);
16997 +       bcpup = *rbcpup;
16998 +       err = au_pin(pin, dentry, bcpup, udba,
16999 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17000 +       wh_dentry = ERR_PTR(err);
17001 +       if (unlikely(err))
17002 +               goto out;
17003 +
17004 +       h_path.dentry = au_pinned_h_parent(pin);
17005 +       if (udba != AuOpt_UDBA_NONE
17006 +           && au_dbstart(dentry) == bcpup) {
17007 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
17008 +               wh_dentry = ERR_PTR(err);
17009 +               if (unlikely(err))
17010 +                       goto out_unpin;
17011 +       }
17012 +
17013 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
17014 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
17015 +       wh_dentry = NULL;
17016 +       if (!need_wh)
17017 +               goto out; /* success, no need to create whiteout */
17018 +
17019 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
17020 +       if (IS_ERR(wh_dentry))
17021 +               goto out_unpin;
17022 +
17023 +       /* returns with the parent is locked and wh_dentry is dget-ed */
17024 +       goto out; /* success */
17025 +
17026 +out_unpin:
17027 +       au_unpin(pin);
17028 +out:
17029 +       return wh_dentry;
17030 +}
17031 +
17032 +/*
17033 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
17034 + * in order to be revertible and save time for removing many child whiteouts
17035 + * under the dir.
17036 + * returns 1 when there are too many child whiteout and caller should remove
17037 + * them asynchronously. returns 0 when the number of children is enough small to
17038 + * remove now or the branch fs is a remote fs.
17039 + * otherwise return an error.
17040 + */
17041 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
17042 +                          struct au_nhash *whlist, struct inode *dir)
17043 +{
17044 +       int rmdir_later, err, dirwh;
17045 +       struct dentry *h_dentry;
17046 +       struct super_block *sb;
17047 +
17048 +       sb = dentry->d_sb;
17049 +       SiMustAnyLock(sb);
17050 +       h_dentry = au_h_dptr(dentry, bindex);
17051 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
17052 +       if (unlikely(err))
17053 +               goto out;
17054 +
17055 +       /* stop monitoring */
17056 +       au_hn_free(au_hi(dentry->d_inode, bindex));
17057 +
17058 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
17059 +               dirwh = au_sbi(sb)->si_dirwh;
17060 +               rmdir_later = (dirwh <= 1);
17061 +               if (!rmdir_later)
17062 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
17063 +                                                             dirwh);
17064 +               if (rmdir_later)
17065 +                       return rmdir_later;
17066 +       }
17067 +
17068 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
17069 +       if (unlikely(err)) {
17070 +               AuIOErr("rmdir %.*s, b%d failed, %d. ignored\n",
17071 +                       AuDLNPair(h_dentry), bindex, err);
17072 +               err = 0;
17073 +       }
17074 +
17075 +out:
17076 +       AuTraceErr(err);
17077 +       return err;
17078 +}
17079 +
17080 +/*
17081 + * final procedure for deleting a entry.
17082 + * maintain dentry and iattr.
17083 + */
17084 +static void epilog(struct inode *dir, struct dentry *dentry,
17085 +                  aufs_bindex_t bindex)
17086 +{
17087 +       struct inode *inode;
17088 +
17089 +       inode = dentry->d_inode;
17090 +       d_drop(dentry);
17091 +       inode->i_ctime = dir->i_ctime;
17092 +
17093 +       if (au_ibstart(dir) == bindex)
17094 +               au_cpup_attr_timesizes(dir);
17095 +       dir->i_version++;
17096 +}
17097 +
17098 +/*
17099 + * when an error happened, remove the created whiteout and revert everything.
17100 + */
17101 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
17102 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
17103 +                    struct dentry *dentry, struct au_dtime *dt)
17104 +{
17105 +       int rerr;
17106 +       struct path h_path = {
17107 +               .dentry = wh_dentry,
17108 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
17109 +       };
17110 +
17111 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
17112 +       if (!rerr) {
17113 +               au_set_dbwh(dentry, bwh);
17114 +               au_dtime_revert(dt);
17115 +               return 0;
17116 +       }
17117 +
17118 +       AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
17119 +               AuDLNPair(dentry), err, rerr);
17120 +       return -EIO;
17121 +}
17122 +
17123 +/* ---------------------------------------------------------------------- */
17124 +
17125 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
17126 +{
17127 +       int err;
17128 +       aufs_bindex_t bwh, bindex, bstart;
17129 +       struct au_dtime dt;
17130 +       struct au_pin pin;
17131 +       struct path h_path;
17132 +       struct inode *inode, *h_dir;
17133 +       struct dentry *parent, *wh_dentry;
17134 +
17135 +       IMustLock(dir);
17136 +
17137 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17138 +       if (unlikely(err))
17139 +               goto out;
17140 +       err = au_d_hashed_positive(dentry);
17141 +       if (unlikely(err))
17142 +               goto out_unlock;
17143 +       inode = dentry->d_inode;
17144 +       IMustLock(inode);
17145 +       err = -EISDIR;
17146 +       if (unlikely(S_ISDIR(inode->i_mode)))
17147 +               goto out_unlock; /* possible? */
17148 +
17149 +       bstart = au_dbstart(dentry);
17150 +       bwh = au_dbwh(dentry);
17151 +       bindex = -1;
17152 +       parent = dentry->d_parent; /* dir inode is locked */
17153 +       di_write_lock_parent(parent);
17154 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &dt, &pin);
17155 +       err = PTR_ERR(wh_dentry);
17156 +       if (IS_ERR(wh_dentry))
17157 +               goto out_parent;
17158 +
17159 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
17160 +       h_path.dentry = au_h_dptr(dentry, bstart);
17161 +       dget(h_path.dentry);
17162 +       if (bindex == bstart) {
17163 +               h_dir = au_pinned_h_dir(&pin);
17164 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
17165 +       } else {
17166 +               /* dir inode is locked */
17167 +               h_dir = wh_dentry->d_parent->d_inode;
17168 +               IMustLock(h_dir);
17169 +               err = 0;
17170 +       }
17171 +
17172 +       if (!err) {
17173 +               vfsub_drop_nlink(inode);
17174 +               epilog(dir, dentry, bindex);
17175 +
17176 +               /* update target timestamps */
17177 +               if (bindex == bstart) {
17178 +                       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
17179 +                       inode->i_ctime = h_path.dentry->d_inode->i_ctime;
17180 +               } else
17181 +                       /* todo: this timestamp may be reverted later */
17182 +                       inode->i_ctime = h_dir->i_ctime;
17183 +               goto out_unpin; /* success */
17184 +       }
17185 +
17186 +       /* revert */
17187 +       if (wh_dentry) {
17188 +               int rerr;
17189 +
17190 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry, &dt);
17191 +               if (rerr)
17192 +                       err = rerr;
17193 +       }
17194 +
17195 +out_unpin:
17196 +       au_unpin(&pin);
17197 +       dput(wh_dentry);
17198 +       dput(h_path.dentry);
17199 +out_parent:
17200 +       di_write_unlock(parent);
17201 +out_unlock:
17202 +       aufs_read_unlock(dentry, AuLock_DW);
17203 +out:
17204 +       return err;
17205 +}
17206 +
17207 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
17208 +{
17209 +       int err, rmdir_later;
17210 +       aufs_bindex_t bwh, bindex, bstart;
17211 +       struct au_dtime dt;
17212 +       struct au_pin pin;
17213 +       struct inode *inode;
17214 +       struct dentry *parent, *wh_dentry, *h_dentry;
17215 +       struct au_whtmp_rmdir *args;
17216 +
17217 +       IMustLock(dir);
17218 +
17219 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
17220 +       if (unlikely(err))
17221 +               goto out;
17222 +       err = au_alive_dir(dentry);
17223 +       if (unlikely(err))
17224 +               goto out_unlock;
17225 +       inode = dentry->d_inode;
17226 +       IMustLock(inode);
17227 +       err = -ENOTDIR;
17228 +       if (unlikely(!S_ISDIR(inode->i_mode)))
17229 +               goto out_unlock; /* possible? */
17230 +
17231 +       err = -ENOMEM;
17232 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
17233 +       if (unlikely(!args))
17234 +               goto out_unlock;
17235 +
17236 +       parent = dentry->d_parent; /* dir inode is locked */
17237 +       di_write_lock_parent(parent);
17238 +       err = au_test_empty(dentry, &args->whlist);
17239 +       if (unlikely(err))
17240 +               goto out_parent;
17241 +
17242 +       bstart = au_dbstart(dentry);
17243 +       bwh = au_dbwh(dentry);
17244 +       bindex = -1;
17245 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &dt, &pin);
17246 +       err = PTR_ERR(wh_dentry);
17247 +       if (IS_ERR(wh_dentry))
17248 +               goto out_parent;
17249 +
17250 +       h_dentry = au_h_dptr(dentry, bstart);
17251 +       dget(h_dentry);
17252 +       rmdir_later = 0;
17253 +       if (bindex == bstart) {
17254 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
17255 +               if (err > 0) {
17256 +                       rmdir_later = err;
17257 +                       err = 0;
17258 +               }
17259 +       } else {
17260 +               /* stop monitoring */
17261 +               au_hn_free(au_hi(inode, bstart));
17262 +
17263 +               /* dir inode is locked */
17264 +               IMustLock(wh_dentry->d_parent->d_inode);
17265 +               err = 0;
17266 +       }
17267 +
17268 +       if (!err) {
17269 +               vfsub_dead_dir(inode);
17270 +               au_set_dbdiropq(dentry, -1);
17271 +               epilog(dir, dentry, bindex);
17272 +
17273 +               if (rmdir_later) {
17274 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
17275 +                       args = NULL;
17276 +               }
17277 +
17278 +               goto out_unpin; /* success */
17279 +       }
17280 +
17281 +       /* revert */
17282 +       AuLabel(revert);
17283 +       if (wh_dentry) {
17284 +               int rerr;
17285 +
17286 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry, &dt);
17287 +               if (rerr)
17288 +                       err = rerr;
17289 +       }
17290 +
17291 +out_unpin:
17292 +       au_unpin(&pin);
17293 +       dput(wh_dentry);
17294 +       dput(h_dentry);
17295 +out_parent:
17296 +       di_write_unlock(parent);
17297 +       if (args)
17298 +               au_whtmp_rmdir_free(args);
17299 +out_unlock:
17300 +       aufs_read_unlock(dentry, AuLock_DW);
17301 +out:
17302 +       AuTraceErr(err);
17303 +       return err;
17304 +}
17305 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
17306 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
17307 +++ linux/fs/aufs/i_op_ren.c    2012-03-19 12:24:13.683134508 +0100
17308 @@ -0,0 +1,1026 @@
17309 +/*
17310 + * Copyright (C) 2005-2012 Junjiro R. Okajima
17311 + *
17312 + * This program, aufs is free software; you can redistribute it and/or modify
17313 + * it under the terms of the GNU General Public License as published by
17314 + * the Free Software Foundation; either version 2 of the License, or
17315 + * (at your option) any later version.
17316 + *
17317 + * This program is distributed in the hope that it will be useful,
17318 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17319 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17320 + * GNU General Public License for more details.
17321 + *
17322 + * You should have received a copy of the GNU General Public License
17323 + * along with this program; if not, write to the Free Software
17324 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17325 + */
17326 +
17327 +/*
17328 + * inode operation (rename entry)
17329 + * todo: this is crazy monster
17330 + */
17331 +
17332 +#include "aufs.h"
17333 +
17334 +enum { AuSRC, AuDST, AuSrcDst };
17335 +enum { AuPARENT, AuCHILD, AuParentChild };
17336 +
17337 +#define AuRen_ISDIR    1
17338 +#define AuRen_ISSAMEDIR        (1 << 1)
17339 +#define AuRen_WHSRC    (1 << 2)
17340 +#define AuRen_WHDST    (1 << 3)
17341 +#define AuRen_MNT_WRITE        (1 << 4)
17342 +#define AuRen_DT_DSTDIR        (1 << 5)
17343 +#define AuRen_DIROPQ   (1 << 6)
17344 +#define AuRen_CPUP     (1 << 7)
17345 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
17346 +#define au_fset_ren(flags, name) \
17347 +       do { (flags) |= AuRen_##name; } while (0)
17348 +#define au_fclr_ren(flags, name) \
17349 +       do { (flags) &= ~AuRen_##name; } while (0)
17350 +
17351 +struct au_ren_args {
17352 +       struct {
17353 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
17354 +                       *wh_dentry;
17355 +               struct inode *dir, *inode;
17356 +               struct au_hinode *hdir;
17357 +               struct au_dtime dt[AuParentChild];
17358 +               aufs_bindex_t bstart;
17359 +       } sd[AuSrcDst];
17360 +
17361 +#define src_dentry     sd[AuSRC].dentry
17362 +#define src_dir                sd[AuSRC].dir
17363 +#define src_inode      sd[AuSRC].inode
17364 +#define src_h_dentry   sd[AuSRC].h_dentry
17365 +#define src_parent     sd[AuSRC].parent
17366 +#define src_h_parent   sd[AuSRC].h_parent
17367 +#define src_wh_dentry  sd[AuSRC].wh_dentry
17368 +#define src_hdir       sd[AuSRC].hdir
17369 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
17370 +#define src_dt         sd[AuSRC].dt
17371 +#define src_bstart     sd[AuSRC].bstart
17372 +
17373 +#define dst_dentry     sd[AuDST].dentry
17374 +#define dst_dir                sd[AuDST].dir
17375 +#define dst_inode      sd[AuDST].inode
17376 +#define dst_h_dentry   sd[AuDST].h_dentry
17377 +#define dst_parent     sd[AuDST].parent
17378 +#define dst_h_parent   sd[AuDST].h_parent
17379 +#define dst_wh_dentry  sd[AuDST].wh_dentry
17380 +#define dst_hdir       sd[AuDST].hdir
17381 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
17382 +#define dst_dt         sd[AuDST].dt
17383 +#define dst_bstart     sd[AuDST].bstart
17384 +
17385 +       struct dentry *h_trap;
17386 +       struct au_branch *br;
17387 +       struct au_hinode *src_hinode;
17388 +       struct path h_path;
17389 +       struct au_nhash whlist;
17390 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
17391 +
17392 +       unsigned int flags;
17393 +
17394 +       struct au_whtmp_rmdir *thargs;
17395 +       struct dentry *h_dst;
17396 +};
17397 +
17398 +/* ---------------------------------------------------------------------- */
17399 +
17400 +/*
17401 + * functions for reverting.
17402 + * when an error happened in a single rename systemcall, we should revert
17403 + * everything as if nothing happend.
17404 + * we don't need to revert the copied-up/down the parent dir since they are
17405 + * harmless.
17406 + */
17407 +
17408 +#define RevertFailure(fmt, ...) do { \
17409 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
17410 +               ##__VA_ARGS__, err, rerr); \
17411 +       err = -EIO; \
17412 +} while (0)
17413 +
17414 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
17415 +{
17416 +       int rerr;
17417 +
17418 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
17419 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
17420 +       au_hn_imtx_unlock(a->src_hinode);
17421 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
17422 +       if (rerr)
17423 +               RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
17424 +}
17425 +
17426 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
17427 +{
17428 +       int rerr;
17429 +
17430 +       a->h_path.dentry = au_lkup_one(&a->src_dentry->d_name, a->src_h_parent,
17431 +                                      a->br, /*nd*/NULL);
17432 +       rerr = PTR_ERR(a->h_path.dentry);
17433 +       if (IS_ERR(a->h_path.dentry)) {
17434 +               RevertFailure("au_lkup_one %.*s", AuDLNPair(a->src_dentry));
17435 +               return;
17436 +       }
17437 +
17438 +       rerr = vfsub_rename(a->dst_h_dir,
17439 +                           au_h_dptr(a->src_dentry, a->btgt),
17440 +                           a->src_h_dir, &a->h_path);
17441 +       d_drop(a->h_path.dentry);
17442 +       dput(a->h_path.dentry);
17443 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
17444 +       if (rerr)
17445 +               RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
17446 +}
17447 +
17448 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
17449 +{
17450 +       int rerr;
17451 +
17452 +       a->h_path.dentry = a->dst_h_dentry;
17453 +       rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*force*/0);
17454 +       au_set_h_dptr(a->src_dentry, a->btgt, NULL);
17455 +       au_set_dbstart(a->src_dentry, a->src_bstart);
17456 +       if (rerr)
17457 +               RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
17458 +}
17459 +
17460 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
17461 +{
17462 +       int rerr;
17463 +
17464 +       a->h_path.dentry = au_lkup_one(&a->dst_dentry->d_name, a->dst_h_parent,
17465 +                                      a->br, /*nd*/NULL);
17466 +       rerr = PTR_ERR(a->h_path.dentry);
17467 +       if (IS_ERR(a->h_path.dentry)) {
17468 +               RevertFailure("lookup %.*s", AuDLNPair(a->dst_dentry));
17469 +               return;
17470 +       }
17471 +       if (a->h_path.dentry->d_inode) {
17472 +               d_drop(a->h_path.dentry);
17473 +               dput(a->h_path.dentry);
17474 +               return;
17475 +       }
17476 +
17477 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path);
17478 +       d_drop(a->h_path.dentry);
17479 +       dput(a->h_path.dentry);
17480 +       if (!rerr)
17481 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
17482 +       else
17483 +               RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
17484 +}
17485 +
17486 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
17487 +{
17488 +       int rerr;
17489 +
17490 +       a->h_path.dentry = a->src_wh_dentry;
17491 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
17492 +       au_set_dbwh(a->src_dentry, a->src_bwh);
17493 +       if (rerr)
17494 +               RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
17495 +}
17496 +#undef RevertFailure
17497 +
17498 +/* ---------------------------------------------------------------------- */
17499 +
17500 +/*
17501 + * when we have to copyup the renaming entry, do it with the rename-target name
17502 + * in order to minimize the cost (the later actual rename is unnecessary).
17503 + * otherwise rename it on the target branch.
17504 + */
17505 +static int au_ren_or_cpup(struct au_ren_args *a)
17506 +{
17507 +       int err;
17508 +       struct dentry *d;
17509 +
17510 +       d = a->src_dentry;
17511 +       if (au_dbstart(d) == a->btgt) {
17512 +               a->h_path.dentry = a->dst_h_dentry;
17513 +               if (au_ftest_ren(a->flags, DIROPQ)
17514 +                   && au_dbdiropq(d) == a->btgt)
17515 +                       au_fclr_ren(a->flags, DIROPQ);
17516 +               AuDebugOn(au_dbstart(d) != a->btgt);
17517 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
17518 +                                  a->dst_h_dir, &a->h_path);
17519 +       } else {
17520 +               struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
17521 +               struct file *h_file;
17522 +
17523 +               au_fset_ren(a->flags, CPUP);
17524 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
17525 +               au_set_dbstart(d, a->btgt);
17526 +               au_set_h_dptr(d, a->btgt, dget(a->dst_h_dentry));
17527 +               h_file = au_h_open_pre(d, a->src_bstart);
17528 +               if (IS_ERR(h_file)) {
17529 +                       err = PTR_ERR(h_file);
17530 +                       h_file = NULL;
17531 +               } else
17532 +                       err = au_sio_cpup_single(d, a->btgt, a->src_bstart, -1,
17533 +                                                !AuCpup_DTIME, a->dst_parent);
17534 +               mutex_unlock(h_mtx);
17535 +               au_h_open_post(d, a->src_bstart, h_file);
17536 +               if (!err) {
17537 +                       d = a->dst_dentry;
17538 +                       au_set_h_dptr(d, a->btgt, NULL);
17539 +                       au_update_dbstart(d);
17540 +               } else {
17541 +                       au_set_h_dptr(d, a->btgt, NULL);
17542 +                       au_set_dbstart(d, a->src_bstart);
17543 +               }
17544 +       }
17545 +       if (!err && a->h_dst)
17546 +               /* it will be set to dinfo later */
17547 +               dget(a->h_dst);
17548 +
17549 +       return err;
17550 +}
17551 +
17552 +/* cf. aufs_rmdir() */
17553 +static int au_ren_del_whtmp(struct au_ren_args *a)
17554 +{
17555 +       int err;
17556 +       struct inode *dir;
17557 +
17558 +       dir = a->dst_dir;
17559 +       SiMustAnyLock(dir->i_sb);
17560 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
17561 +                                    au_sbi(dir->i_sb)->si_dirwh)
17562 +           || au_test_fs_remote(a->h_dst->d_sb)) {
17563 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
17564 +               if (unlikely(err))
17565 +                       pr_warning("failed removing whtmp dir %.*s (%d), "
17566 +                                  "ignored.\n", AuDLNPair(a->h_dst), err);
17567 +       } else {
17568 +               au_nhash_wh_free(&a->thargs->whlist);
17569 +               a->thargs->whlist = a->whlist;
17570 +               a->whlist.nh_num = 0;
17571 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
17572 +               dput(a->h_dst);
17573 +               a->thargs = NULL;
17574 +       }
17575 +
17576 +       return 0;
17577 +}
17578 +
17579 +/* make it 'opaque' dir. */
17580 +static int au_ren_diropq(struct au_ren_args *a)
17581 +{
17582 +       int err;
17583 +       struct dentry *diropq;
17584 +
17585 +       err = 0;
17586 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
17587 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
17588 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
17589 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
17590 +       au_hn_imtx_unlock(a->src_hinode);
17591 +       if (IS_ERR(diropq))
17592 +               err = PTR_ERR(diropq);
17593 +       dput(diropq);
17594 +
17595 +       return err;
17596 +}
17597 +
17598 +static int do_rename(struct au_ren_args *a)
17599 +{
17600 +       int err;
17601 +       struct dentry *d, *h_d;
17602 +
17603 +       /* prepare workqueue args for asynchronous rmdir */
17604 +       h_d = a->dst_h_dentry;
17605 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
17606 +               err = -ENOMEM;
17607 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
17608 +               if (unlikely(!a->thargs))
17609 +                       goto out;
17610 +               a->h_dst = dget(h_d);
17611 +       }
17612 +
17613 +       /* create whiteout for src_dentry */
17614 +       if (au_ftest_ren(a->flags, WHSRC)) {
17615 +               a->src_bwh = au_dbwh(a->src_dentry);
17616 +               AuDebugOn(a->src_bwh >= 0);
17617 +               a->src_wh_dentry
17618 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
17619 +               err = PTR_ERR(a->src_wh_dentry);
17620 +               if (IS_ERR(a->src_wh_dentry))
17621 +                       goto out_thargs;
17622 +       }
17623 +
17624 +       /* lookup whiteout for dentry */
17625 +       if (au_ftest_ren(a->flags, WHDST)) {
17626 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
17627 +                                a->br);
17628 +               err = PTR_ERR(h_d);
17629 +               if (IS_ERR(h_d))
17630 +                       goto out_whsrc;
17631 +               if (!h_d->d_inode)
17632 +                       dput(h_d);
17633 +               else
17634 +                       a->dst_wh_dentry = h_d;
17635 +       }
17636 +
17637 +       /* rename dentry to tmpwh */
17638 +       if (a->thargs) {
17639 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
17640 +               if (unlikely(err))
17641 +                       goto out_whdst;
17642 +
17643 +               d = a->dst_dentry;
17644 +               au_set_h_dptr(d, a->btgt, NULL);
17645 +               err = au_lkup_neg(d, a->btgt);
17646 +               if (unlikely(err))
17647 +                       goto out_whtmp;
17648 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
17649 +       }
17650 +
17651 +       /* cpup src */
17652 +       if (a->dst_h_dentry->d_inode && a->src_bstart != a->btgt) {
17653 +               struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
17654 +               struct file *h_file;
17655 +
17656 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
17657 +               AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
17658 +               h_file = au_h_open_pre(a->src_dentry, a->src_bstart);
17659 +               if (IS_ERR(h_file)) {
17660 +                       err = PTR_ERR(h_file);
17661 +                       h_file = NULL;
17662 +               } else
17663 +                       err = au_sio_cpup_simple(a->src_dentry, a->btgt, -1,
17664 +                                                !AuCpup_DTIME);
17665 +               mutex_unlock(h_mtx);
17666 +               au_h_open_post(a->src_dentry, a->src_bstart, h_file);
17667 +               if (unlikely(err))
17668 +                       goto out_whtmp;
17669 +       }
17670 +
17671 +       /* rename by vfs_rename or cpup */
17672 +       d = a->dst_dentry;
17673 +       if (au_ftest_ren(a->flags, ISDIR)
17674 +           && (a->dst_wh_dentry
17675 +               || au_dbdiropq(d) == a->btgt
17676 +               /* hide the lower to keep xino */
17677 +               || a->btgt < au_dbend(d)
17678 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
17679 +               au_fset_ren(a->flags, DIROPQ);
17680 +       err = au_ren_or_cpup(a);
17681 +       if (unlikely(err))
17682 +               /* leave the copied-up one */
17683 +               goto out_whtmp;
17684 +
17685 +       /* make dir opaque */
17686 +       if (au_ftest_ren(a->flags, DIROPQ)) {
17687 +               err = au_ren_diropq(a);
17688 +               if (unlikely(err))
17689 +                       goto out_rename;
17690 +       }
17691 +
17692 +       /* update target timestamps */
17693 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
17694 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
17695 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
17696 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
17697 +
17698 +       /* remove whiteout for dentry */
17699 +       if (a->dst_wh_dentry) {
17700 +               a->h_path.dentry = a->dst_wh_dentry;
17701 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
17702 +                                         a->dst_dentry);
17703 +               if (unlikely(err))
17704 +                       goto out_diropq;
17705 +       }
17706 +
17707 +       /* remove whtmp */
17708 +       if (a->thargs)
17709 +               au_ren_del_whtmp(a); /* ignore this error */
17710 +
17711 +       err = 0;
17712 +       goto out_success;
17713 +
17714 +out_diropq:
17715 +       if (au_ftest_ren(a->flags, DIROPQ))
17716 +               au_ren_rev_diropq(err, a);
17717 +out_rename:
17718 +       if (!au_ftest_ren(a->flags, CPUP))
17719 +               au_ren_rev_rename(err, a);
17720 +       else
17721 +               au_ren_rev_cpup(err, a);
17722 +       dput(a->h_dst);
17723 +out_whtmp:
17724 +       if (a->thargs)
17725 +               au_ren_rev_whtmp(err, a);
17726 +out_whdst:
17727 +       dput(a->dst_wh_dentry);
17728 +       a->dst_wh_dentry = NULL;
17729 +out_whsrc:
17730 +       if (a->src_wh_dentry)
17731 +               au_ren_rev_whsrc(err, a);
17732 +out_success:
17733 +       dput(a->src_wh_dentry);
17734 +       dput(a->dst_wh_dentry);
17735 +out_thargs:
17736 +       if (a->thargs) {
17737 +               dput(a->h_dst);
17738 +               au_whtmp_rmdir_free(a->thargs);
17739 +               a->thargs = NULL;
17740 +       }
17741 +out:
17742 +       return err;
17743 +}
17744 +
17745 +/* ---------------------------------------------------------------------- */
17746 +
17747 +/*
17748 + * test if @dentry dir can be rename destination or not.
17749 + * success means, it is a logically empty dir.
17750 + */
17751 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
17752 +{
17753 +       return au_test_empty(dentry, whlist);
17754 +}
17755 +
17756 +/*
17757 + * test if @dentry dir can be rename source or not.
17758 + * if it can, return 0 and @children is filled.
17759 + * success means,
17760 + * - it is a logically empty dir.
17761 + * - or, it exists on writable branch and has no children including whiteouts
17762 + *       on the lower branch.
17763 + */
17764 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
17765 +{
17766 +       int err;
17767 +       unsigned int rdhash;
17768 +       aufs_bindex_t bstart;
17769 +
17770 +       bstart = au_dbstart(dentry);
17771 +       if (bstart != btgt) {
17772 +               struct au_nhash whlist;
17773 +
17774 +               SiMustAnyLock(dentry->d_sb);
17775 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
17776 +               if (!rdhash)
17777 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
17778 +                                                          dentry));
17779 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
17780 +               if (unlikely(err))
17781 +                       goto out;
17782 +               err = au_test_empty(dentry, &whlist);
17783 +               au_nhash_wh_free(&whlist);
17784 +               goto out;
17785 +       }
17786 +
17787 +       if (bstart == au_dbtaildir(dentry))
17788 +               return 0; /* success */
17789 +
17790 +       err = au_test_empty_lower(dentry);
17791 +
17792 +out:
17793 +       if (err == -ENOTEMPTY) {
17794 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
17795 +                       " is not supported\n");
17796 +               err = -EXDEV;
17797 +       }
17798 +       return err;
17799 +}
17800 +
17801 +/* side effect: sets whlist and h_dentry */
17802 +static int au_ren_may_dir(struct au_ren_args *a)
17803 +{
17804 +       int err;
17805 +       unsigned int rdhash;
17806 +       struct dentry *d;
17807 +
17808 +       d = a->dst_dentry;
17809 +       SiMustAnyLock(d->d_sb);
17810 +
17811 +       err = 0;
17812 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
17813 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
17814 +               if (!rdhash)
17815 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
17816 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
17817 +               if (unlikely(err))
17818 +                       goto out;
17819 +
17820 +               au_set_dbstart(d, a->dst_bstart);
17821 +               err = may_rename_dstdir(d, &a->whlist);
17822 +               au_set_dbstart(d, a->btgt);
17823 +       }
17824 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
17825 +       if (unlikely(err))
17826 +               goto out;
17827 +
17828 +       d = a->src_dentry;
17829 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
17830 +       if (au_ftest_ren(a->flags, ISDIR)) {
17831 +               err = may_rename_srcdir(d, a->btgt);
17832 +               if (unlikely(err)) {
17833 +                       au_nhash_wh_free(&a->whlist);
17834 +                       a->whlist.nh_num = 0;
17835 +               }
17836 +       }
17837 +out:
17838 +       return err;
17839 +}
17840 +
17841 +/* ---------------------------------------------------------------------- */
17842 +
17843 +/*
17844 + * simple tests for rename.
17845 + * following the checks in vfs, plus the parent-child relationship.
17846 + */
17847 +static int au_may_ren(struct au_ren_args *a)
17848 +{
17849 +       int err, isdir;
17850 +       struct inode *h_inode;
17851 +
17852 +       if (a->src_bstart == a->btgt) {
17853 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
17854 +                                au_ftest_ren(a->flags, ISDIR));
17855 +               if (unlikely(err))
17856 +                       goto out;
17857 +               err = -EINVAL;
17858 +               if (unlikely(a->src_h_dentry == a->h_trap))
17859 +                       goto out;
17860 +       }
17861 +
17862 +       err = 0;
17863 +       if (a->dst_bstart != a->btgt)
17864 +               goto out;
17865 +
17866 +       err = -ENOTEMPTY;
17867 +       if (unlikely(a->dst_h_dentry == a->h_trap))
17868 +               goto out;
17869 +
17870 +       err = -EIO;
17871 +       h_inode = a->dst_h_dentry->d_inode;
17872 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
17873 +       if (!a->dst_dentry->d_inode) {
17874 +               if (unlikely(h_inode))
17875 +                       goto out;
17876 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
17877 +                                isdir);
17878 +       } else {
17879 +               if (unlikely(!h_inode || !h_inode->i_nlink))
17880 +                       goto out;
17881 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
17882 +                                isdir);
17883 +               if (unlikely(err))
17884 +                       goto out;
17885 +       }
17886 +
17887 +out:
17888 +       if (unlikely(err == -ENOENT || err == -EEXIST))
17889 +               err = -EIO;
17890 +       AuTraceErr(err);
17891 +       return err;
17892 +}
17893 +
17894 +/* ---------------------------------------------------------------------- */
17895 +
17896 +/*
17897 + * locking order
17898 + * (VFS)
17899 + * - src_dir and dir by lock_rename()
17900 + * - inode if exitsts
17901 + * (aufs)
17902 + * - lock all
17903 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
17904 + *     + si_read_lock
17905 + *     + di_write_lock2_child()
17906 + *       + di_write_lock_child()
17907 + *        + ii_write_lock_child()
17908 + *       + di_write_lock_child2()
17909 + *        + ii_write_lock_child2()
17910 + *     + src_parent and parent
17911 + *       + di_write_lock_parent()
17912 + *        + ii_write_lock_parent()
17913 + *       + di_write_lock_parent2()
17914 + *        + ii_write_lock_parent2()
17915 + *   + lower src_dir and dir by vfsub_lock_rename()
17916 + *   + verify the every relationships between child and parent. if any
17917 + *     of them failed, unlock all and return -EBUSY.
17918 + */
17919 +static void au_ren_unlock(struct au_ren_args *a)
17920 +{
17921 +       struct super_block *sb;
17922 +
17923 +       sb = a->dst_dentry->d_sb;
17924 +       if (au_ftest_ren(a->flags, MNT_WRITE))
17925 +               mnt_drop_write(a->br->br_mnt);
17926 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
17927 +                           a->dst_h_parent, a->dst_hdir);
17928 +}
17929 +
17930 +static int au_ren_lock(struct au_ren_args *a)
17931 +{
17932 +       int err;
17933 +       unsigned int udba;
17934 +
17935 +       err = 0;
17936 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
17937 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
17938 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
17939 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
17940 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
17941 +                                     a->dst_h_parent, a->dst_hdir);
17942 +       udba = au_opt_udba(a->src_dentry->d_sb);
17943 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
17944 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
17945 +               err = au_busy_or_stale();
17946 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
17947 +               err = au_h_verify(a->src_h_dentry, udba,
17948 +                                 a->src_h_parent->d_inode, a->src_h_parent,
17949 +                                 a->br);
17950 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
17951 +               err = au_h_verify(a->dst_h_dentry, udba,
17952 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
17953 +                                 a->br);
17954 +       if (!err) {
17955 +               err = mnt_want_write(a->br->br_mnt);
17956 +               if (unlikely(err))
17957 +                       goto out_unlock;
17958 +               au_fset_ren(a->flags, MNT_WRITE);
17959 +               goto out; /* success */
17960 +       }
17961 +
17962 +       err = au_busy_or_stale();
17963 +
17964 +out_unlock:
17965 +       au_ren_unlock(a);
17966 +out:
17967 +       return err;
17968 +}
17969 +
17970 +/* ---------------------------------------------------------------------- */
17971 +
17972 +static void au_ren_refresh_dir(struct au_ren_args *a)
17973 +{
17974 +       struct inode *dir;
17975 +
17976 +       dir = a->dst_dir;
17977 +       dir->i_version++;
17978 +       if (au_ftest_ren(a->flags, ISDIR)) {
17979 +               /* is this updating defined in POSIX? */
17980 +               au_cpup_attr_timesizes(a->src_inode);
17981 +               au_cpup_attr_nlink(dir, /*force*/1);
17982 +       }
17983 +
17984 +       if (au_ibstart(dir) == a->btgt)
17985 +               au_cpup_attr_timesizes(dir);
17986 +
17987 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
17988 +               return;
17989 +
17990 +       dir = a->src_dir;
17991 +       dir->i_version++;
17992 +       if (au_ftest_ren(a->flags, ISDIR))
17993 +               au_cpup_attr_nlink(dir, /*force*/1);
17994 +       if (au_ibstart(dir) == a->btgt)
17995 +               au_cpup_attr_timesizes(dir);
17996 +}
17997 +
17998 +static void au_ren_refresh(struct au_ren_args *a)
17999 +{
18000 +       aufs_bindex_t bend, bindex;
18001 +       struct dentry *d, *h_d;
18002 +       struct inode *i, *h_i;
18003 +       struct super_block *sb;
18004 +
18005 +       d = a->dst_dentry;
18006 +       d_drop(d);
18007 +       if (a->h_dst)
18008 +               /* already dget-ed by au_ren_or_cpup() */
18009 +               au_set_h_dptr(d, a->btgt, a->h_dst);
18010 +
18011 +       i = a->dst_inode;
18012 +       if (i) {
18013 +               if (!au_ftest_ren(a->flags, ISDIR))
18014 +                       vfsub_drop_nlink(i);
18015 +               else {
18016 +                       vfsub_dead_dir(i);
18017 +                       au_cpup_attr_timesizes(i);
18018 +               }
18019 +               au_update_dbrange(d, /*do_put_zero*/1);
18020 +       } else {
18021 +               bend = a->btgt;
18022 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
18023 +                       au_set_h_dptr(d, bindex, NULL);
18024 +               bend = au_dbend(d);
18025 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
18026 +                       au_set_h_dptr(d, bindex, NULL);
18027 +               au_update_dbrange(d, /*do_put_zero*/0);
18028 +       }
18029 +
18030 +       d = a->src_dentry;
18031 +       au_set_dbwh(d, -1);
18032 +       bend = au_dbend(d);
18033 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18034 +               h_d = au_h_dptr(d, bindex);
18035 +               if (h_d)
18036 +                       au_set_h_dptr(d, bindex, NULL);
18037 +       }
18038 +       au_set_dbend(d, a->btgt);
18039 +
18040 +       sb = d->d_sb;
18041 +       i = a->src_inode;
18042 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
18043 +               return; /* success */
18044 +
18045 +       bend = au_ibend(i);
18046 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18047 +               h_i = au_h_iptr(i, bindex);
18048 +               if (h_i) {
18049 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
18050 +                       /* ignore this error */
18051 +                       au_set_h_iptr(i, bindex, NULL, 0);
18052 +               }
18053 +       }
18054 +       au_set_ibend(i, a->btgt);
18055 +}
18056 +
18057 +/* ---------------------------------------------------------------------- */
18058 +
18059 +/* mainly for link(2) and rename(2) */
18060 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
18061 +{
18062 +       aufs_bindex_t bdiropq, bwh;
18063 +       struct dentry *parent;
18064 +       struct au_branch *br;
18065 +
18066 +       parent = dentry->d_parent;
18067 +       IMustLock(parent->d_inode); /* dir is locked */
18068 +
18069 +       bdiropq = au_dbdiropq(parent);
18070 +       bwh = au_dbwh(dentry);
18071 +       br = au_sbr(dentry->d_sb, btgt);
18072 +       if (au_br_rdonly(br)
18073 +           || (0 <= bdiropq && bdiropq < btgt)
18074 +           || (0 <= bwh && bwh < btgt))
18075 +               btgt = -1;
18076 +
18077 +       AuDbg("btgt %d\n", btgt);
18078 +       return btgt;
18079 +}
18080 +
18081 +/* sets src_bstart, dst_bstart and btgt */
18082 +static int au_ren_wbr(struct au_ren_args *a)
18083 +{
18084 +       int err;
18085 +       struct au_wr_dir_args wr_dir_args = {
18086 +               /* .force_btgt  = -1, */
18087 +               .flags          = AuWrDir_ADD_ENTRY
18088 +       };
18089 +
18090 +       a->src_bstart = au_dbstart(a->src_dentry);
18091 +       a->dst_bstart = au_dbstart(a->dst_dentry);
18092 +       if (au_ftest_ren(a->flags, ISDIR))
18093 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18094 +       wr_dir_args.force_btgt = a->src_bstart;
18095 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
18096 +               wr_dir_args.force_btgt = a->dst_bstart;
18097 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
18098 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
18099 +       a->btgt = err;
18100 +
18101 +       return err;
18102 +}
18103 +
18104 +static void au_ren_dt(struct au_ren_args *a)
18105 +{
18106 +       a->h_path.dentry = a->src_h_parent;
18107 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
18108 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
18109 +               a->h_path.dentry = a->dst_h_parent;
18110 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
18111 +       }
18112 +
18113 +       au_fclr_ren(a->flags, DT_DSTDIR);
18114 +       if (!au_ftest_ren(a->flags, ISDIR))
18115 +               return;
18116 +
18117 +       a->h_path.dentry = a->src_h_dentry;
18118 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
18119 +       if (a->dst_h_dentry->d_inode) {
18120 +               au_fset_ren(a->flags, DT_DSTDIR);
18121 +               a->h_path.dentry = a->dst_h_dentry;
18122 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
18123 +       }
18124 +}
18125 +
18126 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
18127 +{
18128 +       struct dentry *h_d;
18129 +       struct mutex *h_mtx;
18130 +
18131 +       au_dtime_revert(a->src_dt + AuPARENT);
18132 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
18133 +               au_dtime_revert(a->dst_dt + AuPARENT);
18134 +
18135 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
18136 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
18137 +               h_mtx = &h_d->d_inode->i_mutex;
18138 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18139 +               au_dtime_revert(a->src_dt + AuCHILD);
18140 +               mutex_unlock(h_mtx);
18141 +
18142 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
18143 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
18144 +                       h_mtx = &h_d->d_inode->i_mutex;
18145 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18146 +                       au_dtime_revert(a->dst_dt + AuCHILD);
18147 +                       mutex_unlock(h_mtx);
18148 +               }
18149 +       }
18150 +}
18151 +
18152 +/* ---------------------------------------------------------------------- */
18153 +
18154 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
18155 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
18156 +{
18157 +       int err, flags;
18158 +       /* reduce stack space */
18159 +       struct au_ren_args *a;
18160 +
18161 +       AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
18162 +       IMustLock(_src_dir);
18163 +       IMustLock(_dst_dir);
18164 +
18165 +       err = -ENOMEM;
18166 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
18167 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18168 +       if (unlikely(!a))
18169 +               goto out;
18170 +
18171 +       a->src_dir = _src_dir;
18172 +       a->src_dentry = _src_dentry;
18173 +       a->src_inode = a->src_dentry->d_inode;
18174 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
18175 +       a->dst_dir = _dst_dir;
18176 +       a->dst_dentry = _dst_dentry;
18177 +       a->dst_inode = a->dst_dentry->d_inode;
18178 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
18179 +       if (a->dst_inode) {
18180 +               IMustLock(a->dst_inode);
18181 +               au_igrab(a->dst_inode);
18182 +       }
18183 +
18184 +       err = -ENOTDIR;
18185 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
18186 +       if (S_ISDIR(a->src_inode->i_mode)) {
18187 +               au_fset_ren(a->flags, ISDIR);
18188 +               if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
18189 +                       goto out_free;
18190 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18191 +                                               AuLock_DIR | flags);
18192 +       } else
18193 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18194 +                                               flags);
18195 +       if (unlikely(err))
18196 +               goto out_free;
18197 +
18198 +       err = au_d_hashed_positive(a->src_dentry);
18199 +       if (unlikely(err))
18200 +               goto out_unlock;
18201 +       err = -ENOENT;
18202 +       if (a->dst_inode) {
18203 +               /*
18204 +                * If it is a dir, VFS unhash dst_dentry before this
18205 +                * function. It means we cannot rely upon d_unhashed().
18206 +                */
18207 +               if (unlikely(!a->dst_inode->i_nlink))
18208 +                       goto out_unlock;
18209 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
18210 +                       err = au_d_hashed_positive(a->dst_dentry);
18211 +                       if (unlikely(err))
18212 +                               goto out_unlock;
18213 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
18214 +                       goto out_unlock;
18215 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
18216 +               goto out_unlock;
18217 +
18218 +       /*
18219 +        * is it possible?
18220 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
18221 +        * there may exist a problem somewhere else.
18222 +        */
18223 +       err = -EINVAL;
18224 +       if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
18225 +               goto out_unlock;
18226 +
18227 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
18228 +       di_write_lock_parent(a->dst_parent);
18229 +
18230 +       /* which branch we process */
18231 +       err = au_ren_wbr(a);
18232 +       if (unlikely(err < 0))
18233 +               goto out_parent;
18234 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
18235 +       a->h_path.mnt = a->br->br_mnt;
18236 +
18237 +       /* are they available to be renamed */
18238 +       err = au_ren_may_dir(a);
18239 +       if (unlikely(err))
18240 +               goto out_children;
18241 +
18242 +       /* prepare the writable parent dir on the same branch */
18243 +       if (a->dst_bstart == a->btgt) {
18244 +               au_fset_ren(a->flags, WHDST);
18245 +       } else {
18246 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
18247 +               if (unlikely(err))
18248 +                       goto out_children;
18249 +       }
18250 +
18251 +       if (a->src_dir != a->dst_dir) {
18252 +               /*
18253 +                * this temporary unlock is safe,
18254 +                * because both dir->i_mutex are locked.
18255 +                */
18256 +               di_write_unlock(a->dst_parent);
18257 +               di_write_lock_parent(a->src_parent);
18258 +               err = au_wr_dir_need_wh(a->src_dentry,
18259 +                                       au_ftest_ren(a->flags, ISDIR),
18260 +                                       &a->btgt);
18261 +               di_write_unlock(a->src_parent);
18262 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
18263 +               au_fclr_ren(a->flags, ISSAMEDIR);
18264 +       } else
18265 +               err = au_wr_dir_need_wh(a->src_dentry,
18266 +                                       au_ftest_ren(a->flags, ISDIR),
18267 +                                       &a->btgt);
18268 +       if (unlikely(err < 0))
18269 +               goto out_children;
18270 +       if (err)
18271 +               au_fset_ren(a->flags, WHSRC);
18272 +
18273 +       /* lock them all */
18274 +       err = au_ren_lock(a);
18275 +       if (unlikely(err))
18276 +               goto out_children;
18277 +
18278 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
18279 +               err = au_may_ren(a);
18280 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
18281 +               err = -ENAMETOOLONG;
18282 +       if (unlikely(err))
18283 +               goto out_hdir;
18284 +
18285 +       /* store timestamps to be revertible */
18286 +       au_ren_dt(a);
18287 +
18288 +       /* here we go */
18289 +       err = do_rename(a);
18290 +       if (unlikely(err))
18291 +               goto out_dt;
18292 +
18293 +       /* update dir attributes */
18294 +       au_ren_refresh_dir(a);
18295 +
18296 +       /* dput/iput all lower dentries */
18297 +       au_ren_refresh(a);
18298 +
18299 +       goto out_hdir; /* success */
18300 +
18301 +out_dt:
18302 +       au_ren_rev_dt(err, a);
18303 +out_hdir:
18304 +       au_ren_unlock(a);
18305 +out_children:
18306 +       au_nhash_wh_free(&a->whlist);
18307 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
18308 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
18309 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
18310 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
18311 +       }
18312 +out_parent:
18313 +       if (!err)
18314 +               d_move(a->src_dentry, a->dst_dentry);
18315 +       else {
18316 +               au_update_dbstart(a->dst_dentry);
18317 +               if (!a->dst_inode)
18318 +                       d_drop(a->dst_dentry);
18319 +       }
18320 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
18321 +               di_write_unlock(a->dst_parent);
18322 +       else
18323 +               di_write_unlock2(a->src_parent, a->dst_parent);
18324 +out_unlock:
18325 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
18326 +out_free:
18327 +       iput(a->dst_inode);
18328 +       if (a->thargs)
18329 +               au_whtmp_rmdir_free(a->thargs);
18330 +       kfree(a);
18331 +out:
18332 +       AuTraceErr(err);
18333 +       return err;
18334 +}
18335 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
18336 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
18337 +++ linux/fs/aufs/Kconfig       2012-03-19 12:24:13.679801090 +0100
18338 @@ -0,0 +1,203 @@
18339 +config AUFS_FS
18340 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
18341 +       depends on EXPERIMENTAL
18342 +       help
18343 +       Aufs is a stackable unification filesystem such as Unionfs,
18344 +       which unifies several directories and provides a merged single
18345 +       directory.
18346 +       In the early days, aufs was entirely re-designed and
18347 +       re-implemented Unionfs Version 1.x series. Introducing many
18348 +       original ideas, approaches and improvements, it becomes totally
18349 +       different from Unionfs while keeping the basic features.
18350 +
18351 +if AUFS_FS
18352 +choice
18353 +       prompt "Maximum number of branches"
18354 +       default AUFS_BRANCH_MAX_127
18355 +       help
18356 +       Specifies the maximum number of branches (or member directories)
18357 +       in a single aufs. The larger value consumes more system
18358 +       resources and has a minor impact to performance.
18359 +config AUFS_BRANCH_MAX_127
18360 +       bool "127"
18361 +       help
18362 +       Specifies the maximum number of branches (or member directories)
18363 +       in a single aufs. The larger value consumes more system
18364 +       resources and has a minor impact to performance.
18365 +config AUFS_BRANCH_MAX_511
18366 +       bool "511"
18367 +       help
18368 +       Specifies the maximum number of branches (or member directories)
18369 +       in a single aufs. The larger value consumes more system
18370 +       resources and has a minor impact to performance.
18371 +config AUFS_BRANCH_MAX_1023
18372 +       bool "1023"
18373 +       help
18374 +       Specifies the maximum number of branches (or member directories)
18375 +       in a single aufs. The larger value consumes more system
18376 +       resources and has a minor impact to performance.
18377 +config AUFS_BRANCH_MAX_32767
18378 +       bool "32767"
18379 +       help
18380 +       Specifies the maximum number of branches (or member directories)
18381 +       in a single aufs. The larger value consumes more system
18382 +       resources and has a minor impact to performance.
18383 +endchoice
18384 +
18385 +config AUFS_SBILIST
18386 +       bool
18387 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
18388 +       default y
18389 +       help
18390 +       Automatic configuration for internal use.
18391 +       When aufs supports Magic SysRq or /proc, enabled automatically.
18392 +
18393 +config AUFS_HNOTIFY
18394 +       bool "Detect direct branch access (bypassing aufs)"
18395 +       help
18396 +       If you want to modify files on branches directly, eg. bypassing aufs,
18397 +       and want aufs to detect the changes of them fully, then enable this
18398 +       option and use 'udba=notify' mount option.
18399 +       Currently there is only one available configuration, "fsnotify".
18400 +       It will have a negative impact to the performance.
18401 +       See detail in aufs.5.
18402 +
18403 +choice
18404 +       prompt "method" if AUFS_HNOTIFY
18405 +       default AUFS_HFSNOTIFY
18406 +config AUFS_HFSNOTIFY
18407 +       bool "fsnotify"
18408 +       select FSNOTIFY
18409 +endchoice
18410 +
18411 +config AUFS_EXPORT
18412 +       bool "NFS-exportable aufs"
18413 +       depends on EXPORTFS
18414 +       help
18415 +       If you want to export your mounted aufs via NFS, then enable this
18416 +       option. There are several requirements for this configuration.
18417 +       See detail in aufs.5.
18418 +
18419 +config AUFS_INO_T_64
18420 +       bool
18421 +       depends on AUFS_EXPORT
18422 +       depends on 64BIT && !(ALPHA || S390)
18423 +       default y
18424 +       help
18425 +       Automatic configuration for internal use.
18426 +       /* typedef unsigned long/int __kernel_ino_t */
18427 +       /* alpha and s390x are int */
18428 +
18429 +config AUFS_RDU
18430 +       bool "Readdir in userspace"
18431 +       help
18432 +       Aufs has two methods to provide a merged view for a directory,
18433 +       by a user-space library and by kernel-space natively. The latter
18434 +       is always enabled but sometimes large and slow.
18435 +       If you enable this option, install the library in aufs2-util
18436 +       package, and set some environment variables for your readdir(3),
18437 +       then the work will be handled in user-space which generally
18438 +       shows better performance in most cases.
18439 +       See detail in aufs.5.
18440 +
18441 +config AUFS_PROC_MAP
18442 +       bool "support for /proc/maps and lsof(1)"
18443 +       depends on PROC_FS
18444 +       help
18445 +       When you issue mmap(2) in aufs, it is actually a direct mmap(2)
18446 +       call to the file on the branch fs since the file in aufs is
18447 +       purely virtual. And the file path printed in /proc/maps (and
18448 +       others) will be the path on the branch fs. In most cases, it
18449 +       does no harm. But some utilities like lsof(1) may confuse since
18450 +       the utility or user may expect the file path in aufs to be
18451 +       printed.
18452 +       To address this issue, aufs provides a patch which introduces a
18453 +       new member called vm_prfile into struct vm_are_struct. The patch
18454 +       is meaningless without enabling this configuration since nobody
18455 +       sets the new vm_prfile member.
18456 +       If you don't apply the patch, then enabling this configuration
18457 +       will cause a compile error.
18458 +       This approach is fragile since if someone else make some changes
18459 +       around vm_file, then vm_prfile may not work anymore. As a
18460 +       workaround such case, aufs provides this configuration. If you
18461 +       disable it, then lsof(1) may produce incorrect result but the
18462 +       problem will be gone even if the aufs patch is applied (I hope).
18463 +
18464 +config AUFS_SP_IATTR
18465 +       bool "Respect the attributes (mtime/ctime mainly) of special files"
18466 +       help
18467 +       When you write something to a special file, some attributes of it
18468 +       (mtime/ctime mainly) may be updated. Generally such updates are
18469 +       less important (actually some device drivers and NFS ignore
18470 +       it). But some applications (such like test program) requires
18471 +       such updates. If you need these updates, then enable this
18472 +       configuration which introduces some overhead.
18473 +       Currently this configuration handles FIFO only.
18474 +
18475 +config AUFS_SHWH
18476 +       bool "Show whiteouts"
18477 +       help
18478 +       If you want to make the whiteouts in aufs visible, then enable
18479 +       this option and specify 'shwh' mount option. Although it may
18480 +       sounds like philosophy or something, but in technically it
18481 +       simply shows the name of whiteout with keeping its behaviour.
18482 +
18483 +config AUFS_BR_RAMFS
18484 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
18485 +       help
18486 +       If you want to use ramfs as an aufs branch fs, then enable this
18487 +       option. Generally tmpfs is recommended.
18488 +       Aufs prohibited them to be a branch fs by default, because
18489 +       initramfs becomes unusable after switch_root or something
18490 +       generally. If you sets initramfs as an aufs branch and boot your
18491 +       system by switch_root, you will meet a problem easily since the
18492 +       files in initramfs may be inaccessible.
18493 +       Unless you are going to use ramfs as an aufs branch fs without
18494 +       switch_root or something, leave it N.
18495 +
18496 +config AUFS_BR_FUSE
18497 +       bool "Fuse fs as an aufs branch"
18498 +       depends on FUSE_FS
18499 +       select AUFS_POLL
18500 +       help
18501 +       If you want to use fuse-based userspace filesystem as an aufs
18502 +       branch fs, then enable this option.
18503 +       It implements the internal poll(2) operation which is
18504 +       implemented by fuse only (curretnly).
18505 +
18506 +config AUFS_POLL
18507 +       bool
18508 +       help
18509 +       Automatic configuration for internal use.
18510 +
18511 +config AUFS_BR_HFSPLUS
18512 +       bool "Hfsplus as an aufs branch"
18513 +       depends on HFSPLUS_FS
18514 +       default y
18515 +       help
18516 +       If you want to use hfsplus fs as an aufs branch fs, then enable
18517 +       this option. This option introduces a small overhead at
18518 +       copying-up a file on hfsplus.
18519 +
18520 +config AUFS_BDEV_LOOP
18521 +       bool
18522 +       depends on BLK_DEV_LOOP
18523 +       default y
18524 +       help
18525 +       Automatic configuration for internal use.
18526 +       Convert =[ym] into =y.
18527 +
18528 +config AUFS_DEBUG
18529 +       bool "Debug aufs"
18530 +       help
18531 +       Enable this to compile aufs internal debug code.
18532 +       It will have a negative impact to the performance.
18533 +
18534 +config AUFS_MAGIC_SYSRQ
18535 +       bool
18536 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
18537 +       default y
18538 +       help
18539 +       Automatic configuration for internal use.
18540 +       When aufs supports Magic SysRq, enabled automatically.
18541 +endif
18542 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
18543 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
18544 +++ linux/fs/aufs/loop.c        2012-03-19 12:24:13.683134508 +0100
18545 @@ -0,0 +1,133 @@
18546 +/*
18547 + * Copyright (C) 2005-2012 Junjiro R. Okajima
18548 + *
18549 + * This program, aufs is free software; you can redistribute it and/or modify
18550 + * it under the terms of the GNU General Public License as published by
18551 + * the Free Software Foundation; either version 2 of the License, or
18552 + * (at your option) any later version.
18553 + *
18554 + * This program is distributed in the hope that it will be useful,
18555 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18556 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18557 + * GNU General Public License for more details.
18558 + *
18559 + * You should have received a copy of the GNU General Public License
18560 + * along with this program; if not, write to the Free Software
18561 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18562 + */
18563 +
18564 +/*
18565 + * support for loopback block device as a branch
18566 + */
18567 +
18568 +#include <linux/loop.h>
18569 +#include "aufs.h"
18570 +
18571 +/*
18572 + * test if two lower dentries have overlapping branches.
18573 + */
18574 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
18575 +{
18576 +       struct super_block *h_sb;
18577 +       struct loop_device *l;
18578 +
18579 +       h_sb = h_adding->d_sb;
18580 +       if (MAJOR(h_sb->s_dev) != LOOP_MAJOR)
18581 +               return 0;
18582 +
18583 +       l = h_sb->s_bdev->bd_disk->private_data;
18584 +       h_adding = l->lo_backing_file->f_dentry;
18585 +       /*
18586 +        * h_adding can be local NFS.
18587 +        * in this case aufs cannot detect the loop.
18588 +        */
18589 +       if (unlikely(h_adding->d_sb == sb))
18590 +               return 1;
18591 +       return !!au_test_subdir(h_adding, sb->s_root);
18592 +}
18593 +
18594 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
18595 +int au_test_loopback_kthread(void)
18596 +{
18597 +       int ret;
18598 +       struct task_struct *tsk = current;
18599 +
18600 +       ret = 0;
18601 +       if (tsk->flags & PF_KTHREAD) {
18602 +               const char c = tsk->comm[4];
18603 +               ret = ('0' <= c && c <= '9'
18604 +                      && !strncmp(tsk->comm, "loop", 4));
18605 +       }
18606 +
18607 +       return ret;
18608 +}
18609 +
18610 +/* ---------------------------------------------------------------------- */
18611 +
18612 +#define au_warn_loopback_step  16
18613 +static int au_warn_loopback_nelem = au_warn_loopback_step;
18614 +static unsigned long *au_warn_loopback_array;
18615 +
18616 +void au_warn_loopback(struct super_block *h_sb)
18617 +{
18618 +       int i, new_nelem;
18619 +       unsigned long *a, magic;
18620 +       static DEFINE_SPINLOCK(spin);
18621 +
18622 +       magic = h_sb->s_magic;
18623 +       spin_lock(&spin);
18624 +       a = au_warn_loopback_array;
18625 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
18626 +               if (a[i] == magic) {
18627 +                       spin_unlock(&spin);
18628 +                       return;
18629 +               }
18630 +
18631 +       /* h_sb is new to us, print it */
18632 +       if (i < au_warn_loopback_nelem) {
18633 +               a[i] = magic;
18634 +               goto pr;
18635 +       }
18636 +
18637 +       /* expand the array */
18638 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
18639 +       a = au_kzrealloc(au_warn_loopback_array,
18640 +                        au_warn_loopback_nelem * sizeof(unsigned long),
18641 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
18642 +       if (a) {
18643 +               au_warn_loopback_nelem = new_nelem;
18644 +               au_warn_loopback_array = a;
18645 +               a[i] = magic;
18646 +               goto pr;
18647 +       }
18648 +
18649 +       spin_unlock(&spin);
18650 +       AuWarn1("realloc failed, ignored\n");
18651 +       return;
18652 +
18653 +pr:
18654 +       spin_unlock(&spin);
18655 +       pr_warning("you may want to try another patch for loopback file "
18656 +                  "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
18657 +}
18658 +
18659 +int au_loopback_init(void)
18660 +{
18661 +       int err;
18662 +       struct super_block *sb __maybe_unused;
18663 +
18664 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
18665 +
18666 +       err = 0;
18667 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
18668 +                                        sizeof(unsigned long), GFP_NOFS);
18669 +       if (unlikely(!au_warn_loopback_array))
18670 +               err = -ENOMEM;
18671 +
18672 +       return err;
18673 +}
18674 +
18675 +void au_loopback_fin(void)
18676 +{
18677 +       kfree(au_warn_loopback_array);
18678 +}
18679 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
18680 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
18681 +++ linux/fs/aufs/loop.h        2012-03-19 12:24:13.683134508 +0100
18682 @@ -0,0 +1,50 @@
18683 +/*
18684 + * Copyright (C) 2005-2012 Junjiro R. Okajima
18685 + *
18686 + * This program, aufs is free software; you can redistribute it and/or modify
18687 + * it under the terms of the GNU General Public License as published by
18688 + * the Free Software Foundation; either version 2 of the License, or
18689 + * (at your option) any later version.
18690 + *
18691 + * This program is distributed in the hope that it will be useful,
18692 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18693 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18694 + * GNU General Public License for more details.
18695 + *
18696 + * You should have received a copy of the GNU General Public License
18697 + * along with this program; if not, write to the Free Software
18698 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18699 + */
18700 +
18701 +/*
18702 + * support for loopback mount as a branch
18703 + */
18704 +
18705 +#ifndef __AUFS_LOOP_H__
18706 +#define __AUFS_LOOP_H__
18707 +
18708 +#ifdef __KERNEL__
18709 +
18710 +struct dentry;
18711 +struct super_block;
18712 +
18713 +#ifdef CONFIG_AUFS_BDEV_LOOP
18714 +/* loop.c */
18715 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
18716 +int au_test_loopback_kthread(void);
18717 +void au_warn_loopback(struct super_block *h_sb);
18718 +
18719 +int au_loopback_init(void);
18720 +void au_loopback_fin(void);
18721 +#else
18722 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
18723 +          struct dentry *h_adding)
18724 +AuStubInt0(au_test_loopback_kthread, void)
18725 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
18726 +
18727 +AuStubInt0(au_loopback_init, void)
18728 +AuStubVoid(au_loopback_fin, void)
18729 +#endif /* BLK_DEV_LOOP */
18730 +
18731 +#endif /* __KERNEL__ */
18732 +#endif /* __AUFS_LOOP_H__ */
18733 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
18734 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
18735 +++ linux/fs/aufs/magic.mk      2012-03-19 12:24:13.683134508 +0100
18736 @@ -0,0 +1,54 @@
18737 +
18738 +# defined in ${srctree}/fs/fuse/inode.c
18739 +# tristate
18740 +ifdef CONFIG_FUSE_FS
18741 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
18742 +endif
18743 +
18744 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
18745 +# tristate
18746 +ifdef CONFIG_OCFS2_FS
18747 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
18748 +endif
18749 +
18750 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
18751 +# tristate
18752 +ifdef CONFIG_OCFS2_FS_O2CB
18753 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
18754 +endif
18755 +
18756 +# defined in ${srctree}/fs/cifs/cifsfs.c
18757 +# tristate
18758 +ifdef CONFIG_CIFS_FS
18759 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
18760 +endif
18761 +
18762 +# defined in ${srctree}/fs/xfs/xfs_sb.h
18763 +# tristate
18764 +ifdef CONFIG_XFS_FS
18765 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
18766 +endif
18767 +
18768 +# defined in ${srctree}/fs/configfs/mount.c
18769 +# tristate
18770 +ifdef CONFIG_CONFIGFS_FS
18771 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
18772 +endif
18773 +
18774 +# defined in ${srctree}/fs/9p/v9fs.h
18775 +# tristate
18776 +ifdef CONFIG_9P_FS
18777 +ccflags-y += -DV9FS_MAGIC=0x01021997
18778 +endif
18779 +
18780 +# defined in ${srctree}/fs/ubifs/ubifs.h
18781 +# tristate
18782 +ifdef CONFIG_UBIFS_FS
18783 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
18784 +endif
18785 +
18786 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
18787 +# tristate
18788 +ifdef CONFIG_HFSPLUS_FS
18789 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
18790 +endif
18791 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
18792 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
18793 +++ linux/fs/aufs/Makefile      2012-03-19 12:24:13.679801090 +0100
18794 @@ -0,0 +1,42 @@
18795 +
18796 +include ${src}/magic.mk
18797 +ifeq (${CONFIG_AUFS_FS},m)
18798 +include ${src}/conf.mk
18799 +endif
18800 +-include ${src}/priv_def.mk
18801 +
18802 +# cf. include/linux/kernel.h
18803 +# enable pr_debug
18804 +ccflags-y += -DDEBUG
18805 +# sparse requires the full pathname
18806 +ifdef M
18807 +ccflags-y += -include ${M}/../../include/linux/aufs_type.h
18808 +else
18809 +ccflags-y += -include ${srctree}/include/linux/aufs_type.h
18810 +endif
18811 +
18812 +obj-$(CONFIG_AUFS_FS) += aufs.o
18813 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
18814 +       wkq.o vfsub.o dcsub.o \
18815 +       cpup.o whout.o wbr_policy.o \
18816 +       dinfo.o dentry.o \
18817 +       dynop.o \
18818 +       finfo.o file.o f_op.o \
18819 +       dir.o vdir.o \
18820 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
18821 +       ioctl.o
18822 +
18823 +# all are boolean
18824 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
18825 +aufs-$(CONFIG_SYSFS) += sysfs.o
18826 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
18827 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
18828 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
18829 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
18830 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
18831 +aufs-$(CONFIG_AUFS_POLL) += poll.o
18832 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
18833 +aufs-$(CONFIG_AUFS_SP_IATTR) += f_op_sp.o
18834 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
18835 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
18836 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
18837 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
18838 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
18839 +++ linux/fs/aufs/module.c      2012-03-19 12:24:13.683134508 +0100
18840 @@ -0,0 +1,196 @@
18841 +/*
18842 + * Copyright (C) 2005-2012 Junjiro R. Okajima
18843 + *
18844 + * This program, aufs is free software; you can redistribute it and/or modify
18845 + * it under the terms of the GNU General Public License as published by
18846 + * the Free Software Foundation; either version 2 of the License, or
18847 + * (at your option) any later version.
18848 + *
18849 + * This program is distributed in the hope that it will be useful,
18850 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18851 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18852 + * GNU General Public License for more details.
18853 + *
18854 + * You should have received a copy of the GNU General Public License
18855 + * along with this program; if not, write to the Free Software
18856 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18857 + */
18858 +
18859 +/*
18860 + * module global variables and operations
18861 + */
18862 +
18863 +#include <linux/module.h>
18864 +#include <linux/seq_file.h>
18865 +#include "aufs.h"
18866 +
18867 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
18868 +{
18869 +       if (new_sz <= nused)
18870 +               return p;
18871 +
18872 +       p = krealloc(p, new_sz, gfp);
18873 +       if (p)
18874 +               memset(p + nused, 0, new_sz - nused);
18875 +       return p;
18876 +}
18877 +
18878 +/* ---------------------------------------------------------------------- */
18879 +
18880 +/*
18881 + * aufs caches
18882 + */
18883 +struct kmem_cache *au_cachep[AuCache_Last];
18884 +static int __init au_cache_init(void)
18885 +{
18886 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
18887 +       if (au_cachep[AuCache_DINFO])
18888 +               /* SLAB_DESTROY_BY_RCU */
18889 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
18890 +                                                       au_icntnr_init_once);
18891 +       if (au_cachep[AuCache_ICNTNR])
18892 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
18893 +                                                      au_fi_init_once);
18894 +       if (au_cachep[AuCache_FINFO])
18895 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
18896 +       if (au_cachep[AuCache_VDIR])
18897 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
18898 +       if (au_cachep[AuCache_DEHSTR])
18899 +               return 0;
18900 +
18901 +       return -ENOMEM;
18902 +}
18903 +
18904 +static void au_cache_fin(void)
18905 +{
18906 +       int i;
18907 +
18908 +       /* excluding AuCache_HNOTIFY */
18909 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
18910 +       for (i = 0; i < AuCache_HNOTIFY; i++)
18911 +               if (au_cachep[i]) {
18912 +                       kmem_cache_destroy(au_cachep[i]);
18913 +                       au_cachep[i] = NULL;
18914 +               }
18915 +}
18916 +
18917 +/* ---------------------------------------------------------------------- */
18918 +
18919 +int au_dir_roflags;
18920 +
18921 +#ifdef CONFIG_AUFS_SBILIST
18922 +/*
18923 + * iterate_supers_type() doesn't protect us from
18924 + * remounting (branch management)
18925 + */
18926 +struct au_splhead au_sbilist;
18927 +#endif
18928 +
18929 +struct lock_class_key au_lc_key[AuLcKey_Last];
18930 +
18931 +/*
18932 + * functions for module interface.
18933 + */
18934 +MODULE_LICENSE("GPL");
18935 +/* MODULE_LICENSE("GPL v2"); */
18936 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
18937 +MODULE_DESCRIPTION(AUFS_NAME
18938 +       " -- Advanced multi layered unification filesystem");
18939 +MODULE_VERSION(AUFS_VERSION);
18940 +
18941 +/* this module parameter has no meaning when SYSFS is disabled */
18942 +int sysaufs_brs = 1;
18943 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
18944 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
18945 +
18946 +/* ---------------------------------------------------------------------- */
18947 +
18948 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
18949 +
18950 +int au_seq_path(struct seq_file *seq, struct path *path)
18951 +{
18952 +       return seq_path(seq, path, au_esc_chars);
18953 +}
18954 +
18955 +/* ---------------------------------------------------------------------- */
18956 +
18957 +static int __init aufs_init(void)
18958 +{
18959 +       int err, i;
18960 +       char *p;
18961 +
18962 +       p = au_esc_chars;
18963 +       for (i = 1; i <= ' '; i++)
18964 +               *p++ = i;
18965 +       *p++ = '\\';
18966 +       *p++ = '\x7f';
18967 +       *p = 0;
18968 +
18969 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
18970 +
18971 +       au_sbilist_init();
18972 +       sysaufs_brs_init();
18973 +       au_debug_init();
18974 +       au_dy_init();
18975 +       err = sysaufs_init();
18976 +       if (unlikely(err))
18977 +               goto out;
18978 +       err = au_procfs_init();
18979 +       if (unlikely(err))
18980 +               goto out_sysaufs;
18981 +       err = au_wkq_init();
18982 +       if (unlikely(err))
18983 +               goto out_procfs;
18984 +       err = au_loopback_init();
18985 +       if (unlikely(err))
18986 +               goto out_wkq;
18987 +       err = au_hnotify_init();
18988 +       if (unlikely(err))
18989 +               goto out_loopback;
18990 +       err = au_sysrq_init();
18991 +       if (unlikely(err))
18992 +               goto out_hin;
18993 +       err = au_cache_init();
18994 +       if (unlikely(err))
18995 +               goto out_sysrq;
18996 +       err = register_filesystem(&aufs_fs_type);
18997 +       if (unlikely(err))
18998 +               goto out_cache;
18999 +       /* since we define pr_fmt, call printk directly */
19000 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
19001 +       goto out; /* success */
19002 +
19003 +out_cache:
19004 +       au_cache_fin();
19005 +out_sysrq:
19006 +       au_sysrq_fin();
19007 +out_hin:
19008 +       au_hnotify_fin();
19009 +out_loopback:
19010 +       au_loopback_fin();
19011 +out_wkq:
19012 +       au_wkq_fin();
19013 +out_procfs:
19014 +       au_procfs_fin();
19015 +out_sysaufs:
19016 +       sysaufs_fin();
19017 +       au_dy_fin();
19018 +out:
19019 +       return err;
19020 +}
19021 +
19022 +static void __exit aufs_exit(void)
19023 +{
19024 +       unregister_filesystem(&aufs_fs_type);
19025 +       au_cache_fin();
19026 +       au_sysrq_fin();
19027 +       au_hnotify_fin();
19028 +       au_loopback_fin();
19029 +       au_wkq_fin();
19030 +       au_procfs_fin();
19031 +       sysaufs_fin();
19032 +       au_dy_fin();
19033 +}
19034 +
19035 +module_init(aufs_init);
19036 +module_exit(aufs_exit);
19037 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
19038 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
19039 +++ linux/fs/aufs/module.h      2012-03-19 12:24:13.683134508 +0100
19040 @@ -0,0 +1,105 @@
19041 +/*
19042 + * Copyright (C) 2005-2012 Junjiro R. Okajima
19043 + *
19044 + * This program, aufs is free software; you can redistribute it and/or modify
19045 + * it under the terms of the GNU General Public License as published by
19046 + * the Free Software Foundation; either version 2 of the License, or
19047 + * (at your option) any later version.
19048 + *
19049 + * This program is distributed in the hope that it will be useful,
19050 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19051 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19052 + * GNU General Public License for more details.
19053 + *
19054 + * You should have received a copy of the GNU General Public License
19055 + * along with this program; if not, write to the Free Software
19056 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19057 + */
19058 +
19059 +/*
19060 + * module initialization and module-global
19061 + */
19062 +
19063 +#ifndef __AUFS_MODULE_H__
19064 +#define __AUFS_MODULE_H__
19065 +
19066 +#ifdef __KERNEL__
19067 +
19068 +#include <linux/slab.h>
19069 +
19070 +struct path;
19071 +struct seq_file;
19072 +
19073 +/* module parameters */
19074 +extern int sysaufs_brs;
19075 +
19076 +/* ---------------------------------------------------------------------- */
19077 +
19078 +extern int au_dir_roflags;
19079 +
19080 +enum {
19081 +       AuLcNonDir_FIINFO,
19082 +       AuLcNonDir_DIINFO,
19083 +       AuLcNonDir_IIINFO,
19084 +
19085 +       AuLcDir_FIINFO,
19086 +       AuLcDir_DIINFO,
19087 +       AuLcDir_IIINFO,
19088 +
19089 +       AuLcSymlink_DIINFO,
19090 +       AuLcSymlink_IIINFO,
19091 +
19092 +       AuLcKey_Last
19093 +};
19094 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
19095 +
19096 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
19097 +int au_seq_path(struct seq_file *seq, struct path *path);
19098 +
19099 +#ifdef CONFIG_PROC_FS
19100 +/* procfs.c */
19101 +int __init au_procfs_init(void);
19102 +void au_procfs_fin(void);
19103 +#else
19104 +AuStubInt0(au_procfs_init, void);
19105 +AuStubVoid(au_procfs_fin, void);
19106 +#endif
19107 +
19108 +/* ---------------------------------------------------------------------- */
19109 +
19110 +/* kmem cache */
19111 +enum {
19112 +       AuCache_DINFO,
19113 +       AuCache_ICNTNR,
19114 +       AuCache_FINFO,
19115 +       AuCache_VDIR,
19116 +       AuCache_DEHSTR,
19117 +       AuCache_HNOTIFY, /* must be last */
19118 +       AuCache_Last
19119 +};
19120 +
19121 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
19122 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
19123 +#define AuCacheCtor(type, ctor)        \
19124 +       kmem_cache_create(#type, sizeof(struct type), \
19125 +                         __alignof__(struct type), AuCacheFlags, ctor)
19126 +
19127 +extern struct kmem_cache *au_cachep[];
19128 +
19129 +#define AuCacheFuncs(name, index) \
19130 +static inline struct au_##name *au_cache_alloc_##name(void) \
19131 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
19132 +static inline void au_cache_free_##name(struct au_##name *p) \
19133 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
19134 +
19135 +AuCacheFuncs(dinfo, DINFO);
19136 +AuCacheFuncs(icntnr, ICNTNR);
19137 +AuCacheFuncs(finfo, FINFO);
19138 +AuCacheFuncs(vdir, VDIR);
19139 +AuCacheFuncs(vdir_dehstr, DEHSTR);
19140 +#ifdef CONFIG_AUFS_HNOTIFY
19141 +AuCacheFuncs(hnotify, HNOTIFY);
19142 +#endif
19143 +
19144 +#endif /* __KERNEL__ */
19145 +#endif /* __AUFS_MODULE_H__ */
19146 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
19147 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
19148 +++ linux/fs/aufs/opts.c        2012-03-19 12:24:13.683134508 +0100
19149 @@ -0,0 +1,1677 @@
19150 +/*
19151 + * Copyright (C) 2005-2012 Junjiro R. Okajima
19152 + *
19153 + * This program, aufs is free software; you can redistribute it and/or modify
19154 + * it under the terms of the GNU General Public License as published by
19155 + * the Free Software Foundation; either version 2 of the License, or
19156 + * (at your option) any later version.
19157 + *
19158 + * This program is distributed in the hope that it will be useful,
19159 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19160 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19161 + * GNU General Public License for more details.
19162 + *
19163 + * You should have received a copy of the GNU General Public License
19164 + * along with this program; if not, write to the Free Software
19165 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19166 + */
19167 +
19168 +/*
19169 + * mount options/flags
19170 + */
19171 +
19172 +#include <linux/namei.h>
19173 +#include <linux/types.h> /* a distribution requires */
19174 +#include <linux/parser.h>
19175 +#include "aufs.h"
19176 +
19177 +/* ---------------------------------------------------------------------- */
19178 +
19179 +enum {
19180 +       Opt_br,
19181 +       Opt_add, Opt_del, Opt_mod, Opt_reorder, Opt_append, Opt_prepend,
19182 +       Opt_idel, Opt_imod, Opt_ireorder,
19183 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash, Opt_rendir,
19184 +       Opt_rdblk_def, Opt_rdhash_def,
19185 +       Opt_xino, Opt_zxino, Opt_noxino,
19186 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
19187 +       Opt_trunc_xino_path, Opt_itrunc_xino,
19188 +       Opt_trunc_xib, Opt_notrunc_xib,
19189 +       Opt_shwh, Opt_noshwh,
19190 +       Opt_plink, Opt_noplink, Opt_list_plink,
19191 +       Opt_udba,
19192 +       Opt_dio, Opt_nodio,
19193 +       /* Opt_lock, Opt_unlock, */
19194 +       Opt_cmd, Opt_cmd_args,
19195 +       Opt_diropq_a, Opt_diropq_w,
19196 +       Opt_warn_perm, Opt_nowarn_perm,
19197 +       Opt_wbr_copyup, Opt_wbr_create,
19198 +       Opt_refrof, Opt_norefrof,
19199 +       Opt_verbose, Opt_noverbose,
19200 +       Opt_sum, Opt_nosum, Opt_wsum,
19201 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
19202 +};
19203 +
19204 +static match_table_t options = {
19205 +       {Opt_br, "br=%s"},
19206 +       {Opt_br, "br:%s"},
19207 +
19208 +       {Opt_add, "add=%d:%s"},
19209 +       {Opt_add, "add:%d:%s"},
19210 +       {Opt_add, "ins=%d:%s"},
19211 +       {Opt_add, "ins:%d:%s"},
19212 +       {Opt_append, "append=%s"},
19213 +       {Opt_append, "append:%s"},
19214 +       {Opt_prepend, "prepend=%s"},
19215 +       {Opt_prepend, "prepend:%s"},
19216 +
19217 +       {Opt_del, "del=%s"},
19218 +       {Opt_del, "del:%s"},
19219 +       /* {Opt_idel, "idel:%d"}, */
19220 +       {Opt_mod, "mod=%s"},
19221 +       {Opt_mod, "mod:%s"},
19222 +       /* {Opt_imod, "imod:%d:%s"}, */
19223 +
19224 +       {Opt_dirwh, "dirwh=%d"},
19225 +
19226 +       {Opt_xino, "xino=%s"},
19227 +       {Opt_noxino, "noxino"},
19228 +       {Opt_trunc_xino, "trunc_xino"},
19229 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
19230 +       {Opt_notrunc_xino, "notrunc_xino"},
19231 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
19232 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
19233 +       /* {Opt_zxino, "zxino=%s"}, */
19234 +       {Opt_trunc_xib, "trunc_xib"},
19235 +       {Opt_notrunc_xib, "notrunc_xib"},
19236 +
19237 +#ifdef CONFIG_PROC_FS
19238 +       {Opt_plink, "plink"},
19239 +#else
19240 +       {Opt_ignore_silent, "plink"},
19241 +#endif
19242 +
19243 +       {Opt_noplink, "noplink"},
19244 +
19245 +#ifdef CONFIG_AUFS_DEBUG
19246 +       {Opt_list_plink, "list_plink"},
19247 +#endif
19248 +
19249 +       {Opt_udba, "udba=%s"},
19250 +
19251 +       {Opt_dio, "dio"},
19252 +       {Opt_nodio, "nodio"},
19253 +
19254 +       {Opt_diropq_a, "diropq=always"},
19255 +       {Opt_diropq_a, "diropq=a"},
19256 +       {Opt_diropq_w, "diropq=whiteouted"},
19257 +       {Opt_diropq_w, "diropq=w"},
19258 +
19259 +       {Opt_warn_perm, "warn_perm"},
19260 +       {Opt_nowarn_perm, "nowarn_perm"},
19261 +
19262 +       /* keep them temporary */
19263 +       {Opt_ignore_silent, "coo=%s"},
19264 +       {Opt_ignore_silent, "nodlgt"},
19265 +       {Opt_ignore_silent, "nodirperm1"},
19266 +       {Opt_ignore_silent, "clean_plink"},
19267 +
19268 +#ifdef CONFIG_AUFS_SHWH
19269 +       {Opt_shwh, "shwh"},
19270 +#endif
19271 +       {Opt_noshwh, "noshwh"},
19272 +
19273 +       {Opt_rendir, "rendir=%d"},
19274 +
19275 +       {Opt_refrof, "refrof"},
19276 +       {Opt_norefrof, "norefrof"},
19277 +
19278 +       {Opt_verbose, "verbose"},
19279 +       {Opt_verbose, "v"},
19280 +       {Opt_noverbose, "noverbose"},
19281 +       {Opt_noverbose, "quiet"},
19282 +       {Opt_noverbose, "q"},
19283 +       {Opt_noverbose, "silent"},
19284 +
19285 +       {Opt_sum, "sum"},
19286 +       {Opt_nosum, "nosum"},
19287 +       {Opt_wsum, "wsum"},
19288 +
19289 +       {Opt_rdcache, "rdcache=%d"},
19290 +       {Opt_rdblk, "rdblk=%d"},
19291 +       {Opt_rdblk_def, "rdblk=def"},
19292 +       {Opt_rdhash, "rdhash=%d"},
19293 +       {Opt_rdhash_def, "rdhash=def"},
19294 +
19295 +       {Opt_wbr_create, "create=%s"},
19296 +       {Opt_wbr_create, "create_policy=%s"},
19297 +       {Opt_wbr_copyup, "cpup=%s"},
19298 +       {Opt_wbr_copyup, "copyup=%s"},
19299 +       {Opt_wbr_copyup, "copyup_policy=%s"},
19300 +
19301 +       /* internal use for the scripts */
19302 +       {Opt_ignore_silent, "si=%s"},
19303 +
19304 +       {Opt_br, "dirs=%s"},
19305 +       {Opt_ignore, "debug=%d"},
19306 +       {Opt_ignore, "delete=whiteout"},
19307 +       {Opt_ignore, "delete=all"},
19308 +       {Opt_ignore, "imap=%s"},
19309 +
19310 +       /* temporary workaround, due to old mount(8)? */
19311 +       {Opt_ignore_silent, "relatime"},
19312 +
19313 +       {Opt_err, NULL}
19314 +};
19315 +
19316 +/* ---------------------------------------------------------------------- */
19317 +
19318 +static const char *au_parser_pattern(int val, struct match_token *token)
19319 +{
19320 +       while (token->pattern) {
19321 +               if (token->token == val)
19322 +                       return token->pattern;
19323 +               token++;
19324 +       }
19325 +       BUG();
19326 +       return "??";
19327 +}
19328 +
19329 +/* ---------------------------------------------------------------------- */
19330 +
19331 +static match_table_t brperm = {
19332 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
19333 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
19334 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
19335 +       {0, NULL}
19336 +};
19337 +
19338 +static match_table_t brrattr = {
19339 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
19340 +       {0, NULL}
19341 +};
19342 +
19343 +static match_table_t brwattr = {
19344 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
19345 +       {0, NULL}
19346 +};
19347 +
19348 +#define AuBrStr_LONGEST        AUFS_BRPERM_RW "+" AUFS_BRWATTR_NLWH
19349 +
19350 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
19351 +{
19352 +       int attr, v;
19353 +       char *p;
19354 +
19355 +       attr = 0;
19356 +       do {
19357 +               p = strchr(str, '+');
19358 +               if (p)
19359 +                       *p = 0;
19360 +               v = match_token(str, table, args);
19361 +               if (v)
19362 +                       attr |= v;
19363 +               else {
19364 +                       if (p)
19365 +                               *p = '+';
19366 +                       pr_warning("ignored branch attribute %s\n", str);
19367 +                       break;
19368 +               }
19369 +               if (p)
19370 +                       str = p + 1;
19371 +       } while (p);
19372 +
19373 +       return attr;
19374 +}
19375 +
19376 +static int noinline_for_stack br_perm_val(char *perm)
19377 +{
19378 +       int val;
19379 +       char *p;
19380 +       substring_t args[MAX_OPT_ARGS];
19381 +
19382 +       p = strchr(perm, '+');
19383 +       if (p)
19384 +               *p = 0;
19385 +       val = match_token(perm, brperm, args);
19386 +       if (!val) {
19387 +               if (p)
19388 +                       *p = '+';
19389 +               pr_warning("ignored branch permission %s\n", perm);
19390 +               val = AuBrPerm_RO;
19391 +               goto out;
19392 +       }
19393 +       if (!p)
19394 +               goto out;
19395 +
19396 +       switch (val) {
19397 +       case AuBrPerm_RO:
19398 +       case AuBrPerm_RR:
19399 +               val |= br_attr_val(p + 1, brrattr, args);
19400 +               break;
19401 +       case AuBrPerm_RW:
19402 +               val |= br_attr_val(p + 1, brwattr, args);
19403 +               break;
19404 +       }
19405 +
19406 +out:
19407 +       return val;
19408 +}
19409 +
19410 +/* Caller should free the return value */
19411 +char *au_optstr_br_perm(int brperm)
19412 +{
19413 +       char *p, a[sizeof(AuBrStr_LONGEST)];
19414 +       int sz;
19415 +
19416 +#define SetPerm(str) do {                      \
19417 +               sz = sizeof(str);               \
19418 +               memcpy(a, str, sz);             \
19419 +               p = a + sz - 1;                 \
19420 +       } while (0)
19421 +
19422 +#define AppendAttr(flag, str) do {                     \
19423 +               if (brperm & flag) {            \
19424 +                       sz = sizeof(str);       \
19425 +                       *p++ = '+';             \
19426 +                       memcpy(p, str, sz);     \
19427 +                       p += sz - 1;            \
19428 +               }                               \
19429 +       } while (0)
19430 +
19431 +       switch (brperm & AuBrPerm_Mask) {
19432 +       case AuBrPerm_RO:
19433 +               SetPerm(AUFS_BRPERM_RO);
19434 +               break;
19435 +       case AuBrPerm_RR:
19436 +               SetPerm(AUFS_BRPERM_RR);
19437 +               break;
19438 +       case AuBrPerm_RW:
19439 +               SetPerm(AUFS_BRPERM_RW);
19440 +               break;
19441 +       default:
19442 +               AuDebugOn(1);
19443 +       }
19444 +
19445 +       AppendAttr(AuBrRAttr_WH, AUFS_BRRATTR_WH);
19446 +       AppendAttr(AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH);
19447 +
19448 +       AuDebugOn(strlen(a) >= sizeof(a));
19449 +       return kstrdup(a, GFP_NOFS);
19450 +#undef SetPerm
19451 +#undef AppendAttr
19452 +}
19453 +
19454 +/* ---------------------------------------------------------------------- */
19455 +
19456 +static match_table_t udbalevel = {
19457 +       {AuOpt_UDBA_REVAL, "reval"},
19458 +       {AuOpt_UDBA_NONE, "none"},
19459 +#ifdef CONFIG_AUFS_HNOTIFY
19460 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
19461 +#ifdef CONFIG_AUFS_HFSNOTIFY
19462 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
19463 +#endif
19464 +#endif
19465 +       {-1, NULL}
19466 +};
19467 +
19468 +static int noinline_for_stack udba_val(char *str)
19469 +{
19470 +       substring_t args[MAX_OPT_ARGS];
19471 +
19472 +       return match_token(str, udbalevel, args);
19473 +}
19474 +
19475 +const char *au_optstr_udba(int udba)
19476 +{
19477 +       return au_parser_pattern(udba, (void *)udbalevel);
19478 +}
19479 +
19480 +/* ---------------------------------------------------------------------- */
19481 +
19482 +static match_table_t au_wbr_create_policy = {
19483 +       {AuWbrCreate_TDP, "tdp"},
19484 +       {AuWbrCreate_TDP, "top-down-parent"},
19485 +       {AuWbrCreate_RR, "rr"},
19486 +       {AuWbrCreate_RR, "round-robin"},
19487 +       {AuWbrCreate_MFS, "mfs"},
19488 +       {AuWbrCreate_MFS, "most-free-space"},
19489 +       {AuWbrCreate_MFSV, "mfs:%d"},
19490 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
19491 +
19492 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
19493 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
19494 +       {AuWbrCreate_PMFS, "pmfs"},
19495 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
19496 +
19497 +       {-1, NULL}
19498 +};
19499 +
19500 +/*
19501 + * cf. linux/lib/parser.c and cmdline.c
19502 + * gave up calling memparse() since it uses simple_strtoull() instead of
19503 + * kstrto...().
19504 + */
19505 +static int noinline_for_stack
19506 +au_match_ull(substring_t *s, unsigned long long *result)
19507 +{
19508 +       int err;
19509 +       unsigned int len;
19510 +       char a[32];
19511 +
19512 +       err = -ERANGE;
19513 +       len = s->to - s->from;
19514 +       if (len + 1 <= sizeof(a)) {
19515 +               memcpy(a, s->from, len);
19516 +               a[len] = '\0';
19517 +               err = kstrtoull(a, 0, result);
19518 +       }
19519 +       return err;
19520 +}
19521 +
19522 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
19523 +                           struct au_opt_wbr_create *create)
19524 +{
19525 +       int err;
19526 +       unsigned long long ull;
19527 +
19528 +       err = 0;
19529 +       if (!au_match_ull(arg, &ull))
19530 +               create->mfsrr_watermark = ull;
19531 +       else {
19532 +               pr_err("bad integer in %s\n", str);
19533 +               err = -EINVAL;
19534 +       }
19535 +
19536 +       return err;
19537 +}
19538 +
19539 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
19540 +                         struct au_opt_wbr_create *create)
19541 +{
19542 +       int n, err;
19543 +
19544 +       err = 0;
19545 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
19546 +               create->mfs_second = n;
19547 +       else {
19548 +               pr_err("bad integer in %s\n", str);
19549 +               err = -EINVAL;
19550 +       }
19551 +
19552 +       return err;
19553 +}
19554 +
19555 +static int noinline_for_stack
19556 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
19557 +{
19558 +       int err, e;
19559 +       substring_t args[MAX_OPT_ARGS];
19560 +
19561 +       err = match_token(str, au_wbr_create_policy, args);
19562 +       create->wbr_create = err;
19563 +       switch (err) {
19564 +       case AuWbrCreate_MFSRRV:
19565 +               e = au_wbr_mfs_wmark(&args[0], str, create);
19566 +               if (!e)
19567 +                       e = au_wbr_mfs_sec(&args[1], str, create);
19568 +               if (unlikely(e))
19569 +                       err = e;
19570 +               break;
19571 +       case AuWbrCreate_MFSRR:
19572 +               e = au_wbr_mfs_wmark(&args[0], str, create);
19573 +               if (unlikely(e)) {
19574 +                       err = e;
19575 +                       break;
19576 +               }
19577 +               /*FALLTHROUGH*/
19578 +       case AuWbrCreate_MFS:
19579 +       case AuWbrCreate_PMFS:
19580 +               create->mfs_second = AUFS_MFS_DEF_SEC;
19581 +               break;
19582 +       case AuWbrCreate_MFSV:
19583 +       case AuWbrCreate_PMFSV:
19584 +               e = au_wbr_mfs_sec(&args[0], str, create);
19585 +               if (unlikely(e))
19586 +                       err = e;
19587 +               break;
19588 +       }
19589 +
19590 +       return err;
19591 +}
19592 +
19593 +const char *au_optstr_wbr_create(int wbr_create)
19594 +{
19595 +       return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
19596 +}
19597 +
19598 +static match_table_t au_wbr_copyup_policy = {
19599 +       {AuWbrCopyup_TDP, "tdp"},
19600 +       {AuWbrCopyup_TDP, "top-down-parent"},
19601 +       {AuWbrCopyup_BUP, "bup"},
19602 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
19603 +       {AuWbrCopyup_BU, "bu"},
19604 +       {AuWbrCopyup_BU, "bottom-up"},
19605 +       {-1, NULL}
19606 +};
19607 +
19608 +static int noinline_for_stack au_wbr_copyup_val(char *str)
19609 +{
19610 +       substring_t args[MAX_OPT_ARGS];
19611 +
19612 +       return match_token(str, au_wbr_copyup_policy, args);
19613 +}
19614 +
19615 +const char *au_optstr_wbr_copyup(int wbr_copyup)
19616 +{
19617 +       return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
19618 +}
19619 +
19620 +/* ---------------------------------------------------------------------- */
19621 +
19622 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
19623 +
19624 +static void dump_opts(struct au_opts *opts)
19625 +{
19626 +#ifdef CONFIG_AUFS_DEBUG
19627 +       /* reduce stack space */
19628 +       union {
19629 +               struct au_opt_add *add;
19630 +               struct au_opt_del *del;
19631 +               struct au_opt_mod *mod;
19632 +               struct au_opt_xino *xino;
19633 +               struct au_opt_xino_itrunc *xino_itrunc;
19634 +               struct au_opt_wbr_create *create;
19635 +       } u;
19636 +       struct au_opt *opt;
19637 +
19638 +       opt = opts->opt;
19639 +       while (opt->type != Opt_tail) {
19640 +               switch (opt->type) {
19641 +               case Opt_add:
19642 +                       u.add = &opt->add;
19643 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
19644 +                                 u.add->bindex, u.add->pathname, u.add->perm,
19645 +                                 u.add->path.dentry);
19646 +                       break;
19647 +               case Opt_del:
19648 +               case Opt_idel:
19649 +                       u.del = &opt->del;
19650 +                       AuDbg("del {%s, %p}\n",
19651 +                             u.del->pathname, u.del->h_path.dentry);
19652 +                       break;
19653 +               case Opt_mod:
19654 +               case Opt_imod:
19655 +                       u.mod = &opt->mod;
19656 +                       AuDbg("mod {%s, 0x%x, %p}\n",
19657 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
19658 +                       break;
19659 +               case Opt_append:
19660 +                       u.add = &opt->add;
19661 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
19662 +                                 u.add->bindex, u.add->pathname, u.add->perm,
19663 +                                 u.add->path.dentry);
19664 +                       break;
19665 +               case Opt_prepend:
19666 +                       u.add = &opt->add;
19667 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
19668 +                                 u.add->bindex, u.add->pathname, u.add->perm,
19669 +                                 u.add->path.dentry);
19670 +                       break;
19671 +               case Opt_dirwh:
19672 +                       AuDbg("dirwh %d\n", opt->dirwh);
19673 +                       break;
19674 +               case Opt_rdcache:
19675 +                       AuDbg("rdcache %d\n", opt->rdcache);
19676 +                       break;
19677 +               case Opt_rdblk:
19678 +                       AuDbg("rdblk %u\n", opt->rdblk);
19679 +                       break;
19680 +               case Opt_rdblk_def:
19681 +                       AuDbg("rdblk_def\n");
19682 +                       break;
19683 +               case Opt_rdhash:
19684 +                       AuDbg("rdhash %u\n", opt->rdhash);
19685 +                       break;
19686 +               case Opt_rdhash_def:
19687 +                       AuDbg("rdhash_def\n");
19688 +                       break;
19689 +               case Opt_xino:
19690 +                       u.xino = &opt->xino;
19691 +                       AuDbg("xino {%s %.*s}\n",
19692 +                                 u.xino->path,
19693 +                                 AuDLNPair(u.xino->file->f_dentry));
19694 +                       break;
19695 +               case Opt_trunc_xino:
19696 +                       AuLabel(trunc_xino);
19697 +                       break;
19698 +               case Opt_notrunc_xino:
19699 +                       AuLabel(notrunc_xino);
19700 +                       break;
19701 +               case Opt_trunc_xino_path:
19702 +               case Opt_itrunc_xino:
19703 +                       u.xino_itrunc = &opt->xino_itrunc;
19704 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
19705 +                       break;
19706 +
19707 +               case Opt_noxino:
19708 +                       AuLabel(noxino);
19709 +                       break;
19710 +               case Opt_trunc_xib:
19711 +                       AuLabel(trunc_xib);
19712 +                       break;
19713 +               case Opt_notrunc_xib:
19714 +                       AuLabel(notrunc_xib);
19715 +                       break;
19716 +               case Opt_shwh:
19717 +                       AuLabel(shwh);
19718 +                       break;
19719 +               case Opt_noshwh:
19720 +                       AuLabel(noshwh);
19721 +                       break;
19722 +               case Opt_plink:
19723 +                       AuLabel(plink);
19724 +                       break;
19725 +               case Opt_noplink:
19726 +                       AuLabel(noplink);
19727 +                       break;
19728 +               case Opt_list_plink:
19729 +                       AuLabel(list_plink);
19730 +                       break;
19731 +               case Opt_udba:
19732 +                       AuDbg("udba %d, %s\n",
19733 +                                 opt->udba, au_optstr_udba(opt->udba));
19734 +                       break;
19735 +               case Opt_dio:
19736 +                       AuLabel(dio);
19737 +                       break;
19738 +               case Opt_nodio:
19739 +                       AuLabel(nodio);
19740 +                       break;
19741 +               case Opt_diropq_a:
19742 +                       AuLabel(diropq_a);
19743 +                       break;
19744 +               case Opt_diropq_w:
19745 +                       AuLabel(diropq_w);
19746 +                       break;
19747 +               case Opt_warn_perm:
19748 +                       AuLabel(warn_perm);
19749 +                       break;
19750 +               case Opt_nowarn_perm:
19751 +                       AuLabel(nowarn_perm);
19752 +                       break;
19753 +               case Opt_refrof:
19754 +                       AuLabel(refrof);
19755 +                       break;
19756 +               case Opt_norefrof:
19757 +                       AuLabel(norefrof);
19758 +                       break;
19759 +               case Opt_verbose:
19760 +                       AuLabel(verbose);
19761 +                       break;
19762 +               case Opt_noverbose:
19763 +                       AuLabel(noverbose);
19764 +                       break;
19765 +               case Opt_sum:
19766 +                       AuLabel(sum);
19767 +                       break;
19768 +               case Opt_nosum:
19769 +                       AuLabel(nosum);
19770 +                       break;
19771 +               case Opt_wsum:
19772 +                       AuLabel(wsum);
19773 +                       break;
19774 +               case Opt_wbr_create:
19775 +                       u.create = &opt->wbr_create;
19776 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
19777 +                                 au_optstr_wbr_create(u.create->wbr_create));
19778 +                       switch (u.create->wbr_create) {
19779 +                       case AuWbrCreate_MFSV:
19780 +                       case AuWbrCreate_PMFSV:
19781 +                               AuDbg("%d sec\n", u.create->mfs_second);
19782 +                               break;
19783 +                       case AuWbrCreate_MFSRR:
19784 +                               AuDbg("%llu watermark\n",
19785 +                                         u.create->mfsrr_watermark);
19786 +                               break;
19787 +                       case AuWbrCreate_MFSRRV:
19788 +                               AuDbg("%llu watermark, %d sec\n",
19789 +                                         u.create->mfsrr_watermark,
19790 +                                         u.create->mfs_second);
19791 +                               break;
19792 +                       }
19793 +                       break;
19794 +               case Opt_wbr_copyup:
19795 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
19796 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
19797 +                       break;
19798 +               default:
19799 +                       BUG();
19800 +               }
19801 +               opt++;
19802 +       }
19803 +#endif
19804 +}
19805 +
19806 +void au_opts_free(struct au_opts *opts)
19807 +{
19808 +       struct au_opt *opt;
19809 +
19810 +       opt = opts->opt;
19811 +       while (opt->type != Opt_tail) {
19812 +               switch (opt->type) {
19813 +               case Opt_add:
19814 +               case Opt_append:
19815 +               case Opt_prepend:
19816 +                       path_put(&opt->add.path);
19817 +                       break;
19818 +               case Opt_del:
19819 +               case Opt_idel:
19820 +                       path_put(&opt->del.h_path);
19821 +                       break;
19822 +               case Opt_mod:
19823 +               case Opt_imod:
19824 +                       dput(opt->mod.h_root);
19825 +                       break;
19826 +               case Opt_xino:
19827 +                       fput(opt->xino.file);
19828 +                       break;
19829 +               }
19830 +               opt++;
19831 +       }
19832 +}
19833 +
19834 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
19835 +                  aufs_bindex_t bindex)
19836 +{
19837 +       int err;
19838 +       struct au_opt_add *add = &opt->add;
19839 +       char *p;
19840 +
19841 +       add->bindex = bindex;
19842 +       add->perm = AuBrPerm_RO;
19843 +       add->pathname = opt_str;
19844 +       p = strchr(opt_str, '=');
19845 +       if (p) {
19846 +               *p++ = 0;
19847 +               if (*p)
19848 +                       add->perm = br_perm_val(p);
19849 +       }
19850 +
19851 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
19852 +       if (!err) {
19853 +               if (!p) {
19854 +                       add->perm = AuBrPerm_RO;
19855 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
19856 +                               add->perm = AuBrPerm_RR;
19857 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
19858 +                               add->perm = AuBrPerm_RW;
19859 +               }
19860 +               opt->type = Opt_add;
19861 +               goto out;
19862 +       }
19863 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
19864 +       err = -EINVAL;
19865 +
19866 +out:
19867 +       return err;
19868 +}
19869 +
19870 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
19871 +{
19872 +       int err;
19873 +
19874 +       del->pathname = args[0].from;
19875 +       AuDbg("del path %s\n", del->pathname);
19876 +
19877 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
19878 +       if (unlikely(err))
19879 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
19880 +
19881 +       return err;
19882 +}
19883 +
19884 +#if 0 /* reserved for future use */
19885 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
19886 +                             struct au_opt_del *del, substring_t args[])
19887 +{
19888 +       int err;
19889 +       struct dentry *root;
19890 +
19891 +       err = -EINVAL;
19892 +       root = sb->s_root;
19893 +       aufs_read_lock(root, AuLock_FLUSH);
19894 +       if (bindex < 0 || au_sbend(sb) < bindex) {
19895 +               pr_err("out of bounds, %d\n", bindex);
19896 +               goto out;
19897 +       }
19898 +
19899 +       err = 0;
19900 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
19901 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
19902 +
19903 +out:
19904 +       aufs_read_unlock(root, !AuLock_IR);
19905 +       return err;
19906 +}
19907 +#endif
19908 +
19909 +static int noinline_for_stack
19910 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
19911 +{
19912 +       int err;
19913 +       struct path path;
19914 +       char *p;
19915 +
19916 +       err = -EINVAL;
19917 +       mod->path = args[0].from;
19918 +       p = strchr(mod->path, '=');
19919 +       if (unlikely(!p)) {
19920 +               pr_err("no permssion %s\n", args[0].from);
19921 +               goto out;
19922 +       }
19923 +
19924 +       *p++ = 0;
19925 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
19926 +       if (unlikely(err)) {
19927 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
19928 +               goto out;
19929 +       }
19930 +
19931 +       mod->perm = br_perm_val(p);
19932 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
19933 +       mod->h_root = dget(path.dentry);
19934 +       path_put(&path);
19935 +
19936 +out:
19937 +       return err;
19938 +}
19939 +
19940 +#if 0 /* reserved for future use */
19941 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
19942 +                             struct au_opt_mod *mod, substring_t args[])
19943 +{
19944 +       int err;
19945 +       struct dentry *root;
19946 +
19947 +       err = -EINVAL;
19948 +       root = sb->s_root;
19949 +       aufs_read_lock(root, AuLock_FLUSH);
19950 +       if (bindex < 0 || au_sbend(sb) < bindex) {
19951 +               pr_err("out of bounds, %d\n", bindex);
19952 +               goto out;
19953 +       }
19954 +
19955 +       err = 0;
19956 +       mod->perm = br_perm_val(args[1].from);
19957 +       AuDbg("mod path %s, perm 0x%x, %s\n",
19958 +             mod->path, mod->perm, args[1].from);
19959 +       mod->h_root = dget(au_h_dptr(root, bindex));
19960 +
19961 +out:
19962 +       aufs_read_unlock(root, !AuLock_IR);
19963 +       return err;
19964 +}
19965 +#endif
19966 +
19967 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
19968 +                             substring_t args[])
19969 +{
19970 +       int err;
19971 +       struct file *file;
19972 +
19973 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
19974 +       err = PTR_ERR(file);
19975 +       if (IS_ERR(file))
19976 +               goto out;
19977 +
19978 +       err = -EINVAL;
19979 +       if (unlikely(file->f_dentry->d_sb == sb)) {
19980 +               fput(file);
19981 +               pr_err("%s must be outside\n", args[0].from);
19982 +               goto out;
19983 +       }
19984 +
19985 +       err = 0;
19986 +       xino->file = file;
19987 +       xino->path = args[0].from;
19988 +
19989 +out:
19990 +       return err;
19991 +}
19992 +
19993 +static int noinline_for_stack
19994 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
19995 +                              struct au_opt_xino_itrunc *xino_itrunc,
19996 +                              substring_t args[])
19997 +{
19998 +       int err;
19999 +       aufs_bindex_t bend, bindex;
20000 +       struct path path;
20001 +       struct dentry *root;
20002 +
20003 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
20004 +       if (unlikely(err)) {
20005 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
20006 +               goto out;
20007 +       }
20008 +
20009 +       xino_itrunc->bindex = -1;
20010 +       root = sb->s_root;
20011 +       aufs_read_lock(root, AuLock_FLUSH);
20012 +       bend = au_sbend(sb);
20013 +       for (bindex = 0; bindex <= bend; bindex++) {
20014 +               if (au_h_dptr(root, bindex) == path.dentry) {
20015 +                       xino_itrunc->bindex = bindex;
20016 +                       break;
20017 +               }
20018 +       }
20019 +       aufs_read_unlock(root, !AuLock_IR);
20020 +       path_put(&path);
20021 +
20022 +       if (unlikely(xino_itrunc->bindex < 0)) {
20023 +               pr_err("no such branch %s\n", args[0].from);
20024 +               err = -EINVAL;
20025 +       }
20026 +
20027 +out:
20028 +       return err;
20029 +}
20030 +
20031 +/* called without aufs lock */
20032 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
20033 +{
20034 +       int err, n, token;
20035 +       aufs_bindex_t bindex;
20036 +       unsigned char skipped;
20037 +       struct dentry *root;
20038 +       struct au_opt *opt, *opt_tail;
20039 +       char *opt_str;
20040 +       /* reduce the stack space */
20041 +       union {
20042 +               struct au_opt_xino_itrunc *xino_itrunc;
20043 +               struct au_opt_wbr_create *create;
20044 +       } u;
20045 +       struct {
20046 +               substring_t args[MAX_OPT_ARGS];
20047 +       } *a;
20048 +
20049 +       err = -ENOMEM;
20050 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20051 +       if (unlikely(!a))
20052 +               goto out;
20053 +
20054 +       root = sb->s_root;
20055 +       err = 0;
20056 +       bindex = 0;
20057 +       opt = opts->opt;
20058 +       opt_tail = opt + opts->max_opt - 1;
20059 +       opt->type = Opt_tail;
20060 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
20061 +               err = -EINVAL;
20062 +               skipped = 0;
20063 +               token = match_token(opt_str, options, a->args);
20064 +               switch (token) {
20065 +               case Opt_br:
20066 +                       err = 0;
20067 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
20068 +                              && *opt_str) {
20069 +                               err = opt_add(opt, opt_str, opts->sb_flags,
20070 +                                             bindex++);
20071 +                               if (unlikely(!err && ++opt > opt_tail)) {
20072 +                                       err = -E2BIG;
20073 +                                       break;
20074 +                               }
20075 +                               opt->type = Opt_tail;
20076 +                               skipped = 1;
20077 +                       }
20078 +                       break;
20079 +               case Opt_add:
20080 +                       if (unlikely(match_int(&a->args[0], &n))) {
20081 +                               pr_err("bad integer in %s\n", opt_str);
20082 +                               break;
20083 +                       }
20084 +                       bindex = n;
20085 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
20086 +                                     bindex);
20087 +                       if (!err)
20088 +                               opt->type = token;
20089 +                       break;
20090 +               case Opt_append:
20091 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
20092 +                                     /*dummy bindex*/1);
20093 +                       if (!err)
20094 +                               opt->type = token;
20095 +                       break;
20096 +               case Opt_prepend:
20097 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
20098 +                                     /*bindex*/0);
20099 +                       if (!err)
20100 +                               opt->type = token;
20101 +                       break;
20102 +               case Opt_del:
20103 +                       err = au_opts_parse_del(&opt->del, a->args);
20104 +                       if (!err)
20105 +                               opt->type = token;
20106 +                       break;
20107 +#if 0 /* reserved for future use */
20108 +               case Opt_idel:
20109 +                       del->pathname = "(indexed)";
20110 +                       if (unlikely(match_int(&args[0], &n))) {
20111 +                               pr_err("bad integer in %s\n", opt_str);
20112 +                               break;
20113 +                       }
20114 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
20115 +                       if (!err)
20116 +                               opt->type = token;
20117 +                       break;
20118 +#endif
20119 +               case Opt_mod:
20120 +                       err = au_opts_parse_mod(&opt->mod, a->args);
20121 +                       if (!err)
20122 +                               opt->type = token;
20123 +                       break;
20124 +#ifdef IMOD /* reserved for future use */
20125 +               case Opt_imod:
20126 +                       u.mod->path = "(indexed)";
20127 +                       if (unlikely(match_int(&a->args[0], &n))) {
20128 +                               pr_err("bad integer in %s\n", opt_str);
20129 +                               break;
20130 +                       }
20131 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
20132 +                       if (!err)
20133 +                               opt->type = token;
20134 +                       break;
20135 +#endif
20136 +               case Opt_xino:
20137 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
20138 +                       if (!err)
20139 +                               opt->type = token;
20140 +                       break;
20141 +
20142 +               case Opt_trunc_xino_path:
20143 +                       err = au_opts_parse_xino_itrunc_path
20144 +                               (sb, &opt->xino_itrunc, a->args);
20145 +                       if (!err)
20146 +                               opt->type = token;
20147 +                       break;
20148 +
20149 +               case Opt_itrunc_xino:
20150 +                       u.xino_itrunc = &opt->xino_itrunc;
20151 +                       if (unlikely(match_int(&a->args[0], &n))) {
20152 +                               pr_err("bad integer in %s\n", opt_str);
20153 +                               break;
20154 +                       }
20155 +                       u.xino_itrunc->bindex = n;
20156 +                       aufs_read_lock(root, AuLock_FLUSH);
20157 +                       if (n < 0 || au_sbend(sb) < n) {
20158 +                               pr_err("out of bounds, %d\n", n);
20159 +                               aufs_read_unlock(root, !AuLock_IR);
20160 +                               break;
20161 +                       }
20162 +                       aufs_read_unlock(root, !AuLock_IR);
20163 +                       err = 0;
20164 +                       opt->type = token;
20165 +                       break;
20166 +
20167 +               case Opt_dirwh:
20168 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
20169 +                               break;
20170 +                       err = 0;
20171 +                       opt->type = token;
20172 +                       break;
20173 +
20174 +               case Opt_rdcache:
20175 +                       if (unlikely(match_int(&a->args[0], &n))) {
20176 +                               pr_err("bad integer in %s\n", opt_str);
20177 +                               break;
20178 +                       }
20179 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
20180 +                               pr_err("rdcache must be smaller than %d\n",
20181 +                                      AUFS_RDCACHE_MAX);
20182 +                               break;
20183 +                       }
20184 +                       opt->rdcache = n;
20185 +                       err = 0;
20186 +                       opt->type = token;
20187 +                       break;
20188 +               case Opt_rdblk:
20189 +                       if (unlikely(match_int(&a->args[0], &n)
20190 +                                    || n < 0
20191 +                                    || n > KMALLOC_MAX_SIZE)) {
20192 +                               pr_err("bad integer in %s\n", opt_str);
20193 +                               break;
20194 +                       }
20195 +                       if (unlikely(n && n < NAME_MAX)) {
20196 +                               pr_err("rdblk must be larger than %d\n",
20197 +                                      NAME_MAX);
20198 +                               break;
20199 +                       }
20200 +                       opt->rdblk = n;
20201 +                       err = 0;
20202 +                       opt->type = token;
20203 +                       break;
20204 +               case Opt_rdhash:
20205 +                       if (unlikely(match_int(&a->args[0], &n)
20206 +                                    || n < 0
20207 +                                    || n * sizeof(struct hlist_head)
20208 +                                    > KMALLOC_MAX_SIZE)) {
20209 +                               pr_err("bad integer in %s\n", opt_str);
20210 +                               break;
20211 +                       }
20212 +                       opt->rdhash = n;
20213 +                       err = 0;
20214 +                       opt->type = token;
20215 +                       break;
20216 +
20217 +               case Opt_trunc_xino:
20218 +               case Opt_notrunc_xino:
20219 +               case Opt_noxino:
20220 +               case Opt_trunc_xib:
20221 +               case Opt_notrunc_xib:
20222 +               case Opt_shwh:
20223 +               case Opt_noshwh:
20224 +               case Opt_plink:
20225 +               case Opt_noplink:
20226 +               case Opt_list_plink:
20227 +               case Opt_dio:
20228 +               case Opt_nodio:
20229 +               case Opt_diropq_a:
20230 +               case Opt_diropq_w:
20231 +               case Opt_warn_perm:
20232 +               case Opt_nowarn_perm:
20233 +               case Opt_refrof:
20234 +               case Opt_norefrof:
20235 +               case Opt_verbose:
20236 +               case Opt_noverbose:
20237 +               case Opt_sum:
20238 +               case Opt_nosum:
20239 +               case Opt_wsum:
20240 +               case Opt_rdblk_def:
20241 +               case Opt_rdhash_def:
20242 +                       err = 0;
20243 +                       opt->type = token;
20244 +                       break;
20245 +
20246 +               case Opt_udba:
20247 +                       opt->udba = udba_val(a->args[0].from);
20248 +                       if (opt->udba >= 0) {
20249 +                               err = 0;
20250 +                               opt->type = token;
20251 +                       } else
20252 +                               pr_err("wrong value, %s\n", opt_str);
20253 +                       break;
20254 +
20255 +               case Opt_wbr_create:
20256 +                       u.create = &opt->wbr_create;
20257 +                       u.create->wbr_create
20258 +                               = au_wbr_create_val(a->args[0].from, u.create);
20259 +                       if (u.create->wbr_create >= 0) {
20260 +                               err = 0;
20261 +                               opt->type = token;
20262 +                       } else
20263 +                               pr_err("wrong value, %s\n", opt_str);
20264 +                       break;
20265 +               case Opt_wbr_copyup:
20266 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
20267 +                       if (opt->wbr_copyup >= 0) {
20268 +                               err = 0;
20269 +                               opt->type = token;
20270 +                       } else
20271 +                               pr_err("wrong value, %s\n", opt_str);
20272 +                       break;
20273 +
20274 +               case Opt_ignore:
20275 +                       pr_warning("ignored %s\n", opt_str);
20276 +                       /*FALLTHROUGH*/
20277 +               case Opt_ignore_silent:
20278 +                       skipped = 1;
20279 +                       err = 0;
20280 +                       break;
20281 +               case Opt_err:
20282 +                       pr_err("unknown option %s\n", opt_str);
20283 +                       break;
20284 +               }
20285 +
20286 +               if (!err && !skipped) {
20287 +                       if (unlikely(++opt > opt_tail)) {
20288 +                               err = -E2BIG;
20289 +                               opt--;
20290 +                               opt->type = Opt_tail;
20291 +                               break;
20292 +                       }
20293 +                       opt->type = Opt_tail;
20294 +               }
20295 +       }
20296 +
20297 +       kfree(a);
20298 +       dump_opts(opts);
20299 +       if (unlikely(err))
20300 +               au_opts_free(opts);
20301 +
20302 +out:
20303 +       return err;
20304 +}
20305 +
20306 +static int au_opt_wbr_create(struct super_block *sb,
20307 +                            struct au_opt_wbr_create *create)
20308 +{
20309 +       int err;
20310 +       struct au_sbinfo *sbinfo;
20311 +
20312 +       SiMustWriteLock(sb);
20313 +
20314 +       err = 1; /* handled */
20315 +       sbinfo = au_sbi(sb);
20316 +       if (sbinfo->si_wbr_create_ops->fin) {
20317 +               err = sbinfo->si_wbr_create_ops->fin(sb);
20318 +               if (!err)
20319 +                       err = 1;
20320 +       }
20321 +
20322 +       sbinfo->si_wbr_create = create->wbr_create;
20323 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
20324 +       switch (create->wbr_create) {
20325 +       case AuWbrCreate_MFSRRV:
20326 +       case AuWbrCreate_MFSRR:
20327 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
20328 +               /*FALLTHROUGH*/
20329 +       case AuWbrCreate_MFS:
20330 +       case AuWbrCreate_MFSV:
20331 +       case AuWbrCreate_PMFS:
20332 +       case AuWbrCreate_PMFSV:
20333 +               sbinfo->si_wbr_mfs.mfs_expire
20334 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
20335 +               break;
20336 +       }
20337 +
20338 +       if (sbinfo->si_wbr_create_ops->init)
20339 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
20340 +
20341 +       return err;
20342 +}
20343 +
20344 +/*
20345 + * returns,
20346 + * plus: processed without an error
20347 + * zero: unprocessed
20348 + */
20349 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
20350 +                        struct au_opts *opts)
20351 +{
20352 +       int err;
20353 +       struct au_sbinfo *sbinfo;
20354 +
20355 +       SiMustWriteLock(sb);
20356 +
20357 +       err = 1; /* handled */
20358 +       sbinfo = au_sbi(sb);
20359 +       switch (opt->type) {
20360 +       case Opt_udba:
20361 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
20362 +               sbinfo->si_mntflags |= opt->udba;
20363 +               opts->given_udba |= opt->udba;
20364 +               break;
20365 +
20366 +       case Opt_plink:
20367 +               au_opt_set(sbinfo->si_mntflags, PLINK);
20368 +               break;
20369 +       case Opt_noplink:
20370 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
20371 +                       au_plink_put(sb, /*verbose*/1);
20372 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
20373 +               break;
20374 +       case Opt_list_plink:
20375 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
20376 +                       au_plink_list(sb);
20377 +               break;
20378 +
20379 +       case Opt_dio:
20380 +               au_opt_set(sbinfo->si_mntflags, DIO);
20381 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
20382 +               break;
20383 +       case Opt_nodio:
20384 +               au_opt_clr(sbinfo->si_mntflags, DIO);
20385 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
20386 +               break;
20387 +
20388 +       case Opt_diropq_a:
20389 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
20390 +               break;
20391 +       case Opt_diropq_w:
20392 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
20393 +               break;
20394 +
20395 +       case Opt_warn_perm:
20396 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
20397 +               break;
20398 +       case Opt_nowarn_perm:
20399 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
20400 +               break;
20401 +
20402 +       case Opt_refrof:
20403 +               au_opt_set(sbinfo->si_mntflags, REFROF);
20404 +               break;
20405 +       case Opt_norefrof:
20406 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
20407 +               break;
20408 +
20409 +       case Opt_verbose:
20410 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
20411 +               break;
20412 +       case Opt_noverbose:
20413 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
20414 +               break;
20415 +
20416 +       case Opt_sum:
20417 +               au_opt_set(sbinfo->si_mntflags, SUM);
20418 +               break;
20419 +       case Opt_wsum:
20420 +               au_opt_clr(sbinfo->si_mntflags, SUM);
20421 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
20422 +       case Opt_nosum:
20423 +               au_opt_clr(sbinfo->si_mntflags, SUM);
20424 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
20425 +               break;
20426 +
20427 +       case Opt_wbr_create:
20428 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
20429 +               break;
20430 +       case Opt_wbr_copyup:
20431 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
20432 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
20433 +               break;
20434 +
20435 +       case Opt_dirwh:
20436 +               sbinfo->si_dirwh = opt->dirwh;
20437 +               break;
20438 +
20439 +       case Opt_rdcache:
20440 +               sbinfo->si_rdcache
20441 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
20442 +               break;
20443 +       case Opt_rdblk:
20444 +               sbinfo->si_rdblk = opt->rdblk;
20445 +               break;
20446 +       case Opt_rdblk_def:
20447 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
20448 +               break;
20449 +       case Opt_rdhash:
20450 +               sbinfo->si_rdhash = opt->rdhash;
20451 +               break;
20452 +       case Opt_rdhash_def:
20453 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
20454 +               break;
20455 +
20456 +       case Opt_shwh:
20457 +               au_opt_set(sbinfo->si_mntflags, SHWH);
20458 +               break;
20459 +       case Opt_noshwh:
20460 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
20461 +               break;
20462 +
20463 +       case Opt_trunc_xino:
20464 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
20465 +               break;
20466 +       case Opt_notrunc_xino:
20467 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
20468 +               break;
20469 +
20470 +       case Opt_trunc_xino_path:
20471 +       case Opt_itrunc_xino:
20472 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
20473 +               if (!err)
20474 +                       err = 1;
20475 +               break;
20476 +
20477 +       case Opt_trunc_xib:
20478 +               au_fset_opts(opts->flags, TRUNC_XIB);
20479 +               break;
20480 +       case Opt_notrunc_xib:
20481 +               au_fclr_opts(opts->flags, TRUNC_XIB);
20482 +               break;
20483 +
20484 +       default:
20485 +               err = 0;
20486 +               break;
20487 +       }
20488 +
20489 +       return err;
20490 +}
20491 +
20492 +/*
20493 + * returns tri-state.
20494 + * plus: processed without an error
20495 + * zero: unprocessed
20496 + * minus: error
20497 + */
20498 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
20499 +                    struct au_opts *opts)
20500 +{
20501 +       int err, do_refresh;
20502 +
20503 +       err = 0;
20504 +       switch (opt->type) {
20505 +       case Opt_append:
20506 +               opt->add.bindex = au_sbend(sb) + 1;
20507 +               if (opt->add.bindex < 0)
20508 +                       opt->add.bindex = 0;
20509 +               goto add;
20510 +       case Opt_prepend:
20511 +               opt->add.bindex = 0;
20512 +       add:
20513 +       case Opt_add:
20514 +               err = au_br_add(sb, &opt->add,
20515 +                               au_ftest_opts(opts->flags, REMOUNT));
20516 +               if (!err) {
20517 +                       err = 1;
20518 +                       au_fset_opts(opts->flags, REFRESH);
20519 +               }
20520 +               break;
20521 +
20522 +       case Opt_del:
20523 +       case Opt_idel:
20524 +               err = au_br_del(sb, &opt->del,
20525 +                               au_ftest_opts(opts->flags, REMOUNT));
20526 +               if (!err) {
20527 +                       err = 1;
20528 +                       au_fset_opts(opts->flags, TRUNC_XIB);
20529 +                       au_fset_opts(opts->flags, REFRESH);
20530 +               }
20531 +               break;
20532 +
20533 +       case Opt_mod:
20534 +       case Opt_imod:
20535 +               err = au_br_mod(sb, &opt->mod,
20536 +                               au_ftest_opts(opts->flags, REMOUNT),
20537 +                               &do_refresh);
20538 +               if (!err) {
20539 +                       err = 1;
20540 +                       if (do_refresh)
20541 +                               au_fset_opts(opts->flags, REFRESH);
20542 +               }
20543 +               break;
20544 +       }
20545 +
20546 +       return err;
20547 +}
20548 +
20549 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
20550 +                      struct au_opt_xino **opt_xino,
20551 +                      struct au_opts *opts)
20552 +{
20553 +       int err;
20554 +       aufs_bindex_t bend, bindex;
20555 +       struct dentry *root, *parent, *h_root;
20556 +
20557 +       err = 0;
20558 +       switch (opt->type) {
20559 +       case Opt_xino:
20560 +               err = au_xino_set(sb, &opt->xino,
20561 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
20562 +               if (unlikely(err))
20563 +                       break;
20564 +
20565 +               *opt_xino = &opt->xino;
20566 +               au_xino_brid_set(sb, -1);
20567 +
20568 +               /* safe d_parent access */
20569 +               parent = opt->xino.file->f_dentry->d_parent;
20570 +               root = sb->s_root;
20571 +               bend = au_sbend(sb);
20572 +               for (bindex = 0; bindex <= bend; bindex++) {
20573 +                       h_root = au_h_dptr(root, bindex);
20574 +                       if (h_root == parent) {
20575 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
20576 +                               break;
20577 +                       }
20578 +               }
20579 +               break;
20580 +
20581 +       case Opt_noxino:
20582 +               au_xino_clr(sb);
20583 +               au_xino_brid_set(sb, -1);
20584 +               *opt_xino = (void *)-1;
20585 +               break;
20586 +       }
20587 +
20588 +       return err;
20589 +}
20590 +
20591 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
20592 +                  unsigned int pending)
20593 +{
20594 +       int err;
20595 +       aufs_bindex_t bindex, bend;
20596 +       unsigned char do_plink, skip, do_free;
20597 +       struct au_branch *br;
20598 +       struct au_wbr *wbr;
20599 +       struct dentry *root;
20600 +       struct inode *dir, *h_dir;
20601 +       struct au_sbinfo *sbinfo;
20602 +       struct au_hinode *hdir;
20603 +
20604 +       SiMustAnyLock(sb);
20605 +
20606 +       sbinfo = au_sbi(sb);
20607 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
20608 +
20609 +       if (!(sb_flags & MS_RDONLY)) {
20610 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
20611 +                       pr_warning("first branch should be rw\n");
20612 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
20613 +                       pr_warning("shwh should be used with ro\n");
20614 +       }
20615 +
20616 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
20617 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
20618 +               pr_warning("udba=*notify requires xino\n");
20619 +
20620 +       err = 0;
20621 +       root = sb->s_root;
20622 +       dir = root->d_inode;
20623 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
20624 +       bend = au_sbend(sb);
20625 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
20626 +               skip = 0;
20627 +               h_dir = au_h_iptr(dir, bindex);
20628 +               br = au_sbr(sb, bindex);
20629 +               do_free = 0;
20630 +
20631 +               wbr = br->br_wbr;
20632 +               if (wbr)
20633 +                       wbr_wh_read_lock(wbr);
20634 +
20635 +               if (!au_br_writable(br->br_perm)) {
20636 +                       do_free = !!wbr;
20637 +                       skip = (!wbr
20638 +                               || (!wbr->wbr_whbase
20639 +                                   && !wbr->wbr_plink
20640 +                                   && !wbr->wbr_orph));
20641 +               } else if (!au_br_wh_linkable(br->br_perm)) {
20642 +                       /* skip = (!br->br_whbase && !br->br_orph); */
20643 +                       skip = (!wbr || !wbr->wbr_whbase);
20644 +                       if (skip && wbr) {
20645 +                               if (do_plink)
20646 +                                       skip = !!wbr->wbr_plink;
20647 +                               else
20648 +                                       skip = !wbr->wbr_plink;
20649 +                       }
20650 +               } else {
20651 +                       /* skip = (br->br_whbase && br->br_ohph); */
20652 +                       skip = (wbr && wbr->wbr_whbase);
20653 +                       if (skip) {
20654 +                               if (do_plink)
20655 +                                       skip = !!wbr->wbr_plink;
20656 +                               else
20657 +                                       skip = !wbr->wbr_plink;
20658 +                       }
20659 +               }
20660 +               if (wbr)
20661 +                       wbr_wh_read_unlock(wbr);
20662 +
20663 +               if (skip)
20664 +                       continue;
20665 +
20666 +               hdir = au_hi(dir, bindex);
20667 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
20668 +               if (wbr)
20669 +                       wbr_wh_write_lock(wbr);
20670 +               err = au_wh_init(au_h_dptr(root, bindex), br, sb);
20671 +               if (wbr)
20672 +                       wbr_wh_write_unlock(wbr);
20673 +               au_hn_imtx_unlock(hdir);
20674 +
20675 +               if (!err && do_free) {
20676 +                       kfree(wbr);
20677 +                       br->br_wbr = NULL;
20678 +               }
20679 +       }
20680 +
20681 +       return err;
20682 +}
20683 +
20684 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
20685 +{
20686 +       int err;
20687 +       unsigned int tmp;
20688 +       aufs_bindex_t bindex, bend;
20689 +       struct au_opt *opt;
20690 +       struct au_opt_xino *opt_xino, xino;
20691 +       struct au_sbinfo *sbinfo;
20692 +       struct au_branch *br;
20693 +
20694 +       SiMustWriteLock(sb);
20695 +
20696 +       err = 0;
20697 +       opt_xino = NULL;
20698 +       opt = opts->opt;
20699 +       while (err >= 0 && opt->type != Opt_tail)
20700 +               err = au_opt_simple(sb, opt++, opts);
20701 +       if (err > 0)
20702 +               err = 0;
20703 +       else if (unlikely(err < 0))
20704 +               goto out;
20705 +
20706 +       /* disable xino and udba temporary */
20707 +       sbinfo = au_sbi(sb);
20708 +       tmp = sbinfo->si_mntflags;
20709 +       au_opt_clr(sbinfo->si_mntflags, XINO);
20710 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
20711 +
20712 +       opt = opts->opt;
20713 +       while (err >= 0 && opt->type != Opt_tail)
20714 +               err = au_opt_br(sb, opt++, opts);
20715 +       if (err > 0)
20716 +               err = 0;
20717 +       else if (unlikely(err < 0))
20718 +               goto out;
20719 +
20720 +       bend = au_sbend(sb);
20721 +       if (unlikely(bend < 0)) {
20722 +               err = -EINVAL;
20723 +               pr_err("no branches\n");
20724 +               goto out;
20725 +       }
20726 +
20727 +       if (au_opt_test(tmp, XINO))
20728 +               au_opt_set(sbinfo->si_mntflags, XINO);
20729 +       opt = opts->opt;
20730 +       while (!err && opt->type != Opt_tail)
20731 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
20732 +       if (unlikely(err))
20733 +               goto out;
20734 +
20735 +       err = au_opts_verify(sb, sb->s_flags, tmp);
20736 +       if (unlikely(err))
20737 +               goto out;
20738 +
20739 +       /* restore xino */
20740 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
20741 +               xino.file = au_xino_def(sb);
20742 +               err = PTR_ERR(xino.file);
20743 +               if (IS_ERR(xino.file))
20744 +                       goto out;
20745 +
20746 +               err = au_xino_set(sb, &xino, /*remount*/0);
20747 +               fput(xino.file);
20748 +               if (unlikely(err))
20749 +                       goto out;
20750 +       }
20751 +
20752 +       /* restore udba */
20753 +       tmp &= AuOptMask_UDBA;
20754 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
20755 +       sbinfo->si_mntflags |= tmp;
20756 +       bend = au_sbend(sb);
20757 +       for (bindex = 0; bindex <= bend; bindex++) {
20758 +               br = au_sbr(sb, bindex);
20759 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
20760 +               if (unlikely(err))
20761 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
20762 +                               bindex, err);
20763 +               /* go on even if err */
20764 +       }
20765 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
20766 +               struct inode *dir = sb->s_root->d_inode;
20767 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
20768 +       }
20769 +
20770 +out:
20771 +       return err;
20772 +}
20773 +
20774 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
20775 +{
20776 +       int err, rerr;
20777 +       struct inode *dir;
20778 +       struct au_opt_xino *opt_xino;
20779 +       struct au_opt *opt;
20780 +       struct au_sbinfo *sbinfo;
20781 +
20782 +       SiMustWriteLock(sb);
20783 +
20784 +       dir = sb->s_root->d_inode;
20785 +       sbinfo = au_sbi(sb);
20786 +       err = 0;
20787 +       opt_xino = NULL;
20788 +       opt = opts->opt;
20789 +       while (err >= 0 && opt->type != Opt_tail) {
20790 +               err = au_opt_simple(sb, opt, opts);
20791 +               if (!err)
20792 +                       err = au_opt_br(sb, opt, opts);
20793 +               if (!err)
20794 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
20795 +               opt++;
20796 +       }
20797 +       if (err > 0)
20798 +               err = 0;
20799 +       AuTraceErr(err);
20800 +       /* go on even err */
20801 +
20802 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
20803 +       if (unlikely(rerr && !err))
20804 +               err = rerr;
20805 +
20806 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
20807 +               rerr = au_xib_trunc(sb);
20808 +               if (unlikely(rerr && !err))
20809 +                       err = rerr;
20810 +       }
20811 +
20812 +       /* will be handled by the caller */
20813 +       if (!au_ftest_opts(opts->flags, REFRESH)
20814 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
20815 +               au_fset_opts(opts->flags, REFRESH);
20816 +
20817 +       AuDbg("status 0x%x\n", opts->flags);
20818 +       return err;
20819 +}
20820 +
20821 +/* ---------------------------------------------------------------------- */
20822 +
20823 +unsigned int au_opt_udba(struct super_block *sb)
20824 +{
20825 +       return au_mntflags(sb) & AuOptMask_UDBA;
20826 +}
20827 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
20828 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
20829 +++ linux/fs/aufs/opts.h        2012-03-19 12:24:13.683134508 +0100
20830 @@ -0,0 +1,209 @@
20831 +/*
20832 + * Copyright (C) 2005-2012 Junjiro R. Okajima
20833 + *
20834 + * This program, aufs is free software; you can redistribute it and/or modify
20835 + * it under the terms of the GNU General Public License as published by
20836 + * the Free Software Foundation; either version 2 of the License, or
20837 + * (at your option) any later version.
20838 + *
20839 + * This program is distributed in the hope that it will be useful,
20840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20842 + * GNU General Public License for more details.
20843 + *
20844 + * You should have received a copy of the GNU General Public License
20845 + * along with this program; if not, write to the Free Software
20846 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20847 + */
20848 +
20849 +/*
20850 + * mount options/flags
20851 + */
20852 +
20853 +#ifndef __AUFS_OPTS_H__
20854 +#define __AUFS_OPTS_H__
20855 +
20856 +#ifdef __KERNEL__
20857 +
20858 +#include <linux/path.h>
20859 +
20860 +struct file;
20861 +struct super_block;
20862 +
20863 +/* ---------------------------------------------------------------------- */
20864 +
20865 +/* mount flags */
20866 +#define AuOpt_XINO             1               /* external inode number bitmap
20867 +                                                  and translation table */
20868 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
20869 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
20870 +#define AuOpt_UDBA_REVAL       (1 << 3)
20871 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
20872 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
20873 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
20874 +#define AuOpt_DIRPERM1         (1 << 7)        /* unimplemented */
20875 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
20876 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
20877 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
20878 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
20879 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
20880 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
20881 +#define AuOpt_DIO              (1 << 14)       /* direct io */
20882 +
20883 +#ifndef CONFIG_AUFS_HNOTIFY
20884 +#undef AuOpt_UDBA_HNOTIFY
20885 +#define AuOpt_UDBA_HNOTIFY     0
20886 +#endif
20887 +#ifndef CONFIG_AUFS_SHWH
20888 +#undef AuOpt_SHWH
20889 +#define AuOpt_SHWH             0
20890 +#endif
20891 +
20892 +#define AuOpt_Def      (AuOpt_XINO \
20893 +                        | AuOpt_UDBA_REVAL \
20894 +                        | AuOpt_PLINK \
20895 +                        /* | AuOpt_DIRPERM1 */ \
20896 +                        | AuOpt_WARN_PERM)
20897 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
20898 +                        | AuOpt_UDBA_REVAL \
20899 +                        | AuOpt_UDBA_HNOTIFY)
20900 +
20901 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
20902 +#define au_opt_set(flags, name) do { \
20903 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
20904 +       ((flags) |= AuOpt_##name); \
20905 +} while (0)
20906 +#define au_opt_set_udba(flags, name) do { \
20907 +       (flags) &= ~AuOptMask_UDBA; \
20908 +       ((flags) |= AuOpt_##name); \
20909 +} while (0)
20910 +#define au_opt_clr(flags, name) do { \
20911 +       ((flags) &= ~AuOpt_##name); \
20912 +} while (0)
20913 +
20914 +static inline unsigned int au_opts_plink(unsigned int mntflags)
20915 +{
20916 +#ifdef CONFIG_PROC_FS
20917 +       return mntflags;
20918 +#else
20919 +       return mntflags & ~AuOpt_PLINK;
20920 +#endif
20921 +}
20922 +
20923 +/* ---------------------------------------------------------------------- */
20924 +
20925 +/* policies to select one among multiple writable branches */
20926 +enum {
20927 +       AuWbrCreate_TDP,        /* top down parent */
20928 +       AuWbrCreate_RR,         /* round robin */
20929 +       AuWbrCreate_MFS,        /* most free space */
20930 +       AuWbrCreate_MFSV,       /* mfs with seconds */
20931 +       AuWbrCreate_MFSRR,      /* mfs then rr */
20932 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
20933 +       AuWbrCreate_PMFS,       /* parent and mfs */
20934 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
20935 +
20936 +       AuWbrCreate_Def = AuWbrCreate_TDP
20937 +};
20938 +
20939 +enum {
20940 +       AuWbrCopyup_TDP,        /* top down parent */
20941 +       AuWbrCopyup_BUP,        /* bottom up parent */
20942 +       AuWbrCopyup_BU,         /* bottom up */
20943 +
20944 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
20945 +};
20946 +
20947 +/* ---------------------------------------------------------------------- */
20948 +
20949 +struct au_opt_add {
20950 +       aufs_bindex_t   bindex;
20951 +       char            *pathname;
20952 +       int             perm;
20953 +       struct path     path;
20954 +};
20955 +
20956 +struct au_opt_del {
20957 +       char            *pathname;
20958 +       struct path     h_path;
20959 +};
20960 +
20961 +struct au_opt_mod {
20962 +       char            *path;
20963 +       int             perm;
20964 +       struct dentry   *h_root;
20965 +};
20966 +
20967 +struct au_opt_xino {
20968 +       char            *path;
20969 +       struct file     *file;
20970 +};
20971 +
20972 +struct au_opt_xino_itrunc {
20973 +       aufs_bindex_t   bindex;
20974 +};
20975 +
20976 +struct au_opt_wbr_create {
20977 +       int                     wbr_create;
20978 +       int                     mfs_second;
20979 +       unsigned long long      mfsrr_watermark;
20980 +};
20981 +
20982 +struct au_opt {
20983 +       int type;
20984 +       union {
20985 +               struct au_opt_xino      xino;
20986 +               struct au_opt_xino_itrunc xino_itrunc;
20987 +               struct au_opt_add       add;
20988 +               struct au_opt_del       del;
20989 +               struct au_opt_mod       mod;
20990 +               int                     dirwh;
20991 +               int                     rdcache;
20992 +               unsigned int            rdblk;
20993 +               unsigned int            rdhash;
20994 +               int                     udba;
20995 +               struct au_opt_wbr_create wbr_create;
20996 +               int                     wbr_copyup;
20997 +       };
20998 +};
20999 +
21000 +/* opts flags */
21001 +#define AuOpts_REMOUNT         1
21002 +#define AuOpts_REFRESH         (1 << 1)
21003 +#define AuOpts_TRUNC_XIB       (1 << 2)
21004 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
21005 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
21006 +#define au_fset_opts(flags, name) \
21007 +       do { (flags) |= AuOpts_##name; } while (0)
21008 +#define au_fclr_opts(flags, name) \
21009 +       do { (flags) &= ~AuOpts_##name; } while (0)
21010 +
21011 +struct au_opts {
21012 +       struct au_opt   *opt;
21013 +       int             max_opt;
21014 +
21015 +       unsigned int    given_udba;
21016 +       unsigned int    flags;
21017 +       unsigned long   sb_flags;
21018 +};
21019 +
21020 +/* ---------------------------------------------------------------------- */
21021 +
21022 +char *au_optstr_br_perm(int brperm);
21023 +const char *au_optstr_udba(int udba);
21024 +const char *au_optstr_wbr_copyup(int wbr_copyup);
21025 +const char *au_optstr_wbr_create(int wbr_create);
21026 +
21027 +void au_opts_free(struct au_opts *opts);
21028 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
21029 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
21030 +                  unsigned int pending);
21031 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
21032 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
21033 +
21034 +unsigned int au_opt_udba(struct super_block *sb);
21035 +
21036 +/* ---------------------------------------------------------------------- */
21037 +
21038 +#endif /* __KERNEL__ */
21039 +#endif /* __AUFS_OPTS_H__ */
21040 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
21041 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
21042 +++ linux/fs/aufs/plink.c       2012-03-19 12:24:13.683134508 +0100
21043 @@ -0,0 +1,515 @@
21044 +/*
21045 + * Copyright (C) 2005-2012 Junjiro R. Okajima
21046 + *
21047 + * This program, aufs is free software; you can redistribute it and/or modify
21048 + * it under the terms of the GNU General Public License as published by
21049 + * the Free Software Foundation; either version 2 of the License, or
21050 + * (at your option) any later version.
21051 + *
21052 + * This program is distributed in the hope that it will be useful,
21053 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21054 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21055 + * GNU General Public License for more details.
21056 + *
21057 + * You should have received a copy of the GNU General Public License
21058 + * along with this program; if not, write to the Free Software
21059 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21060 + */
21061 +
21062 +/*
21063 + * pseudo-link
21064 + */
21065 +
21066 +#include "aufs.h"
21067 +
21068 +/*
21069 + * the pseudo-link maintenance mode.
21070 + * during a user process maintains the pseudo-links,
21071 + * prohibit adding a new plink and branch manipulation.
21072 + *
21073 + * Flags
21074 + * NOPLM:
21075 + *     For entry functions which will handle plink, and i_mutex is already held
21076 + *     in VFS.
21077 + *     They cannot wait and should return an error at once.
21078 + *     Callers has to check the error.
21079 + * NOPLMW:
21080 + *     For entry functions which will handle plink, but i_mutex is not held
21081 + *     in VFS.
21082 + *     They can wait the plink maintenance mode to finish.
21083 + *
21084 + * They behave like F_SETLK and F_SETLKW.
21085 + * If the caller never handle plink, then both flags are unnecessary.
21086 + */
21087 +
21088 +int au_plink_maint(struct super_block *sb, int flags)
21089 +{
21090 +       int err;
21091 +       pid_t pid, ppid;
21092 +       struct au_sbinfo *sbi;
21093 +
21094 +       SiMustAnyLock(sb);
21095 +
21096 +       err = 0;
21097 +       if (!au_opt_test(au_mntflags(sb), PLINK))
21098 +               goto out;
21099 +
21100 +       sbi = au_sbi(sb);
21101 +       pid = sbi->si_plink_maint_pid;
21102 +       if (!pid || pid == current->pid)
21103 +               goto out;
21104 +
21105 +       /* todo: it highly depends upon /sbin/mount.aufs */
21106 +       rcu_read_lock();
21107 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
21108 +       rcu_read_unlock();
21109 +       if (pid == ppid)
21110 +               goto out;
21111 +
21112 +       if (au_ftest_lock(flags, NOPLMW)) {
21113 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
21114 +               /* AuDebugOn(!lockdep_depth(current)); */
21115 +               while (sbi->si_plink_maint_pid) {
21116 +                       si_read_unlock(sb);
21117 +                       /* gave up wake_up_bit() */
21118 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
21119 +
21120 +                       if (au_ftest_lock(flags, FLUSH))
21121 +                               au_nwt_flush(&sbi->si_nowait);
21122 +                       si_noflush_read_lock(sb);
21123 +               }
21124 +       } else if (au_ftest_lock(flags, NOPLM)) {
21125 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
21126 +               err = -EAGAIN;
21127 +       }
21128 +
21129 +out:
21130 +       return err;
21131 +}
21132 +
21133 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
21134 +{
21135 +       spin_lock(&sbinfo->si_plink_maint_lock);
21136 +       sbinfo->si_plink_maint_pid = 0;
21137 +       spin_unlock(&sbinfo->si_plink_maint_lock);
21138 +       wake_up_all(&sbinfo->si_plink_wq);
21139 +}
21140 +
21141 +int au_plink_maint_enter(struct super_block *sb)
21142 +{
21143 +       int err;
21144 +       struct au_sbinfo *sbinfo;
21145 +
21146 +       err = 0;
21147 +       sbinfo = au_sbi(sb);
21148 +       /* make sure i am the only one in this fs */
21149 +       si_write_lock(sb, AuLock_FLUSH);
21150 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21151 +               spin_lock(&sbinfo->si_plink_maint_lock);
21152 +               if (!sbinfo->si_plink_maint_pid)
21153 +                       sbinfo->si_plink_maint_pid = current->pid;
21154 +               else
21155 +                       err = -EBUSY;
21156 +               spin_unlock(&sbinfo->si_plink_maint_lock);
21157 +       }
21158 +       si_write_unlock(sb);
21159 +
21160 +       return err;
21161 +}
21162 +
21163 +/* ---------------------------------------------------------------------- */
21164 +
21165 +struct pseudo_link {
21166 +       union {
21167 +               struct list_head list;
21168 +               struct rcu_head rcu;
21169 +       };
21170 +       struct inode *inode;
21171 +};
21172 +
21173 +#ifdef CONFIG_AUFS_DEBUG
21174 +void au_plink_list(struct super_block *sb)
21175 +{
21176 +       struct au_sbinfo *sbinfo;
21177 +       struct list_head *plink_list;
21178 +       struct pseudo_link *plink;
21179 +
21180 +       SiMustAnyLock(sb);
21181 +
21182 +       sbinfo = au_sbi(sb);
21183 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21184 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21185 +
21186 +       plink_list = &sbinfo->si_plink.head;
21187 +       rcu_read_lock();
21188 +       list_for_each_entry_rcu(plink, plink_list, list)
21189 +               AuDbg("%lu\n", plink->inode->i_ino);
21190 +       rcu_read_unlock();
21191 +}
21192 +#endif
21193 +
21194 +/* is the inode pseudo-linked? */
21195 +int au_plink_test(struct inode *inode)
21196 +{
21197 +       int found;
21198 +       struct au_sbinfo *sbinfo;
21199 +       struct list_head *plink_list;
21200 +       struct pseudo_link *plink;
21201 +
21202 +       sbinfo = au_sbi(inode->i_sb);
21203 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
21204 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
21205 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
21206 +
21207 +       found = 0;
21208 +       plink_list = &sbinfo->si_plink.head;
21209 +       rcu_read_lock();
21210 +       list_for_each_entry_rcu(plink, plink_list, list)
21211 +               if (plink->inode == inode) {
21212 +                       found = 1;
21213 +                       break;
21214 +               }
21215 +       rcu_read_unlock();
21216 +       return found;
21217 +}
21218 +
21219 +/* ---------------------------------------------------------------------- */
21220 +
21221 +/*
21222 + * generate a name for plink.
21223 + * the file will be stored under AUFS_WH_PLINKDIR.
21224 + */
21225 +/* 20 is max digits length of ulong 64 */
21226 +#define PLINK_NAME_LEN ((20 + 1) * 2)
21227 +
21228 +static int plink_name(char *name, int len, struct inode *inode,
21229 +                     aufs_bindex_t bindex)
21230 +{
21231 +       int rlen;
21232 +       struct inode *h_inode;
21233 +
21234 +       h_inode = au_h_iptr(inode, bindex);
21235 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
21236 +       return rlen;
21237 +}
21238 +
21239 +struct au_do_plink_lkup_args {
21240 +       struct dentry **errp;
21241 +       struct qstr *tgtname;
21242 +       struct dentry *h_parent;
21243 +       struct au_branch *br;
21244 +};
21245 +
21246 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
21247 +                                      struct dentry *h_parent,
21248 +                                      struct au_branch *br)
21249 +{
21250 +       struct dentry *h_dentry;
21251 +       struct mutex *h_mtx;
21252 +
21253 +       h_mtx = &h_parent->d_inode->i_mutex;
21254 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
21255 +       h_dentry = au_lkup_one(tgtname, h_parent, br, /*nd*/NULL);
21256 +       mutex_unlock(h_mtx);
21257 +       return h_dentry;
21258 +}
21259 +
21260 +static void au_call_do_plink_lkup(void *args)
21261 +{
21262 +       struct au_do_plink_lkup_args *a = args;
21263 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
21264 +}
21265 +
21266 +/* lookup the plink-ed @inode under the branch at @bindex */
21267 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
21268 +{
21269 +       struct dentry *h_dentry, *h_parent;
21270 +       struct au_branch *br;
21271 +       struct inode *h_dir;
21272 +       int wkq_err;
21273 +       char a[PLINK_NAME_LEN];
21274 +       struct qstr tgtname = {
21275 +               .name   = a
21276 +       };
21277 +
21278 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
21279 +
21280 +       br = au_sbr(inode->i_sb, bindex);
21281 +       h_parent = br->br_wbr->wbr_plink;
21282 +       h_dir = h_parent->d_inode;
21283 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
21284 +
21285 +       if (current_fsuid()) {
21286 +               struct au_do_plink_lkup_args args = {
21287 +                       .errp           = &h_dentry,
21288 +                       .tgtname        = &tgtname,
21289 +                       .h_parent       = h_parent,
21290 +                       .br             = br
21291 +               };
21292 +
21293 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
21294 +               if (unlikely(wkq_err))
21295 +                       h_dentry = ERR_PTR(wkq_err);
21296 +       } else
21297 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
21298 +
21299 +       return h_dentry;
21300 +}
21301 +
21302 +/* create a pseudo-link */
21303 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
21304 +                     struct dentry *h_dentry, struct au_branch *br)
21305 +{
21306 +       int err;
21307 +       struct path h_path = {
21308 +               .mnt = br->br_mnt
21309 +       };
21310 +       struct inode *h_dir;
21311 +
21312 +       h_dir = h_parent->d_inode;
21313 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
21314 +again:
21315 +       h_path.dentry = au_lkup_one(tgt, h_parent, br, /*nd*/NULL);
21316 +       err = PTR_ERR(h_path.dentry);
21317 +       if (IS_ERR(h_path.dentry))
21318 +               goto out;
21319 +
21320 +       err = 0;
21321 +       /* wh.plink dir is not monitored */
21322 +       /* todo: is it really safe? */
21323 +       if (h_path.dentry->d_inode
21324 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
21325 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
21326 +               dput(h_path.dentry);
21327 +               h_path.dentry = NULL;
21328 +               if (!err)
21329 +                       goto again;
21330 +       }
21331 +       if (!err && !h_path.dentry->d_inode)
21332 +               err = vfsub_link(h_dentry, h_dir, &h_path);
21333 +       dput(h_path.dentry);
21334 +
21335 +out:
21336 +       mutex_unlock(&h_dir->i_mutex);
21337 +       return err;
21338 +}
21339 +
21340 +struct do_whplink_args {
21341 +       int *errp;
21342 +       struct qstr *tgt;
21343 +       struct dentry *h_parent;
21344 +       struct dentry *h_dentry;
21345 +       struct au_branch *br;
21346 +};
21347 +
21348 +static void call_do_whplink(void *args)
21349 +{
21350 +       struct do_whplink_args *a = args;
21351 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
21352 +}
21353 +
21354 +static int whplink(struct dentry *h_dentry, struct inode *inode,
21355 +                  aufs_bindex_t bindex, struct au_branch *br)
21356 +{
21357 +       int err, wkq_err;
21358 +       struct au_wbr *wbr;
21359 +       struct dentry *h_parent;
21360 +       struct inode *h_dir;
21361 +       char a[PLINK_NAME_LEN];
21362 +       struct qstr tgtname = {
21363 +               .name = a
21364 +       };
21365 +
21366 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
21367 +       h_parent = wbr->wbr_plink;
21368 +       h_dir = h_parent->d_inode;
21369 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
21370 +
21371 +       /* always superio. */
21372 +       if (current_fsuid()) {
21373 +               struct do_whplink_args args = {
21374 +                       .errp           = &err,
21375 +                       .tgt            = &tgtname,
21376 +                       .h_parent       = h_parent,
21377 +                       .h_dentry       = h_dentry,
21378 +                       .br             = br
21379 +               };
21380 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
21381 +               if (unlikely(wkq_err))
21382 +                       err = wkq_err;
21383 +       } else
21384 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
21385 +
21386 +       return err;
21387 +}
21388 +
21389 +/* free a single plink */
21390 +static void do_put_plink(struct pseudo_link *plink, int do_del)
21391 +{
21392 +       if (do_del)
21393 +               list_del(&plink->list);
21394 +       iput(plink->inode);
21395 +       kfree(plink);
21396 +}
21397 +
21398 +static void do_put_plink_rcu(struct rcu_head *rcu)
21399 +{
21400 +       struct pseudo_link *plink;
21401 +
21402 +       plink = container_of(rcu, struct pseudo_link, rcu);
21403 +       iput(plink->inode);
21404 +       kfree(plink);
21405 +}
21406 +
21407 +/*
21408 + * create a new pseudo-link for @h_dentry on @bindex.
21409 + * the linked inode is held in aufs @inode.
21410 + */
21411 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
21412 +                    struct dentry *h_dentry)
21413 +{
21414 +       struct super_block *sb;
21415 +       struct au_sbinfo *sbinfo;
21416 +       struct list_head *plink_list;
21417 +       struct pseudo_link *plink, *tmp;
21418 +       int found, err, cnt;
21419 +
21420 +       sb = inode->i_sb;
21421 +       sbinfo = au_sbi(sb);
21422 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21423 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21424 +
21425 +       cnt = 0;
21426 +       found = 0;
21427 +       plink_list = &sbinfo->si_plink.head;
21428 +       rcu_read_lock();
21429 +       list_for_each_entry_rcu(plink, plink_list, list) {
21430 +               cnt++;
21431 +               if (plink->inode == inode) {
21432 +                       found = 1;
21433 +                       break;
21434 +               }
21435 +       }
21436 +       rcu_read_unlock();
21437 +       if (found)
21438 +               return;
21439 +
21440 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
21441 +       if (tmp)
21442 +               tmp->inode = au_igrab(inode);
21443 +       else {
21444 +               err = -ENOMEM;
21445 +               goto out;
21446 +       }
21447 +
21448 +       spin_lock(&sbinfo->si_plink.spin);
21449 +       list_for_each_entry(plink, plink_list, list) {
21450 +               if (plink->inode == inode) {
21451 +                       found = 1;
21452 +                       break;
21453 +               }
21454 +       }
21455 +       if (!found)
21456 +               list_add_rcu(&tmp->list, plink_list);
21457 +       spin_unlock(&sbinfo->si_plink.spin);
21458 +       if (!found) {
21459 +               cnt++;
21460 +               WARN_ONCE(cnt > AUFS_PLINK_WARN,
21461 +                         "unexpectedly many pseudo links, %d\n", cnt);
21462 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
21463 +       } else {
21464 +               do_put_plink(tmp, 0);
21465 +               return;
21466 +       }
21467 +
21468 +out:
21469 +       if (unlikely(err)) {
21470 +               pr_warning("err %d, damaged pseudo link.\n", err);
21471 +               if (tmp) {
21472 +                       au_spl_del_rcu(&tmp->list, &sbinfo->si_plink);
21473 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
21474 +               }
21475 +       }
21476 +}
21477 +
21478 +/* free all plinks */
21479 +void au_plink_put(struct super_block *sb, int verbose)
21480 +{
21481 +       struct au_sbinfo *sbinfo;
21482 +       struct list_head *plink_list;
21483 +       struct pseudo_link *plink, *tmp;
21484 +
21485 +       SiMustWriteLock(sb);
21486 +
21487 +       sbinfo = au_sbi(sb);
21488 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21489 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21490 +
21491 +       plink_list = &sbinfo->si_plink.head;
21492 +       /* no spin_lock since sbinfo is write-locked */
21493 +       WARN(verbose && !list_empty(plink_list), "pseudo-link is not flushed");
21494 +       list_for_each_entry_safe(plink, tmp, plink_list, list)
21495 +               do_put_plink(plink, 0);
21496 +       INIT_LIST_HEAD(plink_list);
21497 +}
21498 +
21499 +void au_plink_clean(struct super_block *sb, int verbose)
21500 +{
21501 +       struct dentry *root;
21502 +
21503 +       root = sb->s_root;
21504 +       aufs_write_lock(root);
21505 +       if (au_opt_test(au_mntflags(sb), PLINK))
21506 +               au_plink_put(sb, verbose);
21507 +       aufs_write_unlock(root);
21508 +}
21509 +
21510 +/* free the plinks on a branch specified by @br_id */
21511 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
21512 +{
21513 +       struct au_sbinfo *sbinfo;
21514 +       struct list_head *plink_list;
21515 +       struct pseudo_link *plink, *tmp;
21516 +       struct inode *inode;
21517 +       aufs_bindex_t bstart, bend, bindex;
21518 +       unsigned char do_put;
21519 +
21520 +       SiMustWriteLock(sb);
21521 +
21522 +       sbinfo = au_sbi(sb);
21523 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
21524 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
21525 +
21526 +       plink_list = &sbinfo->si_plink.head;
21527 +       /* no spin_lock since sbinfo is write-locked */
21528 +       list_for_each_entry_safe(plink, tmp, plink_list, list) {
21529 +               do_put = 0;
21530 +               inode = au_igrab(plink->inode);
21531 +               ii_write_lock_child(inode);
21532 +               bstart = au_ibstart(inode);
21533 +               bend = au_ibend(inode);
21534 +               if (bstart >= 0) {
21535 +                       for (bindex = bstart; bindex <= bend; bindex++) {
21536 +                               if (!au_h_iptr(inode, bindex)
21537 +                                   || au_ii_br_id(inode, bindex) != br_id)
21538 +                                       continue;
21539 +                               au_set_h_iptr(inode, bindex, NULL, 0);
21540 +                               do_put = 1;
21541 +                               break;
21542 +                       }
21543 +               } else
21544 +                       do_put_plink(plink, 1);
21545 +
21546 +               if (do_put) {
21547 +                       for (bindex = bstart; bindex <= bend; bindex++)
21548 +                               if (au_h_iptr(inode, bindex)) {
21549 +                                       do_put = 0;
21550 +                                       break;
21551 +                               }
21552 +                       if (do_put)
21553 +                               do_put_plink(plink, 1);
21554 +               }
21555 +               ii_write_unlock(inode);
21556 +               iput(inode);
21557 +       }
21558 +}
21559 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
21560 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
21561 +++ linux/fs/aufs/poll.c        2012-03-19 12:24:13.683134508 +0100
21562 @@ -0,0 +1,56 @@
21563 +/*
21564 + * Copyright (C) 2005-2012 Junjiro R. Okajima
21565 + *
21566 + * This program, aufs is free software; you can redistribute it and/or modify
21567 + * it under the terms of the GNU General Public License as published by
21568 + * the Free Software Foundation; either version 2 of the License, or
21569 + * (at your option) any later version.
21570 + *
21571 + * This program is distributed in the hope that it will be useful,
21572 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21573 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21574 + * GNU General Public License for more details.
21575 + *
21576 + * You should have received a copy of the GNU General Public License
21577 + * along with this program; if not, write to the Free Software
21578 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21579 + */
21580 +
21581 +/*
21582 + * poll operation
21583 + * There is only one filesystem which implements ->poll operation, currently.
21584 + */
21585 +
21586 +#include "aufs.h"
21587 +
21588 +unsigned int aufs_poll(struct file *file, poll_table *wait)
21589 +{
21590 +       unsigned int mask;
21591 +       int err;
21592 +       struct file *h_file;
21593 +       struct dentry *dentry;
21594 +       struct super_block *sb;
21595 +
21596 +       /* We should pretend an error happened. */
21597 +       mask = POLLERR /* | POLLIN | POLLOUT */;
21598 +       dentry = file->f_dentry;
21599 +       sb = dentry->d_sb;
21600 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
21601 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
21602 +       if (unlikely(err))
21603 +               goto out;
21604 +
21605 +       /* it is not an error if h_file has no operation */
21606 +       mask = DEFAULT_POLLMASK;
21607 +       h_file = au_hf_top(file);
21608 +       if (h_file->f_op && h_file->f_op->poll)
21609 +               mask = h_file->f_op->poll(h_file, wait);
21610 +
21611 +       di_read_unlock(dentry, AuLock_IR);
21612 +       fi_read_unlock(file);
21613 +
21614 +out:
21615 +       si_read_unlock(sb);
21616 +       AuTraceErr((int)mask);
21617 +       return mask;
21618 +}
21619 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
21620 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
21621 +++ linux/fs/aufs/procfs.c      2012-03-19 12:24:13.683134508 +0100
21622 @@ -0,0 +1,170 @@
21623 +/*
21624 + * Copyright (C) 2010-2012 Junjiro R. Okajima
21625 + *
21626 + * This program, aufs is free software; you can redistribute it and/or modify
21627 + * it under the terms of the GNU General Public License as published by
21628 + * the Free Software Foundation; either version 2 of the License, or
21629 + * (at your option) any later version.
21630 + *
21631 + * This program is distributed in the hope that it will be useful,
21632 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21633 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21634 + * GNU General Public License for more details.
21635 + *
21636 + * You should have received a copy of the GNU General Public License
21637 + * along with this program; if not, write to the Free Software
21638 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21639 + */
21640 +
21641 +/*
21642 + * procfs interfaces
21643 + */
21644 +
21645 +#include <linux/proc_fs.h>
21646 +#include "aufs.h"
21647 +
21648 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
21649 +{
21650 +       struct au_sbinfo *sbinfo;
21651 +
21652 +       sbinfo = file->private_data;
21653 +       if (sbinfo) {
21654 +               au_plink_maint_leave(sbinfo);
21655 +               kobject_put(&sbinfo->si_kobj);
21656 +       }
21657 +
21658 +       return 0;
21659 +}
21660 +
21661 +static void au_procfs_plm_write_clean(struct file *file)
21662 +{
21663 +       struct au_sbinfo *sbinfo;
21664 +
21665 +       sbinfo = file->private_data;
21666 +       if (sbinfo)
21667 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
21668 +}
21669 +
21670 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
21671 +{
21672 +       int err;
21673 +       struct super_block *sb;
21674 +       struct au_sbinfo *sbinfo;
21675 +
21676 +       err = -EBUSY;
21677 +       if (unlikely(file->private_data))
21678 +               goto out;
21679 +
21680 +       sb = NULL;
21681 +       /* don't use au_sbilist_lock() here */
21682 +       spin_lock(&au_sbilist.spin);
21683 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
21684 +               if (id == sysaufs_si_id(sbinfo)) {
21685 +                       kobject_get(&sbinfo->si_kobj);
21686 +                       sb = sbinfo->si_sb;
21687 +                       break;
21688 +               }
21689 +       spin_unlock(&au_sbilist.spin);
21690 +
21691 +       err = -EINVAL;
21692 +       if (unlikely(!sb))
21693 +               goto out;
21694 +
21695 +       err = au_plink_maint_enter(sb);
21696 +       if (!err)
21697 +               /* keep kobject_get() */
21698 +               file->private_data = sbinfo;
21699 +       else
21700 +               kobject_put(&sbinfo->si_kobj);
21701 +out:
21702 +       return err;
21703 +}
21704 +
21705 +/*
21706 + * Accept a valid "si=xxxx" only.
21707 + * Once it is accepted successfully, accept "clean" too.
21708 + */
21709 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
21710 +                                  size_t count, loff_t *ppos)
21711 +{
21712 +       ssize_t err;
21713 +       unsigned long id;
21714 +       /* last newline is allowed */
21715 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
21716 +
21717 +       err = -EACCES;
21718 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
21719 +               goto out;
21720 +
21721 +       err = -EINVAL;
21722 +       if (unlikely(count > sizeof(buf)))
21723 +               goto out;
21724 +
21725 +       err = copy_from_user(buf, ubuf, count);
21726 +       if (unlikely(err)) {
21727 +               err = -EFAULT;
21728 +               goto out;
21729 +       }
21730 +       buf[count] = 0;
21731 +
21732 +       err = -EINVAL;
21733 +       if (!strcmp("clean", buf)) {
21734 +               au_procfs_plm_write_clean(file);
21735 +               goto out_success;
21736 +       } else if (unlikely(strncmp("si=", buf, 3)))
21737 +               goto out;
21738 +
21739 +       err = kstrtoul(buf + 3, 16, &id);
21740 +       if (unlikely(err))
21741 +               goto out;
21742 +
21743 +       err = au_procfs_plm_write_si(file, id);
21744 +       if (unlikely(err))
21745 +               goto out;
21746 +
21747 +out_success:
21748 +       err = count; /* success */
21749 +out:
21750 +       return err;
21751 +}
21752 +
21753 +static const struct file_operations au_procfs_plm_fop = {
21754 +       .write          = au_procfs_plm_write,
21755 +       .release        = au_procfs_plm_release,
21756 +       .owner          = THIS_MODULE
21757 +};
21758 +
21759 +/* ---------------------------------------------------------------------- */
21760 +
21761 +static struct proc_dir_entry *au_procfs_dir;
21762 +
21763 +void au_procfs_fin(void)
21764 +{
21765 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
21766 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
21767 +}
21768 +
21769 +int __init au_procfs_init(void)
21770 +{
21771 +       int err;
21772 +       struct proc_dir_entry *entry;
21773 +
21774 +       err = -ENOMEM;
21775 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
21776 +       if (unlikely(!au_procfs_dir))
21777 +               goto out;
21778 +
21779 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
21780 +                           au_procfs_dir, &au_procfs_plm_fop);
21781 +       if (unlikely(!entry))
21782 +               goto out_dir;
21783 +
21784 +       err = 0;
21785 +       goto out; /* success */
21786 +
21787 +
21788 +out_dir:
21789 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
21790 +out:
21791 +       return err;
21792 +}
21793 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
21794 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
21795 +++ linux/fs/aufs/rdu.c 2012-03-19 12:24:13.683134508 +0100
21796 @@ -0,0 +1,383 @@
21797 +/*
21798 + * Copyright (C) 2005-2012 Junjiro R. Okajima
21799 + *
21800 + * This program, aufs is free software; you can redistribute it and/or modify
21801 + * it under the terms of the GNU General Public License as published by
21802 + * the Free Software Foundation; either version 2 of the License, or
21803 + * (at your option) any later version.
21804 + *
21805 + * This program is distributed in the hope that it will be useful,
21806 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21807 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21808 + * GNU General Public License for more details.
21809 + *
21810 + * You should have received a copy of the GNU General Public License
21811 + * along with this program; if not, write to the Free Software
21812 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21813 + */
21814 +
21815 +/*
21816 + * readdir in userspace.
21817 + */
21818 +
21819 +#include <linux/compat.h>
21820 +#include <linux/fs_stack.h>
21821 +#include <linux/security.h>
21822 +#include "aufs.h"
21823 +
21824 +/* bits for struct aufs_rdu.flags */
21825 +#define        AuRdu_CALLED    1
21826 +#define        AuRdu_CONT      (1 << 1)
21827 +#define        AuRdu_FULL      (1 << 2)
21828 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
21829 +#define au_fset_rdu(flags, name) \
21830 +       do { (flags) |= AuRdu_##name; } while (0)
21831 +#define au_fclr_rdu(flags, name) \
21832 +       do { (flags) &= ~AuRdu_##name; } while (0)
21833 +
21834 +struct au_rdu_arg {
21835 +       struct aufs_rdu                 *rdu;
21836 +       union au_rdu_ent_ul             ent;
21837 +       unsigned long                   end;
21838 +
21839 +       struct super_block              *sb;
21840 +       int                             err;
21841 +};
21842 +
21843 +static int au_rdu_fill(void *__arg, const char *name, int nlen,
21844 +                      loff_t offset, u64 h_ino, unsigned int d_type)
21845 +{
21846 +       int err, len;
21847 +       struct au_rdu_arg *arg = __arg;
21848 +       struct aufs_rdu *rdu = arg->rdu;
21849 +       struct au_rdu_ent ent;
21850 +
21851 +       err = 0;
21852 +       arg->err = 0;
21853 +       au_fset_rdu(rdu->cookie.flags, CALLED);
21854 +       len = au_rdu_len(nlen);
21855 +       if (arg->ent.ul + len  < arg->end) {
21856 +               ent.ino = h_ino;
21857 +               ent.bindex = rdu->cookie.bindex;
21858 +               ent.type = d_type;
21859 +               ent.nlen = nlen;
21860 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
21861 +                       ent.type = DT_UNKNOWN;
21862 +
21863 +               /* unnecessary to support mmap_sem since this is a dir */
21864 +               err = -EFAULT;
21865 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
21866 +                       goto out;
21867 +               if (copy_to_user(arg->ent.e->name, name, nlen))
21868 +                       goto out;
21869 +               /* the terminating NULL */
21870 +               if (__put_user(0, arg->ent.e->name + nlen))
21871 +                       goto out;
21872 +               err = 0;
21873 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
21874 +               arg->ent.ul += len;
21875 +               rdu->rent++;
21876 +       } else {
21877 +               err = -EFAULT;
21878 +               au_fset_rdu(rdu->cookie.flags, FULL);
21879 +               rdu->full = 1;
21880 +               rdu->tail = arg->ent;
21881 +       }
21882 +
21883 +out:
21884 +       /* AuTraceErr(err); */
21885 +       return err;
21886 +}
21887 +
21888 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
21889 +{
21890 +       int err;
21891 +       loff_t offset;
21892 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
21893 +
21894 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
21895 +       err = offset;
21896 +       if (unlikely(offset != cookie->h_pos))
21897 +               goto out;
21898 +
21899 +       err = 0;
21900 +       do {
21901 +               arg->err = 0;
21902 +               au_fclr_rdu(cookie->flags, CALLED);
21903 +               /* smp_mb(); */
21904 +               err = vfsub_readdir(h_file, au_rdu_fill, arg);
21905 +               if (err >= 0)
21906 +                       err = arg->err;
21907 +       } while (!err
21908 +                && au_ftest_rdu(cookie->flags, CALLED)
21909 +                && !au_ftest_rdu(cookie->flags, FULL));
21910 +       cookie->h_pos = h_file->f_pos;
21911 +
21912 +out:
21913 +       AuTraceErr(err);
21914 +       return err;
21915 +}
21916 +
21917 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
21918 +{
21919 +       int err;
21920 +       aufs_bindex_t bend;
21921 +       struct au_rdu_arg arg;
21922 +       struct dentry *dentry;
21923 +       struct inode *inode;
21924 +       struct file *h_file;
21925 +       struct au_rdu_cookie *cookie = &rdu->cookie;
21926 +
21927 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
21928 +       if (unlikely(err)) {
21929 +               err = -EFAULT;
21930 +               AuTraceErr(err);
21931 +               goto out;
21932 +       }
21933 +       rdu->rent = 0;
21934 +       rdu->tail = rdu->ent;
21935 +       rdu->full = 0;
21936 +       arg.rdu = rdu;
21937 +       arg.ent = rdu->ent;
21938 +       arg.end = arg.ent.ul;
21939 +       arg.end += rdu->sz;
21940 +
21941 +       err = -ENOTDIR;
21942 +       if (unlikely(!file->f_op || !file->f_op->readdir))
21943 +               goto out;
21944 +
21945 +       err = security_file_permission(file, MAY_READ);
21946 +       AuTraceErr(err);
21947 +       if (unlikely(err))
21948 +               goto out;
21949 +
21950 +       dentry = file->f_dentry;
21951 +       inode = dentry->d_inode;
21952 +#if 1
21953 +       mutex_lock(&inode->i_mutex);
21954 +#else
21955 +       err = mutex_lock_killable(&inode->i_mutex);
21956 +       AuTraceErr(err);
21957 +       if (unlikely(err))
21958 +               goto out;
21959 +#endif
21960 +
21961 +       arg.sb = inode->i_sb;
21962 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
21963 +       if (unlikely(err))
21964 +               goto out_mtx;
21965 +       err = au_alive_dir(dentry);
21966 +       if (unlikely(err))
21967 +               goto out_si;
21968 +       /* todo: reval? */
21969 +       fi_read_lock(file);
21970 +
21971 +       err = -EAGAIN;
21972 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
21973 +                    && cookie->generation != au_figen(file)))
21974 +               goto out_unlock;
21975 +
21976 +       err = 0;
21977 +       if (!rdu->blk) {
21978 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
21979 +               if (!rdu->blk)
21980 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
21981 +       }
21982 +       bend = au_fbstart(file);
21983 +       if (cookie->bindex < bend)
21984 +               cookie->bindex = bend;
21985 +       bend = au_fbend_dir(file);
21986 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
21987 +       for (; !err && cookie->bindex <= bend;
21988 +            cookie->bindex++, cookie->h_pos = 0) {
21989 +               h_file = au_hf_dir(file, cookie->bindex);
21990 +               if (!h_file)
21991 +                       continue;
21992 +
21993 +               au_fclr_rdu(cookie->flags, FULL);
21994 +               err = au_rdu_do(h_file, &arg);
21995 +               AuTraceErr(err);
21996 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
21997 +                       break;
21998 +       }
21999 +       AuDbg("rent %llu\n", rdu->rent);
22000 +
22001 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
22002 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
22003 +               au_fset_rdu(cookie->flags, CONT);
22004 +               cookie->generation = au_figen(file);
22005 +       }
22006 +
22007 +       ii_read_lock_child(inode);
22008 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
22009 +       ii_read_unlock(inode);
22010 +
22011 +out_unlock:
22012 +       fi_read_unlock(file);
22013 +out_si:
22014 +       si_read_unlock(arg.sb);
22015 +out_mtx:
22016 +       mutex_unlock(&inode->i_mutex);
22017 +out:
22018 +       AuTraceErr(err);
22019 +       return err;
22020 +}
22021 +
22022 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
22023 +{
22024 +       int err;
22025 +       ino_t ino;
22026 +       unsigned long long nent;
22027 +       union au_rdu_ent_ul *u;
22028 +       struct au_rdu_ent ent;
22029 +       struct super_block *sb;
22030 +
22031 +       err = 0;
22032 +       nent = rdu->nent;
22033 +       u = &rdu->ent;
22034 +       sb = file->f_dentry->d_sb;
22035 +       si_read_lock(sb, AuLock_FLUSH);
22036 +       while (nent-- > 0) {
22037 +               /* unnecessary to support mmap_sem since this is a dir */
22038 +               err = copy_from_user(&ent, u->e, sizeof(ent));
22039 +               if (!err)
22040 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
22041 +               if (unlikely(err)) {
22042 +                       err = -EFAULT;
22043 +                       AuTraceErr(err);
22044 +                       break;
22045 +               }
22046 +
22047 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
22048 +               if (!ent.wh)
22049 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
22050 +               else
22051 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
22052 +                                       &ino);
22053 +               if (unlikely(err)) {
22054 +                       AuTraceErr(err);
22055 +                       break;
22056 +               }
22057 +
22058 +               err = __put_user(ino, &u->e->ino);
22059 +               if (unlikely(err)) {
22060 +                       err = -EFAULT;
22061 +                       AuTraceErr(err);
22062 +                       break;
22063 +               }
22064 +               u->ul += au_rdu_len(ent.nlen);
22065 +       }
22066 +       si_read_unlock(sb);
22067 +
22068 +       return err;
22069 +}
22070 +
22071 +/* ---------------------------------------------------------------------- */
22072 +
22073 +static int au_rdu_verify(struct aufs_rdu *rdu)
22074 +{
22075 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
22076 +             "%llu, b%d, 0x%x, g%u}\n",
22077 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
22078 +             rdu->blk,
22079 +             rdu->rent, rdu->shwh, rdu->full,
22080 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
22081 +             rdu->cookie.generation);
22082 +
22083 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
22084 +               return 0;
22085 +
22086 +       AuDbg("%u:%u\n",
22087 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
22088 +       return -EINVAL;
22089 +}
22090 +
22091 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22092 +{
22093 +       long err, e;
22094 +       struct aufs_rdu rdu;
22095 +       void __user *p = (void __user *)arg;
22096 +
22097 +       err = copy_from_user(&rdu, p, sizeof(rdu));
22098 +       if (unlikely(err)) {
22099 +               err = -EFAULT;
22100 +               AuTraceErr(err);
22101 +               goto out;
22102 +       }
22103 +       err = au_rdu_verify(&rdu);
22104 +       if (unlikely(err))
22105 +               goto out;
22106 +
22107 +       switch (cmd) {
22108 +       case AUFS_CTL_RDU:
22109 +               err = au_rdu(file, &rdu);
22110 +               if (unlikely(err))
22111 +                       break;
22112 +
22113 +               e = copy_to_user(p, &rdu, sizeof(rdu));
22114 +               if (unlikely(e)) {
22115 +                       err = -EFAULT;
22116 +                       AuTraceErr(err);
22117 +               }
22118 +               break;
22119 +       case AUFS_CTL_RDU_INO:
22120 +               err = au_rdu_ino(file, &rdu);
22121 +               break;
22122 +
22123 +       default:
22124 +               /* err = -ENOTTY; */
22125 +               err = -EINVAL;
22126 +       }
22127 +
22128 +out:
22129 +       AuTraceErr(err);
22130 +       return err;
22131 +}
22132 +
22133 +#ifdef CONFIG_COMPAT
22134 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22135 +{
22136 +       long err, e;
22137 +       struct aufs_rdu rdu;
22138 +       void __user *p = compat_ptr(arg);
22139 +
22140 +       /* todo: get_user()? */
22141 +       err = copy_from_user(&rdu, p, sizeof(rdu));
22142 +       if (unlikely(err)) {
22143 +               err = -EFAULT;
22144 +               AuTraceErr(err);
22145 +               goto out;
22146 +       }
22147 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
22148 +       err = au_rdu_verify(&rdu);
22149 +       if (unlikely(err))
22150 +               goto out;
22151 +
22152 +       switch (cmd) {
22153 +       case AUFS_CTL_RDU:
22154 +               err = au_rdu(file, &rdu);
22155 +               if (unlikely(err))
22156 +                       break;
22157 +
22158 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
22159 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
22160 +               e = copy_to_user(p, &rdu, sizeof(rdu));
22161 +               if (unlikely(e)) {
22162 +                       err = -EFAULT;
22163 +                       AuTraceErr(err);
22164 +               }
22165 +               break;
22166 +       case AUFS_CTL_RDU_INO:
22167 +               err = au_rdu_ino(file, &rdu);
22168 +               break;
22169 +
22170 +       default:
22171 +               /* err = -ENOTTY; */
22172 +               err = -EINVAL;
22173 +       }
22174 +
22175 +out:
22176 +       AuTraceErr(err);
22177 +       return err;
22178 +}
22179 +#endif
22180 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
22181 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
22182 +++ linux/fs/aufs/rwsem.h       2012-03-19 12:24:13.683134508 +0100
22183 @@ -0,0 +1,188 @@
22184 +/*
22185 + * Copyright (C) 2005-2012 Junjiro R. Okajima
22186 + *
22187 + * This program, aufs is free software; you can redistribute it and/or modify
22188 + * it under the terms of the GNU General Public License as published by
22189 + * the Free Software Foundation; either version 2 of the License, or
22190 + * (at your option) any later version.
22191 + *
22192 + * This program is distributed in the hope that it will be useful,
22193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22195 + * GNU General Public License for more details.
22196 + *
22197 + * You should have received a copy of the GNU General Public License
22198 + * along with this program; if not, write to the Free Software
22199 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22200 + */
22201 +
22202 +/*
22203 + * simple read-write semaphore wrappers
22204 + */
22205 +
22206 +#ifndef __AUFS_RWSEM_H__
22207 +#define __AUFS_RWSEM_H__
22208 +
22209 +#ifdef __KERNEL__
22210 +
22211 +#include "debug.h"
22212 +
22213 +struct au_rwsem {
22214 +       struct rw_semaphore     rwsem;
22215 +#ifdef CONFIG_AUFS_DEBUG
22216 +       /* just for debugging, not almighty counter */
22217 +       atomic_t                rcnt, wcnt;
22218 +#endif
22219 +};
22220 +
22221 +#ifdef CONFIG_AUFS_DEBUG
22222 +#define AuDbgCntInit(rw) do { \
22223 +       atomic_set(&(rw)->rcnt, 0); \
22224 +       atomic_set(&(rw)->wcnt, 0); \
22225 +       smp_mb(); /* atomic set */ \
22226 +} while (0)
22227 +
22228 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
22229 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
22230 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
22231 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
22232 +#else
22233 +#define AuDbgCntInit(rw)       do {} while (0)
22234 +#define AuDbgRcntInc(rw)       do {} while (0)
22235 +#define AuDbgRcntDec(rw)       do {} while (0)
22236 +#define AuDbgWcntInc(rw)       do {} while (0)
22237 +#define AuDbgWcntDec(rw)       do {} while (0)
22238 +#endif /* CONFIG_AUFS_DEBUG */
22239 +
22240 +/* to debug easier, do not make them inlined functions */
22241 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
22242 +/* rwsem_is_locked() is unusable */
22243 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
22244 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
22245 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
22246 +                                       && atomic_read(&(rw)->wcnt) <= 0)
22247 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
22248 +                                       || atomic_read(&(rw)->wcnt))
22249 +
22250 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
22251 +
22252 +static inline void au_rw_init(struct au_rwsem *rw)
22253 +{
22254 +       AuDbgCntInit(rw);
22255 +       init_rwsem(&rw->rwsem);
22256 +}
22257 +
22258 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
22259 +{
22260 +       au_rw_init(rw);
22261 +       down_write(&rw->rwsem);
22262 +       AuDbgWcntInc(rw);
22263 +}
22264 +
22265 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
22266 +                                          unsigned int lsc)
22267 +{
22268 +       au_rw_init(rw);
22269 +       down_write_nested(&rw->rwsem, lsc);
22270 +       AuDbgWcntInc(rw);
22271 +}
22272 +
22273 +static inline void au_rw_read_lock(struct au_rwsem *rw)
22274 +{
22275 +       down_read(&rw->rwsem);
22276 +       AuDbgRcntInc(rw);
22277 +}
22278 +
22279 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
22280 +{
22281 +       down_read_nested(&rw->rwsem, lsc);
22282 +       AuDbgRcntInc(rw);
22283 +}
22284 +
22285 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
22286 +{
22287 +       AuRwMustReadLock(rw);
22288 +       AuDbgRcntDec(rw);
22289 +       up_read(&rw->rwsem);
22290 +}
22291 +
22292 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
22293 +{
22294 +       AuRwMustWriteLock(rw);
22295 +       AuDbgRcntInc(rw);
22296 +       AuDbgWcntDec(rw);
22297 +       downgrade_write(&rw->rwsem);
22298 +}
22299 +
22300 +static inline void au_rw_write_lock(struct au_rwsem *rw)
22301 +{
22302 +       down_write(&rw->rwsem);
22303 +       AuDbgWcntInc(rw);
22304 +}
22305 +
22306 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
22307 +                                          unsigned int lsc)
22308 +{
22309 +       down_write_nested(&rw->rwsem, lsc);
22310 +       AuDbgWcntInc(rw);
22311 +}
22312 +
22313 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
22314 +{
22315 +       AuRwMustWriteLock(rw);
22316 +       AuDbgWcntDec(rw);
22317 +       up_write(&rw->rwsem);
22318 +}
22319 +
22320 +/* why is not _nested version defined */
22321 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
22322 +{
22323 +       int ret = down_read_trylock(&rw->rwsem);
22324 +       if (ret)
22325 +               AuDbgRcntInc(rw);
22326 +       return ret;
22327 +}
22328 +
22329 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
22330 +{
22331 +       int ret = down_write_trylock(&rw->rwsem);
22332 +       if (ret)
22333 +               AuDbgWcntInc(rw);
22334 +       return ret;
22335 +}
22336 +
22337 +#undef AuDbgCntInit
22338 +#undef AuDbgRcntInc
22339 +#undef AuDbgRcntDec
22340 +#undef AuDbgWcntInc
22341 +#undef AuDbgWcntDec
22342 +
22343 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
22344 +static inline void prefix##_read_lock(param) \
22345 +{ au_rw_read_lock(rwsem); } \
22346 +static inline void prefix##_write_lock(param) \
22347 +{ au_rw_write_lock(rwsem); } \
22348 +static inline int prefix##_read_trylock(param) \
22349 +{ return au_rw_read_trylock(rwsem); } \
22350 +static inline int prefix##_write_trylock(param) \
22351 +{ return au_rw_write_trylock(rwsem); }
22352 +/* why is not _nested version defined */
22353 +/* static inline void prefix##_read_trylock_nested(param, lsc)
22354 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
22355 +static inline void prefix##_write_trylock_nestd(param, lsc)
22356 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
22357 +
22358 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
22359 +static inline void prefix##_read_unlock(param) \
22360 +{ au_rw_read_unlock(rwsem); } \
22361 +static inline void prefix##_write_unlock(param) \
22362 +{ au_rw_write_unlock(rwsem); } \
22363 +static inline void prefix##_downgrade_lock(param) \
22364 +{ au_rw_dgrade_lock(rwsem); }
22365 +
22366 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
22367 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
22368 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
22369 +
22370 +#endif /* __KERNEL__ */
22371 +#endif /* __AUFS_RWSEM_H__ */
22372 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
22373 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
22374 +++ linux/fs/aufs/sbinfo.c      2012-03-19 12:24:13.683134508 +0100
22375 @@ -0,0 +1,343 @@
22376 +/*
22377 + * Copyright (C) 2005-2012 Junjiro R. Okajima
22378 + *
22379 + * This program, aufs is free software; you can redistribute it and/or modify
22380 + * it under the terms of the GNU General Public License as published by
22381 + * the Free Software Foundation; either version 2 of the License, or
22382 + * (at your option) any later version.
22383 + *
22384 + * This program is distributed in the hope that it will be useful,
22385 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22386 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22387 + * GNU General Public License for more details.
22388 + *
22389 + * You should have received a copy of the GNU General Public License
22390 + * along with this program; if not, write to the Free Software
22391 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22392 + */
22393 +
22394 +/*
22395 + * superblock private data
22396 + */
22397 +
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 2012-03-19 12:24:13.683134508 +0100
22722 @@ -0,0 +1,62 @@
22723 +/*
22724 + * Copyright (C) 2005-2012 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 +struct au_splhead {
22751 +       spinlock_t              spin;
22752 +       struct list_head        head;
22753 +};
22754 +
22755 +static inline void au_spl_init(struct au_splhead *spl)
22756 +{
22757 +       spin_lock_init(&spl->spin);
22758 +       INIT_LIST_HEAD(&spl->head);
22759 +}
22760 +
22761 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
22762 +{
22763 +       spin_lock(&spl->spin);
22764 +       list_add(list, &spl->head);
22765 +       spin_unlock(&spl->spin);
22766 +}
22767 +
22768 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
22769 +{
22770 +       spin_lock(&spl->spin);
22771 +       list_del(list);
22772 +       spin_unlock(&spl->spin);
22773 +}
22774 +
22775 +static inline void au_spl_del_rcu(struct list_head *list,
22776 +                                 struct au_splhead *spl)
22777 +{
22778 +       spin_lock(&spl->spin);
22779 +       list_del_rcu(list);
22780 +       spin_unlock(&spl->spin);
22781 +}
22782 +
22783 +#endif /* __KERNEL__ */
22784 +#endif /* __AUFS_SPL_H__ */
22785 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
22786 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
22787 +++ linux/fs/aufs/super.c       2012-03-19 12:24:13.683134508 +0100
22788 @@ -0,0 +1,936 @@
22789 +/*
22790 + * Copyright (C) 2005-2012 Junjiro R. Okajima
22791 + *
22792 + * This program, aufs is free software; you can redistribute it and/or modify
22793 + * it under the terms of the GNU General Public License as published by
22794 + * the Free Software Foundation; either version 2 of the License, or
22795 + * (at your option) any later version.
22796 + *
22797 + * This program is distributed in the hope that it will be useful,
22798 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22799 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22800 + * GNU General Public License for more details.
22801 + *
22802 + * You should have received a copy of the GNU General Public License
22803 + * along with this program; if not, write to the Free Software
22804 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22805 + */
22806 +
22807 +/*
22808 + * mount and super_block operations
22809 + */
22810 +
22811 +#include <linux/mm.h>
22812 +#include <linux/module.h>
22813 +#include <linux/seq_file.h>
22814 +#include <linux/statfs.h>
22815 +#include <linux/vmalloc.h>
22816 +#include <linux/writeback.h>
22817 +#include "aufs.h"
22818 +
22819 +/*
22820 + * super_operations
22821 + */
22822 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
22823 +{
22824 +       struct au_icntnr *c;
22825 +
22826 +       c = au_cache_alloc_icntnr();
22827 +       if (c) {
22828 +               au_icntnr_init(c);
22829 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
22830 +               c->iinfo.ii_hinode = NULL;
22831 +               return &c->vfs_inode;
22832 +       }
22833 +       return NULL;
22834 +}
22835 +
22836 +static void aufs_destroy_inode_cb(struct rcu_head *head)
22837 +{
22838 +       struct inode *inode = container_of(head, struct inode, i_rcu);
22839 +
22840 +       INIT_LIST_HEAD(&inode->i_dentry);
22841 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
22842 +}
22843 +
22844 +static void aufs_destroy_inode(struct inode *inode)
22845 +{
22846 +       au_iinfo_fin(inode);
22847 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
22848 +}
22849 +
22850 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
22851 +{
22852 +       struct inode *inode;
22853 +       int err;
22854 +
22855 +       inode = iget_locked(sb, ino);
22856 +       if (unlikely(!inode)) {
22857 +               inode = ERR_PTR(-ENOMEM);
22858 +               goto out;
22859 +       }
22860 +       if (!(inode->i_state & I_NEW))
22861 +               goto out;
22862 +
22863 +       err = au_xigen_new(inode);
22864 +       if (!err)
22865 +               err = au_iinfo_init(inode);
22866 +       if (!err)
22867 +               inode->i_version++;
22868 +       else {
22869 +               iget_failed(inode);
22870 +               inode = ERR_PTR(err);
22871 +       }
22872 +
22873 +out:
22874 +       /* never return NULL */
22875 +       AuDebugOn(!inode);
22876 +       AuTraceErrPtr(inode);
22877 +       return inode;
22878 +}
22879 +
22880 +/* lock free root dinfo */
22881 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
22882 +{
22883 +       int err;
22884 +       aufs_bindex_t bindex, bend;
22885 +       struct path path;
22886 +       struct au_hdentry *hdp;
22887 +       struct au_branch *br;
22888 +       char *perm;
22889 +
22890 +       err = 0;
22891 +       bend = au_sbend(sb);
22892 +       hdp = au_di(sb->s_root)->di_hdentry;
22893 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
22894 +               br = au_sbr(sb, bindex);
22895 +               path.mnt = br->br_mnt;
22896 +               path.dentry = hdp[bindex].hd_dentry;
22897 +               err = au_seq_path(seq, &path);
22898 +               if (err > 0) {
22899 +                       perm = au_optstr_br_perm(br->br_perm);
22900 +                       if (perm) {
22901 +                               err = seq_printf(seq, "=%s", perm);
22902 +                               kfree(perm);
22903 +                               if (err == -1)
22904 +                                       err = -E2BIG;
22905 +                       } else
22906 +                               err = -ENOMEM;
22907 +               }
22908 +               if (!err && bindex != bend)
22909 +                       err = seq_putc(seq, ':');
22910 +       }
22911 +
22912 +       return err;
22913 +}
22914 +
22915 +static void au_show_wbr_create(struct seq_file *m, int v,
22916 +                              struct au_sbinfo *sbinfo)
22917 +{
22918 +       const char *pat;
22919 +
22920 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22921 +
22922 +       seq_printf(m, ",create=");
22923 +       pat = au_optstr_wbr_create(v);
22924 +       switch (v) {
22925 +       case AuWbrCreate_TDP:
22926 +       case AuWbrCreate_RR:
22927 +       case AuWbrCreate_MFS:
22928 +       case AuWbrCreate_PMFS:
22929 +               seq_printf(m, pat);
22930 +               break;
22931 +       case AuWbrCreate_MFSV:
22932 +               seq_printf(m, /*pat*/"mfs:%lu",
22933 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
22934 +                          / MSEC_PER_SEC);
22935 +               break;
22936 +       case AuWbrCreate_PMFSV:
22937 +               seq_printf(m, /*pat*/"pmfs:%lu",
22938 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
22939 +                          / MSEC_PER_SEC);
22940 +               break;
22941 +       case AuWbrCreate_MFSRR:
22942 +               seq_printf(m, /*pat*/"mfsrr:%llu",
22943 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
22944 +               break;
22945 +       case AuWbrCreate_MFSRRV:
22946 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
22947 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
22948 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
22949 +                          / MSEC_PER_SEC);
22950 +               break;
22951 +       }
22952 +}
22953 +
22954 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
22955 +{
22956 +#ifdef CONFIG_SYSFS
22957 +       return 0;
22958 +#else
22959 +       int err;
22960 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
22961 +       aufs_bindex_t bindex, brid;
22962 +       struct qstr *name;
22963 +       struct file *f;
22964 +       struct dentry *d, *h_root;
22965 +       struct au_hdentry *hdp;
22966 +
22967 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22968 +
22969 +       err = 0;
22970 +       f = au_sbi(sb)->si_xib;
22971 +       if (!f)
22972 +               goto out;
22973 +
22974 +       /* stop printing the default xino path on the first writable branch */
22975 +       h_root = NULL;
22976 +       brid = au_xino_brid(sb);
22977 +       if (brid >= 0) {
22978 +               bindex = au_br_index(sb, brid);
22979 +               hdp = au_di(sb->s_root)->di_hdentry;
22980 +               h_root = hdp[0 + bindex].hd_dentry;
22981 +       }
22982 +       d = f->f_dentry;
22983 +       name = &d->d_name;
22984 +       /* safe ->d_parent because the file is unlinked */
22985 +       if (d->d_parent == h_root
22986 +           && name->len == len
22987 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
22988 +               goto out;
22989 +
22990 +       seq_puts(seq, ",xino=");
22991 +       err = au_xino_path(seq, f);
22992 +
22993 +out:
22994 +       return err;
22995 +#endif
22996 +}
22997 +
22998 +/* seq_file will re-call me in case of too long string */
22999 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
23000 +{
23001 +       int err;
23002 +       unsigned int mnt_flags, v;
23003 +       struct super_block *sb;
23004 +       struct au_sbinfo *sbinfo;
23005 +
23006 +#define AuBool(name, str) do { \
23007 +       v = au_opt_test(mnt_flags, name); \
23008 +       if (v != au_opt_test(AuOpt_Def, name)) \
23009 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
23010 +} while (0)
23011 +
23012 +#define AuStr(name, str) do { \
23013 +       v = mnt_flags & AuOptMask_##name; \
23014 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
23015 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
23016 +} while (0)
23017 +
23018 +#define AuUInt(name, str, val) do { \
23019 +       if (val != AUFS_##name##_DEF) \
23020 +               seq_printf(m, "," #str "=%u", val); \
23021 +} while (0)
23022 +
23023 +       /* lock free root dinfo */
23024 +       sb = dentry->d_sb;
23025 +       si_noflush_read_lock(sb);
23026 +       sbinfo = au_sbi(sb);
23027 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
23028 +
23029 +       mnt_flags = au_mntflags(sb);
23030 +       if (au_opt_test(mnt_flags, XINO)) {
23031 +               err = au_show_xino(m, sb);
23032 +               if (unlikely(err))
23033 +                       goto out;
23034 +       } else
23035 +               seq_puts(m, ",noxino");
23036 +
23037 +       AuBool(TRUNC_XINO, trunc_xino);
23038 +       AuStr(UDBA, udba);
23039 +       AuBool(SHWH, shwh);
23040 +       AuBool(PLINK, plink);
23041 +       AuBool(DIO, dio);
23042 +       /* AuBool(DIRPERM1, dirperm1); */
23043 +       /* AuBool(REFROF, refrof); */
23044 +
23045 +       v = sbinfo->si_wbr_create;
23046 +       if (v != AuWbrCreate_Def)
23047 +               au_show_wbr_create(m, v, sbinfo);
23048 +
23049 +       v = sbinfo->si_wbr_copyup;
23050 +       if (v != AuWbrCopyup_Def)
23051 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
23052 +
23053 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
23054 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
23055 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
23056 +
23057 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
23058 +
23059 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
23060 +       AuUInt(RDCACHE, rdcache, v);
23061 +
23062 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
23063 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
23064 +
23065 +       AuBool(SUM, sum);
23066 +       /* AuBool(SUM_W, wsum); */
23067 +       AuBool(WARN_PERM, warn_perm);
23068 +       AuBool(VERBOSE, verbose);
23069 +
23070 +out:
23071 +       /* be sure to print "br:" last */
23072 +       if (!sysaufs_brs) {
23073 +               seq_puts(m, ",br:");
23074 +               au_show_brs(m, sb);
23075 +       }
23076 +       si_read_unlock(sb);
23077 +       return 0;
23078 +
23079 +#undef AuBool
23080 +#undef AuStr
23081 +#undef AuUInt
23082 +}
23083 +
23084 +/* ---------------------------------------------------------------------- */
23085 +
23086 +/* sum mode which returns the summation for statfs(2) */
23087 +
23088 +static u64 au_add_till_max(u64 a, u64 b)
23089 +{
23090 +       u64 old;
23091 +
23092 +       old = a;
23093 +       a += b;
23094 +       if (old < a)
23095 +               return a;
23096 +       return ULLONG_MAX;
23097 +}
23098 +
23099 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
23100 +{
23101 +       int err;
23102 +       u64 blocks, bfree, bavail, files, ffree;
23103 +       aufs_bindex_t bend, bindex, i;
23104 +       unsigned char shared;
23105 +       struct path h_path;
23106 +       struct super_block *h_sb;
23107 +
23108 +       blocks = 0;
23109 +       bfree = 0;
23110 +       bavail = 0;
23111 +       files = 0;
23112 +       ffree = 0;
23113 +
23114 +       err = 0;
23115 +       bend = au_sbend(sb);
23116 +       for (bindex = bend; bindex >= 0; bindex--) {
23117 +               h_path.mnt = au_sbr_mnt(sb, bindex);
23118 +               h_sb = h_path.mnt->mnt_sb;
23119 +               shared = 0;
23120 +               for (i = bindex + 1; !shared && i <= bend; i++)
23121 +                       shared = (au_sbr_sb(sb, i) == h_sb);
23122 +               if (shared)
23123 +                       continue;
23124 +
23125 +               /* sb->s_root for NFS is unreliable */
23126 +               h_path.dentry = h_path.mnt->mnt_root;
23127 +               err = vfs_statfs(&h_path, buf);
23128 +               if (unlikely(err))
23129 +                       goto out;
23130 +
23131 +               blocks = au_add_till_max(blocks, buf->f_blocks);
23132 +               bfree = au_add_till_max(bfree, buf->f_bfree);
23133 +               bavail = au_add_till_max(bavail, buf->f_bavail);
23134 +               files = au_add_till_max(files, buf->f_files);
23135 +               ffree = au_add_till_max(ffree, buf->f_ffree);
23136 +       }
23137 +
23138 +       buf->f_blocks = blocks;
23139 +       buf->f_bfree = bfree;
23140 +       buf->f_bavail = bavail;
23141 +       buf->f_files = files;
23142 +       buf->f_ffree = ffree;
23143 +
23144 +out:
23145 +       return err;
23146 +}
23147 +
23148 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
23149 +{
23150 +       int err;
23151 +       struct path h_path;
23152 +       struct super_block *sb;
23153 +
23154 +       /* lock free root dinfo */
23155 +       sb = dentry->d_sb;
23156 +       si_noflush_read_lock(sb);
23157 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
23158 +               /* sb->s_root for NFS is unreliable */
23159 +               h_path.mnt = au_sbr_mnt(sb, 0);
23160 +               h_path.dentry = h_path.mnt->mnt_root;
23161 +               err = vfs_statfs(&h_path, buf);
23162 +       } else
23163 +               err = au_statfs_sum(sb, buf);
23164 +       si_read_unlock(sb);
23165 +
23166 +       if (!err) {
23167 +               buf->f_type = AUFS_SUPER_MAGIC;
23168 +               buf->f_namelen = AUFS_MAX_NAMELEN;
23169 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
23170 +       }
23171 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
23172 +
23173 +       return err;
23174 +}
23175 +
23176 +/* ---------------------------------------------------------------------- */
23177 +
23178 +/* final actions when unmounting a file system */
23179 +static void aufs_put_super(struct super_block *sb)
23180 +{
23181 +       struct au_sbinfo *sbinfo;
23182 +
23183 +       sbinfo = au_sbi(sb);
23184 +       if (!sbinfo)
23185 +               return;
23186 +
23187 +       dbgaufs_si_fin(sbinfo);
23188 +       kobject_put(&sbinfo->si_kobj);
23189 +}
23190 +
23191 +/* ---------------------------------------------------------------------- */
23192 +
23193 +void au_array_free(void *array)
23194 +{
23195 +       if (array) {
23196 +               if (!is_vmalloc_addr(array))
23197 +                       kfree(array);
23198 +               else
23199 +                       vfree(array);
23200 +       }
23201 +}
23202 +
23203 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
23204 +{
23205 +       void *array;
23206 +       unsigned long long n;
23207 +
23208 +       array = NULL;
23209 +       n = 0;
23210 +       if (!*hint)
23211 +               goto out;
23212 +
23213 +       if (*hint > ULLONG_MAX / sizeof(array)) {
23214 +               array = ERR_PTR(-EMFILE);
23215 +               pr_err("hint %llu\n", *hint);
23216 +               goto out;
23217 +       }
23218 +
23219 +       array = kmalloc(sizeof(array) * *hint, GFP_NOFS);
23220 +       if (unlikely(!array))
23221 +               array = vmalloc(sizeof(array) * *hint);
23222 +       if (unlikely(!array)) {
23223 +               array = ERR_PTR(-ENOMEM);
23224 +               goto out;
23225 +       }
23226 +
23227 +       n = cb(array, *hint, arg);
23228 +       AuDebugOn(n > *hint);
23229 +
23230 +out:
23231 +       *hint = n;
23232 +       return array;
23233 +}
23234 +
23235 +static unsigned long long au_iarray_cb(void *a,
23236 +                                      unsigned long long max __maybe_unused,
23237 +                                      void *arg)
23238 +{
23239 +       unsigned long long n;
23240 +       struct inode **p, *inode;
23241 +       struct list_head *head;
23242 +
23243 +       n = 0;
23244 +       p = a;
23245 +       head = arg;
23246 +       spin_lock(&inode_sb_list_lock);
23247 +       list_for_each_entry(inode, head, i_sb_list) {
23248 +               if (!is_bad_inode(inode)
23249 +                   && au_ii(inode)->ii_bstart >= 0) {
23250 +                       spin_lock(&inode->i_lock);
23251 +                       if (atomic_read(&inode->i_count)) {
23252 +                               au_igrab(inode);
23253 +                               *p++ = inode;
23254 +                               n++;
23255 +                               AuDebugOn(n > max);
23256 +                       }
23257 +                       spin_unlock(&inode->i_lock);
23258 +               }
23259 +       }
23260 +       spin_unlock(&inode_sb_list_lock);
23261 +
23262 +       return n;
23263 +}
23264 +
23265 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
23266 +{
23267 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
23268 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
23269 +}
23270 +
23271 +void au_iarray_free(struct inode **a, unsigned long long max)
23272 +{
23273 +       unsigned long long ull;
23274 +
23275 +       for (ull = 0; ull < max; ull++)
23276 +               iput(a[ull]);
23277 +       au_array_free(a);
23278 +}
23279 +
23280 +/* ---------------------------------------------------------------------- */
23281 +
23282 +/*
23283 + * refresh dentry and inode at remount time.
23284 + */
23285 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
23286 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
23287 +                     struct dentry *parent)
23288 +{
23289 +       int err;
23290 +
23291 +       di_write_lock_child(dentry);
23292 +       di_read_lock_parent(parent, AuLock_IR);
23293 +       err = au_refresh_dentry(dentry, parent);
23294 +       if (!err && dir_flags)
23295 +               au_hn_reset(dentry->d_inode, dir_flags);
23296 +       di_read_unlock(parent, AuLock_IR);
23297 +       di_write_unlock(dentry);
23298 +
23299 +       return err;
23300 +}
23301 +
23302 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
23303 +                          struct au_sbinfo *sbinfo,
23304 +                          const unsigned int dir_flags)
23305 +{
23306 +       int err;
23307 +       struct dentry *parent;
23308 +       struct inode *inode;
23309 +
23310 +       err = 0;
23311 +       parent = dget_parent(dentry);
23312 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
23313 +               inode = dentry->d_inode;
23314 +               if (inode) {
23315 +                       if (!S_ISDIR(inode->i_mode))
23316 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
23317 +                                                parent);
23318 +                       else {
23319 +                               err = au_do_refresh(dentry, dir_flags, parent);
23320 +                               if (unlikely(err))
23321 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
23322 +                       }
23323 +               } else
23324 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
23325 +               AuDbgDentry(dentry);
23326 +       }
23327 +       dput(parent);
23328 +
23329 +       AuTraceErr(err);
23330 +       return err;
23331 +}
23332 +
23333 +static int au_refresh_d(struct super_block *sb)
23334 +{
23335 +       int err, i, j, ndentry, e;
23336 +       unsigned int sigen;
23337 +       struct au_dcsub_pages dpages;
23338 +       struct au_dpage *dpage;
23339 +       struct dentry **dentries, *d;
23340 +       struct au_sbinfo *sbinfo;
23341 +       struct dentry *root = sb->s_root;
23342 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
23343 +
23344 +       err = au_dpages_init(&dpages, GFP_NOFS);
23345 +       if (unlikely(err))
23346 +               goto out;
23347 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
23348 +       if (unlikely(err))
23349 +               goto out_dpages;
23350 +
23351 +       sigen = au_sigen(sb);
23352 +       sbinfo = au_sbi(sb);
23353 +       for (i = 0; i < dpages.ndpage; i++) {
23354 +               dpage = dpages.dpages + i;
23355 +               dentries = dpage->dentries;
23356 +               ndentry = dpage->ndentry;
23357 +               for (j = 0; j < ndentry; j++) {
23358 +                       d = dentries[j];
23359 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
23360 +                       if (unlikely(e && !err))
23361 +                               err = e;
23362 +                       /* go on even err */
23363 +               }
23364 +       }
23365 +
23366 +out_dpages:
23367 +       au_dpages_free(&dpages);
23368 +out:
23369 +       return err;
23370 +}
23371 +
23372 +static int au_refresh_i(struct super_block *sb)
23373 +{
23374 +       int err, e;
23375 +       unsigned int sigen;
23376 +       unsigned long long max, ull;
23377 +       struct inode *inode, **array;
23378 +
23379 +       array = au_iarray_alloc(sb, &max);
23380 +       err = PTR_ERR(array);
23381 +       if (IS_ERR(array))
23382 +               goto out;
23383 +
23384 +       err = 0;
23385 +       sigen = au_sigen(sb);
23386 +       for (ull = 0; ull < max; ull++) {
23387 +               inode = array[ull];
23388 +               if (au_iigen(inode) != sigen) {
23389 +                       ii_write_lock_child(inode);
23390 +                       e = au_refresh_hinode_self(inode);
23391 +                       ii_write_unlock(inode);
23392 +                       if (unlikely(e)) {
23393 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
23394 +                               if (!err)
23395 +                                       err = e;
23396 +                               /* go on even if err */
23397 +                       }
23398 +               }
23399 +       }
23400 +
23401 +       au_iarray_free(array, max);
23402 +
23403 +out:
23404 +       return err;
23405 +}
23406 +
23407 +static void au_remount_refresh(struct super_block *sb)
23408 +{
23409 +       int err, e;
23410 +       unsigned int udba;
23411 +       aufs_bindex_t bindex, bend;
23412 +       struct dentry *root;
23413 +       struct inode *inode;
23414 +       struct au_branch *br;
23415 +
23416 +       au_sigen_inc(sb);
23417 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
23418 +
23419 +       root = sb->s_root;
23420 +       DiMustNoWaiters(root);
23421 +       inode = root->d_inode;
23422 +       IiMustNoWaiters(inode);
23423 +
23424 +       udba = au_opt_udba(sb);
23425 +       bend = au_sbend(sb);
23426 +       for (bindex = 0; bindex <= bend; bindex++) {
23427 +               br = au_sbr(sb, bindex);
23428 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
23429 +               if (unlikely(err))
23430 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
23431 +                               bindex, err);
23432 +               /* go on even if err */
23433 +       }
23434 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
23435 +
23436 +       di_write_unlock(root);
23437 +       err = au_refresh_d(sb);
23438 +       e = au_refresh_i(sb);
23439 +       if (unlikely(e && !err))
23440 +               err = e;
23441 +       /* aufs_write_lock() calls ..._child() */
23442 +       di_write_lock_child(root);
23443 +
23444 +       au_cpup_attr_all(inode, /*force*/1);
23445 +
23446 +       if (unlikely(err))
23447 +               AuIOErr("refresh failed, ignored, %d\n", err);
23448 +}
23449 +
23450 +/* stop extra interpretation of errno in mount(8), and strange error messages */
23451 +static int cvt_err(int err)
23452 +{
23453 +       AuTraceErr(err);
23454 +
23455 +       switch (err) {
23456 +       case -ENOENT:
23457 +       case -ENOTDIR:
23458 +       case -EEXIST:
23459 +       case -EIO:
23460 +               err = -EINVAL;
23461 +       }
23462 +       return err;
23463 +}
23464 +
23465 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
23466 +{
23467 +       int err, do_dx;
23468 +       unsigned int mntflags;
23469 +       struct au_opts opts;
23470 +       struct dentry *root;
23471 +       struct inode *inode;
23472 +       struct au_sbinfo *sbinfo;
23473 +
23474 +       err = 0;
23475 +       root = sb->s_root;
23476 +       if (!data || !*data) {
23477 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23478 +               if (!err) {
23479 +                       di_write_lock_child(root);
23480 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
23481 +                       aufs_write_unlock(root);
23482 +               }
23483 +               goto out;
23484 +       }
23485 +
23486 +       err = -ENOMEM;
23487 +       memset(&opts, 0, sizeof(opts));
23488 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
23489 +       if (unlikely(!opts.opt))
23490 +               goto out;
23491 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
23492 +       opts.flags = AuOpts_REMOUNT;
23493 +       opts.sb_flags = *flags;
23494 +
23495 +       /* parse it before aufs lock */
23496 +       err = au_opts_parse(sb, data, &opts);
23497 +       if (unlikely(err))
23498 +               goto out_opts;
23499 +
23500 +       sbinfo = au_sbi(sb);
23501 +       inode = root->d_inode;
23502 +       mutex_lock(&inode->i_mutex);
23503 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23504 +       if (unlikely(err))
23505 +               goto out_mtx;
23506 +       di_write_lock_child(root);
23507 +
23508 +       /* au_opts_remount() may return an error */
23509 +       err = au_opts_remount(sb, &opts);
23510 +       au_opts_free(&opts);
23511 +
23512 +       if (au_ftest_opts(opts.flags, REFRESH))
23513 +               au_remount_refresh(sb);
23514 +
23515 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
23516 +               mntflags = au_mntflags(sb);
23517 +               do_dx = !!au_opt_test(mntflags, DIO);
23518 +               au_dy_arefresh(do_dx);
23519 +       }
23520 +
23521 +       aufs_write_unlock(root);
23522 +
23523 +out_mtx:
23524 +       mutex_unlock(&inode->i_mutex);
23525 +out_opts:
23526 +       free_page((unsigned long)opts.opt);
23527 +out:
23528 +       err = cvt_err(err);
23529 +       AuTraceErr(err);
23530 +       return err;
23531 +}
23532 +
23533 +static const struct super_operations aufs_sop = {
23534 +       .alloc_inode    = aufs_alloc_inode,
23535 +       .destroy_inode  = aufs_destroy_inode,
23536 +       /* always deleting, no clearing */
23537 +       .drop_inode     = generic_delete_inode,
23538 +       .show_options   = aufs_show_options,
23539 +       .statfs         = aufs_statfs,
23540 +       .put_super      = aufs_put_super,
23541 +       .remount_fs     = aufs_remount_fs
23542 +};
23543 +
23544 +/* ---------------------------------------------------------------------- */
23545 +
23546 +static int alloc_root(struct super_block *sb)
23547 +{
23548 +       int err;
23549 +       struct inode *inode;
23550 +       struct dentry *root;
23551 +
23552 +       err = -ENOMEM;
23553 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
23554 +       err = PTR_ERR(inode);
23555 +       if (IS_ERR(inode))
23556 +               goto out;
23557 +
23558 +       inode->i_op = &aufs_dir_iop;
23559 +       inode->i_fop = &aufs_dir_fop;
23560 +       inode->i_mode = S_IFDIR;
23561 +       set_nlink(inode, 2);
23562 +       unlock_new_inode(inode);
23563 +
23564 +       root = d_alloc_root(inode);
23565 +       if (unlikely(!root))
23566 +               goto out_iput;
23567 +       err = PTR_ERR(root);
23568 +       if (IS_ERR(root))
23569 +               goto out_iput;
23570 +
23571 +       err = au_di_init(root);
23572 +       if (!err) {
23573 +               sb->s_root = root;
23574 +               return 0; /* success */
23575 +       }
23576 +       dput(root);
23577 +       goto out; /* do not iput */
23578 +
23579 +out_iput:
23580 +       iget_failed(inode);
23581 +out:
23582 +       return err;
23583 +
23584 +}
23585 +
23586 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
23587 +                          int silent __maybe_unused)
23588 +{
23589 +       int err;
23590 +       struct au_opts opts;
23591 +       struct dentry *root;
23592 +       struct inode *inode;
23593 +       char *arg = raw_data;
23594 +
23595 +       if (unlikely(!arg || !*arg)) {
23596 +               err = -EINVAL;
23597 +               pr_err("no arg\n");
23598 +               goto out;
23599 +       }
23600 +
23601 +       err = -ENOMEM;
23602 +       memset(&opts, 0, sizeof(opts));
23603 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
23604 +       if (unlikely(!opts.opt))
23605 +               goto out;
23606 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
23607 +       opts.sb_flags = sb->s_flags;
23608 +
23609 +       err = au_si_alloc(sb);
23610 +       if (unlikely(err))
23611 +               goto out_opts;
23612 +
23613 +       /* all timestamps always follow the ones on the branch */
23614 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
23615 +       sb->s_op = &aufs_sop;
23616 +       sb->s_d_op = &aufs_dop;
23617 +       sb->s_magic = AUFS_SUPER_MAGIC;
23618 +       sb->s_maxbytes = 0;
23619 +       au_export_init(sb);
23620 +
23621 +       err = alloc_root(sb);
23622 +       if (unlikely(err)) {
23623 +               si_write_unlock(sb);
23624 +               goto out_info;
23625 +       }
23626 +       root = sb->s_root;
23627 +       inode = root->d_inode;
23628 +
23629 +       /*
23630 +        * actually we can parse options regardless aufs lock here.
23631 +        * but at remount time, parsing must be done before aufs lock.
23632 +        * so we follow the same rule.
23633 +        */
23634 +       ii_write_lock_parent(inode);
23635 +       aufs_write_unlock(root);
23636 +       err = au_opts_parse(sb, arg, &opts);
23637 +       if (unlikely(err))
23638 +               goto out_root;
23639 +
23640 +       /* lock vfs_inode first, then aufs. */
23641 +       mutex_lock(&inode->i_mutex);
23642 +       aufs_write_lock(root);
23643 +       err = au_opts_mount(sb, &opts);
23644 +       au_opts_free(&opts);
23645 +       aufs_write_unlock(root);
23646 +       mutex_unlock(&inode->i_mutex);
23647 +       if (!err)
23648 +               goto out_opts; /* success */
23649 +
23650 +out_root:
23651 +       dput(root);
23652 +       sb->s_root = NULL;
23653 +out_info:
23654 +       dbgaufs_si_fin(au_sbi(sb));
23655 +       kobject_put(&au_sbi(sb)->si_kobj);
23656 +       sb->s_fs_info = NULL;
23657 +out_opts:
23658 +       free_page((unsigned long)opts.opt);
23659 +out:
23660 +       AuTraceErr(err);
23661 +       err = cvt_err(err);
23662 +       AuTraceErr(err);
23663 +       return err;
23664 +}
23665 +
23666 +/* ---------------------------------------------------------------------- */
23667 +
23668 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
23669 +                                const char *dev_name __maybe_unused,
23670 +                                void *raw_data)
23671 +{
23672 +       struct dentry *root;
23673 +       struct super_block *sb;
23674 +
23675 +       /* all timestamps always follow the ones on the branch */
23676 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
23677 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
23678 +       if (IS_ERR(root))
23679 +               goto out;
23680 +
23681 +       sb = root->d_sb;
23682 +       si_write_lock(sb, !AuLock_FLUSH);
23683 +       sysaufs_brs_add(sb, 0);
23684 +       si_write_unlock(sb);
23685 +       au_sbilist_add(sb);
23686 +
23687 +out:
23688 +       return root;
23689 +}
23690 +
23691 +static void aufs_kill_sb(struct super_block *sb)
23692 +{
23693 +       struct au_sbinfo *sbinfo;
23694 +
23695 +       sbinfo = au_sbi(sb);
23696 +       if (sbinfo) {
23697 +               au_sbilist_del(sb);
23698 +               aufs_write_lock(sb->s_root);
23699 +               if (sbinfo->si_wbr_create_ops->fin)
23700 +                       sbinfo->si_wbr_create_ops->fin(sb);
23701 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
23702 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
23703 +                       au_remount_refresh(sb);
23704 +               }
23705 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23706 +                       au_plink_put(sb, /*verbose*/1);
23707 +               au_xino_clr(sb);
23708 +               sbinfo->si_sb = NULL;
23709 +               aufs_write_unlock(sb->s_root);
23710 +               au_nwt_flush(&sbinfo->si_nowait);
23711 +       }
23712 +       generic_shutdown_super(sb);
23713 +}
23714 +
23715 +struct file_system_type aufs_fs_type = {
23716 +       .name           = AUFS_FSTYPE,
23717 +       .fs_flags       =
23718 +               FS_RENAME_DOES_D_MOVE   /* a race between rename and others */
23719 +               | FS_REVAL_DOT,         /* for NFS branch and udba */
23720 +       .mount          = aufs_mount,
23721 +       .kill_sb        = aufs_kill_sb,
23722 +       /* no need to __module_get() and module_put(). */
23723 +       .owner          = THIS_MODULE,
23724 +};
23725 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
23726 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
23727 +++ linux/fs/aufs/super.h       2012-03-19 12:24:13.686467926 +0100
23728 @@ -0,0 +1,546 @@
23729 +/*
23730 + * Copyright (C) 2005-2012 Junjiro R. Okajima
23731 + *
23732 + * This program, aufs is free software; you can redistribute it and/or modify
23733 + * it under the terms of the GNU General Public License as published by
23734 + * the Free Software Foundation; either version 2 of the License, or
23735 + * (at your option) any later version.
23736 + *
23737 + * This program is distributed in the hope that it will be useful,
23738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23740 + * GNU General Public License for more details.
23741 + *
23742 + * You should have received a copy of the GNU General Public License
23743 + * along with this program; if not, write to the Free Software
23744 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23745 + */
23746 +
23747 +/*
23748 + * super_block operations
23749 + */
23750 +
23751 +#ifndef __AUFS_SUPER_H__
23752 +#define __AUFS_SUPER_H__
23753 +
23754 +#ifdef __KERNEL__
23755 +
23756 +#include <linux/fs.h>
23757 +#include "rwsem.h"
23758 +#include "spl.h"
23759 +#include "wkq.h"
23760 +
23761 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
23762 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
23763 +                              loff_t *);
23764 +
23765 +/* policies to select one among multiple writable branches */
23766 +struct au_wbr_copyup_operations {
23767 +       int (*copyup)(struct dentry *dentry);
23768 +};
23769 +
23770 +struct au_wbr_create_operations {
23771 +       int (*create)(struct dentry *dentry, int isdir);
23772 +       int (*init)(struct super_block *sb);
23773 +       int (*fin)(struct super_block *sb);
23774 +};
23775 +
23776 +struct au_wbr_mfs {
23777 +       struct mutex    mfs_lock; /* protect this structure */
23778 +       unsigned long   mfs_jiffy;
23779 +       unsigned long   mfs_expire;
23780 +       aufs_bindex_t   mfs_bindex;
23781 +
23782 +       unsigned long long      mfsrr_bytes;
23783 +       unsigned long long      mfsrr_watermark;
23784 +};
23785 +
23786 +struct au_branch;
23787 +struct au_sbinfo {
23788 +       /* nowait tasks in the system-wide workqueue */
23789 +       struct au_nowait_tasks  si_nowait;
23790 +
23791 +       /*
23792 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
23793 +        * rwsem for au_sbinfo is necessary.
23794 +        */
23795 +       struct au_rwsem         si_rwsem;
23796 +
23797 +       /* prevent recursive locking in deleting inode */
23798 +       struct {
23799 +               unsigned long           *bitmap;
23800 +               spinlock_t              tree_lock;
23801 +               struct radix_tree_root  tree;
23802 +       } au_si_pid;
23803 +
23804 +       /*
23805 +        * dirty approach to protect sb->sb_inodes and ->s_files from remount.
23806 +        */
23807 +       atomic_long_t           si_ninodes, si_nfiles;
23808 +
23809 +       /* branch management */
23810 +       unsigned int            si_generation;
23811 +
23812 +       /* see above flags */
23813 +       unsigned char           au_si_status;
23814 +
23815 +       aufs_bindex_t           si_bend;
23816 +
23817 +       /* dirty trick to keep br_id plus */
23818 +       unsigned int            si_last_br_id :
23819 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
23820 +       struct au_branch        **si_branch;
23821 +
23822 +       /* policy to select a writable branch */
23823 +       unsigned char           si_wbr_copyup;
23824 +       unsigned char           si_wbr_create;
23825 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
23826 +       struct au_wbr_create_operations *si_wbr_create_ops;
23827 +
23828 +       /* round robin */
23829 +       atomic_t                si_wbr_rr_next;
23830 +
23831 +       /* most free space */
23832 +       struct au_wbr_mfs       si_wbr_mfs;
23833 +
23834 +       /* mount flags */
23835 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
23836 +       unsigned int            si_mntflags;
23837 +
23838 +       /* external inode number (bitmap and translation table) */
23839 +       au_readf_t              si_xread;
23840 +       au_writef_t             si_xwrite;
23841 +       struct file             *si_xib;
23842 +       struct mutex            si_xib_mtx; /* protect xib members */
23843 +       unsigned long           *si_xib_buf;
23844 +       unsigned long           si_xib_last_pindex;
23845 +       int                     si_xib_next_bit;
23846 +       aufs_bindex_t           si_xino_brid;
23847 +       /* reserved for future use */
23848 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
23849 +
23850 +#ifdef CONFIG_AUFS_EXPORT
23851 +       /* i_generation */
23852 +       struct file             *si_xigen;
23853 +       atomic_t                si_xigen_next;
23854 +#endif
23855 +
23856 +       /* vdir parameters */
23857 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
23858 +       unsigned int            si_rdblk;       /* deblk size */
23859 +       unsigned int            si_rdhash;      /* hash size */
23860 +
23861 +       /*
23862 +        * If the number of whiteouts are larger than si_dirwh, leave all of
23863 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
23864 +        * future fsck.aufs or kernel thread will remove them later.
23865 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
23866 +        */
23867 +       unsigned int            si_dirwh;
23868 +
23869 +       /*
23870 +        * rename(2) a directory with all children.
23871 +        */
23872 +       /* reserved for future use */
23873 +       /* int                  si_rendir; */
23874 +
23875 +       /* pseudo_link list */
23876 +       struct au_splhead       si_plink;
23877 +       wait_queue_head_t       si_plink_wq;
23878 +       spinlock_t              si_plink_maint_lock;
23879 +       pid_t                   si_plink_maint_pid;
23880 +
23881 +       /*
23882 +        * sysfs and lifetime management.
23883 +        * this is not a small structure and it may be a waste of memory in case
23884 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
23885 +        * but using sysfs is majority.
23886 +        */
23887 +       struct kobject          si_kobj;
23888 +#ifdef CONFIG_DEBUG_FS
23889 +       struct dentry            *si_dbgaufs, *si_dbgaufs_xib;
23890 +#ifdef CONFIG_AUFS_EXPORT
23891 +       struct dentry            *si_dbgaufs_xigen;
23892 +#endif
23893 +#endif
23894 +
23895 +#ifdef CONFIG_AUFS_SBILIST
23896 +       struct list_head        si_list;
23897 +#endif
23898 +
23899 +       /* dirty, necessary for unmounting, sysfs and sysrq */
23900 +       struct super_block      *si_sb;
23901 +};
23902 +
23903 +/* sbinfo status flags */
23904 +/*
23905 + * set true when refresh_dirs() failed at remount time.
23906 + * then try refreshing dirs at access time again.
23907 + * if it is false, refreshing dirs at access time is unnecesary
23908 + */
23909 +#define AuSi_FAILED_REFRESH_DIR        1
23910 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
23911 +                                          unsigned int flag)
23912 +{
23913 +       AuRwMustAnyLock(&sbi->si_rwsem);
23914 +       return sbi->au_si_status & flag;
23915 +}
23916 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
23917 +#define au_fset_si(sbinfo, name) do { \
23918 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
23919 +       (sbinfo)->au_si_status |= AuSi_##name; \
23920 +} while (0)
23921 +#define au_fclr_si(sbinfo, name) do { \
23922 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
23923 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
23924 +} while (0)
23925 +
23926 +/* ---------------------------------------------------------------------- */
23927 +
23928 +/* policy to select one among writable branches */
23929 +#define AuWbrCopyup(sbinfo, ...) \
23930 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
23931 +#define AuWbrCreate(sbinfo, ...) \
23932 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
23933 +
23934 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
23935 +#define AuLock_DW              1               /* write-lock dentry */
23936 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
23937 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
23938 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
23939 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
23940 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
23941 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
23942 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
23943 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
23944 +#define au_fset_lock(flags, name) \
23945 +       do { (flags) |= AuLock_##name; } while (0)
23946 +#define au_fclr_lock(flags, name) \
23947 +       do { (flags) &= ~AuLock_##name; } while (0)
23948 +
23949 +/* ---------------------------------------------------------------------- */
23950 +
23951 +/* super.c */
23952 +extern struct file_system_type aufs_fs_type;
23953 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
23954 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
23955 +                                          void *arg);
23956 +void au_array_free(void *array);
23957 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
23958 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
23959 +void au_iarray_free(struct inode **a, unsigned long long max);
23960 +
23961 +/* sbinfo.c */
23962 +void au_si_free(struct kobject *kobj);
23963 +int au_si_alloc(struct super_block *sb);
23964 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
23965 +
23966 +unsigned int au_sigen_inc(struct super_block *sb);
23967 +aufs_bindex_t au_new_br_id(struct super_block *sb);
23968 +
23969 +int si_read_lock(struct super_block *sb, int flags);
23970 +int si_write_lock(struct super_block *sb, int flags);
23971 +int aufs_read_lock(struct dentry *dentry, int flags);
23972 +void aufs_read_unlock(struct dentry *dentry, int flags);
23973 +void aufs_write_lock(struct dentry *dentry);
23974 +void aufs_write_unlock(struct dentry *dentry);
23975 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
23976 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
23977 +
23978 +int si_pid_test_slow(struct super_block *sb);
23979 +void si_pid_set_slow(struct super_block *sb);
23980 +void si_pid_clr_slow(struct super_block *sb);
23981 +
23982 +/* wbr_policy.c */
23983 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
23984 +extern struct au_wbr_create_operations au_wbr_create_ops[];
23985 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
23986 +
23987 +/* ---------------------------------------------------------------------- */
23988 +
23989 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
23990 +{
23991 +       return sb->s_fs_info;
23992 +}
23993 +
23994 +/* ---------------------------------------------------------------------- */
23995 +
23996 +#ifdef CONFIG_AUFS_EXPORT
23997 +void au_export_init(struct super_block *sb);
23998 +
23999 +static inline int au_test_nfsd(void)
24000 +{
24001 +       struct task_struct *tsk = current;
24002 +
24003 +       return (tsk->flags & PF_KTHREAD)
24004 +               && !strcmp(tsk->comm, "nfsd");
24005 +}
24006 +
24007 +void au_xigen_inc(struct inode *inode);
24008 +int au_xigen_new(struct inode *inode);
24009 +int au_xigen_set(struct super_block *sb, struct file *base);
24010 +void au_xigen_clr(struct super_block *sb);
24011 +
24012 +static inline int au_busy_or_stale(void)
24013 +{
24014 +       if (!au_test_nfsd())
24015 +               return -EBUSY;
24016 +       return -ESTALE;
24017 +}
24018 +#else
24019 +AuStubVoid(au_export_init, struct super_block *sb)
24020 +AuStubInt0(au_test_nfsd, void)
24021 +AuStubVoid(au_xigen_inc, struct inode *inode)
24022 +AuStubInt0(au_xigen_new, struct inode *inode)
24023 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
24024 +AuStubVoid(au_xigen_clr, struct super_block *sb)
24025 +static inline int au_busy_or_stale(void)
24026 +{
24027 +       return -EBUSY;
24028 +}
24029 +#endif /* CONFIG_AUFS_EXPORT */
24030 +
24031 +/* ---------------------------------------------------------------------- */
24032 +
24033 +#ifdef CONFIG_AUFS_SBILIST
24034 +/* module.c */
24035 +extern struct au_splhead au_sbilist;
24036 +
24037 +static inline void au_sbilist_init(void)
24038 +{
24039 +       au_spl_init(&au_sbilist);
24040 +}
24041 +
24042 +static inline void au_sbilist_add(struct super_block *sb)
24043 +{
24044 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
24045 +}
24046 +
24047 +static inline void au_sbilist_del(struct super_block *sb)
24048 +{
24049 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
24050 +}
24051 +
24052 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
24053 +static inline void au_sbilist_lock(void)
24054 +{
24055 +       spin_lock(&au_sbilist.spin);
24056 +}
24057 +
24058 +static inline void au_sbilist_unlock(void)
24059 +{
24060 +       spin_unlock(&au_sbilist.spin);
24061 +}
24062 +#define AuGFP_SBILIST  GFP_ATOMIC
24063 +#else
24064 +AuStubVoid(au_sbilist_lock, void)
24065 +AuStubVoid(au_sbilist_unlock, void)
24066 +#define AuGFP_SBILIST  GFP_NOFS
24067 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
24068 +#else
24069 +AuStubVoid(au_sbilist_init, void)
24070 +AuStubVoid(au_sbilist_add, struct super_block*)
24071 +AuStubVoid(au_sbilist_del, struct super_block*)
24072 +AuStubVoid(au_sbilist_lock, void)
24073 +AuStubVoid(au_sbilist_unlock, void)
24074 +#define AuGFP_SBILIST  GFP_NOFS
24075 +#endif
24076 +
24077 +/* ---------------------------------------------------------------------- */
24078 +
24079 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
24080 +{
24081 +       /*
24082 +        * This function is a dynamic '__init' fucntion actually,
24083 +        * so the tiny check for si_rwsem is unnecessary.
24084 +        */
24085 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
24086 +#ifdef CONFIG_DEBUG_FS
24087 +       sbinfo->si_dbgaufs = NULL;
24088 +       sbinfo->si_dbgaufs_xib = NULL;
24089 +#ifdef CONFIG_AUFS_EXPORT
24090 +       sbinfo->si_dbgaufs_xigen = NULL;
24091 +#endif
24092 +#endif
24093 +}
24094 +
24095 +/* ---------------------------------------------------------------------- */
24096 +
24097 +static inline pid_t si_pid_bit(void)
24098 +{
24099 +       /* the origin of pid is 1, but the bitmap's is 0 */
24100 +       return current->pid - 1;
24101 +}
24102 +
24103 +static inline int si_pid_test(struct super_block *sb)
24104 +{
24105 +       pid_t bit = si_pid_bit();
24106 +       if (bit < PID_MAX_DEFAULT)
24107 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
24108 +       else
24109 +               return si_pid_test_slow(sb);
24110 +}
24111 +
24112 +static inline void si_pid_set(struct super_block *sb)
24113 +{
24114 +       pid_t bit = si_pid_bit();
24115 +       if (bit < PID_MAX_DEFAULT) {
24116 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
24117 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
24118 +               /* smp_mb(); */
24119 +       } else
24120 +               si_pid_set_slow(sb);
24121 +}
24122 +
24123 +static inline void si_pid_clr(struct super_block *sb)
24124 +{
24125 +       pid_t bit = si_pid_bit();
24126 +       if (bit < PID_MAX_DEFAULT) {
24127 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
24128 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
24129 +               /* smp_mb(); */
24130 +       } else
24131 +               si_pid_clr_slow(sb);
24132 +}
24133 +
24134 +/* ---------------------------------------------------------------------- */
24135 +
24136 +/* lock superblock. mainly for entry point functions */
24137 +/*
24138 + * __si_read_lock, __si_write_lock,
24139 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
24140 + */
24141 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
24142 +
24143 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
24144 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
24145 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
24146 +
24147 +static inline void si_noflush_read_lock(struct super_block *sb)
24148 +{
24149 +       __si_read_lock(sb);
24150 +       si_pid_set(sb);
24151 +}
24152 +
24153 +static inline int si_noflush_read_trylock(struct super_block *sb)
24154 +{
24155 +       int locked = __si_read_trylock(sb);
24156 +       if (locked)
24157 +               si_pid_set(sb);
24158 +       return locked;
24159 +}
24160 +
24161 +static inline void si_noflush_write_lock(struct super_block *sb)
24162 +{
24163 +       __si_write_lock(sb);
24164 +       si_pid_set(sb);
24165 +}
24166 +
24167 +static inline int si_noflush_write_trylock(struct super_block *sb)
24168 +{
24169 +       int locked = __si_write_trylock(sb);
24170 +       if (locked)
24171 +               si_pid_set(sb);
24172 +       return locked;
24173 +}
24174 +
24175 +#if 0 /* unused */
24176 +static inline int si_read_trylock(struct super_block *sb, int flags)
24177 +{
24178 +       if (au_ftest_lock(flags, FLUSH))
24179 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
24180 +       return si_noflush_read_trylock(sb);
24181 +}
24182 +#endif
24183 +
24184 +static inline void si_read_unlock(struct super_block *sb)
24185 +{
24186 +       si_pid_clr(sb);
24187 +       __si_read_unlock(sb);
24188 +}
24189 +
24190 +#if 0 /* unused */
24191 +static inline int si_write_trylock(struct super_block *sb, int flags)
24192 +{
24193 +       if (au_ftest_lock(flags, FLUSH))
24194 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
24195 +       return si_noflush_write_trylock(sb);
24196 +}
24197 +#endif
24198 +
24199 +static inline void si_write_unlock(struct super_block *sb)
24200 +{
24201 +       si_pid_clr(sb);
24202 +       __si_write_unlock(sb);
24203 +}
24204 +
24205 +#if 0 /* unused */
24206 +static inline void si_downgrade_lock(struct super_block *sb)
24207 +{
24208 +       __si_downgrade_lock(sb);
24209 +}
24210 +#endif
24211 +
24212 +/* ---------------------------------------------------------------------- */
24213 +
24214 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
24215 +{
24216 +       SiMustAnyLock(sb);
24217 +       return au_sbi(sb)->si_bend;
24218 +}
24219 +
24220 +static inline unsigned int au_mntflags(struct super_block *sb)
24221 +{
24222 +       SiMustAnyLock(sb);
24223 +       return au_sbi(sb)->si_mntflags;
24224 +}
24225 +
24226 +static inline unsigned int au_sigen(struct super_block *sb)
24227 +{
24228 +       SiMustAnyLock(sb);
24229 +       return au_sbi(sb)->si_generation;
24230 +}
24231 +
24232 +static inline void au_ninodes_inc(struct super_block *sb)
24233 +{
24234 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
24235 +}
24236 +
24237 +static inline void au_ninodes_dec(struct super_block *sb)
24238 +{
24239 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
24240 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
24241 +}
24242 +
24243 +static inline void au_nfiles_inc(struct super_block *sb)
24244 +{
24245 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
24246 +}
24247 +
24248 +static inline void au_nfiles_dec(struct super_block *sb)
24249 +{
24250 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
24251 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
24252 +}
24253 +
24254 +static inline struct au_branch *au_sbr(struct super_block *sb,
24255 +                                      aufs_bindex_t bindex)
24256 +{
24257 +       SiMustAnyLock(sb);
24258 +       return au_sbi(sb)->si_branch[0 + bindex];
24259 +}
24260 +
24261 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
24262 +{
24263 +       SiMustWriteLock(sb);
24264 +       au_sbi(sb)->si_xino_brid = brid;
24265 +}
24266 +
24267 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
24268 +{
24269 +       SiMustAnyLock(sb);
24270 +       return au_sbi(sb)->si_xino_brid;
24271 +}
24272 +
24273 +#endif /* __KERNEL__ */
24274 +#endif /* __AUFS_SUPER_H__ */
24275 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
24276 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
24277 +++ linux/fs/aufs/sysaufs.c     2012-03-19 12:24:13.686467926 +0100
24278 @@ -0,0 +1,105 @@
24279 +/*
24280 + * Copyright (C) 2005-2012 Junjiro R. Okajima
24281 + *
24282 + * This program, aufs is free software; you can redistribute it and/or modify
24283 + * it under the terms of the GNU General Public License as published by
24284 + * the Free Software Foundation; either version 2 of the License, or
24285 + * (at your option) any later version.
24286 + *
24287 + * This program is distributed in the hope that it will be useful,
24288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24290 + * GNU General Public License for more details.
24291 + *
24292 + * You should have received a copy of the GNU General Public License
24293 + * along with this program; if not, write to the Free Software
24294 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24295 + */
24296 +
24297 +/*
24298 + * sysfs interface and lifetime management
24299 + * they are necessary regardless sysfs is disabled.
24300 + */
24301 +
24302 +#include <linux/random.h>
24303 +#include "aufs.h"
24304 +
24305 +unsigned long sysaufs_si_mask;
24306 +struct kset *sysaufs_kset;
24307 +
24308 +#define AuSiAttr(_name) { \
24309 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
24310 +       .show   = sysaufs_si_##_name,                           \
24311 +}
24312 +
24313 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
24314 +struct attribute *sysaufs_si_attrs[] = {
24315 +       &sysaufs_si_attr_xi_path.attr,
24316 +       NULL,
24317 +};
24318 +
24319 +static const struct sysfs_ops au_sbi_ops = {
24320 +       .show   = sysaufs_si_show
24321 +};
24322 +
24323 +static struct kobj_type au_sbi_ktype = {
24324 +       .release        = au_si_free,
24325 +       .sysfs_ops      = &au_sbi_ops,
24326 +       .default_attrs  = sysaufs_si_attrs
24327 +};
24328 +
24329 +/* ---------------------------------------------------------------------- */
24330 +
24331 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
24332 +{
24333 +       int err;
24334 +
24335 +       sbinfo->si_kobj.kset = sysaufs_kset;
24336 +       /* cf. sysaufs_name() */
24337 +       err = kobject_init_and_add
24338 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
24339 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
24340 +
24341 +       dbgaufs_si_null(sbinfo);
24342 +       if (!err) {
24343 +               err = dbgaufs_si_init(sbinfo);
24344 +               if (unlikely(err))
24345 +                       kobject_put(&sbinfo->si_kobj);
24346 +       }
24347 +       return err;
24348 +}
24349 +
24350 +void sysaufs_fin(void)
24351 +{
24352 +       dbgaufs_fin();
24353 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
24354 +       kset_unregister(sysaufs_kset);
24355 +}
24356 +
24357 +int __init sysaufs_init(void)
24358 +{
24359 +       int err;
24360 +
24361 +       do {
24362 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
24363 +       } while (!sysaufs_si_mask);
24364 +
24365 +       err = -EINVAL;
24366 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
24367 +       if (unlikely(!sysaufs_kset))
24368 +               goto out;
24369 +       err = PTR_ERR(sysaufs_kset);
24370 +       if (IS_ERR(sysaufs_kset))
24371 +               goto out;
24372 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
24373 +       if (unlikely(err)) {
24374 +               kset_unregister(sysaufs_kset);
24375 +               goto out;
24376 +       }
24377 +
24378 +       err = dbgaufs_init();
24379 +       if (unlikely(err))
24380 +               sysaufs_fin();
24381 +out:
24382 +       return err;
24383 +}
24384 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
24385 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
24386 +++ linux/fs/aufs/sysaufs.h     2012-03-19 12:24:13.686467926 +0100
24387 @@ -0,0 +1,104 @@
24388 +/*
24389 + * Copyright (C) 2005-2012 Junjiro R. Okajima
24390 + *
24391 + * This program, aufs is free software; you can redistribute it and/or modify
24392 + * it under the terms of the GNU General Public License as published by
24393 + * the Free Software Foundation; either version 2 of the License, or
24394 + * (at your option) any later version.
24395 + *
24396 + * This program is distributed in the hope that it will be useful,
24397 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24398 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24399 + * GNU General Public License for more details.
24400 + *
24401 + * You should have received a copy of the GNU General Public License
24402 + * along with this program; if not, write to the Free Software
24403 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24404 + */
24405 +
24406 +/*
24407 + * sysfs interface and mount lifetime management
24408 + */
24409 +
24410 +#ifndef __SYSAUFS_H__
24411 +#define __SYSAUFS_H__
24412 +
24413 +#ifdef __KERNEL__
24414 +
24415 +#include <linux/sysfs.h>
24416 +#include "module.h"
24417 +
24418 +struct super_block;
24419 +struct au_sbinfo;
24420 +
24421 +struct sysaufs_si_attr {
24422 +       struct attribute attr;
24423 +       int (*show)(struct seq_file *seq, struct super_block *sb);
24424 +};
24425 +
24426 +/* ---------------------------------------------------------------------- */
24427 +
24428 +/* sysaufs.c */
24429 +extern unsigned long sysaufs_si_mask;
24430 +extern struct kset *sysaufs_kset;
24431 +extern struct attribute *sysaufs_si_attrs[];
24432 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
24433 +int __init sysaufs_init(void);
24434 +void sysaufs_fin(void);
24435 +
24436 +/* ---------------------------------------------------------------------- */
24437 +
24438 +/* some people doesn't like to show a pointer in kernel */
24439 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
24440 +{
24441 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
24442 +}
24443 +
24444 +#define SysaufsSiNamePrefix    "si_"
24445 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
24446 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
24447 +{
24448 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
24449 +                sysaufs_si_id(sbinfo));
24450 +}
24451 +
24452 +struct au_branch;
24453 +#ifdef CONFIG_SYSFS
24454 +/* sysfs.c */
24455 +extern struct attribute_group *sysaufs_attr_group;
24456 +
24457 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
24458 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
24459 +                        char *buf);
24460 +
24461 +void sysaufs_br_init(struct au_branch *br);
24462 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
24463 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
24464 +
24465 +#define sysaufs_brs_init()     do {} while (0)
24466 +
24467 +#else
24468 +#define sysaufs_attr_group     NULL
24469 +
24470 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
24471 +
24472 +static inline
24473 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
24474 +                        char *buf)
24475 +{
24476 +       return 0;
24477 +}
24478 +
24479 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
24480 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
24481 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
24482 +
24483 +static inline void sysaufs_brs_init(void)
24484 +{
24485 +       sysaufs_brs = 0;
24486 +}
24487 +
24488 +#endif /* CONFIG_SYSFS */
24489 +
24490 +#endif /* __KERNEL__ */
24491 +#endif /* __SYSAUFS_H__ */
24492 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
24493 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
24494 +++ linux/fs/aufs/sysfs.c       2012-03-19 12:24:13.686467926 +0100
24495 @@ -0,0 +1,257 @@
24496 +/*
24497 + * Copyright (C) 2005-2012 Junjiro R. Okajima
24498 + *
24499 + * This program, aufs is free software; you can redistribute it and/or modify
24500 + * it under the terms of the GNU General Public License as published by
24501 + * the Free Software Foundation; either version 2 of the License, or
24502 + * (at your option) any later version.
24503 + *
24504 + * This program is distributed in the hope that it will be useful,
24505 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24506 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24507 + * GNU General Public License for more details.
24508 + *
24509 + * You should have received a copy of the GNU General Public License
24510 + * along with this program; if not, write to the Free Software
24511 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24512 + */
24513 +
24514 +/*
24515 + * sysfs interface
24516 + */
24517 +
24518 +#include <linux/seq_file.h>
24519 +#include "aufs.h"
24520 +
24521 +#ifdef CONFIG_AUFS_FS_MODULE
24522 +/* this entry violates the "one line per file" policy of sysfs */
24523 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
24524 +                          char *buf)
24525 +{
24526 +       ssize_t err;
24527 +       static char *conf =
24528 +/* this file is generated at compiling */
24529 +#include "conf.str"
24530 +               ;
24531 +
24532 +       err = snprintf(buf, PAGE_SIZE, conf);
24533 +       if (unlikely(err >= PAGE_SIZE))
24534 +               err = -EFBIG;
24535 +       return err;
24536 +}
24537 +
24538 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
24539 +#endif
24540 +
24541 +static struct attribute *au_attr[] = {
24542 +#ifdef CONFIG_AUFS_FS_MODULE
24543 +       &au_config_attr.attr,
24544 +#endif
24545 +       NULL,   /* need to NULL terminate the list of attributes */
24546 +};
24547 +
24548 +static struct attribute_group sysaufs_attr_group_body = {
24549 +       .attrs = au_attr
24550 +};
24551 +
24552 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
24553 +
24554 +/* ---------------------------------------------------------------------- */
24555 +
24556 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
24557 +{
24558 +       int err;
24559 +
24560 +       SiMustAnyLock(sb);
24561 +
24562 +       err = 0;
24563 +       if (au_opt_test(au_mntflags(sb), XINO)) {
24564 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
24565 +               seq_putc(seq, '\n');
24566 +       }
24567 +       return err;
24568 +}
24569 +
24570 +/*
24571 + * the lifetime of branch is independent from the entry under sysfs.
24572 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
24573 + * unlinked.
24574 + */
24575 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
24576 +                        aufs_bindex_t bindex)
24577 +{
24578 +       int err;
24579 +       struct path path;
24580 +       struct dentry *root;
24581 +       struct au_branch *br;
24582 +       char *perm;
24583 +
24584 +       AuDbg("b%d\n", bindex);
24585 +
24586 +       err = 0;
24587 +       root = sb->s_root;
24588 +       di_read_lock_parent(root, !AuLock_IR);
24589 +       br = au_sbr(sb, bindex);
24590 +       path.mnt = br->br_mnt;
24591 +       path.dentry = au_h_dptr(root, bindex);
24592 +       au_seq_path(seq, &path);
24593 +       di_read_unlock(root, !AuLock_IR);
24594 +       perm = au_optstr_br_perm(br->br_perm);
24595 +       if (perm) {
24596 +               err = seq_printf(seq, "=%s\n", perm);
24597 +               kfree(perm);
24598 +               if (err == -1)
24599 +                       err = -E2BIG;
24600 +       } else
24601 +               err = -ENOMEM;
24602 +       return err;
24603 +}
24604 +
24605 +/* ---------------------------------------------------------------------- */
24606 +
24607 +static struct seq_file *au_seq(char *p, ssize_t len)
24608 +{
24609 +       struct seq_file *seq;
24610 +
24611 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
24612 +       if (seq) {
24613 +               /* mutex_init(&seq.lock); */
24614 +               seq->buf = p;
24615 +               seq->size = len;
24616 +               return seq; /* success */
24617 +       }
24618 +
24619 +       seq = ERR_PTR(-ENOMEM);
24620 +       return seq;
24621 +}
24622 +
24623 +#define SysaufsBr_PREFIX "br"
24624 +
24625 +/* todo: file size may exceed PAGE_SIZE */
24626 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
24627 +                       char *buf)
24628 +{
24629 +       ssize_t err;
24630 +       long l;
24631 +       aufs_bindex_t bend;
24632 +       struct au_sbinfo *sbinfo;
24633 +       struct super_block *sb;
24634 +       struct seq_file *seq;
24635 +       char *name;
24636 +       struct attribute **cattr;
24637 +
24638 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
24639 +       sb = sbinfo->si_sb;
24640 +
24641 +       /*
24642 +        * prevent a race condition between sysfs and aufs.
24643 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
24644 +        * prohibits maintaining the sysfs entries.
24645 +        * hew we acquire read lock after sysfs_get_active_two().
24646 +        * on the other hand, the remount process may maintain the sysfs/aufs
24647 +        * entries after acquiring write lock.
24648 +        * it can cause a deadlock.
24649 +        * simply we gave up processing read here.
24650 +        */
24651 +       err = -EBUSY;
24652 +       if (unlikely(!si_noflush_read_trylock(sb)))
24653 +               goto out;
24654 +
24655 +       seq = au_seq(buf, PAGE_SIZE);
24656 +       err = PTR_ERR(seq);
24657 +       if (IS_ERR(seq))
24658 +               goto out_unlock;
24659 +
24660 +       name = (void *)attr->name;
24661 +       cattr = sysaufs_si_attrs;
24662 +       while (*cattr) {
24663 +               if (!strcmp(name, (*cattr)->name)) {
24664 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
24665 +                               ->show(seq, sb);
24666 +                       goto out_seq;
24667 +               }
24668 +               cattr++;
24669 +       }
24670 +
24671 +       bend = au_sbend(sb);
24672 +       if (!strncmp(name, SysaufsBr_PREFIX, sizeof(SysaufsBr_PREFIX) - 1)) {
24673 +               name += sizeof(SysaufsBr_PREFIX) - 1;
24674 +               err = kstrtol(name, 10, &l);
24675 +               if (!err) {
24676 +                       if (l <= bend)
24677 +                               err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l);
24678 +                       else
24679 +                               err = -ENOENT;
24680 +               }
24681 +               goto out_seq;
24682 +       }
24683 +       BUG();
24684 +
24685 +out_seq:
24686 +       if (!err) {
24687 +               err = seq->count;
24688 +               /* sysfs limit */
24689 +               if (unlikely(err == PAGE_SIZE))
24690 +                       err = -EFBIG;
24691 +       }
24692 +       kfree(seq);
24693 +out_unlock:
24694 +       si_read_unlock(sb);
24695 +out:
24696 +       return err;
24697 +}
24698 +
24699 +/* ---------------------------------------------------------------------- */
24700 +
24701 +void sysaufs_br_init(struct au_branch *br)
24702 +{
24703 +       struct attribute *attr = &br->br_attr;
24704 +
24705 +       sysfs_attr_init(attr);
24706 +       attr->name = br->br_name;
24707 +       attr->mode = S_IRUGO;
24708 +}
24709 +
24710 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
24711 +{
24712 +       struct au_branch *br;
24713 +       struct kobject *kobj;
24714 +       aufs_bindex_t bend;
24715 +
24716 +       dbgaufs_brs_del(sb, bindex);
24717 +
24718 +       if (!sysaufs_brs)
24719 +               return;
24720 +
24721 +       kobj = &au_sbi(sb)->si_kobj;
24722 +       bend = au_sbend(sb);
24723 +       for (; bindex <= bend; bindex++) {
24724 +               br = au_sbr(sb, bindex);
24725 +               sysfs_remove_file(kobj, &br->br_attr);
24726 +       }
24727 +}
24728 +
24729 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
24730 +{
24731 +       int err;
24732 +       aufs_bindex_t bend;
24733 +       struct kobject *kobj;
24734 +       struct au_branch *br;
24735 +
24736 +       dbgaufs_brs_add(sb, bindex);
24737 +
24738 +       if (!sysaufs_brs)
24739 +               return;
24740 +
24741 +       kobj = &au_sbi(sb)->si_kobj;
24742 +       bend = au_sbend(sb);
24743 +       for (; bindex <= bend; bindex++) {
24744 +               br = au_sbr(sb, bindex);
24745 +               snprintf(br->br_name, sizeof(br->br_name), SysaufsBr_PREFIX
24746 +                        "%d", bindex);
24747 +               err = sysfs_create_file(kobj, &br->br_attr);
24748 +               if (unlikely(err))
24749 +                       pr_warning("failed %s under sysfs(%d)\n",
24750 +                                  br->br_name, err);
24751 +       }
24752 +}
24753 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
24754 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
24755 +++ linux/fs/aufs/sysrq.c       2012-03-19 12:24:13.686467926 +0100
24756 @@ -0,0 +1,148 @@
24757 +/*
24758 + * Copyright (C) 2005-2012 Junjiro R. Okajima
24759 + *
24760 + * This program, aufs is free software; you can redistribute it and/or modify
24761 + * it under the terms of the GNU General Public License as published by
24762 + * the Free Software Foundation; either version 2 of the License, or
24763 + * (at your option) any later version.
24764 + *
24765 + * This program is distributed in the hope that it will be useful,
24766 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24767 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24768 + * GNU General Public License for more details.
24769 + *
24770 + * You should have received a copy of the GNU General Public License
24771 + * along with this program; if not, write to the Free Software
24772 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24773 + */
24774 +
24775 +/*
24776 + * magic sysrq hanlder
24777 + */
24778 +
24779 +/* #include <linux/sysrq.h> */
24780 +#include <linux/writeback.h>
24781 +#include "aufs.h"
24782 +
24783 +/* ---------------------------------------------------------------------- */
24784 +
24785 +static void sysrq_sb(struct super_block *sb)
24786 +{
24787 +       char *plevel;
24788 +       struct au_sbinfo *sbinfo;
24789 +       struct file *file;
24790 +
24791 +       plevel = au_plevel;
24792 +       au_plevel = KERN_WARNING;
24793 +
24794 +       sbinfo = au_sbi(sb);
24795 +       /* since we define pr_fmt, call printk directly */
24796 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
24797 +       printk(KERN_WARNING AUFS_NAME ": superblock\n");
24798 +       au_dpri_sb(sb);
24799 +
24800 +#if 0
24801 +       printk(KERN_WARNING AUFS_NAME ": root dentry\n");
24802 +       au_dpri_dentry(sb->s_root);
24803 +       printk(KERN_WARNING AUFS_NAME ": root inode\n");
24804 +       au_dpri_inode(sb->s_root->d_inode);
24805 +#endif
24806 +
24807 +#if 0
24808 +       do {
24809 +               int err, i, j, ndentry;
24810 +               struct au_dcsub_pages dpages;
24811 +               struct au_dpage *dpage;
24812 +
24813 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
24814 +               if (unlikely(err))
24815 +                       break;
24816 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
24817 +               if (!err)
24818 +                       for (i = 0; i < dpages.ndpage; i++) {
24819 +                               dpage = dpages.dpages + i;
24820 +                               ndentry = dpage->ndentry;
24821 +                               for (j = 0; j < ndentry; j++)
24822 +                                       au_dpri_dentry(dpage->dentries[j]);
24823 +                       }
24824 +               au_dpages_free(&dpages);
24825 +       } while (0);
24826 +#endif
24827 +
24828 +#if 1
24829 +       {
24830 +               struct inode *i;
24831 +               printk(KERN_WARNING AUFS_NAME ": isolated inode\n");
24832 +               spin_lock(&inode_sb_list_lock);
24833 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
24834 +                       spin_lock(&i->i_lock);
24835 +                       if (1 || list_empty(&i->i_dentry))
24836 +                               au_dpri_inode(i);
24837 +                       spin_unlock(&i->i_lock);
24838 +               }
24839 +               spin_unlock(&inode_sb_list_lock);
24840 +       }
24841 +#endif
24842 +       printk(KERN_WARNING AUFS_NAME ": files\n");
24843 +       lg_global_lock(files_lglock);
24844 +       do_file_list_for_each_entry(sb, file) {
24845 +               umode_t mode;
24846 +               mode = file->f_dentry->d_inode->i_mode;
24847 +               if (!special_file(mode) || au_special_file(mode))
24848 +                       au_dpri_file(file);
24849 +       } while_file_list_for_each_entry;
24850 +       lg_global_unlock(files_lglock);
24851 +       printk(KERN_WARNING AUFS_NAME ": done\n");
24852 +
24853 +       au_plevel = plevel;
24854 +}
24855 +
24856 +/* ---------------------------------------------------------------------- */
24857 +
24858 +/* module parameter */
24859 +static char *aufs_sysrq_key = "a";
24860 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
24861 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
24862 +
24863 +static void au_sysrq(int key __maybe_unused)
24864 +{
24865 +       struct au_sbinfo *sbinfo;
24866 +
24867 +       lockdep_off();
24868 +       au_sbilist_lock();
24869 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
24870 +               sysrq_sb(sbinfo->si_sb);
24871 +       au_sbilist_unlock();
24872 +       lockdep_on();
24873 +}
24874 +
24875 +static struct sysrq_key_op au_sysrq_op = {
24876 +       .handler        = au_sysrq,
24877 +       .help_msg       = "Aufs",
24878 +       .action_msg     = "Aufs",
24879 +       .enable_mask    = SYSRQ_ENABLE_DUMP
24880 +};
24881 +
24882 +/* ---------------------------------------------------------------------- */
24883 +
24884 +int __init au_sysrq_init(void)
24885 +{
24886 +       int err;
24887 +       char key;
24888 +
24889 +       err = -1;
24890 +       key = *aufs_sysrq_key;
24891 +       if ('a' <= key && key <= 'z')
24892 +               err = register_sysrq_key(key, &au_sysrq_op);
24893 +       if (unlikely(err))
24894 +               pr_err("err %d, sysrq=%c\n", err, key);
24895 +       return err;
24896 +}
24897 +
24898 +void au_sysrq_fin(void)
24899 +{
24900 +       int err;
24901 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
24902 +       if (unlikely(err))
24903 +               pr_err("err %d (ignored)\n", err);
24904 +}
24905 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
24906 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
24907 +++ linux/fs/aufs/vdir.c        2012-03-19 12:24:13.686467926 +0100
24908 @@ -0,0 +1,885 @@
24909 +/*
24910 + * Copyright (C) 2005-2012 Junjiro R. Okajima
24911 + *
24912 + * This program, aufs is free software; you can redistribute it and/or modify
24913 + * it under the terms of the GNU General Public License as published by
24914 + * the Free Software Foundation; either version 2 of the License, or
24915 + * (at your option) any later version.
24916 + *
24917 + * This program is distributed in the hope that it will be useful,
24918 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24919 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24920 + * GNU General Public License for more details.
24921 + *
24922 + * You should have received a copy of the GNU General Public License
24923 + * along with this program; if not, write to the Free Software
24924 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24925 + */
24926 +
24927 +/*
24928 + * virtual or vertical directory
24929 + */
24930 +
24931 +#include "aufs.h"
24932 +
24933 +static unsigned int calc_size(int nlen)
24934 +{
24935 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
24936 +}
24937 +
24938 +static int set_deblk_end(union au_vdir_deblk_p *p,
24939 +                        union au_vdir_deblk_p *deblk_end)
24940 +{
24941 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
24942 +               p->de->de_str.len = 0;
24943 +               /* smp_mb(); */
24944 +               return 0;
24945 +       }
24946 +       return -1; /* error */
24947 +}
24948 +
24949 +/* returns true or false */
24950 +static int is_deblk_end(union au_vdir_deblk_p *p,
24951 +                       union au_vdir_deblk_p *deblk_end)
24952 +{
24953 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
24954 +               return !p->de->de_str.len;
24955 +       return 1;
24956 +}
24957 +
24958 +static unsigned char *last_deblk(struct au_vdir *vdir)
24959 +{
24960 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
24961 +}
24962 +
24963 +/* ---------------------------------------------------------------------- */
24964 +
24965 +/* estimate the apropriate size for name hash table */
24966 +unsigned int au_rdhash_est(loff_t sz)
24967 +{
24968 +       unsigned int n;
24969 +
24970 +       n = UINT_MAX;
24971 +       sz >>= 10;
24972 +       if (sz < n)
24973 +               n = sz;
24974 +       if (sz < AUFS_RDHASH_DEF)
24975 +               n = AUFS_RDHASH_DEF;
24976 +       /* pr_info("n %u\n", n); */
24977 +       return n;
24978 +}
24979 +
24980 +/*
24981 + * the allocated memory has to be freed by
24982 + * au_nhash_wh_free() or au_nhash_de_free().
24983 + */
24984 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
24985 +{
24986 +       struct hlist_head *head;
24987 +       unsigned int u;
24988 +
24989 +       head = kmalloc(sizeof(*nhash->nh_head) * num_hash, gfp);
24990 +       if (head) {
24991 +               nhash->nh_num = num_hash;
24992 +               nhash->nh_head = head;
24993 +               for (u = 0; u < num_hash; u++)
24994 +                       INIT_HLIST_HEAD(head++);
24995 +               return 0; /* success */
24996 +       }
24997 +
24998 +       return -ENOMEM;
24999 +}
25000 +
25001 +static void nhash_count(struct hlist_head *head)
25002 +{
25003 +#if 0
25004 +       unsigned long n;
25005 +       struct hlist_node *pos;
25006 +
25007 +       n = 0;
25008 +       hlist_for_each(pos, head)
25009 +               n++;
25010 +       pr_info("%lu\n", n);
25011 +#endif
25012 +}
25013 +
25014 +static void au_nhash_wh_do_free(struct hlist_head *head)
25015 +{
25016 +       struct au_vdir_wh *tpos;
25017 +       struct hlist_node *pos, *node;
25018 +
25019 +       hlist_for_each_entry_safe(tpos, pos, node, head, wh_hash) {
25020 +               /* hlist_del(pos); */
25021 +               kfree(tpos);
25022 +       }
25023 +}
25024 +
25025 +static void au_nhash_de_do_free(struct hlist_head *head)
25026 +{
25027 +       struct au_vdir_dehstr *tpos;
25028 +       struct hlist_node *pos, *node;
25029 +
25030 +       hlist_for_each_entry_safe(tpos, pos, node, head, hash) {
25031 +               /* hlist_del(pos); */
25032 +               au_cache_free_vdir_dehstr(tpos);
25033 +       }
25034 +}
25035 +
25036 +static void au_nhash_do_free(struct au_nhash *nhash,
25037 +                            void (*free)(struct hlist_head *head))
25038 +{
25039 +       unsigned int n;
25040 +       struct hlist_head *head;
25041 +
25042 +       n = nhash->nh_num;
25043 +       if (!n)
25044 +               return;
25045 +
25046 +       head = nhash->nh_head;
25047 +       while (n-- > 0) {
25048 +               nhash_count(head);
25049 +               free(head++);
25050 +       }
25051 +       kfree(nhash->nh_head);
25052 +}
25053 +
25054 +void au_nhash_wh_free(struct au_nhash *whlist)
25055 +{
25056 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
25057 +}
25058 +
25059 +static void au_nhash_de_free(struct au_nhash *delist)
25060 +{
25061 +       au_nhash_do_free(delist, au_nhash_de_do_free);
25062 +}
25063 +
25064 +/* ---------------------------------------------------------------------- */
25065 +
25066 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
25067 +                           int limit)
25068 +{
25069 +       int num;
25070 +       unsigned int u, n;
25071 +       struct hlist_head *head;
25072 +       struct au_vdir_wh *tpos;
25073 +       struct hlist_node *pos;
25074 +
25075 +       num = 0;
25076 +       n = whlist->nh_num;
25077 +       head = whlist->nh_head;
25078 +       for (u = 0; u < n; u++, head++)
25079 +               hlist_for_each_entry(tpos, pos, head, wh_hash)
25080 +                       if (tpos->wh_bindex == btgt && ++num > limit)
25081 +                               return 1;
25082 +       return 0;
25083 +}
25084 +
25085 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
25086 +                                      unsigned char *name,
25087 +                                      unsigned int len)
25088 +{
25089 +       unsigned int v;
25090 +       /* const unsigned int magic_bit = 12; */
25091 +
25092 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
25093 +
25094 +       v = 0;
25095 +       while (len--)
25096 +               v += *name++;
25097 +       /* v = hash_long(v, magic_bit); */
25098 +       v %= nhash->nh_num;
25099 +       return nhash->nh_head + v;
25100 +}
25101 +
25102 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
25103 +                             int nlen)
25104 +{
25105 +       return str->len == nlen && !memcmp(str->name, name, nlen);
25106 +}
25107 +
25108 +/* returns found or not */
25109 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
25110 +{
25111 +       struct hlist_head *head;
25112 +       struct au_vdir_wh *tpos;
25113 +       struct hlist_node *pos;
25114 +       struct au_vdir_destr *str;
25115 +
25116 +       head = au_name_hash(whlist, name, nlen);
25117 +       hlist_for_each_entry(tpos, pos, head, wh_hash) {
25118 +               str = &tpos->wh_str;
25119 +               AuDbg("%.*s\n", str->len, str->name);
25120 +               if (au_nhash_test_name(str, name, nlen))
25121 +                       return 1;
25122 +       }
25123 +       return 0;
25124 +}
25125 +
25126 +/* returns found(true) or not */
25127 +static int test_known(struct au_nhash *delist, char *name, int nlen)
25128 +{
25129 +       struct hlist_head *head;
25130 +       struct au_vdir_dehstr *tpos;
25131 +       struct hlist_node *pos;
25132 +       struct au_vdir_destr *str;
25133 +
25134 +       head = au_name_hash(delist, name, nlen);
25135 +       hlist_for_each_entry(tpos, pos, head, hash) {
25136 +               str = tpos->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 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
25145 +                           unsigned char d_type)
25146 +{
25147 +#ifdef CONFIG_AUFS_SHWH
25148 +       wh->wh_ino = ino;
25149 +       wh->wh_type = d_type;
25150 +#endif
25151 +}
25152 +
25153 +/* ---------------------------------------------------------------------- */
25154 +
25155 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
25156 +                      unsigned int d_type, aufs_bindex_t bindex,
25157 +                      unsigned char shwh)
25158 +{
25159 +       int err;
25160 +       struct au_vdir_destr *str;
25161 +       struct au_vdir_wh *wh;
25162 +
25163 +       AuDbg("%.*s\n", nlen, name);
25164 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
25165 +
25166 +       err = -ENOMEM;
25167 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
25168 +       if (unlikely(!wh))
25169 +               goto out;
25170 +
25171 +       err = 0;
25172 +       wh->wh_bindex = bindex;
25173 +       if (shwh)
25174 +               au_shwh_init_wh(wh, ino, d_type);
25175 +       str = &wh->wh_str;
25176 +       str->len = nlen;
25177 +       memcpy(str->name, name, nlen);
25178 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
25179 +       /* smp_mb(); */
25180 +
25181 +out:
25182 +       return err;
25183 +}
25184 +
25185 +static int append_deblk(struct au_vdir *vdir)
25186 +{
25187 +       int err;
25188 +       unsigned long ul;
25189 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
25190 +       union au_vdir_deblk_p p, deblk_end;
25191 +       unsigned char **o;
25192 +
25193 +       err = -ENOMEM;
25194 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
25195 +                    GFP_NOFS);
25196 +       if (unlikely(!o))
25197 +               goto out;
25198 +
25199 +       vdir->vd_deblk = o;
25200 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
25201 +       if (p.deblk) {
25202 +               ul = vdir->vd_nblk++;
25203 +               vdir->vd_deblk[ul] = p.deblk;
25204 +               vdir->vd_last.ul = ul;
25205 +               vdir->vd_last.p.deblk = p.deblk;
25206 +               deblk_end.deblk = p.deblk + deblk_sz;
25207 +               err = set_deblk_end(&p, &deblk_end);
25208 +       }
25209 +
25210 +out:
25211 +       return err;
25212 +}
25213 +
25214 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
25215 +                    unsigned int d_type, struct au_nhash *delist)
25216 +{
25217 +       int err;
25218 +       unsigned int sz;
25219 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
25220 +       union au_vdir_deblk_p p, *room, deblk_end;
25221 +       struct au_vdir_dehstr *dehstr;
25222 +
25223 +       p.deblk = last_deblk(vdir);
25224 +       deblk_end.deblk = p.deblk + deblk_sz;
25225 +       room = &vdir->vd_last.p;
25226 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
25227 +                 || !is_deblk_end(room, &deblk_end));
25228 +
25229 +       sz = calc_size(nlen);
25230 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
25231 +               err = append_deblk(vdir);
25232 +               if (unlikely(err))
25233 +                       goto out;
25234 +
25235 +               p.deblk = last_deblk(vdir);
25236 +               deblk_end.deblk = p.deblk + deblk_sz;
25237 +               /* smp_mb(); */
25238 +               AuDebugOn(room->deblk != p.deblk);
25239 +       }
25240 +
25241 +       err = -ENOMEM;
25242 +       dehstr = au_cache_alloc_vdir_dehstr();
25243 +       if (unlikely(!dehstr))
25244 +               goto out;
25245 +
25246 +       dehstr->str = &room->de->de_str;
25247 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
25248 +       room->de->de_ino = ino;
25249 +       room->de->de_type = d_type;
25250 +       room->de->de_str.len = nlen;
25251 +       memcpy(room->de->de_str.name, name, nlen);
25252 +
25253 +       err = 0;
25254 +       room->deblk += sz;
25255 +       if (unlikely(set_deblk_end(room, &deblk_end)))
25256 +               err = append_deblk(vdir);
25257 +       /* smp_mb(); */
25258 +
25259 +out:
25260 +       return err;
25261 +}
25262 +
25263 +/* ---------------------------------------------------------------------- */
25264 +
25265 +void au_vdir_free(struct au_vdir *vdir)
25266 +{
25267 +       unsigned char **deblk;
25268 +
25269 +       deblk = vdir->vd_deblk;
25270 +       while (vdir->vd_nblk--)
25271 +               kfree(*deblk++);
25272 +       kfree(vdir->vd_deblk);
25273 +       au_cache_free_vdir(vdir);
25274 +}
25275 +
25276 +static struct au_vdir *alloc_vdir(struct file *file)
25277 +{
25278 +       struct au_vdir *vdir;
25279 +       struct super_block *sb;
25280 +       int err;
25281 +
25282 +       sb = file->f_dentry->d_sb;
25283 +       SiMustAnyLock(sb);
25284 +
25285 +       err = -ENOMEM;
25286 +       vdir = au_cache_alloc_vdir();
25287 +       if (unlikely(!vdir))
25288 +               goto out;
25289 +
25290 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
25291 +       if (unlikely(!vdir->vd_deblk))
25292 +               goto out_free;
25293 +
25294 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
25295 +       if (!vdir->vd_deblk_sz) {
25296 +               /* estimate the apropriate size for deblk */
25297 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
25298 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
25299 +       }
25300 +       vdir->vd_nblk = 0;
25301 +       vdir->vd_version = 0;
25302 +       vdir->vd_jiffy = 0;
25303 +       err = append_deblk(vdir);
25304 +       if (!err)
25305 +               return vdir; /* success */
25306 +
25307 +       kfree(vdir->vd_deblk);
25308 +
25309 +out_free:
25310 +       au_cache_free_vdir(vdir);
25311 +out:
25312 +       vdir = ERR_PTR(err);
25313 +       return vdir;
25314 +}
25315 +
25316 +static int reinit_vdir(struct au_vdir *vdir)
25317 +{
25318 +       int err;
25319 +       union au_vdir_deblk_p p, deblk_end;
25320 +
25321 +       while (vdir->vd_nblk > 1) {
25322 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
25323 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
25324 +               vdir->vd_nblk--;
25325 +       }
25326 +       p.deblk = vdir->vd_deblk[0];
25327 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
25328 +       err = set_deblk_end(&p, &deblk_end);
25329 +       /* keep vd_dblk_sz */
25330 +       vdir->vd_last.ul = 0;
25331 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
25332 +       vdir->vd_version = 0;
25333 +       vdir->vd_jiffy = 0;
25334 +       /* smp_mb(); */
25335 +       return err;
25336 +}
25337 +
25338 +/* ---------------------------------------------------------------------- */
25339 +
25340 +#define AuFillVdir_CALLED      1
25341 +#define AuFillVdir_WHABLE      (1 << 1)
25342 +#define AuFillVdir_SHWH                (1 << 2)
25343 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
25344 +#define au_fset_fillvdir(flags, name) \
25345 +       do { (flags) |= AuFillVdir_##name; } while (0)
25346 +#define au_fclr_fillvdir(flags, name) \
25347 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
25348 +
25349 +#ifndef CONFIG_AUFS_SHWH
25350 +#undef AuFillVdir_SHWH
25351 +#define AuFillVdir_SHWH                0
25352 +#endif
25353 +
25354 +struct fillvdir_arg {
25355 +       struct file             *file;
25356 +       struct au_vdir          *vdir;
25357 +       struct au_nhash         delist;
25358 +       struct au_nhash         whlist;
25359 +       aufs_bindex_t           bindex;
25360 +       unsigned int            flags;
25361 +       int                     err;
25362 +};
25363 +
25364 +static int fillvdir(void *__arg, const char *__name, int nlen,
25365 +                   loff_t offset __maybe_unused, u64 h_ino,
25366 +                   unsigned int d_type)
25367 +{
25368 +       struct fillvdir_arg *arg = __arg;
25369 +       char *name = (void *)__name;
25370 +       struct super_block *sb;
25371 +       ino_t ino;
25372 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
25373 +
25374 +       arg->err = 0;
25375 +       sb = arg->file->f_dentry->d_sb;
25376 +       au_fset_fillvdir(arg->flags, CALLED);
25377 +       /* smp_mb(); */
25378 +       if (nlen <= AUFS_WH_PFX_LEN
25379 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
25380 +               if (test_known(&arg->delist, name, nlen)
25381 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
25382 +                       goto out; /* already exists or whiteouted */
25383 +
25384 +               sb = arg->file->f_dentry->d_sb;
25385 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
25386 +               if (!arg->err) {
25387 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
25388 +                               d_type = DT_UNKNOWN;
25389 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
25390 +                                            d_type, &arg->delist);
25391 +               }
25392 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
25393 +               name += AUFS_WH_PFX_LEN;
25394 +               nlen -= AUFS_WH_PFX_LEN;
25395 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
25396 +                       goto out; /* already whiteouted */
25397 +
25398 +               if (shwh)
25399 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
25400 +                                            &ino);
25401 +               if (!arg->err) {
25402 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
25403 +                               d_type = DT_UNKNOWN;
25404 +                       arg->err = au_nhash_append_wh
25405 +                               (&arg->whlist, name, nlen, ino, d_type,
25406 +                                arg->bindex, shwh);
25407 +               }
25408 +       }
25409 +
25410 +out:
25411 +       if (!arg->err)
25412 +               arg->vdir->vd_jiffy = jiffies;
25413 +       /* smp_mb(); */
25414 +       AuTraceErr(arg->err);
25415 +       return arg->err;
25416 +}
25417 +
25418 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
25419 +                         struct au_nhash *whlist, struct au_nhash *delist)
25420 +{
25421 +#ifdef CONFIG_AUFS_SHWH
25422 +       int err;
25423 +       unsigned int nh, u;
25424 +       struct hlist_head *head;
25425 +       struct au_vdir_wh *tpos;
25426 +       struct hlist_node *pos, *n;
25427 +       char *p, *o;
25428 +       struct au_vdir_destr *destr;
25429 +
25430 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
25431 +
25432 +       err = -ENOMEM;
25433 +       o = p = __getname_gfp(GFP_NOFS);
25434 +       if (unlikely(!p))
25435 +               goto out;
25436 +
25437 +       err = 0;
25438 +       nh = whlist->nh_num;
25439 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
25440 +       p += AUFS_WH_PFX_LEN;
25441 +       for (u = 0; u < nh; u++) {
25442 +               head = whlist->nh_head + u;
25443 +               hlist_for_each_entry_safe(tpos, pos, n, head, wh_hash) {
25444 +                       destr = &tpos->wh_str;
25445 +                       memcpy(p, destr->name, destr->len);
25446 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
25447 +                                       tpos->wh_ino, tpos->wh_type, delist);
25448 +                       if (unlikely(err))
25449 +                               break;
25450 +               }
25451 +       }
25452 +
25453 +       __putname(o);
25454 +
25455 +out:
25456 +       AuTraceErr(err);
25457 +       return err;
25458 +#else
25459 +       return 0;
25460 +#endif
25461 +}
25462 +
25463 +static int au_do_read_vdir(struct fillvdir_arg *arg)
25464 +{
25465 +       int err;
25466 +       unsigned int rdhash;
25467 +       loff_t offset;
25468 +       aufs_bindex_t bend, bindex, bstart;
25469 +       unsigned char shwh;
25470 +       struct file *hf, *file;
25471 +       struct super_block *sb;
25472 +
25473 +       file = arg->file;
25474 +       sb = file->f_dentry->d_sb;
25475 +       SiMustAnyLock(sb);
25476 +
25477 +       rdhash = au_sbi(sb)->si_rdhash;
25478 +       if (!rdhash)
25479 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
25480 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
25481 +       if (unlikely(err))
25482 +               goto out;
25483 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
25484 +       if (unlikely(err))
25485 +               goto out_delist;
25486 +
25487 +       err = 0;
25488 +       arg->flags = 0;
25489 +       shwh = 0;
25490 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
25491 +               shwh = 1;
25492 +               au_fset_fillvdir(arg->flags, SHWH);
25493 +       }
25494 +       bstart = au_fbstart(file);
25495 +       bend = au_fbend_dir(file);
25496 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
25497 +               hf = au_hf_dir(file, bindex);
25498 +               if (!hf)
25499 +                       continue;
25500 +
25501 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
25502 +               err = offset;
25503 +               if (unlikely(offset))
25504 +                       break;
25505 +
25506 +               arg->bindex = bindex;
25507 +               au_fclr_fillvdir(arg->flags, WHABLE);
25508 +               if (shwh
25509 +                   || (bindex != bend
25510 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
25511 +                       au_fset_fillvdir(arg->flags, WHABLE);
25512 +               do {
25513 +                       arg->err = 0;
25514 +                       au_fclr_fillvdir(arg->flags, CALLED);
25515 +                       /* smp_mb(); */
25516 +                       err = vfsub_readdir(hf, fillvdir, arg);
25517 +                       if (err >= 0)
25518 +                               err = arg->err;
25519 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
25520 +       }
25521 +
25522 +       if (!err && shwh)
25523 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
25524 +
25525 +       au_nhash_wh_free(&arg->whlist);
25526 +
25527 +out_delist:
25528 +       au_nhash_de_free(&arg->delist);
25529 +out:
25530 +       return err;
25531 +}
25532 +
25533 +static int read_vdir(struct file *file, int may_read)
25534 +{
25535 +       int err;
25536 +       unsigned long expire;
25537 +       unsigned char do_read;
25538 +       struct fillvdir_arg arg;
25539 +       struct inode *inode;
25540 +       struct au_vdir *vdir, *allocated;
25541 +
25542 +       err = 0;
25543 +       inode = file->f_dentry->d_inode;
25544 +       IMustLock(inode);
25545 +       SiMustAnyLock(inode->i_sb);
25546 +
25547 +       allocated = NULL;
25548 +       do_read = 0;
25549 +       expire = au_sbi(inode->i_sb)->si_rdcache;
25550 +       vdir = au_ivdir(inode);
25551 +       if (!vdir) {
25552 +               do_read = 1;
25553 +               vdir = alloc_vdir(file);
25554 +               err = PTR_ERR(vdir);
25555 +               if (IS_ERR(vdir))
25556 +                       goto out;
25557 +               err = 0;
25558 +               allocated = vdir;
25559 +       } else if (may_read
25560 +                  && (inode->i_version != vdir->vd_version
25561 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
25562 +               do_read = 1;
25563 +               err = reinit_vdir(vdir);
25564 +               if (unlikely(err))
25565 +                       goto out;
25566 +       }
25567 +
25568 +       if (!do_read)
25569 +               return 0; /* success */
25570 +
25571 +       arg.file = file;
25572 +       arg.vdir = vdir;
25573 +       err = au_do_read_vdir(&arg);
25574 +       if (!err) {
25575 +               /* file->f_pos = 0; */
25576 +               vdir->vd_version = inode->i_version;
25577 +               vdir->vd_last.ul = 0;
25578 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
25579 +               if (allocated)
25580 +                       au_set_ivdir(inode, allocated);
25581 +       } else if (allocated)
25582 +               au_vdir_free(allocated);
25583 +
25584 +out:
25585 +       return err;
25586 +}
25587 +
25588 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
25589 +{
25590 +       int err, rerr;
25591 +       unsigned long ul, n;
25592 +       const unsigned int deblk_sz = src->vd_deblk_sz;
25593 +
25594 +       AuDebugOn(tgt->vd_nblk != 1);
25595 +
25596 +       err = -ENOMEM;
25597 +       if (tgt->vd_nblk < src->vd_nblk) {
25598 +               unsigned char **p;
25599 +
25600 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
25601 +                            GFP_NOFS);
25602 +               if (unlikely(!p))
25603 +                       goto out;
25604 +               tgt->vd_deblk = p;
25605 +       }
25606 +
25607 +       if (tgt->vd_deblk_sz != deblk_sz) {
25608 +               unsigned char *p;
25609 +
25610 +               tgt->vd_deblk_sz = deblk_sz;
25611 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
25612 +               if (unlikely(!p))
25613 +                       goto out;
25614 +               tgt->vd_deblk[0] = p;
25615 +       }
25616 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
25617 +       tgt->vd_version = src->vd_version;
25618 +       tgt->vd_jiffy = src->vd_jiffy;
25619 +
25620 +       n = src->vd_nblk;
25621 +       for (ul = 1; ul < n; ul++) {
25622 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
25623 +                                           GFP_NOFS);
25624 +               if (unlikely(!tgt->vd_deblk[ul]))
25625 +                       goto out;
25626 +               tgt->vd_nblk++;
25627 +       }
25628 +       tgt->vd_nblk = n;
25629 +       tgt->vd_last.ul = tgt->vd_last.ul;
25630 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
25631 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
25632 +               - src->vd_deblk[src->vd_last.ul];
25633 +       /* smp_mb(); */
25634 +       return 0; /* success */
25635 +
25636 +out:
25637 +       rerr = reinit_vdir(tgt);
25638 +       BUG_ON(rerr);
25639 +       return err;
25640 +}
25641 +
25642 +int au_vdir_init(struct file *file)
25643 +{
25644 +       int err;
25645 +       struct inode *inode;
25646 +       struct au_vdir *vdir_cache, *allocated;
25647 +
25648 +       err = read_vdir(file, !file->f_pos);
25649 +       if (unlikely(err))
25650 +               goto out;
25651 +
25652 +       allocated = NULL;
25653 +       vdir_cache = au_fvdir_cache(file);
25654 +       if (!vdir_cache) {
25655 +               vdir_cache = alloc_vdir(file);
25656 +               err = PTR_ERR(vdir_cache);
25657 +               if (IS_ERR(vdir_cache))
25658 +                       goto out;
25659 +               allocated = vdir_cache;
25660 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
25661 +               err = reinit_vdir(vdir_cache);
25662 +               if (unlikely(err))
25663 +                       goto out;
25664 +       } else
25665 +               return 0; /* success */
25666 +
25667 +       inode = file->f_dentry->d_inode;
25668 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
25669 +       if (!err) {
25670 +               file->f_version = inode->i_version;
25671 +               if (allocated)
25672 +                       au_set_fvdir_cache(file, allocated);
25673 +       } else if (allocated)
25674 +               au_vdir_free(allocated);
25675 +
25676 +out:
25677 +       return err;
25678 +}
25679 +
25680 +static loff_t calc_offset(struct au_vdir *vdir)
25681 +{
25682 +       loff_t offset;
25683 +       union au_vdir_deblk_p p;
25684 +
25685 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
25686 +       offset = vdir->vd_last.p.deblk - p.deblk;
25687 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
25688 +       return offset;
25689 +}
25690 +
25691 +/* returns true or false */
25692 +static int seek_vdir(struct file *file)
25693 +{
25694 +       int valid;
25695 +       unsigned int deblk_sz;
25696 +       unsigned long ul, n;
25697 +       loff_t offset;
25698 +       union au_vdir_deblk_p p, deblk_end;
25699 +       struct au_vdir *vdir_cache;
25700 +
25701 +       valid = 1;
25702 +       vdir_cache = au_fvdir_cache(file);
25703 +       offset = calc_offset(vdir_cache);
25704 +       AuDbg("offset %lld\n", offset);
25705 +       if (file->f_pos == offset)
25706 +               goto out;
25707 +
25708 +       vdir_cache->vd_last.ul = 0;
25709 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
25710 +       if (!file->f_pos)
25711 +               goto out;
25712 +
25713 +       valid = 0;
25714 +       deblk_sz = vdir_cache->vd_deblk_sz;
25715 +       ul = div64_u64(file->f_pos, deblk_sz);
25716 +       AuDbg("ul %lu\n", ul);
25717 +       if (ul >= vdir_cache->vd_nblk)
25718 +               goto out;
25719 +
25720 +       n = vdir_cache->vd_nblk;
25721 +       for (; ul < n; ul++) {
25722 +               p.deblk = vdir_cache->vd_deblk[ul];
25723 +               deblk_end.deblk = p.deblk + deblk_sz;
25724 +               offset = ul;
25725 +               offset *= deblk_sz;
25726 +               while (!is_deblk_end(&p, &deblk_end) && offset < file->f_pos) {
25727 +                       unsigned int l;
25728 +
25729 +                       l = calc_size(p.de->de_str.len);
25730 +                       offset += l;
25731 +                       p.deblk += l;
25732 +               }
25733 +               if (!is_deblk_end(&p, &deblk_end)) {
25734 +                       valid = 1;
25735 +                       vdir_cache->vd_last.ul = ul;
25736 +                       vdir_cache->vd_last.p = p;
25737 +                       break;
25738 +               }
25739 +       }
25740 +
25741 +out:
25742 +       /* smp_mb(); */
25743 +       AuTraceErr(!valid);
25744 +       return valid;
25745 +}
25746 +
25747 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir)
25748 +{
25749 +       int err;
25750 +       unsigned int l, deblk_sz;
25751 +       union au_vdir_deblk_p deblk_end;
25752 +       struct au_vdir *vdir_cache;
25753 +       struct au_vdir_de *de;
25754 +
25755 +       vdir_cache = au_fvdir_cache(file);
25756 +       if (!seek_vdir(file))
25757 +               return 0;
25758 +
25759 +       deblk_sz = vdir_cache->vd_deblk_sz;
25760 +       while (1) {
25761 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
25762 +               deblk_end.deblk += deblk_sz;
25763 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
25764 +                       de = vdir_cache->vd_last.p.de;
25765 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
25766 +                             de->de_str.len, de->de_str.name, file->f_pos,
25767 +                             (unsigned long)de->de_ino, de->de_type);
25768 +                       err = filldir(dirent, de->de_str.name, de->de_str.len,
25769 +                                     file->f_pos, de->de_ino, de->de_type);
25770 +                       if (unlikely(err)) {
25771 +                               AuTraceErr(err);
25772 +                               /* todo: ignore the error caused by udba? */
25773 +                               /* return err; */
25774 +                               return 0;
25775 +                       }
25776 +
25777 +                       l = calc_size(de->de_str.len);
25778 +                       vdir_cache->vd_last.p.deblk += l;
25779 +                       file->f_pos += l;
25780 +               }
25781 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
25782 +                       vdir_cache->vd_last.ul++;
25783 +                       vdir_cache->vd_last.p.deblk
25784 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
25785 +                       file->f_pos = deblk_sz * vdir_cache->vd_last.ul;
25786 +                       continue;
25787 +               }
25788 +               break;
25789 +       }
25790 +
25791 +       /* smp_mb(); */
25792 +       return 0;
25793 +}
25794 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
25795 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
25796 +++ linux/fs/aufs/vfsub.c       2012-03-19 12:24:13.686467926 +0100
25797 @@ -0,0 +1,832 @@
25798 +/*
25799 + * Copyright (C) 2005-2012 Junjiro R. Okajima
25800 + *
25801 + * This program, aufs is free software; you can redistribute it and/or modify
25802 + * it under the terms of the GNU General Public License as published by
25803 + * the Free Software Foundation; either version 2 of the License, or
25804 + * (at your option) any later version.
25805 + *
25806 + * This program is distributed in the hope that it will be useful,
25807 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25808 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25809 + * GNU General Public License for more details.
25810 + *
25811 + * You should have received a copy of the GNU General Public License
25812 + * along with this program; if not, write to the Free Software
25813 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25814 + */
25815 +
25816 +/*
25817 + * sub-routines for VFS
25818 + */
25819 +
25820 +#include <linux/ima.h>
25821 +#include <linux/namei.h>
25822 +#include <linux/security.h>
25823 +#include <linux/splice.h>
25824 +#include "aufs.h"
25825 +
25826 +int vfsub_update_h_iattr(struct path *h_path, int *did)
25827 +{
25828 +       int err;
25829 +       struct kstat st;
25830 +       struct super_block *h_sb;
25831 +
25832 +       /* for remote fs, leave work for its getattr or d_revalidate */
25833 +       /* for bad i_attr fs, handle them in aufs_getattr() */
25834 +       /* still some fs may acquire i_mutex. we need to skip them */
25835 +       err = 0;
25836 +       if (!did)
25837 +               did = &err;
25838 +       h_sb = h_path->dentry->d_sb;
25839 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
25840 +       if (*did)
25841 +               err = vfs_getattr(h_path->mnt, h_path->dentry, &st);
25842 +
25843 +       return err;
25844 +}
25845 +
25846 +/* ---------------------------------------------------------------------- */
25847 +
25848 +struct file *vfsub_dentry_open(struct path *path, int flags)
25849 +{
25850 +       struct file *file;
25851 +
25852 +       path_get(path);
25853 +       file = dentry_open(path->dentry, path->mnt,
25854 +                          flags /* | __FMODE_NONOTIFY */,
25855 +                          current_cred());
25856 +       if (!IS_ERR_OR_NULL(file)
25857 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
25858 +               i_readcount_inc(path->dentry->d_inode);
25859 +
25860 +       return file;
25861 +}
25862 +
25863 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
25864 +{
25865 +       struct file *file;
25866 +
25867 +       lockdep_off();
25868 +       file = filp_open(path,
25869 +                        oflags /* | __FMODE_NONOTIFY */,
25870 +                        mode);
25871 +       lockdep_on();
25872 +       if (IS_ERR(file))
25873 +               goto out;
25874 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
25875 +
25876 +out:
25877 +       return file;
25878 +}
25879 +
25880 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
25881 +{
25882 +       int err;
25883 +
25884 +       err = kern_path(name, flags, path);
25885 +       if (!err && path->dentry->d_inode)
25886 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
25887 +       return err;
25888 +}
25889 +
25890 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
25891 +                                   int len)
25892 +{
25893 +       struct path path = {
25894 +               .mnt = NULL
25895 +       };
25896 +
25897 +       /* VFS checks it too, but by WARN_ON_ONCE() */
25898 +       IMustLock(parent->d_inode);
25899 +
25900 +       path.dentry = lookup_one_len(name, parent, len);
25901 +       if (IS_ERR(path.dentry))
25902 +               goto out;
25903 +       if (path.dentry->d_inode)
25904 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
25905 +
25906 +out:
25907 +       AuTraceErrPtr(path.dentry);
25908 +       return path.dentry;
25909 +}
25910 +
25911 +struct dentry *vfsub_lookup_hash(struct nameidata *nd)
25912 +{
25913 +       struct path path = {
25914 +               .mnt = nd->path.mnt
25915 +       };
25916 +
25917 +       IMustLock(nd->path.dentry->d_inode);
25918 +
25919 +       path.dentry = lookup_hash(nd);
25920 +       if (IS_ERR(path.dentry))
25921 +               goto out;
25922 +       if (path.dentry->d_inode)
25923 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
25924 +
25925 +out:
25926 +       AuTraceErrPtr(path.dentry);
25927 +       return path.dentry;
25928 +}
25929 +
25930 +/*
25931 + * this is "VFS:__lookup_one_len()" which was removed and merged into
25932 + * VFS:lookup_one_len() by the commit.
25933 + *     6a96ba5 2011-03-14 kill __lookup_one_len()
25934 + * this function should always be equivalent to the corresponding part in
25935 + * VFS:lookup_one_len().
25936 + */
25937 +int vfsub_name_hash(const char *name, struct qstr *this, int len)
25938 +{
25939 +       unsigned int c;
25940 +
25941 +       this->name = name;
25942 +       this->len = len;
25943 +       this->hash = full_name_hash(name, len);
25944 +       if (!len)
25945 +               return -EACCES;
25946 +
25947 +       while (len--) {
25948 +               c = *(const unsigned char *)name++;
25949 +               if (c == '/' || c == '\0')
25950 +                       return -EACCES;
25951 +       }
25952 +       return 0;
25953 +}
25954 +
25955 +/* ---------------------------------------------------------------------- */
25956 +
25957 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
25958 +                                struct dentry *d2, struct au_hinode *hdir2)
25959 +{
25960 +       struct dentry *d;
25961 +
25962 +       lockdep_off();
25963 +       d = lock_rename(d1, d2);
25964 +       lockdep_on();
25965 +       au_hn_suspend(hdir1);
25966 +       if (hdir1 != hdir2)
25967 +               au_hn_suspend(hdir2);
25968 +
25969 +       return d;
25970 +}
25971 +
25972 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
25973 +                        struct dentry *d2, struct au_hinode *hdir2)
25974 +{
25975 +       au_hn_resume(hdir1);
25976 +       if (hdir1 != hdir2)
25977 +               au_hn_resume(hdir2);
25978 +       lockdep_off();
25979 +       unlock_rename(d1, d2);
25980 +       lockdep_on();
25981 +}
25982 +
25983 +/* ---------------------------------------------------------------------- */
25984 +
25985 +int vfsub_create(struct inode *dir, struct path *path, int mode)
25986 +{
25987 +       int err;
25988 +       struct dentry *d;
25989 +
25990 +       IMustLock(dir);
25991 +
25992 +       d = path->dentry;
25993 +       path->dentry = d->d_parent;
25994 +       err = security_path_mknod(path, d, mode, 0);
25995 +       path->dentry = d;
25996 +       if (unlikely(err))
25997 +               goto out;
25998 +
25999 +       if (au_test_fs_null_nd(dir->i_sb))
26000 +               err = vfs_create(dir, path->dentry, mode, NULL);
26001 +       else {
26002 +               struct nameidata h_nd;
26003 +
26004 +               memset(&h_nd, 0, sizeof(h_nd));
26005 +               h_nd.flags = LOOKUP_CREATE;
26006 +               h_nd.intent.open.flags = O_CREAT
26007 +                       | vfsub_fmode_to_uint(FMODE_READ);
26008 +               h_nd.intent.open.create_mode = mode;
26009 +               h_nd.path.dentry = path->dentry->d_parent;
26010 +               h_nd.path.mnt = path->mnt;
26011 +               path_get(&h_nd.path);
26012 +               err = vfs_create(dir, path->dentry, mode, &h_nd);
26013 +               path_put(&h_nd.path);
26014 +       }
26015 +
26016 +       if (!err) {
26017 +               struct path tmp = *path;
26018 +               int did;
26019 +
26020 +               vfsub_update_h_iattr(&tmp, &did);
26021 +               if (did) {
26022 +                       tmp.dentry = path->dentry->d_parent;
26023 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26024 +               }
26025 +               /*ignore*/
26026 +       }
26027 +
26028 +out:
26029 +       return err;
26030 +}
26031 +
26032 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
26033 +{
26034 +       int err;
26035 +       struct dentry *d;
26036 +
26037 +       IMustLock(dir);
26038 +
26039 +       d = path->dentry;
26040 +       path->dentry = d->d_parent;
26041 +       err = security_path_symlink(path, d, symname);
26042 +       path->dentry = d;
26043 +       if (unlikely(err))
26044 +               goto out;
26045 +
26046 +       err = vfs_symlink(dir, path->dentry, symname);
26047 +       if (!err) {
26048 +               struct path tmp = *path;
26049 +               int did;
26050 +
26051 +               vfsub_update_h_iattr(&tmp, &did);
26052 +               if (did) {
26053 +                       tmp.dentry = path->dentry->d_parent;
26054 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26055 +               }
26056 +               /*ignore*/
26057 +       }
26058 +
26059 +out:
26060 +       return err;
26061 +}
26062 +
26063 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
26064 +{
26065 +       int err;
26066 +       struct dentry *d;
26067 +
26068 +       IMustLock(dir);
26069 +
26070 +       d = path->dentry;
26071 +       path->dentry = d->d_parent;
26072 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
26073 +       path->dentry = d;
26074 +       if (unlikely(err))
26075 +               goto out;
26076 +
26077 +       err = vfs_mknod(dir, path->dentry, mode, dev);
26078 +       if (!err) {
26079 +               struct path tmp = *path;
26080 +               int did;
26081 +
26082 +               vfsub_update_h_iattr(&tmp, &did);
26083 +               if (did) {
26084 +                       tmp.dentry = path->dentry->d_parent;
26085 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26086 +               }
26087 +               /*ignore*/
26088 +       }
26089 +
26090 +out:
26091 +       return err;
26092 +}
26093 +
26094 +static int au_test_nlink(struct inode *inode)
26095 +{
26096 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
26097 +
26098 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
26099 +           || inode->i_nlink < link_max)
26100 +               return 0;
26101 +       return -EMLINK;
26102 +}
26103 +
26104 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
26105 +{
26106 +       int err;
26107 +       struct dentry *d;
26108 +
26109 +       IMustLock(dir);
26110 +
26111 +       err = au_test_nlink(src_dentry->d_inode);
26112 +       if (unlikely(err))
26113 +               return err;
26114 +
26115 +       d = path->dentry;
26116 +       path->dentry = d->d_parent;
26117 +       err = security_path_link(src_dentry, path, d);
26118 +       path->dentry = d;
26119 +       if (unlikely(err))
26120 +               goto out;
26121 +
26122 +       lockdep_off();
26123 +       err = vfs_link(src_dentry, dir, path->dentry);
26124 +       lockdep_on();
26125 +       if (!err) {
26126 +               struct path tmp = *path;
26127 +               int did;
26128 +
26129 +               /* fuse has different memory inode for the same inumber */
26130 +               vfsub_update_h_iattr(&tmp, &did);
26131 +               if (did) {
26132 +                       tmp.dentry = path->dentry->d_parent;
26133 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26134 +                       tmp.dentry = src_dentry;
26135 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26136 +               }
26137 +               /*ignore*/
26138 +       }
26139 +
26140 +out:
26141 +       return err;
26142 +}
26143 +
26144 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
26145 +                struct inode *dir, struct path *path)
26146 +{
26147 +       int err;
26148 +       struct path tmp = {
26149 +               .mnt    = path->mnt
26150 +       };
26151 +       struct dentry *d;
26152 +
26153 +       IMustLock(dir);
26154 +       IMustLock(src_dir);
26155 +
26156 +       d = path->dentry;
26157 +       path->dentry = d->d_parent;
26158 +       tmp.dentry = src_dentry->d_parent;
26159 +       err = security_path_rename(&tmp, src_dentry, path, d);
26160 +       path->dentry = d;
26161 +       if (unlikely(err))
26162 +               goto out;
26163 +
26164 +       lockdep_off();
26165 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
26166 +       lockdep_on();
26167 +       if (!err) {
26168 +               int did;
26169 +
26170 +               tmp.dentry = d->d_parent;
26171 +               vfsub_update_h_iattr(&tmp, &did);
26172 +               if (did) {
26173 +                       tmp.dentry = src_dentry;
26174 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26175 +                       tmp.dentry = src_dentry->d_parent;
26176 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26177 +               }
26178 +               /*ignore*/
26179 +       }
26180 +
26181 +out:
26182 +       return err;
26183 +}
26184 +
26185 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
26186 +{
26187 +       int err;
26188 +       struct dentry *d;
26189 +
26190 +       IMustLock(dir);
26191 +
26192 +       d = path->dentry;
26193 +       path->dentry = d->d_parent;
26194 +       err = security_path_mkdir(path, d, mode);
26195 +       path->dentry = d;
26196 +       if (unlikely(err))
26197 +               goto out;
26198 +
26199 +       err = vfs_mkdir(dir, path->dentry, mode);
26200 +       if (!err) {
26201 +               struct path tmp = *path;
26202 +               int did;
26203 +
26204 +               vfsub_update_h_iattr(&tmp, &did);
26205 +               if (did) {
26206 +                       tmp.dentry = path->dentry->d_parent;
26207 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
26208 +               }
26209 +               /*ignore*/
26210 +       }
26211 +
26212 +out:
26213 +       return err;
26214 +}
26215 +
26216 +int vfsub_rmdir(struct inode *dir, struct path *path)
26217 +{
26218 +       int err;
26219 +       struct dentry *d;
26220 +
26221 +       IMustLock(dir);
26222 +
26223 +       d = path->dentry;
26224 +       path->dentry = d->d_parent;
26225 +       err = security_path_rmdir(path, d);
26226 +       path->dentry = d;
26227 +       if (unlikely(err))
26228 +               goto out;
26229 +
26230 +       lockdep_off();
26231 +       err = vfs_rmdir(dir, path->dentry);
26232 +       lockdep_on();
26233 +       if (!err) {
26234 +               struct path tmp = {
26235 +                       .dentry = path->dentry->d_parent,
26236 +                       .mnt    = path->mnt
26237 +               };
26238 +
26239 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
26240 +       }
26241 +
26242 +out:
26243 +       return err;
26244 +}
26245 +
26246 +/* ---------------------------------------------------------------------- */
26247 +
26248 +/* todo: support mmap_sem? */
26249 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
26250 +                    loff_t *ppos)
26251 +{
26252 +       ssize_t err;
26253 +
26254 +       lockdep_off();
26255 +       err = vfs_read(file, ubuf, count, ppos);
26256 +       lockdep_on();
26257 +       if (err >= 0)
26258 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
26259 +       return err;
26260 +}
26261 +
26262 +/* todo: kernel_read()? */
26263 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
26264 +                    loff_t *ppos)
26265 +{
26266 +       ssize_t err;
26267 +       mm_segment_t oldfs;
26268 +       union {
26269 +               void *k;
26270 +               char __user *u;
26271 +       } buf;
26272 +
26273 +       buf.k = kbuf;
26274 +       oldfs = get_fs();
26275 +       set_fs(KERNEL_DS);
26276 +       err = vfsub_read_u(file, buf.u, count, ppos);
26277 +       set_fs(oldfs);
26278 +       return err;
26279 +}
26280 +
26281 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
26282 +                     loff_t *ppos)
26283 +{
26284 +       ssize_t err;
26285 +
26286 +       lockdep_off();
26287 +       err = vfs_write(file, ubuf, count, ppos);
26288 +       lockdep_on();
26289 +       if (err >= 0)
26290 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
26291 +       return err;
26292 +}
26293 +
26294 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
26295 +{
26296 +       ssize_t err;
26297 +       mm_segment_t oldfs;
26298 +       union {
26299 +               void *k;
26300 +               const char __user *u;
26301 +       } buf;
26302 +
26303 +       buf.k = kbuf;
26304 +       oldfs = get_fs();
26305 +       set_fs(KERNEL_DS);
26306 +       err = vfsub_write_u(file, buf.u, count, ppos);
26307 +       set_fs(oldfs);
26308 +       return err;
26309 +}
26310 +
26311 +int vfsub_flush(struct file *file, fl_owner_t id)
26312 +{
26313 +       int err;
26314 +
26315 +       err = 0;
26316 +       if (file->f_op && file->f_op->flush) {
26317 +               if (!au_test_nfs(file->f_dentry->d_sb))
26318 +                       err = file->f_op->flush(file, id);
26319 +               else {
26320 +                       lockdep_off();
26321 +                       err = file->f_op->flush(file, id);
26322 +                       lockdep_on();
26323 +               }
26324 +               if (!err)
26325 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
26326 +               /*ignore*/
26327 +       }
26328 +       return err;
26329 +}
26330 +
26331 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
26332 +{
26333 +       int err;
26334 +
26335 +       lockdep_off();
26336 +       err = vfs_readdir(file, filldir, arg);
26337 +       lockdep_on();
26338 +       if (err >= 0)
26339 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
26340 +       return err;
26341 +}
26342 +
26343 +long vfsub_splice_to(struct file *in, loff_t *ppos,
26344 +                    struct pipe_inode_info *pipe, size_t len,
26345 +                    unsigned int flags)
26346 +{
26347 +       long err;
26348 +
26349 +       lockdep_off();
26350 +       err = do_splice_to(in, ppos, pipe, len, flags);
26351 +       lockdep_on();
26352 +       file_accessed(in);
26353 +       if (err >= 0)
26354 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
26355 +       return err;
26356 +}
26357 +
26358 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
26359 +                      loff_t *ppos, size_t len, unsigned int flags)
26360 +{
26361 +       long err;
26362 +
26363 +       lockdep_off();
26364 +       err = do_splice_from(pipe, out, ppos, len, flags);
26365 +       lockdep_on();
26366 +       if (err >= 0)
26367 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
26368 +       return err;
26369 +}
26370 +
26371 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
26372 +{
26373 +       int err;
26374 +
26375 +       /* file can be NULL */
26376 +       lockdep_off();
26377 +       err = vfs_fsync(file, datasync);
26378 +       lockdep_on();
26379 +       if (!err) {
26380 +               if (!path) {
26381 +                       AuDebugOn(!file);
26382 +                       path = &file->f_path;
26383 +               }
26384 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
26385 +       }
26386 +       return err;
26387 +}
26388 +
26389 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
26390 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
26391 +               struct file *h_file)
26392 +{
26393 +       int err;
26394 +       struct inode *h_inode;
26395 +
26396 +       h_inode = h_path->dentry->d_inode;
26397 +       if (!h_file) {
26398 +               err = mnt_want_write(h_path->mnt);
26399 +               if (err)
26400 +                       goto out;
26401 +               err = inode_permission(h_inode, MAY_WRITE);
26402 +               if (err)
26403 +                       goto out_mnt;
26404 +               err = get_write_access(h_inode);
26405 +               if (err)
26406 +                       goto out_mnt;
26407 +               err = break_lease(h_inode, O_WRONLY);
26408 +               if (err)
26409 +                       goto out_inode;
26410 +       }
26411 +
26412 +       err = locks_verify_truncate(h_inode, h_file, length);
26413 +       if (!err)
26414 +               err = security_path_truncate(h_path);
26415 +       if (!err) {
26416 +               lockdep_off();
26417 +               err = do_truncate(h_path->dentry, length, attr, h_file);
26418 +               lockdep_on();
26419 +       }
26420 +
26421 +out_inode:
26422 +       if (!h_file)
26423 +               put_write_access(h_inode);
26424 +out_mnt:
26425 +       if (!h_file)
26426 +               mnt_drop_write(h_path->mnt);
26427 +out:
26428 +       return err;
26429 +}
26430 +
26431 +/* ---------------------------------------------------------------------- */
26432 +
26433 +struct au_vfsub_mkdir_args {
26434 +       int *errp;
26435 +       struct inode *dir;
26436 +       struct path *path;
26437 +       int mode;
26438 +};
26439 +
26440 +static void au_call_vfsub_mkdir(void *args)
26441 +{
26442 +       struct au_vfsub_mkdir_args *a = args;
26443 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
26444 +}
26445 +
26446 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
26447 +{
26448 +       int err, do_sio, wkq_err;
26449 +
26450 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
26451 +       if (!do_sio)
26452 +               err = vfsub_mkdir(dir, path, mode);
26453 +       else {
26454 +               struct au_vfsub_mkdir_args args = {
26455 +                       .errp   = &err,
26456 +                       .dir    = dir,
26457 +                       .path   = path,
26458 +                       .mode   = mode
26459 +               };
26460 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
26461 +               if (unlikely(wkq_err))
26462 +                       err = wkq_err;
26463 +       }
26464 +
26465 +       return err;
26466 +}
26467 +
26468 +struct au_vfsub_rmdir_args {
26469 +       int *errp;
26470 +       struct inode *dir;
26471 +       struct path *path;
26472 +};
26473 +
26474 +static void au_call_vfsub_rmdir(void *args)
26475 +{
26476 +       struct au_vfsub_rmdir_args *a = args;
26477 +       *a->errp = vfsub_rmdir(a->dir, a->path);
26478 +}
26479 +
26480 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
26481 +{
26482 +       int err, do_sio, wkq_err;
26483 +
26484 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
26485 +       if (!do_sio)
26486 +               err = vfsub_rmdir(dir, path);
26487 +       else {
26488 +               struct au_vfsub_rmdir_args args = {
26489 +                       .errp   = &err,
26490 +                       .dir    = dir,
26491 +                       .path   = path
26492 +               };
26493 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
26494 +               if (unlikely(wkq_err))
26495 +                       err = wkq_err;
26496 +       }
26497 +
26498 +       return err;
26499 +}
26500 +
26501 +/* ---------------------------------------------------------------------- */
26502 +
26503 +struct notify_change_args {
26504 +       int *errp;
26505 +       struct path *path;
26506 +       struct iattr *ia;
26507 +};
26508 +
26509 +static void call_notify_change(void *args)
26510 +{
26511 +       struct notify_change_args *a = args;
26512 +       struct inode *h_inode;
26513 +
26514 +       h_inode = a->path->dentry->d_inode;
26515 +       IMustLock(h_inode);
26516 +
26517 +       *a->errp = -EPERM;
26518 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
26519 +               *a->errp = notify_change(a->path->dentry, a->ia);
26520 +               if (!*a->errp)
26521 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
26522 +       }
26523 +       AuTraceErr(*a->errp);
26524 +}
26525 +
26526 +int vfsub_notify_change(struct path *path, struct iattr *ia)
26527 +{
26528 +       int err;
26529 +       struct notify_change_args args = {
26530 +               .errp   = &err,
26531 +               .path   = path,
26532 +               .ia     = ia
26533 +       };
26534 +
26535 +       call_notify_change(&args);
26536 +
26537 +       return err;
26538 +}
26539 +
26540 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
26541 +{
26542 +       int err, wkq_err;
26543 +       struct notify_change_args args = {
26544 +               .errp   = &err,
26545 +               .path   = path,
26546 +               .ia     = ia
26547 +       };
26548 +
26549 +       wkq_err = au_wkq_wait(call_notify_change, &args);
26550 +       if (unlikely(wkq_err))
26551 +               err = wkq_err;
26552 +
26553 +       return err;
26554 +}
26555 +
26556 +/* ---------------------------------------------------------------------- */
26557 +
26558 +struct unlink_args {
26559 +       int *errp;
26560 +       struct inode *dir;
26561 +       struct path *path;
26562 +};
26563 +
26564 +static void call_unlink(void *args)
26565 +{
26566 +       struct unlink_args *a = args;
26567 +       struct dentry *d = a->path->dentry;
26568 +       struct inode *h_inode;
26569 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
26570 +                                     && d->d_count == 1);
26571 +
26572 +       IMustLock(a->dir);
26573 +
26574 +       a->path->dentry = d->d_parent;
26575 +       *a->errp = security_path_unlink(a->path, d);
26576 +       a->path->dentry = d;
26577 +       if (unlikely(*a->errp))
26578 +               return;
26579 +
26580 +       if (!stop_sillyrename)
26581 +               dget(d);
26582 +       h_inode = d->d_inode;
26583 +       if (h_inode)
26584 +               ihold(h_inode);
26585 +
26586 +       lockdep_off();
26587 +       *a->errp = vfs_unlink(a->dir, d);
26588 +       lockdep_on();
26589 +       if (!*a->errp) {
26590 +               struct path tmp = {
26591 +                       .dentry = d->d_parent,
26592 +                       .mnt    = a->path->mnt
26593 +               };
26594 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
26595 +       }
26596 +
26597 +       if (!stop_sillyrename)
26598 +               dput(d);
26599 +       if (h_inode)
26600 +               iput(h_inode);
26601 +
26602 +       AuTraceErr(*a->errp);
26603 +}
26604 +
26605 +/*
26606 + * @dir: must be locked.
26607 + * @dentry: target dentry.
26608 + */
26609 +int vfsub_unlink(struct inode *dir, struct path *path, int force)
26610 +{
26611 +       int err;
26612 +       struct unlink_args args = {
26613 +               .errp   = &err,
26614 +               .dir    = dir,
26615 +               .path   = path
26616 +       };
26617 +
26618 +       if (!force)
26619 +               call_unlink(&args);
26620 +       else {
26621 +               int wkq_err;
26622 +
26623 +               wkq_err = au_wkq_wait(call_unlink, &args);
26624 +               if (unlikely(wkq_err))
26625 +                       err = wkq_err;
26626 +       }
26627 +
26628 +       return err;
26629 +}
26630 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
26631 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
26632 +++ linux/fs/aufs/vfsub.h       2012-03-19 12:24:13.686467926 +0100
26633 @@ -0,0 +1,240 @@
26634 +/*
26635 + * Copyright (C) 2005-2012 Junjiro R. Okajima
26636 + *
26637 + * This program, aufs is free software; you can redistribute it and/or modify
26638 + * it under the terms of the GNU General Public License as published by
26639 + * the Free Software Foundation; either version 2 of the License, or
26640 + * (at your option) any later version.
26641 + *
26642 + * This program is distributed in the hope that it will be useful,
26643 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26644 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26645 + * GNU General Public License for more details.
26646 + *
26647 + * You should have received a copy of the GNU General Public License
26648 + * along with this program; if not, write to the Free Software
26649 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26650 + */
26651 +
26652 +/*
26653 + * sub-routines for VFS
26654 + */
26655 +
26656 +#ifndef __AUFS_VFSUB_H__
26657 +#define __AUFS_VFSUB_H__
26658 +
26659 +#ifdef __KERNEL__
26660 +
26661 +#include <linux/fs.h>
26662 +#include <linux/lglock.h>
26663 +#include "debug.h"
26664 +
26665 +/* copied from linux/fs/internal.h */
26666 +/* todo: BAD approach!! */
26667 +DECLARE_BRLOCK(vfsmount_lock);
26668 +extern void file_sb_list_del(struct file *f);
26669 +extern spinlock_t inode_sb_list_lock;
26670 +
26671 +/* copied from linux/fs/file_table.c */
26672 +DECLARE_LGLOCK(files_lglock);
26673 +#ifdef CONFIG_SMP
26674 +/*
26675 + * These macros iterate all files on all CPUs for a given superblock.
26676 + * files_lglock must be held globally.
26677 + */
26678 +#define do_file_list_for_each_entry(__sb, __file)              \
26679 +{                                                              \
26680 +       int i;                                                  \
26681 +       for_each_possible_cpu(i) {                              \
26682 +               struct list_head *list;                         \
26683 +               list = per_cpu_ptr((__sb)->s_files, i);         \
26684 +               list_for_each_entry((__file), list, f_u.fu_list)
26685 +
26686 +#define while_file_list_for_each_entry                         \
26687 +       }                                                       \
26688 +}
26689 +
26690 +#else
26691 +
26692 +#define do_file_list_for_each_entry(__sb, __file)              \
26693 +{                                                              \
26694 +       struct list_head *list;                                 \
26695 +       list = &(sb)->s_files;                                  \
26696 +       list_for_each_entry((__file), list, f_u.fu_list)
26697 +
26698 +#define while_file_list_for_each_entry                         \
26699 +}
26700 +#endif
26701 +
26702 +/* ---------------------------------------------------------------------- */
26703 +
26704 +/* lock subclass for lower inode */
26705 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
26706 +/* reduce? gave up. */
26707 +enum {
26708 +       AuLsc_I_Begin = I_MUTEX_QUOTA, /* 4 */
26709 +       AuLsc_I_PARENT,         /* lower inode, parent first */
26710 +       AuLsc_I_PARENT2,        /* copyup dirs */
26711 +       AuLsc_I_PARENT3,        /* copyup wh */
26712 +       AuLsc_I_CHILD,
26713 +       AuLsc_I_CHILD2,
26714 +       AuLsc_I_End
26715 +};
26716 +
26717 +/* to debug easier, do not make them inlined functions */
26718 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
26719 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
26720 +
26721 +/* ---------------------------------------------------------------------- */
26722 +
26723 +static inline void vfsub_drop_nlink(struct inode *inode)
26724 +{
26725 +       AuDebugOn(!inode->i_nlink);
26726 +       drop_nlink(inode);
26727 +}
26728 +
26729 +static inline void vfsub_dead_dir(struct inode *inode)
26730 +{
26731 +       AuDebugOn(!S_ISDIR(inode->i_mode));
26732 +       inode->i_flags |= S_DEAD;
26733 +       clear_nlink(inode);
26734 +}
26735 +
26736 +static inline void vfsub_set_nlink(struct inode *inode, unsigned int nlink)
26737 +{
26738 +       if (nlink)
26739 +               set_nlink(inode, nlink);
26740 +       else
26741 +               clear_nlink(inode);
26742 +}
26743 +
26744 +/* ---------------------------------------------------------------------- */
26745 +
26746 +int vfsub_update_h_iattr(struct path *h_path, int *did);
26747 +struct file *vfsub_dentry_open(struct path *path, int flags);
26748 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
26749 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
26750 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
26751 +                                   int len);
26752 +struct dentry *vfsub_lookup_hash(struct nameidata *nd);
26753 +int vfsub_name_hash(const char *name, struct qstr *this, int len);
26754 +
26755 +/* ---------------------------------------------------------------------- */
26756 +
26757 +struct au_hinode;
26758 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
26759 +                                struct dentry *d2, struct au_hinode *hdir2);
26760 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
26761 +                        struct dentry *d2, struct au_hinode *hdir2);
26762 +
26763 +int vfsub_create(struct inode *dir, struct path *path, int mode);
26764 +int vfsub_symlink(struct inode *dir, struct path *path,
26765 +                 const char *symname);
26766 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
26767 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
26768 +              struct path *path);
26769 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
26770 +                struct inode *hdir, struct path *path);
26771 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
26772 +int vfsub_rmdir(struct inode *dir, struct path *path);
26773 +
26774 +/* ---------------------------------------------------------------------- */
26775 +
26776 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
26777 +                    loff_t *ppos);
26778 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
26779 +                       loff_t *ppos);
26780 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
26781 +                     loff_t *ppos);
26782 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
26783 +                     loff_t *ppos);
26784 +int vfsub_flush(struct file *file, fl_owner_t id);
26785 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg);
26786 +
26787 +static inline unsigned int vfsub_file_flags(struct file *file)
26788 +{
26789 +       unsigned int flags;
26790 +
26791 +       spin_lock(&file->f_lock);
26792 +       flags = file->f_flags;
26793 +       spin_unlock(&file->f_lock);
26794 +
26795 +       return flags;
26796 +}
26797 +
26798 +static inline void vfsub_file_accessed(struct file *h_file)
26799 +{
26800 +       file_accessed(h_file);
26801 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
26802 +}
26803 +
26804 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
26805 +                                    struct dentry *h_dentry)
26806 +{
26807 +       struct path h_path = {
26808 +               .dentry = h_dentry,
26809 +               .mnt    = h_mnt
26810 +       };
26811 +       touch_atime(h_mnt, h_dentry);
26812 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
26813 +}
26814 +
26815 +long vfsub_splice_to(struct file *in, loff_t *ppos,
26816 +                    struct pipe_inode_info *pipe, size_t len,
26817 +                    unsigned int flags);
26818 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
26819 +                      loff_t *ppos, size_t len, unsigned int flags);
26820 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
26821 +               struct file *h_file);
26822 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
26823 +
26824 +/* ---------------------------------------------------------------------- */
26825 +
26826 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
26827 +{
26828 +       loff_t err;
26829 +
26830 +       lockdep_off();
26831 +       err = vfs_llseek(file, offset, origin);
26832 +       lockdep_on();
26833 +       return err;
26834 +}
26835 +
26836 +/* ---------------------------------------------------------------------- */
26837 +
26838 +/* dirty workaround for strict type of fmode_t */
26839 +union vfsub_fmu {
26840 +       fmode_t fm;
26841 +       unsigned int ui;
26842 +};
26843 +
26844 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
26845 +{
26846 +       union vfsub_fmu u = {
26847 +               .fm = fm
26848 +       };
26849 +
26850 +       BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
26851 +
26852 +       return u.ui;
26853 +}
26854 +
26855 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
26856 +{
26857 +       union vfsub_fmu u = {
26858 +               .ui = ui
26859 +       };
26860 +
26861 +       return u.fm;
26862 +}
26863 +
26864 +/* ---------------------------------------------------------------------- */
26865 +
26866 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
26867 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
26868 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia);
26869 +int vfsub_notify_change(struct path *path, struct iattr *ia);
26870 +int vfsub_unlink(struct inode *dir, struct path *path, int force);
26871 +
26872 +#endif /* __KERNEL__ */
26873 +#endif /* __AUFS_VFSUB_H__ */
26874 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
26875 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
26876 +++ linux/fs/aufs/wbr_policy.c  2012-03-19 12:24:13.686467926 +0100
26877 @@ -0,0 +1,700 @@
26878 +/*
26879 + * Copyright (C) 2005-2012 Junjiro R. Okajima
26880 + *
26881 + * This program, aufs is free software; you can redistribute it and/or modify
26882 + * it under the terms of the GNU General Public License as published by
26883 + * the Free Software Foundation; either version 2 of the License, or
26884 + * (at your option) any later version.
26885 + *
26886 + * This program is distributed in the hope that it will be useful,
26887 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26888 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26889 + * GNU General Public License for more details.
26890 + *
26891 + * You should have received a copy of the GNU General Public License
26892 + * along with this program; if not, write to the Free Software
26893 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26894 + */
26895 +
26896 +/*
26897 + * policies for selecting one among multiple writable branches
26898 + */
26899 +
26900 +#include <linux/statfs.h>
26901 +#include "aufs.h"
26902 +
26903 +/* subset of cpup_attr() */
26904 +static noinline_for_stack
26905 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
26906 +{
26907 +       int err, sbits;
26908 +       struct iattr ia;
26909 +       struct inode *h_isrc;
26910 +
26911 +       h_isrc = h_src->d_inode;
26912 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
26913 +       ia.ia_mode = h_isrc->i_mode;
26914 +       ia.ia_uid = h_isrc->i_uid;
26915 +       ia.ia_gid = h_isrc->i_gid;
26916 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
26917 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc);
26918 +       err = vfsub_sio_notify_change(h_path, &ia);
26919 +
26920 +       /* is this nfs only? */
26921 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
26922 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
26923 +               ia.ia_mode = h_isrc->i_mode;
26924 +               err = vfsub_sio_notify_change(h_path, &ia);
26925 +       }
26926 +
26927 +       return err;
26928 +}
26929 +
26930 +#define AuCpdown_PARENT_OPQ    1
26931 +#define AuCpdown_WHED          (1 << 1)
26932 +#define AuCpdown_MADE_DIR      (1 << 2)
26933 +#define AuCpdown_DIROPQ                (1 << 3)
26934 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
26935 +#define au_fset_cpdown(flags, name) \
26936 +       do { (flags) |= AuCpdown_##name; } while (0)
26937 +#define au_fclr_cpdown(flags, name) \
26938 +       do { (flags) &= ~AuCpdown_##name; } while (0)
26939 +
26940 +struct au_cpdown_dir_args {
26941 +       struct dentry *parent;
26942 +       unsigned int flags;
26943 +};
26944 +
26945 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
26946 +                            struct au_cpdown_dir_args *a)
26947 +{
26948 +       int err;
26949 +       struct dentry *opq_dentry;
26950 +
26951 +       opq_dentry = au_diropq_create(dentry, bdst);
26952 +       err = PTR_ERR(opq_dentry);
26953 +       if (IS_ERR(opq_dentry))
26954 +               goto out;
26955 +       dput(opq_dentry);
26956 +       au_fset_cpdown(a->flags, DIROPQ);
26957 +
26958 +out:
26959 +       return err;
26960 +}
26961 +
26962 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
26963 +                           struct inode *dir, aufs_bindex_t bdst)
26964 +{
26965 +       int err;
26966 +       struct path h_path;
26967 +       struct au_branch *br;
26968 +
26969 +       br = au_sbr(dentry->d_sb, bdst);
26970 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
26971 +       err = PTR_ERR(h_path.dentry);
26972 +       if (IS_ERR(h_path.dentry))
26973 +               goto out;
26974 +
26975 +       err = 0;
26976 +       if (h_path.dentry->d_inode) {
26977 +               h_path.mnt = br->br_mnt;
26978 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
26979 +                                         dentry);
26980 +       }
26981 +       dput(h_path.dentry);
26982 +
26983 +out:
26984 +       return err;
26985 +}
26986 +
26987 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
26988 +                        struct dentry *h_parent, void *arg)
26989 +{
26990 +       int err, rerr;
26991 +       aufs_bindex_t bopq, bstart;
26992 +       struct path h_path;
26993 +       struct dentry *parent;
26994 +       struct inode *h_dir, *h_inode, *inode, *dir;
26995 +       struct au_cpdown_dir_args *args = arg;
26996 +
26997 +       bstart = au_dbstart(dentry);
26998 +       /* dentry is di-locked */
26999 +       parent = dget_parent(dentry);
27000 +       dir = parent->d_inode;
27001 +       h_dir = h_parent->d_inode;
27002 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
27003 +       IMustLock(h_dir);
27004 +
27005 +       err = au_lkup_neg(dentry, bdst);
27006 +       if (unlikely(err < 0))
27007 +               goto out;
27008 +       h_path.dentry = au_h_dptr(dentry, bdst);
27009 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
27010 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
27011 +                             S_IRWXU | S_IRUGO | S_IXUGO);
27012 +       if (unlikely(err))
27013 +               goto out_put;
27014 +       au_fset_cpdown(args->flags, MADE_DIR);
27015 +
27016 +       bopq = au_dbdiropq(dentry);
27017 +       au_fclr_cpdown(args->flags, WHED);
27018 +       au_fclr_cpdown(args->flags, DIROPQ);
27019 +       if (au_dbwh(dentry) == bdst)
27020 +               au_fset_cpdown(args->flags, WHED);
27021 +       if (!au_ftest_cpdown(args->flags, PARENT_OPQ) && bopq <= bdst)
27022 +               au_fset_cpdown(args->flags, PARENT_OPQ);
27023 +       h_inode = h_path.dentry->d_inode;
27024 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
27025 +       if (au_ftest_cpdown(args->flags, WHED)) {
27026 +               err = au_cpdown_dir_opq(dentry, bdst, args);
27027 +               if (unlikely(err)) {
27028 +                       mutex_unlock(&h_inode->i_mutex);
27029 +                       goto out_dir;
27030 +               }
27031 +       }
27032 +
27033 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
27034 +       mutex_unlock(&h_inode->i_mutex);
27035 +       if (unlikely(err))
27036 +               goto out_opq;
27037 +
27038 +       if (au_ftest_cpdown(args->flags, WHED)) {
27039 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
27040 +               if (unlikely(err))
27041 +                       goto out_opq;
27042 +       }
27043 +
27044 +       inode = dentry->d_inode;
27045 +       if (au_ibend(inode) < bdst)
27046 +               au_set_ibend(inode, bdst);
27047 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
27048 +                     au_hi_flags(inode, /*isdir*/1));
27049 +       goto out; /* success */
27050 +
27051 +       /* revert */
27052 +out_opq:
27053 +       if (au_ftest_cpdown(args->flags, DIROPQ)) {
27054 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
27055 +               rerr = au_diropq_remove(dentry, bdst);
27056 +               mutex_unlock(&h_inode->i_mutex);
27057 +               if (unlikely(rerr)) {
27058 +                       AuIOErr("failed removing diropq for %.*s b%d (%d)\n",
27059 +                               AuDLNPair(dentry), bdst, rerr);
27060 +                       err = -EIO;
27061 +                       goto out;
27062 +               }
27063 +       }
27064 +out_dir:
27065 +       if (au_ftest_cpdown(args->flags, MADE_DIR)) {
27066 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
27067 +               if (unlikely(rerr)) {
27068 +                       AuIOErr("failed removing %.*s b%d (%d)\n",
27069 +                               AuDLNPair(dentry), bdst, rerr);
27070 +                       err = -EIO;
27071 +               }
27072 +       }
27073 +out_put:
27074 +       au_set_h_dptr(dentry, bdst, NULL);
27075 +       if (au_dbend(dentry) == bdst)
27076 +               au_update_dbend(dentry);
27077 +out:
27078 +       dput(parent);
27079 +       return err;
27080 +}
27081 +
27082 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
27083 +{
27084 +       int err;
27085 +       struct au_cpdown_dir_args args = {
27086 +               .parent = dget_parent(dentry),
27087 +               .flags  = 0
27088 +       };
27089 +
27090 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &args);
27091 +       dput(args.parent);
27092 +
27093 +       return err;
27094 +}
27095 +
27096 +/* ---------------------------------------------------------------------- */
27097 +
27098 +/* policies for create */
27099 +
27100 +static int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
27101 +{
27102 +       int err, i, j, ndentry;
27103 +       aufs_bindex_t bopq;
27104 +       struct au_dcsub_pages dpages;
27105 +       struct au_dpage *dpage;
27106 +       struct dentry **dentries, *parent, *d;
27107 +
27108 +       err = au_dpages_init(&dpages, GFP_NOFS);
27109 +       if (unlikely(err))
27110 +               goto out;
27111 +       parent = dget_parent(dentry);
27112 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
27113 +       if (unlikely(err))
27114 +               goto out_free;
27115 +
27116 +       err = bindex;
27117 +       for (i = 0; i < dpages.ndpage; i++) {
27118 +               dpage = dpages.dpages + i;
27119 +               dentries = dpage->dentries;
27120 +               ndentry = dpage->ndentry;
27121 +               for (j = 0; j < ndentry; j++) {
27122 +                       d = dentries[j];
27123 +                       di_read_lock_parent2(d, !AuLock_IR);
27124 +                       bopq = au_dbdiropq(d);
27125 +                       di_read_unlock(d, !AuLock_IR);
27126 +                       if (bopq >= 0 && bopq < err)
27127 +                               err = bopq;
27128 +               }
27129 +       }
27130 +
27131 +out_free:
27132 +       dput(parent);
27133 +       au_dpages_free(&dpages);
27134 +out:
27135 +       return err;
27136 +}
27137 +
27138 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
27139 +{
27140 +       for (; bindex >= 0; bindex--)
27141 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
27142 +                       return bindex;
27143 +       return -EROFS;
27144 +}
27145 +
27146 +/* top down parent */
27147 +static int au_wbr_create_tdp(struct dentry *dentry, int isdir __maybe_unused)
27148 +{
27149 +       int err;
27150 +       aufs_bindex_t bstart, bindex;
27151 +       struct super_block *sb;
27152 +       struct dentry *parent, *h_parent;
27153 +
27154 +       sb = dentry->d_sb;
27155 +       bstart = au_dbstart(dentry);
27156 +       err = bstart;
27157 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
27158 +               goto out;
27159 +
27160 +       err = -EROFS;
27161 +       parent = dget_parent(dentry);
27162 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
27163 +               h_parent = au_h_dptr(parent, bindex);
27164 +               if (!h_parent || !h_parent->d_inode)
27165 +                       continue;
27166 +
27167 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
27168 +                       err = bindex;
27169 +                       break;
27170 +               }
27171 +       }
27172 +       dput(parent);
27173 +
27174 +       /* bottom up here */
27175 +       if (unlikely(err < 0)) {
27176 +               err = au_wbr_bu(sb, bstart - 1);
27177 +               if (err >= 0)
27178 +                       err = au_wbr_nonopq(dentry, err);
27179 +       }
27180 +
27181 +out:
27182 +       AuDbg("b%d\n", err);
27183 +       return err;
27184 +}
27185 +
27186 +/* ---------------------------------------------------------------------- */
27187 +
27188 +/* an exception for the policy other than tdp */
27189 +static int au_wbr_create_exp(struct dentry *dentry)
27190 +{
27191 +       int err;
27192 +       aufs_bindex_t bwh, bdiropq;
27193 +       struct dentry *parent;
27194 +
27195 +       err = -1;
27196 +       bwh = au_dbwh(dentry);
27197 +       parent = dget_parent(dentry);
27198 +       bdiropq = au_dbdiropq(parent);
27199 +       if (bwh >= 0) {
27200 +               if (bdiropq >= 0)
27201 +                       err = min(bdiropq, bwh);
27202 +               else
27203 +                       err = bwh;
27204 +               AuDbg("%d\n", err);
27205 +       } else if (bdiropq >= 0) {
27206 +               err = bdiropq;
27207 +               AuDbg("%d\n", err);
27208 +       }
27209 +       dput(parent);
27210 +
27211 +       if (err >= 0)
27212 +               err = au_wbr_nonopq(dentry, err);
27213 +
27214 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
27215 +               err = -1;
27216 +
27217 +       AuDbg("%d\n", err);
27218 +       return err;
27219 +}
27220 +
27221 +/* ---------------------------------------------------------------------- */
27222 +
27223 +/* round robin */
27224 +static int au_wbr_create_init_rr(struct super_block *sb)
27225 +{
27226 +       int err;
27227 +
27228 +       err = au_wbr_bu(sb, au_sbend(sb));
27229 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
27230 +       /* smp_mb(); */
27231 +
27232 +       AuDbg("b%d\n", err);
27233 +       return err;
27234 +}
27235 +
27236 +static int au_wbr_create_rr(struct dentry *dentry, int isdir)
27237 +{
27238 +       int err, nbr;
27239 +       unsigned int u;
27240 +       aufs_bindex_t bindex, bend;
27241 +       struct super_block *sb;
27242 +       atomic_t *next;
27243 +
27244 +       err = au_wbr_create_exp(dentry);
27245 +       if (err >= 0)
27246 +               goto out;
27247 +
27248 +       sb = dentry->d_sb;
27249 +       next = &au_sbi(sb)->si_wbr_rr_next;
27250 +       bend = au_sbend(sb);
27251 +       nbr = bend + 1;
27252 +       for (bindex = 0; bindex <= bend; bindex++) {
27253 +               if (!isdir) {
27254 +                       err = atomic_dec_return(next) + 1;
27255 +                       /* modulo for 0 is meaningless */
27256 +                       if (unlikely(!err))
27257 +                               err = atomic_dec_return(next) + 1;
27258 +               } else
27259 +                       err = atomic_read(next);
27260 +               AuDbg("%d\n", err);
27261 +               u = err;
27262 +               err = u % nbr;
27263 +               AuDbg("%d\n", err);
27264 +               if (!au_br_rdonly(au_sbr(sb, err)))
27265 +                       break;
27266 +               err = -EROFS;
27267 +       }
27268 +
27269 +       if (err >= 0)
27270 +               err = au_wbr_nonopq(dentry, err);
27271 +
27272 +out:
27273 +       AuDbg("%d\n", err);
27274 +       return err;
27275 +}
27276 +
27277 +/* ---------------------------------------------------------------------- */
27278 +
27279 +/* most free space */
27280 +static void au_mfs(struct dentry *dentry)
27281 +{
27282 +       struct super_block *sb;
27283 +       struct au_branch *br;
27284 +       struct au_wbr_mfs *mfs;
27285 +       aufs_bindex_t bindex, bend;
27286 +       int err;
27287 +       unsigned long long b, bavail;
27288 +       struct path h_path;
27289 +       /* reduce the stack usage */
27290 +       struct kstatfs *st;
27291 +
27292 +       st = kmalloc(sizeof(*st), GFP_NOFS);
27293 +       if (unlikely(!st)) {
27294 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
27295 +               return;
27296 +       }
27297 +
27298 +       bavail = 0;
27299 +       sb = dentry->d_sb;
27300 +       mfs = &au_sbi(sb)->si_wbr_mfs;
27301 +       MtxMustLock(&mfs->mfs_lock);
27302 +       mfs->mfs_bindex = -EROFS;
27303 +       mfs->mfsrr_bytes = 0;
27304 +       bend = au_sbend(sb);
27305 +       for (bindex = 0; bindex <= bend; bindex++) {
27306 +               br = au_sbr(sb, bindex);
27307 +               if (au_br_rdonly(br))
27308 +                       continue;
27309 +
27310 +               /* sb->s_root for NFS is unreliable */
27311 +               h_path.mnt = br->br_mnt;
27312 +               h_path.dentry = h_path.mnt->mnt_root;
27313 +               err = vfs_statfs(&h_path, st);
27314 +               if (unlikely(err)) {
27315 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
27316 +                       continue;
27317 +               }
27318 +
27319 +               /* when the available size is equal, select the lower one */
27320 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
27321 +                            || sizeof(b) < sizeof(st->f_bsize));
27322 +               b = st->f_bavail * st->f_bsize;
27323 +               br->br_wbr->wbr_bytes = b;
27324 +               if (b >= bavail) {
27325 +                       bavail = b;
27326 +                       mfs->mfs_bindex = bindex;
27327 +                       mfs->mfs_jiffy = jiffies;
27328 +               }
27329 +       }
27330 +
27331 +       mfs->mfsrr_bytes = bavail;
27332 +       AuDbg("b%d\n", mfs->mfs_bindex);
27333 +       kfree(st);
27334 +}
27335 +
27336 +static int au_wbr_create_mfs(struct dentry *dentry, int isdir __maybe_unused)
27337 +{
27338 +       int err;
27339 +       struct super_block *sb;
27340 +       struct au_wbr_mfs *mfs;
27341 +
27342 +       err = au_wbr_create_exp(dentry);
27343 +       if (err >= 0)
27344 +               goto out;
27345 +
27346 +       sb = dentry->d_sb;
27347 +       mfs = &au_sbi(sb)->si_wbr_mfs;
27348 +       mutex_lock(&mfs->mfs_lock);
27349 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
27350 +           || mfs->mfs_bindex < 0
27351 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
27352 +               au_mfs(dentry);
27353 +       mutex_unlock(&mfs->mfs_lock);
27354 +       err = mfs->mfs_bindex;
27355 +
27356 +       if (err >= 0)
27357 +               err = au_wbr_nonopq(dentry, err);
27358 +
27359 +out:
27360 +       AuDbg("b%d\n", err);
27361 +       return err;
27362 +}
27363 +
27364 +static int au_wbr_create_init_mfs(struct super_block *sb)
27365 +{
27366 +       struct au_wbr_mfs *mfs;
27367 +
27368 +       mfs = &au_sbi(sb)->si_wbr_mfs;
27369 +       mutex_init(&mfs->mfs_lock);
27370 +       mfs->mfs_jiffy = 0;
27371 +       mfs->mfs_bindex = -EROFS;
27372 +
27373 +       return 0;
27374 +}
27375 +
27376 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
27377 +{
27378 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
27379 +       return 0;
27380 +}
27381 +
27382 +/* ---------------------------------------------------------------------- */
27383 +
27384 +/* most free space and then round robin */
27385 +static int au_wbr_create_mfsrr(struct dentry *dentry, int isdir)
27386 +{
27387 +       int err;
27388 +       struct au_wbr_mfs *mfs;
27389 +
27390 +       err = au_wbr_create_mfs(dentry, isdir);
27391 +       if (err >= 0) {
27392 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
27393 +               mutex_lock(&mfs->mfs_lock);
27394 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
27395 +                       err = au_wbr_create_rr(dentry, isdir);
27396 +               mutex_unlock(&mfs->mfs_lock);
27397 +       }
27398 +
27399 +       AuDbg("b%d\n", err);
27400 +       return err;
27401 +}
27402 +
27403 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
27404 +{
27405 +       int err;
27406 +
27407 +       au_wbr_create_init_mfs(sb); /* ignore */
27408 +       err = au_wbr_create_init_rr(sb);
27409 +
27410 +       return err;
27411 +}
27412 +
27413 +/* ---------------------------------------------------------------------- */
27414 +
27415 +/* top down parent and most free space */
27416 +static int au_wbr_create_pmfs(struct dentry *dentry, int isdir)
27417 +{
27418 +       int err, e2;
27419 +       unsigned long long b;
27420 +       aufs_bindex_t bindex, bstart, bend;
27421 +       struct super_block *sb;
27422 +       struct dentry *parent, *h_parent;
27423 +       struct au_branch *br;
27424 +
27425 +       err = au_wbr_create_tdp(dentry, isdir);
27426 +       if (unlikely(err < 0))
27427 +               goto out;
27428 +       parent = dget_parent(dentry);
27429 +       bstart = au_dbstart(parent);
27430 +       bend = au_dbtaildir(parent);
27431 +       if (bstart == bend)
27432 +               goto out_parent; /* success */
27433 +
27434 +       e2 = au_wbr_create_mfs(dentry, isdir);
27435 +       if (e2 < 0)
27436 +               goto out_parent; /* success */
27437 +
27438 +       /* when the available size is equal, select upper one */
27439 +       sb = dentry->d_sb;
27440 +       br = au_sbr(sb, err);
27441 +       b = br->br_wbr->wbr_bytes;
27442 +       AuDbg("b%d, %llu\n", err, b);
27443 +
27444 +       for (bindex = bstart; bindex <= bend; bindex++) {
27445 +               h_parent = au_h_dptr(parent, bindex);
27446 +               if (!h_parent || !h_parent->d_inode)
27447 +                       continue;
27448 +
27449 +               br = au_sbr(sb, bindex);
27450 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
27451 +                       b = br->br_wbr->wbr_bytes;
27452 +                       err = bindex;
27453 +                       AuDbg("b%d, %llu\n", err, b);
27454 +               }
27455 +       }
27456 +
27457 +       if (err >= 0)
27458 +               err = au_wbr_nonopq(dentry, err);
27459 +
27460 +out_parent:
27461 +       dput(parent);
27462 +out:
27463 +       AuDbg("b%d\n", err);
27464 +       return err;
27465 +}
27466 +
27467 +/* ---------------------------------------------------------------------- */
27468 +
27469 +/* policies for copyup */
27470 +
27471 +/* top down parent */
27472 +static int au_wbr_copyup_tdp(struct dentry *dentry)
27473 +{
27474 +       return au_wbr_create_tdp(dentry, /*isdir, anything is ok*/0);
27475 +}
27476 +
27477 +/* bottom up parent */
27478 +static int au_wbr_copyup_bup(struct dentry *dentry)
27479 +{
27480 +       int err;
27481 +       aufs_bindex_t bindex, bstart;
27482 +       struct dentry *parent, *h_parent;
27483 +       struct super_block *sb;
27484 +
27485 +       err = -EROFS;
27486 +       sb = dentry->d_sb;
27487 +       parent = dget_parent(dentry);
27488 +       bstart = au_dbstart(parent);
27489 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
27490 +               h_parent = au_h_dptr(parent, bindex);
27491 +               if (!h_parent || !h_parent->d_inode)
27492 +                       continue;
27493 +
27494 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
27495 +                       err = bindex;
27496 +                       break;
27497 +               }
27498 +       }
27499 +       dput(parent);
27500 +
27501 +       /* bottom up here */
27502 +       if (unlikely(err < 0))
27503 +               err = au_wbr_bu(sb, bstart - 1);
27504 +
27505 +       AuDbg("b%d\n", err);
27506 +       return err;
27507 +}
27508 +
27509 +/* bottom up */
27510 +static int au_wbr_copyup_bu(struct dentry *dentry)
27511 +{
27512 +       int err;
27513 +       aufs_bindex_t bstart;
27514 +
27515 +       bstart = au_dbstart(dentry);
27516 +       err = au_wbr_bu(dentry->d_sb, bstart);
27517 +       AuDbg("b%d\n", err);
27518 +       if (err > bstart)
27519 +               err = au_wbr_nonopq(dentry, err);
27520 +
27521 +       AuDbg("b%d\n", err);
27522 +       return err;
27523 +}
27524 +
27525 +/* ---------------------------------------------------------------------- */
27526 +
27527 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
27528 +       [AuWbrCopyup_TDP] = {
27529 +               .copyup = au_wbr_copyup_tdp
27530 +       },
27531 +       [AuWbrCopyup_BUP] = {
27532 +               .copyup = au_wbr_copyup_bup
27533 +       },
27534 +       [AuWbrCopyup_BU] = {
27535 +               .copyup = au_wbr_copyup_bu
27536 +       }
27537 +};
27538 +
27539 +struct au_wbr_create_operations au_wbr_create_ops[] = {
27540 +       [AuWbrCreate_TDP] = {
27541 +               .create = au_wbr_create_tdp
27542 +       },
27543 +       [AuWbrCreate_RR] = {
27544 +               .create = au_wbr_create_rr,
27545 +               .init   = au_wbr_create_init_rr
27546 +       },
27547 +       [AuWbrCreate_MFS] = {
27548 +               .create = au_wbr_create_mfs,
27549 +               .init   = au_wbr_create_init_mfs,
27550 +               .fin    = au_wbr_create_fin_mfs
27551 +       },
27552 +       [AuWbrCreate_MFSV] = {
27553 +               .create = au_wbr_create_mfs,
27554 +               .init   = au_wbr_create_init_mfs,
27555 +               .fin    = au_wbr_create_fin_mfs
27556 +       },
27557 +       [AuWbrCreate_MFSRR] = {
27558 +               .create = au_wbr_create_mfsrr,
27559 +               .init   = au_wbr_create_init_mfsrr,
27560 +               .fin    = au_wbr_create_fin_mfs
27561 +       },
27562 +       [AuWbrCreate_MFSRRV] = {
27563 +               .create = au_wbr_create_mfsrr,
27564 +               .init   = au_wbr_create_init_mfsrr,
27565 +               .fin    = au_wbr_create_fin_mfs
27566 +       },
27567 +       [AuWbrCreate_PMFS] = {
27568 +               .create = au_wbr_create_pmfs,
27569 +               .init   = au_wbr_create_init_mfs,
27570 +               .fin    = au_wbr_create_fin_mfs
27571 +       },
27572 +       [AuWbrCreate_PMFSV] = {
27573 +               .create = au_wbr_create_pmfs,
27574 +               .init   = au_wbr_create_init_mfs,
27575 +               .fin    = au_wbr_create_fin_mfs
27576 +       }
27577 +};
27578 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
27579 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
27580 +++ linux/fs/aufs/whout.c       2012-03-19 12:24:13.686467926 +0100
27581 @@ -0,0 +1,1049 @@
27582 +/*
27583 + * Copyright (C) 2005-2012 Junjiro R. Okajima
27584 + *
27585 + * This program, aufs is free software; you can redistribute it and/or modify
27586 + * it under the terms of the GNU General Public License as published by
27587 + * the Free Software Foundation; either version 2 of the License, or
27588 + * (at your option) any later version.
27589 + *
27590 + * This program is distributed in the hope that it will be useful,
27591 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27592 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27593 + * GNU General Public License for more details.
27594 + *
27595 + * You should have received a copy of the GNU General Public License
27596 + * along with this program; if not, write to the Free Software
27597 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27598 + */
27599 +
27600 +/*
27601 + * whiteout for logical deletion and opaque directory
27602 + */
27603 +
27604 +#include "aufs.h"
27605 +
27606 +#define WH_MASK                        S_IRUGO
27607 +
27608 +/*
27609 + * If a directory contains this file, then it is opaque.  We start with the
27610 + * .wh. flag so that it is blocked by lookup.
27611 + */
27612 +static struct qstr diropq_name = {
27613 +       .name = AUFS_WH_DIROPQ,
27614 +       .len = sizeof(AUFS_WH_DIROPQ) - 1
27615 +};
27616 +
27617 +/*
27618 + * generate whiteout name, which is NOT terminated by NULL.
27619 + * @name: original d_name.name
27620 + * @len: original d_name.len
27621 + * @wh: whiteout qstr
27622 + * returns zero when succeeds, otherwise error.
27623 + * succeeded value as wh->name should be freed by kfree().
27624 + */
27625 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
27626 +{
27627 +       char *p;
27628 +
27629 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
27630 +               return -ENAMETOOLONG;
27631 +
27632 +       wh->len = name->len + AUFS_WH_PFX_LEN;
27633 +       p = kmalloc(wh->len, GFP_NOFS);
27634 +       wh->name = p;
27635 +       if (p) {
27636 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
27637 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
27638 +               /* smp_mb(); */
27639 +               return 0;
27640 +       }
27641 +       return -ENOMEM;
27642 +}
27643 +
27644 +/* ---------------------------------------------------------------------- */
27645 +
27646 +/*
27647 + * test if the @wh_name exists under @h_parent.
27648 + * @try_sio specifies the necessary of super-io.
27649 + */
27650 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
27651 +              struct au_branch *br, int try_sio)
27652 +{
27653 +       int err;
27654 +       struct dentry *wh_dentry;
27655 +
27656 +       if (!try_sio)
27657 +               wh_dentry = au_lkup_one(wh_name, h_parent, br, /*nd*/NULL);
27658 +       else
27659 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
27660 +       err = PTR_ERR(wh_dentry);
27661 +       if (IS_ERR(wh_dentry))
27662 +               goto out;
27663 +
27664 +       err = 0;
27665 +       if (!wh_dentry->d_inode)
27666 +               goto out_wh; /* success */
27667 +
27668 +       err = 1;
27669 +       if (S_ISREG(wh_dentry->d_inode->i_mode))
27670 +               goto out_wh; /* success */
27671 +
27672 +       err = -EIO;
27673 +       AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
27674 +               AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
27675 +
27676 +out_wh:
27677 +       dput(wh_dentry);
27678 +out:
27679 +       return err;
27680 +}
27681 +
27682 +/*
27683 + * test if the @h_dentry sets opaque or not.
27684 + */
27685 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
27686 +{
27687 +       int err;
27688 +       struct inode *h_dir;
27689 +
27690 +       h_dir = h_dentry->d_inode;
27691 +       err = au_wh_test(h_dentry, &diropq_name, br,
27692 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
27693 +       return err;
27694 +}
27695 +
27696 +/*
27697 + * returns a negative dentry whose name is unique and temporary.
27698 + */
27699 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
27700 +                            struct qstr *prefix)
27701 +{
27702 +       struct dentry *dentry;
27703 +       int i;
27704 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
27705 +               *name, *p;
27706 +       /* strict atomic_t is unnecessary here */
27707 +       static unsigned short cnt;
27708 +       struct qstr qs;
27709 +
27710 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
27711 +
27712 +       name = defname;
27713 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
27714 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
27715 +               dentry = ERR_PTR(-ENAMETOOLONG);
27716 +               if (unlikely(qs.len > NAME_MAX))
27717 +                       goto out;
27718 +               dentry = ERR_PTR(-ENOMEM);
27719 +               name = kmalloc(qs.len + 1, GFP_NOFS);
27720 +               if (unlikely(!name))
27721 +                       goto out;
27722 +       }
27723 +
27724 +       /* doubly whiteout-ed */
27725 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
27726 +       p = name + AUFS_WH_PFX_LEN * 2;
27727 +       memcpy(p, prefix->name, prefix->len);
27728 +       p += prefix->len;
27729 +       *p++ = '.';
27730 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
27731 +
27732 +       qs.name = name;
27733 +       for (i = 0; i < 3; i++) {
27734 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
27735 +               dentry = au_sio_lkup_one(&qs, h_parent, br);
27736 +               if (IS_ERR(dentry) || !dentry->d_inode)
27737 +                       goto out_name;
27738 +               dput(dentry);
27739 +       }
27740 +       /* pr_warning("could not get random name\n"); */
27741 +       dentry = ERR_PTR(-EEXIST);
27742 +       AuDbg("%.*s\n", AuLNPair(&qs));
27743 +       BUG();
27744 +
27745 +out_name:
27746 +       if (name != defname)
27747 +               kfree(name);
27748 +out:
27749 +       AuTraceErrPtr(dentry);
27750 +       return dentry;
27751 +}
27752 +
27753 +/*
27754 + * rename the @h_dentry on @br to the whiteouted temporary name.
27755 + */
27756 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
27757 +{
27758 +       int err;
27759 +       struct path h_path = {
27760 +               .mnt = br->br_mnt
27761 +       };
27762 +       struct inode *h_dir;
27763 +       struct dentry *h_parent;
27764 +
27765 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
27766 +       h_dir = h_parent->d_inode;
27767 +       IMustLock(h_dir);
27768 +
27769 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
27770 +       err = PTR_ERR(h_path.dentry);
27771 +       if (IS_ERR(h_path.dentry))
27772 +               goto out;
27773 +
27774 +       /* under the same dir, no need to lock_rename() */
27775 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
27776 +       AuTraceErr(err);
27777 +       dput(h_path.dentry);
27778 +
27779 +out:
27780 +       AuTraceErr(err);
27781 +       return err;
27782 +}
27783 +
27784 +/* ---------------------------------------------------------------------- */
27785 +/*
27786 + * functions for removing a whiteout
27787 + */
27788 +
27789 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
27790 +{
27791 +       int force;
27792 +
27793 +       /*
27794 +        * forces superio when the dir has a sticky bit.
27795 +        * this may be a violation of unix fs semantics.
27796 +        */
27797 +       force = (h_dir->i_mode & S_ISVTX)
27798 +               && h_path->dentry->d_inode->i_uid != current_fsuid();
27799 +       return vfsub_unlink(h_dir, h_path, force);
27800 +}
27801 +
27802 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
27803 +                       struct dentry *dentry)
27804 +{
27805 +       int err;
27806 +
27807 +       err = do_unlink_wh(h_dir, h_path);
27808 +       if (!err && dentry)
27809 +               au_set_dbwh(dentry, -1);
27810 +
27811 +       return err;
27812 +}
27813 +
27814 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
27815 +                         struct au_branch *br)
27816 +{
27817 +       int err;
27818 +       struct path h_path = {
27819 +               .mnt = br->br_mnt
27820 +       };
27821 +
27822 +       err = 0;
27823 +       h_path.dentry = au_lkup_one(wh, h_parent, br, /*nd*/NULL);
27824 +       if (IS_ERR(h_path.dentry))
27825 +               err = PTR_ERR(h_path.dentry);
27826 +       else {
27827 +               if (h_path.dentry->d_inode
27828 +                   && S_ISREG(h_path.dentry->d_inode->i_mode))
27829 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
27830 +               dput(h_path.dentry);
27831 +       }
27832 +
27833 +       return err;
27834 +}
27835 +
27836 +/* ---------------------------------------------------------------------- */
27837 +/*
27838 + * initialize/clean whiteout for a branch
27839 + */
27840 +
27841 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
27842 +                       const int isdir)
27843 +{
27844 +       int err;
27845 +
27846 +       if (!whpath->dentry->d_inode)
27847 +               return;
27848 +
27849 +       err = mnt_want_write(whpath->mnt);
27850 +       if (!err) {
27851 +               if (isdir)
27852 +                       err = vfsub_rmdir(h_dir, whpath);
27853 +               else
27854 +                       err = vfsub_unlink(h_dir, whpath, /*force*/0);
27855 +               mnt_drop_write(whpath->mnt);
27856 +       }
27857 +       if (unlikely(err))
27858 +               pr_warning("failed removing %.*s (%d), ignored.\n",
27859 +                          AuDLNPair(whpath->dentry), err);
27860 +}
27861 +
27862 +static int test_linkable(struct dentry *h_root)
27863 +{
27864 +       struct inode *h_dir = h_root->d_inode;
27865 +
27866 +       if (h_dir->i_op->link)
27867 +               return 0;
27868 +
27869 +       pr_err("%.*s (%s) doesn't support link(2), use noplink and rw+nolwh\n",
27870 +              AuDLNPair(h_root), au_sbtype(h_root->d_sb));
27871 +       return -ENOSYS;
27872 +}
27873 +
27874 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
27875 +static int au_whdir(struct inode *h_dir, struct path *path)
27876 +{
27877 +       int err;
27878 +
27879 +       err = -EEXIST;
27880 +       if (!path->dentry->d_inode) {
27881 +               int mode = S_IRWXU;
27882 +
27883 +               if (au_test_nfs(path->dentry->d_sb))
27884 +                       mode |= S_IXUGO;
27885 +               err = mnt_want_write(path->mnt);
27886 +               if (!err) {
27887 +                       err = vfsub_mkdir(h_dir, path, mode);
27888 +                       mnt_drop_write(path->mnt);
27889 +               }
27890 +       } else if (S_ISDIR(path->dentry->d_inode->i_mode))
27891 +               err = 0;
27892 +       else
27893 +               pr_err("unknown %.*s exists\n", AuDLNPair(path->dentry));
27894 +
27895 +       return err;
27896 +}
27897 +
27898 +struct au_wh_base {
27899 +       const struct qstr *name;
27900 +       struct dentry *dentry;
27901 +};
27902 +
27903 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
27904 +                         struct path *h_path)
27905 +{
27906 +       h_path->dentry = base[AuBrWh_BASE].dentry;
27907 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
27908 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
27909 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
27910 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
27911 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
27912 +}
27913 +
27914 +/*
27915 + * returns tri-state,
27916 + * minus: error, caller should print the mesage
27917 + * zero: succuess
27918 + * plus: error, caller should NOT print the mesage
27919 + */
27920 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
27921 +                               int do_plink, struct au_wh_base base[],
27922 +                               struct path *h_path)
27923 +{
27924 +       int err;
27925 +       struct inode *h_dir;
27926 +
27927 +       h_dir = h_root->d_inode;
27928 +       h_path->dentry = base[AuBrWh_BASE].dentry;
27929 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
27930 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
27931 +       if (do_plink) {
27932 +               err = test_linkable(h_root);
27933 +               if (unlikely(err)) {
27934 +                       err = 1;
27935 +                       goto out;
27936 +               }
27937 +
27938 +               err = au_whdir(h_dir, h_path);
27939 +               if (unlikely(err))
27940 +                       goto out;
27941 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
27942 +       } else
27943 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
27944 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
27945 +       err = au_whdir(h_dir, h_path);
27946 +       if (unlikely(err))
27947 +               goto out;
27948 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
27949 +
27950 +out:
27951 +       return err;
27952 +}
27953 +
27954 +/*
27955 + * for the moment, aufs supports the branch filesystem which does not support
27956 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
27957 + * copyup failed. finally, such filesystem will not be used as the writable
27958 + * branch.
27959 + *
27960 + * returns tri-state, see above.
27961 + */
27962 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
27963 +                        int do_plink, struct au_wh_base base[],
27964 +                        struct path *h_path)
27965 +{
27966 +       int err;
27967 +       struct inode *h_dir;
27968 +
27969 +       WbrWhMustWriteLock(wbr);
27970 +
27971 +       err = test_linkable(h_root);
27972 +       if (unlikely(err)) {
27973 +               err = 1;
27974 +               goto out;
27975 +       }
27976 +
27977 +       /*
27978 +        * todo: should this create be done in /sbin/mount.aufs helper?
27979 +        */
27980 +       err = -EEXIST;
27981 +       h_dir = h_root->d_inode;
27982 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
27983 +               err = mnt_want_write(h_path->mnt);
27984 +               if (!err) {
27985 +                       h_path->dentry = base[AuBrWh_BASE].dentry;
27986 +                       err = vfsub_create(h_dir, h_path, WH_MASK);
27987 +                       mnt_drop_write(h_path->mnt);
27988 +               }
27989 +       } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
27990 +               err = 0;
27991 +       else
27992 +               pr_err("unknown %.*s/%.*s exists\n",
27993 +                      AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
27994 +       if (unlikely(err))
27995 +               goto out;
27996 +
27997 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
27998 +       if (do_plink) {
27999 +               err = au_whdir(h_dir, h_path);
28000 +               if (unlikely(err))
28001 +                       goto out;
28002 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
28003 +       } else
28004 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
28005 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
28006 +
28007 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
28008 +       err = au_whdir(h_dir, h_path);
28009 +       if (unlikely(err))
28010 +               goto out;
28011 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
28012 +
28013 +out:
28014 +       return err;
28015 +}
28016 +
28017 +/*
28018 + * initialize the whiteout base file/dir for @br.
28019 + */
28020 +int au_wh_init(struct dentry *h_root, struct au_branch *br,
28021 +              struct super_block *sb)
28022 +{
28023 +       int err, i;
28024 +       const unsigned char do_plink
28025 +               = !!au_opt_test(au_mntflags(sb), PLINK);
28026 +       struct path path = {
28027 +               .mnt = br->br_mnt
28028 +       };
28029 +       struct inode *h_dir;
28030 +       struct au_wbr *wbr = br->br_wbr;
28031 +       static const struct qstr base_name[] = {
28032 +               [AuBrWh_BASE] = {
28033 +                       .name   = AUFS_BASE_NAME,
28034 +                       .len    = sizeof(AUFS_BASE_NAME) - 1
28035 +               },
28036 +               [AuBrWh_PLINK] = {
28037 +                       .name   = AUFS_PLINKDIR_NAME,
28038 +                       .len    = sizeof(AUFS_PLINKDIR_NAME) - 1
28039 +               },
28040 +               [AuBrWh_ORPH] = {
28041 +                       .name   = AUFS_ORPHDIR_NAME,
28042 +                       .len    = sizeof(AUFS_ORPHDIR_NAME) - 1
28043 +               }
28044 +       };
28045 +       struct au_wh_base base[] = {
28046 +               [AuBrWh_BASE] = {
28047 +                       .name   = base_name + AuBrWh_BASE,
28048 +                       .dentry = NULL
28049 +               },
28050 +               [AuBrWh_PLINK] = {
28051 +                       .name   = base_name + AuBrWh_PLINK,
28052 +                       .dentry = NULL
28053 +               },
28054 +               [AuBrWh_ORPH] = {
28055 +                       .name   = base_name + AuBrWh_ORPH,
28056 +                       .dentry = NULL
28057 +               }
28058 +       };
28059 +
28060 +       if (wbr)
28061 +               WbrWhMustWriteLock(wbr);
28062 +
28063 +       for (i = 0; i < AuBrWh_Last; i++) {
28064 +               /* doubly whiteouted */
28065 +               struct dentry *d;
28066 +
28067 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
28068 +               err = PTR_ERR(d);
28069 +               if (IS_ERR(d))
28070 +                       goto out;
28071 +
28072 +               base[i].dentry = d;
28073 +               AuDebugOn(wbr
28074 +                         && wbr->wbr_wh[i]
28075 +                         && wbr->wbr_wh[i] != base[i].dentry);
28076 +       }
28077 +
28078 +       if (wbr)
28079 +               for (i = 0; i < AuBrWh_Last; i++) {
28080 +                       dput(wbr->wbr_wh[i]);
28081 +                       wbr->wbr_wh[i] = NULL;
28082 +               }
28083 +
28084 +       err = 0;
28085 +       if (!au_br_writable(br->br_perm)) {
28086 +               h_dir = h_root->d_inode;
28087 +               au_wh_init_ro(h_dir, base, &path);
28088 +       } else if (!au_br_wh_linkable(br->br_perm)) {
28089 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
28090 +               if (err > 0)
28091 +                       goto out;
28092 +               else if (err)
28093 +                       goto out_err;
28094 +       } else {
28095 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
28096 +               if (err > 0)
28097 +                       goto out;
28098 +               else if (err)
28099 +                       goto out_err;
28100 +       }
28101 +       goto out; /* success */
28102 +
28103 +out_err:
28104 +       pr_err("an error(%d) on the writable branch %.*s(%s)\n",
28105 +              err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
28106 +out:
28107 +       for (i = 0; i < AuBrWh_Last; i++)
28108 +               dput(base[i].dentry);
28109 +       return err;
28110 +}
28111 +
28112 +/* ---------------------------------------------------------------------- */
28113 +/*
28114 + * whiteouts are all hard-linked usually.
28115 + * when its link count reaches a ceiling, we create a new whiteout base
28116 + * asynchronously.
28117 + */
28118 +
28119 +struct reinit_br_wh {
28120 +       struct super_block *sb;
28121 +       struct au_branch *br;
28122 +};
28123 +
28124 +static void reinit_br_wh(void *arg)
28125 +{
28126 +       int err;
28127 +       aufs_bindex_t bindex;
28128 +       struct path h_path;
28129 +       struct reinit_br_wh *a = arg;
28130 +       struct au_wbr *wbr;
28131 +       struct inode *dir;
28132 +       struct dentry *h_root;
28133 +       struct au_hinode *hdir;
28134 +
28135 +       err = 0;
28136 +       wbr = a->br->br_wbr;
28137 +       /* big aufs lock */
28138 +       si_noflush_write_lock(a->sb);
28139 +       if (!au_br_writable(a->br->br_perm))
28140 +               goto out;
28141 +       bindex = au_br_index(a->sb, a->br->br_id);
28142 +       if (unlikely(bindex < 0))
28143 +               goto out;
28144 +
28145 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
28146 +       dir = a->sb->s_root->d_inode;
28147 +       hdir = au_hi(dir, bindex);
28148 +       h_root = au_h_dptr(a->sb->s_root, bindex);
28149 +
28150 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
28151 +       wbr_wh_write_lock(wbr);
28152 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
28153 +                         h_root, a->br);
28154 +       if (!err) {
28155 +               err = mnt_want_write(a->br->br_mnt);
28156 +               if (!err) {
28157 +                       h_path.dentry = wbr->wbr_whbase;
28158 +                       h_path.mnt = a->br->br_mnt;
28159 +                       err = vfsub_unlink(hdir->hi_inode, &h_path, /*force*/0);
28160 +                       mnt_drop_write(a->br->br_mnt);
28161 +               }
28162 +       } else {
28163 +               pr_warning("%.*s is moved, ignored\n",
28164 +                          AuDLNPair(wbr->wbr_whbase));
28165 +               err = 0;
28166 +       }
28167 +       dput(wbr->wbr_whbase);
28168 +       wbr->wbr_whbase = NULL;
28169 +       if (!err)
28170 +               err = au_wh_init(h_root, a->br, a->sb);
28171 +       wbr_wh_write_unlock(wbr);
28172 +       au_hn_imtx_unlock(hdir);
28173 +       di_read_unlock(a->sb->s_root, AuLock_IR);
28174 +
28175 +out:
28176 +       if (wbr)
28177 +               atomic_dec(&wbr->wbr_wh_running);
28178 +       atomic_dec(&a->br->br_count);
28179 +       si_write_unlock(a->sb);
28180 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
28181 +       kfree(arg);
28182 +       if (unlikely(err))
28183 +               AuIOErr("err %d\n", err);
28184 +}
28185 +
28186 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
28187 +{
28188 +       int do_dec, wkq_err;
28189 +       struct reinit_br_wh *arg;
28190 +
28191 +       do_dec = 1;
28192 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
28193 +               goto out;
28194 +
28195 +       /* ignore ENOMEM */
28196 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
28197 +       if (arg) {
28198 +               /*
28199 +                * dec(wh_running), kfree(arg) and dec(br_count)
28200 +                * in reinit function
28201 +                */
28202 +               arg->sb = sb;
28203 +               arg->br = br;
28204 +               atomic_inc(&br->br_count);
28205 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
28206 +               if (unlikely(wkq_err)) {
28207 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
28208 +                       atomic_dec(&br->br_count);
28209 +                       kfree(arg);
28210 +               }
28211 +               do_dec = 0;
28212 +       }
28213 +
28214 +out:
28215 +       if (do_dec)
28216 +               atomic_dec(&br->br_wbr->wbr_wh_running);
28217 +}
28218 +
28219 +/* ---------------------------------------------------------------------- */
28220 +
28221 +/*
28222 + * create the whiteout @wh.
28223 + */
28224 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
28225 +                            struct dentry *wh)
28226 +{
28227 +       int err;
28228 +       struct path h_path = {
28229 +               .dentry = wh
28230 +       };
28231 +       struct au_branch *br;
28232 +       struct au_wbr *wbr;
28233 +       struct dentry *h_parent;
28234 +       struct inode *h_dir;
28235 +
28236 +       h_parent = wh->d_parent; /* dir inode is locked */
28237 +       h_dir = h_parent->d_inode;
28238 +       IMustLock(h_dir);
28239 +
28240 +       br = au_sbr(sb, bindex);
28241 +       h_path.mnt = br->br_mnt;
28242 +       wbr = br->br_wbr;
28243 +       wbr_wh_read_lock(wbr);
28244 +       if (wbr->wbr_whbase) {
28245 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path);
28246 +               if (!err || err != -EMLINK)
28247 +                       goto out;
28248 +
28249 +               /* link count full. re-initialize br_whbase. */
28250 +               kick_reinit_br_wh(sb, br);
28251 +       }
28252 +
28253 +       /* return this error in this context */
28254 +       err = vfsub_create(h_dir, &h_path, WH_MASK);
28255 +
28256 +out:
28257 +       wbr_wh_read_unlock(wbr);
28258 +       return err;
28259 +}
28260 +
28261 +/* ---------------------------------------------------------------------- */
28262 +
28263 +/*
28264 + * create or remove the diropq.
28265 + */
28266 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
28267 +                               unsigned int flags)
28268 +{
28269 +       struct dentry *opq_dentry, *h_dentry;
28270 +       struct super_block *sb;
28271 +       struct au_branch *br;
28272 +       int err;
28273 +
28274 +       sb = dentry->d_sb;
28275 +       br = au_sbr(sb, bindex);
28276 +       h_dentry = au_h_dptr(dentry, bindex);
28277 +       opq_dentry = au_lkup_one(&diropq_name, h_dentry, br, /*nd*/NULL);
28278 +       if (IS_ERR(opq_dentry))
28279 +               goto out;
28280 +
28281 +       if (au_ftest_diropq(flags, CREATE)) {
28282 +               err = link_or_create_wh(sb, bindex, opq_dentry);
28283 +               if (!err) {
28284 +                       au_set_dbdiropq(dentry, bindex);
28285 +                       goto out; /* success */
28286 +               }
28287 +       } else {
28288 +               struct path tmp = {
28289 +                       .dentry = opq_dentry,
28290 +                       .mnt    = br->br_mnt
28291 +               };
28292 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
28293 +               if (!err)
28294 +                       au_set_dbdiropq(dentry, -1);
28295 +       }
28296 +       dput(opq_dentry);
28297 +       opq_dentry = ERR_PTR(err);
28298 +
28299 +out:
28300 +       return opq_dentry;
28301 +}
28302 +
28303 +struct do_diropq_args {
28304 +       struct dentry **errp;
28305 +       struct dentry *dentry;
28306 +       aufs_bindex_t bindex;
28307 +       unsigned int flags;
28308 +};
28309 +
28310 +static void call_do_diropq(void *args)
28311 +{
28312 +       struct do_diropq_args *a = args;
28313 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
28314 +}
28315 +
28316 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
28317 +                            unsigned int flags)
28318 +{
28319 +       struct dentry *diropq, *h_dentry;
28320 +
28321 +       h_dentry = au_h_dptr(dentry, bindex);
28322 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
28323 +               diropq = do_diropq(dentry, bindex, flags);
28324 +       else {
28325 +               int wkq_err;
28326 +               struct do_diropq_args args = {
28327 +                       .errp           = &diropq,
28328 +                       .dentry         = dentry,
28329 +                       .bindex         = bindex,
28330 +                       .flags          = flags
28331 +               };
28332 +
28333 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
28334 +               if (unlikely(wkq_err))
28335 +                       diropq = ERR_PTR(wkq_err);
28336 +       }
28337 +
28338 +       return diropq;
28339 +}
28340 +
28341 +/* ---------------------------------------------------------------------- */
28342 +
28343 +/*
28344 + * lookup whiteout dentry.
28345 + * @h_parent: lower parent dentry which must exist and be locked
28346 + * @base_name: name of dentry which will be whiteouted
28347 + * returns dentry for whiteout.
28348 + */
28349 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
28350 +                         struct au_branch *br)
28351 +{
28352 +       int err;
28353 +       struct qstr wh_name;
28354 +       struct dentry *wh_dentry;
28355 +
28356 +       err = au_wh_name_alloc(&wh_name, base_name);
28357 +       wh_dentry = ERR_PTR(err);
28358 +       if (!err) {
28359 +               wh_dentry = au_lkup_one(&wh_name, h_parent, br, /*nd*/NULL);
28360 +               kfree(wh_name.name);
28361 +       }
28362 +       return wh_dentry;
28363 +}
28364 +
28365 +/*
28366 + * link/create a whiteout for @dentry on @bindex.
28367 + */
28368 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
28369 +                           struct dentry *h_parent)
28370 +{
28371 +       struct dentry *wh_dentry;
28372 +       struct super_block *sb;
28373 +       int err;
28374 +
28375 +       sb = dentry->d_sb;
28376 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
28377 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
28378 +               err = link_or_create_wh(sb, bindex, wh_dentry);
28379 +               if (!err)
28380 +                       au_set_dbwh(dentry, bindex);
28381 +               else {
28382 +                       dput(wh_dentry);
28383 +                       wh_dentry = ERR_PTR(err);
28384 +               }
28385 +       }
28386 +
28387 +       return wh_dentry;
28388 +}
28389 +
28390 +/* ---------------------------------------------------------------------- */
28391 +
28392 +/* Delete all whiteouts in this directory on branch bindex. */
28393 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
28394 +                          aufs_bindex_t bindex, struct au_branch *br)
28395 +{
28396 +       int err;
28397 +       unsigned long ul, n;
28398 +       struct qstr wh_name;
28399 +       char *p;
28400 +       struct hlist_head *head;
28401 +       struct au_vdir_wh *tpos;
28402 +       struct hlist_node *pos;
28403 +       struct au_vdir_destr *str;
28404 +
28405 +       err = -ENOMEM;
28406 +       p = __getname_gfp(GFP_NOFS);
28407 +       wh_name.name = p;
28408 +       if (unlikely(!wh_name.name))
28409 +               goto out;
28410 +
28411 +       err = 0;
28412 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
28413 +       p += AUFS_WH_PFX_LEN;
28414 +       n = whlist->nh_num;
28415 +       head = whlist->nh_head;
28416 +       for (ul = 0; !err && ul < n; ul++, head++) {
28417 +               hlist_for_each_entry(tpos, pos, head, wh_hash) {
28418 +                       if (tpos->wh_bindex != bindex)
28419 +                               continue;
28420 +
28421 +                       str = &tpos->wh_str;
28422 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
28423 +                               memcpy(p, str->name, str->len);
28424 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
28425 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
28426 +                               if (!err)
28427 +                                       continue;
28428 +                               break;
28429 +                       }
28430 +                       AuIOErr("whiteout name too long %.*s\n",
28431 +                               str->len, str->name);
28432 +                       err = -EIO;
28433 +                       break;
28434 +               }
28435 +       }
28436 +       __putname(wh_name.name);
28437 +
28438 +out:
28439 +       return err;
28440 +}
28441 +
28442 +struct del_wh_children_args {
28443 +       int *errp;
28444 +       struct dentry *h_dentry;
28445 +       struct au_nhash *whlist;
28446 +       aufs_bindex_t bindex;
28447 +       struct au_branch *br;
28448 +};
28449 +
28450 +static void call_del_wh_children(void *args)
28451 +{
28452 +       struct del_wh_children_args *a = args;
28453 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
28454 +}
28455 +
28456 +/* ---------------------------------------------------------------------- */
28457 +
28458 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
28459 +{
28460 +       struct au_whtmp_rmdir *whtmp;
28461 +       int err;
28462 +       unsigned int rdhash;
28463 +
28464 +       SiMustAnyLock(sb);
28465 +
28466 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
28467 +       if (unlikely(!whtmp)) {
28468 +               whtmp = ERR_PTR(-ENOMEM);
28469 +               goto out;
28470 +       }
28471 +
28472 +       whtmp->dir = NULL;
28473 +       whtmp->br = NULL;
28474 +       whtmp->wh_dentry = NULL;
28475 +       /* no estimation for dir size */
28476 +       rdhash = au_sbi(sb)->si_rdhash;
28477 +       if (!rdhash)
28478 +               rdhash = AUFS_RDHASH_DEF;
28479 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
28480 +       if (unlikely(err)) {
28481 +               kfree(whtmp);
28482 +               whtmp = ERR_PTR(err);
28483 +       }
28484 +
28485 +out:
28486 +       return whtmp;
28487 +}
28488 +
28489 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
28490 +{
28491 +       if (whtmp->br)
28492 +               atomic_dec(&whtmp->br->br_count);
28493 +       dput(whtmp->wh_dentry);
28494 +       iput(whtmp->dir);
28495 +       au_nhash_wh_free(&whtmp->whlist);
28496 +       kfree(whtmp);
28497 +}
28498 +
28499 +/*
28500 + * rmdir the whiteouted temporary named dir @h_dentry.
28501 + * @whlist: whiteouted children.
28502 + */
28503 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
28504 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
28505 +{
28506 +       int err;
28507 +       struct path h_tmp;
28508 +       struct inode *wh_inode, *h_dir;
28509 +       struct au_branch *br;
28510 +
28511 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
28512 +       IMustLock(h_dir);
28513 +
28514 +       br = au_sbr(dir->i_sb, bindex);
28515 +       wh_inode = wh_dentry->d_inode;
28516 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
28517 +
28518 +       /*
28519 +        * someone else might change some whiteouts while we were sleeping.
28520 +        * it means this whlist may have an obsoleted entry.
28521 +        */
28522 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
28523 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
28524 +       else {
28525 +               int wkq_err;
28526 +               struct del_wh_children_args args = {
28527 +                       .errp           = &err,
28528 +                       .h_dentry       = wh_dentry,
28529 +                       .whlist         = whlist,
28530 +                       .bindex         = bindex,
28531 +                       .br             = br
28532 +               };
28533 +
28534 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
28535 +               if (unlikely(wkq_err))
28536 +                       err = wkq_err;
28537 +       }
28538 +       mutex_unlock(&wh_inode->i_mutex);
28539 +
28540 +       if (!err) {
28541 +               h_tmp.dentry = wh_dentry;
28542 +               h_tmp.mnt = br->br_mnt;
28543 +               err = vfsub_rmdir(h_dir, &h_tmp);
28544 +       }
28545 +
28546 +       if (!err) {
28547 +               if (au_ibstart(dir) == bindex) {
28548 +                       /* todo: dir->i_mutex is necessary */
28549 +                       au_cpup_attr_timesizes(dir);
28550 +                       vfsub_drop_nlink(dir);
28551 +               }
28552 +               return 0; /* success */
28553 +       }
28554 +
28555 +       pr_warning("failed removing %.*s(%d), ignored\n",
28556 +                  AuDLNPair(wh_dentry), err);
28557 +       return err;
28558 +}
28559 +
28560 +static void call_rmdir_whtmp(void *args)
28561 +{
28562 +       int err;
28563 +       aufs_bindex_t bindex;
28564 +       struct au_whtmp_rmdir *a = args;
28565 +       struct super_block *sb;
28566 +       struct dentry *h_parent;
28567 +       struct inode *h_dir;
28568 +       struct au_hinode *hdir;
28569 +
28570 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
28571 +       /* mutex_lock(&a->dir->i_mutex); */
28572 +       err = -EROFS;
28573 +       sb = a->dir->i_sb;
28574 +       si_read_lock(sb, !AuLock_FLUSH);
28575 +       if (!au_br_writable(a->br->br_perm))
28576 +               goto out;
28577 +       bindex = au_br_index(sb, a->br->br_id);
28578 +       if (unlikely(bindex < 0))
28579 +               goto out;
28580 +
28581 +       err = -EIO;
28582 +       ii_write_lock_parent(a->dir);
28583 +       h_parent = dget_parent(a->wh_dentry);
28584 +       h_dir = h_parent->d_inode;
28585 +       hdir = au_hi(a->dir, bindex);
28586 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
28587 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
28588 +                         a->br);
28589 +       if (!err) {
28590 +               err = mnt_want_write(a->br->br_mnt);
28591 +               if (!err) {
28592 +                       err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry,
28593 +                                            &a->whlist);
28594 +                       mnt_drop_write(a->br->br_mnt);
28595 +               }
28596 +       }
28597 +       au_hn_imtx_unlock(hdir);
28598 +       dput(h_parent);
28599 +       ii_write_unlock(a->dir);
28600 +
28601 +out:
28602 +       /* mutex_unlock(&a->dir->i_mutex); */
28603 +       au_whtmp_rmdir_free(a);
28604 +       si_read_unlock(sb);
28605 +       au_nwt_done(&au_sbi(sb)->si_nowait);
28606 +       if (unlikely(err))
28607 +               AuIOErr("err %d\n", err);
28608 +}
28609 +
28610 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
28611 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
28612 +{
28613 +       int wkq_err;
28614 +       struct super_block *sb;
28615 +
28616 +       IMustLock(dir);
28617 +
28618 +       /* all post-process will be done in do_rmdir_whtmp(). */
28619 +       sb = dir->i_sb;
28620 +       args->dir = au_igrab(dir);
28621 +       args->br = au_sbr(sb, bindex);
28622 +       atomic_inc(&args->br->br_count);
28623 +       args->wh_dentry = dget(wh_dentry);
28624 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
28625 +       if (unlikely(wkq_err)) {
28626 +               pr_warning("rmdir error %.*s (%d), ignored\n",
28627 +                          AuDLNPair(wh_dentry), wkq_err);
28628 +               au_whtmp_rmdir_free(args);
28629 +       }
28630 +}
28631 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
28632 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
28633 +++ linux/fs/aufs/whout.h       2012-03-19 12:24:13.686467926 +0100
28634 @@ -0,0 +1,88 @@
28635 +/*
28636 + * Copyright (C) 2005-2012 Junjiro R. Okajima
28637 + *
28638 + * This program, aufs is free software; you can redistribute it and/or modify
28639 + * it under the terms of the GNU General Public License as published by
28640 + * the Free Software Foundation; either version 2 of the License, or
28641 + * (at your option) any later version.
28642 + *
28643 + * This program is distributed in the hope that it will be useful,
28644 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28645 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28646 + * GNU General Public License for more details.
28647 + *
28648 + * You should have received a copy of the GNU General Public License
28649 + * along with this program; if not, write to the Free Software
28650 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28651 + */
28652 +
28653 +/*
28654 + * whiteout for logical deletion and opaque directory
28655 + */
28656 +
28657 +#ifndef __AUFS_WHOUT_H__
28658 +#define __AUFS_WHOUT_H__
28659 +
28660 +#ifdef __KERNEL__
28661 +
28662 +#include "dir.h"
28663 +
28664 +/* whout.c */
28665 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
28666 +struct au_branch;
28667 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
28668 +              struct au_branch *br, int try_sio);
28669 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br);
28670 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
28671 +                            struct qstr *prefix);
28672 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
28673 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
28674 +                       struct dentry *dentry);
28675 +int au_wh_init(struct dentry *h_parent, struct au_branch *br,
28676 +              struct super_block *sb);
28677 +
28678 +/* diropq flags */
28679 +#define AuDiropq_CREATE        1
28680 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
28681 +#define au_fset_diropq(flags, name) \
28682 +       do { (flags) |= AuDiropq_##name; } while (0)
28683 +#define au_fclr_diropq(flags, name) \
28684 +       do { (flags) &= ~AuDiropq_##name; } while (0)
28685 +
28686 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
28687 +                            unsigned int flags);
28688 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
28689 +                         struct au_branch *br);
28690 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
28691 +                           struct dentry *h_parent);
28692 +
28693 +/* real rmdir for the whiteout-ed dir */
28694 +struct au_whtmp_rmdir {
28695 +       struct inode *dir;
28696 +       struct au_branch *br;
28697 +       struct dentry *wh_dentry;
28698 +       struct au_nhash whlist;
28699 +};
28700 +
28701 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
28702 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
28703 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
28704 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
28705 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
28706 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
28707 +
28708 +/* ---------------------------------------------------------------------- */
28709 +
28710 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
28711 +                                             aufs_bindex_t bindex)
28712 +{
28713 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
28714 +}
28715 +
28716 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
28717 +{
28718 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
28719 +}
28720 +
28721 +#endif /* __KERNEL__ */
28722 +#endif /* __AUFS_WHOUT_H__ */
28723 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
28724 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
28725 +++ linux/fs/aufs/wkq.c 2012-03-19 12:24:13.686467926 +0100
28726 @@ -0,0 +1,214 @@
28727 +/*
28728 + * Copyright (C) 2005-2012 Junjiro R. Okajima
28729 + *
28730 + * This program, aufs is free software; you can redistribute it and/or modify
28731 + * it under the terms of the GNU General Public License as published by
28732 + * the Free Software Foundation; either version 2 of the License, or
28733 + * (at your option) any later version.
28734 + *
28735 + * This program is distributed in the hope that it will be useful,
28736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28738 + * GNU General Public License for more details.
28739 + *
28740 + * You should have received a copy of the GNU General Public License
28741 + * along with this program; if not, write to the Free Software
28742 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28743 + */
28744 +
28745 +/*
28746 + * workqueue for asynchronous/super-io operations
28747 + * todo: try new dredential scheme
28748 + */
28749 +
28750 +#include <linux/module.h>
28751 +#include "aufs.h"
28752 +
28753 +/* internal workqueue named AUFS_WKQ_NAME */
28754 +
28755 +static struct workqueue_struct *au_wkq;
28756 +
28757 +struct au_wkinfo {
28758 +       struct work_struct wk;
28759 +       struct kobject *kobj;
28760 +
28761 +       unsigned int flags; /* see wkq.h */
28762 +
28763 +       au_wkq_func_t func;
28764 +       void *args;
28765 +
28766 +       struct completion *comp;
28767 +};
28768 +
28769 +/* ---------------------------------------------------------------------- */
28770 +
28771 +static void wkq_func(struct work_struct *wk)
28772 +{
28773 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
28774 +
28775 +       AuDebugOn(current_fsuid());
28776 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
28777 +
28778 +       wkinfo->func(wkinfo->args);
28779 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
28780 +               complete(wkinfo->comp);
28781 +       else {
28782 +               kobject_put(wkinfo->kobj);
28783 +               module_put(THIS_MODULE); /* todo: ?? */
28784 +               kfree(wkinfo);
28785 +       }
28786 +}
28787 +
28788 +/*
28789 + * Since struct completion is large, try allocating it dynamically.
28790 + */
28791 +#if defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS)
28792 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
28793 +
28794 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
28795 +{
28796 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
28797 +       if (*comp) {
28798 +               init_completion(*comp);
28799 +               wkinfo->comp = *comp;
28800 +               return 0;
28801 +       }
28802 +       return -ENOMEM;
28803 +}
28804 +
28805 +static void au_wkq_comp_free(struct completion *comp)
28806 +{
28807 +       kfree(comp);
28808 +}
28809 +
28810 +#else
28811 +
28812 +/* no braces */
28813 +#define AuWkqCompDeclare(name) \
28814 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
28815 +       struct completion *comp = &_ ## name
28816 +
28817 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
28818 +{
28819 +       wkinfo->comp = *comp;
28820 +       return 0;
28821 +}
28822 +
28823 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
28824 +{
28825 +       /* empty */
28826 +}
28827 +#endif /* 4KSTACKS */
28828 +
28829 +static void au_wkq_run(struct au_wkinfo *wkinfo)
28830 +{
28831 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
28832 +               if (au_wkq_test()) {
28833 +                       AuWarn1("wkq from wkq, due to a dead dir by UDBA?\n");
28834 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
28835 +               }
28836 +       } else
28837 +               au_dbg_verify_kthread();
28838 +
28839 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
28840 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
28841 +               queue_work(au_wkq, &wkinfo->wk);
28842 +       } else {
28843 +               INIT_WORK(&wkinfo->wk, wkq_func);
28844 +               schedule_work(&wkinfo->wk);
28845 +       }
28846 +}
28847 +
28848 +/*
28849 + * Be careful. It is easy to make deadlock happen.
28850 + * processA: lock, wkq and wait
28851 + * processB: wkq and wait, lock in wkq
28852 + * --> deadlock
28853 + */
28854 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
28855 +{
28856 +       int err;
28857 +       AuWkqCompDeclare(comp);
28858 +       struct au_wkinfo wkinfo = {
28859 +               .flags  = flags,
28860 +               .func   = func,
28861 +               .args   = args
28862 +       };
28863 +
28864 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
28865 +       if (!err) {
28866 +               au_wkq_run(&wkinfo);
28867 +               /* no timeout, no interrupt */
28868 +               wait_for_completion(wkinfo.comp);
28869 +               au_wkq_comp_free(comp);
28870 +               destroy_work_on_stack(&wkinfo.wk);
28871 +       }
28872 +
28873 +       return err;
28874 +
28875 +}
28876 +
28877 +/*
28878 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
28879 + * problem in a concurrent umounting.
28880 + */
28881 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
28882 +                 unsigned int flags)
28883 +{
28884 +       int err;
28885 +       struct au_wkinfo *wkinfo;
28886 +
28887 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
28888 +
28889 +       /*
28890 +        * wkq_func() must free this wkinfo.
28891 +        * it highly depends upon the implementation of workqueue.
28892 +        */
28893 +       err = 0;
28894 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
28895 +       if (wkinfo) {
28896 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
28897 +               wkinfo->flags = flags & ~AuWkq_WAIT;
28898 +               wkinfo->func = func;
28899 +               wkinfo->args = args;
28900 +               wkinfo->comp = NULL;
28901 +               kobject_get(wkinfo->kobj);
28902 +               __module_get(THIS_MODULE); /* todo: ?? */
28903 +
28904 +               au_wkq_run(wkinfo);
28905 +       } else {
28906 +               err = -ENOMEM;
28907 +               au_nwt_done(&au_sbi(sb)->si_nowait);
28908 +       }
28909 +
28910 +       return err;
28911 +}
28912 +
28913 +/* ---------------------------------------------------------------------- */
28914 +
28915 +void au_nwt_init(struct au_nowait_tasks *nwt)
28916 +{
28917 +       atomic_set(&nwt->nw_len, 0);
28918 +       /* smp_mb(); */ /* atomic_set */
28919 +       init_waitqueue_head(&nwt->nw_wq);
28920 +}
28921 +
28922 +void au_wkq_fin(void)
28923 +{
28924 +       destroy_workqueue(au_wkq);
28925 +}
28926 +
28927 +int __init au_wkq_init(void)
28928 +{
28929 +       int err;
28930 +
28931 +       err = 0;
28932 +       BUILD_BUG_ON(!WQ_RESCUER);
28933 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, !WQ_RESCUER, WQ_DFL_ACTIVE);
28934 +       if (IS_ERR(au_wkq))
28935 +               err = PTR_ERR(au_wkq);
28936 +       else if (!au_wkq)
28937 +               err = -ENOMEM;
28938 +
28939 +       return err;
28940 +}
28941 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
28942 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
28943 +++ linux/fs/aufs/wkq.h 2012-03-19 12:24:13.686467926 +0100
28944 @@ -0,0 +1,92 @@
28945 +/*
28946 + * Copyright (C) 2005-2012 Junjiro R. Okajima
28947 + *
28948 + * This program, aufs is free software; you can redistribute it and/or modify
28949 + * it under the terms of the GNU General Public License as published by
28950 + * the Free Software Foundation; either version 2 of the License, or
28951 + * (at your option) any later version.
28952 + *
28953 + * This program is distributed in the hope that it will be useful,
28954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28956 + * GNU General Public License for more details.
28957 + *
28958 + * You should have received a copy of the GNU General Public License
28959 + * along with this program; if not, write to the Free Software
28960 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28961 + */
28962 +
28963 +/*
28964 + * workqueue for asynchronous/super-io operations
28965 + * todo: try new credentials management scheme
28966 + */
28967 +
28968 +#ifndef __AUFS_WKQ_H__
28969 +#define __AUFS_WKQ_H__
28970 +
28971 +#ifdef __KERNEL__
28972 +
28973 +struct super_block;
28974 +
28975 +/* ---------------------------------------------------------------------- */
28976 +
28977 +/*
28978 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
28979 + */
28980 +struct au_nowait_tasks {
28981 +       atomic_t                nw_len;
28982 +       wait_queue_head_t       nw_wq;
28983 +};
28984 +
28985 +/* ---------------------------------------------------------------------- */
28986 +
28987 +typedef void (*au_wkq_func_t)(void *args);
28988 +
28989 +/* wkq flags */
28990 +#define AuWkq_WAIT     1
28991 +#define AuWkq_NEST     (1 << 1)
28992 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
28993 +#define au_fset_wkq(flags, name) \
28994 +       do { (flags) |= AuWkq_##name; } while (0)
28995 +#define au_fclr_wkq(flags, name) \
28996 +       do { (flags) &= ~AuWkq_##name; } while (0)
28997 +
28998 +#ifndef CONFIG_AUFS_HNOTIFY
28999 +#undef AuWkq_NEST
29000 +#define AuWkq_NEST     0
29001 +#endif
29002 +
29003 +/* wkq.c */
29004 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
29005 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
29006 +                 unsigned int flags);
29007 +void au_nwt_init(struct au_nowait_tasks *nwt);
29008 +int __init au_wkq_init(void);
29009 +void au_wkq_fin(void);
29010 +
29011 +/* ---------------------------------------------------------------------- */
29012 +
29013 +static inline int au_wkq_test(void)
29014 +{
29015 +       return current->flags & PF_WQ_WORKER;
29016 +}
29017 +
29018 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
29019 +{
29020 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
29021 +}
29022 +
29023 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
29024 +{
29025 +       if (atomic_dec_and_test(&nwt->nw_len))
29026 +               wake_up_all(&nwt->nw_wq);
29027 +}
29028 +
29029 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
29030 +{
29031 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
29032 +       return 0;
29033 +}
29034 +
29035 +#endif /* __KERNEL__ */
29036 +#endif /* __AUFS_WKQ_H__ */
29037 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
29038 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
29039 +++ linux/fs/aufs/xino.c        2012-03-19 12:24:13.686467926 +0100
29040 @@ -0,0 +1,1264 @@
29041 +/*
29042 + * Copyright (C) 2005-2012 Junjiro R. Okajima
29043 + *
29044 + * This program, aufs is free software; you can redistribute it and/or modify
29045 + * it under the terms of the GNU General Public License as published by
29046 + * the Free Software Foundation; either version 2 of the License, or
29047 + * (at your option) any later version.
29048 + *
29049 + * This program is distributed in the hope that it will be useful,
29050 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29051 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29052 + * GNU General Public License for more details.
29053 + *
29054 + * You should have received a copy of the GNU General Public License
29055 + * along with this program; if not, write to the Free Software
29056 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29057 + */
29058 +
29059 +/*
29060 + * external inode number translation table and bitmap
29061 + */
29062 +
29063 +#include <linux/seq_file.h>
29064 +#include "aufs.h"
29065 +
29066 +/* todo: unnecessary to support mmap_sem since kernel-space? */
29067 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
29068 +                  loff_t *pos)
29069 +{
29070 +       ssize_t err;
29071 +       mm_segment_t oldfs;
29072 +       union {
29073 +               void *k;
29074 +               char __user *u;
29075 +       } buf;
29076 +
29077 +       buf.k = kbuf;
29078 +       oldfs = get_fs();
29079 +       set_fs(KERNEL_DS);
29080 +       do {
29081 +               /* todo: signal_pending? */
29082 +               err = func(file, buf.u, size, pos);
29083 +       } while (err == -EAGAIN || err == -EINTR);
29084 +       set_fs(oldfs);
29085 +
29086 +#if 0 /* reserved for future use */
29087 +       if (err > 0)
29088 +               fsnotify_access(file->f_dentry);
29089 +#endif
29090 +
29091 +       return err;
29092 +}
29093 +
29094 +/* ---------------------------------------------------------------------- */
29095 +
29096 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
29097 +                             size_t size, loff_t *pos)
29098 +{
29099 +       ssize_t err;
29100 +       mm_segment_t oldfs;
29101 +       union {
29102 +               void *k;
29103 +               const char __user *u;
29104 +       } buf;
29105 +
29106 +       buf.k = kbuf;
29107 +       oldfs = get_fs();
29108 +       set_fs(KERNEL_DS);
29109 +       do {
29110 +               /* todo: signal_pending? */
29111 +               err = func(file, buf.u, size, pos);
29112 +       } while (err == -EAGAIN || err == -EINTR);
29113 +       set_fs(oldfs);
29114 +
29115 +#if 0 /* reserved for future use */
29116 +       if (err > 0)
29117 +               fsnotify_modify(file->f_dentry);
29118 +#endif
29119 +
29120 +       return err;
29121 +}
29122 +
29123 +struct do_xino_fwrite_args {
29124 +       ssize_t *errp;
29125 +       au_writef_t func;
29126 +       struct file *file;
29127 +       void *buf;
29128 +       size_t size;
29129 +       loff_t *pos;
29130 +};
29131 +
29132 +static void call_do_xino_fwrite(void *args)
29133 +{
29134 +       struct do_xino_fwrite_args *a = args;
29135 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
29136 +}
29137 +
29138 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
29139 +                   loff_t *pos)
29140 +{
29141 +       ssize_t err;
29142 +
29143 +       /* todo: signal block and no wkq? */
29144 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
29145 +               lockdep_off();
29146 +               err = do_xino_fwrite(func, file, buf, size, pos);
29147 +               lockdep_on();
29148 +       } else {
29149 +               /*
29150 +                * it breaks RLIMIT_FSIZE and normal user's limit,
29151 +                * users should care about quota and real 'filesystem full.'
29152 +                */
29153 +               int wkq_err;
29154 +               struct do_xino_fwrite_args args = {
29155 +                       .errp   = &err,
29156 +                       .func   = func,
29157 +                       .file   = file,
29158 +                       .buf    = buf,
29159 +                       .size   = size,
29160 +                       .pos    = pos
29161 +               };
29162 +
29163 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
29164 +               if (unlikely(wkq_err))
29165 +                       err = wkq_err;
29166 +       }
29167 +
29168 +       return err;
29169 +}
29170 +
29171 +/* ---------------------------------------------------------------------- */
29172 +
29173 +/*
29174 + * create a new xinofile at the same place/path as @base_file.
29175 + */
29176 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
29177 +{
29178 +       struct file *file;
29179 +       struct dentry *base, *parent;
29180 +       struct inode *dir;
29181 +       struct qstr *name;
29182 +       struct path path;
29183 +       int err;
29184 +
29185 +       base = base_file->f_dentry;
29186 +       parent = base->d_parent; /* dir inode is locked */
29187 +       dir = parent->d_inode;
29188 +       IMustLock(dir);
29189 +
29190 +       file = ERR_PTR(-EINVAL);
29191 +       name = &base->d_name;
29192 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
29193 +       if (IS_ERR(path.dentry)) {
29194 +               file = (void *)path.dentry;
29195 +               pr_err("%.*s lookup err %ld\n",
29196 +                      AuLNPair(name), PTR_ERR(path.dentry));
29197 +               goto out;
29198 +       }
29199 +
29200 +       /* no need to mnt_want_write() since we call dentry_open() later */
29201 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
29202 +       if (unlikely(err)) {
29203 +               file = ERR_PTR(err);
29204 +               pr_err("%.*s create err %d\n", AuLNPair(name), err);
29205 +               goto out_dput;
29206 +       }
29207 +
29208 +       path.mnt = base_file->f_vfsmnt;
29209 +       file = vfsub_dentry_open(&path,
29210 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
29211 +                                /* | __FMODE_NONOTIFY */);
29212 +       if (IS_ERR(file)) {
29213 +               pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
29214 +               goto out_dput;
29215 +       }
29216 +
29217 +       err = vfsub_unlink(dir, &file->f_path, /*force*/0);
29218 +       if (unlikely(err)) {
29219 +               pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
29220 +               goto out_fput;
29221 +       }
29222 +
29223 +       if (copy_src) {
29224 +               /* no one can touch copy_src xino */
29225 +               err = au_copy_file(file, copy_src,
29226 +                                  i_size_read(copy_src->f_dentry->d_inode));
29227 +               if (unlikely(err)) {
29228 +                       pr_err("%.*s copy err %d\n", AuLNPair(name), err);
29229 +                       goto out_fput;
29230 +               }
29231 +       }
29232 +       goto out_dput; /* success */
29233 +
29234 +out_fput:
29235 +       fput(file);
29236 +       file = ERR_PTR(err);
29237 +out_dput:
29238 +       dput(path.dentry);
29239 +out:
29240 +       return file;
29241 +}
29242 +
29243 +struct au_xino_lock_dir {
29244 +       struct au_hinode *hdir;
29245 +       struct dentry *parent;
29246 +       struct mutex *mtx;
29247 +};
29248 +
29249 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
29250 +                            struct au_xino_lock_dir *ldir)
29251 +{
29252 +       aufs_bindex_t brid, bindex;
29253 +
29254 +       ldir->hdir = NULL;
29255 +       bindex = -1;
29256 +       brid = au_xino_brid(sb);
29257 +       if (brid >= 0)
29258 +               bindex = au_br_index(sb, brid);
29259 +       if (bindex >= 0) {
29260 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
29261 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
29262 +       } else {
29263 +               ldir->parent = dget_parent(xino->f_dentry);
29264 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
29265 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
29266 +       }
29267 +}
29268 +
29269 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
29270 +{
29271 +       if (ldir->hdir)
29272 +               au_hn_imtx_unlock(ldir->hdir);
29273 +       else {
29274 +               mutex_unlock(ldir->mtx);
29275 +               dput(ldir->parent);
29276 +       }
29277 +}
29278 +
29279 +/* ---------------------------------------------------------------------- */
29280 +
29281 +/* trucate xino files asynchronously */
29282 +
29283 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
29284 +{
29285 +       int err;
29286 +       aufs_bindex_t bi, bend;
29287 +       struct au_branch *br;
29288 +       struct file *new_xino, *file;
29289 +       struct super_block *h_sb;
29290 +       struct au_xino_lock_dir ldir;
29291 +
29292 +       err = -EINVAL;
29293 +       bend = au_sbend(sb);
29294 +       if (unlikely(bindex < 0 || bend < bindex))
29295 +               goto out;
29296 +       br = au_sbr(sb, bindex);
29297 +       file = br->br_xino.xi_file;
29298 +       if (!file)
29299 +               goto out;
29300 +
29301 +       au_xino_lock_dir(sb, file, &ldir);
29302 +       /* mnt_want_write() is unnecessary here */
29303 +       new_xino = au_xino_create2(file, file);
29304 +       au_xino_unlock_dir(&ldir);
29305 +       err = PTR_ERR(new_xino);
29306 +       if (IS_ERR(new_xino))
29307 +               goto out;
29308 +       err = 0;
29309 +       fput(file);
29310 +       br->br_xino.xi_file = new_xino;
29311 +
29312 +       h_sb = br->br_mnt->mnt_sb;
29313 +       for (bi = 0; bi <= bend; bi++) {
29314 +               if (unlikely(bi == bindex))
29315 +                       continue;
29316 +               br = au_sbr(sb, bi);
29317 +               if (br->br_mnt->mnt_sb != h_sb)
29318 +                       continue;
29319 +
29320 +               fput(br->br_xino.xi_file);
29321 +               br->br_xino.xi_file = new_xino;
29322 +               get_file(new_xino);
29323 +       }
29324 +
29325 +out:
29326 +       return err;
29327 +}
29328 +
29329 +struct xino_do_trunc_args {
29330 +       struct super_block *sb;
29331 +       struct au_branch *br;
29332 +};
29333 +
29334 +static void xino_do_trunc(void *_args)
29335 +{
29336 +       struct xino_do_trunc_args *args = _args;
29337 +       struct super_block *sb;
29338 +       struct au_branch *br;
29339 +       struct inode *dir;
29340 +       int err;
29341 +       aufs_bindex_t bindex;
29342 +
29343 +       err = 0;
29344 +       sb = args->sb;
29345 +       dir = sb->s_root->d_inode;
29346 +       br = args->br;
29347 +
29348 +       si_noflush_write_lock(sb);
29349 +       ii_read_lock_parent(dir);
29350 +       bindex = au_br_index(sb, br->br_id);
29351 +       err = au_xino_trunc(sb, bindex);
29352 +       if (!err
29353 +           && br->br_xino.xi_file->f_dentry->d_inode->i_blocks
29354 +           >= br->br_xino_upper)
29355 +               br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
29356 +
29357 +       ii_read_unlock(dir);
29358 +       if (unlikely(err))
29359 +               pr_warning("err b%d, (%d)\n", bindex, err);
29360 +       atomic_dec(&br->br_xino_running);
29361 +       atomic_dec(&br->br_count);
29362 +       si_write_unlock(sb);
29363 +       au_nwt_done(&au_sbi(sb)->si_nowait);
29364 +       kfree(args);
29365 +}
29366 +
29367 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
29368 +{
29369 +       struct xino_do_trunc_args *args;
29370 +       int wkq_err;
29371 +
29372 +       if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
29373 +           < br->br_xino_upper)
29374 +               return;
29375 +
29376 +       if (atomic_inc_return(&br->br_xino_running) > 1)
29377 +               goto out;
29378 +
29379 +       /* lock and kfree() will be called in trunc_xino() */
29380 +       args = kmalloc(sizeof(*args), GFP_NOFS);
29381 +       if (unlikely(!args)) {
29382 +               AuErr1("no memory\n");
29383 +               goto out_args;
29384 +       }
29385 +
29386 +       atomic_inc(&br->br_count);
29387 +       args->sb = sb;
29388 +       args->br = br;
29389 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
29390 +       if (!wkq_err)
29391 +               return; /* success */
29392 +
29393 +       pr_err("wkq %d\n", wkq_err);
29394 +       atomic_dec(&br->br_count);
29395 +
29396 +out_args:
29397 +       kfree(args);
29398 +out:
29399 +       atomic_dec(&br->br_xino_running);
29400 +}
29401 +
29402 +/* ---------------------------------------------------------------------- */
29403 +
29404 +static int au_xino_do_write(au_writef_t write, struct file *file,
29405 +                           ino_t h_ino, ino_t ino)
29406 +{
29407 +       loff_t pos;
29408 +       ssize_t sz;
29409 +
29410 +       pos = h_ino;
29411 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
29412 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
29413 +               return -EFBIG;
29414 +       }
29415 +       pos *= sizeof(ino);
29416 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
29417 +       if (sz == sizeof(ino))
29418 +               return 0; /* success */
29419 +
29420 +       AuIOErr("write failed (%zd)\n", sz);
29421 +       return -EIO;
29422 +}
29423 +
29424 +/*
29425 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
29426 + * at the position of @h_ino.
29427 + * even if @ino is zero, it is written to the xinofile and means no entry.
29428 + * if the size of the xino file on a specific filesystem exceeds the watermark,
29429 + * try truncating it.
29430 + */
29431 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
29432 +                 ino_t ino)
29433 +{
29434 +       int err;
29435 +       unsigned int mnt_flags;
29436 +       struct au_branch *br;
29437 +
29438 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
29439 +                    || ((loff_t)-1) > 0);
29440 +       SiMustAnyLock(sb);
29441 +
29442 +       mnt_flags = au_mntflags(sb);
29443 +       if (!au_opt_test(mnt_flags, XINO))
29444 +               return 0;
29445 +
29446 +       br = au_sbr(sb, bindex);
29447 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
29448 +                              h_ino, ino);
29449 +       if (!err) {
29450 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
29451 +                   && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
29452 +                       xino_try_trunc(sb, br);
29453 +               return 0; /* success */
29454 +       }
29455 +
29456 +       AuIOErr("write failed (%d)\n", err);
29457 +       return -EIO;
29458 +}
29459 +
29460 +/* ---------------------------------------------------------------------- */
29461 +
29462 +/* aufs inode number bitmap */
29463 +
29464 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
29465 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
29466 +{
29467 +       ino_t ino;
29468 +
29469 +       AuDebugOn(bit < 0 || page_bits <= bit);
29470 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
29471 +       return ino;
29472 +}
29473 +
29474 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
29475 +{
29476 +       AuDebugOn(ino < AUFS_FIRST_INO);
29477 +       ino -= AUFS_FIRST_INO;
29478 +       *pindex = ino / page_bits;
29479 +       *bit = ino % page_bits;
29480 +}
29481 +
29482 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
29483 +{
29484 +       int err;
29485 +       loff_t pos;
29486 +       ssize_t sz;
29487 +       struct au_sbinfo *sbinfo;
29488 +       struct file *xib;
29489 +       unsigned long *p;
29490 +
29491 +       sbinfo = au_sbi(sb);
29492 +       MtxMustLock(&sbinfo->si_xib_mtx);
29493 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
29494 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
29495 +
29496 +       if (pindex == sbinfo->si_xib_last_pindex)
29497 +               return 0;
29498 +
29499 +       xib = sbinfo->si_xib;
29500 +       p = sbinfo->si_xib_buf;
29501 +       pos = sbinfo->si_xib_last_pindex;
29502 +       pos *= PAGE_SIZE;
29503 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
29504 +       if (unlikely(sz != PAGE_SIZE))
29505 +               goto out;
29506 +
29507 +       pos = pindex;
29508 +       pos *= PAGE_SIZE;
29509 +       if (i_size_read(xib->f_dentry->d_inode) >= pos + PAGE_SIZE)
29510 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
29511 +       else {
29512 +               memset(p, 0, PAGE_SIZE);
29513 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
29514 +       }
29515 +       if (sz == PAGE_SIZE) {
29516 +               sbinfo->si_xib_last_pindex = pindex;
29517 +               return 0; /* success */
29518 +       }
29519 +
29520 +out:
29521 +       AuIOErr1("write failed (%zd)\n", sz);
29522 +       err = sz;
29523 +       if (sz >= 0)
29524 +               err = -EIO;
29525 +       return err;
29526 +}
29527 +
29528 +/* ---------------------------------------------------------------------- */
29529 +
29530 +static void au_xib_clear_bit(struct inode *inode)
29531 +{
29532 +       int err, bit;
29533 +       unsigned long pindex;
29534 +       struct super_block *sb;
29535 +       struct au_sbinfo *sbinfo;
29536 +
29537 +       AuDebugOn(inode->i_nlink);
29538 +
29539 +       sb = inode->i_sb;
29540 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
29541 +       AuDebugOn(page_bits <= bit);
29542 +       sbinfo = au_sbi(sb);
29543 +       mutex_lock(&sbinfo->si_xib_mtx);
29544 +       err = xib_pindex(sb, pindex);
29545 +       if (!err) {
29546 +               clear_bit(bit, sbinfo->si_xib_buf);
29547 +               sbinfo->si_xib_next_bit = bit;
29548 +       }
29549 +       mutex_unlock(&sbinfo->si_xib_mtx);
29550 +}
29551 +
29552 +/* for s_op->delete_inode() */
29553 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
29554 +{
29555 +       int err;
29556 +       unsigned int mnt_flags;
29557 +       aufs_bindex_t bindex, bend, bi;
29558 +       unsigned char try_trunc;
29559 +       struct au_iinfo *iinfo;
29560 +       struct super_block *sb;
29561 +       struct au_hinode *hi;
29562 +       struct inode *h_inode;
29563 +       struct au_branch *br;
29564 +       au_writef_t xwrite;
29565 +
29566 +       sb = inode->i_sb;
29567 +       mnt_flags = au_mntflags(sb);
29568 +       if (!au_opt_test(mnt_flags, XINO)
29569 +           || inode->i_ino == AUFS_ROOT_INO)
29570 +               return;
29571 +
29572 +       if (unlinked) {
29573 +               au_xigen_inc(inode);
29574 +               au_xib_clear_bit(inode);
29575 +       }
29576 +
29577 +       iinfo = au_ii(inode);
29578 +       if (!iinfo)
29579 +               return;
29580 +
29581 +       bindex = iinfo->ii_bstart;
29582 +       if (bindex < 0)
29583 +               return;
29584 +
29585 +       xwrite = au_sbi(sb)->si_xwrite;
29586 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
29587 +       hi = iinfo->ii_hinode + bindex;
29588 +       bend = iinfo->ii_bend;
29589 +       for (; bindex <= bend; bindex++, hi++) {
29590 +               h_inode = hi->hi_inode;
29591 +               if (!h_inode
29592 +                   || (!unlinked && h_inode->i_nlink))
29593 +                       continue;
29594 +
29595 +               /* inode may not be revalidated */
29596 +               bi = au_br_index(sb, hi->hi_id);
29597 +               if (bi < 0)
29598 +                       continue;
29599 +
29600 +               br = au_sbr(sb, bi);
29601 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
29602 +                                      h_inode->i_ino, /*ino*/0);
29603 +               if (!err && try_trunc
29604 +                   && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
29605 +                       xino_try_trunc(sb, br);
29606 +       }
29607 +}
29608 +
29609 +/* get an unused inode number from bitmap */
29610 +ino_t au_xino_new_ino(struct super_block *sb)
29611 +{
29612 +       ino_t ino;
29613 +       unsigned long *p, pindex, ul, pend;
29614 +       struct au_sbinfo *sbinfo;
29615 +       struct file *file;
29616 +       int free_bit, err;
29617 +
29618 +       if (!au_opt_test(au_mntflags(sb), XINO))
29619 +               return iunique(sb, AUFS_FIRST_INO);
29620 +
29621 +       sbinfo = au_sbi(sb);
29622 +       mutex_lock(&sbinfo->si_xib_mtx);
29623 +       p = sbinfo->si_xib_buf;
29624 +       free_bit = sbinfo->si_xib_next_bit;
29625 +       if (free_bit < page_bits && !test_bit(free_bit, p))
29626 +               goto out; /* success */
29627 +       free_bit = find_first_zero_bit(p, page_bits);
29628 +       if (free_bit < page_bits)
29629 +               goto out; /* success */
29630 +
29631 +       pindex = sbinfo->si_xib_last_pindex;
29632 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
29633 +               err = xib_pindex(sb, ul);
29634 +               if (unlikely(err))
29635 +                       goto out_err;
29636 +               free_bit = find_first_zero_bit(p, page_bits);
29637 +               if (free_bit < page_bits)
29638 +                       goto out; /* success */
29639 +       }
29640 +
29641 +       file = sbinfo->si_xib;
29642 +       pend = i_size_read(file->f_dentry->d_inode) / PAGE_SIZE;
29643 +       for (ul = pindex + 1; ul <= pend; ul++) {
29644 +               err = xib_pindex(sb, ul);
29645 +               if (unlikely(err))
29646 +                       goto out_err;
29647 +               free_bit = find_first_zero_bit(p, page_bits);
29648 +               if (free_bit < page_bits)
29649 +                       goto out; /* success */
29650 +       }
29651 +       BUG();
29652 +
29653 +out:
29654 +       set_bit(free_bit, p);
29655 +       sbinfo->si_xib_next_bit = free_bit + 1;
29656 +       pindex = sbinfo->si_xib_last_pindex;
29657 +       mutex_unlock(&sbinfo->si_xib_mtx);
29658 +       ino = xib_calc_ino(pindex, free_bit);
29659 +       AuDbg("i%lu\n", (unsigned long)ino);
29660 +       return ino;
29661 +out_err:
29662 +       mutex_unlock(&sbinfo->si_xib_mtx);
29663 +       AuDbg("i0\n");
29664 +       return 0;
29665 +}
29666 +
29667 +/*
29668 + * read @ino from xinofile for the specified branch{@sb, @bindex}
29669 + * at the position of @h_ino.
29670 + * if @ino does not exist and @do_new is true, get new one.
29671 + */
29672 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
29673 +                ino_t *ino)
29674 +{
29675 +       int err;
29676 +       ssize_t sz;
29677 +       loff_t pos;
29678 +       struct file *file;
29679 +       struct au_sbinfo *sbinfo;
29680 +
29681 +       *ino = 0;
29682 +       if (!au_opt_test(au_mntflags(sb), XINO))
29683 +               return 0; /* no xino */
29684 +
29685 +       err = 0;
29686 +       sbinfo = au_sbi(sb);
29687 +       pos = h_ino;
29688 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
29689 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
29690 +               return -EFBIG;
29691 +       }
29692 +       pos *= sizeof(*ino);
29693 +
29694 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
29695 +       if (i_size_read(file->f_dentry->d_inode) < pos + sizeof(*ino))
29696 +               return 0; /* no ino */
29697 +
29698 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
29699 +       if (sz == sizeof(*ino))
29700 +               return 0; /* success */
29701 +
29702 +       err = sz;
29703 +       if (unlikely(sz >= 0)) {
29704 +               err = -EIO;
29705 +               AuIOErr("xino read error (%zd)\n", sz);
29706 +       }
29707 +
29708 +       return err;
29709 +}
29710 +
29711 +/* ---------------------------------------------------------------------- */
29712 +
29713 +/* create and set a new xino file */
29714 +
29715 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
29716 +{
29717 +       struct file *file;
29718 +       struct dentry *h_parent, *d;
29719 +       struct inode *h_dir;
29720 +       int err;
29721 +
29722 +       /*
29723 +        * at mount-time, and the xino file is the default path,
29724 +        * hnotify is disabled so we have no notify events to ignore.
29725 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
29726 +        */
29727 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
29728 +                              /* | __FMODE_NONOTIFY */,
29729 +                              S_IRUGO | S_IWUGO);
29730 +       if (IS_ERR(file)) {
29731 +               if (!silent)
29732 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
29733 +               return file;
29734 +       }
29735 +
29736 +       /* keep file count */
29737 +       h_parent = dget_parent(file->f_dentry);
29738 +       h_dir = h_parent->d_inode;
29739 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
29740 +       /* mnt_want_write() is unnecessary here */
29741 +       err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
29742 +       mutex_unlock(&h_dir->i_mutex);
29743 +       dput(h_parent);
29744 +       if (unlikely(err)) {
29745 +               if (!silent)
29746 +                       pr_err("unlink %s(%d)\n", fname, err);
29747 +               goto out;
29748 +       }
29749 +
29750 +       err = -EINVAL;
29751 +       d = file->f_dentry;
29752 +       if (unlikely(sb == d->d_sb)) {
29753 +               if (!silent)
29754 +                       pr_err("%s must be outside\n", fname);
29755 +               goto out;
29756 +       }
29757 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
29758 +               if (!silent)
29759 +                       pr_err("xino doesn't support %s(%s)\n",
29760 +                              fname, au_sbtype(d->d_sb));
29761 +               goto out;
29762 +       }
29763 +       return file; /* success */
29764 +
29765 +out:
29766 +       fput(file);
29767 +       file = ERR_PTR(err);
29768 +       return file;
29769 +}
29770 +
29771 +/*
29772 + * find another branch who is on the same filesystem of the specified
29773 + * branch{@btgt}. search until @bend.
29774 + */
29775 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
29776 +                       aufs_bindex_t bend)
29777 +{
29778 +       aufs_bindex_t bindex;
29779 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
29780 +
29781 +       for (bindex = 0; bindex < btgt; bindex++)
29782 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
29783 +                       return bindex;
29784 +       for (bindex++; bindex <= bend; bindex++)
29785 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
29786 +                       return bindex;
29787 +       return -1;
29788 +}
29789 +
29790 +/* ---------------------------------------------------------------------- */
29791 +
29792 +/*
29793 + * initialize the xinofile for the specified branch @br
29794 + * at the place/path where @base_file indicates.
29795 + * test whether another branch is on the same filesystem or not,
29796 + * if @do_test is true.
29797 + */
29798 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
29799 +              struct file *base_file, int do_test)
29800 +{
29801 +       int err;
29802 +       ino_t ino;
29803 +       aufs_bindex_t bend, bindex;
29804 +       struct au_branch *shared_br, *b;
29805 +       struct file *file;
29806 +       struct super_block *tgt_sb;
29807 +
29808 +       shared_br = NULL;
29809 +       bend = au_sbend(sb);
29810 +       if (do_test) {
29811 +               tgt_sb = br->br_mnt->mnt_sb;
29812 +               for (bindex = 0; bindex <= bend; bindex++) {
29813 +                       b = au_sbr(sb, bindex);
29814 +                       if (tgt_sb == b->br_mnt->mnt_sb) {
29815 +                               shared_br = b;
29816 +                               break;
29817 +                       }
29818 +               }
29819 +       }
29820 +
29821 +       if (!shared_br || !shared_br->br_xino.xi_file) {
29822 +               struct au_xino_lock_dir ldir;
29823 +
29824 +               au_xino_lock_dir(sb, base_file, &ldir);
29825 +               /* mnt_want_write() is unnecessary here */
29826 +               file = au_xino_create2(base_file, NULL);
29827 +               au_xino_unlock_dir(&ldir);
29828 +               err = PTR_ERR(file);
29829 +               if (IS_ERR(file))
29830 +                       goto out;
29831 +               br->br_xino.xi_file = file;
29832 +       } else {
29833 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
29834 +               get_file(br->br_xino.xi_file);
29835 +       }
29836 +
29837 +       ino = AUFS_ROOT_INO;
29838 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
29839 +                              h_ino, ino);
29840 +       if (unlikely(err)) {
29841 +               fput(br->br_xino.xi_file);
29842 +               br->br_xino.xi_file = NULL;
29843 +       }
29844 +
29845 +out:
29846 +       return err;
29847 +}
29848 +
29849 +/* ---------------------------------------------------------------------- */
29850 +
29851 +/* trucate a xino bitmap file */
29852 +
29853 +/* todo: slow */
29854 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
29855 +{
29856 +       int err, bit;
29857 +       ssize_t sz;
29858 +       unsigned long pindex;
29859 +       loff_t pos, pend;
29860 +       struct au_sbinfo *sbinfo;
29861 +       au_readf_t func;
29862 +       ino_t *ino;
29863 +       unsigned long *p;
29864 +
29865 +       err = 0;
29866 +       sbinfo = au_sbi(sb);
29867 +       MtxMustLock(&sbinfo->si_xib_mtx);
29868 +       p = sbinfo->si_xib_buf;
29869 +       func = sbinfo->si_xread;
29870 +       pend = i_size_read(file->f_dentry->d_inode);
29871 +       pos = 0;
29872 +       while (pos < pend) {
29873 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
29874 +               err = sz;
29875 +               if (unlikely(sz <= 0))
29876 +                       goto out;
29877 +
29878 +               err = 0;
29879 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
29880 +                       if (unlikely(*ino < AUFS_FIRST_INO))
29881 +                               continue;
29882 +
29883 +                       xib_calc_bit(*ino, &pindex, &bit);
29884 +                       AuDebugOn(page_bits <= bit);
29885 +                       err = xib_pindex(sb, pindex);
29886 +                       if (!err)
29887 +                               set_bit(bit, p);
29888 +                       else
29889 +                               goto out;
29890 +               }
29891 +       }
29892 +
29893 +out:
29894 +       return err;
29895 +}
29896 +
29897 +static int xib_restore(struct super_block *sb)
29898 +{
29899 +       int err;
29900 +       aufs_bindex_t bindex, bend;
29901 +       void *page;
29902 +
29903 +       err = -ENOMEM;
29904 +       page = (void *)__get_free_page(GFP_NOFS);
29905 +       if (unlikely(!page))
29906 +               goto out;
29907 +
29908 +       err = 0;
29909 +       bend = au_sbend(sb);
29910 +       for (bindex = 0; !err && bindex <= bend; bindex++)
29911 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
29912 +                       err = do_xib_restore
29913 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
29914 +               else
29915 +                       AuDbg("b%d\n", bindex);
29916 +       free_page((unsigned long)page);
29917 +
29918 +out:
29919 +       return err;
29920 +}
29921 +
29922 +int au_xib_trunc(struct super_block *sb)
29923 +{
29924 +       int err;
29925 +       ssize_t sz;
29926 +       loff_t pos;
29927 +       struct au_xino_lock_dir ldir;
29928 +       struct au_sbinfo *sbinfo;
29929 +       unsigned long *p;
29930 +       struct file *file;
29931 +
29932 +       SiMustWriteLock(sb);
29933 +
29934 +       err = 0;
29935 +       sbinfo = au_sbi(sb);
29936 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
29937 +               goto out;
29938 +
29939 +       file = sbinfo->si_xib;
29940 +       if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
29941 +               goto out;
29942 +
29943 +       au_xino_lock_dir(sb, file, &ldir);
29944 +       /* mnt_want_write() is unnecessary here */
29945 +       file = au_xino_create2(sbinfo->si_xib, NULL);
29946 +       au_xino_unlock_dir(&ldir);
29947 +       err = PTR_ERR(file);
29948 +       if (IS_ERR(file))
29949 +               goto out;
29950 +       fput(sbinfo->si_xib);
29951 +       sbinfo->si_xib = file;
29952 +
29953 +       p = sbinfo->si_xib_buf;
29954 +       memset(p, 0, PAGE_SIZE);
29955 +       pos = 0;
29956 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
29957 +       if (unlikely(sz != PAGE_SIZE)) {
29958 +               err = sz;
29959 +               AuIOErr("err %d\n", err);
29960 +               if (sz >= 0)
29961 +                       err = -EIO;
29962 +               goto out;
29963 +       }
29964 +
29965 +       mutex_lock(&sbinfo->si_xib_mtx);
29966 +       /* mnt_want_write() is unnecessary here */
29967 +       err = xib_restore(sb);
29968 +       mutex_unlock(&sbinfo->si_xib_mtx);
29969 +
29970 +out:
29971 +       return err;
29972 +}
29973 +
29974 +/* ---------------------------------------------------------------------- */
29975 +
29976 +/*
29977 + * xino mount option handlers
29978 + */
29979 +static au_readf_t find_readf(struct file *h_file)
29980 +{
29981 +       const struct file_operations *fop = h_file->f_op;
29982 +
29983 +       if (fop) {
29984 +               if (fop->read)
29985 +                       return fop->read;
29986 +               if (fop->aio_read)
29987 +                       return do_sync_read;
29988 +       }
29989 +       return ERR_PTR(-ENOSYS);
29990 +}
29991 +
29992 +static au_writef_t find_writef(struct file *h_file)
29993 +{
29994 +       const struct file_operations *fop = h_file->f_op;
29995 +
29996 +       if (fop) {
29997 +               if (fop->write)
29998 +                       return fop->write;
29999 +               if (fop->aio_write)
30000 +                       return do_sync_write;
30001 +       }
30002 +       return ERR_PTR(-ENOSYS);
30003 +}
30004 +
30005 +/* xino bitmap */
30006 +static void xino_clear_xib(struct super_block *sb)
30007 +{
30008 +       struct au_sbinfo *sbinfo;
30009 +
30010 +       SiMustWriteLock(sb);
30011 +
30012 +       sbinfo = au_sbi(sb);
30013 +       sbinfo->si_xread = NULL;
30014 +       sbinfo->si_xwrite = NULL;
30015 +       if (sbinfo->si_xib)
30016 +               fput(sbinfo->si_xib);
30017 +       sbinfo->si_xib = NULL;
30018 +       free_page((unsigned long)sbinfo->si_xib_buf);
30019 +       sbinfo->si_xib_buf = NULL;
30020 +}
30021 +
30022 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
30023 +{
30024 +       int err;
30025 +       loff_t pos;
30026 +       struct au_sbinfo *sbinfo;
30027 +       struct file *file;
30028 +
30029 +       SiMustWriteLock(sb);
30030 +
30031 +       sbinfo = au_sbi(sb);
30032 +       file = au_xino_create2(base, sbinfo->si_xib);
30033 +       err = PTR_ERR(file);
30034 +       if (IS_ERR(file))
30035 +               goto out;
30036 +       if (sbinfo->si_xib)
30037 +               fput(sbinfo->si_xib);
30038 +       sbinfo->si_xib = file;
30039 +       sbinfo->si_xread = find_readf(file);
30040 +       sbinfo->si_xwrite = find_writef(file);
30041 +
30042 +       err = -ENOMEM;
30043 +       if (!sbinfo->si_xib_buf)
30044 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
30045 +       if (unlikely(!sbinfo->si_xib_buf))
30046 +               goto out_unset;
30047 +
30048 +       sbinfo->si_xib_last_pindex = 0;
30049 +       sbinfo->si_xib_next_bit = 0;
30050 +       if (i_size_read(file->f_dentry->d_inode) < PAGE_SIZE) {
30051 +               pos = 0;
30052 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
30053 +                                 PAGE_SIZE, &pos);
30054 +               if (unlikely(err != PAGE_SIZE))
30055 +                       goto out_free;
30056 +       }
30057 +       err = 0;
30058 +       goto out; /* success */
30059 +
30060 +out_free:
30061 +       free_page((unsigned long)sbinfo->si_xib_buf);
30062 +       sbinfo->si_xib_buf = NULL;
30063 +       if (err >= 0)
30064 +               err = -EIO;
30065 +out_unset:
30066 +       fput(sbinfo->si_xib);
30067 +       sbinfo->si_xib = NULL;
30068 +       sbinfo->si_xread = NULL;
30069 +       sbinfo->si_xwrite = NULL;
30070 +out:
30071 +       return err;
30072 +}
30073 +
30074 +/* xino for each branch */
30075 +static void xino_clear_br(struct super_block *sb)
30076 +{
30077 +       aufs_bindex_t bindex, bend;
30078 +       struct au_branch *br;
30079 +
30080 +       bend = au_sbend(sb);
30081 +       for (bindex = 0; bindex <= bend; bindex++) {
30082 +               br = au_sbr(sb, bindex);
30083 +               if (!br || !br->br_xino.xi_file)
30084 +                       continue;
30085 +
30086 +               fput(br->br_xino.xi_file);
30087 +               br->br_xino.xi_file = NULL;
30088 +       }
30089 +}
30090 +
30091 +static int au_xino_set_br(struct super_block *sb, struct file *base)
30092 +{
30093 +       int err;
30094 +       ino_t ino;
30095 +       aufs_bindex_t bindex, bend, bshared;
30096 +       struct {
30097 +               struct file *old, *new;
30098 +       } *fpair, *p;
30099 +       struct au_branch *br;
30100 +       struct inode *inode;
30101 +       au_writef_t writef;
30102 +
30103 +       SiMustWriteLock(sb);
30104 +
30105 +       err = -ENOMEM;
30106 +       bend = au_sbend(sb);
30107 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
30108 +       if (unlikely(!fpair))
30109 +               goto out;
30110 +
30111 +       inode = sb->s_root->d_inode;
30112 +       ino = AUFS_ROOT_INO;
30113 +       writef = au_sbi(sb)->si_xwrite;
30114 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
30115 +               br = au_sbr(sb, bindex);
30116 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
30117 +               if (bshared >= 0) {
30118 +                       /* shared xino */
30119 +                       *p = fpair[bshared];
30120 +                       get_file(p->new);
30121 +               }
30122 +
30123 +               if (!p->new) {
30124 +                       /* new xino */
30125 +                       p->old = br->br_xino.xi_file;
30126 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
30127 +                       err = PTR_ERR(p->new);
30128 +                       if (IS_ERR(p->new)) {
30129 +                               p->new = NULL;
30130 +                               goto out_pair;
30131 +                       }
30132 +               }
30133 +
30134 +               err = au_xino_do_write(writef, p->new,
30135 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
30136 +               if (unlikely(err))
30137 +                       goto out_pair;
30138 +       }
30139 +
30140 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
30141 +               br = au_sbr(sb, bindex);
30142 +               if (br->br_xino.xi_file)
30143 +                       fput(br->br_xino.xi_file);
30144 +               get_file(p->new);
30145 +               br->br_xino.xi_file = p->new;
30146 +       }
30147 +
30148 +out_pair:
30149 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
30150 +               if (p->new)
30151 +                       fput(p->new);
30152 +               else
30153 +                       break;
30154 +       kfree(fpair);
30155 +out:
30156 +       return err;
30157 +}
30158 +
30159 +void au_xino_clr(struct super_block *sb)
30160 +{
30161 +       struct au_sbinfo *sbinfo;
30162 +
30163 +       au_xigen_clr(sb);
30164 +       xino_clear_xib(sb);
30165 +       xino_clear_br(sb);
30166 +       sbinfo = au_sbi(sb);
30167 +       /* lvalue, do not call au_mntflags() */
30168 +       au_opt_clr(sbinfo->si_mntflags, XINO);
30169 +}
30170 +
30171 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
30172 +{
30173 +       int err, skip;
30174 +       struct dentry *parent, *cur_parent;
30175 +       struct qstr *dname, *cur_name;
30176 +       struct file *cur_xino;
30177 +       struct inode *dir;
30178 +       struct au_sbinfo *sbinfo;
30179 +
30180 +       SiMustWriteLock(sb);
30181 +
30182 +       err = 0;
30183 +       sbinfo = au_sbi(sb);
30184 +       parent = dget_parent(xino->file->f_dentry);
30185 +       if (remount) {
30186 +               skip = 0;
30187 +               dname = &xino->file->f_dentry->d_name;
30188 +               cur_xino = sbinfo->si_xib;
30189 +               if (cur_xino) {
30190 +                       cur_parent = dget_parent(cur_xino->f_dentry);
30191 +                       cur_name = &cur_xino->f_dentry->d_name;
30192 +                       skip = (cur_parent == parent
30193 +                               && dname->len == cur_name->len
30194 +                               && !memcmp(dname->name, cur_name->name,
30195 +                                          dname->len));
30196 +                       dput(cur_parent);
30197 +               }
30198 +               if (skip)
30199 +                       goto out;
30200 +       }
30201 +
30202 +       au_opt_set(sbinfo->si_mntflags, XINO);
30203 +       dir = parent->d_inode;
30204 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
30205 +       /* mnt_want_write() is unnecessary here */
30206 +       err = au_xino_set_xib(sb, xino->file);
30207 +       if (!err)
30208 +               err = au_xigen_set(sb, xino->file);
30209 +       if (!err)
30210 +               err = au_xino_set_br(sb, xino->file);
30211 +       mutex_unlock(&dir->i_mutex);
30212 +       if (!err)
30213 +               goto out; /* success */
30214 +
30215 +       /* reset all */
30216 +       AuIOErr("failed creating xino(%d).\n", err);
30217 +
30218 +out:
30219 +       dput(parent);
30220 +       return err;
30221 +}
30222 +
30223 +/* ---------------------------------------------------------------------- */
30224 +
30225 +/*
30226 + * create a xinofile at the default place/path.
30227 + */
30228 +struct file *au_xino_def(struct super_block *sb)
30229 +{
30230 +       struct file *file;
30231 +       char *page, *p;
30232 +       struct au_branch *br;
30233 +       struct super_block *h_sb;
30234 +       struct path path;
30235 +       aufs_bindex_t bend, bindex, bwr;
30236 +
30237 +       br = NULL;
30238 +       bend = au_sbend(sb);
30239 +       bwr = -1;
30240 +       for (bindex = 0; bindex <= bend; bindex++) {
30241 +               br = au_sbr(sb, bindex);
30242 +               if (au_br_writable(br->br_perm)
30243 +                   && !au_test_fs_bad_xino(br->br_mnt->mnt_sb)) {
30244 +                       bwr = bindex;
30245 +                       break;
30246 +               }
30247 +       }
30248 +
30249 +       if (bwr >= 0) {
30250 +               file = ERR_PTR(-ENOMEM);
30251 +               page = __getname_gfp(GFP_NOFS);
30252 +               if (unlikely(!page))
30253 +                       goto out;
30254 +               path.mnt = br->br_mnt;
30255 +               path.dentry = au_h_dptr(sb->s_root, bwr);
30256 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
30257 +               file = (void *)p;
30258 +               if (!IS_ERR(p)) {
30259 +                       strcat(p, "/" AUFS_XINO_FNAME);
30260 +                       AuDbg("%s\n", p);
30261 +                       file = au_xino_create(sb, p, /*silent*/0);
30262 +                       if (!IS_ERR(file))
30263 +                               au_xino_brid_set(sb, br->br_id);
30264 +               }
30265 +               __putname(page);
30266 +       } else {
30267 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
30268 +               if (IS_ERR(file))
30269 +                       goto out;
30270 +               h_sb = file->f_dentry->d_sb;
30271 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
30272 +                       pr_err("xino doesn't support %s(%s)\n",
30273 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
30274 +                       fput(file);
30275 +                       file = ERR_PTR(-EINVAL);
30276 +               }
30277 +               if (!IS_ERR(file))
30278 +                       au_xino_brid_set(sb, -1);
30279 +       }
30280 +
30281 +out:
30282 +       return file;
30283 +}
30284 +
30285 +/* ---------------------------------------------------------------------- */
30286 +
30287 +int au_xino_path(struct seq_file *seq, struct file *file)
30288 +{
30289 +       int err;
30290 +
30291 +       err = au_seq_path(seq, &file->f_path);
30292 +       if (unlikely(err < 0))
30293 +               goto out;
30294 +
30295 +       err = 0;
30296 +#define Deleted "\\040(deleted)"
30297 +       seq->count -= sizeof(Deleted) - 1;
30298 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
30299 +                        sizeof(Deleted) - 1));
30300 +#undef Deleted
30301 +
30302 +out:
30303 +       return err;
30304 +}
30305 diff -urN /usr/share/empty/include/linux/aufs_type.h linux/include/linux/aufs_type.h
30306 --- /usr/share/empty/include/linux/aufs_type.h  1970-01-01 01:00:00.000000000 +0100
30307 +++ linux/include/linux/aufs_type.h     2012-03-19 12:24:13.686467926 +0100
30308 @@ -0,0 +1,233 @@
30309 +/*
30310 + * Copyright (C) 2005-2012 Junjiro R. Okajima
30311 + *
30312 + * This program, aufs is free software; you can redistribute it and/or modify
30313 + * it under the terms of the GNU General Public License as published by
30314 + * the Free Software Foundation; either version 2 of the License, or
30315 + * (at your option) any later version.
30316 + *
30317 + * This program is distributed in the hope that it will be useful,
30318 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30319 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30320 + * GNU General Public License for more details.
30321 + *
30322 + * You should have received a copy of the GNU General Public License
30323 + * along with this program; if not, write to the Free Software
30324 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30325 + */
30326 +
30327 +#ifndef __AUFS_TYPE_H__
30328 +#define __AUFS_TYPE_H__
30329 +
30330 +#define AUFS_NAME      "aufs"
30331 +
30332 +#ifdef __KERNEL__
30333 +/*
30334 + * define it before including all other headers.
30335 + * sched.h may use pr_* macros before defining "current", so define the
30336 + * no-current version first, and re-define later.
30337 + */
30338 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
30339 +#include <linux/sched.h>
30340 +#undef pr_fmt
30341 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d:%s[%d]: " fmt, \
30342 +               __func__, __LINE__, current->comm, current->pid
30343 +#else
30344 +#include <stdint.h>
30345 +#include <sys/types.h>
30346 +#endif /* __KERNEL__ */
30347 +
30348 +#include <linux/limits.h>
30349 +
30350 +#define AUFS_VERSION   "3.x-rcN-20120312"
30351 +
30352 +/* todo? move this to linux-2.6.19/include/magic.h */
30353 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
30354 +
30355 +/* ---------------------------------------------------------------------- */
30356 +
30357 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
30358 +typedef int8_t aufs_bindex_t;
30359 +#define AUFS_BRANCH_MAX 127
30360 +#else
30361 +typedef int16_t aufs_bindex_t;
30362 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
30363 +#define AUFS_BRANCH_MAX 511
30364 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
30365 +#define AUFS_BRANCH_MAX 1023
30366 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
30367 +#define AUFS_BRANCH_MAX 32767
30368 +#endif
30369 +#endif
30370 +
30371 +#ifdef __KERNEL__
30372 +#ifndef AUFS_BRANCH_MAX
30373 +#error unknown CONFIG_AUFS_BRANCH_MAX value
30374 +#endif
30375 +#endif /* __KERNEL__ */
30376 +
30377 +/* ---------------------------------------------------------------------- */
30378 +
30379 +#define AUFS_FSTYPE            AUFS_NAME
30380 +
30381 +#define AUFS_ROOT_INO          2
30382 +#define AUFS_FIRST_INO         11
30383 +
30384 +#define AUFS_WH_PFX            ".wh."
30385 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
30386 +#define AUFS_WH_TMP_LEN                4
30387 +/* a limit for rmdir/rename a dir */
30388 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
30389 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
30390 +                               - 1                     /* dot */\
30391 +                               - AUFS_WH_TMP_LEN)      /* hex */
30392 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
30393 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
30394 +#define AUFS_XINO_TRUNC_INIT   64 /* blocks */
30395 +#define AUFS_XINO_TRUNC_STEP   4  /* blocks */
30396 +#define AUFS_DIRWH_DEF         3
30397 +#define AUFS_RDCACHE_DEF       10 /* seconds */
30398 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
30399 +#define AUFS_RDBLK_DEF         512 /* bytes */
30400 +#define AUFS_RDHASH_DEF                32
30401 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
30402 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
30403 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
30404 +#define AUFS_PLINK_WARN                100 /* number of plinks */
30405 +
30406 +/* pseudo-link maintenace under /proc */
30407 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
30408 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
30409 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
30410 +
30411 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
30412 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
30413 +
30414 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
30415 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
30416 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
30417 +
30418 +/* doubly whiteouted */
30419 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
30420 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
30421 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
30422 +
30423 +/* branch permissions and attributes */
30424 +#define AUFS_BRPERM_RW         "rw"
30425 +#define AUFS_BRPERM_RO         "ro"
30426 +#define AUFS_BRPERM_RR         "rr"
30427 +#define AUFS_BRRATTR_WH                "wh"
30428 +#define AUFS_BRWATTR_NLWH      "nolwh"
30429 +
30430 +/* ---------------------------------------------------------------------- */
30431 +
30432 +/* ioctl */
30433 +enum {
30434 +       /* readdir in userspace */
30435 +       AuCtl_RDU,
30436 +       AuCtl_RDU_INO,
30437 +
30438 +       /* pathconf wrapper */
30439 +       AuCtl_WBR_FD,
30440 +
30441 +       /* busy inode */
30442 +       AuCtl_IBUSY
30443 +};
30444 +
30445 +/* borrowed from linux/include/linux/kernel.h */
30446 +#ifndef ALIGN
30447 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
30448 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
30449 +#endif
30450 +
30451 +/* borrowed from linux/include/linux/compiler-gcc3.h */
30452 +#ifndef __aligned
30453 +#define __aligned(x)                   __attribute__((aligned(x)))
30454 +#endif
30455 +
30456 +#ifdef __KERNEL__
30457 +#ifndef __packed
30458 +#define __packed                       __attribute__((packed))
30459 +#endif
30460 +#endif
30461 +
30462 +struct au_rdu_cookie {
30463 +       uint64_t        h_pos;
30464 +       int16_t         bindex;
30465 +       uint8_t         flags;
30466 +       uint8_t         pad;
30467 +       uint32_t        generation;
30468 +} __aligned(8);
30469 +
30470 +struct au_rdu_ent {
30471 +       uint64_t        ino;
30472 +       int16_t         bindex;
30473 +       uint8_t         type;
30474 +       uint8_t         nlen;
30475 +       uint8_t         wh;
30476 +       char            name[0];
30477 +} __aligned(8);
30478 +
30479 +static inline int au_rdu_len(int nlen)
30480 +{
30481 +       /* include the terminating NULL */
30482 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
30483 +                    sizeof(uint64_t));
30484 +}
30485 +
30486 +union au_rdu_ent_ul {
30487 +       struct au_rdu_ent __user        *e;
30488 +       uint64_t                        ul;
30489 +};
30490 +
30491 +enum {
30492 +       AufsCtlRduV_SZ,
30493 +       AufsCtlRduV_End
30494 +};
30495 +
30496 +struct aufs_rdu {
30497 +       /* input */
30498 +       union {
30499 +               uint64_t        sz;     /* AuCtl_RDU */
30500 +               uint64_t        nent;   /* AuCtl_RDU_INO */
30501 +       };
30502 +       union au_rdu_ent_ul     ent;
30503 +       uint16_t                verify[AufsCtlRduV_End];
30504 +
30505 +       /* input/output */
30506 +       uint32_t                blk;
30507 +
30508 +       /* output */
30509 +       union au_rdu_ent_ul     tail;
30510 +       /* number of entries which were added in a single call */
30511 +       uint64_t                rent;
30512 +       uint8_t                 full;
30513 +       uint8_t                 shwh;
30514 +
30515 +       struct au_rdu_cookie    cookie;
30516 +} __aligned(8);
30517 +
30518 +/* ---------------------------------------------------------------------- */
30519 +
30520 +struct aufs_wbr_fd {
30521 +       uint32_t        oflags;
30522 +       int16_t         brid;
30523 +} __aligned(8);
30524 +
30525 +/* ---------------------------------------------------------------------- */
30526 +
30527 +struct aufs_ibusy {
30528 +       uint64_t        ino, h_ino;
30529 +       int16_t         bindex;
30530 +} __aligned(8);
30531 +
30532 +/* ---------------------------------------------------------------------- */
30533 +
30534 +#define AuCtlType              'A'
30535 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
30536 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
30537 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
30538 +                                    struct aufs_wbr_fd)
30539 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
30540 +
30541 +#endif /* __AUFS_TYPE_H__ */
30542
This page took 2.504663 seconds and 4 git commands to generate.